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