2018-11-23 13:13:23 +01:00
|
|
|
/* SPDX-License-Identifier: ISC */
|
2018-02-25 14:33:19 +01:00
|
|
|
#ifndef SERVICECMD_H
|
|
|
|
#define SERVICECMD_H
|
|
|
|
|
2020-03-31 18:19:27 +02:00
|
|
|
#include <stdbool.h>
|
2018-02-25 14:33:19 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2019-03-19 23:52:28 +01:00
|
|
|
#include "service.h"
|
2020-03-31 18:19:27 +02:00
|
|
|
#include "config.h"
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2018-03-24 22:31:05 +01:00
|
|
|
/*
|
|
|
|
Describes a command that can be launched by passing its name as
|
|
|
|
second command line argument to the main() function (i.e. immediately
|
|
|
|
after the actual program name).
|
|
|
|
|
|
|
|
Short and long descriptions can be provided to print out help text.
|
|
|
|
|
|
|
|
The main() function calls into a callback in this structure to execute
|
|
|
|
the command.
|
|
|
|
*/
|
2018-02-25 14:33:19 +01:00
|
|
|
typedef struct command_t {
|
|
|
|
struct command_t *next;
|
|
|
|
|
2018-03-24 22:31:05 +01:00
|
|
|
const char *cmd; /* command name */
|
|
|
|
const char *usage; /* list of possible arguments */
|
|
|
|
const char *s_desc; /* short description used by help */
|
|
|
|
const char *l_desc; /* long description used by help */
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2018-03-24 22:31:05 +01:00
|
|
|
/*
|
|
|
|
Semantics are the same as for main(). Called from main()
|
|
|
|
function with first argument (i.e. top level program name)
|
|
|
|
removed.
|
|
|
|
*/
|
2018-02-25 14:33:19 +01:00
|
|
|
int (*run_cmd)(int argc, char **argv);
|
|
|
|
} command_t;
|
|
|
|
|
2018-03-24 22:31:05 +01:00
|
|
|
/* Global list of available commands */
|
2018-02-25 14:33:19 +01:00
|
|
|
extern command_t *commands;
|
|
|
|
|
2020-04-24 12:24:19 +02:00
|
|
|
service_t *loadsvc(const char *directory, const char *filename);
|
2019-03-19 23:52:28 +01:00
|
|
|
|
2018-03-24 22:31:05 +01:00
|
|
|
/*
|
|
|
|
Implemented in servicecmd.c. Prints program usage message and
|
|
|
|
terminates with the given exit status.
|
|
|
|
*/
|
2020-03-31 18:19:27 +02:00
|
|
|
void usage(int status) __attribute__((noreturn));
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2018-03-25 01:59:38 +01:00
|
|
|
/*
|
|
|
|
Write a message to stderr that advises the user how to consult the
|
|
|
|
help text for a specific command.
|
|
|
|
*/
|
|
|
|
void tell_read_help(const char *cmd);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Check if the argument count is within specified bounds (minc and maxc
|
|
|
|
inclusive). If it is, return 0.
|
|
|
|
|
|
|
|
If it isn't, complain about a wrong number of arguments for a
|
|
|
|
command (cmd), tell the user to consult the help text and return -1.
|
|
|
|
*/
|
|
|
|
int check_arguments(const char *cmd, int argc, int minc, int maxc);
|
|
|
|
|
2018-03-24 22:31:05 +01:00
|
|
|
/*
|
|
|
|
To implement a new command, add a global, static instance of a
|
|
|
|
command_t (or derived) structure to a C file and pass it to this
|
|
|
|
macro to have it automatically registered on program startup.
|
|
|
|
*/
|
2018-02-25 14:33:19 +01:00
|
|
|
#define REGISTER_COMMAND(cmd) \
|
|
|
|
static void __attribute__((constructor)) register_##cmd(void) \
|
|
|
|
{ \
|
|
|
|
command_t *c = (command_t *)&cmd; \
|
|
|
|
\
|
|
|
|
c->next = commands; \
|
|
|
|
commands = c; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* SERVICECMD_H */
|