Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dthelp / parser / pass2 / build / scan.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: scan.c /main/3 1995/11/08 10:43:54 rswiston $ */
24 /*
25               Copyright 1986 Tandem Computers Incorporated.
26 This product and information is proprietary of Tandem Computers Incorporated.
27                    Copyright (c) 1986, 1987, 1988, 1989 Hewlett-Packard Co.
28 */
29
30 /* Scan.c contains scanner procedures for program BUILD */
31
32 #include <string.h>
33 #if defined(MSDOS)
34 #include <process.h>
35 #endif
36 #include "build.h"
37 #define M_CONDEF
38 #include "context.h"
39 #define M_DELIMDEF
40 #include "delim.h"
41 #include "sref.h"
42
43 /* Reads a name */
44 LOGICAL getname(first)
45   int first;
46 {
47 M_WCHAR *p;
48 int c;
49
50 *(p = name) = first;
51 if ((curcon != DEFAULT && curcon != DEFVAL &&
52 curcon != DEFAULT2 && curcon != DEF2VAL) ||
53 newpar->type != CDATA)
54 *p = m_ctupper(*p);
55 while (TRUE)
56     {
57     c = getachar();
58     *++p = (M_WCHAR) c;
59     if (m_cttype(*p) == M_NONNAME) break;
60     if (p >= name + M_NAMELEN)
61         {
62         *(name + M_NAMELEN) = M_EOS;
63         m_error("Name too long");
64         return(FALSE);
65         }
66     if ((curcon != DEFAULT && curcon != DEFVAL &&
67     curcon != DEFAULT2 && curcon != DEF2VAL) ||
68     newpar->type != CDATA)
69     *p = m_ctupper(*p);
70     }
71 ungetachar(c);
72 *p = M_EOS;
73 return(TRUE);
74 }
75
76 /* Returns the next token to the main procedure */
77 int scan(M_NOPAR)
78 {
79 int c;
80 int n;
81 M_WCHAR wsp, wnl, wtb;
82
83 mbtowc(&wsp, " ", 1);
84 mbtowc(&wnl, "\n", 1);
85 mbtowc(&wtb, "\t", 1);
86
87 while (TRUE)
88     {
89     if (! m_newcon(curcon - 1, TEXT - 1))
90         {
91         while ((n = gettoken(&c, COMCON)) == COMMENT)
92             while ((n = gettoken(&c, COMCON)) != COMMENT)
93                 if (c == EOF)
94                     {
95                     m_error("EOF occurred within comment");
96                     exit(ERREXIT);
97                     }
98         ungetachar(c);
99         }
100     n = gettoken(&c, curcon);
101     if (n)
102         {
103         if (n != LIT && n != LITA) return(n);
104         if (litproc(n))
105             {
106             if (scantrace)
107                 {
108                 char *mb_literal;
109
110                 mb_literal = MakeMByteString(literal);
111                 printf("literal '%s'\n", mb_literal);
112                 m_free(mb_literal, "multibyte string");
113                 }
114             return(LITERAL);
115             }
116         /* Litproc returns FALSE in case of error, when returned have
117            scanned to the end of erroneous rule */
118         else continue;
119         }
120     if (c == wsp || c == wnl || c == wtb)
121         {
122         if (m_newcon(curcon - 1, TEXT - 1))
123             {
124             scanval = (M_WCHAR) c;
125             return(TEXT);
126             }
127         else continue;
128         }
129     if (m_newcon(curcon - 1, NAME - 1))
130         if (m_cttype(c) == M_NMSTART ||
131             (m_cttype(c) != M_NONNAME &&
132               (curcon == DEFAULT || curcon == DEFVAL || curcon == NTNAME ||
133                curcon == DEFAULT2 || curcon == DEF2VAL || curcon == NT2NAME
134                ))
135             )
136             {
137             if (getname(c))
138                 {
139                 if (scantrace)
140                     {
141                     char *mb_name;
142
143                     mb_name = MakeMByteString(name);
144                     printf("name '%s'\n", mb_name);
145                     m_free(mb_name, "multibyte string");
146                     }
147                 return(NAME);
148                 }
149             else continue;
150             }
151     if (c == EOF) return(ENDFILE);
152     if (m_newcon(curcon - 1, TEXT - 1))
153         {
154         scanval = (M_WCHAR) c;
155         return(TEXT);
156         }
157     if (curcon != ERROR)
158         {
159         char mbyte[32]; /* bigger than the biggest multibyte char */
160
161         wctomb(mbyte, c);
162
163         fprintf(stderr, "\n'%s' (%d)", mbyte, c);
164         fprintf(m_errfile, "\n'%s' (%d)", mbyte, c);
165         m_error("Unexpected character");
166         }
167     } /* End while */
168 }   /* End scan */
169
170 #include "scanutil.c"
171
172 #if defined(sparse)
173 #include "sparse.c"
174 #endif