1*1914Scasper /* 2*1914Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 3*1914Scasper * Use is subject to license terms. 4*1914Scasper */ 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 280Sstevel@tonic-gate extern pointer malloc PROTOTYPE((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 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"); 62*1914Scasper 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, '/'); 710Sstevel@tonic-gate strcpy(c_file, (q) ? q + 1 : path); 720Sstevel@tonic-gate strcat(c_file, ".c"); 730Sstevel@tonic-gate *p = '.'; 740Sstevel@tonic-gate 75*1914Scasper output_file = fopen(c_file, "w+F"); 760Sstevel@tonic-gate if (!output_file) { 770Sstevel@tonic-gate perror(c_file); 780Sstevel@tonic-gate exit(1); 790Sstevel@tonic-gate } 800Sstevel@tonic-gate 810Sstevel@tonic-gate fputs("/* ", output_file); 820Sstevel@tonic-gate fputs(c_file, output_file); 830Sstevel@tonic-gate fputs(" - automatically generated from ", output_file); 840Sstevel@tonic-gate fputs(path, output_file); 850Sstevel@tonic-gate fputs(" */\n", output_file); 860Sstevel@tonic-gate fputs("#include <ss/ss.h>\n\n", output_file); 870Sstevel@tonic-gate fputs("#ifndef __STDC__\n#define const\n#endif\n\n", output_file); 880Sstevel@tonic-gate /* parse it */ 890Sstevel@tonic-gate result = yyparse(); 900Sstevel@tonic-gate /* put file descriptors back where they belong */ 910Sstevel@tonic-gate fclose(yyin); /* bye bye input file */ 920Sstevel@tonic-gate fclose(output_file); /* bye bye output file */ 930Sstevel@tonic-gate 940Sstevel@tonic-gate return result; 950Sstevel@tonic-gate } 960Sstevel@tonic-gate 970Sstevel@tonic-gate yyerror(s) 980Sstevel@tonic-gate char *s; 990Sstevel@tonic-gate { 1000Sstevel@tonic-gate fputs(s, stderr); 1010Sstevel@tonic-gate #ifdef NO_YYLINENO 1020Sstevel@tonic-gate fprintf(stderr, "\nLast token was '%s'\n", last_token); 1030Sstevel@tonic-gate #else 1040Sstevel@tonic-gate fprintf(stderr, "\nLine %d; last token was '%s'\n", 1050Sstevel@tonic-gate yylineno, last_token); 1060Sstevel@tonic-gate #endif 1070Sstevel@tonic-gate } 108