home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume3 / totri / untri.c < prev   
Encoding:
C/C++ Source or Header  |  1989-02-03  |  1.2 KB  |  61 lines

  1. /* untri - convert ANSI C trigraph sequences to single characters.
  2.  *         currently implemented as a filter, but a rewritten main
  3.  *         could allow a more sophisticated interface.
  4.  *
  5.  *  This source donated to the public domain by John P. Nelson 1988
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <strings.h>
  10.  
  11. char *trichar = "=(/)'<!>-";
  12. char *translate = "#[\\]^{|}~";
  13. main()
  14.     {
  15.     process(stdin, stdout);
  16.     }
  17.  
  18. /*
  19.  * Note:  I used a goto in this function, because we are essentially
  20.  *     performing a two character lookahead, but unputc is only guaranteed
  21.  *     to be able to push back one character.  Otherwise, the goto would be
  22.  *     unnecessary.
  23.  */
  24. process(in, out)
  25. FILE *in, *out;
  26.     {
  27.     int c;
  28.     char *ptr;
  29.  
  30.     while ((c = getchar(in)) != EOF)
  31.     {
  32. reprocess:
  33.     if (c == '?')
  34.         {
  35.         if ((c = getc(in)) != '?')
  36.         {
  37.         if (c != EOF)
  38.             ungetc(c, in);
  39.         putc('?', out);
  40.         continue;
  41.         }
  42.         c = getc(in);
  43.         if (c != EOF)
  44.         {
  45.         if (ptr = strchr(trichar, c))
  46.             {
  47.             /* yup, it's a trigraph */
  48.             putc(translate[ptr - trichar], out);
  49.             continue;
  50.             }
  51.         ungetc(c, in);
  52.         }
  53.         putc('?', out);
  54.         c = '?';
  55.         /* ungetc('?', in); continue; */
  56.         goto reprocess;
  57.         }
  58.     putc(c, out);
  59.     }
  60.     }
  61.