71d1d1b289e40734f706073f81b06830e0fe49d8
[oweals/cde.git] / cde / programs / dthelp / parser / canon1 / parser / parser.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 libraries 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: parser.c /main/3 1995/11/08 09:40:55 rswiston $ */
24 /*  Parser.c contains the main procedure for program PARSER */
25
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include "basic.h"
30 #include "trie.h"
31 #include "dtdext.h"
32 #include "context.h"
33 #include "delim.h"
34 #include "parser.h"
35 #include "if.h"
36 #include "entext.h"
37
38 static void scanloop(LOGICAL prolog);
39
40 /* Main procedure */
41 int main(argc, argv)
42   int argc ;
43   char **argv ;
44 {
45 M_WCHAR *wc_userdef;
46
47 #if defined(linux)
48 m_outfile = stdout;
49 m_errfile = stderr;
50 #endif
51
52 m_argc = argc ;
53 m_argv = argv ;
54
55 m_strtcase(1) ;
56 m_globss() ;
57 m_setoptions() ;
58 m_initialize() ;
59
60 /* open and process helptag.ent */
61 wc_userdef = MakeWideCharString("USER-DEFINED-ENTITIES");
62 m_sysecnt = 0;
63 m_entexpand((M_ENTITY *) m_lookfortrie(wc_userdef, m_enttrie));
64 if (m_sysecnt == 1)
65     {
66     scanloop(TRUE);
67     m_closent(m_sysent[0]);
68     }
69 m_sysecnt = 0;
70 m_free(wc_userdef, "user defined entities name");
71
72 /* open and process document */
73 m_curcon = PREAMBLE;
74 if (! (m_sysent[m_sysecnt] = m_openfirst()))
75     {
76     m_error("Unable to open input file") ;
77     m_exit(TRUE) ;
78     }
79 scanloop(FALSE);
80 /* At EOF */
81 m_done() ;
82 return EXIT_SUCCESS;
83 }
84
85 static void scanloop(LOGICAL prolog)
86 {
87 static char parserr[] = "\nM_token=%d, m_prevcon=%d, m_scanval=%d\n" ;
88 static char sopt[] =
89   "prevcon=%d,token=%d,curcon=%d,scanval='%c'(%d),line=%d,netlevel=%d\n" ;
90 static char name[] = "m_name = '%s'\n" ;
91 static char literal[] = "m_literal = '%s'\n" ;
92
93 #if defined(hpux) || defined(_AIX) || defined(sun) || defined(USL) || defined(__osf__) || defined(linux)
94 char buffer[M_LITLEN + 80] ;
95 #else
96 #define max4(a,b,c,d) (a>b&&a>c&&a>d) ? a : ((b>c&&b>d) ? b : (c>d ? c : d))
97 M_WCHAR buffer[max4(sizeof(parserr) + 3,
98                  sizeof(sopt) + 6,
99                  sizeof(name) + M_NAMELEN - 2,
100                  sizeof(literal) + M_LITLEN - 2)] ;
101 #endif
102 while (TRUE)
103     {
104     m_token = m_scan(prolog) ;
105     m_prevcon = m_curcon ;
106     m_curcon = m_newcon(m_prevcon - 1, m_token - 1) ;
107     if (! m_curcon)
108         {
109         if (m_token != M_ENDFILE)
110             {
111             m_error("Parsing table error") ;
112             sprintf(buffer, parserr, m_token, m_prevcon, m_scanval) ;
113             m_errline(buffer) ;
114             m_done() ;
115             }
116         else if (!prolog)
117             {
118             m_error("Unexpected end of document") ;
119             m_showcurelt() ;
120             m_done() ;
121             }
122         else if (m_prevcon != PROLOG)
123             {
124             m_error("Unexpected end of file") ;
125             m_showcurelt() ;
126             m_done() ;
127             }
128         }
129     if ((! m_stacktop->oldtop) &&
130         m_start &&
131         ! m_aftereod &&
132         m_state[m_stacktop->fsastack->current - 1].final &&
133         (m_token != M_ENDFILE) &&
134          (m_token != M_TEXT || ! m_whitespace((M_WCHAR) m_scanval))
135        )
136         {
137         m_error("Expecting end of document") ;
138         m_aftereod = TRUE ;
139         }
140     if (m_scantrace)
141         {
142         sprintf(buffer,
143                 sopt,
144                 m_prevcon,
145                 m_token,
146                 m_curcon,
147                 m_scanval ? m_scanval : ' ',
148                 m_scanval,
149                 m_line[m_sysecnt],
150                 m_netlevel) ;
151         m_trace(buffer) ;
152         if (m_token == M_NAME)
153             {
154             sprintf(buffer, name, m_name) ;
155             m_trace(buffer) ;
156             }
157         if (m_token == M_LITERAL)
158             {
159             sprintf(buffer, literal, m_literal) ;
160             m_trace(buffer) ;
161             }
162         }
163     #include "case.c"
164     m_adjuststate() ;
165     if (m_token == M_ENDFILE) break ;
166     }
167 }