Merge branch 'maint'
[paraslash.git] / ggo.c
1 /*
2  * Copyright (C) 2008 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file ggo.c Function for printing help. */
8
9
10 #include "para.h"
11 #include "ggo.h"
12 #include "version.h"
13
14 /**
15  * Wrapper for printf() that exits on errors.
16  *
17  * \param fmt Usual format string.
18  *
19  * \return The return value of the underlying (successful) call to vprintf(3),
20  * i.e. the number of characters printed, excluding the terminating null byte.
21  */
22 __printf_1_2 int printf_or_die(const char *fmt, ...)
23 {
24         va_list argp;
25         int ret;
26
27         va_start(argp, fmt);
28         ret = vprintf(fmt, argp);
29         va_end(argp);
30         if (ret >= 0)
31                 return ret;
32         exit(EXIT_FAILURE);
33 }
34
35 /**
36  * Print one of the two given help texts.
37  *
38  * \param help contains the help texts.
39  * \param flags What to print, see \ref ggo_print_help_flags.
40  */
41 void ggo_print_help(struct ggo_help *help, unsigned flags)
42 {
43         const char **p;
44
45         if (help->purpose && (flags & GPH_PRINT_NAME_PURPOSE))
46                 printf_or_die("para_%s - %s\n", help->prefix, help->purpose);
47         if (help->usage && (flags & GPH_PRINT_USAGE))
48                 printf_or_die("\n%s\n", help->usage);
49         if (help->description && (flags & GPH_PRINT_DESCRIPTION))
50                 printf_or_die("\n%s\n", help->description);
51         printf_or_die("\n");
52         if (flags & GPH_DETAILED)
53                 p = help->detailed_help;
54         else
55                 p = help->short_help;
56         if (!p)
57                 return;
58         for (; *p; p++)
59                 printf_or_die("%s\n", *p);
60 }