2004-01-09 06:24:54 +03:00
|
|
|
/*
|
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2004-01-21 02:58:23 +03:00
|
|
|
/** @file **/
|
|
|
|
|
2004-01-09 06:24:54 +03:00
|
|
|
#ifndef LAM_CMD_LINE_H
|
|
|
|
#define LAM_CMD_LINE_H
|
|
|
|
|
2004-01-10 20:21:33 +03:00
|
|
|
#include "lam_config.h"
|
2004-01-10 21:19:55 +03:00
|
|
|
#include "lam/constants.h"
|
2004-01-10 20:21:33 +03:00
|
|
|
#include "lam/lfc/list.h"
|
|
|
|
#include "lam/threads/mutex.h"
|
2004-01-10 21:19:55 +03:00
|
|
|
#include "lam/util/argv.h"
|
|
|
|
|
2004-01-21 02:58:23 +03:00
|
|
|
/*
|
|
|
|
* Top-level descriptor
|
|
|
|
*/
|
2004-01-10 20:21:33 +03:00
|
|
|
struct lam_cmd_line_t {
|
|
|
|
lam_mutex_t lcl_mutex;
|
|
|
|
|
2004-01-21 02:58:23 +03:00
|
|
|
/* List of cmd_line_option_t's, below */
|
2004-01-10 20:21:33 +03:00
|
|
|
|
|
|
|
lam_list_t lcl_options;
|
|
|
|
|
2004-01-21 02:58:23 +03:00
|
|
|
/* Duplicate of the argc / argv passed in to lam_cmd_line_parse() */
|
2004-01-10 20:21:33 +03:00
|
|
|
|
|
|
|
int lcl_argc;
|
|
|
|
char **lcl_argv;
|
|
|
|
|
2004-01-21 02:58:23 +03:00
|
|
|
/* Parsed output; list of cmd_line_param_t's, below */
|
2004-01-10 20:21:33 +03:00
|
|
|
|
|
|
|
lam_list_t lcl_params;
|
|
|
|
|
2004-01-21 02:58:23 +03:00
|
|
|
/* List of tail (unprocessed) arguments */
|
2004-01-10 20:21:33 +03:00
|
|
|
|
|
|
|
int lcl_tail_argc;
|
|
|
|
char **lcl_tail_argv;
|
|
|
|
};
|
|
|
|
typedef struct lam_cmd_line_t lam_cmd_line_t;
|
2004-01-09 06:24:54 +03:00
|
|
|
|
2004-01-21 02:58:23 +03:00
|
|
|
|
2004-01-09 06:24:54 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2004-01-10 20:21:33 +03:00
|
|
|
/* Create / destroy */
|
2004-01-09 06:24:54 +03:00
|
|
|
|
2004-01-10 20:21:33 +03:00
|
|
|
lam_cmd_line_t *lam_cmd_line_create(void);
|
2004-01-21 02:58:23 +03:00
|
|
|
void lam_cmd_line_free(lam_cmd_line_t *cmd);
|
2004-01-09 06:24:54 +03:00
|
|
|
|
2004-01-10 20:21:33 +03:00
|
|
|
/* Writes */
|
|
|
|
|
2004-01-21 02:58:23 +03:00
|
|
|
int lam_cmd_line_make_opt(lam_cmd_line_t *cmd, char short_name,
|
|
|
|
const char *long_name, int num_params,
|
|
|
|
const char *desc);
|
2004-01-10 20:21:33 +03:00
|
|
|
|
2004-01-21 02:58:23 +03:00
|
|
|
int lam_cmd_line_parse(lam_cmd_line_t *cmd, bool ignore_unknown,
|
|
|
|
int argc, char **argv);
|
2004-01-10 20:21:33 +03:00
|
|
|
|
|
|
|
/* Reads */
|
|
|
|
|
|
|
|
static inline int lam_cmd_line_get_argc(lam_cmd_line_t *cmd);
|
|
|
|
static inline char *lam_cmd_line_get_argv(lam_cmd_line_t *cmd, int index);
|
|
|
|
|
2004-01-21 02:58:23 +03:00
|
|
|
char *lam_cmd_line_get_usage_msg(lam_cmd_line_t *cmd);
|
2004-01-10 20:21:33 +03:00
|
|
|
bool lam_cmd_line_is_taken(lam_cmd_line_t *cmd, const char *opt);
|
|
|
|
int lam_cmd_line_get_ninsts(lam_cmd_line_t *cmd, const char *opt);
|
2004-01-21 02:58:23 +03:00
|
|
|
char *lam_cmd_line_get_param(lam_cmd_line_t *cmd, const char *opt,
|
|
|
|
int instance_num, int param_num);
|
2004-01-10 20:21:33 +03:00
|
|
|
|
2004-01-10 21:19:55 +03:00
|
|
|
static inline int lam_cmd_line_get_tail(lam_cmd_line_t *cmd, int *tailc,
|
|
|
|
char ***tailv);
|
2004-01-09 06:24:54 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-01-10 20:21:33 +03:00
|
|
|
/**
|
|
|
|
* Return the number of arguments parsed on a LAM command line handle.
|
|
|
|
*
|
|
|
|
* @param cmd A pointer to the LAM command line handle.
|
|
|
|
*
|
2004-01-21 02:58:23 +03:00
|
|
|
* @retval LAM_ERROR If cmd is NULL.
|
|
|
|
* @retval argc Number of arguments previously added to the handle.
|
2004-01-10 20:21:33 +03:00
|
|
|
*
|
|
|
|
* Arguments are added to the handle via the lam_cmd_line_parse()
|
|
|
|
* function.
|
|
|
|
*/
|
|
|
|
static inline int lam_cmd_line_get_argc(lam_cmd_line_t *cmd)
|
|
|
|
{
|
|
|
|
return (NULL != cmd) ? cmd->lcl_argc : LAM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-10 21:19:55 +03:00
|
|
|
/**
|
|
|
|
* Return a string argument parsed on a LAM command line handle.
|
|
|
|
*
|
|
|
|
* @param cmd A pointer to the LAM command line handle.
|
|
|
|
* @param index The nth argument from the command line (0 is argv[0], etc.).
|
|
|
|
*
|
2004-01-21 02:58:23 +03:00
|
|
|
* @retval NULL If cmd is NULL or index is invalid
|
|
|
|
* @retval argument String of original argv[index]
|
2004-01-10 21:19:55 +03:00
|
|
|
*
|
|
|
|
* This function returns a single token from the arguments parsed on
|
|
|
|
* this handle. Arguments are added bia the lam_cmd_line_parse()
|
|
|
|
* function.
|
|
|
|
*
|
|
|
|
* What is returned is a pointer to the actual string that is on the
|
|
|
|
* handle; it should not be modified or freed.
|
|
|
|
*/
|
|
|
|
static inline char *lam_cmd_line_get_argv(lam_cmd_line_t *cmd, int index)
|
|
|
|
{
|
|
|
|
return (NULL == cmd) ? NULL :
|
|
|
|
(index >= cmd->lcl_argc || index < 0) ? NULL : cmd->lcl_argv[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the entire "tail" of unprocessed argv from a LAM command
|
|
|
|
* line handle.
|
|
|
|
*
|
|
|
|
* @param cmd A pointer to the LAM command line handle.
|
|
|
|
* @param tailc Pointer to the output length of the null-terminated
|
|
|
|
* tail argv array.
|
|
|
|
* @param tailv Pointer to the output null-terminated argv of all
|
|
|
|
* unprocessed arguments from the command line.
|
|
|
|
*
|
2004-01-21 02:58:23 +03:00
|
|
|
* @retval LAM_ERROR If cmd is NULL or otherwise invalid.
|
|
|
|
* @retval LAM_SUCCESS Upon success.
|
2004-01-10 21:19:55 +03:00
|
|
|
*
|
|
|
|
* The "tail" is all the arguments on the command line that were not
|
|
|
|
* processed for some reason. Reasons for not processing arguments
|
|
|
|
* include:
|
|
|
|
*
|
|
|
|
* \sa The argument was not recognized
|
|
|
|
* \sa The argument "--" was seen, and therefore all arguments
|
|
|
|
* following it were not processed
|
|
|
|
*
|
|
|
|
* The output tailc parameter will be filled in with the integer
|
|
|
|
* length of the null-terminated tailv array (length including the
|
|
|
|
* final NULL entry). The output tailv parameter will be a copy of
|
|
|
|
* the tail parameters, and must be freed (likely with a call to
|
|
|
|
* lam_argv_free()) by the caller.
|
|
|
|
*/
|
|
|
|
static inline int lam_cmd_line_get_tail(lam_cmd_line_t *cmd, int *tailc,
|
|
|
|
char ***tailv)
|
2004-01-10 20:21:33 +03:00
|
|
|
{
|
2004-01-10 21:19:55 +03:00
|
|
|
if (NULL != cmd) {
|
2004-01-13 05:34:20 +03:00
|
|
|
lam_mutex_lock(&cmd->lcl_mutex);
|
2004-01-10 21:19:55 +03:00
|
|
|
*tailc = cmd->lcl_tail_argc;
|
|
|
|
*tailv = lam_argv_copy(cmd->lcl_tail_argv);
|
2004-01-13 05:34:20 +03:00
|
|
|
lam_mutex_unlock(&cmd->lcl_mutex);
|
2004-01-10 21:19:55 +03:00
|
|
|
return LAM_SUCCESS;
|
|
|
|
} else {
|
|
|
|
return LAM_ERROR;
|
|
|
|
}
|
2004-01-10 20:21:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* LAM_CMD_LINE_H */
|