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

Example of using the librnr config module. More...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "rnr/config.h"

Go to the source code of this file.

Classes

struct  Cmd_T
 command structure type More...
 

Macros

#define CHKARGS(argc, min)
 Check argument count against minimum required. More...
 
#define CHKCFG()
 Check that the configuration db exists. More...
 
#define CHKNOCFG()
 Check that the configuration db does not exist. More...
 

Typedefs

typedef void(* CmdExec_T) (int, char *[])
 command execution function type
 

Functions

static void CmdHelp (int argc, char *argv[])
 Print command help. More...
 
static void CmdQuit (int argc, char *argv[])
 Quit this example program. More...
 
static void CmdLog (int argc, char *argv[])
 Set diagnotics logging level. More...
 
static void CmdDbDelete (int argc, char *argv[])
 Delete configuration db. More...
 
static void CmdDbNew (int argc, char *argv[])
 Create new, empty configuration db. More...
 
static void CmdDbPrint (int argc, char *argv[])
 Print out entire configuration db. More...
 
static void CmdDbRead (int argc, char *argv[])
 Create new configuration db from file. More...
 
static void CmdDelete (int argc, char *argv[])
 Delete configuration key. More...
 
static void CmdFind (int argc, char *argv[])
 Find configuration key=values matching substring. More...
 
static void CmdGetd (int argc, char *argv[])
 Get configuration value string converting to double. More...
 
static void CmdGeti (int argc, char *argv[])
 Get configuration value string converting to int. More...
 
static void CmdGets (int argc, char *argv[])
 Get configuration value string. More...
 
static void CmdSectDelete (int argc, char *argv[])
 Delete entier configuration db section. More...
 
static void CmdSectNew (int argc, char *argv[])
 Create new, empty configuration db section. More...
 
static void CmdSetd (int argc, char *argv[])
 Set or create key=<double> entry in section. More...
 
static void CmdSeti (int argc, char *argv[])
 Set or create key=<int> entry in section. More...
 
static void CmdSets (int argc, char *argv[])
 Set or create key=<string> entry in section. More...
 
static int Tokenize (char *string, char *tokv[], size_t tokmax)
 Tokenize input. More...
 
static Cmd_TMatchCmd (const char *sPattern, Cmd_T tblCmds[])
 Match command. More...
 
int main (int argc, char *argv[])
 Example main. More...
 

Variables

static Config_TTheConfig = NULL
 the configuration db
 
static Cmd_T CmdTbl []
 The command set.
 

Detailed Description

Example of using the librnr config module.

LastChangedDate
2011-11-18 13:30:34 -0700 (Fri, 18 Nov 2011)
Rev
1577
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 example_config.c.

Macro Definition Documentation

#define CHKARGS (   argc,
  min 
)
Value:
{ if((argc-1) < min) \
{ \
fprintf(stderr, "Error: %d arguments required\n", min); \
return; \
} \
}

Check argument count against minimum required.

Parameters
argcArgument count.
minRequired minimum count.

Definition at line 77 of file example_config.c.

Referenced by CmdDbRead(), CmdDelete(), CmdFind(), CmdGetd(), CmdGeti(), CmdGets(), CmdLog(), CmdSectDelete(), CmdSectNew(), CmdSetd(), CmdSeti(), and CmdSets().

#define CHKCFG ( )
Value:
{ \
if(TheConfig == NULL) \
{ \
fprintf(stderr, "Error: configuration db does not exist\n"); \
return; \
} \
}
#define NULL
null pointer
Definition: rnrconfig.h:199
static Config_T * TheConfig
the configuration db

Check that the configuration db exists.

Definition at line 88 of file example_config.c.

Referenced by CmdDbDelete(), CmdDbPrint(), CmdDelete(), CmdFind(), CmdGetd(), CmdGeti(), CmdGets(), CmdSectDelete(), CmdSectNew(), CmdSetd(), CmdSeti(), and CmdSets().

#define CHKNOCFG ( )
Value:
{ \
if(TheConfig != NULL) \
{ \
fprintf(stderr, "Error: configuration %s exists - delete first\n", \
return; \
} \
}
#define NULL
null pointer
Definition: rnrconfig.h:199
static Config_T * TheConfig
the configuration db
const char * ConfigDbGetName(Config_T *pConfig)
Get the current name assigned to the configuration database.
Definition: config.c:486

Check that the configuration db does not exist.

Definition at line 100 of file example_config.c.

Referenced by CmdDbNew(), and CmdDbRead().

Function Documentation

static void CmdDbDelete ( int  argc,
char *  argv[] 
)
static

Delete configuration db.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 147 of file example_config.c.

References CHKCFG, ConfigDbDelete(), and NULL.

148 {
149  CHKCFG();
150 
152  TheConfig = NULL;
153 }
#define NULL
null pointer
Definition: rnrconfig.h:199
static Config_T * TheConfig
the configuration db
void ConfigDbDelete(Config_T *pConfig)
Delete the configuration database.
Definition: config.c:295
#define CHKCFG()
Check that the configuration db exists.
static void CmdDbNew ( int  argc,
char *  argv[] 
)
static

Create new, empty configuration db.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 161 of file example_config.c.

References CHKNOCFG, ConfigDbNew(), and NULL.

162 {
163  CHKNOCFG();
164 
166 }
#define NULL
null pointer
Definition: rnrconfig.h:199
Config_T * ConfigDbNew(const char *sMainName)
Create and initialize a new empty configuration database.
Definition: config.c:243
static Config_T * TheConfig
the configuration db
#define CHKNOCFG()
Check that the configuration db does not exist.
static void CmdDbPrint ( int  argc,
char *  argv[] 
)
static

Print out entire configuration db.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 174 of file example_config.c.

References CHKCFG, and ConfigDbPrint().

175 {
176  CHKCFG();
177 
178  ConfigDbPrint(TheConfig, stdout);
179 }
void ConfigDbPrint(Config_T *pConfig, FILE *fp)
Print configuration database to the output file stream.
Definition: config.c:514
static Config_T * TheConfig
the configuration db
#define CHKCFG()
Check that the configuration db exists.
static void CmdDbRead ( int  argc,
char *  argv[] 
)
static

Create new configuration db from file.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 187 of file example_config.c.

References CHKARGS, CHKNOCFG, and ConfigDbRead().

188 {
189  CHKARGS(argc, 1);
190  CHKNOCFG();
191 
192  TheConfig = ConfigDbRead(argv[1]);
193 }
static Config_T * TheConfig
the configuration db
#define CHKNOCFG()
Check that the configuration db does not exist.
#define CHKARGS(argc, min)
Check argument count against minimum required.
Config_T * ConfigDbRead(const char *sFileName)
Create a new configuration database from a configuration file.
Definition: config.c:326
static void CmdDelete ( int  argc,
char *  argv[] 
)
static

Delete configuration key.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 201 of file example_config.c.

References CHKARGS, CHKCFG, ConfigDbGetName(), ConfigDelete(), and OK.

202 {
203  CHKARGS(argc, 2);
204  CHKCFG();
205 
206  if( ConfigDelete(TheConfig, argv[1], argv[2]) != OK )
207  {
208  printf("Error: failed to delete\n");
209  }
210  else
211  {
212  printf("%s: [%s]: %s: deleted\n", ConfigDbGetName(TheConfig),
213  argv[1], argv[2]);
214  }
215 }
int ConfigDelete(Config_T *pConfig, const char *sSectionName, const char *sKey)
Delete entry from the configuration database.
Definition: config.c:906
#define OK
Okay.
Definition: rnrconfig.h:301
static Config_T * TheConfig
the configuration db
#define CHKARGS(argc, min)
Check argument count against minimum required.
const char * ConfigDbGetName(Config_T *pConfig)
Get the current name assigned to the configuration database.
Definition: config.c:486
#define CHKCFG()
Check that the configuration db exists.
static void CmdFind ( int  argc,
char *  argv[] 
)
static

Find configuration key=values matching substring.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 223 of file example_config.c.

References CHKARGS, CHKCFG, ConfigDbGetName(), ConfigDbIterNew(), ConfigGetStr(), ConfigIterDelete(), ConfigIterNext(), ConfigSectionIterNew(), and NULL.

224 {
225  char *sPat;
226  size_t lenPat;
227  ConfigIter_T *pIterSects;
228  ConfigIter_T *pIterKeys;
229  const char *sSectionName;
230  const char *sKey;
231  const char *sVal;
232 
233  CHKARGS(argc, 1);
234  CHKCFG();
235 
236  sPat = argv[1];
237  lenPat = strlen(sPat);
238 
239  pIterSects = ConfigDbIterNew(TheConfig);
240 
241  while( (sSectionName = ConfigIterNext(pIterSects)) != NULL )
242  {
243  pIterKeys = ConfigSectionIterNew(TheConfig, sSectionName);
244 
245  while( (sKey = ConfigIterNext(pIterKeys)) != NULL )
246  {
247  if( !strncmp(sPat, sKey, lenPat) )
248  {
249  sVal = ConfigGetStr(TheConfig, sSectionName, sKey);
250  printf("%s: [%s]: %s=%s\n",
251  ConfigDbGetName(TheConfig), sSectionName, sKey, sVal);
252  }
253  }
254 
255  ConfigIterDelete(pIterKeys);
256  }
257 
258  ConfigIterDelete(pIterSects);
259 }
void ConfigIterDelete(ConfigIter_T *pIter)
Delete a configuration iterator.
Definition: config.c:985
#define NULL
null pointer
Definition: rnrconfig.h:199
const char * ConfigGetStr(Config_T *pConfig, const char *sSectionName, const char *sKey)
Get a string value from the configuration database.
Definition: config.c:682
ConfigIter_T * ConfigDbIterNew(Config_T *pConfig)
Create a new configuration database iterator.
Definition: config.c:943
const char * ConfigIterNext(ConfigIter_T *pIter)
Get the next value in iteration.
Definition: config.c:1004
static Config_T * TheConfig
the configuration db
ConfigIter_T * ConfigSectionIterNew(Config_T *pConfig, const char *sSectionName)
Create a new configuration section iterator.
Definition: config.c:963
#define CHKARGS(argc, min)
Check argument count against minimum required.
const char * ConfigDbGetName(Config_T *pConfig)
Get the current name assigned to the configuration database.
Definition: config.c:486
#define CHKCFG()
Check that the configuration db exists.
static void CmdGetd ( int  argc,
char *  argv[] 
)
static

Get configuration value string converting to double.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 267 of file example_config.c.

References CHKARGS, CHKCFG, ConfigDbGetName(), ConfigGetDouble(), and OK.

268 {
269  double fVal;
270 
271  CHKARGS(argc, 2);
272  CHKCFG();
273 
274  if( ConfigGetDouble(TheConfig, argv[1], argv[2], &fVal) != OK )
275  {
276  printf("Error: failed to get/convert\n");
277  }
278  else
279  {
280  printf("%s: [%s]: %s=%f\n", ConfigDbGetName(TheConfig),
281  argv[1], argv[2], fVal);
282  }
283 }
#define OK
Okay.
Definition: rnrconfig.h:301
static Config_T * TheConfig
the configuration db
#define CHKARGS(argc, min)
Check argument count against minimum required.
int ConfigGetDouble(Config_T *pConfig, const char *sSectionName, const char *sKey, double *pVal)
Get and convert a double value from the configuration database.
Definition: config.c:759
const char * ConfigDbGetName(Config_T *pConfig)
Get the current name assigned to the configuration database.
Definition: config.c:486
#define CHKCFG()
Check that the configuration db exists.
static void CmdGeti ( int  argc,
char *  argv[] 
)
static

Get configuration value string converting to int.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 291 of file example_config.c.

References CHKARGS, CHKCFG, ConfigDbGetName(), ConfigGetInt(), and OK.

292 {
293  int iVal;
294 
295  CHKARGS(argc, 2);
296  CHKCFG();
297 
298  if( ConfigGetInt(TheConfig, argv[1], argv[2], &iVal) != OK )
299  {
300  printf("Error: failed to get/convert\n");
301  }
302  else
303  {
304  printf("%s: [%s]: %s=%d\n", ConfigDbGetName(TheConfig),
305  argv[1], argv[2], iVal);
306  }
307 }
#define OK
Okay.
Definition: rnrconfig.h:301
static Config_T * TheConfig
the configuration db
static int ConfigGetInt(Config_T *pConfig, const char *sSectionName, const char *sKey, int *pVal)
Get and convert an int value from the configuration database.
Definition: config.h:152
#define CHKARGS(argc, min)
Check argument count against minimum required.
const char * ConfigDbGetName(Config_T *pConfig)
Get the current name assigned to the configuration database.
Definition: config.c:486
#define CHKCFG()
Check that the configuration db exists.
static void CmdGets ( int  argc,
char *  argv[] 
)
static

Get configuration value string.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 315 of file example_config.c.

References CHKARGS, CHKCFG, ConfigDbGetName(), ConfigGetStr(), and NULL.

316 {
317  const char *sVal;
318 
319  CHKARGS(argc, 2);
320  CHKCFG();
321 
322  if( (sVal = ConfigGetStr(TheConfig, argv[1], argv[2])) == NULL )
323  {
324  printf("Error: failed to get\n");
325  }
326  else
327  {
328  printf("%s: [%s]: %s=%s\n", ConfigDbGetName(TheConfig),
329  argv[1], argv[2], sVal);
330  }
331 }
#define NULL
null pointer
Definition: rnrconfig.h:199
const char * ConfigGetStr(Config_T *pConfig, const char *sSectionName, const char *sKey)
Get a string value from the configuration database.
Definition: config.c:682
static Config_T * TheConfig
the configuration db
#define CHKARGS(argc, min)
Check argument count against minimum required.
const char * ConfigDbGetName(Config_T *pConfig)
Get the current name assigned to the configuration database.
Definition: config.c:486
#define CHKCFG()
Check that the configuration db exists.
static void CmdHelp ( int  argc,
char *  argv[] 
)
static

Print command help.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 524 of file example_config.c.

References Cmd_T::m_sCmdHelpArgs, Cmd_T::m_sCmdHelpDesc, Cmd_T::m_sCmdName, and NULL.

525 {
526  Cmd_T *pCmd;
527  int n;
528 
529  for(pCmd=CmdTbl; pCmd->m_sCmdName!=NULL; pCmd++)
530  {
531  n = printf("%s %s", pCmd->m_sCmdName, pCmd->m_sCmdHelpArgs);
532  printf("%*s - %s\n", 30-n, "", pCmd->m_sCmdHelpDesc);
533  }
534 }
const char * m_sCmdHelpArgs
command help arguments
#define NULL
null pointer
Definition: rnrconfig.h:199
const char * m_sCmdName
command name
command structure type
static Cmd_T CmdTbl[]
The command set.
const char * m_sCmdHelpDesc
command help description
static void CmdLog ( int  argc,
char *  argv[] 
)
static

Set diagnotics logging level.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 130 of file example_config.c.

References CHKARGS, and LOG_SET_THRESHOLD().

131 {
132  int n;
133 
134  CHKARGS(argc, 1);
135 
136  n = atoi(argv[1]);
137 
139 }
int LOG_SET_THRESHOLD(int nLevel)
Set new logging threshold level.
Definition: log.c:96
#define CHKARGS(argc, min)
Check argument count against minimum required.
static void CmdQuit ( int  argc,
char *  argv[] 
)
static

Quit this example program.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 119 of file example_config.c.

120 {
121  exit(0);
122 }
static void CmdSectDelete ( int  argc,
char *  argv[] 
)
static

Delete entier configuration db section.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 339 of file example_config.c.

References CHKARGS, CHKCFG, ConfigDbGetName(), ConfigSectionDelete(), ConfigSectionGet(), and NULL.

340 {
341  ConfigSection_T *pSect;
342 
343  CHKARGS(argc, 1);
344  CHKCFG();
345 
346  if( (pSect = ConfigSectionGet(TheConfig, argv[1])) == NULL )
347  {
348  printf("Error: failed to delete section\n");
349  }
350  else
351  {
353  printf("%s: [%s] deleted\n", ConfigDbGetName(TheConfig), argv[1]);
354  }
355 }
#define NULL
null pointer
Definition: rnrconfig.h:199
int ConfigSectionDelete(Config_T *pConfig, ConfigSection_T *pSect)
Delete a section from the configuration database.
Definition: config.c:600
static Config_T * TheConfig
the configuration db
ConfigSection_T * ConfigSectionGet(Config_T *pConfig, const char *sSectionName)
Get a section from the configuration database.
Definition: config.c:635
#define CHKARGS(argc, min)
Check argument count against minimum required.
const char * ConfigDbGetName(Config_T *pConfig)
Get the current name assigned to the configuration database.
Definition: config.c:486
#define CHKCFG()
Check that the configuration db exists.
static void CmdSectNew ( int  argc,
char *  argv[] 
)
static

Create new, empty configuration db section.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 363 of file example_config.c.

References CHKARGS, CHKCFG, ConfigDbGetName(), ConfigSectionNew(), and NULL.

364 {
365  CHKARGS(argc, 1);
366  CHKCFG();
367 
368  if( ConfigSectionNew(TheConfig, argv[1]) == NULL )
369  {
370  printf("Error: failed to add section\n");
371  }
372  else
373  {
374  printf("%s: [%s] added\n", ConfigDbGetName(TheConfig), argv[1]);
375  }
376 }
#define NULL
null pointer
Definition: rnrconfig.h:199
ConfigSection_T * ConfigSectionNew(Config_T *pConfig, const char *sSectionName)
Add a new empty section to the configuration database.
Definition: config.c:547
static Config_T * TheConfig
the configuration db
#define CHKARGS(argc, min)
Check argument count against minimum required.
const char * ConfigDbGetName(Config_T *pConfig)
Get the current name assigned to the configuration database.
Definition: config.c:486
#define CHKCFG()
Check that the configuration db exists.
static void CmdSetd ( int  argc,
char *  argv[] 
)
static

Set or create key=<double> entry in section.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 384 of file example_config.c.

References CHKARGS, CHKCFG, ConfigDbGetName(), ConfigSetDouble(), and OK.

385 {
386  double fVal;
387 
388  CHKARGS(argc, 3);
389  CHKCFG();
390 
391  fVal = atof(argv[3]);
392 
393  if( ConfigSetDouble(TheConfig, argv[1], argv[2], fVal) != OK )
394  {
395  printf("Error: failed to set\n");
396  }
397  else
398  {
399  printf("%s: [%s]: %s=%f\n", ConfigDbGetName(TheConfig),
400  argv[1], argv[2], fVal);
401  }
402 }
#define OK
Okay.
Definition: rnrconfig.h:301
static Config_T * TheConfig
the configuration db
int ConfigSetDouble(Config_T *pConfig, const char *sSectionName, const char *sKey, double fVal)
Set or update a double value in the configuration database.
Definition: config.c:883
#define CHKARGS(argc, min)
Check argument count against minimum required.
const char * ConfigDbGetName(Config_T *pConfig)
Get the current name assigned to the configuration database.
Definition: config.c:486
#define CHKCFG()
Check that the configuration db exists.
static void CmdSeti ( int  argc,
char *  argv[] 
)
static

Set or create key=<int> entry in section.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 410 of file example_config.c.

References CHKARGS, CHKCFG, ConfigDbGetName(), ConfigSetInt(), and OK.

411 {
412  int iVal;
413 
414  CHKARGS(argc, 3);
415  CHKCFG();
416 
417  iVal = atoi(argv[3]);
418 
419  if( ConfigSetInt(TheConfig, argv[1], argv[2], iVal) != OK )
420  {
421  printf("Error: failed to set\n");
422  }
423  else
424  {
425  printf("%s: [%s]: %s=%d\n", ConfigDbGetName(TheConfig),
426  argv[1], argv[2], iVal);
427  }
428 }
#define OK
Okay.
Definition: rnrconfig.h:301
static Config_T * TheConfig
the configuration db
static int ConfigSetInt(Config_T *pConfig, const char *sSectionName, const char *sKey, int iVal)
Set or update an int value in the configuration database.
Definition: config.h:226
#define CHKARGS(argc, min)
Check argument count against minimum required.
const char * ConfigDbGetName(Config_T *pConfig)
Get the current name assigned to the configuration database.
Definition: config.c:486
#define CHKCFG()
Check that the configuration db exists.
static void CmdSets ( int  argc,
char *  argv[] 
)
static

Set or create key=<string> entry in section.

Parameters
argcCommand argument count.
argvArray of arguments.

Definition at line 436 of file example_config.c.

References CHKARGS, CHKCFG, ConfigDbGetName(), ConfigSetStr(), and OK.

437 {
438  CHKARGS(argc, 3);
439  CHKCFG();
440 
441  if( ConfigSetStr(TheConfig, argv[1], argv[2], argv[3]) != OK )
442  {
443  printf("Error: failed to set\n");
444  }
445  else
446  {
447  printf("%s: [%s]: %s=%s\n", ConfigDbGetName(TheConfig),
448  argv[1], argv[2], argv[3]);
449  }
450 }
int ConfigSetStr(Config_T *pConfig, const char *sSectionName, const char *sKey, const char *sVal)
Set or update a string value in the configuration database.
Definition: config.c:809
#define OK
Okay.
Definition: rnrconfig.h:301
static Config_T * TheConfig
the configuration db
#define CHKARGS(argc, min)
Check argument count against minimum required.
const char * ConfigDbGetName(Config_T *pConfig)
Get the current name assigned to the configuration database.
Definition: config.c:486
#define CHKCFG()
Check that the configuration db exists.
int main ( int  argc,
char *  argv[] 
)

Example main.

Parameters
argcCommand-line argument count.
argvCommand-line argument list.
Exit Status:
Program exits with 0 success, > 0 on failure.

Definition at line 654 of file example_config.c.

References arraysize, Cmd_T::m_fnCmdExec, MatchCmd(), NULL, and Tokenize().

655 {
656  char input[256];
657  int tokc;
658  char *tokv[16];
659  Cmd_T *pCmd;
660 
661  printf(" Configuration Example\n");
662  printf("(enter 'help' for list of commands; "
663  "partial command matching supported)\n\n");
664 
665  // process user commands
666  for(;;)
667  {
668  printf("config> ");
669 
670  // read user input
671  if( !fgets(input, (int)sizeof(input), stdin) )
672  {
673  break;
674  }
675 
676  // tokenize input
677  tokc = Tokenize(input, tokv, arraysize(tokv));
678 
679  if( tokc <= 0 )
680  {
681  continue;
682  }
683 
684  // find command
685  pCmd = MatchCmd(tokv[0], CmdTbl);
686 
687  if( pCmd != NULL )
688  {
689  // execute command
690  pCmd->m_fnCmdExec(tokc, tokv);
691  }
692  }
693 
694  return 0;
695 }
#define NULL
null pointer
Definition: rnrconfig.h:199
#define arraysize(array)
array size, i.e. number of array entries
Definition: rnrconfig.h:259
static int Tokenize(char *string, char *tokv[], size_t tokmax)
Tokenize input.
CmdExec_T m_fnCmdExec
command execution function
static Cmd_T * MatchCmd(const char *sPattern, Cmd_T tblCmds[])
Match command.
command structure type
static Cmd_T CmdTbl[]
The command set.
static Cmd_T* MatchCmd ( const char *  sPattern,
Cmd_T  tblCmds[] 
)
static

Match command.

Parameters
sPattern(Partial) command string.
tblCmdsTable of commands.

Definition at line 581 of file example_config.c.

References Cmd_T::m_sCmdName, and NULL.

Referenced by main().

582 {
583  Cmd_T *p;
584  Cmd_T *pCmd = NULL;
585  size_t n;
586  bool_t bIsUnique = true;
587  int cnt;
588 
589  n = strlen(sPattern);
590 
591  // find command
592  for(p=tblCmds; p->m_sCmdName!=NULL; p++)
593  {
594  if( !strncmp(sPattern, p->m_sCmdName, n) )
595  {
596  pCmd = p;
597  break;
598  }
599  }
600 
601  // no matching command found
602  if( pCmd == NULL )
603  {
604  fprintf(stderr, "Error: %s: Command not found\n", sPattern);
605  return NULL;
606  }
607 
608  // verify uniqueness
609  for(p=tblCmds, cnt=0; p->m_sCmdName!=NULL; p++)
610  {
611  if( (p != pCmd) && !strncmp(sPattern, p->m_sCmdName, n) )
612  {
613  cnt++;
614 
615  // first non-unique
616  if( bIsUnique )
617  {
618  fprintf(stderr, "Error: %s: Command not unique: Matches %s %s ",
619  sPattern, pCmd->m_sCmdName, p->m_sCmdName);
620  bIsUnique = false;
621  }
622  // subsequent non-unique
623  else if( cnt < 3 )
624  {
625  fprintf(stderr, "%s ", p->m_sCmdName);
626  }
627  // too many to print
628  else
629  {
630  fprintf(stderr, "...");
631  break;
632  }
633  }
634  }
635 
636  if( !bIsUnique )
637  {
638  fprintf(stderr, "\n");
639  return NULL;
640  }
641 
642  return pCmd;
643 }
#define NULL
null pointer
Definition: rnrconfig.h:199
const char * m_sCmdName
command name
int bool_t
"boolean" T/F
Definition: rnrconfig.h:187
command structure type
static int Tokenize ( char *  string,
char *  tokv[],
size_t  tokmax 
)
static

Tokenize input.

Parameters
stringInput string
[out]tokvArray of tokens.
tokmaxMaximum number of tokens.
Returns
Number of tokens.

Definition at line 545 of file example_config.c.

Referenced by main().

546 {
547  int tokc = 0;
548 
549  while( tokc < tokmax )
550  {
551  while(*string && isspace((int)*string))
552  {
553  string++;
554  }
555  if(!*string)
556  {
557  break;
558  }
559  tokv[tokc] = string;
560  while(*string && !isspace((int)*string))
561  {
562  string++;
563  }
564  tokc++;
565  if( *string == 0 )
566  {
567  break;
568  }
569  *string++ = 0;
570  }
571 
572  return tokc;
573 }