home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume15 / nroffgraphics / part01 / escapes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-06  |  2.2 KB  |  98 lines

  1. /*
  2.  * escape.c -- interpret and expand character escapes
  3.  *
  4.  * This code brought to you as a public service by Eric S. Raymond, Feb 1988
  5.  * and is copyrighted (c)1988 by the author. Use, distribute, and mangle
  6.  * freely, but don't try to make money selling it unless you're going to send
  7.  * me a cut. Send bug reports, love letters and death threats to eric@snark
  8.  * aka ...!rutgers!vu-vlsi!snark!eric.
  9.  */
  10. /*LINTLIBRARY*/
  11. #include <stdio.h>
  12. #include <ctype.h>
  13.  
  14. int escape(cp, tp)
  15. /* interpret standard C-style octal and hex escapes plus \e for ESC */
  16. char    *cp, *tp;
  17. {
  18.     extern char *strchr();
  19.     int ccount = 0;
  20.  
  21.     while (*cp)
  22.     {
  23.     int    cval = 0;
  24.  
  25.     if (*cp == '\\' && strchr("0123456789xX", cp[1]))
  26.     {
  27.         char *dp, *hex = "00112233445566778899aAbBcCdDeEfF";
  28.         int dcount = 0;
  29.  
  30.         if (*++cp == 'x' || *cp == 'X')
  31.         for (++cp; (dp = strchr(hex, *cp)) && dcount++ < 2; cp++)
  32.             cval = (cval * 16) + (dp - hex) / 2;
  33.         else if (*cp == '0')
  34.         while (strchr("01234567", *cp) && dcount++ < 3)
  35.             cval = (cval * 8) + (*cp++ - '0');
  36.         else
  37.         while (strchr("0123456789", *cp) && dcount++ < 3)
  38.             cval = (cval * 10) + (*cp++ - '0');
  39.     }
  40.     else if (*cp == '\\')        /* C-style character escape */
  41.     {
  42.         switch (*++cp)
  43.         {
  44.         case '\\': cval = '\\'; break;
  45.         case 'n': cval = '\n'; break;
  46.         case 't': cval = '\t'; break;
  47.         case 'b': cval = '\b'; break;
  48.         case 'r': cval = '\r'; break;
  49.         case 'e': cval = 0x1b; break;
  50.         default: cval = *cp;
  51.         }
  52.         cp++;
  53.     }
  54.     else
  55.         cval = *cp++;
  56.     *tp++ = cval;
  57.     ccount++;
  58.     }
  59.     *tp = '\0';
  60.     return(ccount);
  61. }
  62.  
  63. int expand(sp, tp)
  64. /* generate a restricted set of escapes for nonprintable chars in a string */
  65. char *sp;
  66. char *tp;
  67. {
  68.     char *start;
  69.  
  70.     for (start = tp; *sp; sp++)
  71.     {
  72.     if (!isprint(*sp) || *sp == '\\')
  73.         *tp++ = '\\';
  74.  
  75.     if (*sp == '\\')
  76.         *tp++ = '\\';
  77.     else if (*sp == '\b')
  78.         *tp++ = '\b';
  79.     else if (*sp == '\n')
  80.         *tp++ = '\n';
  81.     else if (*sp == '\t')
  82.         *tp++ = '\t';
  83.     else if (*sp == '\r')
  84.         *tp++ = '\r';
  85.     else if (isprint(*sp))
  86.         *tp++ = *sp;
  87.     else
  88.     {
  89.         (void) sprintf(tp, "\\%03.3o", toascii(*sp));
  90.         tp += strlen(tp);
  91.     }
  92.     }
  93.     *tp = '\0';
  94.     return(tp - start);
  95. }
  96.  
  97. /* escape.c ends here */
  98.