librnr  1.14.5
RoadNarrows Robotics Common Library 1
opts.c File Reference

Standard command-line options built-in options parsing and validation definitions. More...

#include <stdio.h>
#include <errno.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdarg.h>
#include <libgen.h>
#include <limits.h>
#include <string.h>
#include "rnr/rnrconfig.h"
#include "rnr/pkg.h"
#include "rnr/new.h"
#include "rnr/assoc.h"
#include "rnr/log.h"
#include "rnr/opts.h"

Go to the source code of this file.

Classes

struct  OptsCtl_T
 

Macros

#define OPTS_LONGOPT_LOG   "log"
 log long option name
 
#define OPTS_LONGOPT_LOGFILE   "logfile"
 log file long option name
 
#define OPTS_LONGOPT_LOGNOCOLOR   "log-no-color"
 log with no color option
 
#define OPTS_LONGOPT_LOGNOTIME   "log-no-timestamp"
 log no timestamps option
 
#define OPTS_RESET_ARGCV(optind, argc, argv)   { argc -= optind; argv += optind; }
 Reset argc,argv to start of non-option command arguments. More...
 
#define MAX_FMT_BUF   ((size_t)1024)
 Format buffer size.
 
#define HELP_OPT_OVER   30
 opt help desc 1st line indent More...
 
#define HELP_OPT_OVER_2   (HELP_OPT_OVER+2)
 opt help desc 2nd line+ indent
 
#define HELP_MAX_COL   79
 max help column
 

Functions

int HelpIndent (int over)
 Indents (spaces) over, returning number of added indentation. More...
 
int HelpPutNL (int over)
 Puts help newline, returning new output column position. More...
 
static int HelpPutBuf (int col, int colstart, char buf[], size_t *pbuflen)
 Puts help buffer, returning new output column position. More...
 
static int HelpPutStr (int col, int colstart, const char *s)
 Puts help string, returning new output column position. More...
 
static int HelpPutC (int col, int colstart, int c)
 Puts help character, returning new output column position. More...
 
static int HelpGetC (int col, int colstart, char buf[], size_t bufsiz, size_t *pbuflen, int c)
 Gets help character into local buffer, returning new/same output column position. More...
 
static int OptsPrintf (int col, int colstart, const char *sFmtDesc, const char *sPgmName, OptsInfo_T *pOptsInfo)
 Prints formatted description. More...
 
static void OptsPrintOpt (const char *argv0, OptsInfo_T *pOptsInfo)
 Prints option syntax and description in standard 'usage' format. More...
 
static void OptsHelp (const char *argv0, OptsInfo_T *pOptsInfo, OptsPgmInfo_T *pPgmInfo, bool_t bHasLogging)
 Prints command-line help. More...
 
static void OptsVersion (const char *argv0, const PkgInfo_T *pPkgInfo)
 Prints command-line program version string(s). More...
 
static void OptsLogSetLevel (const char *argv0, const char *sOptName, char *optarg)
 Set program logging level. More...
 
static void OptsLogSetFile (const char *argv0, const char *sOptName, char *optarg)
 Set program logging output file. More...
 
static void OptsLogDisableColor (const char *argv0, const char *sOptName, char *optarg)
 Disable program logging in color. More...
 
static void OptsLogDisableTimestamp (const char *argv0, const char *sOptName, char *optarg)
 Disable program logging with timestamps. More...
 
static OptsCtl_TOptsNew (OptsInfo_T *pOptsInfo, bool_t bHasLogging)
 Allocate and build short and long options getopt_long() parameters from the provided option info. More...
 
static void OptsDelete (OptsCtl_T *pOptsCtl)
 Delete an allocated options control structure. More...
 
char ** OptsGet (const char *argv0, const PkgInfo_T *pPkgInfo, OptsPgmInfo_T *pPgmInfo, OptsInfo_T *pOptsInfo, bool_t bHasLogging, int *pargc, char *argv[])
 Gets, validates, and sets all command line options. More...
 
int OptsCvtArgStr (const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
 Convert options string argument to string. More...
 
int OptsCvtArgBool (const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
 Convert options boolean argument to bool_t. More...
 
int OptsCvtArgInt (const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
 Convert options integer argument to integer. More...
 
int OptsCvtArgFloat (const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
 Convert options float argument to double. More...
 
int OptsCvtArgLogLevel (const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
 Convert options string argument to log threshold level. More...
 
char * OptsFmtStr (char *buf, size_t buflen, void *pOptVal)
 String option value string formatter. More...
 
char * OptsFmtInt (char *buf, size_t buflen, void *pOptVal)
 Integer option value string formatter. More...
 
char * OptsFmtFloat (char *buf, size_t buflen, void *pOptVal)
 Float option value string formatter. More...
 
char * OptsFmtBool (char *buf, size_t buflen, void *pOptVal)
 Boolean option value string formatter. More...
 
char * OptsFmtChar (char *buf, size_t buflen, void *pOptVal)
 Character option value string formatter. More...
 
char * OptsFmtLogLevel (char *buf, size_t buflen, void *pOptVal)
 Log Level option value string formatter. More...
 
void OptsInvalid (const char *argv0, const char *sFmt,...)
 Invalid option or option argument print and exit. More...
 

Variables

static Nvp_T OptsLogLevelTbl []
 
static int OptsLogLevel = LOG_LEVEL_DFT
 working log level value More...
 
static char * OptsLogFile = LOG_FILENAME_DFT
 working log file value
 
static bool_t OptsLogNoColor = false
 working log no color value
 
static bool_t OptsLogNoTimestamp = false
 working log no timestamp value
 
static OptsInfo_T OptsBuiltIn []
 Built-In Options.
 

Detailed Description

Standard command-line options built-in options parsing and validation definitions.

LastChangedDate
2014-12-06 13:48:30 -0700 (Sat, 06 Dec 2014)
Rev
3823
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Definition in file opts.c.

Macro Definition Documentation

#define HELP_OPT_OVER   30

opt help desc 1st line indent

PrettyPrint formats

Definition at line 224 of file opts.c.

Referenced by OptsPrintOpt().

#define OPTS_RESET_ARGCV (   optind,
  argc,
  argv 
)    { argc -= optind; argv += optind; }

Reset argc,argv to start of non-option command arguments.

Parameters
optindLast option index.
argcArgument count.
argvArgument vector.

Definition at line 216 of file opts.c.

Referenced by OptsGet().

Function Documentation

static int HelpGetC ( int  col,
int  colstart,
char  buf[],
size_t  bufsiz,
size_t *  pbuflen,
int  c 
)
static

Gets help character into local buffer, returning new/same output column position.

Parameters
colCurrent column position.
colstartColumn to start.
buf[out] Buffer to put.
bufsizBuffer maximum size.
pbuflen[out] Buffer insert point.
cHelp character to get (put into buffer).
Returns
New column position.

Definition at line 324 of file opts.c.

References HelpPutBuf().

Referenced by OptsPrintf().

326 {
327  if( *pbuflen >= bufsiz )
328  {
329  *pbuflen = bufsiz - 1;
330  col = HelpPutBuf(col, colstart, buf, pbuflen);
331  }
332  else
333  {
334  buf[*pbuflen] = (char)c;
335  *pbuflen += 1;
336  }
337  return col;
338 }
static int HelpPutBuf(int col, int colstart, char buf[], size_t *pbuflen)
Puts help buffer, returning new output column position.
Definition: opts.c:250
int HelpIndent ( int  over)
inline

Indents (spaces) over, returning number of added indentation.

Parameters
overNumber of spaces.

Definition at line 232 of file opts.c.

Referenced by HelpPutNL(), and OptsPrintOpt().

232 { return printf("%*s", over, ""); }
static int HelpPutBuf ( int  col,
int  colstart,
char  buf[],
size_t *  pbuflen 
)
static

Puts help buffer, returning new output column position.

Parameters
colCurrent column position.
colstartColumn to start.
bufBuffer to output.
pbuflen[out] Buffer length.
Returns
New column position.

Definition at line 250 of file opts.c.

References HELP_MAX_COL, and HelpPutNL().

Referenced by HelpGetC(), and OptsPrintf().

251 {
252  if( *pbuflen == 0 )
253  {
254  return col;
255  }
256  if( (col > colstart) && (col >= (HELP_MAX_COL - *pbuflen)) )
257  {
258  col = HelpPutNL(colstart);
259  }
260  buf[*pbuflen] = 0;
261  col += printf("%s", buf);
262  *pbuflen = 0;
263  return col;
264 }
int HelpPutNL(int over)
Puts help newline, returning new output column position.
Definition: opts.c:238
#define HELP_MAX_COL
max help column
Definition: opts.c:226
static int HelpPutC ( int  col,
int  colstart,
int  c 
)
static

Puts help character, returning new output column position.

Parameters
colCurrent column position.
colstartColumn to start.
cHelp character.
Returns
New column position.

Definition at line 301 of file opts.c.

References HELP_MAX_COL, and HelpPutNL().

Referenced by OptsPrintf().

302 {
303  if( col > HELP_MAX_COL )
304  {
305  col = HelpPutNL(colstart);
306  }
307  col += printf("%c", c);
308  return col;
309 }
int HelpPutNL(int over)
Puts help newline, returning new output column position.
Definition: opts.c:238
#define HELP_MAX_COL
max help column
Definition: opts.c:226
int HelpPutNL ( int  over)
inline

Puts help newline, returning new output column position.

Parameters
overNumber of spaces.

Definition at line 238 of file opts.c.

References HelpIndent().

Referenced by HelpPutBuf(), HelpPutC(), HelpPutStr(), and OptsPrintf().

238 { printf("\n"); return HelpIndent(over); }
int HelpIndent(int over)
Indents (spaces) over, returning number of added indentation.
Definition: opts.c:232
static int HelpPutStr ( int  col,
int  colstart,
const char *  s 
)
static

Puts help string, returning new output column position.

Parameters
colCurrent column position.
colstartColumn to start.
sHelp string.
Returns
New column position.

Definition at line 275 of file opts.c.

References HELP_MAX_COL, HelpPutNL(), and NULL.

Referenced by OptsPrintf().

276 {
277  int n;
278 
279  if( (s == NULL) || (*s == 0) )
280  {
281  return col;
282  }
283  n = (int)strlen(s);
284  if( (col > colstart) && (col >= (HELP_MAX_COL - n)) )
285  {
286  col = HelpPutNL(colstart);
287  }
288  col += printf("%s", s);
289  return col;
290 }
#define NULL
null pointer
Definition: rnrconfig.h:199
int HelpPutNL(int over)
Puts help newline, returning new output column position.
Definition: opts.c:238
#define HELP_MAX_COL
max help column
Definition: opts.c:226
int OptsCvtArgBool ( const char *  argv0,
const char *  sOptName,
char *  optarg,
void *  pOptVal 
)

Convert options boolean argument to bool_t.

1 or "true" converts to true.
0 or "false" converts to false.

Note
Function returns integer for derived applications. librnr Opts ignores the return value.
Parameters
argv0Command name.
sOptNameOption name.
optargParsed option argument to convert (optional).
[out]pOptValPointer to converted option value.
Default:
true
Exceptions
OPTSBADARG()
Returns
If returns, then returns OK.

Definition at line 1018 of file opts.c.

References OK, and OPTSBADARG.

1020 {
1021  if( optarg )
1022  {
1023  if( !strcmp(optarg, "true") || !strcmp(optarg, "1") )
1024  {
1025  *((bool_t *)pOptVal) = true;
1026  }
1027  else if( !strcmp(optarg, "false") || !strcmp(optarg, "0") )
1028  {
1029  *((bool_t *)pOptVal) = false;
1030  }
1031  else
1032  {
1033  OPTSBADARG(argv0, sOptName, optarg);
1034  }
1035  }
1036  // optional argument default
1037  else
1038  {
1039  *((bool_t *)pOptVal) = true;
1040  }
1041 
1042  return OK;
1043 }
#define OK
Okay.
Definition: rnrconfig.h:301
int bool_t
"boolean" T/F
Definition: rnrconfig.h:187
#define OPTSBADARG(argv0, opt, arg)
Standard bad option error message reporter.
Definition: opts.h:263
int OptsCvtArgFloat ( const char *  argv0,
const char *  sOptName,
char *  optarg,
void *  pOptVal 
)

Convert options float argument to double.

Accepted format: optionally signed floating-point number.

Note
Function returns integer for derived applications. librnr Opts ignores the return value.
Parameters
argv0Command name.
sOptNameOption name.
optargParsed option argument to convert (optional).
[out]pOptValPointer to converted option value.
Default:
1.0
Exceptions
OPTSBADARG()
Returns
If returns, then returns OK.

Definition at line 1109 of file opts.c.

References OK, and OPTSBADARG.

1111 {
1112  double optargFloat;
1113 
1114  if( optarg )
1115  {
1116  if( sscanf(optarg, "%lf", &optargFloat) != 1 )
1117  {
1118  OPTSBADARG(argv0, sOptName, optarg);
1119  }
1120  *((double *)pOptVal) = optargFloat;
1121  }
1122  // optional argument default
1123  else
1124  {
1125  *((double *)pOptVal) = 1.0;
1126  }
1127 
1128  return OK;
1129 }
#define OK
Okay.
Definition: rnrconfig.h:301
#define OPTSBADARG(argv0, opt, arg)
Standard bad option error message reporter.
Definition: opts.h:263
int OptsCvtArgInt ( const char *  argv0,
const char *  sOptName,
char *  optarg,
void *  pOptVal 
)

Convert options integer argument to integer.

Accepted formats: decimal hexidecimal(0x<hexdigits>) octal(0<octaldigits>).

Note
Function returns integer for derived applications. librnr Opts ignores the return value.
Parameters
argv0Command name.
sOptNameOption name.
optargParsed option argument to convert (optional).
[out]pOptValPointer to converted option value.
Default:
1
Exceptions
OPTSBADARG()
Returns
If returns, then returns OK.

Definition at line 1065 of file opts.c.

References OK, and OPTSBADARG.

Referenced by OptsCvtArgLogLevel().

1067 {
1068  int optargInt;
1069  char *optargEnd;
1070 
1071  if( optarg )
1072  {
1073  optargInt = (int)strtol(optarg, &optargEnd, 0);
1074  if( *optargEnd != 0 )
1075  {
1076  OPTSBADARG(argv0, sOptName, optarg);
1077  }
1078  *((int *)pOptVal) = optargInt;
1079  }
1080  // optional argument default
1081  else
1082  {
1083  *((int *)pOptVal) = 1;
1084  }
1085 
1086  return OK;
1087 }
#define OK
Okay.
Definition: rnrconfig.h:301
#define OPTSBADARG(argv0, opt, arg)
Standard bad option error message reporter.
Definition: opts.h:263
int OptsCvtArgLogLevel ( const char *  argv0,
const char *  sOptName,
char *  optarg,
void *  pOptVal 
)

Convert options string argument to log threshold level.

Format: "off", "error", "diag1", "diag2", "diag3", "diag4", "diag5", or >= 0

Note
Function returns integer for derived applications. librnr Opts ignores the return value.
Parameters
argv0Command name.
sOptNameOption name.
optargParsed option argument to convert (optional).
[out]pOptValPointer to converted option value.
Default:
LOG_LEVEL_DFT
Exceptions
OPTSBADARG()
Returns
If returns, then returns OK.

Definition at line 1151 of file opts.c.

References arraysize, LOG_LEVEL_DFT, LOG_LEVEL_OFF, NvpName2Val(), OK, and OptsCvtArgInt().

Referenced by OptsLogSetLevel().

1153 {
1154  int logLevel;
1155 
1156  if( optarg )
1157  {
1158  logLevel = NvpName2Val(OptsLogLevelTbl, arraysize(OptsLogLevelTbl), optarg);
1159 
1160  // assume integer format
1161  if( logLevel < LOG_LEVEL_OFF )
1162  {
1163  OptsCvtArgInt(argv0, sOptName, optarg, &logLevel);
1164  }
1165  }
1166  // optional argument default
1167  else
1168  {
1169  logLevel = LOG_LEVEL_DFT;
1170  }
1171 
1172  if( logLevel < LOG_LEVEL_OFF )
1173  {
1174  logLevel = LOG_LEVEL_OFF;
1175  }
1176 
1177  *((int *)pOptVal) = logLevel;
1178 
1179  return OK;
1180 }
static Nvp_T OptsLogLevelTbl[]
Definition: opts.c:72
int OptsCvtArgInt(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options integer argument to integer.
Definition: opts.c:1065
#define LOG_LEVEL_OFF
turn off all non-error logging
Definition: log.h:179
#define OK
Okay.
Definition: rnrconfig.h:301
#define arraysize(array)
array size, i.e. number of array entries
Definition: rnrconfig.h:259
#define LOG_LEVEL_DFT
default log level is off
Definition: log.h:187
int NvpName2Val(Nvp_T tbl[], size_t nTblEntries, const char *sName)
Get the value associated with the name.
Definition: assoc.c:64
int OptsCvtArgStr ( const char *  argv0,
const char *  sOptName,
char *  optarg,
void *  pOptVal 
)

Convert options string argument to string.

Note
Function returns integer for derived applications. librnr Opts ignores the return value.
Parameters
argv0Command name.
sOptNameOption name.
optargParsed option argument to convert (optional).
[out]pOptValPointer to converted option value.
Default:
"" (zero-length string)
Returns
If returns, then returns OK.

Definition at line 987 of file opts.c.

References new_strdup(), and OK.

Referenced by OptsGet(), and OptsLogSetFile().

989 {
990  *((char **)pOptVal) = new_strdup(optarg);
991  return OK;
992 }
char * new_strdup(const char *s)
Duplicate a string.
Definition: new.c:176
#define OK
Okay.
Definition: rnrconfig.h:301
static void OptsDelete ( OptsCtl_T pOptsCtl)
static

Delete an allocated options control structure.

Parameters
pOptsCtlOptions control structure.

Definition at line 832 of file opts.c.

References OptsCtl_T::m_pLongOpts, OptsCtl_T::m_sShortOpts, and NULL.

Referenced by OptsGet().

833 {
834  if( pOptsCtl != NULL )
835  {
836  delete(pOptsCtl->m_pLongOpts);
837  delete(pOptsCtl->m_sShortOpts);
838  delete(pOptsCtl);
839  }
840 }
char * m_sShortOpts
short options string
Definition: opts.c:99
#define NULL
null pointer
Definition: rnrconfig.h:199
struct option * m_pLongOpts
long options structure
Definition: opts.c:98
char* OptsFmtBool ( char *  buf,
size_t  buflen,
void *  pOptVal 
)

Boolean option value string formatter.

Parameters
[out]bufOutput buffer.
buflenBuffer length.
pOptValOption value.
Returns
Pointer to start of buf.

Definition at line 1247 of file opts.c.

1248 {
1249  char *s;
1250 
1251  if( *((int *)pOptVal) == true )
1252  {
1253  s = "true";
1254  }
1255  else
1256  {
1257  s = "false";
1258  }
1259  strncpy(buf, s, buflen);
1260  buf[buflen-1] = 0;
1261  return buf;
1262 }
char* OptsFmtChar ( char *  buf,
size_t  buflen,
void *  pOptVal 
)

Character option value string formatter.

Parameters
[out]bufOutput buffer.
buflenBuffer length.
pOptValOption value.
Returns
Pointer to start of buf.

Definition at line 1273 of file opts.c.

1274 {
1275  buf[0] = *((char *)pOptVal);
1276  buf[1] = 0;
1277  return buf;
1278 }
char* OptsFmtFloat ( char *  buf,
size_t  buflen,
void *  pOptVal 
)

Float option value string formatter.

Parameters
[out]bufOutput buffer.
buflenBuffer length.
pOptValOption value.
Returns
Pointer to start of buf.

Definition at line 1231 of file opts.c.

1232 {
1233  snprintf(buf, buflen, "%f", *((double *)pOptVal));
1234  buf[buflen-1] = 0;
1235  return buf;
1236 }
char* OptsFmtInt ( char *  buf,
size_t  buflen,
void *  pOptVal 
)

Integer option value string formatter.

Parameters
[out]bufOutput buffer.
buflenBuffer length.
pOptValOption value.
Returns
Pointer to start of buf.

Definition at line 1215 of file opts.c.

Referenced by OptsFmtLogLevel().

1216 {
1217  snprintf(buf, buflen, "%d", *((int *)pOptVal));
1218  buf[buflen-1] = 0;
1219  return buf;
1220 }
char* OptsFmtLogLevel ( char *  buf,
size_t  buflen,
void *  pOptVal 
)

Log Level option value string formatter.

Parameters
[out]bufOutput buffer.
buflenBuffer length.
pOptValOption value.
Returns
Pointer to start of buf.

Definition at line 1289 of file opts.c.

References LOG_LEVEL_OFF, OptsFmtInt(), and OptsFmtStr().

1290 {
1291  int level = *((int *)pOptVal);
1292  char *off = "off";
1293 
1294  if( level == LOG_LEVEL_OFF )
1295  {
1296  return OptsFmtStr(buf, buflen, &off);
1297  }
1298  else
1299  {
1300  return OptsFmtInt(buf, buflen, pOptVal);
1301  }
1302 }
#define LOG_LEVEL_OFF
turn off all non-error logging
Definition: log.h:179
char * OptsFmtInt(char *buf, size_t buflen, void *pOptVal)
Integer option value string formatter.
Definition: opts.c:1215
char * OptsFmtStr(char *buf, size_t buflen, void *pOptVal)
String option value string formatter.
Definition: opts.c:1191
char* OptsFmtStr ( char *  buf,
size_t  buflen,
void *  pOptVal 
)

String option value string formatter.

Parameters
[out]bufOutput buffer.
buflenBuffer length.
pOptValOption value.
Returns
Pointer to start of buf.

Definition at line 1191 of file opts.c.

References NULL.

Referenced by OptsFmtLogLevel().

1192 {
1193 
1194  if( (pOptVal == NULL) || (*((char **)pOptVal) == NULL) )
1195  {
1196  buf[0] = 0;
1197  }
1198  else
1199  {
1200  strncpy(buf, *((char **)pOptVal), buflen);
1201  buf[buflen-1] = 0;
1202  }
1203  return buf;
1204 }
#define NULL
null pointer
Definition: rnrconfig.h:199
char** OptsGet ( const char *  argv0,
const PkgInfo_T pPkgInfo,
OptsPgmInfo_T pPgmInfo,
OptsInfo_T pOptsInfo,
bool_t  bHasLogging,
int *  pargc,
char *  argv[] 
)

Gets, validates, and sets all command line options.

Parameters
argv0(Massaged) program name.
pPkgInfoPackage info.
pPgmInfoProgram info.
pOptsInfoProgram options info.
bHasLoggingDo [not] include logging options.
[out]pargcNew non-option argument count.
argvCommand-line options and arguments. arguments.
Returns
Reposition command-line arguments to start of first non-option comand argument.

Definition at line 861 of file opts.c.

References EC_BAD_OPT, OptsInfo_T::fn_cvt, OptsInfo_T::has_arg, OptsInfo_T::long_opt, OptsCtl_T::m_pLongOpts, OptsCtl_T::m_sShortOpts, NULL, OK, OptsInfo_T::opt_addr, OPTS_LONGOPT_LOG, OPTS_LONGOPT_LOGFILE, OPTS_RESET_ARGCV, OPTS_RVAL_ERROR, OPTS_RVAL_HELP, OPTS_RVAL_LOG, OPTS_RVAL_LOGFILE, OPTS_RVAL_LOGNOCOLOR, OPTS_RVAL_LOGNOTIME, OPTS_RVAL_VERSION, OptsCvtArgStr(), OptsDelete(), OptsHelp(), OptsInvalid(), OptsLogDisableColor(), OptsLogDisableTimestamp(), OptsLogSetFile(), OptsLogSetLevel(), OptsNew(), OptsVersion(), and OptsInfo_T::pvt_retval.

Referenced by init().

867 {
868  OptsCtl_T *pOptsCtl;
869  OptsInfo_T *p;
870  int argc = *pargc;
871  int c;
872  int option_index;
873  bool_t bHasOpt;
874 
875  //
876  // Set external getopt_long() variables. N.B. makes OptsGet() non-reentrant.
877  //
878  optarg = NULL; // current argument to option
879  optind = 0; // next index in argv to look for options
880  opterr = 1; // allow getopt_long() to print error messages
881  optopt = 0; // current parsed option character
882 
883  // build option arguments from info
884  pOptsCtl = OptsNew(pOptsInfo, bHasLogging);
885 
886  // parse and convert options
887  while(true)
888  {
889  option_index = 0;
890 
891  // parse next long/short option
892  c = getopt_long(argc, argv, pOptsCtl->m_sShortOpts, pOptsCtl->m_pLongOpts,
893  &option_index);
894 
895  // end of option list
896  if( c == -1 )
897  {
898  break;
899  }
900 
901  // process option
902  switch(c)
903  {
904  case OPTS_RVAL_ERROR: // -? options error
905  OptsInvalid(argv0, "");
906  break;
907  case OPTS_RVAL_HELP: // --help
908  OptsHelp(argv0, pOptsInfo, pPgmInfo, bHasLogging);
909  exit(OK);
910  break;
911  case OPTS_RVAL_VERSION: // --version
912  OptsVersion(argv0, pPkgInfo);
913  exit(OK);
914  break;
915  case OPTS_RVAL_LOG: // -l, --log <level>
916  OptsLogSetLevel(argv0, OPTS_LONGOPT_LOG, optarg);
917  break;
918  case OPTS_RVAL_LOGFILE: // --logfile <file>
919  OptsLogSetFile(argv0, OPTS_LONGOPT_LOGFILE, optarg);
920  break;
921  case OPTS_RVAL_LOGNOCOLOR: // --log-no-color
922  OptsLogDisableColor(argv0, OPTS_LONGOPT_LOG, optarg);
923  break;
924  case OPTS_RVAL_LOGNOTIME: // --log-no-timestamp
926  break;
927  default: // user application option
928  bHasOpt = false;
929  for(p=pOptsInfo; (p!=NULL) && (p->long_opt!=NULL); ++p)
930  {
931  if( p->pvt_retval == c )
932  {
933  bHasOpt = true;
934 
935  // user supplied option conversion function
936  if( p->fn_cvt != NULL )
937  {
938  p->fn_cvt(argv0, p->long_opt, optarg, p->opt_addr);
939  }
940  // default for option w/o argument is boolean true value
941  else if( p->has_arg == no_argument )
942  {
943  *((int *)p->opt_addr) = true;
944  }
945  // default for option w/ argument is string value
946  else
947  {
948  OptsCvtArgStr(argv0, p->long_opt, optarg, p->opt_addr);
949  }
950  }
951  }
952  if( !bHasOpt )
953  {
954  fprintf(stderr, "Warning: getopt_long() return character code 0x%x\n",
955  c);
956  exit(EC_BAD_OPT);
957  }
958  break;
959  }
960  }
961 
962  OptsDelete(pOptsCtl);
963 
964  OPTS_RESET_ARGCV(optind, argc, argv);
965 
966  *pargc = argc;
967 
968  return argv;
969 }
static void OptsDelete(OptsCtl_T *pOptsCtl)
Delete an allocated options control structure.
Definition: opts.c:832
char * m_sShortOpts
short options string
Definition: opts.c:99
#define OPTS_RVAL_LOGNOTIME
–log-no-timestamp rval
Definition: opts.h:111
static void OptsLogDisableTimestamp(const char *argv0, const char *sOptName, char *optarg)
Disable program logging with timestamps.
Definition: opts.c:702
#define OK
Okay.
Definition: rnrconfig.h:301
#define NULL
null pointer
Definition: rnrconfig.h:199
void * opt_addr
Address of option variable.
Definition: opts.h:162
#define OPTS_LONGOPT_LOGFILE
log file long option name
Definition: opts.c:89
static void OptsLogDisableColor(const char *argv0, const char *sOptName, char *optarg)
Disable program logging in color.
Definition: opts.c:684
#define EC_BAD_OPT
bad command line option exit code
Definition: rnrconfig.h:311
#define OPTS_RVAL_HELP
–help return value
Definition: opts.h:107
static void OptsHelp(const char *argv0, OptsInfo_T *pOptsInfo, OptsPgmInfo_T *pPgmInfo, bool_t bHasLogging)
Prints command-line help.
Definition: opts.c:544
#define OPTS_RVAL_LOGFILE
–logfile return value
Definition: opts.h:109
OptsCvtFunc_T fn_cvt
Option conversion function.
Definition: opts.h:170
static void OptsLogSetFile(const char *argv0, const char *sOptName, char *optarg)
Set program logging output file.
Definition: opts.c:664
int pvt_retval
Return value.
Definition: opts.h:204
int bool_t
"boolean" T/F
Definition: rnrconfig.h:187
static OptsCtl_T * OptsNew(OptsInfo_T *pOptsInfo, bool_t bHasLogging)
Allocate and build short and long options getopt_long() parameters from the provided option info...
Definition: opts.c:718
#define OPTS_RVAL_LOGNOCOLOR
–log-no-color rval
Definition: opts.h:110
struct option * m_pLongOpts
long options structure
Definition: opts.c:98
#define OPTS_RESET_ARGCV(optind, argc, argv)
Reset argc,argv to start of non-option command arguments.
Definition: opts.c:216
static void OptsLogSetLevel(const char *argv0, const char *sOptName, char *optarg)
Set program logging level.
Definition: opts.c:644
#define OPTS_RVAL_ERROR
options error
Definition: opts.h:104
void OptsInvalid(const char *argv0, const char *sFmt,...)
Invalid option or option argument print and exit.
Definition: opts.c:1313
int has_arg
Option does [not] have an argument of type.
Definition: opts.h:149
#define OPTS_RVAL_VERSION
–version return value
Definition: opts.h:108
static void OptsVersion(const char *argv0, const PkgInfo_T *pPkgInfo)
Prints command-line program version string(s).
Definition: opts.c:621
#define OPTS_RVAL_LOG
-l, –log return value
Definition: opts.h:105
int OptsCvtArgStr(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options string argument to string.
Definition: opts.c:987
const char * long_opt
Long option string name.
Definition: opts.h:137
#define OPTS_LONGOPT_LOG
log long option name
Definition: opts.c:88
Short and Long Options Info.
Definition: opts.h:134
Definition: opts.c:96
static void OptsHelp ( const char *  argv0,
OptsInfo_T pOptsInfo,
OptsPgmInfo_T pPgmInfo,
bool_t  bHasLogging 
)
static

Prints command-line help.

This functions is called to act on –help option.

Parameters
argv0Program name.
pOptsInfoOptions info.
pPgmInfoProgram info.
bHasLoggingThis program does [not] support the logging optoins.

Definition at line 544 of file opts.c.

References OptsPgmInfo_T::diagnostics, OptsPgmInfo_T::long_desc, OptsInfo_T::long_opt, NULL, OPTS_RVAL_HELP, OPTS_RVAL_LOG, OPTS_RVAL_LOGFILE, OptsPrintf(), OptsPrintOpt(), OptsInfo_T::pvt_retval, OptsPgmInfo_T::synopsis, and OptsPgmInfo_T::usage_args.

Referenced by OptsGet().

548 {
549  OptsInfo_T *p;
550  int col;
551 
552  // usage
553  col = printf("Usage: %s [OPTIONS] ", argv0);
554  if( (pPgmInfo != NULL) && (pPgmInfo->usage_args != NULL) )
555  {
556  col = OptsPrintf(col, 2, pPgmInfo->usage_args, argv0, NULL);
557  }
558 
559  // synopsis
560  printf("\n");
561  if( (pPgmInfo != NULL) && (pPgmInfo->synopsis != NULL) )
562  {
563  col = OptsPrintf(0, 0, pPgmInfo->synopsis, argv0, NULL);
564  printf("\n");
565  }
566 
567  // options
568  printf(
569 "\n"
570 "Mandatory arguments to long options are also mandatory for short options.\n");
571 
572  if( pOptsInfo != NULL )
573  {
574  for(p=pOptsInfo; p->long_opt!=NULL; ++p)
575  {
576  OptsPrintOpt(argv0, p);
577  }
578  }
579 
580  for(p=OptsBuiltIn; p->long_opt!=NULL; ++p)
581  {
582  if( !bHasLogging &&
583  ((p->pvt_retval == OPTS_RVAL_LOG)
584  || (p->pvt_retval == OPTS_RVAL_LOGFILE)) )
585  {
586  continue;
587  }
588  if( p->pvt_retval == OPTS_RVAL_HELP )
589  {
590  printf("\n");
591  }
592  OptsPrintOpt(argv0, p);
593  }
594 
595  // long description
596  if( (pPgmInfo != NULL) && (pPgmInfo->long_desc != NULL) )
597  {
598  printf("\n");
599  col = OptsPrintf(0, 0, pPgmInfo->long_desc, argv0, NULL);
600  printf("\n");
601  }
602 
603  // diagnostics
604  if( (pPgmInfo != NULL) && (pPgmInfo->diagnostics != NULL) )
605  {
606  printf("\n");
607  printf("DIAGNOSTICS:\n");
608  col = OptsPrintf(0, 0, pPgmInfo->diagnostics, argv0, NULL);
609  printf("\n");
610  }
611 }
const char * diagnostics
Programs diagnostics subsection string.
Definition: opts.h:254
#define NULL
null pointer
Definition: rnrconfig.h:199
const char * long_desc
Program full description string.
Definition: opts.h:247
#define OPTS_RVAL_HELP
–help return value
Definition: opts.h:107
const char * synopsis
Simple program synopsis string.
Definition: opts.h:240
static void OptsPrintOpt(const char *argv0, OptsInfo_T *pOptsInfo)
Prints option syntax and description in standard &#39;usage&#39; format.
Definition: opts.c:456
static int OptsPrintf(int col, int colstart, const char *sFmtDesc, const char *sPgmName, OptsInfo_T *pOptsInfo)
Prints formatted description.
Definition: opts.c:362
const char * usage_args
Single line description string of non-option arguments.
Definition: opts.h:233
#define OPTS_RVAL_LOGFILE
–logfile return value
Definition: opts.h:109
int pvt_retval
Return value.
Definition: opts.h:204
#define OPTS_RVAL_LOG
-l, –log return value
Definition: opts.h:105
const char * long_opt
Long option string name.
Definition: opts.h:137
Short and Long Options Info.
Definition: opts.h:134
static OptsInfo_T OptsBuiltIn[]
Built-In Options.
Definition: opts.c:113
void OptsInvalid ( const char *  argv0,
const char *  sFmt,
  ... 
)

Invalid option or option argument print and exit.

Parameters
argv0Program name.
sFmtError format string.
...Variable arguments to format string.
Note
The function terminates process with exit status EC_BAD_OPT(2).

Definition at line 1313 of file opts.c.

References EC_BAD_OPT, MAX_FMT_BUF, and NULL.

Referenced by OptsGet().

1314 {
1315  va_list ap;
1316  char fmtbuf[MAX_FMT_BUF];
1317 
1318  if( (sFmt != NULL) && (*sFmt != 0) )
1319  {
1320  snprintf(fmtbuf, MAX_FMT_BUF, "%s: %s\n", argv0, sFmt);
1321  fmtbuf[MAX_FMT_BUF-1] = 0;
1322  va_start(ap, sFmt);
1323  vfprintf(stderr, fmtbuf, ap);
1324  va_end(ap);
1325  }
1326  fprintf(stderr, "Try '%s --help' for more information.\n", argv0);
1327  exit(EC_BAD_OPT);
1328 }
#define NULL
null pointer
Definition: rnrconfig.h:199
#define EC_BAD_OPT
bad command line option exit code
Definition: rnrconfig.h:311
#define MAX_FMT_BUF
Format buffer size.
Definition: opts.c:219
static void OptsLogDisableColor ( const char *  argv0,
const char *  sOptName,
char *  optarg 
)
static

Disable program logging in color.

This function is called to act on the –log-no-color option.

Parameters
argv0Program name.
sOptNameLong option name.
optargLong option argument (from getopt_long(3)).

Definition at line 684 of file opts.c.

References LOG_SET_COLOR_ENABLE().

Referenced by OptsGet().

686 {
687 #ifdef LOG
688  LOG_SET_COLOR_ENABLE(false);
689 #endif // LOG
690 }
void LOG_SET_COLOR_ENABLE(bool_t enable)
Enable/disable logging in color.
Definition: log.c:160
static void OptsLogDisableTimestamp ( const char *  argv0,
const char *  sOptName,
char *  optarg 
)
static

Disable program logging with timestamps.

This function is called to act on the –log-no-timestamp option.

Parameters
argv0Program name.
sOptNameLong option name.
optargLong option argument (from getopt_long(3)).

Definition at line 702 of file opts.c.

References LOG_SET_TIMESTAMP_ENABLE().

Referenced by OptsGet().

704 {
705 #ifdef LOG
707 #endif // LOG
708 }
void LOG_SET_TIMESTAMP_ENABLE(bool_t enable)
Enable/disable log timestamps.
Definition: log.c:176
static void OptsLogSetFile ( const char *  argv0,
const char *  sOptName,
char *  optarg 
)
static

Set program logging output file.

This function is called to act on the –logfile <file> option.

Parameters
argv0Program name.
sOptNameLong option name.
optargLong option argument (from getopt_long(3)).

Definition at line 664 of file opts.c.

References LOG_SET_LOGFILE(), and OptsCvtArgStr().

Referenced by OptsGet().

666 {
667 #ifdef LOG
668  char *sFileName;
669 
670  OptsCvtArgStr(argv0, sOptName, optarg, &sFileName);
671  LOG_SET_LOGFILE(sFileName);
672 #endif // LOG
673 }
int LOG_SET_LOGFILE(const char *sLogFileName)
Set new logging output file.
Definition: log.c:199
int OptsCvtArgStr(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options string argument to string.
Definition: opts.c:987
static void OptsLogSetLevel ( const char *  argv0,
const char *  sOptName,
char *  optarg 
)
static

Set program logging level.

This function is called to act on the –log <level> option.

Parameters
argv0Program name.
sOptNameLong option name.
optargLong option argument (from getopt_long(3)).

Definition at line 644 of file opts.c.

References LOG_SET_THRESHOLD(), and OptsCvtArgLogLevel().

Referenced by OptsGet().

646 {
647 #ifdef LOG
648  int level;
649 
650  OptsCvtArgLogLevel(argv0, sOptName, optarg, &level);
651  LOG_SET_THRESHOLD(level);
652 #endif // LOG
653 }
int OptsCvtArgLogLevel(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options string argument to log threshold level.
Definition: opts.c:1151
int LOG_SET_THRESHOLD(int nLevel)
Set new logging threshold level.
Definition: log.c:96
static OptsCtl_T* OptsNew ( OptsInfo_T pOptsInfo,
bool_t  bHasLogging 
)
static

Allocate and build short and long options getopt_long() parameters from the provided option info.

Parameters
pOptsInfoOptions info table.
bHasLoggingThis program does [not] support the logging optoins.
Returns
New options control structure.

Definition at line 718 of file opts.c.

References arraysize, OptsInfo_T::has_arg, OptsInfo_T::long_opt, OptsCtl_T::m_pLongOpts, OptsCtl_T::m_sShortOpts, NEW, NULL, OPTS_LONGOPT_LOG, OPTS_LONGOPT_LOGFILE, OPTS_NO_SHORT, OPTS_RVAL_USER, OptsInfo_T::pvt_retval, and OptsInfo_T::short_opt.

Referenced by OptsGet().

719 {
720  size_t n;
721  struct option *pLongOpts, *p;
722  OptsInfo_T *q;
723  char *sShortOpts, *s;
724  OptsCtl_T *pOptsCtl;
725 
726  // count the number of options
727  for(n=0; pOptsInfo[n].long_opt!=NULL; ++n); // user
728 
729  n += arraysize(OptsBuiltIn) - 1;
730 
731  // allocate memory
732  pLongOpts = new(sizeof(struct option)*(n+1)); // include null
733  sShortOpts = new(sizeof(char)*(n*3+1)); // include null
734 
735  //
736  // Make Built-In long and short option arguments to getopt_long()
737  //
738  for(p=pLongOpts, s=sShortOpts, q=OptsBuiltIn; q->long_opt!=NULL; ++q)
739  {
740  // ignore logging options
741  if( !bHasLogging
742  && (!strcmp(q->long_opt, OPTS_LONGOPT_LOG)
743  || !strcmp(q->long_opt, OPTS_LONGOPT_LOGFILE)) )
744  {
745  continue;
746  }
747 
748  // long option also has corresponding short option
749  if( q->short_opt != OPTS_NO_SHORT )
750  {
751  *s++ = (char)(q->short_opt);
752  if( q->has_arg != no_argument )
753  {
754  *s++ = ':';
755  }
756  if( q->has_arg == optional_argument )
757  {
758  *s++ = ':';
759  }
760 
761  // check return value must be an ascii characater
762  if( q->pvt_retval != q->short_opt )
763  {
764  fprintf(stderr,
765  "Warning: Built-in option pvt_retval 0x%x != short_opt '%c'\n",
766  q->pvt_retval, q->short_opt);
767  }
768  }
769 
770  // long option
771  p->name = q->long_opt;
772  p->has_arg = q->has_arg;
773  p->flag = NULL;
774  p->val = q->pvt_retval;
775  p++;
776  }
777 
778  //
779  // Make User-Defined long and short option arguments to getopt_long()
780  //
781  for(n=0, q=pOptsInfo; q->long_opt!=NULL; ++n, ++q)
782  {
783  // long option only
784  if( q->short_opt == OPTS_NO_SHORT )
785  {
786  q->pvt_retval = OPTS_RVAL_USER + (int)n;
787  }
788  // long option also has corresponding short option
789  else
790  {
791  *s++ = (char)(q->short_opt);
792  if( q->has_arg != no_argument )
793  {
794  *s++ = ':';
795  }
796  if( q->has_arg == optional_argument )
797  {
798  *s++ = ':';
799  }
800  q->pvt_retval = q->short_opt;
801  }
802 
803  // long option
804  p->name = q->long_opt;
805  p->has_arg = q->has_arg;
806  p->flag = NULL;
807  p->val = q->pvt_retval;
808  p++;
809  }
810 
811  // end of long options
812  p->name = NULL;
813  p->has_arg = no_argument;
814  p->flag = NULL;
815  p->val = 0;
816 
817  // end of short options string
818  *s = 0;
819 
820  pOptsCtl = NEW(OptsCtl_T);
821  pOptsCtl->m_pLongOpts = pLongOpts;
822  pOptsCtl->m_sShortOpts = sShortOpts;
823 
824  return pOptsCtl;
825 }
char * m_sShortOpts
short options string
Definition: opts.c:99
int short_opt
Short option character.
Definition: opts.h:143
#define OPTS_RVAL_USER
start of user available
Definition: opts.h:112
#define NULL
null pointer
Definition: rnrconfig.h:199
#define OPTS_LONGOPT_LOGFILE
log file long option name
Definition: opts.c:89
#define arraysize(array)
array size, i.e. number of array entries
Definition: rnrconfig.h:259
#define NEW(T)
Allocate new type.
Definition: new.h:49
#define OPTS_NO_SHORT
no short option equivalent
Definition: opts.h:99
int pvt_retval
Return value.
Definition: opts.h:204
struct option * m_pLongOpts
long options structure
Definition: opts.c:98
int has_arg
Option does [not] have an argument of type.
Definition: opts.h:149
const char * long_opt
Long option string name.
Definition: opts.h:137
#define OPTS_LONGOPT_LOG
log long option name
Definition: opts.c:88
Short and Long Options Info.
Definition: opts.h:134
static OptsInfo_T OptsBuiltIn[]
Built-In Options.
Definition: opts.c:113
Definition: opts.c:96
static int OptsPrintf ( int  col,
int  colstart,
const char *  sFmtDesc,
const char *  sPgmName,
OptsInfo_T pOptsInfo 
)
static

Prints formatted description.

Formatting Directives:
The description argumnet sFmtDesc may contain the following formatting directives:
P - print program name (argv0)
L - print name of Long argument
S - print name of Short argument
A - print name of option Argument
D - print value of Default
%% - print %
Parameters
colCurrent column position.
colstartColumn to start.
sFmtDescFormatted description string.
sPgmNameProgram name (argv0).
pOptsInfoOptions info.
Returns
New column position.

Definition at line 362 of file opts.c.

References OptsInfo_T::arg_name, HelpGetC(), HelpPutBuf(), HelpPutC(), HelpPutNL(), HelpPutStr(), LOGERROR, OptsInfo_T::long_opt, NULL, OPTS_NO_SHORT, OptsInfo_T::pvt_opt_dft, and OptsInfo_T::short_opt.

Referenced by OptsHelp(), and OptsPrintOpt().

367 {
368  char buf[1028];
369  size_t buflen = 0;
370  const char *s;
371 
372  for(s=sFmtDesc; s && *s; ++s)
373  {
374  switch( *s )
375  {
376  case '\n':
377  col = HelpPutBuf(col, colstart, buf, &buflen);
378  col = HelpPutNL(colstart);
379  break;
380  case ' ':
381  case '\t':
382  col = HelpPutBuf(col, colstart, buf, &buflen);
383  col = HelpPutC(col, colstart, ' ');
384  break;
385  case '%':
386  ++s;
387  switch(*s)
388  {
389  case 'P':
390  if( sPgmName != NULL )
391  {
392  col = HelpPutBuf(col, colstart, buf, &buflen);
393  col = HelpPutStr(col, colstart, sPgmName);
394  }
395  break;
396  case 'L':
397  if((pOptsInfo != NULL) && (pOptsInfo->long_opt != NULL))
398  {
399  col = HelpPutBuf(col, colstart, buf, &buflen);
400  col = HelpPutStr(col, colstart, pOptsInfo->long_opt);
401  }
402  break;
403  case 'S':
404  if((pOptsInfo != NULL) && (pOptsInfo->short_opt != OPTS_NO_SHORT))
405  {
406  col = HelpGetC(col, colstart, buf, sizeof(buf), &buflen,
407  pOptsInfo->short_opt);
408  }
409  break;
410  case 'A':
411  if((pOptsInfo != NULL) && (pOptsInfo->arg_name != NULL))
412  {
413  col = HelpPutBuf(col, colstart, buf, &buflen);
414  col = HelpPutStr(col, colstart, pOptsInfo->arg_name);
415  }
416  break;
417  case 'D':
418  if((pOptsInfo != NULL) && (pOptsInfo->pvt_opt_dft != NULL))
419  {
420  col = HelpPutBuf(col, colstart, buf, &buflen);
421  col = HelpPutStr(col, colstart, pOptsInfo->pvt_opt_dft);
422  }
423  break;
424  case '%':
425  col = HelpGetC(col, colstart, buf, sizeof(buf), &buflen, *s);
426  break;
427  default:
428  --s;
429  LOGERROR("Invalid print directive: '%.2s'", s);
430  break;
431  }
432  break;
433  default:
434  col = HelpGetC(col, colstart, buf, sizeof(buf), &buflen, *s);
435  break;
436  }
437  }
438 
439  // the last word
440  if( buflen > 0 )
441  {
442  col = HelpPutBuf(col, colstart, buf, &buflen);
443  }
444 
445  return col;
446 }
int short_opt
Short option character.
Definition: opts.h:143
static int HelpPutC(int col, int colstart, int c)
Puts help character, returning new output column position.
Definition: opts.c:301
#define NULL
null pointer
Definition: rnrconfig.h:199
int HelpPutNL(int over)
Puts help newline, returning new output column position.
Definition: opts.c:238
char * pvt_opt_dft
Option default value in string format.
Definition: opts.h:212
#define LOGERROR(fmt,...)
Standard Error logging.
Definition: log.h:488
static int HelpPutStr(int col, int colstart, const char *s)
Puts help string, returning new output column position.
Definition: opts.c:275
const char * arg_name
Option argument name string.
Definition: opts.h:183
#define OPTS_NO_SHORT
no short option equivalent
Definition: opts.h:99
static int HelpPutBuf(int col, int colstart, char buf[], size_t *pbuflen)
Puts help buffer, returning new output column position.
Definition: opts.c:250
static int HelpGetC(int col, int colstart, char buf[], size_t bufsiz, size_t *pbuflen, int c)
Gets help character into local buffer, returning new/same output column position. ...
Definition: opts.c:324
const char * long_opt
Long option string name.
Definition: opts.h:137
static void OptsPrintOpt ( const char *  argv0,
OptsInfo_T pOptsInfo 
)
static

Prints option syntax and description in standard 'usage' format.

Parameters
argv0Program name.
pOptsInfoOptions info.
Returns
New column position.

Definition at line 456 of file opts.c.

References OptsInfo_T::arg_name, OptsInfo_T::fn_fmt, OptsInfo_T::has_arg, OptsInfo_T::has_default, HELP_OPT_OVER, HELP_OPT_OVER_2, HelpIndent(), OptsInfo_T::long_opt, NULL, OptsInfo_T::opt_addr, OptsInfo_T::opt_desc, OPTS_NO_SHORT, OptsPrintf(), OptsInfo_T::pvt_opt_dft, and OptsInfo_T::short_opt.

Referenced by OptsHelp().

457 {
458  char buf[1028], buf2[256];
459  size_t buflen = sizeof(buf), buflen2 = sizeof(buf2);
460  int col;
461 
462  // indent
463  col = snprintf(buf, buflen, " ");
464 
465  // short option
466  if( pOptsInfo->short_opt != OPTS_NO_SHORT )
467  {
468  col += snprintf(buf+col, buflen-(size_t)col, "-%c", pOptsInfo->short_opt);
469  }
470  else
471  {
472  col += snprintf(buf+col, buflen-(size_t)col, " ");
473  }
474 
475  // long option
476  if( pOptsInfo->long_opt != NULL )
477  {
478  col += snprintf(buf+col, buflen-(size_t)col, "%s--%s",
479  (pOptsInfo->short_opt != OPTS_NO_SHORT? ", ": " "),
480  pOptsInfo->long_opt);
481  }
482 
483  // option argument name
484  if( (pOptsInfo->has_arg != no_argument) &&
485  (pOptsInfo->arg_name != NULL) )
486  {
487  col += snprintf(buf+col, buflen-(size_t)col, "%s%s",
488  (pOptsInfo->long_opt != NULL? "=": " "),
489  pOptsInfo->arg_name);
490 
491  }
492 
493  // print option keyword(s) syntax
494  buf[sizeof(buf)-1] = 0;
495  printf("%s", buf);
496  buflen = 0;
497 
498  // scoot over to start of description column
499  if( col > HELP_OPT_OVER )
500  {
501  printf("\n");
502  col = 0;
503  }
505  col = HELP_OPT_OVER;
506 
507  // option default value string
508  if( pOptsInfo->has_default
509  && (pOptsInfo->fn_fmt != NULL)
510  && (pOptsInfo->opt_addr != NULL) )
511  {
512  pOptsInfo->pvt_opt_dft =
513  pOptsInfo->fn_fmt(buf2, buflen2, pOptsInfo->opt_addr);
514  }
515  else
516  {
517  pOptsInfo->pvt_opt_dft = NULL;
518  }
519 
520  // print description
521  col = OptsPrintf(col, HELP_OPT_OVER_2, pOptsInfo->opt_desc, argv0,
522  pOptsInfo);
523 
524  printf("\n");
525 
526  // print default value
527  if( pOptsInfo->pvt_opt_dft != NULL )
528  {
530  printf("DEFAULT: %s\n", pOptsInfo->pvt_opt_dft);
531  }
532 }
int short_opt
Short option character.
Definition: opts.h:143
OptsFmtFunc_T fn_fmt
Option value string formatting function.
Definition: opts.h:177
#define NULL
null pointer
Definition: rnrconfig.h:199
void * opt_addr
Address of option variable.
Definition: opts.h:162
int HelpIndent(int over)
Indents (spaces) over, returning number of added indentation.
Definition: opts.c:232
char * pvt_opt_dft
Option default value in string format.
Definition: opts.h:212
static int OptsPrintf(int col, int colstart, const char *sFmtDesc, const char *sPgmName, OptsInfo_T *pOptsInfo)
Prints formatted description.
Definition: opts.c:362
const char * arg_name
Option argument name string.
Definition: opts.h:183
#define OPTS_NO_SHORT
no short option equivalent
Definition: opts.h:99
#define HELP_OPT_OVER
opt help desc 1st line indent
Definition: opts.c:224
bool_t has_default
Option does [not] have a default value.
Definition: opts.h:155
#define HELP_OPT_OVER_2
opt help desc 2nd line+ indent
Definition: opts.c:225
int has_arg
Option does [not] have an argument of type.
Definition: opts.h:149
const char * opt_desc
Option description string.
Definition: opts.h:195
const char * long_opt
Long option string name.
Definition: opts.h:137
static void OptsVersion ( const char *  argv0,
const PkgInfo_T pPkgInfo 
)
static

Prints command-line program version string(s).

This functions is called to act on –version option.

Parameters
argv0Program name.
pPkgInfoPackage info.

Definition at line 621 of file opts.c.

References PkgInfo_T::m_sPkgAuthors, PkgInfo_T::m_sPkgDate, PkgInfo_T::m_sPkgDisclaimer, PkgInfo_T::m_sPkgName, PkgInfo_T::m_sPkgOwners, PkgInfo_T::m_sPkgTimeStamp, and PkgInfo_T::m_sPkgVersion.

Referenced by OptsGet().

622 {
623  printf(
624 "%s (package %s-%s %s)\n"
625 "Written by %s\n\n"
626 "Copyright (C) %s %s\n"
627 "%s\n",
628  argv0,
629  pPkgInfo->m_sPkgName, pPkgInfo->m_sPkgVersion, pPkgInfo->m_sPkgTimeStamp,
630  pPkgInfo->m_sPkgAuthors,
631  pPkgInfo->m_sPkgDate, pPkgInfo->m_sPkgOwners,
632  pPkgInfo->m_sPkgDisclaimer);
633 }
const char * m_sPkgVersion
package dotted version string
Definition: pkg.h:37
const char * m_sPkgTimeStamp
package build date
Definition: pkg.h:38
const char * m_sPkgDate
package extended creation date string
Definition: pkg.h:39
const char * m_sPkgName
package name string
Definition: pkg.h:36
const char * m_sPkgDisclaimer
package disclaimer string
Definition: pkg.h:43
const char * m_sPkgOwners
package owner(s) string
Definition: pkg.h:42
const char * m_sPkgAuthors
package author(s) string
Definition: pkg.h:41

Variable Documentation

int OptsLogLevel = LOG_LEVEL_DFT
static

working log level value

Built-in logging option working variables.

Definition at line 105 of file opts.c.

Nvp_T OptsLogLevelTbl[]
static
Initial value:
=
{
{"nosym", -1},
{"off", LOG_LEVEL_OFF},
{"error", LOG_LEVEL_ERROR},
{"diag1", LOG_LEVEL_DIAG1},
{"diag2", LOG_LEVEL_DIAG2},
{"diag3", LOG_LEVEL_DIAG3},
{"diag4", LOG_LEVEL_DIAG4},
{"diag5", LOG_LEVEL_DIAG5}
}
#define LOG_LEVEL_DIAG2
diagnostic level 2
Definition: log.h:182
#define LOG_LEVEL_OFF
turn off all non-error logging
Definition: log.h:179
#define LOG_LEVEL_DIAG5
diagnostic level 5
Definition: log.h:185
#define LOG_LEVEL_DIAG3
diagnostic level 3
Definition: log.h:183
#define LOG_LEVEL_DIAG1
diagnostic level 1
Definition: log.h:181
#define LOG_LEVEL_ERROR
errors
Definition: log.h:180
#define LOG_LEVEL_DIAG4
diagnostic level 4
Definition: log.h:184

Name-Value Pair table of names to log levels.

Definition at line 72 of file opts.c.