dthelp: Further coverity fixes
[oweals/cde.git] / cde / programs / dthelp / parser / pass2 / htag2 / 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 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: option.c /main/3 1995/11/08 10:46:37 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__)|| defined(__osf__)
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 or __osf__ */
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         optval = thisopt + 1;
205         break;
206     }
207 }
208
209 /* Process a value for a command line option */
210 #if defined(M_PROTO)
211 LOGICAL setvalopt(int thisopt, char *string, LOGICAL filelenonly)
212 #else
213 LOGICAL setvalopt(thisopt, string, filelenonly)
214 int thisopt;
215 char *string;
216 LOGICAL filelenonly;
217 #endif
218 {
219 char *p;
220 int i;
221 char *tempstr;
222
223 /* Ignore leading = (occurs if "option =val" is entered in .opt file) */
224 if (*string == '=') string++;
225
226 /* Check for empty string (occurs if "option= val" is entered in .opt file) */
227 if (! *string)
228     switch(thisopt)
229         {
230         case ONERROR:
231             optval = thisopt;
232             return TRUE;
233         default:
234             return FALSE;
235         }
236
237 switch (thisopt)
238     {
239     case ONERROR:
240         if (! m_mbmbupstrcmp(string, "GO"))
241             stoponerror = FALSE;
242         else if (! m_mbmbupstrcmp(string, "STOP"))
243             stoponerror = TRUE;
244         else m_mberr1("Invalid ONERROR option: %s", string);
245         return(TRUE);
246     default:
247         return(FALSE);
248     }
249 }