DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (fddffdeab170)

VCS Links

Line Code
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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* ====================================================================
 * Copyright (c) 1999-2004 Carnegie Mellon University.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * This work was supported in part by funding from the Defense Advanced 
 * Research Projects Agency and the National Science Foundation of the 
 * United States of America, and the CMU Sphinx Speech Consortium.
 *
 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 *
 */
/*
 * cmd_ln.h -- Command line argument parsing.
 *
 * **********************************************
 * CMU ARPA Speech Project
 *
 * Copyright (c) 1999 Carnegie Mellon University.
 * ALL RIGHTS RESERVED.
 * **********************************************
 * 
 * HISTORY
 * 
 * 15-Jul-1997	M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
 * 		Added required arguments types.
 * 
 * 07-Dec-96	M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
 * 		Created, based on Eric's implementation.  Basically, combined several
 *		functions into one, eliminated validation, and simplified the interface.
 */


#ifndef _LIBUTIL_CMD_LN_H_
#define _LIBUTIL_CMD_LN_H_

#include <stdio.h>
#include <stdarg.h>

/* Win32/WinCE DLL gunk */
#include <sphinxbase/sphinxbase_export.h>
#include <sphinxbase/prim_type.h>

/**
 * @file cmd_ln.h
 * @brief Command-line and other configurationparsing and handling.
 *  
 * Configuration parameters, optionally parsed from the command line.
 */
  

#ifdef __cplusplus
extern "C" {
#endif
#if 0
/* Fool Emacs. */
}
#endif

/**
 * @struct arg_t
 * Argument definition structure.
 */
typedef struct arg_s {
	char const *name;   /**< Name of the command line switch */
	int type;           /**< Type of the argument in question */
	char const *deflt;  /**< Default value (as a character string), or NULL if none */
	char const *doc;    /**< Documentation/description string */
} arg_t;

/**
 * @name Values for arg_t::type
 */
/* @{ */
/**
 * Bit indicating a required argument.
 */
#define ARG_REQUIRED (1<<0)
/**
 * Integer argument (optional).
 */
#define ARG_INTEGER  (1<<1)
/**
 * Floating point argument (optional).
 */
#define ARG_FLOATING (1<<2)
/**
 * String argument (optional).
 */
#define ARG_STRING   (1<<3)
/**
 * Boolean (true/false) argument (optional).
 */
#define ARG_BOOLEAN  (1<<4)
/**
 * Boolean (true/false) argument (optional).
 */
#define ARG_STRING_LIST  (1<<5)

/**
 * Required integer argument.
 */
#define REQARG_INTEGER (ARG_INTEGER | ARG_REQUIRED)
/**
 * Required floating point argument.
 */
#define REQARG_FLOATING (ARG_FLOATING | ARG_REQUIRED)
/**
 * Required string argument.
 */
#define REQARG_STRING (ARG_STRING | ARG_REQUIRED)
/**
 * Required boolean argument.
 */
#define REQARG_BOOLEAN (ARG_BOOLEAN | ARG_REQUIRED)

/**
 * @deprecated Use ARG_INTEGER instead.
 */
#define ARG_INT32   ARG_INTEGER
/**
 * @deprecated Use ARG_FLOATING instead.
 */
#define ARG_FLOAT32 ARG_FLOATING
/**
 * @deprecated Use ARG_FLOATING instead.
 */
#define ARG_FLOAT64 ARG_FLOATING
/**
 * @deprecated Use REQARG_INTEGER instead.
 */
#define REQARG_INT32 (ARG_INT32 | ARG_REQUIRED)
/**
 * @deprecated Use REQARG_FLOATING instead.
 */
#define REQARG_FLOAT32 (ARG_FLOAT32 | ARG_REQUIRED)
/**
 * @deprecated Use REQARG_FLOATING instead.
 */
#define REQARG_FLOAT64 (ARG_FLOAT64 | ARG_REQUIRED)
/* @} */


/**
 * Helper macro to stringify enums and other non-string values for
 * default arguments.
 **/
#define ARG_STRINGIFY(s) ARG_STRINGIFY1(s)
#define ARG_STRINGIFY1(s) #s

/**
 * @struct cmd_ln_t
 * Opaque structure used to hold the results of command-line parsing.
 */
typedef struct cmd_ln_s cmd_ln_t;

/**
 * Create a cmd_ln_t from NULL-terminated list of arguments.
 *
 * This function creates a cmd_ln_t from a NULL-terminated list of
 * argument strings.  For example, to create the equivalent of passing
 * "-hmm foodir -dsratio 2 -lm bar.lm" on the command-line:
 *
 *  config = cmd_ln_init(NULL, defs, TRUE, "-hmm", "foodir", "-dsratio", "2",
 *                       "-lm", "bar.lm", NULL);
 *
 * Note that for simplicity, <strong>all</strong> arguments are passed
 * as strings, regardless of the actual underlying type.
 *
 * @param inout_cmdln Previous command-line to update, or NULL to create a new one.
 * @param defn Array of argument name definitions, or NULL to allow any arguments.
 * @param strict Whether to fail on duplicate or unknown arguments.
 * @return A cmd_ln_t* containing the results of command line parsing, or NULL on failure.
 */
SPHINXBASE_EXPORT
cmd_ln_t *cmd_ln_init(cmd_ln_t *inout_cmdln, arg_t const *defn, int32 strict, ...);

/**
 * Retain ownership of a command-line argument set.
 *
 * @return pointer to retained command-line argument set.
 */
SPHINXBASE_EXPORT
cmd_ln_t *cmd_ln_retain(cmd_ln_t *cmdln);

/**
 * Release a command-line argument set and all associated strings.
 *
 * @return new reference count (0 if freed completely)
 */
SPHINXBASE_EXPORT
int cmd_ln_free_r(cmd_ln_t *cmdln);

/**
 * Parse a list of strings into argumetns.
 *
 * Parse the given list of arguments (name-value pairs) according to
 * the given definitions.  Argument values can be retrieved in future
 * using cmd_ln_access().  argv[0] is assumed to be the program name
 * and skipped.  Any unknown argument name causes a fatal error.  The
 * routine also prints the prevailing argument values (to stderr)
 * after parsing.
 *
 * @note It is currently assumed that the strings in argv are
 *       allocated statically, or at least that they will be valid as
 *       long as the cmd_ln_t returned from this function.
 *       Unpredictable behaviour will result if they are freed or
 *       otherwise become invalidated.
 *
 * @return A cmd_ln_t containing the results of command line parsing,
 *         or NULL on failure.
 **/
SPHINXBASE_EXPORT
cmd_ln_t *cmd_ln_parse_r(cmd_ln_t *inout_cmdln, /**< In/Out: Previous command-line to update,
                                                     or NULL to create a new one. */
                         arg_t const *defn,	/**< In: Array of argument name definitions */
                         int32 argc,		/**< In: Number of actual arguments */
                         char *argv[],		/**< In: Actual arguments */
                         int32 strict           /**< In: Fail on duplicate or unknown
                                                   arguments, or no arguments? */
    );

/**
 * Parse an arguments file by deliminating on " \r\t\n" and putting each tokens
 * into an argv[] for cmd_ln_parse().
 *
 * @return A cmd_ln_t containing the results of command line parsing, or NULL on failure.
 */
SPHINXBASE_EXPORT
cmd_ln_t *cmd_ln_parse_file_r(cmd_ln_t *inout_cmdln, /**< In/Out: Previous command-line to update,
                                                     or NULL to create a new one. */
                              arg_t const *defn,   /**< In: Array of argument name definitions*/
                              char const *filename,/**< In: A file that contains all
                                                     the arguments */ 
                              int32 strict         /**< In: Fail on duplicate or unknown
                                                     arguments, or no arguments? */
    );

/**
 * Access the generic type union for a command line argument.
 */
SPHINXBASE_EXPORT
anytype_t *cmd_ln_access_r(cmd_ln_t *cmdln, char const *name);

/**
 * Retrieve a string from a command-line object.
 *
 * The command-line object retains ownership of this string, so you
 * should not attempt to free it manually.
 *
 * @param cmdln Command-line object.
 * @param name the command-line flag to retrieve.
 * @return the string value associated with <tt>name</tt>, or NULL if
 *         <tt>name</tt> does not exist.  You must use
 *         cmd_ln_exists_r() to distinguish between cases where a
 *         value is legitimately NULL and where the corresponding flag
 *         is unknown.
 */
SPHINXBASE_EXPORT
char const *cmd_ln_str_r(cmd_ln_t *cmdln, char const *name);

/**
 * Retrieve an array of strings from a command-line object.
 *
 * The command-line object retains ownership of this array, so you
 * should not attempt to free it manually.
 *
 * @param cmdln Command-line object.
 * @param name the command-line flag to retrieve.
 * @return the array of strings associated with <tt>name</tt>, or NULL if
 *         <tt>name</tt> does not exist.  You must use
 *         cmd_ln_exists_r() to distinguish between cases where a
 *         value is legitimately NULL and where the corresponding flag
 *         is unknown.
 */
SPHINXBASE_EXPORT
char const **cmd_ln_str_list_r(cmd_ln_t *cmdln, char const *name);

/**
 * Retrieve an integer from a command-line object.
 *
 * @param cmdln Command-line object.
 * @param name the command-line flag to retrieve.
 * @return the integer value associated with <tt>name</tt>, or 0 if
 *         <tt>name</tt> does not exist.  You must use
 *         cmd_ln_exists_r() to distinguish between cases where a
 *         value is legitimately zero and where the corresponding flag
 *         is unknown.
 */
SPHINXBASE_EXPORT
long cmd_ln_int_r(cmd_ln_t *cmdln, char const *name);

/**
 * Retrieve a floating-point number from a command-line object.
 *
 * @param cmdln Command-line object.
 * @param name the command-line flag to retrieve.
 * @return the float value associated with <tt>name</tt>, or 0.0 if
 *         <tt>name</tt> does not exist.  You must use
 *         cmd_ln_exists_r() to distinguish between cases where a
 *         value is legitimately zero and where the corresponding flag
 *         is unknown.
 */
SPHINXBASE_EXPORT
double cmd_ln_float_r(cmd_ln_t *cmdln, char const *name);

/**
 * Retrieve a boolean value from a command-line object.
 */
#define cmd_ln_boolean_r(c,n) (cmd_ln_int_r(c,n) != 0)

/**
 * Set a string in a command-line object.
 *
 * @param cmdln Command-line object.
 * @param name The command-line flag to set.
 * @param str String value to set.  The command-line object does not
 *            retain ownership of this pointer.
 */
SPHINXBASE_EXPORT
void cmd_ln_set_str_r(cmd_ln_t *cmdln, char const *name, char const *str);

/**
 * Set an integer in a command-line object.
 *
 * @param cmdln Command-line object.
 * @param name The command-line flag to set.
 * @param iv Integer value to set.
 */
SPHINXBASE_EXPORT
void cmd_ln_set_int_r(cmd_ln_t *cmdln, char const *name, long iv);

/**
 * Set a floating-point number in a command-line object.
 *
 * @param cmdln Command-line object.
 * @param name The command-line flag to set.
 * @param fv Integer value to set.
 */
SPHINXBASE_EXPORT
void cmd_ln_set_float_r(cmd_ln_t *cmdln, char const *name, double fv);

/**
 * Set a boolean value in a command-line object.
 */
#define cmd_ln_set_boolean_r(c,n,b) (cmd_ln_set_int_r(c,n,(b)!=0))

/*
 * Compatibility macros
 */
#define cmd_ln_int32_r(c,n)	(int32)cmd_ln_int_r(c,n)
#define cmd_ln_float32_r(c,n)	(float32)cmd_ln_float_r(c,n)
#define cmd_ln_float64_r(c,n)	(float64)cmd_ln_float_r(c,n)
#define cmd_ln_set_int32_r(c,n,i)   cmd_ln_set_int_r(c,n,i)
#define cmd_ln_set_float32_r(c,n,f) cmd_ln_set_float_r(c,n,(double)f)
#define cmd_ln_set_float64_r(c,n,f) cmd_ln_set_float_r(c,n,(double)f)

/**
 * Re-entrant version of cmd_ln_exists().
 *
 * @return True if the command line argument exists (i.e. it
 * was one of the arguments defined in the call to cmd_ln_parse_r().
 */
SPHINXBASE_EXPORT
int cmd_ln_exists_r(cmd_ln_t *cmdln, char const *name);

/**
 * Print a help message listing the valid argument names, and the associated
 * attributes as given in defn.
 *
 * @param fp   output stream
 * @param defn Array of argument name definitions.
 */
SPHINXBASE_EXPORT
void cmd_ln_print_help_r (cmd_ln_t *cmdln, FILE *fp, const arg_t *defn);

/**
 * Non-reentrant version of cmd_ln_parse().
 *
 * @deprecated This is deprecated in favor of the re-entrant API
 * function cmd_ln_parse_r().
 * @return 0 if successful, <0 if error.
 */
SPHINXBASE_EXPORT
int32 cmd_ln_parse(const arg_t *defn,  /**< In: Array of argument name definitions */
                   int32 argc,	       /**< In: Number of actual arguments */
                   char *argv[],       /**< In: Actual arguments */
                   int32 strict        /**< In: Fail on duplicate or unknown
                                          arguments, or no arguments? */
	);

/**
 * Parse an arguments file by deliminating on " \r\t\n" and putting each tokens
 * into an argv[] for cmd_ln_parse().
 *
 * @deprecated This is deprecated in favor of the re-entrant API
 * function cmd_ln_parse_file_r().
 *
 * @return 0 if successful, <0 on error.
 */
SPHINXBASE_EXPORT
int32 cmd_ln_parse_file(const arg_t *defn,   /**< In: Array of argument name definitions*/
			char const *filename,/**< In: A file that contains all the arguments */ 
                        int32 strict         /**< In: Fail on duplicate or unknown
                                                arguments, or no arguments? */
	);

/**
 * Old application initialization routine for Sphinx3 code.
 *
 * @deprecated This is deprecated in favor of the re-entrant API.
 */
SPHINXBASE_EXPORT
void cmd_ln_appl_enter(int argc,   /**< In: Number of actual arguments */
		       char *argv[], /**< In: Number of actual arguments */
		       char const* default_argfn, /**< In: default argument file name*/
		       const arg_t *defn /**< Command-line argument definition */
	);


/**
 * Finalization routine corresponding to cmd_ln_appl_enter().
 *
 * @deprecated This is deprecated in favor of the re-entrant API.
 */

SPHINXBASE_EXPORT
void cmd_ln_appl_exit(void);

/**
 * Retrieve the global cmd_ln_t object used by non-re-entrant functions.
 *
 * @deprecated This is deprecated in favor of the re-entrant API.
 * @return global cmd_ln_t object.
 */
SPHINXBASE_EXPORT
cmd_ln_t *cmd_ln_get(void);

/**
 * Test the existence of a command-line argument in the global set of
 * definitions.
 *
 * @deprecated This is deprecated in favor of the re-entrant API
 * function cmd_ln_exists_r().
 *
 * @return True if the command line argument exists (i.e. it
 * was one of the arguments defined in the call to cmd_ln_parse().
 */
#define cmd_ln_exists(name)	cmd_ln_exists_r(cmd_ln_get(), name)

/**
 * Return a pointer to the previously parsed value for the given argument name.
 *
 * @deprecated This is deprecated in favor of the re-entrant API
 * function cmd_ln_access_r().
 */
#define cmd_ln_access(name)	cmd_ln_access_r(cmd_ln_get(), name)

/**
 * Retrieve a string from the global command line.
 *
 * @deprecated This is deprecated in favor of the re-entrant API
 * function cmd_ln_str_r().
 */
#define cmd_ln_str(name)	cmd_ln_str_r(cmd_ln_get(), name)

/**
 * Retrieve an array of strings in the global command line.
 *
 * @deprecated This is deprecated in favor of the re-entrant API
 * function cmd_ln_str_list_r().
 */
#define cmd_ln_str_list(name)	cmd_ln_str_list_r(cmd_ln_get(), name)

/**
 * Retrieve a 32-bit integer from the global command line.
 *
 * @deprecated This is deprecated in favor of the re-entrant API
 * function cmd_ln_int_r().
 */
#define cmd_ln_int32(name)	(int32)cmd_ln_int_r(cmd_ln_get(), name)
/**
 * Retrieve a 32-bit float from the global command line.
 *
 * @deprecated This is deprecated in favor of the re-entrant API
 * function cmd_ln_float_r().
 */
#define cmd_ln_float32(name)	(float32)cmd_ln_float_r(cmd_ln_get(), name)
/**
 * Retrieve a 64-bit float from the global command line.
 *
 * @deprecated This is deprecated in favor of the re-entrant API
 * function cmd_ln_float_r().
 */
#define cmd_ln_float64(name)	(float64)cmd_ln_float_r(cmd_ln_get(), name)
/**
 * Retrieve a boolean from the global command line.
 *
 * @deprecated This is deprecated in favor of the re-entrant API
 * function cmd_ln_boolean_r().
 */
#define cmd_ln_boolean(name)	cmd_ln_boolean_r(cmd_ln_get(), name)

/**
 * Set a string in the global command line.
 *
 * @deprecated This is deprecated in favor of the re-entrant API
 * function cmd_ln_set_str_r().
 */
#define cmd_ln_set_str(n,s)     cmd_ln_set_str_r(cmd_ln_get(),n,s)
/**
 * Set a 32-bit integer value in the global command line.
 *
 * @deprecated This is deprecated in favor of the re-entrant API
 * function cmd_ln_set_int_r().
 */
#define cmd_ln_set_int32(n,i)   cmd_ln_set_int_r(cmd_ln_get(),n,i)
/**
 * Set a 32-bit float in the global command line.
 *
 * @deprecated This is deprecated in favor of the re-entrant API
 * function cmd_ln_set_float_r().
 */
#define cmd_ln_set_float32(n,f) cmd_ln_set_float_r(cmd_ln_get(),n,f)
/**
 * Set a 64-bit float in the global command line.
 *
 * @deprecated This is deprecated in favor of the re-entrant API
 * function cmd_ln_set_float_r().
 */
#define cmd_ln_set_float64(n,f) cmd_ln_set_float_r(cmd_ln_get(),n,f)
/**
 * Set a boolean value in the global command line.
 *
 * @deprecated This is deprecated in favor of the re-entrant API
 * function cmd_ln_set_boolean_r().
 */
#define cmd_ln_set_boolean(n,b) cmd_ln_set_boolean_r(cmd_ln_get(),n,b)

/**
 * Print a help message listing the valid argument names, and the associated
 * attributes as given in defn.
 *
 * @deprecated This is deprecated in favor of the re-entrant API
 * function cmd_ln_print_help_r().
 */
#define cmd_ln_print_help(f,d) cmd_ln_print_help_r(cmd_ln_get(),f,d)

/**
 * Free the global command line, if any exists.
 * @deprecated Use the re-entrant API instead.
 */
SPHINXBASE_EXPORT
void cmd_ln_free (void);


#ifdef __cplusplus
}
#endif

#endif