home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume5 / smallc / part2 / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.0 KB  |  173 lines

  1. /*    File io.c: 2.1 (83/03/20,16:02:07) */
  2. /*% cc -O -c %
  3.  *
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "defs.h"
  8. #include "data.h"
  9.  
  10. /*
  11.  *    open input file
  12.  */
  13. openin (p) char *p;
  14. {
  15.     strcpy(fname, p);
  16.     fixname (fname);
  17.     if (!checkname (fname))
  18.         return (NO);
  19.     if ((input = fopen (fname, "r")) == NULL) {
  20.         pl ("Open failure\n");
  21.         return (NO);
  22.     }
  23.     kill ();
  24.     return (YES);
  25. }
  26.  
  27. /*
  28.  *    open output file
  29.  */
  30. openout ()
  31. {
  32.     outfname (fname);
  33.     if ((output = fopen (fname, "w")) == NULL) {
  34.         pl ("Open failure");
  35.         return (NO);
  36.     }
  37.     kill ();
  38.     return (YES);
  39. }
  40.  
  41. /*
  42.  *    change input filename to output filename
  43.  */
  44. outfname (s)
  45. char    *s;
  46. {
  47.     while (*s)
  48.         s++;
  49.     *--s = 's';
  50. }
  51.  
  52. /*
  53.  *    remove NL from filenames
  54.  *
  55.  */
  56. fixname (s)
  57. char    *s;
  58. {
  59.     while (*s && *s++ != EOL);
  60.     if (!*s) return;
  61.     *(--s) = 0;
  62. }
  63.  
  64. /*
  65.  *    check that filename is "*.c"
  66.  */
  67. checkname (s)
  68. char    *s;
  69. {
  70.     while (*s)
  71.         s++;
  72.     if (*--s != 'c')
  73.         return (NO);
  74.     if (*--s != '.')
  75.         return (NO);
  76.     return (YES);
  77. }
  78.  
  79. kill ()
  80. {
  81.     lptr = 0;
  82.     line[lptr] = 0;
  83. }
  84.  
  85. inline ()
  86. {
  87.     int    k;
  88.     FILE    *unit;
  89.  
  90.     FOREVER {
  91.         if (feof (input))
  92.             return;
  93.         if ((unit = input2) == NULL)
  94.             unit = input;
  95.         kill ();
  96.         while ((k = fgetc (unit)) != EOF) {
  97.             if ((k == EOL) | (lptr >= LINEMAX))
  98.                 break;
  99.             line[lptr++] = k;
  100.         }
  101.         line[lptr] = 0;
  102.         if (k <= 0)
  103.             if (input2 != NULL) {
  104.                 input2 = inclstk[--inclsp];
  105.                 fclose (unit);
  106.             }
  107.         if (lptr) {
  108.             if ((ctext) & (cmode)) {
  109.                 comment ();
  110.                 outstr (line);
  111.                 nl ();
  112.             }
  113.             lptr = 0;
  114.             return;
  115.         }
  116.     }
  117. }
  118.  
  119. inbyte ()
  120. {
  121.     while (ch () == 0) {
  122.         if (feof (input))
  123.             return (0);
  124.         preprocess ();
  125.     }
  126.     return (gch ());
  127. }
  128.  
  129. inchar ()
  130. {
  131.     if (ch () == 0)
  132.         inline ();
  133.     if (feof (input))
  134.         return (0);
  135.     return (gch ());
  136. }
  137.  
  138. gch ()
  139. {
  140.     if (ch () == 0)
  141.         return (0);
  142.     else
  143.         return (line[lptr++] & 127);
  144. }
  145.  
  146. nch ()
  147. {
  148.     if (ch () == 0)
  149.         return (0);
  150.     else
  151.         return (line[lptr + 1] & 127);
  152. }
  153.  
  154. ch ()
  155. {
  156.     return (line[lptr] & 127);
  157. }
  158.  
  159. /*
  160.  *    print a carriage return and a string only to console
  161.  *
  162.  */
  163. pl (str)
  164. char    *str;
  165. {
  166.     int    k;
  167.  
  168.     k = 0;
  169.     putchar (EOL);
  170.     while (str[k])
  171.         putchar (str[k++]);
  172. }
  173.