summaryrefslogtreecommitdiff
path: root/src/shared/main-func.h
blob: 24bf6c99bfad1a5878c449c6841cc7a1edba95ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once

#include <stdlib.h>

#include "pager.h"
#include "static-destruct.h"

#define _DEFINE_MAIN_FUNCTION(impl, ret)                                \
        int main(int argc, char *argv[]) {                              \
                int r;                                                  \
                r = impl(argc, argv);                                   \
                static_destruct();                                      \
                pager_close();                                          \
                return ret;                                             \
        }

/* Negative return values from impl are mapped to EXIT_FAILURE, and
 * everything else means success! */
#define DEFINE_MAIN_FUNCTION(impl)                                      \
        _DEFINE_MAIN_FUNCTION(impl, r < 0 ? EXIT_FAILURE : EXIT_SUCCESS)

/* Zero is mapped to EXIT_SUCCESS, negative values are mapped to EXIT_FAILURE,
 * and postive values are propagated.
 * Note: "true" means failure! */
#define DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(impl)                \
        _DEFINE_MAIN_FUNCTION(impl, r < 0 ? EXIT_FAILURE : r)