dthelp: Further coverity fixes
[oweals/cde.git] / cde / programs / dthelp / parser / pass1 / helptag / 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/3 1995/11/08 10:07:46 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 (inputname)
58         { /* use main input, instead.  Only if set though. */
59         mbyte = MakeMByteString(inputname);
60         m_errline(" of ");
61         m_errline(mbyte);
62         m_free(mbyte, "multi-byte string");
63         }
64     }   
65 else
66     { /* yes, entity file */
67     mbyte = MakeMByteString(file);
68     m_errline(" of ");
69     m_errline(mbyte);
70     m_free(mbyte, "multi-byte string");
71     }
72 }
73
74 /* Write error message prefix */
75 void m_eprefix(M_NOPAR)
76 {
77 m_errline("\n*****\n");
78 m_dumpline(m_thisfile(), m_thisline());
79 m_errline(",\n");
80 }
81
82 /* Process error message text */
83 void m_errline(p)
84 char *p;
85 {
86 char c;
87
88 for ( ; *p ; p++)
89     {
90     if (m_errfile) putc(*p, m_errfile);
91     putc(*p, stderr);
92     }
93 }
94
95 #if defined(MSDOS)
96 #include <process.h>
97 #endif
98 /* Write error message suffix */
99 void m_esuffix(M_NOPAR)
100 {
101 m_errline(":\n");
102 m_lastchars();
103 if (++m_errcnt == m_errlim)
104     {
105     m_error("Too many errors, processing stopped");
106     m_exit(TRUE);
107     }
108 }
109
110 /* Exit procedure */
111 void m_exit(status)
112 int status;
113 {
114 if (filefound)
115     {
116     if (m_outfile != stdout)
117         {
118         fclose(m_outfile);
119         if (prebye == postpreamble)
120             m_error("No text in document");
121         }
122     }
123
124 if (status)
125     {
126     if (status == 77) /* tell helptag to re-run for forward xrefs */
127         {
128         if (stoponerror)
129             {
130             if (m_errcnt == 0)
131                 exit(77);
132             else
133                 exit(1);
134             }
135         else
136             exit(66);
137         }
138
139     if (stoponerror)
140         exit(1); /* tell helptag to quit */
141
142     exit(2); /* tell helptag to continue to next phases */
143     }
144
145 exit(0);
146 }
147
148 /* Get-char procedure */
149 int m_getc(m_ptr)
150 void *m_ptr;
151 {
152 int  c;
153 M_WCHAR wc;
154 char badch[2];
155 char mbyte[32]; /* make this bigger than any possible multi-byte char */
156 int  length;
157 static M_WCHAR wcr = 0, wsb, wsp, wtb;
158 char tab, space;
159
160 /* Unix/Dos compatibility: 0D0A handling */ 
161 if (!wcr)
162     {
163     mbtowc(&wcr, "\r", 1);
164     mbtowc(&wsb, "\032", 1);
165
166     space = M_SPACE;
167     mbtowc(&wsp, &space, 1);
168
169     tab = M_TAB;
170     mbtowc(&wtb, &tab, 1);
171     }
172
173 do  {
174     length = 0;
175     if ((c = getc((FILE *) m_ptr)) == EOF) return(EOF);
176     while (1)
177         {
178         mbyte[length++] = c;
179         mbyte[length]   = 0;
180         if (mblen(mbyte,length) != -1) break; /* hurray! */
181         if (length == MB_CUR_MAX)
182             { /* reached max without a hit */
183             m_error("An invalid multi-byte character was found in the input");
184             c = ' ';
185             length = 1;
186             break;
187             }
188         if ((c = getc((FILE *) m_ptr)) == EOF)
189             { /* huh? */
190             m_error("End-of-file found in within a multi-byte character");
191             return(EOF);
192             }
193         }
194     mbtowc(&wc,mbyte,length);
195     }
196 while ((wc == wcr) || (wc == wsb));
197
198 /* Change tabs to spaces */
199 if (wc == wtb) return((int) wsp);
200 return((int) wc);
201 }
202
203 /* Open SYSTEM entity procedure */
204 void *m_openent(entcontent)
205 M_WCHAR *entcontent;
206 {
207 FILE *open;
208 char *filename;
209 SEARCH *searchp;
210 char *mb_entcontent;
211
212 mb_entcontent = MakeMByteString(entcontent);
213 if (!*mb_entcontent)  /* null file name, don't open a directory */
214     {
215     m_free(mb_entcontent, "multi-byte string");
216     return NULL;
217     }
218
219 open = fopen(mb_entcontent, "r");
220 if (open)
221     {
222     m_free(mb_entcontent, "multi-byte string");
223     return((void *) open);
224     }
225
226 for (searchp = path ; searchp ; searchp = searchp->next)
227     {
228     filename = (char *)
229              m_malloc(strlen(searchp->directory) +
230                         strlen(mb_entcontent) + 1,
231                       "filename");
232     strcpy(filename, searchp->directory);
233     strcat(filename, mb_entcontent);
234     open = fopen(filename, "r");
235     m_free(filename, "filename");
236     if (open)
237         {
238         m_free(mb_entcontent, "multi-byte string");
239         return((void *) open);
240         }
241     }
242
243 m_free(mb_entcontent, "multi-byte string");
244 return(NULL);
245 }
246
247 /* Open input file */
248 void *m_openfirst(M_NOPAR)
249 {
250 FILE *first;
251 char *input;
252 int   length;
253
254 if (defaultext)
255     {
256     input = (char *)
257         m_malloc(strlen(m_argv[1]) + strlen(".htg") + 1, "input file name");
258     strcpy(input, m_argv[1]);
259     strcat(input, ".htg");
260     m_openchk(&first, input, "r");
261     if (filelist) puts(input);
262     /* keep name for global use */
263     length = strlen(input);
264     inputname = (M_WCHAR *) m_malloc(length + 1, "saved input file name");
265     mbstowcs(inputname, input, length + 1);
266     m_free(input, "input file name");
267     }
268 else
269     {
270     if (filelist) puts(m_argv[1]);
271     m_openchk(&first, m_argv[1], "r");
272     length = strlen(m_argv[1]);
273     inputname = (M_WCHAR *) m_malloc(length + 1, "saved input file name");
274     mbstowcs(inputname, m_argv[1], length + 1);
275     }
276
277 if (first)
278     {
279     filefound = TRUE;
280     }
281 return((void *) first);
282 }
283
284 /* Set program options */
285 void m_setoptions()
286 {
287 /* F option used for FILELIST (checking done in basename, which is
288 called before this function is called) */
289 if (m_argc > 2)
290     {
291     m_optstring(m_argv[2]);
292     if (strchr(m_argv[2], 'o')) tracetostd = TRUE;
293     if (strchr(m_argv[2], 'O')) tracetostd = TRUE;
294     }
295 }
296
297 /* Process signon message text, stripping out MARKUP version number, so
298    only one version number will appear */
299 void m_signmsg(p)
300   char *p;
301   {
302     char *q;
303     char *pCopy;
304
305     if (q = strstr(p, VERSION)) {
306       pCopy = strdup(p);
307       q = strstr(pCopy, VERSION);
308       if(q) {
309         *q = M_EOS;
310       }
311       m_errline(pCopy);
312       free(pCopy);
313       return;
314       }
315     m_errline(p);
316     }
317
318 /* All entity declarations have been processed.  Can now check if .TEX
319    file uptodate and open appropriate output file */
320 void m_startdoc()
321 {
322 LOGICAL init = TRUE;
323 unsigned char type;
324 M_WCHAR *content;
325 unsigned char wheredef;
326 M_WCHAR *name;
327 M_WCHAR *qfile;
328 char *mbyte;
329
330 /* set locale */  
331 SetDefaultLocale();
332
333 if (! filelist)
334     {
335     texinit();
336     }
337 else
338     { /* list files that make up document */
339     while (name = m_cyclent(init, &type, &content, &wheredef))
340         {
341         init = FALSE;
342         qfile = NULL;
343         if (type == M_SYSTEM)
344             qfile = searchforfile(content);
345         if (qfile)
346             {
347             mbyte = MakeMByteString(qfile);
348             puts(mbyte);
349             m_free(qfile, "figure filename");
350             m_free(mbyte, "multi-byte string");
351             }
352         else if (type == M_SYSTEM)
353           m_err2("Can't find file %s (declared in entity %s)", content, name);
354         }
355     exit(m_errexit);
356     }
357 }
358
359 /* Write debugging trace information */
360 void m_trace(p)
361 char *p;
362 {
363 if (tracetostd) fputs(p, stdout);
364 else fputs(p, m_outfile);
365 }
366
367
368 void m_wctrace(p)
369 M_WCHAR *p;
370 {
371 char *mb_p;
372
373 mb_p = MakeMByteString(p);
374 m_trace(mb_p);
375 m_free(mb_p,"multi-byte string");
376 }