dthelp: Further coverity fixes
[oweals/cde.git] / cde / programs / dthelp / parser / pass2 / htag2 / custom.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: custom.c /main/7 1996/11/15 11:55:52 rswiston $ */
24 /*
25 Copyright (c) 1988, 1989 Hewlett-Packard Co.
26 */
27
28 /* Custom.c contains standard PARSER functions, customized for the HP
29    HelpTag formatting system. */
30
31 #include "userinc.h"
32 #include "globdec.h"
33 #include <stdlib.h>
34
35
36 #if defined(MSDOS)
37 /* Standard startup code doesn't have room to load inherited environments
38    in some cases.  Since they're not used, don't bother.  (Using Microsoft
39    C compiler).  */
40 void _setenvp(M_NOPAR);
41 void _setenvp(){}
42 #endif
43
44 /* Write input file and line number for an error message */
45 void m_dumpline(file, line)
46 M_WCHAR *file;
47 int line;
48 {
49 char buffer[10];
50 char *mbyte;
51
52 m_errline("Line ");
53 sprintf(buffer, "%d", line);
54 m_errline(buffer);
55 if (!file)
56     { /* no entity file */
57     if (inFileName)
58         {
59         m_errline(" of ");
60         m_errline(inFileName);
61         }
62     }
63 else
64     { /* yes, entity file */
65     mbyte = MakeMByteString(file);
66     m_errline(" of ");
67     m_errline(mbyte);
68     m_free(mbyte, "multi-byte string");
69     }
70 }
71
72 /* Write error message prefix */
73 void m_eprefix(M_NOPAR)
74 {
75 m_errline("\n*****\n");
76 m_dumpline(m_thisfile(), m_thisline());
77 m_errline(",\n");
78 }
79
80 /* Process error message text */
81 void m_errline(p)
82 char *p;
83 {
84 char c;
85
86 for ( ; *p ; p++)
87     {
88     if (m_errfile) putc(*p, m_errfile);
89     putc(*p, stderr);
90     }
91 }
92
93 #if defined(MSDOS)
94 #include <process.h>
95 #endif
96 /* Write error message suffix */
97 void m_esuffix(M_NOPAR)
98 {
99 m_errline(":\n");
100 m_lastchars();
101 if (++m_errcnt == m_errlim)
102     {
103     m_error("Too many errors, processing stopped");
104     m_exit(TRUE);
105     }
106 }
107
108 /* Exit procedure */
109 void m_exit(status)
110 int status;
111 {
112
113 if (status)
114     {
115     if (status == 77) /* tell helptag to re-run for forward xrefs */
116         {
117         if (stoponerror)
118             {
119             if (m_errcnt == 0)
120                 exit(77);
121             else
122                 exit(1);
123             }
124         else
125             exit(66);
126         }
127
128     if (stoponerror)
129         exit(1); /* tell helptag to quit */
130
131     exit(2); /* tell helptag to continue to next phases */
132     }
133
134 exit(0);
135 }
136
137 /* Get-char procedure */
138 int m_getc(m_ptr)
139 void *m_ptr;
140 {
141 int  c;
142 M_WCHAR wc;
143 char badch[2];
144 char mbyte[32]; /* make this bigger than any possible multi-byte char */
145 int  length;
146 static M_WCHAR wcr = 0, wsb, wsp, wtb;
147 char tab, space;
148
149 /* Unix/Dos compatibility: 0D0A handling */
150 if (!wcr)
151     {
152     mbtowc(&wcr, "\r", 1);
153     mbtowc(&wsb, "\032", 1);
154
155     space = M_SPACE;
156     mbtowc(&wsp, &space, 1);
157
158     tab = M_TAB;
159     mbtowc(&wtb, &tab, 1);
160     }
161
162 do  {
163     length = 0;
164
165 #ifdef NO_IBM_PATCH_U446072
166     /* -------------------------------------------------------------- */
167     /*  Note that the call to fflush before each call to getc is      */
168     /*  a workaround for a bug in the re-entrant (thread-safe) getc   */
169     /*  function. Earlier versions of AIX did not have this problem.  */
170     /*  Made it very specific to AIX 4.2. The bug might appear again  */
171     /*  in future releases of AIX. Hopefully this comment would help  */
172     /*  to identify the problem quickly in case it appears again.     */
173     /* -------------------------------------------------------------- */
174     fflush(NULL);
175 #endif
176
177     if ((c = getc((FILE *)m_ptr)) == EOF) return(EOF);
178
179 #ifdef NO_IBM_PATCH_U446072
180     fflush(NULL);
181 #endif
182
183     while (1)
184     {
185         mbyte[length++] = c;
186         mbyte[length]   = 0;
187         if (mblen(mbyte,length) != -1) break; /* hurray! */
188         if (length == MB_CUR_MAX)
189         { /* reached max without a hit */
190             m_error("An invalid multi-byte character was found in the input");
191             c = ' ';
192             length = 1;
193             break;
194         }
195
196 #ifdef NO_IBM_PATCH_U446072
197         /* -------------------------------------------------------------- */
198         /*  Note that the call to fflush before each call to getc is      */
199         /*  a workaround for a bug in the re-entrant (thread-safe) getc   */
200         /*  function. Earlier versions of AIX did not have this problem.  */
201         /*  Made it very specific to AIX 4.2. The bug might appear again  */
202         /*  in future releases of AIX. Hopefully this comment would help  */
203         /*  to identify the problem quickly in case it appears again.     */
204         /* -------------------------------------------------------------- */
205         fflush(NULL);
206 #endif
207
208         if ((c = getc((FILE *) m_ptr)) == EOF)
209         { /* huh? */
210             m_error("End-of-file found in within a multi-byte character");
211             return(EOF);
212         }
213     }
214     mbtowc(&wc,mbyte,length);
215 }
216
217 while ((wc == wcr) || (wc == wsb));
218
219 /* Change tabs to spaces */
220 if (wc == wtb) return((int) wsp);
221 return((int) wc);
222 }
223
224 /* Open SYSTEM entity procedure */
225 void *m_openent(entcontent)
226 M_WCHAR *entcontent;
227 {
228 FILE *open;
229 char *filename;
230 SEARCH *searchp;
231 char *mb_entcontent;
232
233 mb_entcontent = MakeMByteString(entcontent);
234 if (!mb_entcontent || !*mb_entcontent) {
235     m_free(mb_entcontent, "multi-byte string");
236     return NULL; /* null file name, don't open a directory */
237 }
238
239 open = fopen(mb_entcontent, "r");
240 if (open)
241     {
242     m_free(mb_entcontent, "multi-byte string");
243     return((void *) open);
244     }
245
246 for (searchp = path ; searchp ; searchp = searchp->next)
247     {
248     filename = (char *)
249              m_malloc(strlen(searchp->directory) +
250                         strlen(mb_entcontent) + 1,
251                       "filename");
252     strcpy(filename, searchp->directory);
253     strcat(filename, mb_entcontent);
254     open = fopen(filename, "r");
255     m_free(filename, "filename");
256     if (open)
257         {
258         m_free(mb_entcontent, "multi-byte string");
259         return((void *) open);
260         }
261     }
262
263 m_free(mb_entcontent, "multi-byte string");
264 return(NULL);
265 }
266
267 /* Open input file */
268 void *m_openfirst(M_NOPAR)
269 {
270 return((void *) inFile);
271 }
272
273 /* Set program options */
274 void m_setoptions()
275 {
276 if (m_argc > 2)
277     {
278     m_optstring(m_argv[2]);
279     }
280 }
281
282 /* Process signon message text, stripping out MARKUP version number, so
283    only one version number will appear */
284 void m_signmsg(p)
285   char *p;
286   {
287     char *q;
288     char *pCopy;
289
290     if (q = strstr(p, VERSION)) {
291       pCopy = strdup(p);
292       q = strstr(pCopy, VERSION);
293       *q = M_EOS;
294       m_errline(pCopy);
295       free(pCopy);
296       return;
297       }
298     m_errline(p);
299     }
300
301 /* All entity declarations have been processed. */
302 void m_startdoc()
303 {
304 }
305
306 /* Write debugging trace information */
307 void m_trace(p)
308 char *p;
309 {
310 }
311
312
313 void m_wctrace(p)
314 M_WCHAR *p;
315 {
316 char *mb_p;
317
318 mb_p = MakeMByteString(p);
319 m_trace(mb_p);
320 m_free(mb_p,"multi-byte string");
321 }