aboutsummaryrefslogtreecommitdiffstats
path: root/src/command.h
diff options
context:
space:
mode:
authorLibravatar bigfoot547 <[email protected]>2023-12-26 22:33:51 -0600
committerLibravatar bigfoot547 <[email protected]>2023-12-26 22:33:51 -0600
commit7f4b7142b09205dc773a915e3fe3cb7954f6d041 (patch)
tree34fed74ba2f3715ce2a6a6922cfbd440882b2895 /src/command.h
parentimplement remaining commands (but badly) (diff)
refactor instances
Diffstat (limited to 'src/command.h')
-rw-r--r--src/command.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/command.h b/src/command.h
index 469422c..7709292 100644
--- a/src/command.h
+++ b/src/command.h
@@ -3,6 +3,11 @@
#include <stddef.h>
+/* for CMD_ macros */
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
enum {
CMD_NODE_TYPE_NULL,
CMD_NODE_TYPE_LITERAL,
@@ -32,6 +37,29 @@ enum {
CMD_PARSE_EUSAGE /* function was called with NULL arguments */
};
+#define CMD_FATAL_(_op) do { \
+ _op; \
+ exit(EXIT_FAILURE); \
+} while (0)
+
+#define CMD_MSG(_l, _s, ...) fprintf(stderr, _l ": " _s "\n", __VA_ARGS__)
+#define CMD_MSG0(_l, _s) fputs(_l ": " _s "\n", stderr);
+
+#define CMD_FATAL(_s, ...) CMD_FATAL_(CMD_MSG("fatal", _s, __VA_ARGS__))
+#define CMD_FATAL0(_s) CMD_FATAL_(CMD_MSG0("fatal", _s))
+
+#define CMD_ERROR(_s, ...) CMD_MSG("error", _s, __VA_ARGS__)
+#define CMD_ERROR0(_s) CMD_MSG0("error", _s)
+
+#define CMD_WARN(_s, ...) CMD_MSG("warn", _s, __VA_ARGS__)
+#define CMD_WARN0(_s) CMD_MSG0("warn", _s)
+
+#define CMD_MSG_UNKNOWN_ARGUMENT "Unknown argument '%s'."
+#define CMD_MSG_REQ_ARG_UNSPECIFIED "Required argument '%s' not specified."
+#define CMD_MSG_INVALID_UUID "Invalid UUID '%s'."
+
+#define CMD_MSG_ALLOCATION_FAILED "Allocation failed."
+
struct l2_command_node;
struct l2_context_node {
@@ -74,4 +102,18 @@ struct l2_parseinfo {
unsigned l2_cmd_parse_command(char **argv, struct l2_parseinfo *parseinfo);
void l2_cmd_free_ctx(struct l2_context_node *ctx);
+/* DESCRIPTION:
+ * This function is for unpacking a bunch of arguments from a parsed command into
+ * string variables.
+ *
+ * HOW TO USE:
+ * Pass your command context followed by your arguments (const char *name, char **pvalue).
+ * Prefix the name with "#" if it is optional.
+ *
+ * This function will exit if a required value was not present.
+ *
+ * Example: l2_cmd_collect_args(ctx, 2, "name", &name, "#location", &path);
+ */
+void l2_cmd_collect_args(struct l2_context_node *ctx, unsigned argc, ...);
+
#endif /* include guard */