No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / sl / make_cmds.c
blob324e5f0fd84c5239c4612979258314d8d72aab05
1 /*
2 * Copyright (c) 1998-1999 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "make_cmds.h"
35 #include <getarg.h>
37 __RCSID("$Heimdal: make_cmds.c 15430 2005-06-16 19:25:45Z lha $"
38 "$NetBSD$");
40 #include <roken.h>
41 #include <err.h>
42 #include "parse.h"
44 int numerror;
45 extern FILE *yyin;
46 FILE *c_file;
48 extern void yyparse(void);
50 #ifdef YYDEBUG
51 extern int yydebug = 1;
52 #endif
54 char *filename;
55 char *table_name;
57 static struct command_list *commands;
59 void
60 add_command(char *function,
61 char *help,
62 struct string_list *aliases,
63 unsigned flags)
65 struct command_list *cl = malloc(sizeof(*cl));
67 if (cl == NULL)
68 err (1, "malloc");
69 cl->function = function;
70 cl->help = help;
71 cl->aliases = aliases;
72 cl->flags = flags;
73 cl->next = NULL;
74 if(commands) {
75 *commands->tail = cl;
76 commands->tail = &cl->next;
77 return;
79 cl->tail = &cl->next;
80 commands = cl;
83 static char *
84 quote(const char *str)
86 char buf[1024]; /* XXX */
87 const char *p;
88 char *q;
89 q = buf;
91 *q++ = '\"';
92 for(p = str; *p != '\0'; p++) {
93 if(*p == '\n') {
94 *q++ = '\\';
95 *q++ = 'n';
96 continue;
98 if(*p == '\t') {
99 *q++ = '\\';
100 *q++ = 't';
101 continue;
103 if(*p == '\"' || *p == '\\')
104 *q++ = '\\';
105 *q++ = *p;
107 *q++ = '\"';
108 *q++ = '\0';
109 return strdup(buf);
112 static void
113 generate_commands(void)
115 char *base;
116 char *cfn;
117 char *p, *q;
119 p = strrchr(table_name, '/');
120 if(p == NULL)
121 p = table_name;
122 else
123 p++;
125 base = strdup (p);
126 if (base == NULL)
127 err (1, "strdup");
129 p = strrchr(base, '.');
130 if(p)
131 *p = '\0';
133 asprintf(&cfn, "%s.c", base);
134 if (cfn == NULL)
135 err (1, "asprintf");
137 c_file = fopen(cfn, "w");
138 if (c_file == NULL)
139 err (1, "cannot fopen %s", cfn);
141 fprintf(c_file, "/* Generated from %s */\n", filename);
142 fprintf(c_file, "\n");
143 fprintf(c_file, "#include <stddef.h>\n");
144 fprintf(c_file, "#include <sl.h>\n");
145 fprintf(c_file, "\n");
148 struct command_list *cl, *xl;
150 for(cl = commands; cl; cl = cl->next) {
151 for(xl = commands; xl != cl; xl = xl->next)
152 if(strcmp(cl->function, xl->function) == 0)
153 break;
154 if(xl != cl)
155 continue;
156 /* XXX hack for ss_quit */
157 if(strcmp(cl->function, "ss_quit") == 0) {
158 fprintf(c_file, "int %s (int, char**);\n", cl->function);
159 fprintf(c_file, "#define _ss_quit_wrap ss_quit\n\n");
160 continue;
162 fprintf(c_file, "void %s (int, char**);\n", cl->function);
163 fprintf(c_file, "static int _%s_wrap (int argc, char **argv)\n",
164 cl->function);
165 fprintf(c_file, "{\n");
166 fprintf(c_file, " %s (argc, argv);\n", cl->function);
167 fprintf(c_file, " return 0;\n");
168 fprintf(c_file, "}\n\n");
171 fprintf(c_file, "SL_cmd %s[] = {\n", table_name);
172 for(cl = commands; cl; cl = cl->next) {
173 struct string_list *sl;
174 sl = cl->aliases;
175 p = quote(sl->string);
176 q = quote(cl->help);
177 fprintf(c_file, " { %s, _%s_wrap, %s },\n", p, cl->function, q);
178 free(p);
179 free(q);
181 for(sl = sl->next; sl; sl = sl->next) {
182 p = quote(sl->string);
183 fprintf(c_file, " { %s },\n", p);
184 free(p);
187 fprintf(c_file, " { NULL },\n");
188 fprintf(c_file, "};\n");
189 fprintf(c_file, "\n");
191 fclose(c_file);
192 free(base);
193 free(cfn);
196 int version_flag;
197 int help_flag;
198 struct getargs args[] = {
199 { "version", 0, arg_flag, &version_flag },
200 { "help", 0, arg_flag, &help_flag }
202 int num_args = sizeof(args) / sizeof(args[0]);
204 static void
205 usage(int code)
207 arg_printusage(args, num_args, NULL, "command-table");
208 exit(code);
212 main(int argc, char **argv)
214 int optidx = 0;
216 setprogname(argv[0]);
217 if(getarg(args, num_args, argc, argv, &optidx))
218 usage(1);
219 if(help_flag)
220 usage(0);
221 if(version_flag) {
222 print_version(NULL);
223 exit(0);
226 if(argc == optidx)
227 usage(1);
228 filename = argv[optidx];
229 yyin = fopen(filename, "r");
230 if(yyin == NULL)
231 err(1, "%s", filename);
233 yyparse();
235 generate_commands();
237 if(numerror)
238 return 1;
239 return 0;