📄 cmdline.c
字号:
* Inputs:* char *name: name of field, must be non-null if opt_sw == cl_INIT* int opt_sw: option, switch, init, or arg* int n: argument number (if opt_sw is cl_ARG)* Result:* returns pointer to option value/switch if one exists, otherwise null* Implementation:* parse the command line until name or arg is found* see if the option is followed by a string that does* not start with "-"*****************************************************************/private char *cl_search(name, opt_sw, n) char *name; int opt_sw; int n; /* if opt_sw is cl_ARG, n > 0 tells which one */{ register int i = 1; /* index into command line */ boolean abbr; boolean result = TRUE; ready_check(); /* parse command line: */ while (i < argc) { register char *arg = argv[i]; /* arguments that start with '-' should be quoted and quotes must be removed by the application */ if (*arg == '-') { int arg_type = find_string(arg + 1, &abbr); if (arg_type == cl_OPT) { i += 1; /* skip name and option */ /* don't look for '-' because the option might be a * negative number */ if (i >= argc /* || *arg == '-' */) { if (opt_sw == cl_INIT) { gprintf(ERROR, "missing argument after %s\n", arg); result = FALSE; } } else if (opt_sw == cl_OPT && (strcmp(arg + 1, name) == 0 || (abbr && *(arg + 1) == name[0]))) { return argv[i]; } } else if (arg_type == cl_SW) { if (opt_sw == cl_SW && (strcmp(arg + 1, name) == 0 || (abbr && *(arg + 1) == name[0]))) return arg; } else if (opt_sw == cl_INIT) { gprintf(ERROR, "invalid switch: %s\n", arg); result = FALSE; } } else if (opt_sw == cl_ARG) { if (n == 1) return arg; n--; } i++; /* skip to next field */ } if (opt_sw == cl_INIT) { /* return name or NULL to represent TRUE or FALSE */ return (result ? name : NULL); } return NULL;}/***************************************************************** cl_option* Inputs:* char *name: option name* Outputs:* returns char *: the option string if found, otherwise null****************************************************************/char *cl_option(name)char *name;{ return cl_search(name, cl_OPT, 0);}/***************************************************************** cl_switch* Inputs:* char *name: switch name* Outputs:* boolean: TRUE if switch found****************************************************************/boolean cl_switch(name)char *name;{ return (boolean)(cl_search(name, cl_SW, 0) != NULL);}/* cl_syntax -- install a string specifying options and switches *//**/boolean cl_syntax(char *s){ if (n_syntax < syntax_max) { syntax[n_syntax++] = s; return TRUE; } else { gprintf(ERROR, "cl_syntax: out of room\n"); return FALSE; }}/***************************************************************** find_string* Inputs:* char *s: string to find, terminated by any non-alphanumeric* boolean *abbr: set TRUE if s is an abbreviation, otherwise false* Effect:* Looks for s in syntax strings* Returns:* 0 = FALSE = not found, 1 = cl_OPT = option, 2 = cl_SW = switch*****************************************************************/private int find_string(s, abbr) char *s; boolean *abbr;{ int found_it = FALSE; int i; *abbr = FALSE; for (i = 0; i < n_syntax; i++) { /* loop through strings */ register char *syntax_ptr = syntax[i]; while (*syntax_ptr != EOS) { register char *s_ptr = s; while (*syntax_ptr != EOS && !(isalnum(*syntax_ptr))) syntax_ptr++; while (*s_ptr != EOS && (*s_ptr++ == *syntax_ptr)) syntax_ptr++; /* only increment if there's a match */ if (!(isalnum(*s_ptr)) && *syntax_ptr == '<') { syntax_ptr++; /* advance to the type field */ if (*syntax_ptr == 's') return cl_SW; if (*syntax_ptr != 'o') gprintf(ERROR, "(internal error) bad cl_syntax string: %s\n", syntax[i]); return cl_OPT; } /* no match, so go to next */ while (*syntax_ptr != ';' && *syntax_ptr != EOS) syntax_ptr++; if (*syntax_ptr == ';') syntax_ptr++; } } /* no match, maybe there is a single character match */ if (s[0] == EOS || s[1] != EOS) return FALSE; for (i = 0; i < n_syntax; i++) { /* loop through strings */ char *syntax_ptr = syntax[i]; while (*syntax_ptr != EOS) { while (*syntax_ptr != EOS && !(isalnum(*syntax_ptr))) syntax_ptr++; if (s[0] == *syntax_ptr) { if (found_it) return FALSE; /* ambiguous */ /* else, find the type */ while (*syntax_ptr != '<' && *syntax_ptr != EOS) syntax_ptr++; syntax_ptr++; if (*syntax_ptr == 's') found_it = cl_SW; else if (*syntax_ptr == 'o') found_it = cl_OPT; else return FALSE; /* error in string syntax */ } /* no match, so go to next */ while (*syntax_ptr != ';' && *syntax_ptr != EOS) syntax_ptr++; if (*syntax_ptr == ';') syntax_ptr++; } } if (found_it) *abbr = TRUE; return found_it;}/* get_arg -- get an argument from a file *//**/boolean get_arg(file, arg) FILE *file; char *arg;{ int c; while ((c = getc(file)) != EOF && isspace(c)) ; if (c == EOF) return FALSE; ungetc(c, file); while ((c = getc(file)) != EOF && !isspace(c)) { *arg++ = c; } *arg = 0; return TRUE;}/* indirect_command -- get argv, argc from a file *//**/private void indirect_command(filename, oldarg0) char *filename; char *oldarg0;{ FILE *argfile = fopen(filename, "r"); if (!argfile) { argv = (char **) malloc(sizeof(char *)); argv[0] = oldarg0; argc = 1; } else { int i = 1; char arg[100]; while (get_arg(argfile, arg)) i++; fclose(argfile); argfile = fopen(filename, "r"); argv = (char **) malloc(sizeof(char *) * i); argv[0] = oldarg0; argc = i; i = 1; while (get_arg(argfile, arg)) { argv[i] = (char *) malloc(strlen(arg) + 1); strcpy(argv[i], arg); i++; } }}/***************************************************************** ready_check* Effect:* Halt program if cl_rdy is not true.*****************************************************************/private void ready_check(){ if (!cl_rdy) { gprintf(ERROR, "Internal error: cl_init was not called, see cmdline.c\n"); EXIT(1); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -