home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / general / unvis.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  5KB  |  232 lines

  1. /*-
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted provided
  6.  * that: (1) source distributions retain this entire copyright notice and
  7.  * comment, and (2) distributions including binaries display the following
  8.  * acknowledgement:  ``This product includes software developed by the
  9.  * University of California, Berkeley and its contributors'' in the
  10.  * documentation or other materials provided with the distribution and in
  11.  * all advertising materials mentioning features or use of this software.
  12.  * Neither the name of the University nor the names of its contributors may
  13.  * be used to endorse or promote products derived from this software without
  14.  * specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  16.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  17.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "@(#)unvis.c    1.3 (Berkeley) 6/27/90";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. #define _KERNEL
  25. #include "ixemul.h"
  26.  
  27. #include <vis.h>
  28. #include <ctype.h>
  29.  
  30. /*
  31.  * decode driven by state machine
  32.  */
  33. #define    S_GROUND    0    /* haven't seen escape char */
  34. #define    S_START        1    /* start decoding special sequence */
  35. #define    S_META        2    /* metachar started (M) */
  36. #define    S_META1        3    /* metachar more, regular char (-) */
  37. #define    S_CTRL        4    /* control char started (^) */
  38. #define    S_OCTAL2    5    /* octal digit 2 */
  39. #define    S_OCTAL3    6    /* octal digit 3 */
  40.  
  41. #define    isoctal(c)    (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
  42.  
  43. /*
  44.  * unvis - decode characters previously encoded by vis
  45.  */
  46. int
  47. unvis(char *cp, char c, int *astate, int flag)
  48. {
  49.  
  50.     if (flag & UNVIS_END) {
  51.         if (*astate == S_OCTAL2 || *astate == S_OCTAL3) {
  52.             *astate = S_GROUND;
  53.             return (UNVIS_VALID);
  54.         } 
  55.         return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
  56.     }
  57.  
  58.     switch (*astate) {
  59.  
  60.     case S_GROUND:
  61.         *cp = 0;
  62.         if (c == '\\') {
  63.             *astate = S_START;
  64.             return (0);
  65.         } 
  66.         *cp = c;
  67.         return (UNVIS_VALID);
  68.  
  69.     case S_START:
  70.         switch(c) {
  71.         case '\\':
  72.             *cp = c;
  73.             *astate = S_GROUND;
  74.             return (UNVIS_VALID);
  75.         case '0': case '1': case '2': case '3':
  76.         case '4': case '5': case '6': case '7':
  77.             *cp = (c - '0');
  78.             *astate = S_OCTAL2;
  79.             return (0);
  80.         case 'M':
  81.             *cp = 0200;
  82.             *astate = S_META;
  83.             return (0);
  84.         case '^':
  85.             *astate = S_CTRL;
  86.             return (0);
  87.         case 'n':
  88.             *cp = '\n';
  89.             *astate = S_GROUND;
  90.             return (UNVIS_VALID);
  91.         case 'r':
  92.             *cp = '\r';
  93.             *astate = S_GROUND;
  94.             return (UNVIS_VALID);
  95.         case 'b':
  96.             *cp = '\b';
  97.             *astate = S_GROUND;
  98.             return (UNVIS_VALID);
  99.         case 'a':
  100.             *cp = '\007';
  101.             *astate = S_GROUND;
  102.             return (UNVIS_VALID);
  103.         case 'v':
  104.             *cp = '\v';
  105.             *astate = S_GROUND;
  106.             return (UNVIS_VALID);
  107.         case 't':
  108.             *cp = '\t';
  109.             *astate = S_GROUND;
  110.             return (UNVIS_VALID);
  111.         case 'f':
  112.             *cp = '\f';
  113.             *astate = S_GROUND;
  114.             return (UNVIS_VALID);
  115.         case 's':
  116.             *cp = ' ';
  117.             *astate = S_GROUND;
  118.             return (UNVIS_VALID);
  119.         case 'E':
  120.             *cp = '\033';
  121.             *astate = S_GROUND;
  122.             return (UNVIS_VALID);
  123.         case '\n':
  124.             /*
  125.              * hidden newline
  126.              */
  127.             *astate = S_GROUND;
  128.             return (UNVIS_NOCHAR);
  129.         case '$':
  130.             /*
  131.              * hidden marker
  132.              */
  133.             *astate = S_GROUND;
  134.             return (UNVIS_NOCHAR);
  135.         }
  136.         *astate = S_GROUND;
  137.         return (UNVIS_SYNBAD);
  138.          
  139.     case S_META:
  140.         if (c == '-')
  141.             *astate = S_META1;
  142.         else if (c == '^')
  143.             *astate = S_CTRL;
  144.         else {
  145.             *astate = S_GROUND;
  146.             return (UNVIS_SYNBAD);
  147.         }
  148.         return (0);
  149.          
  150.     case S_META1:
  151.         *astate = S_GROUND;
  152.         *cp |= c;
  153.         return (UNVIS_VALID);
  154.          
  155.     case S_CTRL:
  156.         if (c == '?')
  157.             *cp |= 0177;
  158.         else
  159.             *cp |= c & 037;
  160.         *astate = S_GROUND;
  161.         return (UNVIS_VALID);
  162.  
  163.     case S_OCTAL2:    /* second possible octal digit */
  164.         if (isoctal(c)) {
  165.             /* 
  166.              * yes - and maybe a third 
  167.              */
  168.             *cp = (*cp << 3) + (c - '0');
  169.             *astate = S_OCTAL3;    
  170.             return (0);
  171.         } 
  172.         /* 
  173.          * no - done with current sequence, push back passed char 
  174.          */
  175.         *astate = S_GROUND;
  176.         return (UNVIS_VALIDPUSH);
  177.  
  178.     case S_OCTAL3:    /* third possible octal digit */
  179.         *astate = S_GROUND;
  180.         if (isoctal(c)) {
  181.             *cp = (*cp << 3) + (c - '0');
  182.             return (UNVIS_VALID);
  183.         }
  184.         /*
  185.          * we were done, push back passed char
  186.          */
  187.         return (UNVIS_VALIDPUSH);
  188.             
  189.     default:    
  190.         /* 
  191.          * decoder in unknown state - (probably uninitialized) 
  192.          */
  193.         *astate = S_GROUND;
  194.         return (UNVIS_SYNBAD);
  195.     }
  196. }
  197.  
  198. /*
  199.  * strunvis - decode src into dst 
  200.  *
  201.  *    Number of chars decoded into dst is returned, -1 on error.
  202.  *    Dst is null terminated.
  203.  */
  204.  
  205. int strunvis(char *dst, const char *src)
  206. {
  207.     register char c;
  208.     char *start = dst;
  209.     int state = 0;
  210.  
  211.     while ((c = *src++)) {
  212.     again:
  213.         switch (unvis(dst, c, &state, 0)) {
  214.         case UNVIS_VALID:
  215.             dst++;
  216.             break;
  217.         case UNVIS_VALIDPUSH:
  218.             dst++;
  219.             goto again;
  220.         case 0:
  221.         case UNVIS_NOCHAR:
  222.             break;
  223.         default:
  224.             return (-1);
  225.         }
  226.     }
  227.     if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID)
  228.         dst++;
  229.     *dst = '\0';
  230.     return (dst - start);
  231. }
  232.