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