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