Source restructure. yaz-marcdump part of installation
[yaz-moved-to-github.git] / src / options.c
1 /*
2  * Copyright (c) 1995-2000, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: options.c,v $
7  * Revision 1.1  2003-10-27 12:21:34  adam
8  * Source restructure. yaz-marcdump part of installation
9  *
10  * Revision 1.8  2000/02/29 13:44:55  adam
11  * Check for config.h (currently not generated).
12  *
13  * Revision 1.7  1999/11/30 13:47:12  adam
14  * Improved installation. Moved header files to include/yaz.
15  *
16  * Revision 1.6  1997/09/01 08:54:13  adam
17  * New windows NT/95 port using MSV5.0. Made prefix query handling
18  * thread safe. The function options ignores empty arguments when met.
19  *
20  * Revision 1.5  1995/12/06 13:00:19  adam
21  * Minus alone not treated as an option.
22  *
23  * Revision 1.4  1995/09/29  17:12:35  quinn
24  * Smallish
25  *
26  * Revision 1.3  1995/09/27  15:03:03  quinn
27  * Modified function heads & prototypes.
28  *
29  * Revision 1.2  1995/05/16  08:51:13  quinn
30  * License, documentation, and memory fixes
31  *
32  * Revision 1.1  1995/03/27  08:35:18  quinn
33  * Created util library
34  * Added memory debugging module. Imported options-manager
35  *
36  * Revision 1.2  1994/10/04  17:47:10  adam
37  * Function options now returns arg with error option.
38  *
39  * Revision 1.1  1994/08/16  15:57:22  adam
40  * The first utility modules.
41  *
42  */
43 #if HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46
47 #include <stdlib.h>
48
49 #include <yaz/options.h>
50
51 static int arg_no = 1;
52 static int arg_off = 0;
53
54 int options (const char *desc, char **argv, int argc, char **arg)
55 {
56     int ch, i = 0;
57     
58     if (arg_no >= argc)
59         return -2;
60     if (arg_off == 0)
61     {
62         while (argv[arg_no][0] == '\0')
63         {
64             arg_no++;
65             if (arg_no >= argc)
66                 return -2;
67         }
68         if (argv[arg_no][0] != '-' || argv[arg_no][1] == '\0')
69         {
70             *arg = argv[arg_no++];
71             return 0;
72         }
73         arg_off++;
74     }
75     ch = argv[arg_no][arg_off++];
76     while (desc[i])
77     {
78         int desc_char = desc[i++];
79         int type = 0;
80         if (desc[i] == ':')
81         {       /* string argument */
82             type = desc[i++];
83         }
84         if (desc_char == ch)
85         { /* option with argument */
86             if (type)
87             {
88                 if (argv[arg_no][arg_off])
89                 {
90                     *arg = argv[arg_no]+arg_off;
91                     arg_no++;
92                     arg_off =  0;
93                 }
94                 else
95                 {
96                     arg_no++;
97                     arg_off = 0;
98                     if (arg_no < argc)
99                         *arg = argv[arg_no++];
100                     else
101                         *arg = "";
102                 }
103             }
104             else /* option with no argument */
105             {
106                 if (argv[arg_no][arg_off])
107                     arg_off++;
108                 else
109                 {
110                     arg_off = 0;
111                     arg_no++;
112                 }
113             }
114             return ch;
115         }               
116     }
117     *arg = argv[arg_no]+arg_off-1;
118     arg_no = arg_no + 1;
119     arg_off = 0;
120     return -1;
121 }