11914Scasper /*
21914Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
31914Scasper * Use is subject to license terms.
41914Scasper */
50Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
60Sstevel@tonic-gate
70Sstevel@tonic-gate /*
80Sstevel@tonic-gate * make_commands.c
90Sstevel@tonic-gate *
100Sstevel@tonic-gate * util/ss/mk_cmds.c
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * Copyright 1987, 1988 by MIT Student Information Processing Board
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * For copyright information, see copyright.h.
150Sstevel@tonic-gate */
160Sstevel@tonic-gate
170Sstevel@tonic-gate #include "copyright.h"
180Sstevel@tonic-gate #include <stdio.h>
190Sstevel@tonic-gate #include <sys/param.h>
200Sstevel@tonic-gate #include <sys/types.h>
210Sstevel@tonic-gate #include <sys/file.h>
220Sstevel@tonic-gate #include <string.h>
230Sstevel@tonic-gate #include "ss_internal.h"
240Sstevel@tonic-gate
250Sstevel@tonic-gate static const char copyright[] =
260Sstevel@tonic-gate "Copyright 1987 by MIT Student Information Processing Board";
270Sstevel@tonic-gate
28*2881Smp153739 extern pointer malloc (unsigned);
290Sstevel@tonic-gate extern char *last_token;
300Sstevel@tonic-gate extern FILE *output_file;
310Sstevel@tonic-gate
320Sstevel@tonic-gate extern FILE *yyin, *yyout;
330Sstevel@tonic-gate #ifndef NO_YYLINENO
340Sstevel@tonic-gate extern int yylineno;
350Sstevel@tonic-gate #endif
360Sstevel@tonic-gate
main(argc,argv)370Sstevel@tonic-gate int main(argc, argv)
380Sstevel@tonic-gate int argc;
390Sstevel@tonic-gate char **argv;
400Sstevel@tonic-gate {
410Sstevel@tonic-gate char c_file[MAXPATHLEN];
420Sstevel@tonic-gate int result;
430Sstevel@tonic-gate char *path, *p, *q;
440Sstevel@tonic-gate
450Sstevel@tonic-gate if (argc != 2) {
460Sstevel@tonic-gate fputs("Usage: ", stderr);
470Sstevel@tonic-gate fputs(argv[0], stderr);
480Sstevel@tonic-gate fputs("cmdtbl.ct\n", stderr);
490Sstevel@tonic-gate exit(1);
500Sstevel@tonic-gate }
510Sstevel@tonic-gate
520Sstevel@tonic-gate path = malloc(strlen(argv[1])+4); /* extra space to add ".ct" */
530Sstevel@tonic-gate strcpy(path, argv[1]);
540Sstevel@tonic-gate p = strrchr(path, '/');
550Sstevel@tonic-gate if (p == (char *)NULL)
560Sstevel@tonic-gate p = path;
570Sstevel@tonic-gate else
580Sstevel@tonic-gate p++;
590Sstevel@tonic-gate p = strrchr(p, '.');
600Sstevel@tonic-gate if (p == (char *)NULL || strcmp(p, ".ct"))
610Sstevel@tonic-gate strcat(path, ".ct");
621914Scasper yyin = fopen(path, "rF");
630Sstevel@tonic-gate if (!yyin) {
640Sstevel@tonic-gate perror(path);
650Sstevel@tonic-gate exit(1);
660Sstevel@tonic-gate }
670Sstevel@tonic-gate
680Sstevel@tonic-gate p = strrchr(path, '.');
690Sstevel@tonic-gate *p = '\0';
700Sstevel@tonic-gate q = rindex(path, '/');
71*2881Smp153739 strncpy(c_file, (q) ? q + 1 : path, sizeof(c_file) - 1);
72*2881Smp153739 c_file[sizeof(c_file) - 1] = '\0';
73*2881Smp153739 strncat(c_file, ".c", sizeof(c_file) - 1 - strlen(c_file));
740Sstevel@tonic-gate *p = '.';
750Sstevel@tonic-gate
761914Scasper output_file = fopen(c_file, "w+F");
770Sstevel@tonic-gate if (!output_file) {
780Sstevel@tonic-gate perror(c_file);
790Sstevel@tonic-gate exit(1);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate
820Sstevel@tonic-gate fputs("/* ", output_file);
830Sstevel@tonic-gate fputs(c_file, output_file);
840Sstevel@tonic-gate fputs(" - automatically generated from ", output_file);
850Sstevel@tonic-gate fputs(path, output_file);
860Sstevel@tonic-gate fputs(" */\n", output_file);
870Sstevel@tonic-gate fputs("#include <ss/ss.h>\n\n", output_file);
880Sstevel@tonic-gate fputs("#ifndef __STDC__\n#define const\n#endif\n\n", output_file);
890Sstevel@tonic-gate /* parse it */
900Sstevel@tonic-gate result = yyparse();
910Sstevel@tonic-gate /* put file descriptors back where they belong */
920Sstevel@tonic-gate fclose(yyin); /* bye bye input file */
930Sstevel@tonic-gate fclose(output_file); /* bye bye output file */
940Sstevel@tonic-gate
950Sstevel@tonic-gate return result;
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
yyerror(s)980Sstevel@tonic-gate yyerror(s)
990Sstevel@tonic-gate char *s;
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate fputs(s, stderr);
1020Sstevel@tonic-gate #ifdef NO_YYLINENO
1030Sstevel@tonic-gate fprintf(stderr, "\nLast token was '%s'\n", last_token);
1040Sstevel@tonic-gate #else
1050Sstevel@tonic-gate fprintf(stderr, "\nLine %d; last token was '%s'\n",
1060Sstevel@tonic-gate yylineno, last_token);
1070Sstevel@tonic-gate #endif
1080Sstevel@tonic-gate }
109