IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 38972


Ignore:
Timestamp:
Oct 27, 2015, 2:35:02 PM (11 years ago)
Author:
eugene
Message:

add list -glob option

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/cmd.basic/list.c

    r37049 r38972  
    11# include "basic.h"
     2# include <glob.h>
    23# define D_NLINES 100
    34static char prompt[] = ">> ";
    45
     6int SplitByCharToList (int argc, char **argv, int EXCEL_STYLE);
     7int GlobToList (char *listname, char *Glob, int EXCEL_STYLE);
     8int CommandToList (char *listname, char *Command, int EXCEL_STYLE);
     9
    510int list (int argc, char **argv) {
    611
    712  int ThisList, depth, i, done, found;
    813  char *input, line[1024];
    9   int N, Nbytes, NBYTES, Nread, status;
    10   int RunCommand;
    11   char *A, *B, *val, *Cmd;
    12   FILE *f;
    13 
    14   Cmd = NULL;
    15   RunCommand = FALSE;
     14  int N;
     15
     16  char *Command = NULL;
    1617  if ((N = get_argument (argc, argv, "-x"))) {
    1718    remove_argument (N, &argc, argv);
    18     Cmd = strcreate (argv[N]);
    19     remove_argument (N, &argc, argv);
    20     RunCommand = TRUE;
     19    Command = strcreate (argv[N]);
     20    remove_argument (N, &argc, argv);
     21  }
     22  if ((N = get_argument (argc, argv, "-exec"))) {
     23    remove_argument (N, &argc, argv);
     24    Command = strcreate (argv[N]);
     25    remove_argument (N, &argc, argv);
     26  }
     27
     28  char *Glob = NULL;
     29  if ((N = get_argument (argc, argv, "-glob"))) {
     30    remove_argument (N, &argc, argv);
     31    Glob = strcreate (argv[N]);
     32    remove_argument (N, &argc, argv);
    2133  }
    2234
     
    3749      return (FALSE);
    3850    }
    39     ListVectorsToList (argv[1]);
     51    ListVectorsToList (argv[1]); // <-- generate the list
    4052    return TRUE;
    4153  }
     
    4759      return (FALSE);
    4860    }
    49     ListBuffersToList (argv[1]);
     61    ListBuffersToList (argv[1]); // <-- generate the list
    5062    return TRUE;
     63  }
     64
     65  if (Command) {
     66    if (argc != 2) {
     67      gprint (GP_ERR, "USAGE: list (root) -exec (command)\n");
     68      gprint (GP_ERR, "   OR: list (root) -x (command)\n");
     69      return (FALSE);
     70    }
     71    int status = CommandToList (argv[1], Command, EXCEL_STYLE); // <-- generate the list
     72    return status;
     73  }
     74
     75  if (Glob) {
     76    if (argc != 2) {
     77      gprint (GP_ERR, "USAGE: list (root) -glob (fileglob)\n");
     78      return (FALSE);
     79    }
     80    int status = GlobToList (argv[1], Glob, EXCEL_STYLE); // <-- generate the list
     81    return status;
    5182  }
    5283
     
    74105      return (FALSE);
    75106    }
    76    
    77     int j, nWords;
    78     char splitter;
    79     char *new, *word, *ptr;
    80 
    81     nWords = 0;
    82     splitter = argv[3][0];
    83 
    84     for (i = 0; i < argc - 3; i++) {
    85       new = strcreate (argv[i+3]);
    86       for (j = 0; new[j]; j++) {
    87         if (new[j] == splitter) new[j] = ' ';
    88       }
    89 
    90       ptr = new;
    91       while (ptr) {
    92         word = thisword (ptr);
    93         if (!word) break;
    94        
    95         // sprintf (line, "%s:%d", argv[1], nWords);
    96         set_list_varname (line, argv[1], nWords, EXCEL_STYLE);
    97 
    98         set_str_variable (line, word);
    99         FREE (word);
    100         ptr = nextword (ptr);
    101         nWords ++;
    102       }
    103       FREE (new);
    104     }
    105     sprintf (line, "%s:n", argv[1]);
    106     set_int_variable (line, nWords);
    107 
    108     return (TRUE);
     107    int status = SplitByCharToList (argc, argv, EXCEL_STYLE); // <-- generate the list
     108    return status;
    109109  }
    110110
     
    200200    gprint (GP_ERR, "USAGE: list (root)                                        : supply list data, terminate with 'END'\n");
    201201    gprint (GP_ERR, "USAGE: list (root) -x (command)                           : create list from shell output\n");
     202    gprint (GP_ERR, "USAGE: list (root) -exec (command)                        : create list from shell output\n");
    202203    gprint (GP_ERR, "USAGE: list (root) -vectors                               : create list from vector names\n");
    203204    gprint (GP_ERR, "USAGE: list (root) -buffers                               : create list from buffer names\n");
     
    208209    gprint (GP_ERR, "USAGE: list (root) -del (word)                            : delete the entry by value\n");
    209210    return (FALSE);
    210   }
    211 
    212   if (RunCommand) {
    213    
    214     /* val will hold the result */
    215     NBYTES = 1024;
    216     ALLOCATE (val, char, NBYTES);
    217    
    218     /* need to loop until command produces no more output,
    219        REALLOCATING as needed. */
    220     f = popen (Cmd, "r");
    221     done = FALSE;
    222     Nbytes = 0;
    223     while (!done) {
    224       Nread = fread (&val[Nbytes], 1, 1023, f);
    225       if (Nread < 0) {
    226         gprint (GP_ERR, "error reading from command\n");
    227         done = TRUE;
    228       }
    229       if (Nread > 0) {
    230         Nbytes += Nread;
    231         NBYTES = 1024 + Nbytes;
    232         REALLOCATE (val, char, NBYTES);
    233       }
    234       if (Nread == 0) {
    235         done = TRUE;
    236       }
    237      
    238     }
    239     val[Nbytes] = 0;
    240     status = pclose (f);
    241     free (Cmd);
    242    
    243     if (status) {
    244       gprint (GP_ERR, "warning: exit status of command %d\n", status);
    245     }
    246      
    247     A = B = val;
    248     for (i = 0; B != (char *) NULL;) {
    249       while (isspace (*A) && (*A != 0)) A++;
    250       B = strchr (A, '\n');
    251       if (B != (char *) NULL) { *B = 0; }
    252       if (*A != 0) {
    253         // sprintf (line, "%s:%d", argv[1], i);
    254         set_list_varname (line, argv[1], i, EXCEL_STYLE);
    255         set_str_variable (line, A);
    256         A = B + 1;
    257         i++;
    258       }
    259     }     
    260     free (val);
    261    
    262     sprintf (line, "%s:n", argv[1]);
    263     set_int_variable (line, i);
    264     return (TRUE);
    265211  }
    266212
     
    309255  return (TRUE);
    310256}
     257
     258int CommandToList (char *listname, char *Command, int EXCEL_STYLE) {
     259
     260  char line[1024];
     261
     262  /* val will hold the result */
     263  int NBYTES = 1024;
     264  char *val;
     265  ALLOCATE (val, char, NBYTES);
     266   
     267  /* need to loop until command produces no more output,
     268     REALLOCATING as needed. */
     269  FILE *f = popen (Command, "r");
     270
     271  int done = FALSE;
     272  int Nbytes = 0;
     273  while (!done) {
     274    int Nread = fread (&val[Nbytes], 1, 1023, f);
     275    if (Nread < 0) {
     276      gprint (GP_ERR, "error reading from command\n");
     277      done = TRUE;
     278    }
     279    if (Nread > 0) {
     280      Nbytes += Nread;
     281      NBYTES = 1024 + Nbytes;
     282      REALLOCATE (val, char, NBYTES);
     283    }
     284    if (Nread == 0) {
     285      done = TRUE;
     286    }
     287  }
     288  val[Nbytes] = 0;
     289  int status = pclose (f);
     290  free (Command);
     291   
     292  if (status) {
     293    gprint (GP_ERR, "warning: exit status of command %d\n", status);
     294  }
     295     
     296  int i;
     297  char *A = val;
     298  char *B = val;
     299  for (i = 0; B != (char *) NULL;) {
     300    while (isspace (*A) && (*A != 0)) A++;
     301    B = strchr (A, '\n');
     302    if (B != (char *) NULL) { *B = 0; }
     303    if (*A != 0) {
     304      set_list_varname (line, listname, i, EXCEL_STYLE);
     305      set_str_variable (line, A);
     306      A = B + 1;
     307      i++;
     308    }
     309  }     
     310  free (val);
     311   
     312  sprintf (line, "%s:n", listname);
     313  set_int_variable (line, i);
     314  return (TRUE);
     315}
     316
     317int GlobToList (char *listname, char *Glob, int EXCEL_STYLE) {
     318
     319  int i;
     320  char line[1024];
     321  glob_t globList;
     322
     323  // parse the filename as a glob
     324  globList.gl_offs = 0;
     325  glob (Glob, 0, NULL, &globList);
     326
     327  // if the glob does not match, save the literal word:
     328  // otherwise save all glob matches
     329  if (globList.gl_pathc == 0) {
     330    sprintf (line, "%s:n", listname);
     331    set_int_variable (line, 0);
     332    return TRUE;
     333  }
     334
     335  int Nfile = globList.gl_pathc;
     336  for (i = 0; i < Nfile; i++) {
     337    set_list_varname (line, listname, i, EXCEL_STYLE);
     338    set_str_variable (line, globList.gl_pathv[i]);
     339  }
     340  sprintf (line, "%s:n", listname);
     341  set_int_variable (line, Nfile);
     342  return (TRUE);
     343}
     344
     345int SplitByCharToList (int argc, char **argv, int EXCEL_STYLE) {
     346
     347  int i, j;
     348  char line[1024];
     349
     350  int nWords = 0;
     351  char splitter = argv[3][0];
     352
     353  for (i = 0; i < argc - 3; i++) {
     354    char *new = strcreate (argv[i+3]);
     355    for (j = 0; new[j]; j++) {
     356      if (new[j] == splitter) new[j] = ' ';
     357    }
     358
     359    char *ptr = new;
     360    while (ptr) {
     361      char *word = thisword (ptr);
     362      if (!word) break;
     363       
     364      set_list_varname (line, argv[1], nWords, EXCEL_STYLE);
     365
     366      set_str_variable (line, word);
     367      FREE (word);
     368      ptr = nextword (ptr);
     369      nWords ++;
     370    }
     371    FREE (new);
     372  }
     373  sprintf (line, "%s:n", argv[1]);
     374  set_int_variable (line, nWords);
     375
     376  return (TRUE);
     377}
Note: See TracChangeset for help on using the changeset viewer.