Added state-handle and some support for asynchronous activities.
[yaz-moved-to-github.git] / util / options.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: options.c,v $
7  * Revision 1.1  1995-03-27 08:35:18  quinn
8  * Created util library
9  * Added memory debugging module. Imported options-manager
10  *
11  * Revision 1.2  1994/10/04  17:47:10  adam
12  * Function options now returns arg with error option.
13  *
14  * Revision 1.1  1994/08/16  15:57:22  adam
15  * The first utility modules.
16  *
17  */
18 #include <stdlib.h>
19
20 #include <options.h>
21
22 static int arg_no = 1;
23 static int arg_off = 0;
24
25 int options (const char *desc, char **argv, int argc, char **arg)
26 {
27     int ch, i = 0;
28     
29     if (arg_no >= argc)
30         return -2;
31     if (arg_off == 0)
32     {
33         if (argv[arg_no][0] != '-')
34         {
35             *arg = argv[arg_no++];
36             return 0;
37         }
38         arg_off++;
39     }
40     ch = argv[arg_no][arg_off++];
41     while (desc[i])
42     {
43         int desc_char = desc[i++];
44         int type = 0;
45         if (desc[i] == ':')
46         {       /* string argument */
47             type = desc[i++];
48         }
49         if (desc_char == ch)
50         { /* option with argument */
51             if (type)
52             {
53                 if (argv[arg_no][arg_off])
54                 {
55                     *arg = argv[arg_no]+arg_off;
56                     arg_no++;
57                     arg_off =  0;
58                 }
59                 else
60                 {
61                     arg_no++;
62                     arg_off = 0;
63                     if (arg_no < argc)
64                         *arg = argv[arg_no++];
65                     else
66                         *arg = "";
67                 }
68             }
69             else /* option with no argument */
70             {
71                 if (argv[arg_no][arg_off])
72                     arg_off++;
73                 else
74                 {
75                     arg_off = 0;
76                     arg_no++;
77                 }
78             }
79             return ch;
80         }               
81     }
82     *arg = argv[arg_no]+arg_off-1;
83     arg_no = arg_no + 1;
84     arg_off = 0;
85     return -1;
86 }