Added \n to the isspace rule.
[yaz-moved-to-github.git] / util / options.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: options.c,v $
7  * Revision 1.5  1995-12-06 13:00:19  adam
8  * Minus alone not treated as an option.
9  *
10  * Revision 1.4  1995/09/29  17:12:35  quinn
11  * Smallish
12  *
13  * Revision 1.3  1995/09/27  15:03:03  quinn
14  * Modified function heads & prototypes.
15  *
16  * Revision 1.2  1995/05/16  08:51:13  quinn
17  * License, documentation, and memory fixes
18  *
19  * Revision 1.1  1995/03/27  08:35:18  quinn
20  * Created util library
21  * Added memory debugging module. Imported options-manager
22  *
23  * Revision 1.2  1994/10/04  17:47:10  adam
24  * Function options now returns arg with error option.
25  *
26  * Revision 1.1  1994/08/16  15:57:22  adam
27  * The first utility modules.
28  *
29  */
30 #include <stdlib.h>
31
32 #include <options.h>
33
34 static int arg_no = 1;
35 static int arg_off = 0;
36
37 int options (const char *desc, char **argv, int argc, char **arg)
38 {
39     int ch, i = 0;
40     
41     if (arg_no >= argc)
42         return -2;
43     if (arg_off == 0)
44     {
45         if (argv[arg_no][0] != '-' || argv[arg_no][1] == '\0')
46         {
47             *arg = argv[arg_no++];
48             return 0;
49         }
50         arg_off++;
51     }
52     ch = argv[arg_no][arg_off++];
53     while (desc[i])
54     {
55         int desc_char = desc[i++];
56         int type = 0;
57         if (desc[i] == ':')
58         {       /* string argument */
59             type = desc[i++];
60         }
61         if (desc_char == ch)
62         { /* option with argument */
63             if (type)
64             {
65                 if (argv[arg_no][arg_off])
66                 {
67                     *arg = argv[arg_no]+arg_off;
68                     arg_no++;
69                     arg_off =  0;
70                 }
71                 else
72                 {
73                     arg_no++;
74                     arg_off = 0;
75                     if (arg_no < argc)
76                         *arg = argv[arg_no++];
77                     else
78                         *arg = "";
79                 }
80             }
81             else /* option with no argument */
82             {
83                 if (argv[arg_no][arg_off])
84                     arg_off++;
85                 else
86                 {
87                     arg_off = 0;
88                     arg_no++;
89                 }
90             }
91             return ch;
92         }               
93     }
94     *arg = argv[arg_no]+arg_off-1;
95     arg_no = arg_no + 1;
96     arg_off = 0;
97     return -1;
98 }