diff options
Diffstat (limited to 'games/fortune/strfile')
-rw-r--r-- | games/fortune/strfile/Makefile | 8 | ||||
-rw-r--r-- | games/fortune/strfile/Makefile.depend | 18 | ||||
-rw-r--r-- | games/fortune/strfile/strfile.8 | 159 | ||||
-rw-r--r-- | games/fortune/strfile/strfile.c | 462 | ||||
-rw-r--r-- | games/fortune/strfile/strfile.h | 54 |
5 files changed, 0 insertions, 701 deletions
diff --git a/games/fortune/strfile/Makefile b/games/fortune/strfile/Makefile deleted file mode 100644 index a43e8ea..0000000 --- a/games/fortune/strfile/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -# @(#)Makefile 8.1 (Berkeley) 5/31/93 -# $FreeBSD$ - -PROG= strfile -MAN= strfile.8 -MLINKS= strfile.8 unstr.8 - -.include <bsd.prog.mk> diff --git a/games/fortune/strfile/Makefile.depend b/games/fortune/strfile/Makefile.depend deleted file mode 100644 index 3646e2e..0000000 --- a/games/fortune/strfile/Makefile.depend +++ /dev/null @@ -1,18 +0,0 @@ -# $FreeBSD$ -# Autogenerated - do NOT edit! - -DIRDEPS = \ - gnu/lib/csu \ - gnu/lib/libgcc \ - include \ - include/xlocale \ - lib/${CSU_DIR} \ - lib/libc \ - lib/libcompiler_rt \ - - -.include <dirdeps.mk> - -.if ${DEP_RELDIR} == ${_DEP_RELDIR} -# local dependencies - needed for -jN in clean tree -.endif diff --git a/games/fortune/strfile/strfile.8 b/games/fortune/strfile/strfile.8 deleted file mode 100644 index 26de0d7..0000000 --- a/games/fortune/strfile/strfile.8 +++ /dev/null @@ -1,159 +0,0 @@ -.\" Copyright (c) 1989, 1991, 1993 -.\" The Regents of the University of California. All rights reserved. -.\" -.\" -.\" This code is derived from software contributed to Berkeley by -.\" Ken Arnold. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" @(#)strfile.8 8.1 (Berkeley) 6/9/93 -.\" $FreeBSD$ -.\" -.Dd February 17, 2005 -.Dt STRFILE 8 -.Os -.Sh NAME -.Nm strfile , -.Nm unstr -.Nd "create a random access file for storing strings" -.Sh SYNOPSIS -.Nm -.Op Fl Ciorsx -.Op Fl c Ar char -.Ar source_file -.Op Ar output_file -.Nm unstr -.Ar source_file -.Sh DESCRIPTION -The -.Nm -utility -reads a file containing groups of lines separated by a line containing -a single percent -.Ql % -sign and creates a data file which contains -a header structure and a table of file offsets for each group of lines. -This allows random access of the strings. -.Pp -The output file, if not specified on the command line, is named -.Ar source_file Ns Pa .dat . -.Pp -The options are as follows: -.Bl -tag -width ".Fl c Ar char" -.It Fl C -Flag the file as containing comments. -This option cases the -.Dv STR_COMMENTS -bit in the header -.Va str_flags -field to be set. -Comments are designated by two delimiter characters at the -beginning of the line, though -.Nm -does not give any special -treatment to comment lines. -.It Fl c Ar char -Change the delimiting character from the percent sign to -.Ar char . -.It Fl i -Ignore case when ordering the strings. -.It Fl o -Order the strings in alphabetical order. -The offset table will be sorted in the alphabetical order of the -groups of lines referenced. -Any initial non-alphanumeric characters are ignored. -This option causes the -.Dv STR_ORDERED -bit in the header -.Va str_flags -field to be set. -.It Fl r -Randomize access to the strings. -Entries in the offset table will be randomly ordered. -This option causes the -.Dv STR_RANDOM -bit in the header -.Va str_flags -field to be set. -.It Fl s -Run silently; do not give a summary message when finished. -.It Fl x -Note that each alphabetic character in the groups of lines is rotated -13 positions in a simple caesar cypher. -This option causes the -.Dv STR_ROTATED -bit in the header -.Va str_flags -field to be set. -.El -.Pp -The format of the header is: -.Bd -literal -#define VERSION 1 -uint32_t str_version; /* version number */ -uint32_t str_numstr; /* # of strings in the file */ -uint32_t str_longlen; /* length of longest string */ -uint32_t str_shortlen; /* length of shortest string */ -#define STR_RANDOM 0x1 /* randomized pointers */ -#define STR_ORDERED 0x2 /* ordered pointers */ -#define STR_ROTATED 0x4 /* rot-13'd text */ -#define STR_COMMENTS 0x8 /* embedded comments */ -uint32_t str_flags; /* bit field for flags */ -char str_delim; /* delimiting character */ -.Ed -.Pp -All fields are written in network byte order. -.Pp -The purpose of -.Nm unstr -is to undo the work of -.Nm . -It prints out the strings contained in the file -.Ar source_file -in the order that they are listed in -the header file -.Ar source_file Ns Pa .dat -to standard output. -It is possible to create sorted versions of input files by using -.Fl o -when -.Nm -is run and then using -.Nm unstr -to dump them out in the table order. -.Sh FILES -.Bl -tag -width ".Pa strfile.dat" -compact -.It Pa strfile.dat -default output file. -.El -.Sh SEE ALSO -.Xr byteorder 3 , -.Xr fortune 6 -.Sh HISTORY -The -.Nm -utility first appeared in -.Bx 4.4 . diff --git a/games/fortune/strfile/strfile.c b/games/fortune/strfile/strfile.c deleted file mode 100644 index c88d997..0000000 --- a/games/fortune/strfile/strfile.c +++ /dev/null @@ -1,462 +0,0 @@ -/*- - * Copyright (c) 1989, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Ken Arnold. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#if 0 -#ifndef lint -static const char copyright[] = -"@(#) Copyright (c) 1989, 1993\n\ - The Regents of the University of California. All rights reserved.\n"; -#endif /* not lint */ - -#ifndef lint -static const char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93"; -#endif /* not lint */ -#endif -#include <sys/cdefs.h> -__FBSDID("$FreeBSD$"); - -#include <sys/param.h> -#include <sys/endian.h> -#include <ctype.h> -#include <locale.h> -#include <stdbool.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <time.h> -#include <unistd.h> - -#include "strfile.h" - -/* - * This program takes a file composed of strings separated by - * lines starting with two consecutive delimiting character (default - * character is '%') and creates another file which consists of a table - * describing the file (structure from "strfile.h"), a table of seek - * pointers to the start of the strings, and the strings, each terminated - * by a null byte. Usage: - * - * % strfile [-iorsx] [ -cC ] sourcefile [ datafile ] - * - * C - Allow comments marked by a double delimiter at line's beginning - * c - Change delimiting character from '%' to 'C' - * s - Silent. Give no summary of data processed at the end of - * the run. - * o - order the strings in alphabetic order - * i - if ordering, ignore case - * r - randomize the order of the strings - * x - set rotated bit - * - * Ken Arnold Sept. 7, 1978 -- - * - * Added ordering options. - */ - -#define STORING_PTRS (Oflag || Rflag) -#define CHUNKSIZE 512 - -#define ALLOC(ptr, sz) do { \ - if (ptr == NULL) \ - ptr = malloc(CHUNKSIZE * sizeof(*ptr)); \ - else if (((sz) + 1) % CHUNKSIZE == 0) \ - ptr = realloc(ptr, ((sz) + CHUNKSIZE) * sizeof(*ptr)); \ - if (ptr == NULL) { \ - fprintf(stderr, "out of space\n"); \ - exit(1); \ - } \ - } while (0) - -typedef struct { - int first; - off_t pos; -} STR; - -static char *Infile = NULL, /* input file name */ - Outfile[MAXPATHLEN] = "", /* output file name */ - Delimch = '%'; /* delimiting character */ - -static int Cflag = false; /* embedded comments */ -static int Sflag = false; /* silent run flag */ -static int Oflag = false; /* ordering flag */ -static int Iflag = false; /* ignore case flag */ -static int Rflag = false; /* randomize order flag */ -static int Xflag = false; /* set rotated bit */ -static uint32_t Num_pts = 0; /* number of pointers/strings */ - -static off_t *Seekpts; - -static FILE *Sort_1, *Sort_2; /* pointers for sorting */ - -static STRFILE Tbl; /* statistics table */ - -static STR *Firstch; /* first chars of each string */ - -static void add_offset(FILE *, off_t); -static int cmp_str(const void *, const void *); -static int stable_collate_range_cmp(int, int); -static void do_order(void); -static void getargs(int, char **); -static void randomize(void); -static void usage(void); - -/* - * main: - * Drive the sucker. There are two main modes -- either we store - * the seek pointers, if the table is to be sorted or randomized, - * or we write the pointer directly to the file, if we are to stay - * in file order. If the former, we allocate and re-allocate in - * CHUNKSIZE blocks; if the latter, we just write each pointer, - * and then seek back to the beginning to write in the table. - */ -int -main(int ac, char *av[]) -{ - char *sp, *nsp, dc; - FILE *inf, *outf; - off_t last_off, pos, *p; - size_t length; - int first; - uint32_t cnt; - STR *fp; - static char string[257]; - - setlocale(LC_ALL, ""); - - getargs(ac, av); /* evalute arguments */ - dc = Delimch; - if ((inf = fopen(Infile, "r")) == NULL) { - perror(Infile); - exit(1); - } - - if ((outf = fopen(Outfile, "w")) == NULL) { - perror(Outfile); - exit(1); - } - if (!STORING_PTRS) - fseek(outf, (long)sizeof(Tbl), SEEK_SET); - - /* - * Write the strings onto the file - */ - - Tbl.str_longlen = 0; - Tbl.str_shortlen = 0xffffffff; - Tbl.str_delim = dc; - Tbl.str_version = VERSION; - first = Oflag; - add_offset(outf, ftello(inf)); - last_off = 0; - do { - sp = fgets(string, 256, inf); - if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) { - pos = ftello(inf); - length = (size_t)(pos - last_off) - - (sp != NULL ? strlen(sp) : 0); - last_off = pos; - if (length == 0) - continue; - add_offset(outf, pos); - if ((size_t)Tbl.str_longlen < length) - Tbl.str_longlen = length; - if ((size_t)Tbl.str_shortlen > length) - Tbl.str_shortlen = length; - first = Oflag; - } - else if (first) { - for (nsp = sp; !isalnum((unsigned char)*nsp); nsp++) - continue; - ALLOC(Firstch, Num_pts); - fp = &Firstch[Num_pts - 1]; - if (Iflag && isupper((unsigned char)*nsp)) - fp->first = tolower((unsigned char)*nsp); - else - fp->first = *nsp; - fp->pos = Seekpts[Num_pts - 1]; - first = false; - } - } while (sp != NULL); - - /* - * write the tables in - */ - - fclose(inf); - Tbl.str_numstr = Num_pts - 1; - - if (Cflag) - Tbl.str_flags |= STR_COMMENTS; - - if (Oflag) - do_order(); - else if (Rflag) - randomize(); - - if (Xflag) - Tbl.str_flags |= STR_ROTATED; - - if (!Sflag) { - printf("\"%s\" created\n", Outfile); - if (Num_pts == 2) - puts("There was 1 string"); - else - printf("There were %u strings\n", Num_pts - 1); - printf("Longest string: %u byte%s\n", Tbl.str_longlen, - Tbl.str_longlen == 1 ? "" : "s"); - printf("Shortest string: %u byte%s\n", Tbl.str_shortlen, - Tbl.str_shortlen == 1 ? "" : "s"); - } - - rewind(outf); - Tbl.str_version = htobe32(Tbl.str_version); - Tbl.str_numstr = htobe32(Tbl.str_numstr); - Tbl.str_longlen = htobe32(Tbl.str_longlen); - Tbl.str_shortlen = htobe32(Tbl.str_shortlen); - Tbl.str_flags = htobe32(Tbl.str_flags); - fwrite((char *)&Tbl, sizeof(Tbl), 1, outf); - if (STORING_PTRS) { - for (p = Seekpts, cnt = Num_pts; cnt--; ++p) - *p = htobe64(*p); - fwrite(Seekpts, sizeof(*Seekpts), (size_t)Num_pts, outf); - } - fclose(outf); - exit(0); -} - -/* - * This routine evaluates arguments from the command line - */ -void -getargs(int argc, char **argv) -{ - int ch; - - while ((ch = getopt(argc, argv, "Cc:iorsx")) != -1) - switch(ch) { - case 'C': /* embedded comments */ - Cflag++; - break; - case 'c': /* new delimiting char */ - Delimch = *optarg; - if (!isascii(Delimch)) { - printf("bad delimiting character: '\\%o\n'", - (unsigned char)Delimch); - } - break; - case 'i': /* ignore case in ordering */ - Iflag++; - break; - case 'o': /* order strings */ - Oflag++; - break; - case 'r': /* randomize pointers */ - Rflag++; - break; - case 's': /* silent */ - Sflag++; - break; - case 'x': /* set the rotated bit */ - Xflag++; - break; - case '?': - default: - usage(); - } - argv += optind; - - if (*argv) { - Infile = *argv; - if (*++argv) - strcpy(Outfile, *argv); - } - if (!Infile) { - puts("No input file name"); - usage(); - } - if (*Outfile == '\0') { - strcpy(Outfile, Infile); - strcat(Outfile, ".dat"); - } -} - -void -usage(void) -{ - fprintf(stderr, - "strfile [-Ciorsx] [-c char] source_file [output_file]\n"); - exit(1); -} - -/* - * add_offset: - * Add an offset to the list, or write it out, as appropriate. - */ -void -add_offset(FILE *fp, off_t off) -{ - off_t beoff; - - if (!STORING_PTRS) { - beoff = htobe64(off); - fwrite(&beoff, 1, sizeof(beoff), fp); - } else { - ALLOC(Seekpts, Num_pts + 1); - Seekpts[Num_pts] = off; - } - Num_pts++; -} - -/* - * do_order: - * Order the strings alphabetically (possibly ignoring case). - */ -void -do_order(void) -{ - uint32_t i; - off_t *lp; - STR *fp; - - Sort_1 = fopen(Infile, "r"); - Sort_2 = fopen(Infile, "r"); - qsort(Firstch, (size_t)Tbl.str_numstr, sizeof(*Firstch), cmp_str); - i = Tbl.str_numstr; - lp = Seekpts; - fp = Firstch; - while (i--) - *lp++ = fp++->pos; - fclose(Sort_1); - fclose(Sort_2); - Tbl.str_flags |= STR_ORDERED; -} - -static int -stable_collate_range_cmp(int c1, int c2) -{ - static char s1[2], s2[2]; - int ret; - - s1[0] = c1; - s2[0] = c2; - if ((ret = strcoll(s1, s2)) != 0) - return (ret); - return (c1 - c2); -} - -/* - * cmp_str: - * Compare two strings in the file - */ -int -cmp_str(const void *s1, const void *s2) -{ - const STR *p1, *p2; - int c1, c2, n1, n2, r; - -#define SET_N(nf,ch) (nf = (ch == '\n')) -#define IS_END(ch,nf) (ch == EOF || (ch == (unsigned char)Delimch && nf)) - - p1 = (const STR *)s1; - p2 = (const STR *)s2; - - c1 = (unsigned char)p1->first; - c2 = (unsigned char)p2->first; - if ((r = stable_collate_range_cmp(c1, c2)) != 0) - return (r); - - fseeko(Sort_1, p1->pos, SEEK_SET); - fseeko(Sort_2, p2->pos, SEEK_SET); - - n1 = false; - n2 = false; - while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0' && c1 != EOF) - SET_N(n1, c1); - while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0' && c2 != EOF) - SET_N(n2, c2); - - while (!IS_END(c1, n1) && !IS_END(c2, n2)) { - if (Iflag) { - if (isupper(c1)) - c1 = tolower(c1); - if (isupper(c2)) - c2 = tolower(c2); - } - if ((r = stable_collate_range_cmp(c1, c2)) != 0) - return (r); - SET_N(n1, c1); - SET_N(n2, c2); - c1 = getc(Sort_1); - c2 = getc(Sort_2); - } - if (IS_END(c1, n1)) - c1 = 0; - if (IS_END(c2, n2)) - c2 = 0; - - return (stable_collate_range_cmp(c1, c2)); -} - -/* - * randomize: - * Randomize the order of the string table. We must be careful - * not to randomize across delimiter boundaries. All - * randomization is done within each block. - */ -void -randomize(void) -{ - uint32_t cnt, i; - off_t tmp; - off_t *sp; - -#if __FreeBSD_version < 800041 - srandomdev(); -#endif - - Tbl.str_flags |= STR_RANDOM; - cnt = Tbl.str_numstr; - - /* - * move things around randomly - */ - - for (sp = Seekpts; cnt > 0; cnt--, sp++) { -#if __FreeBSD_version < 800041 - i = random() % cnt; -#else - i = arc4random_uniform(cnt); -#endif - tmp = sp[0]; - sp[0] = sp[i]; - sp[i] = tmp; - } -} diff --git a/games/fortune/strfile/strfile.h b/games/fortune/strfile/strfile.h deleted file mode 100644 index 5d4f875..0000000 --- a/games/fortune/strfile/strfile.h +++ /dev/null @@ -1,54 +0,0 @@ -/*- - * Copyright (c) 1991, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Ken Arnold. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * @(#)strfile.h 8.1 (Berkeley) 5/31/93 - */ -/* $FreeBSD$ */ - -#include <sys/types.h> - -#define STR_ENDSTRING(line,tbl) \ - (((unsigned char)(line)[0]) == (tbl).str_delim && (line)[1] == '\n') - -typedef struct { /* information table */ -#define VERSION 1 - uint32_t str_version; /* version number */ - uint32_t str_numstr; /* # of strings in the file */ - uint32_t str_longlen; /* length of longest string */ - uint32_t str_shortlen; /* length of shortest string */ -#define STR_RANDOM 0x1 /* randomized pointers */ -#define STR_ORDERED 0x2 /* ordered pointers */ -#define STR_ROTATED 0x4 /* rot-13'd text */ -#define STR_COMMENTS 0x8 /* embedded comments */ - uint32_t str_flags; /* bit field for flags */ - unsigned char stuff[4]; /* 64-bit aligned space */ -#define str_delim stuff[0] /* delimiting character */ -} STRFILE; |