1*1182a44cSrillig /* $NetBSD: setup.c,v 1.12 2021/05/02 12:50:43 rillig Exp $ */
243b95251Scgd
3107c0a28Sjtc /*-
4107c0a28Sjtc * Copyright (c) 1991, 1993
5107c0a28Sjtc * The Regents of the University of California. All rights reserved.
6107c0a28Sjtc *
7107c0a28Sjtc * This code is derived from software contributed to Berkeley by
8107c0a28Sjtc * Jim Gillogly at The Rand Corporation.
9107c0a28Sjtc *
10107c0a28Sjtc * Redistribution and use in source and binary forms, with or without
11107c0a28Sjtc * modification, are permitted provided that the following conditions
12107c0a28Sjtc * are met:
13107c0a28Sjtc * 1. Redistributions of source code must retain the above copyright
14107c0a28Sjtc * notice, this list of conditions and the following disclaimer.
15107c0a28Sjtc * 2. Redistributions in binary form must reproduce the above copyright
16107c0a28Sjtc * notice, this list of conditions and the following disclaimer in the
17107c0a28Sjtc * documentation and/or other materials provided with the distribution.
18e5aeb4eaSagc * 3. Neither the name of the University nor the names of its contributors
19107c0a28Sjtc * may be used to endorse or promote products derived from this software
20107c0a28Sjtc * without specific prior written permission.
21107c0a28Sjtc *
22107c0a28Sjtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23107c0a28Sjtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24107c0a28Sjtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25107c0a28Sjtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26107c0a28Sjtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27107c0a28Sjtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28107c0a28Sjtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29107c0a28Sjtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30107c0a28Sjtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31107c0a28Sjtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32107c0a28Sjtc * SUCH DAMAGE.
33107c0a28Sjtc */
34107c0a28Sjtc
35107c0a28Sjtc #ifndef lint
360ac29f66Ssimonb static char copyright[] = "@(#) Copyright (c) 1991, 1993\n\
370ac29f66Ssimonb The Regents of the University of California. All rights reserved.\n";
38107c0a28Sjtc
3943b95251Scgd #if 0
40107c0a28Sjtc static char sccsid[] = "@(#)setup.c 8.1 (Berkeley) 5/31/93";
4143b95251Scgd #else
42*1182a44cSrillig static char rcsid[] = "$NetBSD: setup.c,v 1.12 2021/05/02 12:50:43 rillig Exp $";
4343b95251Scgd #endif
44107c0a28Sjtc #endif /* not lint */
45107c0a28Sjtc
46107c0a28Sjtc /*
47107c0a28Sjtc * Setup: keep the structure of the original Adventure port, but use an
48107c0a28Sjtc * internal copy of the data file, serving as a sort of virtual disk. It's
49107c0a28Sjtc * lightly encrypted to prevent casual snooping of the executable.
50107c0a28Sjtc *
51107c0a28Sjtc * Also do appropriate things to tabs so that bogus editors will do the right
52107c0a28Sjtc * thing with the data file.
53107c0a28Sjtc *
54107c0a28Sjtc */
55107c0a28Sjtc
56107c0a28Sjtc #define SIG1 " * Jim Gillogly"
57107c0a28Sjtc #define SIG2 " * Sterday, 6 Thrimidge S.R. 1993, 15:24"
58107c0a28Sjtc
59107c0a28Sjtc #include <stdio.h>
600ac29f66Ssimonb #include <errno.h>
610ac29f66Ssimonb #include <string.h>
62c7bcded6Shubertf #include <stdlib.h>
63107c0a28Sjtc #include "hdr.h" /* SEED lives in there; keep them coordinated. */
64107c0a28Sjtc
65d929aa8aSjsm #define USAGE "Usage: setup file > data.c (file is typically glorkz)\n"
66107c0a28Sjtc
67107c0a28Sjtc #define YES 1
68107c0a28Sjtc #define NO 0
69107c0a28Sjtc
70107c0a28Sjtc #define LINE 10 /* How many values do we get on a line? */
71107c0a28Sjtc
720ac29f66Ssimonb int main(int, char *[]);
73c7bcded6Shubertf
74ab046d9fSlukem int
main(int argc,char * argv[])75243d04efSjmc main(int argc, char *argv[])
76107c0a28Sjtc {
77107c0a28Sjtc FILE *infile;
78107c0a28Sjtc int c, count, linestart;
79107c0a28Sjtc
800ac29f66Ssimonb if (argc != 2) {
810ac29f66Ssimonb fprintf(stderr, USAGE);
820ac29f66Ssimonb exit(1);
830ac29f66Ssimonb }
84107c0a28Sjtc
850ac29f66Ssimonb if ((infile = fopen(argv[1], "r")) == NULL) {
860ac29f66Ssimonb fprintf(stderr, "Can't read file %s: %s\n", argv[1],
870ac29f66Ssimonb strerror(errno));
880ac29f66Ssimonb exit(1);
890ac29f66Ssimonb }
90107c0a28Sjtc puts("/*\n * data.c: created by setup from the ascii data file.");
91107c0a28Sjtc puts(SIG1);
92107c0a28Sjtc puts(SIG2);
93107c0a28Sjtc puts(" */");
94107c0a28Sjtc printf("\n\nchar data_file[] =\n{");
95107c0a28Sjtc srandom(SEED);
96107c0a28Sjtc count = 0;
97107c0a28Sjtc linestart = YES;
98107c0a28Sjtc
9984f80fd6Slukem while ((c = getc(infile)) != EOF) {
10084f80fd6Slukem if (linestart && c == ' ') { /* Convert first spaces to tab */
101243d04efSjmc printf("0x%02x,",
102243d04efSjmc (unsigned int)('\t' ^ random()) & 0xFF);
103107c0a28Sjtc while ((c = getc(infile)) == ' ' && c != EOF);
104107c0a28Sjtc /* Drop the non-whitespace character through */
105107c0a28Sjtc linestart = NO;
106107c0a28Sjtc }
10784f80fd6Slukem switch (c) {
108107c0a28Sjtc case '\t':
109107c0a28Sjtc linestart = NO; /* Don't need to convert spaces */
110107c0a28Sjtc break;
111107c0a28Sjtc case '\n':
11284f80fd6Slukem linestart = YES; /* Ready to convert spaces
11384f80fd6Slukem * again */
114107c0a28Sjtc break;
115107c0a28Sjtc }
116107c0a28Sjtc if (count++ % LINE == 0) /* Finished a line? */
117107c0a28Sjtc printf("\n\t");
1189e7f2724Shubertf printf("0x%02x,", (unsigned int)(c ^ random()) & 0xFF);
119107c0a28Sjtc }
120107c0a28Sjtc puts("\n\t0\n};");
121107c0a28Sjtc fclose(infile);
12276139748Shubertf fflush(stdout);
1230ac29f66Ssimonb if (ferror(stdout)) {
1240ac29f66Ssimonb perror("error writing standard output");
1250ac29f66Ssimonb exit(1);
1260ac29f66Ssimonb }
127107c0a28Sjtc exit(0);
128107c0a28Sjtc }
129