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