Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dthelp / parser.ccdf / htag / util / scanutil.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: scanutil.c /main/3 1995/11/08 11:42:16 rswiston $ */
24 /* Copyright (c) 1986, 1987, 1988, 1989 Hewlett-Packard Co. */
25
26 /* Scanutil.c contains procedures used in the scanners of BUILD and ELTDEF */
27
28 extern FILE *ifile;
29
30 /* Getachar returns the next character from the input stream */
31 int getachar(M_NOPAR)
32 {
33 int  c;
34 M_WCHAR wc,wnl;
35 char mbyte[32]; /* bigger than any possible multibyte char */
36 int  length;
37
38 if (toundo) wc = (M_WCHAR) savechar[--toundo];
39 else
40     {
41     length = 0;
42     if ((c = getc(ifile)) == EOF) return(EOF);
43     while (1)
44         {
45         mbyte[length++] = c;
46         mbyte[length]   = 0;
47         if (mblen(mbyte,length) != -1) break; /* hurray! */
48         if (length == MB_CUR_MAX)
49             { /* reached max without a hit */
50             m_error("An invalid multi-byte character was found in the input");
51             c = ' ';
52             length = 1;
53             break;
54             }
55         if ((c = getc(ifile)) == EOF)
56             { /* huh? */
57             m_error("End-of-file found in within a multi-byte character");
58             return(EOF);
59             }
60         }
61     mbtowc(&wc,mbyte,length);
62     }
63
64 mbtowc(&wnl, "\n", 1);
65 if (wc == wnl) m_line++;
66
67 if (wc == 65535)
68     return -1;
69
70 return((int) wc);
71 }
72
73 /* Reads the next token stored in a packed trie (as defined by context.dat) */
74 int gettoken(c, context)
75 int *c;
76 int context;
77 {
78 int hold[MAXD + 1];
79 int ucase;
80 int i, n = 0, current, delim[MAXD + 1];
81
82 if (! (current = m_contree[context - 1]))
83     {
84     *c = getachar();
85     return(M_NULLVAL);
86     }
87 current--;
88 while (TRUE)
89     {
90     hold[n] = getachar();
91     ucase = m_ctupper(hold[n]);
92     delim[n] = FALSE;
93     for (i = current;
94          (int) m_delimtrie[i].symbol < ucase && m_delimtrie[i].more;
95          i++);
96     if ((int) m_delimtrie[i].symbol == ucase)
97         {
98         current = m_delimtrie[i].index;
99         if (! m_delimtrie[current].symbol)
100           delim[n] = m_delimtrie[current].index;
101         n++;
102         }
103     else break;
104     }
105 while (n >= 0)
106     {
107     if (delim[n])
108         {
109         /* Found a delimiter. If it ends with a letter, verify
110            that the following character is not a letter, in order
111            to issue error messages in cases such as <!ENTITYrunon ... */
112         if (m_cttype(hold[n]) != M_NMSTART) return(delim[n]);
113         *c = getachar();
114         ungetachar(*c);
115         if (*c == EOF || m_cttype(*c) != M_NMSTART) return(delim[n]);
116         }
117     if (n) ungetachar(hold[n]);
118     n--;
119     }
120 *c = *hold;
121 return(M_NULLVAL);
122 }
123
124 /* Reads a literal (called after the opening quotation mark is read) */
125 LOGICAL litproc(delim)
126 int delim;
127 {
128 int n, i, number;
129 M_WCHAR *p, *pStart; /* bigger than wide versions of lit or lita */
130 int c;
131
132 for (i = 0 ; i < M_LITLEN ; i++)
133     {
134     n = gettoken(&c, LITCON);
135     switch (n)
136         {
137         case LIT:
138         case LITA:
139             if (n == delim)
140                 {
141                 literal[i] = M_EOS;
142                 return(TRUE);
143                 }
144             pStart = p = MakeWideCharString((n == LIT) ? lit : lita);
145             if (w_strlen(p) + i > M_LITLEN)
146                 {
147                 m_error("Literal too long");
148                 return(FALSE);
149                 }
150             while (*p) literal[i++] = *p++;
151             m_free(pStart,"wide character string");
152             i--;
153             break;
154         case CRO:
155             number = 0;
156             while (! (n = gettoken(&c, CHARENT)))
157                 {
158                 if (c <= '0' || c >= '9')
159                     {
160                     ungetachar(c);
161                     break;
162                     }
163                 number = 10 * number + c - '0';
164                 if (number >= M_CHARSETLEN)
165                     {
166                     m_error("Invalid character code");
167                     return(FALSE);
168                     }
169                 }
170           literal[i] = (M_WCHAR) number;
171           break;
172         case LITRS:
173         case LITRSC:
174 #if defined(BUILDEXTERN)
175           if (curcon == SREF || curcon == S2REF)
176             literal[i] = RS;
177           else
178 #endif
179             i--;
180           break;
181         case LITRE:
182         case LITREC:
183           literal[i] = M_RE;
184           break;
185         case LITTAB:
186         case LITTABC:
187           literal[i] = M_TAB;
188           break;
189         case LITSPACE:
190         case LITCSPACE:
191           literal[i] = M_SPACE;
192           break;
193         default:
194           if (c == EOF)
195               {
196               m_error("End of File occurred within literal");
197               return(FALSE);
198               }
199           literal[i] = (M_WCHAR) c;
200           break;
201         } /* End switch */
202     } /* End for i */
203 n = gettoken(&c, LITCON);
204 if (n == delim)
205     {
206     literal[M_LITLEN + 1] = M_EOS;
207     return(TRUE);
208     }
209 if (n) undodelim(m_dlmptr[n - 1]) ; 
210 else ungetachar(c);
211 m_error("Literal too long");
212 return(FALSE);
213 }
214
215 /* Returns a context-dependent delimiter string to input stream so
216    characters can be reread one at a time in another context */
217 void undodelim(delim)
218 M_WCHAR *delim;
219 {
220 M_WCHAR *p;
221
222 for (p = delim ; *p ; p++);
223
224 p--;
225 while (TRUE)
226     {
227     ungetachar(*p);
228     if (p == delim) return;
229     p--;
230     }
231 }
232
233 /* Returns a character to the input stream to read again later. Unexplained
234    problems occurred using standard ungetc procedure; hence
235    explicit declaration of buffer for read-ahead characters */
236 void ungetachar(c)
237 int c;
238 {
239 M_WCHAR wnl;
240
241 if (toundo >= SAVECHAR)
242     {
243     m_error("Exceeded read-ahead buffer");
244     exit(TRUE);
245     }
246 savechar[toundo++] = c;
247 if (toundo > maxundo) maxundo = toundo;
248
249 mbtowc(&wnl, "\n", 1);
250 if (c == wnl) m_line--;
251 }
252