#include <stdio.h>
#include <unistd.h>
#ifdef __QNX__
#include <unix.h>
#endif
extern char *index(const char *, int);
extern char *rindex(const char *, int);
#define BUFSIZE 4096
#define whitespace(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
static void
usage(void){
fprintf (stderr, "call is: makemsg input msgout.h msgout.c\n");
fprintf (stderr, "or: makemsg -c input catalog\n");
exit (1);
}
int
main(int argc, char **argv) {
FILE *fin, *foh, *foc;
char *s, *t;
char *infile, *outcfile, *outhfile;
char buf[BUFSIZE];
int defct = 0;
int makecat = 0;
#define getbuf if (fgets (buf, sizeof(buf), fin) == NULL) {\
fprintf (stderr, "makemsg: unexpected end of input\n");\
fprintf (stderr, "[output file(s) removed]\n");\
unlink (outcfile);\
if (!makecat) unlink (outhfile);\
exit (1);\
}
if (argc != 4)
usage ();
outhfile = 0; foh = 0;
if (!strcmp(argv[1], "-c")) {
makecat = 1;
infile = argv[2];
outcfile = argv[3];
} else {
infile = argv[1];
outhfile = argv[2];
outcfile = argv[3];
}
fin = fopen (infile, "r");
if (!fin) {
perror (infile);
fprintf (stderr, "makemsg: cannot open input file %s\n", infile);
usage ();
}
if (!makecat) {
s = rindex(outhfile, '.');
if (!s || s[1] != 'h') {
fprintf (stderr, "defines output file should have name ending in .h\n");
usage ();
}
s = rindex(outcfile, '.');
if (!s || s[1] != 'c') {
fprintf (stderr, "string output file should have name ending in .c\n");
usage ();
}
}
if (!makecat) {
foh = fopen (outhfile, "w");
if (!foh) {
perror (argv[1]);
fprintf (stderr, "makemsg: cannot open output file %s\n", outhfile);
usage ();
}
}
foc = fopen (outcfile, "w");
if (!foc) {
perror (argv[2]);
fprintf (stderr, "makemsg: cannot open output file %s\n", outcfile);
usage ();
}
if (makecat)
fputs ("$quote \"\n$set 1\n", foc);
else
fputs ("char *msg[] = {\n \"\",\n", foc);
while (fgets (buf, sizeof(buf), fin) != NULL) {
char ss;
s = buf;
while (whitespace(*s))
s++;
if (*s == 0)
continue;
t = s;
while (*s && !whitespace(*s))
s++;
ss = *s;
*s = 0;
if (makecat) {
fprintf (foc, "%d ", ++defct);
} else {
fprintf (foh, "#define %s %d\n", t, ++defct);
fprintf (foc, "/* %2d */ ", defct);
}
*s = ss;
while (whitespace(*s) || *s == 0) {
if (*s == 0) {
getbuf;
s = buf;
} else
s++;
}
while ((t = index(s, '\n')) == NULL || (t > buf && t[-1] == '\\')) {
fputs (s, foc);
getbuf;
s = buf;
}
*t = 0;
fputs (s, foc);
if (makecat)
fputs ("\n", foc);
else
fputs (",\n", foc);
}
if (!makecat) {
fputs ("};\n", foc);
fprintf (foh, "\n#define MAXMSG %d\n", defct);
}
if (!makecat) {
fclose (foh);
}
fclose (foc);
fclose (fin);
return 0;
}