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