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