Serwer/help.c

78 lines
1.5 KiB
C
Raw Permalink Normal View History

2017-01-01 23:03:06 +00:00
/**
* @file help.c
* @author overflowerror
* @date 2016.11.27
* @brief code for the help functions
*
* This file contains the code for this lib.
*/
#include "help.h"
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <stdarg.h>
2017-01-01 23:03:06 +00:00
/**
* @see help.h
*/
const char* progname = "";
/**
* @brief the free function
*
* This function gets executes if bail_out() or usage() are called.
*/
static free_t freeFunction = NULL;
void help_init(free_t function, const char* name) {
freeFunction = function;
progname = name;
2017-01-01 23:03:06 +00:00
}
void bail_out(int exitcode, const char* format, ...) {
(void) fprintf(stderr, "%s: ", progname);
2017-01-01 23:03:06 +00:00
va_list arg;
va_start(arg, format);
(void) vfprintf(stderr, format, arg);
va_end(arg);
2017-01-01 23:03:06 +00:00
(void) fprintf(stderr, "\n");
2017-01-01 23:03:06 +00:00
if (freeFunction != NULL)
(*freeFunction)();
exit(exitcode);
2017-01-01 23:03:06 +00:00
}
void usage(const char* arguments, const char* description, const char* error, int exitcode) {
if (error != NULL) {
(void) fprintf(stderr, "%s\n\n", error);
}
(void) fprintf(stderr, "Usage: %s %s\n", progname, arguments);
if (description != NULL) {
(void) fprintf(stderr, "\n%s\n", description);
}
2017-01-01 23:03:06 +00:00
if (freeFunction != NULL)
(*freeFunction)();
2017-01-01 23:03:06 +00:00
exit(exitcode);
2017-01-01 23:03:06 +00:00
}
void setup_signal_handler(const int signal, sighandler_t handler) {
struct sigaction s;
2017-01-01 23:03:06 +00:00
s.sa_handler = handler;
s.sa_flags = 0;
if(sigfillset(&s.sa_mask) < 0) {
bail_out(EXIT_FAILURE, "sigfillset");
}
if (sigaction(signal, &s, NULL) < 0) {
bail_out(EXIT_FAILURE, "sigaction");
}
2017-01-01 23:03:06 +00:00
}