1004388ebScasper /*
2004388ebScasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
3004388ebScasper * Use is subject to license terms.
4004388ebScasper */
57c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
67c478bd9Sstevel@tonic-gate
77c478bd9Sstevel@tonic-gate /*
87c478bd9Sstevel@tonic-gate * make_commands.c
97c478bd9Sstevel@tonic-gate *
107c478bd9Sstevel@tonic-gate * util/ss/mk_cmds.c
117c478bd9Sstevel@tonic-gate *
127c478bd9Sstevel@tonic-gate * Copyright 1987, 1988 by MIT Student Information Processing Board
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * For copyright information, see copyright.h.
157c478bd9Sstevel@tonic-gate */
167c478bd9Sstevel@tonic-gate
177c478bd9Sstevel@tonic-gate #include "copyright.h"
187c478bd9Sstevel@tonic-gate #include <stdio.h>
197c478bd9Sstevel@tonic-gate #include <sys/param.h>
207c478bd9Sstevel@tonic-gate #include <sys/types.h>
217c478bd9Sstevel@tonic-gate #include <sys/file.h>
227c478bd9Sstevel@tonic-gate #include <string.h>
237c478bd9Sstevel@tonic-gate #include "ss_internal.h"
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate static const char copyright[] =
267c478bd9Sstevel@tonic-gate "Copyright 1987 by MIT Student Information Processing Board";
277c478bd9Sstevel@tonic-gate
28*56a424ccSmp153739 extern pointer malloc (unsigned);
297c478bd9Sstevel@tonic-gate extern char *last_token;
307c478bd9Sstevel@tonic-gate extern FILE *output_file;
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate extern FILE *yyin, *yyout;
337c478bd9Sstevel@tonic-gate #ifndef NO_YYLINENO
347c478bd9Sstevel@tonic-gate extern int yylineno;
357c478bd9Sstevel@tonic-gate #endif
367c478bd9Sstevel@tonic-gate
main(argc,argv)377c478bd9Sstevel@tonic-gate int main(argc, argv)
387c478bd9Sstevel@tonic-gate int argc;
397c478bd9Sstevel@tonic-gate char **argv;
407c478bd9Sstevel@tonic-gate {
417c478bd9Sstevel@tonic-gate char c_file[MAXPATHLEN];
427c478bd9Sstevel@tonic-gate int result;
437c478bd9Sstevel@tonic-gate char *path, *p, *q;
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate if (argc != 2) {
467c478bd9Sstevel@tonic-gate fputs("Usage: ", stderr);
477c478bd9Sstevel@tonic-gate fputs(argv[0], stderr);
487c478bd9Sstevel@tonic-gate fputs("cmdtbl.ct\n", stderr);
497c478bd9Sstevel@tonic-gate exit(1);
507c478bd9Sstevel@tonic-gate }
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate path = malloc(strlen(argv[1])+4); /* extra space to add ".ct" */
537c478bd9Sstevel@tonic-gate strcpy(path, argv[1]);
547c478bd9Sstevel@tonic-gate p = strrchr(path, '/');
557c478bd9Sstevel@tonic-gate if (p == (char *)NULL)
567c478bd9Sstevel@tonic-gate p = path;
577c478bd9Sstevel@tonic-gate else
587c478bd9Sstevel@tonic-gate p++;
597c478bd9Sstevel@tonic-gate p = strrchr(p, '.');
607c478bd9Sstevel@tonic-gate if (p == (char *)NULL || strcmp(p, ".ct"))
617c478bd9Sstevel@tonic-gate strcat(path, ".ct");
62004388ebScasper yyin = fopen(path, "rF");
637c478bd9Sstevel@tonic-gate if (!yyin) {
647c478bd9Sstevel@tonic-gate perror(path);
657c478bd9Sstevel@tonic-gate exit(1);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate p = strrchr(path, '.');
697c478bd9Sstevel@tonic-gate *p = '\0';
707c478bd9Sstevel@tonic-gate q = rindex(path, '/');
71*56a424ccSmp153739 strncpy(c_file, (q) ? q + 1 : path, sizeof(c_file) - 1);
72*56a424ccSmp153739 c_file[sizeof(c_file) - 1] = '\0';
73*56a424ccSmp153739 strncat(c_file, ".c", sizeof(c_file) - 1 - strlen(c_file));
747c478bd9Sstevel@tonic-gate *p = '.';
757c478bd9Sstevel@tonic-gate
76004388ebScasper output_file = fopen(c_file, "w+F");
777c478bd9Sstevel@tonic-gate if (!output_file) {
787c478bd9Sstevel@tonic-gate perror(c_file);
797c478bd9Sstevel@tonic-gate exit(1);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate fputs("/* ", output_file);
837c478bd9Sstevel@tonic-gate fputs(c_file, output_file);
847c478bd9Sstevel@tonic-gate fputs(" - automatically generated from ", output_file);
857c478bd9Sstevel@tonic-gate fputs(path, output_file);
867c478bd9Sstevel@tonic-gate fputs(" */\n", output_file);
877c478bd9Sstevel@tonic-gate fputs("#include <ss/ss.h>\n\n", output_file);
887c478bd9Sstevel@tonic-gate fputs("#ifndef __STDC__\n#define const\n#endif\n\n", output_file);
897c478bd9Sstevel@tonic-gate /* parse it */
907c478bd9Sstevel@tonic-gate result = yyparse();
917c478bd9Sstevel@tonic-gate /* put file descriptors back where they belong */
927c478bd9Sstevel@tonic-gate fclose(yyin); /* bye bye input file */
937c478bd9Sstevel@tonic-gate fclose(output_file); /* bye bye output file */
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate return result;
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
yyerror(s)987c478bd9Sstevel@tonic-gate yyerror(s)
997c478bd9Sstevel@tonic-gate char *s;
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate fputs(s, stderr);
1027c478bd9Sstevel@tonic-gate #ifdef NO_YYLINENO
1037c478bd9Sstevel@tonic-gate fprintf(stderr, "\nLast token was '%s'\n", last_token);
1047c478bd9Sstevel@tonic-gate #else
1057c478bd9Sstevel@tonic-gate fprintf(stderr, "\nLine %d; last token was '%s'\n",
1067c478bd9Sstevel@tonic-gate yylineno, last_token);
1077c478bd9Sstevel@tonic-gate #endif
1087c478bd9Sstevel@tonic-gate }
109