home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / elm / elm2.4 / lib / strtokq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-02  |  2.3 KB  |  83 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: strtokq.c,v 5.2 1993/02/03 16:20:30 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.2 $   $State: Exp $
  6.  *
  7.  *            Copyright (c) 1988-1992 USENET Community Trust
  8.  *            Copyright (c) 1986,1987 Dave Taylor
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log: strtokq.c,v $
  17.  * Revision 5.2  1993/02/03  16:20:30  syd
  18.  * add include file
  19.  *
  20.  * Revision 5.1  1993/02/03  16:19:31  syd
  21.  * Initial checkin, taken from filter/parse.c
  22.  *
  23.  *
  24.  ******************************************************************************/
  25.  
  26.  
  27. /* Like strtok, but returns quoted strings as one token (quotes removed)
  28.  * if flag is non-null. Quotes and backslahes can be escaped with backslash.
  29.  */
  30.  
  31. #include "headers.h"
  32.  
  33. char *strtokq(source, keys, flag)
  34. char *source, *keys;
  35. int flag;
  36. {
  37.     register int  last_ch;
  38.     static   char *sourceptr = NULL;
  39.          char *return_value;
  40.  
  41.     if (source != NULL)
  42.       sourceptr = source;
  43.     
  44.     if (sourceptr == NULL || *sourceptr == '\0') 
  45.       return(NULL);        /* we hit end-of-string last time!? */
  46.  
  47.     sourceptr += strspn(sourceptr, keys);    /* skip leading crap */
  48.     
  49.     if (*sourceptr == '\0') 
  50.       return(NULL);        /* we've hit end-of-string */
  51.  
  52.     if (flag)
  53.       if (*sourceptr == '"' || *sourceptr == '\'') {   /* quoted string */
  54.         register char *sp;
  55.         char quote = *sourceptr++;
  56.  
  57.         for (sp = sourceptr+1; *sp != '\0' && *sp != quote; sp++)
  58.           if (*sp == '\\') sp++;    /* skip escaped characters */
  59.                     /* expand_macros will fix them later */
  60.  
  61.         return_value = sourceptr;
  62.         sourceptr = sp;
  63.         if (*sourceptr != '\0') sourceptr++;
  64.         *sp = '\0';                /* zero at end */
  65.  
  66.         return return_value;
  67.       }
  68.  
  69.     last_ch = strcspn(sourceptr, keys);    /* end of good stuff */
  70.  
  71.     return_value = sourceptr;        /* and get the ret   */
  72.  
  73.     sourceptr += last_ch;            /* ...value          */
  74.  
  75.     if (*sourceptr != '\0')    /* don't forget if we're at END! */
  76.       sourceptr++;           /* and skipping for next time */
  77.  
  78.     return_value[last_ch] = '\0';        /* ..ending right    */
  79.     
  80.     return((char *) return_value);        /* and we're outta here! */
  81. }
  82.  
  83.