Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dthelp / parser.ccdf / htag / helptag / option.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: option.c /main/3 1995/11/08 11:18:32 rswiston $ */
24 /*   Copyright (c) 1986, 1987, 1988, 1989, 1992 Hewlett-Packard Co. */
25 /* Functions for command-line options for Help Tag/Cache Creek translator */
26 #include "userinc.h"
27 #include "globdec.h"
28
29 #if defined(hpux) || defined(_AIX) || defined(sun) || defined(USL) || defined(__uxp__)
30 /* get definition of getenv(3c) for getting of environment variables */
31 #include <stdlib.h>
32 #endif
33
34
35 /* Interpret options from command line and specified files */
36 #define OPTLEN 512 /* If OPTLEN changes, change fscanf call below */
37 #define TAGDOTOPT "helptag.opt"
38 #define DOTOPT ".opt"
39
40 #if defined(M_PROTO)
41 void options(LOGICAL filelenonly)
42 #else
43 void options(filelenonly)
44   LOGICAL filelenonly;
45 #endif
46 {
47 int i;
48 FILE *optfile;
49 char option[OPTLEN + 1];
50 char *nameofoptfile;
51
52 /* Check helptag.opt in installation directory */
53 nameofoptfile = (char *) m_malloc(strlen(install) + strlen(TAGDOTOPT) + 1,
54                                   "installation helptag.opt");
55 strcpy(nameofoptfile, install);
56 strcat(nameofoptfile, TAGDOTOPT);
57 if (optfile = fopen(nameofoptfile, "r"))
58     {
59     while (fscanf(optfile, "%512s", option) != EOF)
60         setopt(option, filelenonly);
61     fclose(optfile);
62     }
63 m_free(nameofoptfile, "installation helptag.opt");
64
65 #if defined(hpux) || defined(_AIX) || defined(sun) || defined(USL) || defined(__uxp__)
66 {
67 char *opts;
68
69 /* Check options in $TAGOPT */
70 opts = getenv("TAGOPT");
71 if (opts)
72     {
73     while (m_whitespace(*opts)) opts++;
74     while (sscanf(opts, "%512s", option) != EOF)
75         {
76         setopt(option, filelenonly);
77         opts += strlen(option);
78         while (m_whitespace(*opts)) opts++;
79         }
80     }
81 }
82 #endif /* hpux or _AIX or sun */
83
84 /* Check helptag.opt in input directory */
85 if (indir)
86     {
87     nameofoptfile = (char *) m_malloc(strlen(indir) + strlen(TAGDOTOPT) + 1,
88                                     "input helptag.opt");
89     strcpy(nameofoptfile, indir);
90     strcat(nameofoptfile, TAGDOTOPT);
91     }
92 else nameofoptfile = TAGDOTOPT;
93
94 if (optfile = fopen(nameofoptfile, "r"))
95     {
96     while (fscanf(optfile, "%512s", option) != EOF)
97         setopt(option, filelenonly);
98     fclose(optfile);
99     }
100
101 if (indir) m_free(nameofoptfile, "input helptag.opt");
102
103 /* Check basename.opt in input directory */
104 nameofoptfile = (char *) m_malloc((indir ? strlen(indir) : 0) +
105                                   strlen(nodirbase) + strlen(DOTOPT) + 1,
106                                   "basename.opt");
107 *nameofoptfile = M_EOS;
108
109 if (indir) strcpy(nameofoptfile, indir);
110
111 strcat(nameofoptfile, nodirbase);
112 strcat(nameofoptfile, DOTOPT);
113
114 if (optfile = fopen(nameofoptfile, "r"))
115     {
116     while (fscanf(optfile, "%512s", option) != EOF)
117         setopt(option, filelenonly);
118     fclose(optfile);
119     }
120 m_free(nameofoptfile, "basename.opt");
121
122 /* Read command line options */
123 for (i = 3 ; i < m_argc ; i++)
124     if (*m_argv[i] == '@')
125         {
126         if (optfile = fopen(m_argv[i] + 1, "r"))
127             {
128             while (fscanf(optfile, "%512s", option) != EOF)
129             setopt(option, filelenonly);
130             fclose(optfile);
131             }
132         else
133             {
134             m_mberr1("Unable to open option file %s", (m_argv[i] + 1));
135             }
136         }
137     else setopt(m_argv[i], filelenonly);
138
139 if (filelenonly) return ;    
140
141 if (optval)
142     {
143     m_mberr1("%s: Expecting value for option on command line or in option file",
144              optkey[optval - 1]);
145     }
146 optval = M_NULLVAL;
147 }
148
149
150 /* Set a single option */
151 /* Workonly parameter described with function options()*/
152 #if defined(M_PROTO)
153 void setopt(char *string, LOGICAL filelenonly)
154 #else
155 void setopt(string, filelenonly)
156 char *string;
157 LOGICAL filelenonly;
158 #endif
159 {
160 char *p;
161 int thisopt;
162 LOGICAL ok;
163
164 if (optval)
165     {
166     /* Ignore '=' by itself */
167     if (*string == '=' && *(string + 1) == M_EOS) return;
168     setvalopt(optval, string, filelenonly);
169     optval = M_NULLVAL;
170     return;
171     }
172
173 if (p = strchr(string, '='))
174     {
175     *p = M_EOS;
176     for (thisopt = 0 ; thisopt < NUMOPTIONS ; thisopt++)
177         if (! m_mbmbupstrcmp(string, optkey[thisopt]))
178             break;
179
180     /* Note: call setvalopt only if thisopt < NUMOPTIONS */
181     ok = (LOGICAL)
182         ((thisopt < NUMOPTIONS) && setvalopt(thisopt + 1, p + 1, filelenonly));
183     *p = '=';
184     if (! ok && ! filelenonly)
185         m_mberr1("%s: Unrecognized option on command line or in option file",
186                  string);
187     return;
188     } /* End strchr(string, '=') */
189
190 for (thisopt = 0 ; thisopt < NUMOPTIONS ; thisopt++)
191     if (! m_mbmbupstrcmp(string, optkey[thisopt]))
192         break;
193
194 if (thisopt >= NUMOPTIONS)
195     {
196     if (! filelenonly)
197         m_mberr1("%s: Unrecognized option on command line or in option file",
198                  string);
199     return;
200     }
201 else switch(thisopt + 1)
202     {
203     case ONERROR:
204     case CHARSET:
205     case SEARCHKEY:
206         optval = thisopt + 1;
207         break;
208     case DRAFT:
209         final = FALSE;
210         break;
211     case FINAL:
212         final = TRUE;
213         break;
214 #if defined(FUTURE_FEATURE)
215     case GRAPHICS:
216         dofigure = SMOOTH;
217         break;
218     case NOGRAPHICS:
219         dofigure = FALSE;
220         break;
221     case REV:
222         prntrevs = TRUE;
223         break;
224     case NOREV:
225         prntrevs = FALSE;
226         break;
227     case TOC:
228         toc = TRUE;
229         if (tocfile!=NULL)
230             {
231             m_free(tocfile, "TOC option");
232             tocfile = NULL;
233             }
234         break;
235     case NOTOC:
236         toc = FALSE;
237         if (tocfile!=NULL)
238             {
239             m_free(tocfile, "NOTOC option");
240             tocfile = NULL;
241             }
242         break;
243     case INDEX:
244         break;
245     case NOINDEX:
246         break;
247 #endif /* FUTURE_FEATURE */
248     case MEMO:
249         memo = TRUE;
250         break;
251     case NOMEMO:
252         memo = FALSE;
253         break;
254     case CLEARSEARCH:
255         {
256         SEARCH *searchp, *searchq;
257
258         for (searchp = path ; searchp ;)
259             {
260             searchq = searchp;
261             searchp = searchp->next;
262             m_free(searchq->directory, "search directory");
263             m_free(searchq, "search path");
264             }
265         path = NULL;
266         endpath = &path;
267         break;
268         }
269     case LONGFILES:
270     case LONGFILE:
271     case LONG:
272         usingshortnames = FALSE;
273     break;
274     case SHORTFILES:
275     case SHORTFILE:
276     case SHORT:
277         usingshortnames = TRUE;
278     break;
279     }
280 }
281
282 /* Process a value for a command line option */
283 #if defined(M_PROTO)
284 LOGICAL setvalopt(int thisopt, char *string, LOGICAL filelenonly)
285 #else
286 LOGICAL setvalopt(thisopt, string, filelenonly)
287 int thisopt;
288 char *string;
289 LOGICAL filelenonly;
290 #endif
291 {
292 char *p;
293 int i;
294 char *tempstr;
295
296 /* Ignore leading = (occurs if "option =val" is entered in .opt file) */
297 if (*string == '=') string++;
298
299 /* Check for empty string (occurs if "option= val" is entered in .opt file) */
300 if (! *string)
301     switch(thisopt)
302         {
303         case SEARCHKEY:
304         case CHARSET:
305         case ONERROR:
306             optval = thisopt;
307             return TRUE;
308         default:
309             return FALSE;
310         }
311
312 switch (thisopt)
313     {
314     case SEARCHKEY:
315         if ( (parentsrch==TRUE) && (*string!=M_EOS) && (*string!=dirsep)
316 #if defined(MSDOS)
317          && ( *(string+1) != ':' )
318 #endif
319       )
320             {
321             /* prepend "..", for index processing in temporary sub-directory */
322             tempstr = (char *) m_malloc( strlen(string) + 4, "tempstr search");
323             strcpy(tempstr, "..");
324             strcat(tempstr, SSEP);
325             strcat(tempstr, string);
326             }
327         else
328             {
329             tempstr = (char *) m_malloc( strlen(string) + 1, "tempstr search");
330             strcpy(tempstr, string);
331             }
332
333         *endpath = (SEARCH *) m_malloc(sizeof(SEARCH), "search path");
334         p = strchr(tempstr, M_EOS);
335         if (p != tempstr && *(p - 1) != dirsep
336     #if defined(MSDOS)
337               && *(p - 1) != ':'
338     #endif
339            )
340             {
341             i = strlen(tempstr);
342             (*endpath)->directory = (char *) m_malloc(i + 2,
343                                                       "search directory");
344             strcpy((*endpath)->directory, tempstr);
345             (*endpath)->directory[i] = dirsep;
346             (*endpath)->directory[i + 1] = M_EOS;
347             }
348         else
349             {
350             (*endpath)->directory = (char *) m_malloc(strlen(tempstr) + 1,
351                                                     "search directory");
352             strcpy((*endpath)->directory, tempstr);
353             }
354         (*endpath)->next = NULL;
355         endpath = &(*endpath)->next;
356         m_free(tempstr, "tempstr search");
357         return(TRUE);
358     case CHARSET:
359         m_free(helpcharset, "help charset");
360         helpcharset = MakeWideCharString(string);
361         return TRUE;
362     case ONERROR:
363         if (! m_mbmbupstrcmp(string, "GO"))
364             stoponerror = FALSE;
365         else if (! m_mbmbupstrcmp(string, "STOP"))
366             stoponerror = TRUE;
367         else m_mberr1("Invalid ONERROR option: %s", string);
368         return(TRUE);
369     default:
370         return(FALSE);
371     }
372 }