Partially-working newspeak().
[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 <ctype.h>
7
8 #include "advent.h"
9 #include "database.h"
10 #include "linenoise/linenoise.h"
11 #include "newdb.h"
12
13 void* xmalloc(size_t size)
14 {
15   void* ptr = malloc(size);
16   if (ptr == NULL)
17     {
18       fprintf(stderr, "Out of memory!\n");
19       exit(EXIT_FAILURE);
20     }
21   return(ptr);
22 }
23
24 void packed_to_token(long packed, char token[6])
25 {
26   for (int i = 0; i < 5; ++i)
27     {
28       char advent = (packed >> i * 6) & 63;
29       token[i] = advent_to_ascii[advent];
30     }
31   token[5] = '\0';
32 }
33
34 /*  I/O routines (SPEAK, PSPEAK, RSPEAK, SETPRM, GETIN, YES) */
35
36 void newspeak(char* msg)
37 {
38   // Do nothing if we got a null pointer.
39   if (msg == NULL)
40     return;
41
42   // Do nothing if we got an empty string.
43   if (strlen(msg) == 0)
44     return;
45
46   // Print a newline if the global game.blklin says to.
47   if (game.blklin == true)
48     printf("\n");
49
50   // Create a copy of our string, so we can edit it.
51   char* copy = (char*) xmalloc(strlen(msg) + 1);
52   strncpy(copy, msg, strlen(msg) + 1);
53
54   // Staging area for parameters.
55   char parameters[2000][5];
56  
57   // Handle format specifiers (including the custom %C, %L, %S) by adjusting the parameter accordingly, and replacing the specifier with %s.
58   int param_index = 0;
59   for (int i = 0; i < strlen(msg); ++i)
60     {
61       if (msg[i] == '%')
62         {
63           ++param_index;
64
65           // Integer specifier. In order to accommodate the fact that PARMS can have both legitimate integers *and* packed tokens, stringify these. Future work may eliminate the need for this.
66           if (msg[i + 1] == 'd')
67             {
68               copy[i + 1] = 's';
69               sprintf(parameters[param_index], "%d", PARMS[param_index]);
70             }
71         }
72     }
73
74   // Render the final string.
75   char rendered[2000];
76   sprintf(&rendered, copy, parameters[1], parameters[2], parameters[3], parameters[4]);
77
78   // Print the message.
79   printf("%s\n", rendered);
80 }
81
82 void SPEAK(vocab_t msg)
83 /*  Print the message which starts at LINES[N].  Precede it with a blank line
84  *  unless game.blklin is false. */
85 {
86     long blank, casemake, i, nxt, neg, nparms, param, prmtyp, state;
87
88     if (msg == 0)
89         return;
90     blank=game.blklin;
91     nparms=1;
92     do {
93         nxt=labs(LINES[msg])-1;
94         ++msg;
95         LNLENG=0;
96         LNPOSN=1;
97         state=0;
98         for (i = msg; i <= nxt; i++) {
99             PUTTXT(LINES[i],&state,2);
100         }
101         LNPOSN=0;
102         ++LNPOSN;
103
104         while (LNPOSN <= LNLENG) { 
105             if (INLINE[LNPOSN] != ascii_to_advent['%']) {
106                 ++LNPOSN;
107                 continue;
108             }
109             prmtyp = INLINE[LNPOSN+1];
110             /*  A "%"; the next character determine the type of
111              *  parameter: 1 (!) = suppress message completely, 29 (S) = NULL
112              *  If PARAM=1, else 'S' (optional plural ending), 33 (W) = word
113              *  (two 30-bit values) with trailing spaces suppressed, 22 (L) or
114              *  31 (U) = word but map to lower/upper case, 13 (C) = word in
115              *  lower case with first letter capitalised, 65-73 (1-9) =
116              *  number using that many characters. */
117             if (prmtyp == ascii_to_advent['!'])
118                 return;
119             if (prmtyp == ascii_to_advent['S']) {
120                 SHFTXT(LNPOSN+2,-1);
121                 INLINE[LNPOSN] = ascii_to_advent['s'];
122                 if (PARMS[nparms] == 1)
123                     SHFTXT(LNPOSN+1,-1);
124                 ++nparms;
125                 continue;
126             }
127             if (prmtyp == ascii_to_advent['W'] || prmtyp == ascii_to_advent['L'] || prmtyp == ascii_to_advent['U'] || prmtyp == ascii_to_advent['C']) {
128                 SHFTXT(LNPOSN+2,-2);
129                 state = 0;
130                 casemake = -1;
131                 if (prmtyp == ascii_to_advent['U'])
132                     casemake=1;
133                 if (prmtyp == ascii_to_advent['W'])
134                     casemake=0;
135                 i = LNPOSN;
136                 PUTTXT(PARMS[nparms],&state,casemake);
137                 PUTTXT(PARMS[nparms+1],&state,casemake);
138                 if (prmtyp == ascii_to_advent['C'] && INLINE[i] >= ascii_to_advent['a'] && INLINE[i] <= ascii_to_advent['z'])
139                   {
140                     // Convert to uppercase.
141                     // Round-trip to ASCII and back so that this code doesn't break when the mapping changes.
142                     // This can be simplified when mapping goes away.
143                     char this = advent_to_ascii[INLINE[i]];
144                     char uc_this = toupper(this);
145                     INLINE[i] = ascii_to_advent[uc_this];
146                   }
147                 nparms += 2;
148                 continue;
149             }
150
151             prmtyp=prmtyp-64;
152             if (prmtyp < ascii_to_advent['!'] || prmtyp > ascii_to_advent['-']) {
153                 ++LNPOSN;
154                 continue;
155             }
156             SHFTXT(LNPOSN+2,prmtyp-2);
157             LNPOSN += prmtyp;
158             param=labs(PARMS[nparms]);
159             neg=0;
160             if (PARMS[nparms] < 0)
161                 neg=9;
162             for (i=1; i <= prmtyp; i++) {
163                 --LNPOSN;
164                 INLINE[LNPOSN]=MOD(param,10)+64;
165                 if (i != 1 && param == 0) {
166                     INLINE[LNPOSN]=neg;
167                     neg=0;
168                 }
169                 param=param/10;
170             }
171             LNPOSN += prmtyp;
172             ++nparms;
173             continue;
174         }
175
176         if (blank)
177             TYPE0();
178         blank=false;
179         TYPE();
180         msg = nxt + 1;
181     } while
182         (LINES[msg] >= 0);
183 }
184
185 void PSPEAK(vocab_t msg,int skip)
186 /*  Find the skip+1st message from msg and print it.  msg should be
187  *  the index of the inventory message for object.  (INVEN+N+1 message
188  *  is game.prop=N message). */
189 {
190   if (skip >= 0)
191     newspeak(object_descriptions[msg].longs[skip]);
192   else
193     newspeak(object_descriptions[msg].inventory);
194 }
195
196 void RSPEAK(vocab_t i)
197 /* Print the i-th "random" message (section 6 of database). */
198 {
199   newspeak(arbitrary_messages[i]);
200 }
201
202 void SETPRM(long first, long p1, long p2)
203 /*  Stores parameters into the PRMCOM parms array for use by speak.  P1 and P2
204  *  are stored into PARMS(first) and PARMS(first+1). */
205 {
206     if (first >= MAXPARMS)
207         BUG(29);
208     else {
209         PARMS[first] = p1;
210         PARMS[first+1] = p2;
211     }
212 }
213
214 bool GETIN(FILE *input,
215             long *pword1, long *pword1x, 
216             long *pword2, long *pword2x) 
217 /*  Get a command from the adventurer.  Snarf out the first word, pad it with
218  *  blanks, and return it in WORD1.  Chars 6 thru 10 are returned in WORD1X, in
219  *  case we need to print out the whole word in an error message.  Any number of
220  *  blanks may follow the word.  If a second word appears, it is returned in
221  *  WORD2 (chars 6 thru 10 in WORD2X), else WORD2 is -1. */
222 {
223     long junk;
224
225     for (;;) {
226         if (game.blklin)
227             TYPE0();
228         if (!MAPLIN(input))
229             return false;
230         *pword1=GETTXT(true,true,true);
231         if (game.blklin && *pword1 < 0)
232             continue;
233         *pword1x=GETTXT(false,true,true);
234         do {    
235             junk=GETTXT(false,true,true);
236         } while 
237             (junk > 0);
238         *pword2=GETTXT(true,true,true);
239         *pword2x=GETTXT(false,true,true);
240         do {
241             junk=GETTXT(false,true,true);
242         } while 
243             (junk > 0);
244         if (GETTXT(true,true,true) <= 0)
245             return true;
246         RSPEAK(53);
247     }
248 }
249
250 long YES(FILE *input, vocab_t x, vocab_t y, vocab_t z)
251 /*  Print message X, wait for yes/no answer.  If yes, print Y and return true;
252  *  if no, print Z and return false. */
253 {
254     token_t reply, junk1, junk2, junk3;
255
256     for (;;) {
257         RSPEAK(x);
258         GETIN(input, &reply, &junk1, &junk2, &junk3);
259         if (reply == MAKEWD(250519) || reply == MAKEWD(25)) {
260             RSPEAK(y);
261             return true;
262         }
263         if (reply == MAKEWD(1415) || reply == MAKEWD(14)) {
264             RSPEAK(z);
265             return false;
266         }
267         RSPEAK(185);
268     }
269 }
270
271 /*  Line-parsing routines (GETTXT, MAKEWD, PUTTXT, SHFTXT, TYPE0) */
272
273 long GETTXT(bool skip, bool onewrd, bool upper)
274 /*  Take characters from an input line and pack them into 30-bit words.
275  *  Skip says to skip leading blanks.  ONEWRD says stop if we come to a
276  *  blank.  UPPER says to map all letters to uppercase.  If we reach the
277  *  end of the line, the word is filled up with blanks (which encode as 0's).
278  *  If we're already at end of line when TEXT is called, we return -1. */
279 {
280     long text;
281     static long splitting = -1;
282
283     if (LNPOSN != splitting)
284         splitting = -1;
285     text= -1;
286     while (true) {
287         if (LNPOSN > LNLENG)
288             return(text);
289         if ((!skip) || INLINE[LNPOSN] != 0)
290             break;
291         ++LNPOSN;
292     }
293
294     text=0;
295     for (int I=1; I<=TOKLEN; I++) {
296         text=text*64;
297         if (LNPOSN > LNLENG || (onewrd && INLINE[LNPOSN] == 0))
298             continue;
299         char current=INLINE[LNPOSN];
300         if (current < ascii_to_advent['%']) {
301             splitting = -1;
302             if (upper && current >= ascii_to_advent['a'])
303                 current=current-26;
304             text=text+current;
305             ++LNPOSN;
306             continue;
307         }
308         if (splitting != LNPOSN) {
309             text=text+ascii_to_advent['%'];
310             splitting = LNPOSN;
311             continue;
312         }
313
314         text=text+current-ascii_to_advent['%'];
315         splitting = -1;
316         ++LNPOSN;
317     }
318
319     return text;
320 }
321
322 token_t MAKEWD(long letters)
323 /*  Combine TOKLEN (currently 5) uppercase letters (represented by
324  *  pairs of decimal digits in lettrs) to form a 30-bit value matching
325  *  the one that GETTXT would return given those characters plus
326  *  trailing blanks.  Caution: lettrs will overflow 31 bits if
327  *  5-letter word starts with V-Z.  As a kludgey workaround, you can
328  *  increment a letter by 5 by adding 50 to the next pair of
329  *  digits. */
330 {
331     long i = 1, word = 0;
332
333     for (long k=letters; k != 0; k=k/100) {
334         word=word+i*(MOD(k,50)+10);
335         i=i*64;
336         if (MOD(k,100) > 50)word=word+i*5;
337     }
338     i=64L*64L*64L*64L*64L/i;
339     word=word*i;
340     return word;
341 }
342
343 void PUTTXT(token_t word, long *state, long casemake)
344 /*  Unpack the 30-bit value in word to obtain up to TOKLEN (currently
345  *  5) integer-encoded chars, and store them in inline starting at
346  *  LNPOSN.  If LNLENG>=LNPOSN, shift existing characters to the right
347  *  to make room.  STATE will be zero when puttxt is called with the
348  *  first of a sequence of words, but is thereafter unchanged by the
349  *  caller, so PUTTXT can use it to maintain state across calls.
350  *  LNPOSN and LNLENG are incremented by the number of chars stored.
351  *  If CASEMAKE=1, all letters are made uppercase; if -1, lowercase; if 0,
352  *  as is.  any other value for case is the same as 0 but also causes
353  *  trailing blanks to be included (in anticipation of subsequent
354  *  additional text). */
355 {
356     long alph1, alph2, byte, div, i, w;
357
358     alph1=13*casemake+24;
359     alph2=26*labs(casemake)+alph1;
360     if (labs(casemake) > 1)
361         alph1=alph2;
362     /*  alph1&2 define range of wrong-case chars, 11-36 or 37-62 or empty. */
363     div=64L*64L*64L*64L;
364     w=word;
365     for (i=1; i<=TOKLEN; i++) 
366     {
367         if (w <= 0 && *state == 0 && labs(casemake) <= 1)
368             return;
369         byte=w/div;
370         w=(w-byte*div)*64;
371         if (!(*state != 0 || byte != ascii_to_advent['%'])) {
372             *state=ascii_to_advent['%'];
373             continue;
374         }
375         SHFTXT(LNPOSN,1);
376         *state=*state+byte;
377         if (*state < alph2 && *state >= alph1)*state=*state-26*casemake;
378         INLINE[LNPOSN]=*state;
379         ++LNPOSN;
380         *state=0;
381     }
382 }
383 #define PUTTXT(WORD,STATE,CASE) fPUTTXT(WORD,&STATE,CASE)
384
385 void SHFTXT(long from, long delta) 
386 /*  Move INLINE(N) to INLINE(N+DELTA) for N=FROM,LNLENG.  Delta can be
387  *  negative.  LNLENG is updated; LNPOSN is not changed. */
388 {
389     long I, k, j;
390
391     if (!(LNLENG < from || delta == 0)) {
392         for (I=from; I<=LNLENG; I++) {
393             k=I;
394             if (delta > 0)
395                 k=from+LNLENG-I;
396             j=k+delta;
397             INLINE[j]=INLINE[k];
398         }
399     }
400     LNLENG=LNLENG+delta;
401 }
402
403 void TYPE0(void)
404 /*  Type a blank line.  This procedure is provided as a convenience for callers
405  *  who otherwise have no use for MAPCOM. */
406 {
407     long temp;
408
409     temp=LNLENG;
410     LNLENG=0;
411     TYPE();
412     LNLENG=temp;
413     return;
414 }
415
416 /*  Data structure  routines */
417
418 long VOCAB(long id, long init) 
419 /*  Look up ID in the vocabulary (ATAB) and return its "definition" (KTAB), or
420  *  -1 if not found.  If INIT is positive, this is an initialisation call setting
421  *  up a keyword variable, and not finding it constitutes a bug.  It also means
422  *  that only KTAB values which taken over 1000 equal INIT may be considered.
423  *  (Thus "STEPS", which is a motion verb as well as an object, may be located
424  *  as an object.)  And it also means the KTAB value is taken modulo 1000. */
425 {
426     long i, lexeme;
427
428     for (i=1; i<=TABSIZ; i++) {
429         if (KTAB[i] == -1) {
430             lexeme= -1;
431             if (init < 0)
432                 return(lexeme);
433             BUG(5);
434         }
435         if (init >= 0 && KTAB[i]/1000 != init) 
436             continue;
437         if (ATAB[i] == id) {
438             lexeme=KTAB[i];
439             if (init >= 0)
440                 lexeme=MOD(lexeme,1000);
441             return(lexeme);
442         }
443     }
444     BUG(21);
445 }
446
447 void DSTROY(long object)
448 /*  Permanently eliminate "object" by moving to a non-existent location. */
449 {
450     MOVE(object,0);
451 }
452
453 void JUGGLE(long object)
454 /*  Juggle an object by picking it up and putting it down again, the purpose
455  *  being to get the object to the front of the chain of things at its loc. */
456 {
457     long i, j;
458
459     i=game.place[object];
460     j=game.fixed[object];
461     MOVE(object,i);
462     MOVE(object+NOBJECTS,j);
463 }
464
465 void MOVE(long object, long where)
466 /*  Place any object anywhere by picking it up and dropping it.  May
467  *  already be toting, in which case the carry is a no-op.  Mustn't
468  *  pick up objects which are not at any loc, since carry wants to
469  *  remove objects from game.atloc chains. */
470 {
471     long from;
472
473     if (object > NOBJECTS) 
474         from=game.fixed[object-NOBJECTS];
475     else
476         from=game.place[object];
477     if (from > 0 && from <= 300)
478         CARRY(object,from);
479     DROP(object,where);
480 }
481
482 long PUT(long object, long where, long pval)
483 /*  PUT is the same as MOVE, except it returns a value used to set up the
484  *  negated game.prop values for the repository objects. */
485 {
486     MOVE(object,where);
487     return (-1)-pval;;
488 }
489
490 void CARRY(long object, long where) 
491 /*  Start toting an object, removing it from the list of things at its former
492  *  location.  Incr holdng unless it was already being toted.  If object>NOBJECTS
493  *  (moving "fixed" second loc), don't change game.place or game.holdng. */
494 {
495     long temp;
496
497     if (object <= NOBJECTS) {
498         if (game.place[object] == -1)
499             return;
500         game.place[object]= -1;
501         ++game.holdng;
502     }
503     if (game.atloc[where] == object) {
504         game.atloc[where]=game.link[object];
505         return;
506     }
507     temp=game.atloc[where];
508     while (game.link[temp] != object) {
509         temp=game.link[temp];
510     }
511     game.link[temp]=game.link[object];
512 }
513
514 void DROP(long object, long where)
515 /*  Place an object at a given loc, prefixing it onto the game.atloc list.  Decr
516  *  game.holdng if the object was being toted. */
517 {
518     if (object > NOBJECTS)
519         game.fixed[object-NOBJECTS] = where;
520     else
521     {
522         if (game.place[object] == -1)
523             --game.holdng;
524         game.place[object] = where;
525     }
526     if (where <= 0)
527         return;
528     game.link[object] = game.atloc[where];
529     game.atloc[where] = object;
530 }
531
532 long ATDWRF(long where)
533 /*  Return the index of first dwarf at the given location, zero if no dwarf is
534  *  there (or if dwarves not active yet), -1 if all dwarves are dead.  Ignore
535  *  the pirate (6th dwarf). */
536 {
537     long at, i;
538
539     at =0;
540     if (game.dflag < 2)
541         return(at);
542     at = -1;
543     for (i=1; i<=NDWARVES-1; i++) {
544         if (game.dloc[i] == where)
545             return i;
546         if (game.dloc[i] != 0)
547             at=0;
548     }
549     return(at);
550 }
551
552 /*  Utility routines (SETBIT, TSTBIT, set_seed, get_next_lcg_value,
553  *  randrange, RNDVOC, BUG) */
554
555 long SETBIT(long bit)
556 /*  Returns 2**bit for use in constructing bit-masks. */
557 {
558     return(1 << bit);
559 }
560
561 bool TSTBIT(long mask, int bit)
562 /*  Returns true if the specified bit is set in the mask. */
563 {
564     return (mask & (1 << bit)) != 0;
565 }
566
567 void set_seed(long seedval)
568 /* Set the LCG seed */
569 {
570     lcgstate.x = (unsigned long) seedval % lcgstate.m;
571 }
572
573 unsigned long get_next_lcg_value(void)
574 /* Return the LCG's current value, and then iterate it. */
575 {
576     unsigned long old_x = lcgstate.x;
577     lcgstate.x = (lcgstate.a * lcgstate.x + lcgstate.c) % lcgstate.m;
578     return old_x;
579 }
580
581 long randrange(long range)
582 /* Return a random integer from [0, range). */
583 {
584     return range * get_next_lcg_value() / lcgstate.m;
585 }
586
587 long RNDVOC(long second, long force)
588 /*  Searches the vocabulary ATAB for a word whose second character is
589  *  char, and changes that word such that each of the other four
590  *  characters is a random letter.  If force is non-zero, it is used
591  *  as the new word.  Returns the new word. */
592 {
593     long rnd = force;
594
595     if (rnd == 0) {
596         for (int i = 1; i <= 5; i++) {
597             long j = 11 + randrange(26);
598             if (i == 2)
599                 j = second;
600             rnd = rnd * 64 + j;
601         }
602     }
603
604     long div = 64L * 64L * 64L;
605     for (int i = 1; i <= TABSIZ; i++) {
606         if (MOD(ATAB[i]/div, 64L) == second)
607         {
608             ATAB[i] = rnd;
609             break;
610         }
611     }
612
613     return rnd;
614 }
615
616 void BUG(long num)
617 /*  The following conditions are currently considered fatal bugs.  Numbers < 20
618  *  are detected while reading the database; the others occur at "run time".
619  *      0       Message line > 70 characters
620  *      1       Null line in message
621  *      2       Too many words of messages
622  *      3       Too many travel options
623  *      4       Too many vocabulary words
624  *      5       Required vocabulary word not found
625  *      6       Too many RTEXT messages
626  *      7       Too many hints
627  *      8       Location has cond bit being set twice
628  *      9       Invalid section number in database
629  *      10      Too many locations
630  *      11      Too many class or turn messages
631  *      20      Special travel (500>L>300) exceeds goto list
632  *      21      Ran off end of vocabulary table
633  *      22      Vocabulary type (N/1000) not between 0 and 3
634  *      23      Intransitive action verb exceeds goto list
635  *      24      Transitive action verb exceeds goto list
636  *      25      Conditional travel entry with no alternative
637  *      26      Location has no travel entries
638  *      27      Hint number exceeds goto list
639  *      28      Invalid month returned by date function
640  *      29      Too many parameters given to SETPRM */
641 {
642
643     printf("Fatal error %ld.  See source code for interpretation.\n", num);
644     exit(0);
645 }
646
647 /*  Machine dependent routines (MAPLIN, TYPE, SAVEIO) */
648
649 bool MAPLIN(FILE *fp)
650 {
651     long i, val;
652     bool eof;
653
654     /*  Read a line of input, from the specified input source,
655      *  translate the chars to integers in the range 0-126 and store
656      *  them in the common array "INLINE".  Integer values are as follows:
657      *     0   = space [ASCII CODE 40 octal, 32 decimal]
658      *    1-2  = !" [ASCII 41-42 octal, 33-34 decimal]
659      *    3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
660      *   11-36 = upper-case letters
661      *   37-62 = lower-case letters
662      *    63   = percent (%) [ASCII 45 octal, 37 decimal]
663      *   64-73 = digits, 0 through 9
664      *  Remaining characters can be translated any way that is convenient;
665      *  The "TYPE" routine below is used to map them back to characters when
666      *  necessary.  The above mappings are required so that certain special
667      *  characters are known to fit in 6 bits and/or can be easily spotted.
668      *  Array elements beyond the end of the line should be filled with 0,
669      *  and LNLENG should be set to the index of the last character.
670      *
671      *  If the data file uses a character other than space (e.g., tab) to
672      *  separate numbers, that character should also translate to 0.
673      *
674      *  This procedure may use the map1,map2 arrays to maintain static data for
675      *  the mapping.  MAP2(1) is set to 0 when the program starts
676      *  and is not changed thereafter unless the routines on this page choose
677      *  to do so. */
678
679     if (!oldstyle && !isatty(1))
680         fputs("> ", stdout);
681     do {
682         if (oldstyle) {
683             IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,fp));
684             eof = (feof(fp));
685         } else {
686             char *cp = linenoise("> ");
687             eof = (cp == NULL);
688             if (!eof) {
689                 strncpy(rawbuf, cp, sizeof(rawbuf)-1);
690                 linenoiseHistoryAdd(rawbuf);
691                 strncat(rawbuf, "\n", sizeof(rawbuf)-1);
692                 linenoiseFree(cp);
693             }
694         }
695     } while
696             (!eof && rawbuf[0] == '#');
697     if (eof) {
698         if (logfp && fp == stdin)
699             fclose(logfp);
700         return false;
701     } else {
702         if (logfp && fp == stdin)
703             IGNORE(fputs(rawbuf, logfp));
704         else if (!isatty(0))
705             IGNORE(fputs(rawbuf, stdout));
706         strcpy(INLINE+1, rawbuf);
707         LNLENG=0;
708         for (i=1; i<=(long)sizeof(INLINE) && INLINE[i]!=0; i++) {
709             val=INLINE[i];
710             INLINE[i]=ascii_to_advent[val];
711             if (INLINE[i] != 0)
712                 LNLENG=i;
713         }
714         LNPOSN=1;
715         return true;
716     }
717 }
718
719 void TYPE(void)
720 /*  Type the first "LNLENG" characters stored in inline, mapping them
721  *  from integers to text per the rules described above.  INLINE
722  *  may be changed by this routine. */
723 {
724     long i;
725
726     if (LNLENG == 0) {
727         printf("\n");
728         return;
729     }
730
731     for (i=1; i<=LNLENG; i++) {
732         INLINE[i]=advent_to_ascii[INLINE[i]];
733     }
734     INLINE[LNLENG+1]=0;
735     printf("%s\n", INLINE+1);
736     return;
737 }
738
739 void DATIME(long* d, long* t)
740 {
741     struct timeval tv;
742     gettimeofday(&tv, NULL);
743     *d = (long) tv.tv_sec;
744     *t = (long) tv.tv_usec;
745 }
746
747 long MOD(long n, long m) 
748 {
749     return(n%m);
750 }