home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / utils / texi2ps / fileio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-08  |  4.1 KB  |  184 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. /* texi2ps -- convert texinfo format files into Postscript files.
  3.  
  4.    Copyright (C) 1995 DJ Delorie (dj@delorie.com)
  5.  
  6.    texi2ps is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY.  No author or distributor accepts
  8.    responsibility to anyone for the consequences of using it or for
  9.    whether it serves any particular purpose or works at all, unless he
  10.    says so in writing.  Refer to the GNU General Public License
  11.    for full details.
  12.  
  13.    Everyone is granted permission to copy, modify and redistribute
  14.    texi2ps, but only under the conditions described in the GNU
  15.    General Public License.   A copy of this license is supposed to
  16.    have been given to you along with texi2ps so you can know your
  17.    rights and responsibilities.  It should be in a file named COPYING.
  18.    Among other things, the copyright notice and this notice must be
  19.    preserved on all copies.  */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "fileio.h"
  25. #include "screenio.h"
  26.  
  27. typedef struct IncludeItem {
  28.   char *path;
  29.   struct IncludeItem *next;
  30. } IncludeItem;
  31.  
  32. static IncludeItem dot_path =
  33. { 0, 0 };
  34.  
  35. IncludeItem *include_path=&dot_path, *last_include_path=&dot_path;
  36.  
  37. typedef struct FileStack {
  38.   FILE *file;
  39.   char *name;
  40.   int line_no;
  41.   struct FileStack *prev;
  42. } FileStack;
  43.  
  44. static FileStack *file_stack = 0;
  45.  
  46. typedef struct FileQueue {
  47.   char *string;
  48.   char *ptr;
  49.   struct FileQueue *next;
  50. } FileQueue;
  51.  
  52. static FileQueue *file_queue = 0;
  53.  
  54. static char unget_buffer[100];
  55. static int unget_ptr = 0;
  56.  
  57. int fileio_get(void)
  58. {
  59.   if (unget_ptr)
  60.     {
  61.       int rv = unget_buffer[--unget_ptr];
  62.       if (rv == '\n')
  63.         file_stack->line_no++;
  64.       return rv;
  65.     }
  66.  
  67.   if (file_queue)
  68.   {
  69.     while (file_queue && *(file_queue->ptr) == 0)
  70.     {
  71.       FileQueue *fq = file_queue;
  72.       file_queue = file_queue->next;
  73.       free(fq->string);
  74.       free(fq);
  75.     }
  76.     if (file_queue)
  77.     {
  78.       int rv = *(file_queue->ptr)++;
  79.       if (rv == '\n')
  80.         file_stack->line_no++;
  81.       return rv;
  82.     }
  83.   }
  84.  
  85.   while (file_stack)
  86.   {
  87.     int rv = fgetc(file_stack->file);
  88.  
  89.     if (rv == '\n')
  90.       file_stack->line_no++;
  91.  
  92.     if (rv == EOF && file_stack)
  93.     {
  94.       FileStack *tmp = file_stack;
  95.       fclose(file_stack->file);
  96.       screenio_print("Done:    %s", file_stack->name);
  97.       free(file_stack->name);
  98.       file_stack = file_stack->prev;
  99.       free(tmp);
  100.     }
  101.     else
  102.       return rv;
  103.   }
  104.   return EOF;
  105. }
  106.  
  107. void fileio_unget(int c)
  108. {
  109.   unget_buffer[unget_ptr++] = c;
  110.   if (c == '\n')
  111.     file_stack->line_no--;
  112. }
  113.  
  114. void fileio_include(char *n)
  115. {
  116.   FILE *f;
  117.   FileStack *fs;
  118.   IncludeItem *ip;
  119.   char tmp[1000];
  120.   char *found;
  121.  
  122.   for (ip=include_path; ip; ip=ip->next)
  123.   {
  124.     if (ip->path)
  125.     {
  126.       sprintf(tmp, "%s/%s", ip->path, n);
  127.       found = tmp;
  128.       f = fopen(tmp, "r");
  129.     }
  130.     else
  131.     {
  132.       f = fopen(n, "r");
  133.       found = n;
  134.     }
  135.     if (f)
  136.       break;
  137.   }
  138.   if (f == 0)
  139.   {
  140.     screenio_enabled++;
  141.     if (file_stack)
  142.       screenio_print("Error: unable to include file %s from %s", n, fileio_where());
  143.     else
  144.       screenio_print("Error: unable to open file %s", n);
  145.     perror("The error was");
  146.     screenio_enabled--;
  147.     return;
  148.   }
  149.   screenio_print("Loading: %s", found);
  150.  
  151.   fs = (FileStack *)malloc(sizeof(FileStack));
  152.   fs->name = strdup(found);
  153.   fs->file = f;
  154.   fs->line_no = 1;
  155.   fs->prev = file_stack;
  156.   file_stack = fs;
  157. }
  158.  
  159. char *fileio_where(void)
  160. {
  161.   static char buf[1000];
  162.   sprintf(buf, "file %s, line %d", file_stack->name, file_stack->line_no);
  163.   return buf;
  164. }
  165.  
  166. void fileio_queue(char *s)
  167. {
  168.   FileQueue *fq = (FileQueue *)malloc(sizeof(FileQueue));
  169.   fq->string = strdup(s);
  170.   fq->ptr = fq->string;
  171.   fq->next = file_queue;
  172.   file_queue = fq;
  173. }
  174.  
  175. void fileio_add_path(char *p)
  176. {
  177.   IncludeItem *ip = (IncludeItem *)malloc(sizeof(IncludeItem));
  178.   ip->next = 0;
  179.   ip->path = (char *)malloc(strlen(p));
  180.   strcpy(ip->path, p);
  181.   last_include_path->next = ip;
  182.   last_include_path = ip;
  183. }
  184.