]> git.draconx.ca Git - cdecl99.git/blobdiff - src/cdecl99.c
Don't call cdecl_free internally.
[cdecl99.git] / src / cdecl99.c
index 2df7ec8c98543293a7c980178a3fd6b28a9cfd89..c5f913bd12d1fd0a442bdc0f2aa82def63d3a5cf 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.");
@@ -94,13 +97,17 @@ retry:
 
 static int cmd_explain(const char *cmd, const char *arg)
 {
+       const struct cdecl_error *err;
        struct cdecl *decl;
        const char *str;
        int ret = -1;
 
        decl = cdecl_parse_decl(arg);
-       if (!decl)
+       if (!decl) {
+               err = cdecl_get_error();
+               fprintf(stderr, "%s\n", err->str);
                goto out;
+       }
 
        for (struct cdecl *i = decl; i; i = i->next) {
                str = do_format(cdecl_explain, i);
@@ -118,13 +125,17 @@ out:
 
 static int cmd_simplify(const char *cmd, const char *arg)
 {
+       const struct cdecl_error *err;
        struct cdecl *decl;
        const char *str;
        int ret = -1;
 
        decl = cdecl_parse_decl(arg);
-       if (!decl)
+       if (!decl) {
+               err = cdecl_get_error();
+               fprintf(stderr, "%s\n", err->str);
                goto out;
+       }
 
        for (struct cdecl *i = decl; i; i = i->next) {
                struct cdecl_declspec *s = i->specifiers;
@@ -151,6 +162,36 @@ out:
        return ret;
 }
 
+static int cmd_declare(const char *cmd, const char *arg)
+{
+       const struct cdecl_error *err;
+       struct cdecl *decl;
+       const char *str;
+       int ret = -1;
+
+       /* The name of the command is significant here. */
+       decl = cdecl_parse_english(cmd);
+       if (!decl) {
+               err = cdecl_get_error();
+               fprintf(stderr, "%s\n", err->str);
+               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;
@@ -165,6 +206,8 @@ static const struct command {
 } 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 }
@@ -288,6 +331,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':