Replaced extract_token() with a completely new
authorArt Cancro <ajc@citadel.org>
Fri, 10 Aug 2007 21:20:21 +0000 (21:20 +0000)
committerArt Cancro <ajc@citadel.org>
Fri, 10 Aug 2007 21:20:21 +0000 (21:20 +0000)
implementation which makes exactly one pass through
the source string, copying characters to the destination
string buffer as it goes.

citadel/parsedate.c
citadel/tools.c
webcit/tools.c
webcit/webcit.h

index 5a355fd15fe1e97ca7163962209c28b61bc58d56..39a3651a8e359348b2085b63e535b02670911506 100644 (file)
@@ -1,7 +1,7 @@
-/* A Bison parser, made by GNU Bison 1.875.  */
+/* A Bison parser, made by GNU Bison 1.875c.  */
 
 /* Skeleton parser for Yacc-like parsing with Bison,
-   Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002 Free Software Foundation, Inc.
+   Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -223,7 +223,7 @@ typedef union YYSTYPE {
     enum _MERIDIAN     Meridian;
 } YYSTYPE;
 /* Line 191 of yacc.c.  */
-#line 226 "y.tab.c"
+#line 227 "y.tab.c"
 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
 # define YYSTYPE_IS_DECLARED 1
 # define YYSTYPE_IS_TRIVIAL 1
@@ -235,22 +235,29 @@ typedef union YYSTYPE {
 
 
 /* Line 214 of yacc.c.  */
-#line 238 "y.tab.c"
+#line 239 "y.tab.c"
 
 #if ! defined (yyoverflow) || YYERROR_VERBOSE
 
+# ifndef YYFREE
+#  define YYFREE free
+# endif
+# ifndef YYMALLOC
+#  define YYMALLOC malloc
+# endif
+
 /* The parser invokes alloca or malloc; define the necessary symbols.  */
 
-# if YYSTACK_USE_ALLOCA
-#  define YYSTACK_ALLOC alloca
+# ifdef YYSTACK_USE_ALLOCA
+#  if YYSTACK_USE_ALLOCA
+#   define YYSTACK_ALLOC alloca
+#  endif
 # else
-#  ifndef YYSTACK_USE_ALLOCA
-#   if defined (alloca) || defined (_ALLOCA_H)
-#    define YYSTACK_ALLOC alloca
-#   else
-#    ifdef __GNUC__
-#     define YYSTACK_ALLOC __builtin_alloca
-#    endif
+#  if defined (alloca) || defined (_ALLOCA_H)
+#   define YYSTACK_ALLOC alloca
+#  else
+#   ifdef __GNUC__
+#    define YYSTACK_ALLOC __builtin_alloca
 #   endif
 #  endif
 # endif
@@ -263,15 +270,15 @@ typedef union YYSTYPE {
 #   include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
 #   define YYSIZE_T size_t
 #  endif
-#  define YYSTACK_ALLOC malloc
-#  define YYSTACK_FREE free
+#  define YYSTACK_ALLOC YYMALLOC
+#  define YYSTACK_FREE YYFREE
 # endif
 #endif /* ! defined (yyoverflow) || YYERROR_VERBOSE */
 
 
 #if (! defined (yyoverflow) \
      && (! defined (__cplusplus) \
-        || (YYSTYPE_IS_TRIVIAL)))
+        || (defined (YYSTYPE_IS_TRIVIAL) && YYSTYPE_IS_TRIVIAL)))
 
 /* A type that is properly aligned for any stack member.  */
 union yyalloc
@@ -292,7 +299,7 @@ union yyalloc
 /* Copy COUNT objects from FROM to TO.  The source and destination do
    not overlap.  */
 # ifndef YYCOPY
-#  if 1 < __GNUC__
+#  if defined (__GNUC__) && 1 < __GNUC__
 #   define YYCOPY(To, From, Count) \
       __builtin_memcpy (To, From, (Count) * sizeof (*(From)))
 #  else
@@ -425,10 +432,10 @@ static const unsigned short yyrline[] =
    First, the terminals, then, starting at YYNTOKENS, nonterminals. */
 static const char *const yytname[] =
 {
-  "$end", "error", "$undefined", "tDAY", "tDAYZONE", "tMERIDIAN", "tMONTH", 
-  "tMONTH_UNIT", "tSEC_UNIT", "tSNUMBER", "tUNUMBER", "tZONE", "':'", 
-  "'/'", "','", "$accept", "spec", "item", "time", "zone", "numzone", 
-  "date", "rel", "o_merid", 0
+  "$end", "error", "$undefined", "tDAY", "tDAYZONE", "tMERIDIAN",
+  "tMONTH", "tMONTH_UNIT", "tSEC_UNIT", "tSNUMBER", "tUNUMBER", "tZONE",
+  "':'", "'/'", "','", "$accept", "spec", "item", "time", "zone",
+  "numzone", "date", "rel", "o_merid", 0
 };
 #endif
 
@@ -553,7 +560,8 @@ static const unsigned char yystos[] =
 
 #define YYACCEPT       goto yyacceptlab
 #define YYABORT                goto yyabortlab
-#define YYERROR                goto yyerrlab1
+#define YYERROR                goto yyerrorlab
+
 
 /* Like YYERROR except do call yyerror.  This remains here temporarily
    to ease the transition to the new meaning of YYERROR, for GCC.
@@ -587,11 +595,11 @@ while (0)
    are run).  */
 
 #ifndef YYLLOC_DEFAULT
-# define YYLLOC_DEFAULT(Current, Rhs, N)         \
-  Current.first_line   = Rhs[1].first_line;      \
-  Current.first_column = Rhs[1].first_column;    \
-  Current.last_line    = Rhs[N].last_line;       \
-  Current.last_column  = Rhs[N].last_column;
+# define YYLLOC_DEFAULT(Current, Rhs, N)               \
+   ((Current).first_line   = (Rhs)[1].first_line,      \
+    (Current).first_column = (Rhs)[1].first_column,    \
+    (Current).last_line    = (Rhs)[N].last_line,       \
+    (Current).last_column  = (Rhs)[N].last_column)
 #endif
 
 /* YYLEX -- calling `yylex' with the right arguments.  */
@@ -635,7 +643,7 @@ do {                                                                \
 
 /*------------------------------------------------------------------.
 | yy_stack_print -- Print the state stack from its BOTTOM up to its |
-| TOP (cinluded).                                                   |
+| TOP (included).                                                   |
 `------------------------------------------------------------------*/
 
 #if defined (__STDC__) || defined (__cplusplus)
@@ -675,9 +683,9 @@ yy_reduce_print (yyrule)
 #endif
 {
   int yyi;
-  unsigned int yylineno = yyrline[yyrule];
+  unsigned int yylno = yyrline[yyrule];
   YYFPRINTF (stderr, "Reducing stack by rule %d (line %u), ",
-             yyrule - 1, yylineno);
+             yyrule - 1, yylno);
   /* Print the symbols being reduced, and their result.  */
   for (yyi = yyprhs[yyrule]; 0 <= yyrhs[yyi]; yyi++)
     YYFPRINTF (stderr, "%s ", yytname [yyrhs[yyi]]);
@@ -714,7 +722,7 @@ int yydebug;
    SIZE_MAX < YYSTACK_BYTES (YYMAXDEPTH)
    evaluated with infinite-precision integer arithmetic.  */
 
-#if YYMAXDEPTH == 0
+#if defined (YYMAXDEPTH) && YYMAXDEPTH == 0
 # undef YYMAXDEPTH
 #endif
 
@@ -1401,8 +1409,8 @@ yyreduce:
 
     }
 
-/* Line 991 of yacc.c.  */
-#line 1405 "y.tab.c"
+/* Line 1000 of yacc.c.  */
+#line 1414 "y.tab.c"
 \f
   yyvsp -= yylen;
   yyssp -= yylen;
@@ -1443,18 +1451,33 @@ yyerrlab:
        {
          YYSIZE_T yysize = 0;
          int yytype = YYTRANSLATE (yychar);
+         const char* yyprefix;
          char *yymsg;
-         int yyx, yycount;
+         int yyx;
 
-         yycount = 0;
          /* Start YYX at -YYN if negative to avoid negative indexes in
             YYCHECK.  */
-         for (yyx = yyn < 0 ? -yyn : 0;
-              yyx < (int) (sizeof (yytname) / sizeof (char *)); yyx++)
+         int yyxbegin = yyn < 0 ? -yyn : 0;
+
+         /* Stay within bounds of both yycheck and yytname.  */
+         int yychecklim = YYLAST - yyn;
+         int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
+         int yycount = 0;
+
+         yyprefix = ", expecting ";
+         for (yyx = yyxbegin; yyx < yyxend; ++yyx)
            if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR)
-             yysize += yystrlen (yytname[yyx]) + 15, yycount++;
-         yysize += yystrlen ("syntax error, unexpected ") + 1;
-         yysize += yystrlen (yytname[yytype]);
+             {
+               yysize += yystrlen (yyprefix) + yystrlen (yytname [yyx]);
+               yycount += 1;
+               if (yycount == 5)
+                 {
+                   yysize = 0;
+                   break;
+                 }
+             }
+         yysize += (sizeof ("syntax error, unexpected ")
+                    + yystrlen (yytname[yytype]));
          yymsg = (char *) YYSTACK_ALLOC (yysize);
          if (yymsg != 0)
            {
@@ -1463,16 +1486,13 @@ yyerrlab:
 
              if (yycount < 5)
                {
-                 yycount = 0;
-                 for (yyx = yyn < 0 ? -yyn : 0;
-                      yyx < (int) (sizeof (yytname) / sizeof (char *));
-                      yyx++)
+                 yyprefix = ", expecting ";
+                 for (yyx = yyxbegin; yyx < yyxend; ++yyx)
                    if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR)
                      {
-                       const char *yyq = ! yycount ? ", expecting " : " or ";
-                       yyp = yystpcpy (yyp, yyq);
+                       yyp = yystpcpy (yyp, yyprefix);
                        yyp = yystpcpy (yyp, yytname[yyx]);
-                       yycount++;
+                       yyprefix = " or ";
                      }
                }
              yyerror (yymsg);
@@ -1493,52 +1513,56 @@ yyerrlab:
       /* If just tried and failed to reuse lookahead token after an
         error, discard it.  */
 
-      /* Return failure if at end of input.  */
-      if (yychar == YYEOF)
+      if (yychar <= YYEOF)
         {
-         /* Pop the error token.  */
-          YYPOPSTACK;
-         /* Pop the rest of the stack.  */
-         while (yyss < yyssp)
-           {
-             YYDSYMPRINTF ("Error: popping", yystos[*yyssp], yyvsp, yylsp);
-             yydestruct (yystos[*yyssp], yyvsp);
-             YYPOPSTACK;
-           }
-         YYABORT;
+          /* If at end of input, pop the error token,
+            then the rest of the stack, then return failure.  */
+         if (yychar == YYEOF)
+            for (;;)
+              {
+                YYPOPSTACK;
+                if (yyssp == yyss)
+                  YYABORT;
+                YYDSYMPRINTF ("Error: popping", yystos[*yyssp], yyvsp, yylsp);
+                yydestruct (yystos[*yyssp], yyvsp);
+              }
         }
+      else
+       {
+         YYDSYMPRINTF ("Error: discarding", yytoken, &yylval, &yylloc);
+         yydestruct (yytoken, &yylval);
+         yychar = YYEMPTY;
 
-      YYDSYMPRINTF ("Error: discarding", yytoken, &yylval, &yylloc);
-      yydestruct (yytoken, &yylval);
-      yychar = YYEMPTY;
-
+       }
     }
 
   /* Else will try to reuse lookahead token after shifting the error
      token.  */
-  goto yyerrlab2;
+  goto yyerrlab1;
 
 
-/*----------------------------------------------------.
-| yyerrlab1 -- error raised explicitly by an action.  |
-`----------------------------------------------------*/
-yyerrlab1:
+/*---------------------------------------------------.
+| yyerrorlab -- error raised explicitly by YYERROR.  |
+`---------------------------------------------------*/
+yyerrorlab:
 
-  /* Suppress GCC warning that yyerrlab1 is unused when no action
-     invokes YYERROR.  */
-#if defined (__GNUC_MINOR__) && 2093 <= (__GNUC__ * 1000 + __GNUC_MINOR__) \
-    && !defined __cplusplus
-  __attribute__ ((__unused__))
+#ifdef __GNUC__
+  /* Pacify GCC when the user code never invokes YYERROR and the label
+     yyerrorlab therefore never appears in user code.  */
+  if (0)
+     goto yyerrorlab;
 #endif
 
-
-  goto yyerrlab2;
+  yyvsp -= yylen;
+  yyssp -= yylen;
+  yystate = *yyssp;
+  goto yyerrlab1;
 
 
-/*---------------------------------------------------------------.
-| yyerrlab2 -- pop states until the error token can be shifted.  |
-`---------------------------------------------------------------*/
-yyerrlab2:
+/*-------------------------------------------------------------.
+| yyerrlab1 -- common code for both syntax error and YYERROR.  |
+`-------------------------------------------------------------*/
+yyerrlab1:
   yyerrstatus = 3;     /* Each real token shifted decrements this.  */
 
   for (;;)
@@ -1561,9 +1585,8 @@ yyerrlab2:
 
       YYDSYMPRINTF ("Error: popping", yystos[*yyssp], yyvsp, yylsp);
       yydestruct (yystos[yystate], yyvsp);
-      yyvsp--;
-      yystate = *--yyssp;
-
+      YYPOPSTACK;
+      yystate = *yyssp;
       YY_STACK_PRINT (yyss, yyssp);
     }
 
index 6d62916b8cf79d159fa2fba09d814d5b13e5e9e1..f0c2776536596ff5df9b0c8d1c2ca3ccfe239a1b 100644 (file)
@@ -108,86 +108,36 @@ int num_tokens(const char *source, char tok)
  */
 long extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
 {
-       const char *s;          /* source */
-       const char *start;
-       const char *end;
-       int count = 0;
-       int len = 0;
-       /* Locate desired parameter */
+       const char *s;                  /* source */
+       int len = 0;                    /* running total length of extracted string */
+       int current_token = 0;          /* token currently being processed */
+
        s = source;
-       while (1) {
-               /* End of string, bail! */
-               if (IsEmptyStr(s)) {
-                       *dest = '\0';
-                       return -1;              /* Parameter not found */
-               }
-               if (*s == separator) {
-                       count++;
-                       if (count == parmnum)
-                               start = s + 1; // we found da start border!
-                       else if (count == parmnum + 1)
-                       {
-                               end = s; // we found da end border!
-                               break; // so we're done.
-                       }
-               }
-               s++;
+
+       if (dest == NULL) {
+               return(-1);
        }
 
-       len = end - start;
-       if (len > maxlen){
-               /** TODO: do find an alternative for this if we're not inside of the server
-               lprintf (ERROR, 
-                        "Warning: Tokenizer failed due to space. better use grab_token here? "
-                        "(Token: %d N: %d Extractstring at: %s FullString: %s", 
-                        (char)separator, parmnum, start, source);
-               */
-               *dest = '\0';
-               return -1;              /* Parameter exceeds space */
-       }               
-       memcpy(dest, start, len);
-       dest[len]='\0';
-       return len;
-}
+       dest[0] = 0;
 
-/*
- * extract_token() - a string tokenizer
- * returns -1 if not found, or length of token.
- */
-long grab_token(char **dest, const char *source, int parmnum, char separator)
-{
-       const char *s;          /* source */
-       const char *start;
-       const char *end;
-       int count = 0;
-       int len = 0;
+       if (s == NULL) {
+               return(-1);
+       }
 
-       /* Locate desired parameter */
-       s = source;
-       while (1) {
-               /* End of string, bail! */
-               if (IsEmptyStr(s)) {
-                       *dest = '\0';
-                       return -1;              /* Parameter not found */
-               }
+       while (*s) {
                if (*s == separator) {
-                       count++;
-                       if (count == parmnum)
-                               start = s + 1; // we found da start border!
-                       else if (count == parmnum + 1)
-                       {
-                               end = s; // we found da end border!
-                               break; // so we're done.
-                       }
+                       ++current_token;
                }
-               s++;
+               else if ( (current_token == parmnum) && (len < maxlen) ) {
+                       dest[len] = *s;
+                       ++len;
+               }
+               ++s;
        }
 
-       len = end - start;
-       *dest = (char*)malloc (len + 1);
-       memcpy(*dest, start, len);
-       (*dest)[len]='\0';
-       return len;
+       dest[len] = 0;
+       if (current_token < parmnum) return(-1);
+       return(len);
 }
 
 
index a237a7e4322687e0510476eb7ff3f521e5735aac..f83a7d863bf60360d99fef3fe986a869b48a1909 100644 (file)
@@ -61,46 +61,45 @@ int num_tokens(char *source, char tok)
        return (count);
 }
 
-/**
- * brief a string tokenizer
- * \param dest destination string 
- * \param source the string to grab tokens from
- * \param parmnum the n'th token to grab
- * \param separator the tokenizer string
- * \param maxlen the length of dest
+/*
+ * extract_token() - a string tokenizer
+ * returns -1 if not found, or length of token.
  */
-void extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
+long extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen)
 {
-       char *d;                /* dest */
-       const char *s;          /* source */
-       int count = 0;
-       int len = 0;
+       const char *s;                  /* source */
+       int len = 0;                    /* running total length of extracted string */
+       int current_token = 0;          /* token currently being processed */
+
+       s = source;
+
+       if (dest == NULL) {
+               return(-1);
+       }
 
        dest[0] = 0;
 
-       /* Locate desired parameter */
-       s = source;
-       while (count < parmnum) {
-               /* End of string, bail! */
-               if (!*s) {
-                       s = NULL;
-                       break;
-               }
+       if (s == NULL) {
+               return(-1);
+       }
+
+       while (*s) {
                if (*s == separator) {
-                       count++;
+                       ++current_token;
                }
-               s++;
+               else if ( (current_token == parmnum) && (len < maxlen) ) {
+                       dest[len] = *s;
+                       ++len;
+               }
+               ++s;
        }
-       if (!s) return;         /* Parameter not found */
 
-       for (d = dest; *s && *s != separator && ++len<maxlen; s++, d++) {
-               *d = *s;
-       }
-       *d = 0;
+       dest[len] = 0;
+       if (current_token < parmnum) return(-1);
+       return(len);
 }
 
 
-
 /**
  * \brief a tokenizer that kills, maims, and destroys
  * \param source the string to process
index d80d6ccc9a1c60f40d6cf0583e041cefcd0dcf11..741003e695743812c8ac45bbcd6d3957f4c2a89c 100644 (file)
@@ -585,7 +585,7 @@ void do_template(void *templatename);
 int lingering_close(int fd);
 char *memreadline(char *start, char *buf, int maxlen);
 int num_tokens (char *source, char tok);
-void extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen);
+long extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen);
 void remove_token(char *source, int parmnum, char separator);
 char *load_mimepart(long msgnum, char *partnum);
 int pattern2(char *search, char *patn);