SphinxBase  5prealpha
cmd_ln.h
Go to the documentation of this file.
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 (rkm@cs.cmu.edu) at Carnegie Mellon University
50  * Added required arguments types.
51  *
52  * 07-Dec-96 M K Ravishankar (rkm@cs.cmu.edu) 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/sphinxbase_export.h>
66 #include <sphinxbase/prim_type.h>
67 
76 #ifdef __cplusplus
77 extern "C" {
78 #endif
79 #if 0
80 /* Fool Emacs. */
81 }
82 #endif
83 
88 typedef struct arg_s {
89  char const *name;
90  int type;
91  char const *deflt;
92  char const *doc;
93 } arg_t;
94 
98 /* @{ */
102 #define ARG_REQUIRED (1<<0)
103 
106 #define ARG_INTEGER (1<<1)
107 
110 #define ARG_FLOATING (1<<2)
111 
114 #define ARG_STRING (1<<3)
115 
118 #define ARG_BOOLEAN (1<<4)
119 
122 #define ARG_STRING_LIST (1<<5)
123 
127 #define REQARG_INTEGER (ARG_INTEGER | ARG_REQUIRED)
128 
131 #define REQARG_FLOATING (ARG_FLOATING | ARG_REQUIRED)
132 
135 #define REQARG_STRING (ARG_STRING | ARG_REQUIRED)
136 
139 #define REQARG_BOOLEAN (ARG_BOOLEAN | ARG_REQUIRED)
140 
144 #define ARG_INT32 ARG_INTEGER
145 
148 #define ARG_FLOAT32 ARG_FLOATING
149 
152 #define ARG_FLOAT64 ARG_FLOATING
153 
156 #define REQARG_INT32 (ARG_INT32 | ARG_REQUIRED)
157 
160 #define REQARG_FLOAT32 (ARG_FLOAT32 | ARG_REQUIRED)
161 
164 #define REQARG_FLOAT64 (ARG_FLOAT64 | ARG_REQUIRED)
165 /* @} */
166 
167 
172 #define ARG_STRINGIFY(s) ARG_STRINGIFY1(s)
173 #define ARG_STRINGIFY1(s) #s
174 
179 typedef struct cmd_ln_s cmd_ln_t;
180 
199 SPHINXBASE_EXPORT
200 cmd_ln_t *cmd_ln_init(cmd_ln_t *inout_cmdln, arg_t const *defn, int32 strict, ...);
201 
207 SPHINXBASE_EXPORT
209 
215 SPHINXBASE_EXPORT
216 int cmd_ln_free_r(cmd_ln_t *cmdln);
217 
237 SPHINXBASE_EXPORT
238 cmd_ln_t *cmd_ln_parse_r(cmd_ln_t *inout_cmdln,
240  arg_t const *defn,
241  int32 argc,
242  char *argv[],
243  int32 strict
245  );
246 
253 SPHINXBASE_EXPORT
254 cmd_ln_t *cmd_ln_parse_file_r(cmd_ln_t *inout_cmdln,
256  arg_t const *defn,
257  char const *filename,
259  int32 strict
261  );
262 
266 SPHINXBASE_EXPORT
267 anytype_t *cmd_ln_access_r(cmd_ln_t *cmdln, char const *name);
268 
283 SPHINXBASE_EXPORT
284 char const *cmd_ln_str_r(cmd_ln_t *cmdln, char const *name);
285 
300 SPHINXBASE_EXPORT
301 char const **cmd_ln_str_list_r(cmd_ln_t *cmdln, char const *name);
302 
314 SPHINXBASE_EXPORT
315 long cmd_ln_int_r(cmd_ln_t *cmdln, char const *name);
316 
328 SPHINXBASE_EXPORT
329 double cmd_ln_float_r(cmd_ln_t *cmdln, char const *name);
330 
334 #define cmd_ln_boolean_r(c,n) (cmd_ln_int_r(c,n) != 0)
335 
344 SPHINXBASE_EXPORT
345 void cmd_ln_set_str_r(cmd_ln_t *cmdln, char const *name, char const *str);
346 
357 SPHINXBASE_EXPORT
358 void cmd_ln_set_str_extra_r(cmd_ln_t *cmdln, char const *name, char const *str);
359 
367 SPHINXBASE_EXPORT
368 void cmd_ln_set_int_r(cmd_ln_t *cmdln, char const *name, long iv);
369 
377 SPHINXBASE_EXPORT
378 void cmd_ln_set_float_r(cmd_ln_t *cmdln, char const *name, double fv);
379 
383 #define cmd_ln_set_boolean_r(c,n,b) (cmd_ln_set_int_r(c,n,(b)!=0))
384 
385 /*
386  * Compatibility macros
387  */
388 #define cmd_ln_int32_r(c,n) (int32)cmd_ln_int_r(c,n)
389 #define cmd_ln_float32_r(c,n) (float32)cmd_ln_float_r(c,n)
390 #define cmd_ln_float64_r(c,n) (float64)cmd_ln_float_r(c,n)
391 #define cmd_ln_set_int32_r(c,n,i) cmd_ln_set_int_r(c,n,i)
392 #define cmd_ln_set_float32_r(c,n,f) cmd_ln_set_float_r(c,n,(double)f)
393 #define cmd_ln_set_float64_r(c,n,f) cmd_ln_set_float_r(c,n,(double)f)
394 
401 SPHINXBASE_EXPORT
402 int cmd_ln_exists_r(cmd_ln_t *cmdln, char const *name);
403 
412 SPHINXBASE_EXPORT
413 void cmd_ln_print_help_r (cmd_ln_t *cmdln, FILE *fp, const arg_t *defn);
414 
422 SPHINXBASE_EXPORT
423 void cmd_ln_print_values_r (cmd_ln_t *cmdln, FILE *fp, const arg_t *defn);
424 
432 SPHINXBASE_EXPORT
433 int32 cmd_ln_parse(const arg_t *defn,
434  int32 argc,
435  char *argv[],
436  int32 strict
438  );
439 
449 SPHINXBASE_EXPORT
450 int32 cmd_ln_parse_file(const arg_t *defn,
451  char const *filename,
452  int32 strict
454  );
455 
461 SPHINXBASE_EXPORT
462 void cmd_ln_appl_enter(int argc,
463  char *argv[],
464  char const* default_argfn,
465  const arg_t *defn
466  );
467 
468 
475 SPHINXBASE_EXPORT
476 void cmd_ln_appl_exit(void);
477 
484 SPHINXBASE_EXPORT
485 cmd_ln_t *cmd_ln_get(void);
486 
497 #define cmd_ln_exists(name) cmd_ln_exists_r(cmd_ln_get(), name)
498 
505 #define cmd_ln_access(name) cmd_ln_access_r(cmd_ln_get(), name)
506 
513 #define cmd_ln_str(name) cmd_ln_str_r(cmd_ln_get(), name)
514 
521 #define cmd_ln_str_list(name) cmd_ln_str_list_r(cmd_ln_get(), name)
522 
529 #define cmd_ln_int32(name) (int32)cmd_ln_int_r(cmd_ln_get(), name)
530 
536 #define cmd_ln_float32(name) (float32)cmd_ln_float_r(cmd_ln_get(), name)
537 
543 #define cmd_ln_float64(name) (float64)cmd_ln_float_r(cmd_ln_get(), name)
544 
550 #define cmd_ln_boolean(name) cmd_ln_boolean_r(cmd_ln_get(), name)
551 
558 #define cmd_ln_set_str(n,s) cmd_ln_set_str_r(cmd_ln_get(),n,s)
559 
565 #define cmd_ln_set_int32(n,i) cmd_ln_set_int_r(cmd_ln_get(),n,i)
566 
572 #define cmd_ln_set_float32(n,f) cmd_ln_set_float_r(cmd_ln_get(),n,f)
573 
579 #define cmd_ln_set_float64(n,f) cmd_ln_set_float_r(cmd_ln_get(),n,f)
580 
586 #define cmd_ln_set_boolean(n,b) cmd_ln_set_boolean_r(cmd_ln_get(),n,b)
587 
595 #define cmd_ln_print_help(f,d) cmd_ln_print_help_r(cmd_ln_get(),f,d)
596 
601 SPHINXBASE_EXPORT
602 void cmd_ln_free (void);
603 
604 
605 #ifdef __cplusplus
606 }
607 #endif
608 
609 #endif
char const * name
Name of the command line switch.
Definition: cmd_ln.h:89
SPHINXBASE_EXPORT cmd_ln_t * cmd_ln_retain(cmd_ln_t *cmdln)
Retain ownership of a command-line argument set.
Definition: cmd_ln.c:1039
SPHINXBASE_EXPORT double cmd_ln_float_r(cmd_ln_t *cmdln, char const *name)
Retrieve a floating-point number from a command-line object.
Definition: cmd_ln.c:979
SPHINXBASE_EXPORT long cmd_ln_int_r(cmd_ln_t *cmdln, char const *name)
Retrieve an integer from a command-line object.
Definition: cmd_ln.c:969
SPHINXBASE_EXPORT anytype_t * cmd_ln_access_r(cmd_ln_t *cmdln, char const *name)
Access the generic type union for a command line argument.
Definition: cmd_ln.c:938
char const * doc
Documentation/description string.
Definition: cmd_ln.h:92
SPHINXBASE_EXPORT void cmd_ln_set_int_r(cmd_ln_t *cmdln, char const *name, long iv)
Set an integer in a command-line object.
Definition: cmd_ln.c:1015
SPHINXBASE_EXPORT int cmd_ln_free_r(cmd_ln_t *cmdln)
Release a command-line argument set and all associated strings.
Definition: cmd_ln.c:1046
int type
Type of the argument in question.
Definition: cmd_ln.h:90
SPHINXBASE_EXPORT int cmd_ln_exists_r(cmd_ln_t *cmdln, char const *name)
Re-entrant version of cmd_ln_exists().
Definition: cmd_ln.c:929
SPHINXBASE_EXPORT cmd_ln_t * cmd_ln_parse_r(cmd_ln_t *inout_cmdln, arg_t const *defn, int32 argc, char *argv[], int32 strict)
Parse a list of strings into argumetns.
Definition: cmd_ln.c:556
Basic type definitions used in Sphinx.
SPHINXBASE_EXPORT char const * cmd_ln_str_r(cmd_ln_t *cmdln, char const *name)
Retrieve a string from a command-line object.
Definition: cmd_ln.c:949
SPHINXBASE_EXPORT char const ** cmd_ln_str_list_r(cmd_ln_t *cmdln, char const *name)
Retrieve an array of strings from a command-line object.
Definition: cmd_ln.c:959
SPHINXBASE_EXPORT cmd_ln_t * cmd_ln_get(void)
Retrieve the global cmd_ln_t object used by non-re-entrant functions.
Definition: cmd_ln.c:493
SPHINXBASE_EXPORT void cmd_ln_print_help_r(cmd_ln_t *cmdln, FILE *fp, const arg_t *defn)
Print a help message listing the valid argument names, and the associated attributes as given in defn...
Definition: cmd_ln.c:911
SPHINXBASE_EXPORT void cmd_ln_print_values_r(cmd_ln_t *cmdln, FILE *fp, const arg_t *defn)
Print current configuration values and defaults.
Definition: cmd_ln.c:920
Definition: cmd_ln.h:88
char const * deflt
Default value (as a character string), or NULL if none.
Definition: cmd_ln.h:91
SPHINXBASE_EXPORT cmd_ln_t * cmd_ln_init(cmd_ln_t *inout_cmdln, arg_t const *defn, int32 strict,...)
Create a cmd_ln_t from NULL-terminated list of arguments.
Definition: cmd_ln.c:709
SPHINXBASE_EXPORT void cmd_ln_appl_exit(void)
Finalization routine corresponding to cmd_ln_appl_enter().
Definition: cmd_ln.c:549
SPHINXBASE_EXPORT cmd_ln_t * cmd_ln_parse_file_r(cmd_ln_t *inout_cmdln, arg_t const *defn, char const *filename, int32 strict)
Parse an arguments file by deliminating on &quot; \r\t\n&quot; and putting each tokens into an argv[] for cmd_l...
Definition: cmd_ln.c:764
Argument definition structure.
Opaque structure used to hold the results of command-line parsing.
SPHINXBASE_EXPORT void cmd_ln_set_float_r(cmd_ln_t *cmdln, char const *name, double fv)
Set a floating-point number in a command-line object.
Definition: cmd_ln.c:1027
Union of basic types.
Definition: prim_type.h:107
SPHINXBASE_EXPORT int32 cmd_ln_parse(const arg_t *defn, int32 argc, char *argv[], int32 strict)
Non-reentrant version of cmd_ln_parse().
Definition: cmd_ln.c:746
SPHINXBASE_EXPORT void cmd_ln_set_str_r(cmd_ln_t *cmdln, char const *name, char const *str)
Set a string in a command-line object.
Definition: cmd_ln.c:989
SPHINXBASE_EXPORT void cmd_ln_free(void)
Free the global command line, if any exists.
Definition: cmd_ln.c:1082
SPHINXBASE_EXPORT void cmd_ln_set_str_extra_r(cmd_ln_t *cmdln, char const *name, char const *str)
Set a string in a command-line object even if it is not present in argument description.
Definition: cmd_ln.c:1002
SPHINXBASE_EXPORT void cmd_ln_appl_enter(int argc, char *argv[], char const *default_argfn, const arg_t *defn)
Old application initialization routine for Sphinx3 code.
Definition: cmd_ln.c:499
SPHINXBASE_EXPORT int32 cmd_ln_parse_file(const arg_t *defn, char const *filename, int32 strict)
Parse an arguments file by deliminating on &quot; \r\t\n&quot; and putting each tokens into an argv[] for cmd_l...
Definition: cmd_ln.c:895