9a34dfd714a5e3922d82bf62affdf153a50b3b51
[oweals/cde.git] / cde / programs / dthelp / parser / pass2 / htag2 / option.c
1 /* $XConsortium: option.c /main/3 1995/11/08 10:46:37 rswiston $ */
2 /*   Copyright (c) 1986, 1987, 1988, 1989, 1992 Hewlett-Packard Co. */
3 /* Functions for command-line options for Help Tag/Cache Creek translator */
4 #include "userinc.h"
5 #include "globdec.h"
6
7 #if defined(hpux) || defined(_AIX) || defined(sun) || defined(USL) || defined(__uxp__)
8 /* get definition of getenv(3c) for getting of environment variables */
9 #include <stdlib.h>
10 #endif
11
12
13 /* Interpret options from command line and specified files */
14 #define OPTLEN 512 /* If OPTLEN changes, change fscanf call below */
15 #define TAGDOTOPT "helptag.opt"
16 #define DOTOPT ".opt"
17
18 #if defined(M_PROTO)
19 void options(LOGICAL filelenonly)
20 #else
21 void options(filelenonly)
22   LOGICAL filelenonly;
23 #endif
24 {
25 int i;
26 FILE *optfile;
27 char option[OPTLEN + 1];
28 char *nameofoptfile;
29
30 /* Check helptag.opt in installation directory */
31 nameofoptfile = (char *) m_malloc(strlen(install) + strlen(TAGDOTOPT) + 1,
32                                   "installation helptag.opt");
33 strcpy(nameofoptfile, install);
34 strcat(nameofoptfile, TAGDOTOPT);
35 if (optfile = fopen(nameofoptfile, "r"))
36     {
37     while (fscanf(optfile, "%512s", option) != EOF)
38         setopt(option, filelenonly);
39     fclose(optfile);
40     }
41 m_free(nameofoptfile, "installation helptag.opt");
42
43 #if defined(hpux) || defined(_AIX) || defined(sun) || defined(USL) || defined(__uxp__)|| defined(__osf__)
44 {
45 char *opts;
46
47 /* Check options in $TAGOPT */
48 opts = getenv("TAGOPT");
49 if (opts)
50     {
51     while (m_whitespace(*opts)) opts++;
52     while (sscanf(opts, "%512s", option) != EOF)
53         {
54         setopt(option, filelenonly);
55         opts += strlen(option);
56         while (m_whitespace(*opts)) opts++;
57         }
58     }
59 }
60 #endif /* hpux or _AIX or sun or __osf__ */
61
62 /* Check helptag.opt in input directory */
63 if (indir)
64     {
65     nameofoptfile = (char *) m_malloc(strlen(indir) + strlen(TAGDOTOPT) + 1,
66                                     "input helptag.opt");
67     strcpy(nameofoptfile, indir);
68     strcat(nameofoptfile, TAGDOTOPT);
69     }
70 else nameofoptfile = TAGDOTOPT;
71
72 if (optfile = fopen(nameofoptfile, "r"))
73     {
74     while (fscanf(optfile, "%512s", option) != EOF)
75         setopt(option, filelenonly);
76     fclose(optfile);
77     }
78
79 if (indir) m_free(nameofoptfile, "input helptag.opt");
80
81 /* Check basename.opt in input directory */
82 nameofoptfile = (char *) m_malloc((indir ? strlen(indir) : 0) +
83                                   strlen(nodirbase) + strlen(DOTOPT) + 1,
84                                   "basename.opt");
85 *nameofoptfile = M_EOS;
86
87 if (indir) strcpy(nameofoptfile, indir);
88
89 strcat(nameofoptfile, nodirbase);
90 strcat(nameofoptfile, DOTOPT);
91
92 if (optfile = fopen(nameofoptfile, "r"))
93     {
94     while (fscanf(optfile, "%512s", option) != EOF)
95         setopt(option, filelenonly);
96     fclose(optfile);
97     }
98 m_free(nameofoptfile, "basename.opt");
99
100 /* Read command line options */
101 for (i = 3 ; i < m_argc ; i++)
102     if (*m_argv[i] == '@')
103         {
104         if (optfile = fopen(m_argv[i] + 1, "r"))
105             {
106             while (fscanf(optfile, "%512s", option) != EOF)
107             setopt(option, filelenonly);
108             fclose(optfile);
109             }
110         else
111             {
112             m_mberr1("Unable to open option file %s", (m_argv[i] + 1));
113             }
114         }
115     else setopt(m_argv[i], filelenonly);
116
117 if (filelenonly) return ;    
118
119 if (optval)
120     {
121     m_mberr1("%s: Expecting value for option on command line or in option file",
122              optkey[optval - 1]);
123     }
124 optval = M_NULLVAL;
125 }
126
127
128 /* Set a single option */
129 /* Workonly parameter described with function options()*/
130 #if defined(M_PROTO)
131 void setopt(char *string, LOGICAL filelenonly)
132 #else
133 void setopt(string, filelenonly)
134 char *string;
135 LOGICAL filelenonly;
136 #endif
137 {
138 char *p;
139 int thisopt;
140 LOGICAL ok;
141
142 if (optval)
143     {
144     /* Ignore '=' by itself */
145     if (*string == '=' && *(string + 1) == M_EOS) return;
146     setvalopt(optval, string, filelenonly);
147     optval = M_NULLVAL;
148     return;
149     }
150
151 if (p = strchr(string, '='))
152     {
153     *p = M_EOS;
154     for (thisopt = 0 ; thisopt < NUMOPTIONS ; thisopt++)
155         if (! m_mbmbupstrcmp(string, optkey[thisopt]))
156             break;
157
158     /* Note: call setvalopt only if thisopt < NUMOPTIONS */
159     ok = (LOGICAL)
160         ((thisopt < NUMOPTIONS) && setvalopt(thisopt + 1, p + 1, filelenonly));
161     *p = '=';
162     if (! ok && ! filelenonly)
163         m_mberr1("%s: Unrecognized option on command line or in option file",
164                  string);
165     return;
166     } /* End strchr(string, '=') */
167
168 for (thisopt = 0 ; thisopt < NUMOPTIONS ; thisopt++)
169     if (! m_mbmbupstrcmp(string, optkey[thisopt]))
170         break;
171
172 if (thisopt >= NUMOPTIONS)
173     {
174     if (! filelenonly)
175         m_mberr1("%s: Unrecognized option on command line or in option file",
176                  string);
177     return;
178     }
179 else switch(thisopt + 1)
180     {
181     case ONERROR:
182         optval = thisopt + 1;
183         break;
184     }
185 }
186
187 /* Process a value for a command line option */
188 #if defined(M_PROTO)
189 LOGICAL setvalopt(int thisopt, char *string, LOGICAL filelenonly)
190 #else
191 LOGICAL setvalopt(thisopt, string, filelenonly)
192 int thisopt;
193 char *string;
194 LOGICAL filelenonly;
195 #endif
196 {
197 char *p;
198 int i;
199 char *tempstr;
200
201 /* Ignore leading = (occurs if "option =val" is entered in .opt file) */
202 if (*string == '=') string++;
203
204 /* Check for empty string (occurs if "option= val" is entered in .opt file) */
205 if (! *string)
206     switch(thisopt)
207         {
208         case ONERROR:
209             optval = thisopt;
210             return TRUE;
211         default:
212             return FALSE;
213         }
214
215 switch (thisopt)
216     {
217     case ONERROR:
218         if (! m_mbmbupstrcmp(string, "GO"))
219             stoponerror = FALSE;
220         else if (! m_mbmbupstrcmp(string, "STOP"))
221             stoponerror = TRUE;
222         else m_mberr1("Invalid ONERROR option: %s", string);
223         return(TRUE);
224     default:
225         return(FALSE);
226     }
227 }