Code

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