home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / nuweb087.zip / input.c < prev    next >
C/C++ Source or Header  |  1996-02-28  |  4KB  |  109 lines

  1. #include "global.h"
  2. static FILE *source_file;  /* the current input file */
  3. static int source_peek;
  4. static int double_at;
  5. static int include_depth;
  6. struct {
  7.   FILE *file;
  8.   char *name;
  9.   int line;
  10. } stack[10];
  11. int source_get()
  12. {
  13.   int c = source_peek;
  14.   switch (c) {
  15.     case EOF:  {
  16.                  fclose(source_file);
  17.                  if (include_depth) {
  18.                    include_depth--;
  19.                    source_file = stack[include_depth].file;
  20.                    source_line = stack[include_depth].line;
  21.                    source_name = stack[include_depth].name;
  22.                    source_peek = getc(source_file);
  23.                    c = source_get();
  24.                  }
  25.                }
  26.                return c;
  27.     case '@':  {
  28.                  c = getc(source_file);
  29.                  if (double_at) {
  30.                    source_peek = c;
  31.                    double_at = FALSE;
  32.                    c = '@';
  33.                  }
  34.                  else
  35.                    switch (c) {
  36.                      case 'i': {
  37.                                  char name[100];
  38.                                  if (include_depth >= 10) {
  39.                                    fprintf(stderr, "%s: include nesting too deep (%s, %d)\n",
  40.                                            command_name, source_name, source_line);
  41.                                    exit(-1);
  42.                                  }
  43.                                  {
  44.                                      char *p = name;
  45.                                      do 
  46.                                        c = getc(source_file);
  47.                                      while (c == ' ' || c == '\t');
  48.                                      while (isgraph(c)) {
  49.                                        *p++ = c;
  50.                                        c = getc(source_file);
  51.                                      }
  52.                                      *p = '\0';
  53.                                      if (c != '\n') {
  54.                                        fprintf(stderr, "%s: unexpected characters after file name (%s, %d)\n",
  55.                                                command_name, source_name, source_line);
  56.                                        exit(-1);
  57.                                      }
  58.                                  }
  59.                                  stack[include_depth].name = source_name;
  60.                                  stack[include_depth].file = source_file;
  61.                                  stack[include_depth].line = source_line + 1;
  62.                                  include_depth++;
  63.                                  source_line = 1;
  64.                                  source_name = save_string(name);
  65.                                  source_file = fopen(source_name, "r");
  66.                                  if (!source_file) {
  67.                                    fprintf(stderr, "%s: can't open include file %s\n",
  68.                                     command_name, source_name);
  69.                                    exit(-1);
  70.                                  }
  71.                                  source_peek = getc(source_file);
  72.                                  c = source_get();
  73.                                }
  74.                                break;
  75.                      case 'f': case 'm': case 'u':
  76.                      case 'd': case 'o': case 'D': case 'O':
  77.                      case '{': case '}': case '<': case '>': case '|':
  78.                                source_peek = c;
  79.                                c = '@';
  80.                                break;
  81.                      case '@': source_peek = c;
  82.                                double_at = TRUE;
  83.                                break;
  84.                      default:  fprintf(stderr, "%s: bad @ sequence (%s, line %d)\n",
  85.                                        command_name, source_name, source_line);
  86.                                exit(-1);
  87.                    }
  88.                }
  89.                return c;
  90.     case '\n': source_line++;
  91.     default:   source_peek = getc(source_file);
  92.                return c;
  93.   }
  94. }
  95. void source_open(name)
  96.      char *name;
  97. {
  98.   source_file = fopen(name, "r");
  99.   if (!source_file) {
  100.     fprintf(stderr, "%s: couldn't open %s\n", command_name, name);
  101.     exit(-1);
  102.   }
  103.   source_name = name;
  104.   source_line = 1;
  105.   source_peek = getc(source_file);
  106.   double_at = FALSE;
  107.   include_depth = 0;
  108. }
  109.