root / trunk / Linux / addons / ofxASR / libs / sphinx / include / sphinxbase / cmd_ln.h @ 59

View | Annotate | Download (18 KB)

1
/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
/* ====================================================================
3
 * Copyright (c) 1999-2004 Carnegie Mellon University.  All rights
4
 * reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer. 
12
 *
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in
15
 *    the documentation and/or other materials provided with the
16
 *    distribution.
17
 *
18
 * This work was supported in part by funding from the Defense Advanced 
19
 * Research Projects Agency and the National Science Foundation of the 
20
 * United States of America, and the CMU Sphinx Speech Consortium.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
23
 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
24
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26
 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
28
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
29
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
30
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
31
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
32
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
 *
34
 * ====================================================================
35
 *
36
 */
37
/*
38
 * cmd_ln.h -- Command line argument parsing.
39
 *
40
 * **********************************************
41
 * CMU ARPA Speech Project
42
 *
43
 * Copyright (c) 1999 Carnegie Mellon University.
44
 * ALL RIGHTS RESERVED.
45
 * **********************************************
46
 * 
47
 * HISTORY
48
 * 
49
 * 15-Jul-1997        M K Ravishankar ([email protected]) at Carnegie Mellon University
50
 *                 Added required arguments types.
51
 * 
52
 * 07-Dec-96        M K Ravishankar ([email protected]) at Carnegie Mellon University
53
 *                 Created, based on Eric's implementation.  Basically, combined several
54
 *                functions into one, eliminated validation, and simplified the interface.
55
 */
56
57
58
#ifndef _LIBUTIL_CMD_LN_H_
59
#define _LIBUTIL_CMD_LN_H_
60
61
#include <stdio.h>
62
#include <stdarg.h>
63
64
/* Win32/WinCE DLL gunk */
65
#include <sphinxbase_export.h>
66
#include <prim_type.h>
67
68
/**
69
 * @file cmd_ln.h
70
 * @brief Command-line and other configurationparsing and handling.
71
 *  
72
 * Configuration parameters, optionally parsed from the command line.
73
 */
74
  
75
76
#ifdef __cplusplus
77
extern "C" {
78
#endif
79
#if 0
80
/* Fool Emacs. */
81
}
82
#endif
83
84
/**
85
 * @struct arg_t
86
 * Argument definition structure.
87
 */
88
typedef struct arg_s {
89
        char const *name;   /**< Name of the command line switch */
90
        int type;           /**< Type of the argument in question */
91
        char const *deflt;  /**< Default value (as a character string), or NULL if none */
92
        char const *doc;    /**< Documentation/description string */
93
} arg_t;
94
95
/**
96
 * @name Values for arg_t::type
97
 */
98
/* @{ */
99
/**
100
 * Bit indicating a required argument.
101
 */
102
#define ARG_REQUIRED (1<<0)
103
/**
104
 * Integer argument (optional).
105
 */
106
#define ARG_INTEGER  (1<<1)
107
/**
108
 * Floating point argument (optional).
109
 */
110
#define ARG_FLOATING (1<<2)
111
/**
112
 * String argument (optional).
113
 */
114
#define ARG_STRING   (1<<3)
115
/**
116
 * Boolean (true/false) argument (optional).
117
 */
118
#define ARG_BOOLEAN  (1<<4)
119
120
/**
121
 * Required integer argument.
122
 */
123
#define REQARG_INTEGER (ARG_INTEGER | ARG_REQUIRED)
124
/**
125
 * Required floating point argument.
126
 */
127
#define REQARG_FLOATING (ARG_FLOATING | ARG_REQUIRED)
128
/**
129
 * Required string argument.
130
 */
131
#define REQARG_STRING (ARG_STRING | ARG_REQUIRED)
132
/**
133
 * Required boolean argument.
134
 */
135
#define REQARG_BOOLEAN (ARG_BOOLEAN | ARG_REQUIRED)
136
137
/**
138
 * @deprecated Use ARG_INTEGER instead.
139
 */
140
#define ARG_INT32   ARG_INTEGER
141
/**
142
 * @deprecated Use ARG_FLOATING instead.
143
 */
144
#define ARG_FLOAT32 ARG_FLOATING
145
/**
146
 * @deprecated Use ARG_FLOATING instead.
147
 */
148
#define ARG_FLOAT64 ARG_FLOATING
149
/**
150
 * @deprecated Use REQARG_INTEGER instead.
151
 */
152
#define REQARG_INT32 (ARG_INT32 | ARG_REQUIRED)
153
/**
154
 * @deprecated Use REQARG_FLOATING instead.
155
 */
156
#define REQARG_FLOAT32 (ARG_FLOAT32 | ARG_REQUIRED)
157
/**
158
 * @deprecated Use REQARG_FLOATING instead.
159
 */
160
#define REQARG_FLOAT64 (ARG_FLOAT64 | ARG_REQUIRED)
161
/* @} */
162
163
164
/**
165
 * Helper macro to stringify enums and other non-string values for
166
 * default arguments.
167
 **/
168
#define ARG_STRINGIFY(s) ARG_STRINGIFY1(s)
169
#define ARG_STRINGIFY1(s) #s
170
171
/**
172
 * @struct cmd_ln_t
173
 * Opaque structure used to hold the results of command-line parsing.
174
 */
175
typedef struct cmd_ln_s cmd_ln_t;
176
177
/**
178
 * Create a cmd_ln_t from NULL-terminated list of arguments.
179
 *
180
 * This function creates a cmd_ln_t from a NULL-terminated list of
181
 * argument strings.  For example, to create the equivalent of passing
182
 * "-hmm foodir -dsratio 2 -lm bar.lm" on the command-line:
183
 *
184
 *  config = cmd_ln_init(NULL, defs, TRUE, "-hmm", "foodir", "-dsratio", "2",
185
 *                       "-lm", "bar.lm", NULL);
186
 *
187
 * Note that for simplicity, <strong>all</strong> arguments are passed
188
 * as strings, regardless of the actual underlying type.
189
 *
190
 * @param inout_cmdln Previous command-line to update, or NULL to create a new one.
191
 * @param defn Array of argument name definitions, or NULL to allow any arguments.
192
 * @param strict Whether to fail on duplicate or unknown arguments.
193
 * @return A cmd_ln_t* containing the results of command line parsing, or NULL on failure.
194
 */
195
SPHINXBASE_EXPORT
196
cmd_ln_t *cmd_ln_init(cmd_ln_t *inout_cmdln, arg_t const *defn, int32 strict, ...);
197
198
/**
199
 * Retain ownership of a command-line argument set.
200
 *
201
 * @return pointer to retained command-line argument set.
202
 */
203
SPHINXBASE_EXPORT
204
cmd_ln_t *cmd_ln_retain(cmd_ln_t *cmdln);
205
206
/**
207
 * Release a command-line argument set and all associated strings.
208
 *
209
 * @return new reference count (0 if freed completely)
210
 */
211
SPHINXBASE_EXPORT
212
int cmd_ln_free_r(cmd_ln_t *cmdln);
213
214
/**
215
 * Parse a list of strings into argumetns.
216
 *
217
 * Parse the given list of arguments (name-value pairs) according to
218
 * the given definitions.  Argument values can be retrieved in future
219
 * using cmd_ln_access().  argv[0] is assumed to be the program name
220
 * and skipped.  Any unknown argument name causes a fatal error.  The
221
 * routine also prints the prevailing argument values (to stderr)
222
 * after parsing.
223
 *
224
 * @note It is currently assumed that the strings in argv are
225
 *       allocated statically, or at least that they will be valid as
226
 *       long as the cmd_ln_t returned from this function.
227
 *       Unpredictable behaviour will result if they are freed or
228
 *       otherwise become invalidated.
229
 *
230
 * @return A cmd_ln_t containing the results of command line parsing,
231
 *         or NULL on failure.
232
 **/
233
SPHINXBASE_EXPORT
234
cmd_ln_t *cmd_ln_parse_r(cmd_ln_t *inout_cmdln, /**< In/Out: Previous command-line to update,
235
                                                     or NULL to create a new one. */
236
                         arg_t const *defn,        /**< In: Array of argument name definitions */
237
                         int32 argc,                /**< In: Number of actual arguments */
238
                         char *argv[],                /**< In: Actual arguments */
239
                         int32 strict           /**< In: Fail on duplicate or unknown
240
                                                   arguments, or no arguments? */
241
    );
242
243
/**
244
 * Parse an arguments file by deliminating on " \r\t\n" and putting each tokens
245
 * into an argv[] for cmd_ln_parse().
246
 *
247
 * @return A cmd_ln_t containing the results of command line parsing, or NULL on failure.
248
 */
249
SPHINXBASE_EXPORT
250
cmd_ln_t *cmd_ln_parse_file_r(cmd_ln_t *inout_cmdln, /**< In/Out: Previous command-line to update,
251
                                                     or NULL to create a new one. */
252
                              arg_t const *defn,   /**< In: Array of argument name definitions*/
253
                              char const *filename,/**< In: A file that contains all
254
                                                     the arguments */ 
255
                              int32 strict         /**< In: Fail on duplicate or unknown
256
                                                     arguments, or no arguments? */
257
    );
258
259
/**
260
 * Access the generic type union for a command line argument.
261
 */
262
SPHINXBASE_EXPORT
263
anytype_t *cmd_ln_access_r(cmd_ln_t *cmdln, char const *name);
264
265
/**
266
 * Retrieve a string from a command-line object.
267
 *
268
 * The command-line object retains ownership of this string, so you
269
 * should not attempt to free it manually.
270
 *
271
 * @param cmdln Command-line object.
272
 * @param name the command-line flag to retrieve.
273
 * @return the string value associated with <tt>name</tt>, or NULL if
274
 *         <tt>name</tt> does not exist.  You must use
275
 *         cmd_ln_exists_r() to distinguish between cases where a
276
 *         value is legitimately NULL and where the corresponding flag
277
 *         is unknown.
278
 */
279
SPHINXBASE_EXPORT
280
char const *cmd_ln_str_r(cmd_ln_t *cmdln, char const *name);
281
282
/**
283
 * Retrieve an integer from a command-line object.
284
 *
285
 * @param cmdln Command-line object.
286
 * @param name the command-line flag to retrieve.
287
 * @return the integer value associated with <tt>name</tt>, or 0 if
288
 *         <tt>name</tt> does not exist.  You must use
289
 *         cmd_ln_exists_r() to distinguish between cases where a
290
 *         value is legitimately zero and where the corresponding flag
291
 *         is unknown.
292
 */
293
SPHINXBASE_EXPORT
294
long cmd_ln_int_r(cmd_ln_t *cmdln, char const *name);
295
296
/**
297
 * Retrieve a floating-point number from a command-line object.
298
 *
299
 * @param cmdln Command-line object.
300
 * @param name the command-line flag to retrieve.
301
 * @return the float value associated with <tt>name</tt>, or 0.0 if
302
 *         <tt>name</tt> does not exist.  You must use
303
 *         cmd_ln_exists_r() to distinguish between cases where a
304
 *         value is legitimately zero and where the corresponding flag
305
 *         is unknown.
306
 */
307
SPHINXBASE_EXPORT
308
double cmd_ln_float_r(cmd_ln_t *cmdln, char const *name);
309
310
/**
311
 * Retrieve a boolean value from a command-line object.
312
 */
313
#define cmd_ln_boolean_r(c,n) (cmd_ln_int_r(c,n) != 0)
314
315
/**
316
 * Set a string in a command-line object.
317
 *
318
 * @param cmdln Command-line object.
319
 * @param name The command-line flag to set.
320
 * @param str String value to set.  The command-line object does not
321
 *            retain ownership of this pointer.
322
 */
323
SPHINXBASE_EXPORT
324
void cmd_ln_set_str_r(cmd_ln_t *cmdln, char const *name, char const *str);
325
326
/**
327
 * Set an integer in a command-line object.
328
 *
329
 * @param cmdln Command-line object.
330
 * @param name The command-line flag to set.
331
 * @param iv Integer value to set.
332
 */
333
SPHINXBASE_EXPORT
334
void cmd_ln_set_int_r(cmd_ln_t *cmdln, char const *name, long iv);
335
336
/**
337
 * Set a floating-point number in a command-line object.
338
 *
339
 * @param cmdln Command-line object.
340
 * @param name The command-line flag to set.
341
 * @param fv Integer value to set.
342
 */
343
SPHINXBASE_EXPORT
344
void cmd_ln_set_float_r(cmd_ln_t *cmdln, char const *name, double fv);
345
346
/**
347
 * Set a boolean value in a command-line object.
348
 */
349
#define cmd_ln_set_boolean_r(c,n,b) (cmd_ln_set_int_r(c,n,(b)!=0))
350
351
/*
352
 * Compatibility macros
353
 */
354
#define cmd_ln_int32_r(c,n)        cmd_ln_int_r(c,n)
355
#define cmd_ln_float32_r(c,n)        (float32)cmd_ln_float_r(c,n)
356
#define cmd_ln_float64_r(c,n)        (float64)cmd_ln_float_r(c,n)
357
#define cmd_ln_set_int32_r(c,n,i)   cmd_ln_set_int_r(c,n,i)
358
#define cmd_ln_set_float32_r(c,n,f) cmd_ln_set_float_r(c,n,(double)f)
359
#define cmd_ln_set_float64_r(c,n,f) cmd_ln_set_float_r(c,n,(double)f)
360
361
/**
362
 * Re-entrant version of cmd_ln_exists().
363
 *
364
 * @return True if the command line argument exists (i.e. it
365
 * was one of the arguments defined in the call to cmd_ln_parse_r().
366
 */
367
SPHINXBASE_EXPORT
368
int cmd_ln_exists_r(cmd_ln_t *cmdln, char const *name);
369
370
/**
371
 * Print a help message listing the valid argument names, and the associated
372
 * attributes as given in defn.
373
 */
374
SPHINXBASE_EXPORT
375
void cmd_ln_print_help_r (cmd_ln_t *cmdln,
376
                          FILE *fp,           /**< In: File to which to print */
377
                          const arg_t *defn /**< In: Array of argument name definitions */
378
        );
379
380
/**
381
 * Non-reentrant version of cmd_ln_parse().
382
 *
383
 * @deprecated This is deprecated in favor of the re-entrant API
384
 * function cmd_ln_parse_r().
385
 * @return 0 if successful, <0 if error.
386
 */
387
SPHINXBASE_EXPORT
388
int32 cmd_ln_parse(const arg_t *defn,  /**< In: Array of argument name definitions */
389
                   int32 argc,               /**< In: Number of actual arguments */
390
                   char *argv[],       /**< In: Actual arguments */
391
                   int32 strict        /**< In: Fail on duplicate or unknown
392
                                          arguments, or no arguments? */
393
        );
394
395
/**
396
 * Parse an arguments file by deliminating on " \r\t\n" and putting each tokens
397
 * into an argv[] for cmd_ln_parse().
398
 *
399
 * @deprecated This is deprecated in favor of the re-entrant API
400
 * function cmd_ln_parse_file_r().
401
 *
402
 * @return 0 if successful, <0 on error.
403
 */
404
SPHINXBASE_EXPORT
405
int32 cmd_ln_parse_file(const arg_t *defn,   /**< In: Array of argument name definitions*/
406
                        char const *filename,/**< In: A file that contains all the arguments */ 
407
                        int32 strict         /**< In: Fail on duplicate or unknown
408
                                                arguments, or no arguments? */
409
        );
410
411
/**
412
 * Old application initialization routine for Sphinx3 code.
413
 *
414
 * @deprecated This is deprecated in favor of the re-entrant API.
415
 */
416
SPHINXBASE_EXPORT
417
void cmd_ln_appl_enter(int argc,   /**< In: Number of actual arguments */
418
                       char *argv[], /**< In: Number of actual arguments */
419
                       char const* default_argfn, /**< In: default argument file name*/
420
                       const arg_t *defn /**< Command-line argument definition */
421
        );
422
423
424
/**
425
 * Finalization routine corresponding to cmd_ln_appl_enter().
426
 *
427
 * @deprecated This is deprecated in favor of the re-entrant API.
428
 */
429
430
SPHINXBASE_EXPORT
431
void cmd_ln_appl_exit(void);
432
433
/**
434
 * Retrieve the global cmd_ln_t object used by non-re-entrant functions.
435
 *
436
 * @deprecated This is deprecated in favor of the re-entrant API.
437
 * @return global cmd_ln_t object.
438
 */
439
SPHINXBASE_EXPORT
440
cmd_ln_t *cmd_ln_get(void);
441
442
/**
443
 * Test the existence of a command-line argument in the global set of
444
 * definitions.
445
 *
446
 * @deprecated This is deprecated in favor of the re-entrant API
447
 * function cmd_ln_exists_r().
448
 *
449
 * @return True if the command line argument exists (i.e. it
450
 * was one of the arguments defined in the call to cmd_ln_parse().
451
 */
452
#define cmd_ln_exists(name)        cmd_ln_exists_r(cmd_ln_get(), name)
453
454
/**
455
 * Return a pointer to the previously parsed value for the given argument name.
456
 *
457
 * @deprecated This is deprecated in favor of the re-entrant API
458
 * function cmd_ln_access_r().
459
 */
460
#define cmd_ln_access(name)        cmd_ln_access_r(cmd_ln_get(), name)
461
462
/**
463
 * Retrieve a string from the global command line.
464
 *
465
 * @deprecated This is deprecated in favor of the re-entrant API
466
 * function cmd_ln_str_r().
467
 */
468
#define cmd_ln_str(name)        cmd_ln_str_r(cmd_ln_get(), name)
469
/**
470
 * Retrieve a 32-bit integer from the global command line.
471
 *
472
 * @deprecated This is deprecated in favor of the re-entrant API
473
 * function cmd_ln_int_r().
474
 */
475
#define cmd_ln_int32(name)        (int32)cmd_ln_int_r(cmd_ln_get(), name)
476
/**
477
 * Retrieve a 32-bit float from the global command line.
478
 *
479
 * @deprecated This is deprecated in favor of the re-entrant API
480
 * function cmd_ln_float_r().
481
 */
482
#define cmd_ln_float32(name)        (float32)cmd_ln_float_r(cmd_ln_get(), name)
483
/**
484
 * Retrieve a 64-bit float from the global command line.
485
 *
486
 * @deprecated This is deprecated in favor of the re-entrant API
487
 * function cmd_ln_float_r().
488
 */
489
#define cmd_ln_float64(name)        (float64)cmd_ln_float_r(cmd_ln_get(), name)
490
/**
491
 * Retrieve a boolean from the global command line.
492
 *
493
 * @deprecated This is deprecated in favor of the re-entrant API
494
 * function cmd_ln_boolean_r().
495
 */
496
#define cmd_ln_boolean(name)        cmd_ln_boolean_r(cmd_ln_get(), name)
497
498
/**
499
 * Set a string in the global command line.
500
 *
501
 * @deprecated This is deprecated in favor of the re-entrant API
502
 * function cmd_ln_set_str_r().
503
 */
504
#define cmd_ln_set_str(n,s)     cmd_ln_set_str_r(cmd_ln_get(),n,s)
505
/**
506
 * Set a 32-bit integer value in the global command line.
507
 *
508
 * @deprecated This is deprecated in favor of the re-entrant API
509
 * function cmd_ln_set_int_r().
510
 */
511
#define cmd_ln_set_int32(n,i)   cmd_ln_set_int_r(cmd_ln_get(),n,i)
512
/**
513
 * Set a 32-bit float in the global command line.
514
 *
515
 * @deprecated This is deprecated in favor of the re-entrant API
516
 * function cmd_ln_set_float_r().
517
 */
518
#define cmd_ln_set_float32(n,f) cmd_ln_set_float_r(cmd_ln_get(),n,f)
519
/**
520
 * Set a 64-bit float in the global command line.
521
 *
522
 * @deprecated This is deprecated in favor of the re-entrant API
523
 * function cmd_ln_set_float_r().
524
 */
525
#define cmd_ln_set_float64(n,f) cmd_ln_set_float_r(cmd_ln_get(),n,f)
526
/**
527
 * Set a boolean value in the global command line.
528
 *
529
 * @deprecated This is deprecated in favor of the re-entrant API
530
 * function cmd_ln_set_boolean_r().
531
 */
532
#define cmd_ln_set_boolean(n,b) cmd_ln_set_boolean_r(cmd_ln_get(),n,b)
533
534
/**
535
 * Print a help message listing the valid argument names, and the associated
536
 * attributes as given in defn.
537
 *
538
 * @deprecated This is deprecated in favor of the re-entrant API
539
 * function cmd_ln_print_help_r().
540
 */
541
#define cmd_ln_print_help(f,d) cmd_ln_print_help_r(cmd_ln_get(),f,d)
542
543
/**
544
 * Free the global command line, if any exists.
545
 * @deprecated Use the re-entrant API instead.
546
 */
547
SPHINXBASE_EXPORT
548
void cmd_ln_free (void);
549
550
551
#ifdef __cplusplus
552
}
553
#endif
554
555
#endif
556
557