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