source: trunk/third/ispell/xgets.c @ 10334

Revision 10334, 4.4 KB checked in by ghudson, 27 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r10333, which included commits to RCS files with non-trunk default branches.
Line 
1#ifndef lint
2static char Rcs_Id[] =
3    "$Id: xgets.c,v 1.1.1.1 1997-09-03 21:08:10 ghudson Exp $";
4#endif
5
6/*
7 * Copyright 1987, 1988, 1989, 1992, 1993, Geoff Kuenning, Granada Hills, CA
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All modifications to the source code must be clearly marked as
20 *    such.  Binary redistributions based on modified source code
21 *    must be clearly marked as modified versions in the documentation
22 *    and/or other materials provided with the distribution.
23 * 4. All advertising materials mentioning features or use of this software
24 *    must display the following acknowledgment:
25 *      This product includes software developed by Geoff Kuenning and
26 *      other unpaid contributors.
27 * 5. The name of Geoff Kuenning may not be used to endorse or promote
28 *    products derived from this software without specific prior
29 *    written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY GEOFF KUENNING AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED.  IN NO EVENT SHALL GEOFF KUENNING OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * SUCH DAMAGE.
42 */
43
44/*
45 * $Log: not supported by cvs2svn $
46 * Revision 1.22  1994/09/16  04:48:34  geoff
47 * Be sure to deliver newlines to the caller, so that it can tell whether
48 * or not a complete line was read.
49 *
50 * Revision 1.21  1994/01/25  07:12:22  geoff
51 * Get rid of all old RCS log lines in preparation for the 3.1 release.
52 *
53 */
54
55#include "config.h"
56#include "ispell.h"
57#include "proto.h"
58
59char *          xgets P ((char * string, int size, FILE * stream));
60
61#ifndef MAXINCLUDEFILES
62#define MAXINCLUDEFILES 1       /* maximum number of new files in stack */
63#endif
64
65/*
66 * xgets () acts just like gets () except that if a line matches
67 * "&Include_File&<something>" xgets () will start reading from the
68 * file <something>.
69 *
70 *  Andrew Vignaux -- andrew@vuwcomp  Fri May  8 16:40:23 NZST 1987
71 * modified
72 *  Mark Davies -- mark@vuwcomp  Mon May 11 22:38:10 NZST 1987
73 */
74
75char * xgets (str, size, stream)
76    char                str[];
77    int                 size;
78    FILE *              stream;
79    {
80#if MAXINCLUDEFILES == 0
81    return fgets (str, size, stream);
82#else
83    static char *       Include_File = DEFINCSTR;
84    static int          Include_Len = 0;
85    static FILE *       F[MAXINCLUDEFILES+1];
86    static FILE **      current_F = F;
87    char *              s = str;
88    int                 c;
89
90    /* read the environment variable if we havent already */
91    if (Include_Len == 0)
92        {
93        char *          env_variable;
94
95        if ((env_variable = getenv (INCSTRVAR)) != NULL)
96            Include_File = env_variable;
97        Include_Len = strlen (Include_File);
98
99        /* initialise the file stack */
100        *current_F = stream;
101        }
102
103    for (  ;  ;  )
104        {
105        c = '\0';
106        if ((s - str) + 1 < size
107          &&  (c = getc (*current_F)) != EOF)
108            {
109            *s++ = (char) c;
110            if (c != '\n')
111                continue;
112            }
113        *s = '\0';              /* end of line */
114        if (c == EOF)
115            {
116            if (current_F == F) /* if end of standard input */
117                {
118                if (s == str)
119                    return (NULL);
120                }
121            else
122                {
123                (void) fclose (*(current_F--));
124                if (s == str) continue;
125                }
126            }
127
128        if (incfileflag != 0
129          &&  strncmp (str, Include_File, (unsigned int) Include_Len) == 0)
130            {
131            char *      file_name = str + Include_Len;
132
133            if (current_F - F < MAXINCLUDEFILES  &&  strlen (file_name) != 0)
134                {
135                FILE *  f;
136
137                if (f = fopen (file_name, "r"))
138                    *(++current_F) = f;
139                }
140            s = str;
141            continue;
142            }
143        break;
144        }
145   
146    return (str);
147#endif
148    }
Note: See TracBrowser for help on using the repository browser.