Debian Codegraph @

terps/agility/os_curses.c

raw symbols SCIP
1 /* os_curse.c -- interface routines for ncurses 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
10
11 #define RVID_BOX 1 /* Make box reverse video? */
12 #define DEBUG_KEY 0
13
14 #if 0
15 #define IMMED /* Immediate update mode? */
16 #endif
17
18 #define REFRESH_LINE_CNT 4 /* Redisplay screen every <n> lines scrolled */
19
20 /*---------------------------------------------------------------------*/
21 /* Header Files and Prototypes of terminal functions */
22 /*---------------------------------------------------------------------*/
23 #include "agility.h"
24 #include "interp.h"
25
26 #include <time.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include <signal.h>
34 #include <sys/ioctl.h>
35 #include <curses.h>
36 #include <sys/types.h>
37
38 #ifdef LINUX
39 #include <linux/kd.h> /* Needed for the font changing commands */
40 #endif
41
42
43
44
45
46 #ifdef TIOCGWINSZ /* Both of these need to be defined. */
47 #ifdef SIGWINCH
48 #define CATCH_WINDOW_CHANGE
49 #endif
50 #endif
51
52 /* 0:Black, 1:Red, 2:Green, 3:Brown, 4:Blue, 5:Magenta, 6:Cyan, 7:White */
53 /* 3n=foreground, 4n=background, semicolon seperated */
54 /* The AGT color codes are as follows: */
55 /* 0=Black, 1=Blue, 2=Green, 3=Cyan, 4=Red, 5=Magenta, 6=Brown, 7=White */
56
57 int agt_col[8]={COLOR_BLACK,COLOR_BLUE,COLOR_GREEN,COLOR_CYAN,
58 COLOR_RED,COLOR_MAGENTA,COLOR_YELLOW,COLOR_WHITE};
59 rbool color_on;
60
61
62
63 /*---------------------------------------------------------------------*/
64 /* Misc. Global variables */
65 /*---------------------------------------------------------------------*/
66
67 static rbool nav_arrow; /* NAVARROW set? */
68 static rbool lower_file; /* Lowercase resource files? */
69 static rbool font_enable;
70 static fc_type game_fc;
71 static char *viewer_name; /* Name of image viewing program */
72
73 rbool have_compass;
74 static int status_height; /* Number of lines in status line */
75
76 static int scroll_count; /* Count of how many lines we have printed since
77 last player input. */
78 volatile int curr_y, winsize_flag;
79
80 WINDOW *mainwin, *statwin;
81
82 /* This gives the first function key in the following table;
83 earlier entries are other macro keys. */
84 #define FIRST_FKEY 10
85
86 static char *fkey_text[FIRST_FKEY+64]={ /* Text for function keys */
87 "exit\n",
88 "w\n","e\n","n\n","s\n",
89 "nw\n","sw\n","se\n","ne\n",
90 "enter\n",
91 "get ", /* <== This is FIRST_FKEY */
92 "drop ",
93 "examine ","read ",
94 "open ","close ",
95 "inventory\n","look\n",
96 "score\n", "help\n",
97 "save\n","restore\n",
98 "","","","","","","","","","","","","",
99 "","","","","","","","","","","","","",
100 "","","","","","","","","","","","","",
101 "","","","","","","","","","","","",""
102 };
103
104 /* These are the macro-bindable non-function keys. */
105 static const char *keylist[FIRST_FKEY]={
106 "del",
107 "left","right","up","down",
108 "home","end","pgdn","pgup",
109 "ins"};
110
111 enum {fn_del=1010,fn_left,fn_right,fn_up,fn_down,fn_home,fn_end,fn_pgdn,
112 fn_pgup, fn_ins} fn_type;
113
114 #if 0
115 {
116 "undo\n",
117 "get ","drop ",
118 "unlock ","read ",
119 "open ","close ",
120 "inventory\n","look\n",
121 "wait\n",
122 "save\n","restore\n"};
123 #endif
124
125 /* Other candidates: view, wait (z), examine (x), again (g) */
126
127
128 /*---------------------------------------------------------------------*/
129 /* Misc. minor functions */
130 /*---------------------------------------------------------------------*/
131
132 #ifdef IMMED
133 #define updatesys() print_statline();
134 #else
135 #define updatesys() print_statline();wrefresh(mainwin);
136 #endif
137
138
139 void agt_delay(int n)
140 {
141 if (BATCH_MODE) return;
142 updatesys();
143 sleep((n+1)/2); /* In theory this should be sleep(n) */
144 }
145
146 void agt_tone(int hz,int ms)
147 /* Produce a hz-Hertz sound for ms milliseconds */
148 {
149 if (BATCH_MODE) return;
150 return;
151 }
152
153
154 int agt_rand(int a,int b)
155 /* Return random number from a to b inclusive */
156 {
157 int n;
158 n=a+(rand()>>2)%(b-a+1);
159 return n;
160 }
161
162 genfile agt_globalfile(int fid)
163 {
164 char *homedir;
165 FILE *cfgfile;
166 char *fname;
167
168 if (fid!=0) return badfile(fCFG);
169 homedir=getenv("HOME");
170 fname=rmalloc(strlen(homedir)+10);
171 sprintf(fname,"%s/.agilrc",homedir);
172 cfgfile=fopen(fname,"r");
173 return cfgfile;
174 }
175
176 void script_out(const char *s)
177 {
178 if (DEBUG_OUT)
179 fprintf(debugfile,"%s",s);
180 if (script_on) fprintf(scriptfile,"%s",s);
181 }
182
183
184
185 /*---------------------------------------------------------------------*/
186 /* Character Appearance Functions */
187 /*---------------------------------------------------------------------*/
188
189 #define STATUS_STATE 0xA007
190 #define START_STATE 0x0007
191
192 /* For state:
193 currently low 4 bits are color.
194 bit 13 (0x2000) indicates whether we are in standout mode or not
195 bit 14 (0x4000) indicates whether we are blinking or not
196 bit 15 (0x8000) indicates whether bold is on or not */
197
198 #define BOLD_BIT 0x8000
199 #define BLINK_BIT 0x4000
200 #define SL_BIT 0x2000
201
202 static int STAT_ATTR; /* Attribute settings for status line */
203
204 typedef unsigned short vstate;
205 static vstate state_stack[16];
206 int stateptr=0; /* Points into state stack */
207
208 #define cstate state_stack[stateptr] /* Current state */
209
210 volatile short *r_f,*r_b;
211
212
213 static void set_state(vstate state)
214 {
215 int c; /* Color */
216 int attr;
217
218 if (BATCH_MODE) return;
219
220 attr=A_NORMAL;
221
222 if (color_on) {
223 c=state & 0xF;
224 if (state & SL_BIT) /* Status line state */
225 c+=8;
226 if (state & BOLD_BIT) /* BOLD */
227 if (c==7) c=6; /* BOLD text will be yellow if not in Status line */
228 attr=COLOR_PAIR(c+1);
229 }
230
231 if (state & BOLD_BIT) /* BOLD */
232 attr|=A_BOLD;
233
234 if (!color_on)
235 if (state & SL_BIT)
236 attr|=A_REVERSE;
237
238 if (state & BLINK_BIT) /* Blinking */
239 attr|=A_BLINK;
240
241 wattrset(mainwin,attr);
242
243 cstate=state;
244 }
245
246 static void push_state(vstate state)
247 {
248 if (stateptr==15) fatal("State stack overflow!");
249 stateptr++;
250 set_state(state);
251 }
252
253 static void pop_state(void)
254 {
255 if (stateptr==0) fatal("State stack error: POP without PUSH!");
256 stateptr--;
257 set_state(cstate);
258 }
259
260
261
262 void agt_textcolor(int c)
263 /* Set text color to color #c, where the colors are as follows: */
264 /* 0=Black, 1=Blue, 2=Green, 3=Cyan, 4=Red, 5=Magenta, 6=Brown, */
265 /* 7=White("normal"), 8=Blinking, */
266 /* 9= *Just* White (not neccessarily "normal" and no need to turn off */
267 /* blinking) */
268 /* Also used to set other text attributes: */
269 /* -1=emphasized text, used (e.g.) for room titles */
270 /* -2=end emphasized text */
271 {
272 vstate nstate;
273
274 nstate=cstate;
275 if (c==-1)
276 nstate=nstate|0x8000;
277 else if (c==-2)
278 nstate=nstate & ~0x8000; /* BOLD off */
279 else if (c==8)
280 nstate=nstate | 0x4000; /* BLINK on */
281 else if (c==7)
282 nstate=0x0007; /* "Normal" */
283 else if (c==9)
284 nstate=(nstate & ~0xF) | 7; /* Set color to 7: white */
285 else if (c>=0 && c<7)
286 nstate=(nstate & ~0xF) | c; /* Set color to c */
287 set_state(nstate);
288 }
289
290
291
292
293 /*---------------------------------------------------------------------*/
294 /* Low-level Terminal Functions: gotoxy(), etc. */
295 /*---------------------------------------------------------------------*/
296
297 static void fix_loc(void)
298 /* This fixes the location if we are restoring from a suspended state */
299 /* Also corrects for a changed window size */
300 {
301 struct winsize winsz;
302 int new_width, new_height;
303
304 #ifdef CATCH_WINDOW_CHANGE
305 if (winsize_flag
306 && ioctl(STDOUT_FILENO,TIOCGWINSZ,&winsz)==0
307 && winsz.ws_row!=0 && winsz.ws_col!=0) {
308
309 winsize_flag=0;
310 new_width=winsz.ws_col;
311 new_height=winsz.ws_row;
312 resizeterm(new_height,new_width);
313
314 set_state(7); /* Make sure we are in normal screen state */
315 wresize(mainwin,new_height-status_height,new_width);
316 wresize(statwin,1,new_width);
317
318 screen_width=status_width=new_width;
319 screen_height=new_height-status_height;
320
321 if (curr_x>screen_width)
322 curr_x=screen_width-1;
323 if (curr_y>screen_height)
324 curr_y=screen_height-1;
325
326 wmove(mainwin,curr_y,curr_x);
327 }
328 #endif
329 }
330
331
332 #ifdef CATCH_WINDOW_CHANGE
333 void window_change(int signum)
334 {
335 signal(SIGWINCH,window_change);
336 winsize_flag=1;
337 }
338 #endif
339
340
341 void int_handler(int signum)
342 {
343 close_interface();
344 exit(0);
345 }
346
347
348
349
350 /*---------------------------------------------------------------------*/
351 /* Functions to read and translate keys */
352 /*---------------------------------------------------------------------*/
353
354 int read_a_key(void)
355 {
356 int c;
357 int i;
358
359 c=wgetch(mainwin);
360 switch(c)
361 {
362 case KEY_HOME:
363 if (nav_arrow) return fn_home; /* Otherwise fall through... */
364 case '\001': return 1006; /* Ctrl-A ==> Home */
365 case KEY_LEFT:
366 if (nav_arrow) return fn_left; /* Otherwise fall through... */
367 case '\002': return 1002; /* Ctrl-B ==> Left arrow */
368 case KEY_END:
369 if (nav_arrow) return fn_end; /* Otherwise fall through... */
370 case '\005': return 1007; /* Ctrl-E ==> End */
371 case KEY_RIGHT:
372 if (nav_arrow) return fn_right; /* Otherwise fall through... */
373 case '\006': return 1003; /* Ctrl-F ==> Right arrow */
374 case '\010': /* Fall though... */
375 case KEY_BACKSPACE:
376 case '\177': return 1000;/* Ctrl-H and <Del> ==> Backspace */
377 case KEY_DC:
378 if (nav_arrow) return fn_del; /* Otherwise fall through... */
379 return 1001; /* Delete */
380 case '\013': return 1500; /* Ctrl-K */
381 case '\014': return 1502; /* Ctrl-L */
382 case KEY_DOWN:
383 if (nav_arrow) return fn_down; /* Otherwise fall through... */
384 case '\016': return 1005; /* Ctrl-N ==> Down arrow */
385 case KEY_UP:
386 if (nav_arrow) return fn_up; /* Otherwise fall through... */
387 case '\020': return 1004; /* Ctrl-P ==> Up arrow */
388 case '\031': return 1501; /* Ctrl-Y */
389 case KEY_PPAGE:
390 if (nav_arrow) return fn_pgup; /* Otherwise fall through... */
391 return 1008; /* Page Up */
392 case KEY_NPAGE:
393 if (nav_arrow) return fn_pgdn; /* Otherwise fall through... */
394 return 1009; /* Page Down */
395 default:
396 for (i=0;i<64;i++)
397 if (c==KEY_F(i)) {
398 return 1010+FIRST_FKEY+i-1;
399 }
400 if (isprint(c) || c=='\n' || c=='\r') return c;
401 return 2000;
402 }
403 }
404
405 /*
406 1000= backspace
407 1001= delete
408 1002= left
409 1003= right
410 1004= up
411 1005= down
412 1006= home
413 1007= end
414 1008= page up
415 1009= page down
416 101n= Function key F<n>. (F0==F10)
417 1020,1021: F11,F12 (?)
418 ... 1074
419 1500= ^K
420 1501= ^Y
421 1502= ^L
422 2000= unknown control character
423 */
424
425
426
427 /*---------------------------------------------------------------------*/
428 /* Functions for the Built-In Line Editor */
429 /*---------------------------------------------------------------------*/
430
431 void agt_putc(char c)
432 {
433 fix_loc(); /* Fixup if returning from being suspended */
434 waddch(mainwin,c);
435 curr_x+=1;
436 }
437
438
439 static int base_x,base_y; /* Base X value of first character of input */
440
441 /* This assumes the cursor is always positioned on the string
442 being edited */
443 static void update_cursor(int cursor)
444 {
445 curr_x=base_x+cursor;
446 curr_y=base_y;
447 while (curr_x>=screen_width) {
448 curr_x-=screen_width;
449 curr_y++;
450 }
451 wmove(mainwin,curr_y,curr_x);
452 }
453
454 static rbool old_yheight=0;
455
456 static void update_line(int cursor,char *buff)
457 {
458 int yval, yheight, xpos, i;
459
460 yheight=(base_x+strlen(buff)+screen_width-1)/screen_width; /* Round up */
461 wmove(mainwin,screen_height-1,screen_width-1);
462 while (base_y+yheight>screen_height && base_y>0) {
463 base_y--;
464 waddch(mainwin,'\n');
465 }
466 for(yval=yheight;yval<old_yheight;yval++) {
467 wmove(mainwin,base_y+yval,0);
468 wclrtoeol(mainwin);
469 }
470 old_yheight=yheight;
471 xpos=cursor-1;
472 if (xpos<0) xpos=0;
473 update_cursor(xpos);
474 for(yval=curr_y-base_y;yval<yheight;yval++) {
475 wclrtoeol(mainwin);
476 for(i=curr_x;i<screen_width;i++) {
477 if (buff[xpos]==0) break;
478 waddch(mainwin,buff[xpos++]);
479 }
480 if (base_y+yval+1<screen_height)
481 wmove(mainwin,base_y+yval+1,0);
482 else break;
483 }
484 update_cursor(cursor);
485 }
486
487
488
489 static void redisplay_line(char *buff)
490 {
491 update_line(0,buff);
492 }
493
494 #define ENABLE_HIST
495 static char **hist=NULL;
496 static long histcount=0;
497 static long histmax=0; /* No limit on history */
498
499
500
501 static char *line_edit(int state)
502 /* state=0, input a line.
503 state=1, input character with echo
504 state=2, input character with no echo */
505 {
506 static char *yank_text=NULL;
507 rbool editmode, exitflag;
508 int key;
509 int buffleng, buffspace; /* buffspace is space allocated for buffer,
510 buffleng is space really used */
511 static int cursor; /* Where the insertion point is in buffer:
512 insert at this location */
513 char *buff, *savebuff;
514 int i, curr_hist;
515
516 buff=rmalloc(2);
517 savebuff=NULL; curr_hist=histcount;
518 buff[0]=buff[1]=0;
519 cursor=buffleng=0;buffspace=2;
520 base_x=curr_x; base_y=curr_y;
521 editmode=(state==0);
522 old_yheight=0;
523 scroll_count=0;
524
525 wmove(mainwin,curr_y,curr_x);
526 for(exitflag=0;!exitflag;) {
527 wrefresh(mainwin);
528 key=read_a_key();
529 if (DEBUG_KEY) wprintw(mainwin,"{%d}",key);
530 if (key<1000) { /* Real character entered */
531 if (state>0) { /* One key */
532 if (state==1) {
533 agt_putc(key);
534 }
535 buff[0]=key; buff[1]=0;
536 buffleng=1;
537 break;
538 }
539
540 #ifdef ENABLE_HIST
541 if (savebuff!=NULL) {
542 rfree(savebuff);
543 buff=rstrdup(buff);
544 buffspace=buffleng+1;
545 }
546 #endif
547 if (key=='\n' || key=='\r') {
548 update_cursor(buffleng);
549 break;
550 }
551 /* add character to buffer */
552 buffleng++;
553 if (buffleng+1>buffspace) buffspace+=20;
554 buff=rrealloc(buff,buffspace);
555 for(i=buffleng;i>cursor;i--)
556 buff[i]=buff[i-1];
557 buff[cursor++]=key;
558 update_line(cursor,buff);
559 }
560 else switch(key-1000)
561 { /* Special key */
562
563
564 case 0: /* Backspace */
565 if (!editmode || cursor==0) break;
566 cursor--;
567 /* Fall through... */
568
569
570 case 1: /* Delete */
571 if (!editmode || buffleng==cursor) break;
572 #ifdef ENABLE_HIST
573 if (savebuff!=NULL) {
574 rfree(savebuff);
575 buff=rstrdup(buff);
576 buffspace=buffleng+1;
577 }
578 #endif
579 for(i=cursor;i<buffleng;i++)
580 buff[i]=buff[i+1];
581 buffleng--;
582 update_line(cursor,buff);
583 break;
584
585
586 case 2: /* Left arrow */
587 if (editmode && cursor>0)
588 update_cursor(--cursor);
589 break;
590
591
592 case 3: /* Right arrow */
593 if (editmode && cursor<buffleng)
594 update_cursor(++cursor);
595 break;
596
597
598 #ifdef ENABLE_HIST
599 case 4: /* Up arrow: Command history */
600 if (!editmode || histcount==0) break;
601 if (savebuff==NULL) { /* Save current line */
602 savebuff=buff;
603 curr_hist=histcount;
604 }
605 curr_hist--;
606 if (curr_hist<0) {
607 curr_hist=0;
608 break;
609 }
610 buff=hist[curr_hist];
611 buffleng=strlen(buff);
612 cursor=0;
613 redisplay_line(buff);
614 break;
615
616 case 5: /* Down arrow: Command history */
617 if (!editmode || savebuff==NULL) break;
618 curr_hist++;
619 if (curr_hist>=histcount) {
620 /* Reached bottom: restore original line */
621 curr_hist=histcount;
622 buff=savebuff;
623 savebuff=NULL;
624 } else
625 buff=hist[curr_hist];
626 buffleng=strlen(buff);
627 cursor=0;
628 redisplay_line(buff);
629 break;
630 #endif
631
632 case 6: /* Home */
633 if (!editmode) break;
634 cursor=0;
635 update_cursor(cursor);
636 break;
637
638 case 7: /* End */
639 if (!editmode) break;
640 cursor=buffleng;
641 update_cursor(cursor);
642 break;
643
644 case 8: /* Page up : Scroll back */
645 case 9: /* Page down */
646 break;
647
648
649 case 500: /* Ctrl-K: Delete to EOL */
650 if (!editmode) break;
651 #ifdef ENABLE_HIST
652 if (savebuff!=NULL) {
653 rfree(savebuff);
654 buff=rstrdup(buff);
655 buffspace=buffleng+1;
656 }
657 #endif
658 rfree(yank_text);
659 yank_text=rstrdup(buff+cursor);
660 buffleng=cursor;
661 buff[buffleng]=0;
662 update_line(cursor,buff);
663 break;
664
665
666 case 501: /* Ctrl-Y: Yank */
667 if (!editmode) break;
668 {
669 int txtleng;
670 #ifdef ENABLE_HIST
671 if (savebuff!=NULL) {
672 rfree(savebuff);
673 buff=rstrdup(buff);
674 buffspace=buffleng+1;
675 }
676 #endif
677 txtleng=strlen(yank_text);
678 buffleng+=txtleng;
679 while(buffleng+1>buffspace) buffspace+=20;
680 buff=rrealloc(buff,buffspace);
681 for(i=buffleng;i>=cursor+txtleng;i--)
682 buff[i]=buff[i-txtleng];
683 for(i=0;i<txtleng;i++)
684 buff[cursor+i]=yank_text[i];
685 update_line(cursor,buff);
686 cursor+=txtleng;
687 update_cursor(cursor);
688 }
689 break;
690
691
692 case 502: /* Ctrl-L: Redraw screen */
693 redrawwin(mainwin);
694 redrawwin(statwin);
695 if (editmode)
696 redisplay_line(buff);
697 break;
698
699 /* Function key <key-1010> */
700 default:
701 if (key-1010<0 || key-1010>=FIRST_FKEY+64) break;
702 if (!editmode) break;
703 {
704 int txtleng;
705 #ifdef ENABLE_HIST
706 if (savebuff!=NULL) {
707 rfree(savebuff);
708 buff=rstrdup(buff);
709 buffspace=buffleng+1;
710 }
711 #endif
712 txtleng=strlen(fkey_text[key-1010]);
713 if (fkey_text[key-1010][txtleng-1]=='\n') {
714 /* Newlines should only appear at the end; they indicate
715 that this function key should cause the line to be entered. */
716 txtleng--;
717 exitflag=1; /* Finish entering this line */
718 }
719 buffleng+=txtleng;
720 while(buffleng+1>buffspace) buffspace+=20;
721 buff=rrealloc(buff,buffspace);
722 for(i=buffleng;i>=cursor+txtleng;i--)
723 buff[i]=buff[i-txtleng];
724 for(i=0;i<txtleng;i++)
725 buff[cursor+i]=fkey_text[key-1010][i];
726 update_line(cursor,buff);
727 cursor+=txtleng;
728 update_cursor(cursor);
729 }
730 break;
731 }
732 if (exitflag) break;
733 }
734 buff=rrealloc(buff,(buffleng+1));
735 if (state==0) {
736 if (histmax==0 || histcount<histmax)
737 hist=rrealloc(hist,(++histcount)*sizeof(char*));
738 else
739 for(i=0;i<histcount-1;i++)
740 hist[i]=hist[i+1];
741 hist[histcount-1]=rstrdup(buff);
742 }
743 return buff;
744 }
745
746
747
748
749 /*---------------------------------------------------------------------*/
750 /* High Level Input Functions */
751 /*---------------------------------------------------------------------*/
752
753 char *agt_input(int in_type)
754 /* read a line from the keyboard, allocating space for it using malloc */
755 /* See os_none.c for a discussion of in_type; we ignore it. */
756 {
757 char *s;
758
759 updatesys();
760 fix_loc();
761
762 s=line_edit(0);
763
764 #ifdef TEST_NONL /* This is for debugging */
765 {
766 char *t;
767 for(t=s+strlen(s)-1;(t>s) && (*t=='\n');t--);
768 if (*t!=0 || t<s) t++;
769 *t=0;
770 }
771 #endif
772
773 script_out(s);
774 agt_newline();
775 scroll_count=0;
776 return s;
777 }
778
779
780
781
782 char agt_getkey(rbool echo_char)
783 /* Reads a character and returns it */
784 /* If echo_char=1, echo character. If 0, don't */
785 {
786 char c;
787 char *s;
788
789 updatesys();
790 scroll_count=0;
791
792 s=line_edit(echo_char?1:2);
793 c=s[0];
794 if (echo_char==1) {
795 script_out(s);
796 agt_newline();
797 }
798 rfree(s);
799
800 scroll_count=0;
801 return c;
802 }
803
804
805
806
807 /*---------------------------------------------------------------------*/
808 /* Printing the Status Line */
809 /*---------------------------------------------------------------------*/
810
811 static void print_compass(void)
812 {
813 int cwidth, i;
814
815 if (status_width< 9+4*12) return;
816
817 waddch(statwin,'\n');
818 waddstr(statwin," EXITS: ");cwidth=9;
819 for(i=0;i<12;i++)
820 if (compass_rose & (1<<i)) {
821 wprintw(statwin,"%s ",exitname[i]);
822 cwidth+=strlen(exitname[i])+1;
823 }
824 for(i=cwidth;i<status_width;i++) waddch(statwin,' ');
825 }
826
827 void agt_statline(const char *s)
828 /* Output a string in reverse video (or whatever) at top of screen */
829 {
830 if (BATCH_MODE) return;
831 waddstr(statwin,s);
832 if (have_compass)
833 print_compass();
834 wrefresh(statwin);
835 }
836
837
838
839 /*---------------------------------------------------------------------*/
840 /* High Level Output Functions */
841 /*---------------------------------------------------------------------*/
842
843 void agt_more(void)
844 {
845 scroll_count=0;
846 if (BATCH_MODE) return;
847 waddstr(mainwin," --MORE--");
848 agt_getkey(0);
849 wmove(mainwin,curr_y,0);
850 wclrtoeol(mainwin);
851 }
852
853
854 void agt_newline(void)
855 {
856 static int refresh_count=0; /* Used for "occasional" refresh */
857
858 script_out("\n");
859
860 if (BATCH_MODE) {
861 curr_x=0;
862 curr_y++;
863 if (curr_y>=screen_height)
864 curr_y=screen_height-1; /* Scrolling has occured */
865 return;
866 }
867
868 if (curr_x<screen_width) /* Make sure we haven't already newlined. */
869 waddch(mainwin,'\n');
870 getyx(mainwin,curr_y,curr_x);
871 scroll_count++;
872 refresh_count++;
873
874 if (scroll_count>=screen_height-1)
875 agt_more(); /* Print out MORE message */
876 else {
877 #ifndef IMMED
878 refresh_count=refresh_count%REFRESH_LINE_CNT;
879 if (refresh_count==0) wrefresh(mainwin);
880 #endif
881 }
882 fix_loc(); /* Fixup if returning from being suspended */
883 }
884
885
886 void agt_puts(const char *s)
887 /* This routine assumes that s is not wider than the screen */
888 {
889 script_out(s);
890
891 if (BATCH_MODE) return;
892
893 fix_loc(); /* Fixup if returning from being suspended */
894 curr_x+=strlen(s);
895 waddstr(mainwin,s);
896 wmove(mainwin,curr_y,curr_x);
897 }
898
899
900 void agt_clrscr(void)
901 {
902 winsize_flag=0;
903 scroll_count=0;
904 curr_x=0;curr_y=0;
905 script_out("\n\n\n\n\n");
906
907 if (BATCH_MODE) return;
908
909 werase(mainwin);
910 wmove(mainwin,0,0);
911 }
912
913
914
915
916 /*---------------------------------------------------------------------*/
917 /* Box Printing Routines */
918 /*---------------------------------------------------------------------*/
919
920 static unsigned long boxflags;
921 static int box_startx; /* Starting x of box */
922 static int box_width;
923 static int delta_scroll; /* Amount we are adding to vertical scroll
924 with the box */
925
926 #define bhline() waddch(mainwin,ACS_HLINE)
927 #define bvline() waddch(mainwin,ACS_VLINE)
928
929 static void boxrule(void)
930 /* Draw line at top or bottom of box */
931 {
932 int i;
933
934 if (RVID_BOX) agt_puts(" ");
935 else {waddch(mainwin,ACS_ULCORNER);bhline();}
936
937 for(i=0;i<box_width;i++)
938 if (RVID_BOX) agt_puts(" ");
939 else bhline();
940
941 if (RVID_BOX) agt_puts(" ");
942 else {bhline();waddch(mainwin,ACS_URCORNER);}
943 }
944
945 static void boxpos(void)
946 {
947 int i;
948
949 curr_x=box_startx; curr_y++;
950 if (curr_y>=screen_height) curr_y=screen_height-1;
951 wmove(mainwin,curr_y,curr_x);
952
953 script_out("\n");
954 for(i=0;i<box_startx;i++)
955 script_out(" ");
956 }
957
958 void agt_makebox(int width,int height,unsigned long flags)
959 /* Flags: TB_TTL, TB_BORDER, TB_NOCENT */
960 {
961 int i;
962
963 boxflags=flags;
964 box_width=width;
965 if (boxflags&TB_BORDER) { /* Add space for border */
966 width+=4;
967 height+=2;
968 }
969 if (boxflags&TB_NOCENT) box_startx=0;
970 else box_startx=(screen_width-width)/2; /* Center the box horizontally */
971 if (box_startx<0) box_startx=0;
972
973 /* Now we need to compute the vertical position of the box */
974 if (flags & TB_TTL) { /* Title: centered horizontally */
975 curr_y=(screen_height-height)/2;
976 if (curr_y+height+8>=screen_height) /* Make room for credits */
977 curr_y=screen_height-height-8-1;
978 if (curr_y<0) curr_y=0;
979 delta_scroll=0;
980 scroll_count=0;
981 } else {
982 /* Compute vertical position of non-title box */
983 curr_y=curr_y-height;
984 if (curr_y<0) {
985 delta_scroll=1-curr_y;
986 curr_y=0;
987 } else delta_scroll=0;
988 }
989
990 curr_x=box_startx;
991 wmove(mainwin,curr_y,curr_x);
992
993 script_out("\n");
994 for(i=0;i<box_startx;i++)
995 script_out(" ");
996
997 if (boxflags&TB_BORDER) {
998 push_state(cstate | 0x2000); /* Turn on reverse video */
999 boxrule();
1000 boxpos();
1001 if (RVID_BOX) agt_puts(" ");
1002 else {bvline();agt_puts(" ");}
1003 }
1004 }
1005
1006 void agt_qnewline(void)
1007 {
1008 if (boxflags&TB_BORDER) {
1009 if (RVID_BOX) agt_puts(" ");
1010 else {agt_puts(" ");bvline();}
1011 }
1012 boxpos();
1013 if (boxflags&TB_BORDER) {
1014 if (RVID_BOX) agt_puts(" ");
1015 else {bvline();agt_puts(" ");}
1016 }
1017 }
1018
1019 void agt_endbox(void)
1020 {
1021 if (boxflags&TB_BORDER) {
1022 if (RVID_BOX) agt_puts(" ");
1023 else {agt_puts(" ");bvline();}
1024 boxpos();
1025 boxrule();
1026 if (RVID_BOX) pop_state();
1027 }
1028 scroll_count+=delta_scroll;
1029 agt_newline(); /* NOT agt_qnewline() */
1030 if ( (boxflags&TB_TTL) ) {
1031 agt_puts(" "); /* Neccessary to get things to work right w/xterm */
1032 while(curr_y<screen_height-8) agt_newline();
1033 }
1034 }
1035
1036
1037
1038
1039
1040 /*---------------------------------------------------------------------*/
1041 /* Option Parsing Routines */
1042 /*---------------------------------------------------------------------*/
1043
1044
1045
1046 /* Duplicate s and add '/' if neccessary */
1047 char *makepathentry(char *s, int leng)
1048 {
1049 char *p;
1050 rbool addslash;
1051
1052 addslash=(s[leng-1]!='/');
1053 p=rmalloc(leng+addslash+1);
1054 strncpy(p,s,leng);
1055 if (addslash)
1056 p[leng++]='/';
1057 p[leng]=0; /* Mark new end of string */
1058 return p;
1059 }
1060
1061 static void free_gamepath(void)
1062 {
1063 char **p;
1064
1065 if (gamepath==NULL) return;
1066
1067 /* We skip the first element which always points to "" */
1068 for(p=gamepath+1;*p!=NULL;p++)
1069 rfree(*p);
1070 rfree(gamepath);
1071 }
1072
1073
1074 static void get_agt_path(void)
1075 {
1076 char *path, *end;
1077 int pathleng; /* Number of elements in gamepath */
1078
1079 free_gamepath();
1080 pathleng=0;
1081 path=getenv("AGT_PATH");
1082 if (path==NULL) return;
1083 while(*path!=0) { /* Find each componenent of the path */
1084 while(isspace(*path) || *path==':') path++; /* Skip excess whitespace */
1085 for(end=path;*end!=0 && !isspace(*end) && *end!=':';end++);
1086 if (end!=path) { /* There's actually something there */
1087 pathleng++;
1088 gamepath=rrealloc(gamepath,(pathleng+1)*sizeof(char*));
1089 gamepath[pathleng-1]=makepathentry(path,end-path);
1090 gamepath[pathleng]=NULL;
1091 }
1092 path=end;
1093 }
1094 }
1095
1096
1097 void set_fkey(char *keyname, char *words[], int numwords)
1098 {
1099 int n,i,leng;
1100 char *s;
1101 rbool err;
1102 static long keys_defined=0;
1103
1104 err=0; n=0;
1105 if (tolower(keyname[0])!='f') {
1106 err=1;
1107 for(i=0;i<10;i++)
1108 if (0==strcasecmp(keylist[i],keyname)) {
1109 err=0;
1110 n=i;
1111 }
1112 } else { /* Name of form 'Fnn' */
1113 n=strtol(keyname+1,&s,0); /* s will point to the first erroneous char */
1114 if (keyname[1]==0 || *s!=0) err=1;
1115 if (n>64) err=1;
1116 n+=FIRST_FKEY;
1117 }
1118 if (err) {
1119 writeln("Unrecognized KEY name.");
1120 return;
1121 }
1122
1123 leng=0;
1124 for(i=0;i<numwords;i++)
1125 leng+=strlen(words[i]);
1126 s=rmalloc(leng+numwords+1);
1127 s[0]=0;
1128
1129 /* This isn't very efficient, but it doesn't need to be. */
1130 for(i=0;i<numwords;i++) {
1131 strcat(s,words[i]);
1132 strcat(s," ");
1133 }
1134 i=strlen(s)-2;
1135 if (i>=0 && s[i]=='+') {
1136 s[i]='\n';
1137 s[i+1]=0;
1138 }
1139 if (keys_defined & (1<<n)) rfree(fkey_text[n]);
1140 fkey_text[n]=s;
1141 keys_defined|=(1<<n);
1142 }
1143
1144
1145
1146 #define opt(s) (!strcasecmp(optstr[0],s))
1147
1148 rbool agt_option(int optnum,char *optstr[],rbool setflag)
1149 /* If setflag is 0, then the option was prefixed with NO_ */
1150 {
1151 int i;
1152
1153
1154 if (opt("COLORTERM") && optnum>=2 && setflag) {
1155 return 1; /* Curses version ignores this. */
1156 }
1157 #ifdef LINUX
1158 if (opt("FONT")) {
1159 font_enable=setflag;
1160 return 1;
1161 }
1162 #endif
1163 if (opt("COMPASS")) {
1164 have_compass=setflag;
1165 status_height=1+setflag;
1166 return 1;
1167 }
1168 if (opt("KEY") && optnum>=2) {
1169 set_fkey(optstr[1],optstr+2,optnum-2);
1170 return 1;
1171 }
1172 if (opt("HISTORY")) {
1173 histmax=strtol(optstr[1],NULL,10);
1174 if (histmax<0) histmax=0;
1175 return 1;
1176 }
1177 if (opt("LOWER_FILE")) {
1178 lower_file=setflag;
1179 return 1;
1180 }
1181 if (opt("NAVARROW")) {
1182 nav_arrow=setflag;
1183 return 1;
1184 }
1185 if (opt("VIEWER")) {
1186 viewer_name=rstrdup(optstr[1]);
1187 return 1;
1188 }
1189 if (opt("PATH") && optnum>=2) {
1190 if (gamepath!=NULL) return 1; /* First setting wins */
1191 gamepath=rmalloc((optnum+1)*sizeof(char*)); /* optnum-1+2 */
1192 gamepath[0]=""; /* ==> The current directory */
1193 for(i=1;i<optnum;i++) /* Starting at the 2nd entry of both */
1194 gamepath[i]=makepathentry(optstr[i],strlen(optstr[i]));
1195 gamepath[optnum]=NULL;
1196 return 1;
1197 }
1198 return 0;
1199 }
1200
1201 #undef opt
1202
1203
1204
1205
1206 /*---------------------------------------------------------------------*/
1207 /* Interface Initializations */
1208 /*---------------------------------------------------------------------*/
1209
1210 void init_interface(int argc,char *argv[])
1211 {
1212 struct stat fileinfo;
1213 ino_t inode;
1214 dev_t devnum;
1215
1216 viewer_name=NULL;
1217 nav_arrow=0;
1218 have_compass=0; status_height=1;
1219 script_on=0;scriptfile=NULL;
1220 center_on=0;
1221 par_fill_on=0;
1222 scroll_count=0;
1223
1224 font_enable=0;
1225
1226 get_agt_path();
1227
1228 if (BATCH_MODE) {
1229 screen_height=25;
1230 screen_width=status_width=80;
1231 return;
1232 }
1233
1234 /* DEBUG_OUT should be true if and only if debugfile and stdin
1235 refer to different devices */
1236 fstat(STDERR_FILENO,&fileinfo);
1237 inode=fileinfo.st_ino;
1238 devnum=fileinfo.st_dev;
1239 fstat(STDIN_FILENO,&fileinfo);
1240 DEBUG_OUT=(inode!=fileinfo.st_ino || devnum!=fileinfo.st_dev);
1241 debugfile=stderr;
1242
1243 /* DEBUG_OUT=!isatty(STDERR_FILENO); */
1244 setvbuf(stdout,NULL,_IONBF,BUFSIZ);
1245
1246 #ifdef LINUX
1247 /* Switch to PC character set */
1248 if (!fix_ascii_flag) printf("\033(U");
1249 #endif
1250
1251 initscr();
1252 intrflush(stdscr,TRUE);
1253 keypad(stdscr,TRUE);
1254 nonl();
1255 cbreak();
1256 noecho();
1257
1258 #ifndef NEXT /* ?!?! */
1259 if (atexit(close_interface)!=0)
1260 {printf("INTERNAL ERROR: Unable to register restore_terminal.\n");
1261 exit(0);}
1262 #endif
1263
1264 screen_height=LINES;
1265 screen_width=COLS;
1266 #ifdef CATCH_WINDOW_CHANGE
1267 signal(SIGWINCH,window_change);
1268 #endif
1269
1270 immedok(stdscr,TRUE);
1271 scrollok(stdscr,TRUE);
1272 mainwin=stdscr; /* Just temporarily */
1273
1274 /* Really need to check control_width here */
1275 status_width=screen_width;
1276
1277 signal(SIGINT,int_handler);
1278 signal(SIGPIPE,SIG_IGN);
1279 }
1280
1281 #ifdef REPLACE_BNW
1282 static void save_old_font(void);
1283 static void restore_font(void);
1284 #endif
1285
1286
1287
1288 void start_interface(fc_type fc)
1289 {
1290 int bkgd_color;
1291
1292 if (stable_random)
1293 srand(6);
1294 else
1295 srand(time(0));
1296 game_fc=fc;
1297 free_gamepath(); /* It's not needed anymore */
1298
1299 if (BATCH_MODE) {
1300 agt_clrscr();
1301 return;
1302 }
1303
1304 if (has_colors())
1305 {
1306 int i;
1307
1308 bkgd_color=COLOR_BLACK;
1309 start_color();
1310 for(i=0;i<8;i++) {/* init_pair calls to set up colors */
1311 init_pair(i+1,agt_col[i],bkgd_color);
1312 init_pair(i+9,agt_col[i],COLOR_BLUE);
1313 }
1314 color_on=1;
1315 STAT_ATTR=COLOR_PAIR(16)|A_BOLD; /* White on blue */
1316 }
1317 else {
1318 color_on=0;
1319 STAT_ATTR=A_REVERSE;
1320 }
1321
1322
1323
1324
1325 clear();refresh(); /* Clean off the screen */
1326
1327 screen_height=LINES-status_height;
1328 screen_width=COLS;
1329
1330 /* Main window: everything but the top line */
1331 mainwin=newwin(0,0,status_height,0);
1332 idlok(mainwin,TRUE);
1333 scrollok(mainwin,TRUE);
1334 keypad(mainwin,TRUE);
1335 #ifdef IMMED
1336 immedok(mainwin,TRUE); /* Not sure about this. */
1337 #endif
1338
1339 /* Status line window */
1340 statwin=newwin(status_height,0,0,0);
1341 idlok(statwin,TRUE);
1342 wattrset(statwin,STAT_ATTR);
1343 keypad(mainwin,TRUE);
1344
1345 set_state(START_STATE);
1346 agt_clrscr();
1347 }
1348
1349
1350
1351 void close_interface(void)
1352 {
1353 if (scriptfile!=NULL)
1354 close_pfile(scriptfile,0);
1355 if (BATCH_MODE) return;
1356 rfree(viewer_name);
1357 set_state(START_STATE);
1358 endwin();
1359 }
1360
1361
1362 #ifdef REPLACE_BNW
1363
1364
1365 /* This cheats a bit, using "hidden" data from filename.c */
1366
1367 static FILE *linopen(char *name, char *ext)
1368 {
1369 FILE *f;
1370 char *fname;
1371
1372 fname=assemble_filename(hold_fc->path,name,ext);
1373 f=fopen(fname,"rb");
1374 rfree(fname);
1375 return f;
1376 }
1377
1378
1379
1380 /*------------------------------------------------------------------*/
1381 /* FONT Routines */
1382 /*------------------------------------------------------------------*/
1383
1384 static char *save_font;
1385 static int save_font_height;
1386
1387 static int set_font(char *fontdef, int font_height)
1388 {
1389 #ifdef PIO_FONTX
1390 struct consolefontdesc fontdesc;
1391
1392 fontdesc.charcount=256;
1393 fontdesc.charheight=font_height;
1394 fontdesc.chardata=fontdef;
1395
1396 return ioctl(STDOUT_FILENO,PIO_FONTX,&fontdesc);
1397 #else
1398 #ifdef PIO_FONT
1399 return ioctl(STDOUT_FILENO,PIO_FONT,fontdef);
1400 #endif
1401 #endif
1402 }
1403
1404
1405 static int getfont(char *fontdef, int *font_height)
1406 {
1407 int rval=-1;
1408
1409 #ifdef GIO_FONTX
1410 struct consolefontdesc fontdesc;
1411
1412 fontdesc.chardata=fontdef;
1413 rval=ioctl(STDOUT_FILENO,GIO_FONTX,&fontdesc);
1414 *font_height=fontdesc.charheight;
1415 #else
1416 #ifdef GIO_FONT
1417 rval=ioctl(STDOUT_FILENO,GIO_FONT,save_font); /* Get current font */
1418 *font_height=16;
1419 #endif
1420 #endif
1421 return rval;
1422 }
1423
1424
1425 static void save_old_font(void)
1426 {
1427 if (BATCH_MODE) return;
1428
1429 /* Save existing font */
1430 save_font=rmalloc(256*32);
1431 if (getfont(save_font,&save_font_height))
1432 /* Failed... issue warning? */
1433 rfree(save_font);
1434 }
1435
1436
1437 static void restore_font(void)
1438 {
1439 if (BATCH_MODE) return;
1440 if (save_font==NULL || set_font(save_font,save_font_height)) {
1441 /* Couldn't restore save_font */
1442 #ifdef PIO_FONTRESET
1443 ioctl(STDOUT_FILENO,PIO_FONTRESET);
1444 #endif
1445 }
1446 }
1447
1448
1449
1450 void fontcmd(int cmd,int font)
1451 /* 0=Load font, name is fontlist[font]
1452 1=Restore original font
1453 2=Set startup font. (<gamename>.FNT)
1454 */
1455 {
1456 FILE *fontfile;
1457 char *buff, *fontname, *s;
1458 int count, height;
1459
1460 if (!font_enable) return;
1461 /* (This should be off by default since it changes the fonts
1462 on *all* of the VC's) */
1463
1464 if (cmd==2)
1465 save_old_font();
1466
1467 if (cmd==0 || cmd==2) {
1468 if (cmd==0) {
1469 fontname=fontlist[font];
1470 if (lower_file)
1471 for(s=fontname;*s!=0;s++) *s=tolower(*s);
1472 /* Lowercase the font names as we use them */
1473 }
1474 else fontname=game_fc->gamename;
1475
1476 fontfile=linopen(fontname,".fnt");
1477 if (fontfile==NULL) return;
1478 buff=rmalloc(256*32);
1479 memset(buff,0,256*32);
1480 fseek(fontfile,0,SEEK_END);
1481 height=ftell(fontfile);
1482 if (height%256!=0) {
1483 /* Error message */
1484 return;
1485 }
1486 height=height/256;
1487
1488 fseek(fontfile,0,SEEK_SET);
1489 for(count=0;count<256;count++)
1490 if (fread(buff+count*32,height,1,fontfile)!=1) break;
1491 if (count!=256) {
1492 fclose(fontfile);
1493 rfree(buff);
1494 return;
1495 }
1496 fclose(fontfile);
1497 if (!BATCH_MODE)
1498 if (set_font(buff,height)) {
1499 /* Print error message */
1500 }
1501 rfree(buff);
1502 return;
1503 } else if (cmd==1)
1504 restore_font();
1505 }
1506
1507
1508
1509 /*------------------------------------------------------------------*/
1510 /* Graphics Routines */
1511 /*------------------------------------------------------------------*/
1512
1513 #define GFX_EXT_CNT 17
1514 /* The extension indicates the video mode the picture was intended
1515 to be viewed in. */
1516 static char *gfxext[GFX_EXT_CNT]={".pcx",
1517 ".p06", /* 640x200x2 */
1518 ".p40",".p41",".p42",".p43", /* 320x200x4 */
1519 ".p13", /* 320x200x16 */
1520 ".p19", /* 320x200x256 */
1521 ".p14",".p16", /* 640x200x16, 640x350x16 */
1522 ".p18", /* 640x480x16 */
1523 ".gif",".png",".bmp",".jpg",
1524 ".fli",".flc"};
1525
1526
1527 void pictcmd(int cmd,int pict)
1528 /* 1=show global picture, name is pictlist[pict]
1529 2=show room picture, name is pixlist[pict]
1530 3=show startup picture <gamename>.P..
1531 */
1532 {
1533 char *base, *cmdstr;
1534 int gmode;
1535 FILE *pcxfile;
1536
1537 if (viewer_name==NULL) return;
1538
1539 if (cmd==1) base=pictlist[pict];
1540 else if (cmd==2) base=pixlist[pict];
1541 else if (cmd==3) base=game_fc->gamename;
1542 else return;
1543
1544 /* Find graphics file; determine mode from extension... */
1545 for(gmode=GFX_EXT_CNT-1;gmode>=0;gmode--) {
1546 pcxfile=linopen(base,gfxext[gmode]);
1547 if (pcxfile!=NULL) break;
1548 }
1549 if (pcxfile==NULL) return;
1550 fclose(pcxfile);
1551
1552 base=assemble_filename(hold_fc->path,base,gfxext[gmode]);
1553
1554 cmdstr=rmalloc(strlen(base)+strlen(viewer_name)+40);
1555 sprintf(cmdstr,"%s %s >/dev/null",viewer_name,base);
1556
1557 if (!BATCH_MODE) {
1558 endwin();
1559 system(cmdstr); /* Don't bother to check return status since
1560 there's nothing we could do anyhow */
1561 wrefresh(mainwin);
1562 wrefresh(statwin);
1563 }
1564
1565 rfree(cmdstr);
1566 rfree(base);
1567
1568 return;
1569 }
1570
1571
1572
1573 /*------------------------------------------------------------------*/
1574 /* Sound Routines */
1575 /*------------------------------------------------------------------*/
1576
1577 #if 0
1578 static rbool repeat_song=0;
1579 static rbool suspend_song=0;
1580 #endif
1581
1582 int musiccmd(int cmd,int song)
1583 /* For cmd=1 or 2, the name of the song is songlist[song]
1584 The other commands don't take an additional argument.
1585 1=play song
1586 2=repeat song
1587 3=end repeat
1588 4=end song
1589 5=suspend song
1590 6=resume song
1591 7=clean-up
1592 8=turn sound on
1593 9=turn sound off
1594 -1=Is a song playing? (0=false, -1=true)
1595 -2=Is the sound on? (0=false, -1=true)
1596 */
1597 {
1598 if (cmd==8) {
1599 sound_on=1;
1600 return 0;
1601 }
1602 if (cmd==9) {sound_on=0;return 0;}
1603 if (cmd==-2) return sound_on;
1604 #if 0
1605 if (cmd==3) {repeat_song=0;return 0;}
1606 if (cmd==7) {
1607 /* Stop song */
1608 /* Do clean-up */
1609 return 0;
1610 }
1611 if (cmd==2) repeat_song=1;
1612 if (cmd==1 || cmd==2) {
1613 /* Start song playing */
1614 }
1615 if (cmd==4) {
1616 /* Stop song */
1617 }
1618 if (cmd==5) {
1619 /* Suspend song */
1620 }
1621 if (cmd==6) {
1622 /* Resume song */
1623 }
1624 #endif
1625 return 0;
1626 }
1627
1628 #endif /* REPLACE_BNW */
1629
1630
1631
1632 /*------------------------------------------------------------------*/
1633 /* File Selection Menu */
1634 /*------------------------------------------------------------------*/
1635
1636 #ifdef REPLACE_GETFILE
1637
1638 void list_files(char *type, char *ext)
1639 {
1640 DIR *currdir;
1641 struct dirent *entry;
1642 ITEM **filelist;
1643 int filecnt, listsize;
1644 int i,j;
1645
1646 filelist=NULL; filecnt=listsize=0; maxleng=0;
1647 currdir=opendir(".");
1648 if (currdir==NULL) return; /* Nothing we can do except give up */
1649 do {
1650 entry=readdir(currdir);
1651 if ( entry!=NULL && check_fname(entry->d_name,ext) ) {
1652 /* It has the right extension; add it to our list of files */
1653 if (filecnt>=listsize) {
1654 listsize+=5;
1655 filelist=rrealloc(filelist,listsize*sizeof(ITEM*));
1656 }
1657 filelist[filecnt]=new_item(entry->d_name,"");
1658 filecnt++;
1659 }
1660 } while (entry!=NULL);
1661 closedir(currdir);
1662 if (filecnt==0) return; /* No files */
1663 }
1664
1665 writestr("Existing ");writestr(type);writestr("files:");
1666
1667
1668 rfree(filelist);
1669 }
1670
1671
1672
1673
1674 /* This opens the file refered to by fname and returns it */
1675 static FILE *uf_open(char *fname, char *ext, char *otype)
1676 {
1677 if (otype[0]=='w') { /* Check to see if we are overwriting... */
1678 FILE *fd;
1679
1680 fd=fopen(fname,"r");
1681 if (fd!=NULL) { /* File already exists */
1682 fclose(fd);
1683 if (!yesno("This file already exists; overwrite?"))
1684 /* That is, DON'T overwrite */
1685 return NULL;
1686 }
1687 }
1688 return fopen(fname,otype);
1689 }
1690
1691
1692
1693 static char *last_save=NULL;
1694 static char *last_log=NULL;
1695 static char *last_script=NULL;
1696
1697 FILE *get_user_file(int ft)
1698 /* ft= 0:script, 1:save 2:restore, 3:log(read) 4:log(write) */
1699 /* Should return file in open state, ready to be read or written to,
1700 as the case may be */
1701 {
1702 /* int extlen;*/
1703 char *fname,*otype, *ext;
1704 char *defname; /* Default file name */
1705 char *p,*q;
1706 FILE *fd;
1707 char *ftype;
1708
1709 switch (ft) {
1710 case 0: ftype="script ";
1711 defname=last_script;
1712 otype="a";ext=pSCR; break;
1713 case 1: ftype="save ";
1714 defname=last_save;
1715 otype="wb";ext=pSAV;break;
1716 case 2: ftype="restore ";
1717 defname=last_save;
1718 otype="rb";ext=pSAV;break;
1719 case 3: ftype="log ";
1720 defname=last_log;
1721 otype="r";ext=pLOG;break;
1722 case 4: ftype="log ";
1723 defname=last_log;
1724 otype="w";ext=pLOG;break;
1725 default: writeln("<INTERNAL ERROR: invalid file type>");
1726 return NULL;
1727 }
1728 if (otype[0]=='r') { /* List available files. */
1729 list_files(ftype,ext);
1730 ftype=NULL;
1731 } else
1732
1733 writestr("Enter ");
1734 if (ftype!=NULL) writestr(ftype);
1735 writestr("file name");
1736 if (defname!=NULL) {
1737 writestr(" (");
1738 writestr(defname);
1739 writestr(")");
1740 }
1741 writestr(": ");
1742
1743 if (PURE_INPUT) agt_textcolor(-1);
1744 fname=agt_input(4);
1745 if (PURE_INPUT) agt_textcolor(-2);
1746
1747 /* Delete whitespace before and after the file name. */
1748 for(p=fname;isspace(*p);p++);
1749 for(q=fname;*p!=0;p++,q++)
1750 *q=*p;
1751 q--;
1752 while(isspace(*q)) q--;
1753 q++;
1754 *q=0;
1755
1756 if (q==fname) /* ie we are left with the empty string */
1757 if (defname==NULL) {
1758 writeln("Never mind.");
1759 rfree(fname);
1760 return NULL;}
1761 else {
1762 rfree(fname);
1763 fname=defname;
1764 }
1765
1766 #ifdef UNIX
1767 if ( (ft==0 || ft==4) && *fname=='|') { /* pipe => program */
1768 fd=popen(fname+1,"w");
1769 if (fd!=NULL) ispipe[ ft==0 ? 0 : 2 ]=1;
1770 } else if (ft==3 && *(q-1)=='|') { /* pipe <= program */
1771 *(q-1)=0;
1772 fd=popen(fname,"r");
1773 if (fd!=NULL) ispipe[1]=1;
1774 *(q-1)='|'; /* For next time */
1775 } else
1776 #endif
1777 {
1778 /* We've gotten a new file name; need to add default extension */
1779 if (fname!=defname && strlen(ext)>0 && !check_fname(fname,ext))
1780 fname=remake_fname(fname,ext);
1781
1782 fd=uf_open(fname,ext,otype);
1783 }
1784
1785 if (fd==NULL) {
1786 if (fname!=defname) rfree(fname);
1787 return NULL;
1788 }
1789
1790 switch(ft)
1791 {
1792 case 0: last_script=fname;break;
1793 case 1: last_save=fname; break;
1794 case 2: last_save=fname; break;
1795 case 3: last_log=fname; break;
1796 case 4: last_log=fname; break;
1797 }
1798 if (fname!=defname) rfree(defname);
1799 return fd;
1800 }
1801
1802
1803 void set_default_filenames(const char *gamename)
1804 {
1805 fix_filename=2; /* Remove path: by default, all save files go in
1806 the *current* directory, not the game's directory */
1807 last_save=make_fname(gamename,pSAV);
1808 last_log=make_fname(gamename,pLOG);
1809 last_script=make_fname(gamename,pSCR);
1810 fix_filename=0;
1811 }
1812
1813 #endif /* REPLACE_GETFILE */
1814
1815
1816
1817
1818
1819
1820
1821