IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 6, 2005, 10:29:08 AM (21 years ago)
Author:
eugene
Message:

fixed multicommand to parse subcommands sequentially, added modulus operator

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/lib.shell/command.c

    r4448 r4462  
    22# define VERBOSE 0
    33
    4 static int Ncalls = 0;
    5 
    64int command (char *line, char **outline) {
    75
    8   int i, status, argc, targc;
    9   char **argv, **targv;
     6  int i, status, argc;
     7  char **argv;
    108  Command *cmd;
    11 
    12   Ncalls ++;
    139
    1410  /* force a space between ! and first word: !ls becomes ! ls */
     
    2622  line = parse (line);        /* line is freed here, new one allocated */
    2723  /* any entry in line of the form {foo} returns value or tmp vector / buffer */
     24
     25  /* we may have reallocated line, return new pointer */
     26  *outline = line;
    2827 
    29   targv = parse_commands (line, &targc);
    30   if (targc == 0) {
    31     /* empty command or assignment */
    32     if (line != (char *) NULL) free (line);
    33     *outline = (char *) NULL;
    34     return (TRUE);
     28  argv = parse_commands (line, &argc);
     29  if (argc == 0) return (TRUE);  /* empty command or assignment */
     30
     31  cmd = MatchCommand (argv[0], TRUE, FALSE);
     32  if (cmd == NULL) {
     33    status = FALSE;
     34  } else {
     35    free (argv[0]);
     36    argv[0] = strcreate (cmd[0].name);
     37    status = (*cmd[0].func) (argc, argv);
     38  }
     39  for (i = 0; i < argc; i++) free (argv[i]);
     40  free (argv);
     41
     42  if (!status) {
     43    char *msg;
     44    msg = get_variable_ptr ("ERRORMSG");
     45    if (msg != (char *) NULL) fprintf (stderr, "%s\n", msg);
    3546  }
    3647
    37   /* this needs some work to use multicommand correctly */
    38   /* allocate extra space to have null terminated list */
    39   ALLOCATE (argv, char *, targc + 1);
    40   argc = 0;
    41   status = TRUE;
    42   for (i = 0; status && (i < targc); i++) {
    43     if (targv[i][0] != ';') {
    44       argv[argc] = targv[i];
    45       argc ++;
    46     }
    47     if ((targv[i][0] == ';') || (i == targc - 1)) {
    48 
    49       if (argc == 0) continue;
    50       argv[argc] = 0;
    51 
    52       /* i +> argc + 1, argv[argc-1] = targv[i]
    53        * argv[n] = argv[argc - 1 - argc + 1 + n] = targv[i - argc + 1 + n]
    54        */
    55 
    56       cmd = MatchCommand (argv[0], TRUE, FALSE);
    57       if (cmd != NULL) {
    58         REALLOCATE (argv[0], char, strlen(cmd[0].name) + 1);
    59         targv[i-argc+1] = argv[0];  /* need to keep ptr in sync */
    60         strcpy (argv[0], cmd[0].name);
    61         status &= (*cmd[0].func) (argc, argv);
    62         if (auto_break && !status) break;
    63       }
    64       argc = 0;
    65     }
    66   }
    67   for (i = 0; i < targc; i++) free (targv[i]);
    68   free (argv);
    69   free (targv);
    70   *outline = line;
    71   if (!status) {
    72     char *msg;
    73     msg = get_variable ("ERRORMSG");
    74     if (msg != (char *) NULL) {
    75       fprintf (stderr, "%s\n", msg);
    76       free (msg);
    77     }
    78   }
    7948  set_int_variable ("STATUS", status);
    8049  if (VERBOSE) fprintf (stderr, "command: %s, status: %d\n", line, status);
    8150  return (status);
    82 
    8351}
    84 
    85 void print_ncalls () {
    86   fprintf (stderr, "Ncalls to command(): %d\n", Ncalls);
    87 }
    88 
Note: See TracChangeset for help on using the changeset viewer.