home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d191 / ispell.lha / ISpell / src.zoo / xgets.c < prev   
C/C++ Source or Header  |  1989-02-22  |  2KB  |  78 lines

  1.  
  2. #include <stdio.h>
  3. #include "config.h"
  4. #include "ispell.h"
  5.  
  6.  
  7. #ifndef MAXINCLUDEFILES
  8. #define MAXINCLUDEFILES    1    /* maximum number of new files in stack */
  9. #endif
  10.  
  11. /*
  12.  * xgets () acts just like gets () except that if a line matches
  13.  * "&Include_File&<something>" xgets () will start reading from the
  14.  * file <something>.
  15.  *
  16.  *  Andrew Vignaux -- andrew@vuwcomp  Fri May  8 16:40:23 NZST 1987
  17.  * modified
  18.  *  Mark Davies -- mark@vuwcomp  Mon May 11 22:38:10 NZST 1987
  19.  */
  20.  
  21. extern int incfileflag;        /* whether xgets() acts exactly like gets() */
  22.  
  23. char *
  24. xgets (str)
  25. char str [];
  26. {
  27. #if MAXINCLUDEFILES == 0
  28.     return gets (str);
  29. #else
  30.     static char * Include_File = DEFINCSTR;
  31.     static int    Include_Len = 0, strlen ();
  32.     static FILE * F [MAXINCLUDEFILES+1], ** current_F = F;
  33.     char * s = str;
  34.     int c;
  35.  
  36.     /* read the environment variable if we havent already */
  37.     if (Include_Len == 0) {
  38.     char * env_variable, * getenv ();
  39.  
  40.     if ((env_variable = getenv (INCSTRVAR)) != NULL)
  41.         Include_File = env_variable;
  42.     Include_Len = strlen (Include_File);
  43.  
  44.     /* initialise the file stack */
  45.     *current_F = stdin;
  46.     }
  47.  
  48.     while (1) {
  49.         if ((c = getc (*current_F)) != EOF && c != '\n') {
  50.         *s++ = c;
  51.         continue;
  52.     }
  53.     *s = '\0';        /* end of line */
  54.     if (c == EOF)
  55.         if (current_F == F) { /* if end of standard input */
  56.         if (s == str) return (NULL);
  57.         } else {
  58.             (void) fclose (*(current_F--));
  59.               if (s == str) continue;
  60.         }
  61.  
  62.     if (incfileflag != 0 && strncmp (str, Include_File, Include_Len) == 0) {
  63.         char * file_name = str + Include_Len;
  64.         if (current_F - F < MAXINCLUDEFILES && strlen (file_name) > 0) {
  65.         FILE * f;
  66.         if (f = fopen (file_name, "r"))
  67.             *(++current_F) = f;
  68.         }
  69.         s = str;
  70.         continue;
  71.     }
  72.     break;
  73.     }
  74.     
  75.     return (str);
  76. #endif
  77. }
  78.