/* -*- mode: c; tabs: 4; -*- ************************************************ * Module name : longopt.c * Module type : CMDFILE source file * Environment(s): n/a * * Version Who Date Description APY 12/99 Created 05/10/04 -e/E exclusive check/error * * $Source: /cvsroot/device/DEVL/UTILS/CMDFILE/longopt.c,v $ * $Revision: 1.7 $ $Date: 2004/11/05 07:51:39 $ $State: Exp $ * $Author: ayoung $ $Locker: $ *.........................................................................*/ #include #include #include #include "getopt.h" #include "longopt.h" #include "cmdfile.h" #define VALID_OPTIONS "nNokreEvtwWmd" #define HAS_ARGUMENT "krmdW" #define OPTIONAL_ARGUMENT "k" static struct option const long_options[] = { { "help", no_argument, 0, 'h' }, { "version", no_argument, 0, 'v' }, { 0, 0, 0, 0 } }; /* Process long options --help and --version, but only if argc == 2. Be careful not to gobble up `--'. */ void parse_long_options( argc, argv, command_name, version_string, usage ) int argc; char **argv; const char *command_name; const char *version_string; void (*usage)(); { int c; int saved_opterr; int saved_optind; saved_opterr = opterr; saved_optind = optind; /* Don't print an error message for unrecognized options. */ opterr = 0; if (argc == 2 && (c = getoptl(argc, argv, "+", long_options, 0)) != EOF) { switch (c) { case 'h': (*usage)(0); case 'v': printf("%s - %s\n", command_name, version_string); exit(0); default: /* Don't process any other long-named options. */ break; } } /* Restore previous value */ opterr = saved_opterr; /* Restore optind in case it has advanced past a leading `--'. */ optind = saved_optind; } int parse_short_argument( const register char *arg ) { int argument = FALSE; register int i; /* If it appears that we are handling options, then make sure that all of the options specified are actually valid. Otherwise, the string should just be echoed. */ for (i = 0; arg[i]; i++) { if (arg[i] == 'o') break; /* must be last */ if (argument) argument = FALSE; /* argument to previous */ else { if (strrchr (VALID_OPTIONS, arg[i]) == 0) { /* not within valid list */ if (vflg > 1) printf( "invalid character '%c' in option list -- ignored\n", arg[i] ); return (-1); } if (strrchr (HAS_ARGUMENT, arg[i])) { /* argument expected */ if (arg[i+1] == '\0') { if (!strrchr (OPTIONAL_ARGUMENT, arg[i])) { if (vflg > 1) printf( "option '%c' missing argument -- ignored\n", arg[i] ); return (-1); } } argument = TRUE; } } } /* All of the options are valid options ... handle them. */ while (*arg) { if (*arg == 'o') { arg++; /* skip 'o */ if (strlen(arg) == 0) fatalerr("Missing -o"); if (output != stdout) /* shouldn't happen */ fatalerr("Second -o encountered \"%s\"", arg); oflg = arg; if (*oflg == '\0') fatalerr("Missing output file name"); break; } else if (*arg == 'n') /* no newline */ nflg = 0; else if (*arg == 'N') nlflg = 1; /* newline was seperator */ else if (*arg == 'k') { if (arg[1] == '1') arg++, kflg++; /* lf/cr kludge */ else if (arg[1] == '2') arg++, k2flg++; /* MRI kludge */ else { kflg = 1; } } else if (*arg == 'r') /* inline realpath */ { if (rflg == -1) /* remove default (if any) */ rtags[ rflg = 0 ] = '\0'; if (arg[1]) rtags[ rflg++ ] = arg[1], arg++; } else if (*arg == 'e') { if (eflg != 1) fatalerr("-E and -e are exclusive"); eflg = 2; } else if (*arg == 'E') { if (eflg != 1) fatalerr("-E and -e are exclusive"); eflg = 0; } else if (*arg == 'v') vflg++; else if (*arg == 'c') cflg++; else if (*arg == 'M') Mflg++; else if (*arg == 't') tflg++; else if (*arg == 'w') wflg++; else if (*arg == 'W') /* whitespace handling */ { switch(arg[1]) { case '0': /* method 0: nothing */ case '-': whitespace = WS_IGNORE; break; case '1': /* method 1: escape */ case 'e': whitespace = WS_ESCAPE; break; case '2': /* method 2: quote path */ case 'q': whitespace = WS_QUOTE; break; /* case '3': */ /* method 3: junk */ /* case 'j': */ /* whitespace = WS_JUNK; */ /* break; */ /* case '4': */ /* method 4: URL */ /* case 'u': */ /* whitespace = WS_URL; */ /* break; */ default: fatalerr( "Unknown -W specification '%c'", arg[1] ); break; } arg++; } else if (*arg == 'm' && arg[1]) mflg = arg[1], arg++; else if (*arg == 'd' && arg[1]) { dflg[0] = arg[1]; /* known pairs */ if (dflg[0] == '(') dflg[1] = ')'; else if (dflg[0] == '{') dflg[1] = '}'; else if (dflg[0] == '[') dflg[1] = ']'; else if (dflg[0] == '<') dflg[1] = '>'; else dflg[1] = arg[1]; /* single */ arg++; } else { return (-1); /* unknown, exit arg loop */ } arg++; } return (1); }