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