These are unused.
[super-star-trek.git] / io.c
1 #ifdef SERGEEV
2 #define _GNU_SOURCE
3 #endif /* SERGEEV */
4 #include <stdio.h>
5 #ifdef SERGEEV
6 #include <unistd.h>
7 #endif /* SERGEEV */
8 #include <termios.h>
9 #include <curses.h>
10 #include <signal.h>
11 #include <ctype.h>
12 #include <stdarg.h>
13 #ifdef SERGEEV
14 #include <conio.h>
15 #endif /* SERGEEV */
16 #ifdef MSDOS
17 #include <dos.h>
18 #endif
19 #include <time.h>
20
21 #ifdef SERGEEV
22 #include "sstlinux.h"
23 #endif /* SERGEEV */
24 #include "sst.h"
25
26 #ifndef SERGEEV
27 static int linecount;   /* for paging */
28 #endif /* SERGEEV */
29 static int screenheight = 24, screenwidth = 80;
30 #ifndef SERGEEV
31 static int curses = FALSE;
32 #else /* SERGEEV */
33 static int curses = TRUE;
34 #endif /* SERGEEV */
35
36 static void outro(int sig) {
37 /* wrap up, either normally or due to signal */
38     if (curses) {
39         clear();
40         (void)refresh();
41         (void)resetterm();
42         //(void)echo();
43         (void)endwin();
44     }
45 }
46
47 static void fastexit(int sig) {
48     outro(sig);
49     putchar('\n');
50     exit(0);
51 }
52
53 void iostart(int usecurses) {
54         (void) signal(SIGINT, fastexit);
55         (void) signal(SIGINT, fastexit);
56 #ifdef SIGIOT
57         (void) signal(SIGIOT,fastexit);         /* for assert(3) */
58 #endif /* SIGIOT */
59         if(signal(SIGQUIT,SIG_IGN) != SIG_IGN)
60             (void)signal(SIGQUIT,fastexit);
61
62 #ifndef SERGEEV
63         if (curses = usecurses) {
64 #else /* SERGEEV */
65         if ((curses = usecurses)) {
66 #endif /* SERGEEV */
67                 (void)initscr();
68 #ifdef KEY_MIN
69                 keypad(stdscr, TRUE);
70 #endif /* KEY_MIN */
71                 (void)saveterm();
72                 (void)nonl();
73                 (void)cbreak();
74                 //(void)noecho();
75                 scrollok(stdscr, TRUE);
76                 getmaxyx(stdscr, screenheight, screenwidth);
77         } else {
78                 char *LINES = getenv("LINES");
79                 if (LINES)
80                     screenheight = atoi(LINES);
81         }
82 }
83
84 void ioend(void) {
85     outro(0);
86 }
87
88 void clearscreen(void) {
89         /* Somehow we need to clear the screen */
90 #ifdef __BORLANDC__
91         extern void clrscr(void);
92         clrscr();
93 #else
94         if (curses) {
95             wclear(stdscr);
96             wrefresh(stdscr);
97         } else {
98                 // proutn("\033[2J"); /* Hope for an ANSI display */
99                 /* much more in that old-TTY spirit to just throw linefeeds */
100                 int i;
101                 for (i = 0; i < screenheight; i++)
102                     putchar('\n');
103         }
104 #endif
105 }
106
107 void pause_game(int i) {
108 #ifndef SERGEEV
109     char buf[BUFSIZ], *prompt;
110 #else /* SERGEEV */
111         char *prompt;
112         drawmaps(0);
113         setwnd(5);
114 #endif /* SERGEEV */
115         if (i==1) {
116                 if (skill > 2)
117                         prompt = "[ANOUNCEMENT ARRIVING...]";
118                 else
119                         prompt = "[IMPORTANT ANNOUNCEMENT ARRIVING -- PRESS ENTER TO CONTINUE]";
120         }
121         else {
122                 if (skill > 2)
123                         prompt = "[CONTINUE?]";
124                 else
125                         prompt = "[PRESS ENTER TO CONTINUE]";
126
127         }
128 #ifndef SERGEEV
129         if (curses) {
130             waddch(stdscr, '\n');
131                 waddstr(stdscr, prompt);
132                 wgetnstr(stdscr, buf, sizeof(buf));
133                 wclear(stdscr);
134                 wrefresh(stdscr);
135         } else {
136                 putchar('\n');
137                 prout(prompt);
138                 fgets(buf, sizeof(buf), stdin);
139                 if (i != 0) {
140                         clearscreen();
141                 }
142                 linecount = 0;
143         }
144 #else /* SERGEEV */
145         proutn(prompt);
146         getche();
147         clrscr();
148         setwnd(4);
149         clrscr();
150 #endif /* SERGEEV */
151 }
152
153
154 void skip(int i) {
155 #ifndef SERGEEV
156     while (i-- > 0) {
157         if (curses) {
158             int y, x;
159             getyx(stdscr, y, x);
160             if (y == screenheight-1)
161                 pause_game(0);
162             else
163                 waddch(stdscr, '\n');
164         } else {
165             linecount++;
166             if (linecount >= screenheight)
167                 pause_game(0);
168             else
169                 putchar('\n');
170         }
171 #else /* SERGEEV */
172         while (i-- > 0) proutn("\n\r");
173 }
174
175 static void vproutn(char *fmt, va_list ap) {
176     char *strbuf, *p, *s;
177     vasprintf(&strbuf, fmt, ap);
178     p=s=strbuf;
179     if ((curwnd==4)&&(wherey()==wnds[curwnd].wndbottom-wnds[curwnd].wndtop)){
180        if (strchr(strbuf,'\n')){
181           p=strchr(strbuf,'\n');
182           p[0]=0;
183           cprintf("%s",strbuf);
184           p++;
185           pause_game(0);
186        }
187 #endif /* SERGEEV */
188     }
189 #ifdef SERGEEV
190     if ((curwnd==4)&&(wherey()>wnds[curwnd].wndbottom-wnds[curwnd].wndtop+1))
191        cprintf("\r");
192 //        setwnd(curwnd);
193     if (strchr(s,'\n') || strchr(s,'\r')) clreol();
194     cprintf("%s",p);
195     free(strbuf);
196 #endif /* SERGEEV */
197 }
198
199 void proutn(char *fmt, ...) {
200     va_list ap;
201     va_start(ap, fmt);
202 #ifndef SERGEEV
203     if (curses) {
204         vw_printw(stdscr, fmt, ap);
205         wrefresh(stdscr);
206     } else
207         vprintf(fmt, ap);
208 #else /* SERGEEV */
209     vproutn(fmt, ap);
210 #endif /* SERGEEV */
211     va_end(ap);
212 }
213
214 void prout(char *fmt, ...) {
215     va_list ap;
216     va_start(ap, fmt);
217 #ifndef SERGEEV
218     if (curses) {
219         vw_printw(stdscr, fmt, ap);
220         wrefresh(stdscr);
221     } else
222         vprintf(fmt, ap);
223 #else /* SERGEEV */
224     vproutn(fmt, ap);
225 #endif /* SERGEEV */
226     va_end(ap);
227     skip(1);
228 }
229
230 void proutc(char *line) {
231     line[strlen(line)-1] = '\0';
232 #ifndef SERGEEV
233     if (curses)
234         waddstr(stdscr, line);
235     else
236         fputs(line, stdout);
237 #else /* SERGEEV */
238     cputs(line);
239 #endif /* SERGEEV */
240     skip(1);
241 }
242
243 #ifdef SERGEEV
244 static void prchr(char *s){
245      char str[2];
246      strncpy(str,s,1);
247      str[1]=0;
248      proutn(str);
249 }
250
251 static void vprouts(char *fmt, va_list ap) {
252     char *s;
253     vasprintf(&s, fmt, ap);
254     while (*s) {
255         prchr(s++);
256         delay(30);
257     }
258     free(s);
259 }
260
261 #endif /* SERGEEV */
262 void prouts(char *fmt, ...) {
263 #ifndef SERGEEV
264         clock_t endTime;
265         char *s, buf[BUFSIZ];
266         /* print slowly! */
267         va_list ap;
268         va_start(ap, fmt);
269         vsprintf(buf, fmt, ap);
270         va_end(ap);
271         skip(1);
272         for (s = buf; *s; s++) {
273                 endTime = clock() + CLOCKS_PER_SEC*0.05;
274                 while (clock() < endTime) continue;
275                 if (curses) {
276                     waddch(stdscr, *s);
277                     wrefresh(stdscr);
278                 }
279                 else {
280                     putchar(*s);
281                     fflush(stdout);
282                 }
283         }
284 #else /* SERGEEV */
285     va_list ap;
286     va_start(ap, fmt);
287     vprouts(fmt, ap);
288     va_end(ap);
289 #endif /* SERGEEV */
290 }
291
292 #ifndef SERGEEV
293 void getline(char *line, int max) {
294 #else /* SERGEEV */
295 void cgetline(char *line, int max) {
296 #endif /* SERGEEV */
297     if (curses) {
298 #ifndef SERGEEV
299         wgetnstr(stdscr, line, max);
300         wrefresh(stdscr);
301 #else /* SERGEEV */
302         line[0]=max-1;
303         cgets(line);
304         memmove(line,&line[2],max-3);
305 #endif /* SERGEEV */
306     } else {
307         fgets(line, max, stdin);
308         line[strlen(line)-1] = '\0';
309     }
310 }
311
312 void commandhook(char *cmd, int before) {
313 }