home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 495a.lha / ISpell_v3.1ljr / src / xgets.c < prev   
C/C++ Source or Header  |  1991-04-06  |  2KB  |  87 lines

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