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