Debian Codegraph @

terps/agility/interface.c

raw symbols SCIP
1 /* interface.c-- high-level interface functions and misc. utilities */
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 /* */
7 /* This is part of the source for AGiliTy, the (Mostly) Universal */
8 /* AGT Interpreter */
9
10 /* This module contains a miscellany of things that are somewhat */
11 /* system dependent but not enough so to justify being put in */
12 /* OS_<whatever>.c */
13 /* --writestr() and writeln().*/
14 /* --Hooks for sound, pictures, and fonts. */
15 /* --yesno() and wait_return() */
16 /* --Some lower level file stuff */
17 /* --main() and command line parseing stuff */
18
19 #include <stdlib.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <assert.h>
23
24 #include "agility.h"
25 #include "interp.h"
26
27 #ifdef _DOS
28 #ifdef UNIX_IO
29 #undef UNIX_IO
30 #endif
31 #endif
32
33 #ifndef REPLACE_GETFILE
34 #ifdef UNIX_IO
35 #include <dirent.h>
36 #endif
37 #endif
38
39
40 #ifdef UNIX
41 /* Needed because we are compiling with ANSI set */
42 FILE *popen(const char*,const char*);
43 int pclose(FILE*);
44 #endif
45
46 #ifndef REPLACE_BNW
47
48 /* #define DEBUG_BELLS_AND_WHISTLES */
49
50 /* Warning for fontcmd, pictcmd, musiccmd:
51 These all extract filenames from fontlist, pictlist, pixlist, songlist.
52 Any of these are allowed to be NULL and this should be checked
53 before accessing them. */
54
55 #ifdef DEBUG_BELLS_AND_WHISTLES
56 void bnw_report(char *cmdstr,filename *list,int index)
57 {
58 writeln("");
59 writestr(">** ");writestr(cmdstr);writestr(" ");
60 if (list!=NULL) {
61 writestr(list[index]);
62 writestr(" ");}
63 writeln("**<");
64 }
65 #endif /* DEBUG_BELLS_AND_WHISTLES */
66
67 void fontcmd(int cmd,int font)
68 /* 0=Load font, name is fontlist[font]
69 1=Restore original (pre-startup) font
70 2=Set startup font. (<gamename>.FNT)
71 */
72 {
73 #ifdef DEBUG_BELLS_AND_WHISTLES
74 if (cmd==0) bnw_report("Loading Font",fontlist,font);
75 else if (cmd==1) bnw_report("Restoring original font",NULL,0);
76 #endif
77 return;
78 }
79
80 void pictcmd(int cmd,int pict)
81 /* 1=show global picture, name is pictlist[pict]
82 2=show room picture, name is pixlist[pict]
83 3=show startup picture <gamename>.P..
84 */
85 {
86 #ifdef DEBUG_BELLS_AND_WHISTLES
87 if (cmd==1) bnw_report("Showing picture",pictlist,pict);
88 else if (cmd==2) bnw_report("Showing pix",pixlist,pict);
89 agt_waitkey();
90 #endif
91 return;
92 }
93
94
95
96 int musiccmd(int cmd,int song)
97 /* For cmd=1 or 2, the name of the song is songlist[song]
98 The other commands don't take an additional argument.
99 1=play song
100 2=repeat song
101 3=end repeat
102 4=end song
103 5=suspend song
104 6=resume song
105 7=clean-up
106 8=turn sound on
107 9=turn sound off
108 -1=Is a song playing? (0=false, -1=true)
109 -2=Is the sound on? (0=false, -1=true)
110 */
111 {
112 if (cmd==8) sound_on=1;
113 else if (cmd==9) sound_on=0;
114 #ifdef DEBUG_BELLS_AND_WHISTLES
115 switch (cmd) {
116 case 1:bnw_report("Play song",songlist,song);break;
117 case 2:bnw_report("Repeat song",songlist,song);break;
118 case 3:bnw_report("End repeat",NULL,0);break;
119 case 4:bnw_report("End song",NULL,0);break;
120 case 5:bnw_report("Suspend song",NULL,0);break;
121 case 6:bnw_report("Resume song",NULL,0);break;
122 case 7:bnw_report("Clean up",NULL,0);break;
123 case 8:bnw_report("Sound On",NULL,0);break;
124 case 9:bnw_report("Sound Off",NULL,0);break;
125 case -1:return yesno("Is song playing?");
126 case -2:return 1;
127 }
128 #endif
129 return 0;
130 }
131
132 #endif /* REPLACE_BNW */
133
134 static char linebuff[100];
135 static int lp; /* Line pointer */
136 static rbool savenl=0;
137 static rbool needfill; /* Used for paragraph filling */
138 static rbool quotemode=0;
139
140 #ifdef UNIX
141 static rbool ispipe[3]={0,0,0}; /* script, log_in, log_out */
142 #endif
143
144
145
146 void debugout(const char *s)
147 {
148 int i;
149
150 if (DEBUG_OUT) fputs(s,debugfile);
151 else {
152 lp=0;
153 for(;*s!=0;s++) {
154 if (curr_x+lp>=screen_width || lp>80) {
155 if (lp+curr_x>=screen_width)
156 lp=screen_width-curr_x-1;
157 linebuff[lp]=0;
158 agt_puts(linebuff);
159 agt_newline();
160 lp=0;
161 }
162 if (*s=='\n')
163 {linebuff[lp]=0;
164 agt_puts(linebuff);
165 agt_newline();
166 lp=0;}
167 else if (*s=='\t')
168 {for(i=0;i<3;i++) linebuff[lp++]=' ';}
169 else if (*s>=0 && *s<=9) linebuff[lp++]=' ';
170 else linebuff[lp++]=*s;
171 }
172 linebuff[lp]=0;
173 agt_puts(linebuff);
174 }
175 }
176
177
178 int close_pfile(genfile f, int ft)
179 /* ft=0 for script, 4 for log_in, 5 for log_out */
180 {
181 #ifdef UNIX
182 if (ft>=4) ft-=3;
183 if (ispipe[ft]) {
184 ispipe[ft]=0;
185 pclose(f);
186 return 0;
187 }
188 #endif
189 if (ft==4)
190 writeclose(f,NO_FILE_ID);
191 else
192 readclose(f);
193 return 0;
194 }
195
196
197
198 static char *get_log(void)
199 /* Read string from logfile_in */
200 {
201 char *s;
202 static int dead_log;
203
204 if (!filevalid(log_in,fLOG)) { /* We are finishing up */
205 if (++dead_log>100) fatal("Internal error: LOG.");
206 assert(BATCH_MODE);
207 s=rmalloc(2);
208 s[0]=' ';
209 s[1]=0;
210 return s;
211 }
212
213 s=rmalloc(1000);s[0]=' ';s[1]=0;
214 readln(log_in,s,1000);
215 if (texteof(log_in)) { /* Reached end of logfile */
216 close_pfile(log_in,1);
217 log_in=BAD_TEXTFILE;
218 if (BATCH_MODE) {
219 writeln("");
220 writeln("ERROR: Unexpected end of log file.");
221 agt_quit(); /* This doesn't actually quit; it just sets things
222 up so we *will* quit. */
223 dead_log=0;
224 } else {
225 logflag&=~2;
226 fast_replay=0;
227 if (s[0]!=0) writestr(s);
228 }
229 } else { /* Need to delay or wait for keypress */
230 if (logdelay==-1) agt_waitkey();
231 else agt_delay(logdelay);
232 if (s[0]!=0) writestr(s);
233 }
234 return s;
235 }
236
237
238 static void put_log(const char *s)
239 /* Write s to logfile_out */
240 {
241 textputs(log_out,s);
242 if (s[strlen(s)-1]!='\n')
243 textputs(log_out,"\n");
244 }
245
246
247 char *agt_readline(int in_type)
248 {
249 char *s;
250
251 if (PURE_INPUT) agt_textcolor(-1);
252 if (logflag&2) {
253 s=get_log();
254 agt_newline();
255 } else
256 s=agt_input(in_type);
257 if (PURE_INPUT) agt_textcolor(-2);
258
259 if (logflag&1)
260 put_log(s);
261
262 return s;
263 }
264
265 char agt_getchar(void)
266 {
267 char c, *s, buff[2];
268
269 if (PURE_INPUT) agt_textcolor(-1);
270 if (logflag&2) {
271 s=get_log();
272 c=s[0];
273 rfree(s);
274 } else
275 c=agt_getkey(1);
276 if (PURE_INPUT) agt_textcolor(-2);
277 if (logflag&1) {
278 buff[0]=c;buff[1]=0;
279 put_log(buff);
280 }
281 return c;
282 }
283
284 void agt_center(rbool b)
285 /* 1=turn on text centering, 0=turn off */
286 /* At the moment, this is only used for game end messages */
287 /* When it is on, text output with writeln() should be 'centered'; */
288 /* it is up to the interface to decide what that means. */
289 /* writestr() should not be called while centering is on. */
290 {
291 center_on=b;
292 }
293
294 void agt_par(rbool b)
295 /* This has been added for the sake of anyone trying to get this to */
296 /* work on less-than-80 column screens. My personal opinion is that this */
297 /* is probably hopeless; many AGT games assume 80-column format for */
298 /* creating tables and ascii graphics. Nevertheless... */
299 /* Text between an agt_par(1) and an agt_par(0) is logically connected */
300 /* (all part of one description) and so it *might* be possible to reformat */
301 /* it, treating multiple lines as being one paragraph. */
302 /* At the very least, you should look for blank lines and indentation */
303 /* since a single section of text could contain multiple paragraphs. */
304 /* Sections of text _not_ between an agt_par(1) and an agt_par(0) should */
305 /* be treated as though each line were a new paragraph */
306 {
307 par_fill_on=b;
308 if (b==0 && savenl) agt_newline();
309 savenl=0;needfill=0;
310 }
311
312 /* This handles the various format code. They all show up after
313 '\r'; unrecogonized codes are just ignored */
314 static uchar xlat_format_code(uchar c)
315 {
316 if (c==0xFF) {
317 if (fix_ascii) return trans_ibm[0xFF-0x80];
318 else return 0xFF;
319 }
320 return 0;
321 }
322
323 #define FMT_CODE_CNT 15
324
325 static void run_format_code(uchar c)
326 {
327 if (c<FMT_CODE_CNT)
328 agt_textcolor(c-3);
329 }
330
331 #define format_code(c) ((c>0 && c<=LAST_TEXTCODE)||((uchar)c==FORMAT_CODE))
332
333 void writestr(const char *s)
334 {
335 int i, j;
336 char c;
337 int endmark, old_x;
338
339 if (savenl) {
340 assert(par_fill_on);
341 if (!isalnum(s[0])) agt_newline();
342 else agt_puts(" ");
343 /* If combining two lines, insert a space between them. */
344 }
345 savenl=0;
346 i=0;lp=0;
347
348 while(s[i]!=0) {
349 for(;s[i]!=0 && lp<90 && curr_x+lp<screen_width;i++)
350 if (s[i]=='\t')
351 for(j=0;j<TAB_SIZE && curr_x+lp<screen_width;j++) linebuff[lp++]=' ';
352 else if (format_code(s[i])) {linebuff[lp++]=' ';break;} /* Color code */
353 else if (s[i]=='\r') { /* New format code */
354 if (s[i+1]==0) continue; /* Bogus format code */
355 if ( ((uchar)s[i+1])<FMT_CODE_CNT) break;
356 c=(char)xlat_format_code((uchar)s[++i]);
357 if (c!=0) linebuff[lp++]=c;
358 }
359 else if (s[i]=='\n') { break;}
360 else linebuff[lp++]=s[i];
361
362 linebuff[lp]=0;
363
364 /* Backtrack to last space; in case of formatting codes, we should
365 already have one */
366 endmark=lp;
367
368 if (!isspace(s[i]) && !format_code(s[i]) && s[i]!=0)
369 {/* If we aren't conveniently at a break...*/
370 do { /* Find last space */
371 endmark--;
372 } while(endmark>0 && !isspace(linebuff[endmark]));
373 }
374
375 if (endmark==0 && !isspace(linebuff[endmark])) { /* Can't find a break */
376 if (curr_x+lp<screen_width) /* Not a line break */
377 endmark=lp; /* Break at end; it doesn't matter that much */
378 else /* We _need_ a line break but are having trouble finding one */
379 if (curr_x>0) /* already stuff on this line printed previously */
380 endmark=0; /* i.e. print out nothing; move it to next line */
381 else /* We have a single word that is longer than our line */
382 endmark=screen_width; /* Give up */
383 }
384
385 c=linebuff[endmark];
386 linebuff[endmark]=0;
387 old_x=curr_x;
388
389 agt_puts(linebuff);
390
391 linebuff[endmark]=c;
392
393 if (old_x+lp>=screen_width)
394 /* Need to insert line break and skip any spaces */
395 {
396 if (!quotemode) agt_newline();
397 else return; /* In quote mode, just truncate */
398
399 /* Now set up beginning of next line: skip over whitespace */
400 while(endmark<lp && isspace(linebuff[endmark]))
401 endmark++; /* Eliminate EOL whitespace */
402 if (endmark==lp) {/* Nothing left; eliminate whitespace at beginning
403 of next line */
404 while(isspace(s[i]) && s[i]!='\r') i++;
405 lp=endmark=0;
406 }
407 needfill=1;
408 if (endmark==lp && s[i]==0) {
409 needfill=2;
410 return; /* If only spaces left, don't print them */
411 }
412 }
413
414 /* Now copy remaining text */
415 for(j=0;endmark<lp;j++,endmark++) linebuff[j]=linebuff[endmark];
416 lp=j;
417
418 /* Now to deal with format codes */
419 if ((unsigned char)s[i]==FORMAT_CODE) {
420 i++;
421 if (bold_mode) /* Translate as BOLD toggle */
422 {
423 if (textbold)
424 agt_textcolor(-2); /* Turn bold off */
425 else agt_textcolor(-1); /* Turn bold on */
426 textbold=!textbold;
427 }
428 else /* translate as BLACK */
429 agt_textcolor(0);
430 }
431 else if (s[i]>0 && s[i]<=LAST_TEXTCODE)
432 agt_textcolor(s[i++]);
433 else if (s[i]=='\r') {
434 run_format_code((uchar)s[i+1]);
435 i+=2;
436 }
437 else if (s[i]=='\n') {
438 i+=1;
439 agt_newline();
440 }
441 }
442 }
443
444
445
446 void writeln(const char *s)
447 {
448 int i,pad;
449 char *padstr;
450
451 if (center_on && strlen(s)+curr_x<screen_width) {
452 pad=(screen_width-strlen(s))/2;
453 padstr=rmalloc((pad+1)*sizeof(char));
454 for(i=0;i<pad;i++) padstr[i]=' ';
455 padstr[i]=0;
456 agt_puts(padstr);
457 rfree(padstr);
458 }
459 writestr(s);
460 /* needfill=2 if writestr ended with a line that wrapped only
461 because of excess spaces (which have been discarded); needfill==1
462 if writestr wrapped a line for any reason */
463 /* If needfill==2, we've already issued a new-line, so don't issue a
464 second one. */
465 /* If needfill==1, set savenl rather than wrapping (writestr will
466 then decide to wrap or not depending on whether the next line starts
467 with text or nontext), *unless* we are version magx, in which case
468 the game author presumably knew what they were doing, so honor their
469 wishes. */
470 if (par_fill_on && needfill==1)
471 if (aver>=AGX00) agt_newline();
472 else savenl=1;
473 else if (needfill!=2)
474 agt_newline();
475 needfill=0;
476 }
477
478
479 static char fixstatchar(uchar c)
480 /* Eliminate formating characters in the status line */
481 {
482 if (c=='\t' || c<=LAST_TEXTCODE ||
483 (c==FORMAT_CODE) || c=='\r' || c=='\n')
484 return ' ';
485 return c;
486 }
487
488 void print_statline(void)
489 /* Use strings in l_stat and r_stat */
490 {
491 int i,j;
492 char *s, *t;
493 static rbool lastline=0; /* Was a non-empty status line printed last time? */
494
495 s=rmalloc((status_width+1)*sizeof(char));
496
497 /* If both strings empty, don't print the status line */
498 if (l_stat[0]==0 && r_stat[0]==0 && !lastline) return;
499 lastline=(l_stat[0] || r_stat[0]);
500
501 i=status_width-strlen(l_stat)-strlen(r_stat);
502
503 j=0;
504 if (r_stat[0]==0) {/* Center the status line */
505 while(j<i/2) s[j++]=' ';
506 i-=j;
507 } else if (i>6) {s[j++]=' ';i-=2;} /* If statline is wide enough, put a
508 space on each side */
509
510 if (strlen(l_stat)<status_width) /* Copy left side of status line into s*/
511 for(t=l_stat;*t!=0;t++) s[j++]=fixstatchar(*t);
512
513 for(;i>0;i--) s[j++]=' '; /* Insert space between left and right sides */
514
515 if (j+strlen(r_stat)<=status_width) /*Copy right side into s */
516 for(t=r_stat;*t!=0;t++) s[j++]=fixstatchar(*t);
517
518 while(j<status_width) s[j++]=' '; /* Pad any extra width with spaces */
519 s[j]=0; /* Put end of string marker */
520 agt_statline(s); /* Output it */
521 rfree(s);
522 }
523
524
525 void padout(int padleng)
526 {
527 int i;
528 char *pstr;
529
530 if (padleng<=0) return;
531 pstr=rmalloc(padleng+1);
532 for(i=0;i<padleng;i++) pstr[i]=' ';
533 pstr[padleng]=0;
534 writestr(pstr);
535 free(pstr);
536 }
537
538 static int textwidth(char *s)
539 {
540 int n;
541
542 n=0;
543 for(;*s!=0;s++) n+=(*s=='\t') ? TAB_SIZE : 1;
544 return n;
545 }
546
547 void textbox(char *(txt[]),int len,unsigned long flags)
548 /* TB_TTL, TB_BOLD, TB_BORDER, TB_CENTER */
549 {
550 int i, width, padwidth;
551 int *linewidth;
552
553 agt_textcolor(7);
554 if (flags & TB_BOLD) agt_textcolor(-1);
555 else agt_textcolor(-2);
556
557 linewidth=rmalloc(len*sizeof(int));
558
559 width=0; /* This contains the maximum width of any line */
560 for(i=0;i<len;i++) {
561 linewidth[i]=textwidth(txt[i]);
562 if (linewidth[i]>width) width=linewidth[i];
563 }
564
565 agt_makebox(width,len,flags&~(TB_BOLD|TB_CENTER));
566 quotemode=1; /* So newlines will cause truncation rather than a
567 real newline */
568 for(i=0;i<len;i++) {
569 padwidth=width-linewidth[i]; /* Amount of padding we need */
570 if (flags&TB_CENTER)
571 { padout(padwidth/2);padwidth-=padwidth/2;}
572 writestr(txt[i]);
573 padout(padwidth);
574 if (i!=len-1) agt_qnewline();
575 }
576 agt_endbox();
577 quotemode=0; /* Back to normal */
578
579 agt_textcolor(7);
580 textbold=0;
581 }
582
583
584 #ifndef REPLACE_MENU
585
586 int agt_menu(char *header,int size,int width,menuentry *menu)
587 /* This is _very_ minimal as it stands */
588 {
589 int i,j;
590 char sbuff[10];
591 int numcol, colheight;
592
593 if (size==0) return 0;
594
595 width=width+5;
596 numcol=screen_width/width;
597 colheight=size/numcol;
598 if (size%numcol!=0) colheight++;
599
600 writeln(header);
601 for(i=0;i<colheight;i++) {
602 for(j=0;j<numcol;j++) {
603 if (j*colheight+i>=size) break;
604 sprintf(sbuff,"%2d.",j*colheight+i+1);
605 writestr(sbuff);
606 writestr(menu[j*colheight+i]);
607 if (j<numcol-1) padout(width-3-strlen(menu[j*colheight+i]));
608 }
609 writeln("");
610 }
611 do {
612 writestr("Choice:");
613 i=read_number()-1;
614 if (i<0 || i>=size)
615 writeln("Please choose an option from the menu.");
616 } while (!quitflag && (i<0 || i>=size));
617 return i;
618 }
619
620 #endif /* REPLACE_MENU */
621
622
623
624 void prompt_out(int n)
625 /* n=1 standard prompt
626 n=2 question prompt */
627 {
628 agt_textcolor(7);
629 if (PURE_INPUT && n==1) agt_textcolor(-1);
630 if (n==1) {
631 agt_newline();
632 gen_sysmsg(1,">",MSG_MAIN,NULL);
633 }
634 if (n==2) agt_puts("? ");
635 agt_textcolor(7);
636 }
637
638 void agt_waitkey(void)
639 {
640 if (BATCH_MODE || fast_replay)
641 return;
642 agt_getkey(0);
643 }
644
645
646 void wait_return(void)
647 {
648 writeln(" --- HIT ANY KEY ---");
649 agt_waitkey();
650 }
651
652
653 rbool yesno(const char *s)
654 /* True for yes, false for no. */
655 {
656 char c;
657
658 writestr(s);writestr(" ");
659 c='y';
660 do {
661 if (c!='y')
662 writestr("Please answer <y>es or <n>o. ");
663 c=tolower(agt_getchar());
664 } while (c!='y' && c!='n' && !quitflag);
665 return (c=='y');
666 }
667
668
669 void set_test_mode(fc_type fc)
670 {
671 char *errstr;
672
673 log_in=readopen(fc,fLOG,&errstr);
674
675 if (make_test) {
676 if (errstr==NULL)
677 fatal("Log file already exists.");
678 log_out=writeopen(fc,fLOG,NULL,&errstr);
679 if (errstr!=NULL)
680 fatal("Couldn't create log file.");
681 logflag=1;
682 return;
683 }
684
685 logdelay=0;
686 if (errstr!=NULL)
687 fatal("Couldn't open log file.");
688 logflag=2;
689
690 script_on=1;
691 scriptfile=writeopen(fc,fSCR,NULL,&errstr);
692 if (errstr!=NULL)
693 fatal("Couldn't open script file.");
694 }
695
696
697 #ifndef REPLACE_GETFILE
698
699
700 #ifdef UNIX_IO
701
702 extern const char *extname[]; /* From filename.c */
703
704 static rbool check_fname(char *name, filetype ext)
705 {
706 return 0==strcmp(name+strlen(name)-strlen(extname[ext]),extname[ext]);
707 }
708
709
710 static void list_files(char *type, filetype ext)
711 {
712 DIR *currdir;
713 struct dirent *entry;
714 char **filelist;
715 int filecnt, listsize;
716 int maxleng; /* Longest filename; used for formatting */
717 int i,j;
718 int numcols, height;
719
720 filelist=NULL; filecnt=listsize=0; maxleng=0;
721 currdir=opendir(".");
722 if (currdir==NULL) return; /* Nothing we can do except give up */
723 do {
724 entry=readdir(currdir);
725 if ( entry!=NULL && check_fname(entry->d_name,ext) ) {
726 /* It has the right extension; add it to our list of files */
727 if (filecnt>=listsize) {
728 listsize+=5;
729 filelist=rrealloc(filelist,listsize*sizeof(char*));
730 }
731 filelist[filecnt]=rstrdup(entry->d_name);
732 i=strlen(entry->d_name);
733 if (i>screen_width-1) {
734 filelist[filecnt][screen_width-1]=0;
735 i=screen_width-1;
736 }
737 if (i>maxleng) maxleng=i;
738 filecnt++;
739 }
740 } while (entry!=NULL);
741 closedir(currdir);
742 if (filecnt==0) return; /* No files */
743
744 numcols=(screen_width-1)/(maxleng+2); /* Two spaces between columns */
745 if (numcols<1)
746 numcols=1;
747 height=(filecnt+numcols-1)/numcols; /* Height, rounded up. */
748
749 writeln("");
750 writestr("Existing ");writestr(type);writestr("files:");
751 for(i=0;i<height;i++) {
752 writeln("");
753 for(j=0;j<numcols;j++)
754 if (i+j*height < filecnt) {
755 if (maxleng+2<=screen_width-1) writestr(" ");
756 writestr(filelist[i+j*height]);
757 padout(maxleng-strlen(filelist[i+j*height]));
758 rfree(filelist[i+j*height]);
759 }
760 }
761 writeln("");
762 rfree(filelist);
763 }
764 #endif /* UNIX_IO */
765
766
767
768
769 /* This opens the file refered to by fname and returns it */
770 static genfile uf_open(fc_type fc, filetype ext, rbool rw)
771 {
772 char *errstr;
773 genfile f;
774
775 if (rw) { /* Check to see if we are overwriting... */
776 #ifdef UNIX
777 if (fc->special)
778 f=writeopen(fc,ext,NULL,&errstr);
779 else
780 #endif
781 {
782 if (fileexist(fc,ext) && ext!=fSCR) {
783 if (!yesno("This file already exists; overwrite?"))
784 /* That is, DON'T overwrite */
785 return badfile(ext);
786 }
787 f=writeopen(fc,ext,NULL,&errstr);
788 }
789 } else
790 f=readopen(fc,ext,&errstr);
791 if (errstr!=NULL) writeln(errstr);
792 return f;
793 }
794
795 static fc_type last_save=NULL;
796 static fc_type last_log=NULL;
797 static fc_type last_script=NULL;
798
799
800 genfile get_user_file(int ft)
801 /* ft= 0:script, 1:save 2:restore, 3:log(read) 4:log(write) */
802 /* Should return file in open state, ready to be read or written to,
803 as the case may be */
804 {
805 /* int extlen;*/
806 rbool rw; /* True if writing, false if reading */
807 filetype ext;
808 genfile fd;
809 fc_type def_fc, fc;
810 char *fname;
811 char *ftype;
812 char *p,*q;
813
814 switch (ft) {
815 case 0: ftype="script ";
816 def_fc=last_script;
817 rw=1;ext=fSCR; break;
818 case 1: ftype="save ";
819 def_fc=last_save;
820 rw=1;ext=fSAV;break;
821 case 2: ftype="restore ";
822 def_fc=last_save;
823 rw=0;ext=fSAV;break;
824 case 3: ftype="log ";
825 def_fc=last_log;
826 rw=0;ext=fLOG;break;
827 case 4: ftype="log ";
828 def_fc=last_log;
829 rw=1;ext=fLOG;break;
830 default: writeln("<INTERNAL ERROR: invalid file type>");
831 return badfile(fSAV);
832 }
833 #ifdef UNIX_IO
834 if (!rw) { /* List available files. */
835 list_files(ftype,ext);
836 ftype=NULL;
837 } else
838 #else
839 writestr(" ");
840 #endif
841 writestr("Enter ");
842 if (ftype!=NULL) writestr(ftype);
843 writestr("file name");
844 if (def_fc!=NULL) {
845 char *s;
846 s=formal_name(def_fc,ext);
847 writestr(" (");
848 writestr(s);
849 writestr(")");
850 rfree(s);
851 }
852 writestr(": ");
853
854 if (PURE_INPUT) agt_textcolor(-1);
855 fname=agt_input(4);
856 if (PURE_INPUT) agt_textcolor(-2);
857
858 /* Delete whitespace before and after the file name. */
859 for(p=fname;isspace(*p);p++);
860 if (*p==0) { /* Line is all whitespace; use default if there is one */
861 if (def_fc==NULL) {
862 writeln("Never mind.");
863 rfree(fname);
864 return badfile(ext);}
865 else {
866 rfree(fname);
867 fc=def_fc;
868 }
869 } else { /* Line is _not_ all whitespace: we have a file name */
870 for(q=fname;*p!=0;p++,q++)
871 *q=*p;
872 q--;
873 while(isspace(*q)) q--;
874 q++;
875 *q=0;
876 fc=init_file_context(fname,ext);
877 }
878
879 fd=uf_open(fc,ext,rw);
880
881 if (!filevalid(fd,ext)) {
882 if (fc!=def_fc) release_file_context(&fc);
883 return fd;
884 }
885
886 switch(ft)
887 {
888 case 0: last_script=fc;break;
889 case 1: last_save=fc; break;
890 case 2: last_save=fc; break;
891 case 3: last_log=fc; break;
892 case 4: last_log=fc; break;
893 }
894 if (fc!=def_fc) release_file_context(&def_fc);
895 return fd;
896 }
897
898
899 void set_default_filenames(fc_type fc)
900 {
901 last_save=convert_file_context(fc,fSAV,NULL);
902 last_log=convert_file_context(fc,fLOG,NULL);
903 last_script=convert_file_context(fc,fSCR,NULL);
904 }
905
906
907
908 #endif /* REPLACE_GETFILE */
909
910
911
912
913 void script(uchar onp)
914 {
915 if (onp==script_on)
916 if (onp==0) writeln("Scripting wasn't on.");
917 else writeln("Scripting is already on.");
918 else if (onp==1) {
919 scriptfile=get_user_file(0);
920 if (filevalid(scriptfile,fSCR)) script_on=1;
921 } else if (filevalid(scriptfile,fSCR)) {
922 if (close_pfile(scriptfile,0))
923 rprintf("Script file: %s",strerror(errno));
924 scriptfile=BAD_TEXTFILE;
925 script_on=0;
926 }
927 }
928
929
930 void logon(void)
931 {
932 if (logflag&1) {
933 writeln("Already logging");
934 return;}
935 log_out=get_user_file(4);
936 if (filevalid(log_out,fLOG))
937 logflag|=1;
938 }
939
940 void replay(int delay)
941 {
942 if (logflag&2) return; /* Nested replays are meaningless */
943 log_in=get_user_file(3);
944 if (filevalid(log_in,fLOG)) {
945 logflag|=2;
946 logdelay=delay;
947 }
948 }
949
950
951 /* These two are intended to be called by the platform-dependent
952 interface (e.g. if the user had chosen these from some general purpose
953 menu) */
954 /* They're never called from the rest of the code */
955
956 void agt_save(void)
957 {
958 savegame();
959 }
960
961 void agt_restore(void)
962 {
963 doing_restore=1;
964 }
965
966 void agt_restart(void)
967 {
968 doing_restore=2;
969 }
970
971 void agt_quit(void)
972 {
973 doing_restore=4;
974 }
975
976
977 /* This should be rmalloc'd */
978 static fc_type newgame_fc;
979
980 fc_type new_game(void)
981 {
982 return newgame_fc;
983 }
984
985 void agt_newgame(fc_type fc)
986 {
987 newgame_fc=fc;
988 doing_restore=3;
989 }
990
991 static rbool end_cmd_options;
992
993 void set_default_options(void)
994 {
995 init_flags();
996 flag=rmalloc(sizeof(rbool));
997 debug_parse=0;
998 DEBUG_AGT_CMD=0;
999 DEBUG_EXEC_VERB=0;
1000 DEBUG_DISAMBIG=0;
1001 DEBUG_SMSG=0;
1002 }
1003
1004 void helpmsg(void)
1005 {
1006 printf("AGiliTy: "
1007 "The (Mostly) Universal AGT Interpreter, ");
1008 printf("%s\n",version_str);
1009 printf(" Copyright (C) 1996-1999,2001 Robert Masenten\n");
1010 printf("[%s]\n",portstr);
1011 printf("Syntax: agil <options> <game name>\n");
1012 printf("Options:\n");
1013 printf("(all of these can be turned off by putting a - after the option)\n");
1014 printf(" -i Try to use IBM character set.\n");
1015 printf(" -1 IRUN Mode: Print messages in first person\n");
1016 printf(" -h Print out this message\n");
1017 printf(" -d Debug metacommand execution\n");
1018 printf(" -t Test mode; see accompanying documentation. Implies -r.\n");
1019 printf(" -c Create test file.\n");
1020 printf(" -m Force descriptions to be loaded from disk.\n");
1021 #ifdef OPEN_AS_TEXT
1022 printf(" -b Open data files as binary files.\n");
1023 #endif
1024 printf("\nTechnical options (intended for debugging AGiliTy itself).\n");
1025 printf(" -p Debug parser\n");
1026 printf(" -x Debug verb execution loop\n");
1027 printf(" -a Debug disambiguation system\n");
1028 printf(" -s Debug STANDARD message handler\n");
1029 #ifdef MEM_INFO
1030 printf(" -M Debug memory allocation\n");
1031 #endif
1032 }
1033
1034
1035 static rbool setarg(char **optptr)
1036 {
1037 if ( (*optptr)[1]=='+') {(*optptr)++;return 1;}
1038 if ( (*optptr)[1]=='-') {(*optptr)++;return 0;}
1039 return 1;
1040 }
1041
1042 #ifdef UNIX
1043 #define fixcase(c) (c)
1044 #else
1045 #define fixcase(c) tolower(c)
1046 #endif
1047
1048 void parse_options(char *opt,char *next)
1049 {
1050 if (opt[0]=='-' && opt[1]==0) /* -- */
1051 {end_cmd_options=1;return;}
1052 for(;*opt!=0;opt++)
1053 switch(fixcase(*opt))
1054 {
1055 case '?': case 'h':
1056 helpmsg();
1057 exit(EXIT_SUCCESS);
1058 case 'p': debug_parse=setarg(&opt);break;
1059 case 'a': DEBUG_DISAMBIG=setarg(&opt);break;
1060 case 'd': DEBUG_AGT_CMD=setarg(&opt);break;
1061 case 'x':DEBUG_EXEC_VERB=setarg(&opt);break;
1062 case 's':DEBUG_SMSG=setarg(&opt);break;
1063 #ifdef MEM_INFO
1064 case 'M': DEBUG_MEM=setarg(&opt);break;
1065 #endif
1066 case 'm': descr_maxmem=0; break;
1067 case 'i': fix_ascii_flag=!setarg(&opt);break;
1068 case 't': BATCH_MODE=setarg(&opt); break;
1069 case 'c': make_test=setarg(&opt); break;
1070 case '1': irun_mode=setarg(&opt);break;
1071 #ifdef OPEN_FILE_AS_TEXT
1072 case 'b': open_as_binary=setarg(&opt);break;
1073 #endif
1074 default:printf("Do not recognize option %c\n",*opt);
1075 helpmsg();
1076 exit(EXIT_FAILURE);
1077 }
1078 }
1079
1080
1081 #ifndef REPLACE_MAIN
1082
1083 #ifdef MSDOS
1084 extern rbool use_bios;
1085 #endif
1086
1087
1088 int main(int argc,char *argv[])
1089 {
1090 int i;
1091 char *gamefile;
1092 #ifdef MSDOS
1093 rbool biosvar=0;
1094 #endif
1095
1096 set_default_options();
1097 end_cmd_options=0;
1098 gamefile=NULL;
1099 for(i=1;i<argc;i++)
1100 if (argv[i][0]=='-' && !end_cmd_options)
1101 parse_options(argv[i]+1,argv[i+1]);
1102 #ifdef MSDOS /* For backward compatibility w/ original AGT interpreters */
1103 else if (argv[i][0]=='/' && tolower(argv[i][1])=='b'
1104 && argv[i][2]==0)
1105 biosvar=1;
1106 #endif
1107 else if (gamefile==NULL)
1108 gamefile=argv[i];
1109 else fatal("Please specify only one game\n");
1110 if (gamefile==NULL) {
1111 helpmsg();
1112 exit(EXIT_FAILURE);}
1113
1114 init_interface(argc,argv);
1115 /* From this point on, MUST use writestr/writeln or may
1116 cause problems w/ the interfaces on some platforms
1117 that have to keep track of cursor position */
1118 #ifdef MSDOS
1119 use_bios=biosvar;
1120 #endif
1121 run_game(init_file_context(gamefile,fDA1));
1122 return EXIT_SUCCESS;
1123 }
1124
1125 #endif /* REPLACE_MAIN */
1126
1127