Debian Codegraph @

terps/agility/os_termcap.c

raw symbols SCIP
1 /* os_termcap.c -- interface routines for termcap systems */
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 the (Mostly) Universal */
7 /* AGT Interpreter */
8
9 /* This interface is not quite as careful as it should be; */
10 /* in particular, it doesn't check for changed appearance modes */
11 /* before moving the cursor around, which could possibly cause */
12 /* problems on some terminals. */
13
14 /* The internal line editor */
15 #define USE_EDITLINE
16
17 /* If you use this, you'll also need to link with the readline library.*/
18 /* I'm not certain it even still works; I haven't tested it lately */
19 /* #define USE_READLINE*/
20
21
22 #define RVID_BOX 1 /* Make box reverse video? */
23 #define DEBUG_KEY 0
24
25 /*---------------------------------------------------------------------*/
26 /* Header Files and Prototypes of terminal functions */
27 /*---------------------------------------------------------------------*/
28
29 #include "agility.h"
30 #include "interp.h"
31
32 #include <time.h>
33 #include <assert.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <ctype.h>
39 #include <termios.h>
40 #include <signal.h>
41 #include <sys/ioctl.h>
42
43 #ifdef LINUX
44 #include <linux/kd.h>
45 #undef HAVE_TPARAM
46 #endif
47
48 #ifdef BSD_TERM
49 #define tcgetattr(fd,data) ioctl(fd,TIOCGETA,data)
50 #define tcsetattr(fd,junk,data) ioctl(fd,TIOCSETAF,data)
51 /* junk needs to be equal to TCSAFLUSH */
52 #endif
53
54 /* Need one of the following */
55
56 #ifdef NO_TERMCAP_H
57 char *tgoto();
58 char *tparam();
59 int tputs();
60 extern char *BC, *UP;
61 extern int PC;
62 int tgetent();
63 int tgetnum();
64 char *tgetstr();
65 #else
66 #include <term.h>
67 #include <termcap.h>
68 #endif
69
70 #ifdef USE_READLINE
71 #include <readline/readline.h>
72 #include <readline/history.h>
73 #endif
74
75 #ifdef TIOCGWINSZ /* Both of these need to be defined. */
76 #ifdef SIGWINCH
77 #define CATCH_WINDOW_CHANGE
78 #endif
79 #endif
80
81 #ifdef LINUX_COLOR
82 /* 0:Black, 1:Red, 2:Green, 3:Brown, 4:Blue, 5:Magenta, 6:Cyan, 7:White */
83 /* 3n=foreground, 4n=background, semicolon seperated */
84 #define TS_LINUX_SL "\033[37;44m"
85 #define TS_LINUX_NORM "\033[m"
86 #define TS_LINUX_SETCOLOR "\033[3%dm"
87 /* The AGT color codes are as follows: */
88 /* 0=Black, 1=Blue, 2=Green, 3=Cyan, 4=Red, 5=Magenta, 6=Brown, 7=White */
89 int color_trans[8]={0,4,2,6,1,5,3,7};
90 rbool color_on;
91 #endif
92
93 #define pFNT ".fnt"
94
95
96 /*---------------------------------------------------------------------*/
97 /* Misc. Global variables */
98 /*---------------------------------------------------------------------*/
99
100 static rbool nav_arrow; /* NAVARROW set? */
101 static rbool lower_file; /* Lowercase resource files? */
102 static rbool font_enable;
103 static fc_type game_fc;
104 static char *viewer_name; /* Name of image viewing program */
105
106 rbool have_compass;
107 static int status_height; /* Number of lines in status line */
108
109 static int control_width;
110 static int scroll_count; /* Count of how many lines we have printed since
111 last player input. */
112 volatile int curr_y, winsize_flag, cont_flag;
113
114
115
116 /* NUM_KEYS must be less than 32 */
117 #define NUM_KEYS 23
118 #define FUN_KEY_BASE 11
119 #define FUN_KEY_END 22
120 #define LAST_FUN_KEY (FUN_KEY_END+10)
121 static char *term_keycap[NUM_KEYS]=
122 {"kb","kD",
123 "kl","kr","ku","kd",
124 "kh","@7",
125 "kN","kP","kI",
126 "k0","k1","k2","k3","k4","k5","k6","k7","k8","k9",
127 "F1","F2"};
128 static char *term_key[NUM_KEYS];
129
130 static char *fkey_text[22]={ /* Text for function keys */
131 "help\n",
132 "get ","drop ",
133 "examine ","read ",
134 "open ","close ",
135 "inventory\n","look\n",
136 "score\n",
137 "save\n","restore\n",
138 "exit\n",
139 "w\n","e\n","n\n","s\n",
140 "nw\n","sw\n","se\n","ne\n",
141 "enter\n"
142 };
143
144 static const char *keylist[12]={
145 "del",
146 "left","right","up","down",
147 "home","end","pgdn","pgup",
148 "ins"};
149
150
151 #if 0
152 "undo\n",
153 "get ","drop ",
154 "unlock ","read ",
155 "open ","close ",
156 "inventory\n","look\n",
157 "wait\n",
158 "save\n","restore\n"
159 #endif
160
161 /* Other candidates: view, wait (z), examine (x), again (g) */
162
163
164 /* These are used to hold termcap strings */
165 static char term_buff[3000];
166 static char term_cmd_buff[3000];
167
168 static char *term_cmd[16];
169
170 #define TC_GOXY 0
171 #define TC_CLRSCR 1
172 #define TC_HIDE 2
173 #define TC_SHOW 3
174 #define TC_SL 4
175 #define TC_ENDSL 5
176 #define TC_BLINK 6
177 #define TC_NORM 7
178 #define TC_DELLINE 8
179 #define TC_SETSCROLL 9
180 #define TC_CR 10
181 #define TC_CLEARLINE 11
182 #define TC_BOLD 12
183 #define TC_NOBOLD 13
184 #define TC_INITKEY 14
185 #define TC_RESTOREKEY 15
186
187
188 /*---------------------------------------------------------------------*/
189 /* Misc. minor functions */
190 /*---------------------------------------------------------------------*/
191
192 void agt_delay(int n)
193 {
194 if (fast_replay || BATCH_MODE) return;
195 print_statline();
196 sleep((n+1)/2); /* In theory this should be sleep(n) */
197 }
198
199 void agt_tone(int hz,int ms)
200 /* Produce a hz-Hertz sound for ms milliseconds */
201 {
202 if (BATCH_MODE) return;
203 return;
204 }
205
206
207 int agt_rand(int a,int b)
208 /* Return random number from a to b inclusive */
209 {
210 int n;
211 n=a+(rand()>>2)%(b-a+1);
212 return n;
213 }
214
215 genfile agt_globalfile(int fid)
216 {
217 char *homedir;
218 FILE *cfgfile;
219 char *fname;
220
221 if (fid!=0) return badfile(fCFG);
222 homedir=getenv("HOME");
223 fname=rmalloc(strlen(homedir)+10);
224 sprintf(fname,"%s/.agilrc",homedir);
225 cfgfile=fopen(fname,"r");
226 return cfgfile;
227 }
228
229 void script_out(const char *s)
230 {
231 if (DEBUG_OUT)
232 fprintf(debugfile,"%s",s);
233 if (script_on) textputs(scriptfile,s);
234 }
235
236
237 /*---------------------------------------------------------------------*/
238 /* Low-level Terminal Functions: gotoxy(), etc. */
239 /*---------------------------------------------------------------------*/
240
241 void fix_loc(void)
242 /* This fixes the location if we are restoring from a suspended state */
243 {
244 #ifdef LOCAL_FIX_CONT
245 char *cmd;
246 #endif
247
248 if (cont_flag || winsize_flag)
249 agt_clrscr();
250
251 #ifdef LOCAL_FIX_CONT
252 {
253 cmd=tgoto(term_cmd[TC_GOXY],curr_x,curr_y);
254 cont_flag=winsize_flag=0;
255 tputs(cmd,1,putchar);
256 }
257 #endif
258 }
259
260 static void gotoxy(int x,int y)
261 {
262 char *cmd;
263
264 if (BATCH_MODE) return;
265 cmd=tgoto(term_cmd[TC_GOXY],x,y);
266 tputs(cmd,1,putchar);
267 }
268
269 static void cleareol(void)
270 {
271 char *cmd;
272 int i;
273
274 if (term_cmd[TC_CLEARLINE]!=NULL)
275 tputs(term_cmd[TC_CLEARLINE],1,putchar);
276 else {
277 for(i=0;i<screen_width-1-curr_x;i++)
278 putchar(' ');
279 cmd=tgoto(term_cmd[TC_GOXY],curr_x,curr_y);
280 tputs(cmd,1,putchar);
281 }
282 }
283
284 static void clearline(void)
285 /* Clears current line and leaves cursor at beginning of it */
286 {
287 char *cmd;
288
289 if (term_cmd[TC_CR]!=NULL)
290 tputs(term_cmd[TC_CR],1,putchar);
291 else {
292 cmd=tgoto(term_cmd[TC_GOXY],0,curr_y);
293 tputs(cmd,1,putchar);
294 }
295 curr_x=0;
296 cleareol();
297 }
298
299
300 static int acc_ptr;
301 static char accum_text[81]; /* Used for prompt */
302
303 void set_new_line(void)
304 /* Does the math to update variables when we skip down a line */
305 {
306 curr_x=0;
307 curr_y++;
308 if (curr_y>=screen_height)
309 curr_y=screen_height-1; /* Scrolling has occured */
310 scroll_count++;
311 accum_text[0]=0;acc_ptr=0;
312 }
313
314
315 void add_accum_text(const char *s)
316 {
317 while(*s!=0 && acc_ptr<80)
318 accum_text[acc_ptr++]=*(s++);
319 accum_text[acc_ptr]=0;
320 }
321
322
323
324 void set_scroll(int top)
325 {
326 #ifdef HAVE_TPARAM
327 char buff[20];
328 char *buff2;
329
330 if (term_cmd[TC_SETSCROLL]!=NULL) {
331 buff2=tparam(term_cmd[TC_SETSCROLL],buff,20,top,screen_height);
332 tputs(buff2,1,putchar);
333 if (buff2!=buff) free(buff2);
334 }
335 #endif
336 }
337
338
339 void show_cursor(rbool b)
340 /* 0=hide, 1=show */
341 {
342 if (b==0 && term_cmd[TC_HIDE]!=NULL)
343 tputs(term_cmd[TC_HIDE],1,putchar);
344 if (b==1 && term_cmd[TC_HIDE]!=NULL)
345 tputs(term_cmd[TC_SHOW],1,putchar);
346 }
347
348
349
350 static struct termios termsave;
351
352 /* Put the terminal in noncanonical mode */
353 static void set_term_noncanon(void)
354 {
355 struct termios newterm;
356 fflush(stdin);
357 tcgetattr(STDIN_FILENO,&termsave); /* Save terminal settings */
358 tcgetattr(STDIN_FILENO,&newterm);
359 newterm.c_lflag&=~ICANON; /* Set noncanonical mode */
360 newterm.c_lflag&=~ECHO; /* ...with no echo */
361 newterm.c_cc[VMIN]=1; /* Read one character at a time */
362 newterm.c_cc[VTIME]=0; /* No time limit */
363 tcsetattr(STDIN_FILENO,TCSAFLUSH,&newterm);
364 }
365
366 /* Restore terminal to previous state */
367 static void set_term_normal(void)
368 {
369 tcsetattr(STDIN_FILENO,TCSAFLUSH,&termsave); /* Restore terminal */
370 }
371
372
373 #ifdef CATCH_WINDOW_CHANGE
374 void window_change(int signum)
375 {
376 struct winsize winsz;
377
378 signal(SIGWINCH,window_change);
379 if (ioctl(STDOUT_FILENO,TIOCGWINSZ,&winsz)!=0) return;
380 if (winsz.ws_row==0 || winsz.ws_col==0) return;
381 status_width=screen_width=winsz.ws_col;
382 screen_height=winsz.ws_row;
383 if (curr_x>screen_width)
384 {curr_x=screen_width;winsize_flag=1;}
385 if (curr_y>screen_height)
386 {curr_y=screen_height;winsize_flag=1;}
387 }
388 #endif
389
390
391 static struct termios term_save;
392
393 void restore_terminal(void)
394 {
395 show_cursor(1);
396 set_scroll(0);
397 #ifdef BSD_TERM
398 ioctl(STDIN_FILENO,TIOCSETA,&term_save);
399 #else
400 tcsetattr(STDIN_FILENO,TCSANOW,&term_save);
401 #endif
402 }
403
404
405 /* SIGTSTP, SIGCONT */
406 void cont_handler(int signum)
407 {
408 signal(SIGCONT,cont_handler);
409 cont_flag=1;
410 }
411
412
413
414
415 /*---------------------------------------------------------------------*/
416 /* Character Appearance Functions */
417 /*---------------------------------------------------------------------*/
418
419 #define STATUS_STATE 0xA800
420 #define START_STATE 0x0800
421
422 /* For state:
423 currently low 4 bits are color.
424 bit 11 (0x0800) indicates normal color
425 bit 13 (0x2000) indicates whether we are in standout mode or not
426 bit 14 (0x4000) indicates whether we are blinking or not
427 bit 15 (0x8000) indicates whether bold is on or not */
428
429 #define NORM_BIT 0x0800
430 #define BOLD_BIT 0x8000
431 #define BLINK_BIT 0x4000
432 #define SL_BIT 0x2000
433
434 typedef unsigned short vstate;
435 static vstate state_stack[16];
436 int stateptr=0; /* Points into state stack */
437
438 #define cstate state_stack[stateptr] /* Current state */
439
440 static void set_state(vstate state)
441 {
442 vstate diff;
443 char sbuff[10];
444 int c; /* Color */
445
446 if (BATCH_MODE) return;
447
448 diff=state^cstate;
449
450 tputs(term_cmd[TC_NORM],1,putchar); /* First put the screen in known state */
451
452 c=state & 0xF;
453 if (state & NORM_BIT) c=7;
454
455 /* if (term_cmd[TC_NOBOLD]!=NULL)
456 tputs(term_cmd[TC_NOBOLD],1,putchar);*/
457 /* tputs(term_cmd[TC_ENDSL],1,putchar);*/
458
459 if (term_cmd[TC_BOLD]!=NULL) {
460 if (state & BOLD_BIT) { /* BOLD */
461 tputs(term_cmd[TC_BOLD],1,putchar);
462 if ((state&NORM_BIT) && !(state&SL_BIT)) c=6;
463 /* BOLD text will be yellow */
464 }
465 } else
466 if (diff & BOLD_BIT) {
467 if (state & BOLD_BIT) writestr("<<");
468 else writestr(">>");
469 }
470
471 if ((state & BLINK_BIT) && term_cmd[TC_BLINK]!=NULL) /* Blinking */
472 tputs(term_cmd[TC_BLINK],1,putchar);
473
474 if ((state & SL_BIT) && term_cmd[TC_SL]!=NULL) /* Status line state */
475 tputs(term_cmd[TC_SL],1,putchar);
476
477 #ifdef LINUX_COLOR
478 if (color_on) {
479 sprintf(sbuff,TS_LINUX_SETCOLOR,color_trans[c]);
480 tputs(sbuff,1,putchar);
481 }
482 #endif
483
484 cstate=state;
485 }
486
487 static void push_state(vstate state)
488 {
489 if (stateptr==15) fatal("State stack overflow!");
490 stateptr++;
491 set_state(state);
492 }
493
494 static void pop_state(void)
495 {
496 if (stateptr==0) fatal("State stack error: POP without PUSH!");
497 stateptr--;
498 set_state(cstate);
499 }
500
501
502 void agt_textcolor(int c)
503 /* Set text color to color #c, where the colors are as follows: */
504 /* 0=Black, 1=Blue, 2=Green, 3=Cyan, 4=Red, 5=Magenta, 6=Brown, */
505 /* 7=White("normal"), 8=Blinking, */
506 /* 9= *Just* White (not neccessarily "normal" and no need to turn off */
507 /* blinking) */
508 /* Also used to set other text attributes: */
509 /* -1=emphasized text, used (e.g.) for room titles */
510 /* -2=end emphasized text */
511 {
512 vstate nstate;
513
514 nstate=cstate;
515 if (c==-1) /* Bold on */
516 nstate=nstate|0x8000;
517 else if (c==-2)
518 nstate=nstate & ~0x8000; /* BOLD off */
519 else if (c==8)
520 nstate=nstate | 0x4000; /* BLINK on */
521 else if (c==7)
522 nstate=0x0800; /* "Normal" */
523 else if (c==9)
524 nstate=(nstate & ~0x80F) | 7; /* Set color to 7: white */
525 else if (c>=0 && c<7)
526 nstate=(nstate & ~0x80F) | c; /* Set color to c */
527 set_state(nstate);
528 }
529
530
531 /*---------------------------------------------------------------------*/
532 /* Functions to read and translate keys */
533 /*---------------------------------------------------------------------*/
534
535 #ifdef USE_EDITLINE
536
537 #define KEYBUFF_SIZE 64 /* Needs to be power of 2 */
538 static int keyptr=0; /* Where new characters go */
539 static int keyread=0; /* Where old characters get lifted from */
540 static char keybuff[KEYBUFF_SIZE];
541
542 static void push_keychar(char c)
543 {
544 keyread=(keyread+KEYBUFF_SIZE-1)&(KEYBUFF_SIZE-1);
545 keybuff[keyread]=c;
546 }
547
548 static void key_backup(int n)
549 {
550 assert(n>=0);
551 keyread=(keyread+KEYBUFF_SIZE-n)&(KEYBUFF_SIZE-1);
552 }
553
554 static int get_next_char(rbool timed_input)
555 {
556 char c;
557
558 if (keyread!=keyptr) {
559 c=keybuff[keyread];
560 } else {
561 if (timed_input) {
562 read(STDIN_FILENO,&c,1); /* Actually read in the character */
563 } else
564 read(STDIN_FILENO,&c,1); /* Actually read in the character */
565 if (DEBUG_KEY) printf("<%d>",c);
566 keybuff[keyptr]=c;
567 keyptr=(keyptr+1)&(KEYBUFF_SIZE-1);
568 }
569 keyread=(keyread+1)&(KEYBUFF_SIZE-1);
570 return c;
571 }
572
573
574 int read_a_key(void)
575 {
576 unsigned long keyflag;
577 int c;
578 int keymatch; /* Possible matched keys */
579 int key_leng; /* Length of matched key */
580 int i,j;
581 rbool nomatch;
582
583 scroll_count=0;
584 keyflag=(1<<NUM_KEYS)-1; /* All keys are possible */
585 j=0; /* Start with first character */
586 keymatch=1000; /* No keys matched yet. */
587 key_leng=0;
588
589 do {
590 c=get_next_char(j!=0);
591 if (c==500) { /* Out of time */
592 j++;
593 break;
594 }
595 for(i=0;i<NUM_KEYS;i++)
596 if (keyflag&(1<<i)) { /* Still in consideration */
597 if (term_key[i]!=NULL && term_key[i][j]!=c)
598 nomatch=1;
599 else nomatch=0;
600 if (nomatch && j==1 && term_key[i][0]=='\033') {
601 /* Check alternative VT-100 codes */
602 if ( (c=='O' || c=='[') &&
603 (term_key[i][1]=='O' || term_key[i][1]=='[') )
604 nomatch=0;
605 }
606 if (term_key[i]==NULL || nomatch)
607 keyflag&=~(1<<i);
608 else if (term_key[i][j+1]==0) {
609 keymatch=i;
610 key_leng=j+1;
611 }
612 }
613 if (DEBUG_KEY) printf("[%lx]",keyflag);
614 j++;
615 } while (keyflag!=0 && keymatch==1000);
616 if (key_leng==0) key_leng=1; /* Always skip at least one character */
617 key_backup(j-key_leng);
618 if (keymatch==1000) {
619 key_backup(1);
620 c=get_next_char(0);
621 if (isprint(c)) return c;
622 else if (c=='\n' || c=='\r') return '\n';
623 else if (c=='\t') {
624 for(i=0;i<4;i++)
625 push_keychar(' ');
626 return ' ';
627 }
628 else switch(c)
629 {
630 case '\001': return 1006; /* Ctrl-A ==> Home */
631 case '\002': return 1002; /* Ctrl-B ==> Left arrow */
632 case '\005': return 1007; /* Ctrl-E ==> End */
633 case '\006': return 1003; /* Ctrl-F ==> Right arrow */
634 case '\010': /* Fall though... */
635 case '\177': return 1000;/* Ctrl-H and <Del> ==> Backspace */
636 case '\013': return 1500; /* Ctrl-K */
637 case '\014': return 1502; /* Ctrl-L */
638 case '\016': return 1005; /* Ctrl-N ==> Down arrow */
639 case '\020': return 1004; /* Ctrl-P ==> Up arrow */
640 case '\031': return 1501; /* Ctrl-Y */
641 default: return 2000;
642 }
643 }
644 if (nav_arrow) {
645 /* Translate keymatches to macro codes */
646 if (keymatch>=1 && keymatch<=1010)
647 keymatch+=FUN_KEY_END-1;
648 }
649 return keymatch+1000;
650 }
651
652 /*
653 1000= backspace
654 1001= delete
655 1002= left
656 1003= right
657 1004= up
658 1005= down
659 1006= home
660 1007= end
661 1008= page up
662 1009= page down
663 101n= Function key F<n>. (F0==F10)
664 1020,1021: F11,F12 (?)
665
666 1500= ^K
667 1501= ^Y
668 1502= ^L
669 2000= unknown control character
670 */
671
672
673
674 /*---------------------------------------------------------------------*/
675 /* Functions for the Built-In Line Editor */
676 /*---------------------------------------------------------------------*/
677
678 void agt_putc(char c)
679 {
680 fix_loc(); /* Fixup if returning from being suspended */
681 printf("%c",c);
682 curr_x+=1;
683 }
684
685
686 static int base_x,base_y; /* Base X value of first character of input */
687
688 /* This assumes the cursor is always positioned on the string
689 being edited */
690 static void update_cursor(int cursor)
691 {
692 curr_x=base_x+cursor;
693 curr_y=base_y;
694 while (curr_x>=screen_width) {
695 curr_x-=screen_width;
696 curr_y++;
697 }
698 gotoxy(curr_x,curr_y);
699 }
700
701 static rbool save_accum_text=0;
702 static rbool old_yheight=0;
703
704 static void update_line(int cursor,char *buff)
705 {
706 int yval, yheight, xpos, i;
707
708 yheight=(base_x+strlen(buff)+screen_width-1)/screen_width; /* Round up */
709 gotoxy(screen_width-1,screen_height-1);
710 while (base_y+yheight>screen_height && base_y>1) {
711 base_y--;
712 save_accum_text=1;
713 agt_newline();
714 save_accum_text=0;
715 }
716 for(yval=yheight;yval<old_yheight;yval++) {
717 gotoxy(0,base_y+yval);
718 cleareol();
719 }
720 old_yheight=yheight;
721 xpos=cursor-1;
722 if (xpos<0) xpos=0;
723 update_cursor(xpos);
724 for(yval=curr_y-base_y;yval<yheight;yval++) {
725 cleareol();
726 for(i=curr_x;i<screen_width;i++) {
727 if (buff[xpos]==0) break;
728 putchar(buff[xpos++]);
729 }
730 curr_x=0;
731 if (base_y+yval+1<screen_height)
732 gotoxy(curr_x,base_y+yval+1);
733 else break;
734 }
735 update_cursor(cursor);
736 }
737
738 static void redisplay_line(char *buff)
739 {
740 update_line(0,buff);
741 }
742
743 #define ENABLE_HIST
744 static char **hist=NULL;
745 static long histcount=0;
746 static long histmax=0; /* No limit on history */
747
748 static char *line_edit(int state)
749 /* state=0, input a line.
750 state=1, input character with echo
751 state=2, input character with no echo */
752 {
753 static char *yank_text=NULL;
754 rbool editmode, exitflag;
755 int key;
756 int buffleng, buffspace; /* buffspace is space allocated for buffer,
757 buffleng is space really used */
758 static int cursor; /* Where the insertion point is in buffer:
759 insert at this location */
760 char *buff, *savebuff;
761 int i, curr_hist;
762
763 buff=rmalloc(2);
764 savebuff=NULL; curr_hist=histcount;
765 buff[0]=buff[1]=0;
766 cursor=buffleng=0;buffspace=2;
767 base_x=curr_x; base_y=curr_y;
768 editmode=(state==0);
769 old_yheight=0;
770 scroll_count=0;
771
772 set_term_noncanon();
773 keyread=keyptr=0; /* Clear key-buffer */
774
775 for(exitflag=0;!exitflag;) {
776 key=read_a_key();
777 if (DEBUG_KEY) printf("{%d}",key);
778 if (key<1000) { /* Real character entered */
779 if (state>0) { /* One key */
780 if (state==1) {
781 agt_putc(key);
782 }
783 buff[0]=key; buff[1]=0;
784 buffleng=1;
785 break;
786 }
787
788 #ifdef ENABLE_HIST
789 if (savebuff!=NULL) {
790 rfree(savebuff);
791 buff=rstrdup(buff);
792 buffspace=buffleng+1;
793 }
794 #endif
795 if (key=='\n') {
796 update_cursor(buffleng);
797 break;
798 }
799 /* add character to buffer */
800 buffleng++;
801 if (buffleng+1>buffspace) buffspace+=20;
802 buff=rrealloc(buff,buffspace);
803 for(i=buffleng;i>cursor;i--)
804 buff[i]=buff[i-1];
805 buff[cursor++]=key;
806 update_line(cursor,buff);
807 }
808 else switch(key-1000)
809 { /* Special key */
810
811
812 case 0: /* Backspace */
813 if (!editmode || cursor==0) break;
814 cursor--;
815 /* Fall through... */
816
817
818 case 1: /* Delete */
819 if (!editmode || buffleng==cursor) break;
820 #ifdef ENABLE_HIST
821 if (savebuff!=NULL) {
822 rfree(savebuff);
823 buff=rstrdup(buff);
824 buffspace=buffleng+1;
825 }
826 #endif
827 for(i=cursor;i<buffleng;i++)
828 buff[i]=buff[i+1];
829 buffleng--;
830 update_line(cursor,buff);
831 break;
832
833
834 case 2: /* Left arrow */
835 if (editmode && cursor>0)
836 update_cursor(--cursor);
837 break;
838
839
840 case 3: /* Right arrow */
841 if (editmode && cursor<buffleng)
842 update_cursor(++cursor);
843 break;
844
845
846 #ifdef ENABLE_HIST
847 case 4: /* Up arrow: Command history */
848 if (!editmode || histcount==0) break;
849 if (savebuff==NULL) { /* Save current line */
850 savebuff=buff;
851 curr_hist=histcount;
852 }
853 curr_hist--;
854 if (curr_hist<0) {
855 curr_hist=0;
856 break;
857 }
858 buff=hist[curr_hist];
859 buffleng=strlen(buff);
860 cursor=0;
861 redisplay_line(buff);
862 break;
863
864 case 5: /* Down arrow: Command history */
865 if (!editmode || savebuff==NULL) break;
866 curr_hist++;
867 if (curr_hist>=histcount) {
868 /* Reached bottom: restore original line */
869 curr_hist=histcount;
870 buff=savebuff;
871 savebuff=NULL;
872 } else
873 buff=hist[curr_hist];
874 buffleng=strlen(buff);
875 cursor=0;
876 redisplay_line(buff);
877 break;
878 #endif
879
880 case 6: /* Home */
881 if (!editmode) break;
882 cursor=0;
883 update_cursor(cursor);
884 break;
885
886 case 7: /* End */
887 if (!editmode) break;
888 cursor=buffleng;
889 update_cursor(cursor);
890 break;
891
892 case 8: /* Page up : Scroll back */
893 case 9: /* Page down */
894 break;
895
896
897 case 500: /* Ctrl-K: Delete to EOL */
898 if (!editmode) break;
899 #ifdef ENABLE_HIST
900 if (savebuff!=NULL) {
901 rfree(savebuff);
902 buff=rstrdup(buff);
903 buffspace=buffleng+1;
904 }
905 #endif
906 rfree(yank_text);
907 yank_text=rstrdup(buff+cursor);
908 buffleng=cursor;
909 buff[buffleng]=0;
910 update_line(cursor,buff);
911 break;
912
913
914 case 501: /* Ctrl-Y: Yank */
915 if (!editmode) break;
916 {
917 int txtleng;
918 #ifdef ENABLE_HIST
919 if (savebuff!=NULL) {
920 rfree(savebuff);
921 buff=rstrdup(buff);
922 buffspace=buffleng+1;
923 }
924 #endif
925 if (yank_text==NULL)
926 txtleng=0;
927 else
928 txtleng=strlen(yank_text);
929 buffleng+=txtleng;
930 while(buffleng+1>buffspace) buffspace+=20;
931 buff=rrealloc(buff,buffspace);
932 for(i=buffleng;i>=cursor+txtleng;i--)
933 buff[i]=buff[i-txtleng];
934 for(i=0;i<txtleng;i++)
935 buff[cursor+i]=yank_text[i];
936 update_line(cursor,buff);
937 cursor+=txtleng;
938 update_cursor(cursor);
939 }
940 break;
941
942
943 case 502: /* Ctrl-L: Redraw screen */
944 agt_clrscr();
945 base_y=curr_y;
946 printf("%s",accum_text); /* Redraw prompt */
947 if (editmode)
948 redisplay_line(buff);
949 break;
950
951 /* Function key <key-1010> */
952 default:
953 if (key-1000<FUN_KEY_BASE || key-1000>LAST_FUN_KEY)
954 break;
955
956 if (!editmode) break;
957 {
958 int txtleng;
959 #ifdef ENABLE_HIST
960 if (savebuff!=NULL) {
961 rfree(savebuff);
962 buff=rstrdup(buff);
963 buffspace=buffleng+1;
964 }
965 #endif
966 txtleng=strlen(fkey_text[key-1010]);
967 if (fkey_text[key-1010][txtleng-1]=='\n') {
968 /* Newlines should only appear at the end; they indicate
969 that this function key should cause the line to be entered. */
970 txtleng--;
971 exitflag=1; /* Finish entering this line */
972 }
973 buffleng+=txtleng;
974 while(buffleng+1>buffspace) buffspace+=20;
975 buff=rrealloc(buff,buffspace);
976 for(i=buffleng;i>=cursor+txtleng;i--)
977 buff[i]=buff[i-txtleng];
978 for(i=0;i<txtleng;i++)
979 buff[cursor+i]=fkey_text[key-1010][i];
980 update_line(cursor,buff);
981 cursor+=txtleng;
982 update_cursor(cursor);
983 }
984 break;
985 }
986 if (exitflag) break;
987 }
988 set_term_normal();
989 buff=rrealloc(buff,(buffleng+1));
990 if (state==0) {
991 if (histmax==0 || histcount<histmax)
992 hist=rrealloc(hist,(++histcount)*sizeof(char*));
993 else
994 for(i=0;i<histcount-1;i++)
995 hist[i]=hist[i+1];
996 hist[histcount-1]=rstrdup(buff);
997 }
998 return buff;
999 }
1000
1001 #endif
1002
1003
1004
1005
1006
1007 /*---------------------------------------------------------------------*/
1008 /* High Level Input Functions */
1009 /*---------------------------------------------------------------------*/
1010
1011 char *agt_input(int in_type)
1012 /* read a line from the keyboard, allocating space for it using malloc */
1013 /* See os_none.c for a discussion of in_type; we ignore it. */
1014 {
1015 #ifndef USE_EDITLINE
1016 static int input_error=0;
1017 #endif
1018 char *s;
1019
1020 print_statline();
1021 fix_loc();
1022
1023 #ifdef USE_READLINE /* This doesn't quite work yet. */
1024 if (in_type==4) /* Filename */
1025 rl_bind_key('\t',rl_complete);
1026 clearline();
1027 s=readline(accum_text);
1028 if (in_type==0) /* Normal input */
1029 add_history(s);
1030 if (in_type==4)
1031 rl_bind_key('\t',rl_insert);
1032 #elif defined(USE_EDITLINE)
1033 s=line_edit(0);
1034 #else
1035 s=rmalloc(1001);
1036 if (fgets(s,1000,stdin)==NULL) {
1037 if (++input_error>10) {
1038 printf("Read error on input\n");
1039 exit(EXIT_FAILURE);
1040 }
1041 s[0]=0;
1042 clearerr(stdin);
1043 } else input_error=0; /* Reset counter */
1044 #endif
1045
1046 #ifdef TEST_NONL
1047 {
1048 char *t;
1049 for(t=s+strlen(s)-1;(t>s) && (*t=='\n');t--);
1050 if (*t!=0 || t<s) t++;
1051 *t=0;
1052 }
1053 #endif
1054
1055 script_out(s);
1056
1057 #ifdef USE_EDITLINE
1058 agt_newline();
1059 #else
1060 curr_x+=strlen(s);
1061 while(curr_x>=screen_width) {
1062 curr_x-=screen_width;
1063 set_new_line();
1064 }
1065 set_new_line();
1066 #endif
1067
1068 scroll_count=0;
1069 return s;
1070 }
1071
1072
1073
1074
1075 char agt_getkey(rbool echo_char)
1076 /* Reads a character and returns it */
1077 /* If echo_char=1, echo character. If 0, don't */
1078 {
1079 char c;
1080 #ifdef USE_EDITLINE
1081 char *s;
1082 #endif
1083
1084 print_statline();
1085 scroll_count=0;
1086
1087 #ifdef USE_EDITLINE
1088 s=line_edit(echo_char?1:2);
1089 c=s[0];
1090 if (echo_char==1) {
1091 script_out(s);
1092 agt_newline();
1093 }
1094 rfree(s);
1095 #else
1096 set_term_noncanon();
1097 read(STDIN_FILENO,&c,1); /* Actually read in the character */
1098 set_term_normal();
1099 if (echo_char) {
1100 char cstr[2];
1101
1102 cstr[0]=c;cstr[1]=0;
1103 agt_puts(cstr);
1104 agt_newline();
1105 }
1106 #endif
1107
1108 scroll_count=0;
1109 if (!save_accum_text) {
1110 acc_ptr=0;accum_text[0]=0;
1111 }
1112 return c;
1113 }
1114
1115
1116
1117
1118 /*---------------------------------------------------------------------*/
1119 /* Printing the Status Line */
1120 /*---------------------------------------------------------------------*/
1121
1122 static void print_compass(void)
1123 {
1124 int cwidth, i;
1125
1126 if (status_width< 9+4*12) return;
1127
1128 printf("\n EXITS: ");cwidth=9;
1129 for(i=0;i<12;i++)
1130 if (compass_rose & (1<<i)) {
1131 printf("%s ",exitname[i]);
1132 cwidth+=strlen(exitname[i])+1;
1133 }
1134 for(i=cwidth;i<status_width;i++) putchar(' ');
1135 }
1136
1137 void agt_statline(const char *s)
1138 /* Output a string in reverse video (or whatever) at top of screen */
1139 {
1140 if (BATCH_MODE) return;
1141
1142 show_cursor(0);
1143 set_scroll(0);
1144 gotoxy(0,0);
1145
1146 push_state(STATUS_STATE);
1147 printf("%s",s);
1148 if (have_compass)
1149 print_compass();
1150 pop_state();
1151
1152 set_scroll(status_height);
1153 winsize_flag=0;
1154 gotoxy(curr_x,curr_y);
1155 putchar(' '); /* To supress spurious bold square */
1156 gotoxy(curr_x,curr_y);
1157 show_cursor(1);
1158 }
1159
1160
1161
1162 /*---------------------------------------------------------------------*/
1163 /* High Level Output Functions */
1164 /*---------------------------------------------------------------------*/
1165
1166 void agt_more(void)
1167 {
1168 if (BATCH_MODE) return;
1169 printf(" --MORE--");
1170 agt_waitkey();
1171 clearline();
1172 scroll_count=0;
1173 }
1174
1175
1176 void agt_newline(void)
1177 {
1178 if (!save_accum_text) {
1179 script_out("\n");
1180 set_new_line();
1181 }
1182
1183 if (BATCH_MODE) return;
1184
1185 if (curr_y!=screen_height || /* No need to scroll */
1186 term_cmd[TC_SETSCROLL]!=NULL || /* ..or automatic scrolling */
1187 term_cmd[TC_DELLINE]==NULL) /* ... or nothing we can do */
1188 printf("\n");
1189 else {
1190 show_cursor(0);
1191 gotoxy(0,status_height);
1192 tputs(term_cmd[TC_DELLINE],screen_height-1,putchar);
1193 gotoxy(0,screen_height-1);
1194 show_cursor(1);
1195 }
1196 if (!save_accum_text) {
1197 if (scroll_count>=screen_height-(have_compass? 3 : 2) )
1198 agt_more(); /* Print out MORE message */
1199 fix_loc(); /* Fixup if returning from being suspended */
1200 }
1201 }
1202
1203
1204 void agt_puts(const char *s)
1205 /* This routine assumes that s is not wider than the screen */
1206 {
1207 script_out(s);
1208 curr_x+=strlen(s);
1209 add_accum_text(s);
1210
1211 if (BATCH_MODE) return;
1212
1213 fix_loc(); /* Fixup if returning from being suspended */
1214 printf("%s",s);
1215 }
1216
1217
1218 void agt_clrscr(void)
1219 {
1220 cont_flag=0;winsize_flag=0;
1221 scroll_count=0;
1222 curr_x=0;
1223 if (have_compass) curr_y=2;
1224 else curr_y=1;
1225 script_out("\n\n\n\n\n");
1226
1227 if (BATCH_MODE) return;
1228
1229 tputs(term_cmd[TC_CLRSCR],screen_height,putchar);
1230 printf("\n"); /* To make space for status line */
1231 if (have_compass) printf("\n");
1232 }
1233
1234
1235
1236
1237 /*---------------------------------------------------------------------*/
1238 /* Box Printing Routines */
1239 /*---------------------------------------------------------------------*/
1240
1241 static unsigned long boxflags;
1242 static int box_startx; /* Starting x of box */
1243 static int box_width;
1244 static int delta_scroll; /* Amount we are adding to vertical scroll
1245 with the box */
1246
1247 static void boxrule(void)
1248 /* Draw line at top or bottom of box */
1249 {
1250 int i;
1251
1252 if (RVID_BOX) agt_puts(" ");
1253 else agt_puts("+-");
1254
1255 for(i=0;i<box_width;i++)
1256 if (RVID_BOX) agt_puts(" ");
1257 else agt_puts("-");
1258
1259 if (RVID_BOX) agt_puts(" ");
1260 else agt_puts("-+");
1261 }
1262
1263 static void boxpos(void)
1264 {
1265 int i;
1266
1267 curr_x=box_startx; curr_y++;
1268 if (curr_y>=screen_height) curr_y=screen_height-1;
1269 gotoxy(curr_x,curr_y);
1270
1271 script_out("\n");
1272 for(i=0;i<box_startx;i++)
1273 script_out(" ");
1274 }
1275
1276 void agt_makebox(int width,int height,unsigned long flags)
1277 /* Flags: TB_TTL, TB_BORDER, TB_NOCENT */
1278 {
1279 int i;
1280
1281 boxflags=flags;
1282 box_width=width;
1283 if (boxflags&TB_BORDER) { /* Add space for border */
1284 width+=4;
1285 height+=2;
1286 }
1287 if (boxflags&TB_NOCENT) box_startx=0;
1288 else box_startx=(screen_width-width)/2; /* Center the box horizontally */
1289 if (box_startx<0) box_startx=0;
1290
1291 /* Now we need to compute the vertical position of the box */
1292 if (flags & TB_TTL) { /* Title: centered horizontally */
1293 curr_y=(screen_height-height)/2;
1294 if (curr_y+height+8>screen_height) /* Make room for credits */
1295 curr_y=screen_height-height-8;
1296 if (curr_y<0) curr_y=0;
1297 delta_scroll=0;
1298 scroll_count=0;
1299 } else { /* Compute vertical position of non-title box */
1300 curr_y=curr_y-height;
1301 if (curr_y<1) {
1302 delta_scroll=1-curr_y;
1303 curr_y=1;
1304 } else delta_scroll=0;
1305 }
1306
1307 curr_x=box_startx;
1308 gotoxy(curr_x,curr_y);
1309
1310 script_out("\n");
1311 for(i=0;i<box_startx;i++)
1312 script_out(" ");
1313
1314 if (boxflags&TB_BORDER) {
1315 push_state(cstate | 0x2000); /* Turn on reverse video */
1316 boxrule();
1317 boxpos();
1318 if (RVID_BOX) agt_puts(" ");
1319 else agt_puts("| ");
1320 }
1321 }
1322
1323 void agt_qnewline(void)
1324 {
1325 if (boxflags&TB_BORDER) {
1326 if (RVID_BOX) agt_puts(" ");
1327 else agt_puts(" |");
1328 }
1329 boxpos();
1330 if (boxflags&TB_BORDER) {
1331 if (RVID_BOX) agt_puts(" ");
1332 else agt_puts("| ");
1333 }
1334 }
1335
1336 void agt_endbox(void)
1337 {
1338 if (boxflags&TB_BORDER) {
1339 if (RVID_BOX) agt_puts(" ");
1340 else agt_puts(" |");
1341 boxpos();
1342 boxrule();
1343 if (RVID_BOX) pop_state();
1344 }
1345 scroll_count+=delta_scroll;
1346 agt_newline(); /* NOT agt_qnewline() */
1347 if ( (boxflags&TB_TTL) ) {
1348 agt_puts(" "); /* Neccessary to get things to work right w/xterm */
1349 while(curr_y<screen_height-8) agt_newline();
1350 }
1351 }
1352
1353
1354
1355
1356
1357 /*---------------------------------------------------------------------*/
1358 /* Option Parsing Routines */
1359 /*---------------------------------------------------------------------*/
1360
1361
1362 #ifdef LINUX_COLOR
1363 /* It would be good to make this list user-customizable */
1364 int NUM_COLOR_TERM=0;
1365 char **color_term=NULL;
1366
1367 void add_colorterm(char *term_name)
1368 {
1369 NUM_COLOR_TERM++;
1370 color_term=rrealloc(color_term,NUM_COLOR_TERM*sizeof(char*));
1371 color_term[NUM_COLOR_TERM-1]=rstrdup(term_name);
1372 }
1373
1374 #endif
1375
1376
1377
1378 /* Duplicate s and add '/' if neccessary */
1379 char *makepathentry(char *s, int leng)
1380 {
1381 char *p;
1382 rbool addslash;
1383
1384 addslash=(s[leng-1]!='/');
1385 p=rmalloc(leng+addslash+1);
1386 strncpy(p,s,leng);
1387 if (addslash)
1388 p[leng++]='/';
1389 p[leng]=0; /* Mark new end of string */
1390 return p;
1391 }
1392
1393 static void free_gamepath(void)
1394 {
1395 char **p;
1396
1397 if (gamepath==NULL) return;
1398
1399 /* We skip the first element which always points to "" */
1400 for(p=gamepath+1;*p!=NULL;p++)
1401 rfree(*p);
1402 rfree(gamepath);
1403 }
1404
1405
1406 static void get_agt_path(void)
1407 {
1408 char *path, *end;
1409 int pathleng; /* Number of elements in gamepath */
1410
1411 free_gamepath();
1412 pathleng=0;
1413 path=getenv("AGT_PATH");
1414 if (path==NULL) return;
1415 while(*path!=0) { /* Find each componenent of the path */
1416 while(isspace(*path) || *path==':') path++; /* Skip excess whitespace */
1417 for(end=path;*end!=0 && !isspace(*end) && *end!=':';end++);
1418 if (end!=path) { /* There's actually something there */
1419 pathleng++;
1420 gamepath=rrealloc(gamepath,(pathleng+1)*sizeof(char*));
1421 gamepath[pathleng-1]=makepathentry(path,end-path);
1422 gamepath[pathleng]=NULL;
1423 }
1424 path=end;
1425 }
1426 }
1427
1428
1429 void set_fkey(char *keyname, char *words[], int numwords)
1430 {
1431 int n,i,leng;
1432 char *s;
1433 rbool err;
1434 static long keys_defined=0;
1435
1436 err=0; n=0;
1437 if (tolower(keyname[0])!='f') {
1438 err=1;
1439 for(i=0;i<10;i++)
1440 if (0==strcasecmp(keylist[i],keyname)) {
1441 err=0;
1442 n=i+12;
1443 }
1444 } else { /* Name of form 'Fnn' */
1445 n=strtol(keyname+1,&s,0); /* s will point to the first erroneous char */
1446 if (keyname[1]==0 || *s!=0) err=1;
1447 if (n>12) err=1;
1448 if (n==10) n=0;
1449 if (n>10) n--;
1450 }
1451 if (err) {
1452 writeln("Unrecognized KEY name.");
1453 return;
1454 }
1455
1456 leng=0;
1457 for(i=0;i<numwords;i++)
1458 leng+=strlen(words[i]);
1459 s=rmalloc(leng+numwords+1);
1460 s[0]=0;
1461
1462 /* This isn't very efficient, but it doesn't need to be. */
1463 for(i=0;i<numwords;i++) {
1464 strcat(s,words[i]);
1465 strcat(s," ");
1466 }
1467 i=strlen(s)-2;
1468 if (i>=0 && s[i]=='+') {
1469 s[i]='\n';
1470 s[i+1]=0;
1471 }
1472 if (keys_defined & (1<<n)) rfree(fkey_text[n]);
1473 fkey_text[n]=s;
1474 keys_defined|=(1<<n);
1475 }
1476
1477
1478 #define opt(s) (!strcasecmp(optstr[0],s))
1479
1480 rbool agt_option(int optnum,char *optstr[],rbool setflag)
1481 /* If setflag is 0, then the option was prefixed with NO_ */
1482 {
1483 int i;
1484
1485 #ifdef LINUX_COLOR
1486 if (opt("COLORTERM") && optnum>=2 && setflag) {
1487 for(i=1;i<optnum;i++)
1488 add_colorterm(optstr[i]);
1489 return 1;
1490 }
1491 #endif
1492 #ifdef LINUX
1493 if (opt("FONT")) {
1494 font_enable=setflag;
1495 return 1;
1496 }
1497 #endif
1498 if (opt("COMPASS")) {
1499 have_compass=setflag;
1500 status_height=1+setflag;
1501 return 1;
1502 }
1503 if (opt("KEY") && optnum>=2) {
1504 set_fkey(optstr[1],optstr+2,optnum-2);
1505 return 1;
1506 }
1507 if (opt("HISTORY")) {
1508 histmax=strtol(optstr[1],NULL,10);
1509 if (histmax<0) histmax=0;
1510 return 1;
1511 }
1512 if (opt("LOWER_FILE")) {
1513 lower_file=setflag;
1514 return 1;
1515 }
1516 if (opt("NAVARROW")) {
1517 nav_arrow=setflag;
1518 return 1;
1519 }
1520 if (opt("VIEWER")) {
1521 viewer_name=rstrdup(optstr[1]);
1522 return 1;
1523 }
1524 if (opt("PATH") && optnum>=2) {
1525 if (gamepath!=NULL) return 1; /* First setting wins */
1526 gamepath=rmalloc((optnum+1)*sizeof(char*)); /* optnum-1+2 */
1527 gamepath[0]=""; /* ==> The current directory */
1528 for(i=1;i<optnum;i++) /* Starting at the 2nd entry of both */
1529 gamepath[i]=makepathentry(optstr[i],strlen(optstr[i]));
1530 gamepath[optnum]=NULL;
1531 return 1;
1532 }
1533 return 0;
1534 }
1535
1536 #undef opt
1537
1538
1539
1540
1541 /*---------------------------------------------------------------------*/
1542 /* Interface Initializations */
1543 /*---------------------------------------------------------------------*/
1544
1545 #ifdef LINUX_COLOR
1546
1547 /* Return true if tty supports colors */
1548 /* It also frees the color_term array, so it can only be called once */
1549 rbool color_tty(char *termtype)
1550 {
1551 int i;
1552 rbool ctty;
1553
1554 ctty=0;
1555 for(i=0;i<NUM_COLOR_TERM;i++) {
1556 if (strcmp(termtype,color_term[i])==0) ctty=1;
1557 rfree(color_term[i]);
1558 }
1559 rfree(color_term);
1560 return ctty;
1561 }
1562
1563 #endif /* LINUX_COLOR */
1564
1565
1566
1567 static char *tcptr;
1568 static char *termtype;
1569
1570 void init_interface(int argc,char *argv[])
1571 {
1572 int i;
1573 struct stat fileinfo;
1574 ino_t inode;
1575 dev_t devnum;
1576
1577 viewer_name=NULL;
1578 have_compass=0; status_height=1;
1579 script_on=0;scriptfile=BAD_TEXTFILE;
1580 center_on=0;
1581 par_fill_on=0;
1582 scroll_count=0;
1583 nav_arrow=0;
1584
1585 font_enable=0;
1586
1587 acc_ptr=0; accum_text[0]=0;
1588
1589 if (BATCH_MODE) {
1590 screen_height=25;
1591 screen_width=status_width=80;
1592 return;
1593 }
1594
1595 get_agt_path();
1596
1597 /* DEBUG_OUT should be true if and only if debugfile and stdin
1598 refer to different devices */
1599 fstat(STDERR_FILENO,&fileinfo);
1600 inode=fileinfo.st_ino;
1601 devnum=fileinfo.st_dev;
1602 fstat(STDIN_FILENO,&fileinfo);
1603 DEBUG_OUT=(inode!=fileinfo.st_ino || devnum!=fileinfo.st_dev);
1604 debugfile=stderr;
1605
1606 /* DEBUG_OUT=!isatty(STDERR_FILENO); */
1607 setvbuf(stdout,NULL,_IONBF,BUFSIZ);
1608
1609 if (!isatty(STDIN_FILENO)) {
1610 printf("Sorry, this program requires standard input to be a tty.");
1611 exit(0);
1612 }
1613 tcgetattr(STDIN_FILENO,&term_save);
1614
1615 #ifndef NEXT /* ?!?! */
1616 if (atexit(restore_terminal)!=0)
1617 {printf("INTERNAL ERROR: Unable to register restore_terminal.\n");
1618 exit(0);}
1619 #endif
1620
1621 #ifndef NO_TERMCAP_H
1622 ospeed=cfgetospeed(&term_save);
1623 #endif
1624
1625 termtype=getenv("TERM");
1626 if (termtype==NULL) {
1627 printf("ERROR: No terminal type defined.");
1628 exit(0);}
1629 switch(tgetent(term_buff,termtype)) {
1630 case -1:printf("ERROR: Unable to access TERMCAP database.\n");
1631 exit(0);
1632 case 0:printf("ERROR: Unknown terminal type.\n");
1633 exit(0);
1634 }
1635
1636 screen_height=screen_width=0;
1637 #ifdef CATCH_WINDOW_CHANGE
1638 window_change(SIGWINCH);
1639 #endif
1640
1641 if (screen_width==0) screen_width=tgetnum("co");
1642 if (screen_width==0) screen_width=80;
1643 if (screen_height==0) screen_height=tgetnum("li");
1644 if (screen_height==0) screen_height=24;
1645
1646 tcptr=term_cmd_buff;
1647 BC=tgetstr("le",&tcptr);
1648 UP=tgetstr("up",&tcptr);
1649 PC=tgetnum("pc");
1650
1651 /* First, the essentials */
1652 term_cmd[TC_GOXY]=tgetstr("cm",&tcptr);
1653 term_cmd[TC_CLRSCR]=tgetstr("cl",&tcptr);
1654 if (term_cmd[TC_GOXY]==NULL || term_cmd[TC_CLRSCR]==NULL) {
1655 printf("You terminal is missing features required by this program.");
1656 exit(0);
1657 }
1658
1659 /* Now a bunch of optional features */
1660 term_cmd[TC_CR]=tgetstr("cr",&tcptr);
1661 term_cmd[TC_CLEARLINE]=tgetstr("ce",&tcptr);
1662 term_cmd[TC_HIDE]=tgetstr("vi",&tcptr);
1663 term_cmd[TC_SHOW]=tgetstr("ve",&tcptr);
1664 term_cmd[TC_BLINK]=tgetstr("mb",&tcptr);
1665 term_cmd[TC_NORM]=tgetstr("me",&tcptr);
1666 term_cmd[TC_DELLINE]=tgetstr("dl",&tcptr);
1667 #ifdef HAVE_TPARAM
1668 term_cmd[TC_SETSCROLL]=tgetstr("cs",&tcptr);
1669 #else
1670 term_cmd[TC_SETSCROLL]=NULL;
1671 #endif
1672 term_cmd[TC_BOLD]=tgetstr("md",&tcptr);
1673 term_cmd[TC_NOBOLD]=tgetstr("me",&tcptr);
1674 term_cmd[TC_INITKEY]=tgetstr("ks",&tcptr);
1675 term_cmd[TC_RESTOREKEY]=tgetstr("ke",&tcptr);
1676
1677 /* And key codes */
1678 for(i=0;i<NUM_KEYS;i++)
1679 term_key[i]=tgetstr(term_keycap[i],&tcptr);
1680
1681 if (term_key[10]==NULL) /* If at first you don't succeed... */
1682 term_key[10]=tgetstr("k;",&tcptr);
1683
1684
1685 control_width=tgetnum("sg"); /* Width of control markers */
1686 if (control_width<0) control_width=0;
1687 status_width=screen_width-2*control_width;
1688 signal(SIGCONT,cont_handler);
1689 signal(SIGPIPE,SIG_IGN);
1690
1691 #ifdef USE_READLINE
1692 rl_bind_key('\t',rl_insert); /* Turn off tab-completion */
1693 using_history();
1694 #endif
1695 }
1696
1697 #ifdef REPLACE_BNW
1698 static void save_old_font(void);
1699 static void restore_font(void);
1700 #endif
1701
1702 void start_interface(fc_type fc)
1703 {
1704 if (stable_random)
1705 srand(6);
1706 else
1707 srand(time(0));
1708 game_fc=fc;
1709 free_gamepath(); /* It's not needed anymore */
1710
1711 if (BATCH_MODE) {
1712 agt_clrscr();
1713 return;
1714 }
1715
1716 #ifdef LINUX_COLOR
1717 color_on=0;
1718 add_colorterm("linux");
1719 add_colorterm("xterm");
1720 if (color_tty(termtype))
1721 {
1722 strcpy(tcptr,TS_LINUX_SL);
1723 term_cmd[TC_SL]=tcptr;
1724 tcptr+=strlen(TS_LINUX_SL)+1;
1725 strcpy(tcptr,TS_LINUX_NORM);
1726 term_cmd[TC_ENDSL]=tcptr;
1727 tcptr+=strlen(TS_LINUX_NORM)+1;
1728 tputs(term_cmd[TC_ENDSL],1,putchar);
1729 color_on=1;
1730 /* Switch to PC character set */
1731 if (!fix_ascii_flag) printf("\033(U");
1732 }
1733 else
1734 #endif
1735 {
1736 term_cmd[TC_SL]=tgetstr("mr",&tcptr); /* Status line attributes */
1737 if (term_cmd[TC_SL]!=NULL)
1738 term_cmd[TC_ENDSL]=term_cmd[TC_NORM];
1739 else { /* Reverse video unavailable; try standout */
1740 term_cmd[TC_SL]=tgetstr("so",&tcptr);
1741 if (term_cmd[TC_SL]!=NULL)
1742 term_cmd[TC_ENDSL]=tgetstr("se",&tcptr);
1743 else { /* That didn't work either... we'll just have to use vanilla
1744 text */
1745 term_cmd[TC_SL]=term_cmd[TC_ENDSL]="";
1746 }
1747 }
1748 }
1749 set_state(START_STATE);
1750 tputs(term_cmd[TC_INITKEY],1,putchar);
1751 agt_clrscr();
1752 }
1753
1754
1755 void close_interface(void)
1756 {
1757 rfree(viewer_name);
1758 set_state(START_STATE);
1759 tputs(term_cmd[TC_RESTOREKEY],1,putchar);
1760 #ifdef LINUX_COLOR
1761 if (!fix_ascii_flag) printf("\033c"); /* Reset console. */
1762 #endif
1763 if (filevalid(scriptfile,fSCR))
1764 close_pfile(scriptfile,0);
1765 }
1766
1767
1768
1769 #ifdef REPLACE_BNW
1770
1771 /* This cheats a bit, using "hidden" data from filename.c */
1772
1773 static FILE *linopen(char *name, char *ext)
1774 {
1775 FILE *f;
1776 char *fname;
1777
1778 fname=assemble_filename(hold_fc->path,name,ext);
1779 f=fopen(fname,"rb");
1780 rfree(fname);
1781 return f;
1782 }
1783
1784
1785 /*------------------------------------------------------------------*/
1786 /* FONT Routines */
1787 /*------------------------------------------------------------------*/
1788
1789 static char *save_font;
1790 static int save_font_height;
1791
1792 static int set_font(char *fontdef, int font_height)
1793 {
1794 #ifdef PIO_FONTX
1795 struct consolefontdesc fontdesc;
1796
1797 fontdesc.charcount=256;
1798 fontdesc.charheight=font_height;
1799 fontdesc.chardata=fontdef;
1800
1801 return ioctl(STDOUT_FILENO,PIO_FONTX,&fontdesc);
1802 #else
1803 #ifdef PIO_FONT
1804 return ioctl(STDOUT_FILENO,PIO_FONT,fontdef);
1805 #endif
1806 #endif
1807 }
1808
1809
1810 static int getfont(char *fontdef, int *font_height)
1811 {
1812 int rval=-1;
1813
1814 #ifdef GIO_FONTX
1815 struct consolefontdesc fontdesc;
1816
1817 fontdesc.chardata=fontdef;
1818 rval=ioctl(STDOUT_FILENO,GIO_FONTX,&fontdesc);
1819 *font_height=fontdesc.charheight;
1820 #else
1821 #ifdef GIO_FONT
1822 rval=ioctl(STDOUT_FILENO,GIO_FONT,save_font); /* Get current font */
1823 *font_height=16;
1824 #endif
1825 #endif
1826 return rval;
1827 }
1828
1829
1830 static void save_old_font(void)
1831 {
1832 if (BATCH_MODE) return;
1833
1834 /* Save existing font */
1835 save_font=rmalloc(256*32);
1836 if (getfont(save_font,&save_font_height))
1837 /* Failed... issue warning? */
1838 rfree(save_font);
1839 }
1840
1841
1842 static void restore_font(void)
1843 {
1844 if (BATCH_MODE) return;
1845 if (save_font==NULL || set_font(save_font,save_font_height)) {
1846 /* Couldn't restore save_font */
1847 #ifdef PIO_FONTRESET
1848 ioctl(STDOUT_FILENO,PIO_FONTRESET);
1849 #endif
1850 }
1851 }
1852
1853
1854
1855 void fontcmd(int cmd,int font)
1856 /* 0=Load font, name is fontlist[font]
1857 1=Restore original font
1858 2=Set startup font. (<gamename>.FNT)
1859 */
1860 {
1861 FILE *fontfile;
1862 char *buff, *fontname, *s;
1863 int count, height;
1864
1865 if (!font_enable) return;
1866 /* (This should be off by default since it changes the fonts
1867 on *all* of the VC's) */
1868
1869 if (cmd==2)
1870 save_old_font();
1871
1872 if (cmd==0 || cmd==2) {
1873 if (cmd==0) {
1874 fontname=fontlist[font];
1875 if (lower_file)
1876 for(s=fontname;*s!=0;s++) *s=tolower(*s);
1877 /* Lowercase the font names as we use them */
1878 }
1879 else fontname=game_fc->gamename;
1880
1881 fontfile=linopen(fontname,pFNT);
1882 if (fontfile==NULL) return;
1883 buff=rmalloc(256*32);
1884 memset(buff,0,256*32);
1885 fseek(fontfile,0,SEEK_END);
1886 height=ftell(fontfile);
1887 if (height%256!=0) {
1888 /* Error message */
1889 return;
1890 }
1891 height=height/256;
1892
1893 fseek(fontfile,0,SEEK_SET);
1894 for(count=0;count<256;count++)
1895 if (fread(buff+count*32,height,1,fontfile)!=1) break;
1896 if (count!=256) {
1897 fclose(fontfile);
1898 rfree(buff);
1899 return;
1900 }
1901 fclose(fontfile);
1902 if (!BATCH_MODE)
1903 if (set_font(buff,height)) {
1904 /* Print error message */
1905 }
1906 rfree(buff);
1907 return;
1908 } else if (cmd==1)
1909 restore_font();
1910 }
1911
1912
1913
1914 /*------------------------------------------------------------------*/
1915 /* Graphics Routines */
1916 /*------------------------------------------------------------------*/
1917
1918 #define GFX_EXT_CNT 17
1919 /* The extension indicates the video mode the picture was intended
1920 to be viewed in. */
1921 static char *gfxext[GFX_EXT_CNT]={".pcx",
1922 ".p06", /* 640x200x2 */
1923 ".p40",".p41",".p42",".p43", /* 320x200x4 */
1924 ".p13", /* 320x200x16 */
1925 ".p19", /* 320x200x256 */
1926 ".p14",".p16", /* 640x200x16, 640x350x16 */
1927 ".p18", /* 640x480x16 */
1928 ".gif",".png",".bmp",".jpg",
1929 ".fli",".flc"};
1930
1931
1932 void pictcmd(int cmd,int pict)
1933 /* 1=show global picture, name is pictlist[pict]
1934 2=show room picture, name is pixlist[pict]
1935 3=show startup picture <gamename>.P..
1936 */
1937 {
1938 char *base, *cmdstr;
1939 int gmode;
1940 FILE *pcxfile;
1941
1942 if (viewer_name==NULL) return;
1943
1944 if (cmd==1) base=pictlist[pict];
1945 else if (cmd==2) base=pixlist[pict];
1946 else if (cmd==3) base=game_fc->gamename;
1947 else return;
1948
1949 /* Find graphics file; determine mode from extension... */
1950 for(gmode=GFX_EXT_CNT-1;gmode>=0;gmode--) {
1951 pcxfile=linopen(base,gfxext[gmode]);
1952 if (pcxfile!=NULL) break;
1953 }
1954 if (pcxfile==NULL) return;
1955 fclose(pcxfile);
1956
1957 base=assemble_filename(hold_fc->path,base,gfxext[gmode]);
1958
1959 cmdstr=rmalloc(strlen(base)+strlen(viewer_name)+40);
1960 sprintf(cmdstr,"%s %s >/dev/null",viewer_name,base);
1961
1962 if (!BATCH_MODE)
1963 system(cmdstr); /* Don't bother to check return status since
1964 there's nothing we could do anyhow */
1965
1966 rfree(cmdstr);
1967 rfree(base);
1968
1969 return;
1970 }
1971
1972
1973
1974 /*------------------------------------------------------------------*/
1975 /* Sound Routines */
1976 /*------------------------------------------------------------------*/
1977
1978 #if 0
1979 static rbool repeat_song=0;
1980 static rbool suspend_song=0;
1981 #endif
1982
1983 int musiccmd(int cmd,int song)
1984 /* For cmd=1 or 2, the name of the song is songlist[song]
1985 The other commands don't take an additional argument.
1986 1=play song
1987 2=repeat song
1988 3=end repeat
1989 4=end song
1990 5=suspend song
1991 6=resume song
1992 7=clean-up
1993 8=turn sound on
1994 9=turn sound off
1995 -1=Is a song playing? (0=false, -1=true)
1996 -2=Is the sound on? (0=false, -1=true)
1997 */
1998 {
1999 if (cmd==8) {
2000 sound_on=1;
2001 return 0;
2002 }
2003 if (cmd==9) {sound_on=0;return 0;}
2004 if (cmd==-2) return sound_on;
2005 #if 0
2006 if (cmd==3) {repeat_song=0;return 0;}
2007 if (cmd==7) {
2008 /* Stop song */
2009 /* Do clean-up */
2010 return 0;
2011 }
2012 if (cmd==2) repeat_song=1;
2013 if (cmd==1 || cmd==2) {
2014 /* Start song playing */
2015 }
2016 if (cmd==4) {
2017 /* Stop song */
2018 }
2019 if (cmd==5) {
2020 /* Suspend song */
2021 }
2022 if (cmd==6) {
2023 /* Resume song */
2024 }
2025 #endif
2026 return 0;
2027 }
2028
2029 #endif /* REPLACE_BNW */