]> git.draconx.ca Git - slotifier.git/blobdiff - src/slotifier.c
Trivial manual fixes.
[slotifier.git] / src / slotifier.c
index 19865c09c571882a5bc93eb80c6ac5e5c8e0baac..09e5aeecce281f894bca42b8c87630c37eb8c6ff 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Utility to convert overlapping Excellon drill hits into drill slots.
- * Copyright © 2018, 2021 Nick Bowler
+ * Copyright © 2018, 2021, 2023 Nick Bowler
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdint.h>
 #include <assert.h>
 #include <locale.h>
 
 #include <gettext.h>
 #include <localcharset.h>
 #include <mbswidth.h>
-#include <striconv.h>
 
 #include <CNearTree.h>
 #include <gerbv.h>
 
+#include "options.h"
+#include "help.h"
+#include "xtra.h"
+#include "copysym.h"
+
 #if !ENABLE_NLS
 #  undef ENABLE_NLS
 #  define ENABLE_NLS 0
 static const char *progname = "slotifier";
 static unsigned verbose;
 
-#include "options.h"
-static const char sopts[] = SOPT_STRING;
-static const struct option lopts[] = {
-       LOPTS_INITIALIZER,
-       {0}
-};
-
 static void print_version(void)
 {
-       const char *copysign = "(C)";
-       void *convsign = NULL;
+       const char *copysign = copyright_symbol(locale_charset());
 
        puts(PACKAGE_STRING);
-
-       if (ENABLE_NLS) {
-               convsign = str_iconv("\xc2\xa9", "UTF-8", locale_charset());
-               if (convsign)
-                       copysign = convsign;
-       }
-
-       printf("Copyright %s 2021 Nick Bowler.\n", copysign);
+       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)
@@ -78,66 +66,7 @@ static void print_usage(FILE *f)
                           progname);
 }
 
-static int
-print_optstring(const struct option *opt, const struct lopt_help *help)
-{
-       char optstring[100];
-       int w;
-
-       if (!ENABLE_NLS)
-               goto no_translate;
-
-       if (opt->has_arg) {
-               w = snprintf(optstring, sizeof optstring,
-                            _("  -%c, --%s=%s"), opt->val, opt->name,
-                            pgettext_expr(opt->name, help->arg));
-       } else {
-               w = snprintf(optstring, sizeof optstring,
-                            _("  -%c, --%s"), opt->val, opt->name);
-       }
-
-       if (w < 0)
-               goto no_translate;
-
-       w = mbsnwidth(optstring, w, 0);
-       printf("%s", optstring);
-       goto out;
-
-no_translate:
-       if (opt->has_arg) {
-               w = printf("  -%c, --%s=%s", opt->val, opt->name, help->arg);
-       } else {
-               w = printf("  -%c, --%s", opt->val, opt->name);
-       }
-out:
-       if (w < 0 || w > 18) {
-               putchar('\n');
-               return 0;
-       }
-
-       return w;
-}
-
-/*
- * Print a string, with each line indented by i spaces.  The first line
- * will be indented by w fewer spaces (to account for the cursor being in
- * some other column).
- */
-static void print_block(const char *s, int i, int w)
-{
-       for (; *s; w = 0) {
-               const char *nl = strchr(s, '\n');
-               int n = (nl ? nl-s : -1);
-
-               printf("%*s%.*s\n", i-w, "", n, s);
-               if (!nl)
-                       break;
-
-               s = nl+1;
-       }
-}
-
-static void print_help(void)
+static void print_help(const struct option *lopts)
 {
        const struct option *opt;
 
@@ -150,17 +79,11 @@ static void print_help(void)
        puts(_("Options:"));
        for (opt = lopts; opt->val; opt++) {
                struct lopt_help help;
-               int w;
 
                if (!lopt_get_help(opt, &help))
                        continue;
 
-               w = print_optstring(opt, &help);
-
-               if (ENABLE_NLS)
-                       help.desc = pgettext_expr(opt->name, help.desc);
-
-               print_block(help.desc, 20, w);
+               help_print_option(opt, help.arg, help.desc, 20);
        }
        putchar('\n');
 
@@ -460,35 +383,33 @@ err_free_tree:
        return ret;
 }
 
-int main(int argc, char **argv)
+static int do_cmdline(int argc, char **argv, const char **outfile)
 {
-       const char *outfile = "/dev/stdout";
-       gerbv_project_t *gp;
-       gerbv_image_t *drill;
-       int opt, ret = 0;
+       const char *sopts = SOPT_STRING;
+       int opt;
+
+       XTRA_PACKED_LOPTS(lopts);
 
        if (argc > 0)
                progname = argv[0];
 
-       init_i18n();
-
        while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
                switch (opt) {
                case 'o':
-                       outfile = optarg;
+                       *outfile = optarg;
                        break;
                case 'v':
                        verbose++;
                        break;
                case 'V':
                        print_version();
-                       return EXIT_SUCCESS;
+                       return 1;
                case 'H':
-                       print_help();
-                       return EXIT_SUCCESS;
+                       print_help(lopts);
+                       return 1;
                default:
                        print_usage(stderr);
-                       return EXIT_FAILURE;
+                       return -1;
                }
        }
 
@@ -496,14 +417,30 @@ int main(int argc, char **argv)
                fprintf(stderr, _("%s: error: must specify a filename\n"),
                                progname);
                print_usage(stderr);
-               return EXIT_FAILURE;
+               return -1;
        }
 
        if (optind + 1 < argc) {
                fprintf(stderr, _("%s: error: excess command-line arguments\n"),
                                progname);
                print_usage(stderr);
-               return EXIT_FAILURE;
+               return -1;
+       }
+
+       return 0;
+}
+
+int main(int argc, char **argv)
+{
+       const char *outfile = "/dev/stdout";
+       gerbv_project_t *gp;
+       gerbv_image_t *drill;
+       int ret = 0;
+
+       init_i18n();
+       switch (do_cmdline(argc, argv, &outfile)) {
+       case -1: return EXIT_FAILURE;
+       case 1: return EXIT_SUCCESS;
        }
 
        gp = gerbv_create_project();