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