IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 5, 2022, 6:34:32 AM (5 years ago)
Author:
eugene
Message:

adding getargs command (argument parsing for macros)

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  
    2828$(SRC)/file.$(ARCH).o        \
    2929$(SRC)/getchr.$(ARCH).o     \
     30$(SRC)/getargs.$(ARCH).o     \
    3031$(SRC)/help.$(ARCH).o        \
    3132$(SRC)/input.$(ARCH).o       \
  • branches/eam_branches/ipp-20211108/Ohana/src/opihi/cmd.basic/getargs.c

    r41974 r41978  
    11# include "basic.h"
     2# define NLINE 1024
     3
     4enum {MODE_NONE, MODE_BOOL, MODE_VALUE};
    25
    36int getargs (int argc, char **argv) {
    47
    5   int N, Nkey, Noutput;
    6   char *p, *q, *input, *output, *key, *value;
     8  int N;
     9  char templine[NLINE];
    710
    8   // clear all sections
     11  // variable name to save the value
    912  char *varName = NULL;
    1013  if ((N = get_argument (argc, argv, "-var"))) {
     
    1417  }
    1518
    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");
    1922    return (FALSE);
    2023  }
    2124
    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
    2433  int   MacroDepth = GetMacroDepth ();
    2534
    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");
    3039
     40  int argnum = atoi (p);
    3141
    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;
    5851  }
    5952 
    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;
    6462  }
    6563
    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  }
    67101  return (TRUE);
    68102}
  • branches/eam_branches/ipp-20211108/Ohana/src/opihi/cmd.basic/init.c

    r41341 r41978  
    1414int file            PROTO((int, char **));
    1515int getchr_func     PROTO((int, char **));
     16int getargs         PROTO((int, char **));
    1617int help            PROTO((int, char **));
    1718int input           PROTO((int, char **));
     
    6667  {1, "file",          file,               "test file existence"},
    6768  {1, "getchr",        getchr_func,        "find character in string"},
     69  {1, "getargs",       getargs,            "find and remove arguments from the macro argument list"},
    6870  {1, "help",          help,               "get help on a function *"},
    6971  {1, "input",         input,              "read command lines from a file *"},
Note: See TracChangeset for help on using the changeset viewer.