File: cmd_line.cc

package info (click to toggle)
speech-tools 1.1.0-1
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 5,280 kB
  • ctags: 6,800
  • sloc: cpp: 53,460; ansic: 2,936; java: 1,405; makefile: 830; perl: 728; sh: 538; awk: 49; pascal: 14
file content (376 lines) | stat: -rw-r--r-- 11,869 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*************************************************************************/
/*                                                                       */
/*                Centre for Speech Technology Research                  */
/*                     University of Edinburgh, UK                       */
/*                    Copyright (c) 1994,1995,1996                       */
/*                        All Rights Reserved.                           */
/*                                                                       */
/*  Permission to use, copy, modify, distribute this software and its    */
/*  documentation for research, educational and individual use only, is  */
/*  hereby granted without fee, subject to the following conditions:     */
/*   1. The code must retain the above copyright notice, this list of    */
/*      conditions and the following disclaimer.                         */
/*   2. Any modifications must be clearly marked as such.                */
/*   3. Original authors' names are not deleted.                         */
/*  This software may not be used for commercial purposes without        */
/*  specific prior written permission from the authors.                  */
/*                                                                       */
/*  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK        */
/*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
/*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
/*  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE     */
/*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
/*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
/*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
/*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
/*  THIS SOFTWARE.                                                       */
/*                                                                       */
/*************************************************************************/
/*                       Author :  Paul Taylor                           */
/*                       Date   :  October 1994                          */
/*-----------------------------------------------------------------------*/
/*                      Command Line Utilities                           */
/*                                                                       */
/*  awb merged the help usage and argment definitions April 97           */
/*                                                                       */
/*=======================================================================*/
#include <stdlib.h>
#include "EST_unix.h"
#include "EST_String.h"
#include "EST_io_aux.h"
#include "EST_Token.h"
#include "EST_cutils.h"
#include "EST_TList.h"
#include "EST_string_aux.h"
#include "EST_cmd_line.h"

// This is reset by the command line options functions to argv[0]
EST_String est_progname = "ESTtools";

static int valid_option(const EST_Option &option,const char *arg,
			EST_String &sarg);
static void standard_options(int argc, char **argv, const EST_String &usage);
static void arg_error(const EST_String &progname, const EST_String &message);
static void parse_usage(const EST_String &progname, const EST_String &usage, 
			EST_Option &options, EST_Option &al);
static void output_man_options(const EST_String &usage);

int init_lib_ops(EST_Option &al, EST_Option &op)
{
    char *envname;

    // read environment variable operations file if specified
    if ((al.val("-N", 0) != "true") && 
	((envname = getenv("IA_OP_FILE")) != 0))
	if (op.load(getenv("IA_OP_FILE")) != read_ok)
	    exit (1);
    
    // read command line operations file if specified
    if (al.val("-c", 0) != "") 
	if (op.load(al.val("-c")) != read_ok)
	    exit (1);
    
    // override operations with command line options
    override_lib_ops(op, al); 

    if (al.val("-ops", 0) == "true") // print options if required
	cout << op;
    
    return 0;
}

//  An attempt to integrate help, usage and argument definitions in 
//  one string (seems to work)
//  Still to do: 
// *   adding arbitrary "-" at end of files (do we need that ?)
//     dealing with environment var specification of extra options
//     override options function (maybe no longer needed)
//     list of named values for argument
//     Way to identify mandatory argments
int parse_command_line(int argc, 
		       char *argv[],
		       const EST_String &usage,
		       EST_StrList &files,
		       EST_Option &al, int make_stdio)
{
    // Parse the command line arguments returning them in a normalised
    // form in al, and files in files.
    int i;
    EST_Option options;
    EST_String arg;
    (void)make_stdio;

    // help, version, man_options are always supported
    standard_options(argc,argv,usage); 

    // Find valid options, arguments and defaults
    // sets defaults in al
    est_progname = argv[0];
    parse_usage(argv[0],usage,options,al);

    for (i=1; i < argc; i++)
    {
	if (!EST_String(argv[i]).contains("-",0))  // its a filename
	    files.append(argv[i]);
	else if (streq(argv[i],"-"))   // single "-" denotes stdin/out
	    files.append("-");
	else if (!valid_option(options,argv[i],arg))   
	{
	    arg_error(argv[0],
		      EST_String(": Unknown option \"")+argv[i]+"\"\n");
	}
	else                                       // valid option, check args
	{
	    if (options.val(arg) == "true")  // no argument required
		al.add_item(arg, "true");
	    else if (options.val(arg) == "<int>")
	    {
		if (i+1 == argc)
		    arg_error(argv[0],
			      EST_String(": missing int argument for \"")+
			      arg+"\"\n");
		i++;
		if (!EST_String(argv[i]).matches(RXint))
		    arg_error(argv[0],
			      EST_String(": argument for \"")+
			      arg+"\" not an int\n");
		al.add_item(arg,argv[i]);
	    }
	    else if ((options.val(arg) == "<float>") ||
		     (options.val(arg) == "<double>"))
	    {
		if (i+1 == argc)
		    arg_error(argv[0],
			      EST_String(": missing float argument for \"")+
			      arg+"\"\n");
		i++;
		if (!EST_String(argv[i]).matches(RXdouble))
		    arg_error(argv[0],
			      EST_String(": argument for \"")+
			      arg+"\" not a float\n");
		al.add_item(arg,argv[i]);
	    }
	    else if (options.val(arg) == "<string>")
	    {
		if (i+1 == argc)
		    arg_error(argv[0],
			      EST_String(": missing string argument for \"")+
			      arg+"\"\n");
		i++;
		al.add_item(arg,argv[i]);
	    }
	    else if (options.val(arg) == "<ofile>")
	    {
		if (i+1 == argc)
		    arg_error(argv[0],
			      EST_String(": missing ifile argument for \"")+
			      arg+"\"\n");
		i++;
		if (writable_file(argv[i]) == TRUE)
		    al.add_item(arg,argv[i]);
		else 
		    arg_error(argv[0],
			      EST_String(": output file not accessible \"")+
			      argv[i]+"\"\n");
	    }
	    else if (options.val(arg) == "<ifile>")
	    {
		if (i+1 == argc)
		    arg_error(argv[0],
			      EST_String(": missing ifile argument for \"")+
			      arg+"\"\n");
		i++;
		if (readable_file(argv[i]) == TRUE)
		    al.add_item(arg,argv[i]);
		else
		    arg_error(argv[0],
			      EST_String(": input file not accessible \"")+
			      argv[i]+"\"\n");

	    }
	    else if (options.val(arg) == "<star>")
	    {
		al.add_item(arg,EST_String(argv[i]).after(arg));
	    }
	    // else string list 
	    else
	    {
		arg_error(argv[0],
			  EST_String(": unknown argument type \"")+
			  options.val(argv[i])+"\" (misparsed usage string)\n");
	    }
	}
    }

    if (files.length() == 0)
	files.append("-");

    return 0;
}

static int valid_option(const EST_Option &options,const char *arg,
			EST_String &sarg)
{
    // Checks to see arg is declared as an option.
    // This would be trivial were it not for options containing *
    // The actual arg name is put in sarg
    EST_Litem *p;
    
    for (p = options.list.head(); p != 0; p = next(p))
    {
	if (options.key(p) == arg)
	{
	    sarg = arg;
	    return TRUE;
	}
	else if ((options.val(p) == "<star>") &&
		 (EST_String(arg).contains(options.key(p), 0)))
	{
	    sarg = options.key(p);
	    return TRUE;
	}
    }
    
    return FALSE;
}

static void parse_usage(const EST_String &progname, const EST_String &usage, 
			EST_Option &options, EST_Option &al)
{
    // Extract option definitions from usage and put them in options
    // If defaults are specified add them al
    EST_TokenStream ts;
    EST_Token t;
    
    ts.open_string(usage);
    ts.set_SingleCharSymbols("{}[]|");
    ts.set_PunctuationSymbols("");
    ts.set_PrePunctuationSymbols("");
    
    while (!ts.eof())
    {
	t = ts.get();
	if ((t.string().contains("-",0)) &&
	    (t.whitespace().contains("\n")))
	{			// An argument
	    if ((ts.peek().string() == "<string>") ||
		(ts.peek().string() == "<float>") ||
		(ts.peek().string() == "<double>") ||
		(ts.peek().string() == "<ifile>") ||
		(ts.peek().string() == "<ofile>") ||
		(ts.peek().string() == "<int>"))
	    {
		options.add_item(t.string(),ts.get().string());	
		if (ts.peek().string() == "{") // a default is given
		{
		    ts.get();
		    al.add_item(t.string(),ts.get().string());
		    if (ts.get() != "}")
			arg_error(progname,
				  EST_String(": malformed default value for \"")+
				  t.string()+"\" (missing closing brace)\n");
		}
	    }
	    else if (t.string().contains("*"))
		options.add_item(t.string().before("*"),"<star>");
	    // else check for explicit list of names 
	    else
		options.add_item(t.string(),"true"); // simple argument
	}
    }
}

static void arg_error(const EST_String &progname, const EST_String &message)
{
    // Output message  and pointer to more help then exit
    cerr << progname << message;
    cerr << "Type -h for help on options.\n";
    exit(-1);
}

static void standard_options(int argc, char **argv, const EST_String &usage)
{
    // A number of options are always supported 
    int i;
    
    for (i=1; i < argc; i++)
    {
	if (streq(argv[i],"-man_options"))
	{
	    output_man_options(usage);
	    exit(0);
	}
	if ((streq(argv[i],"-h")) ||
	    (streq(argv[i],"-help")) ||
	    (streq(argv[i],"-?")) ||
	    (streq(argv[i],"--help")))
	{
	    cout << usage << endl;
	    exit(0);
	}
	if (((streq(argv[i],"-version")) ||
	     (streq(argv[i],"--version")))&&
	    (!usage.contains("\n-v")))
	{
	    cout << argv[0] << ": " << est_tools_version << endl;
	    exit(0);
	}
    }
    
    return;
}

static void output_man_options(const EST_String &usage)
{
    EST_TokenStream ts;
    EST_Token t;
    int in_options = FALSE;
    
    ts.open_string(usage);
    ts.set_SingleCharSymbols("{}[]|");
    ts.set_PunctuationSymbols("");
    ts.set_PrePunctuationSymbols("");
    
    while (!ts.eof())
    {
	t = ts.get();
	if ((t.string().contains("-",0)) &&
	    (t.whitespace().contains("\n")))
	{			// An argument
	    fprintf(stdout,"\n.TP 8\n.BI \"%s \" ",(const char *)t.string());
	    if ((ts.peek().string() == "<string>") ||
		(ts.peek().string() == "<float>") ||
		(ts.peek().string() == "<double>") ||
		(ts.peek().string() == "<int>"))
		fprintf(stdout,"%s",(const char *)ts.get().string());
	    if ((ts.peek().string() == "{"))
	    {			// a default value
		ts.get();
		fprintf(stdout," \" {%s}\"",(const char *)ts.get().string());
		ts.get();
	    }
	    if (!ts.peek().whitespace().contains("\n"))
		fprintf(stdout,"\n");
	    in_options = TRUE;
	}
	else if (in_options)
	{
	    if (t.whitespace().contains("\n"))
		fprintf(stdout,"\n");
	    fprintf(stdout,"%s ",(const char *)t.string());
	}
    }
    if (in_options)
	fprintf(stdout,"\n");
    
    
}

EST_String options_general(void)
{
    // The standard waveform input options 
    return
	EST_String("")+
	    "-o <ofile>      output file" +
		"-otype <string> output file type\n";
}