xref: /openbsd/games/adventure/setup.c (revision 133306f0)
1 /*	$OpenBSD: setup.c,v 1.5 1998/09/02 06:36:07 pjanzen Exp $	*/
2 /*	$NetBSD: setup.c,v 1.2 1995/03/21 12:05:10 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Jim Gillogly at The Rand Corporation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 #ifndef lint
41 static char copyright[] =
42 "@(#) Copyright (c) 1991, 1993\n\
43 	The Regents of the University of California.  All rights reserved.\n";
44 #endif /* not lint */
45 
46 #ifndef lint
47 #if 0
48 static char sccsid[] = "@(#)setup.c	8.1 (Berkeley) 5/31/93";
49 #else
50 static char rcsid[] = "$OpenBSD: setup.c,v 1.5 1998/09/02 06:36:07 pjanzen Exp $";
51 #endif
52 #endif /* not lint */
53 
54 /*
55  * Setup: keep the structure of the original Adventure port, but use an
56  * internal copy of the data file, serving as a sort of virtual disk.  It's
57  * lightly encrypted to prevent casual snooping of the executable.
58  *
59  * Also do appropriate things to tabs so that bogus editors will do the right
60  * thing with the data file.
61  *
62  */
63 
64 #define SIG1 " *      Jim Gillogly"
65 #define SIG2 " *      Sterday, 6 Thrimidge S.R. 1993, 15:24"
66 
67 #include <err.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include "hdr.h"	/* SEED lives in there; keep them coordinated. */
71 
72 #define USAGE "Usage: setup file > data.c (file is typically glorkz)"
73 
74 #define YES 1
75 #define NO  0
76 
77 #define LINE 10		/* How many values do we get on a line? */
78 
79 int
80 main(argc, argv)
81 	int argc;
82 	char *argv[];
83 {
84 	FILE	*infile;
85 	int	c, count, linestart;
86 
87 	if (argc != 2)
88 		errx(1, USAGE);
89 
90 	if ((infile = fopen(argv[1], "r")) == NULL)
91 		err(1, "Can't read file %s", argv[1]);
92 	puts("/*\n * data.c: created by setup from the ascii data file.");
93 	puts(SIG1);
94 	puts(SIG2);
95 	puts(" */");
96 	printf("\n\nchar data_file[] =\n{");
97 	srandom(SEED);
98 	count = 0;
99 	linestart = YES;
100 
101 	while ((c = getc(infile)) != EOF) {
102 		if (count++ % LINE == 0)
103 			printf("\n\t");
104 		if (linestart && c == ' ') { /* Convert first spaces to tab */
105 			printf("0x%02x,", (unsigned int)('\t' ^ random()) & 0xFF);
106 			while ((c = getc(infile)) == ' ' && c != EOF);
107 			/* Drop the non-whitespace character through */
108 			linestart = NO;
109 		}
110 		switch (c) {
111 		case '\t':
112 			linestart = NO; /* Don't need to convert spaces */
113 			break;
114 		case '\n':
115 			linestart = YES; /* Ready to convert spaces again */
116 			break;
117 		}
118 		if (count++ % LINE == 0)
119 			printf("\n\t");
120 		printf("0x%02x,", (unsigned int)(c ^ random()) & 0xFF);
121 	}
122 	puts("\n\t0\n};");
123 	fclose(infile);
124 	exit(0);
125 }
126