strfile.c revision 1.36 1 /* $NetBSD: strfile.c,v 1.36 2013/09/18 23:18:14 uwe Exp $ */
2
3 /*-
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Ken Arnold.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
38
39 #ifdef __NetBSD__
40 #include <sys/cdefs.h>
41 #ifndef lint
42 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
43 The Regents of the University of California. All rights reserved.");
44 #endif /* not lint */
45
46 #ifndef lint
47 #if 0
48 static char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
49 #else
50 __RCSID("$NetBSD: strfile.c,v 1.36 2013/09/18 23:18:14 uwe Exp $");
51 #endif
52 #endif /* not lint */
53 #endif /* __NetBSD__ */
54
55 /* n.b.: this file is used at build-time - i.e. during build.sh. */
56
57 #include <sys/types.h>
58 #include <sys/param.h>
59 #include <ctype.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <time.h>
64 #include <unistd.h>
65 #include <inttypes.h>
66 #include <err.h>
67
68 #include "strfile.h"
69
70 #ifndef MAXPATHLEN
71 #define MAXPATHLEN 1024
72 #endif /* MAXPATHLEN */
73
74 #if defined(__NetBSD__) || defined(__dead)
75 #define NORETURN __dead
76 #elif defined __GNUC__
77 #define NORETURN __attribute__((__noreturn__))
78 #else
79 #define NORETURN
80 #endif
81
82 /*
83 * This program takes a file composed of strings separated by
84 * lines starting with two consecutive delimiting character (default
85 * character is '%') and creates another file which consists of a table
86 * describing the file (structure from "strfile.h"), a table of seek
87 * pointers to the start of the strings, and the strings, each terminated
88 * by a null byte. Usage:
89 *
90 * % strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
91 *
92 * c - Change delimiting character from '%' to 'C'
93 * s - Silent. Give no summary of data processed at the end of
94 * the run.
95 * o - order the strings in alphabetic order
96 * i - if ordering, ignore case
97 * r - randomize the order of the strings
98 * x - set rotated bit
99 *
100 * Ken Arnold Sept. 7, 1978 --
101 *
102 * Added ordering options.
103 */
104
105 # define TRUE 1
106 # define FALSE 0
107
108 # define STORING_PTRS (Oflag || Rflag)
109 # define CHUNKSIZE 512
110
111 # define ALLOC(ptr,sz) do { \
112 if (ptr == NULL) \
113 ptr = malloc(CHUNKSIZE * sizeof *ptr); \
114 else if (((sz) + 1) % CHUNKSIZE == 0) \
115 ptr = realloc(ptr, ((sz) + CHUNKSIZE) * sizeof *ptr); \
116 if (ptr == NULL) \
117 err(1, "out of space"); \
118 } while (0)
119
120 typedef struct {
121 char first;
122 off_t pos;
123 } STR;
124
125 static char *Infile = NULL; /* input file name */
126 static char Outfile[MAXPATHLEN] = ""; /* output file name */
127 static char Delimch = '%'; /* delimiting character */
128
129 static int Sflag = FALSE; /* silent run flag */
130 static int Oflag = FALSE; /* ordering flag */
131 static int Iflag = FALSE; /* ignore case flag */
132 static int Rflag = FALSE; /* randomize order flag */
133 static int Xflag = FALSE; /* set rotated bit */
134 static long Num_pts = 0; /* number of pointers/strings */
135
136 static off_t *Seekpts;
137
138 static FILE *Sort_1, *Sort_2; /* pointers for sorting */
139
140 static STRFILE Tbl; /* statistics table */
141
142 static STR *Firstch; /* first chars of each string */
143
144
145 static uint32_t h2nl(uint32_t h);
146 static void getargs(int argc, char **argv);
147 static void usage(void) NORETURN;
148 static void add_offset(FILE *fp, off_t off);
149 static void do_order(void);
150 static int cmp_str(const void *vp1, const void *vp2);
151 static void randomize(void);
152 static void fwrite_be_offt(off_t off, FILE *f);
153
154
155 /*
156 * main:
157 * Drive the sucker. There are two main modes -- either we store
158 * the seek pointers, if the table is to be sorted or randomized,
159 * or we write the pointer directly to the file, if we are to stay
160 * in file order. If the former, we allocate and re-allocate in
161 * CHUNKSIZE blocks; if the latter, we just write each pointer,
162 * and then seek back to the beginning to write in the table.
163 */
164 int
165 main(int ac, char **av)
166 {
167 char *sp, dc;
168 FILE *inf, *outf;
169 off_t last_off, length, pos;
170 int first;
171 char *nsp;
172 STR *fp;
173 static char string[257];
174 long i;
175
176 /* sanity test */
177 if (sizeof(uint32_t) != 4)
178 errx(1, "sizeof(uint32_t) != 4");
179
180 getargs(ac, av); /* evalute arguments */
181 dc = Delimch;
182 if ((inf = fopen(Infile, "r")) == NULL)
183 err(1, "open `%s'", Infile);
184
185 if ((outf = fopen(Outfile, "w")) == NULL)
186 err(1, "open `%s'", Outfile);
187 if (!STORING_PTRS)
188 (void) fseek(outf, sizeof Tbl, SEEK_SET);
189
190 /*
191 * Write the strings onto the file
192 */
193
194 Tbl.str_longlen = 0;
195 Tbl.str_shortlen = (unsigned int) 0x7fffffff;
196 Tbl.str_delim = dc;
197 Tbl.str_version = VERSION;
198 first = Oflag;
199 add_offset(outf, ftell(inf));
200 last_off = 0;
201 do {
202 sp = fgets(string, 256, inf);
203 if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) {
204 pos = ftell(inf);
205 length = pos - last_off - (sp ? strlen(sp) : 0);
206 last_off = pos;
207 if (!length)
208 continue;
209 add_offset(outf, pos);
210 if ((off_t)Tbl.str_longlen < length)
211 Tbl.str_longlen = length;
212 if ((off_t)Tbl.str_shortlen > length)
213 Tbl.str_shortlen = length;
214 first = Oflag;
215 }
216 else if (first) {
217 for (nsp = sp; !isalnum((unsigned char)*nsp); nsp++)
218 continue;
219 ALLOC(Firstch, Num_pts);
220 fp = &Firstch[Num_pts - 1];
221 if (Iflag && isupper((unsigned char)*nsp))
222 fp->first = tolower((unsigned char)*nsp);
223 else
224 fp->first = *nsp;
225 fp->pos = Seekpts[Num_pts - 1];
226 first = FALSE;
227 }
228 } while (sp != NULL);
229
230 /*
231 * write the tables in
232 */
233
234 (void) fclose(inf);
235
236 if (Oflag)
237 do_order();
238 else if (Rflag)
239 randomize();
240
241 if (Xflag)
242 Tbl.str_flags |= STR_ROTATED;
243
244 if (!Sflag) {
245 printf("\"%s\" created\n", Outfile);
246 if (Num_pts == 2)
247 puts("There was 1 string");
248 else
249 printf("There were %d strings\n", (int)(Num_pts - 1));
250 printf("Longest string: %lu byte%s\n", (unsigned long)Tbl.str_longlen,
251 Tbl.str_longlen == 1 ? "" : "s");
252 printf("Shortest string: %lu byte%s\n", (unsigned long)Tbl.str_shortlen,
253 Tbl.str_shortlen == 1 ? "" : "s");
254 }
255
256 (void) fseek(outf, (off_t) 0, SEEK_SET);
257 Tbl.str_version = h2nl(Tbl.str_version);
258 Tbl.str_numstr = h2nl(Num_pts - 1);
259 Tbl.str_longlen = h2nl(Tbl.str_longlen);
260 Tbl.str_shortlen = h2nl(Tbl.str_shortlen);
261 Tbl.str_flags = h2nl(Tbl.str_flags);
262 (void) fwrite((char *) &Tbl, sizeof Tbl, 1, outf);
263 if (STORING_PTRS) {
264 for (i = 0; i < Num_pts; i++)
265 fwrite_be_offt(Seekpts[i], outf);
266 }
267 fflush(outf);
268 if (ferror(outf))
269 err(1, "fwrite %s", Outfile);
270 (void) fclose(outf);
271 exit(0);
272 }
273
274 /*
275 * This routine evaluates arguments from the command line
276 */
277 static void
278 getargs(int argc, char **argv)
279 {
280 int ch;
281 extern int optind;
282 extern char *optarg;
283
284 while ((ch = getopt(argc, argv, "c:iorsx")) != -1)
285 switch(ch) {
286 case 'c': /* new delimiting char */
287 Delimch = *optarg;
288 if (!isascii(Delimch)) {
289 printf("bad delimiting character: '\\%o\n'",
290 Delimch);
291 }
292 break;
293 case 'i': /* ignore case in ordering */
294 Iflag++;
295 break;
296 case 'o': /* order strings */
297 Oflag++;
298 break;
299 case 'r': /* randomize pointers */
300 Rflag++;
301 break;
302 case 's': /* silent */
303 Sflag++;
304 break;
305 case 'x': /* set the rotated bit */
306 Xflag++;
307 break;
308 case '?':
309 default:
310 usage();
311 }
312 argv += optind;
313
314 if (*argv) {
315 Infile = *argv;
316 if (*++argv)
317 (void) strcpy(Outfile, *argv);
318 }
319 if (!Infile) {
320 puts("No input file name");
321 usage();
322 }
323 if (*Outfile == '\0') {
324 (void) strcpy(Outfile, Infile);
325 (void) strcat(Outfile, ".dat");
326 }
327 }
328
329 static void
330 usage(void)
331 {
332 (void) fprintf(stderr,
333 "Usage: %s [-iorsx] [-c char] sourcefile [datafile]\n",
334 getprogname());
335 exit(1);
336 }
337
338 /*
339 * add_offset:
340 * Add an offset to the list, or write it out, as appropriate.
341 */
342 static void
343 add_offset(FILE *fp, off_t off)
344 {
345
346 if (!STORING_PTRS) {
347 fwrite_be_offt(off, fp);
348 } else {
349 ALLOC(Seekpts, Num_pts + 1);
350 Seekpts[Num_pts] = off;
351 }
352 Num_pts++;
353 }
354
355 /*
356 * do_order:
357 * Order the strings alphabetically (possibly ignoring case).
358 */
359 static void
360 do_order(void)
361 {
362 int i;
363 off_t *lp;
364 STR *fp;
365
366 Sort_1 = fopen(Infile, "r");
367 Sort_2 = fopen(Infile, "r");
368 qsort((char *) Firstch, (int) Tbl.str_numstr, sizeof *Firstch, cmp_str);
369 i = Tbl.str_numstr;
370 lp = Seekpts;
371 fp = Firstch;
372 while (i--)
373 *lp++ = fp++->pos;
374 (void) fclose(Sort_1);
375 (void) fclose(Sort_2);
376 Tbl.str_flags |= STR_ORDERED;
377 }
378
379 static int
380 cmp_str(const void *vp1, const void *vp2)
381 {
382 const STR *p1, *p2;
383 int c1, c2;
384 int n1, n2;
385
386 p1 = (const STR *)vp1;
387 p2 = (const STR *)vp2;
388
389 # define SET_N(nf,ch) (nf = (ch == '\n'))
390 # define IS_END(ch,nf) (ch == Delimch && nf)
391
392 c1 = p1->first;
393 c2 = p2->first;
394 if (c1 != c2)
395 return c1 - c2;
396
397 (void) fseek(Sort_1, p1->pos, SEEK_SET);
398 (void) fseek(Sort_2, p2->pos, SEEK_SET);
399
400 n1 = FALSE;
401 n2 = FALSE;
402 while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0')
403 SET_N(n1, c1);
404 while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0')
405 SET_N(n2, c2);
406
407 while (!IS_END(c1, n1) && !IS_END(c2, n2)) {
408 if (Iflag) {
409 if (isupper(c1))
410 c1 = tolower(c1);
411 if (isupper(c2))
412 c2 = tolower(c2);
413 }
414 if (c1 != c2)
415 return c1 - c2;
416 SET_N(n1, c1);
417 SET_N(n2, c2);
418 c1 = getc(Sort_1);
419 c2 = getc(Sort_2);
420 }
421 if (IS_END(c1, n1))
422 c1 = 0;
423 if (IS_END(c2, n2))
424 c2 = 0;
425 return c1 - c2;
426 }
427
428 /*
429 * randomize:
430 * Randomize the order of the string table. We must be careful
431 * not to randomize across delimiter boundaries. All
432 * randomization is done within each block.
433 */
434 static void
435 randomize(void)
436 {
437 int cnt, i;
438 off_t tmp;
439 off_t *sp;
440
441 srandom((int)(time(NULL) + getpid()));
442
443 Tbl.str_flags |= STR_RANDOM;
444 cnt = Tbl.str_numstr;
445
446 /*
447 * move things around randomly
448 */
449
450 for (sp = Seekpts; cnt > 0; cnt--, sp++) {
451 i = random() % cnt;
452 tmp = sp[0];
453 sp[0] = sp[i];
454 sp[i] = tmp;
455 }
456 }
457
458 /*
459 * fwrite_be_offt:
460 * Write out the off paramater as a 64 bit big endian number
461 */
462
463 static void
464 fwrite_be_offt(off_t off, FILE *f)
465 {
466 int i;
467 unsigned char c[8];
468
469 for (i = 7; i >= 0; i--) {
470 c[i] = off & 0xff;
471 off >>= 8;
472 }
473 fwrite(c, sizeof(c), 1, f);
474 }
475
476 static uint32_t
477 h2nl(uint32_t h)
478 {
479 unsigned char c[4];
480 uint32_t rv;
481
482 c[0] = (h >> 24) & 0xff;
483 c[1] = (h >> 16) & 0xff;
484 c[2] = (h >> 8) & 0xff;
485 c[3] = (h >> 0) & 0xff;
486 memcpy(&rv, c, sizeof rv);
487
488 return (rv);
489 }
490