Towards 2.1.44. Bump copyright year.
[yaz-moved-to-github.git] / src / options.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: options.c,v 1.5 2007-01-03 08:42:15 adam Exp $
6  */
7 /**
8  * \file options.c
9  * \brief Implements command line options parsing
10  */
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <stdlib.h>
16
17 #include <yaz/options.h>
18
19 static int arg_no = 1;
20 static int arg_off = 0;
21
22 int options (const char *desc, char **argv, int argc, char **arg)
23 {
24     int ch, i = 0;
25     
26     if (arg_no >= argc)
27         return -2;
28     if (arg_off == 0)
29     {
30         while (argv[arg_no][0] == '\0')
31         {
32             arg_no++;
33             if (arg_no >= argc)
34                 return -2;
35         }
36         if (argv[arg_no][0] != '-' || argv[arg_no][1] == '\0')
37         {
38             *arg = argv[arg_no++];
39             return 0;
40         }
41         arg_off++;
42     }
43     ch = argv[arg_no][arg_off++];
44     while (desc[i])
45     {
46         int desc_char = desc[i++];
47         int type = 0;
48         if (desc[i] == ':')
49         {       /* string argument */
50             type = desc[i++];
51         }
52         if (desc_char == ch)
53         { /* option with argument */
54             if (type)
55             {
56                 if (argv[arg_no][arg_off])
57                 {
58                     *arg = argv[arg_no]+arg_off;
59                     arg_no++;
60                     arg_off =  0;
61                 }
62                 else
63                 {
64                     arg_no++;
65                     arg_off = 0;
66                     if (arg_no < argc)
67                         *arg = argv[arg_no++];
68                     else
69                         *arg = "";
70                 }
71             }
72             else /* option with no argument */
73             {
74                 if (argv[arg_no][arg_off])
75                     arg_off++;
76                 else
77                 {
78                     arg_off = 0;
79                     arg_no++;
80                 }
81             }
82             return ch;
83         }               
84     }
85     *arg = argv[arg_no]+arg_off-1;
86     arg_no = arg_no + 1;
87     arg_off = 0;
88     return -1;
89 }
90 /*
91  * Local variables:
92  * c-basic-offset: 4
93  * indent-tabs-mode: nil
94  * End:
95  * vim: shiftwidth=4 tabstop=8 expandtab
96  */
97