strfile.c revision 1.32 1 /* $NetBSD: strfile.c,v 1.32 2011/08/17 18:16:51 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.32 2011/08/17 18:16:51 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 void add_offset(FILE *, off_t);
151 int cmp_str(const void *, const void *);
152 void do_order(void);
153 void fwrite_be_offt(off_t, FILE *);
154 void getargs(int, char *[]);
155 int main(int, char *[]);
156 void randomize(void);
157 void usage(void) NORETURN;
158
159
160 /*
161 * main:
162 * Drive the sucker. There are two main modes -- either we store
163 * the seek pointers, if the table is to be sorted or randomized,
164 * or we write the pointer directly to the file, if we are to stay
165 * in file order. If the former, we allocate and re-allocate in
166 * CHUNKSIZE blocks; if the latter, we just write each pointer,
167 * and then seek back to the beginning to write in the table.
168 */
169 int
170 main(int ac, char **av)
171 {
172 char *sp, dc;
173 FILE *inf, *outf;
174 off_t last_off, length, pos, *p;
175 int first, cnt;
176 char *nsp;
177 STR *fp;
178 static char string[257];
179
180 /* sanity test */
181 if (sizeof(uint32_t) != 4)
182 errx(1, "sizeof(uint32_t) != 4");
183
184 getargs(ac, av); /* evalute arguments */
185 dc = Delimch;
186 if ((inf = fopen(Infile, "r")) == NULL)
187 err(1, "open `%s'", Infile);
188
189 if ((outf = fopen(Outfile, "w")) == NULL)
190 err(1, "open `%s'", Outfile);
191 if (!STORING_PTRS)
192 (void) fseek(outf, sizeof Tbl, SEEK_SET);
193
194 /*
195 * Write the strings onto the file
196 */
197
198 Tbl.str_longlen = 0;
199 Tbl.str_shortlen = (unsigned int) 0x7fffffff;
200 Tbl.str_delim = dc;
201 Tbl.str_version = VERSION;
202 first = Oflag;
203 add_offset(outf, ftell(inf));
204 last_off = 0;
205 do {
206 sp = fgets(string, 256, inf);
207 if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) {
208 pos = ftell(inf);
209 length = pos - last_off - (sp ? strlen(sp) : 0);
210 last_off = pos;
211 if (!length)
212 continue;
213 add_offset(outf, pos);
214 if ((off_t)Tbl.str_longlen < length)
215 Tbl.str_longlen = length;
216 if ((off_t)Tbl.str_shortlen > length)
217 Tbl.str_shortlen = length;
218 first = Oflag;
219 }
220 else if (first) {
221 for (nsp = sp; !isalnum((unsigned char)*nsp); nsp++)
222 continue;
223 ALLOC(Firstch, Num_pts);
224 fp = &Firstch[Num_pts - 1];
225 if (Iflag && isupper((unsigned char)*nsp))
226 fp->first = tolower((unsigned char)*nsp);
227 else
228 fp->first = *nsp;
229 fp->pos = Seekpts[Num_pts - 1];
230 first = FALSE;
231 }
232 } while (sp != NULL);
233
234 /*
235 * write the tables in
236 */
237
238 (void) fclose(inf);
239
240 if (Oflag)
241 do_order();
242 else if (Rflag)
243 randomize();
244
245 if (Xflag)
246 Tbl.str_flags |= STR_ROTATED;
247
248 if (!Sflag) {
249 printf("\"%s\" created\n", Outfile);
250 if (Num_pts == 2)
251 puts("There was 1 string");
252 else
253 printf("There were %d strings\n", (int)(Num_pts - 1));
254 printf("Longest string: %lu byte%s\n", (unsigned long)Tbl.str_longlen,
255 Tbl.str_longlen == 1 ? "" : "s");
256 printf("Shortest string: %lu byte%s\n", (unsigned long)Tbl.str_shortlen,
257 Tbl.str_shortlen == 1 ? "" : "s");
258 }
259
260 (void) fseek(outf, (off_t) 0, SEEK_SET);
261 Tbl.str_version = h2nl(Tbl.str_version);
262 Tbl.str_numstr = h2nl(Num_pts - 1);
263 Tbl.str_longlen = h2nl(Tbl.str_longlen);
264 Tbl.str_shortlen = h2nl(Tbl.str_shortlen);
265 Tbl.str_flags = h2nl(Tbl.str_flags);
266 (void) fwrite((char *) &Tbl, sizeof Tbl, 1, outf);
267 if (STORING_PTRS) {
268 for (p = Seekpts, cnt = Num_pts; cnt--; ++p)
269 fwrite_be_offt(*p, outf);
270 }
271 fflush(outf);
272 if (ferror(outf))
273 err(1, "fwrite %s", Outfile);
274 (void) fclose(outf);
275 exit(0);
276 }
277
278 /*
279 * This routine evaluates arguments from the command line
280 */
281 static void
282 getargs(int argc, char **argv)
283 {
284 int ch;
285 extern int optind;
286 extern char *optarg;
287
288 while ((ch = getopt(argc, argv, "c:iorsx")) != -1)
289 switch(ch) {
290 case 'c': /* new delimiting char */
291 Delimch = *optarg;
292 if (!isascii(Delimch)) {
293 printf("bad delimiting character: '\\%o\n'",
294 Delimch);
295 }
296 break;
297 case 'i': /* ignore case in ordering */
298 Iflag++;
299 break;
300 case 'o': /* order strings */
301 Oflag++;
302 break;
303 case 'r': /* randomize pointers */
304 Rflag++;
305 break;
306 case 's': /* silent */
307 Sflag++;
308 break;
309 case 'x': /* set the rotated bit */
310 Xflag++;
311 break;
312 case '?':
313 default:
314 usage();
315 }
316 argv += optind;
317
318 if (*argv) {
319 Infile = *argv;
320 if (*++argv)
321 (void) strcpy(Outfile, *argv);
322 }
323 if (!Infile) {
324 puts("No input file name");
325 usage();
326 }
327 if (*Outfile == '\0') {
328 (void) strcpy(Outfile, Infile);
329 (void) strcat(Outfile, ".dat");
330 }
331 }
332
333 static void
334 usage(void)
335 {
336 (void) fprintf(stderr,
337 "Usage: %s [-iorsx] [-c char] sourcefile [datafile]\n",
338 getprogname());
339 exit(1);
340 }
341
342 /*
343 * add_offset:
344 * Add an offset to the list, or write it out, as appropriate.
345 */
346 static void
347 add_offset(FILE *fp, off_t off)
348 {
349
350 if (!STORING_PTRS) {
351 fwrite_be_offt(off, fp);
352 } else {
353 ALLOC(Seekpts, Num_pts + 1);
354 Seekpts[Num_pts] = off;
355 }
356 Num_pts++;
357 }
358
359 /*
360 * do_order:
361 * Order the strings alphabetically (possibly ignoring case).
362 */
363 static void
364 do_order(void)
365 {
366 int i;
367 off_t *lp;
368 STR *fp;
369
370 Sort_1 = fopen(Infile, "r");
371 Sort_2 = fopen(Infile, "r");
372 qsort((char *) Firstch, (int) Tbl.str_numstr, sizeof *Firstch, cmp_str);
373 i = Tbl.str_numstr;
374 lp = Seekpts;
375 fp = Firstch;
376 while (i--)
377 *lp++ = fp++->pos;
378 (void) fclose(Sort_1);
379 (void) fclose(Sort_2);
380 Tbl.str_flags |= STR_ORDERED;
381 }
382
383 static int
384 cmp_str(const void *vp1, const void *vp2)
385 {
386 const STR *p1, *p2;
387 int c1, c2;
388 int n1, n2;
389
390 p1 = (const STR *)vp1;
391 p2 = (const STR *)vp2;
392
393 # define SET_N(nf,ch) (nf = (ch == '\n'))
394 # define IS_END(ch,nf) (ch == Delimch && nf)
395
396 c1 = p1->first;
397 c2 = p2->first;
398 if (c1 != c2)
399 return c1 - c2;
400
401 (void) fseek(Sort_1, p1->pos, SEEK_SET);
402 (void) fseek(Sort_2, p2->pos, SEEK_SET);
403
404 n1 = FALSE;
405 n2 = FALSE;
406 while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0')
407 SET_N(n1, c1);
408 while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0')
409 SET_N(n2, c2);
410
411 while (!IS_END(c1, n1) && !IS_END(c2, n2)) {
412 if (Iflag) {
413 if (isupper(c1))
414 c1 = tolower(c1);
415 if (isupper(c2))
416 c2 = tolower(c2);
417 }
418 if (c1 != c2)
419 return c1 - c2;
420 SET_N(n1, c1);
421 SET_N(n2, c2);
422 c1 = getc(Sort_1);
423 c2 = getc(Sort_2);
424 }
425 if (IS_END(c1, n1))
426 c1 = 0;
427 if (IS_END(c2, n2))
428 c2 = 0;
429 return c1 - c2;
430 }
431
432 /*
433 * randomize:
434 * Randomize the order of the string table. We must be careful
435 * not to randomize across delimiter boundaries. All
436 * randomization is done within each block.
437 */
438 static void
439 randomize(void)
440 {
441 int cnt, i;
442 off_t tmp;
443 off_t *sp;
444
445 srandom((int)(time((time_t *) NULL) + getpid()));
446
447 Tbl.str_flags |= STR_RANDOM;
448 cnt = Tbl.str_numstr;
449
450 /*
451 * move things around randomly
452 */
453
454 for (sp = Seekpts; cnt > 0; cnt--, sp++) {
455 i = random() % cnt;
456 tmp = sp[0];
457 sp[0] = sp[i];
458 sp[i] = tmp;
459 }
460 }
461
462 /*
463 * fwrite_be_offt:
464 * Write out the off paramater as a 64 bit big endian number
465 */
466
467 static void
468 fwrite_be_offt(off_t off, FILE *f)
469 {
470 int i;
471 unsigned char c[8];
472
473 for (i = 7; i >= 0; i--) {
474 c[i] = off & 0xff;
475 off >>= 8;
476 }
477 fwrite(c, sizeof(c), 1, f);
478 }
479
480 static uint32_t
481 h2nl(uint32_t h)
482 {
483 unsigned char c[4];
484 uint32_t rv;
485
486 c[0] = (h >> 24) & 0xff;
487 c[1] = (h >> 16) & 0xff;
488 c[2] = (h >> 8) & 0xff;
489 c[3] = (h >> 0) & 0xff;
490 memcpy(&rv, c, sizeof rv);
491
492 return (rv);
493 }
494