allow for input lines of arbitrary length in rrdtool pipe mode -- roger.meier terreac...
[rrdtool.git] / src / rrd_tool.c
index ecd88f9..8bd4ded 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * RRDtool 1.2.11  Copyright by Tobi Oetiker, 1997-2005
+ * RRDtool 1.2.12  Copyright by Tobi Oetiker, 1997-2005
  *****************************************************************************
  * rrd_tool.c  Startup wrapper
  *****************************************************************************/
@@ -22,12 +22,8 @@ void PrintUsage(char *cmd)
 {
 
     char help_main[] =
-          "RRDtool " PACKAGE_VERSION "  Copyright 1997-2005 by Tobias Oetiker <tobi@oetiker.ch>\n"
-#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
+          "RRDtool " PACKAGE_VERSION "  Copyright 1997-2006 by Tobias Oetiker <tobi@oetiker.ch>\n"
            "               Compiled " __DATE__ " " __TIME__ "\n\n"
-#else
-           "               Compiled " MAKE_TIMESTAMP "\n\n"
-#endif          
           "Usage: rrdtool [options] command command_options\n\n";
 
     char help_list[] =
@@ -118,9 +114,10 @@ void PrintUsage(char *cmd)
           "\t\t[-f|--imginfo printfstr]\n"
           "\t\t[-a|--imgformat PNG]\n"
           "\t\t[-c|--color COLORTAG#rrggbb[aa]] [-t|--title string]\n"
-          "\t\t[DEF:vname=rrd:ds-name:CF]\n"
-          "\t\t[CDEF:vname=rpn-expression]\n";
+          "\t\t[-W|--watermark string]\n"
+          "\t\t[DEF:vname=rrd:ds-name:CF]\n";
     char help_graph3[] =
+          "\t\t[CDEF:vname=rpn-expression]\n"
           "\t\t[VDEF:vdefname=rpn-expression]\n"
           "\t\t[PRINT:vdefname:format]\n"
           "\t\t[GPRINT:vdefname:format]\n"
@@ -298,14 +295,35 @@ void PrintUsage(char *cmd)
     fputs(help_lic, stdout);
 }
 
+static char *fgetslong(char **aLinePtr, FILE *stream)
+{
+   char *linebuf;
+   size_t bufsize = MAX_LENGTH;
+   int eolpos = 0;
+
+   if (feof(stream)) return *aLinePtr = 0;
+   if (!(linebuf = malloc(bufsize))) {
+      perror("fgetslong: malloc");
+      exit(1);
+   }
+   linebuf[0] = '\0';
+   while (fgets(linebuf + eolpos, MAX_LENGTH, stream)) {
+      eolpos += strlen(linebuf + eolpos);
+      if (linebuf[eolpos - 1] == '\n') return *aLinePtr = linebuf;
+      bufsize += MAX_LENGTH;
+      if (!(linebuf = realloc(linebuf, bufsize))) {
+         perror("fgetslong: realloc");
+         exit(1);
+      }
+   }
+   return *aLinePtr = linebuf[0] ? linebuf : 0;
+}
 
 int main(int argc, char *argv[])
 {
     char **myargv;
-    char aLine[MAX_LENGTH];
-#ifdef HAVE_CHROOT    
+    char *aLine;
     char *firstdir="";
-#endif
 #ifdef MUST_DISABLE_SIGFPE
     signal(SIGFPE,SIG_IGN);
 #endif
@@ -331,9 +349,17 @@ int main(int argc, char *argv[])
            gettimeofday(&starttime,&tz);
 #endif
          RemoteMode=1;
-#ifdef HAVE_CHROOT
           if ((argc == 3) && strcmp("",argv[2])){
-             if (getuid()==0){
+
+             if (
+#ifdef HAVE_GETUID
+                getuid()
+#else
+                1
+#endif
+                == 0 ){
+
+#ifdef HAVE_CHROOT
                 chroot(argv[2]);
                 if (errno!=0){
                    fprintf(stderr,"ERROR: can't change root to '%s' errno=%d\n",
@@ -342,8 +368,12 @@ int main(int argc, char *argv[])
                 }
                 ChangeRoot=1;
                 firstdir="/";
-             }
-             else{
+#else
+                fprintf(stderr,"ERROR: change root is not supported by your OS "
+                         "or at least by this copy of rrdtool\n");
+                exit(1);
+#endif
+             } else {
                 firstdir=argv[2];
              }
           }
@@ -354,15 +384,10 @@ int main(int argc, char *argv[])
                 exit(errno);
              }
           }
-#else
-          fprintf(stderr,"ERROR: change root is not supported by your OS "
-                         "or at least by this copy of rrdtool\n");
-          exit(1);
-#endif
 
-           while (fgets(aLine, sizeof(aLine)-1, stdin)){
+           while (fgetslong(&aLine, stdin)){
                if ((argc = CountArgs(aLine)) == 0)  {
-                   fprintf(stderr,"ERROR: not enough arguments\n");
+                   printf("ERROR: not enough arguments\n");
                }
                if ((myargv = (char **) malloc((argc+1) * 
                                               sizeof(char *))) == NULL)   {
@@ -395,6 +420,7 @@ int main(int argc, char *argv[])
                  }
                }
                fflush(stdout); /* this is important for pipes to work */
+                free(aLine);
            }
        }
     else if (argc == 2)
@@ -442,7 +468,7 @@ int HandleInputLine(int argc, char **argv, FILE* out)
 #if ! defined(HAVE_CHROOT) || ! defined(HAVE_GETUID)
           if (getuid()==0 && ! ChangeRoot){
              printf("ERROR: chdir security problem - rrdtool is running as "
-                    "root an no chroot!\n");
+                    "root but not chroot!\n");
              return(1); 
           }
 #endif
@@ -461,7 +487,7 @@ int HandleInputLine(int argc, char **argv, FILE* out)
 #if ! defined(HAVE_CHROOT) || ! defined(HAVE_GETUID)
           if (getuid()==0 && ! ChangeRoot){
              printf("ERROR: mkdir security problem - rrdtool is running as "
-                    "root an no chroot!\n");
+                    "root but not chroot!\n");
              return(1); 
           }
 #endif
@@ -649,8 +675,15 @@ int HandleInputLine(int argc, char **argv, FILE* out)
        double ymin,ymax;
        int i;
        int tostdout = (strcmp(argv[2],"-") == 0);      
+       int imginfo = 0;
+       for (i=2;i<argc;i++){
+               if (strcmp(argv[i],"--imginfo") == 0 || strcmp(argv[i],"-f") == 0){
+                       imginfo = 1;
+                       break;
+               }
+       }
        if( rrd_graph(argc-1, &argv[1], &calcpr, &xsize, &ysize, NULL, &ymin, &ymax) != -1 ) {
-           if (!tostdout) 
+           if (!tostdout && !imginfo
                printf ("%dx%d\n",xsize,ysize);
            if (calcpr) {
                for(i=0;calcpr[i];i++){