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