home *** CD-ROM | disk | FTP | other *** search
/ cs.rhul.ac.uk / www.cs.rhul.ac.uk.zip / www.cs.rhul.ac.uk / pub / rdp / rdp_cs3470.tar / rdp_supp / textio.c~ < prev    next >
Text File  |  1998-05-07  |  21KB  |  778 lines

  1. /*******************************************************************************
  2. *
  3. * RDP release 1.50 by Adrian Johnstone (A.Johnstone@rhbnc.ac.uk) 20 December 1997
  4. *
  5. * textio.c - file opening, text buffering and error reporting
  6. *
  7. * This file may be freely distributed. Please mail improvements to the author.
  8. *
  9. *******************************************************************************/
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdarg.h>
  14. #include <time.h>
  15. #include <ctype.h>
  16. #include "scan.h"
  17. #include "memalloc.h"
  18. #include "symbol.h"
  19. #include "set.h"
  20. #include "textio.h"
  21.  
  22. #define MAX_ECHO 9              /* Maximum number of error markers per line */
  23. char * text_bot = NULL;       /* text array for storing id's and strings */
  24. char * text_top;              /* top of text character */
  25. static unsigned maxerrors = 25;  /* crash if error count exceeds this value */
  26. static unsigned maxwarnings = 100;  /* crash if warning count exceeds this value */
  27. static unsigned totalerrors = 0;  /* total number of errors this run */
  28. static unsigned totalwarnings = 0;  /* total number of warnings this run */
  29. static size_t maxtext = 20000;  /* size of text buffer */
  30. static unsigned tabwidth = 8;  /* tab expansion width: 2 is a good value for tgrind */
  31. static FILE * messages = stdout;  /* TEXT_MESSAGES; */
  32.  
  33. static unsigned errors = 0;   /* total errors for this file */
  34. static FILE * file = NULL;    /* current file handle */
  35. static char * first_char;     /* first character of current source line */
  36. static char * last_char;      /* last character of current source line */
  37. static unsigned long linenumber = 0;  /* current line in this file */
  38. static char * name = NULL;    /* filename */
  39. int text_char = ' ';          /* current text character */
  40. char * text_current;          /* pointer to current source character */
  41. void * text_scan_data;        /* pointer to the last thing read by the scanner */
  42. static char * symbol_first_char;  /* first character if this symbol */
  43. static unsigned warnings = 0;  /* total warnings for this file */
  44.  
  45. /* data block for linked list of stored file contexts */
  46. static struct source_list
  47. {
  48.   char * name;                /* copy of filename */
  49.   unsigned errors;            /* copy of total errors for this file */
  50.   FILE * file;                /* copy of current file handle */
  51.   char * first_char;          /* copy of first character of current source line */
  52.   char * last_char;           /* copy of last character of current source line */
  53.   unsigned long linenumber;   /* copy of current line in this file */
  54.   int text_char;              /* copy of current text character */
  55.   char * text_current;        /* copy of pointer to current source character */
  56.   scan_data text_scan_data;   /* copy of pointer to the last thing read by the scanner */
  57.   char * symbol_first_char;   /* copy of first character if this symbol */
  58.   unsigned warnings;          /* copy of total warnings for this file */
  59.   
  60.   struct source_list * previous;  /* previous file descriptor */
  61. } * source_descriptor_list = NULL;  /* head of file descriptor list */
  62.  
  63. static int echo = 0;          /* enable line echoing */
  64. static int echo_pos[MAX_ECHO];  /* array of error positions */
  65. static int echo_num = - 1;    /* current error number this line */
  66.  
  67. char * text_capitalise_string(char * str)
  68. {
  69.   char * ret = str; 
  70.   
  71.   int non_alpha_seen = 1; 
  72.   
  73.   while (* str != 0)
  74.   {
  75.     if (non_alpha_seen && isalpha(* str))
  76.       * str =(char) toupper(* str); 
  77.     else if (!non_alpha_seen && isalpha(* str))
  78.       * str =(char) tolower(* str); 
  79.     
  80.     non_alpha_seen = !isalpha(* str); 
  81.     
  82.     str++; 
  83.   }
  84.   
  85.   return ret; 
  86. }
  87.  
  88. unsigned long text_column_number(void)
  89. {
  90.   return first_char - text_current; 
  91. }
  92.  
  93. void text_dump(void)          /* debugging routine to dump text space */
  94. {
  95.   char * p = text_bot - 1; 
  96.   
  97.   while (p <= text_top)
  98.   {
  99.     while (++p <= text_top && * p != 0)
  100.       text_printf("%c", isprint(* p)? * p: '.'); 
  101.     
  102.     text_printf("\n"); 
  103.   }
  104. }
  105.  
  106. static void text_echo_line_number(void)
  107. {
  108.   if (linenumber != 0)
  109.     fprintf(messages, "%6lu: ", linenumber);  /* Print present line number */
  110.   else
  111.     fprintf(messages, "******: "); 
  112. }
  113.  
  114. static void text_echo_line(void)
  115. {
  116.   char * temp; 
  117.   
  118.   text_echo_line_number();    /* print line number */
  119.   
  120.   /* current input line is stored in reverse order at top of text buffer: print backwards from last character of text buffer */
  121.   for (temp = first_char - 1; temp > last_char; temp--)
  122.     fputc(* temp, messages); 
  123.   
  124.   /* now print out the echo number line */
  125.   if (echo_num >= 0)
  126.   {
  127.     int num_count = - 1, char_count = 1; 
  128.     
  129.     if (echo_num >= MAX_ECHO)
  130.       echo_num = MAX_ECHO - 1;  /* only the first MAX_ECHO errors have pointers */
  131.     text_echo_line_number(); 
  132.     
  133.     while (++num_count <= echo_num)
  134.     {
  135.       while (char_count++ < echo_pos[num_count]- 1)
  136.         fputc('-', messages); 
  137.       fputc('1' + num_count, messages); 
  138.     }
  139.     fputc('\n', messages); 
  140.   }
  141.   
  142.   echo_num = - 1;             /* reset echo numbering array pointer */
  143. }
  144.  
  145. static void text_close(void)
  146. {
  147.   struct source_list * temp = source_descriptor_list; 
  148.   
  149.   if (file == NULL)
  150.     return; 
  151.   
  152.   linenumber = 0;             /* switch off line number on messages */
  153.   
  154.   fclose(file);               /* close the file */
  155.   file = NULL; 
  156.   
  157.   if (source_descriptor_list != NULL) /* unload next file if there is one */
  158.   {
  159.     source_descriptor_list = source_descriptor_list->previous; 
  160.     
  161.     errors = temp->errors; 
  162.     file = temp->file; 
  163.     first_char = temp->first_char; 
  164.     last_char = temp->last_char; 
  165.     linenumber = temp->linenumber; 
  166.     name = temp->name; 
  167.     text_char = temp->text_char; 
  168.     text_current = temp->text_current; 
  169.     memcpy(text_scan_data, &(temp->text_scan_data), sizeof(scan_data)); 
  170.     symbol_first_char = temp->symbol_first_char; 
  171.     warnings = temp->warnings; 
  172.     
  173.     free(temp);               /* give the storage back */
  174.     
  175.     if (echo)
  176.     {
  177.       text_message(TEXT_INFO, "\n"); 
  178.       text_echo_line();       /* reecho line in case there are errors */
  179.     }
  180.   }
  181. }
  182.  
  183. void text_echo(const int i)
  184. {
  185.   echo = i; 
  186. }
  187.  
  188. int text_get_echo(void)
  189. {
  190.   return echo; 
  191. }
  192.  
  193. char * text_default_filetype(char * fname, const char * ftype)
  194. {
  195.   char * fullname; 
  196.   
  197.   if (* ftype == 0)           /* no file type to be added */
  198.     return fname;             /* no change */
  199.   
  200.   fullname =(char *) mem_malloc(strlen(fname)+ strlen(ftype)+ 2); 
  201.   
  202.   strcpy(fullname, fname); 
  203.   
  204.   if (strchr(fullname, '.')== NULL)
  205.   {
  206.     strcat(fullname, "."); 
  207.     strcat(fullname, ftype); 
  208.   }
  209.   
  210.   return fullname; 
  211. }
  212.  
  213. char * text_extract_filename(char * fname)
  214. {
  215.   char * name; 
  216.   char * temp; 
  217.   
  218.   name =(char *) mem_malloc(strlen(fname)+ 1); 
  219.   strcpy(name, fname); 
  220.   temp = name + strlen(fname); 
  221.   
  222.   /* search backwards for '.' and terminate the string there */
  223.   while (--temp > name)
  224.   {
  225.     if (* temp == '.')
  226.     {
  227.       * temp = 0; 
  228.       break; 
  229.     }
  230.   }
  231.   
  232.   if (* temp != 0)            /* we didn't find a dot, so start again at the end */
  233.     temp = name + strlen(fname); 
  234.   
  235.   /* search backwards for '/' or '\' and start the string there */
  236.   
  237.   while (--temp > name)
  238.   {
  239.     if (* temp == '/' || * temp == '\\')
  240.     {
  241.       name = temp + 1; 
  242.       break; 
  243.     }
  244.   }
  245.   
  246.   return name; 
  247. }
  248.  
  249. /* Table of string equivalents for ASCII codes */
  250. static char * text_ASCII_table =
  251. "NUL" "SOH" "STX" "ETX" "EOT" "ENQ" "ACK" "BEL" "BS" "HT" "LF" "VT" "FF"
  252. "CR" "SO" "SI" "DLE" "DC1" "DC2" "DC3" "DC4" "NAK" "SYN" "ETB" "CAN"
  253. "EM" "SUB" "ESC" "FS" "GS" "RS" "US" "SPACE" "SHREIK" "DBLQUOTE" "HASH"
  254. "DOLLAR" "PERCENT" "AMPERSAND" "QUOTE" "LPAR" "RPAR" "STAR" "PLUS"
  255. "COMMA" "MINUS" "PERIOD" "SLASH" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
  256. "COLON" "SEMICOLON" "LT" "EQUAL" "GT" "QUERY" "AT" "A" "B" "C" "D" "E"
  257. "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W"
  258. "X" "Y" "Z" "LBRACK" "BACKSLASH" "RBRACK" "UPARROW" "_" "`" "a" "b" "c"
  259. "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u"
  260. "v" "w" "x" "y" "z" "LBRACE" "BAR" "RBRACE" "TILDE" "DEL"; 
  261.  
  262. char * text_find_ASCII_element(int c)
  263. {
  264.   char * temp = text_ASCII_table; 
  265.   
  266.   if (c > 127)
  267.     return"???"; 
  268.   else
  269.     while (c-- > 0)
  270.     while (* temp++ != 0)
  271.     ; 
  272.   
  273.   return temp; 
  274. }
  275.  
  276. /* Release all memory held by the text package */
  277. void text_free(void)
  278. {
  279. }
  280.  
  281. /* A very time-inefficient routine to build an C identifier-compatible string
  282.    from an arbitrary ASCII string */
  283. char * text_make_C_identifier(char * str)
  284. {
  285.   int length = 0; 
  286.   char * temp = str; 
  287.   
  288.   /* First calculate the length of the completed string */
  289.   while (* temp != 0)
  290.     length += strlen(text_find_ASCII_element(* temp++)); 
  291.   
  292.   /* get space for output string */
  293.   temp =(char *) mem_malloc(length + 1); 
  294.   
  295.   * temp = 0;                 /* make temp an empty string */
  296.   
  297.   while (* str != 0)
  298.     strcat(temp, text_find_ASCII_element(* str++)); 
  299.   
  300.   return temp; 
  301. }
  302.  
  303. /* add a new filetype. If ftype is NULL, return just filename */
  304. char * text_force_filetype(char * fname, const char * ftype)
  305. {
  306.   char * fullname; 
  307.   size_t length; 
  308.   
  309.   /* work backwards from end of filename looking for a dot, or a directory separator */
  310.   
  311.   length = strlen(fname); 
  312.   while (fname[length]!= '.' && fname[length]!= '/' /* Unix */ &&
  313.     fname[length]!= '\\'      /* DOS */ && length > 0)
  314.   length--; 
  315.   
  316.   if (fname[length]!= '.')    /* no dot found */
  317.     length = strlen(fname); 
  318.   
  319.   if (ftype == NULL)
  320.   {
  321.     fullname =(char *) mem_malloc(length + 1); 
  322.     strncpy(fullname, fname, length); 
  323.     fullname[length]= 0; 
  324.   }
  325.   else
  326.   {
  327.     fullname =(char *) mem_malloc(length + strlen(ftype)+ 2); 
  328.     
  329.     strncpy(fullname, fname, length); 
  330.     fullname[length]= 0; 
  331.     strcat(fullname, "."); 
  332.     strcat(fullname, ftype); 
  333.   }
  334.   
  335.   return fullname; 
  336. }
  337.  
  338. void text_get_char(void)
  339. {                             /* advance text_current, reading another line if necessary */
  340.   if (text_current <= last_char)
  341.   {
  342.     if (file != NULL)
  343.       if (feof(file))
  344.       text_close(); 
  345.     
  346.     if (file == NULL)
  347.     {
  348.       text_char = EOF; 
  349.       return; 
  350.     }
  351.     
  352.     while (text_current <= last_char)
  353.     {
  354.       if ((echo || echo_num >= 0)&& linenumber > 0)
  355.         text_echo_line(); 
  356.       
  357.       linenumber++;           /* increment current line number */
  358.       last_char = text_current = first_char;  /* initialise pointers to empty line */
  359.       do
  360.       {
  361.         text_char = getc(file); 
  362.         * --last_char =(char) text_char; 
  363.         
  364.         if (text_char == EOF)
  365.           * last_char = ' ';  /* kludge */
  366.         else if (text_char == '\t' && tabwidth != 0) /* expand tabs to next tabstop */
  367.         {
  368.           * last_char = ' ';  /* make tab a space */
  369.           while ((text_current - last_char)% tabwidth != 0)
  370.             *(--last_char)= ' '; 
  371.         }
  372.       }
  373.       while (text_char != '\n' && text_char != EOF); 
  374.         
  375.       *(--last_char)= ' ';    /* kludge to ensure delayed echoing of lines */
  376.     }
  377.   }
  378.   text_char = * --text_current; 
  379. }
  380.  
  381. unsigned text_get_tab_width(void)
  382. {
  383.   return tabwidth; 
  384. }
  385.  
  386. void text_init(const long max_text, const unsigned max_errors, const unsigned max_warnings, const unsigned tab_width)
  387. {
  388.   tabwidth = tab_width; 
  389.   maxtext =(size_t) max_text;  /* set text buffer size */
  390.   
  391.   if ((long) maxtext != max_text)
  392.     text_message(TEXT_WARNING, "-T%lu too large for architecture: recast to -T%u\n", max_text, maxtext); 
  393.   maxerrors = max_errors;     /* set maximum number of errors per file */
  394.   maxwarnings = max_warnings;  /* set maximum number of warnings per file */
  395.   
  396.   text_bot =(char *) mem_malloc(maxtext);  /* allocate text buffer */
  397.   
  398.   text_top = text_bot;        /* top of text character */
  399.   text_current = last_char = first_char = text_bot + maxtext;  /* make new buffer region below top of text */
  400. }
  401.  
  402. char * text_insert_char(const char c)
  403. {
  404.   char * start = text_top; 
  405.   
  406.   if (text_top >= last_char)
  407.   {
  408.     #if 0
  409.     /* Dump buffer on overflow */
  410.     text_message(TEXT_INFO, "Ran out of text space - dumping buffer\n"); 
  411.     text_dump(); 
  412.     #endif
  413.     text_message(TEXT_FATAL, "Ran out of text space\n"); 
  414.   }
  415.   else
  416.     * text_top++ = c; 
  417.   
  418.   return start; 
  419. }
  420.  
  421. char * text_insert_characters(const char * str)
  422. {
  423.   char * start = text_top; 
  424.   
  425.   while (* str != '\0')
  426.     text_insert_char(* str++); 
  427.   
  428.   return start; 
  429. }
  430.  
  431. char * text_insert_integer(const unsigned n)
  432. {
  433.   char * start = text_top; 
  434.   
  435.   if (n > 9)
  436.     text_insert_integer(n / 10);  /* recursively handle multi-digit numbers */
  437.   text_insert_char((char)(n % 10 + '0')); 
  438.   
  439.   return start; 
  440. }
  441.  
  442. char * text_insert_string(const char * str)
  443. {
  444.   char * start = text_top; 
  445.   
  446.   do
  447.     text_insert_char(* str); 
  448.   while (* str++ != '\0'); 
  449.     
  450.   return start; 
  451. }
  452.  
  453. char * text_insert_substring(const char * prefix, const char * str, const unsigned n) /* put an id_number into text buffer */
  454. {
  455.   char * start = text_top; 
  456.   
  457.   text_insert_characters(prefix); 
  458.   text_insert_char('_'); 
  459.   text_insert_characters(str); 
  460.   text_insert_char('_'); 
  461.   text_insert_integer(n); 
  462.   text_insert_char('\0'); 
  463.   return start; 
  464. }
  465.  
  466. int text_is_valid_C_id(char * s)
  467. {
  468.   int temp; 
  469.   
  470.   temp =(isalpha(* s)|| * s == '_'); 
  471.   while (* ++s != '\0')
  472.     temp = temp &&(isalnum(* s)|| * s == '_'); 
  473.   return(temp); 
  474. }
  475.  
  476. unsigned long text_line_number(void)
  477. {
  478.   return linenumber; 
  479. }
  480.  
  481. char * text_lowercase_string(char * str)
  482. {
  483.   char * ret = str; 
  484.   
  485.   while (* str != 0)
  486.   {
  487.     * str =(char)(tolower(* str)); 
  488.     str++; 
  489.   }
  490.   
  491.   return ret; 
  492. }
  493.  
  494. /* A simple template matching routine: return true if the str parameter
  495.    matches the beginning of the current text line exactly, except that
  496.    any occurence of wildcard matches any character and any occurence
  497.    of digitwildcard matches any digit */
  498. int text_match_template(char * str, char wildcard, char digitwildcard)
  499. {
  500.   char * temp = first_char - 1; 
  501.   
  502.   while (1)
  503.   {
  504.     if (* str == 0)
  505.       return 1; 
  506.     
  507.     if ((temp <= last_char))
  508.       return 0; 
  509.     
  510.     if (!(* str == wildcard || * str == digitwildcard)&&(* str != * temp))
  511.       return 0; 
  512.     
  513.     if (* str == digitwildcard && !isdigit(* temp))
  514.       return 0; 
  515.     
  516.     str++; 
  517.     temp--; 
  518.   }
  519. }
  520.  
  521. int text_match_blank(void)
  522. {
  523.   char * temp = first_char - 1; 
  524.   
  525.   while (1)
  526.   {
  527.     if (isgraph(* temp))
  528.       return 0; 
  529.     
  530.     if ((temp <= last_char))
  531.       return 1; 
  532.     
  533.     temp--; 
  534.   }
  535. }
  536.  
  537. int text_message(const enum text_message_type type, const char * fmt, ...)
  538. {
  539.   int i; 
  540.   va_list ap;                 /* argument list walker */
  541.   
  542.   if (fmt == NULL)            /* No-op if errors are suppressed */
  543.     return 0; 
  544.   
  545.   if (type == TEXT_INFO_ECHO || type == TEXT_WARNING_ECHO ||
  546.     type == TEXT_ERROR_ECHO || type == TEXT_FATAL_ECHO)
  547.   {
  548.     if (++echo_num < MAX_ECHO)
  549.       echo_pos[echo_num]=(int)(first_char - text_current); 
  550.   }
  551.   
  552.   text_echo_line_number(); 
  553.   
  554.   fprintf(messages, "%s", 
  555.   type == TEXT_INFO || type == TEXT_INFO_ECHO ? "": 
  556.   type == TEXT_WARNING || type == TEXT_WARNING_ECHO ?(warnings++, totalwarnings++, "Warning "): 
  557.   type == TEXT_ERROR || type == TEXT_ERROR_ECHO ?(errors++, totalerrors++, "Error "): 
  558.   type == TEXT_FATAL || type == TEXT_FATAL_ECHO ? "Fatal ": "Unknown "); 
  559.   
  560.   if (type == TEXT_WARNING_ECHO || type == TEXT_ERROR_ECHO)
  561.     fprintf(messages, "%.1i ", echo_num + 1); 
  562.   
  563.   if (name != NULL && linenumber != 0)
  564.     fprintf(messages, "(%s) ", name); 
  565.   else if (type != TEXT_INFO && type != TEXT_INFO_ECHO)
  566.     fprintf(messages, "- "); 
  567.   
  568.   va_start(ap, fmt);          /* pass parameters to vprintf */
  569.   i = vfprintf(messages, fmt, ap);  /* remember count of characaters printed */
  570.   va_end(ap);                 /* end of var args block */
  571.   
  572.   if (type == TEXT_FATAL || type == TEXT_FATAL_ECHO)
  573.     exit(EXIT_FAILURE); 
  574.   
  575.   if ((errors > maxerrors)&&(maxerrors > 0))
  576.   {
  577.     fprintf(messages, "Fatal (%s): too many errors\n", 
  578.     name == NULL ? "null file": name); 
  579.     exit(EXIT_FAILURE); 
  580.   }
  581.   
  582.   if ((warnings > maxwarnings)&&(maxwarnings > 0))
  583.   {
  584.     fprintf(messages, "Fatal (%s): too many warnings\n", 
  585.     name == NULL ? "null file": name); 
  586.     exit(EXIT_FAILURE); 
  587.   }
  588.   
  589.   return i + 1;               /* return number of characters printed */
  590. }
  591.  
  592. FILE * text_open(char * s)
  593. {
  594.   FILE * handle, 
  595.   * old = file; 
  596.   
  597.   if (* s == '-')
  598.     handle = stdin; 
  599.   else
  600.     handle = fopen(s, "r");   /* try and get the file */
  601.   
  602.   if (handle != NULL)         /* we found a file */
  603.   {
  604.     if (old != NULL)          /* save current file context */
  605.     {
  606.       struct source_list * temp =(struct source_list *) mem_calloc(1, sizeof(struct source_list)); 
  607.       
  608.       /* load descriptor block */
  609.       temp->errors = errors; 
  610.       temp->file = file; 
  611.       temp->first_char = first_char; 
  612.       temp->last_char = last_char; 
  613.       temp->linenumber = linenumber; 
  614.       temp->name = name; 
  615.       temp->text_char = text_char; 
  616.       temp->text_current = text_current; 
  617.       memcpy(&(temp->text_scan_data), text_scan_data, sizeof(scan_data)); 
  618.       temp->symbol_first_char = symbol_first_char; 
  619.       temp->warnings = warnings; 
  620.       
  621.       /* link descriptor block into head of list */
  622.       temp->previous = source_descriptor_list; 
  623.       source_descriptor_list = temp; 
  624.     }
  625.     
  626.     /* re-initialise file context */
  627.     errors = 0; 
  628.     file = handle; 
  629.     linenumber = 0; 
  630.     name = s; 
  631.     warnings = 0; 
  632.     
  633.     if (echo)
  634.       text_message(TEXT_INFO, "\n"); 
  635.     
  636.     text_current = last_char = first_char = last_char - 1;  /* make new buffer region below current line */
  637.   }
  638.   
  639.   return handle; 
  640. }
  641.  
  642. int text_printf(const char * fmt, ...)
  643. {
  644.   int i; 
  645.   va_list ap;                 /* argument list walker */
  646.   
  647.   va_start(ap, fmt);          /* pass parameters to vprintf */
  648.   i = vfprintf(messages, fmt, ap);  /* remember count of characaters printed */
  649.   va_end(ap);                 /* end of var args block */
  650.   
  651.   return i;                   /* return number of characters printed */
  652. }
  653.  
  654. int text_print_as_C_string_or_char(FILE * file, char * string, int is_char_string)
  655. {
  656.   int i = 0; 
  657.   
  658.   if (is_char_string && * string == '\0') /* special case of zero length string */
  659.   {
  660.     i += 2; putc('\\', file); putc('0', file); 
  661.   }
  662.   else
  663.     while (* string != '\0')
  664.   {
  665.     if (isprint(* string)&& * string != '\"' && * string != '\'' && * string != '\\')
  666.     {
  667.       putc(* string, file); 
  668.       i++; 
  669.     }
  670.     else
  671.     {
  672.       putc('\\', file); 
  673.       i += 2; 
  674.       switch (* string)
  675.       {
  676.         case'\a': putc('a', file); break; 
  677.         case'\b': putc('b', file); break; 
  678.         case'\f': putc('f', file); break; 
  679.         case'\n': putc('n', file); break; 
  680.         case'\r': putc('r', file); break; 
  681.         case'\t': putc('t', file); break; 
  682.         case'\v': putc('v', file); break; 
  683.         case'\\': putc('\\', file); break; 
  684.         case'\'': putc('\'', file); break; 
  685.         case'\"': putc('\"', file); break; 
  686.         default: i += fprintf(file, "X%.2X", * string); break; 
  687.       }
  688.     }
  689.     string++; 
  690.   }
  691.   return i; 
  692. }
  693.  
  694. int text_print_C_char(char * string)
  695. {
  696.   return text_print_as_C_string_or_char(messages, string, 1); 
  697. }
  698.  
  699. int text_print_C_char_file(FILE * file, char * string)
  700. {
  701.   return text_print_as_C_string_or_char(file, string, 1); 
  702. }
  703.  
  704. int text_print_C_string(char * string)
  705. {
  706.   return text_print_as_C_string_or_char(messages, string, 0); 
  707. }
  708.  
  709. int text_print_C_string_file(FILE * file, char * string)
  710. {
  711.   return text_print_as_C_string_or_char(file, string, 0); 
  712. }
  713.  
  714. void text_print_statistics(void)
  715. {
  716.   long symbolcount = text_top - text_bot, 
  717.  
  718.   linecount =(text_bot - last_char)+ maxtext;
  719.  
  720.   if (text_bot == NULL)
  721.     text_message(TEXT_INFO, "Text buffer uninitialised\n"); 
  722.   else
  723.     text_message(TEXT_INFO, "Text buffer size %u bytes with %lu bytes free\n", 
  724.   maxtext, maxtext - symbolcount - linecount); 
  725. }
  726.  
  727. void text_print_time(void)
  728. {
  729.   char line[80]; 
  730.   time_t timer = time(NULL); 
  731.   
  732.   strftime(line, 80, "%b %d %Y %H:%M:%S", localtime(& timer)); 
  733.   text_printf("%s", line); 
  734. }
  735.  
  736. int text_print_total_errors(void)
  737. {
  738.   if (totalerrors != 0 || totalwarnings != 0 || echo)
  739.     text_message(TEXT_INFO, "%u error%s and %u warning%s\n", 
  740.   totalerrors, (totalerrors == 1 ? "": "s"), 
  741.   totalwarnings, (totalwarnings == 1 ? "": "s")); 
  742.   
  743.   return totalerrors != 0; 
  744. }
  745.  
  746. void text_redirect(FILE * file)
  747. {
  748.   if (messages != stdout && messages != stderr)
  749.     fclose(messages); 
  750.   
  751.   messages = file; 
  752. }
  753.  
  754. unsigned text_total_errors(void)
  755. {
  756.   return totalerrors; 
  757. }
  758.  
  759. unsigned text_total_warnings(void)
  760. {
  761.   return totalwarnings; 
  762. }
  763.  
  764. char * text_uppercase_string(char * str)
  765. {
  766.   char * ret = str; 
  767.   
  768.   while (* str != 0)
  769.   {
  770.     * str =(char)(toupper(* str)); 
  771.     str++; 
  772.   }
  773.   
  774.   return ret; 
  775. }
  776.  
  777. /* End of textio.c */
  778.