Code

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