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