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