-
Notifications
You must be signed in to change notification settings - Fork 0
/
advint.c
136 lines (117 loc) · 2.48 KB
/
advint.c
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
/* advint.c - an interpreter for adventure games */
/*
Copyright (c) 1986, by David Michael Betz
All rights reserved
*/
#include "advint.h"
#include "advdbs.h"
#ifndef MAC
#include <setjmp.h>
#endif
/* global variables */
jmp_buf restart;
/* external variables */
extern int h_init;
extern int h_update;
extern int h_before;
extern int h_after;
extern int h_error;
void play(void);
int single(void);
void error(char *msg);
/* main - the main routine */
int main(int argc,char *argv[])
{
char *fname,*lname;
int rows,cols,i;
#ifdef MAC
char name[50];
macinit(name);
fname = name;
lname = NULL;
rows = 20;
cols = 80;
#else
printf("ADVINT v1.2 - Copyright (c) 1986, by David Betz\n");
fname = NULL;
lname = NULL;
rows = 24;
cols = 80;
/* parse the command line */
for (i = 1; i < argc; i++)
if (argv[i][0] == '-')
switch (argv[i][1]) {
case 'r':
case 'R':
rows = atoi(&argv[i][2]);
break;
case 'c':
case 'C':
cols = atoi(&argv[i][2]);
break;
case 'l':
case 'L':
lname = &argv[i][2];
break;
}
else
fname = argv[i];
if (fname == NULL) {
printf("usage: advint [-r<rows>] [-c<columns>] [-l<log-file>] <file>\n");
exit(EXIT_FAILURE);
}
#endif
/* initialize terminal i/o */
trm_init(rows,cols,lname);
/* initialize the database */
db_init(fname);
/* play the game */
play();
return 0;
}
/* play - the main loop */
void play(void)
{
/* establish the restart point */
setjmp(restart);
/* execute the initialization code */
execute(h_init);
/* turn handling loop */
for (;;) {
/* execute the update code */
execute(h_update);
/* parse the next input command */
if (parse()) {
if (single())
while (next() && single())
;
}
/* parse error, call the error handling code */
else
execute(h_error);
}
}
/* single - handle a single action */
int single(void)
{
/* execute the before code */
switch (execute(h_before)) {
case ABORT: /* before handler aborted sequence */
return (FALSE);
case CHAIN: /* execute the action handler */
if (execute(getafield(getvalue(V_ACTION),A_CODE)) == ABORT)
return (FALSE);
case FINISH:/* execute the after code */
if (execute(h_after) == ABORT)
return (FALSE);
break;
}
return (TRUE);
}
/* error - print an error message and exit */
void error(char *msg)
{
trm_str(msg);
trm_chr('\n');
exit(EXIT_FAILURE);
}