Debian Codegraph @

terps/agility/os_none.c

raw symbols SCIP
1 /* os_none.c -- interface routine template for generic system */
2 /* Copyright (C) 1996-1999,2001 Robert Masenten */
3 /* This program may be redistributed under the terms of the
4 GNU General Public License, version 2; see agility.h for details. */
5 /* */
6 /* This is part of the source for AGiliTy, the (Mostly) Universal */
7 /* AGT Interpreter */
8
9 /* This should work on any system supporting ANSI C, at the price
10 of playing without a status line or other system-specific
11 i/o features. */
12
13
14
15 #include <stdlib.h>
16 #include <stdio.h>
17
18 #include <time.h>
19 #include <ctype.h>
20 #include <limits.h>
21
22 #include "agility.h"
23 #include "interp.h"
24
25
26 int scroll_count; /* Count of how many lines we have printed since
27 last player input. */
28 void print_statline(void);
29
30
31
32
33 void agt_delay(int n)
34 /* This should wait for n seconds */
35 {
36 if (fast_replay || BATCH_MODE) return;
37 print_statline();
38 /* sleep(n);*/
39 }
40
41
42 void agt_tone(int hz,int ms)
43 /* Produce a hz-Hertz sound for ms milliseconds */
44 {
45 return;
46 }
47
48 int agt_rand(int a,int b)
49 /* Return random number from a to b inclusive */
50 {
51 return a+(rand()>>2)%(b-a+1);
52 }
53
54
55 char *agt_input(int in_type)
56 /* read a line from the keyboard, allocating space for it using malloc */
57 /* in_type: 0=command, 1=number, 2=question, 3=userstr, 4=filename,*/
58 /* 5=RESTART,RESTORE,UNDO,QUIT */
59 /* Negative values are for internal use by the interface (i.e. this module) */
60 /* and so are free to be defined by the porter. */
61 {
62 static int input_error=0;
63 char *s;
64
65 scroll_count=0;
66 print_statline();
67 s=rmalloc(1000);
68
69 if (fgets(s,1000,stdin)==NULL) {
70 if (++input_error>25) {
71 printf("Read error on input\n");
72 exit(EXIT_FAILURE);
73 }
74 s[0]=0;
75 clearerr(stdin);
76 } else input_error=0; /* Reset counter */
77
78 if (DEBUG_OUT)
79 fprintf(debugfile,"%s\n",s);
80 if (script_on) textputs(scriptfile,s);
81 return s;
82 }
83
84 char agt_getkey(rbool echo_char)
85 /* Reads a character and returns it, possibly reading in a full line
86 depending on the platform */
87 /* If echo_char=1, echo character. If 0, then the character is not
88 required to be echoed (and ideally shouldn't be) */
89 {
90 char *t, *s, c;
91
92 print_statline();
93
94 /* We use agt_input here only because in pure ANSI C there is no way of */
95 /* reading in input from the keyboard without waiting for return. */
96 /* In general agt_getkey() and agt_input() will be independent. */
97 t=agt_input(-1);
98 for(s=t;isspace(*s);s++);
99 c=*s;
100 rfree(t);
101
102 return c;
103 }
104
105 void agt_textcolor(int c)
106 /* Set text color to color #c, where the colors are as follows: */
107 /* 0=Black, 1=Blue, 2=Green, 3=Cyan, */
108 /* 4=Red, 5=Magenta, 6=Brown, */
109 /* 7=Normal("White"-- which may actually be some other color) */
110 /* This should turn off blinking, bold, color, etc. and restore */
111 /* the text mode to its default appearance. */
112 /* 8=Turn on blinking. */
113 /* 9= *Just* White (not neccessarily "normal" and no need to turn off */
114 /* blinking) */
115 /* 10=Turn on fixed pitch font. */
116 /* 11=Turn off fixed pitch font */
117 /* Also used to set other text attributes: */
118 /* -1=emphasized text, used (e.g.) for room titles */
119 /* -2=end emphasized text */
120 {
121 #if 0
122 if (c==-1) writestr("<<");
123 if (c==-2) writestr(">>");
124 #endif
125 return;
126 }
127
128
129
130 void agt_statline(const char *s)
131 /* Output a string on the status line, if possible */
132 {
133 return;
134 }
135
136
137 void agt_clrscr(void)
138 /* This should clear the screen and put the cursor at the upper left
139 corner (or one down if there is a status line) */
140 {
141 int i;
142
143 for(i=0;i<screen_height;i++)
144 printf("\n");
145 if (DEBUG_OUT) fprintf(debugfile,"\n\n<CLRSCR>\n\n");
146 if (script_on) textputs(scriptfile,"\n\n\n\n");
147 scroll_count=0;
148 }
149
150
151 void agt_puts(const char *s)
152 {
153 printf("%s",s);
154 curr_x+=strlen(s);
155 if (DEBUG_OUT) fprintf(debugfile,"%s",s);
156 if (script_on) textputs(scriptfile,s);
157 }
158
159 void agt_newline(void)
160 {
161 printf("\n");
162 curr_x=0;
163 if (DEBUG_OUT) fprintf(debugfile,"\n");
164 if (script_on) textputs(scriptfile,"\n");
165 scroll_count++;
166 if (scroll_count>=screen_height-2) {
167 printf(" --MORE--"); /* If possible, this should disappear after
168 a key is pressed */
169 agt_waitkey();
170 }
171 }
172
173
174 static unsigned long boxflags;
175 static int box_startx; /* Starting x of box */
176 static int box_width;
177 static int delta_scroll; /* Amount we are adding to vertical scroll
178 with the box */
179 static void boxrule(void)
180 /* Draw line at top or bottom of box */
181 {
182 int i;
183
184 agt_puts("+");
185 for(i=0;i<box_width;i++) agt_puts("-");
186 agt_puts("+");
187 }
188
189 static void boxpos(void)
190 {
191 int i;
192
193 agt_newline();
194 for(i=0;i<box_startx;i++) agt_puts(" ");
195 }
196
197 void agt_makebox(int width,int height,unsigned long flags)
198 /* Flags: TB_TTL, TB_BORDER */
199 {
200 boxflags=flags;
201 box_width=width;
202 if (boxflags&TB_BORDER) width+=2; /* Add space for border */
203 box_startx=(screen_width-width)/2; /* Center the box horizontally */
204 delta_scroll=height;
205 boxpos();
206 if (boxflags&TB_BORDER) {
207 boxrule();
208 boxpos();
209 agt_puts("|");
210 }
211 }
212
213 void agt_qnewline(void)
214 {
215 if (boxflags&TB_BORDER) agt_puts("|");
216 boxpos();
217 if (boxflags&TB_BORDER) agt_puts("|");
218 }
219
220 void agt_endbox(void)
221 {
222 if (boxflags&TB_BORDER) {
223 agt_puts("|");
224 boxpos();
225 boxrule();
226 }
227 scroll_count+=delta_scroll;
228 agt_newline(); /* NOT agt_qnewline() */
229 }
230
231
232 #define opt(s) (!strcmp(optstr[0],s))
233
234 rbool agt_option(int optnum,char *optstr[],rbool setflag)
235 /* If setflag is 0, then the option was prefixed with NO_ */
236 {
237 /* Return 1 if the option is recognized */
238 return 0;
239 }
240
241 #undef opt
242
243 genfile agt_globalfile(int fid)
244 {
245 if (fid==0)
246 return badfile(fCFG); /* Should return FILE* to open
247 global configuration file */
248 else return badfile(fCFG);
249 }
250
251
252 void init_interface(int argc,char *argv[])
253 {
254 script_on=0;scriptfile=badfile(fSCR);
255 scroll_count=0;
256 center_on=par_fill_on=0;
257 debugfile=stderr;
258 DEBUG_OUT=0; /* If set, we echo all output to debugfile */
259 /* This should be 1 if stderr points to a different */
260 /* device than stdin. (E.g. to a file instead of to the */
261 /* console) */
262 screen_height=25; /* Assume PC dimensions */
263 status_width=screen_width=80;
264 agt_clrscr();
265 }
266
267 void start_interface(fc_type fc)
268 {
269 if (stable_random)
270 srand(6);
271 else
272 srand(time(0));
273 }
274
275 void close_interface(void)
276 {
277 if (filevalid(scriptfile,fSCR))
278 close_pfile(scriptfile,0);
279 }