dthelp: Further coverity fixes
[oweals/cde.git] / cde / programs / dthelp / parser / canon1 / 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 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: entout.c /main/3 1995/11/08 09:50:30 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     snprintf(efilename, sizeof(efilename), "%s.h", fname) ;
76     m_openchk(&entfile, efilename, "w") ;
77
78     fprintf(entfile, "#include \"entdef.h\"\n") ;
79     fprintf(entfile, "#if defined(M_ENTDEF)\n") ;
80     fprintf(entfile, "#define M_ENTEXTERN\n") ;
81     fprintf(entfile, "#define M_ENTINIT(a) = a\n") ;
82     fprintf(entfile, "#else\n") ;
83     fprintf(entfile, "#define M_ENTEXTERN extern\n") ;
84     fprintf(entfile, "#define M_ENTINIT(a)\n") ;
85     fprintf(entfile, "#endif\n\n") ;
86
87     if (m_entcnt) {
88       for (ent = firstent, conindex = 0, nameindex = 0 ;
89            ent ; ent = ent->next) {
90         if (ent->content) conindex += w_strlen(ent->content) + 1 ;
91         nameindex += w_strlen(ent->name) + 1 ;
92         }
93       fprintf(entfile, "M_ENTEXTERN M_WCHAR m_entcon[%d]\n",
94         conindex ? conindex : 1) ;
95       if (conindex) {
96         fputs("#if defined(M_ENTDEF)\n  = {\n", entfile) ;
97         start = FALSE ;
98         for (ent = firstent ; ent ; ent = ent->next)
99           if (ent->content) {
100             if (start) fputs(",\n", entfile) ;
101             start = TRUE ;
102             for (p = ent->content ; *p ; p++)
103               fprintf(entfile, "  %d,\n", (int)*p) ;
104             fputs("  0", entfile) ;
105             }
106         fprintf(entfile, "}\n#endif\n") ;
107         }
108       fputs("  ;\n\n", entfile) ;
109
110       fprintf(entfile, "M_ENTEXTERN M_WCHAR m_entname[%d]\n", nameindex) ;
111       fputs("#if defined(M_ENTDEF)\n  = {\n", entfile) ;
112       for (ent = firstent ; ent ; ent = ent->next) {
113         for (p = ent->name ; *p ; p++)
114           fprintf(entfile, "  %d,\n", (int)*p) ;
115         if (ent != lastent) fputs("  0,\n", entfile) ;
116         else fputs("  0\n", entfile) ;
117         }
118       fprintf(entfile, "}\n#endif\n  ;\n\n") ;
119
120       }
121     else {
122       fputs("M_ENTEXTERN M_WCHAR m_entcon[1] ;\n", entfile) ;
123       fputs("M_ENTEXTERN M_WCHAR m_entname[1] ;\n", entfile) ;
124       }
125
126     fprintf(entfile, "M_ENTEXTERN M_ENTITY m_entities[%d]\n",
127       m_entcnt ? m_entcnt : 1) ;
128     if (m_entcnt) {
129       fprintf(entfile, "#if defined(M_ENTDEF)\n  = {\n") ;
130       for (ent = firstent, conindex = 0, nameindex = 0 ;
131            ent ; ent = ent->next) {
132         fprintf(entfile, "  {%s, %s, ",
133           typetype(ent->type),
134           typewhere(ent->wheredef)) ;
135         if (ent->content) {
136           fprintf(entfile, "&m_entcon[%d]", conindex) ;
137           conindex += w_strlen(ent->content) + 1 ;
138           }
139         else fputs(" NULL", entfile) ;
140         fprintf(entfile, ", &m_entname[%d]", nameindex) ;
141         nameindex += w_strlen(ent->name) + 1 ;
142 #if defined(BUILDEXTERN)
143         fprintf(entfile, ", %d", ent->index) ;
144         if (ent != lastent)
145           fprintf(entfile, ", &m_entities[%d], 0}", ent->index) ;
146         else fputs(", NULL, 0}", entfile) ;
147 #else
148         fprintf(entfile, ", %d}", ent->codeindex) ;
149 #endif
150         if (ent != lastent) fprintf(entfile, ", \n") ;
151         else fprintf(entfile, "}\n#endif\n") ;
152         }
153       }
154     fprintf(entfile, "  ;\n\n") ;
155
156     if (m_enttrie->data) countdown(m_enttrie, &count) ;
157     fprintf(entfile,
158       "M_ENTEXTERN M_TRIE m_enttrie[%d]\n", count) ;
159     if (m_enttrie->data) {
160       count = 0 ;
161       fputs("#if defined(M_ENTDEF)\n  = {\n  {0, NULL, &m_enttrie[1]}", entfile) ;
162       m_dumptrie(entfile, m_enttrie->data, "m_enttrie", &count, entptr) ;
163       fprintf(entfile, "}\n#endif\n") ;
164       }
165     fprintf(entfile, "  ;\n\n") ;
166 #if defined(BUILDEXTERN)
167     fprintf(entfile, "M_ENTEXTERN int m_entcnt M_ENTINIT(%d) ;\n", m_entcnt) ;
168 #endif
169
170 #if defined(BUILDEXTERN)
171       if (m_entcnt) {
172         fputs("M_ENTEXTERN M_ENTITY *firstent M_ENTINIT(&m_entities[0]) ;\n",
173           entfile) ;
174         fprintf(entfile,
175           "M_ENTEXTERN M_ENTITY *lastent M_ENTINIT(&m_entities[%d]) ;\n",
176           m_entcnt - 1) ;
177         }
178       else {
179         fputs("M_ENTEXTERN M_ENTITY *firstent M_ENTINIT(NULL) ;\n", entfile) ;
180         fputs("M_ENTEXTERN M_ENTITY *lastent M_ENTINIT(NULL) ;\n", entfile) ;
181         }
182 #endif
183
184   } /* End entout() */
185
186 /* Entptr is called by m_dumptrie to output the value stored with a
187    particular entity in the entity trie */
188 void entptr(data)
189   M_ENTITY *data ;
190   {
191     fprintf(entfile, "(M_TRIE *) &m_entities[%d]", data->index - 1) ;
192     }
193
194 /* Typetype returns a string indicating the type of the nth entity.*/
195 char *typetype(n)
196   int n ;
197   {
198     switch(n) {
199       case M_GENERAL: return(xgeneral) ;
200       case M_SYSTEM: return(xsystem) ;
201       case M_STARTTAG: return(xstarttag) ;
202       case M_ENDTAG: return(xendtag) ;
203       case M_MD: return(xmd) ;
204       case M_MS: return(xms) ;
205       case M_PI: return(xpi) ;
206       case M_CDATAENT: return(xcdataent) ;
207       case M_SDATA: return(xsdata) ;
208       case M_CODEPI: return(xcodepi) ;
209       case M_CODESDATA: return(xcodesdata) ;
210       default: return(xerror) ;
211       }
212     }
213
214 /* Typewhere returns a string indicating where the nth entity was defined.*/
215 #if defined(M_PROTO)
216 char *typewhere(unsigned char n)
217 #else
218 char *typewhere(n)
219   unsigned char n ;
220 #endif
221   {
222     switch(n) {
223       case M_DBUILD: return(xdbuild) ;
224       case M_DELTDEF: return(xdeltdef) ;
225       case FALSE: return("0") ;
226       }
227     return("0");
228     }
229
230
231