Code

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