started updating for 1.2 release
[rrdtool.git] / src / parsetime.c
index 9ad9c55..775a40a 100644 (file)
@@ -2,7 +2,7 @@
  *  parsetime.c - parse time for at(1)
  *  Copyright (C) 1993, 1994  Thomas Koenig
  *
- *  modifications for english-language times
+ *  modifications for English-language times
  *  Copyright (C) 1993  David Parsons
  *
  *  A lot of modifications and extensions 
@@ -228,31 +228,30 @@ static struct SpecialToken TimeMultipliers[] = {
 
 /* File scope variables */
 
-/* context dependant list of specials for parser to recognize,
+/* context dependent list of specials for parser to recognize,
  * required for us to be able distinguish between 'mon' as 'month'
  * and 'mon' as 'monday'
  */
 static struct SpecialToken *Specials;
 
-static char **scp;     /* scanner - pointer at arglist */
+static const char **scp;       /* scanner - pointer at arglist */
 static char scc;       /* scanner - count of remaining arguments */
-static char *sct;      /* scanner - next char pointer in current argument */
+static const char *sct;        /* scanner - next char pointer in current argument */
 static int need;       /* scanner - need to advance to next argument */
 
 static char *sc_token=NULL;    /* scanner - token buffer */
-static size_t sc_len;   /* scanner - lenght of token buffer */
+static size_t sc_len;   /* scanner - length of token buffer */
 static int sc_tokid;   /* scanner - token id */
 
-static int need_to_free = 0; /* means that we need deallocating memory */
-
 /* Local functions */
+static void EnsureMemFree (void);
 
-void EnsureMemFree ()
+static void EnsureMemFree (void)
 {
-  if( need_to_free )
+  if( sc_token )
     {
     free(sc_token);
-    need_to_free = 0;
+    sc_token = NULL;
     }
 }
 
@@ -263,12 +262,12 @@ void EnsureMemFree ()
  * should return TIME_OK (aka NULL) or pointer to the error message,
  * and should be called like this: try(func(args));
  *
- * if the try is not successfull it will reset the token pointer ...
+ * if the try is not successful it will reset the token pointer ...
  *
  * [NOTE: when try(...) is used as the only statement in the "if-true"
  *  part of the if statement that also has an "else" part it should be
  *  either enclosed in the curly braces (despite the fact that it looks
- *  like a single statement) or NOT follwed by the ";"]
+ *  like a single statement) or NOT followed by the ";"]
  */
 #define try(b)         { \
                        char *_e; \
@@ -291,7 +290,7 @@ void EnsureMemFree ()
 
 /*
  * ve() and e() are used to set the return error,
- * the most aprropriate use for these is inside panic(...) 
+ * the most appropriate use for these is inside panic(...) 
  */
 #define MAX_ERR_MSG_LEN        1024
 static char errmsg[ MAX_ERR_MSG_LEN ];
@@ -320,7 +319,7 @@ e ( char *fmt, ... )
 }
 
 /* Compare S1 and S2, ignoring case, returning less than, equal to or
-   greater than zero if S1 is lexiographically less than,
+   greater than zero if S1 is lexicographically less than,
    equal to or greater than S2.  -- copied from GNU libc*/
 static int
 mystrcasecmp (s1, s2)
@@ -368,7 +367,7 @@ parse_token(char *arg)
  * init_scanner() sets up the scanner to eat arguments
  */
 static char *
-init_scanner(int argc, char **argv)
+init_scanner(int argc, const char **argv)
 {
     scp = argv;
     scc = argc;
@@ -380,7 +379,6 @@ init_scanner(int argc, char **argv)
     sc_token = (char *) malloc(sc_len*sizeof(char));
     if( sc_token == NULL )
       return "Failed to allocate memory";
-    need_to_free = 1;
     return TIME_OK;
 } /* init_scanner */
 
@@ -453,10 +451,10 @@ token()
 
 
 /* 
- * expect() gets a token and complins if it's not the token we want
+ * expect2() gets a token and complains if it's not the token we want
  */
 static char *
-expect(int desired, char *complain_fmt, ...)
+expect2(int desired, char *complain_fmt, ...)
 {
     va_list ap;
     va_start( ap, complain_fmt );
@@ -466,16 +464,16 @@ expect(int desired, char *complain_fmt, ...)
     va_end( ap );
     return TIME_OK;
     
-} /* expect */
+} /* expect2 */
 
 
 /*
  * plus_minus() is used to parse a single NUMBER TIME-UNIT pair
  *              for the OFFSET-SPEC.
- *              It allso applies those m-guessing euristics.
+ *              It also applies those m-guessing heuristics.
  */
 static char *
-plus_minus(struct time_value *ptv, int doop)
+plus_minus(struct rrd_time_value *ptv, int doop)
 {
     static int op = PLUS;
     static int prev_multiplier = -1;
@@ -484,7 +482,7 @@ plus_minus(struct time_value *ptv, int doop)
     if( doop >= 0 ) 
       {
       op = doop;
-      try(expect(NUMBER,"There should be number after '%c'", op == PLUS ? '+' : '-'));
+      try(expect2(NUMBER,"There should be number after '%c'", op == PLUS ? '+' : '-'));
       prev_multiplier = -1; /* reset months-minutes guessing mechanics */
       }
     /* if doop is < 0 then we repeat the previous op
@@ -555,13 +553,13 @@ plus_minus(struct time_value *ptv, int doop)
  * tod() computes the time of day (TIME-OF-DAY-SPEC)
  */
 static char *
-tod(struct time_value *ptv)
+tod(struct rrd_time_value *ptv)
 {
     int hour, minute = 0;
     int tlen;
     /* save token status in  case we must abort */
     int scc_sv = scc; 
-    char *sct_sv = sct; 
+    const char *sct_sv = sct; 
     int sc_tokid_sv = sc_tokid;
 
     tlen = strlen(sc_token);
@@ -584,7 +582,7 @@ tod(struct time_value *ptv)
       return TIME_OK;
     }
     if (sc_tokid == COLON ) {
-       try(expect(NUMBER,
+       try(expect2(NUMBER,
             "Parsing HH:MM syntax, expecting MM as number, got none"));
        minute = atoi(sc_token);
        if (minute > 59) {
@@ -631,7 +629,7 @@ tod(struct time_value *ptv)
  * assign_date() assigns a date, adjusting year as appropriate
  */
 static char *
-assign_date(struct time_value *ptv, long mday, long mon, long year)
+assign_date(struct rrd_time_value *ptv, long mday, long mon, long year)
 {
     if (year > 138) {
        if (year > 1970)
@@ -659,7 +657,7 @@ assign_date(struct time_value *ptv, long mday, long mon, long year)
  * day() picks apart DAY-SPEC-[12]
  */
 static char *
-day(struct time_value *ptv)
+day(struct rrd_time_value *ptv)
 {
     long mday=0, wday, mon, year = ptv->tm.tm_year;
     int tlen;
@@ -681,7 +679,7 @@ day(struct time_value *ptv)
            /* do month mday [year]
             */
            mon = (sc_tokid-JAN);
-           try(expect(NUMBER,
+           try(expect2(NUMBER,
                "the day of the month should follow month name"));
            mday = atol(sc_token);
            if (token() == NUMBER) {
@@ -716,7 +714,7 @@ day(struct time_value *ptv)
             */
            tlen = strlen(sc_token);
            mon = atol(sc_token);
-            if (mon > 10*356*24*60*60) {
+            if (mon > 10*365*24*60*60) {
                ptv->tm=*localtime(&mon);
                token();
                break;
@@ -737,17 +735,17 @@ day(struct time_value *ptv)
              if (mon <= 31 && (sc_tokid == SLASH || sc_tokid == DOT)) {
                int sep;                    
                sep = sc_tokid;
-               try(expect(NUMBER,"there should be %s number after '%c'",
+               try(expect2(NUMBER,"there should be %s number after '%c'",
                           sep == DOT ? "month" : "day", sep == DOT ? '.' : '/'));
                mday = atol(sc_token);
                if (token() == sep) {
-                 try(expect(NUMBER,"there should be year number after '%c'",
+                 try(expect2(NUMBER,"there should be year number after '%c'",
                             sep == DOT ? '.' : '/'));
                  year = atol(sc_token);
                  token();
                }
                
-               /* flip months and days for european timing
+               /* flip months and days for European timing
                 */
                if (sep == DOT) {
                  long x = mday;
@@ -777,7 +775,7 @@ day(struct time_value *ptv)
 
 /*
  * parsetime() is the external interface that takes tspec, parses
- * it and puts the result in the time_value structure *ptv.
+ * it and puts the result in the rrd_time_value structure *ptv.
  * It can return either absolute times (these are ensured to be
  * correct) or relative time references that are expected to be
  * added to some absolute time value and then normalized by
@@ -785,7 +783,7 @@ day(struct time_value *ptv)
  * the pointer to the error message in the case of problems
  */
 char *
-parsetime(char *tspec, struct time_value *ptv)
+parsetime(const char *tspec, struct rrd_time_value *ptv)
 {
     time_t now = time(NULL);
     int hr = 0;
@@ -849,7 +847,7 @@ parsetime(char *tspec, struct time_value *ptv)
            try(tod(ptv))
            break;
 
-           /* evil coding for TEATIME|NOON|MIDNIGHT - we've initialised
+           /* evil coding for TEATIME|NOON|MIDNIGHT - we've initialized
             * hr to zero up above, then fall into this case in such a
             * way so we add +12 +4 hours to it for teatime, +12 hours
             * to it for noon, and nothing at all for midnight, then
@@ -913,8 +911,8 @@ parsetime(char *tspec, struct time_value *ptv)
 } /* parsetime */
 
 
-int proc_start_end (struct time_value *start_tv, 
-                   struct time_value *end_tv, 
+int proc_start_end (struct rrd_time_value *start_tv, 
+                   struct rrd_time_value *end_tv, 
                    time_t *start, 
                    time_t *end){
     if (start_tv->type == RELATIVE_TO_END_TIME  && /* same as the line above */