1/*  Last edited: Apr 10 09:53 1997 (birney) */
2
3
4%{
5#include "wisebase.h"
6
7
8
9
10
11
12%}
13
14api
15func strip_out_remaining_options_with_warning
16func strip_out_boolean_argument
17func strip_out_assigned_argument
18func strip_out_integer_argument
19func strip_out_float_argument
20endapi
21
22
23
24
25%{
26#include "commandline.h"
27
28
29%func
30This removes all remaining options, issuing warnings
31through warn
32%%
33boolean strip_out_remaining_options_with_warning(int * argc,char ** argv)
34{
35  register int i;
36  boolean ret = FALSE;
37
38
39  for(i=0;i<*argc;i++) {
40    if( argv[i][0] != '-' )
41      continue;
42
43    /*** ignore bare '-' - could be arguments ***/
44
45    if( argv[i][1] == '\0')
46      continue;
47
48    warn("You have an unrecognised argument, %s, removing",argv[i]);
49
50    memmove(argv+i,argv+i+1,sizeof(char *)*(*argc - i -1));
51    *argc -= 1;
52    ret = TRUE;
53    }
54
55  return ret;
56}
57
58
59%func
60This removes argument in tag from the commandline if there and
61returns TRUE
62
63otherwise returns FALSE
64%arg
65argc argc from main declaration (call as &argc)
66argv argv from main declaration
67tag  r -[string] argument to find
68%%
69boolean strip_out_boolean_argument(int * argc,char ** argv,char * tag)
70{
71  register int i;
72
73  for(i=0;i<*argc;i++) {
74    if( argv[i][0] != '-' )
75      continue;
76
77    /** ignore '-' by themselves **/
78
79    if( argv[i][1] == '\0')
80      continue;
81
82    /** try to strcmp with tag **/
83
84    if( strcmp(tag,argv[i]+1) == 0 ) {
85      memmove(argv+i,argv+i+1,sizeof(char *)*(*argc - i -1));
86
87      *argc -= 1;
88
89      /* return */
90
91      return TRUE;
92    }
93  }
94
95  return FALSE;
96}
97
98%func
99This sets a boolean argument as
100   -tag   == TRUE
101   -notag == FALSE
102
103This is probably better than strip_out_boolean_argument
104as
105  defaults can be provided
106  user can switch either on or off
107%arg
108argc argc from main declaration (call as &argc)
109argv argv from main declaration
110tag  r -[string] argument to find
111value w boolean pointer to write value to
112%%
113boolean strip_out_boolean_def_argument(int * argc,char ** argv,char * tag,boolean * value)
114{
115  int arg;
116  char buffer[64];
117
118  if( (arg = strip_out_boolean_argument(argc,argv,tag)) == TRUE ) {
119    *value = TRUE;
120  }
121
122  sprintf(buffer,"no%s",tag);
123  if( (arg = strip_out_boolean_argument(argc,argv,buffer)) == TRUE ) {
124    *value = FALSE;
125  }
126
127  return TRUE;
128}
129
130%func
131This removes argument in tag from the commandline if there and
132returns the argument to it (in -tag arg - ie with a space).
133
134otherwise returns NULL
135%arg
136argc argc from main declaration (call as &argc)
137argv argv from main declaration
138tag  r -[string] argument to find
139%%
140char * strip_out_assigned_argument(int * argc,char ** argv,char * tag)
141{
142  register int i;
143  register char * ret;
144
145
146  for(i=0;i<*argc;i++) {
147    if( argv[i][0] != '-' )
148      continue;
149
150    /** ignore '-' by themselves **/
151
152    if( argv[i][1] == '\0')
153      continue;
154
155    /** try to strcmp with tag **/
156
157    if( strcmp(tag,argv[i]+1) == 0 ) {
158      /* pick up next argument: if it has a '-' treat that as the argument. */
159
160      if( i+1 >= *argc ) {
161	warn("In processing command line [%s], tag [%s] expects an argument",argv[0],tag);
162	return NULL; /* give 'em nothing */
163      }
164
165
166      /* assign return value */
167
168      ret = argv[i+1];
169
170      /* ok, now remove both tag and argument from the line */
171
172      memmove(argv+i,argv+i+2,sizeof(char *)*(*argc - i -2));
173
174      *argc -= 2;
175
176      /* return */
177
178      return ret;
179    }
180  }
181
182  return NULL;
183}
184
185
186%func
187This removes argument in tag from the commandline if there, and
188looks at the argument as whether it is an int or not.
189
190No tag - returns FALSE and leaves value alone
191Tag but no integer value - issues warning, returns FALSE
192tag but integer value    - sets value, returns true
193%arg
194argc argc from main declaration (call as &argc)
195argv argv from main declaration
196tag  r -[string] argument to find
197value w int pointer to write value to
198%%
199boolean strip_out_integer_argument(int * argc,char ** argv,char * tag,int * value)
200{
201  char * arg;
202
203  if( (arg = strip_out_assigned_argument(argc,argv,tag)) == NULL )
204    return FALSE;
205
206  if( is_integer_string(arg,value) == FALSE ) {
207    warn("Argument [%s] to [%s] is not an integer. Not changing the value [%d]",arg,tag,value);
208    return FALSE;
209  }
210
211  return TRUE;
212}
213
214%func
215This removes argument in tag from the commandline if there, and
216looks at the argument as whether it is a double or not.
217
218No tag - returns FALSE and leaves value alone
219Tag but no integer value - issues warning, returns FALSE
220tag but integer value    - sets value, returns true
221%arg
222argc argc from main declaration (call as &argc)
223argv argv from main declaration
224tag  r -[string] argument to find
225value w double pointer to write value to
226%%
227boolean strip_out_float_argument(int * argc,char ** argv,char * tag,double * value)
228{
229  char * arg;
230
231  if( (arg = strip_out_assigned_argument(argc,argv,tag)) == NULL )
232    return FALSE;
233
234  if( is_double_string(arg,value) == FALSE ) {
235    warn("Argument [%s] to [%s] is not a double. Not changing the value [%f]",arg,tag,value);
236    return FALSE;
237  }
238
239  return TRUE;
240}
241
242%func
243Shows standard options
244%%
245void show_standard_options(FILE * ofp)
246{
247  fprintf(ofp,"Standard options\n");
248  fprintf(ofp,"  -help       help\n");
249  fprintf(ofp,"  -version    show version and compile info\n");
250  fprintf(ofp,"  -silent     No messages    on stderr\n");
251  fprintf(ofp,"  -quiet      No report/info on stderr\n");
252  fprintf(ofp,"  -erroroffstd No warning messages to stderr\n");
253  fprintf(ofp,"  -errorlog   [file] Log warning messages to file\n");
254  fprintf(ofp,"  -errorstyle [server/program] style of error reporting (default program)\n");
255}
256
257
258%func
259Handles default arguments correctly, setting help,version,errors
260handling
261%%
262void strip_out_standard_options(int * argc,char ** argv,void (*show_help)(FILE * ofp),void (*show_version)(FILE * ofp))
263{
264  char * errlog;
265  char * temp;
266
267
268  if( (strip_out_boolean_argument(argc,argv,"help")) == TRUE ) {
269    (*show_help)(stdout);
270    exit(1);
271  }
272
273  if( (strip_out_boolean_argument(argc,argv,"version")) == TRUE ) {
274    (*show_version)(stdout);
275    exit(1);
276  }
277
278  if( (strip_out_boolean_argument(argc,argv,"silent")) == TRUE ) {
279    erroroff(REPORT);
280    erroroff(INFO);
281    erroroff(WARNING);
282  }
283
284  if( (strip_out_boolean_argument(argc,argv,"quiet")) == TRUE ) {
285    erroroff(REPORT);
286    erroroff(INFO);
287  }
288
289  if( (strip_out_boolean_argument(argc,argv,"erroroffstd")) == TRUE ) {
290    errorstderroff(WARNING);
291  }
292
293  if( (temp = strip_out_assigned_argument(argc,argv,"errorstyle")) != NULL ) {
294    if( strcmp(temp,"server") == 0 ) {
295      set_error_display_style(ERROR_DISPLAY_SERVER);
296    } else if ( strcmp(temp,"program") == 0 ) {
297      set_error_display_style(ERROR_DISPLAY_PROGRAM);
298    } else {
299      fprintf(stderr,"Unknown errorstyle %s",temp);
300    }
301  }
302
303  set_log_display_string(argv[0]);
304
305
306  if( (errlog=strip_out_assigned_argument(argc,argv,"errlog")) != NULL ) {
307    if( add_log_filename(errlog) == FALSE ) {
308      fatal("Could not use %s as a error log file\n",errlog);
309    } else {
310      warn("Logging errors to %s as well as stderr",errlog);
311      errorlogon(WARNING);
312    }
313  }
314}
315
316%}
317
318