Changeset 38972
- Timestamp:
- Oct 27, 2015, 2:35:02 PM (11 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/opihi/cmd.basic/list.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/opihi/cmd.basic/list.c
r37049 r38972 1 1 # include "basic.h" 2 # include <glob.h> 2 3 # define D_NLINES 100 3 4 static char prompt[] = ">> "; 4 5 6 int SplitByCharToList (int argc, char **argv, int EXCEL_STYLE); 7 int GlobToList (char *listname, char *Glob, int EXCEL_STYLE); 8 int CommandToList (char *listname, char *Command, int EXCEL_STYLE); 9 5 10 int list (int argc, char **argv) { 6 11 7 12 int ThisList, depth, i, done, found; 8 13 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; 16 17 if ((N = get_argument (argc, argv, "-x"))) { 17 18 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); 21 33 } 22 34 … … 37 49 return (FALSE); 38 50 } 39 ListVectorsToList (argv[1]); 51 ListVectorsToList (argv[1]); // <-- generate the list 40 52 return TRUE; 41 53 } … … 47 59 return (FALSE); 48 60 } 49 ListBuffersToList (argv[1]); 61 ListBuffersToList (argv[1]); // <-- generate the list 50 62 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; 51 82 } 52 83 … … 74 105 return (FALSE); 75 106 } 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; 109 109 } 110 110 … … 200 200 gprint (GP_ERR, "USAGE: list (root) : supply list data, terminate with 'END'\n"); 201 201 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"); 202 203 gprint (GP_ERR, "USAGE: list (root) -vectors : create list from vector names\n"); 203 204 gprint (GP_ERR, "USAGE: list (root) -buffers : create list from buffer names\n"); … … 208 209 gprint (GP_ERR, "USAGE: list (root) -del (word) : delete the entry by value\n"); 209 210 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);265 211 } 266 212 … … 309 255 return (TRUE); 310 256 } 257 258 int 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 317 int 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 345 int 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.
