#include <config.h>
#include <errno.h>
#include <error.h>
#include <getopt.h>
#include <wchar.h>
#include "system.h"
#include "closeout.h"
#include "configmake.h"
#include "dirname.h"
#include "gettext.h"
#include "progname.h"
#include "propername.h"
#include "version-etc.h"
#include "xalloc.h"
#define PROGRAM_NAME "hello"
#define AUTHORS \
proper_name ("Karl Berry"), \
proper_name ("Sami Kerola"), \
proper_name ("Jim Meyering"), \
proper_name ("Reuben Thomas")
static _Noreturn void
print_help (FILE *restrict out)
{
const char *lc_messages = setlocale (LC_MESSAGES, NULL);
fprintf (out, _("Usage: %s [OPTION]...\n"), program_name);
fputs (_("Print a friendly, customizable greeting.\n"), out);
fputs ("\n", out);
fputs (_(" -t, --traditional use traditional greeting\n"), out);
fputs (_(" -g, --greeting=TEXT use TEXT as the greeting message\n"),
out);
fputs ("\n", out);
fputs (_(" --help display this help and exit\n"), out);
fputs (_(" --version output version information and exit\n"), out);
emit_bug_reporting_address ();
if (lc_messages && STRNCMP_LIT (lc_messages, "en_"))
{
fprintf (out, _("Report %s translation bugs to "
"<https://translationproject.org/team/>\n"),
PACKAGE_NAME);
}
exit (out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
static void
parse_options (int argc, char *argv[], const char **greeting_msg)
{
int optc;
int lose = 0;
enum { OPT_HELP = CHAR_MAX + 1, OPT_VERSION };
static const struct option longopts[] = {
{"greeting", required_argument, NULL, 'g'},
{"traditional", no_argument, NULL, 't'},
{"help", no_argument, NULL, OPT_HELP},
{"version", no_argument, NULL, OPT_VERSION},
{NULL, 0, NULL, 0}
};
while ((optc = getopt_long (argc, argv, "g:t", longopts, NULL)) != -1)
switch (optc)
{
case OPT_VERSION:
version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, PACKAGE_VERSION,
AUTHORS, (char *) NULL);
exit (EXIT_SUCCESS);
case 'g':
*greeting_msg = optarg;
break;
case OPT_HELP:
print_help (stdout);
case 't':
*greeting_msg = _("hello, world");
break;
default:
lose = 1;
break;
}
if (lose || optind < argc)
{
if (argv[optind])
error (0, 0, "%s: %s", _("extra operand"), argv[optind]);
emit_try_help ();
exit (EXIT_FAILURE);
}
}
int
main (int argc, char *argv[])
{
const char *greeting_msg;
wchar_t *mb_greeting;
mbstate_t mbstate; mbszero (&mbstate);
size_t len;
set_program_name (argv[0]);
setlocale (LC_ALL, "");
#if ENABLE_NLS
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
#endif
greeting_msg = _("Hello, world!");
atexit (close_stdout);
parse_options (argc, argv, &greeting_msg);
len = strlen (greeting_msg) + 1;
mb_greeting = xmalloc (len * sizeof (wchar_t));
len = mbsrtowcs (mb_greeting, &greeting_msg, len, &mbstate);
if (len == (size_t) -1)
error (EXIT_FAILURE, errno, _("conversion to a multibyte string failed"));
wprintf (L"%ls\n", mb_greeting);
free (mb_greeting);
exit (EXIT_SUCCESS);
}