Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dthelp / parser / pass2 / util / entout.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: entout.c /main/3 1995/11/08 11:04:43 rswiston $ */
24 /* Copyright (c) 1986, 1987, 1988, 1989 Hewlett-Packard Co. */
25
26 /* Entout has procedures to write generated entity TRIE */
27
28 void entptr(
29 #if defined(M_PROTO)
30   M_ENTITY *data
31 #endif
32   ) ;
33
34 char *typetype(
35 #if defined(M_PROTO)
36   int n
37 #endif
38   ) ;
39
40 char *typewhere(
41 #if defined(M_PROTO)
42   unsigned char n
43 #endif
44   ) ;
45
46 /* Macro names written to output file */
47 char xdbuild[] = "M_DBUILD" ;
48 char xdeltdef[] = "M_DELTDEF" ;
49 char xgeneral[] = "M_GENERAL" ;
50 char xsystem[] = "M_SYSTEM" ;
51 char xstarttag[] = "M_STARTTAG" ;
52 char xendtag[] = "M_ENDTAG" ;
53 char xmd[] = "M_MD" ;
54 char xms[] = "M_MS" ;
55 char xpi[] = "M_PI" ;
56 char xcdataent[] = "M_CDATAENT" ;
57 char xsdata[] = "M_SDATA" ;
58 char xcodepi[] = "M_CODEPI" ;
59 char xcodesdata[] = "M_CODESDATA" ;
60 char xerror[] = "ERROR" ;
61
62 /* Outputs entity definitions */
63 #define ENTFILENAME 12
64 void entout(fname)
65   char *fname ;
66   {
67     char efilename[ENTFILENAME] ;
68     int count = 1 ;
69     M_ENTITY *ent ;
70     M_WCHAR *p ;
71     int conindex ;
72     int nameindex ;
73     LOGICAL start ;
74
75     strcpy(efilename, fname) ;
76     strcpy(&efilename[strlen(efilename)], ".h") ;
77     m_openchk(&entfile, efilename, "w") ;
78
79     fprintf(entfile, "#include \"entdef.h\"\n") ;
80     fprintf(entfile, "#if defined(M_ENTDEF)\n") ;
81     fprintf(entfile, "#define M_ENTEXTERN\n") ;
82     fprintf(entfile, "#define M_ENTINIT(a) = a\n") ;
83     fprintf(entfile, "#else\n") ;
84     fprintf(entfile, "#define M_ENTEXTERN extern\n") ;
85     fprintf(entfile, "#define M_ENTINIT(a)\n") ;
86     fprintf(entfile, "#endif\n\n") ;
87
88     if (m_entcnt) {
89       for (ent = firstent, conindex = 0, nameindex = 0 ;
90            ent ; ent = ent->next) {
91         if (ent->content) conindex += w_strlen(ent->content) + 1 ;
92         nameindex += w_strlen(ent->name) + 1 ;
93         }
94       fprintf(entfile, "M_ENTEXTERN M_WCHAR m_entcon[%d]\n",
95         conindex ? conindex : 1) ;
96       if (conindex) {
97         fputs("#if defined(M_ENTDEF)\n  = {\n", entfile) ;
98         start = FALSE ;
99         for (ent = firstent ; ent ; ent = ent->next)
100           if (ent->content) {
101             if (start) fputs(",\n", entfile) ;
102             start = TRUE ;
103             for (p = ent->content ; *p ; p++)
104               fprintf(entfile, "  %d,\n", *p) ;
105             fputs("  0", entfile) ;
106             }
107         fprintf(entfile, "}\n#endif\n") ;
108         }
109       fputs("  ;\n\n", entfile) ;
110
111       fprintf(entfile, "M_ENTEXTERN M_WCHAR m_entname[%d]\n", nameindex) ;
112       fputs("#if defined(M_ENTDEF)\n  = {\n", entfile) ;
113       for (ent = firstent ; ent ; ent = ent->next) {
114         for (p = ent->name ; *p ; p++)
115           fprintf(entfile, "  %d,\n", *p) ;
116         if (ent != lastent) fputs("  0,\n", entfile) ;
117         else fputs("  0\n", entfile) ;
118         }
119       fprintf(entfile, "}\n#endif\n  ;\n\n") ;
120
121       }
122     else {
123       fputs("M_ENTEXTERN M_WCHAR m_entcon[1] ;\n", entfile) ;
124       fputs("M_ENTEXTERN M_WCHAR m_entname[1] ;\n", entfile) ;
125       }
126
127     fprintf(entfile, "M_ENTEXTERN M_ENTITY m_entities[%d]\n",
128       m_entcnt ? m_entcnt : 1) ;
129     if (m_entcnt) {
130       fprintf(entfile, "#if defined(M_ENTDEF)\n  = {\n") ;
131       for (ent = firstent, conindex = 0, nameindex = 0 ;
132            ent ; ent = ent->next) {
133         fprintf(entfile, "  %s, %s, ",
134           typetype(ent->type),
135           typewhere(ent->wheredef)) ;
136         if (ent->content) {
137           fprintf(entfile, "&m_entcon[%d]", conindex) ;
138           conindex += w_strlen(ent->content) + 1 ;
139           }
140         else fputs(" NULL", entfile) ;
141         fprintf(entfile, ", &m_entname[%d]", nameindex) ;
142         nameindex += w_strlen(ent->name) + 1 ;
143 #if defined(BUILDEXTERN)
144         fprintf(entfile, ", %d", ent->index) ;
145         if (ent != lastent)
146           fprintf(entfile, ", &m_entities[%d], 0", ent->index) ;
147         else fputs(", NULL, 0", entfile) ;
148 #else
149         fprintf(entfile, ", %d", ent->codeindex) ;
150 #endif
151         if (ent != lastent) fprintf(entfile, ", \n") ;
152         else fprintf(entfile, "}\n#endif\n") ;
153         }
154       }
155     fprintf(entfile, "  ;\n\n") ;
156
157     if (m_enttrie->data) countdown(m_enttrie, &count) ;
158     fprintf(entfile,
159       "M_ENTEXTERN M_TRIE m_enttrie[%d]\n", count) ;
160     if (m_enttrie->data) {
161       count = 0 ;
162       fputs("#if defined(M_ENTDEF)\n  = {\n  0, NULL, &m_enttrie[1]", entfile) ;
163       m_dumptrie(entfile, m_enttrie->data, "m_enttrie", &count, entptr) ;
164       fprintf(entfile, "}\n#endif\n") ;
165       }
166     fprintf(entfile, "  ;\n\n") ;
167 #if defined(BUILDEXTERN)
168     fprintf(entfile, "M_ENTEXTERN int m_entcnt M_ENTINIT(%d) ;\n", m_entcnt) ;
169 #endif
170
171 #if defined(BUILDEXTERN)
172       if (m_entcnt) {
173         fputs("M_ENTEXTERN M_ENTITY *firstent M_ENTINIT(&m_entities[0]) ;\n",
174           entfile) ;
175         fprintf(entfile,
176           "M_ENTEXTERN M_ENTITY *lastent M_ENTINIT(&m_entities[%d]) ;\n",
177           m_entcnt - 1) ;
178         }
179       else {
180         fputs("M_ENTEXTERN M_ENTITY *firstent M_ENTINIT(NULL) ;\n", entfile) ;
181         fputs("M_ENTEXTERN M_ENTITY *lastent M_ENTINIT(NULL) ;\n", entfile) ;
182         }
183 #endif
184
185   } /* End entout() */
186
187 /* Entptr is called by m_dumptrie to output the value stored with a
188    particular entity in the entity trie */
189 void entptr(data)
190   M_ENTITY *data ;
191   {
192     fprintf(entfile, "(M_TRIE *) &m_entities[%d]", data->index - 1) ;
193     }
194
195 /* Typetype returns a string indicating the type of the nth entity.*/
196 char *typetype(n)
197   int n ;
198   {
199     switch(n) {
200       case M_GENERAL: return(xgeneral) ;
201       case M_SYSTEM: return(xsystem) ;
202       case M_STARTTAG: return(xstarttag) ;
203       case M_ENDTAG: return(xendtag) ;
204       case M_MD: return(xmd) ;
205       case M_MS: return(xms) ;
206       case M_PI: return(xpi) ;
207       case M_CDATAENT: return(xcdataent) ;
208       case M_SDATA: return(xsdata) ;
209       case M_CODEPI: return(xcodepi) ;
210       case M_CODESDATA: return(xcodesdata) ;
211       default: return(xerror) ;
212       }
213     }
214
215 /* Typewhere returns a string indicating where the nth entity was defined.*/
216 #if defined(M_PROTO)
217 char *typewhere(unsigned char n)
218 #else
219 char *typewhere(n)
220   unsigned char n ;
221 #endif
222   {
223     switch(n) {
224       case M_DBUILD: return(xdbuild) ;
225       case M_DELTDEF: return(xdeltdef) ;
226       case FALSE: return("0") ;
227       }
228     }
229
230
231