BRL-CAD
cmd.c
Go to the documentation of this file.
1 /* C M D . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1998-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <string.h>
24 
25 #include "bu/cmd.h"
26 #include "bu/str.h"
27 
28 
29 int
30 bu_cmd(const struct bu_cmdtab *cmds, int argc, const char **argv, int cmd_index, void *data, int *retval)
31 {
32  const struct bu_cmdtab *ctp = NULL;
33 
34  /* sanity */
35  if (UNLIKELY(cmd_index >= argc)) {
36  bu_log("missing command; must be one of:");
37  goto missing_cmd;
38  }
39 
40  for (ctp = cmds; ctp->ct_name != (char *)NULL; ctp++) {
41  if (ctp->ct_name[0] == argv[cmd_index][0]
42  && BU_STR_EQUAL(ctp->ct_name, argv[cmd_index]))
43  {
44  if (retval) {
45  *retval = (*ctp->ct_func)(data, argc, argv);
46  } else {
47  (void)(*ctp->ct_func)(data, argc, argv);
48  }
49  return BRLCAD_OK;
50  }
51  }
52 
53  bu_log("unknown command: %s; must be one of: ", argv[cmd_index]);
54 
55 missing_cmd:
56  for (ctp = cmds; ctp->ct_name != (char *)NULL; ctp++) {
57  bu_log(" %s", ctp->ct_name);
58  }
59  bu_log("\n");
60 
61  return BRLCAD_ERROR;
62 }
63 
64 
65 /*
66  * Local Variables:
67  * mode: C
68  * tab-width: 8
69  * indent-tabs-mode: t
70  * c-file-style: "stroustrup"
71  * End:
72  * ex: shiftwidth=4 tabstop=8
73  */
Definition: cmd.h:48
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
Header file for the BRL-CAD common definitions.
COMPLEX data[64]
Definition: fftest.c:34
#define BRLCAD_OK
Definition: defines.h:71
const char * ct_name
Definition: cmd.h:49
int(* ct_func)(void *data, int argc, const char *argv[])
Definition: cmd.h:50
int bu_cmd(const struct bu_cmdtab *cmds, int argc, const char **argv, int cmd_index, void *data, int *retval)
Definition: cmd.c:30
#define BRLCAD_ERROR
Definition: defines.h:72
#define UNLIKELY(expression)
Definition: common.h:282
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126