More misc.c cleanup.
[open-adventure.git] / misc.c
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/time.h>
6 #include "advent.h"
7 #include "funcs.h"
8 #include "database.h"
9
10 /* hack to ignore GCC Unused Result */
11 #define IGNORE(r) do{if(r){}}while(0)
12
13 /*  I/O routines (SPEAK, PSPEAK, RSPEAK, SETPRM, GETIN, YES) */
14
15 void SPEAK(vocab_t N)
16 /*  Print the message which starts at LINES(N).  Precede it with a blank line
17  *  unless game.blklin is false. */
18 {
19     long blank, casemake, I, K, L, NEG, NPARMS, PARM, PRMTYP, state;
20
21     if (N == 0)
22         return;
23     blank=game.blklin;
24     K=N;
25     NPARMS=1;
26 L10:
27     L=labs(LINES[K])-1;
28     K=K+1;
29     LNLENG=0;
30     LNPOSN=1;
31     state=0;
32     for (I=K; I<=L; I++) {
33         PUTTXT(LINES[I],state,2);
34     } /* end loop */
35     LNPOSN=0;
36 L30:
37     LNPOSN=LNPOSN+1;
38 L32:
39     if(LNPOSN > LNLENG) 
40         goto L40;
41     if(INLINE[LNPOSN] != 63) 
42         goto L30;
43     {long x = LNPOSN+1; PRMTYP=INLINE[x];}
44     /*  63 is a "%"; the next character determine the type of
45      *  parameter: 1 (!) = suppress message completely, 29 (S) = NULL
46      *  If PARM=1, else 'S' (optional plural ending), 33 (W) = word
47      *  (two 30-bit values) with trailing spaces suppressed, 22 (L) or
48      *  31 (U) = word but map to lower/upper case, 13 (C) = word in
49      *  lower case with first letter capitalised, 30 (T) = text ending
50      *  with a word of -1, 65-73 (1-9) = number using that many
51      *  characters, 12 (B) = variable number of blanks. */
52     if(PRMTYP == 1)
53         return;
54     if(PRMTYP == 29)
55         goto L320;
56     if(PRMTYP == 30)
57         goto L340;
58     if(PRMTYP == 12)
59         goto L360;
60     if(PRMTYP == 33 || PRMTYP == 22 || PRMTYP == 31 || PRMTYP == 13)
61         goto L380;
62     PRMTYP=PRMTYP-64;
63     if(PRMTYP < 1 || PRMTYP > 9) goto L30;
64     SHFTXT(LNPOSN+2,PRMTYP-2);
65     LNPOSN=LNPOSN+PRMTYP;
66     PARM=labs(PARMS[NPARMS]);
67     NEG=0;
68     if(PARMS[NPARMS] < 0)
69         NEG=9;
70     /* 390 */ for (I=1; I<=PRMTYP; I++) {
71         LNPOSN=LNPOSN-1;
72         INLINE[LNPOSN]=MOD(PARM,10)+64;
73         if(I == 1 || PARM != 0)
74             goto L390;
75         INLINE[LNPOSN]=NEG;
76         NEG=0;
77 L390:
78         PARM=PARM/10;
79     }
80     LNPOSN=LNPOSN+PRMTYP;
81 L395:
82     NPARMS=NPARMS+1;
83     goto L32;
84
85 L320:
86     SHFTXT(LNPOSN+2,-1);
87     INLINE[LNPOSN]=55;
88     if(PARMS[NPARMS] == 1)
89         SHFTXT(LNPOSN+1,-1);
90     goto L395;
91
92 L340:
93     SHFTXT(LNPOSN+2,-2);
94     state=0;
95     casemake=2;
96 L345: 
97     if(PARMS[NPARMS] < 0) goto L395;
98     {long x = NPARMS+1; if(PARMS[x] < 0)
99                             casemake=0;}
100     PUTTXT(PARMS[NPARMS],state,casemake);
101     NPARMS=NPARMS+1;
102     goto L345;
103
104 L360:
105     PRMTYP=PARMS[NPARMS];
106     SHFTXT(LNPOSN+2,PRMTYP-2);
107     if(PRMTYP == 0) goto L395;
108     for (I=1; I<=PRMTYP; I++) {
109         INLINE[LNPOSN]=0;
110         LNPOSN=LNPOSN+1;
111     }
112     goto L395;
113
114 L380:
115     SHFTXT(LNPOSN+2,-2);
116     state=0;
117     casemake= -1;
118     if(PRMTYP == 31)
119         casemake=1;
120     if(PRMTYP == 33)
121         casemake=0;
122     I=LNPOSN;
123     PUTTXT(PARMS[NPARMS],state,casemake);
124     {long x = NPARMS+1; PUTTXT(PARMS[x],state,casemake);}
125     if(PRMTYP == 13 && INLINE[I] >= 37 && INLINE[I] <=
126        62)INLINE[I]=INLINE[I]-26;
127     NPARMS=NPARMS+2;
128     goto L32;
129
130 L40:    
131     if (blank)
132         TYPE0();
133     blank=false;
134     TYPE();
135     K=L+1;
136     if(LINES[K] >= 0)
137         goto L10;
138 }
139
140 void PSPEAK(vocab_t msg,int skip)
141 /*  Find the skip+1st message from msg and print it.  msg should be
142  *  the index of the inventory message for object.  (INVEN+N+1 message
143  *  is game.prop=N message). */
144 {
145     long I, M;
146
147     M=PTEXT[msg];
148     if(skip >= 0) {
149         for (I=0; I<=skip; I++) {
150 L1:         M=labs(LINES[M]);
151             if (LINES[M] >= 0)
152                 goto L1;
153         }
154     }
155     SPEAK(M);
156 }
157 void RSPEAK(vocab_t i)
158 /* Print the i-th "random" message (section 6 of database). */
159 {
160     if (i != 0)
161         SPEAK(RTEXT[i]);
162 }
163
164 void SETPRM(long first, long p1, long p2)
165 /*  Stores parameters into the PRMCOM parms array for use by speak.  P1 and P2
166  *  are stored into PARMS(first) and PARMS(first+1). */
167 {
168     if(first >= MAXPARMS)
169         BUG(29);
170     else {
171         PARMS[first] = p1;
172         PARMS[first+1] = p2;
173     }
174 }
175
176 #undef GETIN
177 #define WORD1 (*wORD1)
178 #define WORD1X (*wORD1X)
179 #define WORD2 (*wORD2)
180 #define WORD2X (*wORD2X)
181 bool fGETIN(FILE *input, long *wORD1, long *wORD1X, long *wORD2, long *wORD2X) 
182 /*  Get a command from the adventurer.  Snarf out the first word, pad it with
183  *  blanks, and return it in WORD1.  Chars 6 thru 10 are returned in WORD1X, in
184  *  case we need to print out the whole word in an error message.  Any number of
185  *  blanks may follow the word.  If a second word appears, it is returned in
186  *  WORD2 (chars 6 thru 10 in WORD2X), else WORD2 is -1. */
187 {
188     long JUNK;
189
190     for (;;) {
191         if(game.blklin)
192             TYPE0();
193         MAPLIN(input);
194         if (feof(input))
195             return false;
196         WORD1=GETTXT(true,true,true);
197         if (game.blklin && WORD1 < 0)
198             continue;
199         WORD1X=GETTXT(false,true,true);
200 L12:    JUNK=GETTXT(false,true,true);
201         if(JUNK > 0) goto L12;
202         WORD2=GETTXT(true,true,true);
203         WORD2X=GETTXT(false,true,true);
204 L22:    JUNK=GETTXT(false,true,true);
205         if(JUNK > 0) goto L22;
206         if(GETTXT(true,true,true) <= 0)
207             return true;
208         RSPEAK(53);
209     }
210 }
211 #define GETIN(SRC,WORD1,WORD1X,WORD2,WORD2X) fGETIN(SRC,&WORD1,&WORD1X,&WORD2,&WORD2X)
212
213 long YES(FILE *input, vocab_t x, vocab_t y, vocab_t z)
214 /*  Print message X, wait for yes/no answer.  If yes, print Y and return true;
215  *  if no, print Z and return false. */
216 {
217     token_t yeah, reply, junk1, junk2, junk3;
218
219 L1:     RSPEAK(x);
220     GETIN(input, reply,junk1,junk2,junk3);
221     if(reply == MAKEWD(250519) || reply == MAKEWD(25)) goto L10;
222     if(reply == MAKEWD(1415) || reply == MAKEWD(14)) goto L20;
223     RSPEAK(185);
224     goto L1;
225 L10:    yeah=true;
226     RSPEAK(y);
227     return(yeah);
228 L20:    yeah=false;
229     RSPEAK(z);
230     return(yeah);
231 }
232
233 /*  Line-parsing routines (GETTXT, MAKEWD, PUTTXT, SHFTXT, TYPE0) */
234
235 long GETTXT(bool skip, bool onewrd, bool upper)
236 /*  Take characters from an input line and pack them into 30-bit words.
237  *  Skip says to skip leading blanks.  ONEWRD says stop if we come to a
238  *  blank.  UPPER says to map all letters to uppercase.  If we reach the
239  *  end of the line, the word is filled up with blanks (which encode as 0's).
240  *  If we're already at end of line when TEXT is called, we return -1. */
241 {
242     long text;
243     static long splitting = -1;
244
245     if(LNPOSN != splitting)
246         splitting = -1;
247     text= -1;
248     while (true) {
249         if(LNPOSN > LNLENG)
250             return(text);
251         if((!skip) || INLINE[LNPOSN] != 0)
252             break;
253         LNPOSN=LNPOSN+1;
254     }
255
256     text=0;
257     for (int I=1; I<=TOKLEN; I++) {
258         text=text*64;
259         if(LNPOSN > LNLENG || (onewrd && INLINE[LNPOSN] == 0))
260             continue;
261         char current=INLINE[LNPOSN];
262         if(current < 63) {
263             splitting = -1;
264             if(upper && current >= 37)
265                 current=current-26;
266             text=text+current;
267             LNPOSN=LNPOSN+1;
268             continue;
269         }
270         if(splitting != LNPOSN) {
271             text=text+63;
272             splitting = LNPOSN;
273             continue;
274         }
275
276         text=text+current-63;
277         splitting = -1;
278         LNPOSN=LNPOSN+1;
279     }
280
281     return text;
282 }
283
284 token_t MAKEWD(long letters)
285 /*  Combine TOKLEN (currently 5) uppercase letters (represented by
286  *  pairs of decimal digits in lettrs) to form a 30-bit value matching
287  *  the one that GETTXT would return given those characters plus
288  *  trailing blanks.  Caution: lettrs will overflow 31 bits if
289  *  5-letter word starts with V-Z.  As a kludgey workaround, you can
290  *  increment a letter by 5 by adding 50 to the next pair of
291  *  digits. */
292 {
293     long i = 1, word = 0;
294
295     for (long k=letters; k != 0; k=k/100) {
296         word=word+i*(MOD(k,50)+10);
297         i=i*64;
298         if(MOD(k,100) > 50)word=word+i*5;
299     }
300     i=64L*64L*64L*64L*64L/i;
301     word=word*i;
302     return word;
303 }
304
305 void fPUTTXT(token_t word, long *state, long casemake)
306 /*  Unpack the 30-bit value in word to obtain up to TOKLEN (currently
307  *  5) integer-encoded chars, and store them in inline starting at
308  *  LNPOSN.  If LNLENG>=LNPOSN, shift existing characters to the right
309  *  to make room.  STATE will be zero when puttxt is called with the
310  *  first of a sequence of words, but is thereafter unchanged by the
311  *  caller, so PUTTXT can use it to maintain state across calls.
312  *  LNPOSN and LNLENG are incremented by the number of chars stored.
313  *  If CASEMAKE=1, all letters are made uppercase; if -1, lowercase; if 0,
314  *  as is.  any other value for case is the same as 0 but also causes
315  *  trailing blanks to be included (in anticipation of subsequent
316  *  additional text). */
317 {
318     long alph1, alph2, byte, div, i, w;
319
320     alph1=13*casemake+24;
321     alph2=26*labs(casemake)+alph1;
322     if(labs(casemake) > 1)
323         alph1=alph2;
324     /*  alph1&2 define range of wrong-case chars, 11-36 or 37-62 or empty. */
325     div=64L*64L*64L*64L;
326     w=word;
327     for (i=1; i<=TOKLEN; i++) 
328     {
329         if(w <= 0 && *state == 0 && labs(casemake) <= 1)
330             return;
331         byte=w/div;
332         if(*state != 0 || byte != 63) goto L12;
333         *state=63;
334         goto L18;
335
336 L12:    SHFTXT(LNPOSN,1);
337         *state=*state+byte;
338         if(*state < alph2 && *state >= alph1)*state=*state-26*casemake;
339         INLINE[LNPOSN]=*state;
340         LNPOSN=LNPOSN+1;
341         *state=0;
342 L18:    w=(w-byte*div)*64;
343     }
344 }
345 #define PUTTXT(WORD,STATE,CASE) fPUTTXT(WORD,&STATE,CASE)
346
347 void SHFTXT(long from, long delta) 
348 /*  Move INLINE(N) to INLINE(N+DELTA) for N=FROM,LNLENG.  Delta can be
349  *  negative.  LNLENG is updated; LNPOSN is not changed. */
350 {
351     long I, k, j;
352
353     if (!(LNLENG < from || delta == 0)) {
354         for (I=from; I<=LNLENG; I++) {
355             k=I;
356             if(delta > 0)
357                 k=from+LNLENG-I;
358             j=k+delta;
359             INLINE[j]=INLINE[k];
360         } /* end loop */
361     }
362     LNLENG=LNLENG+delta;
363     return;
364 }
365
366
367 void TYPE0(void)
368 /*  Type a blank line.  This procedure is provided as a convenience for callers
369  *  who otherwise have no use for MAPCOM. */
370 {
371     long temp;
372
373     temp=LNLENG;
374     LNLENG=0;
375     TYPE();
376     LNLENG=temp;
377     return;
378 }
379
380 /*  Suspend/resume I/O routines (SAVWDS, SAVARR, SAVWRD) */
381
382 #undef SAVWDS
383 void fSAVWDS(long *W1, long *W2, long *W3, long *W4,
384              long *W5, long *W6, long *W7)
385 /* Write or read 7 variables.  See SAVWRD. */
386 {
387     SAVWRD(0,(*W1));
388     SAVWRD(0,(*W2));
389     SAVWRD(0,(*W3));
390     SAVWRD(0,(*W4));
391     SAVWRD(0,(*W5));
392     SAVWRD(0,(*W6));
393     SAVWRD(0,(*W7));
394     return;
395 }
396 #define SAVWDS(W1,W2,W3,W4,W5,W6,W7) fSAVWDS(&W1,&W2,&W3,&W4,&W5,&W6,&W7)
397
398 #undef SAVARR
399 void fSAVARR(long arr[], long n)
400 /* Write or read an array of n words.  See SAVWRD. */
401 {
402     long i;
403
404     for (i=1; i<=n; i++) {
405         SAVWRD(0,arr[i]);
406     }
407     return;
408 }
409 #define SAVARR(ARR,N) fSAVARR(ARR,N)
410
411 #undef SAVWRD
412 #define WORD (*wORD)
413 void fSAVWRD(long OP, long *wORD) {
414 static long BUF[250], CKSUM = 0, H1, HASH = 0, N = 0, STATE = 0;
415
416 /*  If OP<0, start writing a file, using word to initialise encryption; save
417  *  word in the file.  If OP>0, start reading a file; read the file to find
418  *  the value with which to decrypt the rest.  In either case, if a file is
419  *  already open, finish writing/reading it and don't start a new one.  If OP=0,
420  *  read/write a single word.  Words are buffered in case that makes for more
421  *  efficient disk use.  We also compute a simple checksum to catch elementary
422  *  poking within the saved file.  When we finish reading/writing the file,
423  *  we store zero into WORD if there's no checksum error, else nonzero. */
424
425         if(OP != 0){long ifvar; ifvar=(STATE); switch (ifvar<0? -1 : ifvar>0? 1 :
426                 0) { case -1: goto L30; case 0: goto L10; case 1: goto L30; }}
427         if(STATE == 0)return;
428         if(N == 250)SAVEIO(1,STATE > 0,BUF);
429         N=MOD(N,250)+1;
430         H1=MOD(HASH*1093L+221573L,1048576L);
431         HASH=MOD(H1*1093L+221573L,1048576L);
432         H1=MOD(H1,1234)*765432+MOD(HASH,123);
433         N--;
434         if(STATE > 0)WORD=BUF[N]+H1;
435         BUF[N]=WORD-H1;
436         N++;
437         CKSUM=MOD(CKSUM*13+WORD,1000000000L);
438         return;
439
440 L10:    STATE=OP;
441         SAVEIO(0,STATE > 0,BUF);
442         N=1;
443         if(STATE > 0) goto L15;
444         HASH=MOD(WORD,1048576L);
445         BUF[0]=1234L*5678L-HASH;
446 L13:    CKSUM=BUF[0];
447         return;
448
449 L15:    SAVEIO(1,true,BUF);
450         HASH=MOD(1234L*5678L-BUF[0],1048576L);
451          goto L13;
452
453 L30:    if(N == 250)SAVEIO(1,STATE > 0,BUF);
454         N=MOD(N,250)+1;
455         if(STATE > 0) goto L32;
456         N--; BUF[N]=CKSUM; N++;
457         SAVEIO(1,false,BUF);
458 L32:    N--; WORD=BUF[N]-CKSUM; N++;
459         SAVEIO(-1,STATE > 0,BUF);
460         STATE=0;
461         return;
462 }
463 #undef WORD
464 #define SAVWRD(OP,WORD) fSAVWRD(OP,&WORD)
465
466 /*  Data structure  routines */
467
468 long VOCAB(long id, long init) 
469 /*  Look up ID in the vocabulary (ATAB) and return its "definition" (KTAB), or
470  *  -1 if not found.  If INIT is positive, this is an initialisation call setting
471  *  up a keyword variable, and not finding it constitutes a bug.  It also means
472  *  that only KTAB values which taken over 1000 equal INIT may be considered.
473  *  (Thus "STEPS", which is a motion verb as well as an object, may be located
474  *  as an object.)  And it also means the KTAB value is taken modulo 1000. */
475 {
476     long i, lexeme;
477
478     for (i=1; i<=TABSIZ; i++) {
479         if(KTAB[i] == -1) 
480             goto L2;
481         if(init >= 0 && KTAB[i]/1000 != init) 
482             goto L1;
483         if(ATAB[i] == id)
484             goto L3;
485 L1:;
486     }
487     BUG(21);
488
489 L2: lexeme= -1;
490     if(init < 0)
491         return(lexeme);
492     BUG(5);
493
494 L3: lexeme=KTAB[i];
495     if(init >= 0)lexeme=MOD(lexeme,1000);
496     return(lexeme);
497 }
498
499 void DSTROY(long object)
500 /*  Permanently eliminate "object" by moving to a non-existent location. */
501 {
502     MOVE(object,0);
503 }
504
505 void JUGGLE(long object)
506 /*  Juggle an object by picking it up and putting it down again, the purpose
507  *  being to get the object to the front of the chain of things at its loc. */
508 {
509     long i, j;
510
511     i=game.place[object];
512     j=game.fixed[object];
513     MOVE(object,i);
514     MOVE(object+NOBJECTS,j);
515 }
516
517 void MOVE(long object, long where)
518 /*  Place any object anywhere by picking it up and dropping it.  May
519  *  already be toting, in which case the carry is a no-op.  Mustn't
520  *  pick up objects which are not at any loc, since carry wants to
521  *  remove objects from game.atloc chains. */
522 {
523     long from;
524
525     if(object > NOBJECTS) 
526         goto L1;
527     from=game.place[object];
528     goto L2;
529 L1:
530     from=game.fixed[object-NOBJECTS];
531 L2:
532     if(from > 0 && from <= 300)
533         CARRY(object,from);
534     DROP(object,where);
535 }
536
537 long PUT(long object, long where, long pval)
538 /*  PUT is the same as MOVE, except it returns a value used to set up the
539  *  negated game.prop values for the repository objects. */
540 {
541     MOVE(object,where);
542     return (-1)-pval;;
543 }
544
545 void CARRY(long object, long where) 
546 /*  Start toting an object, removing it from the list of things at its former
547  *  location.  Incr holdng unless it was already being toted.  If object>NOBJECTS
548  *  (moving "fixed" second loc), don't change game.place or game.holdng. */
549 {
550     long temp;
551
552     if(object <= NOBJECTS) {
553         if(game.place[object] == -1)
554             return;
555         game.place[object]= -1;
556         game.holdng=game.holdng+1;
557     }
558     if(game.atloc[where] == object) {
559         game.atloc[where]=game.link[object];
560         return;
561     }
562     temp=game.atloc[where];
563 L7: if(game.link[temp] == object)
564         goto L8;
565     temp=game.link[temp];
566     goto L7;
567 L8: game.link[temp]=game.link[object];
568 }
569
570 void DROP(long object, long where)
571 /*  Place an object at a given loc, prefixing it onto the game.atloc list.  Decr
572  *  game.holdng if the object was being toted. */
573 {
574     if(object > NOBJECTS)
575         goto L1;
576     if(game.place[object] == -1)
577         game.holdng=game.holdng-1;
578     game.place[object]=where;
579     goto L2;
580 L1: game.fixed[object-NOBJECTS]=where;
581 L2: if(where <= 0)
582         return;
583     game.link[object]=game.atloc[where];
584     game.atloc[where]=object;
585 }
586
587 long ATDWRF(long where)
588 /*  Return the index of first dwarf at the given location, zero if no dwarf is
589  *  there (or if dwarves not active yet), -1 if all dwarves are dead.  Ignore
590  *  the pirate (6th dwarf). */
591 {
592     long at, i;
593
594     at =0;
595     if(game.dflag < 2)
596         return(at);
597     at = -1;
598     for (i=1; i<=NDWARVES-1; i++) {
599         if(game.dloc[i] == where)
600             return i;
601         if(game.dloc[i] != 0)
602             at=0;
603     }
604     return(at);
605 }
606
607 /*  Utility routines (SETBIT, TSTBIT, set_seed, get_next_lcg_value,
608  *  randrange, RNDVOC, BUG) */
609
610 long SETBIT(long bit)
611 /*  Returns 2**bit for use in constructing bit-masks. */
612 {
613     return(2 << bit);
614 }
615
616 bool TSTBIT(long mask, int bit)
617 /*  Returns true if the specified bit is set in the mask. */
618 {
619     return (mask & (1 << bit)) != 0;
620 }
621
622 void set_seed(long seedval)
623 /* Set the LCG seed */
624 {
625     lcgstate.x = (unsigned long) seedval % lcgstate.m;
626 }
627
628 unsigned long get_next_lcg_value(void)
629 /* Return the LCG's current value, and then iterate it. */
630 {
631     unsigned long old_x = lcgstate.x;
632     lcgstate.x = (lcgstate.a * lcgstate.x + lcgstate.c) % lcgstate.m;
633     return old_x;
634 }
635
636 long randrange(long range)
637 /* Return a random integer from [0, range). */
638 {
639     return range * get_next_lcg_value() / lcgstate.m;
640 }
641
642 long RNDVOC(long second, long force)
643 /*  Searches the vocabulary ATAB for a word whose second character is
644  *  char, and changes that word such that each of the other four
645  *  characters is a random letter.  If force is non-zero, it is used
646  *  as the new word.  Returns the new word. */
647 {
648     long rnd = force;
649
650     if (rnd == 0) {
651         for (int i = 1; i <= 5; i++) {
652             long j = 11 + randrange(26);
653             if (i == 2)
654                 j = second;
655             rnd = rnd * 64 + j;
656         }
657     }
658
659     long div = 64L * 64L * 64L;
660     for (int i = 1; i <= TABSIZ; i++) {
661         if (MOD(ATAB[i]/div, 64L) == second)
662         {
663             ATAB[i] = rnd;
664             break;
665         }
666     }
667
668     return rnd;
669 }
670
671 void BUG(long num)
672 /*  The following conditions are currently considered fatal bugs.  Numbers < 20
673  *  are detected while reading the database; the others occur at "run time".
674  *      0       Message line > 70 characters
675  *      1       Null line in message
676  *      2       Too many words of messages
677  *      3       Too many travel options
678  *      4       Too many vocabulary words
679  *      5       Required vocabulary word not found
680  *      6       Too many RTEXT messages
681  *      7       Too many hints
682  *      8       Location has cond bit being set twice
683  *      9       Invalid section number in database
684  *      10      Too many locations
685  *      11      Too many class or turn messages
686  *      20      Special travel (500>L>300) exceeds goto list
687  *      21      Ran off end of vocabulary table
688  *      22      Vocabulary type (N/1000) not between 0 and 3
689  *      23      Intransitive action verb exceeds goto list
690  *      24      Transitive action verb exceeds goto list
691  *      25      Conditional travel entry with no alternative
692  *      26      Location has no travel entries
693  *      27      Hint number exceeds goto list
694  *      28      Invalid month returned by date function
695  *      29      Too many parameters given to SETPRM */
696 {
697
698     printf("Fatal error %ld.  See source code for interpretation.\n", num);
699     exit(0);
700 }
701
702 /*  Machine dependent routines (MAPLIN, TYPE, MPINIT, SAVEIO) */
703
704 void MAPLIN(FILE *fp)
705 {
706     long I, VAL;
707
708     /*  Read a line of input, from the specified input source,
709      *  translate the chars to integers in the range 0-126 and store
710      *  them in the common array "INLINE".  Integer values are as follows:
711      *     0   = space [ASCII CODE 40 octal, 32 decimal]
712      *    1-2  = !" [ASCII 41-42 octal, 33-34 decimal]
713      *    3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
714      *   11-36 = upper-case letters
715      *   37-62 = lower-case letters
716      *    63   = percent (%) [ASCII 45 octal, 37 decimal]
717      *   64-73 = digits, 0 through 9
718      *  Remaining characters can be translated any way that is convenient;
719      *  The "TYPE" routine below is used to map them back to characters when
720      *  necessary.  The above mappings are required so that certain special
721      *  characters are known to fit in 6 bits and/or can be easily spotted.
722      *  Array elements beyond the end of the line should be filled with 0,
723      *  and LNLENG should be set to the index of the last character.
724      *
725      *  If the data file uses a character other than space (e.g., tab) to
726      *  separate numbers, that character should also translate to 0.
727      *
728      *  This procedure may use the map1,map2 arrays to maintain static data for
729      *  the mapping.  MAP2(1) is set to 0 when the program starts
730      *  and is not changed thereafter unless the routines on this page choose
731      *  to do so. */
732
733     if(MAP2[1] == 0)MPINIT();
734
735     if (!oldstyle && fp == stdin)
736         fputs("> ", stdout);
737     do {
738         IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,fp));
739     } while
740             (!feof(fp) && rawbuf[0] == '#');
741     if (feof(fp)) {
742         if (logfp && fp == stdin)
743             fclose(logfp);
744     } else {
745         if (logfp && fp == stdin)
746             IGNORE(fputs(rawbuf, logfp));
747         else if (!isatty(0))
748             IGNORE(fputs(rawbuf, stdout));
749         strcpy(INLINE+1, rawbuf);
750         LNLENG=0;
751         for (I=1; I<=(long)sizeof(INLINE) && INLINE[I]!=0; I++) {
752             VAL=INLINE[I]+1;
753             INLINE[I]=MAP1[VAL];
754             if(INLINE[I] != 0)LNLENG=I;
755         } /* end loop */
756         LNPOSN=1;
757     }
758 }
759
760 void TYPE(void)
761 /*  Type the first "LNLENG" characters stored in inline, mapping them
762  *  from integers to text per the rules described above.  INLINE
763  *  may be changed by this routine. */
764 {
765     long i;
766
767     if(LNLENG == 0) {
768         printf("\n");
769         return;
770     }
771
772     if(MAP2[1] == 0)
773         MPINIT();
774     for (i=1; i<=LNLENG; i++) {
775         INLINE[i]=MAP2[INLINE[i]+1];
776     }
777     INLINE[LNLENG+1]=0;
778     printf("%s\n", INLINE+1);
779     return;
780 }
781
782 void MPINIT(void) 
783 {
784     long first, i, j, last, val;
785     static long RUNS[7][2] = { {32,34}, {39,46}, {65,90}, {97,122}, 
786                                {37,37}, {48,57}, {0,126} };
787     for (i=1; i<=128; i++) {
788         MAP1[i]= -1;
789     }
790     val=0;
791     for (i=0; i<7; i++) {
792         first =RUNS[i][0];
793         last = RUNS[i][1];
794         for (j=first; j<=last; j++) {
795             j++; 
796             if (MAP1[j] < 0) {
797                 MAP1[j]=val;
798                 ++val;
799             }       
800             j--;
801         }
802     }
803     MAP1[128]=MAP1[10];
804     /*  For this version, tab (9) maps to space (32), so del (127)
805      *  uses tab's value */
806     MAP1[10]=MAP1[33];
807     MAP1[11]=MAP1[33];
808
809     for (i=0; i<=126; i++) {
810         i++; val=MAP1[i]+1; i--;
811         MAP2[val] = i*('B'-'A');
812         if(i >= 64)
813             MAP2[val]=(i-64)*('B'-'A')+'@';
814     }
815 }
816
817 void fSAVEIO(long op, long in, long arr[]) 
818 /*  If OP=0, ask for a file name and open a file.  (If IN=true, the file is for
819  *  input, else output.)  If OP>0, read/write ARR from/into the previously-opened
820  *  file.  (ARR is a 250-integer array.)  If OP<0, finish reading/writing the
821  *  file.  (Finishing writing can be a no-op if a "stop" statement does it
822  *  automatically.  Finishing reading can be a no-op as long as a subsequent
823  *  SAVEIO(0,false,X) will still work.) */
824 {
825     static FILE *fp = NULL; 
826     char name[50];
827
828     switch (op < 0 ? -1 : (op > 0 ? 1 : 0)) 
829     { 
830     case -1:
831         fclose(fp);
832         break;
833     case 0:
834         while (fp == NULL) {
835             printf("\nFile name: ");
836             IGNORE(fgets(name, sizeof(name), stdin));
837             fp = fopen(name,(in ? READ_MODE : WRITE_MODE));
838             if(fp == NULL)
839                 printf("Can't open file %s, try again.\n", name); 
840         }
841         break;
842     case 1: 
843         if (in)
844             IGNORE(fread(arr,sizeof(long),250,fp));
845         else
846             IGNORE(fwrite(arr,sizeof(long),250,fp));
847         break;
848     }
849 }
850
851 void DATIME(long* d, long* t)
852 {
853     struct timeval tv;
854     gettimeofday(&tv, NULL);
855     *d = (long) tv.tv_sec;
856     *t = (long) tv.tv_usec;
857 }
858
859 long MOD(long n, long m) 
860 {
861     return(n%m);
862 }