]> git.draconx.ca Git - cdecl99.git/commitdiff
cdecl99: Optimize print_version a bit.
authorNick Bowler <nbowler@draconx.ca>
Wed, 13 Dec 2023 03:01:31 +0000 (22:01 -0500)
committerNick Bowler <nbowler@draconx.ca>
Wed, 13 Dec 2023 03:01:31 +0000 (22:01 -0500)
Instead of a whole bunch of separate print calls, convert print_version
to a single call to a printing function, with a single string argument.
Compilers do not seem to coalesce multiple consecutive prints together,
so doing it explicitly gives a modest reduction in code size.

Wrap that all up into a new set of macros so that the same trick can
also be shared with the test applications.

Makefile.am
src/cdecl99.c
src/version.h [new file with mode: 0644]
t/rendertest.c
t/test.h
t/testlib.c

index 6888d83155b2fde04343cb42f62d0de5ff84dee2..90818f5f496d788de219bb38cea9d6b38599ac73 100644 (file)
@@ -32,7 +32,7 @@ dist_man_MANS = doc/cdecl99.1 doc/libcdecl.3
 
 noinst_HEADERS = conf_pre.h conf_post.h common/src/help.h common/src/tap.h \
                  common/src/xtra.h src/cdecl.h src/scan.h src/parse.h \
-                 t/declgen.h t/test.h
+                 src/version.h t/declgen.h t/test.h
 
 noinst_DATA = $(MOFILES)
 
index 6eb00df53a57b934e06ee353a0813af17d607b08..5f60037f78e23018ee4d118006f89b0579c08611 100644 (file)
 
 #include <getopt.h>
 #include <gettext.h>
-#include <localcharset.h>
 #include <mbswidth.h>
 
 #include "cdecl99.h"
 #include "cdecl.h"
 #include "help.h"
 #include "xtra.h"
-#include "copysym.h"
 #include "options.h"
+#include "version.h"
 
 #if HAVE_READLINE_READLINE_H
 #  include <readline/readline.h>
@@ -77,13 +76,7 @@ void print_error(const char *fmt, ...)
 
 static void print_version(void)
 {
-       const char *copysign = copyright_symbol(locale_charset());
-
-       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.");
+       do_print_version(PACKAGE_STRING);
 }
 
 static void print_usage(FILE *f)
diff --git a/src/version.h b/src/version.h
new file mode 100644 (file)
index 0000000..9d41d11
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright © 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef CDECL99_VERSION_H_
+#define CDECL99_VERSION_H_
+
+#define version_blurb(s, copyright) \
+       s "\n" \
+       copyright "2023 Nick Bowler\n" \
+       "License GPLv3+: GNU GPL version 3 or any later version.\n" \
+       "This is free software: you are free to change and redistribute it.\n" \
+       "There is NO WARRANTY, to the extent permitted by law." \
+
+#if ENABLE_NLS && !VERSION_NO_COPYRIGHT_SYMBOL
+#include <localcharset.h>
+#include "copysym.h"
+
+#define do_print_version(s) do { \
+       printf(version_blurb(s, "Copyright %s ")"\n", \
+               copyright_symbol(locale_charset())); \
+} while (0)
+#else
+#define do_print_version(s) do { \
+       puts(version_blurb(s, "Copyright (C) ")); \
+} while (0)
+#endif
+
+#endif
index 14faafb28990544f38645ccf24500120334efbac..e5299c292fc662f842c03113b232dfa0dbc95d90 100644 (file)
@@ -26,7 +26,7 @@
 #include <cdecl.h>
 #include "test.h"
 
-#define PROGNAME "outbuf"
+#define PROGNAME "rendertest"
 static const char *progname = PROGNAME;
 static const char sopts[] = "n:ECVH";
 static const struct option lopts[] = {
index 091ad88b8b322ab80629048edbe307bf16cea692..2d77abc5d3e84e9d0368347c8194a2cf88b04507 100644 (file)
--- a/t/test.h
+++ b/t/test.h
@@ -43,7 +43,6 @@ void test_explain_decl(struct cdecl *decl);
 
 bool strict_strtoul(unsigned long *val, const char *str, int base);
 
-void test_print_version(const char *program);
 void test_print_options(const struct option *lopts);
 
 /*
@@ -71,4 +70,10 @@ static inline int test_rng_50_50(struct test_rng *rng)
        return test_rng_uniform_int(rng, 2) == 0;
 }
 
+#define VERSION_NO_COPYRIGHT_SYMBOL 1
+#include "version.h"
+
+#define test_print_version(s) \
+       do_print_version(s " (" PACKAGE_NAME ") " PACKAGE_VERSION)
+
 #endif
index 8edb423bb5004c776140ce096d5c08b5a475a7b4..c6b1975b1f3e8fdde8eeec655c3b0c2dae9046c1 100644 (file)
@@ -89,15 +89,6 @@ bool strict_strtoul(unsigned long *val, const char *str, int base)
        return true;
 }
 
-void test_print_version(const char *program)
-{
-       printf("%s (%s) %s\n", program, PACKAGE_NAME, PACKAGE_VERSION);
-       puts("Copyright (C) 2023 Nick Bowler.");
-       puts("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.");
-       puts("This is free software: you are free to change and redistribute it.");
-       puts("There is NO WARRANTY, to the extent permitted by law.");
-}
-
 void test_print_options(const struct option *lopts)
 {
        const struct option *opt;