Changeset 41978 for branches/eam_branches/ipp-20211108
- Timestamp:
- Jan 5, 2022, 6:34:32 AM (5 years ago)
- Location:
- branches/eam_branches/ipp-20211108/Ohana/src/opihi/cmd.basic
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20211108/Ohana/src/opihi/cmd.basic/Makefile
r40522 r41978 28 28 $(SRC)/file.$(ARCH).o \ 29 29 $(SRC)/getchr.$(ARCH).o \ 30 $(SRC)/getargs.$(ARCH).o \ 30 31 $(SRC)/help.$(ARCH).o \ 31 32 $(SRC)/input.$(ARCH).o \ -
branches/eam_branches/ipp-20211108/Ohana/src/opihi/cmd.basic/getargs.c
r41974 r41978 1 1 # include "basic.h" 2 # define NLINE 1024 3 4 enum {MODE_NONE, MODE_BOOL, MODE_VALUE}; 2 5 3 6 int getargs (int argc, char **argv) { 4 7 5 int N , Nkey, Noutput;6 char *p, *q, *input, *output, *key, *value;8 int N; 9 char templine[NLINE]; 7 10 8 // clear all sections11 // variable name to save the value 9 12 char *varName = NULL; 10 13 if ((N = get_argument (argc, argv, "-var"))) { … … 14 17 } 15 18 16 if (argc != 4) {17 gprint (GP_ERR, "USAGE: getargs ( option) (key) (mode) [-var out]\n");18 gprint (GP_ERR, " find argument key and remove the arglist, optionally saving value on variable\n");19 if (argc != 3) { 20 gprint (GP_ERR, "USAGE: getargs (key) (mode) [-var out]\n"); 21 gprint (GP_ERR, " find argument key and remove from the arglist, optionally saving value on variable\n"); 19 22 return (FALSE); 20 23 } 21 24 22 // 23 char *MacroName = GetMacroName (); 25 int mode = MODE_NONE; 26 if (!strcasecmp (argv[2], "bool")) mode = MODE_BOOL; 27 if (!strcasecmp (argv[2], "boolean")) mode = MODE_BOOL; 28 if (!strcasecmp (argv[2], "val")) mode = MODE_VALUE; 29 if (!strcasecmp (argv[2], "value")) mode = MODE_VALUE; 30 if (mode == MODE_NONE) { gprint (GP_ERR, "ERROR: invalid mode %s\n", argv[2]); return (FALSE); } 31 32 // get macro name and depth 24 33 int MacroDepth = GetMacroDepth (); 25 34 26 sprintf (tmp, "%d.%d", MacroDepth, 0);27 params[0] = strcreate (tmp);28 sprintf (tmp, "%d", argc);29 set_str_variable (params[0], tmp);35 // real name of $0 (number of arguments) 36 snprintf (templine, NLINE, "%d.%d", MacroDepth, 0); 37 char *p = get_variable_ptr (templine); 38 myAssert (p, "coding error"); 30 39 40 int argnum = atoi (p); 31 41 32 // input string 33 input = argv[1]; 34 35 // find this in the string 36 key = argv[2]; 37 Nkey = strlen(key); 38 39 // replace with this 40 value = argv[3]; 41 42 Noutput = MAX (16, strlen(input)); 43 ALLOCATE (output, char, Noutput); 44 memset (output, 0, Noutput); 45 46 // 0123456789 47 // wordKEYnew 48 // p = 0, q = 4, q-p = 4 -> p = 7 49 50 p = input; 51 while (*p && ((q = strstr (p, key)) != NULL)) { 52 output = opihi_append (output, &Noutput, p, q); 53 output = opihi_append (output, &Noutput, value, value + strlen(value)); 54 p = q + Nkey; 55 } 56 if (*p) { 57 output = opihi_append (output, &Noutput, p, p + strlen(p)); 42 // look for the requested key (only selects the first example) 43 int argmatch = 0; 44 for (int i = 1; (i < argnum) && !argmatch; i++) { 45 // real name of $i (number of arguments) 46 snprintf (templine, NLINE, "%d.%d", MacroDepth, i); 47 char *p = get_variable_ptr (templine); 48 myAssert (p, "coding error"); 49 if (strcmp (argv[1], p)) continue; 50 argmatch = i; 58 51 } 59 52 60 if (varName) { 61 set_str_variable (varName, output); 62 } else { 63 gprint (GP_LOG, "%s\n", output); 53 if (!argmatch) { 54 if (varName) { 55 if (mode == MODE_BOOL) { 56 set_str_variable (varName, "0"); // NOTE: if a local variable with the same name exists, that will be used 57 } else { 58 set_str_variable (varName, ""); // NOTE: if a local variable with the same name exists, that will be used 59 } 60 } 61 return TRUE; 64 62 } 65 63 66 free (output); 64 if (mode == MODE_VALUE) { 65 if (argnum < argmatch + 2) { gprint (GP_ERR, "ERROR: missing value for argument %s\n", argv[1]); return (FALSE); } 66 snprintf (templine, NLINE, "%d.%d", MacroDepth, argmatch + 1); 67 char *p = get_variable_ptr (templine); 68 myAssert (p, "coding error"); 69 70 if (varName) set_str_variable (varName, p); // NOTE: if a local variable with the same name exists, that will be used 71 72 for (int i = argmatch + 2; i < argnum; i++) { 73 // move $i+2 to $i 74 snprintf (templine, NLINE, "%d.%d", MacroDepth, i); 75 char *p = get_variable_ptr (templine); 76 myAssert (p, "coding error"); 77 78 snprintf (templine, NLINE, "%d.%d", MacroDepth, i - 2); 79 set_str_variable (templine, p); 80 } 81 // reduce $0 by 2 82 snprintf (templine, NLINE, "%d.%d", MacroDepth, 0); 83 set_int_variable (templine, argnum - 2); 84 } 85 if (mode == MODE_BOOL) { 86 if (varName) set_str_variable (varName, "1"); // NOTE: if a local variable with the same name exists, that will be used 87 88 for (int i = argmatch + 1; i < argnum; i++) { 89 // move $i+1 to $i 90 snprintf (templine, NLINE, "%d.%d", MacroDepth, i); 91 char *p = get_variable_ptr (templine); 92 myAssert (p, "coding error"); 93 94 snprintf (templine, NLINE, "%d.%d", MacroDepth, i - 1); 95 set_str_variable (templine, p); 96 } 97 // reduce $0 by 1 98 snprintf (templine, NLINE, "%d.%d", MacroDepth, 0); 99 set_int_variable (templine, argnum - 1); 100 } 67 101 return (TRUE); 68 102 } -
branches/eam_branches/ipp-20211108/Ohana/src/opihi/cmd.basic/init.c
r41341 r41978 14 14 int file PROTO((int, char **)); 15 15 int getchr_func PROTO((int, char **)); 16 int getargs PROTO((int, char **)); 16 17 int help PROTO((int, char **)); 17 18 int input PROTO((int, char **)); … … 66 67 {1, "file", file, "test file existence"}, 67 68 {1, "getchr", getchr_func, "find character in string"}, 69 {1, "getargs", getargs, "find and remove arguments from the macro argument list"}, 68 70 {1, "help", help, "get help on a function *"}, 69 71 {1, "input", input, "read command lines from a file *"},
Note:
See TracChangeset
for help on using the changeset viewer.
