]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl99.c
Initial support for explaining declarations.
[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         size_t rc;
44
45         decl = cdecl_parse_decl(arg);
46         if (!decl)
47                 goto out;
48
49 retry:
50         rc = cdecl_explain(buf, bufsz, decl);
51         if (rc >= bufsz) {
52                 char *tmp;
53
54                 tmp = realloc(buf, rc + 1);
55                 if (!tmp) {
56                         fprintf(stderr, "failed to allocate memory\n");
57                         return 1;
58                 }
59
60                 buf = tmp;
61                 bufsz = rc + 1;
62                 goto retry;
63         }
64
65         printf("%s\n", buf);
66 out:
67         cdecl_free(decl);
68         return 1;
69 }
70
71 static int cmd_quit(char *cmd, char *arg)
72 {
73         return 0;
74 }
75
76 static int cmd_help(char *cmd, char *arg);
77
78 static const struct command {
79         char name[16];
80         int (*func)(char *cmd, char *arg);
81         const char *blurb;
82 } commands[] = {
83         { "explain", cmd_explain, "Explain a C declaration." },
84         { "help",    cmd_help,    "Print this list of commands." },
85         { "quit",    cmd_quit,    "Quit the program." },
86         { "exit",    cmd_quit,    NULL }
87 };
88 static const size_t ncommands = sizeof commands / sizeof commands[0];
89
90 static int cmd_help(char *cmd, char *arg)
91 {
92         for (size_t i = 0; i < ncommands; i++) {
93                 if (!commands[i].blurb)
94                         continue;
95
96                 printf("%s -- %s\n", commands[i].name, commands[i].blurb);
97         }
98
99         return 1;
100 }
101
102 static int repl(void)
103 {
104         char *line;
105         int ret;
106
107         print_version();
108
109         for (; (line = readline("> ")); free(line)) {
110                 char *cmd = line + strspn(line, " \t");
111                 char *arg = cmd  + strcspn(cmd, " \t");
112
113                 if (cmd[0] == '\0')
114                         continue;
115                 if (arg[0] != '\0')
116                         *arg++ = '\0';
117
118                 for (size_t i = 0; i < ncommands; i++) {
119                         if (strcmp(cmd, commands[i].name) != 0)
120                                 continue;
121
122                         ret = commands[i].func(cmd, arg);
123                         if (ret <= 0)
124                                 goto out;
125                         goto next;
126                 }
127
128                 fprintf(stderr, "Undefined command: %s\n", cmd);
129         next:
130                 ;
131         }
132
133         ret = 0;
134 out:
135         free(line);
136         return ret;
137 }
138
139 int main(int argc, char **argv)
140 {
141         int opt;
142
143         if (argc > 0)
144                 progname = argv[0];
145
146         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
147                 switch (opt) {
148                 case 'V':
149                         print_version();
150                         return EXIT_SUCCESS;
151                 case 'H':
152                         print_help();
153                         return EXIT_SUCCESS;
154                 default:
155                         print_usage(stderr);
156                         return EXIT_FAILURE;
157                 }
158         }
159
160         if (repl() != 0)
161                 return EXIT_FAILURE;
162
163         return EXIT_SUCCESS;
164 }