forked from rogueforge/rogomatic14
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.c
More file actions
213 lines (183 loc) · 6.57 KB
/
setup.c
File metadata and controls
213 lines (183 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* setup.c: Rog-O-Matic XIV (CMU) Wed Jan 30 17:38:07 1985 - mlm
* Copyright (C) 1985 by A. Appel, G. Jacobson, L. Hamey, and M. Mauldin
*
* This is the program which forks and execs the Rogue & the Player
*/
# include <stdio.h>
# include <signal.h>
# include <stdlib.h>
# include <fcntl.h>
# include <sys/ioctl.h>
# include <unistd.h>
# include <string.h>
# include "types.h"
# include "globals.h"
# include "install.h"
# define READ 0
# define WRITE 1
/* Define the Rog-O-Matic pseudo-terminal (Concept Based) */
/*
* ROGUETERMINFO was produced by the ./rterm.2.b64.sh script
*/
#define ROGUETERMINFO \
"TERMINFO=b64:GgElAA0AAwCHAD0Acmd8cnRlcm18Um9nLU8tTWF0aWMgUHNldWRvIFRlcm1" \
"pbmFsAAABAAABAAAAAAAAAAFQAAgAGAD__wAAAgD_____BAAGAP_______wkAIgD_____JQD" \
"__________ycA________________________________________KgD________________" \
"__y0A_____________________________zAA_____________zIA___________________" \
"__________________________zQA___________________________________________" \
"__________________zYA___________________________________________________" \
"_______________85AP__________OwAHAA0ADAAbEwAbYSVwMSUnICclKyVjJXAyJScgJyU" \
"rJWMAGzwACAAbOwAbRAAbZAAIAAoACAANCgAKAAkA"
int rfrogue, rtrogue;
extern char *getname(void);
int
main (int argc, char *argv[])
{ int ptc[2], ctp[2];
int child, score = 0, oldgame = 0;
int cheat = 0, noterm = 1, echo = 0, nohalf = 0, replay = 0;
int emacs = 0, rf = 0, terse = 0, user = 0, quitat = 2147483647;
char *rfile = "", *rfilearg = "";
char options[MU_BUF + 1]; /* rogomatic options, +1 for paranoia */
char ropts[SM_BUF + 1]; /* rogue options, +1 for paranoia */
char roguename[MU_BUF + 1]; /* rogue player name, +1 for paranoia */
/* zeroize arrays */
memset (options, 0, sizeof(options)); /* paranoia */
memset (ropts, 0, sizeof(ropts)); /* paranoia */
memset (roguename, 0, sizeof(roguename)); /* paranoia */
while (--argc > 0 && (*++argv)[0] == '-')
{ while (*++(*argv))
{ switch (**argv)
{ case 'c': cheat++; break; /* Will use trap arrows! */
case 'e': echo++; break; /* Echo file to roguelog */
case 'f': rf++; break; /* Next arg is the rogue file */
case 'h': nohalf++; break; /* No halftime show */
case 'p': replay++; break; /* Play back roguelog */
case 'r': oldgame++; break; /* Use saved game */
case 's': score++; break; /* Give scores only */
case 't': terse++; break; /* Give status lines only */
case 'u': user++; break; /* Start up in user mode */
case 'w': noterm = 0; break; /* Watched mode */
case 'E': emacs++; break; /* Emacs mode */
default: printf
("Usage: rogomatic [-cefhprstuwE] or rogomatic [file]\n");
exit (1);
}
}
if (rf)
{ if (--argc) rfilearg = *++argv;
rf = 0;
}
}
if (argc > 1)
{ printf ("Usage: rogomatic [-cefhprstuwE] or rogomatic <file>\n");
exit (1);
}
/* Find which rogue to use */
if (*rfilearg)
{ if (access (rfilearg, 1) == 0) rfile = rfilearg;
else { perror (rfilearg); exit (1); }
}
else if (access ("rogue", 1) == 0) rfile = "rogue";
# ifdef NEWROGUE
else if (access (NEWROGUE, 1) == 0) rfile = NEWROGUE;
# endif
# ifdef ROGUE
else if (access (ROGUE, 1) == 0) rfile = ROGUE;
# endif
else
{ perror ("rogue");
exit (1);
}
if (!replay && !score) quitat = findscore (rfile, "Rog-O-Matic");
snprintf (options, MU_BUF, "%d,%d,%d,%d,%d,%d,%d,%d",
cheat, noterm, echo, nohalf, emacs, terse, user,quitat);
snprintf (roguename, MU_BUF, "Rog-O-Matic %s for rogo-%s", RGMVER, getname ());
snprintf (ropts, SM_BUF, "ROGUEOPTS=name=rogo-%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s",
getname (), "fruit=apricot", "terse", "noflush", "noask",
"jump", "step", "nopassgo", "inven=slow", "seefloor",
"tombstone", "file=/tmp/rogue.sav", "score=/tmp/rogue.scr",
"lock=/tmp/rogue.lck");
if (score) { dumpscore (argc==1 ? argv[0] : DEFVER); exit (0); }
if (replay) { replaylog (argc==1 ? argv[0] : ROGUELOG, options); exit (0); }
if ((pipe (ptc) < 0) || (pipe (ctp) < 0))
{ fprintf (stderr, "Cannot get pipes!\n");
exit (1);
}
rtrogue = ptc[WRITE];
rfrogue = ctp[READ];
if ((child = fork ()) == 0)
{ close (0);
dup (ptc[READ]);
close (1);
dup (ctp[WRITE]);
close (ptc[WRITE]); /* Close parent's (player's) unused end of the pipes */
close (ctp[READ]);
if (putenv (ROGUETERMINFO) != 0) {
fprintf (stderr, "failed to putenv for $TERMINFO\n");
exit(1);
}
if (putenv ("TERM=rg") != 0) {
fprintf (stderr, "failed to putenv for $TERM\n");
exit(1);
}
if (putenv (ropts) != 0) {
fprintf (stderr, "failed to putenv for $ROGUEOPTS\n");
exit(1);
}
if (oldgame) execl (rfile, rfile, "-r", NULL);
if (argc) execl (rfile, rfile, argv[0], NULL);
execl (rfile, rfile, NULL);
_exit (1);
}
else
{ /* Encode the open files into a two character string */
char ft[3] = "aa"; ft[0] += rfrogue; ft[1] += rtrogue;
char rp[MU_BUF + 1]; /* rogue pid, +1 for paranoia */
/* zeroize arrays */
memset (rp, 0, sizeof(rp)); /* paranoia */
close (ptc[READ]); /* Close child's (rogue's) unused end of the pipes */
close (ctp[WRITE]);
/* Pass the process ID of the Rogue process as an ASCII string */
snprintf (rp, MU_BUF, "%d", child);
if (!author ()) nice (4);
execl ("./player", "player", ft, rp, options, roguename, NULL); /* XXX - ./ for debugging - XXX */
# ifdef PLAYER
execl (PLAYER, "player", ft, rp, options, roguename, NULL);
# endif
printf ("Rogomatic not available, 'player' binary missing.\n");
kill (child, SIGKILL);
}
}
/*
* replaylog: Given a log file name and an options string, exec the player
* process to replay the game. No Rogue process is needed (since we are
* replaying an old game), so the rfrogue and rtrogue file descriptors are
* given the fake value 'Z'.
*/
int
replaylog (char *fname, char *options)
{ execl ("./player", "player", "ZZ", "0", options, fname, NULL); /* XXX - ./ for debugging - XXX */
# ifdef PLAYER
execl (PLAYER, "player", "ZZ", "0", options, fname, NULL);
# endif
printf ("Replay not available, 'player' binary missing.\n");
exit (1);
}
/*
* author:
* See if a user is an author of the program
*/
int
author (void)
{
switch (getuid())
{ case 1337: /* Fuzzy */
case 1313: /* Guy */
case 1241: /* Andrew */
case 345: /* Leonard */
case 342: /* Gordon */
return 1;
default: return 0;
}
}