Wed, 19 Apr 2000 22:10:28 +0200
trunk: changeset 8
Moved include/*.h to include/libtu/
| 0 | 1 | /* |
| 2 | * libtu/optparser.c | |
| 3 | * | |
| 4 | * Copyright (c) Tuomo Valkonen 1999-2000. | |
| 5 | * | |
| 6 | * This file is distributed under the terms of the "Artistic License". | |
| 7 | * See the included file LICENSE for details. | |
| 8 | */ | |
| 9 | ||
| 10 | #include <string.h> | |
| 11 | #include <stdlib.h> | |
| 12 | ||
| 5 | 13 | #include <libtu/util.h> |
| 14 | #include <libtu/misc.h> | |
| 15 | #include <libtu/optparser.h> | |
| 16 | #include <libtu/output.h> | |
| 0 | 17 | |
| 18 | ||
| 19 | #define O_ARGS(o) (o->flags&OPT_OPT_ARG) | |
| 20 | #define O_ARG(o) (o->flasg&OPT_ARG) | |
| 21 | #define O_OPT_ARG(o) (O_ARGS(o)==OPT_OPT_ARG) | |
| 22 | #define O_ID(o) (o->optid) | |
| 23 | ||
| 24 | ||
| 25 | static const OptParserOpt *o_opts=NULL; | |
| 26 | static char *const *o_current=NULL; | |
| 27 | static int o_left=0; | |
| 28 | static const char* o_chain_ptr=NULL; | |
| 29 | static int o_args_left=0; | |
| 30 | static const char*o_tmp=NULL; | |
| 31 | static int o_error=0; | |
| 3 | 32 | static int o_mode=OPTP_CHAIN; |
| 0 | 33 | |
| 34 | ||
| 35 | /* */ | |
| 36 | ||
| 37 | ||
| 3 | 38 | void optparser_init(int argc, char *const argv[], int mode, |
| 39 | const OptParserOpt *opts) | |
| 0 | 40 | { |
| 3 | 41 | o_mode=mode; |
| 0 | 42 | o_opts=opts; |
| 43 | o_current=argv+1; | |
| 44 | o_left=argc-1; | |
| 45 | o_chain_ptr=NULL; | |
| 46 | o_args_left=0; | |
| 47 | o_tmp=NULL; | |
| 48 | } | |
| 49 | ||
| 50 | ||
| 51 | /* */ | |
| 52 | ||
| 53 | ||
| 54 | static const OptParserOpt *find_chain_opt(char p, const OptParserOpt *o) | |
| 55 | { | |
| 56 | for(;O_ID(o);o++){ | |
| 3 | 57 | if(O_ID(o)==p) |
| 0 | 58 | return o; |
| 59 | } | |
| 60 | return NULL; | |
| 61 | } | |
| 62 | ||
| 63 | ||
| 64 | static bool is_option(const char *p) | |
| 65 | { | |
| 66 | if(p==NULL) | |
| 67 | return FALSE; | |
| 68 | if(*p++!='-') | |
| 69 | return FALSE; | |
| 70 | if(*p++!='-') | |
| 71 | return FALSE; | |
| 72 | if(*p=='\0') | |
| 73 | return FALSE; | |
| 74 | return TRUE; | |
| 75 | } | |
| 76 | ||
| 77 | ||
| 78 | /* */ | |
| 79 | ||
| 3 | 80 | enum{ |
| 81 | SHORT, MIDLONG, LONG | |
| 82 | }; | |
| 83 | ||
| 0 | 84 | |
| 85 | int optparser_get_opt() | |
| 86 | { | |
| 87 | #define RET(X) return o_tmp=p, o_error=X | |
| 88 | const char *p, *p2=NULL; | |
| 3 | 89 | bool dash=TRUE; |
| 90 | int type=SHORT; | |
| 0 | 91 | const OptParserOpt *o; |
| 92 | int l; | |
| 93 | ||
| 94 | while(o_args_left) | |
| 95 | optparser_get_arg(); | |
| 96 | ||
| 97 | o_tmp=NULL; | |
| 98 | ||
| 3 | 99 | /* Are we doing a chain (i.e. opt. of style 'tar xzf')? */ |
| 0 | 100 | if(o_chain_ptr!=NULL){ |
| 101 | p=o_chain_ptr++; | |
| 102 | if(!*o_chain_ptr) | |
| 103 | o_chain_ptr=NULL; | |
| 104 | ||
| 105 | o=find_chain_opt(*p, o_opts); | |
| 106 | ||
| 107 | if(o==NULL) | |
| 108 | RET(E_OPT_INVALID_CHAIN_OPTION); | |
| 109 | ||
| 110 | goto do_arg; | |
| 111 | } | |
| 112 | ||
| 113 | if(o_left<1) | |
| 114 | return OPT_ID_END; | |
| 115 | ||
| 116 | o_left--; | |
| 117 | p=*o_current++; | |
| 118 | ||
| 119 | if(*p!='-'){ | |
| 120 | dash=FALSE; | |
| 3 | 121 | if(o_mode!=OPTP_NO_DASH) |
| 122 | RET(OPT_ID_ARGUMENT); | |
| 0 | 123 | }else if(*(p+1)=='-'){ |
| 124 | /* --foo */ | |
| 125 | if(*(p+2)=='\0'){ | |
| 3 | 126 | /* -- arguments */ |
| 0 | 127 | o_args_left=o_left; |
| 128 | RET(OPT_ID_ARGUMENT); | |
| 129 | } | |
| 3 | 130 | type=LONG; |
| 0 | 131 | p2=p+2; |
| 132 | }else{ | |
| 133 | /* -foo */ | |
| 134 | if(*(p+1)=='\0'){ | |
| 135 | /* - */ | |
| 136 | o_args_left=1; | |
| 137 | RET(OPT_ID_ARGUMENT); | |
| 138 | } | |
| 3 | 139 | if(*(p+2)!='\0' && o_mode==OPTP_MIDLONG) |
| 140 | type=MIDLONG; | |
| 141 | ||
| 0 | 142 | p2=p+1; |
| 143 | } | |
| 144 | ||
| 145 | o=o_opts; | |
| 146 | ||
| 147 | again: | |
| 148 | for(;O_ID(o);o++){ | |
| 3 | 149 | if(type==LONG){ |
| 0 | 150 | /* Do long option (--foo=bar) */ |
| 151 | if(o->longopt==NULL) | |
| 152 | continue; | |
| 153 | l=strlen(o->longopt); | |
| 154 | if(strncmp(p2, o->longopt, l)!=0) | |
| 155 | continue; | |
| 156 | ||
| 157 | if(p2[l]=='\0'){ | |
| 158 | if(O_ARGS(o)==OPT_ARG) | |
| 159 | RET(E_OPT_MISSING_ARGUMENT); | |
| 160 | return O_ID(o); | |
| 161 | }else if(p2[l]=='='){ | |
| 162 | if(!O_ARGS(o)) | |
| 163 | RET(E_OPT_UNEXPECTED_ARGUMENT); | |
| 164 | if(p2[l+1]=='\0') | |
| 165 | RET(E_OPT_MISSING_ARGUMENT); | |
| 166 | o_tmp=p2+l+1; | |
| 167 | o_args_left=1; | |
| 168 | return O_ID(o); | |
| 169 | } | |
| 3 | 170 | continue; |
| 171 | }else if(type==MIDLONG){ | |
| 172 | if(o->longopt==NULL) | |
| 173 | continue; | |
| 174 | ||
| 175 | if(strcmp(p2, o->longopt)!=0) | |
| 176 | continue; | |
| 177 | }else{ /* type==SHORT */ | |
| 178 | if(*p2!=O_ID(o)) | |
| 0 | 179 | continue; |
| 180 | ||
| 181 | if(*(p2+1)!='\0'){ | |
| 3 | 182 | if(o_mode==OPTP_CHAIN || o_mode==OPTP_NO_DASH){ |
| 183 | /*valid_chain(p2+1, o_opts)*/ | |
| 184 | o_chain_ptr=p2+1; | |
| 185 | p++; | |
| 186 | }else if(o_mode==OPTP_IMMEDIATE){ | |
| 187 | if(!O_ARGS(o)){ | |
| 188 | if(*(p2+1)!='\0') | |
| 189 | RET(E_OPT_UNEXPECTED_ARGUMENT); | |
| 190 | }else{ | |
| 191 | if(*(p2+1)=='\0') | |
| 192 | RET(E_OPT_MISSING_ARGUMENT); | |
| 193 | o_tmp=p2+1; | |
| 194 | o_args_left=1; | |
| 195 | } | |
| 196 | return O_ID(o); | |
| 0 | 197 | }else{ |
| 3 | 198 | RET(E_OPT_SYNTAX_ERROR); |
| 0 | 199 | } |
| 200 | } | |
| 3 | 201 | } |
| 202 | ||
| 203 | do_arg: | |
| 204 | ||
| 205 | if(!O_ARGS(o)) | |
| 206 | return O_ID(o); | |
| 207 | ||
| 208 | if(!o_left || is_option(*o_current)){ | |
| 209 | if(O_ARGS(o)==OPT_OPT_ARG) | |
| 0 | 210 | return O_ID(o); |
| 3 | 211 | RET(E_OPT_MISSING_ARGUMENT); |
| 212 | } | |
| 0 | 213 | |
| 3 | 214 | o_args_left=1; |
| 215 | return O_ID(o); | |
| 0 | 216 | } |
| 217 | ||
| 218 | if(dash) | |
| 219 | RET(E_OPT_INVALID_OPTION); | |
| 220 | ||
| 221 | RET(OPT_ID_ARGUMENT); | |
| 222 | #undef RET | |
| 223 | } | |
| 224 | ||
| 225 | ||
| 226 | /* */ | |
| 227 | ||
| 228 | ||
| 229 | const char* optparser_get_arg() | |
| 230 | { | |
| 231 | const char *p; | |
| 232 | ||
| 233 | if(o_tmp!=NULL){ | |
| 234 | /* If o_args_left==0, then were returning an invalid option | |
| 235 | * otherwise an immediate argument (e.g. -funsigned-char | |
| 236 | * where '-f' is the option and 'unsigned-char' the argument) | |
| 237 | */ | |
| 238 | if(o_args_left>0) | |
| 239 | o_args_left--; | |
| 240 | p=o_tmp; | |
| 241 | o_tmp=NULL; | |
| 242 | return p; | |
| 243 | } | |
| 244 | ||
| 245 | if(o_args_left<1 || o_left<1) | |
| 246 | return NULL; | |
| 247 | ||
| 248 | o_left--; | |
| 249 | o_args_left--; | |
| 250 | return *o_current++; | |
| 251 | } | |
| 252 | ||
| 253 | ||
| 254 | /* */ | |
| 255 | ||
| 256 | static void warn_arg(const char *e) | |
| 257 | { | |
| 3 | 258 | const char *p=optparser_get_arg(); |
| 0 | 259 | |
| 260 | if(p==NULL) | |
| 261 | warn("%s (null)", e); | |
| 262 | else | |
| 263 | warn("%s \'%s\'", e, p); | |
| 264 | } | |
| 265 | ||
| 266 | ||
| 267 | static void warn_opt(const char *e) | |
| 268 | { | |
| 3 | 269 | if(o_tmp!=NULL && o_chain_ptr!=NULL) |
| 0 | 270 | warn("%s \'-%c\'", e, *o_tmp); |
| 271 | else | |
| 272 | warn_arg(e); | |
| 273 | } | |
| 274 | ||
| 275 | ||
| 276 | void optparser_print_error() | |
| 277 | { | |
| 278 | switch(o_error){ | |
| 279 | case E_OPT_INVALID_OPTION: | |
| 3 | 280 | case E_OPT_INVALID_CHAIN_OPTION: |
| 0 | 281 | warn_opt(TR("Invalid option")); |
| 282 | break; | |
| 283 | ||
| 284 | case E_OPT_SYNTAX_ERROR: | |
| 285 | warn_arg(TR("Syntax error while parsing")); | |
| 286 | break; | |
| 287 | ||
| 288 | case E_OPT_MISSING_ARGUMENT: | |
| 289 | warn_opt(TR("Missing argument to")); | |
| 290 | break; | |
| 291 | ||
| 292 | case E_OPT_UNEXPECTED_ARGUMENT: | |
| 3 | 293 | warn_opt(TR("No argument expected:")); |
| 0 | 294 | break; |
| 295 | ||
| 296 | case OPT_ID_ARGUMENT: | |
| 297 | warn(TR("Unexpected argument")); | |
| 298 | break; | |
| 299 | ||
| 300 | default: | |
| 301 | warn(TR("(unknown error)")); | |
| 302 | } | |
| 303 | ||
| 304 | o_tmp=NULL; | |
| 305 | o_error=0; | |
| 306 | } |