75b30033b708eea3f5af1d264648ac10e9ee65c1
1 /*****************************************************************************
2 * RRDtool 1.1.x Copyright Tobias Oetiker, 1997 - 2002
3 *****************************************************************************
4 * rrd_tool.c Startup wrapper
5 *****************************************************************************/
7 #include "rrd_tool.h"
8 #include "rrd_xport.h"
10 void PrintUsage(char *cmd);
11 int CountArgs(char *aLine);
12 int CreateArgs(char *, char *, int, char **);
13 int HandleInputLine(int, char **, FILE*);
14 int RemoteMode=0;
15 int ChangeRoot=0;
16 #define TRUE 1
17 #define FALSE 0
18 #define MAX_LENGTH 10000
21 void PrintUsage(char *cmd)
22 {
24 char help_main[] =
25 "RRDtool 1.1.x Copyright 1997-2003 by Tobias Oetiker <tobi@oetiker.ch>\n"
26 #ifndef WIN32
27 " Compiled " MAKE_TIMESTAMP "\n\n"
28 #else
29 " Compiled " __DATE__ " " __TIME__ "\n\n"
30 #endif
31 "Usage: rrdtool [options] command command_options\n\n";
33 char help_list[] =
34 "Valid commands: create, update, updatev, graph, dump, restore,\n"
35 "\t\tlast, info, fetch, tune, resize, xport\n\n";
37 char help_listremote[] =
38 "Valid remote commands: quit, ls, cd, mkdir\n\n";
41 char help_create[] =
42 "* create - create a new RRD\n\n"
43 "\trrdtool create filename [--start|-b start time]\n"
44 "\t\t[--step|-s step]\n"
45 "\t\t[DS:ds-name:DST:dst arguments]\n"
46 "\t\t[RRA:CF:cf arguments]\n\n";
48 char help_dump[] =
49 "* dump - dump an RRD to XML\n\n"
50 "\trrdtool dump filename.rrd >filename.xml\n\n";
52 char help_info[] =
53 "* info - returns the configuration and status of the RRD\n\n"
54 "\trrdtool info filename.rrd\n\n";
56 char help_restore[] =
57 "* restore - restore an RRD file from its XML form\n\n"
58 "\trrdtool restore [--range-check|-r] filename.xml filename.rrd\n\n";
60 char help_last[] =
61 "* last - show last update time for RRD\n\n"
62 "\trrdtool last filename.rrd\n\n";
64 char help_update[] =
65 "* update - update an RRD\n\n"
66 "\trrdtool update filename\n"
67 "\t\t--template|-t ds-name:ds-name:...\n"
68 "\t\ttime|N:value[:value...]\n\n"
69 "\t\tat-time@value[:value...]\n\n"
70 "\t\t[ time:value[:value...] ..]\n\n";
72 char help_updatev[] =
73 "* updatev - a verbose verion of update\n"
74 "\treturns information about values, RRAs, and datasources updated\n\n"
75 "\trrdtool updatev filename\n"
76 "\t\t--template|-t ds-name:ds-name:...\n"
77 "\t\ttime|N:value[:value...]\n\n"
78 "\t\tat-time@value[:value...]\n\n"
79 "\t\t[ time:value[:value...] ..]\n\n";
81 char help_fetch[] =
82 "* fetch - fetch data out of an RRD\n\n"
83 "\trrdtool fetch filename.rrd CF\n"
84 "\t\t[--resolution|-r resolution]\n"
85 "\t\t[--start|-s start] [--end|-e end]\n\n";
87 char help_graph[] =
88 "* graph - generate a graph from one or several RRD\n\n"
89 "\trrdtool graph filename [-s|--start seconds] [-e|--end seconds]\n"
90 "\t\t[-x|--x-grid x-axis grid and label]\n"
91 "\t\t[--alt-y-grid]\n"
92 "\t\t[-y|--y-grid y-axis grid and label]\n"
93 "\t\t[-v|--vertical-label string] [-w|--width pixels]\n"
94 "\t\t[-h|--height pixels] [-o|--logarithmic]\n"
95 "\t\t[-u|--upper-limit value] [-z|--lazy]\n"
96 "\t\t[-l|--lower-limit value] [-r|--rigid]\n"
97 "\t\t[-g|--no-legend]\n"
98 "\t\t[--font FONTTAG:size:font]\n"
99 "\t\t[--zoom factor]\n"
100 "\t\t[--alt-autoscale]\n"
101 "\t\t[--alt-autoscale-max]\n"
102 "\t\t[--units-exponent value]\n"
103 "\t\t[--step seconds]\n"
104 "\t\t[-f|--imginfo printfstr]\n"
105 "\t\t[-a|--imgformat PNG]\n"
106 "\t\t[-c|--color COLORTAG#rrggbb[aa]] [-t|--title string]\n"
107 "\t\t[DEF:vname=rrd:ds-name:CF]\n"
108 "\t\t[CDEF:vname=rpn-expression]\n"
109 "\t\t[PRINT:vname:CF:format]\n"
110 "\t\t[GPRINT:vname:CF:format]\n"
111 "\t\t[HRULE:value#rrggbb[aa][:legend]]\n"
112 "\t\t[VRULE:value#rrggbb[aa][:legend]]\n"
113 "\t\t[LINE{1|2|3}:vname[#rrggbb[aa][:legend]]]\n"
114 "\t\t[AREA:vname[#rrggbb[aa][:legend]]]\n"
115 "\t\t[STACK:vname[#rrggbb[aa][:legend]]]\n\n";
117 char help_tune[] =
118 " * tune - Modify some basic properties of an RRD\n\n"
119 "\trrdtool tune filename\n"
120 "\t\t[--heartbeat|-h ds-name:heartbeat]\n"
121 "\t\t[--data-source-type|-d ds-name:DST]\n"
122 "\t\t[--data-source-rename|-r old-name:new-name]\n"
123 "\t\t[--minimum|-i ds-name:min] [--maximum|-a ds-name:max]\n"
124 "\t\t[--deltapos scale-value] [--deltaneg scale-value]\n"
125 "\t\t[--failure-threshold integer]\n"
126 "\t\t[--window-length integer]\n"
127 "\t\t[--alpha adaptation-parameter]\n"
128 "\t\t[--beta adaptation-parameter]\n"
129 "\t\t[--gamma adaptation-parameter]\n"
130 "\t\t[--gamma-deviation adaptation-parameter]\n"
131 "\t\t[--aberrant-reset ds-name]\n\n";
133 char help_resize[] =
134 " * resize - alter the lenght of one of the RRAs in an RRD\n\n"
135 "\trrdtool resize filename rranum GROW|SHRINK rows\n\n";
137 char help_xport[] =
138 "* xport - generate XML dump from one or several RRD\n\n"
139 "\trrdtool xport [-s|--start seconds] [-e|--end seconds]\n"
140 "\t\t[-m|--maxrows rows]\n"
141 "\t\t[--step seconds]\n"
142 "\t\t[DEF:vname=rrd:ds-name:CF]\n"
143 "\t\t[CDEF:vname=rpn-expression]\n"
144 "\t\t[XPORT:vname:legend]\n\n";
146 char help_quit[] =
147 " * quit - closeing a session in remote mode\n\n"
148 "\trrdtool quit\n\n";
150 char help_ls[] =
151 " * ls - lists all *.rrd files in current directory\n\n"
152 "\trrdtool ls\n\n";
154 char help_cd[] =
155 " * cd - changes the current directory\n\n"
156 "\trrdtool cd new direcotry\n\n";
158 char help_mkdir[] =
159 " * mkdir - creates a new direcotry\n\n"
160 "\trrdtool mkdir newdirecotryname\n\n";
162 char help_lic[] =
163 "RRDtool is distributed under the Terms of the GNU General\n"
164 "Public License Version 2. (www.gnu.org/copyleft/gpl.html)\n\n"
166 "For more information read the RRD manpages\n\n";
168 enum { C_NONE, C_CREATE, C_DUMP, C_INFO, C_RESTORE, C_LAST,
169 C_UPDATE, C_FETCH, C_GRAPH, C_TUNE, C_RESIZE, C_XPORT,
170 C_QUIT, C_LS, C_CD, C_MKDIR, C_UPDATEV };
172 int help_cmd = C_NONE;
174 if (cmd)
175 {
176 if (!strcmp(cmd,"create"))
177 help_cmd = C_CREATE;
178 else if (!strcmp(cmd,"dump"))
179 help_cmd = C_DUMP;
180 else if (!strcmp(cmd,"info"))
181 help_cmd = C_INFO;
182 else if (!strcmp(cmd,"restore"))
183 help_cmd = C_RESTORE;
184 else if (!strcmp(cmd,"last"))
185 help_cmd = C_LAST;
186 else if (!strcmp(cmd,"update"))
187 help_cmd = C_UPDATE;
188 else if (!strcmp(cmd,"updatev"))
189 help_cmd = C_UPDATEV;
190 else if (!strcmp(cmd,"fetch"))
191 help_cmd = C_FETCH;
192 else if (!strcmp(cmd,"graph"))
193 help_cmd = C_GRAPH;
194 else if (!strcmp(cmd,"tune"))
195 help_cmd = C_TUNE;
196 else if (!strcmp(cmd,"resize"))
197 help_cmd = C_RESIZE;
198 else if (!strcmp(cmd,"xport"))
199 help_cmd = C_XPORT;
200 else if (!strcmp(cmd,"quit"))
201 help_cmd = C_QUIT;
202 else if (!strcmp(cmd,"ls"))
203 help_cmd = C_LS;
204 else if (!strcmp(cmd,"cd"))
205 help_cmd = C_CD;
206 else if (!strcmp(cmd,"mkdir"))
207 help_cmd = C_MKDIR;
208 }
209 fputs(help_main, stdout);
210 switch (help_cmd)
211 {
212 case C_NONE:
213 fputs(help_list, stdout);
214 if (RemoteMode){
215 fputs(help_listremote, stdout);
216 }
217 break;
218 case C_CREATE:
219 fputs(help_create, stdout);
220 break;
221 case C_DUMP:
222 fputs(help_dump, stdout);
223 break;
224 case C_INFO:
225 fputs(help_info, stdout);
226 break;
227 case C_RESTORE:
228 fputs(help_restore, stdout);
229 break;
230 case C_LAST:
231 fputs(help_last, stdout);
232 break;
233 case C_UPDATE:
234 fputs(help_update, stdout);
235 break;
236 case C_UPDATEV:
237 fputs(help_updatev, stdout);
238 break;
239 case C_FETCH:
240 fputs(help_fetch, stdout);
241 break;
242 case C_GRAPH:
243 fputs(help_graph, stdout);
244 break;
245 case C_TUNE:
246 fputs(help_tune, stdout);
247 break;
248 case C_RESIZE:
249 fputs(help_resize, stdout);
250 break;
251 case C_XPORT:
252 fputs(help_xport, stdout);
253 break;
254 case C_QUIT:
255 fputs(help_quit, stdout);
256 break;
257 case C_LS:
258 fputs(help_ls, stdout);
259 break;
260 case C_CD:
261 fputs(help_cd, stdout);
262 break;
263 case C_MKDIR:
264 fputs(help_mkdir, stdout);
265 break;
266 }
267 fputs(help_lic, stdout);
268 }
271 int main(int argc, char *argv[])
272 {
273 char **myargv;
274 char aLine[MAX_LENGTH];
275 #ifdef HAVE_CHROOT
276 char *firstdir="";
277 #endif
278 #ifdef MUST_DISABLE_SIGFPE
279 signal(SIGFPE,SIG_IGN);
280 #endif
281 #ifdef MUST_DISABLE_FPMASK
282 fpsetmask(0);
283 #endif
284 if (argc == 1)
285 {
286 PrintUsage("");
287 return 0;
288 }
290 if (((argc == 2)||(argc == 3)) && !strcmp("-",argv[1]))
291 {
292 #if HAVE_GETRUSAGE
293 struct rusage myusage;
294 struct timeval starttime;
295 struct timeval currenttime;
296 struct timezone tz;
298 tz.tz_minuteswest =0;
299 tz.tz_dsttime=0;
300 gettimeofday(&starttime,&tz);
301 #endif
302 RemoteMode=1;
303 #ifdef HAVE_CHROOT
304 if ((argc == 3) && strcmp("",argv[2])){
305 if (getuid()==0){
306 chroot(argv[2]);
307 if (errno!=0){
308 fprintf(stderr,"ERROR: can't change root to '%s' errno=%d\n",
309 argv[2],errno);
310 exit(errno);
311 }
312 ChangeRoot=1;
313 firstdir="/";
314 }
315 else{
316 firstdir=argv[2];
317 }
318 }
319 if (strcmp(firstdir,"")){
320 chdir(firstdir);
321 if (errno!=0){
322 fprintf(stderr,"ERROR: %s\n",strerror(errno));
323 exit(errno);
324 }
325 }
326 #else
327 fprintf(stderr,"ERROR: change root is not supported by your OS "
328 "or at least by this copy of rrdtool\n");
329 exit(1);
330 #endif
332 while (fgets(aLine, sizeof(aLine)-1, stdin)){
333 if ((argc = CountArgs(aLine)) == 0) {
334 fprintf(stderr,"ERROR: not enough arguments\n");
335 }
336 if ((myargv = (char **) malloc((argc+1) *
337 sizeof(char *))) == NULL) {
338 perror("malloc");
339 return -1;
340 }
341 if ((argc=CreateArgs(argv[0], aLine, argc, myargv)) < 0) {
342 fprintf(stderr, "ERROR: creating arguments\n");
343 return -1;
344 }
346 if (HandleInputLine(argc, myargv, stdout))
347 return -1;
348 free(myargv);
350 #if HAVE_GETRUSAGE
351 getrusage(RUSAGE_SELF,&myusage);
352 gettimeofday(¤ttime,&tz);
353 printf("OK u:%1.2f s:%1.2f r:%1.2f\n",
354 (double)myusage.ru_utime.tv_sec+
355 (double)myusage.ru_utime.tv_usec/1000000.0,
356 (double)myusage.ru_stime.tv_sec+
357 (double)myusage.ru_stime.tv_usec/1000000.0,
358 (double)(currenttime.tv_sec-starttime.tv_sec)
359 +(double)(currenttime.tv_usec-starttime.tv_usec)
360 /1000000.0);
361 #else
362 printf("OK\n");
363 #endif
364 fflush(stdout); /* this is important for pipes to work */
365 }
366 }
367 else if (argc == 2)
368 {
369 PrintUsage(argv[1]);
370 exit(0);
371 }
372 else if (argc == 3 && !strcmp(argv[1],"help"))
373 {
374 PrintUsage(argv[2]);
375 exit(0);
376 }
377 else
378 HandleInputLine(argc, argv, stderr);
379 return 0;
380 }
382 /* HandleInputLine is NOT thread safe - due to readdir issues,
383 resolving them portably is not really simple. */
384 int HandleInputLine(int argc, char **argv, FILE* out)
385 {
386 #if defined(HAVE_OPENDIR) && defined (HAVE_READDIR)
387 DIR *curdir; /* to read current dir with ls */
388 struct dirent *dent;
389 #endif
390 #if defined(HAVE_SYS_STAT_H)
391 struct stat st;
392 #endif
393 optind=0; /* reset gnu getopt */
394 opterr=0; /* no error messages */
396 if (RemoteMode){
397 if (argc>1 && strcmp("quit", argv[1]) == 0){
398 if (argc>2){
399 printf("ERROR: invalid parameter count for quit\n");
400 return(0);
401 }
402 exit(0);
403 }
404 #if defined(HAVE_OPENDIR) && defined(HAVE_READDIR) && defined(HAVE_CHDIR)
405 if (argc>1 && strcmp("cd", argv[1]) == 0){
406 if (argc>3){
407 printf("ERROR: invalid parameter count for cd\n");
408 return(0);
409 }
410 #if ! defined(HAVE_CHROOT) || ! defined(HAVE_GETUID)
411 if (getuid()==0 && ! ChangeRoot){
412 printf("ERROR: chdir security problem - rrdtool is runnig as "
413 "root an no chroot!\n");
414 return(0);
415 }
416 #endif
417 chdir(argv[2]);
418 if (errno!=0){
419 printf("ERROR: %s\n",strerror(errno));
420 }
421 return(0);
422 }
423 if (argc>1 && strcmp("mkdir", argv[1]) == 0){
424 if (argc>3){
425 printf("ERROR: invalid parameter count for mkdir\n");
426 return(0);
427 }
428 #if ! defined(HAVE_CHROOT) || ! defined(HAVE_GETUID)
429 if (getuid()==0 && ! ChangeRoot){
430 printf("ERROR: mkdir security problem - rrdtool is runnig as "
431 "root an no chroot!\n");
432 return(0);
433 }
434 #endif
435 mkdir(argv[2],0777);
436 if (errno!=0){
437 printf("ERROR: %s\n",strerror(errno));
438 }
439 return(0);
440 }
441 if (argc>1 && strcmp("ls", argv[1]) == 0){
442 if (argc>2){
443 printf("ERROR: invalid parameter count for ls\n");
444 return(0);
445 }
446 if ((curdir=opendir("."))!=NULL){
447 while((dent=readdir(curdir))!=NULL){
448 if (!stat(dent->d_name,&st)){
449 if (S_ISDIR(st.st_mode)){
450 printf("d %s\n",dent->d_name);
451 }
452 if (strlen(dent->d_name)>4 && S_ISREG(st.st_mode)){
453 if (!strcmp(dent->d_name+NAMLEN(dent)-4,".rrd") ||
454 !strcmp(dent->d_name+NAMLEN(dent)-4,".RRD")){
455 printf("- %s\n",dent->d_name);
456 }
457 }
458 }
459 }
460 }
461 else{
462 printf("ERROR: %s\n",strerror(errno));
463 return(errno);
464 }
465 return(0);
466 }
467 #endif /* opendir and readdir */
469 }
470 if (argc < 3
471 || strcmp("help", argv[1]) == 0
472 || strcmp("--help", argv[1]) == 0
473 || strcmp("-help", argv[1]) == 0
474 || strcmp("-?", argv[1]) == 0
475 || strcmp("-h", argv[1]) == 0 ) {
476 PrintUsage("");
477 return 0;
478 }
480 if (strcmp("create", argv[1]) == 0)
481 rrd_create(argc-1, &argv[1]);
482 else if (strcmp("dump", argv[1]) == 0)
483 rrd_dump(argc-1, &argv[1]);
484 else if (strcmp("info", argv[1]) == 0
485 || strcmp("updatev", argv[1]) == 0){
486 info_t *data,*save;
487 if (strcmp("info",argv[1]) == 0)
488 data=rrd_info(argc-1, &argv[1]);
489 else
490 data=rrd_update_v(argc-1, &argv[1]);
491 while (data) {
492 save=data;
493 printf ("%s = ", data->key);
494 free(data->key);
496 switch (data->type) {
497 case RD_I_VAL:
498 if (isnan (data->value.u_val))
499 printf("NaN");
500 else
501 printf ("%0.10e", data->value.u_val);
502 break;
503 case RD_I_CNT:
504 printf ("%lu", data->value.u_cnt);
505 break;
506 case RD_I_INT:
507 printf ("%d", data->value.u_int);
508 break;
509 case RD_I_STR:
510 printf ("\"%s\"", data->value.u_str);
511 free(data->value.u_str);
512 break;
513 }
514 data = data->next;
515 free(save);
516 printf ("\n");
517 }
518 free(data);
519 }
521 else if (strcmp("--version", argv[1]) == 0 ||
522 strcmp("version", argv[1]) == 0 ||
523 strcmp("v", argv[1]) == 0 ||
524 strcmp("-v", argv[1]) == 0 ||
525 strcmp("-version", argv[1]) == 0 )
526 printf("RRDtool 1.1.x Copyright (C) 1997-2003 by Tobias Oetiker <tobi@oetiker.ch>\n");
527 else if (strcmp("restore", argv[1]) == 0)
528 rrd_restore(argc-1, &argv[1]);
529 else if (strcmp("resize", argv[1]) == 0)
530 rrd_resize(argc-1, &argv[1]);
531 else if (strcmp("last", argv[1]) == 0)
532 printf("%ld\n",rrd_last(argc-1, &argv[1]));
533 else if (strcmp("update", argv[1]) == 0)
534 rrd_update(argc-1, &argv[1]);
535 else if (strcmp("fetch", argv[1]) == 0) {
536 time_t start,end, ti;
537 unsigned long step, ds_cnt,i,ii;
538 rrd_value_t *data,*datai;
539 char **ds_namv;
540 if (rrd_fetch(argc-1, &argv[1],&start,&end,&step,&ds_cnt,&ds_namv,&data) != -1) {
541 datai=data;
542 printf(" ");
543 for (i = 0; i<ds_cnt;i++)
544 printf("%20s",ds_namv[i]);
545 printf ("\n\n");
546 for (ti = start+step; ti <= end; ti += step){
547 printf("%10lu:", ti);
548 for (ii = 0; ii < ds_cnt; ii++)
549 printf(" %0.10e", *(datai++));
550 printf("\n");
551 }
552 for (i=0;i<ds_cnt;i++)
553 free(ds_namv[i]);
554 free(ds_namv);
555 free (data);
556 }
557 } else if (strcmp("xport", argv[1]) == 0) {
558 int xxsize;
559 unsigned long int j = 0;
560 time_t start,end, ti;
561 unsigned long step, col_cnt,row_cnt;
562 rrd_value_t *data,*ptr;
563 char **legend_v;
564 if(rrd_xport(argc-1, &argv[1], &xxsize,&start,&end,&step,&col_cnt,&legend_v,&data) != -1) {
565 row_cnt = (end-start)/step;
566 ptr = data;
567 printf("<?xml version=\"1.0\" encoding=\"%s\"?>\n\n", XML_ENCODING);
568 printf("<%s>\n", ROOT_TAG);
569 printf(" <%s>\n", META_TAG);
570 printf(" <%s>%lu</%s>\n", META_START_TAG, start+step, META_START_TAG);
571 printf(" <%s>%lu</%s>\n", META_STEP_TAG, step, META_STEP_TAG);
572 printf(" <%s>%lu</%s>\n", META_END_TAG, end, META_END_TAG);
573 printf(" <%s>%lu</%s>\n", META_ROWS_TAG, row_cnt, META_ROWS_TAG);
574 printf(" <%s>%lu</%s>\n", META_COLS_TAG, col_cnt, META_COLS_TAG);
575 printf(" <%s>\n", LEGEND_TAG);
576 for (j = 0; j < col_cnt; j++) {
577 char *entry = NULL;
578 entry = legend_v[j];
579 printf(" <%s>%s</%s>\n", LEGEND_ENTRY_TAG, entry, LEGEND_ENTRY_TAG);
580 free(entry);
581 }
582 free(legend_v);
583 printf(" </%s>\n", LEGEND_TAG);
584 printf(" </%s>\n", META_TAG);
585 printf(" <%s>\n", DATA_TAG);
586 for (ti = start+step; ti <= end; ti += step) {
587 printf (" <%s>", DATA_ROW_TAG);
588 printf ("<%s>%lu</%s>", COL_TIME_TAG, ti, COL_TIME_TAG);
589 for (j = 0; j < col_cnt; j++) {
590 rrd_value_t newval = DNAN;
591 newval = *ptr;
592 if(isnan(newval)){
593 printf("<%s>NaN</%s>", COL_DATA_TAG, COL_DATA_TAG);
594 } else {
595 printf("<%s>%0.10e</%s>", COL_DATA_TAG, newval, COL_DATA_TAG);
596 };
597 ptr++;
598 }
599 printf("</%s>\n", DATA_ROW_TAG);
600 }
601 free(data);
602 printf(" </%s>\n", DATA_TAG);
603 printf("</%s>\n", ROOT_TAG);
604 }
605 }
606 else if (strcmp("graph", argv[1]) == 0) {
607 char **calcpr;
608 const char *imgfile = argv[2]; /* rrd_graph changes argv pointer */
609 int xsize, ysize;
610 int i;
611 int tostdout = (strcmp(argv[2],"-") == 0);
612 if( rrd_graph(argc-1, &argv[1], &calcpr, &xsize, &ysize) != -1 ) {
613 if (!tostdout)
614 printf ("%dx%d\n",xsize,ysize);
615 if (calcpr) {
616 for(i=0;calcpr[i];i++){
617 if (!tostdout)
618 printf("%s\n",calcpr[i]);
619 free(calcpr[i]);
620 }
621 free(calcpr);
622 }
623 }
625 } else if (strcmp("tune", argv[1]) == 0)
626 rrd_tune(argc-1, &argv[1]);
627 else {
628 rrd_set_error("unknown function '%s'",argv[1]);
629 }
630 if (rrd_test_error()) {
631 fprintf(out, "ERROR: %s\n",rrd_get_error());
632 rrd_clear_error();
633 }
634 return(0);
635 }
637 int CountArgs(char *aLine)
638 {
639 int i=0;
640 int aCount = 0;
641 int inarg = 0;
642 while (aLine[i] == ' ') i++;
643 while (aLine[i] != 0){
644 if((aLine[i]== ' ') && inarg){
645 inarg = 0;
646 }
647 if((aLine[i]!= ' ') && ! inarg){
648 inarg = 1;
649 aCount++;
650 }
651 i++;
652 }
653 return aCount;
654 }
656 /*
657 * CreateArgs - take a string (aLine) and tokenize
658 */
659 int CreateArgs(char *pName, char *aLine, int argc, char **argv)
660 {
661 char *getP, *putP;
662 char **pargv = argv;
663 char Quote = 0;
664 int inArg = 0;
665 int len;
667 len = strlen(aLine);
668 /* remove trailing space and newlines */
669 while (len && aLine[len] <= ' ') {
670 aLine[len] = 0 ; len--;
671 }
672 /* sikp leading blanks */
673 while (*aLine && *aLine <= ' ') aLine++;
675 pargv[0] = pName;
676 argc = 1;
677 getP = aLine;
678 putP = aLine;
679 while (*getP){
680 switch (*getP) {
681 case ' ':
682 if (Quote){
683 *(putP++)=*getP;
684 } else
685 if(inArg) {
686 *(putP++) = 0;
687 inArg = 0;
688 }
689 break;
690 case '"':
691 case '\'':
692 if (Quote != 0) {
693 if (Quote == *getP)
694 Quote = 0;
695 else {
696 *(putP++)=*getP;
697 }
698 } else {
699 if(!inArg){
700 pargv[argc++] = putP;
701 inArg=1;
702 }
703 Quote = *getP;
704 }
705 break;
706 default:
707 if(!inArg){
708 pargv[argc++] = putP;
709 inArg=1;
710 }
711 *(putP++)=*getP;
712 break;
713 }
714 getP++;
715 }
717 *putP = '\0';
719 if (Quote)
720 return -1;
721 else
722 return argc;
723 }