]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl99.c
Add missing copyright headers.
[cdecl99.git] / src / cdecl99.c
1 /*
2  *  Command line utility for making sense of C declarations.
3  *  Copyright © 2011 Nick Bowler
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #include <config.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <getopt.h>
23 #include "readline.h"
24 #include "cdecl.h"
25
26 static const char *progname = "cdecl99";
27 static const char sopts[] = "VH";
28 static const struct option lopts[] = {
29         { "version", 0, NULL, 'V' },
30         { "help",    0, NULL, 'H' },
31         { 0 }
32 };
33
34 static void print_version(void)
35 {
36         puts(PACKAGE_STRING);
37         puts("Copyright (C) 2011 Nick Bowler.");
38         puts("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.");
39         puts("This is free software: you are free to change and redistribute it.");
40         puts("There is NO WARRANTY, to the extent permitted by law.");
41 }
42
43 static void print_usage(FILE *f)
44 {
45         fprintf(f, "Usage: %s [options]\n", progname);
46 }
47
48 static void print_help(void)
49 {
50         print_usage(stdout);
51         puts("Detailed help coming soon.");
52 }
53
54 static int cmd_explain(char *cmd, char *arg)
55 {
56         static size_t bufsz;
57         static char *buf;
58
59         struct cdecl *decl;
60         size_t rc;
61
62         decl = cdecl_parse_decl(arg);
63         if (!decl)
64                 goto out;
65
66         for (struct cdecl *i = decl; i; i = i->next) {
67 retry:
68                 rc = cdecl_explain(buf, bufsz, i);
69                 if (rc >= bufsz) {
70                         char *tmp;
71
72                         tmp = realloc(buf, rc + 1);
73                         if (!tmp) {
74                                 fprintf(stderr, "failed to allocate memory\n");
75                                 goto out;
76                         }
77
78                         buf = tmp;
79                         bufsz = rc + 1;
80                         goto retry;
81                 }
82
83                 printf("%s\n", buf);
84         }
85 out:
86         cdecl_free(decl);
87         return 1;
88 }
89
90 static int cmd_quit(char *cmd, char *arg)
91 {
92         return 0;
93 }
94
95 static int cmd_help(char *cmd, char *arg);
96
97 static const struct command {
98         char name[16];
99         int (*func)(char *cmd, char *arg);
100         const char *blurb;
101 } commands[] = {
102         { "explain", cmd_explain, "Explain a C declaration." },
103         { "help",    cmd_help,    "Print this list of commands." },
104         { "quit",    cmd_quit,    "Quit the program." },
105         { "exit",    cmd_quit,    NULL }
106 };
107 static const size_t ncommands = sizeof commands / sizeof commands[0];
108
109 static int cmd_help(char *cmd, char *arg)
110 {
111         for (size_t i = 0; i < ncommands; i++) {
112                 if (!commands[i].blurb)
113                         continue;
114
115                 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
116         }
117
118         return 1;
119 }
120
121 static int repl(void)
122 {
123         char *line;
124         int ret;
125
126         print_version();
127
128         for (; (line = readline("> ")); free(line)) {
129                 char *cmd = line + strspn(line, " \t");
130                 char *arg = cmd  + strcspn(cmd, " \t");
131
132                 if (cmd[0] == '\0')
133                         continue;
134                 if (arg[0] != '\0')
135                         *arg++ = '\0';
136
137                 for (size_t i = 0; i < ncommands; i++) {
138                         if (strcmp(cmd, commands[i].name) != 0)
139                                 continue;
140
141                         ret = commands[i].func(cmd, arg);
142                         if (ret <= 0)
143                                 goto out;
144                         goto next;
145                 }
146
147                 fprintf(stderr, "Undefined command: %s\n", cmd);
148         next:
149                 ;
150         }
151
152         ret = 0;
153 out:
154         free(line);
155         return ret;
156 }
157
158 int main(int argc, char **argv)
159 {
160         int opt;
161
162         if (argc > 0)
163                 progname = argv[0];
164
165         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
166                 switch (opt) {
167                 case 'V':
168                         print_version();
169                         return EXIT_SUCCESS;
170                 case 'H':
171                         print_help();
172                         return EXIT_SUCCESS;
173                 default:
174                         print_usage(stderr);
175                         return EXIT_FAILURE;
176                 }
177         }
178
179         if (repl() != 0)
180                 return EXIT_FAILURE;
181
182         return EXIT_SUCCESS;
183 }