fix right aligned text
[rrdtool.git] / src / rrd_tool.c
1 /*****************************************************************************
2  * RRDtool 1.4.2  Copyright by Tobi Oetiker, 1997-2009
3  *****************************************************************************
4  * rrd_tool.c  Startup wrapper
5  *****************************************************************************/
6
7 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) && !defined(HAVE_CONFIG_H)
8 #include "../win32/config.h"
9 #include <stdlib.h>
10 #include <sys/stat.h>
11 #else
12 #ifdef HAVE_CONFIG_H
13 #include "../rrd_config.h"
14 #endif
15 #endif
16
17 #include "rrd_tool.h"
18 #include "rrd_xport.h"
19 #include "rrd_i18n.h"
20
21 #include <locale.h>
22
23
24 void      PrintUsage(
25     char *cmd);
26 int       CountArgs(
27     char *aLine);
28 int       CreateArgs(
29     char *,
30     char *,
31     char **);
32 int       HandleInputLine(
33     int,
34     char **,
35     FILE *);
36 int       RemoteMode = 0;
37 int       ChangeRoot = 0;
38
39 #define TRUE            1
40 #define FALSE           0
41 #define MAX_LENGTH      10000
42
43
44 void PrintUsage(
45     char *cmd)
46 {
47
48     const char *help_main =
49         N_("RRDtool %s"
50            "  Copyright 1997-2009 by Tobias Oetiker <tobi@oetiker.ch>\n"
51            "               Compiled %s %s\n\n"
52            "Usage: rrdtool [options] command command_options\n");
53
54     const char *help_list =
55         N_
56         ("Valid commands: create, update, updatev, graph, graphv,  dump, restore,\n"
57          "\t\tlast, lastupdate, first, info, fetch, tune,\n"
58          "\t\tresize, xport, flushcached\n");
59
60     const char *help_listremote =
61         N_("Valid remote commands: quit, ls, cd, mkdir, pwd\n");
62
63
64     const char *help_create =
65         N_("* create - create a new RRD\n\n"
66            "\trrdtool create filename [--start|-b start time]\n"
67            "\t\t[--step|-s step]\n"
68            "\t\t[DS:ds-name:DST:dst arguments]\n"
69            "\t\t[RRA:CF:cf arguments]\n");
70
71     const char *help_dump =
72         N_("* dump - dump an RRD to XML\n\n"
73            "\trrdtool dump filename.rrd >filename.xml\n");
74
75     const char *help_info =
76         N_("* info - returns the configuration and status of the RRD\n\n"
77            "\trrdtool info filename.rrd\n");
78
79     const char *help_restore =
80         N_("* restore - restore an RRD file from its XML form\n\n"
81            "\trrdtool restore [--range-check|-r] [--force-overwrite|-f] filename.xml filename.rrd\n");
82
83     const char *help_last =
84         N_("* last - show last update time for RRD\n\n"
85            "\trrdtool last filename.rrd\n");
86
87     const char *help_lastupdate =
88         N_("* lastupdate - returns the most recent datum stored for\n"
89            "  each DS in an RRD\n\n" "\trrdtool lastupdate filename.rrd\n");
90
91     const char *help_first =
92         N_("* first - show first update time for RRA within an RRD\n\n"
93            "\trrdtool first filename.rrd [--rraindex number]\n");
94
95     const char *help_update =
96         N_("* update - update an RRD\n\n"
97            "\trrdtool update filename\n"
98            "\t\t[--template|-t ds-name:ds-name:...]\n"
99            "\t\t[--daemon <address>]\n"
100            "\t\ttime|N:value[:value...]\n\n"
101            "\t\tat-time@value[:value...]\n\n"
102            "\t\t[ time:value[:value...] ..]\n");
103
104     const char *help_updatev =
105         N_("* updatev - a verbose version of update\n"
106            "\treturns information about values, RRAs, and datasources updated\n\n"
107            "\trrdtool updatev filename\n"
108            "\t\t[--template|-t ds-name:ds-name:...]\n"
109            "\t\ttime|N:value[:value...]\n\n"
110            "\t\tat-time@value[:value...]\n\n"
111            "\t\t[ time:value[:value...] ..]\n");
112
113     const char *help_fetch =
114         N_("* fetch - fetch data out of an RRD\n\n"
115            "\trrdtool fetch filename.rrd CF\n"
116            "\t\t[-r|--resolution resolution]\n"
117            "\t\t[-s|--start start] [-e|--end end]\n"
118            "\t\t[--daemon <address>]\n");
119
120     const char *help_flushcached =
121         N_("* flushcached - flush cached data out to an RRD file\n\n"
122            "\trrdtool flushcached filename.rrd\n"
123            "\t\t[--daemon <address>]\n");
124
125 /* break up very large strings (help_graph, help_tune) for ISO C89 compliance*/
126
127     const char *help_graph0 =
128         N_("* graph - generate a graph from one or several RRD\n\n"
129            "\trrdtool graph filename [-s|--start seconds] [-e|--end seconds]\n");
130     const char *help_graphv0 =
131         N_("* graphv - generate a graph from one or several RRD\n"
132            "           with meta data printed before the graph\n\n"
133            "\trrdtool graphv filename [-s|--start seconds] [-e|--end seconds]\n");
134     const char *help_graph1 =
135         N_("\t\t[-x|--x-grid x-axis grid and label]\n"
136            "\t\t[-Y|--alt-y-grid] [--full-size-mode]\n"
137            "\t\t[-y|--y-grid y-axis grid and label]\n"
138            "\t\t[-v|--vertical-label string] [-w|--width pixels]\n"
139            "\t\t[--right-axis scale:shift] [--right-axis-label label]\n"
140            "\t\t[--right-axis-format format]\n"
141            "\t\t[-h|--height pixels] [-o|--logarithmic]\n"
142            "\t\t[-u|--upper-limit value] [-z|--lazy]\n"
143            "\t\t[-l|--lower-limit value] [-r|--rigid]\n"
144            "\t\t[-g|--no-legend] [--daemon <address>]\n"
145            "\t\t[-F|--force-rules-legend]\n" "\t\t[-j|--only-graph]\n");
146     const char *help_graph2 =
147         N_("\t\t[-n|--font FONTTAG:size:font]\n"
148            "\t\t[-m|--zoom factor]\n"
149            "\t\t[-A|--alt-autoscale]\n"
150            "\t\t[-M|--alt-autoscale-max]\n"
151            "\t\t[-G|--graph-render-mode {normal,mono}]\n"
152            "\t\t[-R|--font-render-mode {normal,light,mono}]\n"
153            "\t\t[-B|--font-smoothing-threshold size]\n"
154            "\t\t[-T|--tabwidth width]\n"
155            "\t\t[-E|--slope-mode]\n"
156            "\t\t[-P|--pango-markup]\n"
157            "\t\t[-N|--no-gridfit]\n"
158            "\t\t[-X|--units-exponent value]\n"
159            "\t\t[-L|--units-length value]\n"
160            "\t\t[-S|--step seconds]\n"
161            "\t\t[-f|--imginfo printfstr]\n"
162            "\t\t[-a|--imgformat PNG]\n"
163            "\t\t[-c|--color COLORTAG#rrggbb[aa]]\n"
164            "\t\t[--border width\n"
165            "\t\t[-t|--title string]\n"
166            "\t\t[-W|--watermark string]\n"
167            "\t\t[DEF:vname=rrd:ds-name:CF]\n");
168     const char *help_graph3 =
169         N_("\t\t[CDEF:vname=rpn-expression]\n"
170            "\t\t[VDEF:vdefname=rpn-expression]\n"
171            "\t\t[PRINT:vdefname:format]\n"
172            "\t\t[GPRINT:vdefname:format]\n" "\t\t[COMMENT:text]\n"
173            "\t\t[SHIFT:vname:offset]\n"
174            "\t\t[TEXTALIGN:{left|right|justified|center}]\n"
175            "\t\t[TICK:vname#rrggbb[aa][:[fraction][:legend]]]\n"
176            "\t\t[HRULE:value#rrggbb[aa][:legend]]\n"
177            "\t\t[VRULE:value#rrggbb[aa][:legend]]\n"
178            "\t\t[LINE[width]:vname[#rrggbb[aa][:[legend][:STACK]]]]\n"
179            "\t\t[AREA:vname[#rrggbb[aa][:[legend][:STACK]]]]\n"
180            "\t\t[PRINT:vname:CF:format] (deprecated)\n"
181            "\t\t[GPRINT:vname:CF:format] (deprecated)\n"
182            "\t\t[STACK:vname[#rrggbb[aa][:legend]]] (deprecated)\n");
183     const char *help_tune1 =
184         N_(" * tune -  Modify some basic properties of an RRD\n\n"
185            "\trrdtool tune filename\n"
186            "\t\t[--heartbeat|-h ds-name:heartbeat]\n"
187            "\t\t[--data-source-type|-d ds-name:DST]\n"
188            "\t\t[--data-source-rename|-r old-name:new-name]\n"
189            "\t\t[--minimum|-i ds-name:min] [--maximum|-a ds-name:max]\n"
190            "\t\t[--deltapos scale-value] [--deltaneg scale-value]\n"
191            "\t\t[--failure-threshold integer]\n"
192            "\t\t[--window-length integer]\n"
193            "\t\t[--alpha adaptation-parameter]\n");
194     const char *help_tune2 =
195         N_("\t\t[--beta adaptation-parameter]\n"
196            "\t\t[--gamma adaptation-parameter]\n"
197            "\t\t[--gamma-deviation adaptation-parameter]\n"
198            "\t\t[--aberrant-reset ds-name]\n");
199     const char *help_resize =
200         N_
201         (" * resize - alter the length of one of the RRAs in an RRD\n\n"
202          "\trrdtool resize filename rranum GROW|SHRINK rows\n");
203     const char *help_xport =
204         N_("* xport - generate XML dump from one or several RRD\n\n"
205            "\trrdtool xport [-s|--start seconds] [-e|--end seconds]\n"
206            "\t\t[-m|--maxrows rows]\n" "\t\t[--step seconds]\n"
207            "\t\t[--enumds]\n" "\t\t[DEF:vname=rrd:ds-name:CF]\n"
208            "\t\t[CDEF:vname=rpn-expression]\n"
209            "\t\t[XPORT:vname:legend]\n");
210     const char *help_quit =
211         N_(" * quit - closing a session in remote mode\n\n"
212            "\trrdtool quit\n");
213     const char *help_ls =
214         N_(" * ls - lists all *.rrd files in current directory\n\n"
215            "\trrdtool ls\n");
216     const char *help_cd =
217         N_(" * cd - changes the current directory\n\n"
218            "\trrdtool cd new directory\n");
219     const char *help_mkdir =
220         N_(" * mkdir - creates a new directory\n\n"
221            "\trrdtool mkdir newdirectoryname\n");
222     const char *help_pwd =
223         N_(" * pwd - returns the current working directory\n\n"
224            "\trrdtool pwd\n");
225     const char *help_lic =
226         N_("RRDtool is distributed under the Terms of the GNU General\n"
227            "Public License Version 2. (www.gnu.org/copyleft/gpl.html)\n\n"
228            "For more information read the RRD manpages\n");
229     enum { C_NONE, C_CREATE, C_DUMP, C_INFO, C_RESTORE, C_LAST,
230         C_LASTUPDATE, C_FIRST, C_UPDATE, C_FETCH, C_GRAPH, C_GRAPHV,
231         C_TUNE,
232         C_RESIZE, C_XPORT, C_QUIT, C_LS, C_CD, C_MKDIR, C_PWD,
233         C_UPDATEV, C_FLUSHCACHED
234     };
235     int       help_cmd = C_NONE;
236
237     if (cmd) {
238         if (!strcmp(cmd, "create"))
239             help_cmd = C_CREATE;
240         else if (!strcmp(cmd, "dump"))
241             help_cmd = C_DUMP;
242         else if (!strcmp(cmd, "info"))
243             help_cmd = C_INFO;
244         else if (!strcmp(cmd, "restore"))
245             help_cmd = C_RESTORE;
246         else if (!strcmp(cmd, "last"))
247             help_cmd = C_LAST;
248         else if (!strcmp(cmd, "lastupdate"))
249             help_cmd = C_LASTUPDATE;
250         else if (!strcmp(cmd, "first"))
251             help_cmd = C_FIRST;
252         else if (!strcmp(cmd, "update"))
253             help_cmd = C_UPDATE;
254         else if (!strcmp(cmd, "updatev"))
255             help_cmd = C_UPDATEV;
256         else if (!strcmp(cmd, "fetch"))
257             help_cmd = C_FETCH;
258         else if (!strcmp(cmd, "flushcached"))
259             help_cmd = C_FLUSHCACHED;
260         else if (!strcmp(cmd, "graph"))
261             help_cmd = C_GRAPH;
262         else if (!strcmp(cmd, "graphv"))
263             help_cmd = C_GRAPHV;
264         else if (!strcmp(cmd, "tune"))
265             help_cmd = C_TUNE;
266         else if (!strcmp(cmd, "resize"))
267             help_cmd = C_RESIZE;
268         else if (!strcmp(cmd, "xport"))
269             help_cmd = C_XPORT;
270         else if (!strcmp(cmd, "quit"))
271             help_cmd = C_QUIT;
272         else if (!strcmp(cmd, "ls"))
273             help_cmd = C_LS;
274         else if (!strcmp(cmd, "cd"))
275             help_cmd = C_CD;
276         else if (!strcmp(cmd, "mkdir"))
277             help_cmd = C_MKDIR;
278         else if (!strcmp(cmd, "pwd"))
279             help_cmd = C_PWD;
280     }
281     fprintf(stdout, _(help_main), PACKAGE_VERSION, __DATE__, __TIME__);
282     fflush(stdout);
283     switch (help_cmd) {
284     case C_NONE:
285         puts(_(help_list));
286         if (RemoteMode) {
287             puts(_(help_listremote));
288         }
289         break;
290     case C_CREATE:
291         puts(_(help_create));
292         break;
293     case C_DUMP:
294         puts(_(help_dump));
295         break;
296     case C_INFO:
297         puts(_(help_info));
298         break;
299     case C_RESTORE:
300         puts(_(help_restore));
301         break;
302     case C_LAST:
303         puts(_(help_last));
304         break;
305     case C_LASTUPDATE:
306         puts(_(help_lastupdate));
307         break;
308     case C_FIRST:
309         puts(_(help_first));
310         break;
311     case C_UPDATE:
312         puts(_(help_update));
313         break;
314     case C_UPDATEV:
315         puts(_(help_updatev));
316         break;
317     case C_FETCH:
318         puts(_(help_fetch));
319         break;
320     case C_FLUSHCACHED:
321         puts(_(help_flushcached));
322         break;
323     case C_GRAPH:
324         puts(_(help_graph0));
325         puts(_(help_graph1));
326         puts(_(help_graph2));
327         puts(_(help_graph3));
328         break;
329     case C_GRAPHV:
330         puts(_(help_graphv0));
331         puts(_(help_graph1));
332         puts(_(help_graph2));
333         puts(_(help_graph3));
334         break;
335     case C_TUNE:
336         puts(_(help_tune1));
337         puts(_(help_tune2));
338         break;
339     case C_RESIZE:
340         puts(_(help_resize));
341         break;
342     case C_XPORT:
343         puts(_(help_xport));
344         break;
345     case C_QUIT:
346         puts(_(help_quit));
347         break;
348     case C_LS:
349         puts(_(help_ls));
350         break;
351     case C_CD:
352         puts(_(help_cd));
353         break;
354     case C_MKDIR:
355         puts(_(help_mkdir));
356         break;
357     case C_PWD:
358         puts(_(help_pwd));
359         break;
360     }
361     puts(_(help_lic));
362 }
363
364 static char *fgetslong(
365     char **aLinePtr,
366     FILE * stream)
367 {
368     char     *linebuf;
369     size_t    bufsize = MAX_LENGTH;
370     int       eolpos = 0;
371
372     if (feof(stream))
373         return *aLinePtr = 0;
374     if (!(linebuf = malloc(bufsize))) {
375         perror("fgetslong: malloc");
376         exit(1);
377     }
378     linebuf[0] = '\0';
379     while (fgets(linebuf + eolpos, MAX_LENGTH, stream)) {
380         eolpos += strlen(linebuf + eolpos);
381         if (linebuf[eolpos - 1] == '\n')
382             return *aLinePtr = linebuf;
383         bufsize += MAX_LENGTH;
384         if (!(linebuf = realloc(linebuf, bufsize))) {
385             free(linebuf);
386             perror("fgetslong: realloc");
387             exit(1);
388         }
389     }
390     if (linebuf[0]){
391         return  *aLinePtr = linebuf;
392     }
393     free(linebuf);
394     return *aLinePtr = 0;
395 }
396
397 int main(
398     int argc,
399     char *argv[])
400 {
401     char    **myargv;
402     char     *aLine;
403     char     *firstdir = "";
404
405 #ifdef MUST_DISABLE_SIGFPE
406     signal(SIGFPE, SIG_IGN);
407 #endif
408 #ifdef MUST_DISABLE_FPMASK
409     fpsetmask(0);
410 #endif
411
412     /* initialize locale settings
413        according to localeconv(3) */       
414     setlocale(LC_ALL, "");
415
416 #if defined(HAVE_LIBINTL_H) && defined(BUILD_LIBINTL)
417     bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
418     textdomain(GETTEXT_PACKAGE);
419 #endif
420     if (argc == 1) {
421         PrintUsage("");
422         return 0;
423     }
424
425     if (((argc == 2) || (argc == 3)) && !strcmp("-", argv[1])) {
426 #if HAVE_GETRUSAGE
427         struct rusage myusage;
428         struct timeval starttime;
429         struct timeval currenttime;
430
431         gettimeofday(&starttime, NULL);
432 #endif
433         RemoteMode = 1;
434         if ((argc == 3) && strcmp("", argv[2])) {
435
436             if (
437 #ifdef HAVE_GETUID
438                    getuid()
439 #else
440                    1
441 #endif
442                    == 0) {
443
444 #ifdef HAVE_CHROOT
445                 if (chroot(argv[2]) != 0){
446                     fprintf(stderr, "ERROR: chroot %s: %s\n", argv[2],rrd_strerror(errno));
447                     exit(errno);
448                 }
449                 ChangeRoot = 1;
450                 firstdir = "/";
451 #else
452                 fprintf(stderr,
453                         "ERROR: change root is not supported by your OS "
454                         "or at least by this copy of rrdtool\n");
455                 exit(1);
456 #endif
457             } else {
458                 firstdir = argv[2];
459             }
460         }
461         if (strcmp(firstdir, "")) {
462             if (chdir(firstdir) != 0){
463                 fprintf(stderr, "ERROR: chdir %s %s\n", firstdir,rrd_strerror(errno));
464                 exit(errno);
465             }
466         }
467
468         while (fgetslong(&aLine, stdin)) {
469             char *aLineOrig = aLine;
470             if ((argc = CountArgs(aLine)) == 0) {
471                 free(aLine);
472                 printf("ERROR: not enough arguments\n");
473                 continue;                
474             }
475             if ((myargv = (char **) malloc((argc + 1) *
476                                            sizeof(char *))) == NULL) {
477                 perror("malloc");
478                 exit(1);
479             }
480             if ((argc = CreateArgs(argv[0], aLine, myargv)) < 0) {
481                 printf("ERROR: creating arguments\n");
482             } else {
483                 if ( HandleInputLine(argc, myargv, stdout) == 0 ){
484 #if HAVE_GETRUSAGE
485                     getrusage(RUSAGE_SELF, &myusage);
486                     gettimeofday(&currenttime, NULL);
487                     printf("OK u:%1.2f s:%1.2f r:%1.2f\n",
488                            (double) myusage.ru_utime.tv_sec +
489                            (double) myusage.ru_utime.tv_usec / 1000000.0,
490                            (double) myusage.ru_stime.tv_sec +
491                            (double) myusage.ru_stime.tv_usec / 1000000.0,
492                            (double) (currenttime.tv_sec - starttime.tv_sec)
493                            + (double) (currenttime.tv_usec -
494                                        starttime.tv_usec)
495                            / 1000000.0);
496 #else
497                     printf("OK\n");
498 #endif
499                 }
500             }
501             fflush(stdout); /* this is important for pipes to work */
502             free(myargv);
503             free(aLineOrig);
504         }
505     } else if (argc == 2) {
506         PrintUsage(argv[1]);
507         exit(0);
508     } else if (argc == 3 && !strcmp(argv[1], "help")) {
509         PrintUsage(argv[2]);
510         exit(0);
511     } else {
512         exit(HandleInputLine(argc, argv, stderr));
513     }
514     return 0;
515 }
516
517 /* HandleInputLine is NOT thread safe - due to readdir issues,
518    resolving them portably is not really simple. */
519 int HandleInputLine(
520     int argc,
521     char **argv,
522     FILE * out)
523 {
524 #if defined(HAVE_OPENDIR) && defined (HAVE_READDIR)
525     DIR      *curdir;   /* to read current dir with ls */
526     struct dirent *dent;
527 #endif
528 #if defined(HAVE_SYS_STAT_H)
529     struct stat st;
530 #endif
531
532     /* Reset errno to 0 before we start.
533      */
534     if (RemoteMode) {
535         if (argc > 1 && strcmp("quit", argv[1]) == 0) {
536             if (argc > 2) {
537                 printf("ERROR: invalid parameter count for quit\n");
538                 return (1);
539             }
540             exit(0);
541         }
542 #if defined(HAVE_OPENDIR) && defined(HAVE_READDIR) && defined(HAVE_CHDIR)
543         if (argc > 1 && strcmp("cd", argv[1]) == 0) {
544             if (argc > 3) {
545                 printf("ERROR: invalid parameter count for cd\n");
546                 return (1);
547             }
548 #if ! defined(HAVE_CHROOT) || ! defined(HAVE_GETUID)
549             if (getuid() == 0 && !ChangeRoot) {
550                 printf
551                     ("ERROR: chdir security problem - rrdtool is running as "
552                      "root but not chroot!\n");
553                 return (1);
554             }
555 #endif
556             if (chdir(argv[2]) != 0){
557                 printf("ERROR: chdir %s %s\n", argv[2], rrd_strerror(errno));
558                 return (1);
559             }
560             return (0);
561         }
562         if (argc > 1 && strcmp("pwd", argv[1]) == 0) {
563             char     *cwd;      /* To hold current working dir on call to pwd */
564             if (argc > 2) {
565                 printf("ERROR: invalid parameter count for pwd\n");
566                 return (1);
567             }
568             cwd = getcwd(NULL, MAXPATH);
569             if (cwd == NULL) {
570                 printf("ERROR: getcwd %s\n", rrd_strerror(errno));
571                 return (1);
572             }
573             printf("%s\n", cwd);
574             free(cwd);
575             return (0);
576         }
577         if (argc > 1 && strcmp("mkdir", argv[1]) == 0) {
578             if (argc > 3) {
579                 printf("ERROR: invalid parameter count for mkdir\n");
580                 return (1);
581             }
582 #if ! defined(HAVE_CHROOT) || ! defined(HAVE_GETUID)
583             if (getuid() == 0 && !ChangeRoot) {
584                 printf
585                     ("ERROR: mkdir security problem - rrdtool is running as "
586                      "root but not chroot!\n");
587                 return (1);
588             }
589 #endif
590             if(mkdir(argv[2], 0777)!=0){
591                 printf("ERROR: mkdir %s: %s\n", argv[2],rrd_strerror(errno));
592                 return (1);
593             }
594             return (0);
595         }
596         if (argc > 1 && strcmp("ls", argv[1]) == 0) {
597             if (argc > 2) {
598                 printf("ERROR: invalid parameter count for ls\n");
599                 return (1);
600             }
601             if ((curdir = opendir(".")) != NULL) {
602                 while ((dent = readdir(curdir)) != NULL) {
603                     if (!stat(dent->d_name, &st)) {
604                         if (S_ISDIR(st.st_mode)) {
605                             printf("d %s\n", dent->d_name);
606                         }
607                         if (strlen(dent->d_name) > 4 && S_ISREG(st.st_mode)) {
608                             if (!strcmp
609                                 (dent->d_name + NAMLEN(dent) - 4, ".rrd")
610                                 || !strcmp(dent->d_name + NAMLEN(dent) - 4,
611                                            ".RRD")) {
612                                 printf("- %s\n", dent->d_name);
613                             }
614                         }
615                     }
616                 }
617                 closedir(curdir);
618             } else {
619                 printf("ERROR: opendir .: %s\n", rrd_strerror(errno));
620                 return (errno);
621             }
622             return (0);
623         }
624 #endif                          /* opendir and readdir */
625
626     }
627     if (argc < 3
628         || strcmp("help", argv[1]) == 0
629         || strcmp("--help", argv[1]) == 0
630         || strcmp("-help", argv[1]) == 0
631         || strcmp("-?", argv[1]) == 0 || strcmp("-h", argv[1]) == 0) {
632         PrintUsage("");
633         return 0;
634     }
635
636     if (strcmp("create", argv[1]) == 0)
637         rrd_create(argc - 1, &argv[1]);
638     else if (strcmp("dump", argv[1]) == 0)
639         rrd_dump(argc - 1, &argv[1]);
640     else if (strcmp("info", argv[1]) == 0 || strcmp("updatev", argv[1]) == 0) {
641         rrd_info_t *data;
642
643         if (strcmp("info", argv[1]) == 0)
644
645             data = rrd_info(argc - 1, &argv[1]);
646         else
647             data = rrd_update_v(argc - 1, &argv[1]);
648         rrd_info_print(data);
649         rrd_info_free(data);
650     }
651
652     else if (strcmp("--version", argv[1]) == 0 ||
653              strcmp("version", argv[1]) == 0 ||
654              strcmp("v", argv[1]) == 0 ||
655              strcmp("-v", argv[1]) == 0 || strcmp("-version", argv[1]) == 0)
656         printf("RRDtool " PACKAGE_VERSION
657                "  Copyright by Tobi Oetiker, 1997-2008 (%f)\n",
658                rrd_version());
659     else if (strcmp("restore", argv[1]) == 0)
660         rrd_restore(argc - 1, &argv[1]);
661     else if (strcmp("resize", argv[1]) == 0)
662         rrd_resize(argc - 1, &argv[1]);
663     else if (strcmp("last", argv[1]) == 0)
664         printf("%ld\n", rrd_last(argc - 1, &argv[1]));
665     else if (strcmp("lastupdate", argv[1]) == 0) {
666         rrd_lastupdate(argc - 1, &argv[1]);
667     } else if (strcmp("first", argv[1]) == 0)
668         printf("%ld\n", rrd_first(argc - 1, &argv[1]));
669     else if (strcmp("update", argv[1]) == 0)
670         rrd_update(argc - 1, &argv[1]);
671     else if (strcmp("fetch", argv[1]) == 0) {
672         time_t    start, end, ti;
673         unsigned long step, ds_cnt, i, ii;
674         rrd_value_t *data, *datai;
675         char    **ds_namv;
676
677         if (rrd_fetch
678             (argc - 1, &argv[1], &start, &end, &step, &ds_cnt, &ds_namv,
679              &data) != -1) {
680             datai = data;
681             printf("           ");
682             for (i = 0; i < ds_cnt; i++)
683                 printf("%20s", ds_namv[i]);
684             printf("\n\n");
685             for (ti = start + step; ti <= end; ti += step) {
686                 printf("%10lu:", ti);
687                 for (ii = 0; ii < ds_cnt; ii++)
688                     printf(" %0.10e", *(datai++));
689                 printf("\n");
690             }
691             for (i = 0; i < ds_cnt; i++)
692                 free(ds_namv[i]);
693             free(ds_namv);
694             free(data);
695         }
696     } else if (strcmp("xport", argv[1]) == 0) {
697         int       xxsize;
698         unsigned long int j = 0;
699         time_t    start, end, ti;
700         unsigned long step, col_cnt, row_cnt;
701         rrd_value_t *data, *ptr;
702         char    **legend_v;
703         int       enumds = 0;
704         int       i;
705         size_t    vtag_s = strlen(COL_DATA_TAG) + 10;
706         char     *vtag = malloc(vtag_s);
707
708         for (i = 2; i < argc; i++) {
709             if (strcmp("--enumds", argv[i]) == 0)
710                 enumds = 1;
711         }
712
713         if (rrd_xport
714             (argc - 1, &argv[1], &xxsize, &start, &end, &step, &col_cnt,
715              &legend_v, &data) != -1) {
716             char *old_locale = setlocale(LC_NUMERIC, "C");
717             row_cnt = (end - start) / step;
718             ptr = data;
719             printf("<?xml version=\"1.0\" encoding=\"%s\"?>\n\n",
720                    XML_ENCODING);
721             printf("<%s>\n", ROOT_TAG);
722             printf("  <%s>\n", META_TAG);
723             printf("    <%s>%lu</%s>\n", META_START_TAG,
724                    (unsigned long) start + step, META_START_TAG);
725             printf("    <%s>%lu</%s>\n", META_STEP_TAG, step, META_STEP_TAG);
726             printf("    <%s>%lu</%s>\n", META_END_TAG, (unsigned long) end,
727                    META_END_TAG);
728             printf("    <%s>%lu</%s>\n", META_ROWS_TAG, row_cnt,
729                    META_ROWS_TAG);
730             printf("    <%s>%lu</%s>\n", META_COLS_TAG, col_cnt,
731                    META_COLS_TAG);
732             printf("    <%s>\n", LEGEND_TAG);
733             for (j = 0; j < col_cnt; j++) {
734                 char     *entry = NULL;
735
736                 entry = legend_v[j];
737                 printf("      <%s>%s</%s>\n", LEGEND_ENTRY_TAG, entry,
738                        LEGEND_ENTRY_TAG);
739                 free(entry);
740             }
741             free(legend_v);
742             printf("    </%s>\n", LEGEND_TAG);
743             printf("  </%s>\n", META_TAG);
744             printf("  <%s>\n", DATA_TAG);
745             for (ti = start + step; ti <= end; ti += step) {
746                 printf("    <%s>", DATA_ROW_TAG);
747                 printf("<%s>%lu</%s>", COL_TIME_TAG, ti, COL_TIME_TAG);
748                 for (j = 0; j < col_cnt; j++) {
749                     rrd_value_t newval = DNAN;
750
751                     if (enumds == 1)
752
753                         snprintf(vtag, vtag_s, "%s%lu", COL_DATA_TAG, j);
754                     else
755                         snprintf(vtag, vtag_s, "%s", COL_DATA_TAG);
756                     newval = *ptr;
757                     if (isnan(newval)) {
758                         printf("<%s>NaN</%s>", vtag, vtag);
759                     } else {
760                         printf("<%s>%0.10e</%s>", vtag, newval, vtag);
761                     };
762                     ptr++;
763                 }
764                 printf("</%s>\n", DATA_ROW_TAG);
765             }
766             free(data);
767             printf("  </%s>\n", DATA_TAG);
768             printf("</%s>\n", ROOT_TAG);
769             setlocale(LC_NUMERIC, old_locale);
770         }
771         free(vtag);
772     } else if (strcmp("graph", argv[1]) == 0) {
773         char    **calcpr;
774
775 #ifdef notused /*XXX*/
776         const char *imgfile = argv[2];  /* rrd_graph changes argv pointer */
777 #endif
778         int       xsize, ysize;
779         double    ymin, ymax;
780         int       i;
781         int       tostdout = (strcmp(argv[2], "-") == 0);
782         int       imginfo = 0;
783
784         for (i = 2; i < argc; i++) {
785             if (strcmp(argv[i], "--imginfo") == 0
786                 || strcmp(argv[i], "-f") == 0) {
787                 imginfo = 1;
788                 break;
789             }
790         }
791         if (rrd_graph
792             (argc - 1, &argv[1], &calcpr, &xsize, &ysize, NULL, &ymin,
793              &ymax) != -1) {
794             if (!tostdout && !imginfo)
795                 printf("%dx%d\n", xsize, ysize);
796             if (calcpr) {
797                 for (i = 0; calcpr[i]; i++) {
798                     if (!tostdout)
799                         printf("%s\n", calcpr[i]);
800                     free(calcpr[i]);
801                 }
802                 free(calcpr);
803             }
804         }
805
806     } else if (strcmp("graphv", argv[1]) == 0) {
807         rrd_info_t *grinfo = NULL;  /* 1 to distinguish it from the NULL that rrd_graph sends in */
808
809         grinfo = rrd_graph_v(argc - 1, &argv[1]);
810         if (grinfo) {
811             rrd_info_print(grinfo);
812             rrd_info_free(grinfo);
813         }
814
815     } else if (strcmp("tune", argv[1]) == 0)
816         rrd_tune(argc - 1, &argv[1]);
817     else if (strcmp("flushcached", argv[1]) == 0)
818         rrd_flushcached(argc - 1, &argv[1]);
819     else {
820         rrd_set_error("unknown function '%s'", argv[1]);
821     }
822     if (rrd_test_error()) {
823         fprintf(out, "ERROR: %s\n", rrd_get_error());
824         rrd_clear_error();
825         return 1;
826     }
827     return (0);
828 }
829
830 int CountArgs(
831     char *aLine)
832 {
833     int       i = 0;
834     int       aCount = 0;
835     int       inarg = 0;
836
837     while (aLine[i] == ' ')
838         i++;
839     while (aLine[i] != 0) {
840         if ((aLine[i] == ' ') && inarg) {
841             inarg = 0;
842         }
843         if ((aLine[i] != ' ') && !inarg) {
844             inarg = 1;
845             aCount++;
846         }
847         i++;
848     }
849     return aCount;
850 }
851
852 /*
853  * CreateArgs - take a string (aLine) and tokenize
854  */
855 int CreateArgs(
856     char *pName,
857     char *aLine,
858     char **argv)
859 {
860     char     *getP, *putP;
861     char    **pargv = argv;
862     char      Quote = 0;
863     int       inArg = 0;
864     int       len;
865     int       argc = 1;
866
867     len = strlen(aLine);
868     /* remove trailing space and newlines */
869     while (len && aLine[len] <= ' ') {
870         aLine[len] = 0;
871         len--;
872     }
873     /* sikp leading blanks */
874     while (*aLine && *aLine <= ' ')
875         aLine++;
876     pargv[0] = pName;
877     argc = 1;
878     getP = aLine;
879     putP = aLine;
880     while (*getP) {
881         switch (*getP) {
882         case ' ':
883             if (Quote) {
884                 *(putP++) = *getP;
885             } else if (inArg) {
886                 *(putP++) = 0;
887                 inArg = 0;
888             }
889             break;
890         case '"':
891         case '\'':
892             if (Quote != 0) {
893                 if (Quote == *getP)
894                     Quote = 0;
895                 else {
896                     *(putP++) = *getP;
897                 }
898             } else {
899                 if (!inArg) {
900                     pargv[argc++] = putP;
901                     inArg = 1;
902                 }
903                 Quote = *getP;
904             }
905             break;
906         default:
907             if (!inArg) {
908                 pargv[argc++] = putP;
909                 inArg = 1;
910             }
911             *(putP++) = *getP;
912             break;
913         }
914         getP++;
915     }
916
917     *putP = '\0';
918     if (Quote)
919         return -1;
920     else
921         return argc;
922 }