1 /* $FreeBSD: src/usr.bin/gencat/gencat.c,v 1.5.2.1 2001/12/17 12:09:35 phantom Exp $ */
3 /***********************************************************
4 Copyright 1990, by Alfalfa Software Incorporated, Cambridge, Massachusetts.
8 Permission to use, copy, modify, and distribute this software and its
9 documentation for any purpose and without fee is hereby granted,
10 provided that the above copyright notice appear in all copies and that
11 both that copyright notice and this permission notice appear in
12 supporting documentation, and that Alfalfa's name not be used in
13 advertising or publicity pertaining to distribution of the software
14 without specific, written prior permission.
16 ALPHALPHA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
17 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
18 ALPHALPHA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
19 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24 If you make any modifications, bugfixes or other changes to this software
25 we'd appreciate it if you could send a copy to us so we can keep things
26 up-to-date. Many thanks.
28 Alfalfa Software, Inc.
30 Cambridge, MA 02139 USA
33 ******************************************************************/
35 #include <sys/types.h>
46 * The spec says the syntax is "gencat catfile msgfile...".
48 * gencat [-lang C|C++|ANSIC] catfile msgfile [-h <header-file>]...
49 * Flags are order dependant, we'll take whatever lang was most recently chosen
50 * and use it to generate the next header file. The header files are generated
51 * at the point in the command line they are listed. Thus the sequence:
52 * gencat -lang C foo.cat foo.mcs -h foo.h -lang C++ bar.mcs -h bar.H
53 * will put constants from foo.mcs into foo.h and constants from bar.mcs into
54 * bar.h. Constants are not saved in the catalog file, so nothing will come
55 * from that, even if things have been defined before. The constants in foo.h
56 * will be in C syntax, in bar.H in C++ syntax.
59 static void writeIfChanged(char *, int, int);
64 fprintf(stderr, "usage: gencat [-new] [-or] [-lang C|C++|ANSIC]\n"
65 " catfile msgfile [-h <header-file>]...\n");
70 main(int argc, char *argv[])
79 for (i = 1; i < argc; ++i) {
80 if (argv[i][0] == '-') {
81 if (strcmp(argv[i], "-lang") == 0) {
83 if (strcmp(argv[i], "C") == 0) lang = MCLangC;
84 else if (strcmp(argv[i], "C++") == 0) lang = MCLangCPlusPlus;
85 else if (strcmp(argv[i], "ANSIC") == 0) lang = MCLangANSIC;
87 errx(1, "unrecognized language: %s", argv[i]);
89 } else if (strcmp(argv[i], "-h") == 0) {
91 errx(1, "can't write to a header before reading something");
93 writeIfChanged(argv[i], lang, orConsts);
94 } else if (strcmp(argv[i], "-new") == 0) {
96 errx(1, "you must specify -new before the catalog file name");
98 } else if (strcmp(argv[i], "-or") == 0) {
107 if ((ofd = open(catfile, O_WRONLY|O_TRUNC|O_CREAT, 0666)) < 0)
108 errx(1, "unable to create a new %s", catfile);
109 } else if ((ofd = open(catfile, O_RDONLY)) < 0) {
110 if ((ofd = open(catfile, O_WRONLY|O_CREAT, 0666)) < 0)
111 errx(1, "unable to create %s", catfile);
115 if ((ofd = open(catfile, O_WRONLY|O_TRUNC)) < 0)
116 errx(1, "unable to truncate %s", catfile);
120 if ((ifd = open(input, O_RDONLY)) < 0)
121 errx(1, "unable to read %s", input);
137 writeIfChanged(char *fname, int lang, int orConsts)
140 char buf[BUFSIZ], tbuf[BUFSIZ], *cptr, *tptr;
146 /* If it doesn't exist, just create it */
147 if (stat(fname, &sbuf)) {
148 if ((fd = open(fname, O_WRONLY|O_CREAT, 0666)) < 0)
149 errx(1, "unable to create header file %s", fname);
150 MCWriteConst(fd, lang, orConsts);
155 /* If it does exist, create a temp file for now */
156 sprintf(tmpname, "/tmp/gencat.%d", (int) getpid());
157 if ((tfd = open(tmpname, O_RDWR|O_CREAT, 0666)) < 0)
158 errx(1, "unable to open temporary file: %s", tmpname);
161 /* Write to the temp file and rewind */
162 MCWriteConst(tfd, lang, orConsts);
164 /* Open the real header file */
165 if ((fd = open(fname, O_RDONLY)) < 0)
166 errx(1, "unable to read header file: %s", fname);
168 /* Backup to the start of the temp file */
169 if (lseek(tfd, (off_t)0, L_SET) < 0)
170 errx(1, "unable to seek in tempfile: %s", tmpname);
172 /* Now compare them */
173 while ((tlen = read(tfd, tbuf, BUFSIZ)) > 0) {
174 if ((len = read(fd, buf, BUFSIZ)) != tlen) {
178 for (cptr = buf, tptr = tbuf; cptr < buf+len; ++cptr, ++tptr) {
179 if (*tptr != *cptr) {
187 if (lseek(tfd, (off_t)0, L_SET) < 0)
188 errx(1, "unable to seek in tempfile: %s", tmpname);
190 if ((fd = open(fname, O_WRONLY|O_TRUNC)) < 0)
191 errx(1, "unable to truncate header file: %s", fname);
192 while ((len = read(tfd, buf, BUFSIZ)) > 0) {
193 if (write(fd, buf, (size_t)len) != len)
194 warnx("error writing to header file: %s", fname);