BRL-CAD
Loading...
Searching...
No Matches
cmd.h
Go to the documentation of this file.
1/* C M D . H
2 * BRL-CAD
3 *
4 * Copyright (c) 1993-2025 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/** @addtogroup bu_cmd
22 * @brief
23 * Routine(s) for processing subcommands
24 */
25/** @{ */
26/** @file bu/cmd.h */
27
28#ifndef BU_CMD_H
29#define BU_CMD_H
30
31#include "common.h"
32
33#include <time.h>
34
35#include "bsocket.h" /* for timeval */
36
37#define BU_CMD_NULL (int (*)(void *, int, const char **))NULL
38
39#include "bu/defines.h"
40#include "bu/log.h"
41#include "bu/vls.h"
42
43/**
44 * Generic keyword-to-command callback interface intended for use with
45 * bu_cmd().
46 */
47struct bu_cmdtab {
48 const char *ct_name;
49 int (*ct_func)(void *data, int argc, const char *argv[]);
50};
51
53
54/** @brief Routine(s) for processing subcommands */
55
56/**
57 * This function is intended to be used for parsing subcommands. If
58 * the command is found in the array of commands, the associated
59 * function is called. Otherwise, an error message is printed along
60 * with a list of available commands. This behavior can be suppressed
61 * by registering a bu_log() callback.
62 *
63 * @code
64 * struct bu_hook_list saved_hooks = BU_HOOK_LIST_INIT_ZERO;
65 * bu_log_hook_save_all(&saved_hooks);
66 * bu_log_hook_delete_all();
67 * bu_log_add_hook(NULL, NULL); // disables logging
68 * bu_cmd(...);
69 * bu_log_hook_restore_all(&saved_hooks);
70 * @endcode
71 *
72 * @param cmds - commands and related function pointers
73 * @param argc - number of arguments in argv
74 * @param argv - command to execute and its arguments
75 * @param cmd_index - indicates which argv element holds the subcommand
76 * @param data - data/state associated with the command
77 * @param result - if non-NULL, return value from the command
78 *
79 * @return BRLCAD_OK if command was found, otherwise, BRLCAD_ERROR.
80 */
81BU_EXPORT extern int bu_cmd(const struct bu_cmdtab *cmds, int argc, const char *argv[], int cmd_index, void *data, int *result);
82
83/**
84 * Returns BRLCAD_OK if cmdname defines a command in the cmds table, and BRLCAD_ERROR otherwise */
85BU_EXPORT extern int bu_cmd_valid(const struct bu_cmdtab *cmds, const char *cmdname);
86
88
89#endif /* BU_CMD_H */
90
91/** @} */
92/*
93 * Local Variables:
94 * mode: C
95 * tab-width: 8
96 * indent-tabs-mode: t
97 * c-file-style: "stroustrup"
98 * End:
99 * ex: shiftwidth=4 tabstop=8
100 */
Definition dvec.h:74
Header file for the BRL-CAD common definitions.
int bu_cmd_valid(const struct bu_cmdtab *cmds, const char *cmdname)
int bu_cmd(const struct bu_cmdtab *cmds, int argc, const char *argv[], int cmd_index, void *data, int *result)
Routine(s) for processing subcommands.
const char * ct_name
Definition cmd.h:48
int(* ct_func)(void *data, int argc, const char *argv[])
Definition cmd.h:49