License, documentation, and memory fixes
[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.2  1995-05-16 08:51:13  quinn
8  * License, documentation, and memory fixes
9  *
10  * Revision 1.1  1995/03/27  08:35:18  quinn
11  * Created util library
12  * Added memory debugging module. Imported options-manager
13  *
14  * Revision 1.2  1994/10/04  17:47:10  adam
15  * Function options now returns arg with error option.
16  *
17  * Revision 1.1  1994/08/16  15:57:22  adam
18  * The first utility modules.
19  *
20  */
21 #include <stdlib.h>
22
23 #include <options.h>
24
25 static int arg_no = 1;
26 static int arg_off = 0;
27
28 int options (const char *desc, char **argv, int argc, char **arg)
29 {
30     int ch, i = 0;
31     
32     if (arg_no >= argc)
33         return -2;
34     if (arg_off == 0)
35     {
36         if (argv[arg_no][0] != '-')
37         {
38             *arg = argv[arg_no++];
39             return 0;
40         }
41         arg_off++;
42     }
43     ch = argv[arg_no][arg_off++];
44     while (desc[i])
45     {
46         int desc_char = desc[i++];
47         int type = 0;
48         if (desc[i] == ':')
49         {       /* string argument */
50             type = desc[i++];
51         }
52         if (desc_char == ch)
53         { /* option with argument */
54             if (type)
55             {
56                 if (argv[arg_no][arg_off])
57                 {
58                     *arg = argv[arg_no]+arg_off;
59                     arg_no++;
60                     arg_off =  0;
61                 }
62                 else
63                 {
64                     arg_no++;
65                     arg_off = 0;
66                     if (arg_no < argc)
67                         *arg = argv[arg_no++];
68                     else
69                         *arg = "";
70                 }
71             }
72             else /* option with no argument */
73             {
74                 if (argv[arg_no][arg_off])
75                     arg_off++;
76                 else
77                 {
78                     arg_off = 0;
79                     arg_no++;
80                 }
81             }
82             return ch;
83         }               
84     }
85     *arg = argv[arg_no]+arg_off-1;
86     arg_no = arg_no + 1;
87     arg_off = 0;
88     return -1;
89 }