home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / c / abmake14.arc / tstring.c < prev    next >
C/C++ Source or Header  |  1989-01-03  |  4KB  |  205 lines

  1. /*
  2.  * tstring.c
  3.  *
  4.  * 88-10-01 v1.0    created by greg yachuk, placed in the public domain
  5.  * 88-10-06 v1.1    changed prerequisite list handling
  6.  *
  7.  */
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include <malloc.h>
  11. #include <string.h>
  12.  
  13. #include "tstring.h"
  14.  
  15.  
  16. void *talloc(n)
  17. int    n;
  18. {
  19. register void *s;
  20.  
  21.     s = malloc(n);
  22.     if (s == NULL)
  23.         terror(1, "no free memory");
  24.     return (s);
  25. }
  26.  
  27.  
  28. void tfree(s)
  29. void    *s;
  30. {
  31.     if (s != NULL)
  32.         free(s);
  33. }
  34.  
  35.  
  36. void *trealloc(s, n)
  37. register char *s;
  38. int    n;
  39. {
  40.     s = realloc(s, n);
  41.     if (s == NULL)
  42.         talloc(n);                /* force an error */
  43.     return (s);
  44. }
  45.  
  46.  
  47. void *tstrncpy(s, n)
  48. register void *s;
  49. int    n;
  50. {
  51.     s = strncpy(talloc(n+1), s, n);
  52.     *((char *)s + n) = '\0';
  53.     return (s);
  54. }
  55.  
  56.  
  57. terror(n, s)
  58. int    n;
  59. char   *s;
  60. {
  61.     fputs("Make: ", stderr);
  62.     fputs(s, stderr);
  63.     if (n)
  64.         exit(n);
  65. }
  66.  
  67.  
  68. /*
  69.  * token    - take an input string and return a token each call
  70.  *            - default token delimiter characters are `isspace()'
  71.  *            - text between quotes (" and ') is a single token
  72.  *
  73.  *    called as    s = token(string, seps);
  74.  *            or    s = token(string, NULL);
  75.  *
  76.  *    followed by    s = token(NULL, seps);
  77.  *            or    s = token(NULL, NULL);
  78.  *
  79.  *    returns NULL when no more tokens are available
  80.  */
  81. char *token(s, sep)
  82. char   *s;
  83. char   *sep;
  84. {
  85. static    char *olds = NULL;
  86. char    quote = 0;                /* processing a quoted token */
  87.  
  88.     if ( s )
  89.         olds = s;                /* we are starting all over again */
  90.  
  91.     if ( !olds || !*olds )
  92.         return (NULL);            /* no tokens left */
  93.  
  94.     while ( isspace(*olds) || (sep && strchr(sep, *olds)) )
  95.         ++olds;                    /* skip leading spaces and sep's */
  96.     s = olds;
  97.  
  98.     while ( *olds )
  99.     {
  100.         if ( isspace(*olds) || (sep && strchr(sep, *olds)) )
  101.         {
  102.             *olds++ = '\0';        /* delimit the token */
  103.             return (s);
  104.         }
  105.         else if ( *olds == '"' || *olds == '\'' )
  106.         {
  107.             quote = *olds++;
  108.             while ( *olds != quote && *olds )
  109.                 ++olds;            /* search for the proper quote char */
  110.             if ( *olds != '\0' )
  111.                 ++olds;            /* didn't hit eos, so skip quote */
  112.         }
  113.         else
  114.             ++olds;                /* otherwise, pass over char */
  115.     }
  116.  
  117.     olds = NULL;
  118.     return (s);                    /* return last token */
  119. }
  120.  
  121.  
  122. /*
  123.  * tokenize    - chop a string up into an array of (char *)'s
  124.  */
  125. char  **tokenize(input)
  126. char   *input;
  127. {
  128. char  **argv;
  129. register int argc = 0;
  130. register int alen;
  131.  
  132.     alen = 20;        /* good initial guess */
  133.     argv = (char **)talloc((alen + 1) * sizeof(char *));
  134.  
  135.     input = token(input, NULL);    /* use default separators */
  136.     while (input)
  137.     {
  138.         if (alen == argc)
  139.             argv = (char **) trealloc((char *) argv,
  140.                                             (alen <<= 1) * sizeof(char *));
  141.         argv[argc++] = input;
  142.         input = token(NULL, NULL);
  143.     }
  144.  
  145.     argv[argc] = NULL;    /* mark end of array */
  146.  
  147.     return (argv);
  148. }
  149.  
  150.  
  151. /*
  152.  * tgets    - read input, swallowing escaped newlines as necessary
  153.  */
  154. char *tgets(fd)
  155. FILE   *fd;
  156. {
  157. static    char *input = NULL;
  158. static    int inlen = 0;
  159. register char *ep;
  160. int        len;
  161.  
  162.     if ( inlen == 0 )
  163.         input = talloc(inlen = 162);
  164.  
  165.     input[inlen-2] = '\n';
  166.     ep = input - 1;
  167.     while ((fgets(input, inlen, fd)) != NULL)
  168.     {
  169.         for(;;)
  170.         {
  171.             while (input[inlen-2] != '\n' && input[inlen-2] != '\0')
  172.             {
  173.                 len = inlen;
  174.                 input = trealloc(input, inlen <<= 1);
  175.                 ep = &input[len-2];
  176.                 input[inlen-2] = '\n';
  177.                 fgets(ep+1, len+1, fd);
  178.             }
  179.  
  180.             while (*++ep)
  181.                 {}
  182.  
  183.             *--ep = '\0';
  184.             do {
  185.                 --ep;
  186.             } while (isspace(*ep) && ep >= input);
  187.  
  188.             if ( *ep == '\\' && *(--ep) != '\\')
  189.                 fgets(ep+1, inlen - (ep - input) - 1, fd);
  190.             else
  191.                 break;
  192.         }
  193.  
  194.         return (input);
  195.     }
  196.  
  197.     inlen = 0;
  198.     tfree(input);
  199.     input = NULL;
  200.  
  201.     return (NULL);
  202. }
  203.  
  204.  
  205.