switch to async system
[vg.git] / vg_opt.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef VG_OPT_H
6 #define VG_OPT_H
7
8 #include <stdlib.h>
9 #include "vg_platform.h"
10 #include "vg_log.h"
11
12 /*
13 * Supported:
14 * short flags | -abc
15 * short options | -a value
16 * multi-set options | -ab value
17 *
18 * long gnu options | --long-value=test
19 * standard agument | regular_thing
20 */
21
22 static int vg_argi = 1;
23 static int vg_argj = 1;
24 static int vg_argc = 0;
25 static int vg_consume_next = 0;
26 static char **vg_argv;
27
28 /* Will return 0 if exhausted */
29 int vg_argp( int argc, char *argv[] )
30 {
31 vg_argv = argv;
32 vg_argc = argc;
33
34 static int delta_i = 0;
35 static int delta_j = 0;
36
37 if( vg_argj != 1 && !vg_argv[ vg_argi ][ vg_argj ] )
38 {
39 vg_argj = 1;
40 vg_argi ++;
41 }
42
43 if( vg_consume_next )
44 {
45 vg_consume_next = 0;
46 vg_argi ++;
47 }
48
49 if( vg_argi >= argc )
50 return 0;
51
52 if( (delta_i == vg_argi) && (delta_j == vg_argj) )
53 {
54 char *cur = &vg_argv[ vg_argi ][ vg_argj ];
55
56 if( *cur != '-' )
57 {
58 vg_error( "Unknown opt '-%c'\n", *cur );
59 }
60 else
61 {
62 vg_error( "Unknown opt '--%s'\n", cur + 1 );
63 }
64
65 exit(0);
66 }
67
68 delta_i = vg_argi;
69 delta_j = vg_argj;
70
71 return 1;
72 }
73
74 /* Example: see if -c is set */
75 int vg_opt( char c )
76 {
77 char *carg = vg_argv[ vg_argi ];
78
79 if( carg[0] == '-' )
80 {
81 if( carg[1] == '-' )
82 return 0;
83
84 if( carg[ vg_argj ] == c )
85 {
86 vg_argj ++;
87
88 return 1;
89 }
90 }
91
92 return 0;
93 }
94
95 /* Example: get -c *value* */
96 char *vg_opt_arg( char c )
97 {
98 if( vg_opt( c ) )
99 {
100 if( vg_argi < vg_argc-1 )
101 {
102 if( vg_argv[ vg_argi + 1 ][0] != '-' )
103 {
104 vg_consume_next = 1;
105 return vg_argv[ vg_argi + 1 ];
106 }
107 }
108
109 vg_error( "Option '%c' requires argument!\n", c );
110 exit(0);
111 }
112
113 return NULL;
114 }
115
116 /* Example see if --big is set */
117 int vg_long_opt( char *name )
118 {
119 char *carg = vg_argv[ vg_argi ];
120
121 if( carg[0] == '-' )
122 {
123 if( carg[1] == '-' )
124 {
125 if( !strcmp( name, carg+2 ) )
126 {
127 vg_consume_next = 1;
128 return 1;
129 }
130 }
131 }
132
133 return 0;
134 }
135
136 /* Example: get --big=value */
137 char *vg_long_opt_arg( char *name )
138 {
139 char *carg = vg_argv[ vg_argi ];
140
141 if( carg[0] == '-' )
142 {
143 if( carg[1] == '-' )
144 {
145 int k = 2; int set = 0;
146 while( carg[ k ] )
147 {
148 if( carg[ k ] == '=' )
149 {
150 set = 1;
151 break;
152 }
153
154 k ++;
155 }
156
157 if( !strncmp( name, carg+2, k-2 ) )
158 {
159 vg_consume_next = 1;
160
161 // the rest
162 if( set )
163 {
164 return carg + k + 1;
165 }
166 else
167 {
168 vg_error( "Long option '%s' requires argument\n", name );
169 }
170 }
171 }
172 }
173
174 return NULL;
175 }
176
177 /* Example: get regular_thing */
178 char *vg_arg(void)
179 {
180 char *carg = vg_argv[ vg_argi ];
181
182 if( carg[0] != '-' )
183 {
184 vg_consume_next = 1;
185 return carg;
186 }
187
188 return NULL;
189 }
190
191 #endif