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