home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pccts.zip / pccts / EXTEND.nonprinting.char < prev    next >
Text File  |  1994-03-31  |  5KB  |  223 lines

  1.              Printing non-printing chars for syntax errors
  2.  
  3.                   for ANTLR 1.10 (and hopefully 1.20)
  4.  
  5.                                 by
  6.  
  7.                     tmoog@polhode.com (Tom Moog)
  8.  
  9. /******************************************************************************/
  10. /*
  11.   zzexpand_text
  12.  
  13.   For use with pccts
  14.   No copyright restrictions
  15.   No liability accepted
  16.  
  17.     Copies a string from the source to the destination while replacing
  18.     recognized characters with a string from a table.  Non-printable
  19.     characters NOT found in the table are expanded with a string
  20.     composed of their octal equivalent.  If the destination string
  21.     is not long enough to contain the expanded string a "..." is
  22.     appended to the output.
  23.  
  24.     Returns a pointer to the destination string.
  25.  
  26.     Example using the default table:
  27.  
  28.     "abc"            -> "abc"
  29.     "abc\n"            -> "abc<newline>"
  30.     "abc\n\fdef"        -> "abc<newline>
  31.                     <form feed>         
  32.                     def"
  33.     "abc\tdef"        -> "abc<tab>def"
  34.     "abc\1\2\177"        -> "abc<\001><\002><delete>"
  35.  
  36.    Prototype:
  37.  
  38.       char *zzexpand_text (char               *source,
  39.                          Zzexpand_text_entry *table,
  40.                          char             *dest,
  41.                          size_t          ldest)
  42.  
  43.        If the table is coded as 0 then the default table is used
  44. */
  45.  
  46. /*
  47.     Initial Release:  9-Mar-94
  48. */
  49.  
  50. #include <string.h>
  51. #include <ctype.h>
  52.  
  53. #include "expandtext.h"
  54.  
  55. #if __STDC__
  56. #include <limits.h>
  57. #define ZZCHAR_MASK UCHAR_MAX
  58. #else
  59. #define ZZCHAR_MASK 255
  60. #endif
  61.  
  62. Zzexpand_text_entry
  63.          zzdefault_expand_text_table[]={
  64. #ifdef __STDC__
  65.                 {'\a'   ,"<bell>"},
  66. #else
  67.                 {'\007' ,"<bell>"},
  68. #endif
  69.                 {'\b'   ,"<back space>"},
  70.                 {'\t'   ,"<tab>"},
  71.                 {'\n'   ,"<newline>\n"},
  72.                 {'\v'   ,"<vtab>"},
  73.                 {'\f'   ,"\n<form feed>\n"},
  74.                 {'\r'   ,"<return>\n"},
  75.                 {'\033' ,"<esc>"},
  76.                 {'\177' ,"<del>"},
  77.                 {0      ,0},
  78. };
  79.  
  80. Zzexpand_text_entry (*zzexpand_text_table)[]=0;
  81.  
  82. /******************************************************************************/
  83.  
  84.  
  85. #ifdef __STDC__
  86. char * zzexpand_text (char             *source,
  87.                      Zzexpand_text_entry *table,
  88.                      char            *dest,
  89.                      size_t            ldest)
  90. #else
  91. char * zzexpand_text (source,
  92.                   table,
  93.                   dest,
  94.                   ldest)
  95.  
  96. char            *source;
  97. Zzexpand_text_entry *table;
  98. char            *dest;
  99. size_t            ldest;
  100. #endif
  101.  
  102. {
  103. static Zzexpand_text_entry *current_table;
  104. static char *d;
  105. static Zzexpand_text_entry *next;
  106. static int amount_left;
  107. static char *replace;
  108. static int extra_newline;
  109. static char char_as_octal[12];
  110. static int l;
  111. static char dot_dot_dot[]=" ...";
  112.  
  113. extra_newline=0;
  114. d=dest;
  115.  
  116. amount_left=ldest-sizeof(dot_dot_dot)-5; 
  117.                 /* Leave room for " ..." + null */
  118.  
  119. if (dest==0) return dest;
  120. if (source==0) {
  121.    *dest=0;
  122.    return dest;
  123. };
  124.  
  125. if (ldest <= 0) return dest;
  126.  
  127. if (amount_left <=0) {
  128.    strncpy(dest,dot_dot_dot,ldest);
  129.    dest[ldest-1]=0;
  130.    return dest;
  131. };
  132.  
  133. if (table==0) {
  134.   current_table=zzdefault_expand_text_table;
  135. } else {
  136.   current_table=table;
  137. };
  138.  
  139. for ( ;*source!=0;source++) {
  140.  
  141.   if (amount_left <= 0) {
  142.      amount_left=(-1);
  143.      break;
  144.   };
  145.  
  146. /* first search the table then check isprint so that user can
  147.    choose how to represent a space character or anyother printable
  148.    character */
  149.  
  150.   next=current_table;
  151.   for ( ;(next->text!=0) &&
  152.          (next->match!=*source);
  153.           next++) { continue; };
  154.  
  155. /* not in table:                    */
  156. /*     if printable copy directly to the destination   */
  157. /*     otherwise encode as octal character        */
  158.  
  159.   if (next->text==0) {
  160.      if (isprint(*source)) {
  161.        extra_newline=0;            /* printable */
  162.        *d++=*source;
  163.        amount_left--;
  164.       } else {                /* not printable */
  165.  
  166.         sprintf (char_as_octal,"<\\%.3o>",(*source) & ZZCHAR_MASK);
  167.  
  168.     l=strlen(char_as_octal);
  169.         amount_left-=l;
  170.         if (amount_left < 0) {
  171.           break;
  172.         };
  173.         strcpy(d,char_as_octal);
  174.         d+=l;
  175.       };
  176.    } else {                /* in table */
  177.      replace=next->text;
  178.      l=strlen(replace);
  179.      amount_left-=l;
  180.      if (amount_left < 0) {
  181.     break;
  182.      };
  183.         /* check for two artificial newlines in a row */
  184.  
  185.      if (*replace=='\n') {    /* does expansion text start with \n     */
  186.     if (d==dest) {        /* don't insert \n at start         */
  187.       replace++;
  188.       amount_left--;
  189.       l--;
  190.         };
  191.     if (extra_newline) {    /* don't insert two \n             */
  192.       d--;
  193.       amount_left++;
  194.     };
  195.      };
  196.      strcpy (d,replace);
  197.      d+=l;
  198.      if (replace[l-1]=='\n') {
  199.     extra_newline=1;
  200.      } else {
  201.     extra_newline=0;
  202.      };
  203.   };
  204. };
  205.  
  206.  
  207. if (extra_newline) {
  208.   d--;
  209. };
  210.  
  211. /*  Don't get alarmed - remember that some extra space was reserved */
  212.  
  213. if (amount_left < 0) {
  214.     strcpy (d,dot_dot_dot);
  215.     d+=strlen(dot_dot_dot);
  216. };
  217.  
  218. *d=0;
  219. return dest;
  220. }
  221.  
  222.  
  223.