"Fossies" - the Fresh Open Source Software Archive

Member "libgcgi.a-0.9.5/src/parse.c" (22 Jun 2002, 11325 Bytes) of package /linux/www/old/gcgi-0.9.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "parse.c" see the Fossies "Dox" file reference documentation.

    1 /* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
    2 /*
    3  * GCGI Library, implementing NCSA'a Common Gateway Interface and RFC2338.
    4  * Copyright (C) 2001-2002 Julian Catchen, julian@catchen.org
    5  *
    6  * This library is free software; you can redistribute it and/or
    7  * modify it under the terms of the GNU Lesser General Public
    8  * License as published by the Free Software Foundation; either
    9  * version 2.1 of the License, or (at your option) any later version.
   10  *
   11  * This library is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   14  * Lesser General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU Lesser General Public
   17  * License along with this library; if not, write to the Free Software
   18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   19  */
   20 
   21 #include "parse.h"
   22 
   23 int 
   24 numOccurances(char *string, char *pattern)
   25 {
   26     char *pin;
   27     int patternLen;
   28     int count = 0;
   29 
   30     patternLen = strlen(pattern);
   31 
   32     for (pin=string; *pin != '\0'; pin++) {
   33     if (*pin == pattern[0]) {
   34         if(!strncasecmp(pin,pattern,patternLen)) {
   35         pin += patternLen-1;
   36         count++;
   37         }
   38     }
   39     } 
   40 
   41     return count;
   42 }
   43 
   44 int 
   45 replacePat(char **string, char *pattern, char *replace, int *size)
   46 {
   47     char *pin, *pout, *output;
   48     int  stringLen, patternLen, replaceLen;
   49     int  osize;
   50     int  count;
   51 
   52     pattern ? (patternLen = strlen(pattern)) : (patternLen = 0);
   53     replace ? (replaceLen = strlen(replace)) : (replaceLen = 0);
   54     *string ? (stringLen  = strlen(*string)) : (stringLen  = 0);
   55 
   56     //DEBUG("START LEN: %d, START SIZE: %d",strlen(*string), *size);
   57     
   58     /* Count up the number of occurances of the pattern to  *
   59      * determine how big to make the new string.            */
   60     count = numOccurances(*string, pattern);
   61 
   62     if (count < 1) {
   63     //DEBUG("Pattern does not occur");
   64     return 0;
   65     }
   66     
   67     if (replaceLen <= patternLen)
   68     osize  = stringLen + 1;
   69     else 
   70     osize  = stringLen + ((replaceLen - patternLen) * count) + 1;
   71     
   72     //DEBUG("OUTPUT SIZE: %d", osize);
   73 
   74     output = XMALLOC(char, osize);
   75     memset(output, 0, osize);
   76 
   77     count = 0; 
   78     pin   = *string; 
   79     pout  = output;
   80 
   81     while (*pin != '\0') {
   82     if (*pin == pattern[0] && (strncasecmp(pin, pattern, patternLen) == 0)) {
   83         strncpy(pout, replace, replaceLen);
   84         pin  += patternLen;
   85         pout += replaceLen; 
   86         count++;
   87     }
   88     else {
   89         *pout = *pin;
   90         pout++; 
   91         pin++;
   92     }
   93     }
   94 
   95     *pout = '\0';
   96 
   97     if (osize > *size) {
   98     *size = osize;
   99     *string = XREALLOC(char, *string, *size);
  100     }
  101 
  102     strncpy(*string, output, pout - output);
  103     (*string)[pout - output] = '\0';
  104 
  105     XFREE(output);
  106 
  107     return count;
  108 }
  109 
  110 
  111 int 
  112 removePat(char *string, char *pattern, int stringLen)
  113 {
  114   char *pin, *pout, *out;
  115   int   patternLen;
  116   int   count;
  117 
  118   count = 0;
  119   pattern ? (patternLen = strlen(pattern)) : (patternLen = 0);
  120 
  121   out = XMALLOC(char, stringLen + 1);
  122 
  123   for (pin = string, pout = out; *pin != '\0'; pout++, pin++)  {     
  124       if (*pin == pattern[0] && (strncasecmp(pin, pattern, patternLen) == 0)) {
  125       pin += patternLen;
  126       *pout = *pin;
  127       count++; 
  128       }
  129       else {
  130       *pout = *pin;
  131       }
  132   } 
  133 
  134   *pout = '\0';
  135 
  136   if (count)
  137       strcpy(string, out);
  138   
  139   XFREE(out);
  140 
  141   return count;
  142 }
  143 
  144 
  145 int 
  146 regexPat(char *string, char *pattern, int stringLen)
  147 {
  148     int         res, outLen, limit;
  149     int         count = 0;
  150     int         nmatch = 1;
  151     char       *pout, *sptr, *eptr;
  152     regex_t    *rx;
  153     regmatch_t  rm[1];
  154     
  155     /* Calculate a limit to ensure we stop regex */
  156     limit = floor(stringLen / 5);
  157     
  158     rx = XMALLOC(regex_t, 1);
  159     res = regcomp(rx, pattern, REG_ICASE);  
  160     if (res != 0) 
  161     return -1;
  162     
  163     res = regexec(rx, string, nmatch, rm, 0);
  164     
  165     if (res == REG_NOMATCH)     /* Didn't find the pattern. */      
  166     return 0;
  167     
  168     else if (res == REG_ESPACE) /* Regex ran out of memory. */
  169     return -1;
  170     
  171     pout = XMALLOC(char, stringLen);
  172     
  173     count++;
  174     sptr = string + rm[0].rm_so;
  175     eptr = string + rm[0].rm_eo;
  176     strncpy(pout, string, sptr-string);
  177     *(pout+(sptr-string)) = '\0';
  178     
  179     strcat(pout, eptr);
  180     outLen = strlen(pout);
  181     strcpy(string, pout);
  182     *pout = '\0';
  183     
  184     /* Loop until we no longer find the pattern (or hit the safety limit) */
  185     while (((res = regexec(rx,string,nmatch,rm,0)) != REG_NOMATCH) && (count < limit)) {
  186     if (res == REG_ESPACE) /* Regex ran out of memory. */
  187         return -1;
  188     
  189     count++;
  190     sptr = string + rm[0].rm_so;        /* Beginning of pattern.       */
  191     eptr = string + rm[0].rm_eo;        /* End of pattern.             */
  192     strncpy(pout, string, sptr-string); /* Copy the first half.        */
  193     *(pout+(sptr-string)) = '\0';       /* Zero out the partial string *
  194                          * so strcat works properly.   */
  195     strcat(pout, eptr);                 /* Add on the second half.     */
  196     outLen = strlen(pout);            
  197     strcpy(string, pout);               /* Copy back to our original string */
  198     *pout = '\0';
  199     }
  200     
  201     regfree(rx);
  202     XFREE(rx);
  203     
  204     XFREE(pout);
  205     return count;
  206 }
  207 
  208 
  209 void 
  210 insertString(char **string, char *insert, int offset, int *size)
  211 {
  212   char *p, *len, *tmp;
  213   int  insertLen, stringLen, tmpLen;
  214 
  215   len = *string + offset; 
  216   stringLen = strlen(*string);
  217   insertLen = strlen(insert);
  218 
  219   tmp = XMALLOC(char, (stringLen+insertLen+1));
  220   for (p = *string; p < len; p++);
  221 
  222   strncpy(tmp, *string, (p - *string));
  223   tmp[(p - *string)] = '\0';
  224   strcat(tmp,insert);
  225   strcat(tmp,p);
  226   tmpLen = strlen(tmp);
  227   
  228   if (tmpLen >= *size) {
  229     *size = tmpLen * 2;    /* Double the string size */  
  230     *string = XREALLOC(char, *string, (*size));
  231   }
  232 
  233   strcpy(*string,tmp);
  234 
  235   XFREE(tmp);
  236 }
  237 
  238 
  239 /* Return a NULL-terminated array of strings. */
  240 int
  241 tokenizeString(char *string, int size, char ***output)
  242 {
  243     char  *start, *end;
  244     char  *p, *q;
  245     int   i;
  246 
  247     end = string + size;
  248     
  249     /* Remove spaces from end of string */
  250     for (p = string; p < end && *p != '\0'; p++);
  251     for (p--; isspace((int)*p); p--);
  252     end = p + 1;
  253 
  254     /* Remove spaces from beginning of string */
  255     for (p = string; p < end && isspace((int)*p); p++);
  256     start = p;
  257 
  258     /* If start == end, then we have an empty string. */
  259     if (start == end) {
  260     *output = XMALLOC(char *, 1);
  261     (*output)[0] = '\0';
  262     return 0;
  263     }
  264 
  265     /* Count number of tokens */
  266     for (p = end-1, i = 0; p > start; p--) {
  267     /* Treat quoted text as opaque, do not count spaces. */
  268     if (*p == '\'' || *p == '"') {                        
  269         for (p--; *p != '\'' && *p != '"' && p > start; p--);
  270     }                                                     
  271     if (isspace((int)*p) && !isspace((int)*(p-1))) {
  272         i++;                        
  273     }
  274     }
  275     
  276     /* Number of tokens equals spaces + 1 */
  277     i++;
  278     
  279     *output = XMALLOC(char *, i + 1);
  280     
  281     (*output)[i] = '\0';
  282 
  283     /* Copy tokens to the char array */
  284     for (i = 0, p = q = start; p < end && q < end; p = q, i++) {
  285     for (q = p; !isspace((int)*q) && q < end; q++) {
  286         
  287         /* If we hit a quote, forward to the end of the quote *
  288          * i.e. treat quoted text as an opaque token.         */
  289         if (*q == '\'' || *q == '"') {
  290         for (q++; *q != '\'' && *q != '"' && q < end; q++);
  291         }
  292     }      
  293     
  294     /* Copy the token to the string array */
  295     (*output)[i] = XMALLOC(char, (q - p + 1));
  296     strncpy((*output)[i], p, q-p);
  297     (*output)[i][q-p] = '\0';
  298     
  299     /* Forward past any extra spaces between tokens */
  300     for (q++; isspace((int)*q) && q < end; q++);
  301     }
  302     
  303     return 0;
  304 }
  305 
  306 
  307 int
  308 tokenizeURLString(char *string, int size, char ***output)
  309 {
  310     char  *start, *end;
  311     char  *p, *q;
  312     int   i;
  313 
  314     end = string + size;
  315     
  316     /* Remove spaces from end of string */
  317     for (p = string; p < end && *p != '\0'; p++);
  318     for (p--; isspace(*p); p--);
  319     end = p + 1;
  320 
  321     /* Remove spaces from beginning of string */
  322     for (p = string; p < end && isspace(*p); p++);
  323     start = p;
  324 
  325     /* If start == end, then we have an empty string. */
  326     if (start == end) {
  327     *output = XMALLOC(char *, 1);
  328     (*output)[0] = '\0';
  329     return 0;
  330     }
  331 
  332     /* Count number of tokens */
  333     for (p = end-1, i = 0; p > start; p--) {
  334     if (*p == '&') 
  335         i++;
  336     }
  337     
  338     /* Number of tokens equals spaces + 1 */
  339     i++;
  340     
  341     *output = XMALLOC(char *, i + 1);
  342     
  343     (*output)[i] = '\0';
  344 
  345     /* Copy tokens to the char array */
  346     for (i = 0, p = q = start; p < end && q < end; p = q, i++) {
  347 
  348     for (q = p; *q != '&' && q < end; q++);     
  349     
  350     /* Copy the token to the string array */
  351     (*output)[i] = XMALLOC(char, (q - p + 1));
  352     strncpy((*output)[i], p, q-p);
  353     (*output)[i][q-p] = '\0';
  354 
  355     if (q < end) q++;
  356     }
  357     
  358     return 0;
  359 }
  360 
  361 
  362 /* Free a NULL-terminated array of character strings. */
  363 int
  364 freeStringArray(char **string)
  365 {
  366     int i = 0;
  367 
  368     if (string == NULL)
  369     return 0;
  370 
  371     while (string[i] != '\0') {
  372     XFREE(string[i]);
  373     i++;
  374     }
  375 
  376     XFREE(string);
  377 
  378     return 0;
  379 }
  380 
  381 
  382 /* Parse a token, such as 'charset=ISO-8859-1' *
  383  * or 'filename="pic17958.pcx"' or 'inline;'   */
  384 int
  385 parseToken(char *token, char **property, char **value)
  386 {
  387     char *start, *end;
  388     char *p, *q;
  389     int   tlen;
  390 
  391     token? (tlen = strlen(token)) : (tlen = 0);
  392     end = token + tlen;
  393 
  394     /* Return if string is empty or NULL. */
  395     if (tlen == 0) {
  396     *property = NULL;
  397     *value    = NULL;
  398     return 0;
  399     }
  400 
  401     /* Remove spaces from end of string */
  402     for (p = token; p < end && *p != '\0'; p++);
  403     for (p--; isspace((int)*p); p--);
  404     end = p + 1;
  405 
  406     /* Remove spaces from beginning of string */
  407     for (p = token; p < end && isspace((int)*p); p++);
  408     start = p;
  409 
  410     /* Parse property. If no '=' found then treat *
  411      * the entire token as the value.             */
  412     for (p = start; *p != '=' && p < end; p++);
  413     if (p == end) {
  414     (*property) = NULL;
  415     p = start;
  416     }
  417     else {
  418     (*property) = XMALLOC(char, p - start + 1);
  419     strncpy(*property, start, p - start);
  420     (*property)[p-start] = '\0';
  421     }
  422     
  423     /* Parse the value */
  424     (p == start)? (q = p) : (q = p + 1);
  425     for (; p < end; p++);
  426     
  427     /* Remove semi-colon if it is at the end of the value. */
  428     if (*(p-1) == ';')
  429     p--;
  430 
  431     /* Remove quotes if they surround the value */
  432     if (*q == '"' || *q =='\'')
  433     q++;
  434     if (*(p-1) == '"' || *(p-1) =='\'')
  435     p--;
  436     (*value) = XMALLOC(char, p - q + 1);
  437     strncpy(*value, q, p - q);
  438     (*value)[p-q] = '\0';
  439     
  440     return 0;
  441 }
  442 
  443 
  444 /* Read a line in from the file stream and check to make  *
  445  * sure that the entire string was read. The string is    *
  446  * terminated by "\r\n" (CRLF). If the character array is *
  447  * too short, make it bigger.                             */
  448 int
  449 readCRLFLine(FILE *input, char **line, int *size)
  450 {
  451     off_t pos;
  452     char  buf[256];
  453     int   blen, llen;
  454 
  455     pos = ftell(input);
  456     memset(*line, 0, *size);   
  457     llen = 0;
  458 
  459     /* Make sure we read the entire line. */
  460     do {        
  461     fgets(buf, sizeof(buf), input);
  462     blen = strlen(buf);
  463     if (blen + llen <= (*size) - 1) {
  464         strcat(*line, buf);
  465         llen += blen;
  466     }
  467     else {
  468         *size *= 2;
  469         llen  += blen;
  470         *line  = XREALLOC(char, *line, *size);
  471         strcat(*line, buf);
  472     }
  473     } while (buf[blen - 1] != '\n'&& !feof(input));
  474     
  475     return 0;
  476 }