]> git.draconx.ca Git - cdecl99.git/blobdiff - src/cdecl99.c
Fix build with NLS disabled.
[cdecl99.git] / src / cdecl99.c
index 5c67ed8de9af74456901115c4f57fd41e9503c98..04de1a6640e92225676f3e56872a63e26945a70d 100644 (file)
@@ -21,7 +21,9 @@
 #include <stdbool.h>
 #include <string.h>
 #include <errno.h>
+#include <locale.h>
 #include <getopt.h>
+#include <gettext.h>
 #include "readline.h"
 #include "cdecl.h"
 
@@ -41,7 +43,8 @@ static const struct option lopts[] = {
 static void print_version(void)
 {
        puts(PACKAGE_STRING);
-       puts("Copyright (C) 2011 Nick Bowler.");
+       /* TRANSLATORS: (C) must *always* be translated as ©. */
+       printf("Copyright %s 2011 Nick Bowler.\n", gettext("(C)"));
        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.");
@@ -92,7 +95,7 @@ retry:
        return buf;
 }
 
-static int cmd_explain(char *cmd, char *arg)
+static int cmd_explain(const char *cmd, const char *arg)
 {
        struct cdecl *decl;
        const char *str;
@@ -116,7 +119,7 @@ out:
        return ret;
 }
 
-static int cmd_simplify(char *cmd, char *arg)
+static int cmd_simplify(const char *cmd, const char *arg)
 {
        struct cdecl *decl;
        const char *str;
@@ -151,27 +154,55 @@ out:
        return ret;
 }
 
-static int cmd_quit(char *cmd, char *arg)
+static int cmd_declare(const char *cmd, const char *arg)
+{
+       struct cdecl *decl;
+       const char *str;
+       int ret = -1;
+
+       /* The name of the command is significant here. */
+       decl = cdecl_parse_english(cmd);
+       if (!decl)
+               goto out;
+
+       /*
+        * English parses have at most one full declarator, so no loop is
+        * needed here.
+        */
+       str = do_format(cdecl_declare, decl);
+       if (!str)
+               goto out;
+
+       printf("%s\n", str);
+       ret = 1;
+out:
+       cdecl_free(decl);
+       return ret;
+}
+
+static int cmd_quit(const char *cmd, const char *arg)
 {
        return 0;
 }
 
-static int cmd_help(char *cmd, char *arg);
+static int cmd_help(const char *cmd, const char *arg);
 
 static const struct command {
        char name[16];
-       int (*func)(char *cmd, char *arg);
+       int (*func)(const char *cmd, const char *arg);
        const char *blurb;
 } commands[] = {
        { "explain",  cmd_explain,  "Explain a C declaration." },
        { "simplify", cmd_simplify, "Simplify a C declaration." },
+       { "declare",  cmd_declare,  "Construct a C declaration." },
+       { "type",     cmd_declare,  "Construct a C type name." },
        { "help",     cmd_help,     "Print this list of commands." },
        { "quit",     cmd_quit,     "Quit the program." },
        { "exit",     cmd_quit,     NULL }
 };
 static const size_t ncommands = sizeof commands / sizeof commands[0];
 
-static int cmd_help(char *cmd, char *arg)
+static int cmd_help(const char *cmd, const char *arg)
 {
        for (size_t i = 0; i < ncommands; i++) {
                if (!commands[i].blurb)
@@ -183,24 +214,37 @@ static int cmd_help(char *cmd, char *arg)
        return 1;
 }
 
-static int run_command(char *line)
+/*
+ * Ensure that the first n characters of str equal the entire (null-terminated)
+ * string cmd.
+ */
+static int cmd_cmp(const char *str, const char *cmd, size_t n)
+{
+       size_t cmdlen = strlen(cmd);
+
+       if (n < cmdlen)
+               return -1;
+       if (n > cmdlen)
+               return 1;
+       return memcmp(str, cmd, n);
+}
+
+static int run_command(const char *line)
 {
-       char *cmd = line + strspn(line, " \t");
-       char *arg = cmd  + strcspn(cmd, " \t");
+       const char *cmd = line + strspn(line, " \t");
+       const char *arg = cmd  + strcspn(cmd, " \t");
 
        if (cmd[0] == '\0')
                return 1;
-       if (arg[0] != '\0')
-               *arg++ = '\0';
 
        for (size_t i = 0; i < ncommands; i++) {
-               if (strcmp(cmd, commands[i].name) != 0)
+               if (cmd_cmp(cmd, commands[i].name, arg-cmd) != 0)
                        continue;
 
                return commands[i].func(cmd, arg);
        }
 
-       fprintf(stderr, "Undefined command: %s\n", cmd);
+       fprintf(stderr, "Undefined command: %.*s\n", (int)(arg-cmd), cmd);
        return -1;
 }
 
@@ -275,6 +319,11 @@ int main(int argc, char **argv)
        if (argc > 0)
                progname = argv[0];
 
+       /* Initialize gettext. */
+       setlocale(LC_ALL, "");
+       bindtextdomain(PACKAGE, LOCALEDIR);
+       textdomain(PACKAGE);
+
        while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
                switch (opt) {
                case 'q':