+#include "help.h"
+
+static const char *progname = "cdecl99";
+static bool interactive = true;
+
+#include "options.h"
+static const char sopts[] = SOPT_STRING;
+static const struct option lopts[] = {
+ LOPTS_INITIALIZER,
+ {0}
+};
+
+void print_error(const char *fmt, ...)
+{
+ va_list(ap);
+
+ if (!interactive)
+ fprintf(stderr, "%s: ", progname);
+ fprintf(stderr, "%s", _("error: "));
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+
+ fprintf(stderr, "\n");
+}
+
+static void print_version(void)
+{
+ const char *copysign = "(C)";
+ void *convsign = NULL;
+
+ if (ENABLE_NLS) {
+ convsign = str_iconv("\xc2\xa9", "UTF-8", locale_charset());
+ if (convsign)
+ copysign = convsign;
+ }
+
+ puts(PACKAGE_STRING);
+ printf("Copyright %s 2023 Nick Bowler.\n", copysign);
+ puts("License GPLv3+: GNU GPL version 3 or any later version.");
+ puts("This is free software: you are free to change and redistribute it.");
+ puts("There is NO WARRANTY, to the extent permitted by law.");
+
+ free(convsign);
+}
+
+static void print_usage(FILE *f)
+{
+ fprintf(f, _("Usage: %s [options]\n"), progname);
+ if (f != stdout)
+ fprintf(f, _("Try %s --help for more information.\n"),
+ progname);
+}
+
+static void print_help(void)
+{
+ const struct option *opt;
+
+ print_usage(stdout);
+
+ puts(_("This is \"cdecl99\": a command-line tool for parsing and constructing\n"
+ "complicated C declarations."));
+ putchar('\n');
+
+ puts(_("Options:"));
+ for (opt = lopts; opt->name; opt++) {
+ struct lopt_help help;
+
+ if (!lopt_get_help(opt, &help))
+ continue;
+
+ help_print_option(opt, help.arg, help.desc, 20);
+ }
+ putchar('\n');
+
+ puts(_("For more information, see the cdecl99(1) man page."));
+ putchar('\n');
+
+ /*
+ * TRANSLATORS: Please add *another line* indicating where users should
+ * report translation bugs.
+ */
+ printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
+}
+
+static bool is_blank_line(const char *line)
+{
+ for (size_t i = 0; line[i]; i++) {
+ if (!isblank((unsigned char)line[i]))
+ return false;
+ }
+
+ return true;
+}
+
+static int repl(void)
+{
+ char *line;
+
+ for (; (line = readline("> ")); free(line)) {
+ if (!is_blank_line(line))
+ add_history(line);
+
+ if (run_command(line, true) > 0)
+ break;
+ }
+
+ free(line);
+ return 0;
+}
+
+static int repl_cmdline(unsigned count, char **commands)
+{
+ int ret = 0;
+ unsigned i;
+
+ for (i = 0; i < count; i++) {
+ int rc = run_command(commands[i], false);
+ if (rc < 0)
+ ret = -1;
+ else if (rc > 0)
+ break;
+ }
+
+ return ret;
+}
+
+static int repl_noninteractive(void)
+{
+ int rc, ret = 0, saved_errno;
+ char *line = NULL;
+ size_t n;
+
+ while (getline(&line, &n, stdin) >= 0) {
+ char *c = strchr(line, '\n');
+ if (c)
+ *c = '\0';
+
+ rc = run_command(line, false);
+ if (rc < 0)
+ ret = -1;
+ else if (rc > 0)
+ break;
+ }
+
+ saved_errno = errno;
+ free(line);
+
+ if (ferror(stdin)) {
+ print_error("%s", strerror(saved_errno));
+ return -1;
+ }
+
+ return ret;
+}
+
+/* Initialize gettext */
+static void init_i18n(void)
+{
+ if (!ENABLE_NLS)
+ return;
+
+ setlocale(LC_ALL, "");
+ bindtextdomain(PACKAGE, LOCALEDIR);
+ textdomain(PACKAGE);
+}