xref: /illumos-gate/usr/src/lib/krb5/ss/mk_cmds.c (revision 1da57d55)
1 /*
2  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * make_commands.c
8  *
9  * util/ss/mk_cmds.c
10  *
11  * Copyright 1987, 1988 by MIT Student Information Processing Board
12  *
13  * For copyright information, see copyright.h.
14  */
15 
16 #include "copyright.h"
17 #include <stdio.h>
18 #include <sys/param.h>
19 #include <sys/types.h>
20 #include <sys/file.h>
21 #include <string.h>
22 #include "ss_internal.h"
23 
24 static const char copyright[] =
25     "Copyright 1987 by MIT Student Information Processing Board";
26 
27 extern pointer malloc (unsigned);
28 extern char *last_token;
29 extern FILE *output_file;
30 
31 extern FILE *yyin, *yyout;
32 #ifndef NO_YYLINENO
33 extern int yylineno;
34 #endif
35 
main(argc,argv)36 int main(argc, argv)
37     int argc;
38     char **argv;
39 {
40     char c_file[MAXPATHLEN];
41     int result;
42     char *path, *p, *q;
43 
44     if (argc != 2) {
45 	fputs("Usage: ", stderr);
46 	fputs(argv[0], stderr);
47 	fputs("cmdtbl.ct\n", stderr);
48 	exit(1);
49     }
50 
51     path = malloc(strlen(argv[1])+4); /* extra space to add ".ct" */
52     strcpy(path, argv[1]);
53     p = strrchr(path, '/');
54     if (p == (char *)NULL)
55 	p = path;
56     else
57 	p++;
58     p = strrchr(p, '.');
59     if (p == (char *)NULL || strcmp(p, ".ct"))
60 	strcat(path, ".ct");
61     yyin = fopen(path, "rF");
62     if (!yyin) {
63 	perror(path);
64 	exit(1);
65     }
66 
67     p = strrchr(path, '.');
68     *p = '\0';
69     q = rindex(path, '/');
70     strncpy(c_file, (q) ? q + 1 : path, sizeof(c_file) - 1);
71     c_file[sizeof(c_file) - 1] = '\0';
72     strncat(c_file, ".c", sizeof(c_file) - 1 - strlen(c_file));
73     *p = '.';
74 
75     output_file = fopen(c_file, "w+F");
76     if (!output_file) {
77 	perror(c_file);
78 	exit(1);
79     }
80 
81     fputs("/* ", output_file);
82     fputs(c_file, output_file);
83     fputs(" - automatically generated from ", output_file);
84     fputs(path, output_file);
85     fputs(" */\n", output_file);
86     fputs("#include <ss/ss.h>\n\n", output_file);
87     fputs("#ifndef __STDC__\n#define const\n#endif\n\n", output_file);
88     /* parse it */
89     result = yyparse();
90     /* put file descriptors back where they belong */
91     fclose(yyin);		/* bye bye input file */
92     fclose(output_file);	/* bye bye output file */
93 
94     return result;
95 }
96 
yyerror(s)97 yyerror(s)
98     char *s;
99 {
100     fputs(s, stderr);
101 #ifdef NO_YYLINENO
102     fprintf(stderr, "\nLast token was '%s'\n", last_token);
103 #else
104     fprintf(stderr, "\nLine %d; last token was '%s'\n",
105 	    yylineno, last_token);
106 #endif
107 }
108