1995-03-30 12:49:09 +00:00
|
|
|
/***********************************************************
|
|
|
|
Copyright 1990, by Alfalfa Software Incorporated, Cambridge, Massachusetts.
|
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
|
|
|
provided that the above copyright notice appear in all copies and that
|
|
|
|
both that copyright notice and this permission notice appear in
|
|
|
|
supporting documentation, and that Alfalfa's name not be used in
|
|
|
|
advertising or publicity pertaining to distribution of the software
|
|
|
|
without specific, written prior permission.
|
|
|
|
|
|
|
|
ALPHALPHA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
|
|
|
|
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
|
|
|
|
ALPHALPHA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
|
|
|
|
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
|
|
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
|
|
|
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
|
|
SOFTWARE.
|
|
|
|
|
|
|
|
If you make any modifications, bugfixes or other changes to this software
|
|
|
|
we'd appreciate it if you could send a copy to us so we can keep things
|
|
|
|
up-to-date. Many thanks.
|
|
|
|
Kee Hinckley
|
|
|
|
Alfalfa Software, Inc.
|
|
|
|
267 Allston St., #3
|
|
|
|
Cambridge, MA 02139 USA
|
|
|
|
nazgul@alfalfa.com
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
******************************************************************/
|
|
|
|
|
2002-03-26 12:39:08 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/stat.h>
|
2001-02-08 16:38:16 +00:00
|
|
|
#include <err.h>
|
2002-05-29 14:23:10 +00:00
|
|
|
#include <paths.h>
|
2001-02-08 16:38:16 +00:00
|
|
|
#include <stdio.h>
|
2001-06-24 19:41:18 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-02-08 16:38:16 +00:00
|
|
|
#include <unistd.h>
|
1995-03-30 12:49:09 +00:00
|
|
|
#include "gencat.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The spec says the syntax is "gencat catfile msgfile...".
|
|
|
|
* We extend it to:
|
|
|
|
* gencat [-lang C|C++|ANSIC] catfile msgfile [-h <header-file>]...
|
2002-03-26 12:39:08 +00:00
|
|
|
* Flags are order dependent, we'll take whatever lang was most recently chosen
|
1995-03-30 12:49:09 +00:00
|
|
|
* and use it to generate the next header file. The header files are generated
|
|
|
|
* at the point in the command line they are listed. Thus the sequence:
|
|
|
|
* gencat -lang C foo.cat foo.mcs -h foo.h -lang C++ bar.mcs -h bar.H
|
|
|
|
* will put constants from foo.mcs into foo.h and constants from bar.mcs into
|
|
|
|
* bar.h. Constants are not saved in the catalog file, so nothing will come
|
|
|
|
* from that, even if things have been defined before. The constants in foo.h
|
|
|
|
* will be in C syntax, in bar.H in C++ syntax.
|
|
|
|
*/
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
static void writeIfChanged(char *, int, int);
|
1995-03-30 12:49:09 +00:00
|
|
|
|
1997-07-08 11:05:59 +00:00
|
|
|
static void
|
2001-02-08 16:38:16 +00:00
|
|
|
usage(void)
|
1997-07-08 11:05:59 +00:00
|
|
|
{
|
2001-02-08 16:38:16 +00:00
|
|
|
fprintf(stderr, "usage: gencat [-new] [-or] [-lang C|C++|ANSIC]\n"
|
|
|
|
" catfile msgfile [-h <header-file>]...\n");
|
|
|
|
exit(1);
|
1995-03-30 12:49:09 +00:00
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
|
|
|
int ofd, ifd, i;
|
|
|
|
char *catfile = NULL;
|
|
|
|
char *input = NULL;
|
|
|
|
int lang = MCLangC;
|
2001-02-08 16:38:16 +00:00
|
|
|
int new = FALSE;
|
|
|
|
int orConsts = FALSE;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-03-30 12:49:09 +00:00
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
if (argv[i][0] == '-') {
|
|
|
|
if (strcmp(argv[i], "-lang") == 0) {
|
|
|
|
++i;
|
|
|
|
if (strcmp(argv[i], "C") == 0) lang = MCLangC;
|
|
|
|
else if (strcmp(argv[i], "C++") == 0) lang = MCLangCPlusPlus;
|
|
|
|
else if (strcmp(argv[i], "ANSIC") == 0) lang = MCLangANSIC;
|
|
|
|
else {
|
1997-07-08 11:05:59 +00:00
|
|
|
errx(1, "unrecognized language: %s", argv[i]);
|
1995-05-30 06:41:30 +00:00
|
|
|
}
|
1995-03-30 12:49:09 +00:00
|
|
|
} else if (strcmp(argv[i], "-h") == 0) {
|
1997-07-08 11:05:59 +00:00
|
|
|
if (!input)
|
|
|
|
errx(1, "can't write to a header before reading something");
|
1995-03-30 12:49:09 +00:00
|
|
|
++i;
|
|
|
|
writeIfChanged(argv[i], lang, orConsts);
|
|
|
|
} else if (strcmp(argv[i], "-new") == 0) {
|
1997-07-08 11:05:59 +00:00
|
|
|
if (catfile)
|
|
|
|
errx(1, "you must specify -new before the catalog file name");
|
2001-02-08 16:38:16 +00:00
|
|
|
new = TRUE;
|
1995-03-30 12:49:09 +00:00
|
|
|
} else if (strcmp(argv[i], "-or") == 0) {
|
|
|
|
orConsts = ~orConsts;
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!catfile) {
|
|
|
|
catfile = argv[i];
|
|
|
|
if (new) {
|
1997-07-08 11:05:59 +00:00
|
|
|
if ((ofd = open(catfile, O_WRONLY|O_TRUNC|O_CREAT, 0666)) < 0)
|
|
|
|
errx(1, "unable to create a new %s", catfile);
|
1995-03-30 12:49:09 +00:00
|
|
|
} else if ((ofd = open(catfile, O_RDONLY)) < 0) {
|
1997-07-08 11:05:59 +00:00
|
|
|
if ((ofd = open(catfile, O_WRONLY|O_CREAT, 0666)) < 0)
|
|
|
|
errx(1, "unable to create %s", catfile);
|
1995-03-30 12:49:09 +00:00
|
|
|
} else {
|
|
|
|
MCReadCat(ofd);
|
|
|
|
close(ofd);
|
1997-07-08 11:05:59 +00:00
|
|
|
if ((ofd = open(catfile, O_WRONLY|O_TRUNC)) < 0)
|
|
|
|
errx(1, "unable to truncate %s", catfile);
|
1995-03-30 12:49:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
input = argv[i];
|
1997-07-08 11:05:59 +00:00
|
|
|
if ((ifd = open(input, O_RDONLY)) < 0)
|
|
|
|
errx(1, "unable to read %s", input);
|
1995-03-30 12:49:09 +00:00
|
|
|
MCParse(ifd);
|
|
|
|
close(ifd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (catfile) {
|
|
|
|
MCWriteCat(ofd);
|
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
1999-04-25 22:37:58 +00:00
|
|
|
return 0;
|
1995-03-30 12:49:09 +00:00
|
|
|
}
|
|
|
|
|
2001-02-08 16:38:16 +00:00
|
|
|
static void
|
|
|
|
writeIfChanged(char *fname, int lang, int orConsts)
|
1995-03-30 12:49:09 +00:00
|
|
|
{
|
2002-05-29 14:23:10 +00:00
|
|
|
char tmpname[] = _PATH_TMP"/gencat.XXXXXX";
|
1995-03-30 12:49:09 +00:00
|
|
|
char buf[BUFSIZ], tbuf[BUFSIZ], *cptr, *tptr;
|
|
|
|
int fd, tfd;
|
2001-02-08 16:38:16 +00:00
|
|
|
int diff = FALSE;
|
1997-07-08 11:05:59 +00:00
|
|
|
int len, tlen;
|
1995-03-30 12:49:09 +00:00
|
|
|
struct stat sbuf;
|
|
|
|
|
|
|
|
/* If it doesn't exist, just create it */
|
|
|
|
if (stat(fname, &sbuf)) {
|
1997-07-08 11:05:59 +00:00
|
|
|
if ((fd = open(fname, O_WRONLY|O_CREAT, 0666)) < 0)
|
|
|
|
errx(1, "unable to create header file %s", fname);
|
1995-03-30 12:49:09 +00:00
|
|
|
MCWriteConst(fd, lang, orConsts);
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If it does exist, create a temp file for now */
|
2002-05-29 14:23:10 +00:00
|
|
|
if ((tfd = mkstemp(tmpname)) < 0)
|
|
|
|
err(1, "mkstemp");
|
1995-03-30 12:49:09 +00:00
|
|
|
unlink(tmpname);
|
|
|
|
|
|
|
|
/* Write to the temp file and rewind */
|
|
|
|
MCWriteConst(tfd, lang, orConsts);
|
|
|
|
|
|
|
|
/* Open the real header file */
|
1997-07-08 11:05:59 +00:00
|
|
|
if ((fd = open(fname, O_RDONLY)) < 0)
|
|
|
|
errx(1, "unable to read header file: %s", fname);
|
1995-03-30 12:49:09 +00:00
|
|
|
|
|
|
|
/* Backup to the start of the temp file */
|
2001-02-08 16:38:16 +00:00
|
|
|
if (lseek(tfd, (off_t)0, L_SET) < 0)
|
1997-07-08 11:05:59 +00:00
|
|
|
errx(1, "unable to seek in tempfile: %s", tmpname);
|
1995-03-30 12:49:09 +00:00
|
|
|
|
|
|
|
/* Now compare them */
|
|
|
|
while ((tlen = read(tfd, tbuf, BUFSIZ)) > 0) {
|
|
|
|
if ((len = read(fd, buf, BUFSIZ)) != tlen) {
|
2001-02-08 16:38:16 +00:00
|
|
|
diff = TRUE;
|
1995-03-30 12:49:09 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
for (cptr = buf, tptr = tbuf; cptr < buf+len; ++cptr, ++tptr) {
|
|
|
|
if (*tptr != *cptr) {
|
2001-02-08 16:38:16 +00:00
|
|
|
diff = TRUE;
|
1995-03-30 12:49:09 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
done:
|
1995-03-30 12:49:09 +00:00
|
|
|
if (diff) {
|
2001-02-08 16:38:16 +00:00
|
|
|
if (lseek(tfd, (off_t)0, L_SET) < 0)
|
1997-07-08 11:05:59 +00:00
|
|
|
errx(1, "unable to seek in tempfile: %s", tmpname);
|
1995-03-30 12:49:09 +00:00
|
|
|
close(fd);
|
1997-07-08 11:05:59 +00:00
|
|
|
if ((fd = open(fname, O_WRONLY|O_TRUNC)) < 0)
|
|
|
|
errx(1, "unable to truncate header file: %s", fname);
|
1995-03-30 12:49:09 +00:00
|
|
|
while ((len = read(tfd, buf, BUFSIZ)) > 0) {
|
2001-02-08 16:38:16 +00:00
|
|
|
if (write(fd, buf, (size_t)len) != len)
|
|
|
|
warnx("error writing to header file: %s", fname);
|
1995-03-30 12:49:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
close(tfd);
|
|
|
|
}
|