summaryrefslogtreecommitdiff
path: root/lang/langcomp.c
blob: 772cacfede1fc2da6dd3ab98bf4bbcdad6e3c9f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* Compiler for language definition files.
 *
 * (C) 2003-2011 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for further details.
 *
 * Based on the original code of Epona by Lara.
 * Based on the original code of Services by Andy Church.
 * 
 *
 */

/*
 * A language definition file contains all strings which Services sends to
 * users in a particular language.  A language file may contain comments
 * (lines beginning with "#") and blank lines.  All other lines must adhere
 * to the following format:
 *
 * Each string definition begins with the C name of a message (as defined
 * in the file "index"--see below).  This must be alone on a line, preceded
 * and followed by no blank space.  Following this line are zero or more
 * lines of text; each line of text must begin with exactly one tab
 * character, which is discarded.  Newlines are retained in the strings,
 * except the last newline in the text, which is discarded.  A message with
 * no text is replaced by a null pointer in the array (not an empty
 * string).
 *
 * All messages in the program are listed, one per line, in the "index"
 * file.  No comments or blank lines are permitted in that file.  The index
 * file can be generated from a language file with a command like:
 *	grep '^[A-Z]' en_us.l >index
 *
 * This program takes one parameter, the name of the language file.  It
 * generates a compiled language file whose name is created by removing any
 * extension on the source file on the input filename.
 *
 * You may also pass a "-w" option to print warnings for missing strings.
 *
 * This program isn't very flexible, because it doesn't need to be, but
 * anyone who wants to try making it more flexible is welcome to.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#undef getline

int numstrings = 0;	/* Number of strings we should have */
char **stringnames;	/* Names of the strings (from index file) */
char **strings;		/* Strings we have loaded */

int linenum = 0;	/* Current line number in input file */

#ifdef _WIN32
#define snprintf _snprintf
#endif

char *anopeStrDup(const char *src);

/*************************************************************************/

/* Read the index file and load numstrings and stringnames.  Return -1 on
 * error, 0 on success. */

int read_index_file()
{
    FILE *f;
    char buf[256];
    int i;

    if (!(f = fopen("index", "rb"))) {
	perror("fopen(index)");
	return -1;
    }
    while (fgets(buf, sizeof(buf), f))
	numstrings++;
    if (!(stringnames = calloc(sizeof(char *), numstrings))) {
	perror("calloc(stringnames)");
	fclose(f);
	return -1;
    }
    if (!(strings = calloc(sizeof(char *), numstrings))) {
	perror("calloc(strings)");
	fclose(f);
	return -1;
    }
    fseek(f, 0, SEEK_SET);
    i = 0;
    while (fgets(buf, sizeof(buf), f)) {
	if (buf[strlen(buf)-1] == '\n')
	    buf[strlen(buf)-1] = '\0';
	if (!(stringnames[i++] = anopeStrDup(buf))) {
	    perror("strdup()");
	    fclose(f);
	    return -1;
	}
    }
    fclose(f);
    return 0;
}

/*************************************************************************/

/* Return the index of a string name in stringnames, or -1 if not found. */

int stringnum(const char *name)
{
    int i;

    for (i = 0; i < numstrings; i++) {
	if (strcmp(stringnames[i], name) == 0)
	    return i;
    }
    return -1;
}

/*************************************************************************/

/* Read a non-comment, non-blank line from the input file.  Return NULL at
 * end of file. */

char *ano_getline(FILE *f)
{
    static char buf[1024];
    char *s;

    do {
	if (!(fgets(buf, sizeof(buf), f)))
	    return NULL;
	linenum++;
    } while (*buf == '#' || *buf == '\n');
    s = buf + strlen(buf)-1;
    if (*s == '\n')
	*s = '\0';
    return buf;
}

/*************************************************************************/

/* Write a 32-bit value to a file in big-endian order. */

int fput32(int val, FILE *f)
{
    if (fputc(val>>24, f) < 0 ||
        fputc(val>>16, f) < 0 ||
        fputc(val>> 8, f) < 0 ||
        fputc(val    , f) < 0
    ) {
	return -1;
    } else {
	return 0;
    }
}

/*************************************************************************/
char *anopeStrDup(const char *src) {
    char *ret=NULL;
    if(src) {
        if( (ret = (char *)malloc(strlen(src)+1)) ) {;
            strcpy(ret,src);
        }
    }
    return ret;
}

/*************************************************************************/
int main(int ac, char **av)
{
    char *filename = NULL, *s;
    char langname[254], outfile[256];
    FILE *in, *out;
    int warn = 0;
    int retval = 0;
    int curstring = -2, i;
    char *line;
    int pos;
    int maxerr = 50;	/* Max errors before we bail out */

    if (ac >= 2 && strcmp(av[1], "-w") == 0) {
	warn = 1;
	av[1] = av[2];
	ac--;
    }
    if (ac != 2) {
	fprintf(stderr, "Usage: %s [-w] <lang-file>\n", av[0]);
	return 1;
    }
    filename = av[1];
    s = strrchr(filename, '.');
    if (!s)
	s = filename + strlen(filename);
    if (s-filename > sizeof(langname)-3)
	s = filename + sizeof(langname)-1;
    strncpy(langname, filename, s-filename);
    langname[s-filename] = '\0';
    snprintf(outfile, sizeof(outfile), "%s", langname);

    if (read_index_file() < 0)
	return 1;
    if (!(in = fopen(filename, "rb"))) {
	perror(filename);
	return 1;
    }
    if (!(out = fopen(outfile, "wb"))) {
	perror(outfile);
	fclose(in);
	return 1;
    }

    while (maxerr > 0 && (line = ano_getline(in)) != NULL) {
	if (*line == '\t') {
	    if (curstring == -2) {
		fprintf(stderr, "%s:%d: Junk at beginning of file\n",
			filename, linenum);
		retval = 1;
	    } else if (curstring >= 0) {
		line++;
		i = strings[curstring] ? strlen(strings[curstring]) : 0;
		if (!(strings[curstring] =
			realloc(strings[curstring], i+strlen(line)+2))) {
		    fprintf(stderr, "%s:%d: Out of memory!\n",filename,linenum);
		    return 2;
		}
		sprintf(strings[curstring]+i, "%s\n", line);
	    }
	} else {

	    if ((curstring = stringnum(line)) < 0) {
		fprintf(stderr, "%s:%d: Unknown string name `%s'\n",
			filename, linenum, line);
		retval = 1;
		maxerr--;
	    } else if (strings[curstring]) {
		fprintf(stderr, "%s:%d: Duplicate occurrence of string `%s'\n",
			filename, linenum, line);
		retval = 1;
		maxerr--;
	    } else {
		if (!(strings[curstring] = malloc(1))) {
		    fprintf(stderr, "%s:%d: Out of memory!\n",filename,linenum);
		    return 2;
		}
		*strings[curstring] = '\0';
	    }

	    if (maxerr == 0)
		fprintf(stderr, "%s:%d: Too many errors!\n", filename, linenum);
	    
	}
    }

    fput32(numstrings, out);
    pos = numstrings * 8 + 4;
    for (i = 0; i < numstrings; i++) {
	int len = strings[i] && *strings[i] ? strlen(strings[i])-1 : 0;
	fput32(pos, out);
	fput32(len, out);
	pos += len;
    }
    for (i = 0; i < numstrings; i++) {
	if (strings[i]) {
	    if (*strings[i])
		strings[i][strlen(strings[i])-1] = '\0';   /* kill last \n */
	    if (*strings[i])
		fputs(strings[i], out);
	} else if (warn) {
	    fprintf(stderr, "%s: String `%s' missing\n", filename,
			stringnames[i]);
	}
    }

    fclose(in);
    fclose(out);
    return retval;
}

/*************************************************************************/