home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 355_02 / slk2.exe / SPP / SYS.C < prev    next >
C/C++ Source or Header  |  1991-06-09  |  19KB  |  940 lines

  1. /*
  2.     New Sherlock Preprocessor -- system module.
  3.  
  4.     Source:  sys.c
  5.     Started: October 7, 1985
  6.     Version:
  7.         July 20, 1988;
  8.         November 1, 1988  bug fix: extra '\r' in sysnlput().
  9.         February 10, 1989 bug fix:  kludge in sysopen().
  10.         February 16, 1989
  11.             periods removed from error messages.
  12.             t_line bug fixed in sysopen().
  13.         June 22, 1989
  14.             backslash newlines now filtered by sysnext().
  15.         August 3, 1989
  16.             signal code added to syscsts().
  17.             errors written to stderr.
  18.  
  19.  
  20.     PUBLIC DOMAIN SOFTWARE
  21.  
  22.     Sherlock, including the SPP, SDEL and SDIF programs, was placed in
  23.     the public domain on June 15, 1991, by its author,
  24.  
  25.         Edward K. Ream
  26.         166 North Prospect Ave.
  27.         Madison, WI 53705.
  28.         (608) 257-0802
  29.  
  30.     Sherlock may be used for any commercial or non-commercial purpose.
  31.  
  32.  
  33.     DISCLAIMER OF WARRANTIES
  34.  
  35.     Edward K. Ream (Ream) specifically disclaims all warranties,
  36.     expressed or implied, with respect to this computer software,
  37.     including but not limited to implied warranties of merchantability
  38.     and fitness for a particular purpose.  In no event shall Ream be
  39.     liable for any loss of profit or any commercial damage, including
  40.     but not limited to special, incidental consequential or other damages.
  41. */
  42.  
  43. #include "spp.h"
  44.  
  45. #ifdef MICRO_SOFT
  46. #include <signal.h>
  47. #include <fcntl.h>
  48. #include <sys\types.h>
  49. #include <sys\stat.h>
  50. #include <io.h>
  51. #include <time.h>
  52. #endif
  53.  
  54. #ifdef TURBOC
  55. #include <signal.h>
  56. #include <fcntl.h>
  57. #include <sys\stat.h>
  58. #include <io.h>
  59. #include <conio.h>
  60. #include <time.h>
  61. #endif
  62.  
  63. /*
  64.     Define the format of a file node.
  65.     The node contains all information pertaining to the file.
  66.     The fil_name and f_line are used to update preprocessor globals.
  67. */
  68. typedef enum { NULL_STAT,
  69.     CLOSED_STAT, INPUT_STAT, OUTPUT_STAT, LOCAL_STAT, EOF_STAT
  70. } en_stat;
  71.  
  72. static struct FN {
  73.     struct FN * f_next;    /* pointer to next node        */
  74.     char *    fil_name;    /* file name            */
  75.     int    f_line;        /* line number            */
  76.     char *    f_buffer;    /* file buffer            */
  77.     int    f_bufsize;    /* size of file buffer: bytes    */
  78.     int    f_handle;    /* handle to file.        */
  79.     char *  f_bufp;        /* pointer into file buffer    */
  80.     int    f_bufc;        /* characters in buffer        */
  81.     en_stat    f_type;        /* status of file        */
  82.     int    f_lastc;    /* restart character        */
  83. };
  84. #define FN_SIZE (sizeof(struct FN))
  85.  
  86. static struct FN * in_list   = NULL;    /* List of input nodes.        */
  87. static struct FN * outn_list = NULL;    /* List of output nodes.    */
  88.  
  89. /*
  90.     The following global variables are copies of the fields in FN.
  91.     Global variables are used as an efficiency measure.
  92. */
  93. static char *    s_ip = NULL;    /* Copy of f_bufp for input file.    */
  94. static int    s_ic = 0;        /* Copy of f_bufc for input file.    */
  95. static char *    s_op = NULL;    /* Copy of f_bufp for output file.    */
  96. static int    s_oc = 0;    /* Copy of f_bufc for output file.    */
  97.  
  98. #define MAX_INLEVEL 20    /* Max depth of nested #includes.    */
  99. #define IBUF_SIZE 2048    /* Size of each input buffer.        */
  100. #define OBUF_SIZE 2048    /* Size of the output buffer.        */
  101.  
  102. /*
  103.     Define "pushed back" characters used by sysn1().
  104. */
  105. static int    push_back = -1;        /* Used by macro logic.        */
  106. static int    file_put_back = -1;    /* Used by non-macro logic.    */
  107.  
  108. /*
  109.     Define variables used by time and date routines.
  110. */
  111. static    long ltime;
  112. static    struct tm *newtime;
  113. static char time_buf [30];
  114. static char date_buf [30];
  115.  
  116. /*
  117.     Declare static functions in this module.
  118. */
  119. static void    raw_close    (int);
  120. static int    raw_creat    (char *);
  121. static int    raw_open    (char *);
  122. static int    raw_read    (int, char *, int);
  123. static int    raw_write    (int, char *, int);
  124. static int    syscstat    (void);
  125. static void    sysn1        (void);
  126. static struct FN *sys_new    (int);
  127. static void    sys_release    (void);
  128.  
  129. /*
  130.     Close a file opened with raw_open() or raw_creat().
  131. */
  132. static void
  133. raw_close(handle)
  134. int handle;
  135. {
  136.     TRACEP("raw_close", printf("(handle: %d)\n", handle));
  137.  
  138.     close (handle);
  139. }
  140.  
  141. /*
  142.     Open the file for writing only.
  143.     Return a handle (int) or ERROR.
  144. */
  145. static int
  146. raw_creat(name)
  147. char *name;
  148. {
  149.  
  150.     TRACEP("raw_creat", printf("(%s)\n", name));
  151.  
  152.     chmod(name, S_IREAD | S_IWRITE);
  153.     return creat(name, S_IREAD | S_IWRITE);
  154. }
  155.  
  156. /*
  157.     Open the file for reading only.
  158.     Return a handle (int) or ERROR.
  159. */
  160. static int
  161. raw_open(name)
  162. char *name;
  163. {
  164.     TRACEP("raw_open", printf("(%s)\n", name));
  165.  
  166.     return open(name, O_RDONLY | O_BINARY);
  167. }
  168.  
  169. /*
  170.     Read n bytes from the file described by handle into buffer[].
  171.     Return the number of bytes read.
  172. */
  173. static int
  174. raw_read(handle, buffer, n)
  175. int handle;
  176. char *buffer;
  177. int n;
  178. {
  179.     int result;
  180.  
  181.     TRACEP("raw_read", printf("(handle: %d, buffer: %lx, n: %d)\n",
  182.         handle, buffer, n));
  183.         
  184.     result = read (handle, buffer, n);
  185.  
  186.     TRACEP("raw_read", printf("returns %d\n", result));
  187.     return result;
  188. }
  189.  
  190. /*
  191.     Write n bytes from buffer[] to the file described by handle.
  192.     Return the number of bytes written.
  193. */
  194. static int
  195. raw_write(handle, buffer, n)
  196. int handle;
  197. char *buffer;
  198. int n;
  199. {
  200.     TRACEP("raw_write", printf("(handle: %dx, buffer: %lx, n: %d)\n",
  201.         handle, buffer, n));
  202.         
  203.     return write (handle, buffer, n);    
  204. }
  205.  
  206. /*
  207.     Close all files and exit.
  208.  
  209.     Do not call fatal() or sysabort() from inside
  210.     sysiclose(), sysoclose(), or sys_release().
  211. */
  212. void
  213. sysabort()
  214. {
  215.     /* Close the output file. */
  216.     if (outn_list != NULL) {
  217.         sysoclose();
  218.     }
  219.  
  220.     /* Close all input files. */
  221.     while(in_list != NULL) {
  222.         sysiclose();
  223.     }
  224.     sysend();
  225.     exit(BAD_EXIT);
  226. }        
  227.  
  228. /*
  229.     Put a non-newline to the output file.
  230.     This is the second most called routine after sysnext().
  231. */
  232. void
  233. syscput(c)
  234. char c;
  235. {
  236.     struct FN * fnp;
  237.  
  238.     *s_op++ = c;
  239.     s_oc++;
  240.     if (s_oc == OBUF_SIZE) {
  241.  
  242.         fnp = outn_list;
  243.         if (raw_write(fnp->f_handle, fnp->f_buffer, OBUF_SIZE) != 
  244.             OBUF_SIZE) {
  245.             error("Disk full");
  246.             return;
  247.         }
  248.         
  249.         s_oc = 0;
  250.         s_op = fnp -> f_buffer;
  251.     }
  252. }
  253.  
  254. /*
  255.     Open a file for output.  Only one such file may be open at a time.
  256.  
  257.     Return TRUE if all went well.
  258. */
  259. bool
  260. syscreat(name)
  261. register char * name;
  262. {
  263.     register struct FN * fnp;
  264.     int handle;
  265.  
  266.     TRACEP("syscreat", printf("(%s)\n", name));
  267.  
  268.     fnp = outn_list;
  269.     if (fnp == NULL) {
  270.         /* Allocate node for output file. */
  271.         fnp = outn_list  = sys_new(OBUF_SIZE);
  272.     }
  273.     else if (fnp -> f_type != CLOSED_STAT) {
  274.         syserr("syscreat: Can't happen");
  275.     }
  276.     
  277.     /* Actually open the file. */
  278.     handle = raw_creat(name);
  279.     if (handle == ERROR) {
  280.         return FALSE;
  281.     }
  282.     fnp -> f_handle = handle;
  283.     fnp -> f_type = OUTPUT_STAT;
  284.  
  285.     /* The output buffer is empty. */
  286.     s_oc = 0;
  287.     s_op = fnp -> f_buffer;
  288.  
  289.     return TRUE;
  290. }
  291.  
  292. /*
  293.     Return 0 if no character is ready from the keyboard.
  294.     Otherwise, return the character itself.
  295. */
  296. static int sys_inited = 0;
  297.  
  298. /*
  299.     Interrupt handler for control-c.
  300. */
  301. static void
  302. ctrl_handler(int sig)
  303. {
  304.     fprintf(stderr, "\nSPP terminated by operator\n");
  305.     exit(0);
  306. }
  307.  
  308. void
  309. syscsts()
  310. {
  311.     if (sys_inited == 0) {
  312.         sys_inited = 1;
  313.         if (signal(SIGINT, ctrl_handler) == SIG_ERR) {
  314.             fprintf(stderr, "\nsignal failed.\n");
  315.             exit(-1);
  316.             sysabort();
  317.         }
  318.     }
  319. }
  320.  
  321. #if 0 /* Machine independent version.  Old code. */
  322. static int
  323. syscstat()
  324. {
  325.     if (kbhit()) {
  326.         return fgetchar() & 0x7f;
  327.     }
  328.     else {
  329.         return 0;
  330.     }
  331. }
  332.  
  333. /*
  334.     Get console status and pause if the user has hit control S.
  335.     Abort if user has hit control C.
  336. */
  337.  
  338. #define CONTROL_C 3
  339.  
  340. void
  341. syscsts()
  342. {
  343.     int c;
  344.  
  345.     c = syscstat();
  346.     if (c == CONTROL_C) {
  347.         fprintf(stderr, "\nSPP terminated by operator\n");
  348.         sysabort();
  349.     }
  350. }
  351. #endif /* old code */
  352.  
  353. /*
  354.     Return the print string of the current date.
  355. */
  356. char *
  357. sysdate()
  358. {
  359.     char *p;
  360.  
  361.     time(<ime);    
  362.     newtime = localtime(<ime);
  363.     p = asctime(newtime);
  364.     if (strlen(p) != 25) {
  365.         return "";
  366.     }
  367.     else {
  368.         strcpy(date_buf, "\"");
  369.         p[11] = '\0';
  370.         strcat(date_buf, p);
  371.         p[24] = '\0';
  372.         strcat(date_buf, p + 20);
  373.         strcat(date_buf, "\"");
  374.  
  375.         TRACEPN("sysdate", printf("returns: %s\n", date_buf));
  376.         return date_buf;
  377.     }
  378. }
  379.  
  380. /*
  381.     Shut down the system.  This routine is called just before doing an
  382.     exit().  Do any last minute things here.
  383. */
  384. void
  385. sysend()
  386. {
  387.     TICK("sysend");
  388. }
  389.  
  390. /*
  391.     Push c back into the file input stream, not the macro input stream.
  392. */
  393. void
  394. sys_fpb(c)
  395. int c;
  396. {
  397.     file_put_back = c;
  398. }
  399.  
  400. /*
  401.     Close the current input file and pop back one level.
  402. */
  403. void
  404. sysiclose()
  405. {
  406.     register struct FN * fnp;
  407.  
  408.     TICK("sysiclose");
  409.  
  410. #ifdef DEBUG
  411.     if (t_inlevel < 0) {
  412.         syserr("sysiclose: Bad t_inlevel");
  413.     }
  414. #endif
  415.  
  416.     /* Close the current input file. */
  417.     fnp = in_list;    
  418.     raw_close(fnp -> f_handle);
  419.  
  420.     /* Release the current node. */
  421.     sys_release();
  422.     if (t_inlevel > 0 && in_list == NULL) {
  423.         syserr("sysiclose: NULL in_list");
  424.     }
  425.  
  426.     /* Pop back one level. */
  427.     if (in_list != NULL) {
  428.         if (t_inlevel < 0) {
  429.             syserr("sysiclose: Unexpected in_list");
  430.         }
  431.         fnp = in_list;
  432.         s_ic = fnp -> f_bufc;
  433.         s_ip = fnp -> f_bufp;
  434.         t_file = fnp -> fil_name;
  435.         t_line = fnp -> f_line;
  436.     }
  437.     t_inlevel--;
  438.  
  439.     if (t_inlevel == -1) {
  440.         ch = END_FILE;
  441.         TRACEPN("sysiclose", printf("restarts. ch=END_FILE\n"));
  442.     }
  443.     else {
  444.         ch = (unsigned char) fnp -> f_lastc;
  445.         TRACEPN("sysiclose", printf("retarts. ch=lastc: %s\n",
  446.             pr_ch(ch)));
  447.     }
  448.         
  449.     TRACEP("sysiclose", printf("exit: t_inlevel=%d\n", t_inlevel));
  450. }
  451.  
  452. void
  453. syshdump(void)
  454. {
  455.     int i;
  456.     int c;
  457.  
  458.     /* Output to file. */
  459.     TRACEPN("file_dump",
  460.         syssput("\nDump of hold buffer<");
  461.         for (i = 0; i < hold_count; i++) {
  462.             c = hold_buf[i];
  463.             syssput(pr_ch(c));
  464.         }
  465.         syscput('>');
  466.         sysnlput());
  467.  
  468.     printf("\n\nDump of hold buffer:  hold_count = %d\n<", hold_count);
  469.     for (i = 0; i < hold_count; i++) {
  470.         c = hold_buf[i];
  471.         printf("%s", pr_ch(c));
  472.     }
  473.     printf(">\n\n");
  474. }
  475.  
  476. /* Output ALL held output. */
  477. void
  478. syshflush(void)
  479. {
  480.     int i;
  481.  
  482.     TRACEP("syshflush", syshdump());
  483.  
  484.     for (i = 0; hold_count > 0; i++, hold_count--) {
  485.         syscput(hold_buf [i]);
  486.     }
  487. }
  488.  
  489. /* Discard all held output. */
  490. void
  491. syshkill(void)
  492. {
  493.     TRACEP("syshkill", syshdump());
  494.  
  495.     hold_count = 0;
  496. }
  497.  
  498. /*
  499.     Delete all leading newlines and white space from the hold buffer,
  500.     but leave the last leading white space.
  501.  
  502.     Return a pointer at the first non-blank character or NULL.
  503. */
  504. char *
  505. syshnldel(void)
  506. {
  507.     int head, tail, end, i;
  508.     char c;
  509.  
  510.     TICK("syshnldel");
  511.  
  512.     end = hold_count;
  513.     hold_count = 0;
  514.     head = tail = 0;
  515.  
  516.     /* Scan through the buffer, deleting leading white space. */
  517.     while (head < end) {
  518.         c = hold_buf[head++];
  519.         if (c == '\n') {
  520.             /* Delete any tentatively held white space. */
  521.             tail = 0;
  522.             hold_count = 0;
  523.         }
  524.         else if (c == ' ' || c == '\t') {
  525.             /* Tentatively hold the white space. */
  526.             hold_buf[tail++] = c;
  527.             hold_count++;
  528.         }
  529.         else {
  530.             hold_buf[tail++] = c;
  531.             hold_count++;
  532.             break;
  533.         }
  534.     }
  535.  
  536.     /* Copy the remainder of the hold buffer. */
  537.     while (head < end) {
  538.         hold_buf[tail++] = hold_buf[head++];
  539.         hold_count++;
  540.     }
  541.     hold_buf[tail] = '\0';
  542.  
  543.     TRACEPN("syshnldel", syshdump());
  544.  
  545.     /* Return the first non-white space character of the buffer. */
  546.     for (i = 0; i < hold_count; i++) {
  547.         c = hold_buf[i];
  548.         if (c != ' ' && c != '\t' && c != '\n') {
  549.             return &hold_buf[i];
  550.         }
  551.     }
  552.  
  553.     return NULL;
  554. }
  555.  
  556. /*
  557.     Allocate space for a new file node and file buffer.
  558.     Fill in all other fields to neutral values.
  559. */
  560. static struct FN *
  561. sys_new(buf_size)
  562. int buf_size;
  563. {
  564.     register struct FN * fnp;
  565.  
  566.     TRACEP("sys_new", printf("(buf_size: %d)\n", buf_size));
  567.  
  568.     fnp         = (struct FN *) m_alloc(sizeof(struct FN));
  569.     fnp -> f_buffer     = (char *)      m_alloc(buf_size);
  570.     fnp -> f_bufsize = buf_size;
  571.  
  572.     fnp -> f_next    = NULL;
  573.     fnp -> fil_name    = NULL;
  574.     fnp -> f_bufp    = fnp -> f_buffer;
  575.     fnp -> f_bufc    = 0;
  576.     fnp -> f_type    = CLOSED_STAT;
  577.  
  578.     TRACEPN("sys_new", printf("returns %lx\n", fnp));
  579.     return fnp;
  580. }
  581.  
  582. /*
  583.     Set the global variable ch to the next character from the input stream.
  584.     This is the most often called routine in the whole program.
  585.  
  586.     This routine eliminates all  '\r' and backslash-newlines.
  587.     Thus, '\r' and backslash-newline may appear ANYWHERE in the program.
  588. */
  589. void
  590. sysnext()
  591. {
  592.     for(;;) {
  593.         /* sysn1() filters out all '\r' characters. */
  594.         sysn1();
  595.  
  596.         /* Intercept backslash newline combinations here. */
  597.         if (ch == '\\') {
  598.             sysn1();
  599.             if (ch == '\n') {
  600.                 t_line++;
  601.                 continue;
  602.             }
  603.             else {
  604.                 push_back = ch;
  605.                 ch = '\\';
  606.                 return;
  607.             }
  608.         }
  609.         else {
  610.             return;
  611.         }
  612.     }
  613. }
  614.  
  615. void
  616. sysn1(void)
  617. {
  618.     register int n;
  619.     register struct FN * fnp;
  620.  
  621.     /* Priority 1: A pushed back character. */
  622.     if (push_back != -1) {
  623.         ch = push_back;
  624.         TRACEP("sysnext", printf("pushback: %s\n", pr_ch(ch)));
  625.         push_back = -1;
  626.         goto done;
  627.     }
  628.  
  629.     if (!m_flag && !hold_flag && ch_hold) {
  630.         TRACEPN("sysnext", printf("immediate out %s\n", pr_ch(ch)));
  631.         syscput(ch);
  632.     }
  633.     else if (!m_flag && ch_hold) {
  634.         if (hold_count >= MAX_HOLD) {
  635.             warning("Hold buffer overflow");
  636.             syshflush();
  637.         }
  638.         hold_buf [hold_count++] = ch;
  639.         TRACEPN("sysnext", printf("buffers %s\n", pr_ch(ch)));
  640.     }
  641.  
  642. loop:
  643.  
  644.     /* Priority 2:  The rescan buffer. */
  645.     if (m_flag) {
  646.         ch = *p_rescan++;
  647.         if (ch) {
  648.             TRACEP("sysnext", printf("rescan ch: %s\n",
  649.                 pr_ch(ch)));
  650.             goto done;
  651.         }
  652.         m_flag = FALSE;
  653.  
  654.         /*
  655.             Priority 2A:  The trailing character.
  656.             This is NOT part of the rescan buffer.
  657.         */
  658.         if (file_put_back != -1) {
  659.             ch = file_put_back;
  660.             TRACEP("sysnext", printf("fpb ch: %s\n",
  661.                 pr_ch(ch)));
  662.             file_put_back = -1;
  663.             goto done;
  664.         }
  665.         /* FALL THROUGH. */
  666.     }
  667.  
  668.     /* Priority 3:  The current file. */
  669.     if (s_ic > 0) {
  670.         s_ic--;
  671.         ch = *s_ip++;
  672.         TRACEP("sysnext_v", printf("ch: %s\n", pr_ch(ch)));
  673.         goto done;
  674.     }
  675.  
  676.     /* A file or local buffer is empty. */
  677.     fnp = in_list;
  678.     switch(fnp -> f_type) {
  679.  
  680.     case EOF_STAT:
  681.         /* Continue returning END_FILE until the file is closed. */
  682.         ch = END_FILE;
  683.         return;
  684.  
  685.     case INPUT_STAT:
  686.         n = raw_read(fnp->f_handle, fnp->f_buffer, IBUF_SIZE);
  687.         if (n > 0) {
  688.             s_ic  = n;
  689.             s_ip = fnp -> f_buffer;
  690.             s_ic--;
  691.             ch = *s_ip++;
  692.             goto done;
  693.         }
  694.         else {
  695.             fnp -> f_type = EOF_STAT;
  696.             ch = END_FILE;
  697.             return;
  698.         }
  699.  
  700.     default:
  701.         TRACEP("sysn1",
  702.             printf("in_list = %lx, in_list -> f_type = %d\n",
  703.                 in_list, in_list -> f_type));
  704.  
  705.         syserr("sysn1: Bad f_type field");
  706.     }
  707.  
  708. done:
  709.     /* Never pass '\r' to the rest of the program. */
  710.     if (ch == '\r') {
  711.         goto loop;
  712.     }
  713.  
  714.     /*
  715.         Only buffer non-macro characters in the current file.
  716.         Do not buffer any character if hold_flag is TRUE.
  717.     */
  718.     ch_hold = !m_flag && t_inlevel == 0;
  719.  
  720.     TRACEP("sysnext", printf("returns %s\n", pr_ch(ch)));
  721. }
  722.  
  723. /*
  724.     Put a newline to the output file.
  725. */
  726. void
  727. sysnlput()
  728. {
  729.     TICK("sysnlput");
  730.  
  731.     /* November 1, 1988: We are in text mode, so this is not needed.
  732.     syscput('\r');
  733.     ----- */
  734.  
  735.     syscput('\n');
  736.  
  737.     /* Give user a chance to stop. */
  738.     syscsts();
  739. }
  740.  
  741. /*
  742.     Close the output file(s).
  743. */
  744. void
  745. sysoclose()
  746. {
  747.     register struct FN * fnp;
  748.  
  749.     TICK("sysoclose");
  750.  
  751.     fnp = outn_list;
  752.     if (fnp != NULL && fnp -> f_type != CLOSED_STAT) {
  753.         syscput(END_FILE);
  754.         raw_write(fnp -> f_handle, fnp -> f_buffer, s_oc);
  755.         raw_close(fnp -> f_handle);
  756.         fnp -> f_type = CLOSED_STAT;
  757.     }
  758. }
  759.  
  760. /*
  761.     Open a file for input.
  762.     Return TRUE if all went well.
  763. */
  764. bool
  765. sysopen(name)
  766. char *name;
  767. {
  768.     struct FN * fnp;
  769.     int handle;
  770.  
  771.     TRACEP("sysopen", printf("(%s); old t_inlevel = %d\n",
  772.         name, t_inlevel));
  773.  
  774.     if (m_flag == TRUE) {
  775.         m_flag = FALSE;
  776.         error("Macro expansion truncated following #include");
  777.     }
  778.  
  779.     /* Save information about the current level on the stack. */
  780.     if (t_inlevel != -1) {
  781.         if (in_list == NULL) {
  782.             syserr("sysopen: Can't happen");
  783.         }
  784.         fnp = in_list;
  785.         fnp -> f_line = t_line;
  786.         fnp -> f_bufc = s_ic;
  787.         fnp -> f_bufp = s_ip;
  788.         fnp -> f_lastc = (int) ch;
  789.     }
  790.  
  791.     /* Enter a new level. */
  792.     if (t_inlevel >= MAX_INLEVEL) {
  793.         fatal("include files nested too deeply");
  794.     }
  795.     else {
  796.         t_inlevel++;
  797.     }
  798.  
  799.     /* Create a new file node and link to the front of the list. */
  800.  
  801.     TRACEP("sysopen", printf("set up new file: call sys_new\n"));
  802.  
  803.     fnp          = sys_new(IBUF_SIZE);
  804.     fnp -> f_next = in_list;
  805.     fnp -> f_type = INPUT_STAT;
  806.     in_list       = fnp;
  807.  
  808.     /* Actually open the file. */
  809.  
  810.     handle = raw_open(name);
  811.     if (handle == ERROR) {
  812.  
  813.         /* Deallocate the node. */
  814.         TRACEP("sysopen", printf("file open %s fails\n", name));
  815.         sys_release();
  816.         t_inlevel--;
  817.         return FALSE;
  818.     }
  819.     else {
  820.         fnp -> f_handle = handle;
  821.     }
  822.  
  823.     /* Set the global variables. */
  824.     t_file = str_alloc(name);
  825.     fnp -> fil_name = t_file;
  826.  
  827.     /* Put the first character of the file into ch. */
  828.  
  829.     /* 2/10/88
  830.         Force a '\n' as the first character and decrease
  831.         the line number by one to compensate.
  832.  
  833.         This little kludge will make preprocessor directives
  834.         legal in the first line of any file.
  835.  
  836.         Since sysnext() is not called to get this character,
  837.         it won't be buffered or output.
  838.     */
  839.     s_ic = 0;
  840.     s_ip = fnp -> f_buffer;
  841.  
  842.     /* 2/16/89: Since we add a newline, start t_line at 0. */
  843.     ch = '\n';
  844.     t_line = 0;
  845.  
  846.     /* -----
  847.     sysnext();
  848.     ----- */
  849.  
  850.     TRACEP("sysopen", printf("exit: t_inlevel=%d, in_list=%lx\n",
  851.                         t_inlevel, (long)in_list));
  852.     return TRUE;
  853. }
  854.  
  855. /*
  856.     Push back c so that sysnext() will return it next.
  857. */
  858. void
  859. syspushback(c)
  860. int c;
  861. {
  862.     push_back = c;
  863. }
  864.  
  865. /*
  866.     Remove one node from the input list.
  867. */
  868. static void
  869. sys_release()
  870. {
  871.     register struct FN * fnp;
  872.  
  873.     TICK("sys_release");
  874.  
  875. #ifdef DEBUG
  876.     if (in_list == NULL) {
  877.         syserr("sys_release: Can't happen");
  878.     }
  879. #endif
  880.  
  881.     /* Remove the node from the input list. */
  882.     fnp     = in_list;
  883.     in_list = fnp -> f_next;
  884.     fnp -> f_type = CLOSED_STAT;
  885.  
  886.     /* Deallocate the node. */
  887.     if (fnp -> f_buffer != NULL) {
  888.         m_free(fnp -> f_buffer);
  889.     }
  890.     m_free(fnp);
  891.  
  892.     /* Reset the global variables from the next node. */
  893.     if (in_list == NULL) {
  894.         s_ic = 0;
  895.     }
  896.     else {
  897.         fnp = in_list;
  898.         s_ic = fnp -> f_bufc;
  899.         s_ip = fnp -> f_bufp;
  900.     }
  901. }
  902.  
  903. /*
  904.     Put one string to the output file.
  905. */
  906. void
  907. syssput(s)
  908. register char * s;
  909. {
  910.     TRACEP("syssput", printf("(%s)\n", s));
  911.  
  912.     while (*s) {
  913.         syscput(*s++);
  914.     }
  915. }
  916.  
  917. /*
  918.     Return a print string of the current time.
  919. */
  920. char *
  921. systime()
  922. {
  923.     char *p;
  924.  
  925.     time(<ime);    
  926.     newtime = localtime(<ime);
  927.     p = asctime(newtime);
  928.     if (strlen(p) != 25) {
  929.         return "\"\"";
  930.     }
  931.     else {
  932.         strcpy(time_buf, "\"");
  933.         *(p+19) = '\0';
  934.         strcat(time_buf, p+11);
  935.         strcat(time_buf, "\"");
  936.         TRACEPN("systime", printf("returns: %s\n", time_buf));
  937.         return time_buf;
  938.     }
  939. }
  940.