fix for OS X (default ALLOC_RETURN_TYPE to void)
[mplib] / src / texk / web2c / mpdir / lib / mp.w
1 % $Id: mp.web,v 1.8 2005/08/24 10:54:02 taco Exp $
2 % MetaPost, by John Hobby.  Public domain.
3
4 % Much of this program was copied with permission from MF.web Version 1.9
5 % It interprets a language very similar to D.E. Knuth's METAFONT, but with
6 % changes designed to make it more suitable for PostScript output.
7
8 % TeX is a trademark of the American Mathematical Society.
9 % METAFONT is a trademark of Addison-Wesley Publishing Company.
10 % PostScript is a trademark of Adobe Systems Incorporated.
11
12 % Here is TeX material that gets inserted after \input webmac
13 \def\hang{\hangindent 3em\noindent\ignorespaces}
14 \def\textindent#1{\hangindent2.5em\noindent\hbox to2.5em{\hss#1 }\ignorespaces}
15 \def\ps{PostScript}
16 \def\psqrt#1{\sqrt{\mathstrut#1}}
17 \def\k{_{k+1}}
18 \def\pct!{{\char`\%}} % percent sign in ordinary text
19 \font\tenlogo=logo10 % font used for the METAFONT logo
20 \font\logos=logosl10
21 \def\MF{{\tenlogo META}\-{\tenlogo FONT}}
22 \def\MP{{\tenlogo META}\-{\tenlogo POST}}
23 \def\[#1]{\ignorespaces} % left over from pascal web
24 \def\<#1>{$\langle#1\rangle$}
25 \def\section{\mathhexbox278}
26 \let\swap=\leftrightarrow
27 \def\round{\mathop{\rm round}\nolimits}
28 \mathchardef\vb="026A % synonym for `\|'
29
30 \def\(#1){} % this is used to make section names sort themselves better
31 \def\9#1{} % this is used for sort keys in the index via @@:sort key}{entry@@>
32 \def\title{MetaPost}
33 \pdfoutput=1
34 \pageno=3
35
36 @* \[1] Introduction.
37
38 This is \MP, a graphics-language processor based on D. E. Knuth's \MF.
39
40 The main purpose of the following program is to explain the algorithms of \MP\
41 as clearly as possible. However, the program has been written so that it
42 can be tuned to run efficiently in a wide variety of operating environments
43 by making comparatively few changes. Such flexibility is possible because
44 the documentation that follows is written in the \.{WEB} language, which is
45 at a higher level than C.
46
47 A large piece of software like \MP\ has inherent complexity that cannot
48 be reduced below a certain level of difficulty, although each individual
49 part is fairly simple by itself. The \.{WEB} language is intended to make
50 the algorithms as readable as possible, by reflecting the way the
51 individual program pieces fit together and by providing the
52 cross-references that connect different parts. Detailed comments about
53 what is going on, and about why things were done in certain ways, have
54 been liberally sprinkled throughout the program.  These comments explain
55 features of the implementation, but they rarely attempt to explain the
56 \MP\ language itself, since the reader is supposed to be familiar with
57 {\sl The {\logos METAFONT\/}book} as well as the manual
58 @.WEB@>
59 @:METAFONTbook}{\sl The {\logos METAFONT\/}book@>
60 {\sl A User's Manual for MetaPost}, Computing Science Technical Report 162,
61 AT\AM T Bell Laboratories.
62
63 @ The present implementation is a preliminary version, but the possibilities
64 for new features are limited by the desire to remain as nearly compatible
65 with \MF\ as possible.
66
67 On the other hand, the \.{WEB} description can be extended without changing
68 the core of the program, and it has been designed so that such
69 extensions are not extremely difficult to make.
70 The |banner| string defined here should be changed whenever \MP\
71 undergoes any modifications, so that it will be clear which version of
72 \MP\ might be the guilty party when a problem arises.
73 @^extensions to \MP@>
74 @^system dependencies@>
75
76 @d banner "This is MetaPost, Version 1.004" /* printed when \MP\ starts */
77 @d metapost_version "1.004"
78 @d mplib_version "0.45"
79 @d version_string " (Cweb version 0.45)"
80
81 @d true 1
82 @d false 0
83
84 @ The external library header for \MP\ is |mplib.h|. It contains a
85 few typedefs and the header defintions for the externally used
86 fuctions.
87
88 The most important of the typedefs is the definition of the structure 
89 |MP_options|, that acts as a small, configurable front-end to the fairly 
90 large |MP_instance| structure.
91  
92 @(mplib.h@>=
93 typedef struct MP_instance * MP;
94 @<Exported types@>
95 typedef struct MP_options {
96   @<Option variables@>
97 } MP_options;
98 @<Exported function headers@>
99
100 @ The internal header file is much longer: it not only lists the complete
101 |MP_instance|, but also a lot of functions that have to be available to
102 the \ps\ backend, that is defined in a separate \.{WEB} file. 
103
104 The variables from |MP_options| are included inside the |MP_instance| 
105 wholesale.
106
107 @(mpmp.h@>=
108 #include <setjmp.h>
109 typedef struct psout_data_struct * psout_data;
110 typedef int boolean;
111 typedef signed int integer;
112 @<Declare helpers@>
113 @<Types in the outer block@>
114 @<Constants in the outer block@>
115 #  ifndef LIBAVL_ALLOCATOR
116 #    define LIBAVL_ALLOCATOR
117     struct libavl_allocator {
118         void *(*libavl_malloc) (struct libavl_allocator *, size_t libavl_size);
119         void (*libavl_free) (struct libavl_allocator *, void *libavl_block);
120     };
121 #  endif
122 typedef struct MP_instance {
123   @<Option variables@>
124   @<Global variables@>
125 } MP_instance;
126 @<Internal library declarations@>
127
128 @ @c 
129 #include <stdio.h>
130 #include <stdlib.h>
131 #include <string.h>
132 #include <stdarg.h>
133 #include <assert.h>
134 #include <unistd.h> /* for access() */
135 #include <time.h> /* for struct tm \& co */
136 #include "mplib.h"
137 #include "mpmp.h" /* internal header */
138 #include "mppsout.h" /* internal header */
139 @h
140 @<Declarations@>
141 @<Basic printing procedures@>
142 @<Error handling procedures@>
143
144 @ Here are the functions that set up the \MP\ instance.
145
146 @<Declarations@> =
147 @<Declare |mp_reallocate| functions@>
148 struct MP_options *mp_options (void);
149 MP mp_new (struct MP_options *opt);
150
151 @ @c
152 struct MP_options *mp_options (void) {
153   struct MP_options *opt;
154   opt = malloc(sizeof(MP_options));
155   if (opt!=NULL) {
156     memset (opt,0,sizeof(MP_options));
157   }
158   return opt;
159
160
161 @ The |__attribute__| pragma is gcc-only.
162
163 @<Internal library ... @>=
164 #if !defined(__GNUC__) || (__GNUC__ < 2)
165 # define __attribute__(x)
166 #endif
167
168 @ @c
169 MP __attribute__ ((noinline))
170 mp_new (struct MP_options *opt) {
171   MP mp;
172   mp = malloc(1*sizeof(MP_instance));
173   if (mp==NULL)
174         return mp;
175   @<Set |ini_version|@>;
176   @<Setup the non-local jump buffer in |mp_new|@>;
177   @<Allocate or initialize variables@>
178   if (opt->main_memory>mp->mem_max)
179     mp_reallocate_memory(mp,opt->main_memory);
180   mp_reallocate_paths(mp,1000);
181   mp_reallocate_fonts(mp,8);
182   return mp;
183 }
184
185 @ @c
186 void mp_free (MP mp) {
187   int k; /* loop variable */
188   @<Dealloc variables@>
189   xfree(mp);
190 }
191
192 @ @c
193 void  __attribute__((noinline))
194 mp_do_initialize ( MP mp) {
195   @<Local variables for initialization@>
196   @<Set initial values of key variables@>
197 }
198 int mp_initialize (MP mp) { /* this procedure gets things started properly */
199   mp->history=mp_fatal_error_stop; /* in case we quit during initialization */
200   @<Install and test the non-local jump buffer@>;
201   t_open_out; /* open the terminal for output */
202   @<Check the ``constant'' values...@>;
203   if ( mp->bad>0 ) {
204         char ss[256];
205     snprintf(ss,256,"Ouch---my internal constants have been clobbered!\n"
206                    "---case %i",(int)mp->bad);
207     do_fprintf(mp->err_out,(char *)ss);
208 @.Ouch...clobbered@>
209     return mp->history;
210   }
211   mp_do_initialize(mp); /* erase preloaded mem */
212   if (mp->ini_version) {
213     @<Run inimpost commands@>;
214   }
215   @<Initialize the output routines@>;
216   @<Get the first line of input and prepare to start@>;
217   mp_set_job_id(mp);
218   mp_init_map_file(mp, mp->troff_mode);
219   mp->history=mp_spotless; /* ready to go! */
220   if (mp->troff_mode) {
221     mp->internal[mp_gtroffmode]=unity; 
222     mp->internal[mp_prologues]=unity; 
223   }
224   if ( mp->start_sym>0 ) { /* insert the `\&{everyjob}' symbol */
225     mp->cur_sym=mp->start_sym; mp_back_input(mp);
226   }
227   return mp->history;
228 }
229
230
231 @<Exported function headers@>=
232 extern struct MP_options *mp_options (void);
233 extern MP mp_new (struct MP_options *opt) ;
234 extern void mp_free (MP mp);
235 extern int mp_initialize (MP mp);
236
237 @ The overall \MP\ program begins with the heading just shown, after which
238 comes a bunch of procedure declarations and function declarations.
239 Finally we will get to the main program, which begins with the
240 comment `|start_here|'. If you want to skip down to the
241 main program now, you can look up `|start_here|' in the index.
242 But the author suggests that the best way to understand this program
243 is to follow pretty much the order of \MP's components as they appear in the
244 \.{WEB} description you are now reading, since the present ordering is
245 intended to combine the advantages of the ``bottom up'' and ``top down''
246 approaches to the problem of understanding a somewhat complicated system.
247
248 @ Some of the code below is intended to be used only when diagnosing the
249 strange behavior that sometimes occurs when \MP\ is being installed or
250 when system wizards are fooling around with \MP\ without quite knowing
251 what they are doing. Such code will not normally be compiled; it is
252 delimited by the preprocessor test `|#ifdef DEBUG .. #endif|'.
253
254 @ This program has two important variations: (1) There is a long and slow
255 version called \.{INIMP}, which does the extra calculations needed to
256 @.INIMP@>
257 initialize \MP's internal tables; and (2)~there is a shorter and faster
258 production version, which cuts the initialization to a bare minimum.
259
260 Which is which is decided at runtime.
261
262 @ The following parameters can be changed at compile time to extend or
263 reduce \MP's capacity. They may have different values in \.{INIMP} and
264 in production versions of \MP.
265 @.INIMP@>
266 @^system dependencies@>
267
268 @<Constants...@>=
269 #define file_name_size 255 /* file names shouldn't be longer than this */
270 #define bistack_size 1500 /* size of stack for bisection algorithms;
271   should probably be left at this value */
272
273 @ Like the preceding parameters, the following quantities can be changed
274 at compile time to extend or reduce \MP's capacity. But if they are changed,
275 it is necessary to rerun the initialization program \.{INIMP}
276 @.INIMP@>
277 to generate new tables for the production \MP\ program.
278 One can't simply make helter-skelter changes to the following constants,
279 since certain rather complex initialization
280 numbers are computed from them. 
281
282 @ @<Glob...@>=
283 int max_strings; /* maximum number of strings; must not exceed |max_halfword| */
284 int pool_size; /* maximum number of characters in strings, including all
285   error messages and help texts, and the names of all identifiers */
286 int mem_max; /* greatest index in \MP's internal |mem| array;
287   must be strictly less than |max_halfword|;
288   must be equal to |mem_top| in \.{INIMP}, otherwise |>=mem_top| */
289 int mem_top; /* largest index in the |mem| array dumped by \.{INIMP};
290   must not be greater than |mem_max| */
291
292 @ @<Option variables@>=
293 int error_line; /* width of context lines on terminal error messages */
294 int half_error_line; /* width of first lines of contexts in terminal
295   error messages; should be between 30 and |error_line-15| */
296 int max_print_line; /* width of longest text lines output; should be at least 60 */
297 int hash_size; /* maximum number of symbolic tokens,
298   must be less than |max_halfword-3*param_size| */
299 int hash_prime; /* a prime number equal to about 85\pct! of |hash_size| */
300 int param_size; /* maximum number of simultaneous macro parameters */
301 int max_in_open; /* maximum number of input files and error insertions that
302   can be going on simultaneously */
303 int main_memory; /* only for options, to set up |mem_max| and |mem_top| */
304 void *userdata; /* this allows the calling application to setup local */
305
306
307 @d set_value(a,b,c) do { a=c; if (b>c) a=b; } while (0)
308
309 @<Allocate or ...@>=
310 mp->max_strings=500;
311 mp->pool_size=10000;
312 set_value(mp->error_line,opt->error_line,79);
313 set_value(mp->half_error_line,opt->half_error_line,50);
314 set_value(mp->max_print_line,opt->max_print_line,100);
315 mp->main_memory=5000;
316 mp->mem_max=5000;
317 mp->mem_top=5000;
318 set_value(mp->hash_size,opt->hash_size,9500);
319 set_value(mp->hash_prime,opt->hash_prime,7919);
320 set_value(mp->param_size,opt->param_size,150);
321 set_value(mp->max_in_open,opt->max_in_open,10);
322 mp->userdata=opt->userdata;
323
324 @ In case somebody has inadvertently made bad settings of the ``constants,''
325 \MP\ checks them using a global variable called |bad|.
326
327 This is the first of many sections of \MP\ where global variables are
328 defined.
329
330 @<Glob...@>=
331 integer bad; /* is some ``constant'' wrong? */
332
333 @ Later on we will say `\ignorespaces|if (mem_max>=max_halfword) bad=10;|',
334 or something similar. (We can't do that until |max_halfword| has been defined.)
335
336 @<Check the ``constant'' values for consistency@>=
337 mp->bad=0;
338 if ( (mp->half_error_line<30)||(mp->half_error_line>mp->error_line-15) ) mp->bad=1;
339 if ( mp->max_print_line<60 ) mp->bad=2;
340 if ( mp->mem_top<=1100 ) mp->bad=4;
341 if (mp->hash_prime>mp->hash_size ) mp->bad=5;
342
343 @ Some |goto| labels are used by the following definitions. The label
344 `|restart|' is occasionally used at the very beginning of a procedure; and
345 the label `|reswitch|' is occasionally used just prior to a |case|
346 statement in which some cases change the conditions and we wish to branch
347 to the newly applicable case.  Loops that are set up with the |loop|
348 construction defined below are commonly exited by going to `|done|' or to
349 `|found|' or to `|not_found|', and they are sometimes repeated by going to
350 `|continue|'.  If two or more parts of a subroutine start differently but
351 end up the same, the shared code may be gathered together at
352 `|common_ending|'.
353
354 @ Here are some macros for common programming idioms.
355
356 @d incr(A)   (A)=(A)+1 /* increase a variable by unity */
357 @d decr(A)   (A)=(A)-1 /* decrease a variable by unity */
358 @d negate(A) (A)=-(A) /* change the sign of a variable */
359 @d double(A) (A)=(A)+(A)
360 @d odd(A)   ((A)%2==1)
361 @d chr(A)   (A)
362 @d do_nothing   /* empty statement */
363 @d Return   goto exit /* terminate a procedure call */
364 @f return   nil /* \.{WEB} will henceforth say |return| instead of \\{return} */
365
366 @* \[2] The character set.
367 In order to make \MP\ readily portable to a wide variety of
368 computers, all of its input text is converted to an internal eight-bit
369 code that includes standard ASCII, the ``American Standard Code for
370 Information Interchange.''  This conversion is done immediately when each
371 character is read in. Conversely, characters are converted from ASCII to
372 the user's external representation just before they are output to a
373 text file.
374 @^ASCII code@>
375
376 Such an internal code is relevant to users of \MP\ only with respect to
377 the \&{char} and \&{ASCII} operations, and the comparison of strings.
378
379 @ Characters of text that have been converted to \MP's internal form
380 are said to be of type |ASCII_code|, which is a subrange of the integers.
381
382 @<Types...@>=
383 typedef unsigned char ASCII_code; /* eight-bit numbers */
384
385 @ The present specification of \MP\ has been written under the assumption
386 that the character set contains at least the letters and symbols associated
387 with ASCII codes 040 through 0176; all of these characters are now
388 available on most computer terminals.
389
390 We shall use the name |text_char| to stand for the data type of the characters 
391 that are converted to and from |ASCII_code| when they are input and output. 
392 We shall also assume that |text_char| consists of the elements 
393 |chr(first_text_char)| through |chr(last_text_char)|, inclusive. 
394 The following definitions should be adjusted if necessary.
395 @^system dependencies@>
396
397 @d first_text_char 0 /* ordinal number of the smallest element of |text_char| */
398 @d last_text_char 255 /* ordinal number of the largest element of |text_char| */
399
400 @<Types...@>=
401 typedef unsigned char text_char; /* the data type of characters in text files */
402
403 @ @<Local variables for init...@>=
404 integer i;
405
406 @ The \MP\ processor converts between ASCII code and
407 the user's external character set by means of arrays |xord| and |xchr|
408 that are analogous to Pascal's |ord| and |chr| functions.
409
410 @d xchr(A) mp->xchr[(A)]
411 @d xord(A) mp->xord[(A)]
412
413 @<Glob...@>=
414 ASCII_code xord[256];  /* specifies conversion of input characters */
415 text_char xchr[256];  /* specifies conversion of output characters */
416
417 @ The core system assumes all 8-bit is acceptable.  If it is not,
418 a change file has to alter the below section.
419 @^system dependencies@>
420
421 Additionally, people with extended character sets can
422 assign codes arbitrarily, giving an |xchr| equivalent to whatever
423 characters the users of \MP\ are allowed to have in their input files.
424 Appropriate changes to \MP's |char_class| table should then be made.
425 (Unlike \TeX, each installation of \MP\ has a fixed assignment of category
426 codes, called the |char_class|.) Such changes make portability of programs
427 more difficult, so they should be introduced cautiously if at all.
428 @^character set dependencies@>
429 @^system dependencies@>
430
431 @<Set initial ...@>=
432 for (i=0;i<=0377;i++) { xchr(i)=i; }
433
434 @ The following system-independent code makes the |xord| array contain a
435 suitable inverse to the information in |xchr|. Note that if |xchr[i]=xchr[j]|
436 where |i<j<0177|, the value of |xord[xchr[i]]| will turn out to be
437 |j| or more; hence, standard ASCII code numbers will be used instead of
438 codes below 040 in case there is a coincidence.
439
440 @<Set initial ...@>=
441 for (i=first_text_char;i<=last_text_char;i++) { 
442    xord(chr(i))=0177;
443 }
444 for (i=0200;i<=0377;i++) { xord(xchr(i))=i;}
445 for (i=0;i<=0176;i++) { xord(xchr(i))=i;}
446
447 @* \[3] Input and output.
448 The bane of portability is the fact that different operating systems treat
449 input and output quite differently, perhaps because computer scientists
450 have not given sufficient attention to this problem. People have felt somehow
451 that input and output are not part of ``real'' programming. Well, it is true
452 that some kinds of programming are more fun than others. With existing
453 input/output conventions being so diverse and so messy, the only sources of
454 joy in such parts of the code are the rare occasions when one can find a
455 way to make the program a little less bad than it might have been. We have
456 two choices, either to attack I/O now and get it over with, or to postpone
457 I/O until near the end. Neither prospect is very attractive, so let's
458 get it over with.
459
460 The basic operations we need to do are (1)~inputting and outputting of
461 text, to or from a file or the user's terminal; (2)~inputting and
462 outputting of eight-bit bytes, to or from a file; (3)~instructing the
463 operating system to initiate (``open'') or to terminate (``close'') input or
464 output from a specified file; (4)~testing whether the end of an input
465 file has been reached; (5)~display of bits on the user's screen.
466 The bit-display operation will be discussed in a later section; we shall
467 deal here only with more traditional kinds of I/O.
468
469 @ Finding files happens in a slightly roundabout fashion: the \MP\
470 instance object contains a field that holds a function pointer that finds a
471 file, and returns its name, or NULL. For this, it receives three
472 parameters: the non-qualified name |fname|, the intended |fopen|
473 operation type |fmode|, and the type of the file |ftype|.
474
475 The file types that are passed on in |ftype| can be  used to 
476 differentiate file searches if a library like kpathsea is used,
477 the fopen mode is passed along for the same reason.
478
479 @<Types...@>=
480 typedef unsigned char eight_bits ; /* unsigned one-byte quantity */
481
482 @ @<Exported types@>=
483 enum mp_filetype {
484   mp_filetype_terminal = 0, /* the terminal */
485   mp_filetype_error, /* the terminal */
486   mp_filetype_program , /* \MP\ language input */
487   mp_filetype_log,  /* the log file */
488   mp_filetype_postscript, /* the postscript output */
489   mp_filetype_memfile, /* memory dumps */
490   mp_filetype_metrics, /* TeX font metric files */
491   mp_filetype_fontmap, /* PostScript font mapping files */
492   mp_filetype_font, /*  PostScript type1 font programs */
493   mp_filetype_encoding, /*  PostScript font encoding files */
494   mp_filetype_text  /* first text file for readfrom and writeto primitives */
495 };
496 typedef char *(*mp_file_finder)(MP, const char *, const char *, int);
497 typedef void *(*mp_file_opener)(MP, const char *, const char *, int);
498 typedef char *(*mp_file_reader)(MP, void *, size_t *);
499 typedef void (*mp_binfile_reader)(MP, void *, void **, size_t *);
500 typedef void (*mp_file_closer)(MP, void *);
501 typedef int (*mp_file_eoftest)(MP, void *);
502 typedef void (*mp_file_flush)(MP, void *);
503 typedef void (*mp_file_writer)(MP, void *, const char *);
504 typedef void (*mp_binfile_writer)(MP, void *, void *, size_t);
505 #define NOTTESTING 1
506
507 @ @<Option variables@>=
508 mp_file_finder find_file;
509 mp_file_opener open_file;
510 mp_file_reader read_ascii_file;
511 mp_binfile_reader read_binary_file;
512 mp_file_closer close_file;
513 mp_file_eoftest eof_file;
514 mp_file_flush flush_file;
515 mp_file_writer write_ascii_file;
516 mp_binfile_writer write_binary_file;
517
518 @ The default function for finding files is |mp_find_file|. It is 
519 pretty stupid: it will only find files in the current directory.
520
521 This function may disappear altogether, it is currently only
522 used for the default font map file.
523
524 @c
525 char *mp_find_file (MP mp, const char *fname, const char *fmode, int ftype)  {
526   (void) mp;
527   if (fmode[0] != 'r' || (! access (fname,R_OK)) || ftype) {  
528      return strdup(fname);
529   }
530   return NULL;
531 }
532
533 @ This has to be done very early on, so it is best to put it in with
534 the |mp_new| allocations
535
536 @d set_callback_option(A) do { mp->A = mp_##A;
537   if (opt->A!=NULL) mp->A = opt->A;
538 } while (0)
539
540 @<Allocate or initialize ...@>=
541 set_callback_option(find_file);
542 set_callback_option(open_file);
543 set_callback_option(read_ascii_file);
544 set_callback_option(read_binary_file);
545 set_callback_option(close_file);
546 set_callback_option(eof_file);
547 set_callback_option(flush_file);
548 set_callback_option(write_ascii_file);
549 set_callback_option(write_binary_file);
550
551 @ Because |mp_find_file| is used so early, it has to be in the helpers
552 section.
553
554 @<Internal ...@>=
555 char *mp_find_file (MP mp, const char *fname, const char *fmode, int ftype) ;
556 void *mp_open_file (MP mp , const char *fname, const char *fmode, int ftype) ;
557 char *mp_read_ascii_file (MP mp, void *f, size_t *size) ;
558 void mp_read_binary_file (MP mp, void *f, void **d, size_t *size) ;
559 void mp_close_file (MP mp, void *f) ;
560 int mp_eof_file (MP mp, void *f) ;
561 void mp_flush_file (MP mp, void *f) ;
562 void mp_write_ascii_file (MP mp, void *f, const char *s) ;
563 void mp_write_binary_file (MP mp, void *f, void *s, size_t t) ;
564
565 @ The function to open files can now be very short.
566
567 @c
568 void *mp_open_file(MP mp, const char *fname, const char *fmode, int ftype)  {
569   char realmode[3];
570   (void) mp;
571   realmode[0] = *fmode;
572   realmode[1] = 'b';
573   realmode[2] = 0;
574 #if NOTTESTING
575   if (ftype==mp_filetype_terminal) {
576     return (fmode[0] == 'r' ? stdin : stdout);
577   } else if (ftype==mp_filetype_error) {
578     return stderr;
579   } else if (fname != NULL && (fmode[0] != 'r' || (! access (fname,R_OK)))) {
580     return (void *)fopen(fname, realmode);
581   }
582 #endif
583   return NULL;
584 }
585
586 @ This is a legacy interface: (almost) all file names pass through |name_of_file|.
587
588 @<Glob...@>=
589 char name_of_file[file_name_size+1]; /* the name of a system file */
590 int name_length;/* this many characters are actually
591   relevant in |name_of_file| (the rest are blank) */
592
593 @ @<Option variables@>=
594 int print_found_names; /* configuration parameter */
595
596 @ If this parameter is true, the terminal and log will report the found
597 file names for input files instead of the requested ones. 
598 It is off by default because it creates an extra filename lookup.
599
600 @<Allocate or initialize ...@>=
601 mp->print_found_names = (opt->print_found_names>0 ? true : false);
602
603 @ \MP's file-opening procedures return |false| if no file identified by
604 |name_of_file| could be opened.
605
606 The |OPEN_FILE| macro takes care of the |print_found_names| parameter.
607 It is not used for opening a mem file for read, because that file name 
608 is never printed.
609
610 @d OPEN_FILE(A) do {
611   if (mp->print_found_names) {
612     char *s = (mp->find_file)(mp,mp->name_of_file,A,ftype);
613     if (s!=NULL) {
614       *f = (mp->open_file)(mp,mp->name_of_file,A, ftype); 
615       strncpy(mp->name_of_file,s,file_name_size);
616       xfree(s);
617     } else {
618       *f = NULL;
619     }
620   } else {
621     *f = (mp->open_file)(mp,mp->name_of_file,A, ftype); 
622   }
623 } while (0);
624 return (*f ? true : false)
625
626 @c 
627 boolean mp_a_open_in (MP mp, void **f, int ftype) {
628   /* open a text file for input */
629   OPEN_FILE("r");
630 }
631 @#
632 boolean mp_w_open_in (MP mp, void **f) {
633   /* open a word file for input */
634   *f = (mp->open_file)(mp,mp->name_of_file,"r",mp_filetype_memfile); 
635   return (*f ? true : false);
636 }
637 @#
638 boolean mp_a_open_out (MP mp, void **f, int ftype) {
639   /* open a text file for output */
640   OPEN_FILE("w");
641 }
642 @#
643 boolean mp_b_open_out (MP mp, void **f, int ftype) {
644   /* open a binary file for output */
645   OPEN_FILE("w");
646 }
647 @#
648 boolean mp_w_open_out (MP mp, void **f) {
649   /* open a word file for output */
650   int ftype = mp_filetype_memfile;
651   OPEN_FILE("w");
652 }
653
654 @ @c
655 char *mp_read_ascii_file (MP mp, void *ff, size_t *size) {
656   int c;
657   size_t len = 0, lim = 128;
658   char *s = NULL;
659   FILE *f = (FILE *)ff;
660   *size = 0;
661   (void) mp; /* for -Wunused */
662   if (f==NULL)
663     return NULL;
664 #if NOTTESTING
665   c = fgetc(f);
666   if (c==EOF)
667     return NULL;
668   s = malloc(lim); 
669   if (s==NULL) return NULL;
670   while (c!=EOF && c!='\n' && c!='\r') { 
671     if (len==lim) {
672       s =realloc(s, (lim+(lim>>2)));
673       if (s==NULL) return NULL;
674       lim+=(lim>>2);
675     }
676         s[len++] = c;
677     c =fgetc(f);
678   }
679   if (c=='\r') {
680     c = fgetc(f);
681     if (c!=EOF && c!='\n')
682        ungetc(c,f);
683   }
684   s[len] = 0;
685   *size = len;
686 #endif
687   return s;
688 }
689
690 @ @c
691 void mp_write_ascii_file (MP mp, void *f, const char *s) {
692   (void) mp;
693 #if NOTTESTING
694   if (f!=NULL) {
695     fputs(s,(FILE *)f);
696   }
697 #endif
698 }
699
700 @ @c
701 void mp_read_binary_file (MP mp, void *f, void **data, size_t *size) {
702   size_t len = 0;
703   (void) mp;
704 #if NOTTESTING
705   if (f!=NULL)
706     len = fread(*data,1,*size,(FILE *)f);
707 #endif
708   *size = len;
709 }
710
711 @ @c
712 void mp_write_binary_file (MP mp, void *f, void *s, size_t size) {
713   (void) mp;
714 #if NOTTESTING
715   if (f!=NULL)
716     fwrite(s,size,1,(FILE *)f);
717 #endif
718 }
719
720
721 @ @c
722 void mp_close_file (MP mp, void *f) {
723   (void) mp;
724 #if NOTTESTING
725   if (f!=NULL)
726     fclose((FILE *)f);
727 #endif
728 }
729
730 @ @c
731 int mp_eof_file (MP mp, void *f) {
732   (void) mp;
733 #if NOTTESTING
734   if (f!=NULL)
735     return feof((FILE *)f);
736    else 
737     return 1;
738 #else
739   return 0;
740 #endif
741 }
742
743 @ @c
744 void mp_flush_file (MP mp, void *f) {
745   (void) mp;
746 #if NOTTESTING
747   if (f!=NULL)
748     fflush((FILE *)f);
749 #endif
750 }
751
752 @ Input from text files is read one line at a time, using a routine called
753 |input_ln|. This function is defined in terms of global variables called
754 |buffer|, |first|, and |last| that will be described in detail later; for
755 now, it suffices for us to know that |buffer| is an array of |ASCII_code|
756 values, and that |first| and |last| are indices into this array
757 representing the beginning and ending of a line of text.
758
759 @<Glob...@>=
760 size_t buf_size; /* maximum number of characters simultaneously present in
761                     current lines of open files */
762 ASCII_code *buffer; /* lines of characters being read */
763 size_t first; /* the first unused position in |buffer| */
764 size_t last; /* end of the line just input to |buffer| */
765 size_t max_buf_stack; /* largest index used in |buffer| */
766
767 @ @<Allocate or initialize ...@>=
768 mp->buf_size = 200;
769 mp->buffer = xmalloc((mp->buf_size+1),sizeof(ASCII_code));
770
771 @ @<Dealloc variables@>=
772 xfree(mp->buffer);
773
774 @ @c
775 void mp_reallocate_buffer(MP mp, size_t l) {
776   ASCII_code *buffer;
777   if (l>max_halfword) {
778     mp_confusion(mp,"buffer size"); /* can't happen (I hope) */
779   }
780   buffer = xmalloc((l+1),sizeof(ASCII_code));
781   memcpy(buffer,mp->buffer,(mp->buf_size+1));
782   xfree(mp->buffer);
783   mp->buffer = buffer ;
784   mp->buf_size = l;
785 }
786
787 @ The |input_ln| function brings the next line of input from the specified
788 field into available positions of the buffer array and returns the value
789 |true|, unless the file has already been entirely read, in which case it
790 returns |false| and sets |last:=first|.  In general, the |ASCII_code|
791 numbers that represent the next line of the file are input into
792 |buffer[first]|, |buffer[first+1]|, \dots, |buffer[last-1]|; and the
793 global variable |last| is set equal to |first| plus the length of the
794 line. Trailing blanks are removed from the line; thus, either |last=first|
795 (in which case the line was entirely blank) or |buffer[last-1]<>" "|.
796 @^inner loop@>
797
798 The variable |max_buf_stack|, which is used to keep track of how large
799 the |buf_size| parameter must be to accommodate the present job, is
800 also kept up to date by |input_ln|.
801
802 @c 
803 boolean mp_input_ln (MP mp, void *f ) {
804   /* inputs the next line or returns |false| */
805   char *s;
806   size_t size = 0; 
807   mp->last=mp->first; /* cf.\ Matthew 19\thinspace:\thinspace30 */
808   s = (mp->read_ascii_file)(mp,f, &size);
809   if (s==NULL)
810         return false;
811   if (size>0) {
812     mp->last = mp->first+size;
813     if ( mp->last>=mp->max_buf_stack ) { 
814       mp->max_buf_stack=mp->last+1;
815       while ( mp->max_buf_stack>=mp->buf_size ) {
816         mp_reallocate_buffer(mp,(mp->buf_size+(mp->buf_size>>2)));
817       }
818     }
819     memcpy((mp->buffer+mp->first),s,size);
820     /* while ( mp->buffer[mp->last]==' ' ) mp->last--; */
821   } 
822   free(s);
823   return true;
824 }
825
826 @ The user's terminal acts essentially like other files of text, except
827 that it is used both for input and for output. When the terminal is
828 considered an input file, the file variable is called |term_in|, and when it
829 is considered an output file the file variable is |term_out|.
830 @^system dependencies@>
831
832 @<Glob...@>=
833 void * term_in; /* the terminal as an input file */
834 void * term_out; /* the terminal as an output file */
835 void * err_out; /* the terminal as an output file */
836
837 @ Here is how to open the terminal files. In the default configuration,
838 nothing happens except that the command line (if there is one) is copied
839 to the input buffer.  The variable |command_line| will be filled by the 
840 |main| procedure. The copying can not be done earlier in the program 
841 logic because in the |INI| version, the |buffer| is also used for primitive 
842 initialization.
843
844 @^system dependencies@>
845
846 @d t_open_out  do {/* open the terminal for text output */
847     mp->term_out = (mp->open_file)(mp,"terminal", "w", mp_filetype_terminal);
848     mp->err_out = (mp->open_file)(mp,"error", "w", mp_filetype_error);
849 } while (0)
850 @d t_open_in  do { /* open the terminal for text input */
851     mp->term_in = (mp->open_file)(mp,"terminal", "r", mp_filetype_terminal);
852     if (mp->command_line!=NULL) {
853       mp->last = strlen(mp->command_line);
854       strncpy((char *)mp->buffer,mp->command_line,mp->last);
855       xfree(mp->command_line);
856     } else {
857           mp->last = 0;
858     }
859 } while (0)
860
861 @d t_close_out do { /* close the terminal */
862   (mp->close_file)(mp,mp->term_out);
863   (mp->close_file)(mp,mp->err_out);
864 } while (0)
865
866 @d t_close_in do { /* close the terminal */
867   (mp->close_file)(mp,mp->term_in);
868 } while (0)
869
870 @<Option variables@>=
871 char *command_line;
872
873 @ @<Allocate or initialize ...@>=
874 mp->command_line = xstrdup(opt->command_line);
875
876 @ Sometimes it is necessary to synchronize the input/output mixture that
877 happens on the user's terminal, and three system-dependent
878 procedures are used for this
879 purpose. The first of these, |update_terminal|, is called when we want
880 to make sure that everything we have output to the terminal so far has
881 actually left the computer's internal buffers and been sent.
882 The second, |clear_terminal|, is called when we wish to cancel any
883 input that the user may have typed ahead (since we are about to
884 issue an unexpected error message). The third, |wake_up_terminal|,
885 is supposed to revive the terminal if the user has disabled it by
886 some instruction to the operating system.  The following macros show how
887 these operations can be specified:
888 @^system dependencies@>
889
890 @d update_terminal  (mp->flush_file)(mp,mp->term_out) /* empty the terminal output buffer */
891 @d clear_terminal   do_nothing /* clear the terminal input buffer */
892 @d wake_up_terminal (mp->flush_file)(mp,mp->term_out) 
893                     /* cancel the user's cancellation of output */
894
895 @ We need a special routine to read the first line of \MP\ input from
896 the user's terminal. This line is different because it is read before we
897 have opened the transcript file; there is sort of a ``chicken and
898 egg'' problem here. If the user types `\.{input cmr10}' on the first
899 line, or if some macro invoked by that line does such an \.{input},
900 the transcript file will be named `\.{cmr10.log}'; but if no \.{input}
901 commands are performed during the first line of terminal input, the transcript
902 file will acquire its default name `\.{mpout.log}'. (The transcript file
903 will not contain error messages generated by the first line before the
904 first \.{input} command.)
905
906 The first line is even more special. It's nice to let the user start
907 running a \MP\ job by typing a command line like `\.{MP cmr10}'; in
908 such a case, \MP\ will operate as if the first line of input were
909 `\.{cmr10}', i.e., the first line will consist of the remainder of the
910 command line, after the part that invoked \MP.
911
912 @ Different systems have different ways to get started. But regardless of
913 what conventions are adopted, the routine that initializes the terminal
914 should satisfy the following specifications:
915
916 \yskip\textindent{1)}It should open file |term_in| for input from the
917   terminal. (The file |term_out| will already be open for output to the
918   terminal.)
919
920 \textindent{2)}If the user has given a command line, this line should be
921   considered the first line of terminal input. Otherwise the
922   user should be prompted with `\.{**}', and the first line of input
923   should be whatever is typed in response.
924
925 \textindent{3)}The first line of input, which might or might not be a
926   command line, should appear in locations |first| to |last-1| of the
927   |buffer| array.
928
929 \textindent{4)}The global variable |loc| should be set so that the
930   character to be read next by \MP\ is in |buffer[loc]|. This
931   character should not be blank, and we should have |loc<last|.
932
933 \yskip\noindent(It may be necessary to prompt the user several times
934 before a non-blank line comes in. The prompt is `\.{**}' instead of the
935 later `\.*' because the meaning is slightly different: `\.{input}' need
936 not be typed immediately after~`\.{**}'.)
937
938 @d loc mp->cur_input.loc_field /* location of first unread character in |buffer| */
939
940 @ The following program does the required initialization
941 without retrieving a possible command line.
942 It should be clear how to modify this routine to deal with command lines,
943 if the system permits them.
944 @^system dependencies@>
945
946 @c 
947 boolean mp_init_terminal (MP mp) { /* gets the terminal input started */
948   t_open_in; 
949   if (mp->last!=0) {
950     loc = mp->first = 0;
951         return true;
952   }
953   while (1) { 
954     if (!mp->noninteractive) {
955           wake_up_terminal; do_fprintf(mp->term_out,"**"); update_terminal;
956 @.**@>
957     }
958     if ( ! mp_input_ln(mp, mp->term_in ) ) { /* this shouldn't happen */
959       do_fprintf(mp->term_out,"\n! End of file on the terminal... why?");
960 @.End of file on the terminal@>
961       return false;
962     }
963     loc=mp->first;
964     while ( (loc<(int)mp->last)&&(mp->buffer[loc]==' ') ) 
965       incr(loc);
966     if ( loc<(int)mp->last ) { 
967       return true; /* return unless the line was all blank */
968     }
969     if (!mp->noninteractive) {
970           do_fprintf(mp->term_out,"Please type the name of your input file.\n");
971     }
972   }
973 }
974
975 @ @<Declarations@>=
976 boolean mp_init_terminal (MP mp) ;
977
978
979 @* \[4] String handling.
980 Symbolic token names and diagnostic messages are variable-length strings
981 of eight-bit characters. Many strings \MP\ uses are simply literals
982 in the compiled source, like the error messages and the names of the
983 internal parameters. Other strings are used or defined from the \MP\ input 
984 language, and these have to be interned.
985
986 \MP\ uses strings more extensively than \MF\ does, but the necessary
987 operations can still be handled with a fairly simple data structure.
988 The array |str_pool| contains all of the (eight-bit) ASCII codes in all
989 of the strings, and the array |str_start| contains indices of the starting
990 points of each string. Strings are referred to by integer numbers, so that
991 string number |s| comprises the characters |str_pool[j]| for
992 |str_start[s]<=j<str_start[ss]| where |ss=next_str[s]|.  The string pool
993 is allocated sequentially and |str_pool[pool_ptr]| is the next unused
994 location.  The first string number not currently in use is |str_ptr|
995 and |next_str[str_ptr]| begins a list of free string numbers.  String
996 pool entries |str_start[str_ptr]| up to |pool_ptr| are reserved for a
997 string currently being constructed.
998
999 String numbers 0 to 255 are reserved for strings that correspond to single
1000 ASCII characters. This is in accordance with the conventions of \.{WEB},
1001 @.WEB@>
1002 which converts single-character strings into the ASCII code number of the
1003 single character involved, while it converts other strings into integers
1004 and builds a string pool file. Thus, when the string constant \.{"."} appears
1005 in the program below, \.{WEB} converts it into the integer 46, which is the
1006 ASCII code for a period, while \.{WEB} will convert a string like \.{"hello"}
1007 into some integer greater than~255. String number 46 will presumably be the
1008 single character `\..'\thinspace; but some ASCII codes have no standard visible
1009 representation, and \MP\ may need to be able to print an arbitrary
1010 ASCII character, so the first 256 strings are used to specify exactly what
1011 should be printed for each of the 256 possibilities.
1012
1013 @<Types...@>=
1014 typedef int pool_pointer; /* for variables that point into |str_pool| */
1015 typedef int str_number; /* for variables that point into |str_start| */
1016
1017 @ @<Glob...@>=
1018 ASCII_code *str_pool; /* the characters */
1019 pool_pointer *str_start; /* the starting pointers */
1020 str_number *next_str; /* for linking strings in order */
1021 pool_pointer pool_ptr; /* first unused position in |str_pool| */
1022 str_number str_ptr; /* number of the current string being created */
1023 pool_pointer init_pool_ptr; /* the starting value of |pool_ptr| */
1024 str_number init_str_use; /* the initial number of strings in use */
1025 pool_pointer max_pool_ptr; /* the maximum so far of |pool_ptr| */
1026 str_number max_str_ptr; /* the maximum so far of |str_ptr| */
1027
1028 @ @<Allocate or initialize ...@>=
1029 mp->str_pool  = xmalloc ((mp->pool_size +1),sizeof(ASCII_code));
1030 mp->str_start = xmalloc ((mp->max_strings+1),sizeof(pool_pointer));
1031 mp->next_str  = xmalloc ((mp->max_strings+1),sizeof(str_number));
1032
1033 @ @<Dealloc variables@>=
1034 xfree(mp->str_pool);
1035 xfree(mp->str_start);
1036 xfree(mp->next_str);
1037
1038 @ Most printing is done from |char *|s, but sometimes not. Here are
1039 functions that convert an internal string into a |char *| for use
1040 by the printing routines, and vice versa.
1041
1042 @d str(A) mp_str(mp,A)
1043 @d rts(A) mp_rts(mp,A)
1044
1045 @<Internal ...@>=
1046 int mp_xstrcmp (const char *a, const char *b);
1047 char * mp_str (MP mp, str_number s);
1048
1049 @ @<Declarations@>=
1050 str_number mp_rts (MP mp, const char *s);
1051 str_number mp_make_string (MP mp);
1052
1053 @ The attempt to catch interrupted strings that is in |mp_rts|, is not 
1054 very good: it does not handle nesting over more than one level.
1055
1056 @c 
1057 int mp_xstrcmp (const char *a, const char *b) {
1058         if (a==NULL && b==NULL) 
1059           return 0;
1060     if (a==NULL)
1061       return -1;
1062     if (b==NULL)
1063       return 1;
1064     return strcmp(a,b);
1065 }
1066
1067 @ @c
1068 char * mp_str (MP mp, str_number ss) {
1069   char *s;
1070   int len;
1071   if (ss==mp->str_ptr) {
1072     return NULL;
1073   } else {
1074     len = length(ss);
1075     s = xmalloc(len+1,sizeof(char));
1076     strncpy(s,(char *)(mp->str_pool+(mp->str_start[ss])),len);
1077     s[len] = 0;
1078     return (char *)s;
1079   }
1080 }
1081 str_number mp_rts (MP mp, const char *s) {
1082   int r; /* the new string */ 
1083   int old; /* a possible string in progress */
1084   int i=0;
1085   if (strlen(s)==0) {
1086     return 256;
1087   } else if (strlen(s)==1) {
1088     return s[0];
1089   } else {
1090    old=0;
1091    str_room((integer)strlen(s));
1092    if (mp->str_start[mp->str_ptr]<mp->pool_ptr)
1093      old = mp_make_string(mp);
1094    while (*s) {
1095      append_char(*s);
1096      s++;
1097    }
1098    r = mp_make_string(mp);
1099    if (old!=0) {
1100       str_room(length(old));
1101       while (i<length(old)) {
1102         append_char((mp->str_start[old]+i));
1103       } 
1104       mp_flush_string(mp,old);
1105     }
1106     return r;
1107   }
1108 }
1109
1110 @ Except for |strs_used_up|, the following string statistics are only
1111 maintained when code between |stat| $\ldots$ |tats| delimiters is not
1112 commented out:
1113
1114 @<Glob...@>=
1115 integer strs_used_up; /* strings in use or unused but not reclaimed */
1116 integer pool_in_use; /* total number of cells of |str_pool| actually in use */
1117 integer strs_in_use; /* total number of strings actually in use */
1118 integer max_pl_used; /* maximum |pool_in_use| so far */
1119 integer max_strs_used; /* maximum |strs_in_use| so far */
1120
1121 @ Several of the elementary string operations are performed using \.{WEB}
1122 macros instead of functions, because many of the
1123 operations are done quite frequently and we want to avoid the
1124 overhead of procedure calls. For example, here is
1125 a simple macro that computes the length of a string.
1126 @.WEB@>
1127
1128 @d str_stop(A) mp->str_start[mp->next_str[(A)]] /* one cell past the end of string
1129   number \# */
1130 @d length(A) (str_stop((A))-mp->str_start[(A)]) /* the number of characters in string \# */
1131
1132 @ The length of the current string is called |cur_length|.  If we decide that
1133 the current string is not needed, |flush_cur_string| resets |pool_ptr| so that
1134 |cur_length| becomes zero.
1135
1136 @d cur_length   (mp->pool_ptr - mp->str_start[mp->str_ptr])
1137 @d flush_cur_string   mp->pool_ptr=mp->str_start[mp->str_ptr]
1138
1139 @ Strings are created by appending character codes to |str_pool|.
1140 The |append_char| macro, defined here, does not check to see if the
1141 value of |pool_ptr| has gotten too high; this test is supposed to be
1142 made before |append_char| is used.
1143
1144 To test if there is room to append |l| more characters to |str_pool|,
1145 we shall write |str_room(l)|, which tries to make sure there is enough room
1146 by compacting the string pool if necessary.  If this does not work,
1147 |do_compaction| aborts \MP\ and gives an apologetic error message.
1148
1149 @d append_char(A)   /* put |ASCII_code| \# at the end of |str_pool| */
1150 { mp->str_pool[mp->pool_ptr]=(A); incr(mp->pool_ptr);
1151 }
1152 @d str_room(A)   /* make sure that the pool hasn't overflowed */
1153   { if ( mp->pool_ptr+(A) > mp->max_pool_ptr ) {
1154     if ( mp->pool_ptr+(A) > mp->pool_size ) mp_do_compaction(mp, (A));
1155     else mp->max_pool_ptr=mp->pool_ptr+(A); }
1156   }
1157
1158 @ The following routine is similar to |str_room(1)| but it uses the
1159 argument |mp->pool_size| to prevent |do_compaction| from aborting when
1160 string space is exhausted.
1161
1162 @<Declare the procedure called |unit_str_room|@>=
1163 void mp_unit_str_room (MP mp);
1164
1165 @ @c
1166 void mp_unit_str_room (MP mp) { 
1167   if ( mp->pool_ptr>=mp->pool_size ) mp_do_compaction(mp, mp->pool_size);
1168   if ( mp->pool_ptr>=mp->max_pool_ptr ) mp->max_pool_ptr=mp->pool_ptr+1;
1169 }
1170
1171 @ \MP's string expressions are implemented in a brute-force way: Every
1172 new string or substring that is needed is simply copied into the string pool.
1173 Space is eventually reclaimed by a procedure called |do_compaction| with
1174 the aid of a simple system system of reference counts.
1175 @^reference counts@>
1176
1177 The number of references to string number |s| will be |str_ref[s]|. The
1178 special value |str_ref[s]=max_str_ref=127| is used to denote an unknown
1179 positive number of references; such strings will never be recycled. If
1180 a string is ever referred to more than 126 times, simultaneously, we
1181 put it in this category. Hence a single byte suffices to store each |str_ref|.
1182
1183 @d max_str_ref 127 /* ``infinite'' number of references */
1184 @d add_str_ref(A) { if ( mp->str_ref[(A)]<max_str_ref ) incr(mp->str_ref[(A)]);
1185   }
1186
1187 @<Glob...@>=
1188 int *str_ref;
1189
1190 @ @<Allocate or initialize ...@>=
1191 mp->str_ref = xmalloc ((mp->max_strings+1),sizeof(int));
1192
1193 @ @<Dealloc variables@>=
1194 xfree(mp->str_ref);
1195
1196 @ Here's what we do when a string reference disappears:
1197
1198 @d delete_str_ref(A)  { 
1199     if ( mp->str_ref[(A)]<max_str_ref ) {
1200        if ( mp->str_ref[(A)]>1 ) decr(mp->str_ref[(A)]); 
1201        else mp_flush_string(mp, (A));
1202     }
1203   }
1204
1205 @<Declare the procedure called |flush_string|@>=
1206 void mp_flush_string (MP mp,str_number s) ;
1207
1208
1209 @ We can't flush the first set of static strings at all, so there 
1210 is no point in trying
1211
1212 @c
1213 void mp_flush_string (MP mp,str_number s) { 
1214   if (length(s)>1) {
1215     mp->pool_in_use=mp->pool_in_use-length(s);
1216     decr(mp->strs_in_use);
1217     if ( mp->next_str[s]!=mp->str_ptr ) {
1218       mp->str_ref[s]=0;
1219     } else { 
1220       mp->str_ptr=s;
1221       decr(mp->strs_used_up);
1222     }
1223     mp->pool_ptr=mp->str_start[mp->str_ptr];
1224   }
1225 }
1226
1227 @ C literals cannot be simply added, they need to be set so they can't
1228 be flushed.
1229
1230 @d intern(A) mp_intern(mp,(A))
1231
1232 @c
1233 str_number mp_intern (MP mp, const char *s) {
1234   str_number r ;
1235   r = rts(s);
1236   mp->str_ref[r] = max_str_ref;
1237   return r;
1238 }
1239
1240 @ @<Declarations@>=
1241 str_number mp_intern (MP mp, const char *s);
1242
1243
1244 @ Once a sequence of characters has been appended to |str_pool|, it
1245 officially becomes a string when the function |make_string| is called.
1246 This function returns the identification number of the new string as its
1247 value.
1248
1249 When getting the next unused string number from the linked list, we pretend
1250 that
1251 $$ \hbox{|max_str_ptr+1|, |max_str_ptr+2|, $\ldots$, |mp->max_strings|} $$
1252 are linked sequentially even though the |next_str| entries have not been
1253 initialized yet.  We never allow |str_ptr| to reach |mp->max_strings|;
1254 |do_compaction| is responsible for making sure of this.
1255
1256 @<Declarations@>=
1257 @<Declare the procedure called |do_compaction|@>
1258 @<Declare the procedure called |unit_str_room|@>
1259 str_number mp_make_string (MP mp);
1260
1261 @ @c 
1262 str_number mp_make_string (MP mp) { /* current string enters the pool */
1263   str_number s; /* the new string */
1264 RESTART: 
1265   s=mp->str_ptr;
1266   mp->str_ptr=mp->next_str[s];
1267   if ( mp->str_ptr>mp->max_str_ptr ) {
1268     if ( mp->str_ptr==mp->max_strings ) { 
1269       mp->str_ptr=s;
1270       mp_do_compaction(mp, 0);
1271       goto RESTART;
1272     } else {
1273 #ifdef DEBUG 
1274       if ( mp->strs_used_up!=mp->max_str_ptr ) mp_confusion(mp, "s");
1275 @:this can't happen s}{\quad \.s@>
1276 #endif
1277       mp->max_str_ptr=mp->str_ptr;
1278       mp->next_str[mp->str_ptr]=mp->max_str_ptr+1;
1279     }
1280   }
1281   mp->str_ref[s]=1;
1282   mp->str_start[mp->str_ptr]=mp->pool_ptr;
1283   incr(mp->strs_used_up);
1284   incr(mp->strs_in_use);
1285   mp->pool_in_use=mp->pool_in_use+length(s);
1286   if ( mp->pool_in_use>mp->max_pl_used ) 
1287     mp->max_pl_used=mp->pool_in_use;
1288   if ( mp->strs_in_use>mp->max_strs_used ) 
1289     mp->max_strs_used=mp->strs_in_use;
1290   return s;
1291 }
1292
1293 @ The most interesting string operation is string pool compaction.  The idea
1294 is to recover unused space in the |str_pool| array by recopying the strings
1295 to close the gaps created when some strings become unused.  All string
1296 numbers~$k$ where |str_ref[k]=0| are to be linked into the list of free string
1297 numbers after |str_ptr|.  If this fails to free enough pool space we issue an
1298 |overflow| error unless |needed=mp->pool_size|.  Calling |do_compaction|
1299 with |needed=mp->pool_size| supresses all overflow tests.
1300
1301 The compaction process starts with |last_fixed_str| because all lower numbered
1302 strings are permanently allocated with |max_str_ref| in their |str_ref| entries.
1303
1304 @<Glob...@>=
1305 str_number last_fixed_str; /* last permanently allocated string */
1306 str_number fixed_str_use; /* number of permanently allocated strings */
1307
1308 @ @<Declare the procedure called |do_compaction|@>=
1309 void mp_do_compaction (MP mp, pool_pointer needed) ;
1310
1311 @ @c
1312 void mp_do_compaction (MP mp, pool_pointer needed) {
1313   str_number str_use; /* a count of strings in use */
1314   str_number r,s,t; /* strings being manipulated */
1315   pool_pointer p,q; /* destination and source for copying string characters */
1316   @<Advance |last_fixed_str| as far as possible and set |str_use|@>;
1317   r=mp->last_fixed_str;
1318   s=mp->next_str[r];
1319   p=mp->str_start[s];
1320   while ( s!=mp->str_ptr ) { 
1321     while ( mp->str_ref[s]==0 ) {
1322       @<Advance |s| and add the old |s| to the list of free string numbers;
1323         then |break| if |s=str_ptr|@>;
1324     }
1325     r=s; s=mp->next_str[s];
1326     incr(str_use);
1327     @<Move string |r| back so that |str_start[r]=p|; make |p| the location
1328      after the end of the string@>;
1329   }
1330   @<Move the current string back so that it starts at |p|@>;
1331   if ( needed<mp->pool_size ) {
1332     @<Make sure that there is room for another string with |needed| characters@>;
1333   }
1334   @<Account for the compaction and make sure the statistics agree with the
1335      global versions@>;
1336   mp->strs_used_up=str_use;
1337 }
1338
1339 @ @<Advance |last_fixed_str| as far as possible and set |str_use|@>=
1340 t=mp->next_str[mp->last_fixed_str];
1341 while (t!=mp->str_ptr && mp->str_ref[t]==max_str_ref) {
1342   incr(mp->fixed_str_use);
1343   mp->last_fixed_str=t;
1344   t=mp->next_str[t];
1345 }
1346 str_use=mp->fixed_str_use
1347
1348 @ Because of the way |flush_string| has been written, it should never be
1349 necessary to |break| here.  The extra line of code seems worthwhile to
1350 preserve the generality of |do_compaction|.
1351
1352 @<Advance |s| and add the old |s| to the list of free string numbers;...@>=
1353 {
1354 t=s;
1355 s=mp->next_str[s];
1356 mp->next_str[r]=s;
1357 mp->next_str[t]=mp->next_str[mp->str_ptr];
1358 mp->next_str[mp->str_ptr]=t;
1359 if ( s==mp->str_ptr ) break;
1360 }
1361
1362 @ The string currently starts at |str_start[r]| and ends just before
1363 |str_start[s]|.  We don't change |str_start[s]| because it might be needed
1364 to locate the next string.
1365
1366 @<Move string |r| back so that |str_start[r]=p|; make |p| the location...@>=
1367 q=mp->str_start[r];
1368 mp->str_start[r]=p;
1369 while ( q<mp->str_start[s] ) { 
1370   mp->str_pool[p]=mp->str_pool[q];
1371   incr(p); incr(q);
1372 }
1373
1374 @ Pointers |str_start[str_ptr]| and |pool_ptr| have not been updated.  When
1375 we do this, anything between them should be moved.
1376
1377 @ @<Move the current string back so that it starts at |p|@>=
1378 q=mp->str_start[mp->str_ptr];
1379 mp->str_start[mp->str_ptr]=p;
1380 while ( q<mp->pool_ptr ) { 
1381   mp->str_pool[p]=mp->str_pool[q];
1382   incr(p); incr(q);
1383 }
1384 mp->pool_ptr=p
1385
1386 @ We must remember that |str_ptr| is not allowed to reach |mp->max_strings|.
1387
1388 @<Make sure that there is room for another string with |needed| char...@>=
1389 if ( str_use>=mp->max_strings-1 )
1390   mp_reallocate_strings (mp,str_use);
1391 if ( mp->pool_ptr+needed>mp->max_pool_ptr ) {
1392   mp_reallocate_pool(mp, mp->pool_ptr+needed);
1393   mp->max_pool_ptr=mp->pool_ptr+needed;
1394 }
1395
1396 @ @<Declarations@>=
1397 void mp_reallocate_strings (MP mp, str_number str_use) ;
1398 void mp_reallocate_pool(MP mp, pool_pointer needed) ;
1399
1400 @ @c 
1401 void mp_reallocate_strings (MP mp, str_number str_use) { 
1402   while ( str_use>=mp->max_strings-1 ) {
1403     int l = mp->max_strings + (mp->max_strings>>2);
1404     XREALLOC (mp->str_ref,   l, int);
1405     XREALLOC (mp->str_start, l, pool_pointer);
1406     XREALLOC (mp->next_str,  l, str_number);
1407     mp->max_strings = l;
1408   }
1409 }
1410 void mp_reallocate_pool(MP mp, pool_pointer needed) {
1411   while ( needed>mp->pool_size ) {
1412     int l = mp->pool_size + (mp->pool_size>>2);
1413         XREALLOC (mp->str_pool, l, ASCII_code);
1414     mp->pool_size = l;
1415   }
1416 }
1417
1418 @ @<Account for the compaction and make sure the statistics agree with...@>=
1419 if ( (mp->str_start[mp->str_ptr]!=mp->pool_in_use)||(str_use!=mp->strs_in_use) )
1420   mp_confusion(mp, "string");
1421 @:this can't happen string}{\quad string@>
1422 incr(mp->pact_count);
1423 mp->pact_chars=mp->pact_chars+mp->pool_ptr-str_stop(mp->last_fixed_str);
1424 mp->pact_strs=mp->pact_strs+str_use-mp->fixed_str_use;
1425 #ifdef DEBUG
1426 s=mp->str_ptr; t=str_use;
1427 while ( s<=mp->max_str_ptr ){
1428   if ( t>mp->max_str_ptr ) mp_confusion(mp, "\"");
1429   incr(t); s=mp->next_str[s];
1430 };
1431 if ( t<=mp->max_str_ptr ) mp_confusion(mp, "\"");
1432 #endif
1433
1434 @ A few more global variables are needed to keep track of statistics when
1435 |stat| $\ldots$ |tats| blocks are not commented out.
1436
1437 @<Glob...@>=
1438 integer pact_count; /* number of string pool compactions so far */
1439 integer pact_chars; /* total number of characters moved during compactions */
1440 integer pact_strs; /* total number of strings moved during compactions */
1441
1442 @ @<Initialize compaction statistics@>=
1443 mp->pact_count=0;
1444 mp->pact_chars=0;
1445 mp->pact_strs=0;
1446
1447 @ The following subroutine compares string |s| with another string of the
1448 same length that appears in |buffer| starting at position |k|;
1449 the result is |true| if and only if the strings are equal.
1450
1451 @c 
1452 boolean mp_str_eq_buf (MP mp,str_number s, integer k) {
1453   /* test equality of strings */
1454   pool_pointer j; /* running index */
1455   j=mp->str_start[s];
1456   while ( j<str_stop(s) ) { 
1457     if ( mp->str_pool[j++]!=mp->buffer[k++] ) 
1458       return false;
1459   }
1460   return true;
1461 }
1462
1463 @ Here is a similar routine, but it compares two strings in the string pool,
1464 and it does not assume that they have the same length. If the first string
1465 is lexicographically greater than, less than, or equal to the second,
1466 the result is respectively positive, negative, or zero.
1467
1468 @c 
1469 integer mp_str_vs_str (MP mp, str_number s, str_number t) {
1470   /* test equality of strings */
1471   pool_pointer j,k; /* running indices */
1472   integer ls,lt; /* lengths */
1473   integer l; /* length remaining to test */
1474   ls=length(s); lt=length(t);
1475   if ( ls<=lt ) l=ls; else l=lt;
1476   j=mp->str_start[s]; k=mp->str_start[t];
1477   while ( l-->0 ) { 
1478     if ( mp->str_pool[j]!=mp->str_pool[k] ) {
1479        return (mp->str_pool[j]-mp->str_pool[k]); 
1480     }
1481     incr(j); incr(k);
1482   }
1483   return (ls-lt);
1484 }
1485
1486 @ The initial values of |str_pool|, |str_start|, |pool_ptr|,
1487 and |str_ptr| are computed by the \.{INIMP} program, based in part
1488 on the information that \.{WEB} has output while processing \MP.
1489 @.INIMP@>
1490 @^string pool@>
1491
1492 @c 
1493 void mp_get_strings_started (MP mp) { 
1494   /* initializes the string pool,
1495     but returns |false| if something goes wrong */
1496   int k; /* small indices or counters */
1497   str_number g; /* a new string */
1498   mp->pool_ptr=0; mp->str_ptr=0; mp->max_pool_ptr=0; mp->max_str_ptr=0;
1499   mp->str_start[0]=0;
1500   mp->next_str[0]=1;
1501   mp->pool_in_use=0; mp->strs_in_use=0;
1502   mp->max_pl_used=0; mp->max_strs_used=0;
1503   @<Initialize compaction statistics@>;
1504   mp->strs_used_up=0;
1505   @<Make the first 256 strings@>;
1506   g=mp_make_string(mp); /* string 256 == "" */
1507   mp->str_ref[g]=max_str_ref;
1508   mp->last_fixed_str=mp->str_ptr-1;
1509   mp->fixed_str_use=mp->str_ptr;
1510   return;
1511 }
1512
1513 @ @<Declarations@>=
1514 void mp_get_strings_started (MP mp);
1515
1516 @ The first 256 strings will consist of a single character only.
1517
1518 @<Make the first 256...@>=
1519 for (k=0;k<=255;k++) { 
1520   append_char(k);
1521   g=mp_make_string(mp); 
1522   mp->str_ref[g]=max_str_ref;
1523 }
1524
1525 @ The first 128 strings will contain 95 standard ASCII characters, and the
1526 other 33 characters will be printed in three-symbol form like `\.{\^\^A}'
1527 unless a system-dependent change is made here. Installations that have
1528 an extended character set, where for example |xchr[032]=@t\.{'^^Z'}@>|,
1529 would like string 032 to be printed as the single character 032 instead
1530 of the three characters 0136, 0136, 0132 (\.{\^\^Z}). On the other hand,
1531 even people with an extended character set will want to represent string
1532 015 by \.{\^\^M}, since 015 is ASCII's ``carriage return'' code; the idea is
1533 to produce visible strings instead of tabs or line-feeds or carriage-returns
1534 or bell-rings or characters that are treated anomalously in text files.
1535
1536 Unprintable characters of codes 128--255 are, similarly, rendered
1537 \.{\^\^80}--\.{\^\^ff}.
1538
1539 The boolean expression defined here should be |true| unless \MP\ internal
1540 code number~|k| corresponds to a non-troublesome visible symbol in the
1541 local character set.
1542 If character |k| cannot be printed, and |k<0200|, then character |k+0100| or
1543 |k-0100| must be printable; moreover, ASCII codes |[060..071, 0141..0146]|
1544 must be printable.
1545 @^character set dependencies@>
1546 @^system dependencies@>
1547
1548 @<Character |k| cannot be printed@>=
1549   (k<' ')||(k>'~')
1550
1551 @* \[5] On-line and off-line printing.
1552 Messages that are sent to a user's terminal and to the transcript-log file
1553 are produced by several `|print|' procedures. These procedures will
1554 direct their output to a variety of places, based on the setting of
1555 the global variable |selector|, which has the following possible
1556 values:
1557
1558 \yskip
1559 \hang |term_and_log|, the normal setting, prints on the terminal and on the
1560   transcript file.
1561
1562 \hang |log_only|, prints only on the transcript file.
1563
1564 \hang |term_only|, prints only on the terminal.
1565
1566 \hang |no_print|, doesn't print at all. This is used only in rare cases
1567   before the transcript file is open.
1568
1569 \hang |pseudo|, puts output into a cyclic buffer that is used
1570   by the |show_context| routine; when we get to that routine we shall discuss
1571   the reasoning behind this curious mode.
1572
1573 \hang |new_string|, appends the output to the current string in the
1574   string pool.
1575
1576 \hang |>=write_file| prints on one of the files used for the \&{write}
1577 @:write_}{\&{write} primitive@>
1578   command.
1579
1580 \yskip
1581 \noindent The symbolic names `|term_and_log|', etc., have been assigned
1582 numeric codes that satisfy the convenient relations |no_print+1=term_only|,
1583 |no_print+2=log_only|, |term_only+2=log_only+1=term_and_log|.  These
1584 relations are not used when |selector| could be |pseudo|, or |new_string|.
1585 We need not check for unprintable characters when |selector<pseudo|.
1586
1587 Three additional global variables, |tally|, |term_offset| and |file_offset|
1588 record the number of characters that have been printed
1589 since they were most recently cleared to zero. We use |tally| to record
1590 the length of (possibly very long) stretches of printing; |term_offset|,
1591 and |file_offset|, on the other hand, keep track of how many
1592 characters have appeared so far on the current line that has been output
1593 to the terminal, the transcript file, or the \ps\ output file, respectively.
1594
1595 @d new_string 0 /* printing is deflected to the string pool */
1596 @d pseudo 2 /* special |selector| setting for |show_context| */
1597 @d no_print 3 /* |selector| setting that makes data disappear */
1598 @d term_only 4 /* printing is destined for the terminal only */
1599 @d log_only 5 /* printing is destined for the transcript file only */
1600 @d term_and_log 6 /* normal |selector| setting */
1601 @d write_file 7 /* first write file selector */
1602
1603 @<Glob...@>=
1604 void * log_file; /* transcript of \MP\ session */
1605 void * ps_file; /* the generic font output goes here */
1606 unsigned int selector; /* where to print a message */
1607 unsigned char dig[23]; /* digits in a number being output */
1608 integer tally; /* the number of characters recently printed */
1609 unsigned int term_offset;
1610   /* the number of characters on the current terminal line */
1611 unsigned int file_offset;
1612   /* the number of characters on the current file line */
1613 ASCII_code *trick_buf; /* circular buffer for pseudoprinting */
1614 integer trick_count; /* threshold for pseudoprinting, explained later */
1615 integer first_count; /* another variable for pseudoprinting */
1616
1617 @ @<Allocate or initialize ...@>=
1618 memset(mp->dig,0,23);
1619 mp->trick_buf = xmalloc((mp->error_line+1),sizeof(ASCII_code));
1620
1621 @ @<Dealloc variables@>=
1622 xfree(mp->trick_buf);
1623
1624 @ @<Initialize the output routines@>=
1625 mp->selector=term_only; mp->tally=0; mp->term_offset=0; mp->file_offset=0; 
1626
1627 @ Macro abbreviations for output to the terminal and to the log file are
1628 defined here for convenience. Some systems need special conventions
1629 for terminal output, and it is possible to adhere to those conventions
1630 by changing |wterm|, |wterm_ln|, and |wterm_cr| here.
1631 @^system dependencies@>
1632
1633 @d do_fprintf(f,b) (mp->write_ascii_file)(mp,f,b)
1634 @d wterm(A)     do_fprintf(mp->term_out,(A))
1635 @d wterm_chr(A) { unsigned char ss[2]; ss[0]=(A); ss[1]=0; do_fprintf(mp->term_out,(char *)ss); }
1636 @d wterm_cr     do_fprintf(mp->term_out,"\n")
1637 @d wterm_ln(A)  { wterm_cr; do_fprintf(mp->term_out,(A)); }
1638 @d wlog(A)      do_fprintf(mp->log_file,(A))
1639 @d wlog_chr(A)  { unsigned char ss[2]; ss[0]=(A); ss[1]=0; do_fprintf(mp->log_file,(char *)ss); }
1640 @d wlog_cr      do_fprintf(mp->log_file, "\n")
1641 @d wlog_ln(A)   { wlog_cr; do_fprintf(mp->log_file,(A)); }
1642
1643
1644 @ To end a line of text output, we call |print_ln|.  Cases |0..max_write_files|
1645 use an array |wr_file| that will be declared later.
1646
1647 @d mp_print_text(A) mp_print_str(mp,text((A)))
1648
1649 @<Internal ...@>=
1650 void mp_print_ln (MP mp);
1651 void mp_print_visible_char (MP mp, ASCII_code s); 
1652 void mp_print_char (MP mp, ASCII_code k);
1653 void mp_print (MP mp, const char *s);
1654 void mp_print_str (MP mp, str_number s);
1655 void mp_print_nl (MP mp, const char *s);
1656 void mp_print_two (MP mp,scaled x, scaled y) ;
1657 void mp_print_scaled (MP mp,scaled s);
1658
1659 @ @<Basic print...@>=
1660 void mp_print_ln (MP mp) { /* prints an end-of-line */
1661  switch (mp->selector) {
1662   case term_and_log: 
1663     wterm_cr; wlog_cr;
1664     mp->term_offset=0;  mp->file_offset=0;
1665     break;
1666   case log_only: 
1667     wlog_cr; mp->file_offset=0;
1668     break;
1669   case term_only: 
1670     wterm_cr; mp->term_offset=0;
1671     break;
1672   case no_print:
1673   case pseudo: 
1674   case new_string: 
1675     break;
1676   default: 
1677     do_fprintf(mp->wr_file[(mp->selector-write_file)],"\n");
1678   }
1679 } /* note that |tally| is not affected */
1680
1681 @ The |print_visible_char| procedure sends one character to the desired
1682 destination, using the |xchr| array to map it into an external character
1683 compatible with |input_ln|.  (It assumes that it is always called with
1684 a visible ASCII character.)  All printing comes through |print_ln| or
1685 |print_char|, which ultimately calls |print_visible_char|, hence these
1686 routines are the ones that limit lines to at most |max_print_line| characters.
1687 But we must make an exception for the \ps\ output file since it is not safe
1688 to cut up lines arbitrarily in \ps.
1689
1690 Procedure |unit_str_room| needs to be declared |forward| here because it calls
1691 |do_compaction| and |do_compaction| can call the error routines.  Actually,
1692 |unit_str_room| avoids |overflow| errors but it can call |confusion|.
1693
1694 @<Basic printing...@>=
1695 void mp_print_visible_char (MP mp, ASCII_code s) { /* prints a single character */
1696   switch (mp->selector) {
1697   case term_and_log: 
1698     wterm_chr(xchr(s)); wlog_chr(xchr(s));
1699     incr(mp->term_offset); incr(mp->file_offset);
1700     if ( mp->term_offset==(unsigned)mp->max_print_line ) { 
1701        wterm_cr; mp->term_offset=0;
1702     };
1703     if ( mp->file_offset==(unsigned)mp->max_print_line ) { 
1704        wlog_cr; mp->file_offset=0;
1705     };
1706     break;
1707   case log_only: 
1708     wlog_chr(xchr(s)); incr(mp->file_offset);
1709     if ( mp->file_offset==(unsigned)mp->max_print_line ) mp_print_ln(mp);
1710     break;
1711   case term_only: 
1712     wterm_chr(xchr(s)); incr(mp->term_offset);
1713     if ( mp->term_offset==(unsigned)mp->max_print_line ) mp_print_ln(mp);
1714     break;
1715   case no_print: 
1716     break;
1717   case pseudo: 
1718     if ( mp->tally<mp->trick_count ) 
1719       mp->trick_buf[mp->tally % mp->error_line]=s;
1720     break;
1721   case new_string: 
1722     if ( mp->pool_ptr>=mp->max_pool_ptr ) { 
1723       mp_unit_str_room(mp);
1724       if ( mp->pool_ptr>=mp->pool_size ) 
1725         goto DONE; /* drop characters if string space is full */
1726     };
1727     append_char(s);
1728     break;
1729   default:
1730     { char ss[2]; ss[0] = xchr(s); ss[1]=0;
1731       do_fprintf(mp->wr_file[(mp->selector-write_file)],(char *)ss);
1732     }
1733   }
1734 DONE:
1735   incr(mp->tally);
1736 }
1737
1738 @ The |print_char| procedure sends one character to the desired destination.
1739 File names and string expressions might contain |ASCII_code| values that
1740 can't be printed using |print_visible_char|.  These characters will be
1741 printed in three- or four-symbol form like `\.{\^\^A}' or `\.{\^\^e4}'.
1742 (This procedure assumes that it is safe to bypass all checks for unprintable
1743 characters when |selector| is in the range |0..max_write_files-1|.
1744 The user might want to write unprintable characters.
1745
1746 @d print_lc_hex(A) do { l=(A);
1747     mp_print_visible_char(mp, (l<10 ? l+'0' : l-10+'a'));
1748   } while (0)
1749
1750 @<Basic printing...@>=
1751 void mp_print_char (MP mp, ASCII_code k) { /* prints a single character */
1752   int l; /* small index or counter */
1753   if ( mp->selector<pseudo || mp->selector>=write_file) {
1754     mp_print_visible_char(mp, k);
1755   } else if ( @<Character |k| cannot be printed@> ) { 
1756     mp_print(mp, "^^"); 
1757     if ( k<0100 ) { 
1758       mp_print_visible_char(mp, k+0100); 
1759     } else if ( k<0200 ) { 
1760       mp_print_visible_char(mp, k-0100); 
1761     } else { 
1762       print_lc_hex(k / 16);  
1763       print_lc_hex(k % 16); 
1764     }
1765   } else {
1766     mp_print_visible_char(mp, k);
1767   }
1768 }
1769
1770 @ An entire string is output by calling |print|. Note that if we are outputting
1771 the single standard ASCII character \.c, we could call |print("c")|, since
1772 |"c"=99| is the number of a single-character string, as explained above. But
1773 |print_char("c")| is quicker, so \MP\ goes directly to the |print_char|
1774 routine when it knows that this is safe. (The present implementation
1775 assumes that it is always safe to print a visible ASCII character.)
1776 @^system dependencies@>
1777
1778 @<Basic print...@>=
1779 void mp_do_print (MP mp, const char *ss, unsigned int len) { /* prints string |s| */
1780   unsigned int j = 0;
1781   while ( j<len ){ 
1782     mp_print_char(mp, ss[j]); incr(j);
1783   }
1784 }
1785
1786
1787 @<Basic print...@>=
1788 void mp_print (MP mp, const char *ss) {
1789   mp_do_print(mp, ss, strlen(ss));
1790 }
1791 void mp_print_str (MP mp, str_number s) {
1792   pool_pointer j; /* current character code position */
1793   if ( (s<0)||(s>mp->max_str_ptr) ) {
1794      mp_do_print(mp,"???",3); /* this can't happen */
1795 @.???@>
1796   }
1797   j=mp->str_start[s];
1798   mp_do_print(mp, (char *)(mp->str_pool+j), (str_stop(s)-j));
1799 }
1800
1801
1802 @ Here is the very first thing that \MP\ prints: a headline that identifies
1803 the version number and base name. The |term_offset| variable is temporarily
1804 incorrect, but the discrepancy is not serious since we assume that the banner
1805 and mem identifier together will occupy at most |max_print_line|
1806 character positions.
1807
1808 @<Initialize the output...@>=
1809 wterm (banner);
1810 wterm (version_string);
1811 if (mp->mem_ident!=NULL) 
1812   mp_print(mp,mp->mem_ident); 
1813 mp_print_ln(mp);
1814 update_terminal;
1815
1816 @ The procedure |print_nl| is like |print|, but it makes sure that the
1817 string appears at the beginning of a new line.
1818
1819 @<Basic print...@>=
1820 void mp_print_nl (MP mp, const char *s) { /* prints string |s| at beginning of line */
1821   switch(mp->selector) {
1822   case term_and_log: 
1823     if ( (mp->term_offset>0)||(mp->file_offset>0) ) mp_print_ln(mp);
1824     break;
1825   case log_only: 
1826     if ( mp->file_offset>0 ) mp_print_ln(mp);
1827     break;
1828   case term_only: 
1829     if ( mp->term_offset>0 ) mp_print_ln(mp);
1830     break;
1831   case no_print:
1832   case pseudo:
1833   case new_string: 
1834         break;
1835   } /* there are no other cases */
1836   mp_print(mp, s);
1837 }
1838
1839 @ An array of digits in the range |0..9| is printed by |print_the_digs|.
1840
1841 @<Basic print...@>=
1842 void mp_print_the_digs (MP mp, eight_bits k) {
1843   /* prints |dig[k-1]|$\,\ldots\,$|dig[0]| */
1844   while ( k>0 ){ 
1845     decr(k); mp_print_char(mp, '0'+mp->dig[k]);
1846   }
1847 }
1848
1849 @ The following procedure, which prints out the decimal representation of a
1850 given integer |n|, has been written carefully so that it works properly
1851 if |n=0| or if |(-n)| would cause overflow. It does not apply |%| or |/|
1852 to negative arguments, since such operations are not implemented consistently
1853 on all platforms.
1854
1855 @<Basic print...@>=
1856 void mp_print_int (MP mp,integer n) { /* prints an integer in decimal form */
1857   integer m; /* used to negate |n| in possibly dangerous cases */
1858   int k = 0; /* index to current digit; we assume that $|n|<10^{23}$ */
1859   if ( n<0 ) { 
1860     mp_print_char(mp, '-');
1861     if ( n>-100000000 ) {
1862           negate(n);
1863     } else  { 
1864           m=-1-n; n=m / 10; m=(m % 10)+1; k=1;
1865       if ( m<10 ) {
1866         mp->dig[0]=m;
1867       } else { 
1868         mp->dig[0]=0; incr(n);
1869       }
1870     }
1871   }
1872   do {  
1873     mp->dig[k]=n % 10; n=n / 10; incr(k);
1874   } while (n!=0);
1875   mp_print_the_digs(mp, k);
1876 }
1877
1878 @ @<Internal ...@>=
1879 void mp_print_int (MP mp,integer n);
1880
1881 @ \MP\ also makes use of a trivial procedure to print two digits. The
1882 following subroutine is usually called with a parameter in the range |0<=n<=99|.
1883
1884 @c 
1885 void mp_print_dd (MP mp,integer n) { /* prints two least significant digits */
1886   n=abs(n) % 100; 
1887   mp_print_char(mp, '0'+(n / 10));
1888   mp_print_char(mp, '0'+(n % 10));
1889 }
1890
1891
1892 @ @<Internal ...@>=
1893 void mp_print_dd (MP mp,integer n);
1894
1895 @ Here is a procedure that asks the user to type a line of input,
1896 assuming that the |selector| setting is either |term_only| or |term_and_log|.
1897 The input is placed into locations |first| through |last-1| of the
1898 |buffer| array, and echoed on the transcript file if appropriate.
1899
1900 This procedure is never called when |interaction<mp_scroll_mode|.
1901
1902 @d prompt_input(A) do { 
1903     if (!mp->noninteractive) {
1904       wake_up_terminal; mp_print(mp, (A)); 
1905     }
1906     mp_term_input(mp);
1907   } while (0) /* prints a string and gets a line of input */
1908
1909 @c 
1910 void mp_term_input (MP mp) { /* gets a line from the terminal */
1911   size_t k; /* index into |buffer| */
1912   update_terminal; /* Now the user sees the prompt for sure */
1913   if (!mp_input_ln(mp, mp->term_in )) {
1914     if (!mp->noninteractive) {
1915           mp_fatal_error(mp, "End of file on the terminal!");
1916 @.End of file on the terminal@>
1917     } else { /* we are done with this input chunk */
1918           longjmp(mp->jump_buf,1);      
1919     }
1920   }
1921   if (!mp->noninteractive) {
1922     mp->term_offset=0; /* the user's line ended with \<\rm return> */
1923     decr(mp->selector); /* prepare to echo the input */
1924     if ( mp->last!=mp->first ) {
1925       for (k=mp->first;k<=mp->last-1;k++) {
1926         mp_print_char(mp, mp->buffer[k]);
1927       }
1928     }
1929     mp_print_ln(mp); 
1930     mp->buffer[mp->last]='%'; 
1931     incr(mp->selector); /* restore previous status */
1932   }
1933 }
1934
1935 @* \[6] Reporting errors.
1936 When something anomalous is detected, \MP\ typically does something like this:
1937 $$\vbox{\halign{#\hfil\cr
1938 |print_err("Something anomalous has been detected");|\cr
1939 |help3("This is the first line of my offer to help.")|\cr
1940 |("This is the second line. I'm trying to")|\cr
1941 |("explain the best way for you to proceed.");|\cr
1942 |error;|\cr}}$$
1943 A two-line help message would be given using |help2|, etc.; these informal
1944 helps should use simple vocabulary that complements the words used in the
1945 official error message that was printed. (Outside the U.S.A., the help
1946 messages should preferably be translated into the local vernacular. Each
1947 line of help is at most 60 characters long, in the present implementation,
1948 so that |max_print_line| will not be exceeded.)
1949
1950 The |print_err| procedure supplies a `\.!' before the official message,
1951 and makes sure that the terminal is awake if a stop is going to occur.
1952 The |error| procedure supplies a `\..' after the official message, then it
1953 shows the location of the error; and if |interaction=error_stop_mode|,
1954 it also enters into a dialog with the user, during which time the help
1955 message may be printed.
1956 @^system dependencies@>
1957
1958 @ The global variable |interaction| has four settings, representing increasing
1959 amounts of user interaction:
1960
1961 @<Exported types@>=
1962 enum mp_interaction_mode { 
1963  mp_unspecified_mode=0, /* extra value for command-line switch */
1964  mp_batch_mode, /* omits all stops and omits terminal output */
1965  mp_nonstop_mode, /* omits all stops */
1966  mp_scroll_mode, /* omits error stops */
1967  mp_error_stop_mode /* stops at every opportunity to interact */
1968 };
1969
1970 @ @<Option variables@>=
1971 int interaction; /* current level of interaction */
1972 int noninteractive; /* do we have a terminal? */
1973
1974 @ Set it here so it can be overwritten by the commandline
1975
1976 @<Allocate or initialize ...@>=
1977 mp->interaction=opt->interaction;
1978 if (mp->interaction==mp_unspecified_mode || mp->interaction>mp_error_stop_mode) 
1979   mp->interaction=mp_error_stop_mode;
1980 if (mp->interaction<mp_unspecified_mode) 
1981   mp->interaction=mp_batch_mode;
1982 mp->noninteractive=opt->noninteractive;
1983
1984
1985
1986 @d print_err(A) mp_print_err(mp,(A))
1987
1988 @<Internal ...@>=
1989 void mp_print_err(MP mp, const char * A);
1990
1991 @ @c
1992 void mp_print_err(MP mp, const char * A) { 
1993   if ( mp->interaction==mp_error_stop_mode ) 
1994     wake_up_terminal;
1995   mp_print_nl(mp, "! "); 
1996   mp_print(mp, A);
1997 @.!\relax@>
1998 }
1999
2000
2001 @ \MP\ is careful not to call |error| when the print |selector| setting
2002 might be unusual. The only possible values of |selector| at the time of
2003 error messages are
2004
2005 \yskip\hang|no_print| (when |interaction=mp_batch_mode|
2006   and |log_file| not yet open);
2007
2008 \hang|term_only| (when |interaction>mp_batch_mode| and |log_file| not yet open);
2009
2010 \hang|log_only| (when |interaction=mp_batch_mode| and |log_file| is open);
2011
2012 \hang|term_and_log| (when |interaction>mp_batch_mode| and |log_file| is open).
2013
2014 @<Initialize the print |selector| based on |interaction|@>=
2015 if ( mp->interaction==mp_batch_mode ) mp->selector=no_print; else mp->selector=term_only
2016
2017 @ A global variable |deletions_allowed| is set |false| if the |get_next|
2018 routine is active when |error| is called; this ensures that |get_next|
2019 will never be called recursively.
2020 @^recursion@>
2021
2022 The global variable |history| records the worst level of error that
2023 has been detected. It has four possible values: |spotless|, |warning_issued|,
2024 |error_message_issued|, and |fatal_error_stop|.
2025
2026 Another global variable, |error_count|, is increased by one when an
2027 |error| occurs without an interactive dialog, and it is reset to zero at
2028 the end of every statement.  If |error_count| reaches 100, \MP\ decides
2029 that there is no point in continuing further.
2030
2031 @<Types...@>=
2032 enum mp_history_states {
2033   mp_spotless=0, /* |history| value when nothing has been amiss yet */
2034   mp_warning_issued, /* |history| value when |begin_diagnostic| has been called */
2035   mp_error_message_issued, /* |history| value when |error| has been called */
2036   mp_fatal_error_stop /* |history| value when termination was premature */
2037 };
2038
2039 @ @<Glob...@>=
2040 boolean deletions_allowed; /* is it safe for |error| to call |get_next|? */
2041 int history; /* has the source input been clean so far? */
2042 int error_count; /* the number of scrolled errors since the last statement ended */
2043
2044 @ The value of |history| is initially |fatal_error_stop|, but it will
2045 be changed to |spotless| if \MP\ survives the initialization process.
2046
2047 @<Allocate or ...@>=
2048 mp->deletions_allowed=true; mp->error_count=0; /* |history| is initialized elsewhere */
2049
2050 @ Since errors can be detected almost anywhere in \MP, we want to declare the
2051 error procedures near the beginning of the program. But the error procedures
2052 in turn use some other procedures, which need to be declared |forward|
2053 before we get to |error| itself.
2054
2055 It is possible for |error| to be called recursively if some error arises
2056 when |get_next| is being used to delete a token, and/or if some fatal error
2057 occurs while \MP\ is trying to fix a non-fatal one. But such recursion
2058 @^recursion@>
2059 is never more than two levels deep.
2060
2061 @<Declarations@>=
2062 void mp_get_next (MP mp);
2063 void mp_term_input (MP mp);
2064 void mp_show_context (MP mp);
2065 void mp_begin_file_reading (MP mp);
2066 void mp_open_log_file (MP mp);
2067 void mp_clear_for_error_prompt (MP mp);
2068 void mp_debug_help (MP mp);
2069 @<Declare the procedure called |flush_string|@>
2070
2071 @ @<Internal ...@>=
2072 void mp_normalize_selector (MP mp);
2073
2074 @ Individual lines of help are recorded in the array |help_line|, which
2075 contains entries in positions |0..(help_ptr-1)|. They should be printed
2076 in reverse order, i.e., with |help_line[0]| appearing last.
2077
2078 @d hlp1(A) mp->help_line[0]=(A); }
2079 @d hlp2(A) mp->help_line[1]=(A); hlp1
2080 @d hlp3(A) mp->help_line[2]=(A); hlp2
2081 @d hlp4(A) mp->help_line[3]=(A); hlp3
2082 @d hlp5(A) mp->help_line[4]=(A); hlp4
2083 @d hlp6(A) mp->help_line[5]=(A); hlp5
2084 @d help0 mp->help_ptr=0 /* sometimes there might be no help */
2085 @d help1  { mp->help_ptr=1; hlp1 /* use this with one help line */
2086 @d help2  { mp->help_ptr=2; hlp2 /* use this with two help lines */
2087 @d help3  { mp->help_ptr=3; hlp3 /* use this with three help lines */
2088 @d help4  { mp->help_ptr=4; hlp4 /* use this with four help lines */
2089 @d help5  { mp->help_ptr=5; hlp5 /* use this with five help lines */
2090 @d help6  { mp->help_ptr=6; hlp6 /* use this with six help lines */
2091
2092 @<Glob...@>=
2093 const char * help_line[6]; /* helps for the next |error| */
2094 unsigned int help_ptr; /* the number of help lines present */
2095 boolean use_err_help; /* should the |err_help| string be shown? */
2096 str_number err_help; /* a string set up by \&{errhelp} */
2097 str_number filename_template; /* a string set up by \&{filenametemplate} */
2098
2099 @ @<Allocate or ...@>=
2100 mp->help_ptr=0; mp->use_err_help=false; mp->err_help=0; mp->filename_template=0;
2101
2102 @ The |jump_out| procedure just cuts across all active procedure levels and
2103 goes to |end_of_MP|. This is the only nonlocal |goto| statement in the
2104 whole program. It is used when there is no recovery from a particular error.
2105
2106 The program uses a |jump_buf| to handle this, this is initialized at three
2107 spots: the start of |mp_new|, the start of |mp_initialize|, and the start 
2108 of |mp_run|. Those are the only library enty points.
2109
2110 @^system dependencies@>
2111
2112 @<Glob...@>=
2113 jmp_buf jump_buf;
2114
2115 @ @<Install and test the non-local jump buffer@>=
2116 if (setjmp(mp->jump_buf) != 0) { return mp->history; }
2117
2118 @ @<Setup the non-local jump buffer in |mp_new|@>=
2119 if (setjmp(mp->jump_buf) != 0) return NULL;
2120
2121
2122 @ If the array of internals is still |NULL| when |jump_out| is called, a
2123 crash occured during initialization, and it is not safe to run the normal
2124 cleanup routine.
2125
2126 @<Error hand...@>=
2127 void mp_jump_out (MP mp) { 
2128   if(mp->internal!=NULL)
2129     mp_close_files_and_terminate(mp);
2130   longjmp(mp->jump_buf,1);
2131 }
2132
2133 @ Here now is the general |error| routine.
2134
2135 @<Error hand...@>=
2136 void mp_error (MP mp) { /* completes the job of error reporting */
2137   ASCII_code c; /* what the user types */
2138   integer s1,s2,s3; /* used to save global variables when deleting tokens */
2139   pool_pointer j; /* character position being printed */
2140   if ( mp->history<mp_error_message_issued ) 
2141         mp->history=mp_error_message_issued;
2142   mp_print_char(mp, '.'); mp_show_context(mp);
2143   if ((!mp->noninteractive) && (mp->interaction==mp_error_stop_mode )) {
2144     @<Get user's advice and |return|@>;
2145   }
2146   incr(mp->error_count);
2147   if ( mp->error_count==100 ) { 
2148     mp_print_nl(mp,"(That makes 100 errors; please try again.)");
2149 @.That makes 100 errors...@>
2150     mp->history=mp_fatal_error_stop; mp_jump_out(mp);
2151   }
2152   @<Put help message on the transcript file@>;
2153 }
2154 void mp_warn (MP mp, const char *msg) {
2155   int saved_selector = mp->selector;
2156   mp_normalize_selector(mp);
2157   mp_print_nl(mp,"Warning: ");
2158   mp_print(mp,msg);
2159   mp_print_ln(mp);
2160   mp->selector = saved_selector;
2161 }
2162
2163 @ @<Exported function ...@>=
2164 void mp_error (MP mp);
2165 void mp_warn (MP mp, const char *msg);
2166
2167
2168 @ @<Get user's advice...@>=
2169 while (1) { 
2170 CONTINUE:
2171   mp_clear_for_error_prompt(mp); prompt_input("? ");
2172 @.?\relax@>
2173   if ( mp->last==mp->first ) return;
2174   c=mp->buffer[mp->first];
2175   if ( c>='a' ) c=c+'A'-'a'; /* convert to uppercase */
2176   @<Interpret code |c| and |return| if done@>;
2177 }
2178
2179 @ It is desirable to provide an `\.E' option here that gives the user
2180 an easy way to return from \MP\ to the system editor, with the offending
2181 line ready to be edited. But such an extension requires some system
2182 wizardry, so the present implementation simply types out the name of the
2183 file that should be
2184 edited and the relevant line number.
2185 @^system dependencies@>
2186
2187 @<Exported types@>=
2188 typedef void (*mp_run_editor_command)(MP, char *, int);
2189
2190 @ @<Option variables@>=
2191 mp_run_editor_command run_editor;
2192
2193 @ @<Allocate or initialize ...@>=
2194 set_callback_option(run_editor);
2195
2196 @ @<Declarations@>=
2197 void mp_run_editor (MP mp, char *fname, int fline);
2198
2199 @ @c void mp_run_editor (MP mp, char *fname, int fline) {
2200     mp_print_nl(mp, "You want to edit file ");
2201 @.You want to edit file x@>
2202     mp_print(mp, fname);
2203     mp_print(mp, " at line "); 
2204     mp_print_int(mp, fline);
2205     mp->interaction=mp_scroll_mode; 
2206     mp_jump_out(mp);
2207 }
2208
2209
2210 There is a secret `\.D' option available when the debugging routines haven't
2211 been commented~out.
2212 @^debugging@>
2213
2214 @<Interpret code |c| and |return| if done@>=
2215 switch (c) {
2216 case '0': case '1': case '2': case '3': case '4':
2217 case '5': case '6': case '7': case '8': case '9': 
2218   if ( mp->deletions_allowed ) {
2219     @<Delete |c-"0"| tokens and |continue|@>;
2220   }
2221   break;
2222 #ifdef DEBUG
2223 case 'D': 
2224   mp_debug_help(mp); continue; 
2225   break;
2226 #endif
2227 case 'E': 
2228   if ( mp->file_ptr>0 ){ 
2229     (mp->run_editor)(mp, 
2230                      str(mp->input_stack[mp->file_ptr].name_field), 
2231                      mp_true_line(mp));
2232   }
2233   break;
2234 case 'H': 
2235   @<Print the help information and |continue|@>;
2236   break;
2237 case 'I':
2238   @<Introduce new material from the terminal and |return|@>;
2239   break;
2240 case 'Q': case 'R': case 'S':
2241   @<Change the interaction level and |return|@>;
2242   break;
2243 case 'X':
2244   mp->interaction=mp_scroll_mode; mp_jump_out(mp);
2245   break;
2246 default:
2247   break;
2248 }
2249 @<Print the menu of available options@>
2250
2251 @ @<Print the menu...@>=
2252
2253   mp_print(mp, "Type <return> to proceed, S to scroll future error messages,");
2254 @.Type <return> to proceed...@>
2255   mp_print_nl(mp, "R to run without stopping, Q to run quietly,");
2256   mp_print_nl(mp, "I to insert something, ");
2257   if ( mp->file_ptr>0 ) 
2258     mp_print(mp, "E to edit your file,");
2259   if ( mp->deletions_allowed )
2260     mp_print_nl(mp, "1 or ... or 9 to ignore the next 1 to 9 tokens of input,");
2261   mp_print_nl(mp, "H for help, X to quit.");
2262 }
2263
2264 @ Here the author of \MP\ apologizes for making use of the numerical
2265 relation between |"Q"|, |"R"|, |"S"|, and the desired interaction settings
2266 |mp_batch_mode|, |mp_nonstop_mode|, |mp_scroll_mode|.
2267 @^Knuth, Donald Ervin@>
2268
2269 @<Change the interaction...@>=
2270
2271   mp->error_count=0; mp->interaction=mp_batch_mode+c-'Q';
2272   mp_print(mp, "OK, entering ");
2273   switch (c) {
2274   case 'Q': mp_print(mp, "batchmode"); decr(mp->selector); break;
2275   case 'R': mp_print(mp, "nonstopmode"); break;
2276   case 'S': mp_print(mp, "scrollmode"); break;
2277   } /* there are no other cases */
2278   mp_print(mp, "..."); mp_print_ln(mp); update_terminal; return;
2279 }
2280
2281 @ When the following code is executed, |buffer[(first+1)..(last-1)]| may
2282 contain the material inserted by the user; otherwise another prompt will
2283 be given. In order to understand this part of the program fully, you need
2284 to be familiar with \MP's input stacks.
2285
2286 @<Introduce new material...@>=
2287
2288   mp_begin_file_reading(mp); /* enter a new syntactic level for terminal input */
2289   if ( mp->last>mp->first+1 ) { 
2290     loc=mp->first+1; mp->buffer[mp->first]=' ';
2291   } else { 
2292    prompt_input("insert>"); loc=mp->first;
2293 @.insert>@>
2294   };
2295   mp->first=mp->last+1; mp->cur_input.limit_field=mp->last; return;
2296 }
2297
2298 @ We allow deletion of up to 99 tokens at a time.
2299
2300 @<Delete |c-"0"| tokens...@>=
2301
2302   s1=mp->cur_cmd; s2=mp->cur_mod; s3=mp->cur_sym; mp->OK_to_interrupt=false;
2303   if ( (mp->last>mp->first+1) && (mp->buffer[mp->first+1]>='0')&&(mp->buffer[mp->first+1]<='9') )
2304     c=c*10+mp->buffer[mp->first+1]-'0'*11;
2305   else 
2306     c=c-'0';
2307   while ( c>0 ) { 
2308     mp_get_next(mp); /* one-level recursive call of |error| is possible */
2309     @<Decrease the string reference count, if the current token is a string@>;
2310     decr(c);
2311   };
2312   mp->cur_cmd=s1; mp->cur_mod=s2; mp->cur_sym=s3; mp->OK_to_interrupt=true;
2313   help2("I have just deleted some text, as you asked.")
2314        ("You can now delete more, or insert, or whatever.");
2315   mp_show_context(mp); 
2316   goto CONTINUE;
2317 }
2318
2319 @ @<Print the help info...@>=
2320
2321   if ( mp->use_err_help ) { 
2322     @<Print the string |err_help|, possibly on several lines@>;
2323     mp->use_err_help=false;
2324   } else { 
2325     if ( mp->help_ptr==0 ) {
2326       help2("Sorry, I don't know how to help in this situation.")
2327            ("Maybe you should try asking a human?");
2328      }
2329     do { 
2330       decr(mp->help_ptr); mp_print(mp, mp->help_line[mp->help_ptr]); mp_print_ln(mp);
2331     } while (mp->help_ptr!=0);
2332   };
2333   help4("Sorry, I already gave what help I could...")
2334        ("Maybe you should try asking a human?")
2335        ("An error might have occurred before I noticed any problems.")
2336        ("``If all else fails, read the instructions.''");
2337   goto CONTINUE;
2338 }
2339
2340 @ @<Print the string |err_help|, possibly on several lines@>=
2341 j=mp->str_start[mp->err_help];
2342 while ( j<str_stop(mp->err_help) ) { 
2343   if ( mp->str_pool[j]!='%' ) mp_print_str(mp, mp->str_pool[j]);
2344   else if ( j+1==str_stop(mp->err_help) ) mp_print_ln(mp);
2345   else if ( mp->str_pool[j+1]!='%' ) mp_print_ln(mp);
2346   else  { incr(j); mp_print_char(mp, '%'); };
2347   incr(j);
2348 }
2349
2350 @ @<Put help message on the transcript file@>=
2351 if ( mp->interaction>mp_batch_mode ) decr(mp->selector); /* avoid terminal output */
2352 if ( mp->use_err_help ) { 
2353   mp_print_nl(mp, "");
2354   @<Print the string |err_help|, possibly on several lines@>;
2355 } else { 
2356   while ( mp->help_ptr>0 ){ 
2357     decr(mp->help_ptr); mp_print_nl(mp, mp->help_line[mp->help_ptr]);
2358   };
2359 }
2360 mp_print_ln(mp);
2361 if ( mp->interaction>mp_batch_mode ) incr(mp->selector); /* re-enable terminal output */
2362 mp_print_ln(mp)
2363
2364 @ In anomalous cases, the print selector might be in an unknown state;
2365 the following subroutine is called to fix things just enough to keep
2366 running a bit longer.
2367
2368 @c 
2369 void mp_normalize_selector (MP mp) { 
2370   if ( mp->log_opened ) mp->selector=term_and_log;
2371   else mp->selector=term_only;
2372   if ( mp->job_name==NULL ) mp_open_log_file(mp);
2373   if ( mp->interaction==mp_batch_mode ) decr(mp->selector);
2374 }
2375
2376 @ The following procedure prints \MP's last words before dying.
2377
2378 @d succumb { if ( mp->interaction==mp_error_stop_mode )
2379     mp->interaction=mp_scroll_mode; /* no more interaction */
2380   if ( mp->log_opened ) mp_error(mp);
2381   /*| if ( mp->interaction>mp_batch_mode ) mp_debug_help(mp); |*/
2382   mp->history=mp_fatal_error_stop; mp_jump_out(mp); /* irrecoverable error */
2383   }
2384
2385 @<Error hand...@>=
2386 void mp_fatal_error (MP mp, const char *s) { /* prints |s|, and that's it */
2387   mp_normalize_selector(mp);
2388   print_err("Emergency stop"); help1(s); succumb;
2389 @.Emergency stop@>
2390 }
2391
2392 @ @<Exported function ...@>=
2393 void mp_fatal_error (MP mp, const char *s);
2394
2395
2396 @ Here is the most dreaded error message.
2397
2398 @<Error hand...@>=
2399 void mp_overflow (MP mp, const char *s, integer n) { /* stop due to finiteness */
2400   mp_normalize_selector(mp);
2401   print_err("MetaPost capacity exceeded, sorry [");
2402 @.MetaPost capacity exceeded ...@>
2403   mp_print(mp, s); mp_print_char(mp, '='); mp_print_int(mp, n); mp_print_char(mp, ']');
2404   help2("If you really absolutely need more capacity,")
2405        ("you can ask a wizard to enlarge me.");
2406   succumb;
2407 }
2408
2409 @ @<Internal library declarations@>=
2410 void mp_overflow (MP mp, const char *s, integer n);
2411
2412 @ The program might sometime run completely amok, at which point there is
2413 no choice but to stop. If no previous error has been detected, that's bad
2414 news; a message is printed that is really intended for the \MP\
2415 maintenance person instead of the user (unless the user has been
2416 particularly diabolical).  The index entries for `this can't happen' may
2417 help to pinpoint the problem.
2418 @^dry rot@>
2419
2420 @<Internal library ...@>=
2421 void mp_confusion (MP mp, const char *s);
2422
2423 @ @<Error hand...@>=
2424 void mp_confusion (MP mp, const char *s) {
2425   /* consistency check violated; |s| tells where */
2426   mp_normalize_selector(mp);
2427   if ( mp->history<mp_error_message_issued ) { 
2428     print_err("This can't happen ("); mp_print(mp, s); mp_print_char(mp, ')');
2429 @.This can't happen@>
2430     help1("I'm broken. Please show this to someone who can fix can fix");
2431   } else { 
2432     print_err("I can\'t go on meeting you like this");
2433 @.I can't go on...@>
2434     help2("One of your faux pas seems to have wounded me deeply...")
2435          ("in fact, I'm barely conscious. Please fix it and try again.");
2436   }
2437   succumb;
2438 }
2439
2440 @ Users occasionally want to interrupt \MP\ while it's running.
2441 If the runtime system allows this, one can implement
2442 a routine that sets the global variable |interrupt| to some nonzero value
2443 when such an interrupt is signaled. Otherwise there is probably at least
2444 a way to make |interrupt| nonzero using the C debugger.
2445 @^system dependencies@>
2446 @^debugging@>
2447
2448 @d check_interrupt { if ( mp->interrupt!=0 )
2449    mp_pause_for_instructions(mp); }
2450
2451 @<Global...@>=
2452 integer interrupt; /* should \MP\ pause for instructions? */
2453 boolean OK_to_interrupt; /* should interrupts be observed? */
2454 integer run_state; /* are we processing input ?*/
2455
2456 @ @<Allocate or ...@>=
2457 mp->interrupt=0; mp->OK_to_interrupt=true; mp->run_state=0; 
2458
2459 @ When an interrupt has been detected, the program goes into its
2460 highest interaction level and lets the user have the full flexibility of
2461 the |error| routine.  \MP\ checks for interrupts only at times when it is
2462 safe to do this.
2463
2464 @c 
2465 void mp_pause_for_instructions (MP mp) { 
2466   if ( mp->OK_to_interrupt ) { 
2467     mp->interaction=mp_error_stop_mode;
2468     if ( (mp->selector==log_only)||(mp->selector==no_print) )
2469       incr(mp->selector);
2470     print_err("Interruption");
2471 @.Interruption@>
2472     help3("You rang?")
2473          ("Try to insert some instructions for me (e.g.,`I show x'),")
2474          ("unless you just want to quit by typing `X'.");
2475     mp->deletions_allowed=false; mp_error(mp); mp->deletions_allowed=true;
2476     mp->interrupt=0;
2477   }
2478 }
2479
2480 @ Many of \MP's error messages state that a missing token has been
2481 inserted behind the scenes. We can save string space and program space
2482 by putting this common code into a subroutine.
2483
2484 @c 
2485 void mp_missing_err (MP mp, const char *s) { 
2486   print_err("Missing `"); mp_print(mp, s); mp_print(mp, "' has been inserted");
2487 @.Missing...inserted@>
2488 }
2489
2490 @* \[7] Arithmetic with scaled numbers.
2491 The principal computations performed by \MP\ are done entirely in terms of
2492 integers less than $2^{31}$ in magnitude; thus, the arithmetic specified in this
2493 program can be carried out in exactly the same way on a wide variety of
2494 computers, including some small ones.
2495 @^small computers@>
2496
2497 But C does not rigidly define the |/| operation in the case of negative
2498 dividends; for example, the result of |(-2*n-1) / 2| is |-(n+1)| on some
2499 computers and |-n| on others (is this true ?).  There are two principal
2500 types of arithmetic: ``translation-preserving,'' in which the identity
2501 |(a+q*b)/b=(a/b)+q| is valid; and ``negation-preserving,'' in which
2502 |(-a)/b=-(a/b)|. This leads to two \MP s, which can produce
2503 different results, although the differences should be negligible when the
2504 language is being used properly.  The \TeX\ processor has been defined
2505 carefully so that both varieties of arithmetic will produce identical
2506 output, but it would be too inefficient to constrain \MP\ in a similar way.
2507
2508 @d el_gordo   017777777777 /* $2^{31}-1$, the largest value that \MP\ likes */
2509
2510 @ One of \MP's most common operations is the calculation of
2511 $\lfloor{a+b\over2}\rfloor$,
2512 the midpoint of two given integers |a| and~|b|. The most decent way to do
2513 this is to write `|(a+b)/2|'; but on many machines it is more efficient 
2514 to calculate `|(a+b)>>1|'.
2515
2516 Therefore the midpoint operation will always be denoted by `|half(a+b)|'
2517 in this program. If \MP\ is being implemented with languages that permit
2518 binary shifting, the |half| macro should be changed to make this operation
2519 as efficient as possible.  Since some systems have shift operators that can
2520 only be trusted to work on positive numbers, there is also a macro |halfp|
2521 that is used only when the quantity being halved is known to be positive
2522 or zero.
2523
2524 @d half(A) ((A) / 2)
2525 @d halfp(A) ((A) >> 1)
2526
2527 @ A single computation might use several subroutine calls, and it is
2528 desirable to avoid producing multiple error messages in case of arithmetic
2529 overflow. So the routines below set the global variable |arith_error| to |true|
2530 instead of reporting errors directly to the user.
2531 @^overflow in arithmetic@>
2532
2533 @<Glob...@>=
2534 boolean arith_error; /* has arithmetic overflow occurred recently? */
2535
2536 @ @<Allocate or ...@>=
2537 mp->arith_error=false;
2538
2539 @ At crucial points the program will say |check_arith|, to test if
2540 an arithmetic error has been detected.
2541
2542 @d check_arith { if ( mp->arith_error ) mp_clear_arith(mp); }
2543
2544 @c 
2545 void mp_clear_arith (MP mp) { 
2546   print_err("Arithmetic overflow");
2547 @.Arithmetic overflow@>
2548   help4("Uh, oh. A little while ago one of the quantities that I was")
2549        ("computing got too large, so I'm afraid your answers will be")
2550        ("somewhat askew. You'll probably have to adopt different")
2551        ("tactics next time. But I shall try to carry on anyway.");
2552   mp_error(mp); 
2553   mp->arith_error=false;
2554 }
2555
2556 @ Addition is not always checked to make sure that it doesn't overflow,
2557 but in places where overflow isn't too unlikely the |slow_add| routine
2558 is used.
2559
2560 @c integer mp_slow_add (MP mp,integer x, integer y) { 
2561   if ( x>=0 )  {
2562     if ( y<=el_gordo-x ) { 
2563       return x+y;
2564     } else  { 
2565       mp->arith_error=true; 
2566           return el_gordo;
2567     }
2568   } else  if ( -y<=el_gordo+x ) {
2569     return x+y;
2570   } else { 
2571     mp->arith_error=true; 
2572         return -el_gordo;
2573   }
2574 }
2575
2576 @ Fixed-point arithmetic is done on {\sl scaled integers\/} that are multiples
2577 of $2^{-16}$. In other words, a binary point is assumed to be sixteen bit
2578 positions from the right end of a binary computer word.
2579
2580 @d quarter_unit   040000 /* $2^{14}$, represents 0.250000 */
2581 @d half_unit   0100000 /* $2^{15}$, represents 0.50000 */
2582 @d three_quarter_unit   0140000 /* $3\cdot2^{14}$, represents 0.75000 */
2583 @d unity   0200000 /* $2^{16}$, represents 1.00000 */
2584 @d two   0400000 /* $2^{17}$, represents 2.00000 */
2585 @d three   0600000 /* $2^{17}+2^{16}$, represents 3.00000 */
2586
2587 @<Types...@>=
2588 typedef integer scaled; /* this type is used for scaled integers */
2589 typedef unsigned char small_number; /* this type is self-explanatory */
2590
2591 @ The following function is used to create a scaled integer from a given decimal
2592 fraction $(.d_0d_1\ldots d_{k-1})$, where |0<=k<=17|. The digit $d_i$ is
2593 given in |dig[i]|, and the calculation produces a correctly rounded result.
2594
2595 @c 
2596 scaled mp_round_decimals (MP mp,small_number k) {
2597   /* converts a decimal fraction */
2598  integer a = 0; /* the accumulator */
2599  while ( k-->0 ) { 
2600     a=(a+mp->dig[k]*two) / 10;
2601   }
2602   return halfp(a+1);
2603 }
2604
2605 @ Conversely, here is a procedure analogous to |print_int|. If the output
2606 of this procedure is subsequently read by \MP\ and converted by the
2607 |round_decimals| routine above, it turns out that the original value will
2608 be reproduced exactly. A decimal point is printed only if the value is
2609 not an integer. If there is more than one way to print the result with
2610 the optimum number of digits following the decimal point, the closest
2611 possible value is given.
2612
2613 The invariant relation in the \&{repeat} loop is that a sequence of
2614 decimal digits yet to be printed will yield the original number if and only if
2615 they form a fraction~$f$ in the range $s-\delta\L10\cdot2^{16}f<s$.
2616 We can stop if and only if $f=0$ satisfies this condition; the loop will
2617 terminate before $s$ can possibly become zero.
2618
2619 @<Basic printing...@>=
2620 void mp_print_scaled (MP mp,scaled s) { /* prints scaled real, rounded to five  digits */
2621   scaled delta; /* amount of allowable inaccuracy */
2622   if ( s<0 ) { 
2623         mp_print_char(mp, '-'); 
2624     negate(s); /* print the sign, if negative */
2625   }
2626   mp_print_int(mp, s / unity); /* print the integer part */
2627   s=10*(s % unity)+5;
2628   if ( s!=5 ) { 
2629     delta=10; 
2630     mp_print_char(mp, '.');
2631     do {  
2632       if ( delta>unity )
2633         s=s+0100000-(delta / 2); /* round the final digit */
2634       mp_print_char(mp, '0'+(s / unity)); 
2635       s=10*(s % unity); 
2636       delta=delta*10;
2637     } while (s>delta);
2638   }
2639 }
2640
2641 @ We often want to print two scaled quantities in parentheses,
2642 separated by a comma.
2643
2644 @<Basic printing...@>=
2645 void mp_print_two (MP mp,scaled x, scaled y) { /* prints `|(x,y)|' */
2646   mp_print_char(mp, '('); 
2647   mp_print_scaled(mp, x); 
2648   mp_print_char(mp, ','); 
2649   mp_print_scaled(mp, y);
2650   mp_print_char(mp, ')');
2651 }
2652
2653 @ The |scaled| quantities in \MP\ programs are generally supposed to be
2654 less than $2^{12}$ in absolute value, so \MP\ does much of its internal
2655 arithmetic with 28~significant bits of precision. A |fraction| denotes
2656 a scaled integer whose binary point is assumed to be 28 bit positions
2657 from the right.
2658
2659 @d fraction_half 01000000000 /* $2^{27}$, represents 0.50000000 */
2660 @d fraction_one 02000000000 /* $2^{28}$, represents 1.00000000 */
2661 @d fraction_two 04000000000 /* $2^{29}$, represents 2.00000000 */
2662 @d fraction_three 06000000000 /* $3\cdot2^{28}$, represents 3.00000000 */
2663 @d fraction_four 010000000000 /* $2^{30}$, represents 4.00000000 */
2664
2665 @<Types...@>=
2666 typedef integer fraction; /* this type is used for scaled fractions */
2667
2668 @ In fact, the two sorts of scaling discussed above aren't quite
2669 sufficient; \MP\ has yet another, used internally to keep track of angles
2670 in units of $2^{-20}$ degrees.
2671
2672 @d forty_five_deg 0264000000 /* $45\cdot2^{20}$, represents $45^\circ$ */
2673 @d ninety_deg 0550000000 /* $90\cdot2^{20}$, represents $90^\circ$ */
2674 @d one_eighty_deg 01320000000 /* $180\cdot2^{20}$, represents $180^\circ$ */
2675 @d three_sixty_deg 02640000000 /* $360\cdot2^{20}$, represents $360^\circ$ */
2676
2677 @<Types...@>=
2678 typedef integer angle; /* this type is used for scaled angles */
2679
2680 @ The |make_fraction| routine produces the |fraction| equivalent of
2681 |p/q|, given integers |p| and~|q|; it computes the integer
2682 $f=\lfloor2^{28}p/q+{1\over2}\rfloor$, when $p$ and $q$ are
2683 positive. If |p| and |q| are both of the same scaled type |t|,
2684 the ``type relation'' |make_fraction(t,t)=fraction| is valid;
2685 and it's also possible to use the subroutine ``backwards,'' using
2686 the relation |make_fraction(t,fraction)=t| between scaled types.
2687
2688 If the result would have magnitude $2^{31}$ or more, |make_fraction|
2689 sets |arith_error:=true|. Most of \MP's internal computations have
2690 been designed to avoid this sort of error.
2691
2692 If this subroutine were programmed in assembly language on a typical
2693 machine, we could simply compute |(@t$2^{28}$@>*p)div q|, since a
2694 double-precision product can often be input to a fixed-point division
2695 instruction. But when we are restricted to int-eger arithmetic it
2696 is necessary either to resort to multiple-precision maneuvering
2697 or to use a simple but slow iteration. The multiple-precision technique
2698 would be about three times faster than the code adopted here, but it
2699 would be comparatively long and tricky, involving about sixteen
2700 additional multiplications and divisions.
2701
2702 This operation is part of \MP's ``inner loop''; indeed, it will
2703 consume nearly 10\pct! of the running time (exclusive of input and output)
2704 if the code below is left unchanged. A machine-dependent recoding
2705 will therefore make \MP\ run faster. The present implementation
2706 is highly portable, but slow; it avoids multiplication and division
2707 except in the initial stage. System wizards should be careful to
2708 replace it with a routine that is guaranteed to produce identical
2709 results in all cases.
2710 @^system dependencies@>
2711
2712 As noted below, a few more routines should also be replaced by machine-dependent
2713 code, for efficiency. But when a procedure is not part of the ``inner loop,''
2714 such changes aren't advisable; simplicity and robustness are
2715 preferable to trickery, unless the cost is too high.
2716 @^inner loop@>
2717
2718 @<Internal ...@>=
2719 fraction mp_make_fraction (MP mp,integer p, integer q);
2720 integer mp_take_scaled (MP mp,integer q, scaled f) ;
2721
2722 @ If FIXPT is not defined, we need these preprocessor values
2723
2724 @d ELGORDO  0x7fffffff
2725 @d TWEXP31  2147483648.0
2726 @d TWEXP28  268435456.0
2727 @d TWEXP16 65536.0
2728 @d TWEXP_16 (1.0/65536.0)
2729 @d TWEXP_28 (1.0/268435456.0)
2730
2731
2732 @c 
2733 fraction mp_make_fraction (MP mp,integer p, integer q) {
2734 #ifdef FIXPT
2735   integer f; /* the fraction bits, with a leading 1 bit */
2736   integer n; /* the integer part of $\vert p/q\vert$ */
2737   integer be_careful; /* disables certain compiler optimizations */
2738   boolean negative = false; /* should the result be negated? */
2739   if ( p<0 ) {
2740     negate(p); negative=true;
2741   }
2742   if ( q<=0 ) { 
2743 #ifdef DEBUG
2744     if ( q==0 ) mp_confusion(mp, '/');
2745 #endif
2746 @:this can't happen /}{\quad \./@>
2747     negate(q); negative = ! negative;
2748   };
2749   n=p / q; p=p % q;
2750   if ( n>=8 ){ 
2751     mp->arith_error=true;
2752     return ( negative ? -el_gordo : el_gordo);
2753   } else { 
2754     n=(n-1)*fraction_one;
2755     @<Compute $f=\lfloor 2^{28}(1+p/q)+{1\over2}\rfloor$@>;
2756     return (negative ? (-(f+n)) : (f+n));
2757   }
2758 #else /* FIXPT */
2759     register double d;
2760         register integer i;
2761 #ifdef DEBUG
2762         if (q==0) mp_confusion(mp,'/'); 
2763 #endif /* DEBUG */
2764         d = TWEXP28 * (double)p /(double)q;
2765         if ((p^q) >= 0) {
2766                 d += 0.5;
2767                 if (d>=TWEXP31) {mp->arith_error=true; return ELGORDO;}
2768                 i = (integer) d;
2769                 if (d==i && ( ((q>0 ? -q : q)&077777)
2770                                 * (((i&037777)<<1)-1) & 04000)!=0) --i;
2771         } else {
2772                 d -= 0.5;
2773                 if (d<= -TWEXP31) {mp->arith_error=true; return -ELGORDO;}
2774                 i = (integer) d;
2775                 if (d==i && ( ((q>0 ? q : -q)&077777)
2776                                 * (((i&037777)<<1)+1) & 04000)!=0) ++i;
2777         }
2778         return i;
2779 #endif /* FIXPT */
2780 }
2781
2782 @ The |repeat| loop here preserves the following invariant relations
2783 between |f|, |p|, and~|q|:
2784 (i)~|0<=p<q|; (ii)~$fq+p=2^k(q+p_0)$, where $k$ is an integer and
2785 $p_0$ is the original value of~$p$.
2786
2787 Notice that the computation specifies
2788 |(p-q)+p| instead of |(p+p)-q|, because the latter could overflow.
2789 Let us hope that optimizing compilers do not miss this point; a
2790 special variable |be_careful| is used to emphasize the necessary
2791 order of computation. Optimizing compilers should keep |be_careful|
2792 in a register, not store it in memory.
2793 @^inner loop@>
2794
2795 @<Compute $f=\lfloor 2^{28}(1+p/q)+{1\over2}\rfloor$@>=
2796 {
2797   f=1;
2798   do {  
2799     be_careful=p-q; p=be_careful+p;
2800     if ( p>=0 ) { 
2801       f=f+f+1;
2802     } else  { 
2803       f+=f; p=p+q;
2804     }
2805   } while (f<fraction_one);
2806   be_careful=p-q;
2807   if ( be_careful+p>=0 ) incr(f);
2808 }
2809
2810 @ The dual of |make_fraction| is |take_fraction|, which multiplies a
2811 given integer~|q| by a fraction~|f|. When the operands are positive, it
2812 computes $p=\lfloor qf/2^{28}+{1\over2}\rfloor$, a symmetric function
2813 of |q| and~|f|.
2814
2815 This routine is even more ``inner loopy'' than |make_fraction|;
2816 the present implementation consumes almost 20\pct! of \MP's computation
2817 time during typical jobs, so a machine-language substitute is advisable.
2818 @^inner loop@> @^system dependencies@>
2819
2820 @<Declarations@>=
2821 integer mp_take_fraction (MP mp,integer q, fraction f) ;
2822
2823 @ @c 
2824 #ifdef FIXPT
2825 integer mp_take_fraction (MP mp,integer q, fraction f) {
2826   integer p; /* the fraction so far */
2827   boolean negative; /* should the result be negated? */
2828   integer n; /* additional multiple of $q$ */
2829   integer be_careful; /* disables certain compiler optimizations */
2830   @<Reduce to the case that |f>=0| and |q>=0|@>;
2831   if ( f<fraction_one ) { 
2832     n=0;
2833   } else { 
2834     n=f / fraction_one; f=f % fraction_one;
2835     if ( q<=el_gordo / n ) { 
2836       n=n*q ; 
2837     } else { 
2838       mp->arith_error=true; n=el_gordo;
2839     }
2840   }
2841   f=f+fraction_one;
2842   @<Compute $p=\lfloor qf/2^{28}+{1\over2}\rfloor-q$@>;
2843   be_careful=n-el_gordo;
2844   if ( be_careful+p>0 ){ 
2845     mp->arith_error=true; n=el_gordo-p;
2846   }
2847   if ( negative ) 
2848         return (-(n+p));
2849   else 
2850     return (n+p);
2851 #else /* FIXPT */
2852 integer mp_take_fraction (MP mp,integer p, fraction q) {
2853     register double d;
2854         register integer i;
2855         d = (double)p * (double)q * TWEXP_28;
2856         if ((p^q) >= 0) {
2857                 d += 0.5;
2858                 if (d>=TWEXP31) {
2859                         if (d!=TWEXP31 || (((p&077777)*(q&077777))&040000)==0)
2860                                 mp->arith_error = true;
2861                         return ELGORDO;
2862                 }
2863                 i = (integer) d;
2864                 if (d==i && (((p&077777)*(q&077777))&040000)!=0) --i;
2865         } else {
2866                 d -= 0.5;
2867                 if (d<= -TWEXP31) {
2868                         if (d!= -TWEXP31 || ((-(p&077777)*(q&077777))&040000)==0)
2869                                 mp->arith_error = true;
2870                         return -ELGORDO;
2871                 }
2872                 i = (integer) d;
2873                 if (d==i && ((-(p&077777)*(q&077777))&040000)!=0) ++i;
2874         }
2875         return i;
2876 #endif /* FIXPT */
2877 }
2878
2879 @ @<Reduce to the case that |f>=0| and |q>=0|@>=
2880 if ( f>=0 ) {
2881   negative=false;
2882 } else { 
2883   negate( f); negative=true;
2884 }
2885 if ( q<0 ) { 
2886   negate(q); negative=! negative;
2887 }
2888
2889 @ The invariant relations in this case are (i)~$\lfloor(qf+p)/2^k\rfloor
2890 =\lfloor qf_0/2^{28}+{1\over2}\rfloor$, where $k$ is an integer and
2891 $f_0$ is the original value of~$f$; (ii)~$2^k\L f<2^{k+1}$.
2892 @^inner loop@>
2893
2894 @<Compute $p=\lfloor qf/2^{28}+{1\over2}\rfloor-q$@>=
2895 p=fraction_half; /* that's $2^{27}$; the invariants hold now with $k=28$ */
2896 if ( q<fraction_four ) {
2897   do {  
2898     if ( odd(f) ) p=halfp(p+q); else p=halfp(p);
2899     f=halfp(f);
2900   } while (f!=1);
2901 } else  {
2902   do {  
2903     if ( odd(f) ) p=p+halfp(q-p); else p=halfp(p);
2904     f=halfp(f);
2905   } while (f!=1);
2906 }
2907
2908
2909 @ When we want to multiply something by a |scaled| quantity, we use a scheme
2910 analogous to |take_fraction| but with a different scaling.
2911 Given positive operands, |take_scaled|
2912 computes the quantity $p=\lfloor qf/2^{16}+{1\over2}\rfloor$.
2913
2914 Once again it is a good idea to use a machine-language replacement if
2915 possible; otherwise |take_scaled| will use more than 2\pct! of the running time
2916 when the Computer Modern fonts are being generated.
2917 @^inner loop@>
2918
2919 @c 
2920 #ifdef FIXPT
2921 integer mp_take_scaled (MP mp,integer q, scaled f) {
2922   integer p; /* the fraction so far */
2923   boolean negative; /* should the result be negated? */
2924   integer n; /* additional multiple of $q$ */
2925   integer be_careful; /* disables certain compiler optimizations */
2926   @<Reduce to the case that |f>=0| and |q>=0|@>;
2927   if ( f<unity ) { 
2928     n=0;
2929   } else  { 
2930     n=f / unity; f=f % unity;
2931     if ( q<=el_gordo / n ) {
2932       n=n*q;
2933     } else  { 
2934       mp->arith_error=true; n=el_gordo;
2935     }
2936   }
2937   f=f+unity;
2938   @<Compute $p=\lfloor qf/2^{16}+{1\over2}\rfloor-q$@>;
2939   be_careful=n-el_gordo;
2940   if ( be_careful+p>0 ) { 
2941     mp->arith_error=true; n=el_gordo-p;
2942   }
2943   return ( negative ?(-(n+p)) :(n+p));
2944 #else /* FIXPT */
2945 integer mp_take_scaled (MP mp,integer p, scaled q) {
2946     register double d;
2947         register integer i;
2948         d = (double)p * (double)q * TWEXP_16;
2949         if ((p^q) >= 0) {
2950                 d += 0.5;
2951                 if (d>=TWEXP31) {
2952                         if (d!=TWEXP31 || (((p&077777)*(q&077777))&040000)==0)
2953                                 mp->arith_error = true;
2954                         return ELGORDO;
2955                 }
2956                 i = (integer) d;
2957                 if (d==i && (((p&077777)*(q&077777))&040000)!=0) --i;
2958         } else {
2959                 d -= 0.5;
2960                 if (d<= -TWEXP31) {
2961                         if (d!= -TWEXP31 || ((-(p&077777)*(q&077777))&040000)==0)
2962                                 mp->arith_error = true;
2963                         return -ELGORDO;
2964                 }
2965                 i = (integer) d;
2966                 if (d==i && ((-(p&077777)*(q&077777))&040000)!=0) ++i;
2967         }
2968         return i;
2969 #endif /* FIXPT */
2970 }
2971
2972 @ @<Compute $p=\lfloor qf/2^{16}+{1\over2}\rfloor-q$@>=
2973 p=half_unit; /* that's $2^{15}$; the invariants hold now with $k=16$ */
2974 @^inner loop@>
2975 if ( q<fraction_four ) {
2976   do {  
2977     p = (odd(f) ? halfp(p+q) : halfp(p));
2978     f=halfp(f);
2979   } while (f!=1);
2980 } else {
2981   do {  
2982     p = (odd(f) ? p+halfp(q-p) : halfp(p));
2983     f=halfp(f);
2984   } while (f!=1);
2985 }
2986
2987 @ For completeness, there's also |make_scaled|, which computes a
2988 quotient as a |scaled| number instead of as a |fraction|.
2989 In other words, the result is $\lfloor2^{16}p/q+{1\over2}\rfloor$, if the
2990 operands are positive. \ (This procedure is not used especially often,
2991 so it is not part of \MP's inner loop.)
2992
2993 @<Internal library ...@>=
2994 scaled mp_make_scaled (MP mp,integer p, integer q) ;
2995
2996 @ @c 
2997 scaled mp_make_scaled (MP mp,integer p, integer q) {
2998 #ifdef FIXPT 
2999   integer f; /* the fraction bits, with a leading 1 bit */
3000   integer n; /* the integer part of $\vert p/q\vert$ */
3001   boolean negative; /* should the result be negated? */
3002   integer be_careful; /* disables certain compiler optimizations */
3003   if ( p>=0 ) negative=false;
3004   else  { negate(p); negative=true; };
3005   if ( q<=0 ) { 
3006 #ifdef DEBUG 
3007     if ( q==0 ) mp_confusion(mp, "/");
3008 @:this can't happen /}{\quad \./@>
3009 #endif
3010     negate(q); negative=! negative;
3011   }
3012   n=p / q; p=p % q;
3013   if ( n>=0100000 ) { 
3014     mp->arith_error=true;
3015     return (negative ? (-el_gordo) : el_gordo);
3016   } else  { 
3017     n=(n-1)*unity;
3018     @<Compute $f=\lfloor 2^{16}(1+p/q)+{1\over2}\rfloor$@>;
3019     return ( negative ? (-(f+n)) :(f+n));
3020   }
3021 #else /* FIXPT */
3022     register double d;
3023         register integer i;
3024 #ifdef DEBUG
3025         if (q==0) mp_confusion(mp,"/"); 
3026 #endif /* DEBUG */
3027         d = TWEXP16 * (double)p /(double)q;
3028         if ((p^q) >= 0) {
3029                 d += 0.5;
3030                 if (d>=TWEXP31) {mp->arith_error=true; return ELGORDO;}
3031                 i = (integer) d;
3032                 if (d==i && ( ((q>0 ? -q : q)&077777)
3033                                 * (((i&037777)<<1)-1) & 04000)!=0) --i;
3034         } else {
3035                 d -= 0.5;
3036                 if (d<= -TWEXP31) {mp->arith_error=true; return -ELGORDO;}
3037                 i = (integer) d;
3038                 if (d==i && ( ((q>0 ? q : -q)&077777)
3039                                 * (((i&037777)<<1)+1) & 04000)!=0) ++i;
3040         }
3041         return i;
3042 #endif /* FIXPT */
3043 }
3044
3045 @ @<Compute $f=\lfloor 2^{16}(1+p/q)+{1\over2}\rfloor$@>=
3046 f=1;
3047 do {  
3048   be_careful=p-q; p=be_careful+p;
3049   if ( p>=0 ) f=f+f+1;
3050   else  { f+=f; p=p+q; };
3051 } while (f<unity);
3052 be_careful=p-q;
3053 if ( be_careful+p>=0 ) incr(f)
3054
3055 @ Here is a typical example of how the routines above can be used.
3056 It computes the function
3057 $${1\over3\tau}f(\theta,\phi)=
3058 {\tau^{-1}\bigl(2+\sqrt2\,(\sin\theta-{1\over16}\sin\phi)
3059  (\sin\phi-{1\over16}\sin\theta)(\cos\theta-\cos\phi)\bigr)\over
3060 3\,\bigl(1+{1\over2}(\sqrt5-1)\cos\theta+{1\over2}(3-\sqrt5\,)\cos\phi\bigr)},$$
3061 where $\tau$ is a |scaled| ``tension'' parameter. This is \MP's magic
3062 fudge factor for placing the first control point of a curve that starts
3063 at an angle $\theta$ and ends at an angle $\phi$ from the straight path.
3064 (Actually, if the stated quantity exceeds 4, \MP\ reduces it to~4.)
3065
3066 The trigonometric quantity to be multiplied by $\sqrt2$ is less than $\sqrt2$.
3067 (It's a sum of eight terms whose absolute values can be bounded using
3068 relations such as $\sin\theta\cos\theta\L{1\over2}$.) Thus the numerator
3069 is positive; and since the tension $\tau$ is constrained to be at least
3070 $3\over4$, the numerator is less than $16\over3$. The denominator is
3071 nonnegative and at most~6.  Hence the fixed-point calculations below
3072 are guaranteed to stay within the bounds of a 32-bit computer word.
3073
3074 The angles $\theta$ and $\phi$ are given implicitly in terms of |fraction|
3075 arguments |st|, |ct|, |sf|, and |cf|, representing $\sin\theta$, $\cos\theta$,
3076 $\sin\phi$, and $\cos\phi$, respectively.
3077
3078 @c 
3079 fraction mp_velocity (MP mp,fraction st, fraction ct, fraction sf,
3080                       fraction cf, scaled t) {
3081   integer acc,num,denom; /* registers for intermediate calculations */
3082   acc=mp_take_fraction(mp, st-(sf / 16), sf-(st / 16));
3083   acc=mp_take_fraction(mp, acc,ct-cf);
3084   num=fraction_two+mp_take_fraction(mp, acc,379625062);
3085                    /* $2^{28}\sqrt2\approx379625062.497$ */
3086   denom=fraction_three+mp_take_fraction(mp, ct,497706707)+mp_take_fraction(mp, cf,307599661);
3087                       /* $3\cdot2^{27}\cdot(\sqrt5-1)\approx497706706.78$ and
3088                          $3\cdot2^{27}\cdot(3-\sqrt5\,)\approx307599661.22$ */
3089   if ( t!=unity ) num=mp_make_scaled(mp, num,t);
3090   /* |make_scaled(fraction,scaled)=fraction| */
3091   if ( num / 4>=denom ) 
3092     return fraction_four;
3093   else 
3094     return mp_make_fraction(mp, num, denom);
3095 }
3096
3097 @ The following somewhat different subroutine tests rigorously if $ab$ is
3098 greater than, equal to, or less than~$cd$,
3099 given integers $(a,b,c,d)$. In most cases a quick decision is reached.
3100 The result is $+1$, 0, or~$-1$ in the three respective cases.
3101
3102 @d mp_ab_vs_cd(M,A,B,C,D) mp_do_ab_vs_cd(A,B,C,D)
3103
3104 @c 
3105 integer mp_do_ab_vs_cd (integer a,integer b, integer c, integer d) {
3106   integer q,r; /* temporary registers */
3107   @<Reduce to the case that |a,c>=0|, |b,d>0|@>;
3108   while (1) { 
3109     q = a / d; r = c / b;
3110     if ( q!=r )
3111       return ( q>r ? 1 : -1);
3112     q = a % d; r = c % b;
3113     if ( r==0 )
3114       return (q ? 1 : 0);
3115     if ( q==0 ) return -1;
3116     a=b; b=q; c=d; d=r;
3117   } /* now |a>d>0| and |c>b>0| */
3118 }
3119
3120 @ @<Reduce to the case that |a...@>=
3121 if ( a<0 ) { negate(a); negate(b);  };
3122 if ( c<0 ) { negate(c); negate(d);  };
3123 if ( d<=0 ) { 
3124   if ( b>=0 ) {
3125     if ( (a==0||b==0)&&(c==0||d==0) ) return 0;
3126     else return 1;
3127   }
3128   if ( d==0 )
3129     return ( a==0 ? 0 : -1);
3130   q=a; a=c; c=q; q=-b; b=-d; d=q;
3131 } else if ( b<=0 ) { 
3132   if ( b<0 ) if ( a>0 ) return -1;
3133   return (c==0 ? 0 : -1);
3134 }
3135
3136 @ We conclude this set of elementary routines with some simple rounding
3137 and truncation operations.
3138
3139 @<Internal library declarations@>=
3140 #define mp_floor_scaled(M,i) ((i)&(-65536))
3141 #define mp_round_unscaled(M,i) (((i>>15)+1)>>1)
3142 #define mp_round_fraction(M,i) (((i>>11)+1)>>1)
3143
3144
3145 @* \[8] Algebraic and transcendental functions.
3146 \MP\ computes all of the necessary special functions from scratch, without
3147 relying on |real| arithmetic or system subroutines for sines, cosines, etc.
3148
3149 @ To get the square root of a |scaled| number |x|, we want to calculate
3150 $s=\lfloor 2^8\!\sqrt x +{1\over2}\rfloor$. If $x>0$, this is the unique
3151 integer such that $2^{16}x-s\L s^2<2^{16}x+s$. The following subroutine
3152 determines $s$ by an iterative method that maintains the invariant
3153 relations $x=2^{46-2k}x_0\bmod 2^{30}$, $0<y=\lfloor 2^{16-2k}x_0\rfloor
3154 -s^2+s\L q=2s$, where $x_0$ is the initial value of $x$. The value of~$y$
3155 might, however, be zero at the start of the first iteration.
3156
3157 @<Declarations@>=
3158 scaled mp_square_rt (MP mp,scaled x) ;
3159
3160 @ @c 
3161 scaled mp_square_rt (MP mp,scaled x) {
3162   small_number k; /* iteration control counter */
3163   integer y,q; /* registers for intermediate calculations */
3164   if ( x<=0 ) { 
3165     @<Handle square root of zero or negative argument@>;
3166   } else { 
3167     k=23; q=2;
3168     while ( x<fraction_two ) { /* i.e., |while x<@t$2^{29}$@>|\unskip */
3169       decr(k); x=x+x+x+x;
3170     }
3171     if ( x<fraction_four ) y=0;
3172     else  { x=x-fraction_four; y=1; };
3173     do {  
3174       @<Decrease |k| by 1, maintaining the invariant
3175       relations between |x|, |y|, and~|q|@>;
3176     } while (k!=0);
3177     return (halfp(q));
3178   }
3179 }
3180
3181 @ @<Handle square root of zero...@>=
3182
3183   if ( x<0 ) { 
3184     print_err("Square root of ");
3185 @.Square root...replaced by 0@>
3186     mp_print_scaled(mp, x); mp_print(mp, " has been replaced by 0");
3187     help2("Since I don't take square roots of negative numbers,")
3188          ("I'm zeroing this one. Proceed, with fingers crossed.");
3189     mp_error(mp);
3190   };
3191   return 0;
3192 }
3193
3194 @ @<Decrease |k| by 1, maintaining...@>=
3195 x+=x; y+=y;
3196 if ( x>=fraction_four ) { /* note that |fraction_four=@t$2^{30}$@>| */
3197   x=x-fraction_four; incr(y);
3198 };
3199 x+=x; y=y+y-q; q+=q;
3200 if ( x>=fraction_four ) { x=x-fraction_four; incr(y); };
3201 if ( y>q ){ y=y-q; q=q+2; }
3202 else if ( y<=0 )  { q=q-2; y=y+q;  };
3203 decr(k)
3204
3205 @ Pythagorean addition $\psqrt{a^2+b^2}$ is implemented by an elegant
3206 iterative scheme due to Cleve Moler and Donald Morrison [{\sl IBM Journal
3207 @^Moler, Cleve Barry@>
3208 @^Morrison, Donald Ross@>
3209 of Research and Development\/ \bf27} (1983), 577--581]. It modifies |a| and~|b|
3210 in such a way that their Pythagorean sum remains invariant, while the
3211 smaller argument decreases.
3212
3213 @<Internal library ...@>=
3214 integer mp_pyth_add (MP mp,integer a, integer b);
3215
3216
3217 @ @c 
3218 integer mp_pyth_add (MP mp,integer a, integer b) {
3219   fraction r; /* register used to transform |a| and |b| */
3220   boolean big; /* is the result dangerously near $2^{31}$? */
3221   a=abs(a); b=abs(b);
3222   if ( a<b ) { r=b; b=a; a=r; }; /* now |0<=b<=a| */
3223   if ( b>0 ) {
3224     if ( a<fraction_two ) {
3225       big=false;
3226     } else { 
3227       a=a / 4; b=b / 4; big=true;
3228     }; /* we reduced the precision to avoid arithmetic overflow */
3229     @<Replace |a| by an approximation to $\psqrt{a^2+b^2}$@>;
3230     if ( big ) {
3231       if ( a<fraction_two ) {
3232         a=a+a+a+a;
3233       } else  { 
3234         mp->arith_error=true; a=el_gordo;
3235       };
3236     }
3237   }
3238   return a;
3239 }
3240
3241 @ The key idea here is to reflect the vector $(a,b)$ about the
3242 line through $(a,b/2)$.
3243
3244 @<Replace |a| by an approximation to $\psqrt{a^2+b^2}$@>=
3245 while (1) {  
3246   r=mp_make_fraction(mp, b,a);
3247   r=mp_take_fraction(mp, r,r); /* now $r\approx b^2/a^2$ */
3248   if ( r==0 ) break;
3249   r=mp_make_fraction(mp, r,fraction_four+r);
3250   a=a+mp_take_fraction(mp, a+a,r); b=mp_take_fraction(mp, b,r);
3251 }
3252
3253
3254 @ Here is a similar algorithm for $\psqrt{a^2-b^2}$.
3255 It converges slowly when $b$ is near $a$, but otherwise it works fine.
3256
3257 @c 
3258 integer mp_pyth_sub (MP mp,integer a, integer b) {
3259   fraction r; /* register used to transform |a| and |b| */
3260   boolean big; /* is the input dangerously near $2^{31}$? */
3261   a=abs(a); b=abs(b);
3262   if ( a<=b ) {
3263     @<Handle erroneous |pyth_sub| and set |a:=0|@>;
3264   } else { 
3265     if ( a<fraction_four ) {
3266       big=false;
3267     } else  { 
3268       a=halfp(a); b=halfp(b); big=true;
3269     }
3270     @<Replace |a| by an approximation to $\psqrt{a^2-b^2}$@>;
3271     if ( big ) double(a);
3272   }
3273   return a;
3274 }
3275
3276 @ @<Replace |a| by an approximation to $\psqrt{a^2-b^2}$@>=
3277 while (1) { 
3278   r=mp_make_fraction(mp, b,a);
3279   r=mp_take_fraction(mp, r,r); /* now $r\approx b^2/a^2$ */
3280   if ( r==0 ) break;
3281   r=mp_make_fraction(mp, r,fraction_four-r);
3282   a=a-mp_take_fraction(mp, a+a,r); b=mp_take_fraction(mp, b,r);
3283 }
3284
3285 @ @<Handle erroneous |pyth_sub| and set |a:=0|@>=
3286
3287   if ( a<b ){ 
3288     print_err("Pythagorean subtraction "); mp_print_scaled(mp, a);
3289     mp_print(mp, "+-+"); mp_print_scaled(mp, b); 
3290     mp_print(mp, " has been replaced by 0");
3291 @.Pythagorean...@>
3292     help2("Since I don't take square roots of negative numbers,")
3293          ("I'm zeroing this one. Proceed, with fingers crossed.");
3294     mp_error(mp);
3295   }
3296   a=0;
3297 }
3298
3299 @ The subroutines for logarithm and exponential involve two tables.
3300 The first is simple: |two_to_the[k]| equals $2^k$. The second involves
3301 a bit more calculation, which the author claims to have done correctly:
3302 |spec_log[k]| is $2^{27}$ times $\ln\bigl(1/(1-2^{-k})\bigr)=
3303 2^{-k}+{1\over2}2^{-2k}+{1\over3}2^{-3k}+\cdots\,$, rounded to the
3304 nearest integer.
3305
3306 @d two_to_the(A) (1<<(A))
3307
3308 @<Constants ...@>=
3309 static const integer spec_log[29] = { 0, /* special logarithms */
3310 93032640, 38612034, 17922280, 8662214, 4261238, 2113709,
3311 1052693, 525315, 262400, 131136, 65552, 32772, 16385,
3312 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 1 };
3313
3314 @ @<Local variables for initialization@>=
3315 integer k; /* all-purpose loop index */
3316
3317
3318 @ Here is the routine that calculates $2^8$ times the natural logarithm
3319 of a |scaled| quantity; it is an integer approximation to $2^{24}\ln(x/2^{16})$,
3320 when |x| is a given positive integer.
3321
3322 The method is based on exercise 1.2.2--25 in {\sl The Art of Computer
3323 Programming\/}: During the main iteration we have $1\L 2^{-30}x<1/(1-2^{1-k})$,
3324 and the logarithm of $2^{30}x$ remains to be added to an accumulator
3325 register called~$y$. Three auxiliary bits of accuracy are retained in~$y$
3326 during the calculation, and sixteen auxiliary bits to extend |y| are
3327 kept in~|z| during the initial argument reduction. (We add
3328 $100\cdot2^{16}=6553600$ to~|z| and subtract 100 from~|y| so that |z| will
3329 not become negative; also, the actual amount subtracted from~|y| is~96,
3330 not~100, because we want to add~4 for rounding before the final division by~8.)
3331
3332 @c 
3333 scaled mp_m_log (MP mp,scaled x) {
3334   integer y,z; /* auxiliary registers */
3335   integer k; /* iteration counter */
3336   if ( x<=0 ) {
3337      @<Handle non-positive logarithm@>;
3338   } else  { 
3339     y=1302456956+4-100; /* $14\times2^{27}\ln2\approx1302456956.421063$ */
3340     z=27595+6553600; /* and $2^{16}\times .421063\approx 27595$ */
3341     while ( x<fraction_four ) {
3342        double(x); y-=93032639; z-=48782;
3343     } /* $2^{27}\ln2\approx 93032639.74436163$ and $2^{16}\times.74436163\approx 48782$ */
3344     y=y+(z / unity); k=2;
3345     while ( x>fraction_four+4 ) {
3346       @<Increase |k| until |x| can be multiplied by a
3347         factor of $2^{-k}$, and adjust $y$ accordingly@>;
3348     }
3349     return (y / 8);
3350   }
3351 }
3352
3353 @ @<Increase |k| until |x| can...@>=
3354
3355   z=((x-1) / two_to_the(k))+1; /* $z=\lceil x/2^k\rceil$ */
3356   while ( x<fraction_four+z ) { z=halfp(z+1); incr(k); };
3357   y+=spec_log[k]; x-=z;
3358 }
3359
3360 @ @<Handle non-positive logarithm@>=
3361
3362   print_err("Logarithm of ");
3363 @.Logarithm...replaced by 0@>
3364   mp_print_scaled(mp, x); mp_print(mp, " has been replaced by 0");
3365   help2("Since I don't take logs of non-positive numbers,")
3366        ("I'm zeroing this one. Proceed, with fingers crossed.");
3367   mp_error(mp); 
3368   return 0;
3369 }
3370
3371 @ Conversely, the exponential routine calculates $\exp(x/2^8)$,
3372 when |x| is |scaled|. The result is an integer approximation to
3373 $2^{16}\exp(x/2^{24})$, when |x| is regarded as an integer.
3374
3375 @c 
3376 scaled mp_m_exp (MP mp,scaled x) {
3377   small_number k; /* loop control index */
3378   integer y,z; /* auxiliary registers */
3379   if ( x>174436200 ) {
3380     /* $2^{24}\ln((2^{31}-1)/2^{16})\approx 174436199.51$ */
3381     mp->arith_error=true; 
3382     return el_gordo;
3383   } else if ( x<-197694359 ) {
3384         /* $2^{24}\ln(2^{-1}/2^{16})\approx-197694359.45$ */
3385     return 0;
3386   } else { 
3387     if ( x<=0 ) { 
3388        z=-8*x; y=04000000; /* $y=2^{20}$ */
3389     } else { 
3390       if ( x<=127919879 ) { 
3391         z=1023359037-8*x;
3392         /* $2^{27}\ln((2^{31}-1)/2^{20})\approx 1023359037.125$ */
3393       } else {
3394        z=8*(174436200-x); /* |z| is always nonnegative */
3395       }
3396       y=el_gordo;
3397     };
3398     @<Multiply |y| by $\exp(-z/2^{27})$@>;
3399     if ( x<=127919879 ) 
3400        return ((y+8) / 16);
3401      else 
3402        return y;
3403   }
3404 }
3405
3406 @ The idea here is that subtracting |spec_log[k]| from |z| corresponds
3407 to multiplying |y| by $1-2^{-k}$.
3408
3409 A subtle point (which had to be checked) was that if $x=127919879$, the
3410 value of~|y| will decrease so that |y+8| doesn't overflow. In fact,
3411 $z$ will be 5 in this case, and |y| will decrease by~64 when |k=25|
3412 and by~16 when |k=27|.
3413
3414 @<Multiply |y| by...@>=
3415 k=1;
3416 while ( z>0 ) { 
3417   while ( z>=spec_log[k] ) { 
3418     z-=spec_log[k];
3419     y=y-1-((y-two_to_the(k-1)) / two_to_the(k));
3420   }
3421   incr(k);
3422 }
3423
3424 @ The trigonometric subroutines use an auxiliary table such that
3425 |spec_atan[k]| contains an approximation to the |angle| whose tangent
3426 is~$1/2^k$. $\arctan2^{-k}$ times $2^{20}\cdot180/\pi$ 
3427
3428 @<Constants ...@>=
3429 static const angle spec_atan[27] = { 0, 27855475, 14718068, 7471121, 3750058, 
3430 1876857, 938658, 469357, 234682, 117342, 58671, 29335, 14668, 7334, 3667, 
3431 1833, 917, 458, 229, 115, 57, 29, 14, 7, 4, 2, 1 };
3432
3433 @ Given integers |x| and |y|, not both zero, the |n_arg| function
3434 returns the |angle| whose tangent points in the direction $(x,y)$.
3435 This subroutine first determines the correct octant, then solves the
3436 problem for |0<=y<=x|, then converts the result appropriately to
3437 return an answer in the range |-one_eighty_deg<=@t$\theta$@><=one_eighty_deg|.
3438 (The answer is |+one_eighty_deg| if |y=0| and |x<0|, but an answer of
3439 |-one_eighty_deg| is possible if, for example, |y=-1| and $x=-2^{30}$.)
3440
3441 The octants are represented in a ``Gray code,'' since that turns out
3442 to be computationally simplest.
3443
3444 @d negate_x 1
3445 @d negate_y 2
3446 @d switch_x_and_y 4
3447 @d first_octant 1
3448 @d second_octant (first_octant+switch_x_and_y)
3449 @d third_octant (first_octant+switch_x_and_y+negate_x)
3450 @d fourth_octant (first_octant+negate_x)
3451 @d fifth_octant (first_octant+negate_x+negate_y)
3452 @d sixth_octant (first_octant+switch_x_and_y+negate_x+negate_y)
3453 @d seventh_octant (first_octant+switch_x_and_y+negate_y)
3454 @d eighth_octant (first_octant+negate_y)
3455
3456 @c 
3457 angle mp_n_arg (MP mp,integer x, integer y) {
3458   angle z; /* auxiliary register */
3459   integer t; /* temporary storage */
3460   small_number k; /* loop counter */
3461   int octant; /* octant code */
3462   if ( x>=0 ) {
3463     octant=first_octant;
3464   } else { 
3465     negate(x); octant=first_octant+negate_x;
3466   }
3467   if ( y<0 ) { 
3468     negate(y); octant=octant+negate_y;
3469   }
3470   if ( x<y ) { 
3471     t=y; y=x; x=t; octant=octant+switch_x_and_y;
3472   }
3473   if ( x==0 ) { 
3474     @<Handle undefined arg@>; 
3475   } else { 
3476     @<Set variable |z| to the arg of $(x,y)$@>;
3477     @<Return an appropriate answer based on |z| and |octant|@>;
3478   }
3479 }
3480
3481 @ @<Handle undefined arg@>=
3482
3483   print_err("angle(0,0) is taken as zero");
3484 @.angle(0,0)...zero@>
3485   help2("The `angle' between two identical points is undefined.")
3486        ("I'm zeroing this one. Proceed, with fingers crossed.");
3487   mp_error(mp); 
3488   return 0;
3489 }
3490
3491 @ @<Return an appropriate answer...@>=
3492 switch (octant) {
3493 case first_octant: return z;
3494 case second_octant: return (ninety_deg-z);
3495 case third_octant: return (ninety_deg+z);
3496 case fourth_octant: return (one_eighty_deg-z);
3497 case fifth_octant: return (z-one_eighty_deg);
3498 case sixth_octant: return (-z-ninety_deg);
3499 case seventh_octant: return (z-ninety_deg);
3500 case eighth_octant: return (-z);
3501 }; /* there are no other cases */
3502 return 0
3503
3504 @ At this point we have |x>=y>=0|, and |x>0|. The numbers are scaled up
3505 or down until $2^{28}\L x<2^{29}$, so that accurate fixed-point calculations
3506 will be made.
3507
3508 @<Set variable |z| to the arg...@>=
3509 while ( x>=fraction_two ) { 
3510   x=halfp(x); y=halfp(y);
3511 }
3512 z=0;
3513 if ( y>0 ) { 
3514  while ( x<fraction_one ) { 
3515     x+=x; y+=y; 
3516  };
3517  @<Increase |z| to the arg of $(x,y)$@>;
3518 }
3519
3520 @ During the calculations of this section, variables |x| and~|y|
3521 represent actual coordinates $(x,2^{-k}y)$. We will maintain the
3522 condition |x>=y|, so that the tangent will be at most $2^{-k}$.
3523 If $x<2y$, the tangent is greater than $2^{-k-1}$. The transformation
3524 $(a,b)\mapsto(a+b\tan\phi,b-a\tan\phi)$ replaces $(a,b)$ by
3525 coordinates whose angle has decreased by~$\phi$; in the special case
3526 $a=x$, $b=2^{-k}y$, and $\tan\phi=2^{-k-1}$, this operation reduces
3527 to the particularly simple iteration shown here. [Cf.~John E. Meggitt,
3528 @^Meggitt, John E.@>
3529 {\sl IBM Journal of Research and Development\/ \bf6} (1962), 210--226.]
3530
3531 The initial value of |x| will be multiplied by at most
3532 $(1+{1\over2})(1+{1\over8})(1+{1\over32})\cdots\approx 1.7584$; hence
3533 there is no chance of integer overflow.
3534
3535 @<Increase |z|...@>=
3536 k=0;
3537 do {  
3538   y+=y; incr(k);
3539   if ( y>x ){ 
3540     z=z+spec_atan[k]; t=x; x=x+(y / two_to_the(k+k)); y=y-t;
3541   };
3542 } while (k!=15);
3543 do {  
3544   y+=y; incr(k);
3545   if ( y>x ) { z=z+spec_atan[k]; y=y-x; };
3546 } while (k!=26)
3547
3548 @ Conversely, the |n_sin_cos| routine takes an |angle| and produces the sine
3549 and cosine of that angle. The results of this routine are
3550 stored in global integer variables |n_sin| and |n_cos|.
3551
3552 @<Glob...@>=
3553 fraction n_sin;fraction n_cos; /* results computed by |n_sin_cos| */
3554
3555 @ Given an integer |z| that is $2^{20}$ times an angle $\theta$ in degrees,
3556 the purpose of |n_sin_cos(z)| is to set
3557 |x=@t$r\cos\theta$@>| and |y=@t$r\sin\theta$@>| (approximately),
3558 for some rather large number~|r|. The maximum of |x| and |y|
3559 will be between $2^{28}$ and $2^{30}$, so that there will be hardly
3560 any loss of accuracy. Then |x| and~|y| are divided by~|r|.
3561
3562 @c 
3563 void mp_n_sin_cos (MP mp,angle z) { /* computes a multiple of the sine
3564                                        and cosine */ 
3565   small_number k; /* loop control variable */
3566   int q; /* specifies the quadrant */
3567   fraction r; /* magnitude of |(x,y)| */
3568   integer x,y,t; /* temporary registers */
3569   while ( z<0 ) z=z+three_sixty_deg;
3570   z=z % three_sixty_deg; /* now |0<=z<three_sixty_deg| */
3571   q=z / forty_five_deg; z=z % forty_five_deg;
3572   x=fraction_one; y=x;
3573   if ( ! odd(q) ) z=forty_five_deg-z;
3574   @<Subtract angle |z| from |(x,y)|@>;
3575   @<Convert |(x,y)| to the octant determined by~|q|@>;
3576   r=mp_pyth_add(mp, x,y); 
3577   mp->n_cos=mp_make_fraction(mp, x,r); 
3578   mp->n_sin=mp_make_fraction(mp, y,r);
3579 }
3580
3581 @ In this case the octants are numbered sequentially.
3582
3583 @<Convert |(x,...@>=
3584 switch (q) {
3585 case 0: break;
3586 case 1: t=x; x=y; y=t; break;
3587 case 2: t=x; x=-y; y=t; break;
3588 case 3: negate(x); break;
3589 case 4: negate(x); negate(y); break;
3590 case 5: t=x; x=-y; y=-t; break;
3591 case 6: t=x; x=y; y=-t; break;
3592 case 7: negate(y); break;
3593 } /* there are no other cases */
3594
3595 @ The main iteration of |n_sin_cos| is similar to that of |n_arg| but
3596 applied in reverse. The values of |spec_atan[k]| decrease slowly enough
3597 that this loop is guaranteed to terminate before the (nonexistent) value
3598 |spec_atan[27]| would be required.
3599
3600 @<Subtract angle |z|...@>=
3601 k=1;
3602 while ( z>0 ){ 
3603   if ( z>=spec_atan[k] ) { 
3604     z=z-spec_atan[k]; t=x;
3605     x=t+y / two_to_the(k);
3606     y=y-t / two_to_the(k);
3607   }
3608   incr(k);
3609 }
3610 if ( y<0 ) y=0 /* this precaution may never be needed */
3611
3612 @ And now let's complete our collection of numeric utility routines
3613 by considering random number generation.
3614 \MP\ generates pseudo-random numbers with the additive scheme recommended
3615 in Section 3.6 of {\sl The Art of Computer Programming}; however, the
3616 results are random fractions between 0 and |fraction_one-1|, inclusive.
3617
3618 There's an auxiliary array |randoms| that contains 55 pseudo-random
3619 fractions. Using the recurrence $x_n=(x_{n-55}-x_{n-31})\bmod 2^{28}$,
3620 we generate batches of 55 new $x_n$'s at a time by calling |new_randoms|.
3621 The global variable |j_random| tells which element has most recently
3622 been consumed.
3623 The global variable |random_seed| was introduced in version 0.9,
3624 for the sole reason of stressing the fact that the initial value of the
3625 random seed is system-dependant. The initialization code below will initialize
3626 this variable to |(internal[mp_time] div unity)+internal[mp_day]|, but this 
3627 is not good enough on modern fast machines that are capable of running
3628 multiple MetaPost processes within the same second.
3629 @^system dependencies@>
3630
3631 @<Glob...@>=
3632 fraction randoms[55]; /* the last 55 random values generated */
3633 int j_random; /* the number of unused |randoms| */
3634
3635 @ @<Option variables@>=
3636 int random_seed; /* the default random seed */
3637
3638 @ @<Allocate or initialize ...@>=
3639 mp->random_seed = (scaled)opt->random_seed;
3640
3641 @ To consume a random fraction, the program below will say `|next_random|'
3642 and then it will fetch |randoms[j_random]|.
3643
3644 @d next_random { if ( mp->j_random==0 ) mp_new_randoms(mp);
3645   else decr(mp->j_random); }
3646
3647 @c 
3648 void mp_new_randoms (MP mp) {
3649   int k; /* index into |randoms| */
3650   fraction x; /* accumulator */
3651   for (k=0;k<=23;k++) { 
3652    x=mp->randoms[k]-mp->randoms[k+31];
3653     if ( x<0 ) x=x+fraction_one;
3654     mp->randoms[k]=x;
3655   }
3656   for (k=24;k<= 54;k++){ 
3657     x=mp->randoms[k]-mp->randoms[k-24];
3658     if ( x<0 ) x=x+fraction_one;
3659     mp->randoms[k]=x;
3660   }
3661   mp->j_random=54;
3662 }
3663
3664 @ @<Declarations@>=
3665 void mp_init_randoms (MP mp,scaled seed);
3666
3667 @ To initialize the |randoms| table, we call the following routine.
3668
3669 @c 
3670 void mp_init_randoms (MP mp,scaled seed) {
3671   fraction j,jj,k; /* more or less random integers */
3672   int i; /* index into |randoms| */
3673   j=abs(seed);
3674   while ( j>=fraction_one ) j=halfp(j);
3675   k=1;
3676   for (i=0;i<=54;i++ ){ 
3677     jj=k; k=j-k; j=jj;
3678     if ( k<0 ) k=k+fraction_one;
3679     mp->randoms[(i*21)% 55]=j;
3680   }
3681   mp_new_randoms(mp); 
3682   mp_new_randoms(mp); 
3683   mp_new_randoms(mp); /* ``warm up'' the array */
3684 }
3685
3686 @ To produce a uniform random number in the range |0<=u<x| or |0>=u>x|
3687 or |0=u=x|, given a |scaled| value~|x|, we proceed as shown here.
3688
3689 Note that the call of |take_fraction| will produce the values 0 and~|x|
3690 with about half the probability that it will produce any other particular
3691 values between 0 and~|x|, because it rounds its answers.
3692
3693 @c 
3694 scaled mp_unif_rand (MP mp,scaled x) {
3695   scaled y; /* trial value */
3696   next_random; y=mp_take_fraction(mp, abs(x),mp->randoms[mp->j_random]);
3697   if ( y==abs(x) ) return 0;
3698   else if ( x>0 ) return y;
3699   else return (-y);
3700 }
3701
3702 @ Finally, a normal deviate with mean zero and unit standard deviation
3703 can readily be obtained with the ratio method (Algorithm 3.4.1R in
3704 {\sl The Art of Computer Programming\/}).
3705
3706 @c 
3707 scaled mp_norm_rand (MP mp) {
3708   integer x,u,l; /* what the book would call $2^{16}X$, $2^{28}U$, and $-2^{24}\ln U$ */
3709   do { 
3710     do {  
3711       next_random;
3712       x=mp_take_fraction(mp, 112429,mp->randoms[mp->j_random]-fraction_half);
3713       /* $2^{16}\sqrt{8/e}\approx 112428.82793$ */
3714       next_random; u=mp->randoms[mp->j_random];
3715     } while (abs(x)>=u);
3716     x=mp_make_fraction(mp, x,u);
3717     l=139548960-mp_m_log(mp, u); /* $2^{24}\cdot12\ln2\approx139548959.6165$ */
3718   } while (mp_ab_vs_cd(mp, 1024,l,x,x)<0);
3719   return x;
3720 }
3721
3722 @* \[9] Packed data.
3723 In order to make efficient use of storage space, \MP\ bases its major data
3724 structures on a |memory_word|, which contains either a (signed) integer,
3725 possibly scaled, or a small number of fields that are one half or one
3726 quarter of the size used for storing integers.
3727
3728 If |x| is a variable of type |memory_word|, it contains up to four
3729 fields that can be referred to as follows:
3730 $$\vbox{\halign{\hfil#&#\hfil&#\hfil\cr
3731 |x|&.|int|&(an |integer|)\cr
3732 |x|&.|sc|\qquad&(a |scaled| integer)\cr
3733 |x.hh.lh|, |x.hh|&.|rh|&(two halfword fields)\cr
3734 |x.hh.b0|, |x.hh.b1|, |x.hh|&.|rh|&(two quarterword fields, one halfword
3735   field)\cr
3736 |x.qqqq.b0|, |x.qqqq.b1|, |x.qqqq|&.|b2|, |x.qqqq.b3|\hskip-100pt
3737   &\qquad\qquad\qquad(four quarterword fields)\cr}}$$
3738 This is somewhat cumbersome to write, and not very readable either, but
3739 macros will be used to make the notation shorter and more transparent.
3740 The code below gives a formal definition of |memory_word| and
3741 its subsidiary types, using packed variant records. \MP\ makes no
3742 assumptions about the relative positions of the fields within a word.
3743
3744 @d max_quarterword 0x3FFF /* largest allowable value in a |quarterword| */
3745 @d max_halfword 0xFFFFFFF /* largest allowable value in a |halfword| */
3746
3747 @ Here are the inequalities that the quarterword and halfword values
3748 must satisfy (or rather, the inequalities that they mustn't satisfy):
3749
3750 @<Check the ``constant''...@>=
3751 if (mp->ini_version) {
3752   if ( mp->mem_max!=mp->mem_top ) mp->bad=8;
3753 } else {
3754   if ( mp->mem_max<mp->mem_top ) mp->bad=8;
3755 }
3756 if ( max_quarterword<255 ) mp->bad=9;
3757 if ( max_halfword<65535 ) mp->bad=10;
3758 if ( max_quarterword>max_halfword ) mp->bad=11;
3759 if ( mp->mem_max>=max_halfword ) mp->bad=12;
3760 if ( mp->max_strings>max_halfword ) mp->bad=13;
3761
3762 @ The macros |qi| and |qo| are used for input to and output 
3763 from quarterwords. These are legacy macros.
3764 @^system dependencies@>
3765
3766 @d qo(A) (A) /* to read eight bits from a quarterword */
3767 @d qi(A) (A) /* to store eight bits in a quarterword */
3768
3769 @ The reader should study the following definitions closely:
3770 @^system dependencies@>
3771
3772 @d sc cint /* |scaled| data is equivalent to |integer| */
3773
3774 @<Types...@>=
3775 typedef short quarterword; /* 1/4 of a word */
3776 typedef int halfword; /* 1/2 of a word */
3777 typedef union {
3778   struct {
3779     halfword RH, LH;
3780   } v;
3781   struct { /* Make B0,B1 overlap the most significant bytes of LH.  */
3782     halfword junk;
3783     quarterword B0, B1;
3784   } u;
3785 } two_halves;
3786 typedef struct {
3787   struct {
3788     quarterword B2, B3, B0, B1;
3789   } u;
3790 } four_quarters;
3791 typedef union {
3792   two_halves hh;
3793   integer cint;
3794   four_quarters qqqq;
3795 } memory_word;
3796 #define b0 u.B0
3797 #define b1 u.B1
3798 #define b2 u.B2
3799 #define b3 u.B3
3800 #define rh v.RH
3801 #define lh v.LH
3802
3803 @ When debugging, we may want to print a |memory_word| without knowing
3804 what type it is; so we print it in all modes.
3805 @^debugging@>
3806
3807 @c 
3808 void mp_print_word (MP mp,memory_word w) {
3809   /* prints |w| in all ways */
3810   mp_print_int(mp, w.cint); mp_print_char(mp, ' ');
3811   mp_print_scaled(mp, w.sc); mp_print_char(mp, ' '); 
3812   mp_print_scaled(mp, w.sc / 010000); mp_print_ln(mp);
3813   mp_print_int(mp, w.hh.lh); mp_print_char(mp, '='); 
3814   mp_print_int(mp, w.hh.b0); mp_print_char(mp, ':');
3815   mp_print_int(mp, w.hh.b1); mp_print_char(mp, ';'); 
3816   mp_print_int(mp, w.hh.rh); mp_print_char(mp, ' ');
3817   mp_print_int(mp, w.qqqq.b0); mp_print_char(mp, ':'); 
3818   mp_print_int(mp, w.qqqq.b1); mp_print_char(mp, ':');
3819   mp_print_int(mp, w.qqqq.b2); mp_print_char(mp, ':'); 
3820   mp_print_int(mp, w.qqqq.b3);
3821 }
3822
3823
3824 @* \[10] Dynamic memory allocation.
3825
3826 The \MP\ system does nearly all of its own memory allocation, so that it
3827 can readily be transported into environments that do not have automatic
3828 facilities for strings, garbage collection, etc., and so that it can be in
3829 control of what error messages the user receives. The dynamic storage
3830 requirements of \MP\ are handled by providing a large array |mem| in
3831 which consecutive blocks of words are used as nodes by the \MP\ routines.
3832
3833 Pointer variables are indices into this array, or into another array
3834 called |eqtb| that will be explained later. A pointer variable might
3835 also be a special flag that lies outside the bounds of |mem|, so we
3836 allow pointers to assume any |halfword| value. The minimum memory
3837 index represents a null pointer.
3838
3839 @d null 0 /* the null pointer */
3840 @d mp_void (null+1) /* a null pointer different from |null| */
3841
3842
3843 @<Types...@>=
3844 typedef halfword pointer; /* a flag or a location in |mem| or |eqtb| */
3845
3846 @ The |mem| array is divided into two regions that are allocated separately,
3847 but the dividing line between these two regions is not fixed; they grow
3848 together until finding their ``natural'' size in a particular job.
3849 Locations less than or equal to |lo_mem_max| are used for storing
3850 variable-length records consisting of two or more words each. This region
3851 is maintained using an algorithm similar to the one described in exercise
3852 2.5--19 of {\sl The Art of Computer Programming}. However, no size field
3853 appears in the allocated nodes; the program is responsible for knowing the
3854 relevant size when a node is freed. Locations greater than or equal to
3855 |hi_mem_min| are used for storing one-word records; a conventional
3856 \.{AVAIL} stack is used for allocation in this region.
3857
3858 Locations of |mem| between |0| and |mem_top| may be dumped as part
3859 of preloaded mem files, by the \.{INIMP} preprocessor.
3860 @.INIMP@>
3861 Production versions of \MP\ may extend the memory at the top end in order to
3862 provide more space; these locations, between |mem_top| and |mem_max|,
3863 are always used for single-word nodes.
3864
3865 The key pointers that govern |mem| allocation have a prescribed order:
3866 $$\hbox{|null=0<lo_mem_max<hi_mem_min<mem_top<=mem_end<=mem_max|.}$$
3867
3868 @<Glob...@>=
3869 memory_word *mem; /* the big dynamic storage area */
3870 pointer lo_mem_max; /* the largest location of variable-size memory in use */
3871 pointer hi_mem_min; /* the smallest location of one-word memory in use */
3872
3873
3874
3875 @d xfree(A) do { mp_xfree(A); A=NULL; } while (0)
3876 @d xrealloc(P,A,B) mp_xrealloc(mp,P,A,B)
3877 @d xmalloc(A,B)  mp_xmalloc(mp,A,B)
3878 @d xstrdup(A)  mp_xstrdup(mp,A)
3879 @d XREALLOC(a,b,c) a = xrealloc(a,(b+1),sizeof(c));
3880
3881 @<Declare helpers@>=
3882 void mp_xfree (void *x);
3883 void *mp_xrealloc (MP mp, void *p, size_t nmem, size_t size) ;
3884 void *mp_xmalloc (MP mp, size_t nmem, size_t size) ;
3885 char *mp_xstrdup(MP mp, const char *s);
3886
3887 @ The |max_size_test| guards against overflow, on the assumption that
3888 |size_t| is at least 31bits wide.
3889
3890 @d max_size_test 0x7FFFFFFF
3891
3892 @c
3893 void mp_xfree (void *x) {
3894   if (x!=NULL) free(x);
3895 }
3896 void  *mp_xrealloc (MP mp, void *p, size_t nmem, size_t size) {
3897   void *w ; 
3898   if ((max_size_test/size)<nmem) {
3899     do_fprintf(mp->err_out,"Memory size overflow!\n");
3900     mp->history =mp_fatal_error_stop;    mp_jump_out(mp);
3901   }
3902   w = realloc (p,(nmem*size));
3903   if (w==NULL) {
3904     do_fprintf(mp->err_out,"Out of memory!\n");
3905     mp->history =mp_fatal_error_stop;    mp_jump_out(mp);
3906   }
3907   return w;
3908 }
3909 void  *mp_xmalloc (MP mp, size_t nmem, size_t size) {
3910   void *w;
3911   if ((max_size_test/size)<nmem) {
3912     do_fprintf(mp->err_out,"Memory size overflow!\n");
3913     mp->history =mp_fatal_error_stop;    mp_jump_out(mp);
3914   }
3915   w = malloc (nmem*size);
3916   if (w==NULL) {
3917     do_fprintf(mp->err_out,"Out of memory!\n");
3918     mp->history =mp_fatal_error_stop;    mp_jump_out(mp);
3919   }
3920   return w;
3921 }
3922 char *mp_xstrdup(MP mp, const char *s) {
3923   char *w; 
3924   if (s==NULL)
3925     return NULL;
3926   w = strdup(s);
3927   if (w==NULL) {
3928     do_fprintf(mp->err_out,"Out of memory!\n");
3929     mp->history =mp_fatal_error_stop;    mp_jump_out(mp);
3930   }
3931   return w;
3932 }
3933
3934
3935
3936 @<Allocate or initialize ...@>=
3937 mp->mem = xmalloc ((mp->mem_max+1),sizeof (memory_word));
3938 memset(mp->mem,0,(mp->mem_max+1)*sizeof (memory_word));
3939
3940 @ @<Dealloc variables@>=
3941 xfree(mp->mem);
3942
3943 @ Users who wish to study the memory requirements of particular applications can
3944 can use optional special features that keep track of current and
3945 maximum memory usage. When code between the delimiters |stat| $\ldots$
3946 |tats| is not ``commented out,'' \MP\ will run a bit slower but it will
3947 report these statistics when |mp_tracing_stats| is positive.
3948
3949 @<Glob...@>=
3950 integer var_used; integer dyn_used; /* how much memory is in use */
3951
3952 @ Let's consider the one-word memory region first, since it's the
3953 simplest. The pointer variable |mem_end| holds the highest-numbered location
3954 of |mem| that has ever been used. The free locations of |mem| that
3955 occur between |hi_mem_min| and |mem_end|, inclusive, are of type
3956 |two_halves|, and we write |info(p)| and |link(p)| for the |lh|
3957 and |rh| fields of |mem[p]| when it is of this type. The single-word
3958 free locations form a linked list
3959 $$|avail|,\;\hbox{|link(avail)|},\;\hbox{|link(link(avail))|},\;\ldots$$
3960 terminated by |null|.
3961
3962 @d link(A)   mp->mem[(A)].hh.rh /* the |link| field of a memory word */
3963 @d info(A)   mp->mem[(A)].hh.lh /* the |info| field of a memory word */
3964
3965 @<Glob...@>=
3966 pointer avail; /* head of the list of available one-word nodes */
3967 pointer mem_end; /* the last one-word node used in |mem| */
3968
3969 @ If one-word memory is exhausted, it might mean that the user has forgotten
3970 a token like `\&{enddef}' or `\&{endfor}'. We will define some procedures
3971 later that try to help pinpoint the trouble.
3972
3973 @c 
3974 @<Declare the procedure called |show_token_list|@>
3975 @<Declare the procedure called |runaway|@>
3976
3977 @ The function |get_avail| returns a pointer to a new one-word node whose
3978 |link| field is null. However, \MP\ will halt if there is no more room left.
3979 @^inner loop@>
3980
3981 @c 
3982 pointer mp_get_avail (MP mp) { /* single-word node allocation */
3983   pointer p; /* the new node being got */
3984   p=mp->avail; /* get top location in the |avail| stack */
3985   if ( p!=null ) {
3986     mp->avail=link(mp->avail); /* and pop it off */
3987   } else if ( mp->mem_end<mp->mem_max ) { /* or go into virgin territory */
3988     incr(mp->mem_end); p=mp->mem_end;
3989   } else { 
3990     decr(mp->hi_mem_min); p=mp->hi_mem_min;
3991     if ( mp->hi_mem_min<=mp->lo_mem_max ) { 
3992       mp_runaway(mp); /* if memory is exhausted, display possible runaway text */
3993       mp_overflow(mp, "main memory size",mp->mem_max);
3994       /* quit; all one-word nodes are busy */
3995 @:MetaPost capacity exceeded main memory size}{\quad main memory size@>
3996     }
3997   }
3998   link(p)=null; /* provide an oft-desired initialization of the new node */
3999   incr(mp->dyn_used);/* maintain statistics */
4000   return p;
4001 }
4002
4003 @ Conversely, a one-word node is recycled by calling |free_avail|.
4004
4005 @d free_avail(A)  /* single-word node liberation */
4006   { link((A))=mp->avail; mp->avail=(A); decr(mp->dyn_used);  }
4007
4008 @ There's also a |fast_get_avail| routine, which saves the procedure-call
4009 overhead at the expense of extra programming. This macro is used in
4010 the places that would otherwise account for the most calls of |get_avail|.
4011 @^inner loop@>
4012
4013 @d fast_get_avail(A) { 
4014   (A)=mp->avail; /* avoid |get_avail| if possible, to save time */
4015   if ( (A)==null ) { (A)=mp_get_avail(mp); } 
4016   else { mp->avail=link((A)); link((A))=null;  incr(mp->dyn_used); }
4017   }
4018
4019 @ The available-space list that keeps track of the variable-size portion
4020 of |mem| is a nonempty, doubly-linked circular list of empty nodes,
4021 pointed to by the roving pointer |rover|.
4022
4023 Each empty node has size 2 or more; the first word contains the special
4024 value |max_halfword| in its |link| field and the size in its |info| field;
4025 the second word contains the two pointers for double linking.
4026
4027 Each nonempty node also has size 2 or more. Its first word is of type
4028 |two_halves|\kern-1pt, and its |link| field is never equal to |max_halfword|.
4029 Otherwise there is complete flexibility with respect to the contents
4030 of its other fields and its other words.
4031
4032 (We require |mem_max<max_halfword| because terrible things can happen
4033 when |max_halfword| appears in the |link| field of a nonempty node.)
4034
4035 @d empty_flag   max_halfword /* the |link| of an empty variable-size node */
4036 @d is_empty(A)   (link((A))==empty_flag) /* tests for empty node */
4037 @d node_size   info /* the size field in empty variable-size nodes */
4038 @d llink(A)   info((A)+1) /* left link in doubly-linked list of empty nodes */
4039 @d rlink(A)   link((A)+1) /* right link in doubly-linked list of empty nodes */
4040
4041 @<Glob...@>=
4042 pointer rover; /* points to some node in the list of empties */
4043
4044 @ A call to |get_node| with argument |s| returns a pointer to a new node
4045 of size~|s|, which must be 2~or more. The |link| field of the first word
4046 of this new node is set to null. An overflow stop occurs if no suitable
4047 space exists.
4048
4049 If |get_node| is called with $s=2^{30}$, it simply merges adjacent free
4050 areas and returns the value |max_halfword|.
4051
4052 @<Internal library declarations@>=
4053 pointer mp_get_node (MP mp,integer s) ;
4054
4055 @ @c 
4056 pointer mp_get_node (MP mp,integer s) { /* variable-size node allocation */
4057   pointer p; /* the node currently under inspection */
4058   pointer q;  /* the node physically after node |p| */
4059   integer r; /* the newly allocated node, or a candidate for this honor */
4060   integer t,tt; /* temporary registers */
4061 @^inner loop@>
4062  RESTART: 
4063   p=mp->rover; /* start at some free node in the ring */
4064   do {  
4065     @<Try to allocate within node |p| and its physical successors,
4066      and |goto found| if allocation was possible@>;
4067     if (rlink(p)==null || (rlink(p)==p && p!=mp->rover)) {
4068       print_err("Free list garbled");
4069       help3("I found an entry in the list of free nodes that links")
4070        ("badly. I will try to ignore the broken link, but something")
4071        ("is seriously amiss. It is wise to warn the maintainers.")
4072           mp_error(mp);
4073       rlink(p)=mp->rover;
4074     }
4075         p=rlink(p); /* move to the next node in the ring */
4076   } while (p!=mp->rover); /* repeat until the whole list has been traversed */
4077   if ( s==010000000000 ) { 
4078     return max_halfword;
4079   };
4080   if ( mp->lo_mem_max+2<mp->hi_mem_min ) {
4081     if ( mp->lo_mem_max+2<=max_halfword ) {
4082       @<Grow more variable-size memory and |goto restart|@>;
4083     }
4084   }
4085   mp_overflow(mp, "main memory size",mp->mem_max);
4086   /* sorry, nothing satisfactory is left */
4087 @:MetaPost capacity exceeded main memory size}{\quad main memory size@>
4088 FOUND: 
4089   link(r)=null; /* this node is now nonempty */
4090   mp->var_used+=s; /* maintain usage statistics */
4091   return r;
4092 }
4093
4094 @ The lower part of |mem| grows by 1000 words at a time, unless
4095 we are very close to going under. When it grows, we simply link
4096 a new node into the available-space list. This method of controlled
4097 growth helps to keep the |mem| usage consecutive when \MP\ is
4098 implemented on ``virtual memory'' systems.
4099 @^virtual memory@>
4100
4101 @<Grow more variable-size memory and |goto restart|@>=
4102
4103   if ( mp->hi_mem_min-mp->lo_mem_max>=1998 ) {
4104     t=mp->lo_mem_max+1000;
4105   } else {
4106     t=mp->lo_mem_max+1+(mp->hi_mem_min-mp->lo_mem_max) / 2; 
4107     /* |lo_mem_max+2<=t<hi_mem_min| */
4108   }
4109   if ( t>max_halfword ) t=max_halfword;
4110   p=llink(mp->rover); q=mp->lo_mem_max; rlink(p)=q; llink(mp->rover)=q;
4111   rlink(q)=mp->rover; llink(q)=p; link(q)=empty_flag; 
4112   node_size(q)=t-mp->lo_mem_max;
4113   mp->lo_mem_max=t; link(mp->lo_mem_max)=null; info(mp->lo_mem_max)=null;
4114   mp->rover=q; 
4115   goto RESTART;
4116 }
4117
4118 @ @<Try to allocate...@>=
4119 q=p+node_size(p); /* find the physical successor */
4120 while ( is_empty(q) ) { /* merge node |p| with node |q| */
4121   t=rlink(q); tt=llink(q);
4122 @^inner loop@>
4123   if ( q==mp->rover ) mp->rover=t;
4124   llink(t)=tt; rlink(tt)=t;
4125   q=q+node_size(q);
4126 }
4127 r=q-s;
4128 if ( r>p+1 ) {
4129   @<Allocate from the top of node |p| and |goto found|@>;
4130 }
4131 if ( r==p ) { 
4132   if ( rlink(p)!=p ) {
4133     @<Allocate entire node |p| and |goto found|@>;
4134   }
4135 }
4136 node_size(p)=q-p /* reset the size in case it grew */
4137
4138 @ @<Allocate from the top...@>=
4139
4140   node_size(p)=r-p; /* store the remaining size */
4141   mp->rover=p; /* start searching here next time */
4142   goto FOUND;
4143 }
4144
4145 @ Here we delete node |p| from the ring, and let |rover| rove around.
4146
4147 @<Allocate entire...@>=
4148
4149   mp->rover=rlink(p); t=llink(p);
4150   llink(mp->rover)=t; rlink(t)=mp->rover;
4151   goto FOUND;
4152 }
4153
4154 @ Conversely, when some variable-size node |p| of size |s| is no longer needed,
4155 the operation |free_node(p,s)| will make its words available, by inserting
4156 |p| as a new empty node just before where |rover| now points.
4157
4158 @<Internal library declarations@>=
4159 void mp_free_node (MP mp, pointer p, halfword s) ;
4160
4161 @ @c 
4162 void mp_free_node (MP mp, pointer p, halfword s) { /* variable-size node
4163   liberation */
4164   pointer q; /* |llink(rover)| */
4165   node_size(p)=s; link(p)=empty_flag;
4166 @^inner loop@>
4167   q=llink(mp->rover); llink(p)=q; rlink(p)=mp->rover; /* set both links */
4168   llink(mp->rover)=p; rlink(q)=p; /* insert |p| into the ring */
4169   mp->var_used-=s; /* maintain statistics */
4170 }
4171
4172 @ Just before \.{INIMP} writes out the memory, it sorts the doubly linked
4173 available space list. The list is probably very short at such times, so a
4174 simple insertion sort is used. The smallest available location will be
4175 pointed to by |rover|, the next-smallest by |rlink(rover)|, etc.
4176
4177 @c 
4178 void mp_sort_avail (MP mp) { /* sorts the available variable-size nodes
4179   by location */
4180   pointer p,q,r; /* indices into |mem| */
4181   pointer old_rover; /* initial |rover| setting */
4182   p=mp_get_node(mp, 010000000000); /* merge adjacent free areas */
4183   p=rlink(mp->rover); rlink(mp->rover)=max_halfword; old_rover=mp->rover;
4184   while ( p!=old_rover ) {
4185     @<Sort |p| into the list starting at |rover|
4186      and advance |p| to |rlink(p)|@>;
4187   }
4188   p=mp->rover;
4189   while ( rlink(p)!=max_halfword ) { 
4190     llink(rlink(p))=p; p=rlink(p);
4191   };
4192   rlink(p)=mp->rover; llink(mp->rover)=p;
4193 }
4194
4195 @ The following |while| loop is guaranteed to
4196 terminate, since the list that starts at
4197 |rover| ends with |max_halfword| during the sorting procedure.
4198
4199 @<Sort |p|...@>=
4200 if ( p<mp->rover ) { 
4201   q=p; p=rlink(q); rlink(q)=mp->rover; mp->rover=q;
4202 } else  { 
4203   q=mp->rover;
4204   while ( rlink(q)<p ) q=rlink(q);
4205   r=rlink(p); rlink(p)=rlink(q); rlink(q)=p; p=r;
4206 }
4207
4208 @* \[11] Memory layout.
4209 Some areas of |mem| are dedicated to fixed usage, since static allocation is
4210 more efficient than dynamic allocation when we can get away with it. For
4211 example, locations |0| to |1| are always used to store a
4212 two-word dummy token whose second word is zero.
4213 The following macro definitions accomplish the static allocation by giving
4214 symbolic names to the fixed positions. Static variable-size nodes appear
4215 in locations |0| through |lo_mem_stat_max|, and static single-word nodes
4216 appear in locations |hi_mem_stat_min| through |mem_top|, inclusive.
4217
4218 @d null_dash (2) /* the first two words are reserved for a null value */
4219 @d dep_head (null_dash+3) /* we will define |dash_node_size=3| */
4220 @d zero_val (dep_head+2) /* two words for a permanently zero value */
4221 @d temp_val (zero_val+2) /* two words for a temporary value node */
4222 @d end_attr temp_val /* we use |end_attr+2| only */
4223 @d inf_val (end_attr+2) /* and |inf_val+1| only */
4224 @d test_pen (inf_val+2)
4225   /* nine words for a pen used when testing the turning number */
4226 @d bad_vardef (test_pen+9) /* two words for \&{vardef} error recovery */
4227 @d lo_mem_stat_max (bad_vardef+1)  /* largest statically
4228   allocated word in the variable-size |mem| */
4229 @#
4230 @d sentinel mp->mem_top /* end of sorted lists */
4231 @d temp_head (mp->mem_top-1) /* head of a temporary list of some kind */
4232 @d hold_head (mp->mem_top-2) /* head of a temporary list of another kind */
4233 @d spec_head (mp->mem_top-3) /* head of a list of unprocessed \&{special} items */
4234 @d hi_mem_stat_min (mp->mem_top-3) /* smallest statically allocated word in
4235   the one-word |mem| */
4236
4237 @ The following code gets the dynamic part of |mem| off to a good start,
4238 when \MP\ is initializing itself the slow way.
4239
4240 @<Initialize table entries (done by \.{INIMP} only)@>=
4241 mp->rover=lo_mem_stat_max+1; /* initialize the dynamic memory */
4242 link(mp->rover)=empty_flag;
4243 node_size(mp->rover)=1000; /* which is a 1000-word available node */
4244 llink(mp->rover)=mp->rover; rlink(mp->rover)=mp->rover;
4245 mp->lo_mem_max=mp->rover+1000; 
4246 link(mp->lo_mem_max)=null; info(mp->lo_mem_max)=null;
4247 for (k=hi_mem_stat_min;k<=(int)mp->mem_top;k++) {
4248   mp->mem[k]=mp->mem[mp->lo_mem_max]; /* clear list heads */
4249 }
4250 mp->avail=null; mp->mem_end=mp->mem_top;
4251 mp->hi_mem_min=hi_mem_stat_min; /* initialize the one-word memory */
4252 mp->var_used=lo_mem_stat_max+1; 
4253 mp->dyn_used=mp->mem_top+1-(hi_mem_stat_min);  /* initialize statistics */
4254 @<Initialize a pen at |test_pen| so that it fits in nine words@>;
4255
4256 @ The procedure |flush_list(p)| frees an entire linked list of one-word
4257 nodes that starts at a given position, until coming to |sentinel| or a
4258 pointer that is not in the one-word region. Another procedure,
4259 |flush_node_list|, frees an entire linked list of one-word and two-word
4260 nodes, until coming to a |null| pointer.
4261 @^inner loop@>
4262
4263 @c 
4264 void mp_flush_list (MP mp,pointer p) { /* makes list of single-word nodes  available */
4265   pointer q,r; /* list traversers */
4266   if ( p>=mp->hi_mem_min ) if ( p!=sentinel ) { 
4267     r=p;
4268     do {  
4269       q=r; r=link(r); 
4270       decr(mp->dyn_used);
4271       if ( r<mp->hi_mem_min ) break;
4272     } while (r!=sentinel);
4273   /* now |q| is the last node on the list */
4274     link(q)=mp->avail; mp->avail=p;
4275   }
4276 }
4277 @#
4278 void mp_flush_node_list (MP mp,pointer p) {
4279   pointer q; /* the node being recycled */
4280   while ( p!=null ){ 
4281     q=p; p=link(p);
4282     if ( q<mp->hi_mem_min ) 
4283       mp_free_node(mp, q,2);
4284     else 
4285       free_avail(q);
4286   }
4287 }
4288
4289 @ If \MP\ is extended improperly, the |mem| array might get screwed up.
4290 For example, some pointers might be wrong, or some ``dead'' nodes might not
4291 have been freed when the last reference to them disappeared. Procedures
4292 |check_mem| and |search_mem| are available to help diagnose such
4293 problems. These procedures make use of two arrays called |free| and
4294 |was_free| that are present only if \MP's debugging routines have
4295 been included. (You may want to decrease the size of |mem| while you
4296 @^debugging@>
4297 are debugging.)
4298
4299 Because |boolean|s are typedef-d as ints, it is better to use
4300 unsigned chars here.
4301
4302 @<Glob...@>=
4303 unsigned char *free; /* free cells */
4304 unsigned char *was_free; /* previously free cells */
4305 pointer was_mem_end; pointer was_lo_max; pointer was_hi_min;
4306   /* previous |mem_end|, |lo_mem_max|,and |hi_mem_min| */
4307 boolean panicking; /* do we want to check memory constantly? */
4308
4309 @ @<Allocate or initialize ...@>=
4310 mp->free = xmalloc ((mp->mem_max+1),sizeof (unsigned char));
4311 mp->was_free = xmalloc ((mp->mem_max+1), sizeof (unsigned char));
4312
4313 @ @<Dealloc variables@>=
4314 xfree(mp->free);
4315 xfree(mp->was_free);
4316
4317 @ @<Allocate or ...@>=
4318 mp->was_mem_end=0; /* indicate that everything was previously free */
4319 mp->was_lo_max=0; mp->was_hi_min=mp->mem_max;
4320 mp->panicking=false;
4321
4322 @ @<Declare |mp_reallocate| functions@>=
4323 void mp_reallocate_memory(MP mp, int l) ;
4324
4325 @ @c
4326 void mp_reallocate_memory(MP mp, int l) {
4327    XREALLOC(mp->free,     l, unsigned char);
4328    XREALLOC(mp->was_free, l, unsigned char);
4329    if (mp->mem) {
4330          int newarea = l-mp->mem_max;
4331      XREALLOC(mp->mem,      l, memory_word);
4332      memset (mp->mem+(mp->mem_max+1),0,sizeof(memory_word)*(newarea));
4333    } else {
4334      XREALLOC(mp->mem,      l, memory_word);
4335      memset(mp->mem,0,sizeof(memory_word)*(l+1));
4336    }
4337    mp->mem_max = l;
4338    if (mp->ini_version) 
4339      mp->mem_top = l;
4340 }
4341
4342
4343
4344 @ Procedure |check_mem| makes sure that the available space lists of
4345 |mem| are well formed, and it optionally prints out all locations
4346 that are reserved now but were free the last time this procedure was called.
4347
4348 @c 
4349 void mp_check_mem (MP mp,boolean print_locs ) {
4350   pointer p,q,r; /* current locations of interest in |mem| */
4351   boolean clobbered; /* is something amiss? */
4352   for (p=0;p<=mp->lo_mem_max;p++) {
4353     mp->free[p]=false; /* you can probably do this faster */
4354   }
4355   for (p=mp->hi_mem_min;p<= mp->mem_end;p++) {
4356     mp->free[p]=false; /* ditto */
4357   }
4358   @<Check single-word |avail| list@>;
4359   @<Check variable-size |avail| list@>;
4360   @<Check flags of unavailable nodes@>;
4361   @<Check the list of linear dependencies@>;
4362   if ( print_locs ) {
4363     @<Print newly busy locations@>;
4364   }
4365   memcpy(mp->was_free,mp->free, sizeof(char)*(mp->mem_end+1));
4366   mp->was_mem_end=mp->mem_end; 
4367   mp->was_lo_max=mp->lo_mem_max; 
4368   mp->was_hi_min=mp->hi_mem_min;
4369 }
4370
4371 @ @<Check single-word...@>=
4372 p=mp->avail; q=null; clobbered=false;
4373 while ( p!=null ) { 
4374   if ( (p>mp->mem_end)||(p<mp->hi_mem_min) ) clobbered=true;
4375   else if ( mp->free[p] ) clobbered=true;
4376   if ( clobbered ) { 
4377     mp_print_nl(mp, "AVAIL list clobbered at ");
4378 @.AVAIL list clobbered...@>
4379     mp_print_int(mp, q); break;
4380   }
4381   mp->free[p]=true; q=p; p=link(q);
4382 }
4383
4384 @ @<Check variable-size...@>=
4385 p=mp->rover; q=null; clobbered=false;
4386 do {  
4387   if ( (p>=mp->lo_mem_max)||(p<0) ) clobbered=true;
4388   else if ( (rlink(p)>=mp->lo_mem_max)||(rlink(p)<0) ) clobbered=true;
4389   else if (  !(is_empty(p))||(node_size(p)<2)||
4390    (p+node_size(p)>mp->lo_mem_max)|| (llink(rlink(p))!=p) ) clobbered=true;
4391   if ( clobbered ) { 
4392     mp_print_nl(mp, "Double-AVAIL list clobbered at ");
4393 @.Double-AVAIL list clobbered...@>
4394     mp_print_int(mp, q); break;
4395   }
4396   for (q=p;q<=p+node_size(p)-1;q++) { /* mark all locations free */
4397     if ( mp->free[q] ) { 
4398       mp_print_nl(mp, "Doubly free location at ");
4399 @.Doubly free location...@>
4400       mp_print_int(mp, q); break;
4401     }
4402     mp->free[q]=true;
4403   }
4404   q=p; p=rlink(p);
4405 } while (p!=mp->rover)
4406
4407
4408 @ @<Check flags...@>=
4409 p=0;
4410 while ( p<=mp->lo_mem_max ) { /* node |p| should not be empty */
4411   if ( is_empty(p) ) {
4412     mp_print_nl(mp, "Bad flag at "); mp_print_int(mp, p);
4413 @.Bad flag...@>
4414   }
4415   while ( (p<=mp->lo_mem_max) && ! mp->free[p] ) incr(p);
4416   while ( (p<=mp->lo_mem_max) && mp->free[p] ) incr(p);
4417 }
4418
4419 @ @<Print newly busy...@>=
4420
4421   @<Do intialization required before printing new busy locations@>;
4422   mp_print_nl(mp, "New busy locs:");
4423 @.New busy locs@>
4424   for (p=0;p<= mp->lo_mem_max;p++ ) {
4425     if ( ! mp->free[p] && ((p>mp->was_lo_max) || mp->was_free[p]) ) {
4426       @<Indicate that |p| is a new busy location@>;
4427     }
4428   }
4429   for (p=mp->hi_mem_min;p<=mp->mem_end;p++ ) {
4430     if ( ! mp->free[p] &&
4431         ((p<mp->was_hi_min) || (p>mp->was_mem_end) || mp->was_free[p]) ) {
4432       @<Indicate that |p| is a new busy location@>;
4433     }
4434   }
4435   @<Finish printing new busy locations@>;
4436 }
4437
4438 @ There might be many new busy locations so we are careful to print contiguous
4439 blocks compactly.  During this operation |q| is the last new busy location and
4440 |r| is the start of the block containing |q|.
4441
4442 @<Indicate that |p| is a new busy location@>=
4443
4444   if ( p>q+1 ) { 
4445     if ( q>r ) { 
4446       mp_print(mp, ".."); mp_print_int(mp, q);
4447     }
4448     mp_print_char(mp, ' '); mp_print_int(mp, p);
4449     r=p;
4450   }
4451   q=p;
4452 }
4453
4454 @ @<Do intialization required before printing new busy locations@>=
4455 q=mp->mem_max; r=mp->mem_max
4456
4457 @ @<Finish printing new busy locations@>=
4458 if ( q>r ) { 
4459   mp_print(mp, ".."); mp_print_int(mp, q);
4460 }
4461
4462 @ The |search_mem| procedure attempts to answer the question ``Who points
4463 to node~|p|?'' In doing so, it fetches |link| and |info| fields of |mem|
4464 that might not be of type |two_halves|. Strictly speaking, this is
4465 undefined, and it can lead to ``false drops'' (words that seem to
4466 point to |p| purely by coincidence). But for debugging purposes, we want
4467 to rule out the places that do {\sl not\/} point to |p|, so a few false
4468 drops are tolerable.
4469
4470 @c
4471 void mp_search_mem (MP mp, pointer p) { /* look for pointers to |p| */
4472   integer q; /* current position being searched */
4473   for (q=0;q<=mp->lo_mem_max;q++) { 
4474     if ( link(q)==p ){ 
4475       mp_print_nl(mp, "LINK("); mp_print_int(mp, q); mp_print_char(mp, ')');
4476     }
4477     if ( info(q)==p ) { 
4478       mp_print_nl(mp, "INFO("); mp_print_int(mp, q); mp_print_char(mp, ')');
4479     }
4480   }
4481   for (q=mp->hi_mem_min;q<=mp->mem_end;q++) {
4482     if ( link(q)==p ) {
4483       mp_print_nl(mp, "LINK("); mp_print_int(mp, q); mp_print_char(mp, ')');
4484     }
4485     if ( info(q)==p ) {
4486       mp_print_nl(mp, "INFO("); mp_print_int(mp, q); mp_print_char(mp, ')');
4487     }
4488   }
4489   @<Search |eqtb| for equivalents equal to |p|@>;
4490 }
4491
4492 @* \[12] The command codes.
4493 Before we can go much further, we need to define symbolic names for the internal
4494 code numbers that represent the various commands obeyed by \MP. These codes
4495 are somewhat arbitrary, but not completely so. For example,
4496 some codes have been made adjacent so that |case| statements in the
4497 program need not consider cases that are widely spaced, or so that |case|
4498 statements can be replaced by |if| statements. A command can begin an
4499 expression if and only if its code lies between |min_primary_command| and
4500 |max_primary_command|, inclusive. The first token of a statement that doesn't
4501 begin with an expression has a command code between |min_command| and
4502 |max_statement_command|, inclusive. Anything less than |min_command| is
4503 eliminated during macro expansions, and anything no more than |max_pre_command|
4504 is eliminated when expanding \TeX\ material.  Ranges such as
4505 |min_secondary_command..max_secondary_command| are used when parsing
4506 expressions, but the relative ordering within such a range is generally not
4507 critical.
4508
4509 The ordering of the highest-numbered commands
4510 (|comma<semicolon<end_group<stop|) is crucial for the parsing and
4511 error-recovery methods of this program as is the ordering |if_test<fi_or_else|
4512 for the smallest two commands.  The ordering is also important in the ranges
4513 |numeric_token..plus_or_minus| and |left_brace..ampersand|.
4514
4515 At any rate, here is the list, for future reference.
4516
4517 @d start_tex 1 /* begin \TeX\ material (\&{btex}, \&{verbatimtex}) */
4518 @d etex_marker 2 /* end \TeX\ material (\&{etex}) */
4519 @d mpx_break 3 /* stop reading an \.{MPX} file (\&{mpxbreak}) */
4520 @d max_pre_command mpx_break
4521 @d if_test 4 /* conditional text (\&{if}) */
4522 @d fi_or_else 5 /* delimiters for conditionals (\&{elseif}, \&{else}, \&{fi}) */
4523 @d input 6 /* input a source file (\&{input}, \&{endinput}) */
4524 @d iteration 7 /* iterate (\&{for}, \&{forsuffixes}, \&{forever}, \&{endfor}) */
4525 @d repeat_loop 8 /* special command substituted for \&{endfor} */
4526 @d exit_test 9 /* premature exit from a loop (\&{exitif}) */
4527 @d relax 10 /* do nothing (\.{\char`\\}) */
4528 @d scan_tokens 11 /* put a string into the input buffer */
4529 @d expand_after 12 /* look ahead one token */
4530 @d defined_macro 13 /* a macro defined by the user */
4531 @d min_command (defined_macro+1)
4532 @d save_command 14 /* save a list of tokens (\&{save}) */
4533 @d interim_command 15 /* save an internal quantity (\&{interim}) */
4534 @d let_command 16 /* redefine a symbolic token (\&{let}) */
4535 @d new_internal 17 /* define a new internal quantity (\&{newinternal}) */
4536 @d macro_def 18 /* define a macro (\&{def}, \&{vardef}, etc.) */
4537 @d ship_out_command 19 /* output a character (\&{shipout}) */
4538 @d add_to_command 20 /* add to edges (\&{addto}) */
4539 @d bounds_command 21  /* add bounding path to edges (\&{setbounds}, \&{clip}) */
4540 @d tfm_command 22 /* command for font metric info (\&{ligtable}, etc.) */
4541 @d protection_command 23 /* set protection flag (\&{outer}, \&{inner}) */
4542 @d show_command 24 /* diagnostic output (\&{show}, \&{showvariable}, etc.) */
4543 @d mode_command 25 /* set interaction level (\&{batchmode}, etc.) */
4544 @d mp_random_seed 26 /* initialize random number generator (\&{randomseed}) */
4545 @d message_command 27 /* communicate to user (\&{message}, \&{errmessage}) */
4546 @d every_job_command 28 /* designate a starting token (\&{everyjob}) */
4547 @d delimiters 29 /* define a pair of delimiters (\&{delimiters}) */
4548 @d special_command 30 /* output special info (\&{special})
4549                        or font map info (\&{fontmapfile}, \&{fontmapline}) */
4550 @d write_command 31 /* write text to a file (\&{write}) */
4551 @d type_name 32 /* declare a type (\&{numeric}, \&{pair}, etc.) */
4552 @d max_statement_command type_name
4553 @d min_primary_command type_name
4554 @d left_delimiter 33 /* the left delimiter of a matching pair */
4555 @d begin_group 34 /* beginning of a group (\&{begingroup}) */
4556 @d nullary 35 /* an operator without arguments (e.g., \&{normaldeviate}) */
4557 @d unary 36 /* an operator with one argument (e.g., \&{sqrt}) */
4558 @d str_op 37 /* convert a suffix to a string (\&{str}) */
4559 @d cycle 38 /* close a cyclic path (\&{cycle}) */
4560 @d primary_binary 39 /* binary operation taking `\&{of}' (e.g., \&{point}) */
4561 @d capsule_token 40 /* a value that has been put into a token list */
4562 @d string_token 41 /* a string constant (e.g., |"hello"|) */
4563 @d internal_quantity 42 /* internal numeric parameter (e.g., \&{pausing}) */
4564 @d min_suffix_token internal_quantity
4565 @d tag_token 43 /* a symbolic token without a primitive meaning */
4566 @d numeric_token 44 /* a numeric constant (e.g., \.{3.14159}) */
4567 @d max_suffix_token numeric_token
4568 @d plus_or_minus 45 /* either `\.+' or `\.-' */
4569 @d max_primary_command plus_or_minus /* should also be |numeric_token+1| */
4570 @d min_tertiary_command plus_or_minus
4571 @d tertiary_secondary_macro 46 /* a macro defined by \&{secondarydef} */
4572 @d tertiary_binary 47 /* an operator at the tertiary level (e.g., `\.{++}') */
4573 @d max_tertiary_command tertiary_binary
4574 @d left_brace 48 /* the operator `\.{\char`\{}' */
4575 @d min_expression_command left_brace
4576 @d path_join 49 /* the operator `\.{..}' */
4577 @d ampersand 50 /* the operator `\.\&' */
4578 @d expression_tertiary_macro 51 /* a macro defined by \&{tertiarydef} */
4579 @d expression_binary 52 /* an operator at the expression level (e.g., `\.<') */
4580 @d equals 53 /* the operator `\.=' */
4581 @d max_expression_command equals
4582 @d and_command 54 /* the operator `\&{and}' */
4583 @d min_secondary_command and_command
4584 @d secondary_primary_macro 55 /* a macro defined by \&{primarydef} */
4585 @d slash 56 /* the operator `\./' */
4586 @d secondary_binary 57 /* an operator at the binary level (e.g., \&{shifted}) */
4587 @d max_secondary_command secondary_binary
4588 @d param_type 58 /* type of parameter (\&{primary}, \&{expr}, \&{suffix}, etc.) */
4589 @d controls 59 /* specify control points explicitly (\&{controls}) */
4590 @d tension 60 /* specify tension between knots (\&{tension}) */
4591 @d at_least 61 /* bounded tension value (\&{atleast}) */
4592 @d curl_command 62 /* specify curl at an end knot (\&{curl}) */
4593 @d macro_special 63 /* special macro operators (\&{quote}, \.{\#\AT!}, etc.) */
4594 @d right_delimiter 64 /* the right delimiter of a matching pair */
4595 @d left_bracket 65 /* the operator `\.[' */
4596 @d right_bracket 66 /* the operator `\.]' */
4597 @d right_brace 67 /* the operator `\.{\char`\}}' */
4598 @d with_option 68 /* option for filling (\&{withpen}, \&{withweight}, etc.) */
4599 @d thing_to_add 69
4600   /* variant of \&{addto} (\&{contour}, \&{doublepath}, \&{also}) */
4601 @d of_token 70 /* the operator `\&{of}' */
4602 @d to_token 71 /* the operator `\&{to}' */
4603 @d step_token 72 /* the operator `\&{step}' */
4604 @d until_token 73 /* the operator `\&{until}' */
4605 @d within_token 74 /* the operator `\&{within}' */
4606 @d lig_kern_token 75
4607   /* the operators `\&{kern}' and `\.{=:}' and `\.{=:\char'174}', etc. */
4608 @d assignment 76 /* the operator `\.{:=}' */
4609 @d skip_to 77 /* the operation `\&{skipto}' */
4610 @d bchar_label 78 /* the operator `\.{\char'174\char'174:}' */
4611 @d double_colon 79 /* the operator `\.{::}' */
4612 @d colon 80 /* the operator `\.:' */
4613 @#
4614 @d comma 81 /* the operator `\.,', must be |colon+1| */
4615 @d end_of_statement (mp->cur_cmd>comma)
4616 @d semicolon 82 /* the operator `\.;', must be |comma+1| */
4617 @d end_group 83 /* end a group (\&{endgroup}), must be |semicolon+1| */
4618 @d stop 84 /* end a job (\&{end}, \&{dump}), must be |end_group+1| */
4619 @d max_command_code stop
4620 @d outer_tag (max_command_code+1) /* protection code added to command code */
4621
4622 @<Types...@>=
4623 typedef int command_code;
4624
4625 @ Variables and capsules in \MP\ have a variety of ``types,''
4626 distinguished by the code numbers defined here. These numbers are also
4627 not completely arbitrary.  Things that get expanded must have types
4628 |>mp_independent|; a type remaining after expansion is numeric if and only if
4629 its code number is at least |numeric_type|; objects containing numeric
4630 parts must have types between |transform_type| and |pair_type|;
4631 all other types must be smaller than |transform_type|; and among the types
4632 that are not unknown or vacuous, the smallest two must be |boolean_type|
4633 and |string_type| in that order.
4634  
4635 @d undefined 0 /* no type has been declared */
4636 @d unknown_tag 1 /* this constant is added to certain type codes below */
4637 @d unknown_types mp_unknown_boolean: case mp_unknown_string:
4638   case mp_unknown_pen: case mp_unknown_picture: case mp_unknown_path
4639
4640 @<Types...@>=
4641 enum mp_variable_type {
4642 mp_vacuous=1, /* no expression was present */
4643 mp_boolean_type, /* \&{boolean} with a known value */
4644 mp_unknown_boolean,
4645 mp_string_type, /* \&{string} with a known value */
4646 mp_unknown_string,
4647 mp_pen_type, /* \&{pen} with a known value */
4648 mp_unknown_pen,
4649 mp_path_type, /* \&{path} with a known value */
4650 mp_unknown_path,
4651 mp_picture_type, /* \&{picture} with a known value */
4652 mp_unknown_picture,
4653 mp_transform_type, /* \&{transform} variable or capsule */
4654 mp_color_type, /* \&{color} variable or capsule */
4655 mp_cmykcolor_type, /* \&{cmykcolor} variable or capsule */
4656 mp_pair_type, /* \&{pair} variable or capsule */
4657 mp_numeric_type, /* variable that has been declared \&{numeric} but not used */
4658 mp_known, /* \&{numeric} with a known value */
4659 mp_dependent, /* a linear combination with |fraction| coefficients */
4660 mp_proto_dependent, /* a linear combination with |scaled| coefficients */
4661 mp_independent, /* \&{numeric} with unknown value */
4662 mp_token_list, /* variable name or suffix argument or text argument */
4663 mp_structured, /* variable with subscripts and attributes */
4664 mp_unsuffixed_macro, /* variable defined with \&{vardef} but no \.{\AT!\#} */
4665 mp_suffixed_macro /* variable defined with \&{vardef} and \.{\AT!\#} */
4666 } ;
4667
4668 @ @<Declarations@>=
4669 void mp_print_type (MP mp,small_number t) ;
4670
4671 @ @<Basic printing procedures@>=
4672 void mp_print_type (MP mp,small_number t) { 
4673   switch (t) {
4674   case mp_vacuous:mp_print(mp, "mp_vacuous"); break;
4675   case mp_boolean_type:mp_print(mp, "boolean"); break;
4676   case mp_unknown_boolean:mp_print(mp, "unknown boolean"); break;
4677   case mp_string_type:mp_print(mp, "string"); break;
4678   case mp_unknown_string:mp_print(mp, "unknown string"); break;
4679   case mp_pen_type:mp_print(mp, "pen"); break;
4680   case mp_unknown_pen:mp_print(mp, "unknown pen"); break;
4681   case mp_path_type:mp_print(mp, "path"); break;
4682   case mp_unknown_path:mp_print(mp, "unknown path"); break;
4683   case mp_picture_type:mp_print(mp, "picture"); break;
4684   case mp_unknown_picture:mp_print(mp, "unknown picture"); break;
4685   case mp_transform_type:mp_print(mp, "transform"); break;
4686   case mp_color_type:mp_print(mp, "color"); break;
4687   case mp_cmykcolor_type:mp_print(mp, "cmykcolor"); break;
4688   case mp_pair_type:mp_print(mp, "pair"); break;
4689   case mp_known:mp_print(mp, "known numeric"); break;
4690   case mp_dependent:mp_print(mp, "dependent"); break;
4691   case mp_proto_dependent:mp_print(mp, "proto-dependent"); break;
4692   case mp_numeric_type:mp_print(mp, "numeric"); break;
4693   case mp_independent:mp_print(mp, "independent"); break;
4694   case mp_token_list:mp_print(mp, "token list"); break;
4695   case mp_structured:mp_print(mp, "mp_structured"); break;
4696   case mp_unsuffixed_macro:mp_print(mp, "unsuffixed macro"); break;
4697   case mp_suffixed_macro:mp_print(mp, "suffixed macro"); break;
4698   default: mp_print(mp, "undefined"); break;
4699   }
4700 }
4701
4702 @ Values inside \MP\ are stored in two-word nodes that have a |name_type|
4703 as well as a |type|. The possibilities for |name_type| are defined
4704 here; they will be explained in more detail later.
4705
4706 @<Types...@>=
4707 enum mp_name_type {
4708  mp_root=0, /* |name_type| at the top level of a variable */
4709  mp_saved_root, /* same, when the variable has been saved */
4710  mp_structured_root, /* |name_type| where a |mp_structured| branch occurs */
4711  mp_subscr, /* |name_type| in a subscript node */
4712  mp_attr, /* |name_type| in an attribute node */
4713  mp_x_part_sector, /* |name_type| in the \&{xpart} of a node */
4714  mp_y_part_sector, /* |name_type| in the \&{ypart} of a node */
4715  mp_xx_part_sector, /* |name_type| in the \&{xxpart} of a node */
4716  mp_xy_part_sector, /* |name_type| in the \&{xypart} of a node */
4717  mp_yx_part_sector, /* |name_type| in the \&{yxpart} of a node */
4718  mp_yy_part_sector, /* |name_type| in the \&{yypart} of a node */
4719  mp_red_part_sector, /* |name_type| in the \&{redpart} of a node */
4720  mp_green_part_sector, /* |name_type| in the \&{greenpart} of a node */
4721  mp_blue_part_sector, /* |name_type| in the \&{bluepart} of a node */
4722  mp_cyan_part_sector, /* |name_type| in the \&{redpart} of a node */
4723  mp_magenta_part_sector, /* |name_type| in the \&{greenpart} of a node */
4724  mp_yellow_part_sector, /* |name_type| in the \&{bluepart} of a node */
4725  mp_black_part_sector, /* |name_type| in the \&{greenpart} of a node */
4726  mp_grey_part_sector, /* |name_type| in the \&{bluepart} of a node */
4727  mp_capsule, /* |name_type| in stashed-away subexpressions */
4728  mp_token  /* |name_type| in a numeric token or string token */
4729 };
4730
4731 @ Primitive operations that produce values have a secondary identification
4732 code in addition to their command code; it's something like genera and species.
4733 For example, `\.*' has the command code |primary_binary|, and its
4734 secondary identification is |times|. The secondary codes start at 30 so that
4735 they don't overlap with the type codes; some type codes (e.g., |mp_string_type|)
4736 are used as operators as well as type identifications.  The relative values
4737 are not critical, except for |true_code..false_code|, |or_op..and_op|,
4738 and |filled_op..bounded_op|.  The restrictions are that
4739 |and_op-false_code=or_op-true_code|, that the ordering of
4740 |x_part...blue_part| must match that of |x_part_sector..mp_blue_part_sector|,
4741 and the ordering of |filled_op..bounded_op| must match that of the code
4742 values they test for.
4743
4744 @d true_code 30 /* operation code for \.{true} */
4745 @d false_code 31 /* operation code for \.{false} */
4746 @d null_picture_code 32 /* operation code for \.{nullpicture} */
4747 @d null_pen_code 33 /* operation code for \.{nullpen} */
4748 @d job_name_op 34 /* operation code for \.{jobname} */
4749 @d read_string_op 35 /* operation code for \.{readstring} */
4750 @d pen_circle 36 /* operation code for \.{pencircle} */
4751 @d normal_deviate 37 /* operation code for \.{normaldeviate} */
4752 @d read_from_op 38 /* operation code for \.{readfrom} */
4753 @d close_from_op 39 /* operation code for \.{closefrom} */
4754 @d odd_op 40 /* operation code for \.{odd} */
4755 @d known_op 41 /* operation code for \.{known} */
4756 @d unknown_op 42 /* operation code for \.{unknown} */
4757 @d not_op 43 /* operation code for \.{not} */
4758 @d decimal 44 /* operation code for \.{decimal} */
4759 @d reverse 45 /* operation code for \.{reverse} */
4760 @d make_path_op 46 /* operation code for \.{makepath} */
4761 @d make_pen_op 47 /* operation code for \.{makepen} */
4762 @d oct_op 48 /* operation code for \.{oct} */
4763 @d hex_op 49 /* operation code for \.{hex} */
4764 @d ASCII_op 50 /* operation code for \.{ASCII} */
4765 @d char_op 51 /* operation code for \.{char} */
4766 @d length_op 52 /* operation code for \.{length} */
4767 @d turning_op 53 /* operation code for \.{turningnumber} */
4768 @d color_model_part 54 /* operation code for \.{colormodel} */
4769 @d x_part 55 /* operation code for \.{xpart} */
4770 @d y_part 56 /* operation code for \.{ypart} */
4771 @d xx_part 57 /* operation code for \.{xxpart} */
4772 @d xy_part 58 /* operation code for \.{xypart} */
4773 @d yx_part 59 /* operation code for \.{yxpart} */
4774 @d yy_part 60 /* operation code for \.{yypart} */
4775 @d red_part 61 /* operation code for \.{redpart} */
4776 @d green_part 62 /* operation code for \.{greenpart} */
4777 @d blue_part 63 /* operation code for \.{bluepart} */
4778 @d cyan_part 64 /* operation code for \.{cyanpart} */
4779 @d magenta_part 65 /* operation code for \.{magentapart} */
4780 @d yellow_part 66 /* operation code for \.{yellowpart} */
4781 @d black_part 67 /* operation code for \.{blackpart} */
4782 @d grey_part 68 /* operation code for \.{greypart} */
4783 @d font_part 69 /* operation code for \.{fontpart} */
4784 @d text_part 70 /* operation code for \.{textpart} */
4785 @d path_part 71 /* operation code for \.{pathpart} */
4786 @d pen_part 72 /* operation code for \.{penpart} */
4787 @d dash_part 73 /* operation code for \.{dashpart} */
4788 @d sqrt_op 74 /* operation code for \.{sqrt} */
4789 @d m_exp_op 75 /* operation code for \.{mexp} */
4790 @d m_log_op 76 /* operation code for \.{mlog} */
4791 @d sin_d_op 77 /* operation code for \.{sind} */
4792 @d cos_d_op 78 /* operation code for \.{cosd} */
4793 @d floor_op 79 /* operation code for \.{floor} */
4794 @d uniform_deviate 80 /* operation code for \.{uniformdeviate} */
4795 @d char_exists_op 81 /* operation code for \.{charexists} */
4796 @d font_size 82 /* operation code for \.{fontsize} */
4797 @d ll_corner_op 83 /* operation code for \.{llcorner} */
4798 @d lr_corner_op 84 /* operation code for \.{lrcorner} */
4799 @d ul_corner_op 85 /* operation code for \.{ulcorner} */
4800 @d ur_corner_op 86 /* operation code for \.{urcorner} */
4801 @d arc_length 87 /* operation code for \.{arclength} */
4802 @d angle_op 88 /* operation code for \.{angle} */
4803 @d cycle_op 89 /* operation code for \.{cycle} */
4804 @d filled_op 90 /* operation code for \.{filled} */
4805 @d stroked_op 91 /* operation code for \.{stroked} */
4806 @d textual_op 92 /* operation code for \.{textual} */
4807 @d clipped_op 93 /* operation code for \.{clipped} */
4808 @d bounded_op 94 /* operation code for \.{bounded} */
4809 @d plus 95 /* operation code for \.+ */
4810 @d minus 96 /* operation code for \.- */
4811 @d times 97 /* operation code for \.* */
4812 @d over 98 /* operation code for \./ */
4813 @d pythag_add 99 /* operation code for \.{++} */
4814 @d pythag_sub 100 /* operation code for \.{+-+} */
4815 @d or_op 101 /* operation code for \.{or} */
4816 @d and_op 102 /* operation code for \.{and} */
4817 @d less_than 103 /* operation code for \.< */
4818 @d less_or_equal 104 /* operation code for \.{<=} */
4819 @d greater_than 105 /* operation code for \.> */
4820 @d greater_or_equal 106 /* operation code for \.{>=} */
4821 @d equal_to 107 /* operation code for \.= */
4822 @d unequal_to 108 /* operation code for \.{<>} */
4823 @d concatenate 109 /* operation code for \.\& */
4824 @d rotated_by 110 /* operation code for \.{rotated} */
4825 @d slanted_by 111 /* operation code for \.{slanted} */
4826 @d scaled_by 112 /* operation code for \.{scaled} */
4827 @d shifted_by 113 /* operation code for \.{shifted} */
4828 @d transformed_by 114 /* operation code for \.{transformed} */
4829 @d x_scaled 115 /* operation code for \.{xscaled} */
4830 @d y_scaled 116 /* operation code for \.{yscaled} */
4831 @d z_scaled 117 /* operation code for \.{zscaled} */
4832 @d in_font 118 /* operation code for \.{infont} */
4833 @d intersect 119 /* operation code for \.{intersectiontimes} */
4834 @d double_dot 120 /* operation code for improper \.{..} */
4835 @d substring_of 121 /* operation code for \.{substring} */
4836 @d min_of substring_of
4837 @d subpath_of 122 /* operation code for \.{subpath} */
4838 @d direction_time_of 123 /* operation code for \.{directiontime} */
4839 @d point_of 124 /* operation code for \.{point} */
4840 @d precontrol_of 125 /* operation code for \.{precontrol} */
4841 @d postcontrol_of 126 /* operation code for \.{postcontrol} */
4842 @d pen_offset_of 127 /* operation code for \.{penoffset} */
4843 @d arc_time_of 128 /* operation code for \.{arctime} */
4844 @d mp_version 129 /* operation code for \.{mpversion} */
4845 @d envelope_of 130 /* operation code for \.{envelope} */
4846
4847 @c void mp_print_op (MP mp,quarterword c) { 
4848   if (c<=mp_numeric_type ) {
4849     mp_print_type(mp, c);
4850   } else {
4851     switch (c) {
4852     case true_code:mp_print(mp, "true"); break;
4853     case false_code:mp_print(mp, "false"); break;
4854     case null_picture_code:mp_print(mp, "nullpicture"); break;
4855     case null_pen_code:mp_print(mp, "nullpen"); break;
4856     case job_name_op:mp_print(mp, "jobname"); break;
4857     case read_string_op:mp_print(mp, "readstring"); break;
4858     case pen_circle:mp_print(mp, "pencircle"); break;
4859     case normal_deviate:mp_print(mp, "normaldeviate"); break;
4860     case read_from_op:mp_print(mp, "readfrom"); break;
4861     case close_from_op:mp_print(mp, "closefrom"); break;
4862     case odd_op:mp_print(mp, "odd"); break;
4863     case known_op:mp_print(mp, "known"); break;
4864     case unknown_op:mp_print(mp, "unknown"); break;
4865     case not_op:mp_print(mp, "not"); break;
4866     case decimal:mp_print(mp, "decimal"); break;
4867     case reverse:mp_print(mp, "reverse"); break;
4868     case make_path_op:mp_print(mp, "makepath"); break;
4869     case make_pen_op:mp_print(mp, "makepen"); break;
4870     case oct_op:mp_print(mp, "oct"); break;
4871     case hex_op:mp_print(mp, "hex"); break;
4872     case ASCII_op:mp_print(mp, "ASCII"); break;
4873     case char_op:mp_print(mp, "char"); break;
4874     case length_op:mp_print(mp, "length"); break;
4875     case turning_op:mp_print(mp, "turningnumber"); break;
4876     case x_part:mp_print(mp, "xpart"); break;
4877     case y_part:mp_print(mp, "ypart"); break;
4878     case xx_part:mp_print(mp, "xxpart"); break;
4879     case xy_part:mp_print(mp, "xypart"); break;
4880     case yx_part:mp_print(mp, "yxpart"); break;
4881     case yy_part:mp_print(mp, "yypart"); break;
4882     case red_part:mp_print(mp, "redpart"); break;
4883     case green_part:mp_print(mp, "greenpart"); break;
4884     case blue_part:mp_print(mp, "bluepart"); break;
4885     case cyan_part:mp_print(mp, "cyanpart"); break;
4886     case magenta_part:mp_print(mp, "magentapart"); break;
4887     case yellow_part:mp_print(mp, "yellowpart"); break;
4888     case black_part:mp_print(mp, "blackpart"); break;
4889     case grey_part:mp_print(mp, "greypart"); break;
4890     case color_model_part:mp_print(mp, "colormodel"); break;
4891     case font_part:mp_print(mp, "fontpart"); break;
4892     case text_part:mp_print(mp, "textpart"); break;
4893     case path_part:mp_print(mp, "pathpart"); break;
4894     case pen_part:mp_print(mp, "penpart"); break;
4895     case dash_part:mp_print(mp, "dashpart"); break;
4896     case sqrt_op:mp_print(mp, "sqrt"); break;
4897     case m_exp_op:mp_print(mp, "mexp"); break;
4898     case m_log_op:mp_print(mp, "mlog"); break;
4899     case sin_d_op:mp_print(mp, "sind"); break;
4900     case cos_d_op:mp_print(mp, "cosd"); break;
4901     case floor_op:mp_print(mp, "floor"); break;
4902     case uniform_deviate:mp_print(mp, "uniformdeviate"); break;
4903     case char_exists_op:mp_print(mp, "charexists"); break;
4904     case font_size:mp_print(mp, "fontsize"); break;
4905     case ll_corner_op:mp_print(mp, "llcorner"); break;
4906     case lr_corner_op:mp_print(mp, "lrcorner"); break;
4907     case ul_corner_op:mp_print(mp, "ulcorner"); break;
4908     case ur_corner_op:mp_print(mp, "urcorner"); break;
4909     case arc_length:mp_print(mp, "arclength"); break;
4910     case angle_op:mp_print(mp, "angle"); break;
4911     case cycle_op:mp_print(mp, "cycle"); break;
4912     case filled_op:mp_print(mp, "filled"); break;
4913     case stroked_op:mp_print(mp, "stroked"); break;
4914     case textual_op:mp_print(mp, "textual"); break;
4915     case clipped_op:mp_print(mp, "clipped"); break;
4916     case bounded_op:mp_print(mp, "bounded"); break;
4917     case plus:mp_print_char(mp, '+'); break;
4918     case minus:mp_print_char(mp, '-'); break;
4919     case times:mp_print_char(mp, '*'); break;
4920     case over:mp_print_char(mp, '/'); break;
4921     case pythag_add:mp_print(mp, "++"); break;
4922     case pythag_sub:mp_print(mp, "+-+"); break;
4923     case or_op:mp_print(mp, "or"); break;
4924     case and_op:mp_print(mp, "and"); break;
4925     case less_than:mp_print_char(mp, '<'); break;
4926     case less_or_equal:mp_print(mp, "<="); break;
4927     case greater_than:mp_print_char(mp, '>'); break;
4928     case greater_or_equal:mp_print(mp, ">="); break;
4929     case equal_to:mp_print_char(mp, '='); break;
4930     case unequal_to:mp_print(mp, "<>"); break;
4931     case concatenate:mp_print(mp, "&"); break;
4932     case rotated_by:mp_print(mp, "rotated"); break;
4933     case slanted_by:mp_print(mp, "slanted"); break;
4934     case scaled_by:mp_print(mp, "scaled"); break;
4935     case shifted_by:mp_print(mp, "shifted"); break;
4936     case transformed_by:mp_print(mp, "transformed"); break;
4937     case x_scaled:mp_print(mp, "xscaled"); break;
4938     case y_scaled:mp_print(mp, "yscaled"); break;
4939     case z_scaled:mp_print(mp, "zscaled"); break;
4940     case in_font:mp_print(mp, "infont"); break;
4941     case intersect:mp_print(mp, "intersectiontimes"); break;
4942     case substring_of:mp_print(mp, "substring"); break;
4943     case subpath_of:mp_print(mp, "subpath"); break;
4944     case direction_time_of:mp_print(mp, "directiontime"); break;
4945     case point_of:mp_print(mp, "point"); break;
4946     case precontrol_of:mp_print(mp, "precontrol"); break;
4947     case postcontrol_of:mp_print(mp, "postcontrol"); break;
4948     case pen_offset_of:mp_print(mp, "penoffset"); break;
4949     case arc_time_of:mp_print(mp, "arctime"); break;
4950     case mp_version:mp_print(mp, "mpversion"); break;
4951     case envelope_of:mp_print(mp, "envelope"); break;
4952     default: mp_print(mp, ".."); break;
4953     }
4954   }
4955 }
4956
4957 @ \MP\ also has a bunch of internal parameters that a user might want to
4958 fuss with. Every such parameter has an identifying code number, defined here.
4959
4960 @<Types...@>=
4961 enum mp_given_internal {
4962   mp_tracing_titles=1, /* show titles online when they appear */
4963   mp_tracing_equations, /* show each variable when it becomes known */
4964   mp_tracing_capsules, /* show capsules too */
4965   mp_tracing_choices, /* show the control points chosen for paths */
4966   mp_tracing_specs, /* show path subdivision prior to filling with polygonal a pen */
4967   mp_tracing_commands, /* show commands and operations before they are performed */
4968   mp_tracing_restores, /* show when a variable or internal is restored */
4969   mp_tracing_macros, /* show macros before they are expanded */
4970   mp_tracing_output, /* show digitized edges as they are output */
4971   mp_tracing_stats, /* show memory usage at end of job */
4972   mp_tracing_lost_chars, /* show characters that aren't \&{infont} */
4973   mp_tracing_online, /* show long diagnostics on terminal and in the log file */
4974   mp_year, /* the current year (e.g., 1984) */
4975   mp_month, /* the current month (e.g., 3 $\equiv$ March) */
4976   mp_day, /* the current day of the month */
4977   mp_time, /* the number of minutes past midnight when this job started */
4978   mp_char_code, /* the number of the next character to be output */
4979   mp_char_ext, /* the extension code of the next character to be output */
4980   mp_char_wd, /* the width of the next character to be output */
4981   mp_char_ht, /* the height of the next character to be output */
4982   mp_char_dp, /* the depth of the next character to be output */
4983   mp_char_ic, /* the italic correction of the next character to be output */
4984   mp_design_size, /* the unit of measure used for |mp_char_wd..mp_char_ic|, in points */
4985   mp_pausing, /* positive to display lines on the terminal before they are read */
4986   mp_showstopping, /* positive to stop after each \&{show} command */
4987   mp_fontmaking, /* positive if font metric output is to be produced */
4988   mp_linejoin, /* as in \ps: 0 for mitered, 1 for round, 2 for beveled */
4989   mp_linecap, /* as in \ps: 0 for butt, 1 for round, 2 for square */
4990   mp_miterlimit, /* controls miter length as in \ps */
4991   mp_warning_check, /* controls error message when variable value is large */
4992   mp_boundary_char, /* the right boundary character for ligatures */
4993   mp_prologues, /* positive to output conforming PostScript using built-in fonts */
4994   mp_true_corners, /* positive to make \&{llcorner} etc. ignore \&{setbounds} */
4995   mp_default_color_model, /* the default color model for unspecified items */
4996   mp_restore_clip_color,
4997   mp_procset, /* wether or not create PostScript command shortcuts */
4998   mp_gtroffmode  /* whether the user specified |-troff| on the command line */
4999 };
5000
5001 @
5002
5003 @d max_given_internal mp_gtroffmode
5004
5005 @<Glob...@>=
5006 scaled *internal;  /* the values of internal quantities */
5007 char **int_name;  /* their names */
5008 int int_ptr;  /* the maximum internal quantity defined so far */
5009 int max_internal; /* current maximum number of internal quantities */
5010
5011 @ @<Option variables@>=
5012 int troff_mode; 
5013
5014 @ @<Allocate or initialize ...@>=
5015 mp->max_internal=2*max_given_internal;
5016 mp->internal = xmalloc ((mp->max_internal+1), sizeof(scaled));
5017 mp->int_name = xmalloc ((mp->max_internal+1), sizeof(char *));
5018 mp->troff_mode=(opt->troff_mode>0 ? true : false);
5019
5020 @ @<Exported function ...@>=
5021 int mp_troff_mode(MP mp);
5022
5023 @ @c
5024 int mp_troff_mode(MP mp) { return mp->troff_mode; }
5025
5026 @ @<Set initial ...@>=
5027 for (k=0;k<= mp->max_internal; k++ ) { 
5028    mp->internal[k]=0; 
5029    mp->int_name[k]=NULL; 
5030 }
5031 mp->int_ptr=max_given_internal;
5032
5033 @ The symbolic names for internal quantities are put into \MP's hash table
5034 by using a routine called |primitive|, which will be defined later. Let us
5035 enter them now, so that we don't have to list all those names again
5036 anywhere else.
5037
5038 @<Put each of \MP's primitives into the hash table@>=
5039 mp_primitive(mp, "tracingtitles",internal_quantity,mp_tracing_titles);
5040 @:tracingtitles_}{\&{tracingtitles} primitive@>
5041 mp_primitive(mp, "tracingequations",internal_quantity,mp_tracing_equations);
5042 @:mp_tracing_equations_}{\&{tracingequations} primitive@>
5043 mp_primitive(mp, "tracingcapsules",internal_quantity,mp_tracing_capsules);
5044 @:mp_tracing_capsules_}{\&{tracingcapsules} primitive@>
5045 mp_primitive(mp, "tracingchoices",internal_quantity,mp_tracing_choices);
5046 @:mp_tracing_choices_}{\&{tracingchoices} primitive@>
5047 mp_primitive(mp, "tracingspecs",internal_quantity,mp_tracing_specs);
5048 @:mp_tracing_specs_}{\&{tracingspecs} primitive@>
5049 mp_primitive(mp, "tracingcommands",internal_quantity,mp_tracing_commands);
5050 @:mp_tracing_commands_}{\&{tracingcommands} primitive@>
5051 mp_primitive(mp, "tracingrestores",internal_quantity,mp_tracing_restores);
5052 @:mp_tracing_restores_}{\&{tracingrestores} primitive@>
5053 mp_primitive(mp, "tracingmacros",internal_quantity,mp_tracing_macros);
5054 @:mp_tracing_macros_}{\&{tracingmacros} primitive@>
5055 mp_primitive(mp, "tracingoutput",internal_quantity,mp_tracing_output);
5056 @:mp_tracing_output_}{\&{tracingoutput} primitive@>
5057 mp_primitive(mp, "tracingstats",internal_quantity,mp_tracing_stats);
5058 @:mp_tracing_stats_}{\&{tracingstats} primitive@>
5059 mp_primitive(mp, "tracinglostchars",internal_quantity,mp_tracing_lost_chars);
5060 @:mp_tracing_lost_chars_}{\&{tracinglostchars} primitive@>
5061 mp_primitive(mp, "tracingonline",internal_quantity,mp_tracing_online);
5062 @:mp_tracing_online_}{\&{tracingonline} primitive@>
5063 mp_primitive(mp, "year",internal_quantity,mp_year);
5064 @:mp_year_}{\&{year} primitive@>
5065 mp_primitive(mp, "month",internal_quantity,mp_month);
5066 @:mp_month_}{\&{month} primitive@>
5067 mp_primitive(mp, "day",internal_quantity,mp_day);
5068 @:mp_day_}{\&{day} primitive@>
5069 mp_primitive(mp, "time",internal_quantity,mp_time);
5070 @:time_}{\&{time} primitive@>
5071 mp_primitive(mp, "charcode",internal_quantity,mp_char_code);
5072 @:mp_char_code_}{\&{charcode} primitive@>
5073 mp_primitive(mp, "charext",internal_quantity,mp_char_ext);
5074 @:mp_char_ext_}{\&{charext} primitive@>
5075 mp_primitive(mp, "charwd",internal_quantity,mp_char_wd);
5076 @:mp_char_wd_}{\&{charwd} primitive@>
5077 mp_primitive(mp, "charht",internal_quantity,mp_char_ht);
5078 @:mp_char_ht_}{\&{charht} primitive@>
5079 mp_primitive(mp, "chardp",internal_quantity,mp_char_dp);
5080 @:mp_char_dp_}{\&{chardp} primitive@>
5081 mp_primitive(mp, "charic",internal_quantity,mp_char_ic);
5082 @:mp_char_ic_}{\&{charic} primitive@>
5083 mp_primitive(mp, "designsize",internal_quantity,mp_design_size);
5084 @:mp_design_size_}{\&{designsize} primitive@>
5085 mp_primitive(mp, "pausing",internal_quantity,mp_pausing);
5086 @:mp_pausing_}{\&{pausing} primitive@>
5087 mp_primitive(mp, "showstopping",internal_quantity,mp_showstopping);
5088 @:mp_showstopping_}{\&{showstopping} primitive@>
5089 mp_primitive(mp, "fontmaking",internal_quantity,mp_fontmaking);
5090 @:mp_fontmaking_}{\&{fontmaking} primitive@>
5091 mp_primitive(mp, "linejoin",internal_quantity,mp_linejoin);
5092 @:mp_linejoin_}{\&{linejoin} primitive@>
5093 mp_primitive(mp, "linecap",internal_quantity,mp_linecap);
5094 @:mp_linecap_}{\&{linecap} primitive@>
5095 mp_primitive(mp, "miterlimit",internal_quantity,mp_miterlimit);
5096 @:mp_miterlimit_}{\&{miterlimit} primitive@>
5097 mp_primitive(mp, "warningcheck",internal_quantity,mp_warning_check);
5098 @:mp_warning_check_}{\&{warningcheck} primitive@>
5099 mp_primitive(mp, "boundarychar",internal_quantity,mp_boundary_char);
5100 @:mp_boundary_char_}{\&{boundarychar} primitive@>
5101 mp_primitive(mp, "prologues",internal_quantity,mp_prologues);
5102 @:mp_prologues_}{\&{prologues} primitive@>
5103 mp_primitive(mp, "truecorners",internal_quantity,mp_true_corners);
5104 @:mp_true_corners_}{\&{truecorners} primitive@>
5105 mp_primitive(mp, "mpprocset",internal_quantity,mp_procset);
5106 @:mp_procset_}{\&{mpprocset} primitive@>
5107 mp_primitive(mp, "troffmode",internal_quantity,mp_gtroffmode);
5108 @:troffmode_}{\&{troffmode} primitive@>
5109 mp_primitive(mp, "defaultcolormodel",internal_quantity,mp_default_color_model);
5110 @:mp_default_color_model_}{\&{defaultcolormodel} primitive@>
5111 mp_primitive(mp, "restoreclipcolor",internal_quantity,mp_restore_clip_color);
5112 @:mp_restore_clip_color_}{\&{restoreclipcolor} primitive@>
5113
5114 @ Colors can be specified in four color models. In the special
5115 case of |no_model|, MetaPost does not output any color operator to
5116 the postscript output.
5117
5118 Note: these values are passed directly on to |with_option|. This only
5119 works because the other possible values passed to |with_option| are
5120 8 and 10 respectively (from |with_pen| and |with_picture|).
5121
5122 There is a first state, that is only used for |gs_colormodel|. It flags
5123 the fact that there has not been any kind of color specification by
5124 the user so far in the game.
5125
5126 @<Types...@>=
5127 enum mp_color_model {
5128   mp_no_model=1,
5129   mp_grey_model=3,
5130   mp_rgb_model=5,
5131   mp_cmyk_model=7,
5132   mp_uninitialized_model=9
5133 };
5134
5135
5136 @ @<Initialize table entries (done by \.{INIMP} only)@>=
5137 mp->internal[mp_default_color_model]=(mp_rgb_model*unity);
5138 mp->internal[mp_restore_clip_color]=unity;
5139
5140 @ Well, we do have to list the names one more time, for use in symbolic
5141 printouts.
5142
5143 @<Initialize table...@>=
5144 mp->int_name[mp_tracing_titles]=xstrdup("tracingtitles");
5145 mp->int_name[mp_tracing_equations]=xstrdup("tracingequations");
5146 mp->int_name[mp_tracing_capsules]=xstrdup("tracingcapsules");
5147 mp->int_name[mp_tracing_choices]=xstrdup("tracingchoices");
5148 mp->int_name[mp_tracing_specs]=xstrdup("tracingspecs");
5149 mp->int_name[mp_tracing_commands]=xstrdup("tracingcommands");
5150 mp->int_name[mp_tracing_restores]=xstrdup("tracingrestores");
5151 mp->int_name[mp_tracing_macros]=xstrdup("tracingmacros");
5152 mp->int_name[mp_tracing_output]=xstrdup("tracingoutput");
5153 mp->int_name[mp_tracing_stats]=xstrdup("tracingstats");
5154 mp->int_name[mp_tracing_lost_chars]=xstrdup("tracinglostchars");
5155 mp->int_name[mp_tracing_online]=xstrdup("tracingonline");
5156 mp->int_name[mp_year]=xstrdup("year");
5157 mp->int_name[mp_month]=xstrdup("month");
5158 mp->int_name[mp_day]=xstrdup("day");
5159 mp->int_name[mp_time]=xstrdup("time");
5160 mp->int_name[mp_char_code]=xstrdup("charcode");
5161 mp->int_name[mp_char_ext]=xstrdup("charext");
5162 mp->int_name[mp_char_wd]=xstrdup("charwd");
5163 mp->int_name[mp_char_ht]=xstrdup("charht");
5164 mp->int_name[mp_char_dp]=xstrdup("chardp");
5165 mp->int_name[mp_char_ic]=xstrdup("charic");
5166 mp->int_name[mp_design_size]=xstrdup("designsize");
5167 mp->int_name[mp_pausing]=xstrdup("pausing");
5168 mp->int_name[mp_showstopping]=xstrdup("showstopping");
5169 mp->int_name[mp_fontmaking]=xstrdup("fontmaking");
5170 mp->int_name[mp_linejoin]=xstrdup("linejoin");
5171 mp->int_name[mp_linecap]=xstrdup("linecap");
5172 mp->int_name[mp_miterlimit]=xstrdup("miterlimit");
5173 mp->int_name[mp_warning_check]=xstrdup("warningcheck");
5174 mp->int_name[mp_boundary_char]=xstrdup("boundarychar");
5175 mp->int_name[mp_prologues]=xstrdup("prologues");
5176 mp->int_name[mp_true_corners]=xstrdup("truecorners");
5177 mp->int_name[mp_default_color_model]=xstrdup("defaultcolormodel");
5178 mp->int_name[mp_procset]=xstrdup("mpprocset");
5179 mp->int_name[mp_gtroffmode]=xstrdup("troffmode");
5180 mp->int_name[mp_restore_clip_color]=xstrdup("restoreclipcolor");
5181
5182 @ The following procedure, which is called just before \MP\ initializes its
5183 input and output, establishes the initial values of the date and time.
5184 @^system dependencies@>
5185
5186 Note that the values are |scaled| integers. Hence \MP\ can no longer
5187 be used after the year 32767.
5188
5189 @c 
5190 void mp_fix_date_and_time (MP mp) { 
5191   time_t aclock = time ((time_t *) 0);
5192   struct tm *tmptr = localtime (&aclock);
5193   mp->internal[mp_time]=
5194       (tmptr->tm_hour*60+tmptr->tm_min)*unity; /* minutes since midnight */
5195   mp->internal[mp_day]=(tmptr->tm_mday)*unity; /* fourth day of the month */
5196   mp->internal[mp_month]=(tmptr->tm_mon+1)*unity; /* seventh month of the year */
5197   mp->internal[mp_year]=(tmptr->tm_year+1900)*unity; /* Anno Domini */
5198 }
5199
5200 @ @<Declarations@>=
5201 void mp_fix_date_and_time (MP mp) ;
5202
5203 @ \MP\ is occasionally supposed to print diagnostic information that
5204 goes only into the transcript file, unless |mp_tracing_online| is positive.
5205 Now that we have defined |mp_tracing_online| we can define
5206 two routines that adjust the destination of print commands:
5207
5208 @<Declarations@>=
5209 void mp_begin_diagnostic (MP mp) ;
5210 void mp_end_diagnostic (MP mp,boolean blank_line);
5211 void mp_print_diagnostic (MP mp, const char *s, const char *t, boolean nuline) ;
5212
5213 @ @<Basic printing...@>=
5214 @<Declare a function called |true_line|@>
5215 void mp_begin_diagnostic (MP mp) { /* prepare to do some tracing */
5216   mp->old_setting=mp->selector;
5217   if ((mp->internal[mp_tracing_online]<=0)&&(mp->selector==term_and_log)){ 
5218     decr(mp->selector);
5219     if ( mp->history==mp_spotless ) mp->history=mp_warning_issued;
5220   }
5221 }
5222 @#
5223 void mp_end_diagnostic (MP mp,boolean blank_line) {
5224   /* restore proper conditions after tracing */
5225   mp_print_nl(mp, "");
5226   if ( blank_line ) mp_print_ln(mp);
5227   mp->selector=mp->old_setting;
5228 }
5229
5230
5231
5232 @<Glob...@>=
5233 unsigned int old_setting;
5234
5235 @ We will occasionally use |begin_diagnostic| in connection with line-number
5236 printing, as follows. (The parameter |s| is typically |"Path"| or
5237 |"Cycle spec"|, etc.)
5238
5239 @<Basic printing...@>=
5240 void mp_print_diagnostic (MP mp, const char *s, const char *t, boolean nuline) { 
5241   mp_begin_diagnostic(mp);
5242   if ( nuline ) mp_print_nl(mp, s); else mp_print(mp, s);
5243   mp_print(mp, " at line "); 
5244   mp_print_int(mp, mp_true_line(mp));
5245   mp_print(mp, t); mp_print_char(mp, ':');
5246 }
5247
5248 @ The 256 |ASCII_code| characters are grouped into classes by means of
5249 the |char_class| table. Individual class numbers have no semantic
5250 or syntactic significance, except in a few instances defined here.
5251 There's also |max_class|, which can be used as a basis for additional
5252 class numbers in nonstandard extensions of \MP.
5253
5254 @d digit_class 0 /* the class number of \.{0123456789} */
5255 @d period_class 1 /* the class number of `\..' */
5256 @d space_class 2 /* the class number of spaces and nonstandard characters */
5257 @d percent_class 3 /* the class number of `\.\%' */
5258 @d string_class 4 /* the class number of `\."' */
5259 @d right_paren_class 8 /* the class number of `\.)' */
5260 @d isolated_classes 5: case 6: case 7: case 8 /* characters that make length-one tokens only */
5261 @d letter_class 9 /* letters and the underline character */
5262 @d left_bracket_class 17 /* `\.[' */
5263 @d right_bracket_class 18 /* `\.]' */
5264 @d invalid_class 20 /* bad character in the input */
5265 @d max_class 20 /* the largest class number */
5266
5267 @<Glob...@>=
5268 int char_class[256]; /* the class numbers */
5269
5270 @ If changes are made to accommodate non-ASCII character sets, they should
5271 follow the guidelines in Appendix~C of {\sl The {\logos METAFONT\/}book}.
5272 @:METAFONTbook}{\sl The {\logos METAFONT\/}book@>
5273 @^system dependencies@>
5274
5275 @<Set initial ...@>=
5276 for (k='0';k<='9';k++) 
5277   mp->char_class[k]=digit_class;
5278 mp->char_class['.']=period_class;
5279 mp->char_class[' ']=space_class;
5280 mp->char_class['%']=percent_class;
5281 mp->char_class['"']=string_class;
5282 mp->char_class[',']=5;
5283 mp->char_class[';']=6;
5284 mp->char_class['(']=7;
5285 mp->char_class[')']=right_paren_class;
5286 for (k='A';k<= 'Z';k++ )
5287   mp->char_class[k]=letter_class;
5288 for (k='a';k<='z';k++) 
5289   mp->char_class[k]=letter_class;
5290 mp->char_class['_']=letter_class;
5291 mp->char_class['<']=10;
5292 mp->char_class['=']=10;
5293 mp->char_class['>']=10;
5294 mp->char_class[':']=10;
5295 mp->char_class['|']=10;
5296 mp->char_class['`']=11;
5297 mp->char_class['\'']=11;
5298 mp->char_class['+']=12;
5299 mp->char_class['-']=12;
5300 mp->char_class['/']=13;
5301 mp->char_class['*']=13;
5302 mp->char_class['\\']=13;
5303 mp->char_class['!']=14;
5304 mp->char_class['?']=14;
5305 mp->char_class['#']=15;
5306 mp->char_class['&']=15;
5307 mp->char_class['@@']=15;
5308 mp->char_class['$']=15;
5309 mp->char_class['^']=16;
5310 mp->char_class['~']=16;
5311 mp->char_class['[']=left_bracket_class;
5312 mp->char_class[']']=right_bracket_class;
5313 mp->char_class['{']=19;
5314 mp->char_class['}']=19;
5315 for (k=0;k<' ';k++)
5316   mp->char_class[k]=invalid_class;
5317 mp->char_class['\t']=space_class;
5318 mp->char_class['\f']=space_class;
5319 for (k=127;k<=255;k++)
5320   mp->char_class[k]=invalid_class;
5321
5322 @* \[13] The hash table.
5323 Symbolic tokens are stored and retrieved by means of a fairly standard hash
5324 table algorithm called the method of ``coalescing lists'' (cf.\ Algorithm 6.4C
5325 in {\sl The Art of Computer Programming\/}). Once a symbolic token enters the
5326 table, it is never removed.
5327
5328 The actual sequence of characters forming a symbolic token is
5329 stored in the |str_pool| array together with all the other strings. An
5330 auxiliary array |hash| consists of items with two halfword fields per
5331 word. The first of these, called |next(p)|, points to the next identifier
5332 belonging to the same coalesced list as the identifier corresponding to~|p|;
5333 and the other, called |text(p)|, points to the |str_start| entry for
5334 |p|'s identifier. If position~|p| of the hash table is empty, we have
5335 |text(p)=0|; if position |p| is either empty or the end of a coalesced
5336 hash list, we have |next(p)=0|.
5337
5338 An auxiliary pointer variable called |hash_used| is maintained in such a
5339 way that all locations |p>=hash_used| are nonempty. The global variable
5340 |st_count| tells how many symbolic tokens have been defined, if statistics
5341 are being kept.
5342
5343 The first 256 locations of |hash| are reserved for symbols of length one.
5344
5345 There's a parallel array called |eqtb| that contains the current equivalent
5346 values of each symbolic token. The entries of this array consist of
5347 two halfwords called |eq_type| (a command code) and |equiv| (a secondary
5348 piece of information that qualifies the |eq_type|).
5349
5350 @d next(A)   mp->hash[(A)].lh /* link for coalesced lists */
5351 @d text(A)   mp->hash[(A)].rh /* string number for symbolic token name */
5352 @d eq_type(A)   mp->eqtb[(A)].lh /* the current ``meaning'' of a symbolic token */
5353 @d equiv(A)   mp->eqtb[(A)].rh /* parametric part of a token's meaning */
5354 @d hash_base 257 /* hashing actually starts here */
5355 @d hash_is_full   (mp->hash_used==hash_base) /* are all positions occupied? */
5356
5357 @<Glob...@>=
5358 pointer hash_used; /* allocation pointer for |hash| */
5359 integer st_count; /* total number of known identifiers */
5360
5361 @ Certain entries in the hash table are ``frozen'' and not redefinable,
5362 since they are used in error recovery.
5363
5364 @d hash_top (hash_base+mp->hash_size) /* the first location of the frozen area */
5365 @d frozen_inaccessible hash_top /* |hash| location to protect the frozen area */
5366 @d frozen_repeat_loop (hash_top+1) /* |hash| location of a loop-repeat token */
5367 @d frozen_right_delimiter (hash_top+2) /* |hash| location of a permanent `\.)' */
5368 @d frozen_left_bracket (hash_top+3) /* |hash| location of a permanent `\.[' */
5369 @d frozen_slash (hash_top+4) /* |hash| location of a permanent `\./' */
5370 @d frozen_colon (hash_top+5) /* |hash| location of a permanent `\.:' */
5371 @d frozen_semicolon (hash_top+6) /* |hash| location of a permanent `\.;' */
5372 @d frozen_end_for (hash_top+7) /* |hash| location of a permanent \&{endfor} */
5373 @d frozen_end_def (hash_top+8) /* |hash| location of a permanent \&{enddef} */
5374 @d frozen_fi (hash_top+9) /* |hash| location of a permanent \&{fi} */
5375 @d frozen_end_group (hash_top+10) /* |hash| location of a permanent `\.{endgroup}' */
5376 @d frozen_etex (hash_top+11) /* |hash| location of a permanent \&{etex} */
5377 @d frozen_mpx_break (hash_top+12) /* |hash| location of a permanent \&{mpxbreak} */
5378 @d frozen_bad_vardef (hash_top+13) /* |hash| location of `\.{a bad variable}' */
5379 @d frozen_undefined (hash_top+14) /* |hash| location that never gets defined */
5380 @d hash_end (hash_top+14) /* the actual size of the |hash| and |eqtb| arrays */
5381
5382 @<Glob...@>=
5383 two_halves *hash; /* the hash table */
5384 two_halves *eqtb; /* the equivalents */
5385
5386 @ @<Allocate or initialize ...@>=
5387 mp->hash = xmalloc((hash_end+1),sizeof(two_halves));
5388 mp->eqtb = xmalloc((hash_end+1),sizeof(two_halves));
5389
5390 @ @<Dealloc variables@>=
5391 xfree(mp->hash);
5392 xfree(mp->eqtb);
5393
5394 @ @<Set init...@>=
5395 next(1)=0; text(1)=0; eq_type(1)=tag_token; equiv(1)=null;
5396 for (k=2;k<=hash_end;k++)  { 
5397   mp->hash[k]=mp->hash[1]; mp->eqtb[k]=mp->eqtb[1];
5398 }
5399
5400 @ @<Initialize table entries...@>=
5401 mp->hash_used=frozen_inaccessible; /* nothing is used */
5402 mp->st_count=0;
5403 text(frozen_bad_vardef)=intern("a bad variable");
5404 text(frozen_etex)=intern("etex");
5405 text(frozen_mpx_break)=intern("mpxbreak");
5406 text(frozen_fi)=intern("fi");
5407 text(frozen_end_group)=intern("endgroup");
5408 text(frozen_end_def)=intern("enddef");
5409 text(frozen_end_for)=intern("endfor");
5410 text(frozen_semicolon)=intern(";");
5411 text(frozen_colon)=intern(":");
5412 text(frozen_slash)=intern("/");
5413 text(frozen_left_bracket)=intern("[");
5414 text(frozen_right_delimiter)=intern(")");
5415 text(frozen_inaccessible)=intern(" INACCESSIBLE");
5416 eq_type(frozen_right_delimiter)=right_delimiter;
5417
5418 @ @<Check the ``constant'' values...@>=
5419 if ( hash_end+mp->max_internal>max_halfword ) mp->bad=17;
5420
5421 @ Here is the subroutine that searches the hash table for an identifier
5422 that matches a given string of length~|l| appearing in |buffer[j..
5423 (j+l-1)]|. If the identifier is not found, it is inserted; hence it
5424 will always be found, and the corresponding hash table address
5425 will be returned.
5426
5427 @c 
5428 pointer mp_id_lookup (MP mp,integer j, integer l) { /* search the hash table */
5429   integer h; /* hash code */
5430   pointer p; /* index in |hash| array */
5431   pointer k; /* index in |buffer| array */
5432   if (l==1) {
5433     @<Treat special case of length 1 and |break|@>;
5434   }
5435   @<Compute the hash code |h|@>;
5436   p=h+hash_base; /* we start searching here; note that |0<=h<hash_prime| */
5437   while (true)  { 
5438         if (text(p)>0 && length(text(p))==l && mp_str_eq_buf(mp, text(p),j)) 
5439       break;
5440     if ( next(p)==0 ) {
5441       @<Insert a new symbolic token after |p|, then
5442         make |p| point to it and |break|@>;
5443     }
5444     p=next(p);
5445   }
5446   return p;
5447 }
5448
5449 @ @<Treat special case of length 1...@>=
5450  p=mp->buffer[j]+1; text(p)=p-1; return p;
5451
5452
5453 @ @<Insert a new symbolic...@>=
5454 {
5455 if ( text(p)>0 ) { 
5456   do {  
5457     if ( hash_is_full )
5458       mp_overflow(mp, "hash size",mp->hash_size);
5459 @:MetaPost capacity exceeded hash size}{\quad hash size@>
5460     decr(mp->hash_used);
5461   } while (text(mp->hash_used)!=0); /* search for an empty location in |hash| */
5462   next(p)=mp->hash_used; 
5463   p=mp->hash_used;
5464 }
5465 str_room(l);
5466 for (k=j;k<=j+l-1;k++) {
5467   append_char(mp->buffer[k]);
5468 }
5469 text(p)=mp_make_string(mp); 
5470 mp->str_ref[text(p)]=max_str_ref;
5471 incr(mp->st_count);
5472 break;
5473 }
5474
5475
5476 @ The value of |hash_prime| should be roughly 85\pct! of |hash_size|, and it
5477 should be a prime number.  The theory of hashing tells us to expect fewer
5478 than two table probes, on the average, when the search is successful.
5479 [See J.~S. Vitter, {\sl Journal of the ACM\/ \bf30} (1983), 231--258.]
5480 @^Vitter, Jeffrey Scott@>
5481
5482 @<Compute the hash code |h|@>=
5483 h=mp->buffer[j];
5484 for (k=j+1;k<=j+l-1;k++){ 
5485   h=h+h+mp->buffer[k];
5486   while ( h>=mp->hash_prime ) h=h-mp->hash_prime;
5487 }
5488
5489 @ @<Search |eqtb| for equivalents equal to |p|@>=
5490 for (q=1;q<=hash_end;q++) { 
5491   if ( equiv(q)==p ) { 
5492     mp_print_nl(mp, "EQUIV("); 
5493     mp_print_int(mp, q); 
5494     mp_print_char(mp, ')');
5495   }
5496 }
5497
5498 @ We need to put \MP's ``primitive'' symbolic tokens into the hash
5499 table, together with their command code (which will be the |eq_type|)
5500 and an operand (which will be the |equiv|). The |primitive| procedure
5501 does this, in a way that no \MP\ user can. The global value |cur_sym|
5502 contains the new |eqtb| pointer after |primitive| has acted.
5503
5504 @c 
5505 void mp_primitive (MP mp, const char *ss, halfword c, halfword o) {
5506   pool_pointer k; /* index into |str_pool| */
5507   small_number j; /* index into |buffer| */
5508   small_number l; /* length of the string */
5509   str_number s;
5510   s = intern(ss);
5511   k=mp->str_start[s]; l=str_stop(s)-k;
5512   /* we will move |s| into the (empty) |buffer| */
5513   for (j=0;j<=l-1;j++) {
5514     mp->buffer[j]=mp->str_pool[k+j];
5515   }
5516   mp->cur_sym=mp_id_lookup(mp, 0,l);
5517   if ( s>=256 ) { /* we don't want to have the string twice */
5518     mp_flush_string(mp, text(mp->cur_sym)); text(mp->cur_sym)=s;
5519   };
5520   eq_type(mp->cur_sym)=c; 
5521   equiv(mp->cur_sym)=o;
5522 }
5523
5524
5525 @ Many of \MP's primitives need no |equiv|, since they are identifiable
5526 by their |eq_type| alone. These primitives are loaded into the hash table
5527 as follows:
5528
5529 @<Put each of \MP's primitives into the hash table@>=
5530 mp_primitive(mp, "..",path_join,0);
5531 @:.._}{\.{..} primitive@>
5532 mp_primitive(mp, "[",left_bracket,0); mp->eqtb[frozen_left_bracket]=mp->eqtb[mp->cur_sym];
5533 @:[ }{\.{[} primitive@>
5534 mp_primitive(mp, "]",right_bracket,0);
5535 @:] }{\.{]} primitive@>
5536 mp_primitive(mp, "}",right_brace,0);
5537 @:]]}{\.{\char`\}} primitive@>
5538 mp_primitive(mp, "{",left_brace,0);
5539 @:][}{\.{\char`\{} primitive@>
5540 mp_primitive(mp, ":",colon,0); mp->eqtb[frozen_colon]=mp->eqtb[mp->cur_sym];
5541 @:: }{\.{:} primitive@>
5542 mp_primitive(mp, "::",double_colon,0);
5543 @::: }{\.{::} primitive@>
5544 mp_primitive(mp, "||:",bchar_label,0);
5545 @:::: }{\.{\char'174\char'174:} primitive@>
5546 mp_primitive(mp, ":=",assignment,0);
5547 @::=_}{\.{:=} primitive@>
5548 mp_primitive(mp, ",",comma,0);
5549 @:, }{\., primitive@>
5550 mp_primitive(mp, ";",semicolon,0); mp->eqtb[frozen_semicolon]=mp->eqtb[mp->cur_sym];
5551 @:; }{\.; primitive@>
5552 mp_primitive(mp, "\\",relax,0);
5553 @:]]\\}{\.{\char`\\} primitive@>
5554 @#
5555 mp_primitive(mp, "addto",add_to_command,0);
5556 @:add_to_}{\&{addto} primitive@>
5557 mp_primitive(mp, "atleast",at_least,0);
5558 @:at_least_}{\&{atleast} primitive@>
5559 mp_primitive(mp, "begingroup",begin_group,0); mp->bg_loc=mp->cur_sym;
5560 @:begin_group_}{\&{begingroup} primitive@>
5561 mp_primitive(mp, "controls",controls,0);
5562 @:controls_}{\&{controls} primitive@>
5563 mp_primitive(mp, "curl",curl_command,0);
5564 @:curl_}{\&{curl} primitive@>
5565 mp_primitive(mp, "delimiters",delimiters,0);
5566 @:delimiters_}{\&{delimiters} primitive@>
5567 mp_primitive(mp, "endgroup",end_group,0);
5568  mp->eqtb[frozen_end_group]=mp->eqtb[mp->cur_sym]; mp->eg_loc=mp->cur_sym;
5569 @:endgroup_}{\&{endgroup} primitive@>
5570 mp_primitive(mp, "everyjob",every_job_command,0);
5571 @:every_job_}{\&{everyjob} primitive@>
5572 mp_primitive(mp, "exitif",exit_test,0);
5573 @:exit_if_}{\&{exitif} primitive@>
5574 mp_primitive(mp, "expandafter",expand_after,0);
5575 @:expand_after_}{\&{expandafter} primitive@>
5576 mp_primitive(mp, "interim",interim_command,0);
5577 @:interim_}{\&{interim} primitive@>
5578 mp_primitive(mp, "let",let_command,0);
5579 @:let_}{\&{let} primitive@>
5580 mp_primitive(mp, "newinternal",new_internal,0);
5581 @:new_internal_}{\&{newinternal} primitive@>
5582 mp_primitive(mp, "of",of_token,0);
5583 @:of_}{\&{of} primitive@>
5584 mp_primitive(mp, "randomseed",mp_random_seed,0);
5585 @:mp_random_seed_}{\&{randomseed} primitive@>
5586 mp_primitive(mp, "save",save_command,0);
5587 @:save_}{\&{save} primitive@>
5588 mp_primitive(mp, "scantokens",scan_tokens,0);
5589 @:scan_tokens_}{\&{scantokens} primitive@>
5590 mp_primitive(mp, "shipout",ship_out_command,0);
5591 @:ship_out_}{\&{shipout} primitive@>
5592 mp_primitive(mp, "skipto",skip_to,0);
5593 @:skip_to_}{\&{skipto} primitive@>
5594 mp_primitive(mp, "special",special_command,0);
5595 @:special}{\&{special} primitive@>
5596 mp_primitive(mp, "fontmapfile",special_command,1);
5597 @:fontmapfile}{\&{fontmapfile} primitive@>
5598 mp_primitive(mp, "fontmapline",special_command,2);
5599 @:fontmapline}{\&{fontmapline} primitive@>
5600 mp_primitive(mp, "step",step_token,0);
5601 @:step_}{\&{step} primitive@>
5602 mp_primitive(mp, "str",str_op,0);
5603 @:str_}{\&{str} primitive@>
5604 mp_primitive(mp, "tension",tension,0);
5605 @:tension_}{\&{tension} primitive@>
5606 mp_primitive(mp, "to",to_token,0);
5607 @:to_}{\&{to} primitive@>
5608 mp_primitive(mp, "until",until_token,0);
5609 @:until_}{\&{until} primitive@>
5610 mp_primitive(mp, "within",within_token,0);
5611 @:within_}{\&{within} primitive@>
5612 mp_primitive(mp, "write",write_command,0);
5613 @:write_}{\&{write} primitive@>
5614
5615 @ Each primitive has a corresponding inverse, so that it is possible to
5616 display the cryptic numeric contents of |eqtb| in symbolic form.
5617 Every call of |primitive| in this program is therefore accompanied by some
5618 straightforward code that forms part of the |print_cmd_mod| routine
5619 explained below.
5620
5621 @<Cases of |print_cmd_mod| for symbolic printing of primitives@>=
5622 case add_to_command:mp_print(mp, "addto"); break;
5623 case assignment:mp_print(mp, ":="); break;
5624 case at_least:mp_print(mp, "atleast"); break;
5625 case bchar_label:mp_print(mp, "||:"); break;
5626 case begin_group:mp_print(mp, "begingroup"); break;
5627 case colon:mp_print(mp, ":"); break;
5628 case comma:mp_print(mp, ","); break;
5629 case controls:mp_print(mp, "controls"); break;
5630 case curl_command:mp_print(mp, "curl"); break;
5631 case delimiters:mp_print(mp, "delimiters"); break;
5632 case double_colon:mp_print(mp, "::"); break;
5633 case end_group:mp_print(mp, "endgroup"); break;
5634 case every_job_command:mp_print(mp, "everyjob"); break;
5635 case exit_test:mp_print(mp, "exitif"); break;
5636 case expand_after:mp_print(mp, "expandafter"); break;
5637 case interim_command:mp_print(mp, "interim"); break;
5638 case left_brace:mp_print(mp, "{"); break;
5639 case left_bracket:mp_print(mp, "["); break;
5640 case let_command:mp_print(mp, "let"); break;
5641 case new_internal:mp_print(mp, "newinternal"); break;
5642 case of_token:mp_print(mp, "of"); break;
5643 case path_join:mp_print(mp, ".."); break;
5644 case mp_random_seed:mp_print(mp, "randomseed"); break;
5645 case relax:mp_print_char(mp, '\\'); break;
5646 case right_brace:mp_print(mp, "}"); break;
5647 case right_bracket:mp_print(mp, "]"); break;
5648 case save_command:mp_print(mp, "save"); break;
5649 case scan_tokens:mp_print(mp, "scantokens"); break;
5650 case semicolon:mp_print(mp, ";"); break;
5651 case ship_out_command:mp_print(mp, "shipout"); break;
5652 case skip_to:mp_print(mp, "skipto"); break;
5653 case special_command: if ( m==2 ) mp_print(mp, "fontmapline"); else
5654                  if ( m==1 ) mp_print(mp, "fontmapfile"); else
5655                  mp_print(mp, "special"); break;
5656 case step_token:mp_print(mp, "step"); break;
5657 case str_op:mp_print(mp, "str"); break;
5658 case tension:mp_print(mp, "tension"); break;
5659 case to_token:mp_print(mp, "to"); break;
5660 case until_token:mp_print(mp, "until"); break;
5661 case within_token:mp_print(mp, "within"); break;
5662 case write_command:mp_print(mp, "write"); break;
5663
5664 @ We will deal with the other primitives later, at some point in the program
5665 where their |eq_type| and |equiv| values are more meaningful.  For example,
5666 the primitives for macro definitions will be loaded when we consider the
5667 routines that define macros.
5668 It is easy to find where each particular
5669 primitive was treated by looking in the index at the end; for example, the
5670 section where |"def"| entered |eqtb| is listed under `\&{def} primitive'.
5671
5672 @* \[14] Token lists.
5673 A \MP\ token is either symbolic or numeric or a string, or it denotes
5674 a macro parameter or capsule; so there are five corresponding ways to encode it
5675 @^token@>
5676 internally: (1)~A symbolic token whose hash code is~|p|
5677 is represented by the number |p|, in the |info| field of a single-word
5678 node in~|mem|. (2)~A numeric token whose |scaled| value is~|v| is
5679 represented in a two-word node of~|mem|; the |type| field is |known|,
5680 the |name_type| field is |token|, and the |value| field holds~|v|.
5681 The fact that this token appears in a two-word node rather than a
5682 one-word node is, of course, clear from the node address.
5683 (3)~A string token is also represented in a two-word node; the |type|
5684 field is |mp_string_type|, the |name_type| field is |token|, and the
5685 |value| field holds the corresponding |str_number|.  (4)~Capsules have
5686 |name_type=capsule|, and their |type| and |value| fields represent
5687 arbitrary values (in ways to be explained later).  (5)~Macro parameters
5688 are like symbolic tokens in that they appear in |info| fields of
5689 one-word nodes. The $k$th parameter is represented by |expr_base+k| if it
5690 is of type \&{expr}, or by |suffix_base+k| if it is of type \&{suffix}, or
5691 by |text_base+k| if it is of type \&{text}.  (Here |0<=k<param_size|.)
5692 Actual values of these parameters are kept in a separate stack, as we will
5693 see later.  The constants |expr_base|, |suffix_base|, and |text_base| are,
5694 of course, chosen so that there will be no confusion between symbolic
5695 tokens and parameters of various types.
5696
5697 Note that
5698 the `\\{type}' field of a node has nothing to do with ``type'' in a
5699 printer's sense. It's curious that the same word is used in such different ways.
5700
5701 @d type(A)   mp->mem[(A)].hh.b0 /* identifies what kind of value this is */
5702 @d name_type(A)   mp->mem[(A)].hh.b1 /* a clue to the name of this value */
5703 @d token_node_size 2 /* the number of words in a large token node */
5704 @d value_loc(A) ((A)+1) /* the word that contains the |value| field */
5705 @d value(A) mp->mem[value_loc((A))].cint /* the value stored in a large token node */
5706 @d expr_base (hash_end+1) /* code for the zeroth \&{expr} parameter */
5707 @d suffix_base (expr_base+mp->param_size) /* code for the zeroth \&{suffix} parameter */
5708 @d text_base (suffix_base+mp->param_size) /* code for the zeroth \&{text} parameter */
5709
5710 @<Check the ``constant''...@>=
5711 if ( text_base+mp->param_size>max_halfword ) mp->bad=18;
5712
5713 @ We have set aside a two word node beginning at |null| so that we can have
5714 |value(null)=0|.  We will make use of this coincidence later.
5715
5716 @<Initialize table entries...@>=
5717 link(null)=null; value(null)=0;
5718
5719 @ A numeric token is created by the following trivial routine.
5720
5721 @c 
5722 pointer mp_new_num_tok (MP mp,scaled v) {
5723   pointer p; /* the new node */
5724   p=mp_get_node(mp, token_node_size); value(p)=v;
5725   type(p)=mp_known; name_type(p)=mp_token; 
5726   return p;
5727 }
5728
5729 @ A token list is a singly linked list of nodes in |mem|, where
5730 each node contains a token and a link.  Here's a subroutine that gets rid
5731 of a token list when it is no longer needed.
5732
5733 @c void mp_flush_token_list (MP mp,pointer p) {
5734   pointer q; /* the node being recycled */
5735   while ( p!=null ) { 
5736     q=p; p=link(p);
5737     if ( q>=mp->hi_mem_min ) {
5738      free_avail(q);
5739     } else { 
5740       switch (type(q)) {
5741       case mp_vacuous: case mp_boolean_type: case mp_known:
5742         break;
5743       case mp_string_type:
5744         delete_str_ref(value(q));
5745         break;
5746       case unknown_types: case mp_pen_type: case mp_path_type: 
5747       case mp_picture_type: case mp_pair_type: case mp_color_type:
5748       case mp_cmykcolor_type: case mp_transform_type: case mp_dependent:
5749       case mp_proto_dependent: case mp_independent:
5750         mp_recycle_value(mp,q);
5751         break;
5752       default: mp_confusion(mp, "token");
5753 @:this can't happen token}{\quad token@>
5754       }
5755       mp_free_node(mp, q,token_node_size);
5756     }
5757   }
5758 }
5759
5760 @ The procedure |show_token_list|, which prints a symbolic form of
5761 the token list that starts at a given node |p|, illustrates these
5762 conventions. The token list being displayed should not begin with a reference
5763 count. However, the procedure is intended to be fairly robust, so that if the
5764 memory links are awry or if |p| is not really a pointer to a token list,
5765 almost nothing catastrophic can happen.
5766
5767 An additional parameter |q| is also given; this parameter is either null
5768 or it points to a node in the token list where a certain magic computation
5769 takes place that will be explained later. (Basically, |q| is non-null when
5770 we are printing the two-line context information at the time of an error
5771 message; |q| marks the place corresponding to where the second line
5772 should begin.)
5773
5774 The generation will stop, and `\.{\char`\ ETC.}' will be printed, if the length
5775 of printing exceeds a given limit~|l|; the length of printing upon entry is
5776 assumed to be a given amount called |null_tally|. (Note that
5777 |show_token_list| sometimes uses itself recursively to print
5778 variable names within a capsule.)
5779 @^recursion@>
5780
5781 Unusual entries are printed in the form of all-caps tokens
5782 preceded by a space, e.g., `\.{\char`\ BAD}'.
5783
5784 @<Declare the procedure called |show_token_list|@>=
5785 void mp_show_token_list (MP mp, integer p, integer q, integer l,
5786                          integer null_tally) ;
5787
5788 @ @c
5789 void mp_show_token_list (MP mp, integer p, integer q, integer l,
5790                          integer null_tally) {
5791   small_number class,c; /* the |char_class| of previous and new tokens */
5792   integer r,v; /* temporary registers */
5793   class=percent_class;
5794   mp->tally=null_tally;
5795   while ( (p!=null) && (mp->tally<l) ) { 
5796     if ( p==q ) 
5797       @<Do magic computation@>;
5798     @<Display token |p| and set |c| to its class;
5799       but |return| if there are problems@>;
5800     class=c; p=link(p);
5801   }
5802   if ( p!=null ) 
5803      mp_print(mp, " ETC.");
5804 @.ETC@>
5805   return;
5806 }
5807
5808 @ @<Display token |p| and set |c| to its class...@>=
5809 c=letter_class; /* the default */
5810 if ( (p<0)||(p>mp->mem_end) ) { 
5811   mp_print(mp, " CLOBBERED"); return;
5812 @.CLOBBERED@>
5813 }
5814 if ( p<mp->hi_mem_min ) { 
5815   @<Display two-word token@>;
5816 } else { 
5817   r=info(p);
5818   if ( r>=expr_base ) {
5819      @<Display a parameter token@>;
5820   } else {
5821     if ( r<1 ) {
5822       if ( r==0 ) { 
5823         @<Display a collective subscript@>
5824       } else {
5825         mp_print(mp, " IMPOSSIBLE");
5826 @.IMPOSSIBLE@>
5827       }
5828     } else { 
5829       r=text(r);
5830       if ( (r<0)||(r>mp->max_str_ptr) ) {
5831         mp_print(mp, " NONEXISTENT");
5832 @.NONEXISTENT@>
5833       } else {
5834        @<Print string |r| as a symbolic token
5835         and set |c| to its class@>;
5836       }
5837     }
5838   }
5839 }
5840
5841 @ @<Display two-word token@>=
5842 if ( name_type(p)==mp_token ) {
5843   if ( type(p)==mp_known ) {
5844     @<Display a numeric token@>;
5845   } else if ( type(p)!=mp_string_type ) {
5846     mp_print(mp, " BAD");
5847 @.BAD@>
5848   } else { 
5849     mp_print_char(mp, '"'); mp_print_str(mp, value(p)); mp_print_char(mp, '"');
5850     c=string_class;
5851   }
5852 } else if ((name_type(p)!=mp_capsule)||(type(p)<mp_vacuous)||(type(p)>mp_independent) ) {
5853   mp_print(mp, " BAD");
5854 } else { 
5855   mp_print_capsule(mp,p); c=right_paren_class;
5856 }
5857
5858 @ @<Display a numeric token@>=
5859 if ( class==digit_class ) 
5860   mp_print_char(mp, ' ');
5861 v=value(p);
5862 if ( v<0 ){ 
5863   if ( class==left_bracket_class ) 
5864     mp_print_char(mp, ' ');
5865   mp_print_char(mp, '['); mp_print_scaled(mp, v); mp_print_char(mp, ']');
5866   c=right_bracket_class;
5867 } else { 
5868   mp_print_scaled(mp, v); c=digit_class;
5869 }
5870
5871
5872 @ Strictly speaking, a genuine token will never have |info(p)=0|.
5873 But we will see later (in the |print_variable_name| routine) that
5874 it is convenient to let |info(p)=0| stand for `\.{[]}'.
5875
5876 @<Display a collective subscript@>=
5877 {
5878 if ( class==left_bracket_class ) 
5879   mp_print_char(mp, ' ');
5880 mp_print(mp, "[]"); c=right_bracket_class;
5881 }
5882
5883 @ @<Display a parameter token@>=
5884 {
5885 if ( r<suffix_base ) { 
5886   mp_print(mp, "(EXPR"); r=r-(expr_base);
5887 @.EXPR@>
5888 } else if ( r<text_base ) { 
5889   mp_print(mp, "(SUFFIX"); r=r-(suffix_base);
5890 @.SUFFIX@>
5891 } else { 
5892   mp_print(mp, "(TEXT"); r=r-(text_base);
5893 @.TEXT@>
5894 }
5895 mp_print_int(mp, r); mp_print_char(mp, ')'); c=right_paren_class;
5896 }
5897
5898
5899 @ @<Print string |r| as a symbolic token...@>=
5900
5901 c=mp->char_class[mp->str_pool[mp->str_start[r]]];
5902 if ( c==class ) {
5903   switch (c) {
5904   case letter_class:mp_print_char(mp, '.'); break;
5905   case isolated_classes: break;
5906   default: mp_print_char(mp, ' '); break;
5907   }
5908 }
5909 mp_print_str(mp, r);
5910 }
5911
5912 @ @<Declarations@>=
5913 void mp_print_capsule (MP mp, pointer p);
5914
5915 @ @<Declare miscellaneous procedures that were declared |forward|@>=
5916 void mp_print_capsule (MP mp, pointer p) { 
5917   mp_print_char(mp, '('); mp_print_exp(mp,p,0); mp_print_char(mp, ')');
5918 }
5919
5920 @ Macro definitions are kept in \MP's memory in the form of token lists
5921 that have a few extra one-word nodes at the beginning.
5922
5923 The first node contains a reference count that is used to tell when the
5924 list is no longer needed. To emphasize the fact that a reference count is
5925 present, we shall refer to the |info| field of this special node as the
5926 |ref_count| field.
5927 @^reference counts@>
5928
5929 The next node or nodes after the reference count serve to describe the
5930 formal parameters. They consist of zero or more parameter tokens followed
5931 by a code for the type of macro.
5932
5933 @d ref_count info
5934   /* reference count preceding a macro definition or picture header */
5935 @d add_mac_ref(A) incr(ref_count((A))) /* make a new reference to a macro list */
5936 @d general_macro 0 /* preface to a macro defined with a parameter list */
5937 @d primary_macro 1 /* preface to a macro with a \&{primary} parameter */
5938 @d secondary_macro 2 /* preface to a macro with a \&{secondary} parameter */
5939 @d tertiary_macro 3 /* preface to a macro with a \&{tertiary} parameter */
5940 @d expr_macro 4 /* preface to a macro with an undelimited \&{expr} parameter */
5941 @d of_macro 5 /* preface to a macro with
5942   undelimited `\&{expr} |x| \&{of}~|y|' parameters */
5943 @d suffix_macro 6 /* preface to a macro with an undelimited \&{suffix} parameter */
5944 @d text_macro 7 /* preface to a macro with an undelimited \&{text} parameter */
5945
5946 @c 
5947 void mp_delete_mac_ref (MP mp,pointer p) {
5948   /* |p| points to the reference count of a macro list that is
5949     losing one reference */
5950   if ( ref_count(p)==null ) mp_flush_token_list(mp, p);
5951   else decr(ref_count(p));
5952 }
5953
5954 @ The following subroutine displays a macro, given a pointer to its
5955 reference count.
5956
5957 @c 
5958 @<Declare the procedure called |print_cmd_mod|@>
5959 void mp_show_macro (MP mp, pointer p, integer q, integer l) {
5960   pointer r; /* temporary storage */
5961   p=link(p); /* bypass the reference count */
5962   while ( info(p)>text_macro ){ 
5963     r=link(p); link(p)=null;
5964     mp_show_token_list(mp, p,null,l,0); link(p)=r; p=r;
5965     if ( l>0 ) l=l-mp->tally; else return;
5966   } /* control printing of `\.{ETC.}' */
5967 @.ETC@>
5968   mp->tally=0;
5969   switch(info(p)) {
5970   case general_macro:mp_print(mp, "->"); break;
5971 @.->@>
5972   case primary_macro: case secondary_macro: case tertiary_macro:
5973     mp_print_char(mp, '<');
5974     mp_print_cmd_mod(mp, param_type,info(p)); 
5975     mp_print(mp, ">->");
5976     break;
5977   case expr_macro:mp_print(mp, "<expr>->"); break;
5978   case of_macro:mp_print(mp, "<expr>of<primary>->"); break;
5979   case suffix_macro:mp_print(mp, "<suffix>->"); break;
5980   case text_macro:mp_print(mp, "<text>->"); break;
5981   } /* there are no other cases */
5982   mp_show_token_list(mp, link(p),q,l-mp->tally,0);
5983 }
5984
5985 @* \[15] Data structures for variables.
5986 The variables of \MP\ programs can be simple, like `\.x', or they can
5987 combine the structural properties of arrays and records, like `\.{x20a.b}'.
5988 A \MP\ user assigns a type to a variable like \.{x20a.b} by saying, for
5989 example, `\.{boolean} \.{x[]a.b}'. It's time for us to study how such
5990 things are represented inside of the computer.
5991
5992 Each variable value occupies two consecutive words, either in a two-word
5993 node called a value node, or as a two-word subfield of a larger node.  One
5994 of those two words is called the |value| field; it is an integer,
5995 containing either a |scaled| numeric value or the representation of some
5996 other type of quantity. (It might also be subdivided into halfwords, in
5997 which case it is referred to by other names instead of |value|.) The other
5998 word is broken into subfields called |type|, |name_type|, and |link|.  The
5999 |type| field is a quarterword that specifies the variable's type, and
6000 |name_type| is a quarterword from which \MP\ can reconstruct the
6001 variable's name (sometimes by using the |link| field as well).  Thus, only
6002 1.25 words are actually devoted to the value itself; the other
6003 three-quarters of a word are overhead, but they aren't wasted because they
6004 allow \MP\ to deal with sparse arrays and to provide meaningful diagnostics.
6005
6006 In this section we shall be concerned only with the structural aspects of
6007 variables, not their values. Later parts of the program will change the
6008 |type| and |value| fields, but we shall treat those fields as black boxes
6009 whose contents should not be touched.
6010
6011 However, if the |type| field is |mp_structured|, there is no |value| field,
6012 and the second word is broken into two pointer fields called |attr_head|
6013 and |subscr_head|. Those fields point to additional nodes that
6014 contain structural information, as we shall see.
6015
6016 @d subscr_head_loc(A)   (A)+1 /* where |value|, |subscr_head| and |attr_head| are */
6017 @d attr_head(A)   info(subscr_head_loc((A))) /* pointer to attribute info */
6018 @d subscr_head(A)   link(subscr_head_loc((A))) /* pointer to subscript info */
6019 @d value_node_size 2 /* the number of words in a value node */
6020
6021 @ An attribute node is three words long. Two of these words contain |type|
6022 and |value| fields as described above, and the third word contains
6023 additional information:  There is an |attr_loc| field, which contains the
6024 hash address of the token that names this attribute; and there's also a
6025 |parent| field, which points to the value node of |mp_structured| type at the
6026 next higher level (i.e., at the level to which this attribute is
6027 subsidiary).  The |name_type| in an attribute node is `|attr|'.  The
6028 |link| field points to the next attribute with the same parent; these are
6029 arranged in increasing order, so that |attr_loc(link(p))>attr_loc(p)|. The
6030 final attribute node links to the constant |end_attr|, whose |attr_loc|
6031 field is greater than any legal hash address. The |attr_head| in the
6032 parent points to a node whose |name_type| is |mp_structured_root|; this
6033 node represents the null attribute, i.e., the variable that is relevant
6034 when no attributes are attached to the parent. The |attr_head| node
6035 has the fields of either
6036 a value node, a subscript node, or an attribute node, depending on what
6037 the parent would be if it were not structured; but the subscript and
6038 attribute fields are ignored, so it effectively contains only the data of
6039 a value node. The |link| field in this special node points to an attribute
6040 node whose |attr_loc| field is zero; the latter node represents a collective
6041 subscript `\.{[]}' attached to the parent, and its |link| field points to
6042 the first non-special attribute node (or to |end_attr| if there are none).
6043
6044 A subscript node likewise occupies three words, with |type| and |value| fields
6045 plus extra information; its |name_type| is |subscr|. In this case the
6046 third word is called the |subscript| field, which is a |scaled| integer.
6047 The |link| field points to the subscript node with the next larger
6048 subscript, if any; otherwise the |link| points to the attribute node
6049 for collective subscripts at this level. We have seen that the latter node
6050 contains an upward pointer, so that the parent can be deduced.
6051
6052 The |name_type| in a parent-less value node is |root|, and the |link|
6053 is the hash address of the token that names this value.
6054
6055 In other words, variables have a hierarchical structure that includes
6056 enough threads running around so that the program is able to move easily
6057 between siblings, parents, and children. An example should be helpful:
6058 (The reader is advised to draw a picture while reading the following
6059 description, since that will help to firm up the ideas.)
6060 Suppose that `\.x' and `\.{x.a}' and `\.{x[]b}' and `\.{x5}'
6061 and `\.{x20b}' have been mentioned in a user's program, where
6062 \.{x[]b} has been declared to be of \&{boolean} type. Let |h(x)|, |h(a)|,
6063 and |h(b)| be the hash addresses of \.x, \.a, and~\.b. Then
6064 |eq_type(h(x))=name| and |equiv(h(x))=p|, where |p|~is a two-word value
6065 node with |name_type(p)=root| and |link(p)=h(x)|. We have |type(p)=mp_structured|,
6066 |attr_head(p)=q|, and |subscr_head(p)=r|, where |q| points to a value
6067 node and |r| to a subscript node. (Are you still following this? Use
6068 a pencil to draw a diagram.) The lone variable `\.x' is represented by
6069 |type(q)| and |value(q)|; furthermore
6070 |name_type(q)=mp_structured_root| and |link(q)=q1|, where |q1| points
6071 to an attribute node representing `\.{x[]}'. Thus |name_type(q1)=attr|,
6072 |attr_loc(q1)=collective_subscript=0|, |parent(q1)=p|,
6073 |type(q1)=mp_structured|, |attr_head(q1)=qq|, and |subscr_head(q1)=qq1|;
6074 |qq| is a  three-word ``attribute-as-value'' node with |type(qq)=numeric_type|
6075 (assuming that \.{x5} is numeric, because |qq| represents `\.{x[]}' 
6076 with no further attributes), |name_type(qq)=structured_root|, 
6077 |attr_loc(qq)=0|, |parent(qq)=p|, and
6078 |link(qq)=qq1|. (Now pay attention to the next part.) Node |qq1| is
6079 an attribute node representing `\.{x[][]}', which has never yet
6080 occurred; its |type| field is |undefined|, and its |value| field is
6081 undefined. We have |name_type(qq1)=attr|, |attr_loc(qq1)=collective_subscript|,
6082 |parent(qq1)=q1|, and |link(qq1)=qq2|. Since |qq2| represents
6083 `\.{x[]b}', |type(qq2)=mp_unknown_boolean|; also |attr_loc(qq2)=h(b)|,
6084 |parent(qq2)=q1|, |name_type(qq2)=attr|, |link(qq2)=end_attr|.
6085 (Maybe colored lines will help untangle your picture.)
6086  Node |r| is a subscript node with |type| and |value|
6087 representing `\.{x5}'; |name_type(r)=subscr|, |subscript(r)=5.0|,
6088 and |link(r)=r1| is another subscript node. To complete the picture,
6089 see if you can guess what |link(r1)| is; give up? It's~|q1|.
6090 Furthermore |subscript(r1)=20.0|, |name_type(r1)=subscr|,
6091 |type(r1)=mp_structured|, |attr_head(r1)=qqq|, |subscr_head(r1)=qqq1|,
6092 and we finish things off with three more nodes
6093 |qqq|, |qqq1|, and |qqq2| hung onto~|r1|. (Perhaps you should start again
6094 with a larger sheet of paper.) The value of variable \.{x20b}
6095 appears in node~|qqq2|, as you can well imagine.
6096
6097 If the example in the previous paragraph doesn't make things crystal
6098 clear, a glance at some of the simpler subroutines below will reveal how
6099 things work out in practice.
6100
6101 The only really unusual thing about these conventions is the use of
6102 collective subscript attributes. The idea is to avoid repeating a lot of
6103 type information when many elements of an array are identical macros
6104 (for which distinct values need not be stored) or when they don't have
6105 all of the possible attributes. Branches of the structure below collective
6106 subscript attributes do not carry actual values except for macro identifiers;
6107 branches of the structure below subscript nodes do not carry significant
6108 information in their collective subscript attributes.
6109
6110 @d attr_loc_loc(A) ((A)+2) /* where the |attr_loc| and |parent| fields are */
6111 @d attr_loc(A) info(attr_loc_loc((A))) /* hash address of this attribute */
6112 @d parent(A) link(attr_loc_loc((A))) /* pointer to |mp_structured| variable */
6113 @d subscript_loc(A) ((A)+2) /* where the |subscript| field lives */
6114 @d subscript(A) mp->mem[subscript_loc((A))].sc /* subscript of this variable */
6115 @d attr_node_size 3 /* the number of words in an attribute node */
6116 @d subscr_node_size 3 /* the number of words in a subscript node */
6117 @d collective_subscript 0 /* code for the attribute `\.{[]}' */
6118
6119 @<Initialize table...@>=
6120 attr_loc(end_attr)=hash_end+1; parent(end_attr)=null;
6121
6122 @ Variables of type \&{pair} will have values that point to four-word
6123 nodes containing two numeric values. The first of these values has
6124 |name_type=mp_x_part_sector| and the second has |name_type=mp_y_part_sector|;
6125 the |link| in the first points back to the node whose |value| points
6126 to this four-word node.
6127
6128 Variables of type \&{transform} are similar, but in this case their
6129 |value| points to a 12-word node containing six values, identified by
6130 |x_part_sector|, |y_part_sector|, |mp_xx_part_sector|, |mp_xy_part_sector|,
6131 |mp_yx_part_sector|, and |mp_yy_part_sector|.
6132 Finally, variables of type \&{color} have 3~values in 6~words
6133 identified by |mp_red_part_sector|, |mp_green_part_sector|, and |mp_blue_part_sector|.
6134
6135 When an entire structured variable is saved, the |root| indication
6136 is temporarily replaced by |saved_root|.
6137
6138 Some variables have no name; they just are used for temporary storage
6139 while expressions are being evaluated. We call them {\sl capsules}.
6140
6141 @d x_part_loc(A) (A) /* where the \&{xpart} is found in a pair or transform node */
6142 @d y_part_loc(A) ((A)+2) /* where the \&{ypart} is found in a pair or transform node */
6143 @d xx_part_loc(A) ((A)+4) /* where the \&{xxpart} is found in a transform node */
6144 @d xy_part_loc(A) ((A)+6) /* where the \&{xypart} is found in a transform node */
6145 @d yx_part_loc(A) ((A)+8) /* where the \&{yxpart} is found in a transform node */
6146 @d yy_part_loc(A) ((A)+10) /* where the \&{yypart} is found in a transform node */
6147 @d red_part_loc(A) (A) /* where the \&{redpart} is found in a color node */
6148 @d green_part_loc(A) ((A)+2) /* where the \&{greenpart} is found in a color node */
6149 @d blue_part_loc(A) ((A)+4) /* where the \&{bluepart} is found in a color node */
6150 @d cyan_part_loc(A) (A) /* where the \&{cyanpart} is found in a color node */
6151 @d magenta_part_loc(A) ((A)+2) /* where the \&{magentapart} is found in a color node */
6152 @d yellow_part_loc(A) ((A)+4) /* where the \&{yellowpart} is found in a color node */
6153 @d black_part_loc(A) ((A)+6) /* where the \&{blackpart} is found in a color node */
6154 @d grey_part_loc(A) (A) /* where the \&{greypart} is found in a color node */
6155 @#
6156 @d pair_node_size 4 /* the number of words in a pair node */
6157 @d transform_node_size 12 /* the number of words in a transform node */
6158 @d color_node_size 6 /* the number of words in a color node */
6159 @d cmykcolor_node_size 8 /* the number of words in a color node */
6160
6161 @<Glob...@>=
6162 small_number big_node_size[mp_pair_type+1];
6163 small_number sector0[mp_pair_type+1];
6164 small_number sector_offset[mp_black_part_sector+1];
6165
6166 @ The |sector0| array gives for each big node type, |name_type| values
6167 for its first subfield; the |sector_offset| array gives for each
6168 |name_type| value, the offset from the first subfield in words;
6169 and the |big_node_size| array gives the size in words for each type of
6170 big node.
6171
6172 @<Set init...@>=
6173 mp->big_node_size[mp_transform_type]=transform_node_size;
6174 mp->big_node_size[mp_pair_type]=pair_node_size;
6175 mp->big_node_size[mp_color_type]=color_node_size;
6176 mp->big_node_size[mp_cmykcolor_type]=cmykcolor_node_size;
6177 mp->sector0[mp_transform_type]=mp_x_part_sector;
6178 mp->sector0[mp_pair_type]=mp_x_part_sector;
6179 mp->sector0[mp_color_type]=mp_red_part_sector;
6180 mp->sector0[mp_cmykcolor_type]=mp_cyan_part_sector;
6181 for (k=mp_x_part_sector;k<= mp_yy_part_sector;k++ ) {
6182   mp->sector_offset[k]=2*(k-mp_x_part_sector);
6183 }
6184 for (k=mp_red_part_sector;k<= mp_blue_part_sector ; k++) {
6185   mp->sector_offset[k]=2*(k-mp_red_part_sector);
6186 }
6187 for (k=mp_cyan_part_sector;k<= mp_black_part_sector;k++ ) {
6188   mp->sector_offset[k]=2*(k-mp_cyan_part_sector);
6189 }
6190
6191 @ If |type(p)=mp_pair_type| or |mp_transform_type| and if |value(p)=null|, the
6192 procedure call |init_big_node(p)| will allocate a pair or transform node
6193 for~|p|.  The individual parts of such nodes are initially of type
6194 |mp_independent|.
6195
6196 @c 
6197 void mp_init_big_node (MP mp,pointer p) {
6198   pointer q; /* the new node */
6199   small_number s; /* its size */
6200   s=mp->big_node_size[type(p)]; q=mp_get_node(mp, s);
6201   do {  
6202     s=s-2; 
6203     @<Make variable |q+s| newly independent@>;
6204     name_type(q+s)=halfp(s)+mp->sector0[type(p)]; 
6205     link(q+s)=null;
6206   } while (s!=0);
6207   link(q)=p; value(p)=q;
6208 }
6209
6210 @ The |id_transform| function creates a capsule for the
6211 identity transformation.
6212
6213 @c 
6214 pointer mp_id_transform (MP mp) {
6215   pointer p,q,r; /* list manipulation registers */
6216   p=mp_get_node(mp, value_node_size); type(p)=mp_transform_type;
6217   name_type(p)=mp_capsule; value(p)=null; mp_init_big_node(mp, p); q=value(p);
6218   r=q+transform_node_size;
6219   do {  
6220     r=r-2;
6221     type(r)=mp_known; value(r)=0;
6222   } while (r!=q);
6223   value(xx_part_loc(q))=unity; 
6224   value(yy_part_loc(q))=unity;
6225   return p;
6226 }
6227
6228 @ Tokens are of type |tag_token| when they first appear, but they point
6229 to |null| until they are first used as the root of a variable.
6230 The following subroutine establishes the root node on such grand occasions.
6231
6232 @c 
6233 void mp_new_root (MP mp,pointer x) {
6234   pointer p; /* the new node */
6235   p=mp_get_node(mp, value_node_size); type(p)=undefined; name_type(p)=mp_root;
6236   link(p)=x; equiv(x)=p;
6237 }
6238
6239 @ These conventions for variable representation are illustrated by the
6240 |print_variable_name| routine, which displays the full name of a
6241 variable given only a pointer to its two-word value packet.
6242
6243 @<Declarations@>=
6244 void mp_print_variable_name (MP mp, pointer p);
6245
6246 @ @c 
6247 void mp_print_variable_name (MP mp, pointer p) {
6248   pointer q; /* a token list that will name the variable's suffix */
6249   pointer r; /* temporary for token list creation */
6250   while ( name_type(p)>=mp_x_part_sector ) {
6251     @<Preface the output with a part specifier; |return| in the
6252       case of a capsule@>;
6253   }
6254   q=null;
6255   while ( name_type(p)>mp_saved_root ) {
6256     @<Ascend one level, pushing a token onto list |q|
6257      and replacing |p| by its parent@>;
6258   }
6259   r=mp_get_avail(mp); info(r)=link(p); link(r)=q;
6260   if ( name_type(p)==mp_saved_root ) mp_print(mp, "(SAVED)");
6261 @.SAVED@>
6262   mp_show_token_list(mp, r,null,el_gordo,mp->tally); 
6263   mp_flush_token_list(mp, r);
6264 }
6265
6266 @ @<Ascend one level, pushing a token onto list |q|...@>=
6267
6268   if ( name_type(p)==mp_subscr ) { 
6269     r=mp_new_num_tok(mp, subscript(p));
6270     do {  
6271       p=link(p);
6272     } while (name_type(p)!=mp_attr);
6273   } else if ( name_type(p)==mp_structured_root ) {
6274     p=link(p); goto FOUND;
6275   } else { 
6276     if ( name_type(p)!=mp_attr ) mp_confusion(mp, "var");
6277 @:this can't happen var}{\quad var@>
6278     r=mp_get_avail(mp); info(r)=attr_loc(p);
6279   }
6280   link(r)=q; q=r;
6281 FOUND:  
6282   p=parent(p);
6283 }
6284
6285 @ @<Preface the output with a part specifier...@>=
6286 { switch (name_type(p)) {
6287   case mp_x_part_sector: mp_print_char(mp, 'x'); break;
6288   case mp_y_part_sector: mp_print_char(mp, 'y'); break;
6289   case mp_xx_part_sector: mp_print(mp, "xx"); break;
6290   case mp_xy_part_sector: mp_print(mp, "xy"); break;
6291   case mp_yx_part_sector: mp_print(mp, "yx"); break;
6292   case mp_yy_part_sector: mp_print(mp, "yy"); break;
6293   case mp_red_part_sector: mp_print(mp, "red"); break;
6294   case mp_green_part_sector: mp_print(mp, "green"); break;
6295   case mp_blue_part_sector: mp_print(mp, "blue"); break;
6296   case mp_cyan_part_sector: mp_print(mp, "cyan"); break;
6297   case mp_magenta_part_sector: mp_print(mp, "magenta"); break;
6298   case mp_yellow_part_sector: mp_print(mp, "yellow"); break;
6299   case mp_black_part_sector: mp_print(mp, "black"); break;
6300   case mp_grey_part_sector: mp_print(mp, "grey"); break;
6301   case mp_capsule: 
6302     mp_print(mp, "%CAPSULE"); mp_print_int(mp, p-null); return;
6303     break;
6304 @.CAPSULE@>
6305   } /* there are no other cases */
6306   mp_print(mp, "part "); 
6307   p=link(p-mp->sector_offset[name_type(p)]);
6308 }
6309
6310 @ The |interesting| function returns |true| if a given variable is not
6311 in a capsule, or if the user wants to trace capsules.
6312
6313 @c 
6314 boolean mp_interesting (MP mp,pointer p) {
6315   small_number t; /* a |name_type| */
6316   if ( mp->internal[mp_tracing_capsules]>0 ) {
6317     return true;
6318   } else { 
6319     t=name_type(p);
6320     if ( t>=mp_x_part_sector ) if ( t!=mp_capsule )
6321       t=name_type(link(p-mp->sector_offset[t]));
6322     return (t!=mp_capsule);
6323   }
6324 }
6325
6326 @ Now here is a subroutine that converts an unstructured type into an
6327 equivalent structured type, by inserting a |mp_structured| node that is
6328 capable of growing. This operation is done only when |name_type(p)=root|,
6329 |subscr|, or |attr|.
6330
6331 The procedure returns a pointer to the new node that has taken node~|p|'s
6332 place in the structure. Node~|p| itself does not move, nor are its
6333 |value| or |type| fields changed in any way.
6334
6335 @c 
6336 pointer mp_new_structure (MP mp,pointer p) {
6337   pointer q,r=0; /* list manipulation registers */
6338   switch (name_type(p)) {
6339   case mp_root: 
6340     q=link(p); r=mp_get_node(mp, value_node_size); equiv(q)=r;
6341     break;
6342   case mp_subscr: 
6343     @<Link a new subscript node |r| in place of node |p|@>;
6344     break;
6345   case mp_attr: 
6346     @<Link a new attribute node |r| in place of node |p|@>;
6347     break;
6348   default: 
6349     mp_confusion(mp, "struct");
6350 @:this can't happen struct}{\quad struct@>
6351     break;
6352   }
6353   link(r)=link(p); type(r)=mp_structured; name_type(r)=name_type(p);
6354   attr_head(r)=p; name_type(p)=mp_structured_root;
6355   q=mp_get_node(mp, attr_node_size); link(p)=q; subscr_head(r)=q;
6356   parent(q)=r; type(q)=undefined; name_type(q)=mp_attr; link(q)=end_attr;
6357   attr_loc(q)=collective_subscript; 
6358   return r;
6359 }
6360
6361 @ @<Link a new subscript node |r| in place of node |p|@>=
6362
6363   q=p;
6364   do {  
6365     q=link(q);
6366   } while (name_type(q)!=mp_attr);
6367   q=parent(q); r=subscr_head_loc(q); /* |link(r)=subscr_head(q)| */
6368   do {  
6369     q=r; r=link(r);
6370   } while (r!=p);
6371   r=mp_get_node(mp, subscr_node_size);
6372   link(q)=r; subscript(r)=subscript(p);
6373 }
6374
6375 @ If the attribute is |collective_subscript|, there are two pointers to
6376 node~|p|, so we must change both of them.
6377
6378 @<Link a new attribute node |r| in place of node |p|@>=
6379
6380   q=parent(p); r=attr_head(q);
6381   do {  
6382     q=r; r=link(r);
6383   } while (r!=p);
6384   r=mp_get_node(mp, attr_node_size); link(q)=r;
6385   mp->mem[attr_loc_loc(r)]=mp->mem[attr_loc_loc(p)]; /* copy |attr_loc| and |parent| */
6386   if ( attr_loc(p)==collective_subscript ) { 
6387     q=subscr_head_loc(parent(p));
6388     while ( link(q)!=p ) q=link(q);
6389     link(q)=r;
6390   }
6391 }
6392
6393 @ The |find_variable| routine is given a pointer~|t| to a nonempty token
6394 list of suffixes; it returns a pointer to the corresponding two-word
6395 value. For example, if |t| points to token \.x followed by a numeric
6396 token containing the value~7, |find_variable| finds where the value of
6397 \.{x7} is stored in memory. This may seem a simple task, and it
6398 usually is, except when \.{x7} has never been referenced before.
6399 Indeed, \.x may never have even been subscripted before; complexities
6400 arise with respect to updating the collective subscript information.
6401
6402 If a macro type is detected anywhere along path~|t|, or if the first
6403 item on |t| isn't a |tag_token|, the value |null| is returned.
6404 Otherwise |p| will be a non-null pointer to a node such that
6405 |undefined<type(p)<mp_structured|.
6406
6407 @d abort_find { return null; }
6408
6409 @c 
6410 pointer mp_find_variable (MP mp,pointer t) {
6411   pointer p,q,r,s; /* nodes in the ``value'' line */
6412   pointer pp,qq,rr,ss; /* nodes in the ``collective'' line */
6413   integer n; /* subscript or attribute */
6414   memory_word save_word; /* temporary storage for a word of |mem| */
6415 @^inner loop@>
6416   p=info(t); t=link(t);
6417   if ( (eq_type(p) % outer_tag) != tag_token ) abort_find;
6418   if ( equiv(p)==null ) mp_new_root(mp, p);
6419   p=equiv(p); pp=p;
6420   while ( t!=null ) { 
6421     @<Make sure that both nodes |p| and |pp| are of |mp_structured| type@>;
6422     if ( t<mp->hi_mem_min ) {
6423       @<Descend one level for the subscript |value(t)|@>
6424     } else {
6425       @<Descend one level for the attribute |info(t)|@>;
6426     }
6427     t=link(t);
6428   }
6429   if ( type(pp)>=mp_structured ) {
6430     if ( type(pp)==mp_structured ) pp=attr_head(pp); else abort_find;
6431   }
6432   if ( type(p)==mp_structured ) p=attr_head(p);
6433   if ( type(p)==undefined ) { 
6434     if ( type(pp)==undefined ) { type(pp)=mp_numeric_type; value(pp)=null; };
6435     type(p)=type(pp); value(p)=null;
6436   };
6437   return p;
6438 }
6439
6440 @ Although |pp| and |p| begin together, they diverge when a subscript occurs;
6441 |pp|~stays in the collective line while |p|~goes through actual subscript
6442 values.
6443
6444 @<Make sure that both nodes |p| and |pp|...@>=
6445 if ( type(pp)!=mp_structured ) { 
6446   if ( type(pp)>mp_structured ) abort_find;
6447   ss=mp_new_structure(mp, pp);
6448   if ( p==pp ) p=ss;
6449   pp=ss;
6450 }; /* now |type(pp)=mp_structured| */
6451 if ( type(p)!=mp_structured ) /* it cannot be |>mp_structured| */
6452   p=mp_new_structure(mp, p) /* now |type(p)=mp_structured| */
6453
6454 @ We want this part of the program to be reasonably fast, in case there are
6455 @^inner loop@>
6456 lots of subscripts at the same level of the data structure. Therefore
6457 we store an ``infinite'' value in the word that appears at the end of the
6458 subscript list, even though that word isn't part of a subscript node.
6459
6460 @<Descend one level for the subscript |value(t)|@>=
6461
6462   n=value(t);
6463   pp=link(attr_head(pp)); /* now |attr_loc(pp)=collective_subscript| */
6464   q=link(attr_head(p)); save_word=mp->mem[subscript_loc(q)];
6465   subscript(q)=el_gordo; s=subscr_head_loc(p); /* |link(s)=subscr_head(p)| */
6466   do {  
6467     r=s; s=link(s);
6468   } while (n>subscript(s));
6469   if ( n==subscript(s) ) {
6470     p=s;
6471   } else { 
6472     p=mp_get_node(mp, subscr_node_size); link(r)=p; link(p)=s;
6473     subscript(p)=n; name_type(p)=mp_subscr; type(p)=undefined;
6474   }
6475   mp->mem[subscript_loc(q)]=save_word;
6476 }
6477
6478 @ @<Descend one level for the attribute |info(t)|@>=
6479
6480   n=info(t);
6481   ss=attr_head(pp);
6482   do {  
6483     rr=ss; ss=link(ss);
6484   } while (n>attr_loc(ss));
6485   if ( n<attr_loc(ss) ) { 
6486     qq=mp_get_node(mp, attr_node_size); link(rr)=qq; link(qq)=ss;
6487     attr_loc(qq)=n; name_type(qq)=mp_attr; type(qq)=undefined;
6488     parent(qq)=pp; ss=qq;
6489   }
6490   if ( p==pp ) { 
6491     p=ss; pp=ss;
6492   } else { 
6493     pp=ss; s=attr_head(p);
6494     do {  
6495       r=s; s=link(s);
6496     } while (n>attr_loc(s));
6497     if ( n==attr_loc(s) ) {
6498       p=s;
6499     } else { 
6500       q=mp_get_node(mp, attr_node_size); link(r)=q; link(q)=s;
6501       attr_loc(q)=n; name_type(q)=mp_attr; type(q)=undefined;
6502       parent(q)=p; p=q;
6503     }
6504   }
6505 }
6506
6507 @ Variables lose their former values when they appear in a type declaration,
6508 or when they are defined to be macros or \&{let} equal to something else.
6509 A subroutine will be defined later that recycles the storage associated
6510 with any particular |type| or |value|; our goal now is to study a higher
6511 level process called |flush_variable|, which selectively frees parts of a
6512 variable structure.
6513
6514 This routine has some complexity because of examples such as
6515 `\hbox{\tt numeric x[]a[]b}'
6516 which recycles all variables of the form \.{x[i]a[j]b} (and no others), while
6517 `\hbox{\tt vardef x[]a[]=...}'
6518 discards all variables of the form \.{x[i]a[j]} followed by an arbitrary
6519 suffix, except for the collective node \.{x[]a[]} itself. The obvious way
6520 to handle such examples is to use recursion; so that's what we~do.
6521 @^recursion@>
6522
6523 Parameter |p| points to the root information of the variable;
6524 parameter |t| points to a list of one-word nodes that represent
6525 suffixes, with |info=collective_subscript| for subscripts.
6526
6527 @<Declarations@>=
6528 @<Declare subroutines for printing expressions@>
6529 @<Declare basic dependency-list subroutines@>
6530 @<Declare the recycling subroutines@>
6531 void mp_flush_cur_exp (MP mp,scaled v) ;
6532 @<Declare the procedure called |flush_below_variable|@>
6533
6534 @ @c 
6535 void mp_flush_variable (MP mp,pointer p, pointer t, boolean discard_suffixes) {
6536   pointer q,r; /* list manipulation */
6537   halfword n; /* attribute to match */
6538   while ( t!=null ) { 
6539     if ( type(p)!=mp_structured ) return;
6540     n=info(t); t=link(t);
6541     if ( n==collective_subscript ) { 
6542       r=subscr_head_loc(p); q=link(r); /* |q=subscr_head(p)| */
6543       while ( name_type(q)==mp_subscr ){ 
6544         mp_flush_variable(mp, q,t,discard_suffixes);
6545         if ( t==null ) {
6546           if ( type(q)==mp_structured ) r=q;
6547           else  { link(r)=link(q); mp_free_node(mp, q,subscr_node_size);   }
6548         } else {
6549           r=q;
6550         }
6551         q=link(r);
6552       }
6553     }
6554     p=attr_head(p);
6555     do {  
6556       r=p; p=link(p);
6557     } while (attr_loc(p)<n);
6558     if ( attr_loc(p)!=n ) return;
6559   }
6560   if ( discard_suffixes ) {
6561     mp_flush_below_variable(mp, p);
6562   } else { 
6563     if ( type(p)==mp_structured ) p=attr_head(p);
6564     mp_recycle_value(mp, p);
6565   }
6566 }
6567
6568 @ The next procedure is simpler; it wipes out everything but |p| itself,
6569 which becomes undefined.
6570
6571 @<Declare the procedure called |flush_below_variable|@>=
6572 void mp_flush_below_variable (MP mp, pointer p);
6573
6574 @ @c
6575 void mp_flush_below_variable (MP mp,pointer p) {
6576    pointer q,r; /* list manipulation registers */
6577   if ( type(p)!=mp_structured ) {
6578     mp_recycle_value(mp, p); /* this sets |type(p)=undefined| */
6579   } else { 
6580     q=subscr_head(p);
6581     while ( name_type(q)==mp_subscr ) { 
6582       mp_flush_below_variable(mp, q); r=q; q=link(q);
6583       mp_free_node(mp, r,subscr_node_size);
6584     }
6585     r=attr_head(p); q=link(r); mp_recycle_value(mp, r);
6586     if ( name_type(p)<=mp_saved_root ) mp_free_node(mp, r,value_node_size);
6587     else mp_free_node(mp, r,subscr_node_size);
6588     /* we assume that |subscr_node_size=attr_node_size| */
6589     do {  
6590       mp_flush_below_variable(mp, q); r=q; q=link(q); mp_free_node(mp, r,attr_node_size);
6591     } while (q!=end_attr);
6592     type(p)=undefined;
6593   }
6594 }
6595
6596 @ Just before assigning a new value to a variable, we will recycle the
6597 old value and make the old value undefined. The |und_type| routine
6598 determines what type of undefined value should be given, based on
6599 the current type before recycling.
6600
6601 @c 
6602 small_number mp_und_type (MP mp,pointer p) { 
6603   switch (type(p)) {
6604   case undefined: case mp_vacuous:
6605     return undefined;
6606   case mp_boolean_type: case mp_unknown_boolean:
6607     return mp_unknown_boolean;
6608   case mp_string_type: case mp_unknown_string:
6609     return mp_unknown_string;
6610   case mp_pen_type: case mp_unknown_pen:
6611     return mp_unknown_pen;
6612   case mp_path_type: case mp_unknown_path:
6613     return mp_unknown_path;
6614   case mp_picture_type: case mp_unknown_picture:
6615     return mp_unknown_picture;
6616   case mp_transform_type: case mp_color_type: case mp_cmykcolor_type:
6617   case mp_pair_type: case mp_numeric_type: 
6618     return type(p);
6619   case mp_known: case mp_dependent: case mp_proto_dependent: case mp_independent:
6620     return mp_numeric_type;
6621   } /* there are no other cases */
6622   return 0;
6623 }
6624
6625 @ The |clear_symbol| routine is used when we want to redefine the equivalent
6626 of a symbolic token. It must remove any variable structure or macro
6627 definition that is currently attached to that symbol. If the |saving|
6628 parameter is true, a subsidiary structure is saved instead of destroyed.
6629
6630 @c 
6631 void mp_clear_symbol (MP mp,pointer p, boolean saving) {
6632   pointer q; /* |equiv(p)| */
6633   q=equiv(p);
6634   switch (eq_type(p) % outer_tag)  {
6635   case defined_macro:
6636   case secondary_primary_macro:
6637   case tertiary_secondary_macro:
6638   case expression_tertiary_macro: 
6639     if ( ! saving ) mp_delete_mac_ref(mp, q);
6640     break;
6641   case tag_token:
6642     if ( q!=null ) {
6643       if ( saving ) {
6644         name_type(q)=mp_saved_root;
6645       } else { 
6646         mp_flush_below_variable(mp, q); 
6647             mp_free_node(mp,q,value_node_size); 
6648       }
6649     }
6650     break;
6651   default:
6652     break;
6653   }
6654   mp->eqtb[p]=mp->eqtb[frozen_undefined];
6655 }
6656
6657 @* \[16] Saving and restoring equivalents.
6658 The nested structure given by \&{begingroup} and \&{endgroup}
6659 allows |eqtb| entries to be saved and restored, so that temporary changes
6660 can be made without difficulty.  When the user requests a current value to
6661 be saved, \MP\ puts that value into its ``save stack.'' An appearance of
6662 \&{endgroup} ultimately causes the old values to be removed from the save
6663 stack and put back in their former places.
6664
6665 The save stack is a linked list containing three kinds of entries,
6666 distinguished by their |info| fields. If |p| points to a saved item,
6667 then
6668
6669 \smallskip\hang
6670 |info(p)=0| stands for a group boundary; each \&{begingroup} contributes
6671 such an item to the save stack and each \&{endgroup} cuts back the stack
6672 until the most recent such entry has been removed.
6673
6674 \smallskip\hang
6675 |info(p)=q|, where |1<=q<=hash_end|, means that |mem[p+1]| holds the former
6676 contents of |eqtb[q]|. Such save stack entries are generated by \&{save}
6677 commands.
6678
6679 \smallskip\hang
6680 |info(p)=hash_end+q|, where |q>0|, means that |value(p)| is a |scaled|
6681 integer to be restored to internal parameter number~|q|. Such entries
6682 are generated by \&{interim} commands.
6683
6684 \smallskip\noindent
6685 The global variable |save_ptr| points to the top item on the save stack.
6686
6687 @d save_node_size 2 /* number of words per non-boundary save-stack node */
6688 @d saved_equiv(A) mp->mem[(A)+1].hh /* where an |eqtb| entry gets saved */
6689 @d save_boundary_item(A) { (A)=mp_get_avail(mp); info((A))=0;
6690   link((A))=mp->save_ptr; mp->save_ptr=(A);
6691   }
6692
6693 @<Glob...@>=
6694 pointer save_ptr; /* the most recently saved item */
6695
6696 @ @<Set init...@>=mp->save_ptr=null;
6697
6698 @ The |save_variable| routine is given a hash address |q|; it salts this
6699 address in the save stack, together with its current equivalent,
6700 then makes token~|q| behave as though it were brand new.
6701
6702 Nothing is stacked when |save_ptr=null|, however; there's no way to remove
6703 things from the stack when the program is not inside a group, so there's
6704 no point in wasting the space.
6705
6706 @c void mp_save_variable (MP mp,pointer q) {
6707   pointer p; /* temporary register */
6708   if ( mp->save_ptr!=null ){ 
6709     p=mp_get_node(mp, save_node_size); info(p)=q; link(p)=mp->save_ptr;
6710     saved_equiv(p)=mp->eqtb[q]; mp->save_ptr=p;
6711   }
6712   mp_clear_symbol(mp, q,(mp->save_ptr!=null));
6713 }
6714
6715 @ Similarly, |save_internal| is given the location |q| of an internal
6716 quantity like |mp_tracing_pens|. It creates a save stack entry of the
6717 third kind.
6718
6719 @c void mp_save_internal (MP mp,halfword q) {
6720   pointer p; /* new item for the save stack */
6721   if ( mp->save_ptr!=null ){ 
6722      p=mp_get_node(mp, save_node_size); info(p)=hash_end+q;
6723     link(p)=mp->save_ptr; value(p)=mp->internal[q]; mp->save_ptr=p;
6724   }
6725 }
6726
6727 @ At the end of a group, the |unsave| routine restores all of the saved
6728 equivalents in reverse order. This routine will be called only when there
6729 is at least one boundary item on the save stack.
6730
6731 @c 
6732 void mp_unsave (MP mp) {
6733   pointer q; /* index to saved item */
6734   pointer p; /* temporary register */
6735   while ( info(mp->save_ptr)!=0 ) {
6736     q=info(mp->save_ptr);
6737     if ( q>hash_end ) {
6738       if ( mp->internal[mp_tracing_restores]>0 ) {
6739         mp_begin_diagnostic(mp); mp_print_nl(mp, "{restoring ");
6740         mp_print(mp, mp->int_name[q-(hash_end)]); mp_print_char(mp, '=');
6741         mp_print_scaled(mp, value(mp->save_ptr)); mp_print_char(mp, '}');
6742         mp_end_diagnostic(mp, false);
6743       }
6744       mp->internal[q-(hash_end)]=value(mp->save_ptr);
6745     } else { 
6746       if ( mp->internal[mp_tracing_restores]>0 ) {
6747         mp_begin_diagnostic(mp); mp_print_nl(mp, "{restoring ");
6748         mp_print_text(q); mp_print_char(mp, '}');
6749         mp_end_diagnostic(mp, false);
6750       }
6751       mp_clear_symbol(mp, q,false);
6752       mp->eqtb[q]=saved_equiv(mp->save_ptr);
6753       if ( eq_type(q) % outer_tag==tag_token ) {
6754         p=equiv(q);
6755         if ( p!=null ) name_type(p)=mp_root;
6756       }
6757     }
6758     p=link(mp->save_ptr); 
6759     mp_free_node(mp, mp->save_ptr,save_node_size); mp->save_ptr=p;
6760   }
6761   p=link(mp->save_ptr); free_avail(mp->save_ptr); mp->save_ptr=p;
6762 }
6763
6764 @* \[17] Data structures for paths.
6765 When a \MP\ user specifies a path, \MP\ will create a list of knots
6766 and control points for the associated cubic spline curves. If the
6767 knots are $z_0$, $z_1$, \dots, $z_n$, there are control points
6768 $z_k^+$ and $z_{k+1}^-$ such that the cubic splines between knots
6769 $z_k$ and $z_{k+1}$ are defined by B\'ezier's formula
6770 @:Bezier}{B\'ezier, Pierre Etienne@>
6771 $$\eqalign{z(t)&=B(z_k,z_k^+,z_{k+1}^-,z_{k+1};t)\cr
6772 &=(1-t)^3z_k+3(1-t)^2tz_k^++3(1-t)t^2z_{k+1}^-+t^3z_{k+1}\cr}$$
6773 for |0<=t<=1|.
6774
6775 There is a 8-word node for each knot $z_k$, containing one word of
6776 control information and six words for the |x| and |y| coordinates of
6777 $z_k^-$ and $z_k$ and~$z_k^+$. The control information appears in the
6778 |left_type| and |right_type| fields, which each occupy a quarter of
6779 the first word in the node; they specify properties of the curve as it
6780 enters and leaves the knot. There's also a halfword |link| field,
6781 which points to the following knot, and a final supplementary word (of
6782 which only a quarter is used).
6783
6784 If the path is a closed contour, knots 0 and |n| are identical;
6785 i.e., the |link| in knot |n-1| points to knot~0. But if the path
6786 is not closed, the |left_type| of knot~0 and the |right_type| of knot~|n|
6787 are equal to |endpoint|. In the latter case the |link| in knot~|n| points
6788 to knot~0, and the control points $z_0^-$ and $z_n^+$ are not used.
6789
6790 @d left_type(A)   mp->mem[(A)].hh.b0 /* characterizes the path entering this knot */
6791 @d right_type(A)   mp->mem[(A)].hh.b1 /* characterizes the path leaving this knot */
6792 @d x_coord(A)   mp->mem[(A)+1].sc /* the |x| coordinate of this knot */
6793 @d y_coord(A)   mp->mem[(A)+2].sc /* the |y| coordinate of this knot */
6794 @d left_x(A)   mp->mem[(A)+3].sc /* the |x| coordinate of previous control point */
6795 @d left_y(A)   mp->mem[(A)+4].sc /* the |y| coordinate of previous control point */
6796 @d right_x(A)   mp->mem[(A)+5].sc /* the |x| coordinate of next control point */
6797 @d right_y(A)   mp->mem[(A)+6].sc /* the |y| coordinate of next control point */
6798 @d x_loc(A)   ((A)+1) /* where the |x| coordinate is stored in a knot */
6799 @d y_loc(A)   ((A)+2) /* where the |y| coordinate is stored in a knot */
6800 @d knot_coord(A)   mp->mem[(A)].sc /* |x| or |y| coordinate given |x_loc| or |y_loc| */
6801 @d left_coord(A)   mp->mem[(A)+2].sc
6802   /* coordinate of previous control point given |x_loc| or |y_loc| */
6803 @d right_coord(A)   mp->mem[(A)+4].sc
6804   /* coordinate of next control point given |x_loc| or |y_loc| */
6805 @d knot_node_size 8 /* number of words in a knot node */
6806
6807 @<Types...@>=
6808 enum mp_knot_type {
6809  mp_endpoint=0, /* |left_type| at path beginning and |right_type| at path end */
6810  mp_explicit, /* |left_type| or |right_type| when control points are known */
6811  mp_given, /* |left_type| or |right_type| when a direction is given */
6812  mp_curl, /* |left_type| or |right_type| when a curl is desired */
6813  mp_open, /* |left_type| or |right_type| when \MP\ should choose the direction */
6814  mp_end_cycle
6815 };
6816
6817 @ Before the B\'ezier control points have been calculated, the memory
6818 space they will ultimately occupy is taken up by information that can be
6819 used to compute them. There are four cases:
6820
6821 \yskip
6822 \textindent{$\bullet$} If |right_type=mp_open|, the curve should leave
6823 the knot in the same direction it entered; \MP\ will figure out a
6824 suitable direction.
6825
6826 \yskip
6827 \textindent{$\bullet$} If |right_type=mp_curl|, the curve should leave the
6828 knot in a direction depending on the angle at which it enters the next
6829 knot and on the curl parameter stored in |right_curl|.
6830
6831 \yskip
6832 \textindent{$\bullet$} If |right_type=mp_given|, the curve should leave the
6833 knot in a nonzero direction stored as an |angle| in |right_given|.
6834
6835 \yskip
6836 \textindent{$\bullet$} If |right_type=mp_explicit|, the B\'ezier control
6837 point for leaving this knot has already been computed; it is in the
6838 |right_x| and |right_y| fields.
6839
6840 \yskip\noindent
6841 The rules for |left_type| are similar, but they refer to the curve entering
6842 the knot, and to \\{left} fields instead of \\{right} fields.
6843
6844 Non-|explicit| control points will be chosen based on ``tension'' parameters
6845 in the |left_tension| and |right_tension| fields. The
6846 `\&{atleast}' option is represented by negative tension values.
6847 @:at_least_}{\&{atleast} primitive@>
6848
6849 For example, the \MP\ path specification
6850 $$\.{z0..z1..tension atleast 1..\{curl 2\}z2..z3\{-1,-2\}..tension
6851   3 and 4..p},$$
6852 where \.p is the path `\.{z4..controls z45 and z54..z5}', will be represented
6853 by the six knots
6854 \def\lodash{\hbox to 1.1em{\thinspace\hrulefill\thinspace}}
6855 $$\vbox{\halign{#\hfil&&\qquad#\hfil\cr
6856 |left_type|&\\{left} info&|x_coord,y_coord|&|right_type|&\\{right} info\cr
6857 \noalign{\yskip}
6858 |endpoint|&\lodash$,\,$\lodash&$x_0,y_0$&|curl|&$1.0,1.0$\cr
6859 |open|&\lodash$,1.0$&$x_1,y_1$&|open|&\lodash$,-1.0$\cr
6860 |curl|&$2.0,-1.0$&$x_2,y_2$&|curl|&$2.0,1.0$\cr
6861 |given|&$d,1.0$&$x_3,y_3$&|given|&$d,3.0$\cr
6862 |open|&\lodash$,4.0$&$x_4,y_4$&|explicit|&$x_{45},y_{45}$\cr
6863 |explicit|&$x_{54},y_{54}$&$x_5,y_5$&|endpoint|&\lodash$,\,$\lodash\cr}}$$
6864 Here |d| is the |angle| obtained by calling |n_arg(-unity,-two)|.
6865 Of course, this example is more complicated than anything a normal user
6866 would ever write.
6867
6868 These types must satisfy certain restrictions because of the form of \MP's
6869 path syntax:
6870 (i)~|open| type never appears in the same node together with |endpoint|,
6871 |given|, or |curl|.
6872 (ii)~The |right_type| of a node is |explicit| if and only if the
6873 |left_type| of the following node is |explicit|.
6874 (iii)~|endpoint| types occur only at the ends, as mentioned above.
6875
6876 @d left_curl left_x /* curl information when entering this knot */
6877 @d left_given left_x /* given direction when entering this knot */
6878 @d left_tension left_y /* tension information when entering this knot */
6879 @d right_curl right_x /* curl information when leaving this knot */
6880 @d right_given right_x /* given direction when leaving this knot */
6881 @d right_tension right_y /* tension information when leaving this knot */
6882
6883 @ Knots can be user-supplied, or they can be created by program code,
6884 like the |split_cubic| function, or |copy_path|. The distinction is
6885 needed for the cleanup routine that runs after |split_cubic|, because
6886 it should only delete knots it has previously inserted, and never
6887 anything that was user-supplied. In order to be able to differentiate
6888 one knot from another, we will set |originator(p):=mp_metapost_user| when
6889 it appeared in the actual metapost program, and
6890 |originator(p):=mp_program_code| in all other cases.
6891
6892 @d originator(A)   mp->mem[(A)+7].hh.b0 /* the creator of this knot */
6893
6894 @<Types...@>=
6895 enum {
6896   mp_program_code=0, /* not created by a user */
6897   mp_metapost_user /* created by a user */
6898 };
6899
6900 @ Here is a routine that prints a given knot list
6901 in symbolic form. It illustrates the conventions discussed above,
6902 and checks for anomalies that might arise while \MP\ is being debugged.
6903
6904 @<Declare subroutines for printing expressions@>=
6905 void mp_pr_path (MP mp,pointer h);
6906
6907 @ @c
6908 void mp_pr_path (MP mp,pointer h) {
6909   pointer p,q; /* for list traversal */
6910   p=h;
6911   do {  
6912     q=link(p);
6913     if ( (p==null)||(q==null) ) { 
6914       mp_print_nl(mp, "???"); return; /* this won't happen */
6915 @.???@>
6916     }
6917     @<Print information for adjacent knots |p| and |q|@>;
6918   DONE1:
6919     p=q;
6920     if ( (p!=h)||(left_type(h)!=mp_endpoint) ) {
6921       @<Print two dots, followed by |given| or |curl| if present@>;
6922     }
6923   } while (p!=h);
6924   if ( left_type(h)!=mp_endpoint ) 
6925     mp_print(mp, "cycle");
6926 }
6927
6928 @ @<Print information for adjacent knots...@>=
6929 mp_print_two(mp, x_coord(p),y_coord(p));
6930 switch (right_type(p)) {
6931 case mp_endpoint: 
6932   if ( left_type(p)==mp_open ) mp_print(mp, "{open?}"); /* can't happen */
6933 @.open?@>
6934   if ( (left_type(q)!=mp_endpoint)||(q!=h) ) q=null; /* force an error */
6935   goto DONE1;
6936   break;
6937 case mp_explicit: 
6938   @<Print control points between |p| and |q|, then |goto done1|@>;
6939   break;
6940 case mp_open: 
6941   @<Print information for a curve that begins |open|@>;
6942   break;
6943 case mp_curl:
6944 case mp_given: 
6945   @<Print information for a curve that begins |curl| or |given|@>;
6946   break;
6947 default:
6948   mp_print(mp, "???"); /* can't happen */
6949 @.???@>
6950   break;
6951 }
6952 if ( left_type(q)<=mp_explicit ) {
6953   mp_print(mp, "..control?"); /* can't happen */
6954 @.control?@>
6955 } else if ( (right_tension(p)!=unity)||(left_tension(q)!=unity) ) {
6956   @<Print tension between |p| and |q|@>;
6957 }
6958
6959 @ Since |n_sin_cos| produces |fraction| results, which we will print as if they
6960 were |scaled|, the magnitude of a |given| direction vector will be~4096.
6961
6962 @<Print two dots...@>=
6963
6964   mp_print_nl(mp, " ..");
6965   if ( left_type(p)==mp_given ) { 
6966     mp_n_sin_cos(mp, left_given(p)); mp_print_char(mp, '{');
6967     mp_print_scaled(mp, mp->n_cos); mp_print_char(mp, ',');
6968     mp_print_scaled(mp, mp->n_sin); mp_print_char(mp, '}');
6969   } else if ( left_type(p)==mp_curl ){ 
6970     mp_print(mp, "{curl "); 
6971     mp_print_scaled(mp, left_curl(p)); mp_print_char(mp, '}');
6972   }
6973 }
6974
6975 @ @<Print tension between |p| and |q|@>=
6976
6977   mp_print(mp, "..tension ");
6978   if ( right_tension(p)<0 ) mp_print(mp, "atleast");
6979   mp_print_scaled(mp, abs(right_tension(p)));
6980   if ( right_tension(p)!=left_tension(q) ){ 
6981     mp_print(mp, " and ");
6982     if ( left_tension(q)<0 ) mp_print(mp, "atleast");
6983     mp_print_scaled(mp, abs(left_tension(q)));
6984   }
6985 }
6986
6987 @ @<Print control points between |p| and |q|, then |goto done1|@>=
6988
6989   mp_print(mp, "..controls "); 
6990   mp_print_two(mp, right_x(p),right_y(p)); 
6991   mp_print(mp, " and ");
6992   if ( left_type(q)!=mp_explicit ) { 
6993     mp_print(mp, "??"); /* can't happen */
6994 @.??@>
6995   } else {
6996     mp_print_two(mp, left_x(q),left_y(q));
6997   }
6998   goto DONE1;
6999 }
7000
7001 @ @<Print information for a curve that begins |open|@>=
7002 if ( (left_type(p)!=mp_explicit)&&(left_type(p)!=mp_open) ) {
7003   mp_print(mp, "{open?}"); /* can't happen */
7004 @.open?@>
7005 }
7006
7007 @ A curl of 1 is shown explicitly, so that the user sees clearly that
7008 \MP's default curl is present.
7009
7010 @<Print information for a curve that begins |curl|...@>=
7011
7012   if ( left_type(p)==mp_open )  
7013     mp_print(mp, "??"); /* can't happen */
7014 @.??@>
7015   if ( right_type(p)==mp_curl ) { 
7016     mp_print(mp, "{curl "); mp_print_scaled(mp, right_curl(p));
7017   } else { 
7018     mp_n_sin_cos(mp, right_given(p)); mp_print_char(mp, '{');
7019     mp_print_scaled(mp, mp->n_cos); mp_print_char(mp, ','); 
7020     mp_print_scaled(mp, mp->n_sin);
7021   }
7022   mp_print_char(mp, '}');
7023 }
7024
7025 @ It is convenient to have another version of |pr_path| that prints the path
7026 as a diagnostic message.
7027
7028 @<Declare subroutines for printing expressions@>=
7029 void mp_print_path (MP mp,pointer h, const char *s, boolean nuline) { 
7030   mp_print_diagnostic(mp, "Path", s, nuline); mp_print_ln(mp);
7031 @.Path at line...@>
7032   mp_pr_path(mp, h);
7033   mp_end_diagnostic(mp, true);
7034 }
7035
7036 @ If we want to duplicate a knot node, we can say |copy_knot|:
7037
7038 @c 
7039 pointer mp_copy_knot (MP mp,pointer p) {
7040   pointer q; /* the copy */
7041   int k; /* runs through the words of a knot node */
7042   q=mp_get_node(mp, knot_node_size);
7043   for (k=0;k<knot_node_size;k++) {
7044     mp->mem[q+k]=mp->mem[p+k];
7045   }
7046   originator(q)=originator(p);
7047   return q;
7048 }
7049
7050 @ The |copy_path| routine makes a clone of a given path.
7051
7052 @c 
7053 pointer mp_copy_path (MP mp, pointer p) {
7054   pointer q,pp,qq; /* for list manipulation */
7055   q=mp_copy_knot(mp, p);
7056   qq=q; pp=link(p);
7057   while ( pp!=p ) { 
7058     link(qq)=mp_copy_knot(mp, pp);
7059     qq=link(qq);
7060     pp=link(pp);
7061   }
7062   link(qq)=q;
7063   return q;
7064 }
7065
7066
7067 @ Just before |ship_out|, knot lists are exported for printing.
7068
7069 The |gr_XXXX| macros are defined in |mppsout.h|.
7070
7071 @c 
7072 mp_knot *mp_export_knot (MP mp,pointer p) {
7073   mp_knot *q; /* the copy */
7074   if (p==null)
7075      return NULL;
7076   q = mp_xmalloc(mp, 1, sizeof (mp_knot));
7077   memset(q,0,sizeof (mp_knot));
7078   gr_left_type(q)  = left_type(p);
7079   gr_right_type(q) = right_type(p);
7080   gr_x_coord(q)    = x_coord(p);
7081   gr_y_coord(q)    = y_coord(p);
7082   gr_left_x(q)     = left_x(p);
7083   gr_left_y(q)     = left_y(p);
7084   gr_right_x(q)    = right_x(p);
7085   gr_right_y(q)    = right_y(p);
7086   gr_originator(q) = originator(p);
7087   return q;
7088 }
7089
7090 @ The |export_knot_list| routine therefore also makes a clone 
7091 of a given path.
7092
7093 @c 
7094 mp_knot *mp_export_knot_list (MP mp, pointer p) {
7095   mp_knot *q, *qq; /* for list manipulation */
7096   pointer pp; /* for list manipulation */
7097   if (p==null)
7098      return NULL;
7099   q=mp_export_knot(mp, p);
7100   qq=q; pp=link(p);
7101   while ( pp!=p ) { 
7102     gr_next_knot(qq)=mp_export_knot(mp, pp);
7103     qq=gr_next_knot(qq);
7104     pp=link(pp);
7105   }
7106   gr_next_knot(qq)=q;
7107   return q;
7108 }
7109
7110
7111 @ Similarly, there's a way to copy the {\sl reverse\/} of a path. This procedure
7112 returns a pointer to the first node of the copy, if the path is a cycle,
7113 but to the final node of a non-cyclic copy. The global
7114 variable |path_tail| will point to the final node of the original path;
7115 this trick makes it easier to implement `\&{doublepath}'.
7116
7117 All node types are assumed to be |endpoint| or |explicit| only.
7118
7119 @c 
7120 pointer mp_htap_ypoc (MP mp,pointer p) {
7121   pointer q,pp,qq,rr; /* for list manipulation */
7122   q=mp_get_node(mp, knot_node_size); /* this will correspond to |p| */
7123   qq=q; pp=p;
7124   while (1) { 
7125     right_type(qq)=left_type(pp); left_type(qq)=right_type(pp);
7126     x_coord(qq)=x_coord(pp); y_coord(qq)=y_coord(pp);
7127     right_x(qq)=left_x(pp); right_y(qq)=left_y(pp);
7128     left_x(qq)=right_x(pp); left_y(qq)=right_y(pp);
7129     originator(qq)=originator(pp);
7130     if ( link(pp)==p ) { 
7131       link(q)=qq; mp->path_tail=pp; return q;
7132     }
7133     rr=mp_get_node(mp, knot_node_size); link(rr)=qq; qq=rr; pp=link(pp);
7134   }
7135 }
7136
7137 @ @<Glob...@>=
7138 pointer path_tail; /* the node that links to the beginning of a path */
7139
7140 @ When a cyclic list of knot nodes is no longer needed, it can be recycled by
7141 calling the following subroutine.
7142
7143 @<Declare the recycling subroutines@>=
7144 void mp_toss_knot_list (MP mp,pointer p) ;
7145
7146 @ @c
7147 void mp_toss_knot_list (MP mp,pointer p) {
7148   pointer q; /* the node being freed */
7149   pointer r; /* the next node */
7150   q=p;
7151   do {  
7152     r=link(q); 
7153     mp_free_node(mp, q,knot_node_size); q=r;
7154   } while (q!=p);
7155 }
7156
7157 @* \[18] Choosing control points.
7158 Now we must actually delve into one of \MP's more difficult routines,
7159 the |make_choices| procedure that chooses angles and control points for
7160 the splines of a curve when the user has not specified them explicitly.
7161 The parameter to |make_choices| points to a list of knots and
7162 path information, as described above.
7163
7164 A path decomposes into independent segments at ``breakpoint'' knots,
7165 which are knots whose left and right angles are both prespecified in
7166 some way (i.e., their |left_type| and |right_type| aren't both open).
7167
7168 @c 
7169 @<Declare the procedure called |solve_choices|@>
7170 void mp_make_choices (MP mp,pointer knots) {
7171   pointer h; /* the first breakpoint */
7172   pointer p,q; /* consecutive breakpoints being processed */
7173   @<Other local variables for |make_choices|@>;
7174   check_arith; /* make sure that |arith_error=false| */
7175   if ( mp->internal[mp_tracing_choices]>0 )
7176     mp_print_path(mp, knots,", before choices",true);
7177   @<If consecutive knots are equal, join them explicitly@>;
7178   @<Find the first breakpoint, |h|, on the path;
7179     insert an artificial breakpoint if the path is an unbroken cycle@>;
7180   p=h;
7181   do {  
7182     @<Fill in the control points between |p| and the next breakpoint,
7183       then advance |p| to that breakpoint@>;
7184   } while (p!=h);
7185   if ( mp->internal[mp_tracing_choices]>0 )
7186     mp_print_path(mp, knots,", after choices",true);
7187   if ( mp->arith_error ) {
7188     @<Report an unexpected problem during the choice-making@>;
7189   }
7190 }
7191
7192 @ @<Report an unexpected problem during the choice...@>=
7193
7194   print_err("Some number got too big");
7195 @.Some number got too big@>
7196   help2("The path that I just computed is out of range.")
7197        ("So it will probably look funny. Proceed, for a laugh.");
7198   mp_put_get_error(mp); mp->arith_error=false;
7199 }
7200
7201 @ Two knots in a row with the same coordinates will always be joined
7202 by an explicit ``curve'' whose control points are identical with the
7203 knots.
7204
7205 @<If consecutive knots are equal, join them explicitly@>=
7206 p=knots;
7207 do {  
7208   q=link(p);
7209   if ( x_coord(p)==x_coord(q) && y_coord(p)==y_coord(q) && right_type(p)>mp_explicit ) { 
7210     right_type(p)=mp_explicit;
7211     if ( left_type(p)==mp_open ) { 
7212       left_type(p)=mp_curl; left_curl(p)=unity;
7213     }
7214     left_type(q)=mp_explicit;
7215     if ( right_type(q)==mp_open ) { 
7216       right_type(q)=mp_curl; right_curl(q)=unity;
7217     }
7218     right_x(p)=x_coord(p); left_x(q)=x_coord(p);
7219     right_y(p)=y_coord(p); left_y(q)=y_coord(p);
7220   }
7221   p=q;
7222 } while (p!=knots)
7223
7224 @ If there are no breakpoints, it is necessary to compute the direction
7225 angles around an entire cycle. In this case the |left_type| of the first
7226 node is temporarily changed to |end_cycle|.
7227
7228 @<Find the first breakpoint, |h|, on the path...@>=
7229 h=knots;
7230 while (1) { 
7231   if ( left_type(h)!=mp_open ) break;
7232   if ( right_type(h)!=mp_open ) break;
7233   h=link(h);
7234   if ( h==knots ) { 
7235     left_type(h)=mp_end_cycle; break;
7236   }
7237 }
7238
7239 @ If |right_type(p)<given| and |q=link(p)|, we must have
7240 |right_type(p)=left_type(q)=mp_explicit| or |endpoint|.
7241
7242 @<Fill in the control points between |p| and the next breakpoint...@>=
7243 q=link(p);
7244 if ( right_type(p)>=mp_given ) { 
7245   while ( (left_type(q)==mp_open)&&(right_type(q)==mp_open) ) q=link(q);
7246   @<Fill in the control information between
7247     consecutive breakpoints |p| and |q|@>;
7248 } else if ( right_type(p)==mp_endpoint ) {
7249   @<Give reasonable values for the unused control points between |p| and~|q|@>;
7250 }
7251 p=q
7252
7253 @ This step makes it possible to transform an explicitly computed path without
7254 checking the |left_type| and |right_type| fields.
7255
7256 @<Give reasonable values for the unused control points between |p| and~|q|@>=
7257
7258   right_x(p)=x_coord(p); right_y(p)=y_coord(p);
7259   left_x(q)=x_coord(q); left_y(q)=y_coord(q);
7260 }
7261
7262 @ Before we can go further into the way choices are made, we need to
7263 consider the underlying theory. The basic ideas implemented in |make_choices|
7264 are due to John Hobby, who introduced the notion of ``mock curvature''
7265 @^Hobby, John Douglas@>
7266 at a knot. Angles are chosen so that they preserve mock curvature when
7267 a knot is passed, and this has been found to produce excellent results.
7268
7269 It is convenient to introduce some notations that simplify the necessary
7270 formulas. Let $d_{k,k+1}=\vert z\k-z_k\vert$ be the (nonzero) distance
7271 between knots |k| and |k+1|; and let
7272 $${z\k-z_k\over z_k-z_{k-1}}={d_{k,k+1}\over d_{k-1,k}}e^{i\psi_k}$$
7273 so that a polygonal line from $z_{k-1}$ to $z_k$ to $z\k$ turns left
7274 through an angle of~$\psi_k$. We assume that $\vert\psi_k\vert\L180^\circ$.
7275 The control points for the spline from $z_k$ to $z\k$ will be denoted by
7276 $$\eqalign{z_k^+&=z_k+
7277   \textstyle{1\over3}\rho_k e^{i\theta_k}(z\k-z_k),\cr
7278  z\k^-&=z\k-
7279   \textstyle{1\over3}\sigma\k e^{-i\phi\k}(z\k-z_k),\cr}$$
7280 where $\rho_k$ and $\sigma\k$ are nonnegative ``velocity ratios'' at the
7281 beginning and end of the curve, while $\theta_k$ and $\phi\k$ are the
7282 corresponding ``offset angles.'' These angles satisfy the condition
7283 $$\theta_k+\phi_k+\psi_k=0,\eqno(*)$$
7284 whenever the curve leaves an intermediate knot~|k| in the direction that
7285 it enters.
7286
7287 @ Let $\alpha_k$ and $\beta\k$ be the reciprocals of the ``tension'' of
7288 the curve at its beginning and ending points. This means that
7289 $\rho_k=\alpha_k f(\theta_k,\phi\k)$ and $\sigma\k=\beta\k f(\phi\k,\theta_k)$,
7290 where $f(\theta,\phi)$ is \MP's standard velocity function defined in
7291 the |velocity| subroutine. The cubic spline $B(z_k^{\phantom+},z_k^+,
7292 z\k^-,z\k^{\phantom+};t)$
7293 has curvature
7294 @^curvature@>
7295 $${2\sigma\k\sin(\theta_k+\phi\k)-6\sin\theta_k\over\rho_k^2d_{k,k+1}}
7296 \qquad{\rm and}\qquad
7297 {2\rho_k\sin(\theta_k+\phi\k)-6\sin\phi\k\over\sigma\k^2d_{k,k+1}}$$
7298 at |t=0| and |t=1|, respectively. The mock curvature is the linear
7299 @^mock curvature@>
7300 approximation to this true curvature that arises in the limit for
7301 small $\theta_k$ and~$\phi\k$, if second-order terms are discarded.
7302 The standard velocity function satisfies
7303 $$f(\theta,\phi)=1+O(\theta^2+\theta\phi+\phi^2);$$
7304 hence the mock curvatures are respectively
7305 $${2\beta\k(\theta_k+\phi\k)-6\theta_k\over\alpha_k^2d_{k,k+1}}
7306 \qquad{\rm and}\qquad
7307 {2\alpha_k(\theta_k+\phi\k)-6\phi\k\over\beta\k^2d_{k,k+1}}.\eqno(**)$$
7308
7309 @ The turning angles $\psi_k$ are given, and equation $(*)$ above
7310 determines $\phi_k$ when $\theta_k$ is known, so the task of
7311 angle selection is essentially to choose appropriate values for each
7312 $\theta_k$. When equation~$(*)$ is used to eliminate $\phi$~variables
7313 from $(**)$, we obtain a system of linear equations of the form
7314 $$A_k\theta_{k-1}+(B_k+C_k)\theta_k+D_k\theta\k=-B_k\psi_k-D_k\psi\k,$$
7315 where
7316 $$A_k={\alpha_{k-1}\over\beta_k^2d_{k-1,k}},
7317 \qquad B_k={3-\alpha_{k-1}\over\beta_k^2d_{k-1,k}},
7318 \qquad C_k={3-\beta\k\over\alpha_k^2d_{k,k+1}},
7319 \qquad D_k={\beta\k\over\alpha_k^2d_{k,k+1}}.$$
7320 The tensions are always $3\over4$ or more, hence each $\alpha$ and~$\beta$
7321 will be at most $4\over3$. It follows that $B_k\G{5\over4}A_k$ and
7322 $C_k\G{5\over4}D_k$; hence the equations are diagonally dominant;
7323 hence they have a unique solution. Moreover, in most cases the tensions
7324 are equal to~1, so that $B_k=2A_k$ and $C_k=2D_k$. This makes the
7325 solution numerically stable, and there is an exponential damping
7326 effect: The data at knot $k\pm j$ affects the angle at knot~$k$ by
7327 a factor of~$O(2^{-j})$.
7328
7329 @ However, we still must consider the angles at the starting and ending
7330 knots of a non-cyclic path. These angles might be given explicitly, or
7331 they might be specified implicitly in terms of an amount of ``curl.''
7332
7333 Let's assume that angles need to be determined for a non-cyclic path
7334 starting at $z_0$ and ending at~$z_n$. Then equations of the form
7335 $$A_k\theta_{k-1}+(B_k+C_k)\theta_k+D_k\theta_{k+1}=R_k$$
7336 have been given for $0<k<n$, and it will be convenient to introduce
7337 equations of the same form for $k=0$ and $k=n$, where
7338 $$A_0=B_0=C_n=D_n=0.$$
7339 If $\theta_0$ is supposed to have a given value $E_0$, we simply
7340 define $C_0=1$, $D_0=0$, and $R_0=E_0$. Otherwise a curl
7341 parameter, $\gamma_0$, has been specified at~$z_0$; this means
7342 that the mock curvature at $z_0$ should be $\gamma_0$ times the
7343 mock curvature at $z_1$; i.e.,
7344 $${2\beta_1(\theta_0+\phi_1)-6\theta_0\over\alpha_0^2d_{01}}
7345 =\gamma_0{2\alpha_0(\theta_0+\phi_1)-6\phi_1\over\beta_1^2d_{01}}.$$
7346 This equation simplifies to
7347 $$(\alpha_0\chi_0+3-\beta_1)\theta_0+
7348  \bigl((3-\alpha_0)\chi_0+\beta_1\bigr)\theta_1=
7349  -\bigl((3-\alpha_0)\chi_0+\beta_1\bigr)\psi_1,$$
7350 where $\chi_0=\alpha_0^2\gamma_0/\beta_1^2$; so we can set $C_0=
7351 \chi_0\alpha_0+3-\beta_1$, $D_0=(3-\alpha_0)\chi_0+\beta_1$, $R_0=-D_0\psi_1$.
7352 It can be shown that $C_0>0$ and $C_0B_1-A_1D_0>0$ when $\gamma_0\G0$,
7353 hence the linear equations remain nonsingular.
7354
7355 Similar considerations apply at the right end, when the final angle $\phi_n$
7356 may or may not need to be determined. It is convenient to let $\psi_n=0$,
7357 hence $\theta_n=-\phi_n$. We either have an explicit equation $\theta_n=E_n$,
7358 or we have
7359 $$\bigl((3-\beta_n)\chi_n+\alpha_{n-1}\bigr)\theta_{n-1}+
7360 (\beta_n\chi_n+3-\alpha_{n-1})\theta_n=0,\qquad
7361   \chi_n={\beta_n^2\gamma_n\over\alpha_{n-1}^2}.$$
7362
7363 When |make_choices| chooses angles, it must compute the coefficients of
7364 these linear equations, then solve the equations. To compute the coefficients,
7365 it is necessary to compute arctangents of the given turning angles~$\psi_k$.
7366 When the equations are solved, the chosen directions $\theta_k$ are put
7367 back into the form of control points by essentially computing sines and
7368 cosines.
7369
7370 @ OK, we are ready to make the hard choices of |make_choices|.
7371 Most of the work is relegated to an auxiliary procedure
7372 called |solve_choices|, which has been introduced to keep
7373 |make_choices| from being extremely long.
7374
7375 @<Fill in the control information between...@>=
7376 @<Calculate the turning angles $\psi_k$ and the distances $d_{k,k+1}$;
7377   set $n$ to the length of the path@>;
7378 @<Remove |open| types at the breakpoints@>;
7379 mp_solve_choices(mp, p,q,n)
7380
7381 @ It's convenient to precompute quantities that will be needed several
7382 times later. The values of |delta_x[k]| and |delta_y[k]| will be the
7383 coordinates of $z\k-z_k$, and the magnitude of this vector will be
7384 |delta[k]=@t$d_{k,k+1}$@>|. The path angle $\psi_k$ between $z_k-z_{k-1}$
7385 and $z\k-z_k$ will be stored in |psi[k]|.
7386
7387 @<Glob...@>=
7388 int path_size; /* maximum number of knots between breakpoints of a path */
7389 scaled *delta_x;
7390 scaled *delta_y;
7391 scaled *delta; /* knot differences */
7392 angle  *psi; /* turning angles */
7393
7394 @ @<Allocate or initialize ...@>=
7395 mp->delta_x = NULL;
7396 mp->delta_y = NULL;
7397 mp->delta = NULL;
7398 mp->psi = NULL;
7399
7400 @ @<Dealloc variables@>=
7401 xfree(mp->delta_x);
7402 xfree(mp->delta_y);
7403 xfree(mp->delta);
7404 xfree(mp->psi);
7405
7406 @ @<Other local variables for |make_choices|@>=
7407   int k,n; /* current and final knot numbers */
7408   pointer s,t; /* registers for list traversal */
7409   scaled delx,dely; /* directions where |open| meets |explicit| */
7410   fraction sine,cosine; /* trig functions of various angles */
7411
7412 @ @<Calculate the turning angles...@>=
7413 {
7414 RESTART:
7415   k=0; s=p; n=mp->path_size;
7416   do {  
7417     t=link(s);
7418     mp->delta_x[k]=x_coord(t)-x_coord(s);
7419     mp->delta_y[k]=y_coord(t)-y_coord(s);
7420     mp->delta[k]=mp_pyth_add(mp, mp->delta_x[k],mp->delta_y[k]);
7421     if ( k>0 ) { 
7422       sine=mp_make_fraction(mp, mp->delta_y[k-1],mp->delta[k-1]);
7423       cosine=mp_make_fraction(mp, mp->delta_x[k-1],mp->delta[k-1]);
7424       mp->psi[k]=mp_n_arg(mp, mp_take_fraction(mp, mp->delta_x[k],cosine)+
7425         mp_take_fraction(mp, mp->delta_y[k],sine),
7426         mp_take_fraction(mp, mp->delta_y[k],cosine)-
7427           mp_take_fraction(mp, mp->delta_x[k],sine));
7428     }
7429     incr(k); s=t;
7430     if ( k==mp->path_size ) {
7431       mp_reallocate_paths(mp, mp->path_size+(mp->path_size>>2));
7432       goto RESTART; /* retry, loop size has changed */
7433     }
7434     if ( s==q ) n=k;
7435   } while (!((k>=n)&&(left_type(s)!=mp_end_cycle)));
7436   if ( k==n ) mp->psi[n]=0; else mp->psi[k]=mp->psi[1];
7437 }
7438
7439 @ When we get to this point of the code, |right_type(p)| is either
7440 |given| or |curl| or |open|. If it is |open|, we must have
7441 |left_type(p)=mp_end_cycle| or |left_type(p)=mp_explicit|. In the latter
7442 case, the |open| type is converted to |given|; however, if the
7443 velocity coming into this knot is zero, the |open| type is
7444 converted to a |curl|, since we don't know the incoming direction.
7445
7446 Similarly, |left_type(q)| is either |given| or |curl| or |open| or
7447 |mp_end_cycle|. The |open| possibility is reduced either to |given| or to |curl|.
7448
7449 @<Remove |open| types at the breakpoints@>=
7450 if ( left_type(q)==mp_open ) { 
7451   delx=right_x(q)-x_coord(q); dely=right_y(q)-y_coord(q);
7452   if ( (delx==0)&&(dely==0) ) { 
7453     left_type(q)=mp_curl; left_curl(q)=unity;
7454   } else { 
7455     left_type(q)=mp_given; left_given(q)=mp_n_arg(mp, delx,dely);
7456   }
7457 }
7458 if ( (right_type(p)==mp_open)&&(left_type(p)==mp_explicit) ) { 
7459   delx=x_coord(p)-left_x(p); dely=y_coord(p)-left_y(p);
7460   if ( (delx==0)&&(dely==0) ) { 
7461     right_type(p)=mp_curl; right_curl(p)=unity;
7462   } else { 
7463     right_type(p)=mp_given; right_given(p)=mp_n_arg(mp, delx,dely);
7464   }
7465 }
7466
7467 @ Linear equations need to be solved whenever |n>1|; and also when |n=1|
7468 and exactly one of the breakpoints involves a curl. The simplest case occurs
7469 when |n=1| and there is a curl at both breakpoints; then we simply draw
7470 a straight line.
7471
7472 But before coding up the simple cases, we might as well face the general case,
7473 since we must deal with it sooner or later, and since the general case
7474 is likely to give some insight into the way simple cases can be handled best.
7475
7476 When there is no cycle, the linear equations to be solved form a tridiagonal
7477 system, and we can apply the standard technique of Gaussian elimination
7478 to convert that system to a sequence of equations of the form
7479 $$\theta_0+u_0\theta_1=v_0,\quad
7480 \theta_1+u_1\theta_2=v_1,\quad\ldots,\quad
7481 \theta_{n-1}+u_{n-1}\theta_n=v_{n-1},\quad
7482 \theta_n=v_n.$$
7483 It is possible to do this diagonalization while generating the equations.
7484 Once $\theta_n$ is known, it is easy to determine $\theta_{n-1}$, \dots,
7485 $\theta_1$, $\theta_0$; thus, the equations will be solved.
7486
7487 The procedure is slightly more complex when there is a cycle, but the
7488 basic idea will be nearly the same. In the cyclic case the right-hand
7489 sides will be $v_k+w_k\theta_0$ instead of simply $v_k$, and we will start
7490 the process off with $u_0=v_0=0$, $w_0=1$. The final equation will be not
7491 $\theta_n=v_n$ but $\theta_n+u_n\theta_1=v_n+w_n\theta_0$; an appropriate
7492 ending routine will take account of the fact that $\theta_n=\theta_0$ and
7493 eliminate the $w$'s from the system, after which the solution can be
7494 obtained as before.
7495
7496 When $u_k$, $v_k$, and $w_k$ are being computed, the three pointer
7497 variables |r|, |s|,~|t| will point respectively to knots |k-1|, |k|,
7498 and~|k+1|. The $u$'s and $w$'s are scaled by $2^{28}$, i.e., they are
7499 of type |fraction|; the $\theta$'s and $v$'s are of type |angle|.
7500
7501 @<Glob...@>=
7502 angle *theta; /* values of $\theta_k$ */
7503 fraction *uu; /* values of $u_k$ */
7504 angle *vv; /* values of $v_k$ */
7505 fraction *ww; /* values of $w_k$ */
7506
7507 @ @<Allocate or initialize ...@>=
7508 mp->theta = NULL;
7509 mp->uu = NULL;
7510 mp->vv = NULL;
7511 mp->ww = NULL;
7512
7513 @ @<Dealloc variables@>=
7514 xfree(mp->theta);
7515 xfree(mp->uu);
7516 xfree(mp->vv);
7517 xfree(mp->ww);
7518
7519 @ @<Declare |mp_reallocate| functions@>=
7520 void mp_reallocate_paths (MP mp, int l);
7521
7522 @ @c
7523 void mp_reallocate_paths (MP mp, int l) {
7524   XREALLOC (mp->delta_x, l, scaled);
7525   XREALLOC (mp->delta_y, l, scaled);
7526   XREALLOC (mp->delta,   l, scaled);
7527   XREALLOC (mp->psi,     l, angle);
7528   XREALLOC (mp->theta,   l, angle);
7529   XREALLOC (mp->uu,      l, fraction);
7530   XREALLOC (mp->vv,      l, angle);
7531   XREALLOC (mp->ww,      l, fraction);
7532   mp->path_size = l;
7533 }
7534
7535 @ Our immediate problem is to get the ball rolling by setting up the
7536 first equation or by realizing that no equations are needed, and to fit
7537 this initialization into a framework suitable for the overall computation.
7538
7539 @<Declare the procedure called |solve_choices|@>=
7540 @<Declare subroutines needed by |solve_choices|@>
7541 void mp_solve_choices (MP mp,pointer p, pointer q, halfword n) {
7542   int k; /* current knot number */
7543   pointer r,s,t; /* registers for list traversal */
7544   @<Other local variables for |solve_choices|@>;
7545   k=0; s=p; r=0;
7546   while (1) { 
7547     t=link(s);
7548     if ( k==0 ) {
7549       @<Get the linear equations started; or |return|
7550         with the control points in place, if linear equations
7551         needn't be solved@>
7552     } else  { 
7553       switch (left_type(s)) {
7554       case mp_end_cycle: case mp_open:
7555         @<Set up equation to match mock curvatures
7556           at $z_k$; then |goto found| with $\theta_n$
7557           adjusted to equal $\theta_0$, if a cycle has ended@>;
7558         break;
7559       case mp_curl:
7560         @<Set up equation for a curl at $\theta_n$
7561           and |goto found|@>;
7562         break;
7563       case mp_given:
7564         @<Calculate the given value of $\theta_n$
7565           and |goto found|@>;
7566         break;
7567       } /* there are no other cases */
7568     }
7569     r=s; s=t; incr(k);
7570   }
7571 FOUND:
7572   @<Finish choosing angles and assigning control points@>;
7573 }
7574
7575 @ On the first time through the loop, we have |k=0| and |r| is not yet
7576 defined. The first linear equation, if any, will have $A_0=B_0=0$.
7577
7578 @<Get the linear equations started...@>=
7579 switch (right_type(s)) {
7580 case mp_given: 
7581   if ( left_type(t)==mp_given ) {
7582     @<Reduce to simple case of two givens  and |return|@>
7583   } else {
7584     @<Set up the equation for a given value of $\theta_0$@>;
7585   }
7586   break;
7587 case mp_curl: 
7588   if ( left_type(t)==mp_curl ) {
7589     @<Reduce to simple case of straight line and |return|@>
7590   } else {
7591     @<Set up the equation for a curl at $\theta_0$@>;
7592   }
7593   break;
7594 case mp_open: 
7595   mp->uu[0]=0; mp->vv[0]=0; mp->ww[0]=fraction_one;
7596   /* this begins a cycle */
7597   break;
7598 } /* there are no other cases */
7599
7600 @ The general equation that specifies equality of mock curvature at $z_k$ is
7601 $$A_k\theta_{k-1}+(B_k+C_k)\theta_k+D_k\theta\k=-B_k\psi_k-D_k\psi\k,$$
7602 as derived above. We want to combine this with the already-derived equation
7603 $\theta_{k-1}+u_{k-1}\theta_k=v_{k-1}+w_{k-1}\theta_0$ in order to obtain
7604 a new equation
7605 $\theta_k+u_k\theta\k=v_k+w_k\theta_0$. This can be done by dividing the
7606 equation
7607 $$(B_k-u_{k-1}A_k+C_k)\theta_k+D_k\theta\k=-B_k\psi_k-D_k\psi\k-A_kv_{k-1}
7608     -A_kw_{k-1}\theta_0$$
7609 by $B_k-u_{k-1}A_k+C_k$. The trick is to do this carefully with
7610 fixed-point arithmetic, avoiding the chance of overflow while retaining
7611 suitable precision.
7612
7613 The calculations will be performed in several registers that
7614 provide temporary storage for intermediate quantities.
7615
7616 @<Other local variables for |solve_choices|@>=
7617 fraction aa,bb,cc,ff,acc; /* temporary registers */
7618 scaled dd,ee; /* likewise, but |scaled| */
7619 scaled lt,rt; /* tension values */
7620
7621 @ @<Set up equation to match mock curvatures...@>=
7622 { @<Calculate the values $\\{aa}=A_k/B_k$, $\\{bb}=D_k/C_k$,
7623     $\\{dd}=(3-\alpha_{k-1})d_{k,k+1}$, $\\{ee}=(3-\beta\k)d_{k-1,k}$,
7624     and $\\{cc}=(B_k-u_{k-1}A_k)/B_k$@>;
7625   @<Calculate the ratio $\\{ff}=C_k/(C_k+B_k-u_{k-1}A_k)$@>;
7626   mp->uu[k]=mp_take_fraction(mp, ff,bb);
7627   @<Calculate the values of $v_k$ and $w_k$@>;
7628   if ( left_type(s)==mp_end_cycle ) {
7629     @<Adjust $\theta_n$ to equal $\theta_0$ and |goto found|@>;
7630   }
7631 }
7632
7633 @ Since tension values are never less than 3/4, the values |aa| and
7634 |bb| computed here are never more than 4/5.
7635
7636 @<Calculate the values $\\{aa}=...@>=
7637 if ( abs(right_tension(r))==unity) { 
7638   aa=fraction_half; dd=2*mp->delta[k];
7639 } else { 
7640   aa=mp_make_fraction(mp, unity,3*abs(right_tension(r))-unity);
7641   dd=mp_take_fraction(mp, mp->delta[k],
7642     fraction_three-mp_make_fraction(mp, unity,abs(right_tension(r))));
7643 }
7644 if ( abs(left_tension(t))==unity ){ 
7645   bb=fraction_half; ee=2*mp->delta[k-1];
7646 } else { 
7647   bb=mp_make_fraction(mp, unity,3*abs(left_tension(t))-unity);
7648   ee=mp_take_fraction(mp, mp->delta[k-1],
7649     fraction_three-mp_make_fraction(mp, unity,abs(left_tension(t))));
7650 }
7651 cc=fraction_one-mp_take_fraction(mp, mp->uu[k-1],aa)
7652
7653 @ The ratio to be calculated in this step can be written in the form
7654 $$\beta_k^2\cdot\\{ee}\over\beta_k^2\cdot\\{ee}+\alpha_k^2\cdot
7655   \\{cc}\cdot\\{dd},$$
7656 because of the quantities just calculated. The values of |dd| and |ee|
7657 will not be needed after this step has been performed.
7658
7659 @<Calculate the ratio $\\{ff}=C_k/(C_k+B_k-u_{k-1}A_k)$@>=
7660 dd=mp_take_fraction(mp, dd,cc); lt=abs(left_tension(s)); rt=abs(right_tension(s));
7661 if ( lt!=rt ) { /* $\beta_k^{-1}\ne\alpha_k^{-1}$ */
7662   if ( lt<rt ) { 
7663     ff=mp_make_fraction(mp, lt,rt);
7664     ff=mp_take_fraction(mp, ff,ff); /* $\alpha_k^2/\beta_k^2$ */
7665     dd=mp_take_fraction(mp, dd,ff);
7666   } else { 
7667     ff=mp_make_fraction(mp, rt,lt);
7668     ff=mp_take_fraction(mp, ff,ff); /* $\beta_k^2/\alpha_k^2$ */
7669     ee=mp_take_fraction(mp, ee,ff);
7670   }
7671 }
7672 ff=mp_make_fraction(mp, ee,ee+dd)
7673
7674 @ The value of $u_{k-1}$ will be |<=1| except when $k=1$ and the previous
7675 equation was specified by a curl. In that case we must use a special
7676 method of computation to prevent overflow.
7677
7678 Fortunately, the calculations turn out to be even simpler in this ``hard''
7679 case. The curl equation makes $w_0=0$ and $v_0=-u_0\psi_1$, hence
7680 $-B_1\psi_1-A_1v_0=-(B_1-u_0A_1)\psi_1=-\\{cc}\cdot B_1\psi_1$.
7681
7682 @<Calculate the values of $v_k$ and $w_k$@>=
7683 acc=-mp_take_fraction(mp, mp->psi[k+1],mp->uu[k]);
7684 if ( right_type(r)==mp_curl ) { 
7685   mp->ww[k]=0;
7686   mp->vv[k]=acc-mp_take_fraction(mp, mp->psi[1],fraction_one-ff);
7687 } else { 
7688   ff=mp_make_fraction(mp, fraction_one-ff,cc); /* this is
7689     $B_k/(C_k+B_k-u_{k-1}A_k)<5$ */
7690   acc=acc-mp_take_fraction(mp, mp->psi[k],ff);
7691   ff=mp_take_fraction(mp, ff,aa); /* this is $A_k/(C_k+B_k-u_{k-1}A_k)$ */
7692   mp->vv[k]=acc-mp_take_fraction(mp, mp->vv[k-1],ff);
7693   if ( mp->ww[k-1]==0 ) mp->ww[k]=0;
7694   else mp->ww[k]=-mp_take_fraction(mp, mp->ww[k-1],ff);
7695 }
7696
7697 @ When a complete cycle has been traversed, we have $\theta_k+u_k\theta\k=
7698 v_k+w_k\theta_0$, for |1<=k<=n|. We would like to determine the value of
7699 $\theta_n$ and reduce the system to the form $\theta_k+u_k\theta\k=v_k$
7700 for |0<=k<n|, so that the cyclic case can be finished up just as if there
7701 were no cycle.
7702
7703 The idea in the following code is to observe that
7704 $$\eqalign{\theta_n&=v_n+w_n\theta_0-u_n\theta_1=\cdots\cr
7705 &=v_n+w_n\theta_0-u_n\bigl(v_1+w_1\theta_0-u_1(v_2+\cdots
7706   -u_{n-2}(v_{n-1}+w_{n-1}\theta_0-u_{n-1}\theta_0))\bigr),\cr}$$
7707 so we can solve for $\theta_n=\theta_0$.
7708
7709 @<Adjust $\theta_n$ to equal $\theta_0$ and |goto found|@>=
7710
7711 aa=0; bb=fraction_one; /* we have |k=n| */
7712 do {  decr(k);
7713 if ( k==0 ) k=n;
7714   aa=mp->vv[k]-mp_take_fraction(mp, aa,mp->uu[k]);
7715   bb=mp->ww[k]-mp_take_fraction(mp, bb,mp->uu[k]);
7716 } while (k!=n); /* now $\theta_n=\\{aa}+\\{bb}\cdot\theta_n$ */
7717 aa=mp_make_fraction(mp, aa,fraction_one-bb);
7718 mp->theta[n]=aa; mp->vv[0]=aa;
7719 for (k=1;k<=n-1;k++) {
7720   mp->vv[k]=mp->vv[k]+mp_take_fraction(mp, aa,mp->ww[k]);
7721 }
7722 goto FOUND;
7723 }
7724
7725 @ @d reduce_angle(A) if ( abs((A))>one_eighty_deg ) {
7726   if ( (A)>0 ) (A)=(A)-three_sixty_deg; else (A)=(A)+three_sixty_deg; }
7727
7728 @<Calculate the given value of $\theta_n$...@>=
7729
7730   mp->theta[n]=left_given(s)-mp_n_arg(mp, mp->delta_x[n-1],mp->delta_y[n-1]);
7731   reduce_angle(mp->theta[n]);
7732   goto FOUND;
7733 }
7734
7735 @ @<Set up the equation for a given value of $\theta_0$@>=
7736
7737   mp->vv[0]=right_given(s)-mp_n_arg(mp, mp->delta_x[0],mp->delta_y[0]);
7738   reduce_angle(mp->vv[0]);
7739   mp->uu[0]=0; mp->ww[0]=0;
7740 }
7741
7742 @ @<Set up the equation for a curl at $\theta_0$@>=
7743 { cc=right_curl(s); lt=abs(left_tension(t)); rt=abs(right_tension(s));
7744   if ( (rt==unity)&&(lt==unity) )
7745     mp->uu[0]=mp_make_fraction(mp, cc+cc+unity,cc+two);
7746   else 
7747     mp->uu[0]=mp_curl_ratio(mp, cc,rt,lt);
7748   mp->vv[0]=-mp_take_fraction(mp, mp->psi[1],mp->uu[0]); mp->ww[0]=0;
7749 }
7750
7751 @ @<Set up equation for a curl at $\theta_n$...@>=
7752 { cc=left_curl(s); lt=abs(left_tension(s)); rt=abs(right_tension(r));
7753   if ( (rt==unity)&&(lt==unity) )
7754     ff=mp_make_fraction(mp, cc+cc+unity,cc+two);
7755   else 
7756     ff=mp_curl_ratio(mp, cc,lt,rt);
7757   mp->theta[n]=-mp_make_fraction(mp, mp_take_fraction(mp, mp->vv[n-1],ff),
7758     fraction_one-mp_take_fraction(mp, ff,mp->uu[n-1]));
7759   goto FOUND;
7760 }
7761
7762 @ The |curl_ratio| subroutine has three arguments, which our previous notation
7763 encourages us to call $\gamma$, $\alpha^{-1}$, and $\beta^{-1}$. It is
7764 a somewhat tedious program to calculate
7765 $${(3-\alpha)\alpha^2\gamma+\beta^3\over
7766   \alpha^3\gamma+(3-\beta)\beta^2},$$
7767 with the result reduced to 4 if it exceeds 4. (This reduction of curl
7768 is necessary only if the curl and tension are both large.)
7769 The values of $\alpha$ and $\beta$ will be at most~4/3.
7770
7771 @<Declare subroutines needed by |solve_choices|@>=
7772 fraction mp_curl_ratio (MP mp,scaled gamma, scaled a_tension, 
7773                         scaled b_tension) {
7774   fraction alpha,beta,num,denom,ff; /* registers */
7775   alpha=mp_make_fraction(mp, unity,a_tension);
7776   beta=mp_make_fraction(mp, unity,b_tension);
7777   if ( alpha<=beta ) {
7778     ff=mp_make_fraction(mp, alpha,beta); ff=mp_take_fraction(mp, ff,ff);
7779     gamma=mp_take_fraction(mp, gamma,ff);
7780     beta=beta / 010000; /* convert |fraction| to |scaled| */
7781     denom=mp_take_fraction(mp, gamma,alpha)+three-beta;
7782     num=mp_take_fraction(mp, gamma,fraction_three-alpha)+beta;
7783   } else { 
7784     ff=mp_make_fraction(mp, beta,alpha); ff=mp_take_fraction(mp, ff,ff);
7785     beta=mp_take_fraction(mp, beta,ff) / 010000; /* convert |fraction| to |scaled| */
7786     denom=mp_take_fraction(mp, gamma,alpha)+(ff / 1365)-beta;
7787       /* $1365\approx 2^{12}/3$ */
7788     num=mp_take_fraction(mp, gamma,fraction_three-alpha)+beta;
7789   }
7790   if ( num>=denom+denom+denom+denom ) return fraction_four;
7791   else return mp_make_fraction(mp, num,denom);
7792 }
7793
7794 @ We're in the home stretch now.
7795
7796 @<Finish choosing angles and assigning control points@>=
7797 for (k=n-1;k>=0;k--) {
7798   mp->theta[k]=mp->vv[k]-mp_take_fraction(mp,mp->theta[k+1],mp->uu[k]);
7799 }
7800 s=p; k=0;
7801 do {  
7802   t=link(s);
7803   mp_n_sin_cos(mp, mp->theta[k]); mp->st=mp->n_sin; mp->ct=mp->n_cos;
7804   mp_n_sin_cos(mp, -mp->psi[k+1]-mp->theta[k+1]); mp->sf=mp->n_sin; mp->cf=mp->n_cos;
7805   mp_set_controls(mp, s,t,k);
7806   incr(k); s=t;
7807 } while (k!=n)
7808
7809 @ The |set_controls| routine actually puts the control points into
7810 a pair of consecutive nodes |p| and~|q|. Global variables are used to
7811 record the values of $\sin\theta$, $\cos\theta$, $\sin\phi$, and
7812 $\cos\phi$ needed in this calculation.
7813
7814 @<Glob...@>=
7815 fraction st;
7816 fraction ct;
7817 fraction sf;
7818 fraction cf; /* sines and cosines */
7819
7820 @ @<Declare subroutines needed by |solve_choices|@>=
7821 void mp_set_controls (MP mp,pointer p, pointer q, integer k) {
7822   fraction rr,ss; /* velocities, divided by thrice the tension */
7823   scaled lt,rt; /* tensions */
7824   fraction sine; /* $\sin(\theta+\phi)$ */
7825   lt=abs(left_tension(q)); rt=abs(right_tension(p));
7826   rr=mp_velocity(mp, mp->st,mp->ct,mp->sf,mp->cf,rt);
7827   ss=mp_velocity(mp, mp->sf,mp->cf,mp->st,mp->ct,lt);
7828   if ( (right_tension(p)<0)||(left_tension(q)<0) ) {
7829     @<Decrease the velocities,
7830       if necessary, to stay inside the bounding triangle@>;
7831   }
7832   right_x(p)=x_coord(p)+mp_take_fraction(mp, 
7833                           mp_take_fraction(mp, mp->delta_x[k],mp->ct)-
7834                           mp_take_fraction(mp, mp->delta_y[k],mp->st),rr);
7835   right_y(p)=y_coord(p)+mp_take_fraction(mp, 
7836                           mp_take_fraction(mp, mp->delta_y[k],mp->ct)+
7837                           mp_take_fraction(mp, mp->delta_x[k],mp->st),rr);
7838   left_x(q)=x_coord(q)-mp_take_fraction(mp, 
7839                          mp_take_fraction(mp, mp->delta_x[k],mp->cf)+
7840                          mp_take_fraction(mp, mp->delta_y[k],mp->sf),ss);
7841   left_y(q)=y_coord(q)-mp_take_fraction(mp, 
7842                          mp_take_fraction(mp, mp->delta_y[k],mp->cf)-
7843                          mp_take_fraction(mp, mp->delta_x[k],mp->sf),ss);
7844   right_type(p)=mp_explicit; left_type(q)=mp_explicit;
7845 }
7846
7847 @ The boundedness conditions $\\{rr}\L\sin\phi\,/\sin(\theta+\phi)$ and
7848 $\\{ss}\L\sin\theta\,/\sin(\theta+\phi)$ are to be enforced if $\sin\theta$,
7849 $\sin\phi$, and $\sin(\theta+\phi)$ all have the same sign. Otherwise
7850 there is no ``bounding triangle.''
7851
7852 @<Decrease the velocities, if necessary...@>=
7853 if (((mp->st>=0)&&(mp->sf>=0))||((mp->st<=0)&&(mp->sf<=0)) ) {
7854   sine=mp_take_fraction(mp, abs(mp->st),mp->cf)+
7855                             mp_take_fraction(mp, abs(mp->sf),mp->ct);
7856   if ( sine>0 ) {
7857     sine=mp_take_fraction(mp, sine,fraction_one+unity); /* safety factor */
7858     if ( right_tension(p)<0 )
7859      if ( mp_ab_vs_cd(mp, abs(mp->sf),fraction_one,rr,sine)<0 )
7860       rr=mp_make_fraction(mp, abs(mp->sf),sine);
7861     if ( left_tension(q)<0 )
7862      if ( mp_ab_vs_cd(mp, abs(mp->st),fraction_one,ss,sine)<0 )
7863       ss=mp_make_fraction(mp, abs(mp->st),sine);
7864   }
7865 }
7866
7867 @ Only the simple cases remain to be handled.
7868
7869 @<Reduce to simple case of two givens and |return|@>=
7870
7871   aa=mp_n_arg(mp, mp->delta_x[0],mp->delta_y[0]);
7872   mp_n_sin_cos(mp, right_given(p)-aa); mp->ct=mp->n_cos; mp->st=mp->n_sin;
7873   mp_n_sin_cos(mp, left_given(q)-aa); mp->cf=mp->n_cos; mp->sf=-mp->n_sin;
7874   mp_set_controls(mp, p,q,0); return;
7875 }
7876
7877 @ @<Reduce to simple case of straight line and |return|@>=
7878
7879   right_type(p)=mp_explicit; left_type(q)=mp_explicit;
7880   lt=abs(left_tension(q)); rt=abs(right_tension(p));
7881   if ( rt==unity ) {
7882     if ( mp->delta_x[0]>=0 ) right_x(p)=x_coord(p)+((mp->delta_x[0]+1) / 3);
7883     else right_x(p)=x_coord(p)+((mp->delta_x[0]-1) / 3);
7884     if ( mp->delta_y[0]>=0 ) right_y(p)=y_coord(p)+((mp->delta_y[0]+1) / 3);
7885     else right_y(p)=y_coord(p)+((mp->delta_y[0]-1) / 3);
7886   } else { 
7887     ff=mp_make_fraction(mp, unity,3*rt); /* $\alpha/3$ */
7888     right_x(p)=x_coord(p)+mp_take_fraction(mp, mp->delta_x[0],ff);
7889     right_y(p)=y_coord(p)+mp_take_fraction(mp, mp->delta_y[0],ff);
7890   }
7891   if ( lt==unity ) {
7892     if ( mp->delta_x[0]>=0 ) left_x(q)=x_coord(q)-((mp->delta_x[0]+1) / 3);
7893     else left_x(q)=x_coord(q)-((mp->delta_x[0]-1) / 3);
7894     if ( mp->delta_y[0]>=0 ) left_y(q)=y_coord(q)-((mp->delta_y[0]+1) / 3);
7895     else left_y(q)=y_coord(q)-((mp->delta_y[0]-1) / 3);
7896   } else  { 
7897     ff=mp_make_fraction(mp, unity,3*lt); /* $\beta/3$ */
7898     left_x(q)=x_coord(q)-mp_take_fraction(mp, mp->delta_x[0],ff);
7899     left_y(q)=y_coord(q)-mp_take_fraction(mp, mp->delta_y[0],ff);
7900   }
7901   return;
7902 }
7903
7904 @* \[19] Measuring paths.
7905 \MP's \&{llcorner}, \&{lrcorner}, \&{ulcorner}, and \&{urcorner} operators
7906 allow the user to measure the bounding box of anything that can go into a
7907 picture.  It's easy to get rough bounds on the $x$ and $y$ extent of a path
7908 by just finding the bounding box of the knots and the control points. We
7909 need a more accurate version of the bounding box, but we can still use the
7910 easy estimate to save time by focusing on the interesting parts of the path.
7911
7912 @ Computing an accurate bounding box involves a theme that will come up again
7913 and again. Given a Bernshte{\u\i}n polynomial
7914 @^Bernshte{\u\i}n, Serge{\u\i} Natanovich@>
7915 $$B(z_0,z_1,\ldots,z_n;t)=\sum_k{n\choose k}t^k(1-t)^{n-k}z_k,$$
7916 we can conveniently bisect its range as follows:
7917
7918 \smallskip
7919 \textindent{1)} Let $z_k^{(0)}=z_k$, for |0<=k<=n|.
7920
7921 \smallskip
7922 \textindent{2)} Let $z_k^{(j+1)}={1\over2}(z_k^{(j)}+z\k^{(j)})$, for
7923 |0<=k<n-j|, for |0<=j<n|.
7924
7925 \smallskip\noindent
7926 Then
7927 $$B(z_0,z_1,\ldots,z_n;t)=B(z_0^{(0)},z_0^{(1)},\ldots,z_0^{(n)};2t)
7928  =B(z_0^{(n)},z_1^{(n-1)},\ldots,z_n^{(0)};2t-1).$$
7929 This formula gives us the coefficients of polynomials to use over the ranges
7930 $0\L t\L{1\over2}$ and ${1\over2}\L t\L1$.
7931
7932 @ Now here's a subroutine that's handy for all sorts of path computations:
7933 Given a quadratic polynomial $B(a,b,c;t)$, the |crossing_point| function
7934 returns the unique |fraction| value |t| between 0 and~1 at which
7935 $B(a,b,c;t)$ changes from positive to negative, or returns
7936 |t=fraction_one+1| if no such value exists. If |a<0| (so that $B(a,b,c;t)$
7937 is already negative at |t=0|), |crossing_point| returns the value zero.
7938
7939 @d no_crossing {  return (fraction_one+1); }
7940 @d one_crossing { return fraction_one; }
7941 @d zero_crossing { return 0; }
7942 @d mp_crossing_point(M,A,B,C) mp_do_crossing_point(A,B,C)
7943
7944 @c fraction mp_do_crossing_point (integer a, integer b, integer c) {
7945   integer d; /* recursive counter */
7946   integer x,xx,x0,x1,x2; /* temporary registers for bisection */
7947   if ( a<0 ) zero_crossing;
7948   if ( c>=0 ) { 
7949     if ( b>=0 ) {
7950       if ( c>0 ) { no_crossing; }
7951       else if ( (a==0)&&(b==0) ) { no_crossing;} 
7952       else { one_crossing; } 
7953     }
7954     if ( a==0 ) zero_crossing;
7955   } else if ( a==0 ) {
7956     if ( b<=0 ) zero_crossing;
7957   }
7958   @<Use bisection to find the crossing point, if one exists@>;
7959 }
7960
7961 @ The general bisection method is quite simple when $n=2$, hence
7962 |crossing_point| does not take much time. At each stage in the
7963 recursion we have a subinterval defined by |l| and~|j| such that
7964 $B(a,b,c;2^{-l}(j+t))=B(x_0,x_1,x_2;t)$, and we want to ``zero in'' on
7965 the subinterval where $x_0\G0$ and $\min(x_1,x_2)<0$.
7966
7967 It is convenient for purposes of calculation to combine the values
7968 of |l| and~|j| in a single variable $d=2^l+j$, because the operation
7969 of bisection then corresponds simply to doubling $d$ and possibly
7970 adding~1. Furthermore it proves to be convenient to modify
7971 our previous conventions for bisection slightly, maintaining the
7972 variables $X_0=2^lx_0$, $X_1=2^l(x_0-x_1)$, and $X_2=2^l(x_1-x_2)$.
7973 With these variables the conditions $x_0\ge0$ and $\min(x_1,x_2)<0$ are
7974 equivalent to $\max(X_1,X_1+X_2)>X_0\ge0$.
7975
7976 The following code maintains the invariant relations
7977 $0\L|x0|<\max(|x1|,|x1|+|x2|)$,
7978 $\vert|x1|\vert<2^{30}$, $\vert|x2|\vert<2^{30}$;
7979 it has been constructed in such a way that no arithmetic overflow
7980 will occur if the inputs satisfy
7981 $a<2^{30}$, $\vert a-b\vert<2^{30}$, and $\vert b-c\vert<2^{30}$.
7982
7983 @<Use bisection to find the crossing point...@>=
7984 d=1; x0=a; x1=a-b; x2=b-c;
7985 do {  
7986   x=half(x1+x2);
7987   if ( x1-x0>x0 ) { 
7988     x2=x; x0+=x0; d+=d;  
7989   } else { 
7990     xx=x1+x-x0;
7991     if ( xx>x0 ) { 
7992       x2=x; x0+=x0; d+=d;
7993     }  else { 
7994       x0=x0-xx;
7995       if ( x<=x0 ) { if ( x+x2<=x0 ) no_crossing; }
7996       x1=x; d=d+d+1;
7997     }
7998   }
7999 } while (d<fraction_one);
8000 return (d-fraction_one)
8001
8002 @ Here is a routine that computes the $x$ or $y$ coordinate of the point on
8003 a cubic corresponding to the |fraction| value~|t|.
8004
8005 It is convenient to define a \.{WEB} macro |t_of_the_way| such that
8006 |t_of_the_way(a,b)| expands to |a-(a-b)*t|, i.e., to |t[a,b]|.
8007
8008 @d t_of_the_way(A,B) ((A)-mp_take_fraction(mp,((A)-(B)),t))
8009
8010 @c scaled mp_eval_cubic (MP mp,pointer p, pointer q, fraction t) {
8011   scaled x1,x2,x3; /* intermediate values */
8012   x1=t_of_the_way(knot_coord(p),right_coord(p));
8013   x2=t_of_the_way(right_coord(p),left_coord(q));
8014   x3=t_of_the_way(left_coord(q),knot_coord(q));
8015   x1=t_of_the_way(x1,x2);
8016   x2=t_of_the_way(x2,x3);
8017   return t_of_the_way(x1,x2);
8018 }
8019
8020 @ The actual bounding box information is stored in global variables.
8021 Since it is convenient to address the $x$ and $y$ information
8022 separately, we define arrays indexed by |x_code..y_code| and use
8023 macros to give them more convenient names.
8024
8025 @<Types...@>=
8026 enum mp_bb_code  {
8027   mp_x_code=0, /* index for |minx| and |maxx| */
8028   mp_y_code /* index for |miny| and |maxy| */
8029 } ;
8030
8031
8032 @d minx mp->bbmin[mp_x_code]
8033 @d maxx mp->bbmax[mp_x_code]
8034 @d miny mp->bbmin[mp_y_code]
8035 @d maxy mp->bbmax[mp_y_code]
8036
8037 @<Glob...@>=
8038 scaled bbmin[mp_y_code+1];
8039 scaled bbmax[mp_y_code+1]; 
8040 /* the result of procedures that compute bounding box information */
8041
8042 @ Now we're ready for the key part of the bounding box computation.
8043 The |bound_cubic| procedure updates |bbmin[c]| and |bbmax[c]| based on
8044 $$B(\hbox{|knot_coord(p)|}, \hbox{|right_coord(p)|},
8045     \hbox{|left_coord(q)|}, \hbox{|knot_coord(q)|};t)
8046 $$
8047 for $0<t\le1$.  In other words, the procedure adjusts the bounds to
8048 accommodate |knot_coord(q)| and any extremes over the range $0<t<1$.
8049 The |c| parameter is |x_code| or |y_code|.
8050
8051 @c void mp_bound_cubic (MP mp,pointer p, pointer q, small_number c) {
8052   boolean wavy; /* whether we need to look for extremes */
8053   scaled del1,del2,del3,del,dmax; /* proportional to the control
8054      points of a quadratic derived from a cubic */
8055   fraction t,tt; /* where a quadratic crosses zero */
8056   scaled x; /* a value that |bbmin[c]| and |bbmax[c]| must accommodate */
8057   x=knot_coord(q);
8058   @<Adjust |bbmin[c]| and |bbmax[c]| to accommodate |x|@>;
8059   @<Check the control points against the bounding box and set |wavy:=true|
8060     if any of them lie outside@>;
8061   if ( wavy ) {
8062     del1=right_coord(p)-knot_coord(p);
8063     del2=left_coord(q)-right_coord(p);
8064     del3=knot_coord(q)-left_coord(q);
8065     @<Scale up |del1|, |del2|, and |del3| for greater accuracy;
8066       also set |del| to the first nonzero element of |(del1,del2,del3)|@>;
8067     if ( del<0 ) {
8068       negate(del1); negate(del2); negate(del3);
8069     };
8070     t=mp_crossing_point(mp, del1,del2,del3);
8071     if ( t<fraction_one ) {
8072       @<Test the extremes of the cubic against the bounding box@>;
8073     }
8074   }
8075 }
8076
8077 @ @<Adjust |bbmin[c]| and |bbmax[c]| to accommodate |x|@>=
8078 if ( x<mp->bbmin[c] ) mp->bbmin[c]=x;
8079 if ( x>mp->bbmax[c] ) mp->bbmax[c]=x
8080
8081 @ @<Check the control points against the bounding box and set...@>=
8082 wavy=true;
8083 if ( mp->bbmin[c]<=right_coord(p) )
8084   if ( right_coord(p)<=mp->bbmax[c] )
8085     if ( mp->bbmin[c]<=left_coord(q) )
8086       if ( left_coord(q)<=mp->bbmax[c] )
8087         wavy=false
8088
8089 @ If |del1=del2=del3=0|, it's impossible to obey the title of this
8090 section. We just set |del=0| in that case.
8091
8092 @<Scale up |del1|, |del2|, and |del3| for greater accuracy...@>=
8093 if ( del1!=0 ) del=del1;
8094 else if ( del2!=0 ) del=del2;
8095 else del=del3;
8096 if ( del!=0 ) {
8097   dmax=abs(del1);
8098   if ( abs(del2)>dmax ) dmax=abs(del2);
8099   if ( abs(del3)>dmax ) dmax=abs(del3);
8100   while ( dmax<fraction_half ) {
8101     dmax+=dmax; del1+=del1; del2+=del2; del3+=del3;
8102   }
8103 }
8104
8105 @ Since |crossing_point| has tried to choose |t| so that
8106 $B(|del1|,|del2|,|del3|;\tau)$ crosses zero at $\tau=|t|$ with negative
8107 slope, the value of |del2| computed below should not be positive.
8108 But rounding error could make it slightly positive in which case we
8109 must cut it to zero to avoid confusion.
8110
8111 @<Test the extremes of the cubic against the bounding box@>=
8112
8113   x=mp_eval_cubic(mp, p,q,t);
8114   @<Adjust |bbmin[c]| and |bbmax[c]| to accommodate |x|@>;
8115   del2=t_of_the_way(del2,del3);
8116     /* now |0,del2,del3| represent the derivative on the remaining interval */
8117   if ( del2>0 ) del2=0;
8118   tt=mp_crossing_point(mp, 0,-del2,-del3);
8119   if ( tt<fraction_one ) {
8120     @<Test the second extreme against the bounding box@>;
8121   }
8122 }
8123
8124 @ @<Test the second extreme against the bounding box@>=
8125 {
8126    x=mp_eval_cubic(mp, p,q,t_of_the_way(tt,fraction_one));
8127   @<Adjust |bbmin[c]| and |bbmax[c]| to accommodate |x|@>;
8128 }
8129
8130 @ Finding the bounding box of a path is basically a matter of applying
8131 |bound_cubic| twice for each pair of adjacent knots.
8132
8133 @c void mp_path_bbox (MP mp,pointer h) {
8134   pointer p,q; /* a pair of adjacent knots */
8135    minx=x_coord(h); miny=y_coord(h);
8136   maxx=minx; maxy=miny;
8137   p=h;
8138   do {  
8139     if ( right_type(p)==mp_endpoint ) return;
8140     q=link(p);
8141     mp_bound_cubic(mp, x_loc(p),x_loc(q),mp_x_code);
8142     mp_bound_cubic(mp, y_loc(p),y_loc(q),mp_y_code);
8143     p=q;
8144   } while (p!=h);
8145 }
8146
8147 @ Another important way to measure a path is to find its arc length.  This
8148 is best done by using the general bisection algorithm to subdivide the path
8149 until obtaining ``well behaved'' subpaths whose arc lengths can be approximated
8150 by simple means.
8151
8152 Since the arc length is the integral with respect to time of the magnitude of
8153 the velocity, it is natural to use Simpson's rule for the approximation.
8154 @^Simpson's rule@>
8155 If $\dot B(t)$ is the spline velocity, Simpson's rule gives
8156 $$ \vb\dot B(0)\vb + 4\vb\dot B({1\over2})\vb + \vb\dot B(1)\vb \over 6 $$
8157 for the arc length of a path of length~1.  For a cubic spline
8158 $B(z_0,z_1,z_2,z_3;t)$, the time derivative $\dot B(t)$ is
8159 $3B(dz_0,dz_1,dz_2;t)$, where $dz_i=z_{i+1}-z_i$.  Hence the arc length
8160 approximation is
8161 $$ {\vb dz_0\vb \over 2} + 2\vb dz_{02}\vb + {\vb dz_2\vb \over 2}, $$
8162 where
8163 $$ dz_{02}={1\over2}\left({dz_0+dz_1\over 2}+{dz_1+dz_2\over 2}\right)$$
8164 is the result of the bisection algorithm.
8165
8166 @ The remaining problem is how to decide when a subpath is ``well behaved.''
8167 This could be done via the theoretical error bound for Simpson's rule,
8168 @^Simpson's rule@>
8169 but this is impractical because it requires an estimate of the fourth
8170 derivative of the quantity being integrated.  It is much easier to just perform
8171 a bisection step and see how much the arc length estimate changes.  Since the
8172 error for Simpson's rule is proportional to the fourth power of the sample
8173 spacing, the remaining error is typically about $1\over16$ of the amount of
8174 the change.  We say ``typically'' because the error has a pseudo-random behavior
8175 that could cause the two estimates to agree when each contain large errors.
8176
8177 To protect against disasters such as undetected cusps, the bisection process
8178 should always continue until all the $dz_i$ vectors belong to a single
8179 $90^\circ$ sector.  This ensures that no point on the spline can have velocity
8180 less than 70\% of the minimum of $\vb dz_0\vb$, $\vb dz_1\vb$ and $\vb dz_2\vb$.
8181 If such a spline happens to produce an erroneous arc length estimate that
8182 is little changed by bisection, the amount of the error is likely to be fairly
8183 small.  We will try to arrange things so that freak accidents of this type do
8184 not destroy the inverse relationship between the \&{arclength} and
8185 \&{arctime} operations.
8186 @:arclength_}{\&{arclength} primitive@>
8187 @:arctime_}{\&{arctime} primitive@>
8188
8189 @ The \&{arclength} and \&{arctime} operations are both based on a recursive
8190 @^recursion@>
8191 function that finds the arc length of a cubic spline given $dz_0$, $dz_1$,
8192 $dz_2$. This |arc_test| routine also takes an arc length goal |a_goal| and
8193 returns the time when the arc length reaches |a_goal| if there is such a time.
8194 Thus the return value is either an arc length less than |a_goal| or, if the
8195 arc length would be at least |a_goal|, it returns a time value decreased by
8196 |two|.  This allows the caller to use the sign of the result to distinguish
8197 between arc lengths and time values.  On certain types of overflow, it is
8198 possible for |a_goal| and the result of |arc_test| both to be |el_gordo|.
8199 Otherwise, the result is always less than |a_goal|.
8200
8201 Rather than halving the control point coordinates on each recursive call to
8202 |arc_test|, it is better to keep them proportional to velocity on the original
8203 curve and halve the results instead.  This means that recursive calls can
8204 potentially use larger error tolerances in their arc length estimates.  How
8205 much larger depends on to what extent the errors behave as though they are
8206 independent of each other.  To save computing time, we use optimistic assumptions
8207 and increase the tolerance by a factor of about $\sqrt2$ for each recursive
8208 call.
8209
8210 In addition to the tolerance parameter, |arc_test| should also have parameters
8211 for ${1\over3}\vb\dot B(0)\vb$, ${2\over3}\vb\dot B({1\over2})\vb$, and
8212 ${1\over3}\vb\dot B(1)\vb$.  These quantities are relatively expensive to compute
8213 and they are needed in different instances of |arc_test|.
8214
8215 @c @<Declare subroutines needed by |arc_test|@>
8216 scaled mp_arc_test (MP mp, scaled dx0, scaled dy0, scaled dx1, scaled dy1, 
8217                     scaled dx2, scaled dy2, scaled  v0, scaled v02, 
8218                     scaled v2, scaled a_goal, scaled tol) {
8219   boolean simple; /* are the control points confined to a $90^\circ$ sector? */
8220   scaled dx01, dy01, dx12, dy12, dx02, dy02;  /* bisection results */
8221   scaled v002, v022;
8222     /* twice the velocity magnitudes at $t={1\over4}$ and $t={3\over4}$ */
8223   scaled arc; /* best arc length estimate before recursion */
8224   @<Other local variables in |arc_test|@>;
8225   @<Bisect the B\'ezier quadratic given by |dx0|, |dy0|, |dx1|, |dy1|,
8226     |dx2|, |dy2|@>;
8227   @<Initialize |v002|, |v022|, and the arc length estimate |arc|; if it overflows
8228     set |arc_test| and |return|@>;
8229   @<Test if the control points are confined to one quadrant or rotating them
8230     $45^\circ$ would put them in one quadrant.  Then set |simple| appropriately@>;
8231   if ( simple && (abs(arc-v02-halfp(v0+v2)) <= tol) ) {
8232     if ( arc < a_goal ) {
8233       return arc;
8234     } else {
8235        @<Estimate when the arc length reaches |a_goal| and set |arc_test| to
8236          that time minus |two|@>;
8237     }
8238   } else {
8239     @<Use one or two recursive calls to compute the |arc_test| function@>;
8240   }
8241 }
8242
8243 @ The |tol| value should by multiplied by $\sqrt 2$ before making recursive
8244 calls, but $1.5$ is an adequate approximation.  It is best to avoid using
8245 |make_fraction| in this inner loop.
8246 @^inner loop@>
8247
8248 @<Use one or two recursive calls to compute the |arc_test| function@>=
8249
8250   @<Set |a_new| and |a_aux| so their sum is |2*a_goal| and |a_new| is as
8251     large as possible@>;
8252   tol = tol + halfp(tol);
8253   a = mp_arc_test(mp, dx0,dy0, dx01,dy01, dx02,dy02, v0, v002, 
8254                   halfp(v02), a_new, tol);
8255   if ( a<0 )  {
8256      return (-halfp(two-a));
8257   } else { 
8258     @<Update |a_new| to reduce |a_new+a_aux| by |a|@>;
8259     b = mp_arc_test(mp, dx02,dy02, dx12,dy12, dx2,dy2,
8260                     halfp(v02), v022, v2, a_new, tol);
8261     if ( b<0 )  
8262       return (-halfp(-b) - half_unit);
8263     else  
8264       return (a + half(b-a));
8265   }
8266 }
8267
8268 @ @<Other local variables in |arc_test|@>=
8269 scaled a,b; /* results of recursive calls */
8270 scaled a_new,a_aux; /* the sum of these gives the |a_goal| */
8271
8272 @ @<Set |a_new| and |a_aux| so their sum is |2*a_goal| and |a_new| is...@>=
8273 a_aux = el_gordo - a_goal;
8274 if ( a_goal > a_aux ) {
8275   a_aux = a_goal - a_aux;
8276   a_new = el_gordo;
8277 } else { 
8278   a_new = a_goal + a_goal;
8279   a_aux = 0;
8280 }
8281
8282 @ There is no need to maintain |a_aux| at this point so we use it as a temporary
8283 to force the additions and subtractions to be done in an order that avoids
8284 overflow.
8285
8286 @<Update |a_new| to reduce |a_new+a_aux| by |a|@>=
8287 if ( a > a_aux ) {
8288   a_aux = a_aux - a;
8289   a_new = a_new + a_aux;
8290 }
8291
8292 @ This code assumes all {\it dx} and {\it dy} variables have magnitude less than
8293 |fraction_four|.  To simplify the rest of the |arc_test| routine, we strengthen
8294 this assumption by requiring the norm of each $({\it dx},{\it dy})$ pair to obey
8295 this bound.  Note that recursive calls will maintain this invariant.
8296
8297 @<Bisect the B\'ezier quadratic given by |dx0|, |dy0|, |dx1|, |dy1|,...@>=
8298 dx01 = half(dx0 + dx1);
8299 dx12 = half(dx1 + dx2);
8300 dx02 = half(dx01 + dx12);
8301 dy01 = half(dy0 + dy1);
8302 dy12 = half(dy1 + dy2);
8303 dy02 = half(dy01 + dy12)
8304
8305 @ We should be careful to keep |arc<el_gordo| so that calling |arc_test| with
8306 |a_goal=el_gordo| is guaranteed to yield the arc length.
8307
8308 @<Initialize |v002|, |v022|, and the arc length estimate |arc|;...@>=
8309 v002 = mp_pyth_add(mp, dx01+half(dx0+dx02), dy01+half(dy0+dy02));
8310 v022 = mp_pyth_add(mp, dx12+half(dx02+dx2), dy12+half(dy02+dy2));
8311 tmp = halfp(v02+2);
8312 arc1 = v002 + half(halfp(v0+tmp) - v002);
8313 arc = v022 + half(halfp(v2+tmp) - v022);
8314 if ( (arc < el_gordo-arc1) )  {
8315   arc = arc+arc1;
8316 } else { 
8317   mp->arith_error = true;
8318   if ( a_goal==el_gordo )  return (el_gordo);
8319   else return (-two);
8320 }
8321
8322 @ @<Other local variables in |arc_test|@>=
8323 scaled tmp, tmp2; /* all purpose temporary registers */
8324 scaled arc1; /* arc length estimate for the first half */
8325
8326 @ @<Test if the control points are confined to one quadrant or rotating...@>=
8327 simple = ((dx0>=0) && (dx1>=0) && (dx2>=0)) ||
8328          ((dx0<=0) && (dx1<=0) && (dx2<=0));
8329 if ( simple )
8330   simple = ((dy0>=0) && (dy1>=0) && (dy2>=0)) ||
8331            ((dy0<=0) && (dy1<=0) && (dy2<=0));
8332 if ( ! simple ) {
8333   simple = ((dx0>=dy0) && (dx1>=dy1) && (dx2>=dy2)) ||
8334            ((dx0<=dy0) && (dx1<=dy1) && (dx2<=dy2));
8335   if ( simple ) 
8336     simple = ((-dx0>=dy0) && (-dx1>=dy1) && (-dx2>=dy2)) ||
8337              ((-dx0<=dy0) && (-dx1<=dy1) && (-dx2<=dy2));
8338 }
8339
8340 @ Since Simpson's rule is based on approximating the integrand by a parabola,
8341 @^Simpson's rule@>
8342 it is appropriate to use the same approximation to decide when the integral
8343 reaches the intermediate value |a_goal|.  At this point
8344 $$\eqalign{
8345     {\vb\dot B(0)\vb\over 3} &= \hbox{|v0|}, \qquad
8346     {\vb\dot B({1\over4})\vb\over 3} = {\hbox{|v002|}\over 2}, \qquad
8347     {\vb\dot B({1\over2})\vb\over 3} = {\hbox{|v02|}\over 2}, \cr
8348     {\vb\dot B({3\over4})\vb\over 3} &= {\hbox{|v022|}\over 2}, \qquad
8349     {\vb\dot B(1)\vb\over 3} = \hbox{|v2|} \cr
8350 }
8351 $$
8352 and
8353 $$ {\vb\dot B(t)\vb\over 3} \approx
8354   \cases{B\left(\hbox{|v0|},
8355       \hbox{|v002|}-{1\over 2}\hbox{|v0|}-{1\over 4}\hbox{|v02|},
8356       {1\over 2}\hbox{|v02|}; 2t \right)&
8357     if $t\le{1\over 2}$\cr
8358   B\left({1\over 2}\hbox{|v02|},
8359       \hbox{|v022|}-{1\over 4}\hbox{|v02|}-{1\over 2}\hbox{|v2|},
8360       \hbox{|v2|}; 2t-1 \right)&
8361     if $t\ge{1\over 2}$.\cr}
8362  \eqno (*)
8363 $$
8364 We can integrate $\vb\dot B(t)\vb$ by using
8365 $$\int 3B(a,b,c;\tau)\,dt =
8366   {B(0,a,a+b,a+b+c;\tau) + {\rm constant} \over {d\tau\over dt}}.
8367 $$
8368
8369 This construction allows us to find the time when the arc length reaches
8370 |a_goal| by solving a cubic equation of the form
8371 $$ B(0,a,a+b,a+b+c;\tau) = x, $$
8372 where $\tau$ is $2t$ or $2t+1$, $x$ is |a_goal| or |a_goal-arc1|, and $a$, $b$,
8373 and $c$ are the Bernshte{\u\i}n coefficients from $(*)$ divided by
8374 @^Bernshte{\u\i}n, Serge{\u\i} Natanovich@>
8375 $d\tau\over dt$.  We shall define a function |solve_rising_cubic| that finds
8376 $\tau$ given $a$, $b$, $c$, and $x$.
8377
8378 @<Estimate when the arc length reaches |a_goal| and set |arc_test| to...@>=
8379
8380   tmp = (v02 + 2) / 4;
8381   if ( a_goal<=arc1 ) {
8382     tmp2 = halfp(v0);
8383     return 
8384       (halfp(mp_solve_rising_cubic(mp, tmp2, arc1-tmp2-tmp, tmp, a_goal))- two);
8385   } else { 
8386     tmp2 = halfp(v2);
8387     return ((half_unit - two) +
8388       halfp(mp_solve_rising_cubic(mp, tmp, arc-arc1-tmp-tmp2, tmp2, a_goal-arc1)));
8389   }
8390 }
8391
8392 @ Here is the |solve_rising_cubic| routine that finds the time~$t$ when
8393 $$ B(0, a, a+b, a+b+c; t) = x. $$
8394 This routine is based on |crossing_point| but is simplified by the
8395 assumptions that $B(a,b,c;t)\ge0$ for $0\le t\le1$ and that |0<=x<=a+b+c|.
8396 If rounding error causes this condition to be violated slightly, we just ignore
8397 it and proceed with binary search.  This finds a time when the function value
8398 reaches |x| and the slope is positive.
8399
8400 @<Declare subroutines needed by |arc_test|@>=
8401 scaled mp_solve_rising_cubic (MP mp,scaled a, scaled b,  scaled c, scaled x) {
8402   scaled ab, bc, ac; /* bisection results */
8403   integer t; /* $2^k+q$ where unscaled answer is in $[q2^{-k},(q+1)2^{-k})$ */
8404   integer xx; /* temporary for updating |x| */
8405   if ( (a<0) || (c<0) ) mp_confusion(mp, "rising?");
8406 @:this can't happen rising?}{\quad rising?@>
8407   if ( x<=0 ) {
8408         return 0;
8409   } else if ( x >= a+b+c ) {
8410     return unity;
8411   } else { 
8412     t = 1;
8413     @<Rescale if necessary to make sure |a|, |b|, and |c| are all less than
8414       |el_gordo div 3|@>;
8415     do {  
8416       t+=t;
8417       @<Subdivide the B\'ezier quadratic defined by |a|, |b|, |c|@>;
8418       xx = x - a - ab - ac;
8419       if ( xx < -x ) { x+=x; b=ab; c=ac;  }
8420       else { x = x + xx;  a=ac; b=bc; t = t+1; };
8421     } while (t < unity);
8422     return (t - unity);
8423   }
8424 }
8425
8426 @ @<Subdivide the B\'ezier quadratic defined by |a|, |b|, |c|@>=
8427 ab = half(a+b);
8428 bc = half(b+c);
8429 ac = half(ab+bc)
8430
8431 @ @d one_third_el_gordo 05252525252 /* upper bound on |a|, |b|, and |c| */
8432
8433 @<Rescale if necessary to make sure |a|, |b|, and |c| are all less than...@>=
8434 while ((a>one_third_el_gordo)||(b>one_third_el_gordo)||(c>one_third_el_gordo)) { 
8435   a = halfp(a);
8436   b = half(b);
8437   c = halfp(c);
8438   x = halfp(x);
8439 }
8440
8441 @ It is convenient to have a simpler interface to |arc_test| that requires no
8442 unnecessary arguments and ensures that each $({\it dx},{\it dy})$ pair has
8443 length less than |fraction_four|.
8444
8445 @d arc_tol   16  /* quit when change in arc length estimate reaches this */
8446
8447 @c scaled mp_do_arc_test (MP mp,scaled dx0, scaled dy0, scaled dx1, 
8448                           scaled dy1, scaled dx2, scaled dy2, scaled a_goal) {
8449   scaled v0,v1,v2; /* length of each $({\it dx},{\it dy})$ pair */
8450   scaled v02; /* twice the norm of the quadratic at $t={1\over2}$ */
8451   v0 = mp_pyth_add(mp, dx0,dy0);
8452   v1 = mp_pyth_add(mp, dx1,dy1);
8453   v2 = mp_pyth_add(mp, dx2,dy2);
8454   if ( (v0>=fraction_four) || (v1>=fraction_four) || (v2>=fraction_four) ) { 
8455     mp->arith_error = true;
8456     if ( a_goal==el_gordo )  return el_gordo;
8457     else return (-two);
8458   } else { 
8459     v02 = mp_pyth_add(mp, dx1+half(dx0+dx2), dy1+half(dy0+dy2));
8460     return (mp_arc_test(mp, dx0,dy0, dx1,dy1, dx2,dy2,
8461                                  v0, v02, v2, a_goal, arc_tol));
8462   }
8463 }
8464
8465 @ Now it is easy to find the arc length of an entire path.
8466
8467 @c scaled mp_get_arc_length (MP mp,pointer h) {
8468   pointer p,q; /* for traversing the path */
8469   scaled a,a_tot; /* current and total arc lengths */
8470   a_tot = 0;
8471   p = h;
8472   while ( right_type(p)!=mp_endpoint ){ 
8473     q = link(p);
8474     a = mp_do_arc_test(mp, right_x(p)-x_coord(p), right_y(p)-y_coord(p),
8475       left_x(q)-right_x(p), left_y(q)-right_y(p),
8476       x_coord(q)-left_x(q), y_coord(q)-left_y(q), el_gordo);
8477     a_tot = mp_slow_add(mp, a, a_tot);
8478     if ( q==h ) break;  else p=q;
8479   }
8480   check_arith;
8481   return a_tot;
8482 }
8483
8484 @ The inverse operation of finding the time on a path~|h| when the arc length
8485 reaches some value |arc0| can also be accomplished via |do_arc_test|.  Some care
8486 is required to handle very large times or negative times on cyclic paths.  For
8487 non-cyclic paths, |arc0| values that are negative or too large cause
8488 |get_arc_time| to return 0 or the length of path~|h|.
8489
8490 If |arc0| is greater than the arc length of a cyclic path~|h|, the result is a
8491 time value greater than the length of the path.  Since it could be much greater,
8492 we must be prepared to compute the arc length of path~|h| and divide this into
8493 |arc0| to find how many multiples of the length of path~|h| to add.
8494
8495 @c scaled mp_get_arc_time (MP mp,pointer h, scaled  arc0) {
8496   pointer p,q; /* for traversing the path */
8497   scaled t_tot; /* accumulator for the result */
8498   scaled t; /* the result of |do_arc_test| */
8499   scaled arc; /* portion of |arc0| not used up so far */
8500   integer n; /* number of extra times to go around the cycle */
8501   if ( arc0<0 ) {
8502     @<Deal with a negative |arc0| value and |return|@>;
8503   }
8504   if ( arc0==el_gordo ) decr(arc0);
8505   t_tot = 0;
8506   arc = arc0;
8507   p = h;
8508   while ( (right_type(p)!=mp_endpoint) && (arc>0) ) {
8509     q = link(p);
8510     t = mp_do_arc_test(mp, right_x(p)-x_coord(p), right_y(p)-y_coord(p),
8511       left_x(q)-right_x(p), left_y(q)-right_y(p),
8512       x_coord(q)-left_x(q), y_coord(q)-left_y(q), arc);
8513     @<Update |arc| and |t_tot| after |do_arc_test| has just returned |t|@>;
8514     if ( q==h ) {
8515       @<Update |t_tot| and |arc| to avoid going around the cyclic
8516         path too many times but set |arith_error:=true| and |goto done| on
8517         overflow@>;
8518     }
8519     p = q;
8520   }
8521   check_arith;
8522   return t_tot;
8523 }
8524
8525 @ @<Update |arc| and |t_tot| after |do_arc_test| has just returned |t|@>=
8526 if ( t<0 ) { t_tot = t_tot + t + two;  arc = 0;  }
8527 else { t_tot = t_tot + unity;  arc = arc - t;  }
8528
8529 @ @<Deal with a negative |arc0| value and |return|@>=
8530
8531   if ( left_type(h)==mp_endpoint ) {
8532     t_tot=0;
8533   } else { 
8534     p = mp_htap_ypoc(mp, h);
8535     t_tot = -mp_get_arc_time(mp, p, -arc0);
8536     mp_toss_knot_list(mp, p);
8537   }
8538   check_arith;
8539   return t_tot;
8540 }
8541
8542 @ @<Update |t_tot| and |arc| to avoid going around the cyclic...@>=
8543 if ( arc>0 ) { 
8544   n = arc / (arc0 - arc);
8545   arc = arc - n*(arc0 - arc);
8546   if ( t_tot > (el_gordo / (n+1)) ) { 
8547         return el_gordo;
8548   }
8549   t_tot = (n + 1)*t_tot;
8550 }
8551
8552 @* \[20] Data structures for pens.
8553 A Pen in \MP\ can be either elliptical or polygonal.  Elliptical pens result
8554 in \ps\ \&{stroke} commands, while anything drawn with a polygonal pen is
8555 @:stroke}{\&{stroke} command@>
8556 converted into an area fill as described in the next part of this program.
8557 The mathematics behind this process is based on simple aspects of the theory
8558 of tracings developed by Leo Guibas, Lyle Ramshaw, and Jorge Stolfi
8559 [``A kinematic framework for computational geometry,'' Proc.\ IEEE Symp.\
8560 Foundations of Computer Science {\bf 24} (1983), 100--111].
8561
8562 Polygonal pens are created from paths via \MP's \&{makepen} primitive.
8563 @:makepen_}{\&{makepen} primitive@>
8564 This path representation is almost sufficient for our purposes except that
8565 a pen path should always be a convex polygon with the vertices in
8566 counter-clockwise order.
8567 Since we will need to scan pen polygons both forward and backward, a pen
8568 should be represented as a doubly linked ring of knot nodes.  There is
8569 room for the extra back pointer because we do not need the
8570 |left_type| or |right_type| fields.  In fact, we don't need the |left_x|,
8571 |left_y|, |right_x|, or |right_y| fields either but we leave these alone
8572 so that certain procedures can operate on both pens and paths.  In particular,
8573 pens can be copied using |copy_path| and recycled using |toss_knot_list|.
8574
8575 @d knil info
8576   /* this replaces the |left_type| and |right_type| fields in a pen knot */
8577
8578 @ The |make_pen| procedure turns a path into a pen by initializing
8579 the |knil| pointers and making sure the knots form a convex polygon.
8580 Thus each cubic in the given path becomes a straight line and the control
8581 points are ignored.  If the path is not cyclic, the ends are connected by a
8582 straight line.
8583
8584 @d copy_pen(A) mp_make_pen(mp, mp_copy_path(mp, (A)),false)
8585
8586 @c @<Declare a function called |convex_hull|@>
8587 pointer mp_make_pen (MP mp,pointer h, boolean need_hull) {
8588   pointer p,q; /* two consecutive knots */
8589   q=h;
8590   do {  
8591     p=q; q=link(q);
8592     knil(q)=p;
8593   } while (q!=h);
8594   if ( need_hull ){ 
8595     h=mp_convex_hull(mp, h);
8596     @<Make sure |h| isn't confused with an elliptical pen@>;
8597   }
8598   return h;
8599 }
8600
8601 @ The only information required about an elliptical pen is the overall
8602 transformation that has been applied to the original \&{pencircle}.
8603 @:pencircle_}{\&{pencircle} primitive@>
8604 Since it suffices to keep track of how the three points $(0,0)$, $(1,0)$,
8605 and $(0,1)$ are transformed, an elliptical pen can be stored in a single
8606 knot node and transformed as if it were a path.
8607
8608 @d pen_is_elliptical(A) ((A)==link((A)))
8609
8610 @c pointer mp_get_pen_circle (MP mp,scaled diam) {
8611   pointer h; /* the knot node to return */
8612   h=mp_get_node(mp, knot_node_size);
8613   link(h)=h; knil(h)=h;
8614   originator(h)=mp_program_code;
8615   x_coord(h)=0; y_coord(h)=0;
8616   left_x(h)=diam; left_y(h)=0;
8617   right_x(h)=0; right_y(h)=diam;
8618   return h;
8619 }
8620
8621 @ If the polygon being returned by |make_pen| has only one vertex, it will
8622 be interpreted as an elliptical pen.  This is no problem since a degenerate
8623 polygon can equally well be thought of as a degenerate ellipse.  We need only
8624 initialize the |left_x|, |left_y|, |right_x|, and |right_y| fields.
8625
8626 @<Make sure |h| isn't confused with an elliptical pen@>=
8627 if ( pen_is_elliptical( h) ){ 
8628   left_x(h)=x_coord(h); left_y(h)=y_coord(h);
8629   right_x(h)=x_coord(h); right_y(h)=y_coord(h);
8630 }
8631
8632 @ We have to cheat a little here but most operations on pens only use
8633 the first three words in each knot node.
8634 @^data structure assumptions@>
8635
8636 @<Initialize a pen at |test_pen| so that it fits in nine words@>=
8637 x_coord(test_pen)=-half_unit;
8638 y_coord(test_pen)=0;
8639 x_coord(test_pen+3)=half_unit;
8640 y_coord(test_pen+3)=0;
8641 x_coord(test_pen+6)=0;
8642 y_coord(test_pen+6)=unity;
8643 link(test_pen)=test_pen+3;
8644 link(test_pen+3)=test_pen+6;
8645 link(test_pen+6)=test_pen;
8646 knil(test_pen)=test_pen+6;
8647 knil(test_pen+3)=test_pen;
8648 knil(test_pen+6)=test_pen+3
8649
8650 @ Printing a polygonal pen is very much like printing a path
8651
8652 @<Declare subroutines for printing expressions@>=
8653 void mp_pr_pen (MP mp,pointer h) {
8654   pointer p,q; /* for list traversal */
8655   if ( pen_is_elliptical(h) ) {
8656     @<Print the elliptical pen |h|@>;
8657   } else { 
8658     p=h;
8659     do {  
8660       mp_print_two(mp, x_coord(p),y_coord(p));
8661       mp_print_nl(mp, " .. ");
8662       @<Advance |p| making sure the links are OK and |return| if there is
8663         a problem@>;
8664      } while (p!=h);
8665      mp_print(mp, "cycle");
8666   }
8667 }
8668
8669 @ @<Advance |p| making sure the links are OK and |return| if there is...@>=
8670 q=link(p);
8671 if ( (q==null) || (knil(q)!=p) ) { 
8672   mp_print_nl(mp, "???"); return; /* this won't happen */
8673 @.???@>
8674 }
8675 p=q
8676
8677 @ @<Print the elliptical pen |h|@>=
8678
8679 mp_print(mp, "pencircle transformed (");
8680 mp_print_scaled(mp, x_coord(h));
8681 mp_print_char(mp, ',');
8682 mp_print_scaled(mp, y_coord(h));
8683 mp_print_char(mp, ',');
8684 mp_print_scaled(mp, left_x(h)-x_coord(h));
8685 mp_print_char(mp, ',');
8686 mp_print_scaled(mp, right_x(h)-x_coord(h));
8687 mp_print_char(mp, ',');
8688 mp_print_scaled(mp, left_y(h)-y_coord(h));
8689 mp_print_char(mp, ',');
8690 mp_print_scaled(mp, right_y(h)-y_coord(h));
8691 mp_print_char(mp, ')');
8692 }
8693
8694 @ Here us another version of |pr_pen| that prints the pen as a diagnostic
8695 message.
8696
8697 @<Declare subroutines for printing expressions@>=
8698 void mp_print_pen (MP mp,pointer h, const char *s, boolean nuline) { 
8699   mp_print_diagnostic(mp, "Pen",s,nuline); mp_print_ln(mp);
8700 @.Pen at line...@>
8701   mp_pr_pen(mp, h);
8702   mp_end_diagnostic(mp, true);
8703 }
8704
8705 @ Making a polygonal pen into a path involves restoring the |left_type| and
8706 |right_type| fields and setting the control points so as to make a polygonal
8707 path.
8708
8709 @c 
8710 void mp_make_path (MP mp,pointer h) {
8711   pointer p; /* for traversing the knot list */
8712   small_number k; /* a loop counter */
8713   @<Other local variables in |make_path|@>;
8714   if ( pen_is_elliptical(h) ) {
8715     @<Make the elliptical pen |h| into a path@>;
8716   } else { 
8717     p=h;
8718     do {  
8719       left_type(p)=mp_explicit;
8720       right_type(p)=mp_explicit;
8721       @<copy the coordinates of knot |p| into its control points@>;
8722        p=link(p);
8723     } while (p!=h);
8724   }
8725 }
8726
8727 @ @<copy the coordinates of knot |p| into its control points@>=
8728 left_x(p)=x_coord(p);
8729 left_y(p)=y_coord(p);
8730 right_x(p)=x_coord(p);
8731 right_y(p)=y_coord(p)
8732
8733 @ We need an eight knot path to get a good approximation to an ellipse.
8734
8735 @<Make the elliptical pen |h| into a path@>=
8736
8737   @<Extract the transformation parameters from the elliptical pen~|h|@>;
8738   p=h;
8739   for (k=0;k<=7;k++ ) { 
8740     @<Initialize |p| as the |k|th knot of a circle of unit diameter,
8741       transforming it appropriately@>;
8742     if ( k==7 ) link(p)=h;  else link(p)=mp_get_node(mp, knot_node_size);
8743     p=link(p);
8744   }
8745 }
8746
8747 @ @<Extract the transformation parameters from the elliptical pen~|h|@>=
8748 center_x=x_coord(h);
8749 center_y=y_coord(h);
8750 width_x=left_x(h)-center_x;
8751 width_y=left_y(h)-center_y;
8752 height_x=right_x(h)-center_x;
8753 height_y=right_y(h)-center_y
8754
8755 @ @<Other local variables in |make_path|@>=
8756 scaled center_x,center_y; /* translation parameters for an elliptical pen */
8757 scaled width_x,width_y; /* the effect of a unit change in $x$ */
8758 scaled height_x,height_y; /* the effect of a unit change in $y$ */
8759 scaled dx,dy; /* the vector from knot |p| to its right control point */
8760 integer kk;
8761   /* |k| advanced $270^\circ$ around the ring (cf. $\sin\theta=\cos(\theta+270)$) */
8762
8763 @ The only tricky thing here are the tables |half_cos| and |d_cos| used to
8764 find the point $k/8$ of the way around the circle and the direction vector
8765 to use there.
8766
8767 @<Initialize |p| as the |k|th knot of a circle of unit diameter,...@>=
8768 kk=(k+6)% 8;
8769 x_coord(p)=center_x+mp_take_fraction(mp, mp->half_cos[k],width_x)
8770            +mp_take_fraction(mp, mp->half_cos[kk],height_x);
8771 y_coord(p)=center_y+mp_take_fraction(mp, mp->half_cos[k],width_y)
8772            +mp_take_fraction(mp, mp->half_cos[kk],height_y);
8773 dx=-mp_take_fraction(mp, mp->d_cos[kk],width_x)
8774    +mp_take_fraction(mp, mp->d_cos[k],height_x);
8775 dy=-mp_take_fraction(mp, mp->d_cos[kk],width_y)
8776    +mp_take_fraction(mp, mp->d_cos[k],height_y);
8777 right_x(p)=x_coord(p)+dx;
8778 right_y(p)=y_coord(p)+dy;
8779 left_x(p)=x_coord(p)-dx;
8780 left_y(p)=y_coord(p)-dy;
8781 left_type(p)=mp_explicit;
8782 right_type(p)=mp_explicit;
8783 originator(p)=mp_program_code
8784
8785 @ @<Glob...@>=
8786 fraction half_cos[8]; /* ${1\over2}\cos(45k)$ */
8787 fraction d_cos[8]; /* a magic constant times $\cos(45k)$ */
8788
8789 @ The magic constant for |d_cos| is the distance between $({1\over2},0)$ and
8790 $({1\over4}\sqrt2,{1\over4}\sqrt2)$ times the result of the |velocity|
8791 function for $\theta=\phi=22.5^\circ$.  This comes out to be
8792 $$ d = {\sqrt{2-\sqrt2}\over 3+3\cos22.5^\circ}
8793   \approx 0.132608244919772.
8794 $$
8795
8796 @<Set init...@>=
8797 mp->half_cos[0]=fraction_half;
8798 mp->half_cos[1]=94906266; /* $2^{26}\sqrt2\approx94906265.62$ */
8799 mp->half_cos[2]=0;
8800 mp->d_cos[0]=35596755; /* $2^{28}d\approx35596754.69$ */
8801 mp->d_cos[1]=25170707; /* $2^{27}\sqrt2\,d\approx25170706.63$ */
8802 mp->d_cos[2]=0;
8803 for (k=3;k<= 4;k++ ) { 
8804   mp->half_cos[k]=-mp->half_cos[4-k];
8805   mp->d_cos[k]=-mp->d_cos[4-k];
8806 }
8807 for (k=5;k<= 7;k++ ) { 
8808   mp->half_cos[k]=mp->half_cos[8-k];
8809   mp->d_cos[k]=mp->d_cos[8-k];
8810 }
8811
8812 @ The |convex_hull| function forces a pen polygon to be convex when it is
8813 returned by |make_pen| and after any subsequent transformation where rounding
8814 error might allow the convexity to be lost.
8815 The convex hull algorithm used here is described by F.~P. Preparata and
8816 M.~I. Shamos [{\sl Computational Geometry}, Springer-Verlag, 1985].
8817
8818 @<Declare a function called |convex_hull|@>=
8819 @<Declare a procedure called |move_knot|@>
8820 pointer mp_convex_hull (MP mp,pointer h) { /* Make a polygonal pen convex */
8821   pointer l,r; /* the leftmost and rightmost knots */
8822   pointer p,q; /* knots being scanned */
8823   pointer s; /* the starting point for an upcoming scan */
8824   scaled dx,dy; /* a temporary pointer */
8825   if ( pen_is_elliptical(h) ) {
8826      return h;
8827   } else { 
8828     @<Set |l| to the leftmost knot in polygon~|h|@>;
8829     @<Set |r| to the rightmost knot in polygon~|h|@>;
8830     if ( l!=r ) { 
8831       s=link(r);
8832       @<Find any knots on the path from |l| to |r| above the |l|-|r| line and
8833         move them past~|r|@>;
8834       @<Find any knots on the path from |s| to |l| below the |l|-|r| line and
8835         move them past~|l|@>;
8836       @<Sort the path from |l| to |r| by increasing $x$@>;
8837       @<Sort the path from |r| to |l| by decreasing $x$@>;
8838     }
8839     if ( l!=link(l) ) {
8840       @<Do a Gramm scan and remove vertices where there is no left turn@>;
8841     }
8842     return l;
8843   }
8844 }
8845
8846 @ All comparisons are done primarily on $x$ and secondarily on $y$.
8847
8848 @<Set |l| to the leftmost knot in polygon~|h|@>=
8849 l=h;
8850 p=link(h);
8851 while ( p!=h ) { 
8852   if ( x_coord(p)<=x_coord(l) )
8853     if ( (x_coord(p)<x_coord(l)) || (y_coord(p)<y_coord(l)) )
8854       l=p;
8855   p=link(p);
8856 }
8857
8858 @ @<Set |r| to the rightmost knot in polygon~|h|@>=
8859 r=h;
8860 p=link(h);
8861 while ( p!=h ) { 
8862   if ( x_coord(p)>=x_coord(r) )
8863     if ( (x_coord(p)>x_coord(r)) || (y_coord(p)>y_coord(r)) )
8864       r=p;
8865   p=link(p);
8866 }
8867
8868 @ @<Find any knots on the path from |l| to |r| above the |l|-|r| line...@>=
8869 dx=x_coord(r)-x_coord(l);
8870 dy=y_coord(r)-y_coord(l);
8871 p=link(l);
8872 while ( p!=r ) { 
8873   q=link(p);
8874   if ( mp_ab_vs_cd(mp, dx,y_coord(p)-y_coord(l),dy,x_coord(p)-x_coord(l))>0 )
8875     mp_move_knot(mp, p, r);
8876   p=q;
8877 }
8878
8879 @ The |move_knot| procedure removes |p| from a doubly linked list and inserts
8880 it after |q|.
8881
8882 @ @<Declare a procedure called |move_knot|@>=
8883 void mp_move_knot (MP mp,pointer p, pointer q) { 
8884   link(knil(p))=link(p);
8885   knil(link(p))=knil(p);
8886   knil(p)=q;
8887   link(p)=link(q);
8888   link(q)=p;
8889   knil(link(p))=p;
8890 }
8891
8892 @ @<Find any knots on the path from |s| to |l| below the |l|-|r| line...@>=
8893 p=s;
8894 while ( p!=l ) { 
8895   q=link(p);
8896   if ( mp_ab_vs_cd(mp, dx,y_coord(p)-y_coord(l),dy,x_coord(p)-x_coord(l))<0 )
8897     mp_move_knot(mp, p,l);
8898   p=q;
8899 }
8900
8901 @ The list is likely to be in order already so we just do linear insertions.
8902 Secondary comparisons on $y$ ensure that the sort is consistent with the
8903 choice of |l| and |r|.
8904
8905 @<Sort the path from |l| to |r| by increasing $x$@>=
8906 p=link(l);
8907 while ( p!=r ) { 
8908   q=knil(p);
8909   while ( x_coord(q)>x_coord(p) ) q=knil(q);
8910   while ( x_coord(q)==x_coord(p) ) {
8911     if ( y_coord(q)>y_coord(p) ) q=knil(q); else break;
8912   }
8913   if ( q==knil(p) ) p=link(p);
8914   else { p=link(p); mp_move_knot(mp, knil(p),q); };
8915 }
8916
8917 @ @<Sort the path from |r| to |l| by decreasing $x$@>=
8918 p=link(r);
8919 while ( p!=l ){ 
8920   q=knil(p);
8921   while ( x_coord(q)<x_coord(p) ) q=knil(q);
8922   while ( x_coord(q)==x_coord(p) ) {
8923     if ( y_coord(q)<y_coord(p) ) q=knil(q); else break;
8924   }
8925   if ( q==knil(p) ) p=link(p);
8926   else { p=link(p); mp_move_knot(mp, knil(p),q); };
8927 }
8928
8929 @ The condition involving |ab_vs_cd| tests if there is not a left turn
8930 at knot |q|.  There usually will be a left turn so we streamline the case
8931 where the |then| clause is not executed.
8932
8933 @<Do a Gramm scan and remove vertices where there...@>=
8934
8935 p=l; q=link(l);
8936 while (1) { 
8937   dx=x_coord(q)-x_coord(p);
8938   dy=y_coord(q)-y_coord(p);
8939   p=q; q=link(q);
8940   if ( p==l ) break;
8941   if ( p!=r )
8942     if ( mp_ab_vs_cd(mp, dx,y_coord(q)-y_coord(p),dy,x_coord(q)-x_coord(p))<=0 ) {
8943       @<Remove knot |p| and back up |p| and |q| but don't go past |l|@>;
8944     }
8945   }
8946 }
8947
8948 @ @<Remove knot |p| and back up |p| and |q| but don't go past |l|@>=
8949
8950 s=knil(p);
8951 mp_free_node(mp, p,knot_node_size);
8952 link(s)=q; knil(q)=s;
8953 if ( s==l ) p=s;
8954 else { p=knil(s); q=s; };
8955 }
8956
8957 @ The |find_offset| procedure sets global variables |(cur_x,cur_y)| to the
8958 offset associated with the given direction |(x,y)|.  If two different offsets
8959 apply, it chooses one of them.
8960
8961 @c 
8962 void mp_find_offset (MP mp,scaled x, scaled y, pointer h) {
8963   pointer p,q; /* consecutive knots */
8964   scaled wx,wy,hx,hy;
8965   /* the transformation matrix for an elliptical pen */
8966   fraction xx,yy; /* untransformed offset for an elliptical pen */
8967   fraction d; /* a temporary register */
8968   if ( pen_is_elliptical(h) ) {
8969     @<Find the offset for |(x,y)| on the elliptical pen~|h|@>
8970   } else { 
8971     q=h;
8972     do {  
8973       p=q; q=link(q);
8974     } while (!(mp_ab_vs_cd(mp, x_coord(q)-x_coord(p),y, y_coord(q)-y_coord(p),x)>=0));
8975     do {  
8976       p=q; q=link(q);
8977     } while (!(mp_ab_vs_cd(mp, x_coord(q)-x_coord(p),y, y_coord(q)-y_coord(p),x)<=0));
8978     mp->cur_x=x_coord(p);
8979     mp->cur_y=y_coord(p);
8980   }
8981 }
8982
8983 @ @<Glob...@>=
8984 scaled cur_x;
8985 scaled cur_y; /* all-purpose return value registers */
8986
8987 @ @<Find the offset for |(x,y)| on the elliptical pen~|h|@>=
8988 if ( (x==0) && (y==0) ) {
8989   mp->cur_x=x_coord(h); mp->cur_y=y_coord(h);  
8990 } else { 
8991   @<Find the non-constant part of the transformation for |h|@>;
8992   while ( (abs(x)<fraction_half) && (abs(y)<fraction_half) ){ 
8993     x+=x; y+=y;  
8994   };
8995   @<Make |(xx,yy)| the offset on the untransformed \&{pencircle} for the
8996     untransformed version of |(x,y)|@>;
8997   mp->cur_x=x_coord(h)+mp_take_fraction(mp, xx,wx)+mp_take_fraction(mp, yy,hx);
8998   mp->cur_y=y_coord(h)+mp_take_fraction(mp, xx,wy)+mp_take_fraction(mp, yy,hy);
8999 }
9000
9001 @ @<Find the non-constant part of the transformation for |h|@>=
9002 wx=left_x(h)-x_coord(h);
9003 wy=left_y(h)-y_coord(h);
9004 hx=right_x(h)-x_coord(h);
9005 hy=right_y(h)-y_coord(h)
9006
9007 @ @<Make |(xx,yy)| the offset on the untransformed \&{pencircle} for the...@>=
9008 yy=-(mp_take_fraction(mp, x,hy)+mp_take_fraction(mp, y,-hx));
9009 xx=mp_take_fraction(mp, x,-wy)+mp_take_fraction(mp, y,wx);
9010 d=mp_pyth_add(mp, xx,yy);
9011 if ( d>0 ) { 
9012   xx=half(mp_make_fraction(mp, xx,d));
9013   yy=half(mp_make_fraction(mp, yy,d));
9014 }
9015
9016 @ Finding the bounding box of a pen is easy except if the pen is elliptical.
9017 But we can handle that case by just calling |find_offset| twice.  The answer
9018 is stored in the global variables |minx|, |maxx|, |miny|, and |maxy|.
9019
9020 @c 
9021 void mp_pen_bbox (MP mp,pointer h) {
9022   pointer p; /* for scanning the knot list */
9023   if ( pen_is_elliptical(h) ) {
9024     @<Find the bounding box of an elliptical pen@>;
9025   } else { 
9026     minx=x_coord(h); maxx=minx;
9027     miny=y_coord(h); maxy=miny;
9028     p=link(h);
9029     while ( p!=h ) {
9030       if ( x_coord(p)<minx ) minx=x_coord(p);
9031       if ( y_coord(p)<miny ) miny=y_coord(p);
9032       if ( x_coord(p)>maxx ) maxx=x_coord(p);
9033       if ( y_coord(p)>maxy ) maxy=y_coord(p);
9034       p=link(p);
9035     }
9036   }
9037 }
9038
9039 @ @<Find the bounding box of an elliptical pen@>=
9040
9041 mp_find_offset(mp, 0,fraction_one,h);
9042 maxx=mp->cur_x;
9043 minx=2*x_coord(h)-mp->cur_x;
9044 mp_find_offset(mp, -fraction_one,0,h);
9045 maxy=mp->cur_y;
9046 miny=2*y_coord(h)-mp->cur_y;
9047 }
9048
9049 @* \[21] Edge structures.
9050 Now we come to \MP's internal scheme for representing pictures.
9051 The representation is very different from \MF's edge structures
9052 because \MP\ pictures contain \ps\ graphics objects instead of pixel
9053 images.  However, the basic idea is somewhat similar in that shapes
9054 are represented via their boundaries.
9055
9056 The main purpose of edge structures is to keep track of graphical objects
9057 until it is time to translate them into \ps.  Since \MP\ does not need to
9058 know anything about an edge structure other than how to translate it into
9059 \ps\ and how to find its bounding box, edge structures can be just linked
9060 lists of graphical objects.  \MP\ has no easy way to determine whether
9061 two such objects overlap, but it suffices to draw the first one first and
9062 let the second one overwrite it if necessary.
9063
9064 @<Types...@>=
9065 enum mp_graphical_object_code {
9066   @<Graphical object codes@>
9067   mp_final_graphic
9068 };
9069
9070 @ Let's consider the types of graphical objects one at a time.
9071 First of all, a filled contour is represented by a eight-word node.  The first
9072 word contains |type| and |link| fields, and the next six words contain a
9073 pointer to a cyclic path and the value to use for \ps' \&{currentrgbcolor}
9074 parameter.  If a pen is used for filling |pen_p|, |ljoin_val| and |miterlim_val|
9075 give the relevant information.
9076
9077 @d path_p(A) link((A)+1)
9078   /* a pointer to the path that needs filling */
9079 @d pen_p(A) info((A)+1)
9080   /* a pointer to the pen to fill or stroke with */
9081 @d color_model(A) type((A)+2) /*  the color model  */
9082 @d obj_red_loc(A) ((A)+3)  /* the first of three locations for the color */
9083 @d obj_cyan_loc obj_red_loc  /* the first of four locations for the color */
9084 @d obj_grey_loc obj_red_loc  /* the location for the color */
9085 @d red_val(A) mp->mem[(A)+3].sc
9086   /* the red component of the color in the range $0\ldots1$ */
9087 @d cyan_val red_val
9088 @d grey_val red_val
9089 @d green_val(A) mp->mem[(A)+4].sc
9090   /* the green component of the color in the range $0\ldots1$ */
9091 @d magenta_val green_val
9092 @d blue_val(A) mp->mem[(A)+5].sc
9093   /* the blue component of the color in the range $0\ldots1$ */
9094 @d yellow_val blue_val
9095 @d black_val(A) mp->mem[(A)+6].sc
9096   /* the blue component of the color in the range $0\ldots1$ */
9097 @d ljoin_val(A) name_type((A))  /* the value of \&{linejoin} */
9098 @:mp_linejoin_}{\&{linejoin} primitive@>
9099 @d miterlim_val(A) mp->mem[(A)+7].sc  /* the value of \&{miterlimit} */
9100 @:mp_miterlimit_}{\&{miterlimit} primitive@>
9101 @d obj_color_part(A) mp->mem[(A)+3-red_part].sc
9102   /* interpret an object pointer that has been offset by |red_part..blue_part| */
9103 @d pre_script(A) mp->mem[(A)+8].hh.lh
9104 @d post_script(A) mp->mem[(A)+8].hh.rh
9105 @d fill_node_size 9
9106
9107 @ @<Graphical object codes@>=
9108 mp_fill_code=1,
9109
9110 @ @c 
9111 pointer mp_new_fill_node (MP mp,pointer p) {
9112   /* make a fill node for cyclic path |p| and color black */
9113   pointer t; /* the new node */
9114   t=mp_get_node(mp, fill_node_size);
9115   type(t)=mp_fill_code;
9116   path_p(t)=p;
9117   pen_p(t)=null; /* |null| means don't use a pen */
9118   red_val(t)=0;
9119   green_val(t)=0;
9120   blue_val(t)=0;
9121   black_val(t)=0;
9122   color_model(t)=mp_uninitialized_model;
9123   pre_script(t)=null;
9124   post_script(t)=null;
9125   @<Set the |ljoin_val| and |miterlim_val| fields in object |t|@>;
9126   return t;
9127 }
9128
9129 @ @<Set the |ljoin_val| and |miterlim_val| fields in object |t|@>=
9130 if ( mp->internal[mp_linejoin]>unity ) ljoin_val(t)=2;
9131 else if ( mp->internal[mp_linejoin]>0 ) ljoin_val(t)=1;
9132 else ljoin_val(t)=0;
9133 if ( mp->internal[mp_miterlimit]<unity )
9134   miterlim_val(t)=unity;
9135 else
9136   miterlim_val(t)=mp->internal[mp_miterlimit]
9137
9138 @ A stroked path is represented by an eight-word node that is like a filled
9139 contour node except that it contains the current \&{linecap} value, a scale
9140 factor for the dash pattern, and a pointer that is non-null if the stroke
9141 is to be dashed.  The purpose of the scale factor is to allow a picture to
9142 be transformed without touching the picture that |dash_p| points to.
9143
9144 @d dash_p(A) link((A)+9)
9145   /* a pointer to the edge structure that gives the dash pattern */
9146 @d lcap_val(A) type((A)+9)
9147   /* the value of \&{linecap} */
9148 @:mp_linecap_}{\&{linecap} primitive@>
9149 @d dash_scale(A) mp->mem[(A)+10].sc /* dash lengths are scaled by this factor */
9150 @d stroked_node_size 11
9151
9152 @ @<Graphical object codes@>=
9153 mp_stroked_code=2,
9154
9155 @ @c 
9156 pointer mp_new_stroked_node (MP mp,pointer p) {
9157   /* make a stroked node for path |p| with |pen_p(p)| temporarily |null| */
9158   pointer t; /* the new node */
9159   t=mp_get_node(mp, stroked_node_size);
9160   type(t)=mp_stroked_code;
9161   path_p(t)=p; pen_p(t)=null;
9162   dash_p(t)=null;
9163   dash_scale(t)=unity;
9164   red_val(t)=0;
9165   green_val(t)=0;
9166   blue_val(t)=0;
9167   black_val(t)=0;
9168   color_model(t)=mp_uninitialized_model;
9169   pre_script(t)=null;
9170   post_script(t)=null;
9171   @<Set the |ljoin_val| and |miterlim_val| fields in object |t|@>;
9172   if ( mp->internal[mp_linecap]>unity ) lcap_val(t)=2;
9173   else if ( mp->internal[mp_linecap]>0 ) lcap_val(t)=1;
9174   else lcap_val(t)=0;
9175   return t;
9176 }
9177
9178 @ When a dashed line is computed in a transformed coordinate system, the dash
9179 lengths get scaled like the pen shape and we need to compensate for this.  Since
9180 there is no unique scale factor for an arbitrary transformation, we use the
9181 the square root of the determinant.  The properties of the determinant make it
9182 easier to maintain the |dash_scale|.  The computation is fairly straight-forward
9183 except for the initialization of the scale factor |s|.  The factor of 64 is
9184 needed because |square_rt| scales its result by $2^8$ while we need $2^{14}$
9185 to counteract the effect of |take_fraction|.
9186
9187 @<Declare subroutines needed by |print_edges|@>=
9188 scaled mp_sqrt_det (MP mp,scaled a, scaled b, scaled c, scaled d) {
9189   scaled maxabs; /* $max(|a|,|b|,|c|,|d|)$ */
9190   integer s; /* amount by which the result of |square_rt| needs to be scaled */
9191   @<Initialize |maxabs|@>;
9192   s=64;
9193   while ( (maxabs<fraction_one) && (s>1) ){ 
9194     a+=a; b+=b; c+=c; d+=d;
9195     maxabs+=maxabs; s=halfp(s);
9196   }
9197   return s*mp_square_rt(mp, abs(mp_take_fraction(mp, a,d)-mp_take_fraction(mp, b,c)));
9198 }
9199 @#
9200 scaled mp_get_pen_scale (MP mp,pointer p) { 
9201   return mp_sqrt_det(mp, 
9202     left_x(p)-x_coord(p), right_x(p)-x_coord(p),
9203     left_y(p)-y_coord(p), right_y(p)-y_coord(p));
9204 }
9205
9206 @ @<Internal library ...@>=
9207 scaled mp_sqrt_det (MP mp,scaled a, scaled b, scaled c, scaled d) ;
9208
9209
9210 @ @<Initialize |maxabs|@>=
9211 maxabs=abs(a);
9212 if ( abs(b)>maxabs ) maxabs=abs(b);
9213 if ( abs(c)>maxabs ) maxabs=abs(c);
9214 if ( abs(d)>maxabs ) maxabs=abs(d)
9215
9216 @ When a picture contains text, this is represented by a fourteen-word node
9217 where the color information and |type| and |link| fields are augmented by
9218 additional fields that describe the text and  how it is transformed.
9219 The |path_p| and |pen_p| pointers are replaced by a number that identifies
9220 the font and a string number that gives the text to be displayed.
9221 The |width|, |height|, and |depth| fields
9222 give the dimensions of the text at its design size, and the remaining six
9223 words give a transformation to be applied to the text.  The |new_text_node|
9224 function initializes everything to default values so that the text comes out
9225 black with its reference point at the origin.
9226
9227 @d text_p(A) link((A)+1)  /* a string pointer for the text to display */
9228 @d font_n(A) info((A)+1)  /* the font number */
9229 @d width_val(A) mp->mem[(A)+7].sc  /* unscaled width of the text */
9230 @d height_val(A) mp->mem[(A)+9].sc  /* unscaled height of the text */
9231 @d depth_val(A) mp->mem[(A)+10].sc  /* unscaled depth of the text */
9232 @d text_tx_loc(A) ((A)+11)
9233   /* the first of six locations for transformation parameters */
9234 @d tx_val(A) mp->mem[(A)+11].sc  /* $x$ shift amount */
9235 @d ty_val(A) mp->mem[(A)+12].sc  /* $y$ shift amount */
9236 @d txx_val(A) mp->mem[(A)+13].sc  /* |txx| transformation parameter */
9237 @d txy_val(A) mp->mem[(A)+14].sc  /* |txy| transformation parameter */
9238 @d tyx_val(A) mp->mem[(A)+15].sc  /* |tyx| transformation parameter */
9239 @d tyy_val(A) mp->mem[(A)+16].sc  /* |tyy| transformation parameter */
9240 @d text_trans_part(A) mp->mem[(A)+11-x_part].sc
9241     /* interpret a text node pointer that has been offset by |x_part..yy_part| */
9242 @d text_node_size 17
9243
9244 @ @<Graphical object codes@>=
9245 mp_text_code=3,
9246
9247 @ @c @<Declare text measuring subroutines@>
9248 pointer mp_new_text_node (MP mp,char *f,str_number s) {
9249   /* make a text node for font |f| and text string |s| */
9250   pointer t; /* the new node */
9251   t=mp_get_node(mp, text_node_size);
9252   type(t)=mp_text_code;
9253   text_p(t)=s;
9254   font_n(t)=mp_find_font(mp, f); /* this identifies the font */
9255   red_val(t)=0;
9256   green_val(t)=0;
9257   blue_val(t)=0;
9258   black_val(t)=0;
9259   color_model(t)=mp_uninitialized_model;
9260   pre_script(t)=null;
9261   post_script(t)=null;
9262   tx_val(t)=0; ty_val(t)=0;
9263   txx_val(t)=unity; txy_val(t)=0;
9264   tyx_val(t)=0; tyy_val(t)=unity;
9265   mp_set_text_box(mp, t); /* this finds the bounding box */
9266   return t;
9267 }
9268
9269 @ The last two types of graphical objects that can occur in an edge structure
9270 are clipping paths and \&{setbounds} paths.  These are slightly more difficult
9271 @:set_bounds_}{\&{setbounds} primitive@>
9272 to implement because we must keep track of exactly what is being clipped or
9273 bounded when pictures get merged together.  For this reason, each clipping or
9274 \&{setbounds} operation is represented by a pair of nodes:  first comes a
9275 two-word node whose |path_p| gives the relevant path, then there is the list
9276 of objects to clip or bound followed by a two-word node whose second word is
9277 unused.
9278
9279 Using at least two words for each graphical object node allows them all to be
9280 allocated and deallocated similarly with a global array |gr_object_size| to
9281 give the size in words for each object type.
9282
9283 @d start_clip_size 2
9284 @d start_bounds_size 2
9285 @d stop_clip_size 2 /* the second word is not used here */
9286 @d stop_bounds_size 2 /* the second word is not used here */
9287 @#
9288 @d stop_type(A) ((A)+2)
9289   /* matching |type| for |start_clip_code| or |start_bounds_code| */
9290 @d has_color(A) (type((A))<mp_start_clip_code)
9291   /* does a graphical object have color fields? */
9292 @d has_pen(A) (type((A))<mp_text_code)
9293   /* does a graphical object have a |pen_p| field? */
9294 @d is_start_or_stop(A) (type((A))>=mp_start_clip_code)
9295 @d is_stop(A) (type((A))>=mp_stop_clip_code)
9296
9297 @ @<Graphical object codes@>=
9298 mp_start_clip_code=4, /* |type| of a node that starts clipping */
9299 mp_start_bounds_code=5, /* |type| of a node that gives a \&{setbounds} path */
9300 mp_stop_clip_code=6, /* |type| of a node that stops clipping */
9301 mp_stop_bounds_code=7, /* |type| of a node that stops \&{setbounds} */
9302
9303 @ @c 
9304 pointer mp_new_bounds_node (MP mp,pointer p, small_number  c) {
9305   /* make a node of type |c| where |p| is the clipping or \&{setbounds} path */
9306   pointer t; /* the new node */
9307   t=mp_get_node(mp, mp->gr_object_size[c]);
9308   type(t)=c;
9309   path_p(t)=p;
9310   return t;
9311 }
9312
9313 @ We need an array to keep track of the sizes of graphical objects.
9314
9315 @<Glob...@>=
9316 small_number gr_object_size[mp_stop_bounds_code+1];
9317
9318 @ @<Set init...@>=
9319 mp->gr_object_size[mp_fill_code]=fill_node_size;
9320 mp->gr_object_size[mp_stroked_code]=stroked_node_size;
9321 mp->gr_object_size[mp_text_code]=text_node_size;
9322 mp->gr_object_size[mp_start_clip_code]=start_clip_size;
9323 mp->gr_object_size[mp_stop_clip_code]=stop_clip_size;
9324 mp->gr_object_size[mp_start_bounds_code]=start_bounds_size;
9325 mp->gr_object_size[mp_stop_bounds_code]=stop_bounds_size;
9326
9327 @ All the essential information in an edge structure is encoded as a linked list
9328 of graphical objects as we have just seen, but it is helpful to add some
9329 redundant information.  A single edge structure might be used as a dash pattern
9330 many times, and it would be nice to avoid scanning the same structure
9331 repeatedly.  Thus, an edge structure known to be a suitable dash pattern
9332 has a header that gives a list of dashes in a sorted order designed for rapid
9333 translation into \ps.
9334
9335 Each dash is represented by a three-word node containing the initial and final
9336 $x$~coordinates as well as the usual |link| field.  The |link| fields points to
9337 the dash node with the next higher $x$-coordinates and the final link points
9338 to a special location called |null_dash|.  (There should be no overlap between
9339 dashes).  Since the $y$~coordinate of the dash pattern is needed to determine
9340 the period of repetition, this needs to be stored in the edge header along
9341 with a pointer to the list of dash nodes.
9342
9343 @d start_x(A) mp->mem[(A)+1].sc  /* the starting $x$~coordinate in a dash node */
9344 @d stop_x(A) mp->mem[(A)+2].sc  /* the ending $x$~coordinate in a dash node */
9345 @d dash_node_size 3
9346 @d dash_list link
9347   /* in an edge header this points to the first dash node */
9348 @d dash_y(A) mp->mem[(A)+1].sc  /* $y$ value for the dash list in an edge header */
9349
9350 @ It is also convenient for an edge header to contain the bounding
9351 box information needed by the \&{llcorner} and \&{urcorner} operators
9352 so that this does not have to be recomputed unnecessarily.  This is done by
9353 adding fields for the $x$~and $y$ extremes as well as a pointer that indicates
9354 how far the bounding box computation has gotten.  Thus if the user asks for
9355 the bounding box and then adds some more text to the picture before asking
9356 for more bounding box information, the second computation need only look at
9357 the additional text.
9358
9359 When the bounding box has not been computed, the |bblast| pointer points
9360 to a dummy link at the head of the graphical object list while the |minx_val|
9361 and |miny_val| fields contain |el_gordo| and the |maxx_val| and |maxy_val|
9362 fields contain |-el_gordo|.
9363
9364 Since the bounding box of pictures containing objects of type
9365 |mp_start_bounds_code| depends on the value of \&{truecorners}, the bounding box
9366 @:mp_true_corners_}{\&{truecorners} primitive@>
9367 data might not be valid for all values of this parameter.  Hence, the |bbtype|
9368 field is needed to keep track of this.
9369
9370 @d minx_val(A) mp->mem[(A)+2].sc
9371 @d miny_val(A) mp->mem[(A)+3].sc
9372 @d maxx_val(A) mp->mem[(A)+4].sc
9373 @d maxy_val(A) mp->mem[(A)+5].sc
9374 @d bblast(A) link((A)+6)  /* last item considered in bounding box computation */
9375 @d bbtype(A) info((A)+6)  /* tells how bounding box data depends on \&{truecorners} */
9376 @d dummy_loc(A) ((A)+7)  /* where the object list begins in an edge header */
9377 @d no_bounds 0
9378   /* |bbtype| value when bounding box data is valid for all \&{truecorners} values */
9379 @d bounds_set 1
9380   /* |bbtype| value when bounding box data is for \&{truecorners}${}\le 0$ */
9381 @d bounds_unset 2
9382   /* |bbtype| value when bounding box data is for \&{truecorners}${}>0$ */
9383
9384 @c 
9385 void mp_init_bbox (MP mp,pointer h) {
9386   /* Initialize the bounding box information in edge structure |h| */
9387   bblast(h)=dummy_loc(h);
9388   bbtype(h)=no_bounds;
9389   minx_val(h)=el_gordo;
9390   miny_val(h)=el_gordo;
9391   maxx_val(h)=-el_gordo;
9392   maxy_val(h)=-el_gordo;
9393 }
9394
9395 @ The only other entries in an edge header are a reference count in the first
9396 word and a pointer to the tail of the object list in the last word.
9397
9398 @d obj_tail(A) info((A)+7)  /* points to the last entry in the object list */
9399 @d edge_header_size 8
9400
9401 @c 
9402 void mp_init_edges (MP mp,pointer h) {
9403   /* initialize an edge header to null values */
9404   dash_list(h)=null_dash;
9405   obj_tail(h)=dummy_loc(h);
9406   link(dummy_loc(h))=null;
9407   ref_count(h)=null;
9408   mp_init_bbox(mp, h);
9409 }
9410
9411 @ Here is how edge structures are deleted.  The process can be recursive because
9412 of the need to dereference edge structures that are used as dash patterns.
9413 @^recursion@>
9414
9415 @d add_edge_ref(A) incr(ref_count(A))
9416 @d delete_edge_ref(A) { 
9417    if ( ref_count((A))==null ) 
9418      mp_toss_edges(mp, A);
9419    else 
9420      decr(ref_count(A)); 
9421    }
9422
9423 @<Declare the recycling subroutines@>=
9424 void mp_flush_dash_list (MP mp,pointer h);
9425 pointer mp_toss_gr_object (MP mp,pointer p) ;
9426 void mp_toss_edges (MP mp,pointer h) ;
9427
9428 @ @c void mp_toss_edges (MP mp,pointer h) {
9429   pointer p,q;  /* pointers that scan the list being recycled */
9430   pointer r; /* an edge structure that object |p| refers to */
9431   mp_flush_dash_list(mp, h);
9432   q=link(dummy_loc(h));
9433   while ( (q!=null) ) { 
9434     p=q; q=link(q);
9435     r=mp_toss_gr_object(mp, p);
9436     if ( r!=null ) delete_edge_ref(r);
9437   }
9438   mp_free_node(mp, h,edge_header_size);
9439 }
9440 void mp_flush_dash_list (MP mp,pointer h) {
9441   pointer p,q;  /* pointers that scan the list being recycled */
9442   q=dash_list(h);
9443   while ( q!=null_dash ) { 
9444     p=q; q=link(q);
9445     mp_free_node(mp, p,dash_node_size);
9446   }
9447   dash_list(h)=null_dash;
9448 }
9449 pointer mp_toss_gr_object (MP mp,pointer p) {
9450   /* returns an edge structure that needs to be dereferenced */
9451   pointer e; /* the edge structure to return */
9452   e=null;
9453   @<Prepare to recycle graphical object |p|@>;
9454   mp_free_node(mp, p,mp->gr_object_size[type(p)]);
9455   return e;
9456 }
9457
9458 @ @<Prepare to recycle graphical object |p|@>=
9459 switch (type(p)) {
9460 case mp_fill_code: 
9461   mp_toss_knot_list(mp, path_p(p));
9462   if ( pen_p(p)!=null ) mp_toss_knot_list(mp, pen_p(p));
9463   if ( pre_script(p)!=null ) delete_str_ref(pre_script(p));
9464   if ( post_script(p)!=null ) delete_str_ref(post_script(p));
9465   break;
9466 case mp_stroked_code: 
9467   mp_toss_knot_list(mp, path_p(p));
9468   if ( pen_p(p)!=null ) mp_toss_knot_list(mp, pen_p(p));
9469   if ( pre_script(p)!=null ) delete_str_ref(pre_script(p));
9470   if ( post_script(p)!=null ) delete_str_ref(post_script(p));
9471   e=dash_p(p);
9472   break;
9473 case mp_text_code: 
9474   delete_str_ref(text_p(p));
9475   if ( pre_script(p)!=null ) delete_str_ref(pre_script(p));
9476   if ( post_script(p)!=null ) delete_str_ref(post_script(p));
9477   break;
9478 case mp_start_clip_code:
9479 case mp_start_bounds_code: 
9480   mp_toss_knot_list(mp, path_p(p));
9481   break;
9482 case mp_stop_clip_code:
9483 case mp_stop_bounds_code: 
9484   break;
9485 } /* there are no other cases */
9486
9487 @ If we use |add_edge_ref| to ``copy'' edge structures, the real copying needs
9488 to be done before making a significant change to an edge structure.  Much of
9489 the work is done in a separate routine |copy_objects| that copies a list of
9490 graphical objects into a new edge header.
9491
9492 @c @<Declare a function called |copy_objects|@>
9493 pointer mp_private_edges (MP mp,pointer h) {
9494   /* make a private copy of the edge structure headed by |h| */
9495   pointer hh;  /* the edge header for the new copy */
9496   pointer p,pp;  /* pointers for copying the dash list */
9497   if ( ref_count(h)==null ) {
9498     return h;
9499   } else { 
9500     decr(ref_count(h));
9501     hh=mp_copy_objects(mp, link(dummy_loc(h)),null);
9502     @<Copy the dash list from |h| to |hh|@>;
9503     @<Copy the bounding box information from |h| to |hh| and make |bblast(hh)|
9504       point into the new object list@>;
9505     return hh;
9506   }
9507 }
9508
9509 @ Here we use the fact that |dash_list(hh)=link(hh)|.
9510 @^data structure assumptions@>
9511
9512 @<Copy the dash list from |h| to |hh|@>=
9513 pp=hh; p=dash_list(h);
9514 while ( (p!=null_dash) ) { 
9515   link(pp)=mp_get_node(mp, dash_node_size);
9516   pp=link(pp);
9517   start_x(pp)=start_x(p);
9518   stop_x(pp)=stop_x(p);
9519   p=link(p);
9520 }
9521 link(pp)=null_dash;
9522 dash_y(hh)=dash_y(h)
9523
9524
9525 @ |h| is an edge structure
9526
9527 @c
9528 mp_dash_object *mp_export_dashes (MP mp, pointer q, scaled *w) {
9529   mp_dash_object *d;
9530   pointer p, h;
9531   scaled scf; /* scale factor */
9532   scaled *dashes = NULL;
9533   int num_dashes = 1;
9534   h = dash_p(q);
9535   if (h==null ||  dash_list(h)==null_dash) 
9536         return NULL;
9537   p = dash_list(h);
9538   scf=mp_get_pen_scale(mp, pen_p(q));
9539   if (scf==0) {
9540     if (*w==0) scf = dash_scale(q); else return NULL;
9541   } else {
9542     scf=mp_make_scaled(mp, *w,scf);
9543     scf=mp_take_scaled(mp, scf,dash_scale(q));
9544   }
9545   *w = scf;
9546   d = mp_xmalloc(mp,1,sizeof(mp_dash_object));
9547   start_x(null_dash)=start_x(p)+dash_y(h);
9548   while (p != null_dash) { 
9549         dashes = mp_xrealloc(mp, dashes, num_dashes+2, sizeof(scaled));
9550         dashes[(num_dashes-1)] = 
9551       mp_take_scaled(mp,(stop_x(p)-start_x(p)),scf);
9552         dashes[(num_dashes)]   = 
9553       mp_take_scaled(mp,(start_x(link(p))-stop_x(p)),scf);
9554         dashes[(num_dashes+1)] = -1; /* terminus */
9555         num_dashes+=2;
9556     p=link(p);
9557   }
9558   d->array_field  = dashes;
9559   d->offset_field = 
9560     mp_take_scaled(mp,mp_dash_offset(mp, h),scf);
9561   return d;
9562 }
9563
9564
9565
9566 @ @<Copy the bounding box information from |h| to |hh|...@>=
9567 minx_val(hh)=minx_val(h);
9568 miny_val(hh)=miny_val(h);
9569 maxx_val(hh)=maxx_val(h);
9570 maxy_val(hh)=maxy_val(h);
9571 bbtype(hh)=bbtype(h);
9572 p=dummy_loc(h); pp=dummy_loc(hh);
9573 while ((p!=bblast(h)) ) { 
9574   if ( p==null ) mp_confusion(mp, "bblast");
9575 @:this can't happen bblast}{\quad bblast@>
9576   p=link(p); pp=link(pp);
9577 }
9578 bblast(hh)=pp
9579
9580 @ Here is the promised routine for copying graphical objects into a new edge
9581 structure.  It starts copying at object~|p| and stops just before object~|q|.
9582 If |q| is null, it copies the entire sublist headed at |p|.  The resulting edge
9583 structure requires further initialization by |init_bbox|.
9584
9585 @<Declare a function called |copy_objects|@>=
9586 pointer mp_copy_objects (MP mp, pointer p, pointer q) {
9587   pointer hh;  /* the new edge header */
9588   pointer pp;  /* the last newly copied object */
9589   small_number k;  /* temporary register */
9590   hh=mp_get_node(mp, edge_header_size);
9591   dash_list(hh)=null_dash;
9592   ref_count(hh)=null;
9593   pp=dummy_loc(hh);
9594   while ( (p!=q) ) {
9595     @<Make |link(pp)| point to a copy of object |p|, and update |p| and |pp|@>;
9596   }
9597   obj_tail(hh)=pp;
9598   link(pp)=null;
9599   return hh;
9600 }
9601
9602 @ @<Make |link(pp)| point to a copy of object |p|, and update |p| and |pp|@>=
9603 { k=mp->gr_object_size[type(p)];
9604   link(pp)=mp_get_node(mp, k);
9605   pp=link(pp);
9606   while ( (k>0) ) { decr(k); mp->mem[pp+k]=mp->mem[p+k];  };
9607   @<Fix anything in graphical object |pp| that should differ from the
9608     corresponding field in |p|@>;
9609   p=link(p);
9610 }
9611
9612 @ @<Fix anything in graphical object |pp| that should differ from the...@>=
9613 switch (type(p)) {
9614 case mp_start_clip_code:
9615 case mp_start_bounds_code: 
9616   path_p(pp)=mp_copy_path(mp, path_p(p));
9617   break;
9618 case mp_fill_code: 
9619   path_p(pp)=mp_copy_path(mp, path_p(p));
9620   if ( pre_script(p)!=null )  add_str_ref(pre_script(p));
9621   if ( post_script(p)!=null ) add_str_ref(post_script(p));
9622   if ( pen_p(p)!=null ) pen_p(pp)=copy_pen(pen_p(p));
9623   break;
9624 case mp_stroked_code: 
9625   if ( pre_script(p)!=null )  add_str_ref(pre_script(p));
9626   if ( post_script(p)!=null ) add_str_ref(post_script(p));
9627   path_p(pp)=mp_copy_path(mp, path_p(p));
9628   pen_p(pp)=copy_pen(pen_p(p));
9629   if ( dash_p(p)!=null ) add_edge_ref(dash_p(pp));
9630   break;
9631 case mp_text_code: 
9632   if ( pre_script(p)!=null )  add_str_ref(pre_script(p));
9633   if ( post_script(p)!=null ) add_str_ref(post_script(p));
9634   add_str_ref(text_p(pp));
9635   break;
9636 case mp_stop_clip_code:
9637 case mp_stop_bounds_code: 
9638   break;
9639 }  /* there are no other cases */
9640
9641 @ Here is one way to find an acceptable value for the second argument to
9642 |copy_objects|.  Given a non-null graphical object list, |skip_1component|
9643 skips past one picture component, where a ``picture component'' is a single
9644 graphical object, or a start bounds or start clip object and everything up
9645 through the matching stop bounds or stop clip object.  The macro version avoids
9646 procedure call overhead and error handling: |skip_component(p)(e)| advances |p|
9647 unless |p| points to a stop bounds or stop clip node, in which case it executes
9648 |e| instead.
9649
9650 @d skip_component(A)
9651     if ( ! is_start_or_stop((A)) ) (A)=link((A));
9652     else if ( ! is_stop((A)) ) (A)=mp_skip_1component(mp, (A));
9653     else 
9654
9655 @c 
9656 pointer mp_skip_1component (MP mp,pointer p) {
9657   integer lev; /* current nesting level */
9658   lev=0;
9659   do {  
9660    if ( is_start_or_stop(p) ) {
9661      if ( is_stop(p) ) decr(lev);  else incr(lev);
9662    }
9663    p=link(p);
9664   } while (lev!=0);
9665   return p;
9666 }
9667
9668 @ Here is a diagnostic routine for printing an edge structure in symbolic form.
9669
9670 @<Declare subroutines for printing expressions@>=
9671 @<Declare subroutines needed by |print_edges|@>
9672 void mp_print_edges (MP mp,pointer h, const char *s, boolean nuline) {
9673   pointer p;  /* a graphical object to be printed */
9674   pointer hh,pp;  /* temporary pointers */
9675   scaled scf;  /* a scale factor for the dash pattern */
9676   boolean ok_to_dash;  /* |false| for polygonal pen strokes */
9677   mp_print_diagnostic(mp, "Edge structure",s,nuline);
9678   p=dummy_loc(h);
9679   while ( link(p)!=null ) { 
9680     p=link(p);
9681     mp_print_ln(mp);
9682     switch (type(p)) {
9683       @<Cases for printing graphical object node |p|@>;
9684     default: 
9685           mp_print(mp, "[unknown object type!]");
9686           break;
9687     }
9688   }
9689   mp_print_nl(mp, "End edges");
9690   if ( p!=obj_tail(h) ) mp_print(mp, "?");
9691 @.End edges?@>
9692   mp_end_diagnostic(mp, true);
9693 }
9694
9695 @ @<Cases for printing graphical object node |p|@>=
9696 case mp_fill_code: 
9697   mp_print(mp, "Filled contour ");
9698   mp_print_obj_color(mp, p);
9699   mp_print_char(mp, ':'); mp_print_ln(mp);
9700   mp_pr_path(mp, path_p(p)); mp_print_ln(mp);
9701   if ( (pen_p(p)!=null) ) {
9702     @<Print join type for graphical object |p|@>;
9703     mp_print(mp, " with pen"); mp_print_ln(mp);
9704     mp_pr_pen(mp, pen_p(p));
9705   }
9706   break;
9707
9708 @ @<Print join type for graphical object |p|@>=
9709 switch (ljoin_val(p)) {
9710 case 0:
9711   mp_print(mp, "mitered joins limited ");
9712   mp_print_scaled(mp, miterlim_val(p));
9713   break;
9714 case 1:
9715   mp_print(mp, "round joins");
9716   break;
9717 case 2:
9718   mp_print(mp, "beveled joins");
9719   break;
9720 default: 
9721   mp_print(mp, "?? joins");
9722 @.??@>
9723   break;
9724 }
9725
9726 @ For stroked nodes, we need to print |lcap_val(p)| as well.
9727
9728 @<Print join and cap types for stroked node |p|@>=
9729 switch (lcap_val(p)) {
9730 case 0:mp_print(mp, "butt"); break;
9731 case 1:mp_print(mp, "round"); break;
9732 case 2:mp_print(mp, "square"); break;
9733 default: mp_print(mp, "??"); break;
9734 @.??@>
9735 }
9736 mp_print(mp, " ends, ");
9737 @<Print join type for graphical object |p|@>
9738
9739 @ Here is a routine that prints the color of a graphical object if it isn't
9740 black (the default color).
9741
9742 @<Declare subroutines needed by |print_edges|@>=
9743 @<Declare a procedure called |print_compact_node|@>
9744 void mp_print_obj_color (MP mp,pointer p) { 
9745   if ( color_model(p)==mp_grey_model ) {
9746     if ( grey_val(p)>0 ) { 
9747       mp_print(mp, "greyed ");
9748       mp_print_compact_node(mp, obj_grey_loc(p),1);
9749     };
9750   } else if ( color_model(p)==mp_cmyk_model ) {
9751     if ( (cyan_val(p)>0) || (magenta_val(p)>0) || 
9752          (yellow_val(p)>0) || (black_val(p)>0) ) { 
9753       mp_print(mp, "processcolored ");
9754       mp_print_compact_node(mp, obj_cyan_loc(p),4);
9755     };
9756   } else if ( color_model(p)==mp_rgb_model ) {
9757     if ( (red_val(p)>0) || (green_val(p)>0) || (blue_val(p)>0) ) { 
9758       mp_print(mp, "colored "); 
9759       mp_print_compact_node(mp, obj_red_loc(p),3);
9760     };
9761   }
9762 }
9763
9764 @ We also need a procedure for printing consecutive scaled values as if they
9765 were a known big node.
9766
9767 @<Declare a procedure called |print_compact_node|@>=
9768 void mp_print_compact_node (MP mp,pointer p, small_number k) {
9769   pointer q;  /* last location to print */
9770   q=p+k-1;
9771   mp_print_char(mp, '(');
9772   while ( p<=q ){ 
9773     mp_print_scaled(mp, mp->mem[p].sc);
9774     if ( p<q ) mp_print_char(mp, ',');
9775     incr(p);
9776   }
9777   mp_print_char(mp, ')');
9778 }
9779
9780 @ @<Cases for printing graphical object node |p|@>=
9781 case mp_stroked_code: 
9782   mp_print(mp, "Filled pen stroke ");
9783   mp_print_obj_color(mp, p);
9784   mp_print_char(mp, ':'); mp_print_ln(mp);
9785   mp_pr_path(mp, path_p(p));
9786   if ( dash_p(p)!=null ) { 
9787     mp_print_nl(mp, "dashed (");
9788     @<Finish printing the dash pattern that |p| refers to@>;
9789   }
9790   mp_print_ln(mp);
9791   @<Print join and cap types for stroked node |p|@>;
9792   mp_print(mp, " with pen"); mp_print_ln(mp);
9793   if ( pen_p(p)==null ) mp_print(mp, "???"); /* shouldn't happen */
9794 @.???@>
9795   else mp_pr_pen(mp, pen_p(p));
9796   break;
9797
9798 @ Normally, the  |dash_list| field in an edge header is set to |null_dash|
9799 when it is not known to define a suitable dash pattern.  This is disallowed
9800 here because the |dash_p| field should never point to such an edge header.
9801 Note that memory is allocated for |start_x(null_dash)| and we are free to
9802 give it any convenient value.
9803
9804 @<Finish printing the dash pattern that |p| refers to@>=
9805 ok_to_dash=pen_is_elliptical(pen_p(p));
9806 if ( ! ok_to_dash ) scf=unity; else scf=dash_scale(p);
9807 hh=dash_p(p);
9808 pp=dash_list(hh);
9809 if ( (pp==null_dash) || (dash_y(hh)<0) ) {
9810   mp_print(mp, " ??");
9811 } else { start_x(null_dash)=start_x(pp)+dash_y(hh);
9812   while ( pp!=null_dash ) { 
9813     mp_print(mp, "on ");
9814     mp_print_scaled(mp, mp_take_scaled(mp, stop_x(pp)-start_x(pp),scf));
9815     mp_print(mp, " off ");
9816     mp_print_scaled(mp, mp_take_scaled(mp, start_x(link(pp))-stop_x(pp),scf));
9817     pp = link(pp);
9818     if ( pp!=null_dash ) mp_print_char(mp, ' ');
9819   }
9820   mp_print(mp, ") shifted ");
9821   mp_print_scaled(mp, -mp_take_scaled(mp, mp_dash_offset(mp, hh),scf));
9822   if ( ! ok_to_dash || (dash_y(hh)==0) ) mp_print(mp, " (this will be ignored)");
9823 }
9824
9825 @ @<Declare subroutines needed by |print_edges|@>=
9826 scaled mp_dash_offset (MP mp,pointer h) {
9827   scaled x;  /* the answer */
9828   if (dash_list(h)==null_dash || dash_y(h)<0) mp_confusion(mp, "dash0");
9829 @:this can't happen dash0}{\quad dash0@>
9830   if ( dash_y(h)==0 ) {
9831     x=0; 
9832   } else { 
9833     x=-(start_x(dash_list(h)) % dash_y(h));
9834     if ( x<0 ) x=x+dash_y(h);
9835   }
9836   return x;
9837 }
9838
9839 @ @<Cases for printing graphical object node |p|@>=
9840 case mp_text_code: 
9841   mp_print_char(mp, '"'); mp_print_str(mp,text_p(p));
9842   mp_print(mp, "\" infont \""); mp_print(mp, mp->font_name[font_n(p)]);
9843   mp_print_char(mp, '"'); mp_print_ln(mp);
9844   mp_print_obj_color(mp, p);
9845   mp_print(mp, "transformed ");
9846   mp_print_compact_node(mp, text_tx_loc(p),6);
9847   break;
9848
9849 @ @<Cases for printing graphical object node |p|@>=
9850 case mp_start_clip_code: 
9851   mp_print(mp, "clipping path:");
9852   mp_print_ln(mp);
9853   mp_pr_path(mp, path_p(p));
9854   break;
9855 case mp_stop_clip_code: 
9856   mp_print(mp, "stop clipping");
9857   break;
9858
9859 @ @<Cases for printing graphical object node |p|@>=
9860 case mp_start_bounds_code: 
9861   mp_print(mp, "setbounds path:");
9862   mp_print_ln(mp);
9863   mp_pr_path(mp, path_p(p));
9864   break;
9865 case mp_stop_bounds_code: 
9866   mp_print(mp, "end of setbounds");
9867   break;
9868
9869 @ To initialize the |dash_list| field in an edge header~|h|, we need a
9870 subroutine that scans an edge structure and tries to interpret it as a dash
9871 pattern.  This can only be done when there are no filled regions or clipping
9872 paths and all the pen strokes have the same color.  The first step is to let
9873 $y_0$ be the initial $y$~coordinate of the first pen stroke.  Then we implicitly
9874 project all the pen stroke paths onto the line $y=y_0$ and require that there
9875 be no retracing.  If the resulting paths cover a range of $x$~coordinates of
9876 length $\Delta x$, we set |dash_y(h)| to the length of the dash pattern by
9877 finding the maximum of $\Delta x$ and the absolute value of~$y_0$.
9878
9879 @c @<Declare a procedure called |x_retrace_error|@>
9880 pointer mp_make_dashes (MP mp,pointer h) { /* returns |h| or |null| */
9881   pointer p;  /* this scans the stroked nodes in the object list */
9882   pointer p0;  /* if not |null| this points to the first stroked node */
9883   pointer pp,qq,rr;  /* pointers into |path_p(p)| */
9884   pointer d,dd;  /* pointers used to create the dash list */
9885   scaled y0;
9886   @<Other local variables in |make_dashes|@>;
9887   y0=0;  /* the initial $y$ coordinate */
9888   if ( dash_list(h)!=null_dash ) 
9889         return h;
9890   p0=null;
9891   p=link(dummy_loc(h));
9892   while ( p!=null ) { 
9893     if ( type(p)!=mp_stroked_code ) {
9894       @<Compain that the edge structure contains a node of the wrong type
9895         and |goto not_found|@>;
9896     }
9897     pp=path_p(p);
9898     if ( p0==null ){ p0=p; y0=y_coord(pp);  };
9899     @<Make |d| point to a new dash node created from stroke |p| and path |pp|
9900       or |goto not_found| if there is an error@>;
9901     @<Insert |d| into the dash list and |goto not_found| if there is an error@>;
9902     p=link(p);
9903   }
9904   if ( dash_list(h)==null_dash ) 
9905     goto NOT_FOUND; /* No error message */
9906   @<Scan |dash_list(h)| and deal with any dashes that are themselves dashed@>;
9907   @<Set |dash_y(h)| and merge the first and last dashes if necessary@>;
9908   return h;
9909 NOT_FOUND: 
9910   @<Flush the dash list, recycle |h| and return |null|@>;
9911 }
9912
9913 @ @<Compain that the edge structure contains a node of the wrong type...@>=
9914
9915 print_err("Picture is too complicated to use as a dash pattern");
9916 help3("When you say `dashed p', picture p should not contain any")
9917   ("text, filled regions, or clipping paths.  This time it did")
9918   ("so I'll just make it a solid line instead.");
9919 mp_put_get_error(mp);
9920 goto NOT_FOUND;
9921 }
9922
9923 @ A similar error occurs when monotonicity fails.
9924
9925 @<Declare a procedure called |x_retrace_error|@>=
9926 void mp_x_retrace_error (MP mp) { 
9927 print_err("Picture is too complicated to use as a dash pattern");
9928 help3("When you say `dashed p', every path in p should be monotone")
9929   ("in x and there must be no overlapping.  This failed")
9930   ("so I'll just make it a solid line instead.");
9931 mp_put_get_error(mp);
9932 }
9933
9934 @ We stash |p| in |info(d)| if |dash_p(p)<>0| so that subsequent processing can
9935 handle the case where the pen stroke |p| is itself dashed.
9936
9937 @<Make |d| point to a new dash node created from stroke |p| and path...@>=
9938 @<Make sure |p| and |p0| are the same color and |goto not_found| if there is
9939   an error@>;
9940 rr=pp;
9941 if ( link(pp)!=pp ) {
9942   do {  
9943     qq=rr; rr=link(rr);
9944     @<Check for retracing between knots |qq| and |rr| and |goto not_found|
9945       if there is a problem@>;
9946   } while (right_type(rr)!=mp_endpoint);
9947 }
9948 d=mp_get_node(mp, dash_node_size);
9949 if ( dash_p(p)==0 ) info(d)=0;  else info(d)=p;
9950 if ( x_coord(pp)<x_coord(rr) ) { 
9951   start_x(d)=x_coord(pp);
9952   stop_x(d)=x_coord(rr);
9953 } else { 
9954   start_x(d)=x_coord(rr);
9955   stop_x(d)=x_coord(pp);
9956 }
9957
9958 @ We also need to check for the case where the segment from |qq| to |rr| is
9959 monotone in $x$ but is reversed relative to the path from |pp| to |qq|.
9960
9961 @<Check for retracing between knots |qq| and |rr| and |goto not_found|...@>=
9962 x0=x_coord(qq);
9963 x1=right_x(qq);
9964 x2=left_x(rr);
9965 x3=x_coord(rr);
9966 if ( (x0>x1) || (x1>x2) || (x2>x3) ) {
9967   if ( (x0<x1) || (x1<x2) || (x2<x3) ) {
9968     if ( mp_ab_vs_cd(mp, x2-x1,x2-x1,x1-x0,x3-x2)>0 ) {
9969       mp_x_retrace_error(mp); goto NOT_FOUND;
9970     }
9971   }
9972 }
9973 if ( (x_coord(pp)>x0) || (x0>x3) ) {
9974   if ( (x_coord(pp)<x0) || (x0<x3) ) {
9975     mp_x_retrace_error(mp); goto NOT_FOUND;
9976   }
9977 }
9978
9979 @ @<Other local variables in |make_dashes|@>=
9980   scaled x0,x1,x2,x3;  /* $x$ coordinates of the segment from |qq| to |rr| */
9981
9982 @ @<Make sure |p| and |p0| are the same color and |goto not_found|...@>=
9983 if ( (red_val(p)!=red_val(p0)) || (black_val(p)!=black_val(p0)) ||
9984   (green_val(p)!=green_val(p0)) || (blue_val(p)!=blue_val(p0)) ) {
9985   print_err("Picture is too complicated to use as a dash pattern");
9986   help3("When you say `dashed p', everything in picture p should")
9987     ("be the same color.  I can\'t handle your color changes")
9988     ("so I'll just make it a solid line instead.");
9989   mp_put_get_error(mp);
9990   goto NOT_FOUND;
9991 }
9992
9993 @ @<Insert |d| into the dash list and |goto not_found| if there is an error@>=
9994 start_x(null_dash)=stop_x(d);
9995 dd=h; /* this makes |link(dd)=dash_list(h)| */
9996 while ( start_x(link(dd))<stop_x(d) )
9997   dd=link(dd);
9998 if ( dd!=h ) {
9999   if ( (stop_x(dd)>start_x(d)) )
10000     { mp_x_retrace_error(mp); goto NOT_FOUND;  };
10001 }
10002 link(d)=link(dd);
10003 link(dd)=d
10004
10005 @ @<Set |dash_y(h)| and merge the first and last dashes if necessary@>=
10006 d=dash_list(h);
10007 while ( (link(d)!=null_dash) )
10008   d=link(d);
10009 dd=dash_list(h);
10010 dash_y(h)=stop_x(d)-start_x(dd);
10011 if ( abs(y0)>dash_y(h) ) {
10012   dash_y(h)=abs(y0);
10013 } else if ( d!=dd ) { 
10014   dash_list(h)=link(dd);
10015   stop_x(d)=stop_x(dd)+dash_y(h);
10016   mp_free_node(mp, dd,dash_node_size);
10017 }
10018
10019 @ We get here when the argument is a null picture or when there is an error.
10020 Recovering from an error involves making |dash_list(h)| empty to indicate
10021 that |h| is not known to be a valid dash pattern.  We also dereference |h|
10022 since it is not being used for the return value.
10023
10024 @<Flush the dash list, recycle |h| and return |null|@>=
10025 mp_flush_dash_list(mp, h);
10026 delete_edge_ref(h);
10027 return null
10028
10029 @ Having carefully saved the dashed stroked nodes in the
10030 corresponding dash nodes, we must be prepared to break up these dashes into
10031 smaller dashes.
10032
10033 @<Scan |dash_list(h)| and deal with any dashes that are themselves dashed@>=
10034 d=h;  /* now |link(d)=dash_list(h)| */
10035 while ( link(d)!=null_dash ) {
10036   ds=info(link(d));
10037   if ( ds==null ) { 
10038     d=link(d);
10039   } else {
10040     hh=dash_p(ds);
10041     hsf=dash_scale(ds);
10042     if ( (hh==null) ) mp_confusion(mp, "dash1");
10043 @:this can't happen dash0}{\quad dash1@>
10044     if ( dash_y(hh)==0 ) {
10045       d=link(d);
10046     } else { 
10047       if ( dash_list(hh)==null ) mp_confusion(mp, "dash1");
10048 @:this can't happen dash0}{\quad dash1@>
10049       @<Replace |link(d)| by a dashed version as determined by edge header
10050           |hh| and scale factor |ds|@>;
10051     }
10052   }
10053 }
10054
10055 @ @<Other local variables in |make_dashes|@>=
10056 pointer dln;  /* |link(d)| */
10057 pointer hh;  /* an edge header that tells how to break up |dln| */
10058 scaled hsf;  /* the dash pattern from |hh| gets scaled by this */
10059 pointer ds;  /* the stroked node from which |hh| and |hsf| are derived */
10060 scaled xoff;  /* added to $x$ values in |dash_list(hh)| to match |dln| */
10061
10062 @ @<Replace |link(d)| by a dashed version as determined by edge header...@>=
10063 dln=link(d);
10064 dd=dash_list(hh);
10065 xoff=start_x(dln)-mp_take_scaled(mp, hsf,start_x(dd))-
10066         mp_take_scaled(mp, hsf,mp_dash_offset(mp, hh));
10067 start_x(null_dash)=mp_take_scaled(mp, hsf,start_x(dd))
10068                    +mp_take_scaled(mp, hsf,dash_y(hh));
10069 stop_x(null_dash)=start_x(null_dash);
10070 @<Advance |dd| until finding the first dash that overlaps |dln| when
10071   offset by |xoff|@>;
10072 while ( start_x(dln)<=stop_x(dln) ) {
10073   @<If |dd| has `fallen off the end', back up to the beginning and fix |xoff|@>;
10074   @<Insert a dash between |d| and |dln| for the overlap with the offset version
10075     of |dd|@>;
10076   dd=link(dd);
10077   start_x(dln)=xoff+mp_take_scaled(mp, hsf,start_x(dd));
10078 }
10079 link(d)=link(dln);
10080 mp_free_node(mp, dln,dash_node_size)
10081
10082 @ The name of this module is a bit of a lie because we just find the
10083 first |dd| where |take_scaled (hsf, stop_x(dd))| is large enough to make an
10084 overlap possible.  It could be that the unoffset version of dash |dln| falls
10085 in the gap between |dd| and its predecessor.
10086
10087 @<Advance |dd| until finding the first dash that overlaps |dln| when...@>=
10088 while ( xoff+mp_take_scaled(mp, hsf,stop_x(dd))<start_x(dln) ) {
10089   dd=link(dd);
10090 }
10091
10092 @ @<If |dd| has `fallen off the end', back up to the beginning and fix...@>=
10093 if ( dd==null_dash ) { 
10094   dd=dash_list(hh);
10095   xoff=xoff+mp_take_scaled(mp, hsf,dash_y(hh));
10096 }
10097
10098 @ At this point we already know that
10099 |start_x(dln)<=xoff+take_scaled(hsf,stop_x(dd))|.
10100
10101 @<Insert a dash between |d| and |dln| for the overlap with the offset...@>=
10102 if ( (xoff+mp_take_scaled(mp, hsf,start_x(dd)))<=stop_x(dln) ) {
10103   link(d)=mp_get_node(mp, dash_node_size);
10104   d=link(d);
10105   link(d)=dln;
10106   if ( start_x(dln)>(xoff+mp_take_scaled(mp, hsf,start_x(dd))))
10107     start_x(d)=start_x(dln);
10108   else 
10109     start_x(d)=xoff+mp_take_scaled(mp, hsf,start_x(dd));
10110   if ( stop_x(dln)<(xoff+mp_take_scaled(mp, hsf,stop_x(dd)))) 
10111     stop_x(d)=stop_x(dln);
10112   else 
10113     stop_x(d)=xoff+mp_take_scaled(mp, hsf,stop_x(dd));
10114 }
10115
10116 @ The next major task is to update the bounding box information in an edge
10117 header~|h|. This is done via a procedure |adjust_bbox| that enlarges an edge
10118 header's bounding box to accommodate the box computed by |path_bbox| or
10119 |pen_bbox|. (This is stored in global variables |minx|, |miny|, |maxx|, and
10120 |maxy|.)
10121
10122 @c void mp_adjust_bbox (MP mp,pointer h) { 
10123   if ( minx<minx_val(h) ) minx_val(h)=minx;
10124   if ( miny<miny_val(h) ) miny_val(h)=miny;
10125   if ( maxx>maxx_val(h) ) maxx_val(h)=maxx;
10126   if ( maxy>maxy_val(h) ) maxy_val(h)=maxy;
10127 }
10128
10129 @ Here is a special routine for updating the bounding box information in
10130 edge header~|h| to account for the squared-off ends of a non-cyclic path~|p|
10131 that is to be stroked with the pen~|pp|.
10132
10133 @c void mp_box_ends (MP mp, pointer p, pointer pp, pointer h) {
10134   pointer q;  /* a knot node adjacent to knot |p| */
10135   fraction dx,dy;  /* a unit vector in the direction out of the path at~|p| */
10136   scaled d;  /* a factor for adjusting the length of |(dx,dy)| */
10137   scaled z;  /* a coordinate being tested against the bounding box */
10138   scaled xx,yy;  /* the extreme pen vertex in the |(dx,dy)| direction */
10139   integer i; /* a loop counter */
10140   if ( right_type(p)!=mp_endpoint ) { 
10141     q=link(p);
10142     while (1) { 
10143       @<Make |(dx,dy)| the final direction for the path segment from
10144         |q| to~|p|; set~|d|@>;
10145       d=mp_pyth_add(mp, dx,dy);
10146       if ( d>0 ) { 
10147          @<Normalize the direction |(dx,dy)| and find the pen offset |(xx,yy)|@>;
10148          for (i=1;i<= 2;i++) { 
10149            @<Use |(dx,dy)| to generate a vertex of the square end cap and
10150              update the bounding box to accommodate it@>;
10151            dx=-dx; dy=-dy; 
10152         }
10153       }
10154       if ( right_type(p)==mp_endpoint ) {
10155          return;
10156       } else {
10157         @<Advance |p| to the end of the path and make |q| the previous knot@>;
10158       } 
10159     }
10160   }
10161 }
10162
10163 @ @<Make |(dx,dy)| the final direction for the path segment from...@>=
10164 if ( q==link(p) ) { 
10165   dx=x_coord(p)-right_x(p);
10166   dy=y_coord(p)-right_y(p);
10167   if ( (dx==0)&&(dy==0) ) {
10168     dx=x_coord(p)-left_x(q);
10169     dy=y_coord(p)-left_y(q);
10170   }
10171 } else { 
10172   dx=x_coord(p)-left_x(p);
10173   dy=y_coord(p)-left_y(p);
10174   if ( (dx==0)&&(dy==0) ) {
10175     dx=x_coord(p)-right_x(q);
10176     dy=y_coord(p)-right_y(q);
10177   }
10178 }
10179 dx=x_coord(p)-x_coord(q);
10180 dy=y_coord(p)-y_coord(q)
10181
10182 @ @<Normalize the direction |(dx,dy)| and find the pen offset |(xx,yy)|@>=
10183 dx=mp_make_fraction(mp, dx,d);
10184 dy=mp_make_fraction(mp, dy,d);
10185 mp_find_offset(mp, -dy,dx,pp);
10186 xx=mp->cur_x; yy=mp->cur_y
10187
10188 @ @<Use |(dx,dy)| to generate a vertex of the square end cap and...@>=
10189 mp_find_offset(mp, dx,dy,pp);
10190 d=mp_take_fraction(mp, xx-mp->cur_x,dx)+mp_take_fraction(mp, yy-mp->cur_y,dy);
10191 if ( ((d<0)&&(i==1)) || ((d>0)&&(i==2))) 
10192   mp_confusion(mp, "box_ends");
10193 @:this can't happen box ends}{\quad\\{box\_ends}@>
10194 z=x_coord(p)+mp->cur_x+mp_take_fraction(mp, d,dx);
10195 if ( z<minx_val(h) ) minx_val(h)=z;
10196 if ( z>maxx_val(h) ) maxx_val(h)=z;
10197 z=y_coord(p)+mp->cur_y+mp_take_fraction(mp, d,dy);
10198 if ( z<miny_val(h) ) miny_val(h)=z;
10199 if ( z>maxy_val(h) ) maxy_val(h)=z
10200
10201 @ @<Advance |p| to the end of the path and make |q| the previous knot@>=
10202 do {  
10203   q=p;
10204   p=link(p);
10205 } while (right_type(p)!=mp_endpoint)
10206
10207 @ The major difficulty in finding the bounding box of an edge structure is the
10208 effect of clipping paths.  We treat them conservatively by only clipping to the
10209 clipping path's bounding box, but this still
10210 requires recursive calls to |set_bbox| in order to find the bounding box of
10211 @^recursion@>
10212 the objects to be clipped.  Such calls are distinguished by the fact that the
10213 boolean parameter |top_level| is false.
10214
10215 @c void mp_set_bbox (MP mp,pointer h, boolean top_level) {
10216   pointer p;  /* a graphical object being considered */
10217   scaled sminx,sminy,smaxx,smaxy;
10218   /* for saving the bounding box during recursive calls */
10219   scaled x0,x1,y0,y1;  /* temporary registers */
10220   integer lev;  /* nesting level for |mp_start_bounds_code| nodes */
10221   @<Wipe out any existing bounding box information if |bbtype(h)| is
10222   incompatible with |internal[mp_true_corners]|@>;
10223   while ( link(bblast(h))!=null ) { 
10224     p=link(bblast(h));
10225     bblast(h)=p;
10226     switch (type(p)) {
10227     case mp_stop_clip_code: 
10228       if ( top_level ) mp_confusion(mp, "bbox");  else return;
10229 @:this can't happen bbox}{\quad bbox@>
10230       break;
10231     @<Other cases for updating the bounding box based on the type of object |p|@>;
10232     } /* all cases are enumerated above */
10233   }
10234   if ( ! top_level ) mp_confusion(mp, "bbox");
10235 }
10236
10237 @ @<Internal library declarations@>=
10238 void mp_set_bbox (MP mp,pointer h, boolean top_level);
10239
10240 @ @<Wipe out any existing bounding box information if |bbtype(h)| is...@>=
10241 switch (bbtype(h)) {
10242 case no_bounds: 
10243   break;
10244 case bounds_set: 
10245   if ( mp->internal[mp_true_corners]>0 ) mp_init_bbox(mp, h);
10246   break;
10247 case bounds_unset: 
10248   if ( mp->internal[mp_true_corners]<=0 ) mp_init_bbox(mp, h);
10249   break;
10250 } /* there are no other cases */
10251
10252 @ @<Other cases for updating the bounding box...@>=
10253 case mp_fill_code: 
10254   mp_path_bbox(mp, path_p(p));
10255   if ( pen_p(p)!=null ) { 
10256     x0=minx; y0=miny;
10257     x1=maxx; y1=maxy;
10258     mp_pen_bbox(mp, pen_p(p));
10259     minx=minx+x0;
10260     miny=miny+y0;
10261     maxx=maxx+x1;
10262     maxy=maxy+y1;
10263   }
10264   mp_adjust_bbox(mp, h);
10265   break;
10266
10267 @ @<Other cases for updating the bounding box...@>=
10268 case mp_start_bounds_code: 
10269   if ( mp->internal[mp_true_corners]>0 ) {
10270     bbtype(h)=bounds_unset;
10271   } else { 
10272     bbtype(h)=bounds_set;
10273     mp_path_bbox(mp, path_p(p));
10274     mp_adjust_bbox(mp, h);
10275     @<Scan to the matching |mp_stop_bounds_code| node and update |p| and
10276       |bblast(h)|@>;
10277   }
10278   break;
10279 case mp_stop_bounds_code: 
10280   if ( mp->internal[mp_true_corners]<=0 ) mp_confusion(mp, "bbox2");
10281 @:this can't happen bbox2}{\quad bbox2@>
10282   break;
10283
10284 @ @<Scan to the matching |mp_stop_bounds_code| node and update |p| and...@>=
10285 lev=1;
10286 while ( lev!=0 ) { 
10287   if ( link(p)==null ) mp_confusion(mp, "bbox2");
10288 @:this can't happen bbox2}{\quad bbox2@>
10289   p=link(p);
10290   if ( type(p)==mp_start_bounds_code ) incr(lev);
10291   else if ( type(p)==mp_stop_bounds_code ) decr(lev);
10292 }
10293 bblast(h)=p
10294
10295 @ It saves a lot of grief here to be slightly conservative and not account for
10296 omitted parts of dashed lines.  We also don't worry about the material omitted
10297 when using butt end caps.  The basic computation is for round end caps and
10298 |box_ends| augments it for square end caps.
10299
10300 @<Other cases for updating the bounding box...@>=
10301 case mp_stroked_code: 
10302   mp_path_bbox(mp, path_p(p));
10303   x0=minx; y0=miny;
10304   x1=maxx; y1=maxy;
10305   mp_pen_bbox(mp, pen_p(p));
10306   minx=minx+x0;
10307   miny=miny+y0;
10308   maxx=maxx+x1;
10309   maxy=maxy+y1;
10310   mp_adjust_bbox(mp, h);
10311   if ( (left_type(path_p(p))==mp_endpoint)&&(lcap_val(p)==2) )
10312     mp_box_ends(mp, path_p(p), pen_p(p), h);
10313   break;
10314
10315 @ The height width and depth information stored in a text node determines a
10316 rectangle that needs to be transformed according to the transformation
10317 parameters stored in the text node.
10318
10319 @<Other cases for updating the bounding box...@>=
10320 case mp_text_code: 
10321   x1=mp_take_scaled(mp, txx_val(p),width_val(p));
10322   y0=mp_take_scaled(mp, txy_val(p),-depth_val(p));
10323   y1=mp_take_scaled(mp, txy_val(p),height_val(p));
10324   minx=tx_val(p);
10325   maxx=minx;
10326   if ( y0<y1 ) { minx=minx+y0; maxx=maxx+y1;  }
10327   else         { minx=minx+y1; maxx=maxx+y0;  }
10328   if ( x1<0 ) minx=minx+x1;  else maxx=maxx+x1;
10329   x1=mp_take_scaled(mp, tyx_val(p),width_val(p));
10330   y0=mp_take_scaled(mp, tyy_val(p),-depth_val(p));
10331   y1=mp_take_scaled(mp, tyy_val(p),height_val(p));
10332   miny=ty_val(p);
10333   maxy=miny;
10334   if ( y0<y1 ) { miny=miny+y0; maxy=maxy+y1;  }
10335   else         { miny=miny+y1; maxy=maxy+y0;  }
10336   if ( x1<0 ) miny=miny+x1;  else maxy=maxy+x1;
10337   mp_adjust_bbox(mp, h);
10338   break;
10339
10340 @ This case involves a recursive call that advances |bblast(h)| to the node of
10341 type |mp_stop_clip_code| that matches |p|.
10342
10343 @<Other cases for updating the bounding box...@>=
10344 case mp_start_clip_code: 
10345   mp_path_bbox(mp, path_p(p));
10346   x0=minx; y0=miny;
10347   x1=maxx; y1=maxy;
10348   sminx=minx_val(h); sminy=miny_val(h);
10349   smaxx=maxx_val(h); smaxy=maxy_val(h);
10350   @<Reinitialize the bounding box in header |h| and call |set_bbox| recursively
10351     starting at |link(p)|@>;
10352   @<Clip the bounding box in |h| to the rectangle given by |x0|, |x1|,
10353     |y0|, |y1|@>;
10354   minx=sminx; miny=sminy;
10355   maxx=smaxx; maxy=smaxy;
10356   mp_adjust_bbox(mp, h);
10357   break;
10358
10359 @ @<Reinitialize the bounding box in header |h| and call |set_bbox|...@>=
10360 minx_val(h)=el_gordo;
10361 miny_val(h)=el_gordo;
10362 maxx_val(h)=-el_gordo;
10363 maxy_val(h)=-el_gordo;
10364 mp_set_bbox(mp, h,false)
10365
10366 @ @<Clip the bounding box in |h| to the rectangle given by |x0|, |x1|,...@>=
10367 if ( minx_val(h)<x0 ) minx_val(h)=x0;
10368 if ( miny_val(h)<y0 ) miny_val(h)=y0;
10369 if ( maxx_val(h)>x1 ) maxx_val(h)=x1;
10370 if ( maxy_val(h)>y1 ) maxy_val(h)=y1
10371
10372 @* \[22] Finding an envelope.
10373 When \MP\ has a path and a polygonal pen, it needs to express the desired
10374 shape in terms of things \ps\ can understand.  The present task is to compute
10375 a new path that describes the region to be filled.  It is convenient to
10376 define this as a two step process where the first step is determining what
10377 offset to use for each segment of the path.
10378
10379 @ Given a pointer |c| to a cyclic path,
10380 and a pointer~|h| to the first knot of a pen polygon,
10381 the |offset_prep| routine changes the path into cubics that are
10382 associated with particular pen offsets. Thus if the cubic between |p|
10383 and~|q| is associated with the |k|th offset and the cubic between |q| and~|r|
10384 has offset |l| then |info(q)=zero_off+l-k|. (The constant |zero_off| is added
10385 to because |l-k| could be negative.)
10386
10387 After overwriting the type information with offset differences, we no longer
10388 have a true path so we refer to the knot list returned by |offset_prep| as an
10389 ``envelope spec.''
10390 @^envelope spec@>
10391 Since an envelope spec only determines relative changes in pen offsets,
10392 |offset_prep| sets a global variable |spec_offset| to the relative change from
10393 |h| to the first offset.
10394
10395 @d zero_off 16384 /* added to offset changes to make them positive */
10396
10397 @<Glob...@>=
10398 integer spec_offset; /* number of pen edges between |h| and the initial offset */
10399
10400 @ @c @<Declare subroutines needed by |offset_prep|@>
10401 pointer mp_offset_prep (MP mp,pointer c, pointer h) {
10402   halfword n; /* the number of vertices in the pen polygon */
10403   pointer c0,p,q,q0,r,w, ww; /* for list manipulation */
10404   integer k_needed; /* amount to be added to |info(p)| when it is computed */
10405   pointer w0; /* a pointer to pen offset to use just before |p| */
10406   scaled dxin,dyin; /* the direction into knot |p| */
10407   integer turn_amt; /* change in pen offsets for the current cubic */
10408   @<Other local variables for |offset_prep|@>;
10409   dx0=0; dy0=0;
10410   @<Initialize the pen size~|n|@>;
10411   @<Initialize the incoming direction and pen offset at |c|@>;
10412   p=c; c0=c; k_needed=0;
10413   do {  
10414     q=link(p);
10415     @<Split the cubic between |p| and |q|, if necessary, into cubics
10416       associated with single offsets, after which |q| should
10417       point to the end of the final such cubic@>;
10418   NOT_FOUND:
10419     @<Advance |p| to node |q|, removing any ``dead'' cubics that
10420       might have been introduced by the splitting process@>;
10421   } while (q!=c);
10422   @<Fix the offset change in |info(c)| and set |c| to the return value of
10423     |offset_prep|@>;
10424   return c;
10425 }
10426
10427 @ We shall want to keep track of where certain knots on the cyclic path
10428 wind up in the envelope spec.  It doesn't suffice just to keep pointers to
10429 knot nodes because some nodes are deleted while removing dead cubics.  Thus
10430 |offset_prep| updates the following pointers
10431
10432 @<Glob...@>=
10433 pointer spec_p1;
10434 pointer spec_p2; /* pointers to distinguished knots */
10435
10436 @ @<Set init...@>=
10437 mp->spec_p1=null; mp->spec_p2=null;
10438
10439 @ @<Initialize the pen size~|n|@>=
10440 n=0; p=h;
10441 do {  
10442   incr(n);
10443   p=link(p);
10444 } while (p!=h)
10445
10446 @ Since the true incoming direction isn't known yet, we just pick a direction
10447 consistent with the pen offset~|h|.  If this is wrong, it can be corrected
10448 later.
10449
10450 @<Initialize the incoming direction and pen offset at |c|@>=
10451 dxin=x_coord(link(h))-x_coord(knil(h));
10452 dyin=y_coord(link(h))-y_coord(knil(h));
10453 if ( (dxin==0)&&(dyin==0) ) {
10454   dxin=y_coord(knil(h))-y_coord(h);
10455   dyin=x_coord(h)-x_coord(knil(h));
10456 }
10457 w0=h
10458
10459 @ We must be careful not to remove the only cubic in a cycle.
10460
10461 But we must also be careful for another reason. If the user-supplied
10462 path starts with a set of degenerate cubics, the target node |q| can
10463 be collapsed to the initial node |p| which might be the same as the
10464 initial node |c| of the curve. This would cause the |offset_prep| routine
10465 to bail out too early, causing distress later on. (See for example
10466 the testcase reported by Bogus\l{}aw Jackowski in tracker id 267, case 52c
10467 on Sarovar.)
10468
10469 @<Advance |p| to node |q|, removing any ``dead'' cubics...@>=
10470 q0=q;
10471 do { 
10472   r=link(p);
10473   if ( x_coord(p)==right_x(p) && y_coord(p)==right_y(p) &&
10474        x_coord(p)==left_x(r)  && y_coord(p)==left_y(r) &&
10475        x_coord(p)==x_coord(r) && y_coord(p)==y_coord(r) &&
10476        r!=p ) {
10477       @<Remove the cubic following |p| and update the data structures
10478         to merge |r| into |p|@>;
10479   }
10480   p=r;
10481 } while (p!=q);
10482 /* Check if we removed too much */
10483 if ((q!=q0)&&(q!=c||c==c0))
10484   q = link(q)
10485
10486 @ @<Remove the cubic following |p| and update the data structures...@>=
10487 { k_needed=info(p)-zero_off;
10488   if ( r==q ) { 
10489     q=p;
10490   } else { 
10491     info(p)=k_needed+info(r);
10492     k_needed=0;
10493   };
10494   if ( r==c ) { info(p)=info(c); c=p; };
10495   if ( r==mp->spec_p1 ) mp->spec_p1=p;
10496   if ( r==mp->spec_p2 ) mp->spec_p2=p;
10497   r=p; mp_remove_cubic(mp, p);
10498 }
10499
10500 @ Not setting the |info| field of the newly created knot allows the splitting
10501 routine to work for paths.
10502
10503 @<Declare subroutines needed by |offset_prep|@>=
10504 void mp_split_cubic (MP mp,pointer p, fraction t) { /* splits the cubic after |p| */
10505   scaled v; /* an intermediate value */
10506   pointer q,r; /* for list manipulation */
10507   q=link(p); r=mp_get_node(mp, knot_node_size); link(p)=r; link(r)=q;
10508   originator(r)=mp_program_code;
10509   left_type(r)=mp_explicit; right_type(r)=mp_explicit;
10510   v=t_of_the_way(right_x(p),left_x(q));
10511   right_x(p)=t_of_the_way(x_coord(p),right_x(p));
10512   left_x(q)=t_of_the_way(left_x(q),x_coord(q));
10513   left_x(r)=t_of_the_way(right_x(p),v);
10514   right_x(r)=t_of_the_way(v,left_x(q));
10515   x_coord(r)=t_of_the_way(left_x(r),right_x(r));
10516   v=t_of_the_way(right_y(p),left_y(q));
10517   right_y(p)=t_of_the_way(y_coord(p),right_y(p));
10518   left_y(q)=t_of_the_way(left_y(q),y_coord(q));
10519   left_y(r)=t_of_the_way(right_y(p),v);
10520   right_y(r)=t_of_the_way(v,left_y(q));
10521   y_coord(r)=t_of_the_way(left_y(r),right_y(r));
10522 }
10523
10524 @ This does not set |info(p)| or |right_type(p)|.
10525
10526 @<Declare subroutines needed by |offset_prep|@>=
10527 void mp_remove_cubic (MP mp,pointer p) { /* removes the dead cubic following~|p| */
10528   pointer q; /* the node that disappears */
10529   q=link(p); link(p)=link(q);
10530   right_x(p)=right_x(q); right_y(p)=right_y(q);
10531   mp_free_node(mp, q,knot_node_size);
10532 }
10533
10534 @ Let $d\prec d'$ mean that the counter-clockwise angle from $d$ to~$d'$ is
10535 strictly between zero and $180^\circ$.  Then we can define $d\preceq d'$ to
10536 mean that the angle could be zero or $180^\circ$. If $w_k=(u_k,v_k)$ is the
10537 $k$th pen offset, the $k$th pen edge direction is defined by the formula
10538 $$d_k=(u\k-u_k,\,v\k-v_k).$$
10539 When listed by increasing $k$, these directions occur in counter-clockwise
10540 order so that $d_k\preceq d\k$ for all~$k$.
10541 The goal of |offset_prep| is to find an offset index~|k| to associate with
10542 each cubic, such that the direction $d(t)$ of the cubic satisfies
10543 $$d_{k-1}\preceq d(t)\preceq d_k\qquad\hbox{for $0\le t\le 1$.}\eqno(*)$$
10544 We may have to split a cubic into many pieces before each
10545 piece corresponds to a unique offset.
10546
10547 @<Split the cubic between |p| and |q|, if necessary, into cubics...@>=
10548 info(p)=zero_off+k_needed;
10549 k_needed=0;
10550 @<Prepare for derivative computations;
10551   |goto not_found| if the current cubic is dead@>;
10552 @<Find the initial direction |(dx,dy)|@>;
10553 @<Update |info(p)| and find the offset $w_k$ such that
10554   $d_{k-1}\preceq(\\{dx},\\{dy})\prec d_k$; also advance |w0| for
10555   the direction change at |p|@>;
10556 @<Find the final direction |(dxin,dyin)|@>;
10557 @<Decide on the net change in pen offsets and set |turn_amt|@>;
10558 @<Complete the offset splitting process@>;
10559 w0=mp_pen_walk(mp, w0,turn_amt)
10560
10561 @ @<Declare subroutines needed by |offset_prep|@>=
10562 pointer mp_pen_walk (MP mp,pointer w, integer k) {
10563   /* walk |k| steps around a pen from |w| */
10564   while ( k>0 ) { w=link(w); decr(k);  };
10565   while ( k<0 ) { w=knil(w); incr(k);  };
10566   return w;
10567 }
10568
10569 @ The direction of a cubic $B(z_0,z_1,z_2,z_3;t)=\bigl(x(t),y(t)\bigr)$ can be
10570 calculated from the quadratic polynomials
10571 ${1\over3}x'(t)=B(x_1-x_0,x_2-x_1,x_3-x_2;t)$ and
10572 ${1\over3}y'(t)=B(y_1-y_0,y_2-y_1,y_3-y_2;t)$.
10573 Since we may be calculating directions from several cubics
10574 split from the current one, it is desirable to do these calculations
10575 without losing too much precision. ``Scaled up'' values of the
10576 derivatives, which will be less tainted by accumulated errors than
10577 derivatives found from the cubics themselves, are maintained in
10578 local variables |x0|, |x1|, and |x2|, representing $X_0=2^l(x_1-x_0)$,
10579 $X_1=2^l(x_2-x_1)$, and $X_2=2^l(x_3-x_2)$; similarly |y0|, |y1|, and~|y2|
10580 represent $Y_0=2^l(y_1-y_0)$, $Y_1=2^l(y_2-y_1)$, and $Y_2=2^l(y_3-y_2)$.
10581
10582 @<Other local variables for |offset_prep|@>=
10583 integer x0,x1,x2,y0,y1,y2; /* representatives of derivatives */
10584 integer t0,t1,t2; /* coefficients of polynomial for slope testing */
10585 integer du,dv,dx,dy; /* for directions of the pen and the curve */
10586 integer dx0,dy0; /* initial direction for the first cubic in the curve */
10587 integer max_coef; /* used while scaling */
10588 integer x0a,x1a,x2a,y0a,y1a,y2a; /* intermediate values */
10589 fraction t; /* where the derivative passes through zero */
10590 fraction s; /* a temporary value */
10591
10592 @ @<Prepare for derivative computations...@>=
10593 x0=right_x(p)-x_coord(p);
10594 x2=x_coord(q)-left_x(q);
10595 x1=left_x(q)-right_x(p);
10596 y0=right_y(p)-y_coord(p); y2=y_coord(q)-left_y(q);
10597 y1=left_y(q)-right_y(p);
10598 max_coef=abs(x0);
10599 if ( abs(x1)>max_coef ) max_coef=abs(x1);
10600 if ( abs(x2)>max_coef ) max_coef=abs(x2);
10601 if ( abs(y0)>max_coef ) max_coef=abs(y0);
10602 if ( abs(y1)>max_coef ) max_coef=abs(y1);
10603 if ( abs(y2)>max_coef ) max_coef=abs(y2);
10604 if ( max_coef==0 ) goto NOT_FOUND;
10605 while ( max_coef<fraction_half ) {
10606   double(max_coef);
10607   double(x0); double(x1); double(x2);
10608   double(y0); double(y1); double(y2);
10609 }
10610
10611 @ Let us first solve a special case of the problem: Suppose we
10612 know an index~$k$ such that either (i)~$d(t)\succeq d_{k-1}$ for all~$t$
10613 and $d(0)\prec d_k$, or (ii)~$d(t)\preceq d_k$ for all~$t$ and
10614 $d(0)\succ d_{k-1}$.
10615 Then, in a sense, we're halfway done, since one of the two relations
10616 in $(*)$ is satisfied, and the other couldn't be satisfied for
10617 any other value of~|k|.
10618
10619 Actually, the conditions can be relaxed somewhat since a relation such as
10620 $d(t)\succeq d_{k-1}$ restricts $d(t)$ to a half plane when all that really
10621 matters is whether $d(t)$ crosses the ray in the $d_{k-1}$ direction from
10622 the origin.  The condition for case~(i) becomes $d_{k-1}\preceq d(0)\prec d_k$
10623 and $d(t)$ never crosses the $d_{k-1}$ ray in the clockwise direction.
10624 Case~(ii) is similar except $d(t)$ cannot cross the $d_k$ ray in the
10625 counterclockwise direction.
10626
10627 The |fin_offset_prep| subroutine solves the stated subproblem.
10628 It has a parameter called |rise| that is |1| in
10629 case~(i), |-1| in case~(ii). Parameters |x0| through |y2| represent
10630 the derivative of the cubic following |p|.
10631 The |w| parameter should point to offset~$w_k$ and |info(p)| should already
10632 be set properly.  The |turn_amt| parameter gives the absolute value of the
10633 overall net change in pen offsets.
10634
10635 @<Declare subroutines needed by |offset_prep|@>=
10636 void mp_fin_offset_prep (MP mp,pointer p, pointer w, integer 
10637   x0,integer x1, integer x2, integer y0, integer y1, integer y2, 
10638   integer rise, integer turn_amt)  {
10639   pointer ww; /* for list manipulation */
10640   scaled du,dv; /* for slope calculation */
10641   integer t0,t1,t2; /* test coefficients */
10642   fraction t; /* place where the derivative passes a critical slope */
10643   fraction s; /* slope or reciprocal slope */
10644   integer v; /* intermediate value for updating |x0..y2| */
10645   pointer q; /* original |link(p)| */
10646   q=link(p);
10647   while (1)  { 
10648     if ( rise>0 ) ww=link(w); /* a pointer to $w\k$ */
10649     else  ww=knil(w); /* a pointer to $w_{k-1}$ */
10650     @<Compute test coefficients |(t0,t1,t2)|
10651       for $d(t)$ versus $d_k$ or $d_{k-1}$@>;
10652     t=mp_crossing_point(mp, t0,t1,t2);
10653     if ( t>=fraction_one ) {
10654       if ( turn_amt>0 ) t=fraction_one;  else return;
10655     }
10656     @<Split the cubic at $t$,
10657       and split off another cubic if the derivative crosses back@>;
10658     w=ww;
10659   }
10660 }
10661
10662 @ We want $B(\\{t0},\\{t1},\\{t2};t)$ to be the dot product of $d(t)$ with a
10663 $-90^\circ$ rotation of the vector from |w| to |ww|.  This makes the resulting
10664 function cross from positive to negative when $d_{k-1}\preceq d(t)\preceq d_k$
10665 begins to fail.
10666
10667 @<Compute test coefficients |(t0,t1,t2)| for $d(t)$ versus...@>=
10668 du=x_coord(ww)-x_coord(w); dv=y_coord(ww)-y_coord(w);
10669 if ( abs(du)>=abs(dv) ) {
10670   s=mp_make_fraction(mp, dv,du);
10671   t0=mp_take_fraction(mp, x0,s)-y0;
10672   t1=mp_take_fraction(mp, x1,s)-y1;
10673   t2=mp_take_fraction(mp, x2,s)-y2;
10674   if ( du<0 ) { negate(t0); negate(t1); negate(t2);  }
10675 } else { 
10676   s=mp_make_fraction(mp, du,dv);
10677   t0=x0-mp_take_fraction(mp, y0,s);
10678   t1=x1-mp_take_fraction(mp, y1,s);
10679   t2=x2-mp_take_fraction(mp, y2,s);
10680   if ( dv<0 ) { negate(t0); negate(t1); negate(t2);  }
10681 }
10682 if ( t0<0 ) t0=0 /* should be positive without rounding error */
10683
10684 @ The curve has crossed $d_k$ or $d_{k-1}$; its initial segment satisfies
10685 $(*)$, and it might cross again and return towards $s_{k-1}$ or $s_k$,
10686 respectively, yielding another solution of $(*)$.
10687
10688 @<Split the cubic at $t$, and split off another...@>=
10689
10690 mp_split_cubic(mp, p,t); p=link(p); info(p)=zero_off+rise;
10691 decr(turn_amt);
10692 v=t_of_the_way(x0,x1); x1=t_of_the_way(x1,x2);
10693 x0=t_of_the_way(v,x1);
10694 v=t_of_the_way(y0,y1); y1=t_of_the_way(y1,y2);
10695 y0=t_of_the_way(v,y1);
10696 if ( turn_amt<0 ) {
10697   t1=t_of_the_way(t1,t2);
10698   if ( t1>0 ) t1=0; /* without rounding error, |t1| would be |<=0| */
10699   t=mp_crossing_point(mp, 0,-t1,-t2);
10700   if ( t>fraction_one ) t=fraction_one;
10701   incr(turn_amt);
10702   if ( (t==fraction_one)&&(link(p)!=q) ) {
10703     info(link(p))=info(link(p))-rise;
10704   } else { 
10705     mp_split_cubic(mp, p,t); info(link(p))=zero_off-rise;
10706     v=t_of_the_way(x1,x2); x1=t_of_the_way(x0,x1);
10707     x2=t_of_the_way(x1,v);
10708     v=t_of_the_way(y1,y2); y1=t_of_the_way(y0,y1);
10709     y2=t_of_the_way(y1,v);
10710   }
10711 }
10712 }
10713
10714 @ Now we must consider the general problem of |offset_prep|, when
10715 nothing is known about a given cubic. We start by finding its
10716 direction in the vicinity of |t=0|.
10717
10718 If $z'(t)=0$, the given cubic is numerically unstable but |offset_prep|
10719 has not yet introduced any more numerical errors.  Thus we can compute
10720 the true initial direction for the given cubic, even if it is almost
10721 degenerate.
10722
10723 @<Find the initial direction |(dx,dy)|@>=
10724 dx=x0; dy=y0;
10725 if ( dx==0 && dy==0 ) { 
10726   dx=x1; dy=y1;
10727   if ( dx==0 && dy==0 ) { 
10728     dx=x2; dy=y2;
10729   }
10730 }
10731 if ( p==c ) { dx0=dx; dy0=dy;  }
10732
10733 @ @<Find the final direction |(dxin,dyin)|@>=
10734 dxin=x2; dyin=y2;
10735 if ( dxin==0 && dyin==0 ) {
10736   dxin=x1; dyin=y1;
10737   if ( dxin==0 && dyin==0 ) {
10738     dxin=x0; dyin=y0;
10739   }
10740 }
10741
10742 @ The next step is to bracket the initial direction between consecutive
10743 edges of the pen polygon.  We must be careful to turn clockwise only if
10744 this makes the turn less than $180^\circ$. (A $180^\circ$ turn must be
10745 counter-clockwise in order to make \&{doublepath} envelopes come out
10746 @:double_path_}{\&{doublepath} primitive@>
10747 right.) This code depends on |w0| being the offset for |(dxin,dyin)|.
10748
10749 @<Update |info(p)| and find the offset $w_k$ such that...@>=
10750 turn_amt=mp_get_turn_amt(mp,w0,dx,dy,(mp_ab_vs_cd(mp, dy,dxin,dx,dyin)>=0));
10751 w=mp_pen_walk(mp, w0, turn_amt);
10752 w0=w;
10753 info(p)=info(p)+turn_amt
10754
10755 @ Decide how many pen offsets to go away from |w| in order to find the offset
10756 for |(dx,dy)|, going counterclockwise if |ccw| is |true|.  This assumes that
10757 |w| is the offset for some direction $(x',y')$ from which the angle to |(dx,dy)|
10758 in the sense determined by |ccw| is less than or equal to $180^\circ$.
10759
10760 If the pen polygon has only two edges, they could both be parallel
10761 to |(dx,dy)|.  In this case, we must be careful to stop after crossing the first
10762 such edge in order to avoid an infinite loop.
10763
10764 @<Declare subroutines needed by |offset_prep|@>=
10765 integer mp_get_turn_amt (MP mp,pointer w, scaled  dx,
10766                          scaled dy, boolean  ccw) {
10767   pointer ww; /* a neighbor of knot~|w| */
10768   integer s; /* turn amount so far */
10769   integer t; /* |ab_vs_cd| result */
10770   s=0;
10771   if ( ccw ) { 
10772     ww=link(w);
10773     do {  
10774       t=mp_ab_vs_cd(mp, dy,(x_coord(ww)-x_coord(w)),
10775                         dx,(y_coord(ww)-y_coord(w)));
10776       if ( t<0 ) break;
10777       incr(s);
10778       w=ww; ww=link(ww);
10779     } while (t>0);
10780   } else { 
10781     ww=knil(w);
10782     while ( mp_ab_vs_cd(mp, dy,(x_coord(w)-x_coord(ww)),
10783                             dx,(y_coord(w)-y_coord(ww))) < 0) { 
10784       decr(s);
10785       w=ww; ww=knil(ww);
10786     }
10787   }
10788   return s;
10789 }
10790
10791 @ When we're all done, the final offset is |w0| and the final curve direction
10792 is |(dxin,dyin)|.  With this knowledge of the incoming direction at |c|, we
10793 can correct |info(c)| which was erroneously based on an incoming offset
10794 of~|h|.
10795
10796 @d fix_by(A) info(c)=info(c)+(A)
10797
10798 @<Fix the offset change in |info(c)| and set |c| to the return value of...@>=
10799 mp->spec_offset=info(c)-zero_off;
10800 if ( link(c)==c ) {
10801   info(c)=zero_off+n;
10802 } else { 
10803   fix_by(k_needed);
10804   while ( w0!=h ) { fix_by(1); w0=link(w0);  };
10805   while ( info(c)<=zero_off-n ) fix_by(n);
10806   while ( info(c)>zero_off ) fix_by(-n);
10807   if ( (info(c)!=zero_off)&&(mp_ab_vs_cd(mp, dy0,dxin,dx0,dyin)>=0) ) fix_by(n);
10808 }
10809
10810 @ Finally we want to reduce the general problem to situations that
10811 |fin_offset_prep| can handle. We split the cubic into at most three parts
10812 with respect to $d_{k-1}$, and apply |fin_offset_prep| to each part.
10813
10814 @<Complete the offset splitting process@>=
10815 ww=knil(w);
10816 @<Compute test coeff...@>;
10817 @<Find the first |t| where $d(t)$ crosses $d_{k-1}$ or set
10818   |t:=fraction_one+1|@>;
10819 if ( t>fraction_one ) {
10820   mp_fin_offset_prep(mp, p,w,x0,x1,x2,y0,y1,y2,1,turn_amt);
10821 } else {
10822   mp_split_cubic(mp, p,t); r=link(p);
10823   x1a=t_of_the_way(x0,x1); x1=t_of_the_way(x1,x2);
10824   x2a=t_of_the_way(x1a,x1);
10825   y1a=t_of_the_way(y0,y1); y1=t_of_the_way(y1,y2);
10826   y2a=t_of_the_way(y1a,y1);
10827   mp_fin_offset_prep(mp, p,w,x0,x1a,x2a,y0,y1a,y2a,1,0); x0=x2a; y0=y2a;
10828   info(r)=zero_off-1;
10829   if ( turn_amt>=0 ) {
10830     t1=t_of_the_way(t1,t2);
10831     if ( t1>0 ) t1=0;
10832     t=mp_crossing_point(mp, 0,-t1,-t2);
10833     if ( t>fraction_one ) t=fraction_one;
10834     @<Split off another rising cubic for |fin_offset_prep|@>;
10835     mp_fin_offset_prep(mp, r,ww,x0,x1,x2,y0,y1,y2,-1,0);
10836   } else {
10837     mp_fin_offset_prep(mp, r,ww,x0,x1,x2,y0,y1,y2,-1,(-1-turn_amt));
10838   }
10839 }
10840
10841 @ @<Split off another rising cubic for |fin_offset_prep|@>=
10842 mp_split_cubic(mp, r,t); info(link(r))=zero_off+1;
10843 x1a=t_of_the_way(x1,x2); x1=t_of_the_way(x0,x1);
10844 x0a=t_of_the_way(x1,x1a);
10845 y1a=t_of_the_way(y1,y2); y1=t_of_the_way(y0,y1);
10846 y0a=t_of_the_way(y1,y1a);
10847 mp_fin_offset_prep(mp, link(r),w,x0a,x1a,x2,y0a,y1a,y2,1,turn_amt);
10848 x2=x0a; y2=y0a
10849
10850 @ At this point, the direction of the incoming pen edge is |(-du,-dv)|.
10851 When the component of $d(t)$ perpendicular to |(-du,-dv)| crosses zero, we
10852 need to decide whether the directions are parallel or antiparallel.  We
10853 can test this by finding the dot product of $d(t)$ and |(-du,-dv)|, but this
10854 should be avoided when the value of |turn_amt| already determines the
10855 answer.  If |t2<0|, there is one crossing and it is antiparallel only if
10856 |turn_amt>=0|.  If |turn_amt<0|, there should always be at least one
10857 crossing and the first crossing cannot be antiparallel.
10858
10859 @<Find the first |t| where $d(t)$ crosses $d_{k-1}$ or set...@>=
10860 t=mp_crossing_point(mp, t0,t1,t2);
10861 if ( turn_amt>=0 ) {
10862   if ( t2<0 ) {
10863     t=fraction_one+1;
10864   } else { 
10865     u0=t_of_the_way(x0,x1);
10866     u1=t_of_the_way(x1,x2);
10867     ss=mp_take_fraction(mp, -du,t_of_the_way(u0,u1));
10868     v0=t_of_the_way(y0,y1);
10869     v1=t_of_the_way(y1,y2);
10870     ss=ss+mp_take_fraction(mp, -dv,t_of_the_way(v0,v1));
10871     if ( ss<0 ) t=fraction_one+1;
10872   }
10873 } else if ( t>fraction_one ) {
10874   t=fraction_one;
10875 }
10876
10877 @ @<Other local variables for |offset_prep|@>=
10878 integer u0,u1,v0,v1; /* intermediate values for $d(t)$ calculation */
10879 integer ss = 0; /* the part of the dot product computed so far */
10880 int d_sign; /* sign of overall change in direction for this cubic */
10881
10882 @ If the cubic almost has a cusp, it is a numerically ill-conditioned
10883 problem to decide which way it loops around but that's OK as long we're
10884 consistent.  To make \&{doublepath} envelopes work properly, reversing
10885 the path should always change the sign of |turn_amt|.
10886
10887 @<Decide on the net change in pen offsets and set |turn_amt|@>=
10888 d_sign=mp_ab_vs_cd(mp, dx,dyin, dxin,dy);
10889 if ( d_sign==0 ) {
10890   @<Check rotation direction based on node position@>
10891 }
10892 if ( d_sign==0 ) {
10893   if ( dx==0 ) {
10894     if ( dy>0 ) d_sign=1;  else d_sign=-1;
10895   } else {
10896     if ( dx>0 ) d_sign=1;  else d_sign=-1; 
10897   }
10898 }
10899 @<Make |ss| negative if and only if the total change in direction is
10900   more than $180^\circ$@>;
10901 turn_amt=mp_get_turn_amt(mp, w, dxin, dyin, (d_sign>0));
10902 if ( ss<0 ) turn_amt=turn_amt-d_sign*n
10903
10904 @ We check rotation direction by looking at the vector connecting the current
10905 node with the next. If its angle with incoming and outgoing tangents has the
10906 same sign, we pick this as |d_sign|, since it means we have a flex, not a cusp.
10907 Otherwise we proceed to the cusp code.
10908
10909 @<Check rotation direction based on node position@>=
10910 u0=x_coord(q)-x_coord(p);
10911 u1=y_coord(q)-y_coord(p);
10912 d_sign = half(mp_ab_vs_cd(mp, dx, u1, u0, dy)+
10913   mp_ab_vs_cd(mp, u0, dyin, dxin, u1));
10914
10915 @ In order to be invariant under path reversal, the result of this computation
10916 should not change when |x0|, |y0|, $\ldots$ are all negated and |(x0,y0)| is
10917 then swapped with |(x2,y2)|.  We make use of the identities
10918 |take_fraction(-a,-b)=take_fraction(a,b)| and
10919 |t_of_the_way(-a,-b)=-(t_of_the_way(a,b))|.
10920
10921 @<Make |ss| negative if and only if the total change in direction is...@>=
10922 t0=half(mp_take_fraction(mp, x0,y2))-half(mp_take_fraction(mp, x2,y0));
10923 t1=half(mp_take_fraction(mp, x1,(y0+y2)))-half(mp_take_fraction(mp, y1,(x0+x2)));
10924 if ( t0==0 ) t0=d_sign; /* path reversal always negates |d_sign| */
10925 if ( t0>0 ) {
10926   t=mp_crossing_point(mp, t0,t1,-t0);
10927   u0=t_of_the_way(x0,x1);
10928   u1=t_of_the_way(x1,x2);
10929   v0=t_of_the_way(y0,y1);
10930   v1=t_of_the_way(y1,y2);
10931 } else { 
10932   t=mp_crossing_point(mp, -t0,t1,t0);
10933   u0=t_of_the_way(x2,x1);
10934   u1=t_of_the_way(x1,x0);
10935   v0=t_of_the_way(y2,y1);
10936   v1=t_of_the_way(y1,y0);
10937 }
10938 ss=mp_take_fraction(mp, (x0+x2),t_of_the_way(u0,u1))+
10939    mp_take_fraction(mp, (y0+y2),t_of_the_way(v0,v1))
10940
10941 @ Here's a routine that prints an envelope spec in symbolic form.  It assumes
10942 that the |cur_pen| has not been walked around to the first offset.
10943
10944 @c 
10945 void mp_print_spec (MP mp,pointer cur_spec, pointer cur_pen, const char *s) {
10946   pointer p,q; /* list traversal */
10947   pointer w; /* the current pen offset */
10948   mp_print_diagnostic(mp, "Envelope spec",s,true);
10949   p=cur_spec; w=mp_pen_walk(mp, cur_pen,mp->spec_offset);
10950   mp_print_ln(mp);
10951   mp_print_two(mp, x_coord(cur_spec),y_coord(cur_spec));
10952   mp_print(mp, " % beginning with offset ");
10953   mp_print_two(mp, x_coord(w),y_coord(w));
10954   do { 
10955     while (1) {  
10956       q=link(p);
10957       @<Print the cubic between |p| and |q|@>;
10958       p=q;
10959           if ((p==cur_spec) || (info(p)!=zero_off)) 
10960         break;
10961     }
10962     if ( info(p)!=zero_off ) {
10963       @<Update |w| as indicated by |info(p)| and print an explanation@>;
10964     }
10965   } while (p!=cur_spec);
10966   mp_print_nl(mp, " & cycle");
10967   mp_end_diagnostic(mp, true);
10968 }
10969
10970 @ @<Update |w| as indicated by |info(p)| and print an explanation@>=
10971
10972   w=mp_pen_walk(mp, w, (info(p)-zero_off));
10973   mp_print(mp, " % ");
10974   if ( info(p)>zero_off ) mp_print(mp, "counter");
10975   mp_print(mp, "clockwise to offset ");
10976   mp_print_two(mp, x_coord(w),y_coord(w));
10977 }
10978
10979 @ @<Print the cubic between |p| and |q|@>=
10980
10981   mp_print_nl(mp, "   ..controls ");
10982   mp_print_two(mp, right_x(p),right_y(p));
10983   mp_print(mp, " and ");
10984   mp_print_two(mp, left_x(q),left_y(q));
10985   mp_print_nl(mp, " ..");
10986   mp_print_two(mp, x_coord(q),y_coord(q));
10987 }
10988
10989 @ Once we have an envelope spec, the remaining task to construct the actual
10990 envelope by offsetting each cubic as determined by the |info| fields in
10991 the knots.  First we use |offset_prep| to convert the |c| into an envelope
10992 spec. Then we add the offsets so that |c| becomes a cyclic path that represents
10993 the envelope.
10994
10995 The |ljoin| and |miterlim| parameters control the treatment of points where the
10996 pen offset changes, and |lcap| controls the endpoints of a \&{doublepath}.
10997 The endpoints are easily located because |c| is given in undoubled form
10998 and then doubled in this procedure.  We use |spec_p1| and |spec_p2| to keep
10999 track of the endpoints and treat them like very sharp corners.
11000 Butt end caps are treated like beveled joins; round end caps are treated like
11001 round joins; and square end caps are achieved by setting |join_type:=3|.
11002
11003 None of these parameters apply to inside joins where the convolution tracing
11004 has retrograde lines.  In such cases we use a simple connect-the-endpoints
11005 approach that is achieved by setting |join_type:=2|.
11006
11007 @c @<Declare a function called |insert_knot|@>
11008 pointer mp_make_envelope (MP mp,pointer c, pointer h, small_number ljoin,
11009   small_number lcap, scaled miterlim) {
11010   pointer p,q,r,q0; /* for manipulating the path */
11011   int join_type=0; /* codes |0..3| for mitered, round, beveled, or square */
11012   pointer w,w0; /* the pen knot for the current offset */
11013   scaled qx,qy; /* unshifted coordinates of |q| */
11014   halfword k,k0; /* controls pen edge insertion */
11015   @<Other local variables for |make_envelope|@>;
11016   dxin=0; dyin=0; dxout=0; dyout=0;
11017   mp->spec_p1=null; mp->spec_p2=null;
11018   @<If endpoint, double the path |c|, and set |spec_p1| and |spec_p2|@>;
11019   @<Use |offset_prep| to compute the envelope spec then walk |h| around to
11020     the initial offset@>;
11021   w=h;
11022   p=c;
11023   do {  
11024     q=link(p); q0=q;
11025     qx=x_coord(q); qy=y_coord(q);
11026     k=info(q);
11027     k0=k; w0=w;
11028     if ( k!=zero_off ) {
11029       @<Set |join_type| to indicate how to handle offset changes at~|q|@>;
11030     }
11031     @<Add offset |w| to the cubic from |p| to |q|@>;
11032     while ( k!=zero_off ) { 
11033       @<Step |w| and move |k| one step closer to |zero_off|@>;
11034       if ( (join_type==1)||(k==zero_off) )
11035          q=mp_insert_knot(mp, q,qx+x_coord(w),qy+y_coord(w));
11036     };
11037     if ( q!=link(p) ) {
11038       @<Set |p=link(p)| and add knots between |p| and |q| as
11039         required by |join_type|@>;
11040     }
11041     p=q;
11042   } while (q0!=c);
11043   return c;
11044 }
11045
11046 @ @<Use |offset_prep| to compute the envelope spec then walk |h| around to...@>=
11047 c=mp_offset_prep(mp, c,h);
11048 if ( mp->internal[mp_tracing_specs]>0 ) 
11049   mp_print_spec(mp, c,h,"");
11050 h=mp_pen_walk(mp, h,mp->spec_offset)
11051
11052 @ Mitered and squared-off joins depend on path directions that are difficult to
11053 compute for degenerate cubics.  The envelope spec computed by |offset_prep| can
11054 have degenerate cubics only if the entire cycle collapses to a single
11055 degenerate cubic.  Setting |join_type:=2| in this case makes the computed
11056 envelope degenerate as well.
11057
11058 @<Set |join_type| to indicate how to handle offset changes at~|q|@>=
11059 if ( k<zero_off ) {
11060   join_type=2;
11061 } else {
11062   if ( (q!=mp->spec_p1)&&(q!=mp->spec_p2) ) join_type=ljoin;
11063   else if ( lcap==2 ) join_type=3;
11064   else join_type=2-lcap;
11065   if ( (join_type==0)||(join_type==3) ) {
11066     @<Set the incoming and outgoing directions at |q|; in case of
11067       degeneracy set |join_type:=2|@>;
11068     if ( join_type==0 ) {
11069       @<If |miterlim| is less than the secant of half the angle at |q|
11070         then set |join_type:=2|@>;
11071     }
11072   }
11073 }
11074
11075 @ @<If |miterlim| is less than the secant of half the angle at |q|...@>=
11076
11077   tmp=mp_take_fraction(mp, miterlim,fraction_half+
11078       half(mp_take_fraction(mp, dxin,dxout)+mp_take_fraction(mp, dyin,dyout)));
11079   if ( tmp<unity )
11080     if ( mp_take_scaled(mp, miterlim,tmp)<unity ) join_type=2;
11081 }
11082
11083 @ @<Other local variables for |make_envelope|@>=
11084 fraction dxin,dyin,dxout,dyout; /* directions at |q| when square or mitered */
11085 scaled tmp; /* a temporary value */
11086
11087 @ The coordinates of |p| have already been shifted unless |p| is the first
11088 knot in which case they get shifted at the very end.
11089
11090 @<Add offset |w| to the cubic from |p| to |q|@>=
11091 right_x(p)=right_x(p)+x_coord(w);
11092 right_y(p)=right_y(p)+y_coord(w);
11093 left_x(q)=left_x(q)+x_coord(w);
11094 left_y(q)=left_y(q)+y_coord(w);
11095 x_coord(q)=x_coord(q)+x_coord(w);
11096 y_coord(q)=y_coord(q)+y_coord(w);
11097 left_type(q)=mp_explicit;
11098 right_type(q)=mp_explicit
11099
11100 @ @<Step |w| and move |k| one step closer to |zero_off|@>=
11101 if ( k>zero_off ){ w=link(w); decr(k);  }
11102 else { w=knil(w); incr(k);  }
11103
11104 @ The cubic from |q| to the new knot at |(x,y)| becomes a line segment and
11105 the |right_x| and |right_y| fields of |r| are set from |q|.  This is done in
11106 case the cubic containing these control points is ``yet to be examined.''
11107
11108 @<Declare a function called |insert_knot|@>=
11109 pointer mp_insert_knot (MP mp,pointer q, scaled x, scaled y) {
11110   /* returns the inserted knot */
11111   pointer r; /* the new knot */
11112   r=mp_get_node(mp, knot_node_size);
11113   link(r)=link(q); link(q)=r;
11114   right_x(r)=right_x(q);
11115   right_y(r)=right_y(q);
11116   x_coord(r)=x;
11117   y_coord(r)=y;
11118   right_x(q)=x_coord(q);
11119   right_y(q)=y_coord(q);
11120   left_x(r)=x_coord(r);
11121   left_y(r)=y_coord(r);
11122   left_type(r)=mp_explicit;
11123   right_type(r)=mp_explicit;
11124   originator(r)=mp_program_code;
11125   return r;
11126 }
11127
11128 @ After setting |p:=link(p)|, either |join_type=1| or |q=link(p)|.
11129
11130 @<Set |p=link(p)| and add knots between |p| and |q| as...@>=
11131
11132   p=link(p);
11133   if ( (join_type==0)||(join_type==3) ) {
11134     if ( join_type==0 ) {
11135       @<Insert a new knot |r| between |p| and |q| as required for a mitered join@>
11136     } else {
11137       @<Make |r| the last of two knots inserted between |p| and |q| to form a
11138         squared join@>;
11139     }
11140     if ( r!=null ) { 
11141       right_x(r)=x_coord(r);
11142       right_y(r)=y_coord(r);
11143     }
11144   }
11145 }
11146
11147 @ For very small angles, adding a knot is unnecessary and would cause numerical
11148 problems, so we just set |r:=null| in that case.
11149
11150 @<Insert a new knot |r| between |p| and |q| as required for a mitered join@>=
11151
11152   det=mp_take_fraction(mp, dyout,dxin)-mp_take_fraction(mp, dxout,dyin);
11153   if ( abs(det)<26844 ) { 
11154      r=null; /* sine $<10^{-4}$ */
11155   } else { 
11156     tmp=mp_take_fraction(mp, x_coord(q)-x_coord(p),dyout)-
11157         mp_take_fraction(mp, y_coord(q)-y_coord(p),dxout);
11158     tmp=mp_make_fraction(mp, tmp,det);
11159     r=mp_insert_knot(mp, p,x_coord(p)+mp_take_fraction(mp, tmp,dxin),
11160       y_coord(p)+mp_take_fraction(mp, tmp,dyin));
11161   }
11162 }
11163
11164 @ @<Other local variables for |make_envelope|@>=
11165 fraction det; /* a determinant used for mitered join calculations */
11166
11167 @ @<Make |r| the last of two knots inserted between |p| and |q| to form a...@>=
11168
11169   ht_x=y_coord(w)-y_coord(w0);
11170   ht_y=x_coord(w0)-x_coord(w);
11171   while ( (abs(ht_x)<fraction_half)&&(abs(ht_y)<fraction_half) ) { 
11172     ht_x+=ht_x; ht_y+=ht_y;
11173   }
11174   @<Scan the pen polygon between |w0| and |w| and make |max_ht| the range dot
11175     product with |(ht_x,ht_y)|@>;
11176   tmp=mp_make_fraction(mp, max_ht,mp_take_fraction(mp, dxin,ht_x)+
11177                                   mp_take_fraction(mp, dyin,ht_y));
11178   r=mp_insert_knot(mp, p,x_coord(p)+mp_take_fraction(mp, tmp,dxin),
11179                          y_coord(p)+mp_take_fraction(mp, tmp,dyin));
11180   tmp=mp_make_fraction(mp, max_ht,mp_take_fraction(mp, dxout,ht_x)+
11181                                   mp_take_fraction(mp, dyout,ht_y));
11182   r=mp_insert_knot(mp, r,x_coord(q)+mp_take_fraction(mp, tmp,dxout),
11183                          y_coord(q)+mp_take_fraction(mp, tmp,dyout));
11184 }
11185
11186 @ @<Other local variables for |make_envelope|@>=
11187 fraction ht_x,ht_y; /* perpendicular to the segment from |p| to |q| */
11188 scaled max_ht; /* maximum height of the pen polygon above the |w0|-|w| line */
11189 halfword kk; /* keeps track of the pen vertices being scanned */
11190 pointer ww; /* the pen vertex being tested */
11191
11192 @ The dot product of the vector from |w0| to |ww| with |(ht_x,ht_y)| ranges
11193 from zero to |max_ht|.
11194
11195 @<Scan the pen polygon between |w0| and |w| and make |max_ht| the range...@>=
11196 max_ht=0;
11197 kk=zero_off;
11198 ww=w;
11199 while (1)  { 
11200   @<Step |ww| and move |kk| one step closer to |k0|@>;
11201   if ( kk==k0 ) break;
11202   tmp=mp_take_fraction(mp, (x_coord(ww)-x_coord(w0)),ht_x)+
11203       mp_take_fraction(mp, (y_coord(ww)-y_coord(w0)),ht_y);
11204   if ( tmp>max_ht ) max_ht=tmp;
11205 }
11206
11207
11208 @ @<Step |ww| and move |kk| one step closer to |k0|@>=
11209 if ( kk>k0 ) { ww=link(ww); decr(kk);  }
11210 else { ww=knil(ww); incr(kk);  }
11211
11212 @ @<If endpoint, double the path |c|, and set |spec_p1| and |spec_p2|@>=
11213 if ( left_type(c)==mp_endpoint ) { 
11214   mp->spec_p1=mp_htap_ypoc(mp, c);
11215   mp->spec_p2=mp->path_tail;
11216   originator(mp->spec_p1)=mp_program_code;
11217   link(mp->spec_p2)=link(mp->spec_p1);
11218   link(mp->spec_p1)=c;
11219   mp_remove_cubic(mp, mp->spec_p1);
11220   c=mp->spec_p1;
11221   if ( c!=link(c) ) {
11222     originator(mp->spec_p2)=mp_program_code;
11223     mp_remove_cubic(mp, mp->spec_p2);
11224   } else {
11225     @<Make |c| look like a cycle of length one@>;
11226   }
11227 }
11228
11229 @ @<Make |c| look like a cycle of length one@>=
11230
11231   left_type(c)=mp_explicit; right_type(c)=mp_explicit;
11232   left_x(c)=x_coord(c); left_y(c)=y_coord(c);
11233   right_x(c)=x_coord(c); right_y(c)=y_coord(c);
11234 }
11235
11236 @ In degenerate situations we might have to look at the knot preceding~|q|.
11237 That knot is |p| but if |p<>c|, its coordinates have already been offset by |w|.
11238
11239 @<Set the incoming and outgoing directions at |q|; in case of...@>=
11240 dxin=x_coord(q)-left_x(q);
11241 dyin=y_coord(q)-left_y(q);
11242 if ( (dxin==0)&&(dyin==0) ) {
11243   dxin=x_coord(q)-right_x(p);
11244   dyin=y_coord(q)-right_y(p);
11245   if ( (dxin==0)&&(dyin==0) ) {
11246     dxin=x_coord(q)-x_coord(p);
11247     dyin=y_coord(q)-y_coord(p);
11248     if ( p!=c ) { /* the coordinates of |p| have been offset by |w| */
11249       dxin=dxin+x_coord(w);
11250       dyin=dyin+y_coord(w);
11251     }
11252   }
11253 }
11254 tmp=mp_pyth_add(mp, dxin,dyin);
11255 if ( tmp==0 ) {
11256   join_type=2;
11257 } else { 
11258   dxin=mp_make_fraction(mp, dxin,tmp);
11259   dyin=mp_make_fraction(mp, dyin,tmp);
11260   @<Set the outgoing direction at |q|@>;
11261 }
11262
11263 @ If |q=c| then the coordinates of |r| and the control points between |q|
11264 and~|r| have already been offset by |h|.
11265
11266 @<Set the outgoing direction at |q|@>=
11267 dxout=right_x(q)-x_coord(q);
11268 dyout=right_y(q)-y_coord(q);
11269 if ( (dxout==0)&&(dyout==0) ) {
11270   r=link(q);
11271   dxout=left_x(r)-x_coord(q);
11272   dyout=left_y(r)-y_coord(q);
11273   if ( (dxout==0)&&(dyout==0) ) {
11274     dxout=x_coord(r)-x_coord(q);
11275     dyout=y_coord(r)-y_coord(q);
11276   }
11277 }
11278 if ( q==c ) {
11279   dxout=dxout-x_coord(h);
11280   dyout=dyout-y_coord(h);
11281 }
11282 tmp=mp_pyth_add(mp, dxout,dyout);
11283 if ( tmp==0 ) mp_confusion(mp, "degenerate spec");
11284 @:this can't happen degerate spec}{\quad degenerate spec@>
11285 dxout=mp_make_fraction(mp, dxout,tmp);
11286 dyout=mp_make_fraction(mp, dyout,tmp)
11287
11288 @* \[23] Direction and intersection times.
11289 A path of length $n$ is defined parametrically by functions $x(t)$ and
11290 $y(t)$, for |0<=t<=n|; we can regard $t$ as the ``time'' at which the path
11291 reaches the point $\bigl(x(t),y(t)\bigr)$.  In this section of the program
11292 we shall consider operations that determine special times associated with
11293 given paths: the first time that a path travels in a given direction, and
11294 a pair of times at which two paths cross each other.
11295
11296 @ Let's start with the easier task. The function |find_direction_time| is
11297 given a direction |(x,y)| and a path starting at~|h|. If the path never
11298 travels in direction |(x,y)|, the direction time will be~|-1|; otherwise
11299 it will be nonnegative.
11300
11301 Certain anomalous cases can arise: If |(x,y)=(0,0)|, so that the given
11302 direction is undefined, the direction time will be~0. If $\bigl(x'(t),
11303 y'(t)\bigr)=(0,0)$, so that the path direction is undefined, it will be
11304 assumed to match any given direction at time~|t|.
11305
11306 The routine solves this problem in nondegenerate cases by rotating the path
11307 and the given direction so that |(x,y)=(1,0)|; i.e., the main task will be
11308 to find when a given path first travels ``due east.''
11309
11310 @c 
11311 scaled mp_find_direction_time (MP mp,scaled x, scaled y, pointer h) {
11312   scaled max; /* $\max\bigl(\vert x\vert,\vert y\vert\bigr)$ */
11313   pointer p,q; /* for list traversal */
11314   scaled n; /* the direction time at knot |p| */
11315   scaled tt; /* the direction time within a cubic */
11316   @<Other local variables for |find_direction_time|@>;
11317   @<Normalize the given direction for better accuracy;
11318     but |return| with zero result if it's zero@>;
11319   n=0; p=h; phi=0;
11320   while (1) { 
11321     if ( right_type(p)==mp_endpoint ) break;
11322     q=link(p);
11323     @<Rotate the cubic between |p| and |q|; then
11324       |goto found| if the rotated cubic travels due east at some time |tt|;
11325       but |break| if an entire cyclic path has been traversed@>;
11326     p=q; n=n+unity;
11327   }
11328   return (-unity);
11329 FOUND: 
11330   return (n+tt);
11331 }
11332
11333 @ @<Normalize the given direction for better accuracy...@>=
11334 if ( abs(x)<abs(y) ) { 
11335   x=mp_make_fraction(mp, x,abs(y));
11336   if ( y>0 ) y=fraction_one; else y=-fraction_one;
11337 } else if ( x==0 ) { 
11338   return 0;
11339 } else  { 
11340   y=mp_make_fraction(mp, y,abs(x));
11341   if ( x>0 ) x=fraction_one; else x=-fraction_one;
11342 }
11343
11344 @ Since we're interested in the tangent directions, we work with the
11345 derivative $${1\over3}B'(x_0,x_1,x_2,x_3;t)=
11346 B(x_1-x_0,x_2-x_1,x_3-x_2;t)$$ instead of
11347 $B(x_0,x_1,x_2,x_3;t)$ itself. The derived coefficients are also scaled up
11348 in order to achieve better accuracy.
11349
11350 The given path may turn abruptly at a knot, and it might pass the critical
11351 tangent direction at such a time. Therefore we remember the direction |phi|
11352 in which the previous rotated cubic was traveling. (The value of |phi| will be
11353 undefined on the first cubic, i.e., when |n=0|.)
11354
11355 @<Rotate the cubic between |p| and |q|; then...@>=
11356 tt=0;
11357 @<Set local variables |x1,x2,x3| and |y1,y2,y3| to multiples of the control
11358   points of the rotated derivatives@>;
11359 if ( y1==0 ) if ( x1>=0 ) goto FOUND;
11360 if ( n>0 ) { 
11361   @<Exit to |found| if an eastward direction occurs at knot |p|@>;
11362   if ( p==h ) break;
11363   };
11364 if ( (x3!=0)||(y3!=0) ) phi=mp_n_arg(mp, x3,y3);
11365 @<Exit to |found| if the curve whose derivatives are specified by
11366   |x1,x2,x3,y1,y2,y3| travels eastward at some time~|tt|@>
11367
11368 @ @<Other local variables for |find_direction_time|@>=
11369 scaled x1,x2,x3,y1,y2,y3;  /* multiples of rotated derivatives */
11370 angle theta,phi; /* angles of exit and entry at a knot */
11371 fraction t; /* temp storage */
11372
11373 @ @<Set local variables |x1,x2,x3| and |y1,y2,y3| to multiples...@>=
11374 x1=right_x(p)-x_coord(p); x2=left_x(q)-right_x(p);
11375 x3=x_coord(q)-left_x(q);
11376 y1=right_y(p)-y_coord(p); y2=left_y(q)-right_y(p);
11377 y3=y_coord(q)-left_y(q);
11378 max=abs(x1);
11379 if ( abs(x2)>max ) max=abs(x2);
11380 if ( abs(x3)>max ) max=abs(x3);
11381 if ( abs(y1)>max ) max=abs(y1);
11382 if ( abs(y2)>max ) max=abs(y2);
11383 if ( abs(y3)>max ) max=abs(y3);
11384 if ( max==0 ) goto FOUND;
11385 while ( max<fraction_half ){ 
11386   max+=max; x1+=x1; x2+=x2; x3+=x3;
11387   y1+=y1; y2+=y2; y3+=y3;
11388 }
11389 t=x1; x1=mp_take_fraction(mp, x1,x)+mp_take_fraction(mp, y1,y);
11390 y1=mp_take_fraction(mp, y1,x)-mp_take_fraction(mp, t,y);
11391 t=x2; x2=mp_take_fraction(mp, x2,x)+mp_take_fraction(mp, y2,y);
11392 y2=mp_take_fraction(mp, y2,x)-mp_take_fraction(mp, t,y);
11393 t=x3; x3=mp_take_fraction(mp, x3,x)+mp_take_fraction(mp, y3,y);
11394 y3=mp_take_fraction(mp, y3,x)-mp_take_fraction(mp, t,y)
11395
11396 @ @<Exit to |found| if an eastward direction occurs at knot |p|@>=
11397 theta=mp_n_arg(mp, x1,y1);
11398 if ( theta>=0 ) if ( phi<=0 ) if ( phi>=theta-one_eighty_deg ) goto FOUND;
11399 if ( theta<=0 ) if ( phi>=0 ) if ( phi<=theta+one_eighty_deg ) goto FOUND
11400
11401 @ In this step we want to use the |crossing_point| routine to find the
11402 roots of the quadratic equation $B(y_1,y_2,y_3;t)=0$.
11403 Several complications arise: If the quadratic equation has a double root,
11404 the curve never crosses zero, and |crossing_point| will find nothing;
11405 this case occurs iff $y_1y_3=y_2^2$ and $y_1y_2<0$. If the quadratic
11406 equation has simple roots, or only one root, we may have to negate it
11407 so that $B(y_1,y_2,y_3;t)$ crosses from positive to negative at its first root.
11408 And finally, we need to do special things if $B(y_1,y_2,y_3;t)$ is
11409 identically zero.
11410
11411 @ @<Exit to |found| if the curve whose derivatives are specified by...@>=
11412 if ( x1<0 ) if ( x2<0 ) if ( x3<0 ) goto DONE;
11413 if ( mp_ab_vs_cd(mp, y1,y3,y2,y2)==0 ) {
11414   @<Handle the test for eastward directions when $y_1y_3=y_2^2$;
11415     either |goto found| or |goto done|@>;
11416 }
11417 if ( y1<=0 ) {
11418   if ( y1<0 ) { y1=-y1; y2=-y2; y3=-y3; }
11419   else if ( y2>0 ){ y2=-y2; y3=-y3; };
11420 }
11421 @<Check the places where $B(y_1,y_2,y_3;t)=0$ to see if
11422   $B(x_1,x_2,x_3;t)\ge0$@>;
11423 DONE:
11424
11425 @ The quadratic polynomial $B(y_1,y_2,y_3;t)$ begins |>=0| and has at most
11426 two roots, because we know that it isn't identically zero.
11427
11428 It must be admitted that the |crossing_point| routine is not perfectly accurate;
11429 rounding errors might cause it to find a root when $y_1y_3>y_2^2$, or to
11430 miss the roots when $y_1y_3<y_2^2$. The rotation process is itself
11431 subject to rounding errors. Yet this code optimistically tries to
11432 do the right thing.
11433
11434 @d we_found_it { tt=(t+04000) / 010000; goto FOUND; }
11435
11436 @<Check the places where $B(y_1,y_2,y_3;t)=0$...@>=
11437 t=mp_crossing_point(mp, y1,y2,y3);
11438 if ( t>fraction_one ) goto DONE;
11439 y2=t_of_the_way(y2,y3);
11440 x1=t_of_the_way(x1,x2);
11441 x2=t_of_the_way(x2,x3);
11442 x1=t_of_the_way(x1,x2);
11443 if ( x1>=0 ) we_found_it;
11444 if ( y2>0 ) y2=0;
11445 tt=t; t=mp_crossing_point(mp, 0,-y2,-y3);
11446 if ( t>fraction_one ) goto DONE;
11447 x1=t_of_the_way(x1,x2);
11448 x2=t_of_the_way(x2,x3);
11449 if ( t_of_the_way(x1,x2)>=0 ) { 
11450   t=t_of_the_way(tt,fraction_one); we_found_it;
11451 }
11452
11453 @ @<Handle the test for eastward directions when $y_1y_3=y_2^2$;
11454     either |goto found| or |goto done|@>=
11455
11456   if ( mp_ab_vs_cd(mp, y1,y2,0,0)<0 ) {
11457     t=mp_make_fraction(mp, y1,y1-y2);
11458     x1=t_of_the_way(x1,x2);
11459     x2=t_of_the_way(x2,x3);
11460     if ( t_of_the_way(x1,x2)>=0 ) we_found_it;
11461   } else if ( y3==0 ) {
11462     if ( y1==0 ) {
11463       @<Exit to |found| if the derivative $B(x_1,x_2,x_3;t)$ becomes |>=0|@>;
11464     } else if ( x3>=0 ) {
11465       tt=unity; goto FOUND;
11466     }
11467   }
11468   goto DONE;
11469 }
11470
11471 @ At this point we know that the derivative of |y(t)| is identically zero,
11472 and that |x1<0|; but either |x2>=0| or |x3>=0|, so there's some hope of
11473 traveling east.
11474
11475 @<Exit to |found| if the derivative $B(x_1,x_2,x_3;t)$ becomes |>=0|...@>=
11476
11477   t=mp_crossing_point(mp, -x1,-x2,-x3);
11478   if ( t<=fraction_one ) we_found_it;
11479   if ( mp_ab_vs_cd(mp, x1,x3,x2,x2)<=0 ) { 
11480     t=mp_make_fraction(mp, x1,x1-x2); we_found_it;
11481   }
11482 }
11483
11484 @ The intersection of two cubics can be found by an interesting variant
11485 of the general bisection scheme described in the introduction to
11486 |crossing_point|.\
11487 Given $w(t)=B(w_0,w_1,w_2,w_3;t)$ and $z(t)=B(z_0,z_1,z_2,z_3;t)$,
11488 we wish to find a pair of times $(t_1,t_2)$ such that $w(t_1)=z(t_2)$,
11489 if an intersection exists. First we find the smallest rectangle that
11490 encloses the points $\{w_0,w_1,w_2,w_3\}$ and check that it overlaps
11491 the smallest rectangle that encloses
11492 $\{z_0,z_1,z_2,z_3\}$; if not, the cubics certainly don't intersect.
11493 But if the rectangles do overlap, we bisect the intervals, getting
11494 new cubics $w'$ and~$w''$, $z'$~and~$z''$; the intersection routine first
11495 tries for an intersection between $w'$ and~$z'$, then (if unsuccessful)
11496 between $w'$ and~$z''$, then (if still unsuccessful) between $w''$ and~$z'$,
11497 finally (if thrice unsuccessful) between $w''$ and~$z''$. After $l$~successful
11498 levels of bisection we will have determined the intersection times $t_1$
11499 and~$t_2$ to $l$~bits of accuracy.
11500
11501 \def\submin{_{\rm min}} \def\submax{_{\rm max}}
11502 As before, it is better to work with the numbers $W_k=2^l(w_k-w_{k-1})$
11503 and $Z_k=2^l(z_k-z_{k-1})$ rather than the coefficients $w_k$ and $z_k$
11504 themselves. We also need one other quantity, $\Delta=2^l(w_0-z_0)$,
11505 to determine when the enclosing rectangles overlap. Here's why:
11506 The $x$~coordinates of~$w(t)$ are between $u\submin$ and $u\submax$,
11507 and the $x$~coordinates of~$z(t)$ are between $x\submin$ and $x\submax$,
11508 if we write $w_k=(u_k,v_k)$ and $z_k=(x_k,y_k)$ and $u\submin=
11509 \min(u_0,u_1,u_2,u_3)$, etc. These intervals of $x$~coordinates
11510 overlap if and only if $u\submin\L x\submax$ and
11511 $x\submin\L u\submax$. Letting
11512 $$U\submin=\min(0,U_1,U_1+U_2,U_1+U_2+U_3),\;
11513   U\submax=\max(0,U_1,U_1+U_2,U_1+U_2+U_3),$$
11514 we have $2^lu\submin=2^lu_0+U\submin$, etc.; the condition for overlap
11515 reduces to
11516 $$X\submin-U\submax\L 2^l(u_0-x_0)\L X\submax-U\submin.$$
11517 Thus we want to maintain the quantity $2^l(u_0-x_0)$; similarly,
11518 the quantity $2^l(v_0-y_0)$ accounts for the $y$~coordinates. The
11519 coordinates of $\Delta=2^l(w_0-z_0)$ must stay bounded as $l$ increases,
11520 because of the overlap condition; i.e., we know that $X\submin$,
11521 $X\submax$, and their relatives are bounded, hence $X\submax-
11522 U\submin$ and $X\submin-U\submax$ are bounded.
11523
11524 @ Incidentally, if the given cubics intersect more than once, the process
11525 just sketched will not necessarily find the lexicographically smallest pair
11526 $(t_1,t_2)$. The solution actually obtained will be smallest in ``shuffled
11527 order''; i.e., if $t_1=(.a_1a_2\ldots a_{16})_2$ and
11528 $t_2=(.b_1b_2\ldots b_{16})_2$, then we will minimize
11529 $a_1b_1a_2b_2\ldots a_{16}b_{16}$, not
11530 $a_1a_2\ldots a_{16}b_1b_2\ldots b_{16}$.
11531 Shuffled order agrees with lexicographic order if all pairs of solutions
11532 $(t_1,t_2)$ and $(t_1',t_2')$ have the property that $t_1<t_1'$ iff
11533 $t_2<t_2'$; but in general, lexicographic order can be quite different,
11534 and the bisection algorithm would be substantially less efficient if it were
11535 constrained by lexicographic order.
11536
11537 For example, suppose that an overlap has been found for $l=3$ and
11538 $(t_1,t_2)= (.101,.011)$ in binary, but that no overlap is produced by
11539 either of the alternatives $(.1010,.0110)$, $(.1010,.0111)$ at level~4.
11540 Then there is probably an intersection in one of the subintervals
11541 $(.1011,.011x)$; but lexicographic order would require us to explore
11542 $(.1010,.1xxx)$ and $(.1011,.00xx)$ and $(.1011,.010x)$ first. We wouldn't
11543 want to store all of the subdivision data for the second path, so the
11544 subdivisions would have to be regenerated many times. Such inefficiencies
11545 would be associated with every `1' in the binary representation of~$t_1$.
11546
11547 @ The subdivision process introduces rounding errors, hence we need to
11548 make a more liberal test for overlap. It is not hard to show that the
11549 computed values of $U_i$ differ from the truth by at most~$l$, on
11550 level~$l$, hence $U\submin$ and $U\submax$ will be at most $3l$ in error.
11551 If $\beta$ is an upper bound on the absolute error in the computed
11552 components of $\Delta=(|delx|,|dely|)$ on level~$l$, we will replace
11553 the test `$X\submin-U\submax\L|delx|$' by the more liberal test
11554 `$X\submin-U\submax\L|delx|+|tol|$', where $|tol|=6l+\beta$.
11555
11556 More accuracy is obtained if we try the algorithm first with |tol=0|;
11557 the more liberal tolerance is used only if an exact approach fails.
11558 It is convenient to do this double-take by letting `3' in the preceding
11559 paragraph be a parameter, which is first 0, then 3.
11560
11561 @<Glob...@>=
11562 unsigned int tol_step; /* either 0 or 3, usually */
11563
11564 @ We shall use an explicit stack to implement the recursive bisection
11565 method described above. The |bisect_stack| array will contain numerous 5-word
11566 packets like $(U_1,U_2,U_3,U\submin,U\submax)$, as well as 20-word packets
11567 comprising the 5-word packets for $U$, $V$, $X$, and~$Y$.
11568
11569 The following macros define the allocation of stack positions to
11570 the quantities needed for bisection-intersection.
11571
11572 @d stack_1(A) mp->bisect_stack[(A)] /* $U_1$, $V_1$, $X_1$, or $Y_1$ */
11573 @d stack_2(A) mp->bisect_stack[(A)+1] /* $U_2$, $V_2$, $X_2$, or $Y_2$ */
11574 @d stack_3(A) mp->bisect_stack[(A)+2] /* $U_3$, $V_3$, $X_3$, or $Y_3$ */
11575 @d stack_min(A) mp->bisect_stack[(A)+3]
11576   /* $U\submin$, $V\submin$, $X\submin$, or $Y\submin$ */
11577 @d stack_max(A) mp->bisect_stack[(A)+4]
11578   /* $U\submax$, $V\submax$, $X\submax$, or $Y\submax$ */
11579 @d int_packets 20 /* number of words to represent $U_k$, $V_k$, $X_k$, and $Y_k$ */
11580 @#
11581 @d u_packet(A) ((A)-5)
11582 @d v_packet(A) ((A)-10)
11583 @d x_packet(A) ((A)-15)
11584 @d y_packet(A) ((A)-20)
11585 @d l_packets (mp->bisect_ptr-int_packets)
11586 @d r_packets mp->bisect_ptr
11587 @d ul_packet u_packet(l_packets) /* base of $U'_k$ variables */
11588 @d vl_packet v_packet(l_packets) /* base of $V'_k$ variables */
11589 @d xl_packet x_packet(l_packets) /* base of $X'_k$ variables */
11590 @d yl_packet y_packet(l_packets) /* base of $Y'_k$ variables */
11591 @d ur_packet u_packet(r_packets) /* base of $U''_k$ variables */
11592 @d vr_packet v_packet(r_packets) /* base of $V''_k$ variables */
11593 @d xr_packet x_packet(r_packets) /* base of $X''_k$ variables */
11594 @d yr_packet y_packet(r_packets) /* base of $Y''_k$ variables */
11595 @#
11596 @d u1l stack_1(ul_packet) /* $U'_1$ */
11597 @d u2l stack_2(ul_packet) /* $U'_2$ */
11598 @d u3l stack_3(ul_packet) /* $U'_3$ */
11599 @d v1l stack_1(vl_packet) /* $V'_1$ */
11600 @d v2l stack_2(vl_packet) /* $V'_2$ */
11601 @d v3l stack_3(vl_packet) /* $V'_3$ */
11602 @d x1l stack_1(xl_packet) /* $X'_1$ */
11603 @d x2l stack_2(xl_packet) /* $X'_2$ */
11604 @d x3l stack_3(xl_packet) /* $X'_3$ */
11605 @d y1l stack_1(yl_packet) /* $Y'_1$ */
11606 @d y2l stack_2(yl_packet) /* $Y'_2$ */
11607 @d y3l stack_3(yl_packet) /* $Y'_3$ */
11608 @d u1r stack_1(ur_packet) /* $U''_1$ */
11609 @d u2r stack_2(ur_packet) /* $U''_2$ */
11610 @d u3r stack_3(ur_packet) /* $U''_3$ */
11611 @d v1r stack_1(vr_packet) /* $V''_1$ */
11612 @d v2r stack_2(vr_packet) /* $V''_2$ */
11613 @d v3r stack_3(vr_packet) /* $V''_3$ */
11614 @d x1r stack_1(xr_packet) /* $X''_1$ */
11615 @d x2r stack_2(xr_packet) /* $X''_2$ */
11616 @d x3r stack_3(xr_packet) /* $X''_3$ */
11617 @d y1r stack_1(yr_packet) /* $Y''_1$ */
11618 @d y2r stack_2(yr_packet) /* $Y''_2$ */
11619 @d y3r stack_3(yr_packet) /* $Y''_3$ */
11620 @#
11621 @d stack_dx mp->bisect_stack[mp->bisect_ptr] /* stacked value of |delx| */
11622 @d stack_dy mp->bisect_stack[mp->bisect_ptr+1] /* stacked value of |dely| */
11623 @d stack_tol mp->bisect_stack[mp->bisect_ptr+2] /* stacked value of |tol| */
11624 @d stack_uv mp->bisect_stack[mp->bisect_ptr+3] /* stacked value of |uv| */
11625 @d stack_xy mp->bisect_stack[mp->bisect_ptr+4] /* stacked value of |xy| */
11626 @d int_increment (int_packets+int_packets+5) /* number of stack words per level */
11627
11628 @<Glob...@>=
11629 integer *bisect_stack;
11630 unsigned int bisect_ptr;
11631
11632 @ @<Allocate or initialize ...@>=
11633 mp->bisect_stack = xmalloc((bistack_size+1),sizeof(integer));
11634
11635 @ @<Dealloc variables@>=
11636 xfree(mp->bisect_stack);
11637
11638 @ @<Check the ``constant''...@>=
11639 if ( int_packets+17*int_increment>bistack_size ) mp->bad=19;
11640
11641 @ Computation of the min and max is a tedious but fairly fast sequence of
11642 instructions; exactly four comparisons are made in each branch.
11643
11644 @d set_min_max(A) 
11645   if ( stack_1((A))<0 ) {
11646     if ( stack_3((A))>=0 ) {
11647       if ( stack_2((A))<0 ) stack_min((A))=stack_1((A))+stack_2((A));
11648       else stack_min((A))=stack_1((A));
11649       stack_max((A))=stack_1((A))+stack_2((A))+stack_3((A));
11650       if ( stack_max((A))<0 ) stack_max((A))=0;
11651     } else { 
11652       stack_min((A))=stack_1((A))+stack_2((A))+stack_3((A));
11653       if ( stack_min((A))>stack_1((A)) ) stack_min((A))=stack_1((A));
11654       stack_max((A))=stack_1((A))+stack_2((A));
11655       if ( stack_max((A))<0 ) stack_max((A))=0;
11656     }
11657   } else if ( stack_3((A))<=0 ) {
11658     if ( stack_2((A))>0 ) stack_max((A))=stack_1((A))+stack_2((A));
11659     else stack_max((A))=stack_1((A));
11660     stack_min((A))=stack_1((A))+stack_2((A))+stack_3((A));
11661     if ( stack_min((A))>0 ) stack_min((A))=0;
11662   } else  { 
11663     stack_max((A))=stack_1((A))+stack_2((A))+stack_3((A));
11664     if ( stack_max((A))<stack_1((A)) ) stack_max((A))=stack_1((A));
11665     stack_min((A))=stack_1((A))+stack_2((A));
11666     if ( stack_min((A))>0 ) stack_min((A))=0;
11667   }
11668
11669 @ It's convenient to keep the current values of $l$, $t_1$, and $t_2$ in
11670 the integer form $2^l+2^lt_1$ and $2^l+2^lt_2$. The |cubic_intersection|
11671 routine uses global variables |cur_t| and |cur_tt| for this purpose;
11672 after successful completion, |cur_t| and |cur_tt| will contain |unity|
11673 plus the |scaled| values of $t_1$ and~$t_2$.
11674
11675 The values of |cur_t| and |cur_tt| will be set to zero if |cubic_intersection|
11676 finds no intersection. The routine gives up and gives an approximate answer
11677 if it has backtracked
11678 more than 5000 times (otherwise there are cases where several minutes
11679 of fruitless computation would be possible).
11680
11681 @d max_patience 5000
11682
11683 @<Glob...@>=
11684 integer cur_t;integer cur_tt; /* controls and results of |cubic_intersection| */
11685 integer time_to_go; /* this many backtracks before giving up */
11686 integer max_t; /* maximum of $2^{l+1}$ so far achieved */
11687
11688 @ The given cubics $B(w_0,w_1,w_2,w_3;t)$ and
11689 $B(z_0,z_1,z_2,z_3;t)$ are specified in adjacent knot nodes |(p,link(p))|
11690 and |(pp,link(pp))|, respectively.
11691
11692 @c void mp_cubic_intersection (MP mp,pointer p, pointer pp) {
11693   pointer q,qq; /* |link(p)|, |link(pp)| */
11694   mp->time_to_go=max_patience; mp->max_t=2;
11695   @<Initialize for intersections at level zero@>;
11696 CONTINUE:
11697   while (1) { 
11698     if ( mp->delx-mp->tol<=stack_max(x_packet(mp->xy))-stack_min(u_packet(mp->uv)))
11699     if ( mp->delx+mp->tol>=stack_min(x_packet(mp->xy))-stack_max(u_packet(mp->uv)))
11700     if ( mp->dely-mp->tol<=stack_max(y_packet(mp->xy))-stack_min(v_packet(mp->uv)))
11701     if ( mp->dely+mp->tol>=stack_min(y_packet(mp->xy))-stack_max(v_packet(mp->uv))) 
11702     { 
11703       if ( mp->cur_t>=mp->max_t ){ 
11704         if ( mp->max_t==two ) { /* we've done 17 bisections */ 
11705            mp->cur_t=halfp(mp->cur_t+1); 
11706                mp->cur_tt=halfp(mp->cur_tt+1); 
11707            return;
11708         }
11709         mp->max_t+=mp->max_t; mp->appr_t=mp->cur_t; mp->appr_tt=mp->cur_tt;
11710       }
11711       @<Subdivide for a new level of intersection@>;
11712       goto CONTINUE;
11713     }
11714     if ( mp->time_to_go>0 ) {
11715       decr(mp->time_to_go);
11716     } else { 
11717       while ( mp->appr_t<unity ) { 
11718         mp->appr_t+=mp->appr_t; mp->appr_tt+=mp->appr_tt;
11719       }
11720       mp->cur_t=mp->appr_t; mp->cur_tt=mp->appr_tt; return;
11721     }
11722     @<Advance to the next pair |(cur_t,cur_tt)|@>;
11723   }
11724 }
11725
11726 @ The following variables are global, although they are used only by
11727 |cubic_intersection|, because it is necessary on some machines to
11728 split |cubic_intersection| up into two procedures.
11729
11730 @<Glob...@>=
11731 integer delx;integer dely; /* the components of $\Delta=2^l(w_0-z_0)$ */
11732 integer tol; /* bound on the uncertainty in the overlap test */
11733 unsigned int uv;
11734 unsigned int xy; /* pointers to the current packets of interest */
11735 integer three_l; /* |tol_step| times the bisection level */
11736 integer appr_t;integer appr_tt; /* best approximations known to the answers */
11737
11738 @ We shall assume that the coordinates are sufficiently non-extreme that
11739 integer overflow will not occur.
11740 @^overflow in arithmetic@>
11741
11742 @<Initialize for intersections at level zero@>=
11743 q=link(p); qq=link(pp); mp->bisect_ptr=int_packets;
11744 u1r=right_x(p)-x_coord(p); u2r=left_x(q)-right_x(p);
11745 u3r=x_coord(q)-left_x(q); set_min_max(ur_packet);
11746 v1r=right_y(p)-y_coord(p); v2r=left_y(q)-right_y(p);
11747 v3r=y_coord(q)-left_y(q); set_min_max(vr_packet);
11748 x1r=right_x(pp)-x_coord(pp); x2r=left_x(qq)-right_x(pp);
11749 x3r=x_coord(qq)-left_x(qq); set_min_max(xr_packet);
11750 y1r=right_y(pp)-y_coord(pp); y2r=left_y(qq)-right_y(pp);
11751 y3r=y_coord(qq)-left_y(qq); set_min_max(yr_packet);
11752 mp->delx=x_coord(p)-x_coord(pp); mp->dely=y_coord(p)-y_coord(pp);
11753 mp->tol=0; mp->uv=r_packets; mp->xy=r_packets; 
11754 mp->three_l=0; mp->cur_t=1; mp->cur_tt=1
11755
11756 @ @<Subdivide for a new level of intersection@>=
11757 stack_dx=mp->delx; stack_dy=mp->dely; stack_tol=mp->tol; 
11758 stack_uv=mp->uv; stack_xy=mp->xy;
11759 mp->bisect_ptr=mp->bisect_ptr+int_increment;
11760 mp->cur_t+=mp->cur_t; mp->cur_tt+=mp->cur_tt;
11761 u1l=stack_1(u_packet(mp->uv)); u3r=stack_3(u_packet(mp->uv));
11762 u2l=half(u1l+stack_2(u_packet(mp->uv)));
11763 u2r=half(u3r+stack_2(u_packet(mp->uv)));
11764 u3l=half(u2l+u2r); u1r=u3l;
11765 set_min_max(ul_packet); set_min_max(ur_packet);
11766 v1l=stack_1(v_packet(mp->uv)); v3r=stack_3(v_packet(mp->uv));
11767 v2l=half(v1l+stack_2(v_packet(mp->uv)));
11768 v2r=half(v3r+stack_2(v_packet(mp->uv)));
11769 v3l=half(v2l+v2r); v1r=v3l;
11770 set_min_max(vl_packet); set_min_max(vr_packet);
11771 x1l=stack_1(x_packet(mp->xy)); x3r=stack_3(x_packet(mp->xy));
11772 x2l=half(x1l+stack_2(x_packet(mp->xy)));
11773 x2r=half(x3r+stack_2(x_packet(mp->xy)));
11774 x3l=half(x2l+x2r); x1r=x3l;
11775 set_min_max(xl_packet); set_min_max(xr_packet);
11776 y1l=stack_1(y_packet(mp->xy)); y3r=stack_3(y_packet(mp->xy));
11777 y2l=half(y1l+stack_2(y_packet(mp->xy)));
11778 y2r=half(y3r+stack_2(y_packet(mp->xy)));
11779 y3l=half(y2l+y2r); y1r=y3l;
11780 set_min_max(yl_packet); set_min_max(yr_packet);
11781 mp->uv=l_packets; mp->xy=l_packets;
11782 mp->delx+=mp->delx; mp->dely+=mp->dely;
11783 mp->tol=mp->tol-mp->three_l+mp->tol_step; 
11784 mp->tol+=mp->tol; mp->three_l=mp->three_l+mp->tol_step
11785
11786 @ @<Advance to the next pair |(cur_t,cur_tt)|@>=
11787 NOT_FOUND: 
11788 if ( odd(mp->cur_tt) ) {
11789   if ( odd(mp->cur_t) ) {
11790      @<Descend to the previous level and |goto not_found|@>;
11791   } else { 
11792     incr(mp->cur_t);
11793     mp->delx=mp->delx+stack_1(u_packet(mp->uv))+stack_2(u_packet(mp->uv))
11794       +stack_3(u_packet(mp->uv));
11795     mp->dely=mp->dely+stack_1(v_packet(mp->uv))+stack_2(v_packet(mp->uv))
11796       +stack_3(v_packet(mp->uv));
11797     mp->uv=mp->uv+int_packets; /* switch from |l_packets| to |r_packets| */
11798     decr(mp->cur_tt); mp->xy=mp->xy-int_packets; 
11799          /* switch from |r_packets| to |l_packets| */
11800     mp->delx=mp->delx+stack_1(x_packet(mp->xy))+stack_2(x_packet(mp->xy))
11801       +stack_3(x_packet(mp->xy));
11802     mp->dely=mp->dely+stack_1(y_packet(mp->xy))+stack_2(y_packet(mp->xy))
11803       +stack_3(y_packet(mp->xy));
11804   }
11805 } else { 
11806   incr(mp->cur_tt); mp->tol=mp->tol+mp->three_l;
11807   mp->delx=mp->delx-stack_1(x_packet(mp->xy))-stack_2(x_packet(mp->xy))
11808     -stack_3(x_packet(mp->xy));
11809   mp->dely=mp->dely-stack_1(y_packet(mp->xy))-stack_2(y_packet(mp->xy))
11810     -stack_3(y_packet(mp->xy));
11811   mp->xy=mp->xy+int_packets; /* switch from |l_packets| to |r_packets| */
11812 }
11813
11814 @ @<Descend to the previous level...@>=
11815
11816   mp->cur_t=halfp(mp->cur_t); mp->cur_tt=halfp(mp->cur_tt);
11817   if ( mp->cur_t==0 ) return;
11818   mp->bisect_ptr=mp->bisect_ptr-int_increment; 
11819   mp->three_l=mp->three_l-mp->tol_step;
11820   mp->delx=stack_dx; mp->dely=stack_dy; mp->tol=stack_tol; 
11821   mp->uv=stack_uv; mp->xy=stack_xy;
11822   goto NOT_FOUND;
11823 }
11824
11825 @ The |path_intersection| procedure is much simpler.
11826 It invokes |cubic_intersection| in lexicographic order until finding a
11827 pair of cubics that intersect. The final intersection times are placed in
11828 |cur_t| and~|cur_tt|.
11829
11830 @c void mp_path_intersection (MP mp,pointer h, pointer hh) {
11831   pointer p,pp; /* link registers that traverse the given paths */
11832   integer n,nn; /* integer parts of intersection times, minus |unity| */
11833   @<Change one-point paths into dead cycles@>;
11834   mp->tol_step=0;
11835   do {  
11836     n=-unity; p=h;
11837     do {  
11838       if ( right_type(p)!=mp_endpoint ) { 
11839         nn=-unity; pp=hh;
11840         do {  
11841           if ( right_type(pp)!=mp_endpoint )  { 
11842             mp_cubic_intersection(mp, p,pp);
11843             if ( mp->cur_t>0 ) { 
11844               mp->cur_t=mp->cur_t+n; mp->cur_tt=mp->cur_tt+nn; 
11845               return;
11846             }
11847           }
11848           nn=nn+unity; pp=link(pp);
11849         } while (pp!=hh);
11850       }
11851       n=n+unity; p=link(p);
11852     } while (p!=h);
11853     mp->tol_step=mp->tol_step+3;
11854   } while (mp->tol_step<=3);
11855   mp->cur_t=-unity; mp->cur_tt=-unity;
11856 }
11857
11858 @ @<Change one-point paths...@>=
11859 if ( right_type(h)==mp_endpoint ) {
11860   right_x(h)=x_coord(h); left_x(h)=x_coord(h);
11861   right_y(h)=y_coord(h); left_y(h)=y_coord(h); right_type(h)=mp_explicit;
11862 }
11863 if ( right_type(hh)==mp_endpoint ) {
11864   right_x(hh)=x_coord(hh); left_x(hh)=x_coord(hh);
11865   right_y(hh)=y_coord(hh); left_y(hh)=y_coord(hh); right_type(hh)=mp_explicit;
11866 }
11867
11868 @* \[24] Dynamic linear equations.
11869 \MP\ users define variables implicitly by stating equations that should be
11870 satisfied; the computer is supposed to be smart enough to solve those equations.
11871 And indeed, the computer tries valiantly to do so, by distinguishing five
11872 different types of numeric values:
11873
11874 \smallskip\hang
11875 |type(p)=mp_known| is the nice case, when |value(p)| is the |scaled| value
11876 of the variable whose address is~|p|.
11877
11878 \smallskip\hang
11879 |type(p)=mp_dependent| means that |value(p)| is not present, but |dep_list(p)|
11880 points to a {\sl dependency list\/} that expresses the value of variable~|p|
11881 as a |scaled| number plus a sum of independent variables with |fraction|
11882 coefficients.
11883
11884 \smallskip\hang
11885 |type(p)=mp_independent| means that |value(p)=64s+m|, where |s>0| is a ``serial
11886 number'' reflecting the time this variable was first used in an equation;
11887 also |0<=m<64|, and each dependent variable
11888 that refers to this one is actually referring to the future value of
11889 this variable times~$2^m$. (Usually |m=0|, but higher degrees of
11890 scaling are sometimes needed to keep the coefficients in dependency lists
11891 from getting too large. The value of~|m| will always be even.)
11892
11893 \smallskip\hang
11894 |type(p)=mp_numeric_type| means that variable |p| hasn't appeared in an
11895 equation before, but it has been explicitly declared to be numeric.
11896
11897 \smallskip\hang
11898 |type(p)=undefined| means that variable |p| hasn't appeared before.
11899
11900 \smallskip\noindent
11901 We have actually discussed these five types in the reverse order of their
11902 history during a computation: Once |known|, a variable never again
11903 becomes |dependent|; once |dependent|, it almost never again becomes
11904 |mp_independent|; once |mp_independent|, it never again becomes |mp_numeric_type|;
11905 and once |mp_numeric_type|, it never again becomes |undefined| (except
11906 of course when the user specifically decides to scrap the old value
11907 and start again). A backward step may, however, take place: Sometimes
11908 a |dependent| variable becomes |mp_independent| again, when one of the
11909 independent variables it depends on is reverting to |undefined|.
11910
11911
11912 The next patch detects overflow of independent-variable serial
11913 numbers. Diagnosed and patched by Thorsten Dahlheimer.
11914
11915 @d s_scale 64 /* the serial numbers are multiplied by this factor */
11916 @d new_indep(A)  /* create a new independent variable */
11917   { if ( mp->serial_no>el_gordo-s_scale )
11918     mp_fatal_error(mp, "variable instance identifiers exhausted");
11919   type((A))=mp_independent; mp->serial_no=mp->serial_no+s_scale;
11920   value((A))=mp->serial_no;
11921   }
11922
11923 @<Glob...@>=
11924 integer serial_no; /* the most recent serial number, times |s_scale| */
11925
11926 @ @<Make variable |q+s| newly independent@>=new_indep(q+s)
11927
11928 @ But how are dependency lists represented? It's simple: The linear combination
11929 $\alpha_1v_1+\cdots+\alpha_kv_k+\beta$ appears in |k+1| value nodes. If
11930 |q=dep_list(p)| points to this list, and if |k>0|, then |value(q)=
11931 @t$\alpha_1$@>| (which is a |fraction|); |info(q)| points to the location
11932 of $\alpha_1$; and |link(p)| points to the dependency list
11933 $\alpha_2v_2+\cdots+\alpha_kv_k+\beta$. On the other hand if |k=0|,
11934 then |value(q)=@t$\beta$@>| (which is |scaled|) and |info(q)=null|.
11935 The independent variables $v_1$, \dots,~$v_k$ have been sorted so that
11936 they appear in decreasing order of their |value| fields (i.e., of
11937 their serial numbers). \ (It is convenient to use decreasing order,
11938 since |value(null)=0|. If the independent variables were not sorted by
11939 serial number but by some other criterion, such as their location in |mem|,
11940 the equation-solving mechanism would be too system-dependent, because
11941 the ordering can affect the computed results.)
11942
11943 The |link| field in the node that contains the constant term $\beta$ is
11944 called the {\sl final link\/} of the dependency list. \MP\ maintains
11945 a doubly-linked master list of all dependency lists, in terms of a permanently
11946 allocated node
11947 in |mem| called |dep_head|. If there are no dependencies, we have
11948 |link(dep_head)=dep_head| and |prev_dep(dep_head)=dep_head|;
11949 otherwise |link(dep_head)| points to the first dependent variable, say~|p|,
11950 and |prev_dep(p)=dep_head|. We have |type(p)=mp_dependent|, and |dep_list(p)|
11951 points to its dependency list. If the final link of that dependency list
11952 occurs in location~|q|, then |link(q)| points to the next dependent
11953 variable (say~|r|); and we have |prev_dep(r)=q|, etc.
11954
11955 @d dep_list(A) link(value_loc((A)))
11956   /* half of the |value| field in a |dependent| variable */
11957 @d prev_dep(A) info(value_loc((A)))
11958   /* the other half; makes a doubly linked list */
11959 @d dep_node_size 2 /* the number of words per dependency node */
11960
11961 @<Initialize table entries...@>= mp->serial_no=0;
11962 link(dep_head)=dep_head; prev_dep(dep_head)=dep_head;
11963 info(dep_head)=null; dep_list(dep_head)=null;
11964
11965 @ Actually the description above contains a little white lie. There's
11966 another kind of variable called |mp_proto_dependent|, which is
11967 just like a |dependent| one except that the $\alpha$ coefficients
11968 in its dependency list are |scaled| instead of being fractions.
11969 Proto-dependency lists are mixed with dependency lists in the
11970 nodes reachable from |dep_head|.
11971
11972 @ Here is a procedure that prints a dependency list in symbolic form.
11973 The second parameter should be either |dependent| or |mp_proto_dependent|,
11974 to indicate the scaling of the coefficients.
11975
11976 @<Declare subroutines for printing expressions@>=
11977 void mp_print_dependency (MP mp,pointer p, small_number t) {
11978   integer v; /* a coefficient */
11979   pointer pp,q; /* for list manipulation */
11980   pp=p;
11981   while (1) { 
11982     v=abs(value(p)); q=info(p);
11983     if ( q==null ) { /* the constant term */
11984       if ( (v!=0)||(p==pp) ) {
11985          if ( value(p)>0 ) if ( p!=pp ) mp_print_char(mp, '+');
11986          mp_print_scaled(mp, value(p));
11987       }
11988       return;
11989     }
11990     @<Print the coefficient, unless it's $\pm1.0$@>;
11991     if ( type(q)!=mp_independent ) mp_confusion(mp, "dep");
11992 @:this can't happen dep}{\quad dep@>
11993     mp_print_variable_name(mp, q); v=value(q) % s_scale;
11994     while ( v>0 ) { mp_print(mp, "*4"); v=v-2; }
11995     p=link(p);
11996   }
11997 }
11998
11999 @ @<Print the coefficient, unless it's $\pm1.0$@>=
12000 if ( value(p)<0 ) mp_print_char(mp, '-');
12001 else if ( p!=pp ) mp_print_char(mp, '+');
12002 if ( t==mp_dependent ) v=mp_round_fraction(mp, v);
12003 if ( v!=unity ) mp_print_scaled(mp, v)
12004
12005 @ The maximum absolute value of a coefficient in a given dependency list
12006 is returned by the following simple function.
12007
12008 @c fraction mp_max_coef (MP mp,pointer p) {
12009   fraction x; /* the maximum so far */
12010   x=0;
12011   while ( info(p)!=null ) {
12012     if ( abs(value(p))>x ) x=abs(value(p));
12013     p=link(p);
12014   }
12015   return x;
12016 }
12017
12018 @ One of the main operations needed on dependency lists is to add a multiple
12019 of one list to the other; we call this |p_plus_fq|, where |p| and~|q| point
12020 to dependency lists and |f| is a fraction.
12021
12022 If the coefficient of any independent variable becomes |coef_bound| or
12023 more, in absolute value, this procedure changes the type of that variable
12024 to `|independent_needing_fix|', and sets the global variable |fix_needed|
12025 to~|true|. The value of $|coef_bound|=\mu$ is chosen so that
12026 $\mu^2+\mu<8$; this means that the numbers we deal with won't
12027 get too large. (Instead of the ``optimum'' $\mu=(\sqrt{33}-1)/2\approx
12028 2.3723$, the safer value 7/3 is taken as the threshold.)
12029
12030 The changes mentioned in the preceding paragraph are actually done only if
12031 the global variable |watch_coefs| is |true|. But it usually is; in fact,
12032 it is |false| only when \MP\ is making a dependency list that will soon
12033 be equated to zero.
12034
12035 Several procedures that act on dependency lists, including |p_plus_fq|,
12036 set the global variable |dep_final| to the final (constant term) node of
12037 the dependency list that they produce.
12038
12039 @d coef_bound 04525252525 /* |fraction| approximation to 7/3 */
12040 @d independent_needing_fix 0
12041
12042 @<Glob...@>=
12043 boolean fix_needed; /* does at least one |independent| variable need scaling? */
12044 boolean watch_coefs; /* should we scale coefficients that exceed |coef_bound|? */
12045 pointer dep_final; /* location of the constant term and final link */
12046
12047 @ @<Set init...@>=
12048 mp->fix_needed=false; mp->watch_coefs=true;
12049
12050 @ The |p_plus_fq| procedure has a fourth parameter, |t|, that should be
12051 set to |mp_proto_dependent| if |p| is a proto-dependency list. In this
12052 case |f| will be |scaled|, not a |fraction|. Similarly, the fifth parameter~|tt|
12053 should be |mp_proto_dependent| if |q| is a proto-dependency list.
12054
12055 List |q| is unchanged by the operation; but list |p| is totally destroyed.
12056
12057 The final link of the dependency list or proto-dependency list returned
12058 by |p_plus_fq| is the same as the original final link of~|p|. Indeed, the
12059 constant term of the result will be located in the same |mem| location
12060 as the original constant term of~|p|.
12061
12062 Coefficients of the result are assumed to be zero if they are less than
12063 a certain threshold. This compensates for inevitable rounding errors,
12064 and tends to make more variables `|known|'. The threshold is approximately
12065 $10^{-5}$ in the case of normal dependency lists, $10^{-4}$ for
12066 proto-dependencies.
12067
12068 @d fraction_threshold 2685 /* a |fraction| coefficient less than this is zeroed */
12069 @d half_fraction_threshold 1342 /* half of |fraction_threshold| */
12070 @d scaled_threshold 8 /* a |scaled| coefficient less than this is zeroed */
12071 @d half_scaled_threshold 4 /* half of |scaled_threshold| */
12072
12073 @<Declare basic dependency-list subroutines@>=
12074 pointer mp_p_plus_fq ( MP mp, pointer p, integer f, 
12075                       pointer q, small_number t, small_number tt) ;
12076
12077 @ @c
12078 pointer mp_p_plus_fq ( MP mp, pointer p, integer f, 
12079                       pointer q, small_number t, small_number tt) {
12080   pointer pp,qq; /* |info(p)| and |info(q)|, respectively */
12081   pointer r,s; /* for list manipulation */
12082   integer threshold; /* defines a neighborhood of zero */
12083   integer v; /* temporary register */
12084   if ( t==mp_dependent ) threshold=fraction_threshold;
12085   else threshold=scaled_threshold;
12086   r=temp_head; pp=info(p); qq=info(q);
12087   while (1) {
12088     if ( pp==qq ) {
12089       if ( pp==null ) {
12090        break;
12091       } else {
12092         @<Contribute a term from |p|, plus |f| times the
12093           corresponding term from |q|@>
12094       }
12095     } else if ( value(pp)<value(qq) ) {
12096       @<Contribute a term from |q|, multiplied by~|f|@>
12097     } else { 
12098      link(r)=p; r=p; p=link(p); pp=info(p);
12099     }
12100   }
12101   if ( t==mp_dependent )
12102     value(p)=mp_slow_add(mp, value(p),mp_take_fraction(mp, value(q),f));
12103   else  
12104     value(p)=mp_slow_add(mp, value(p),mp_take_scaled(mp, value(q),f));
12105   link(r)=p; mp->dep_final=p; 
12106   return link(temp_head);
12107 }
12108
12109 @ @<Contribute a term from |p|, plus |f|...@>=
12110
12111   if ( tt==mp_dependent ) v=value(p)+mp_take_fraction(mp, f,value(q));
12112   else v=value(p)+mp_take_scaled(mp, f,value(q));
12113   value(p)=v; s=p; p=link(p);
12114   if ( abs(v)<threshold ) {
12115     mp_free_node(mp, s,dep_node_size);
12116   } else {
12117     if ( (abs(v)>=coef_bound)  && mp->watch_coefs ) { 
12118       type(qq)=independent_needing_fix; mp->fix_needed=true;
12119     }
12120     link(r)=s; r=s;
12121   };
12122   pp=info(p); q=link(q); qq=info(q);
12123 }
12124
12125 @ @<Contribute a term from |q|, multiplied by~|f|@>=
12126
12127   if ( tt==mp_dependent ) v=mp_take_fraction(mp, f,value(q));
12128   else v=mp_take_scaled(mp, f,value(q));
12129   if ( abs(v)>halfp(threshold) ) { 
12130     s=mp_get_node(mp, dep_node_size); info(s)=qq; value(s)=v;
12131     if ( (abs(v)>=coef_bound) && mp->watch_coefs ) { 
12132       type(qq)=independent_needing_fix; mp->fix_needed=true;
12133     }
12134     link(r)=s; r=s;
12135   }
12136   q=link(q); qq=info(q);
12137 }
12138
12139 @ It is convenient to have another subroutine for the special case
12140 of |p_plus_fq| when |f=1.0|. In this routine lists |p| and |q| are
12141 both of the same type~|t| (either |dependent| or |mp_proto_dependent|).
12142
12143 @c pointer mp_p_plus_q (MP mp,pointer p, pointer q, small_number t) {
12144   pointer pp,qq; /* |info(p)| and |info(q)|, respectively */
12145   pointer r,s; /* for list manipulation */
12146   integer threshold; /* defines a neighborhood of zero */
12147   integer v; /* temporary register */
12148   if ( t==mp_dependent ) threshold=fraction_threshold;
12149   else threshold=scaled_threshold;
12150   r=temp_head; pp=info(p); qq=info(q);
12151   while (1) {
12152     if ( pp==qq ) {
12153       if ( pp==null ) {
12154         break;
12155       } else {
12156         @<Contribute a term from |p|, plus the
12157           corresponding term from |q|@>
12158       }
12159     } else { 
12160           if ( value(pp)<value(qq) ) {
12161         s=mp_get_node(mp, dep_node_size); info(s)=qq; value(s)=value(q);
12162         q=link(q); qq=info(q); link(r)=s; r=s;
12163       } else { 
12164         link(r)=p; r=p; p=link(p); pp=info(p);
12165       }
12166     }
12167   }
12168   value(p)=mp_slow_add(mp, value(p),value(q));
12169   link(r)=p; mp->dep_final=p; 
12170   return link(temp_head);
12171 }
12172
12173 @ @<Contribute a term from |p|, plus the...@>=
12174
12175   v=value(p)+value(q);
12176   value(p)=v; s=p; p=link(p); pp=info(p);
12177   if ( abs(v)<threshold ) {
12178     mp_free_node(mp, s,dep_node_size);
12179   } else { 
12180     if ( (abs(v)>=coef_bound ) && mp->watch_coefs ) {
12181       type(qq)=independent_needing_fix; mp->fix_needed=true;
12182     }
12183     link(r)=s; r=s;
12184   }
12185   q=link(q); qq=info(q);
12186 }
12187
12188 @ A somewhat simpler routine will multiply a dependency list
12189 by a given constant~|v|. The constant is either a |fraction| less than
12190 |fraction_one|, or it is |scaled|. In the latter case we might be forced to
12191 convert a dependency list to a proto-dependency list.
12192 Parameters |t0| and |t1| are the list types before and after;
12193 they should agree unless |t0=mp_dependent| and |t1=mp_proto_dependent|
12194 and |v_is_scaled=true|.
12195
12196 @c pointer mp_p_times_v (MP mp,pointer p, integer v, small_number t0,
12197                          small_number t1, boolean v_is_scaled) {
12198   pointer r,s; /* for list manipulation */
12199   integer w; /* tentative coefficient */
12200   integer threshold;
12201   boolean scaling_down;
12202   if ( t0!=t1 ) scaling_down=true; else scaling_down=(!v_is_scaled);
12203   if ( t1==mp_dependent ) threshold=half_fraction_threshold;
12204   else threshold=half_scaled_threshold;
12205   r=temp_head;
12206   while ( info(p)!=null ) {    
12207     if ( scaling_down ) w=mp_take_fraction(mp, v,value(p));
12208     else w=mp_take_scaled(mp, v,value(p));
12209     if ( abs(w)<=threshold ) { 
12210       s=link(p); mp_free_node(mp, p,dep_node_size); p=s;
12211     } else {
12212       if ( abs(w)>=coef_bound ) { 
12213         mp->fix_needed=true; type(info(p))=independent_needing_fix;
12214       }
12215       link(r)=p; r=p; value(p)=w; p=link(p);
12216     }
12217   }
12218   link(r)=p;
12219   if ( v_is_scaled ) value(p)=mp_take_scaled(mp, value(p),v);
12220   else value(p)=mp_take_fraction(mp, value(p),v);
12221   return link(temp_head);
12222 }
12223
12224 @ Similarly, we sometimes need to divide a dependency list
12225 by a given |scaled| constant.
12226
12227 @<Declare basic dependency-list subroutines@>=
12228 pointer mp_p_over_v (MP mp,pointer p, scaled v, small_number 
12229   t0, small_number t1) ;
12230
12231 @ @c
12232 pointer mp_p_over_v (MP mp,pointer p, scaled v, small_number 
12233   t0, small_number t1) {
12234   pointer r,s; /* for list manipulation */
12235   integer w; /* tentative coefficient */
12236   integer threshold;
12237   boolean scaling_down;
12238   if ( t0!=t1 ) scaling_down=true; else scaling_down=false;
12239   if ( t1==mp_dependent ) threshold=half_fraction_threshold;
12240   else threshold=half_scaled_threshold;
12241   r=temp_head;
12242   while ( info( p)!=null ) {
12243     if ( scaling_down ) {
12244       if ( abs(v)<02000000 ) w=mp_make_scaled(mp, value(p),v*010000);
12245       else w=mp_make_scaled(mp, mp_round_fraction(mp, value(p)),v);
12246     } else {
12247       w=mp_make_scaled(mp, value(p),v);
12248     }
12249     if ( abs(w)<=threshold ) {
12250       s=link(p); mp_free_node(mp, p,dep_node_size); p=s;
12251     } else { 
12252       if ( abs(w)>=coef_bound ) {
12253          mp->fix_needed=true; type(info(p))=independent_needing_fix;
12254       }
12255       link(r)=p; r=p; value(p)=w; p=link(p);
12256     }
12257   }
12258   link(r)=p; value(p)=mp_make_scaled(mp, value(p),v);
12259   return link(temp_head);
12260 }
12261
12262 @ Here's another utility routine for dependency lists. When an independent
12263 variable becomes dependent, we want to remove it from all existing
12264 dependencies. The |p_with_x_becoming_q| function computes the
12265 dependency list of~|p| after variable~|x| has been replaced by~|q|.
12266
12267 This procedure has basically the same calling conventions as |p_plus_fq|:
12268 List~|q| is unchanged; list~|p| is destroyed; the constant node and the
12269 final link are inherited from~|p|; and the fourth parameter tells whether
12270 or not |p| is |mp_proto_dependent|. However, the global variable |dep_final|
12271 is not altered if |x| does not occur in list~|p|.
12272
12273 @c pointer mp_p_with_x_becoming_q (MP mp,pointer p,
12274            pointer x, pointer q, small_number t) {
12275   pointer r,s; /* for list manipulation */
12276   integer v; /* coefficient of |x| */
12277   integer sx; /* serial number of |x| */
12278   s=p; r=temp_head; sx=value(x);
12279   while ( value(info(s))>sx ) { r=s; s=link(s); };
12280   if ( info(s)!=x ) { 
12281     return p;
12282   } else { 
12283     link(temp_head)=p; link(r)=link(s); v=value(s);
12284     mp_free_node(mp, s,dep_node_size);
12285     return mp_p_plus_fq(mp, link(temp_head),v,q,t,mp_dependent);
12286   }
12287 }
12288
12289 @ Here's a simple procedure that reports an error when a variable
12290 has just received a known value that's out of the required range.
12291
12292 @<Declare basic dependency-list subroutines@>=
12293 void mp_val_too_big (MP mp,scaled x) ;
12294
12295 @ @c void mp_val_too_big (MP mp,scaled x) { 
12296   if ( mp->internal[mp_warning_check]>0 ) { 
12297     print_err("Value is too large ("); mp_print_scaled(mp, x); mp_print_char(mp, ')');
12298 @.Value is too large@>
12299     help4("The equation I just processed has given some variable")
12300       ("a value of 4096 or more. Continue and I'll try to cope")
12301       ("with that big value; but it might be dangerous.")
12302       ("(Set warningcheck:=0 to suppress this message.)");
12303     mp_error(mp);
12304   }
12305 }
12306
12307 @ When a dependent variable becomes known, the following routine
12308 removes its dependency list. Here |p| points to the variable, and
12309 |q| points to the dependency list (which is one node long).
12310
12311 @<Declare basic dependency-list subroutines@>=
12312 void mp_make_known (MP mp,pointer p, pointer q) ;
12313
12314 @ @c void mp_make_known (MP mp,pointer p, pointer q) {
12315   int t; /* the previous type */
12316   prev_dep(link(q))=prev_dep(p);
12317   link(prev_dep(p))=link(q); t=type(p);
12318   type(p)=mp_known; value(p)=value(q); mp_free_node(mp, q,dep_node_size);
12319   if ( abs(value(p))>=fraction_one ) mp_val_too_big(mp, value(p));
12320   if (( mp->internal[mp_tracing_equations]>0) && mp_interesting(mp, p) ) {
12321     mp_begin_diagnostic(mp); mp_print_nl(mp, "#### ");
12322 @:]]]\#\#\#\#_}{\.{\#\#\#\#}@>
12323     mp_print_variable_name(mp, p); 
12324     mp_print_char(mp, '='); mp_print_scaled(mp, value(p));
12325     mp_end_diagnostic(mp, false);
12326   }
12327   if (( mp->cur_exp==p ) && mp->cur_type==t ) {
12328     mp->cur_type=mp_known; mp->cur_exp=value(p);
12329     mp_free_node(mp, p,value_node_size);
12330   }
12331 }
12332
12333 @ The |fix_dependencies| routine is called into action when |fix_needed|
12334 has been triggered. The program keeps a list~|s| of independent variables
12335 whose coefficients must be divided by~4.
12336
12337 In unusual cases, this fixup process might reduce one or more coefficients
12338 to zero, so that a variable will become known more or less by default.
12339
12340 @<Declare basic dependency-list subroutines@>=
12341 void mp_fix_dependencies (MP mp);
12342
12343 @ @c void mp_fix_dependencies (MP mp) {
12344   pointer p,q,r,s,t; /* list manipulation registers */
12345   pointer x; /* an independent variable */
12346   r=link(dep_head); s=null;
12347   while ( r!=dep_head ){ 
12348     t=r;
12349     @<Run through the dependency list for variable |t|, fixing
12350       all nodes, and ending with final link~|q|@>;
12351     r=link(q);
12352     if ( q==dep_list(t) ) mp_make_known(mp, t,q);
12353   }
12354   while ( s!=null ) { 
12355     p=link(s); x=info(s); free_avail(s); s=p;
12356     type(x)=mp_independent; value(x)=value(x)+2;
12357   }
12358   mp->fix_needed=false;
12359 }
12360
12361 @ @d independent_being_fixed 1 /* this variable already appears in |s| */
12362
12363 @<Run through the dependency list for variable |t|...@>=
12364 r=value_loc(t); /* |link(r)=dep_list(t)| */
12365 while (1) { 
12366   q=link(r); x=info(q);
12367   if ( x==null ) break;
12368   if ( type(x)<=independent_being_fixed ) {
12369     if ( type(x)<independent_being_fixed ) {
12370       p=mp_get_avail(mp); link(p)=s; s=p;
12371       info(s)=x; type(x)=independent_being_fixed;
12372     }
12373     value(q)=value(q) / 4;
12374     if ( value(q)==0 ) {
12375       link(r)=link(q); mp_free_node(mp, q,dep_node_size); q=r;
12376     }
12377   }
12378   r=q;
12379 }
12380
12381
12382 @ The |new_dep| routine installs a dependency list~|p| into the value node~|q|,
12383 linking it into the list of all known dependencies. We assume that
12384 |dep_final| points to the final node of list~|p|.
12385
12386 @c void mp_new_dep (MP mp,pointer q, pointer p) {
12387   pointer r; /* what used to be the first dependency */
12388   dep_list(q)=p; prev_dep(q)=dep_head;
12389   r=link(dep_head); link(mp->dep_final)=r; prev_dep(r)=mp->dep_final;
12390   link(dep_head)=q;
12391 }
12392
12393 @ Here is one of the ways a dependency list gets started.
12394 The |const_dependency| routine produces a list that has nothing but
12395 a constant term.
12396
12397 @c pointer mp_const_dependency (MP mp, scaled v) {
12398   mp->dep_final=mp_get_node(mp, dep_node_size);
12399   value(mp->dep_final)=v; info(mp->dep_final)=null;
12400   return mp->dep_final;
12401 }
12402
12403 @ And here's a more interesting way to start a dependency list from scratch:
12404 The parameter to |single_dependency| is the location of an
12405 independent variable~|x|, and the result is the simple dependency list
12406 `|x+0|'.
12407
12408 In the unlikely event that the given independent variable has been doubled so
12409 often that we can't refer to it with a nonzero coefficient,
12410 |single_dependency| returns the simple list `0'.  This case can be
12411 recognized by testing that the returned list pointer is equal to
12412 |dep_final|.
12413
12414 @c pointer mp_single_dependency (MP mp,pointer p) {
12415   pointer q; /* the new dependency list */
12416   integer m; /* the number of doublings */
12417   m=value(p) % s_scale;
12418   if ( m>28 ) {
12419     return mp_const_dependency(mp, 0);
12420   } else { 
12421     q=mp_get_node(mp, dep_node_size);
12422     value(q)=two_to_the(28-m); info(q)=p;
12423     link(q)=mp_const_dependency(mp, 0);
12424     return q;
12425   }
12426 }
12427
12428 @ We sometimes need to make an exact copy of a dependency list.
12429
12430 @c pointer mp_copy_dep_list (MP mp,pointer p) {
12431   pointer q; /* the new dependency list */
12432   q=mp_get_node(mp, dep_node_size); mp->dep_final=q;
12433   while (1) { 
12434     info(mp->dep_final)=info(p); value(mp->dep_final)=value(p);
12435     if ( info(mp->dep_final)==null ) break;
12436     link(mp->dep_final)=mp_get_node(mp, dep_node_size);
12437     mp->dep_final=link(mp->dep_final); p=link(p);
12438   }
12439   return q;
12440 }
12441
12442 @ But how do variables normally become known? Ah, now we get to the heart of the
12443 equation-solving mechanism. The |linear_eq| procedure is given a |dependent|
12444 or |mp_proto_dependent| list,~|p|, in which at least one independent variable
12445 appears. It equates this list to zero, by choosing an independent variable
12446 with the largest coefficient and making it dependent on the others. The
12447 newly dependent variable is eliminated from all current dependencies,
12448 thereby possibly making other dependent variables known.
12449
12450 The given list |p| is, of course, totally destroyed by all this processing.
12451
12452 @c void mp_linear_eq (MP mp, pointer p, small_number t) {
12453   pointer q,r,s; /* for link manipulation */
12454   pointer x; /* the variable that loses its independence */
12455   integer n; /* the number of times |x| had been halved */
12456   integer v; /* the coefficient of |x| in list |p| */
12457   pointer prev_r; /* lags one step behind |r| */
12458   pointer final_node; /* the constant term of the new dependency list */
12459   integer w; /* a tentative coefficient */
12460    @<Find a node |q| in list |p| whose coefficient |v| is largest@>;
12461   x=info(q); n=value(x) % s_scale;
12462   @<Divide list |p| by |-v|, removing node |q|@>;
12463   if ( mp->internal[mp_tracing_equations]>0 ) {
12464     @<Display the new dependency@>;
12465   }
12466   @<Simplify all existing dependencies by substituting for |x|@>;
12467   @<Change variable |x| from |independent| to |dependent| or |known|@>;
12468   if ( mp->fix_needed ) mp_fix_dependencies(mp);
12469 }
12470
12471 @ @<Find a node |q| in list |p| whose coefficient |v| is largest@>=
12472 q=p; r=link(p); v=value(q);
12473 while ( info(r)!=null ) { 
12474   if ( abs(value(r))>abs(v) ) { q=r; v=value(r); };
12475   r=link(r);
12476 }
12477
12478 @ Here we want to change the coefficients from |scaled| to |fraction|,
12479 except in the constant term. In the common case of a trivial equation
12480 like `\.{x=3.14}', we will have |v=-fraction_one|, |q=p|, and |t=mp_dependent|.
12481
12482 @<Divide list |p| by |-v|, removing node |q|@>=
12483 s=temp_head; link(s)=p; r=p;
12484 do { 
12485   if ( r==q ) {
12486     link(s)=link(r); mp_free_node(mp, r,dep_node_size);
12487   } else  { 
12488     w=mp_make_fraction(mp, value(r),v);
12489     if ( abs(w)<=half_fraction_threshold ) {
12490       link(s)=link(r); mp_free_node(mp, r,dep_node_size);
12491     } else { 
12492       value(r)=-w; s=r;
12493     }
12494   }
12495   r=link(s);
12496 } while (info(r)!=null);
12497 if ( t==mp_proto_dependent ) {
12498   value(r)=-mp_make_scaled(mp, value(r),v);
12499 } else if ( v!=-fraction_one ) {
12500   value(r)=-mp_make_fraction(mp, value(r),v);
12501 }
12502 final_node=r; p=link(temp_head)
12503
12504 @ @<Display the new dependency@>=
12505 if ( mp_interesting(mp, x) ) {
12506   mp_begin_diagnostic(mp); mp_print_nl(mp, "## "); 
12507   mp_print_variable_name(mp, x);
12508 @:]]]\#\#_}{\.{\#\#}@>
12509   w=n;
12510   while ( w>0 ) { mp_print(mp, "*4"); w=w-2;  };
12511   mp_print_char(mp, '='); mp_print_dependency(mp, p,mp_dependent); 
12512   mp_end_diagnostic(mp, false);
12513 }
12514
12515 @ @<Simplify all existing dependencies by substituting for |x|@>=
12516 prev_r=dep_head; r=link(dep_head);
12517 while ( r!=dep_head ) {
12518   s=dep_list(r); q=mp_p_with_x_becoming_q(mp, s,x,p,type(r));
12519   if ( info(q)==null ) {
12520     mp_make_known(mp, r,q);
12521   } else { 
12522     dep_list(r)=q;
12523     do {  q=link(q); } while (info(q)!=null);
12524     prev_r=q;
12525   }
12526   r=link(prev_r);
12527 }
12528
12529 @ @<Change variable |x| from |independent| to |dependent| or |known|@>=
12530 if ( n>0 ) @<Divide list |p| by $2^n$@>;
12531 if ( info(p)==null ) {
12532   type(x)=mp_known;
12533   value(x)=value(p);
12534   if ( abs(value(x))>=fraction_one ) mp_val_too_big(mp, value(x));
12535   mp_free_node(mp, p,dep_node_size);
12536   if ( mp->cur_exp==x ) if ( mp->cur_type==mp_independent ) {
12537     mp->cur_exp=value(x); mp->cur_type=mp_known;
12538     mp_free_node(mp, x,value_node_size);
12539   }
12540 } else { 
12541   type(x)=mp_dependent; mp->dep_final=final_node; mp_new_dep(mp, x,p);
12542   if ( mp->cur_exp==x ) if ( mp->cur_type==mp_independent ) mp->cur_type=mp_dependent;
12543 }
12544
12545 @ @<Divide list |p| by $2^n$@>=
12546
12547   s=temp_head; link(temp_head)=p; r=p;
12548   do {  
12549     if ( n>30 ) w=0;
12550     else w=value(r) / two_to_the(n);
12551     if ( (abs(w)<=half_fraction_threshold)&&(info(r)!=null) ) {
12552       link(s)=link(r);
12553       mp_free_node(mp, r,dep_node_size);
12554     } else { 
12555       value(r)=w; s=r;
12556     }
12557     r=link(s);
12558   } while (info(s)!=null);
12559   p=link(temp_head);
12560 }
12561
12562 @ The |check_mem| procedure, which is used only when \MP\ is being
12563 debugged, makes sure that the current dependency lists are well formed.
12564
12565 @<Check the list of linear dependencies@>=
12566 q=dep_head; p=link(q);
12567 while ( p!=dep_head ) {
12568   if ( prev_dep(p)!=q ) {
12569     mp_print_nl(mp, "Bad PREVDEP at "); mp_print_int(mp, p);
12570 @.Bad PREVDEP...@>
12571   }
12572   p=dep_list(p);
12573   while (1) {
12574     r=info(p); q=p; p=link(q);
12575     if ( r==null ) break;
12576     if ( value(info(p))>=value(r) ) {
12577       mp_print_nl(mp, "Out of order at "); mp_print_int(mp, p);
12578 @.Out of order...@>
12579     }
12580   }
12581 }
12582
12583 @* \[25] Dynamic nonlinear equations.
12584 Variables of numeric type are maintained by the general scheme of
12585 independent, dependent, and known values that we have just studied;
12586 and the components of pair and transform variables are handled in the
12587 same way. But \MP\ also has five other types of values: \&{boolean},
12588 \&{string}, \&{pen}, \&{path}, and \&{picture}; what about them?
12589
12590 Equations are allowed between nonlinear quantities, but only in a
12591 simple form. Two variables that haven't yet been assigned values are
12592 either equal to each other, or they're not.
12593
12594 Before a boolean variable has received a value, its type is |mp_unknown_boolean|;
12595 similarly, there are variables whose type is |mp_unknown_string|, |mp_unknown_pen|,
12596 |mp_unknown_path|, and |mp_unknown_picture|. In such cases the value is either
12597 |null| (which means that no other variables are equivalent to this one), or
12598 it points to another variable of the same undefined type. The pointers in the
12599 latter case form a cycle of nodes, which we shall call a ``ring.''
12600 Rings of undefined variables may include capsules, which arise as
12601 intermediate results within expressions or as \&{expr} parameters to macros.
12602
12603 When one member of a ring receives a value, the same value is given to
12604 all the other members. In the case of paths and pictures, this implies
12605 making separate copies of a potentially large data structure; users should
12606 restrain their enthusiasm for such generality, unless they have lots and
12607 lots of memory space.
12608
12609 @ The following procedure is called when a capsule node is being
12610 added to a ring (e.g., when an unknown variable is mentioned in an expression).
12611
12612 @c pointer mp_new_ring_entry (MP mp,pointer p) {
12613   pointer q; /* the new capsule node */
12614   q=mp_get_node(mp, value_node_size); name_type(q)=mp_capsule;
12615   type(q)=type(p);
12616   if ( value(p)==null ) value(q)=p; else value(q)=value(p);
12617   value(p)=q;
12618   return q;
12619 }
12620
12621 @ Conversely, we might delete a capsule or a variable before it becomes known.
12622 The following procedure simply detaches a quantity from its ring,
12623 without recycling the storage.
12624
12625 @<Declare the recycling subroutines@>=
12626 void mp_ring_delete (MP mp,pointer p) {
12627   pointer q; 
12628   q=value(p);
12629   if ( q!=null ) if ( q!=p ){ 
12630     while ( value(q)!=p ) q=value(q);
12631     value(q)=value(p);
12632   }
12633 }
12634
12635 @ Eventually there might be an equation that assigns values to all of the
12636 variables in a ring. The |nonlinear_eq| subroutine does the necessary
12637 propagation of values.
12638
12639 If the parameter |flush_p| is |true|, node |p| itself needn't receive a
12640 value, it will soon be recycled.
12641
12642 @c void mp_nonlinear_eq (MP mp,integer v, pointer p, boolean flush_p) {
12643   small_number t; /* the type of ring |p| */
12644   pointer q,r; /* link manipulation registers */
12645   t=type(p)-unknown_tag; q=value(p);
12646   if ( flush_p ) type(p)=mp_vacuous; else p=q;
12647   do {  
12648     r=value(q); type(q)=t;
12649     switch (t) {
12650     case mp_boolean_type: value(q)=v; break;
12651     case mp_string_type: value(q)=v; add_str_ref(v); break;
12652     case mp_pen_type: value(q)=copy_pen(v); break;
12653     case mp_path_type: value(q)=mp_copy_path(mp, v); break;
12654     case mp_picture_type: value(q)=v; add_edge_ref(v); break;
12655     } /* there ain't no more cases */
12656     q=r;
12657   } while (q!=p);
12658 }
12659
12660 @ If two members of rings are equated, and if they have the same type,
12661 the |ring_merge| procedure is called on to make them equivalent.
12662
12663 @c void mp_ring_merge (MP mp,pointer p, pointer q) {
12664   pointer r; /* traverses one list */
12665   r=value(p);
12666   while ( r!=p ) {
12667     if ( r==q ) {
12668       @<Exclaim about a redundant equation@>;
12669       return;
12670     };
12671     r=value(r);
12672   }
12673   r=value(p); value(p)=value(q); value(q)=r;
12674 }
12675
12676 @ @<Exclaim about a redundant equation@>=
12677
12678   print_err("Redundant equation");
12679 @.Redundant equation@>
12680   help2("I already knew that this equation was true.")
12681    ("But perhaps no harm has been done; let's continue.");
12682   mp_put_get_error(mp);
12683 }
12684
12685 @* \[26] Introduction to the syntactic routines.
12686 Let's pause a moment now and try to look at the Big Picture.
12687 The \MP\ program consists of three main parts: syntactic routines,
12688 semantic routines, and output routines. The chief purpose of the
12689 syntactic routines is to deliver the user's input to the semantic routines,
12690 while parsing expressions and locating operators and operands. The
12691 semantic routines act as an interpreter responding to these operators,
12692 which may be regarded as commands. And the output routines are
12693 periodically called on to produce compact font descriptions that can be
12694 used for typesetting or for making interim proof drawings. We have
12695 discussed the basic data structures and many of the details of semantic
12696 operations, so we are good and ready to plunge into the part of \MP\ that
12697 actually controls the activities.
12698
12699 Our current goal is to come to grips with the |get_next| procedure,
12700 which is the keystone of \MP's input mechanism. Each call of |get_next|
12701 sets the value of three variables |cur_cmd|, |cur_mod|, and |cur_sym|,
12702 representing the next input token.
12703 $$\vbox{\halign{#\hfil\cr
12704   \hbox{|cur_cmd| denotes a command code from the long list of codes
12705    given earlier;}\cr
12706   \hbox{|cur_mod| denotes a modifier of the command code;}\cr
12707   \hbox{|cur_sym| is the hash address of the symbolic token that was
12708    just scanned,}\cr
12709   \hbox{\qquad or zero in the case of a numeric or string
12710    or capsule token.}\cr}}$$
12711 Underlying this external behavior of |get_next| is all the machinery
12712 necessary to convert from character files to tokens. At a given time we
12713 may be only partially finished with the reading of several files (for
12714 which \&{input} was specified), and partially finished with the expansion
12715 of some user-defined macros and/or some macro parameters, and partially
12716 finished reading some text that the user has inserted online,
12717 and so on. When reading a character file, the characters must be
12718 converted to tokens; comments and blank spaces must
12719 be removed, numeric and string tokens must be evaluated.
12720
12721 To handle these situations, which might all be present simultaneously,
12722 \MP\ uses various stacks that hold information about the incomplete
12723 activities, and there is a finite state control for each level of the
12724 input mechanism. These stacks record the current state of an implicitly
12725 recursive process, but the |get_next| procedure is not recursive.
12726
12727 @<Glob...@>=
12728 eight_bits cur_cmd; /* current command set by |get_next| */
12729 integer cur_mod; /* operand of current command */
12730 halfword cur_sym; /* hash address of current symbol */
12731
12732 @ The |print_cmd_mod| routine prints a symbolic interpretation of a
12733 command code and its modifier.
12734 It consists of a rather tedious sequence of print
12735 commands, and most of it is essentially an inverse to the |primitive|
12736 routine that enters a \MP\ primitive into |hash| and |eqtb|. Therefore almost
12737 all of this procedure appears elsewhere in the program, together with the
12738 corresponding |primitive| calls.
12739
12740 @<Declare the procedure called |print_cmd_mod|@>=
12741 void mp_print_cmd_mod (MP mp,integer c, integer m) { 
12742  switch (c) {
12743   @<Cases of |print_cmd_mod| for symbolic printing of primitives@>
12744   default: mp_print(mp, "[unknown command code!]"); break;
12745   }
12746 }
12747
12748 @ Here is a procedure that displays a given command in braces, in the
12749 user's transcript file.
12750
12751 @d show_cur_cmd_mod mp_show_cmd_mod(mp, mp->cur_cmd,mp->cur_mod)
12752
12753 @c 
12754 void mp_show_cmd_mod (MP mp,integer c, integer m) { 
12755   mp_begin_diagnostic(mp); mp_print_nl(mp, "{");
12756   mp_print_cmd_mod(mp, c,m); mp_print_char(mp, '}');
12757   mp_end_diagnostic(mp, false);
12758 }
12759
12760 @* \[27] Input stacks and states.
12761 The state of \MP's input mechanism appears in the input stack, whose
12762 entries are records with five fields, called |index|, |start|, |loc|,
12763 |limit|, and |name|. The top element of this stack is maintained in a
12764 global variable for which no subscripting needs to be done; the other
12765 elements of the stack appear in an array. Hence the stack is declared thus:
12766
12767 @<Types...@>=
12768 typedef struct {
12769   quarterword index_field;
12770   halfword start_field, loc_field, limit_field, name_field;
12771 } in_state_record;
12772
12773 @ @<Glob...@>=
12774 in_state_record *input_stack;
12775 integer input_ptr; /* first unused location of |input_stack| */
12776 integer max_in_stack; /* largest value of |input_ptr| when pushing */
12777 in_state_record cur_input; /* the ``top'' input state */
12778 int stack_size; /* maximum number of simultaneous input sources */
12779
12780 @ @<Allocate or initialize ...@>=
12781 mp->stack_size = 300;
12782 mp->input_stack = xmalloc((mp->stack_size+1),sizeof(in_state_record));
12783
12784 @ @<Dealloc variables@>=
12785 xfree(mp->input_stack);
12786
12787 @ We've already defined the special variable |loc==cur_input.loc_field|
12788 in our discussion of basic input-output routines. The other components of
12789 |cur_input| are defined in the same way:
12790
12791 @d index mp->cur_input.index_field /* reference for buffer information */
12792 @d start mp->cur_input.start_field /* starting position in |buffer| */
12793 @d limit mp->cur_input.limit_field /* end of current line in |buffer| */
12794 @d name mp->cur_input.name_field /* name of the current file */
12795
12796 @ Let's look more closely now at the five control variables
12797 (|index|,~|start|,~|loc|,~|limit|,~|name|),
12798 assuming that \MP\ is reading a line of characters that have been input
12799 from some file or from the user's terminal. There is an array called
12800 |buffer| that acts as a stack of all lines of characters that are
12801 currently being read from files, including all lines on subsidiary
12802 levels of the input stack that are not yet completed. \MP\ will return to
12803 the other lines when it is finished with the present input file.
12804
12805 (Incidentally, on a machine with byte-oriented addressing, it would be
12806 appropriate to combine |buffer| with the |str_pool| array,
12807 letting the buffer entries grow downward from the top of the string pool
12808 and checking that these two tables don't bump into each other.)
12809
12810 The line we are currently working on begins in position |start| of the
12811 buffer; the next character we are about to read is |buffer[loc]|; and
12812 |limit| is the location of the last character present. We always have
12813 |loc<=limit|. For convenience, |buffer[limit]| has been set to |"%"|, so
12814 that the end of a line is easily sensed.
12815
12816 The |name| variable is a string number that designates the name of
12817 the current file, if we are reading an ordinary text file.  Special codes
12818 |is_term..max_spec_src| indicate other sources of input text.
12819
12820 @d is_term 0 /* |name| value when reading from the terminal for normal input */
12821 @d is_read 1 /* |name| value when executing a \&{readstring} or \&{readfrom} */
12822 @d is_scantok 2 /* |name| value when reading text generated by \&{scantokens} */
12823 @d max_spec_src is_scantok
12824
12825 @ Additional information about the current line is available via the
12826 |index| variable, which counts how many lines of characters are present
12827 in the buffer below the current level. We have |index=0| when reading
12828 from the terminal and prompting the user for each line; then if the user types,
12829 e.g., `\.{input figs}', we will have |index=1| while reading
12830 the file \.{figs.mp}. However, it does not follow that |index| is the
12831 same as the input stack pointer, since many of the levels on the input
12832 stack may come from token lists and some |index| values may correspond
12833 to \.{MPX} files that are not currently on the stack.
12834
12835 The global variable |in_open| is equal to the highest |index| value counting
12836 \.{MPX} files but excluding token-list input levels.  Thus, the number of
12837 partially read lines in the buffer is |in_open+1| and we have |in_open>=index|
12838 when we are not reading a token list.
12839
12840 If we are not currently reading from the terminal,
12841 we are reading from the file variable |input_file[index]|. We use
12842 the notation |terminal_input| as a convenient abbreviation for |name=is_term|,
12843 and |cur_file| as an abbreviation for |input_file[index]|.
12844
12845 When \MP\ is not reading from the terminal, the global variable |line| contains
12846 the line number in the current file, for use in error messages. More precisely,
12847 |line| is a macro for |line_stack[index]| and the |line_stack| array gives
12848 the line number for each file in the |input_file| array.
12849
12850 When an \.{MPX} file is opened the file name is stored in the |mpx_name|
12851 array so that the name doesn't get lost when the file is temporarily removed
12852 from the input stack.
12853 Thus when |input_file[k]| is an \.{MPX} file, its name is |mpx_name[k]|
12854 and it contains translated \TeX\ pictures for |input_file[k-1]|.
12855 Since this is not an \.{MPX} file, we have
12856 $$ \hbox{|mpx_name[k-1]<=absent|}. $$
12857 This |name| field is set to |finished| when |input_file[k]| is completely
12858 read.
12859
12860 If more information about the input state is needed, it can be
12861 included in small arrays like those shown here. For example,
12862 the current page or segment number in the input file might be put
12863 into a variable |page|, that is really a macro for the current entry
12864 in `\ignorespaces|page_stack:array[0..max_in_open] of integer|\unskip'
12865 by analogy with |line_stack|.
12866 @^system dependencies@>
12867
12868 @d terminal_input (name==is_term) /* are we reading from the terminal? */
12869 @d cur_file mp->input_file[index] /* the current |void *| variable */
12870 @d line mp->line_stack[index] /* current line number in the current source file */
12871 @d in_name mp->iname_stack[index] /* a string used to construct \.{MPX} file names */
12872 @d in_area mp->iarea_stack[index] /* another string for naming \.{MPX} files */
12873 @d absent 1 /* |name_field| value for unused |mpx_in_stack| entries */
12874 @d mpx_reading (mp->mpx_name[index]>absent)
12875   /* when reading a file, is it an \.{MPX} file? */
12876 @d finished 0
12877   /* |name_field| value when the corresponding \.{MPX} file is finished */
12878
12879 @<Glob...@>=
12880 integer in_open; /* the number of lines in the buffer, less one */
12881 unsigned int open_parens; /* the number of open text files */
12882 void  * *input_file ;
12883 integer *line_stack ; /* the line number for each file */
12884 char *  *iname_stack; /* used for naming \.{MPX} files */
12885 char *  *iarea_stack; /* used for naming \.{MPX} files */
12886 halfword*mpx_name  ;
12887
12888 @ @<Allocate or ...@>=
12889 mp->input_file  = xmalloc((mp->max_in_open+1),sizeof(void *));
12890 mp->line_stack  = xmalloc((mp->max_in_open+1),sizeof(integer));
12891 mp->iname_stack = xmalloc((mp->max_in_open+1),sizeof(char *));
12892 mp->iarea_stack = xmalloc((mp->max_in_open+1),sizeof(char *));
12893 mp->mpx_name    = xmalloc((mp->max_in_open+1),sizeof(halfword));
12894 {
12895   int k;
12896   for (k=0;k<=mp->max_in_open;k++) {
12897     mp->iname_stack[k] =NULL;
12898     mp->iarea_stack[k] =NULL;
12899   }
12900 }
12901
12902 @ @<Dealloc variables@>=
12903 {
12904   int l;
12905   for (l=0;l<=mp->max_in_open;l++) {
12906     xfree(mp->iname_stack[l]);
12907     xfree(mp->iarea_stack[l]);
12908   }
12909 }
12910 xfree(mp->input_file);
12911 xfree(mp->line_stack);
12912 xfree(mp->iname_stack);
12913 xfree(mp->iarea_stack);
12914 xfree(mp->mpx_name);
12915
12916
12917 @ However, all this discussion about input state really applies only to the
12918 case that we are inputting from a file. There is another important case,
12919 namely when we are currently getting input from a token list. In this case
12920 |index>max_in_open|, and the conventions about the other state variables
12921 are different:
12922
12923 \yskip\hang|loc| is a pointer to the current node in the token list, i.e.,
12924 the node that will be read next. If |loc=null|, the token list has been
12925 fully read.
12926
12927 \yskip\hang|start| points to the first node of the token list; this node
12928 may or may not contain a reference count, depending on the type of token
12929 list involved.
12930
12931 \yskip\hang|token_type|, which takes the place of |index| in the
12932 discussion above, is a code number that explains what kind of token list
12933 is being scanned.
12934
12935 \yskip\hang|name| points to the |eqtb| address of the control sequence
12936 being expanded, if the current token list is a macro not defined by
12937 \&{vardef}. Macros defined by \&{vardef} have |name=null|; their name
12938 can be deduced by looking at their first two parameters.
12939
12940 \yskip\hang|param_start|, which takes the place of |limit|, tells where
12941 the parameters of the current macro or loop text begin in the |param_stack|.
12942
12943 \yskip\noindent The |token_type| can take several values, depending on
12944 where the current token list came from:
12945
12946 \yskip
12947 \indent|forever_text|, if the token list being scanned is the body of
12948 a \&{forever} loop;
12949
12950 \indent|loop_text|, if the token list being scanned is the body of
12951 a \&{for} or \&{forsuffixes} loop;
12952
12953 \indent|parameter|, if a \&{text} or \&{suffix} parameter is being scanned;
12954
12955 \indent|backed_up|, if the token list being scanned has been inserted as
12956 `to be read again'.
12957
12958 \indent|inserted|, if the token list being scanned has been inserted as
12959 part of error recovery;
12960
12961 \indent|macro|, if the expansion of a user-defined symbolic token is being
12962 scanned.
12963
12964 \yskip\noindent
12965 The token list begins with a reference count if and only if |token_type=
12966 macro|.
12967 @^reference counts@>
12968
12969 @d token_type index /* type of current token list */
12970 @d token_state (index>(int)mp->max_in_open) /* are we scanning a token list? */
12971 @d file_state (index<=(int)mp->max_in_open) /* are we scanning a file line? */
12972 @d param_start limit /* base of macro parameters in |param_stack| */
12973 @d forever_text (mp->max_in_open+1) /* |token_type| code for loop texts */
12974 @d loop_text (mp->max_in_open+2) /* |token_type| code for loop texts */
12975 @d parameter (mp->max_in_open+3) /* |token_type| code for parameter texts */
12976 @d backed_up (mp->max_in_open+4) /* |token_type| code for texts to be reread */
12977 @d inserted (mp->max_in_open+5) /* |token_type| code for inserted texts */
12978 @d macro (mp->max_in_open+6) /* |token_type| code for macro replacement texts */
12979
12980 @ The |param_stack| is an auxiliary array used to hold pointers to the token
12981 lists for parameters at the current level and subsidiary levels of input.
12982 This stack grows at a different rate from the others.
12983
12984 @<Glob...@>=
12985 pointer *param_stack;  /* token list pointers for parameters */
12986 integer param_ptr; /* first unused entry in |param_stack| */
12987 integer max_param_stack;  /* largest value of |param_ptr| */
12988
12989 @ @<Allocate or initialize ...@>=
12990 mp->param_stack = xmalloc((mp->param_size+1),sizeof(pointer));
12991
12992 @ @<Dealloc variables@>=
12993 xfree(mp->param_stack);
12994
12995 @ Notice that the |line| isn't valid when |token_state| is true because it
12996 depends on |index|.  If we really need to know the line number for the
12997 topmost file in the index stack we use the following function.  If a page
12998 number or other information is needed, this routine should be modified to
12999 compute it as well.
13000 @^system dependencies@>
13001
13002 @<Declare a function called |true_line|@>=
13003 integer mp_true_line (MP mp) {
13004   int k; /* an index into the input stack */
13005   if ( file_state && (name>max_spec_src) ) {
13006     return line;
13007   } else { 
13008     k=mp->input_ptr;
13009     while ((k>0) &&
13010            ((mp->input_stack[(k-1)].index_field>mp->max_in_open)||
13011             (mp->input_stack[(k-1)].name_field<=max_spec_src))) {
13012       decr(k);
13013     }
13014     return (k>0 ? mp->line_stack[(k-1)] : 0 );
13015   }
13016 }
13017
13018 @ Thus, the ``current input state'' can be very complicated indeed; there
13019 can be many levels and each level can arise in a variety of ways. The
13020 |show_context| procedure, which is used by \MP's error-reporting routine to
13021 print out the current input state on all levels down to the most recent
13022 line of characters from an input file, illustrates most of these conventions.
13023 The global variable |file_ptr| contains the lowest level that was
13024 displayed by this procedure.
13025
13026 @<Glob...@>=
13027 integer file_ptr; /* shallowest level shown by |show_context| */
13028
13029 @ The status at each level is indicated by printing two lines, where the first
13030 line indicates what was read so far and the second line shows what remains
13031 to be read. The context is cropped, if necessary, so that the first line
13032 contains at most |half_error_line| characters, and the second contains
13033 at most |error_line|. Non-current input levels whose |token_type| is
13034 `|backed_up|' are shown only if they have not been fully read.
13035
13036 @c void mp_show_context (MP mp) { /* prints where the scanner is */
13037   int old_setting; /* saved |selector| setting */
13038   @<Local variables for formatting calculations@>
13039   mp->file_ptr=mp->input_ptr; mp->input_stack[mp->file_ptr]=mp->cur_input;
13040   /* store current state */
13041   while (1) { 
13042     mp->cur_input=mp->input_stack[mp->file_ptr]; /* enter into the context */
13043     @<Display the current context@>;
13044     if ( file_state )
13045       if ( (name>max_spec_src) || (mp->file_ptr==0) ) break;
13046     decr(mp->file_ptr);
13047   }
13048   mp->cur_input=mp->input_stack[mp->input_ptr]; /* restore original state */
13049 }
13050
13051 @ @<Display the current context@>=
13052 if ( (mp->file_ptr==mp->input_ptr) || file_state ||
13053    (token_type!=backed_up) || (loc!=null) ) {
13054     /* we omit backed-up token lists that have already been read */
13055   mp->tally=0; /* get ready to count characters */
13056   old_setting=mp->selector;
13057   if ( file_state ) {
13058     @<Print location of current line@>;
13059     @<Pseudoprint the line@>;
13060   } else { 
13061     @<Print type of token list@>;
13062     @<Pseudoprint the token list@>;
13063   }
13064   mp->selector=old_setting; /* stop pseudoprinting */
13065   @<Print two lines using the tricky pseudoprinted information@>;
13066 }
13067
13068 @ This routine should be changed, if necessary, to give the best possible
13069 indication of where the current line resides in the input file.
13070 For example, on some systems it is best to print both a page and line number.
13071 @^system dependencies@>
13072
13073 @<Print location of current line@>=
13074 if ( name>max_spec_src ) {
13075   mp_print_nl(mp, "l."); mp_print_int(mp, mp_true_line(mp));
13076 } else if ( terminal_input ) {
13077   if ( mp->file_ptr==0 ) mp_print_nl(mp, "<*>");
13078   else mp_print_nl(mp, "<insert>");
13079 } else if ( name==is_scantok ) {
13080   mp_print_nl(mp, "<scantokens>");
13081 } else {
13082   mp_print_nl(mp, "<read>");
13083 }
13084 mp_print_char(mp, ' ')
13085
13086 @ Can't use case statement here because the |token_type| is not
13087 a constant expression.
13088
13089 @<Print type of token list@>=
13090 {
13091   if(token_type==forever_text) {
13092     mp_print_nl(mp, "<forever> ");
13093   } else if (token_type==loop_text) {
13094     @<Print the current loop value@>;
13095   } else if (token_type==parameter) {
13096     mp_print_nl(mp, "<argument> "); 
13097   } else if (token_type==backed_up) { 
13098     if ( loc==null ) mp_print_nl(mp, "<recently read> ");
13099     else mp_print_nl(mp, "<to be read again> ");
13100   } else if (token_type==inserted) {
13101     mp_print_nl(mp, "<inserted text> ");
13102   } else if (token_type==macro) {
13103     mp_print_ln(mp);
13104     if ( name!=null ) mp_print_text(name);
13105     else @<Print the name of a \&{vardef}'d macro@>;
13106     mp_print(mp, "->");
13107   } else {
13108     mp_print_nl(mp, "?");/* this should never happen */
13109 @.?\relax@>
13110   }
13111 }
13112
13113 @ The parameter that corresponds to a loop text is either a token list
13114 (in the case of \&{forsuffixes}) or a ``capsule'' (in the case of \&{for}).
13115 We'll discuss capsules later; for now, all we need to know is that
13116 the |link| field in a capsule parameter is |void| and that
13117 |print_exp(p,0)| displays the value of capsule~|p| in abbreviated form.
13118
13119 @<Print the current loop value@>=
13120 { mp_print_nl(mp, "<for("); p=mp->param_stack[param_start];
13121   if ( p!=null ) {
13122     if ( link(p)==mp_void ) mp_print_exp(mp, p,0); /* we're in a \&{for} loop */
13123     else mp_show_token_list(mp, p,null,20,mp->tally);
13124   }
13125   mp_print(mp, ")> ");
13126 }
13127
13128 @ The first two parameters of a macro defined by \&{vardef} will be token
13129 lists representing the macro's prefix and ``at point.'' By putting these
13130 together, we get the macro's full name.
13131
13132 @<Print the name of a \&{vardef}'d macro@>=
13133 { p=mp->param_stack[param_start];
13134   if ( p==null ) {
13135     mp_show_token_list(mp, mp->param_stack[param_start+1],null,20,mp->tally);
13136   } else { 
13137     q=p;
13138     while ( link(q)!=null ) q=link(q);
13139     link(q)=mp->param_stack[param_start+1];
13140     mp_show_token_list(mp, p,null,20,mp->tally);
13141     link(q)=null;
13142   }
13143 }
13144
13145 @ Now it is necessary to explain a little trick. We don't want to store a long
13146 string that corresponds to a token list, because that string might take up
13147 lots of memory; and we are printing during a time when an error message is
13148 being given, so we dare not do anything that might overflow one of \MP's
13149 tables. So `pseudoprinting' is the answer: We enter a mode of printing
13150 that stores characters into a buffer of length |error_line|, where character
13151 $k+1$ is placed into \hbox{|trick_buf[k mod error_line]|} if
13152 |k<trick_count|, otherwise character |k| is dropped. Initially we set
13153 |tally:=0| and |trick_count:=1000000|; then when we reach the
13154 point where transition from line 1 to line 2 should occur, we
13155 set |first_count:=tally| and |trick_count:=@tmax@>(error_line,
13156 tally+1+error_line-half_error_line)|. At the end of the
13157 pseudoprinting, the values of |first_count|, |tally|, and
13158 |trick_count| give us all the information we need to print the two lines,
13159 and all of the necessary text is in |trick_buf|.
13160
13161 Namely, let |l| be the length of the descriptive information that appears
13162 on the first line. The length of the context information gathered for that
13163 line is |k=first_count|, and the length of the context information
13164 gathered for line~2 is $m=\min(|tally|, |trick_count|)-k$. If |l+k<=h|,
13165 where |h=half_error_line|, we print |trick_buf[0..k-1]| after the
13166 descriptive information on line~1, and set |n:=l+k|; here |n| is the
13167 length of line~1. If $l+k>h$, some cropping is necessary, so we set |n:=h|
13168 and print `\.{...}' followed by
13169 $$\hbox{|trick_buf[(l+k-h+3)..k-1]|,}$$
13170 where subscripts of |trick_buf| are circular modulo |error_line|. The
13171 second line consists of |n|~spaces followed by |trick_buf[k..(k+m-1)]|,
13172 unless |n+m>error_line|; in the latter case, further cropping is done.
13173 This is easier to program than to explain.
13174
13175 @<Local variables for formatting...@>=
13176 int i; /* index into |buffer| */
13177 integer l; /* length of descriptive information on line 1 */
13178 integer m; /* context information gathered for line 2 */
13179 int n; /* length of line 1 */
13180 integer p; /* starting or ending place in |trick_buf| */
13181 integer q; /* temporary index */
13182
13183 @ The following code tells the print routines to gather
13184 the desired information.
13185
13186 @d begin_pseudoprint { 
13187   l=mp->tally; mp->tally=0; mp->selector=pseudo;
13188   mp->trick_count=1000000;
13189 }
13190 @d set_trick_count {
13191   mp->first_count=mp->tally;
13192   mp->trick_count=mp->tally+1+mp->error_line-mp->half_error_line;
13193   if ( mp->trick_count<mp->error_line ) mp->trick_count=mp->error_line;
13194 }
13195
13196 @ And the following code uses the information after it has been gathered.
13197
13198 @<Print two lines using the tricky pseudoprinted information@>=
13199 if ( mp->trick_count==1000000 ) set_trick_count;
13200   /* |set_trick_count| must be performed */
13201 if ( mp->tally<mp->trick_count ) m=mp->tally-mp->first_count;
13202 else m=mp->trick_count-mp->first_count; /* context on line 2 */
13203 if ( l+mp->first_count<=mp->half_error_line ) {
13204   p=0; n=l+mp->first_count;
13205 } else  { 
13206   mp_print(mp, "..."); p=l+mp->first_count-mp->half_error_line+3;
13207   n=mp->half_error_line;
13208 }
13209 for (q=p;q<=mp->first_count-1;q++) {
13210   mp_print_char(mp, mp->trick_buf[q % mp->error_line]);
13211 }
13212 mp_print_ln(mp);
13213 for (q=1;q<=n;q++) {
13214   mp_print_char(mp, ' '); /* print |n| spaces to begin line~2 */
13215 }
13216 if ( m+n<=mp->error_line ) p=mp->first_count+m; 
13217 else p=mp->first_count+(mp->error_line-n-3);
13218 for (q=mp->first_count;q<=p-1;q++) {
13219   mp_print_char(mp, mp->trick_buf[q % mp->error_line]);
13220 }
13221 if ( m+n>mp->error_line ) mp_print(mp, "...")
13222
13223 @ But the trick is distracting us from our current goal, which is to
13224 understand the input state. So let's concentrate on the data structures that
13225 are being pseudoprinted as we finish up the |show_context| procedure.
13226
13227 @<Pseudoprint the line@>=
13228 begin_pseudoprint;
13229 if ( limit>0 ) {
13230   for (i=start;i<=limit-1;i++) {
13231     if ( i==loc ) set_trick_count;
13232     mp_print_str(mp, mp->buffer[i]);
13233   }
13234 }
13235
13236 @ @<Pseudoprint the token list@>=
13237 begin_pseudoprint;
13238 if ( token_type!=macro ) mp_show_token_list(mp, start,loc,100000,0);
13239 else mp_show_macro(mp, start,loc,100000)
13240
13241 @ Here is the missing piece of |show_token_list| that is activated when the
13242 token beginning line~2 is about to be shown:
13243
13244 @<Do magic computation@>=set_trick_count
13245
13246 @* \[28] Maintaining the input stacks.
13247 The following subroutines change the input status in commonly needed ways.
13248
13249 First comes |push_input|, which stores the current state and creates a
13250 new level (having, initially, the same properties as the old).
13251
13252 @d push_input  { /* enter a new input level, save the old */
13253   if ( mp->input_ptr>mp->max_in_stack ) {
13254     mp->max_in_stack=mp->input_ptr;
13255     if ( mp->input_ptr==mp->stack_size ) {
13256       int l = (mp->stack_size+(mp->stack_size>>2));
13257       XREALLOC(mp->input_stack, l, in_state_record);
13258       mp->stack_size = l;
13259     }         
13260   }
13261   mp->input_stack[mp->input_ptr]=mp->cur_input; /* stack the record */
13262   incr(mp->input_ptr);
13263 }
13264
13265 @ And of course what goes up must come down.
13266
13267 @d pop_input { /* leave an input level, re-enter the old */
13268     decr(mp->input_ptr); mp->cur_input=mp->input_stack[mp->input_ptr];
13269   }
13270
13271 @ Here is a procedure that starts a new level of token-list input, given
13272 a token list |p| and its type |t|. If |t=macro|, the calling routine should
13273 set |name|, reset~|loc|, and increase the macro's reference count.
13274
13275 @d back_list(A) mp_begin_token_list(mp, (A),backed_up) /* backs up a simple token list */
13276
13277 @c void mp_begin_token_list (MP mp,pointer p, quarterword t)  { 
13278   push_input; start=p; token_type=t;
13279   param_start=mp->param_ptr; loc=p;
13280 }
13281
13282 @ When a token list has been fully scanned, the following computations
13283 should be done as we leave that level of input.
13284 @^inner loop@>
13285
13286 @c void mp_end_token_list (MP mp) { /* leave a token-list input level */
13287   pointer p; /* temporary register */
13288   if ( token_type>=backed_up ) { /* token list to be deleted */
13289     if ( token_type<=inserted ) { 
13290       mp_flush_token_list(mp, start); goto DONE;
13291     } else {
13292       mp_delete_mac_ref(mp, start); /* update reference count */
13293     }
13294   }
13295   while ( mp->param_ptr>param_start ) { /* parameters must be flushed */
13296     decr(mp->param_ptr);
13297     p=mp->param_stack[mp->param_ptr];
13298     if ( p!=null ) {
13299       if ( link(p)==mp_void ) { /* it's an \&{expr} parameter */
13300         mp_recycle_value(mp, p); mp_free_node(mp, p,value_node_size);
13301       } else {
13302         mp_flush_token_list(mp, p); /* it's a \&{suffix} or \&{text} parameter */
13303       }
13304     }
13305   }
13306 DONE: 
13307   pop_input; check_interrupt;
13308 }
13309
13310 @ The contents of |cur_cmd,cur_mod,cur_sym| are placed into an equivalent
13311 token by the |cur_tok| routine.
13312 @^inner loop@>
13313
13314 @c @<Declare the procedure called |make_exp_copy|@>
13315 pointer mp_cur_tok (MP mp) {
13316   pointer p; /* a new token node */
13317   small_number save_type; /* |cur_type| to be restored */
13318   integer save_exp; /* |cur_exp| to be restored */
13319   if ( mp->cur_sym==0 ) {
13320     if ( mp->cur_cmd==capsule_token ) {
13321       save_type=mp->cur_type; save_exp=mp->cur_exp;
13322       mp_make_exp_copy(mp, mp->cur_mod); p=mp_stash_cur_exp(mp); link(p)=null;
13323       mp->cur_type=save_type; mp->cur_exp=save_exp;
13324     } else { 
13325       p=mp_get_node(mp, token_node_size);
13326       value(p)=mp->cur_mod; name_type(p)=mp_token;
13327       if ( mp->cur_cmd==numeric_token ) type(p)=mp_known;
13328       else type(p)=mp_string_type;
13329     }
13330   } else { 
13331     fast_get_avail(p); info(p)=mp->cur_sym;
13332   }
13333   return p;
13334 }
13335
13336 @ Sometimes \MP\ has read too far and wants to ``unscan'' what it has
13337 seen. The |back_input| procedure takes care of this by putting the token
13338 just scanned back into the input stream, ready to be read again.
13339 If |cur_sym<>0|, the values of |cur_cmd| and |cur_mod| are irrelevant.
13340
13341 @<Declarations@>= 
13342 void mp_back_input (MP mp);
13343
13344 @ @c void mp_back_input (MP mp) {/* undoes one token of input */
13345   pointer p; /* a token list of length one */
13346   p=mp_cur_tok(mp);
13347   while ( token_state &&(loc==null) ) 
13348     mp_end_token_list(mp); /* conserve stack space */
13349   back_list(p);
13350 }
13351
13352 @ The |back_error| routine is used when we want to restore or replace an
13353 offending token just before issuing an error message.  We disable interrupts
13354 during the call of |back_input| so that the help message won't be lost.
13355
13356 @<Declarations@>=
13357 void mp_error (MP mp);
13358 void mp_back_error (MP mp);
13359
13360 @ @c void mp_back_error (MP mp) { /* back up one token and call |error| */
13361   mp->OK_to_interrupt=false; 
13362   mp_back_input(mp); 
13363   mp->OK_to_interrupt=true; mp_error(mp);
13364 }
13365 void mp_ins_error (MP mp) { /* back up one inserted token and call |error| */
13366   mp->OK_to_interrupt=false; 
13367   mp_back_input(mp); token_type=inserted;
13368   mp->OK_to_interrupt=true; mp_error(mp);
13369 }
13370
13371 @ The |begin_file_reading| procedure starts a new level of input for lines
13372 of characters to be read from a file, or as an insertion from the
13373 terminal. It does not take care of opening the file, nor does it set |loc|
13374 or |limit| or |line|.
13375 @^system dependencies@>
13376
13377 @c void mp_begin_file_reading (MP mp) { 
13378   if ( mp->in_open==mp->max_in_open ) 
13379     mp_overflow(mp, "text input levels",mp->max_in_open);
13380 @:MetaPost capacity exceeded text input levels}{\quad text input levels@>
13381   if ( mp->first==mp->buf_size ) 
13382     mp_reallocate_buffer(mp,(mp->buf_size+(mp->buf_size>>2)));
13383   incr(mp->in_open); push_input; index=mp->in_open;
13384   mp->mpx_name[index]=absent;
13385   start=mp->first;
13386   name=is_term; /* |terminal_input| is now |true| */
13387 }
13388
13389 @ Conversely, the variables must be downdated when such a level of input
13390 is finished.  Any associated \.{MPX} file must also be closed and popped
13391 off the file stack.
13392
13393 @c void mp_end_file_reading (MP mp) { 
13394   if ( mp->in_open>index ) {
13395     if ( (mp->mpx_name[mp->in_open]==absent)||(name<=max_spec_src) ) {
13396       mp_confusion(mp, "endinput");
13397 @:this can't happen endinput}{\quad endinput@>
13398     } else { 
13399       (mp->close_file)(mp,mp->input_file[mp->in_open]); /* close an \.{MPX} file */
13400       delete_str_ref(mp->mpx_name[mp->in_open]);
13401       decr(mp->in_open);
13402     }
13403   }
13404   mp->first=start;
13405   if ( index!=mp->in_open ) mp_confusion(mp, "endinput");
13406   if ( name>max_spec_src ) {
13407     (mp->close_file)(mp,cur_file);
13408     delete_str_ref(name);
13409     xfree(in_name); 
13410     xfree(in_area);
13411   }
13412   pop_input; decr(mp->in_open);
13413 }
13414
13415 @ Here is a function that tries to resume input from an \.{MPX} file already
13416 associated with the current input file.  It returns |false| if this doesn't
13417 work.
13418
13419 @c boolean mp_begin_mpx_reading (MP mp) { 
13420   if ( mp->in_open!=index+1 ) {
13421      return false;
13422   } else { 
13423     if ( mp->mpx_name[mp->in_open]<=absent ) mp_confusion(mp, "mpx");
13424 @:this can't happen mpx}{\quad mpx@>
13425     if ( mp->first==mp->buf_size ) 
13426       mp_reallocate_buffer(mp,(mp->buf_size+(mp->buf_size>>2)));
13427     push_input; index=mp->in_open;
13428     start=mp->first;
13429     name=mp->mpx_name[mp->in_open]; add_str_ref(name);
13430     @<Put an empty line in the input buffer@>;
13431     return true;
13432   }
13433 }
13434
13435 @ This procedure temporarily stops reading an \.{MPX} file.
13436
13437 @c void mp_end_mpx_reading (MP mp) { 
13438   if ( mp->in_open!=index ) mp_confusion(mp, "mpx");
13439 @:this can't happen mpx}{\quad mpx@>
13440   if ( loc<limit ) {
13441     @<Complain that we are not at the end of a line in the \.{MPX} file@>;
13442   }
13443   mp->first=start;
13444   pop_input;
13445 }
13446
13447 @ Here we enforce a restriction that simplifies the input stacks considerably.
13448 This should not inconvenience the user because \.{MPX} files are generated
13449 by an auxiliary program called \.{DVItoMP}.
13450
13451 @ @<Complain that we are not at the end of a line in the \.{MPX} file@>=
13452
13453 print_err("`mpxbreak' must be at the end of a line");
13454 help4("This file contains picture expressions for btex...etex")
13455   ("blocks.  Such files are normally generated automatically")
13456   ("but this one seems to be messed up.  I'm going to ignore")
13457   ("the rest of this line.");
13458 mp_error(mp);
13459 }
13460
13461 @ In order to keep the stack from overflowing during a long sequence of
13462 inserted `\.{show}' commands, the following routine removes completed
13463 error-inserted lines from memory.
13464
13465 @c void mp_clear_for_error_prompt (MP mp) { 
13466   while ( file_state && terminal_input &&
13467     (mp->input_ptr>0)&&(loc==limit) ) mp_end_file_reading(mp);
13468   mp_print_ln(mp); clear_terminal;
13469 }
13470
13471 @ To get \MP's whole input mechanism going, we perform the following
13472 actions.
13473
13474 @<Initialize the input routines@>=
13475 { mp->input_ptr=0; mp->max_in_stack=0;
13476   mp->in_open=0; mp->open_parens=0; mp->max_buf_stack=0;
13477   mp->param_ptr=0; mp->max_param_stack=0;
13478   mp->first=1;
13479   start=1; index=0; line=0; name=is_term;
13480   mp->mpx_name[0]=absent;
13481   mp->force_eof=false;
13482   if ( ! mp_init_terminal(mp) ) mp_jump_out(mp);
13483   limit=mp->last; mp->first=mp->last+1; 
13484   /* |init_terminal| has set |loc| and |last| */
13485 }
13486
13487 @* \[29] Getting the next token.
13488 The heart of \MP's input mechanism is the |get_next| procedure, which
13489 we shall develop in the next few sections of the program. Perhaps we
13490 shouldn't actually call it the ``heart,'' however; it really acts as \MP's
13491 eyes and mouth, reading the source files and gobbling them up. And it also
13492 helps \MP\ to regurgitate stored token lists that are to be processed again.
13493
13494 The main duty of |get_next| is to input one token and to set |cur_cmd|
13495 and |cur_mod| to that token's command code and modifier. Furthermore, if
13496 the input token is a symbolic token, that token's |hash| address
13497 is stored in |cur_sym|; otherwise |cur_sym| is set to zero.
13498
13499 Underlying this simple description is a certain amount of complexity
13500 because of all the cases that need to be handled.
13501 However, the inner loop of |get_next| is reasonably short and fast.
13502
13503 @ Before getting into |get_next|, we need to consider a mechanism by which
13504 \MP\ helps keep errors from propagating too far. Whenever the program goes
13505 into a mode where it keeps calling |get_next| repeatedly until a certain
13506 condition is met, it sets |scanner_status| to some value other than |normal|.
13507 Then if an input file ends, or if an `\&{outer}' symbol appears,
13508 an appropriate error recovery will be possible.
13509
13510 The global variable |warning_info| helps in this error recovery by providing
13511 additional information. For example, |warning_info| might indicate the
13512 name of a macro whose replacement text is being scanned.
13513
13514 @d normal 0 /* |scanner_status| at ``quiet times'' */
13515 @d skipping 1 /* |scanner_status| when false conditional text is being skipped */
13516 @d flushing 2 /* |scanner_status| when junk after a statement is being ignored */
13517 @d absorbing 3 /* |scanner_status| when a \&{text} parameter is being scanned */
13518 @d var_defining 4 /* |scanner_status| when a \&{vardef} is being scanned */
13519 @d op_defining 5 /* |scanner_status| when a macro \&{def} is being scanned */
13520 @d loop_defining 6 /* |scanner_status| when a \&{for} loop is being scanned */
13521 @d tex_flushing 7 /* |scanner_status| when skipping \TeX\ material */
13522
13523 @<Glob...@>=
13524 integer scanner_status; /* are we scanning at high speed? */
13525 integer warning_info; /* if so, what else do we need to know,
13526     in case an error occurs? */
13527
13528 @ @<Initialize the input routines@>=
13529 mp->scanner_status=normal;
13530
13531 @ The following subroutine
13532 is called when an `\&{outer}' symbolic token has been scanned or
13533 when the end of a file has been reached. These two cases are distinguished
13534 by |cur_sym|, which is zero at the end of a file.
13535
13536 @c boolean mp_check_outer_validity (MP mp) {
13537   pointer p; /* points to inserted token list */
13538   if ( mp->scanner_status==normal ) {
13539     return true;
13540   } else if ( mp->scanner_status==tex_flushing ) {
13541     @<Check if the file has ended while flushing \TeX\ material and set the
13542       result value for |check_outer_validity|@>;
13543   } else { 
13544     mp->deletions_allowed=false;
13545     @<Back up an outer symbolic token so that it can be reread@>;
13546     if ( mp->scanner_status>skipping ) {
13547       @<Tell the user what has run away and try to recover@>;
13548     } else { 
13549       print_err("Incomplete if; all text was ignored after line ");
13550 @.Incomplete if...@>
13551       mp_print_int(mp, mp->warning_info);
13552       help3("A forbidden `outer' token occurred in skipped text.")
13553         ("This kind of error happens when you say `if...' and forget")
13554         ("the matching `fi'. I've inserted a `fi'; this might work.");
13555       if ( mp->cur_sym==0 ) 
13556         mp->help_line[2]="The file ended while I was skipping conditional text.";
13557       mp->cur_sym=frozen_fi; mp_ins_error(mp);
13558     }
13559     mp->deletions_allowed=true; 
13560         return false;
13561   }
13562 }
13563
13564 @ @<Check if the file has ended while flushing \TeX\ material and set...@>=
13565 if ( mp->cur_sym!=0 ) { 
13566    return true;
13567 } else { 
13568   mp->deletions_allowed=false;
13569   print_err("TeX mode didn't end; all text was ignored after line ");
13570   mp_print_int(mp, mp->warning_info);
13571   help2("The file ended while I was looking for the `etex' to")
13572     ("finish this TeX material.  I've inserted `etex' now.");
13573   mp->cur_sym = frozen_etex;
13574   mp_ins_error(mp);
13575   mp->deletions_allowed=true;
13576   return false;
13577 }
13578
13579 @ @<Back up an outer symbolic token so that it can be reread@>=
13580 if ( mp->cur_sym!=0 ) {
13581   p=mp_get_avail(mp); info(p)=mp->cur_sym;
13582   back_list(p); /* prepare to read the symbolic token again */
13583 }
13584
13585 @ @<Tell the user what has run away...@>=
13586
13587   mp_runaway(mp); /* print the definition-so-far */
13588   if ( mp->cur_sym==0 ) {
13589     print_err("File ended");
13590 @.File ended while scanning...@>
13591   } else { 
13592     print_err("Forbidden token found");
13593 @.Forbidden token found...@>
13594   }
13595   mp_print(mp, " while scanning ");
13596   help4("I suspect you have forgotten an `enddef',")
13597     ("causing me to read past where you wanted me to stop.")
13598     ("I'll try to recover; but if the error is serious,")
13599     ("you'd better type `E' or `X' now and fix your file.");
13600   switch (mp->scanner_status) {
13601     @<Complete the error message,
13602       and set |cur_sym| to a token that might help recover from the error@>
13603   } /* there are no other cases */
13604   mp_ins_error(mp);
13605 }
13606
13607 @ As we consider various kinds of errors, it is also appropriate to
13608 change the first line of the help message just given; |help_line[3]|
13609 points to the string that might be changed.
13610
13611 @<Complete the error message,...@>=
13612 case flushing: 
13613   mp_print(mp, "to the end of the statement");
13614   mp->help_line[3]="A previous error seems to have propagated,";
13615   mp->cur_sym=frozen_semicolon;
13616   break;
13617 case absorbing: 
13618   mp_print(mp, "a text argument");
13619   mp->help_line[3]="It seems that a right delimiter was left out,";
13620   if ( mp->warning_info==0 ) {
13621     mp->cur_sym=frozen_end_group;
13622   } else { 
13623     mp->cur_sym=frozen_right_delimiter;
13624     equiv(frozen_right_delimiter)=mp->warning_info;
13625   }
13626   break;
13627 case var_defining:
13628 case op_defining: 
13629   mp_print(mp, "the definition of ");
13630   if ( mp->scanner_status==op_defining ) 
13631      mp_print_text(mp->warning_info);
13632   else 
13633      mp_print_variable_name(mp, mp->warning_info);
13634   mp->cur_sym=frozen_end_def;
13635   break;
13636 case loop_defining: 
13637   mp_print(mp, "the text of a "); 
13638   mp_print_text(mp->warning_info);
13639   mp_print(mp, " loop");
13640   mp->help_line[3]="I suspect you have forgotten an `endfor',";
13641   mp->cur_sym=frozen_end_for;
13642   break;
13643
13644 @ The |runaway| procedure displays the first part of the text that occurred
13645 when \MP\ began its special |scanner_status|, if that text has been saved.
13646
13647 @<Declare the procedure called |runaway|@>=
13648 void mp_runaway (MP mp) { 
13649   if ( mp->scanner_status>flushing ) { 
13650      mp_print_nl(mp, "Runaway ");
13651          switch (mp->scanner_status) { 
13652          case absorbing: mp_print(mp, "text?"); break;
13653          case var_defining: 
13654      case op_defining: mp_print(mp,"definition?"); break;
13655      case loop_defining: mp_print(mp, "loop?"); break;
13656      } /* there are no other cases */
13657      mp_print_ln(mp); 
13658      mp_show_token_list(mp, link(hold_head),null,mp->error_line-10,0);
13659   }
13660 }
13661
13662 @ We need to mention a procedure that may be called by |get_next|.
13663
13664 @<Declarations@>= 
13665 void mp_firm_up_the_line (MP mp);
13666
13667 @ And now we're ready to take the plunge into |get_next| itself.
13668 Note that the behavior depends on the |scanner_status| because percent signs
13669 and double quotes need to be passed over when skipping TeX material.
13670
13671 @c 
13672 void mp_get_next (MP mp) {
13673   /* sets |cur_cmd|, |cur_mod|, |cur_sym| to next token */
13674 @^inner loop@>
13675   /*restart*/ /* go here to get the next input token */
13676   /*exit*/ /* go here when the next input token has been got */
13677   /*|common_ending|*/ /* go here to finish getting a symbolic token */
13678   /*found*/ /* go here when the end of a symbolic token has been found */
13679   /*switch*/ /* go here to branch on the class of an input character */
13680   /*|start_numeric_token|,|start_decimal_token|,|fin_numeric_token|,|done|*/
13681     /* go here at crucial stages when scanning a number */
13682   int k; /* an index into |buffer| */
13683   ASCII_code c; /* the current character in the buffer */
13684   ASCII_code class; /* its class number */
13685   integer n,f; /* registers for decimal-to-binary conversion */
13686 RESTART: 
13687   mp->cur_sym=0;
13688   if ( file_state ) {
13689     @<Input from external file; |goto restart| if no input found,
13690     or |return| if a non-symbolic token is found@>;
13691   } else {
13692     @<Input from token list; |goto restart| if end of list or
13693       if a parameter needs to be expanded,
13694       or |return| if a non-symbolic token is found@>;
13695   }
13696 COMMON_ENDING: 
13697   @<Finish getting the symbolic token in |cur_sym|;
13698    |goto restart| if it is illegal@>;
13699 }
13700
13701 @ When a symbolic token is declared to be `\&{outer}', its command code
13702 is increased by |outer_tag|.
13703 @^inner loop@>
13704
13705 @<Finish getting the symbolic token in |cur_sym|...@>=
13706 mp->cur_cmd=eq_type(mp->cur_sym); mp->cur_mod=equiv(mp->cur_sym);
13707 if ( mp->cur_cmd>=outer_tag ) {
13708   if ( mp_check_outer_validity(mp) ) 
13709     mp->cur_cmd=mp->cur_cmd-outer_tag;
13710   else 
13711     goto RESTART;
13712 }
13713
13714 @ A percent sign appears in |buffer[limit]|; this makes it unnecessary
13715 to have a special test for end-of-line.
13716 @^inner loop@>
13717
13718 @<Input from external file;...@>=
13719
13720 SWITCH: 
13721   c=mp->buffer[loc]; incr(loc); class=mp->char_class[c];
13722   switch (class) {
13723   case digit_class: goto START_NUMERIC_TOKEN; break;
13724   case period_class: 
13725     class=mp->char_class[mp->buffer[loc]];
13726     if ( class>period_class ) {
13727       goto SWITCH;
13728     } else if ( class<period_class ) { /* |class=digit_class| */
13729       n=0; goto START_DECIMAL_TOKEN;
13730     }
13731 @:. }{\..\ token@>
13732     break;
13733   case space_class: goto SWITCH; break;
13734   case percent_class: 
13735     if ( mp->scanner_status==tex_flushing ) {
13736       if ( loc<limit ) goto SWITCH;
13737     }
13738     @<Move to next line of file, or |goto restart| if there is no next line@>;
13739     check_interrupt;
13740     goto SWITCH;
13741     break;
13742   case string_class: 
13743     if ( mp->scanner_status==tex_flushing ) goto SWITCH;
13744     else @<Get a string token and |return|@>;
13745     break;
13746   case isolated_classes: 
13747     k=loc-1; goto FOUND; break;
13748   case invalid_class: 
13749     if ( mp->scanner_status==tex_flushing ) goto SWITCH;
13750     else @<Decry the invalid character and |goto restart|@>;
13751     break;
13752   default: break; /* letters, etc. */
13753   }
13754   k=loc-1;
13755   while ( mp->char_class[mp->buffer[loc]]==class ) incr(loc);
13756   goto FOUND;
13757 START_NUMERIC_TOKEN:
13758   @<Get the integer part |n| of a numeric token;
13759     set |f:=0| and |goto fin_numeric_token| if there is no decimal point@>;
13760 START_DECIMAL_TOKEN:
13761   @<Get the fraction part |f| of a numeric token@>;
13762 FIN_NUMERIC_TOKEN:
13763   @<Pack the numeric and fraction parts of a numeric token
13764     and |return|@>;
13765 FOUND: 
13766   mp->cur_sym=mp_id_lookup(mp, k,loc-k);
13767 }
13768
13769 @ We go to |restart| instead of to |SWITCH|, because we might enter
13770 |token_state| after the error has been dealt with
13771 (cf.\ |clear_for_error_prompt|).
13772
13773 @<Decry the invalid...@>=
13774
13775   print_err("Text line contains an invalid character");
13776 @.Text line contains...@>
13777   help2("A funny symbol that I can\'t read has just been input.")
13778     ("Continue, and I'll forget that it ever happened.");
13779   mp->deletions_allowed=false; mp_error(mp); mp->deletions_allowed=true;
13780   goto RESTART;
13781 }
13782
13783 @ @<Get a string token and |return|@>=
13784
13785   if ( mp->buffer[loc]=='"' ) {
13786     mp->cur_mod=rts("");
13787   } else { 
13788     k=loc; mp->buffer[limit+1]='"';
13789     do {  
13790      incr(loc);
13791     } while (mp->buffer[loc]!='"');
13792     if ( loc>limit ) {
13793       @<Decry the missing string delimiter and |goto restart|@>;
13794     }
13795     if ( loc==k+1 ) {
13796       mp->cur_mod=mp->buffer[k];
13797     } else { 
13798       str_room(loc-k);
13799       do {  
13800         append_char(mp->buffer[k]); incr(k);
13801       } while (k!=loc);
13802       mp->cur_mod=mp_make_string(mp);
13803     }
13804   }
13805   incr(loc); mp->cur_cmd=string_token; 
13806   return;
13807 }
13808
13809 @ We go to |restart| after this error message, not to |SWITCH|,
13810 because the |clear_for_error_prompt| routine might have reinstated
13811 |token_state| after |error| has finished.
13812
13813 @<Decry the missing string delimiter and |goto restart|@>=
13814
13815   loc=limit; /* the next character to be read on this line will be |"%"| */
13816   print_err("Incomplete string token has been flushed");
13817 @.Incomplete string token...@>
13818   help3("Strings should finish on the same line as they began.")
13819     ("I've deleted the partial string; you might want to")
13820     ("insert another by typing, e.g., `I\"new string\"'.");
13821   mp->deletions_allowed=false; mp_error(mp);
13822   mp->deletions_allowed=true; 
13823   goto RESTART;
13824 }
13825
13826 @ @<Get the integer part |n| of a numeric token...@>=
13827 n=c-'0';
13828 while ( mp->char_class[mp->buffer[loc]]==digit_class ) {
13829   if ( n<32768 ) n=10*n+mp->buffer[loc]-'0';
13830   incr(loc);
13831 }
13832 if ( mp->buffer[loc]=='.' ) 
13833   if ( mp->char_class[mp->buffer[loc+1]]==digit_class ) 
13834     goto DONE;
13835 f=0; 
13836 goto FIN_NUMERIC_TOKEN;
13837 DONE: incr(loc)
13838
13839 @ @<Get the fraction part |f| of a numeric token@>=
13840 k=0;
13841 do { 
13842   if ( k<17 ) { /* digits for |k>=17| cannot affect the result */
13843     mp->dig[k]=mp->buffer[loc]-'0'; incr(k);
13844   }
13845   incr(loc);
13846 } while (mp->char_class[mp->buffer[loc]]==digit_class);
13847 f=mp_round_decimals(mp, k);
13848 if ( f==unity ) {
13849   incr(n); f=0;
13850 }
13851
13852 @ @<Pack the numeric and fraction parts of a numeric token and |return|@>=
13853 if ( n<32768 ) {
13854   @<Set |cur_mod:=n*unity+f| and check if it is uncomfortably large@>;
13855 } else if ( mp->scanner_status!=tex_flushing ) {
13856   print_err("Enormous number has been reduced");
13857 @.Enormous number...@>
13858   help2("I can\'t handle numbers bigger than 32767.99998;")
13859     ("so I've changed your constant to that maximum amount.");
13860   mp->deletions_allowed=false; mp_error(mp); mp->deletions_allowed=true;
13861   mp->cur_mod=el_gordo;
13862 }
13863 mp->cur_cmd=numeric_token; return
13864
13865 @ @<Set |cur_mod:=n*unity+f| and check if it is uncomfortably large@>=
13866
13867   mp->cur_mod=n*unity+f;
13868   if ( mp->cur_mod>=fraction_one ) {
13869     if ( (mp->internal[mp_warning_check]>0) &&
13870          (mp->scanner_status!=tex_flushing) ) {
13871       print_err("Number is too large (");
13872       mp_print_scaled(mp, mp->cur_mod);
13873       mp_print_char(mp, ')');
13874       help3("It is at least 4096. Continue and I'll try to cope")
13875       ("with that big value; but it might be dangerous.")
13876       ("(Set warningcheck:=0 to suppress this message.)");
13877       mp_error(mp);
13878     }
13879   }
13880 }
13881
13882 @ Let's consider now what happens when |get_next| is looking at a token list.
13883 @^inner loop@>
13884
13885 @<Input from token list;...@>=
13886 if ( loc>=mp->hi_mem_min ) { /* one-word token */
13887   mp->cur_sym=info(loc); loc=link(loc); /* move to next */
13888   if ( mp->cur_sym>=expr_base ) {
13889     if ( mp->cur_sym>=suffix_base ) {
13890       @<Insert a suffix or text parameter and |goto restart|@>;
13891     } else { 
13892       mp->cur_cmd=capsule_token;
13893       mp->cur_mod=mp->param_stack[param_start+mp->cur_sym-(expr_base)];
13894       mp->cur_sym=0; return;
13895     }
13896   }
13897 } else if ( loc>null ) {
13898   @<Get a stored numeric or string or capsule token and |return|@>
13899 } else { /* we are done with this token list */
13900   mp_end_token_list(mp); goto RESTART; /* resume previous level */
13901 }
13902
13903 @ @<Insert a suffix or text parameter...@>=
13904
13905   if ( mp->cur_sym>=text_base ) mp->cur_sym=mp->cur_sym-mp->param_size;
13906   /* |param_size=text_base-suffix_base| */
13907   mp_begin_token_list(mp,
13908                       mp->param_stack[param_start+mp->cur_sym-(suffix_base)],
13909                       parameter);
13910   goto RESTART;
13911 }
13912
13913 @ @<Get a stored numeric or string or capsule token...@>=
13914
13915   if ( name_type(loc)==mp_token ) {
13916     mp->cur_mod=value(loc);
13917     if ( type(loc)==mp_known ) {
13918       mp->cur_cmd=numeric_token;
13919     } else { 
13920       mp->cur_cmd=string_token; add_str_ref(mp->cur_mod);
13921     }
13922   } else { 
13923     mp->cur_mod=loc; mp->cur_cmd=capsule_token;
13924   };
13925   loc=link(loc); return;
13926 }
13927
13928 @ All of the easy branches of |get_next| have now been taken care of.
13929 There is one more branch.
13930
13931 @<Move to next line of file, or |goto restart|...@>=
13932 if ( name>max_spec_src ) {
13933   @<Read next line of file into |buffer|, or
13934     |goto restart| if the file has ended@>;
13935 } else { 
13936   if ( mp->input_ptr>0 ) {
13937      /* text was inserted during error recovery or by \&{scantokens} */
13938     mp_end_file_reading(mp); goto RESTART; /* resume previous level */
13939   }
13940   if ( mp->selector<log_only || mp->selector>=write_file) mp_open_log_file(mp);
13941   if ( mp->interaction>mp_nonstop_mode ) {
13942     if ( limit==start ) /* previous line was empty */
13943       mp_print_nl(mp, "(Please type a command or say `end')");
13944 @.Please type...@>
13945     mp_print_ln(mp); mp->first=start;
13946     prompt_input("*"); /* input on-line into |buffer| */
13947 @.*\relax@>
13948     limit=mp->last; mp->buffer[limit]='%';
13949     mp->first=limit+1; loc=start;
13950   } else {
13951     mp_fatal_error(mp, "*** (job aborted, no legal end found)");
13952 @.job aborted@>
13953     /* nonstop mode, which is intended for overnight batch processing,
13954     never waits for on-line input */
13955   }
13956 }
13957
13958 @ The global variable |force_eof| is normally |false|; it is set |true|
13959 by an \&{endinput} command.
13960
13961 @<Glob...@>=
13962 boolean force_eof; /* should the next \&{input} be aborted early? */
13963
13964 @ We must decrement |loc| in order to leave the buffer in a valid state
13965 when an error condition causes us to |goto restart| without calling
13966 |end_file_reading|.
13967
13968 @<Read next line of file into |buffer|, or
13969   |goto restart| if the file has ended@>=
13970
13971   incr(line); mp->first=start;
13972   if ( ! mp->force_eof ) {
13973     if ( mp_input_ln(mp, cur_file ) ) /* not end of file */
13974       mp_firm_up_the_line(mp); /* this sets |limit| */
13975     else 
13976       mp->force_eof=true;
13977   };
13978   if ( mp->force_eof ) {
13979     mp->force_eof=false;
13980     decr(loc);
13981     if ( mpx_reading ) {
13982       @<Complain that the \.{MPX} file ended unexpectly; then set
13983         |cur_sym:=frozen_mpx_break| and |goto comon_ending|@>;
13984     } else { 
13985       mp_print_char(mp, ')'); decr(mp->open_parens);
13986       update_terminal; /* show user that file has been read */
13987       mp_end_file_reading(mp); /* resume previous level */
13988       if ( mp_check_outer_validity(mp) ) goto  RESTART;  
13989       else goto RESTART;
13990     }
13991   }
13992   mp->buffer[limit]='%'; mp->first=limit+1; loc=start; /* ready to read */
13993 }
13994
13995 @ We should never actually come to the end of an \.{MPX} file because such
13996 files should have an \&{mpxbreak} after the translation of the last
13997 \&{btex}$\,\ldots\,$\&{etex} block.
13998
13999 @<Complain that the \.{MPX} file ended unexpectly; then set...@>=
14000
14001   mp->mpx_name[index]=finished;
14002   print_err("mpx file ended unexpectedly");
14003   help4("The file had too few picture expressions for btex...etex")
14004     ("blocks.  Such files are normally generated automatically")
14005     ("but this one got messed up.  You might want to insert a")
14006     ("picture expression now.");
14007   mp->deletions_allowed=false; mp_error(mp); mp->deletions_allowed=true;
14008   mp->cur_sym=frozen_mpx_break; goto COMMON_ENDING;
14009 }
14010
14011 @ Sometimes we want to make it look as though we have just read a blank line
14012 without really doing so.
14013
14014 @<Put an empty line in the input buffer@>=
14015 mp->last=mp->first; limit=mp->last; /* simulate |input_ln| and |firm_up_the_line| */
14016 mp->buffer[limit]='%'; mp->first=limit+1; loc=start
14017
14018 @ If the user has set the |mp_pausing| parameter to some positive value,
14019 and if nonstop mode has not been selected, each line of input is displayed
14020 on the terminal and the transcript file, followed by `\.{=>}'.
14021 \MP\ waits for a response. If the response is null (i.e., if nothing is
14022 typed except perhaps a few blank spaces), the original
14023 line is accepted as it stands; otherwise the line typed is
14024 used instead of the line in the file.
14025
14026 @c void mp_firm_up_the_line (MP mp) {
14027   size_t k; /* an index into |buffer| */
14028   limit=mp->last;
14029   if ( mp->internal[mp_pausing]>0) if ( mp->interaction>mp_nonstop_mode ) {
14030     wake_up_terminal; mp_print_ln(mp);
14031     if ( start<limit ) {
14032       for (k=(size_t)start;k<=(size_t)(limit-1);k++) {
14033         mp_print_str(mp, mp->buffer[k]);
14034       } 
14035     }
14036     mp->first=limit; prompt_input("=>"); /* wait for user response */
14037 @.=>@>
14038     if ( mp->last>mp->first ) {
14039       for (k=mp->first;k<=mp->last-1;k++) { /* move line down in buffer */
14040         mp->buffer[k+start-mp->first]=mp->buffer[k];
14041       }
14042       limit=start+mp->last-mp->first;
14043     }
14044   }
14045 }
14046
14047 @* \[30] Dealing with \TeX\ material.
14048 The \&{btex}$\,\ldots\,$\&{etex} and \&{verbatimtex}$\,\ldots\,$\&{etex}
14049 features need to be implemented at a low level in the scanning process
14050 so that \MP\ can stay in synch with the a preprocessor that treats
14051 blocks of \TeX\ material as they occur in the input file without trying
14052 to expand \MP\ macros.  Thus we need a special version of |get_next|
14053 that does not expand macros and such but does handle \&{btex},
14054 \&{verbatimtex}, etc.
14055
14056 The special version of |get_next| is called |get_t_next|.  It works by flushing
14057 \&{btex}$\,\ldots\,$\&{etex} and \&{verbatimtex}\allowbreak
14058 $\,\ldots\,$\&{etex} blocks, switching to the \.{MPX} file when it sees
14059 \&{btex}, and switching back when it sees \&{mpxbreak}.
14060
14061 @d btex_code 0
14062 @d verbatim_code 1
14063
14064 @ @<Put each...@>=
14065 mp_primitive(mp, "btex",start_tex,btex_code);
14066 @:btex_}{\&{btex} primitive@>
14067 mp_primitive(mp, "verbatimtex",start_tex,verbatim_code);
14068 @:verbatimtex_}{\&{verbatimtex} primitive@>
14069 mp_primitive(mp, "etex",etex_marker,0); mp->eqtb[frozen_etex]=mp->eqtb[mp->cur_sym];
14070 @:etex_}{\&{etex} primitive@>
14071 mp_primitive(mp, "mpxbreak",mpx_break,0); mp->eqtb[frozen_mpx_break]=mp->eqtb[mp->cur_sym];
14072 @:mpx_break_}{\&{mpxbreak} primitive@>
14073
14074 @ @<Cases of |print_cmd...@>=
14075 case start_tex: if ( m==btex_code ) mp_print(mp, "btex");
14076   else mp_print(mp, "verbatimtex"); break;
14077 case etex_marker: mp_print(mp, "etex"); break;
14078 case mpx_break: mp_print(mp, "mpxbreak"); break;
14079
14080 @ Actually, |get_t_next| is a macro that avoids procedure overhead except
14081 in the unusual case where \&{btex}, \&{verbatimtex}, \&{etex}, or \&{mpxbreak}
14082 is encountered.
14083
14084 @d get_t_next {mp_get_next(mp); if ( mp->cur_cmd<=max_pre_command ) mp_t_next(mp); }
14085
14086 @<Declarations@>=
14087 void mp_start_mpx_input (MP mp);
14088
14089 @ @c 
14090 void mp_t_next (MP mp) {
14091   int old_status; /* saves the |scanner_status| */
14092   integer old_info; /* saves the |warning_info| */
14093   while ( mp->cur_cmd<=max_pre_command ) {
14094     if ( mp->cur_cmd==mpx_break ) {
14095       if ( ! file_state || (mp->mpx_name[index]==absent) ) {
14096         @<Complain about a misplaced \&{mpxbreak}@>;
14097       } else { 
14098         mp_end_mpx_reading(mp); 
14099         goto TEX_FLUSH;
14100       }
14101     } else if ( mp->cur_cmd==start_tex ) {
14102       if ( token_state || (name<=max_spec_src) ) {
14103         @<Complain that we are not reading a file@>;
14104       } else if ( mpx_reading ) {
14105         @<Complain that \.{MPX} files cannot contain \TeX\ material@>;
14106       } else if ( (mp->cur_mod!=verbatim_code)&&
14107                   (mp->mpx_name[index]!=finished) ) {
14108         if ( ! mp_begin_mpx_reading(mp) ) mp_start_mpx_input(mp);
14109       } else {
14110         goto TEX_FLUSH;
14111       }
14112     } else {
14113        @<Complain about a misplaced \&{etex}@>;
14114     }
14115     goto COMMON_ENDING;
14116   TEX_FLUSH: 
14117     @<Flush the \TeX\ material@>;
14118   COMMON_ENDING: 
14119     mp_get_next(mp);
14120   }
14121 }
14122
14123 @ We could be in the middle of an operation such as skipping false conditional
14124 text when \TeX\ material is encountered, so we must be careful to save the
14125 |scanner_status|.
14126
14127 @<Flush the \TeX\ material@>=
14128 old_status=mp->scanner_status;
14129 old_info=mp->warning_info;
14130 mp->scanner_status=tex_flushing;
14131 mp->warning_info=line;
14132 do {  mp_get_next(mp); } while (mp->cur_cmd!=etex_marker);
14133 mp->scanner_status=old_status;
14134 mp->warning_info=old_info
14135
14136 @ @<Complain that \.{MPX} files cannot contain \TeX\ material@>=
14137 { print_err("An mpx file cannot contain btex or verbatimtex blocks");
14138 help4("This file contains picture expressions for btex...etex")
14139   ("blocks.  Such files are normally generated automatically")
14140   ("but this one seems to be messed up.  I'll just keep going")
14141   ("and hope for the best.");
14142 mp_error(mp);
14143 }
14144
14145 @ @<Complain that we are not reading a file@>=
14146 { print_err("You can only use `btex' or `verbatimtex' in a file");
14147 help3("I'll have to ignore this preprocessor command because it")
14148   ("only works when there is a file to preprocess.  You might")
14149   ("want to delete everything up to the next `etex`.");
14150 mp_error(mp);
14151 }
14152
14153 @ @<Complain about a misplaced \&{mpxbreak}@>=
14154 { print_err("Misplaced mpxbreak");
14155 help2("I'll ignore this preprocessor command because it")
14156   ("doesn't belong here");
14157 mp_error(mp);
14158 }
14159
14160 @ @<Complain about a misplaced \&{etex}@>=
14161 { print_err("Extra etex will be ignored");
14162 help1("There is no btex or verbatimtex for this to match");
14163 mp_error(mp);
14164 }
14165
14166 @* \[31] Scanning macro definitions.
14167 \MP\ has a variety of ways to tuck tokens away into token lists for later
14168 use: Macros can be defined with \&{def}, \&{vardef}, \&{primarydef}, etc.;
14169 repeatable code can be defined with \&{for}, \&{forever}, \&{forsuffixes}.
14170 All such operations are handled by the routines in this part of the program.
14171
14172 The modifier part of each command code is zero for the ``ending delimiters''
14173 like \&{enddef} and \&{endfor}.
14174
14175 @d start_def 1 /* command modifier for \&{def} */
14176 @d var_def 2 /* command modifier for \&{vardef} */
14177 @d end_def 0 /* command modifier for \&{enddef} */
14178 @d start_forever 1 /* command modifier for \&{forever} */
14179 @d end_for 0 /* command modifier for \&{endfor} */
14180
14181 @<Put each...@>=
14182 mp_primitive(mp, "def",macro_def,start_def);
14183 @:def_}{\&{def} primitive@>
14184 mp_primitive(mp, "vardef",macro_def,var_def);
14185 @:var_def_}{\&{vardef} primitive@>
14186 mp_primitive(mp, "primarydef",macro_def,secondary_primary_macro);
14187 @:primary_def_}{\&{primarydef} primitive@>
14188 mp_primitive(mp, "secondarydef",macro_def,tertiary_secondary_macro);
14189 @:secondary_def_}{\&{secondarydef} primitive@>
14190 mp_primitive(mp, "tertiarydef",macro_def,expression_tertiary_macro);
14191 @:tertiary_def_}{\&{tertiarydef} primitive@>
14192 mp_primitive(mp, "enddef",macro_def,end_def); mp->eqtb[frozen_end_def]=mp->eqtb[mp->cur_sym];
14193 @:end_def_}{\&{enddef} primitive@>
14194 @#
14195 mp_primitive(mp, "for",iteration,expr_base);
14196 @:for_}{\&{for} primitive@>
14197 mp_primitive(mp, "forsuffixes",iteration,suffix_base);
14198 @:for_suffixes_}{\&{forsuffixes} primitive@>
14199 mp_primitive(mp, "forever",iteration,start_forever);
14200 @:forever_}{\&{forever} primitive@>
14201 mp_primitive(mp, "endfor",iteration,end_for); mp->eqtb[frozen_end_for]=mp->eqtb[mp->cur_sym];
14202 @:end_for_}{\&{endfor} primitive@>
14203
14204 @ @<Cases of |print_cmd...@>=
14205 case macro_def:
14206   if ( m<=var_def ) {
14207     if ( m==start_def ) mp_print(mp, "def");
14208     else if ( m<start_def ) mp_print(mp, "enddef");
14209     else mp_print(mp, "vardef");
14210   } else if ( m==secondary_primary_macro ) { 
14211     mp_print(mp, "primarydef");
14212   } else if ( m==tertiary_secondary_macro ) { 
14213     mp_print(mp, "secondarydef");
14214   } else { 
14215     mp_print(mp, "tertiarydef");
14216   }
14217   break;
14218 case iteration: 
14219   if ( m<=start_forever ) {
14220     if ( m==start_forever ) mp_print(mp, "forever"); 
14221     else mp_print(mp, "endfor");
14222   } else if ( m==expr_base ) {
14223     mp_print(mp, "for"); 
14224   } else { 
14225     mp_print(mp, "forsuffixes");
14226   }
14227   break;
14228
14229 @ Different macro-absorbing operations have different syntaxes, but they
14230 also have a lot in common. There is a list of special symbols that are to
14231 be replaced by parameter tokens; there is a special command code that
14232 ends the definition; the quotation conventions are identical.  Therefore
14233 it makes sense to have most of the work done by a single subroutine. That
14234 subroutine is called |scan_toks|.
14235
14236 The first parameter to |scan_toks| is the command code that will
14237 terminate scanning (either |macro_def| or |iteration|).
14238
14239 The second parameter, |subst_list|, points to a (possibly empty) list
14240 of two-word nodes whose |info| and |value| fields specify symbol tokens
14241 before and after replacement. The list will be returned to free storage
14242 by |scan_toks|.
14243
14244 The third parameter is simply appended to the token list that is built.
14245 And the final parameter tells how many of the special operations
14246 \.{\#\AT!}, \.{\AT!}, and \.{\AT!\#} are to be replaced by suffix parameters.
14247 When such parameters are present, they are called \.{(SUFFIX0)},
14248 \.{(SUFFIX1)}, and \.{(SUFFIX2)}.
14249
14250 @c pointer mp_scan_toks (MP mp,command_code terminator, pointer 
14251   subst_list, pointer tail_end, small_number suffix_count) {
14252   pointer p; /* tail of the token list being built */
14253   pointer q; /* temporary for link management */
14254   integer balance; /* left delimiters minus right delimiters */
14255   p=hold_head; balance=1; link(hold_head)=null;
14256   while (1) { 
14257     get_t_next;
14258     if ( mp->cur_sym>0 ) {
14259       @<Substitute for |cur_sym|, if it's on the |subst_list|@>;
14260       if ( mp->cur_cmd==terminator ) {
14261         @<Adjust the balance; |break| if it's zero@>;
14262       } else if ( mp->cur_cmd==macro_special ) {
14263         @<Handle quoted symbols, \.{\#\AT!}, \.{\AT!}, or \.{\AT!\#}@>;
14264       }
14265     }
14266     link(p)=mp_cur_tok(mp); p=link(p);
14267   }
14268   link(p)=tail_end; mp_flush_node_list(mp, subst_list);
14269   return link(hold_head);
14270 }
14271
14272 @ @<Substitute for |cur_sym|...@>=
14273
14274   q=subst_list;
14275   while ( q!=null ) {
14276     if ( info(q)==mp->cur_sym ) {
14277       mp->cur_sym=value(q); mp->cur_cmd=relax; break;
14278     }
14279     q=link(q);
14280   }
14281 }
14282
14283 @ @<Adjust the balance; |break| if it's zero@>=
14284 if ( mp->cur_mod>0 ) {
14285   incr(balance);
14286 } else { 
14287   decr(balance);
14288   if ( balance==0 )
14289     break;
14290 }
14291
14292 @ Four commands are intended to be used only within macro texts: \&{quote},
14293 \.{\#\AT!}, \.{\AT!}, and \.{\AT!\#}. They are variants of a single command
14294 code called |macro_special|.
14295
14296 @d quote 0 /* |macro_special| modifier for \&{quote} */
14297 @d macro_prefix 1 /* |macro_special| modifier for \.{\#\AT!} */
14298 @d macro_at 2 /* |macro_special| modifier for \.{\AT!} */
14299 @d macro_suffix 3 /* |macro_special| modifier for \.{\AT!\#} */
14300
14301 @<Put each...@>=
14302 mp_primitive(mp, "quote",macro_special,quote);
14303 @:quote_}{\&{quote} primitive@>
14304 mp_primitive(mp, "#@@",macro_special,macro_prefix);
14305 @:]]]\#\AT!_}{\.{\#\AT!} primitive@>
14306 mp_primitive(mp, "@@",macro_special,macro_at);
14307 @:]]]\AT!_}{\.{\AT!} primitive@>
14308 mp_primitive(mp, "@@#",macro_special,macro_suffix);
14309 @:]]]\AT!\#_}{\.{\AT!\#} primitive@>
14310
14311 @ @<Cases of |print_cmd...@>=
14312 case macro_special: 
14313   switch (m) {
14314   case macro_prefix: mp_print(mp, "#@@"); break;
14315   case macro_at: mp_print_char(mp, '@@'); break;
14316   case macro_suffix: mp_print(mp, "@@#"); break;
14317   default: mp_print(mp, "quote"); break;
14318   }
14319   break;
14320
14321 @ @<Handle quoted...@>=
14322
14323   if ( mp->cur_mod==quote ) { get_t_next; } 
14324   else if ( mp->cur_mod<=suffix_count ) 
14325     mp->cur_sym=suffix_base-1+mp->cur_mod;
14326 }
14327
14328 @ Here is a routine that's used whenever a token will be redefined. If
14329 the user's token is unredefinable, the `|frozen_inaccessible|' token is
14330 substituted; the latter is redefinable but essentially impossible to use,
14331 hence \MP's tables won't get fouled up.
14332
14333 @c void mp_get_symbol (MP mp) { /* sets |cur_sym| to a safe symbol */
14334 RESTART: 
14335   get_t_next;
14336   if ( (mp->cur_sym==0)||(mp->cur_sym>frozen_inaccessible) ) {
14337     print_err("Missing symbolic token inserted");
14338 @.Missing symbolic token...@>
14339     help3("Sorry: You can\'t redefine a number, string, or expr.")
14340       ("I've inserted an inaccessible symbol so that your")
14341       ("definition will be completed without mixing me up too badly.");
14342     if ( mp->cur_sym>0 )
14343       mp->help_line[2]="Sorry: You can\'t redefine my error-recovery tokens.";
14344     else if ( mp->cur_cmd==string_token ) 
14345       delete_str_ref(mp->cur_mod);
14346     mp->cur_sym=frozen_inaccessible; mp_ins_error(mp); goto RESTART;
14347   }
14348 }
14349
14350 @ Before we actually redefine a symbolic token, we need to clear away its
14351 former value, if it was a variable. The following stronger version of
14352 |get_symbol| does that.
14353
14354 @c void mp_get_clear_symbol (MP mp) { 
14355   mp_get_symbol(mp); mp_clear_symbol(mp, mp->cur_sym,false);
14356 }
14357
14358 @ Here's another little subroutine; it checks that an equals sign
14359 or assignment sign comes along at the proper place in a macro definition.
14360
14361 @c void mp_check_equals (MP mp) { 
14362   if ( mp->cur_cmd!=equals ) if ( mp->cur_cmd!=assignment ) {
14363      mp_missing_err(mp, "=");
14364 @.Missing `='@>
14365     help5("The next thing in this `def' should have been `=',")
14366       ("because I've already looked at the definition heading.")
14367       ("But don't worry; I'll pretend that an equals sign")
14368       ("was present. Everything from here to `enddef'")
14369       ("will be the replacement text of this macro.");
14370     mp_back_error(mp);
14371   }
14372 }
14373
14374 @ A \&{primarydef}, \&{secondarydef}, or \&{tertiarydef} is rather easily
14375 handled now that we have |scan_toks|.  In this case there are
14376 two parameters, which will be \.{EXPR0} and \.{EXPR1} (i.e.,
14377 |expr_base| and |expr_base+1|).
14378
14379 @c void mp_make_op_def (MP mp) {
14380   command_code m; /* the type of definition */
14381   pointer p,q,r; /* for list manipulation */
14382   m=mp->cur_mod;
14383   mp_get_symbol(mp); q=mp_get_node(mp, token_node_size);
14384   info(q)=mp->cur_sym; value(q)=expr_base;
14385   mp_get_clear_symbol(mp); mp->warning_info=mp->cur_sym;
14386   mp_get_symbol(mp); p=mp_get_node(mp, token_node_size);
14387   info(p)=mp->cur_sym; value(p)=expr_base+1; link(p)=q;
14388   get_t_next; mp_check_equals(mp);
14389   mp->scanner_status=op_defining; q=mp_get_avail(mp); ref_count(q)=null;
14390   r=mp_get_avail(mp); link(q)=r; info(r)=general_macro;
14391   link(r)=mp_scan_toks(mp, macro_def,p,null,0);
14392   mp->scanner_status=normal; eq_type(mp->warning_info)=m;
14393   equiv(mp->warning_info)=q; mp_get_x_next(mp);
14394 }
14395
14396 @ Parameters to macros are introduced by the keywords \&{expr},
14397 \&{suffix}, \&{text}, \&{primary}, \&{secondary}, and \&{tertiary}.
14398
14399 @<Put each...@>=
14400 mp_primitive(mp, "expr",param_type,expr_base);
14401 @:expr_}{\&{expr} primitive@>
14402 mp_primitive(mp, "suffix",param_type,suffix_base);
14403 @:suffix_}{\&{suffix} primitive@>
14404 mp_primitive(mp, "text",param_type,text_base);
14405 @:text_}{\&{text} primitive@>
14406 mp_primitive(mp, "primary",param_type,primary_macro);
14407 @:primary_}{\&{primary} primitive@>
14408 mp_primitive(mp, "secondary",param_type,secondary_macro);
14409 @:secondary_}{\&{secondary} primitive@>
14410 mp_primitive(mp, "tertiary",param_type,tertiary_macro);
14411 @:tertiary_}{\&{tertiary} primitive@>
14412
14413 @ @<Cases of |print_cmd...@>=
14414 case param_type:
14415   if ( m>=expr_base ) {
14416     if ( m==expr_base ) mp_print(mp, "expr");
14417     else if ( m==suffix_base ) mp_print(mp, "suffix");
14418     else mp_print(mp, "text");
14419   } else if ( m<secondary_macro ) {
14420     mp_print(mp, "primary");
14421   } else if ( m==secondary_macro ) {
14422     mp_print(mp, "secondary");
14423   } else {
14424     mp_print(mp, "tertiary");
14425   }
14426   break;
14427
14428 @ Let's turn next to the more complex processing associated with \&{def}
14429 and \&{vardef}. When the following procedure is called, |cur_mod|
14430 should be either |start_def| or |var_def|.
14431
14432 @c @<Declare the procedure called |check_delimiter|@>
14433 @<Declare the function called |scan_declared_variable|@>
14434 void mp_scan_def (MP mp) {
14435   int m; /* the type of definition */
14436   int n; /* the number of special suffix parameters */
14437   int k; /* the total number of parameters */
14438   int c; /* the kind of macro we're defining */
14439   pointer r; /* parameter-substitution list */
14440   pointer q; /* tail of the macro token list */
14441   pointer p; /* temporary storage */
14442   halfword base; /* |expr_base|, |suffix_base|, or |text_base| */
14443   pointer l_delim,r_delim; /* matching delimiters */
14444   m=mp->cur_mod; c=general_macro; link(hold_head)=null;
14445   q=mp_get_avail(mp); ref_count(q)=null; r=null;
14446   @<Scan the token or variable to be defined;
14447     set |n|, |scanner_status|, and |warning_info|@>;
14448   k=n;
14449   if ( mp->cur_cmd==left_delimiter ) {
14450     @<Absorb delimited parameters, putting them into lists |q| and |r|@>;
14451   }
14452   if ( mp->cur_cmd==param_type ) {
14453     @<Absorb undelimited parameters, putting them into list |r|@>;
14454   }
14455   mp_check_equals(mp);
14456   p=mp_get_avail(mp); info(p)=c; link(q)=p;
14457   @<Attach the replacement text to the tail of node |p|@>;
14458   mp->scanner_status=normal; mp_get_x_next(mp);
14459 }
14460
14461 @ We don't put `|frozen_end_group|' into the replacement text of
14462 a \&{vardef}, because the user may want to redefine `\.{endgroup}'.
14463
14464 @<Attach the replacement text to the tail of node |p|@>=
14465 if ( m==start_def ) {
14466   link(p)=mp_scan_toks(mp, macro_def,r,null,n);
14467 } else { 
14468   q=mp_get_avail(mp); info(q)=mp->bg_loc; link(p)=q;
14469   p=mp_get_avail(mp); info(p)=mp->eg_loc;
14470   link(q)=mp_scan_toks(mp, macro_def,r,p,n);
14471 }
14472 if ( mp->warning_info==bad_vardef ) 
14473   mp_flush_token_list(mp, value(bad_vardef))
14474
14475 @ @<Glob...@>=
14476 int bg_loc;
14477 int eg_loc; /* hash addresses of `\.{begingroup}' and `\.{endgroup}' */
14478
14479 @ @<Scan the token or variable to be defined;...@>=
14480 if ( m==start_def ) {
14481   mp_get_clear_symbol(mp); mp->warning_info=mp->cur_sym; get_t_next;
14482   mp->scanner_status=op_defining; n=0;
14483   eq_type(mp->warning_info)=defined_macro; equiv(mp->warning_info)=q;
14484 } else { 
14485   p=mp_scan_declared_variable(mp);
14486   mp_flush_variable(mp, equiv(info(p)),link(p),true);
14487   mp->warning_info=mp_find_variable(mp, p); mp_flush_list(mp, p);
14488   if ( mp->warning_info==null ) @<Change to `\.{a bad variable}'@>;
14489   mp->scanner_status=var_defining; n=2;
14490   if ( mp->cur_cmd==macro_special ) if ( mp->cur_mod==macro_suffix ) {/* \.{\AT!\#} */
14491     n=3; get_t_next;
14492   }
14493   type(mp->warning_info)=mp_unsuffixed_macro-2+n; value(mp->warning_info)=q;
14494 } /* |mp_suffixed_macro=mp_unsuffixed_macro+1| */
14495
14496 @ @<Change to `\.{a bad variable}'@>=
14497
14498   print_err("This variable already starts with a macro");
14499 @.This variable already...@>
14500   help2("After `vardef a' you can\'t say `vardef a.b'.")
14501     ("So I'll have to discard this definition.");
14502   mp_error(mp); mp->warning_info=bad_vardef;
14503 }
14504
14505 @ @<Initialize table entries...@>=
14506 name_type(bad_vardef)=mp_root; link(bad_vardef)=frozen_bad_vardef;
14507 equiv(frozen_bad_vardef)=bad_vardef; eq_type(frozen_bad_vardef)=tag_token;
14508
14509 @ @<Absorb delimited parameters, putting them into lists |q| and |r|@>=
14510 do {  
14511   l_delim=mp->cur_sym; r_delim=mp->cur_mod; get_t_next;
14512   if ( (mp->cur_cmd==param_type)&&(mp->cur_mod>=expr_base) ) {
14513    base=mp->cur_mod;
14514   } else { 
14515     print_err("Missing parameter type; `expr' will be assumed");
14516 @.Missing parameter type@>
14517     help1("You should've had `expr' or `suffix' or `text' here.");
14518     mp_back_error(mp); base=expr_base;
14519   }
14520   @<Absorb parameter tokens for type |base|@>;
14521   mp_check_delimiter(mp, l_delim,r_delim);
14522   get_t_next;
14523 } while (mp->cur_cmd==left_delimiter)
14524
14525 @ @<Absorb parameter tokens for type |base|@>=
14526 do { 
14527   link(q)=mp_get_avail(mp); q=link(q); info(q)=base+k;
14528   mp_get_symbol(mp); p=mp_get_node(mp, token_node_size); 
14529   value(p)=base+k; info(p)=mp->cur_sym;
14530   if ( k==mp->param_size ) mp_overflow(mp, "parameter stack size",mp->param_size);
14531 @:MetaPost capacity exceeded parameter stack size}{\quad parameter stack size@>
14532   incr(k); link(p)=r; r=p; get_t_next;
14533 } while (mp->cur_cmd==comma)
14534
14535 @ @<Absorb undelimited parameters, putting them into list |r|@>=
14536
14537   p=mp_get_node(mp, token_node_size);
14538   if ( mp->cur_mod<expr_base ) {
14539     c=mp->cur_mod; value(p)=expr_base+k;
14540   } else { 
14541     value(p)=mp->cur_mod+k;
14542     if ( mp->cur_mod==expr_base ) c=expr_macro;
14543     else if ( mp->cur_mod==suffix_base ) c=suffix_macro;
14544     else c=text_macro;
14545   }
14546   if ( k==mp->param_size ) mp_overflow(mp, "parameter stack size",mp->param_size);
14547   incr(k); mp_get_symbol(mp); info(p)=mp->cur_sym; link(p)=r; r=p; get_t_next;
14548   if ( c==expr_macro ) if ( mp->cur_cmd==of_token ) {
14549     c=of_macro; p=mp_get_node(mp, token_node_size);
14550     if ( k==mp->param_size ) mp_overflow(mp, "parameter stack size",mp->param_size);
14551     value(p)=expr_base+k; mp_get_symbol(mp); info(p)=mp->cur_sym;
14552     link(p)=r; r=p; get_t_next;
14553   }
14554 }
14555
14556 @* \[32] Expanding the next token.
14557 Only a few command codes |<min_command| can possibly be returned by
14558 |get_t_next|; in increasing order, they are
14559 |if_test|, |fi_or_else|, |input|, |iteration|, |repeat_loop|,
14560 |exit_test|, |relax|, |scan_tokens|, |expand_after|, and |defined_macro|.
14561
14562 \MP\ usually gets the next token of input by saying |get_x_next|. This is
14563 like |get_t_next| except that it keeps getting more tokens until
14564 finding |cur_cmd>=min_command|. In other words, |get_x_next| expands
14565 macros and removes conditionals or iterations or input instructions that
14566 might be present.
14567
14568 It follows that |get_x_next| might invoke itself recursively. In fact,
14569 there is massive recursion, since macro expansion can involve the
14570 scanning of arbitrarily complex expressions, which in turn involve
14571 macro expansion and conditionals, etc.
14572 @^recursion@>
14573
14574 Therefore it's necessary to declare a whole bunch of |forward|
14575 procedures at this point, and to insert some other procedures
14576 that will be invoked by |get_x_next|.
14577
14578 @<Declarations@>= 
14579 void mp_scan_primary (MP mp);
14580 void mp_scan_secondary (MP mp);
14581 void mp_scan_tertiary (MP mp);
14582 void mp_scan_expression (MP mp);
14583 void mp_scan_suffix (MP mp);
14584 @<Declare the procedure called |macro_call|@>
14585 void mp_get_boolean (MP mp);
14586 void mp_pass_text (MP mp);
14587 void mp_conditional (MP mp);
14588 void mp_start_input (MP mp);
14589 void mp_begin_iteration (MP mp);
14590 void mp_resume_iteration (MP mp);
14591 void mp_stop_iteration (MP mp);
14592
14593 @ An auxiliary subroutine called |expand| is used by |get_x_next|
14594 when it has to do exotic expansion commands.
14595
14596 @c void mp_expand (MP mp) {
14597   pointer p; /* for list manipulation */
14598   size_t k; /* something that we hope is |<=buf_size| */
14599   pool_pointer j; /* index into |str_pool| */
14600   if ( mp->internal[mp_tracing_commands]>unity ) 
14601     if ( mp->cur_cmd!=defined_macro )
14602       show_cur_cmd_mod;
14603   switch (mp->cur_cmd)  {
14604   case if_test:
14605     mp_conditional(mp); /* this procedure is discussed in Part 36 below */
14606     break;
14607   case fi_or_else:
14608     @<Terminate the current conditional and skip to \&{fi}@>;
14609     break;
14610   case input:
14611     @<Initiate or terminate input from a file@>;
14612     break;
14613   case iteration:
14614     if ( mp->cur_mod==end_for ) {
14615       @<Scold the user for having an extra \&{endfor}@>;
14616     } else {
14617       mp_begin_iteration(mp); /* this procedure is discussed in Part 37 below */
14618     }
14619     break;
14620   case repeat_loop: 
14621     @<Repeat a loop@>;
14622     break;
14623   case exit_test: 
14624     @<Exit a loop if the proper time has come@>;
14625     break;
14626   case relax: 
14627     break;
14628   case expand_after: 
14629     @<Expand the token after the next token@>;
14630     break;
14631   case scan_tokens: 
14632     @<Put a string into the input buffer@>;
14633     break;
14634   case defined_macro:
14635    mp_macro_call(mp, mp->cur_mod,null,mp->cur_sym);
14636    break;
14637   }; /* there are no other cases */
14638 }
14639
14640 @ @<Scold the user...@>=
14641
14642   print_err("Extra `endfor'");
14643 @.Extra `endfor'@>
14644   help2("I'm not currently working on a for loop,")
14645     ("so I had better not try to end anything.");
14646   mp_error(mp);
14647 }
14648
14649 @ The processing of \&{input} involves the |start_input| subroutine,
14650 which will be declared later; the processing of \&{endinput} is trivial.
14651
14652 @<Put each...@>=
14653 mp_primitive(mp, "input",input,0);
14654 @:input_}{\&{input} primitive@>
14655 mp_primitive(mp, "endinput",input,1);
14656 @:end_input_}{\&{endinput} primitive@>
14657
14658 @ @<Cases of |print_cmd_mod|...@>=
14659 case input: 
14660   if ( m==0 ) mp_print(mp, "input");
14661   else mp_print(mp, "endinput");
14662   break;
14663
14664 @ @<Initiate or terminate input...@>=
14665 if ( mp->cur_mod>0 ) mp->force_eof=true;
14666 else mp_start_input(mp)
14667
14668 @ We'll discuss the complicated parts of loop operations later. For now
14669 it suffices to know that there's a global variable called |loop_ptr|
14670 that will be |null| if no loop is in progress.
14671
14672 @<Repeat a loop@>=
14673 { while ( token_state &&(loc==null) ) 
14674     mp_end_token_list(mp); /* conserve stack space */
14675   if ( mp->loop_ptr==null ) {
14676     print_err("Lost loop");
14677 @.Lost loop@>
14678     help2("I'm confused; after exiting from a loop, I still seem")
14679       ("to want to repeat it. I'll try to forget the problem.");
14680     mp_error(mp);
14681   } else {
14682     mp_resume_iteration(mp); /* this procedure is in Part 37 below */
14683   }
14684 }
14685
14686 @ @<Exit a loop if the proper time has come@>=
14687 { mp_get_boolean(mp);
14688   if ( mp->internal[mp_tracing_commands]>unity ) 
14689     mp_show_cmd_mod(mp, nullary,mp->cur_exp);
14690   if ( mp->cur_exp==true_code ) {
14691     if ( mp->loop_ptr==null ) {
14692       print_err("No loop is in progress");
14693 @.No loop is in progress@>
14694       help1("Why say `exitif' when there's nothing to exit from?");
14695       if ( mp->cur_cmd==semicolon ) mp_error(mp); else mp_back_error(mp);
14696     } else {
14697      @<Exit prematurely from an iteration@>;
14698     }
14699   } else if ( mp->cur_cmd!=semicolon ) {
14700     mp_missing_err(mp, ";");
14701 @.Missing `;'@>
14702     help2("After `exitif <boolean exp>' I expect to see a semicolon.")
14703     ("I shall pretend that one was there."); mp_back_error(mp);
14704   }
14705 }
14706
14707 @ Here we use the fact that |forever_text| is the only |token_type| that
14708 is less than |loop_text|.
14709
14710 @<Exit prematurely...@>=
14711 { p=null;
14712   do {  
14713     if ( file_state ) {
14714       mp_end_file_reading(mp);
14715     } else { 
14716       if ( token_type<=loop_text ) p=start;
14717       mp_end_token_list(mp);
14718     }
14719   } while (p==null);
14720   if ( p!=info(mp->loop_ptr) ) mp_fatal_error(mp, "*** (loop confusion)");
14721 @.loop confusion@>
14722   mp_stop_iteration(mp); /* this procedure is in Part 34 below */
14723 }
14724
14725 @ @<Expand the token after the next token@>=
14726 { get_t_next;
14727   p=mp_cur_tok(mp); get_t_next;
14728   if ( mp->cur_cmd<min_command ) mp_expand(mp); 
14729   else mp_back_input(mp);
14730   back_list(p);
14731 }
14732
14733 @ @<Put a string into the input buffer@>=
14734 { mp_get_x_next(mp); mp_scan_primary(mp);
14735   if ( mp->cur_type!=mp_string_type ) {
14736     mp_disp_err(mp, null,"Not a string");
14737 @.Not a string@>
14738     help2("I'm going to flush this expression, since")
14739        ("scantokens should be followed by a known string.");
14740     mp_put_get_flush_error(mp, 0);
14741   } else { 
14742     mp_back_input(mp);
14743     if ( length(mp->cur_exp)>0 )
14744        @<Pretend we're reading a new one-line file@>;
14745   }
14746 }
14747
14748 @ @<Pretend we're reading a new one-line file@>=
14749 { mp_begin_file_reading(mp); name=is_scantok;
14750   k=mp->first+length(mp->cur_exp);
14751   if ( k>=mp->max_buf_stack ) {
14752     while ( k>=mp->buf_size ) {
14753       mp_reallocate_buffer(mp,(mp->buf_size+(mp->buf_size>>2)));
14754     }
14755     mp->max_buf_stack=k+1;
14756   }
14757   j=mp->str_start[mp->cur_exp]; limit=k;
14758   while ( mp->first<(size_t)limit ) {
14759     mp->buffer[mp->first]=mp->str_pool[j]; incr(j); incr(mp->first);
14760   }
14761   mp->buffer[limit]='%'; mp->first=limit+1; loc=start; 
14762   mp_flush_cur_exp(mp, 0);
14763 }
14764
14765 @ Here finally is |get_x_next|.
14766
14767 The expression scanning routines to be considered later
14768 communicate via the global quantities |cur_type| and |cur_exp|;
14769 we must be very careful to save and restore these quantities while
14770 macros are being expanded.
14771 @^inner loop@>
14772
14773 @<Declarations@>=
14774 void mp_get_x_next (MP mp);
14775
14776 @ @c void mp_get_x_next (MP mp) {
14777   pointer save_exp; /* a capsule to save |cur_type| and |cur_exp| */
14778   get_t_next;
14779   if ( mp->cur_cmd<min_command ) {
14780     save_exp=mp_stash_cur_exp(mp);
14781     do {  
14782       if ( mp->cur_cmd==defined_macro ) 
14783         mp_macro_call(mp, mp->cur_mod,null,mp->cur_sym);
14784       else 
14785         mp_expand(mp);
14786       get_t_next;
14787      } while (mp->cur_cmd<min_command);
14788      mp_unstash_cur_exp(mp, save_exp); /* that restores |cur_type| and |cur_exp| */
14789   }
14790 }
14791
14792 @ Now let's consider the |macro_call| procedure, which is used to start up
14793 all user-defined macros. Since the arguments to a macro might be expressions,
14794 |macro_call| is recursive.
14795 @^recursion@>
14796
14797 The first parameter to |macro_call| points to the reference count of the
14798 token list that defines the macro. The second parameter contains any
14799 arguments that have already been parsed (see below).  The third parameter
14800 points to the symbolic token that names the macro. If the third parameter
14801 is |null|, the macro was defined by \&{vardef}, so its name can be
14802 reconstructed from the prefix and ``at'' arguments found within the
14803 second parameter.
14804
14805 What is this second parameter? It's simply a linked list of one-word items,
14806 whose |info| fields point to the arguments. In other words, if |arg_list=null|,
14807 no arguments have been scanned yet; otherwise |info(arg_list)| points to
14808 the first scanned argument, and |link(arg_list)| points to the list of
14809 further arguments (if any).
14810
14811 Arguments of type \&{expr} are so-called capsules, which we will
14812 discuss later when we concentrate on expressions; they can be
14813 recognized easily because their |link| field is |void|. Arguments of type
14814 \&{suffix} and \&{text} are token lists without reference counts.
14815
14816 @ After argument scanning is complete, the arguments are moved to the
14817 |param_stack|. (They can't be put on that stack any sooner, because
14818 the stack is growing and shrinking in unpredictable ways as more arguments
14819 are being acquired.)  Then the macro body is fed to the scanner; i.e.,
14820 the replacement text of the macro is placed at the top of the \MP's
14821 input stack, so that |get_t_next| will proceed to read it next.
14822
14823 @<Declare the procedure called |macro_call|@>=
14824 @<Declare the procedure called |print_macro_name|@>
14825 @<Declare the procedure called |print_arg|@>
14826 @<Declare the procedure called |scan_text_arg|@>
14827 void mp_macro_call (MP mp,pointer def_ref, pointer arg_list, 
14828                     pointer macro_name) ;
14829
14830 @ @c
14831 void mp_macro_call (MP mp,pointer def_ref, pointer arg_list, 
14832                     pointer macro_name) {
14833   /* invokes a user-defined control sequence */
14834   pointer r; /* current node in the macro's token list */
14835   pointer p,q; /* for list manipulation */
14836   integer n; /* the number of arguments */
14837   pointer tail = 0; /* tail of the argument list */
14838   pointer l_delim=0,r_delim=0; /* a delimiter pair */
14839   r=link(def_ref); add_mac_ref(def_ref);
14840   if ( arg_list==null ) {
14841     n=0;
14842   } else {
14843    @<Determine the number |n| of arguments already supplied,
14844     and set |tail| to the tail of |arg_list|@>;
14845   }
14846   if ( mp->internal[mp_tracing_macros]>0 ) {
14847     @<Show the text of the macro being expanded, and the existing arguments@>;
14848   }
14849   @<Scan the remaining arguments, if any; set |r| to the first token
14850     of the replacement text@>;
14851   @<Feed the arguments and replacement text to the scanner@>;
14852 }
14853
14854 @ @<Show the text of the macro...@>=
14855 mp_begin_diagnostic(mp); mp_print_ln(mp); 
14856 mp_print_macro_name(mp, arg_list,macro_name);
14857 if ( n==3 ) mp_print(mp, "@@#"); /* indicate a suffixed macro */
14858 mp_show_macro(mp, def_ref,null,100000);
14859 if ( arg_list!=null ) {
14860   n=0; p=arg_list;
14861   do {  
14862     q=info(p);
14863     mp_print_arg(mp, q,n,0);
14864     incr(n); p=link(p);
14865   } while (p!=null);
14866 }
14867 mp_end_diagnostic(mp, false)
14868
14869
14870 @ @<Declare the procedure called |print_macro_name|@>=
14871 void mp_print_macro_name (MP mp,pointer a, pointer n);
14872
14873 @ @c
14874 void mp_print_macro_name (MP mp,pointer a, pointer n) {
14875   pointer p,q; /* they traverse the first part of |a| */
14876   if ( n!=null ) {
14877     mp_print_text(n);
14878   } else  { 
14879     p=info(a);
14880     if ( p==null ) {
14881       mp_print_text(info(info(link(a))));
14882     } else { 
14883       q=p;
14884       while ( link(q)!=null ) q=link(q);
14885       link(q)=info(link(a));
14886       mp_show_token_list(mp, p,null,1000,0);
14887       link(q)=null;
14888     }
14889   }
14890 }
14891
14892 @ @<Declare the procedure called |print_arg|@>=
14893 void mp_print_arg (MP mp,pointer q, integer n, pointer b) ;
14894
14895 @ @c
14896 void mp_print_arg (MP mp,pointer q, integer n, pointer b) {
14897   if ( link(q)==mp_void ) mp_print_nl(mp, "(EXPR");
14898   else if ( (b<text_base)&&(b!=text_macro) ) mp_print_nl(mp, "(SUFFIX");
14899   else mp_print_nl(mp, "(TEXT");
14900   mp_print_int(mp, n); mp_print(mp, ")<-");
14901   if ( link(q)==mp_void ) mp_print_exp(mp, q,1);
14902   else mp_show_token_list(mp, q,null,1000,0);
14903 }
14904
14905 @ @<Determine the number |n| of arguments already supplied...@>=
14906 {  
14907   n=1; tail=arg_list;
14908   while ( link(tail)!=null ) { 
14909     incr(n); tail=link(tail);
14910   }
14911 }
14912
14913 @ @<Scan the remaining arguments, if any; set |r|...@>=
14914 mp->cur_cmd=comma+1; /* anything |<>comma| will do */
14915 while ( info(r)>=expr_base ) { 
14916   @<Scan the delimited argument represented by |info(r)|@>;
14917   r=link(r);
14918 }
14919 if ( mp->cur_cmd==comma ) {
14920   print_err("Too many arguments to ");
14921 @.Too many arguments...@>
14922   mp_print_macro_name(mp, arg_list,macro_name); mp_print_char(mp, ';');
14923   mp_print_nl(mp, "  Missing `"); mp_print_text(r_delim);
14924 @.Missing `)'...@>
14925   mp_print(mp, "' has been inserted");
14926   help3("I'm going to assume that the comma I just read was a")
14927    ("right delimiter, and then I'll begin expanding the macro.")
14928    ("You might want to delete some tokens before continuing.");
14929   mp_error(mp);
14930 }
14931 if ( info(r)!=general_macro ) {
14932   @<Scan undelimited argument(s)@>;
14933 }
14934 r=link(r)
14935
14936 @ At this point, the reader will find it advisable to review the explanation
14937 of token list format that was presented earlier, paying special attention to
14938 the conventions that apply only at the beginning of a macro's token list.
14939
14940 On the other hand, the reader will have to take the expression-parsing
14941 aspects of the following program on faith; we will explain |cur_type|
14942 and |cur_exp| later. (Several things in this program depend on each other,
14943 and it's necessary to jump into the circle somewhere.)
14944
14945 @<Scan the delimited argument represented by |info(r)|@>=
14946 if ( mp->cur_cmd!=comma ) {
14947   mp_get_x_next(mp);
14948   if ( mp->cur_cmd!=left_delimiter ) {
14949     print_err("Missing argument to ");
14950 @.Missing argument...@>
14951     mp_print_macro_name(mp, arg_list,macro_name);
14952     help3("That macro has more parameters than you thought.")
14953      ("I'll continue by pretending that each missing argument")
14954      ("is either zero or null.");
14955     if ( info(r)>=suffix_base ) {
14956       mp->cur_exp=null; mp->cur_type=mp_token_list;
14957     } else { 
14958       mp->cur_exp=0; mp->cur_type=mp_known;
14959     }
14960     mp_back_error(mp); mp->cur_cmd=right_delimiter; 
14961     goto FOUND;
14962   }
14963   l_delim=mp->cur_sym; r_delim=mp->cur_mod;
14964 }
14965 @<Scan the argument represented by |info(r)|@>;
14966 if ( mp->cur_cmd!=comma ) 
14967   @<Check that the proper right delimiter was present@>;
14968 FOUND:  
14969 @<Append the current expression to |arg_list|@>
14970
14971 @ @<Check that the proper right delim...@>=
14972 if ( (mp->cur_cmd!=right_delimiter)||(mp->cur_mod!=l_delim) ) {
14973   if ( info(link(r))>=expr_base ) {
14974     mp_missing_err(mp, ",");
14975 @.Missing `,'@>
14976     help3("I've finished reading a macro argument and am about to")
14977       ("read another; the arguments weren't delimited correctly.")
14978        ("You might want to delete some tokens before continuing.");
14979     mp_back_error(mp); mp->cur_cmd=comma;
14980   } else { 
14981     mp_missing_err(mp, str(text(r_delim)));
14982 @.Missing `)'@>
14983     help2("I've gotten to the end of the macro parameter list.")
14984        ("You might want to delete some tokens before continuing.");
14985     mp_back_error(mp);
14986   }
14987 }
14988
14989 @ A \&{suffix} or \&{text} parameter will have been scanned as
14990 a token list pointed to by |cur_exp|, in which case we will have
14991 |cur_type=token_list|.
14992
14993 @<Append the current expression to |arg_list|@>=
14994
14995   p=mp_get_avail(mp);
14996   if ( mp->cur_type==mp_token_list ) info(p)=mp->cur_exp;
14997   else info(p)=mp_stash_cur_exp(mp);
14998   if ( mp->internal[mp_tracing_macros]>0 ) {
14999     mp_begin_diagnostic(mp); mp_print_arg(mp, info(p),n,info(r)); 
15000     mp_end_diagnostic(mp, false);
15001   }
15002   if ( arg_list==null ) arg_list=p;
15003   else link(tail)=p;
15004   tail=p; incr(n);
15005 }
15006
15007 @ @<Scan the argument represented by |info(r)|@>=
15008 if ( info(r)>=text_base ) {
15009   mp_scan_text_arg(mp, l_delim,r_delim);
15010 } else { 
15011   mp_get_x_next(mp);
15012   if ( info(r)>=suffix_base ) mp_scan_suffix(mp);
15013   else mp_scan_expression(mp);
15014 }
15015
15016 @ The parameters to |scan_text_arg| are either a pair of delimiters
15017 or zero; the latter case is for undelimited text arguments, which
15018 end with the first semicolon or \&{endgroup} or \&{end} that is not
15019 contained in a group.
15020
15021 @<Declare the procedure called |scan_text_arg|@>=
15022 void mp_scan_text_arg (MP mp,pointer l_delim, pointer r_delim) ;
15023
15024 @ @c
15025 void mp_scan_text_arg (MP mp,pointer l_delim, pointer r_delim) {
15026   integer balance; /* excess of |l_delim| over |r_delim| */
15027   pointer p; /* list tail */
15028   mp->warning_info=l_delim; mp->scanner_status=absorbing;
15029   p=hold_head; balance=1; link(hold_head)=null;
15030   while (1)  { 
15031     get_t_next;
15032     if ( l_delim==0 ) {
15033       @<Adjust the balance for an undelimited argument; |break| if done@>;
15034     } else {
15035           @<Adjust the balance for a delimited argument; |break| if done@>;
15036     }
15037     link(p)=mp_cur_tok(mp); p=link(p);
15038   }
15039   mp->cur_exp=link(hold_head); mp->cur_type=mp_token_list;
15040   mp->scanner_status=normal;
15041 }
15042
15043 @ @<Adjust the balance for a delimited argument...@>=
15044 if ( mp->cur_cmd==right_delimiter ) { 
15045   if ( mp->cur_mod==l_delim ) { 
15046     decr(balance);
15047     if ( balance==0 ) break;
15048   }
15049 } else if ( mp->cur_cmd==left_delimiter ) {
15050   if ( mp->cur_mod==r_delim ) incr(balance);
15051 }
15052
15053 @ @<Adjust the balance for an undelimited...@>=
15054 if ( end_of_statement ) { /* |cur_cmd=semicolon|, |end_group|, or |stop| */
15055   if ( balance==1 ) { break; }
15056   else  { if ( mp->cur_cmd==end_group ) decr(balance); }
15057 } else if ( mp->cur_cmd==begin_group ) { 
15058   incr(balance); 
15059 }
15060
15061 @ @<Scan undelimited argument(s)@>=
15062
15063   if ( info(r)<text_macro ) {
15064     mp_get_x_next(mp);
15065     if ( info(r)!=suffix_macro ) {
15066       if ( (mp->cur_cmd==equals)||(mp->cur_cmd==assignment) ) mp_get_x_next(mp);
15067     }
15068   }
15069   switch (info(r)) {
15070   case primary_macro:mp_scan_primary(mp); break;
15071   case secondary_macro:mp_scan_secondary(mp); break;
15072   case tertiary_macro:mp_scan_tertiary(mp); break;
15073   case expr_macro:mp_scan_expression(mp); break;
15074   case of_macro:
15075     @<Scan an expression followed by `\&{of} $\langle$primary$\rangle$'@>;
15076     break;
15077   case suffix_macro:
15078     @<Scan a suffix with optional delimiters@>;
15079     break;
15080   case text_macro:mp_scan_text_arg(mp, 0,0); break;
15081   } /* there are no other cases */
15082   mp_back_input(mp); 
15083   @<Append the current expression to |arg_list|@>;
15084 }
15085
15086 @ @<Scan an expression followed by `\&{of} $\langle$primary$\rangle$'@>=
15087
15088   mp_scan_expression(mp); p=mp_get_avail(mp); info(p)=mp_stash_cur_exp(mp);
15089   if ( mp->internal[mp_tracing_macros]>0 ) { 
15090     mp_begin_diagnostic(mp); mp_print_arg(mp, info(p),n,0); 
15091     mp_end_diagnostic(mp, false);
15092   }
15093   if ( arg_list==null ) arg_list=p; else link(tail)=p;
15094   tail=p;incr(n);
15095   if ( mp->cur_cmd!=of_token ) {
15096     mp_missing_err(mp, "of"); mp_print(mp, " for ");
15097 @.Missing `of'@>
15098     mp_print_macro_name(mp, arg_list,macro_name);
15099     help1("I've got the first argument; will look now for the other.");
15100     mp_back_error(mp);
15101   }
15102   mp_get_x_next(mp); mp_scan_primary(mp);
15103 }
15104
15105 @ @<Scan a suffix with optional delimiters@>=
15106
15107   if ( mp->cur_cmd!=left_delimiter ) {
15108     l_delim=null;
15109   } else { 
15110     l_delim=mp->cur_sym; r_delim=mp->cur_mod; mp_get_x_next(mp);
15111   };
15112   mp_scan_suffix(mp);
15113   if ( l_delim!=null ) {
15114     if ((mp->cur_cmd!=right_delimiter)||(mp->cur_mod!=l_delim) ) {
15115       mp_missing_err(mp, str(text(r_delim)));
15116 @.Missing `)'@>
15117       help2("I've gotten to the end of the macro parameter list.")
15118          ("You might want to delete some tokens before continuing.");
15119       mp_back_error(mp);
15120     }
15121     mp_get_x_next(mp);
15122   }
15123 }
15124
15125 @ Before we put a new token list on the input stack, it is wise to clean off
15126 all token lists that have recently been depleted. Then a user macro that ends
15127 with a call to itself will not require unbounded stack space.
15128
15129 @<Feed the arguments and replacement text to the scanner@>=
15130 while ( token_state &&(loc==null) ) mp_end_token_list(mp); /* conserve stack space */
15131 if ( mp->param_ptr+n>mp->max_param_stack ) {
15132   mp->max_param_stack=mp->param_ptr+n;
15133   if ( mp->max_param_stack>mp->param_size )
15134     mp_overflow(mp, "parameter stack size",mp->param_size);
15135 @:MetaPost capacity exceeded parameter stack size}{\quad parameter stack size@>
15136 }
15137 mp_begin_token_list(mp, def_ref,macro); name=macro_name; loc=r;
15138 if ( n>0 ) {
15139   p=arg_list;
15140   do {  
15141    mp->param_stack[mp->param_ptr]=info(p); incr(mp->param_ptr); p=link(p);
15142   } while (p!=null);
15143   mp_flush_list(mp, arg_list);
15144 }
15145
15146 @ It's sometimes necessary to put a single argument onto |param_stack|.
15147 The |stack_argument| subroutine does this.
15148
15149 @c void mp_stack_argument (MP mp,pointer p) { 
15150   if ( mp->param_ptr==mp->max_param_stack ) {
15151     incr(mp->max_param_stack);
15152     if ( mp->max_param_stack>mp->param_size )
15153       mp_overflow(mp, "parameter stack size",mp->param_size);
15154 @:MetaPost capacity exceeded parameter stack size}{\quad parameter stack size@>
15155   }
15156   mp->param_stack[mp->param_ptr]=p; incr(mp->param_ptr);
15157 }
15158
15159 @* \[33] Conditional processing.
15160 Let's consider now the way \&{if} commands are handled.
15161
15162 Conditions can be inside conditions, and this nesting has a stack
15163 that is independent of other stacks.
15164 Four global variables represent the top of the condition stack:
15165 |cond_ptr| points to pushed-down entries, if~any; |cur_if| tells whether
15166 we are processing \&{if} or \&{elseif}; |if_limit| specifies
15167 the largest code of a |fi_or_else| command that is syntactically legal;
15168 and |if_line| is the line number at which the current conditional began.
15169
15170 If no conditions are currently in progress, the condition stack has the
15171 special state |cond_ptr=null|, |if_limit=normal|, |cur_if=0|, |if_line=0|.
15172 Otherwise |cond_ptr| points to a two-word node; the |type|, |name_type|, and
15173 |link| fields of the first word contain |if_limit|, |cur_if|, and
15174 |cond_ptr| at the next level, and the second word contains the
15175 corresponding |if_line|.
15176
15177 @d if_node_size 2 /* number of words in stack entry for conditionals */
15178 @d if_line_field(A) mp->mem[(A)+1].cint
15179 @d if_code 1 /* code for \&{if} being evaluated */
15180 @d fi_code 2 /* code for \&{fi} */
15181 @d else_code 3 /* code for \&{else} */
15182 @d else_if_code 4 /* code for \&{elseif} */
15183
15184 @<Glob...@>=
15185 pointer cond_ptr; /* top of the condition stack */
15186 integer if_limit; /* upper bound on |fi_or_else| codes */
15187 small_number cur_if; /* type of conditional being worked on */
15188 integer if_line; /* line where that conditional began */
15189
15190 @ @<Set init...@>=
15191 mp->cond_ptr=null; mp->if_limit=normal; mp->cur_if=0; mp->if_line=0;
15192
15193 @ @<Put each...@>=
15194 mp_primitive(mp, "if",if_test,if_code);
15195 @:if_}{\&{if} primitive@>
15196 mp_primitive(mp, "fi",fi_or_else,fi_code); mp->eqtb[frozen_fi]=mp->eqtb[mp->cur_sym];
15197 @:fi_}{\&{fi} primitive@>
15198 mp_primitive(mp, "else",fi_or_else,else_code);
15199 @:else_}{\&{else} primitive@>
15200 mp_primitive(mp, "elseif",fi_or_else,else_if_code);
15201 @:else_if_}{\&{elseif} primitive@>
15202
15203 @ @<Cases of |print_cmd_mod|...@>=
15204 case if_test:
15205 case fi_or_else: 
15206   switch (m) {
15207   case if_code:mp_print(mp, "if"); break;
15208   case fi_code:mp_print(mp, "fi");  break;
15209   case else_code:mp_print(mp, "else"); break;
15210   default: mp_print(mp, "elseif"); break;
15211   }
15212   break;
15213
15214 @ Here is a procedure that ignores text until coming to an \&{elseif},
15215 \&{else}, or \&{fi} at level zero of $\&{if}\ldots\&{fi}$
15216 nesting. After it has acted, |cur_mod| will indicate the token that
15217 was found.
15218
15219 \MP's smallest two command codes are |if_test| and |fi_or_else|; this
15220 makes the skipping process a bit simpler.
15221
15222 @c 
15223 void mp_pass_text (MP mp) {
15224   integer l = 0;
15225   mp->scanner_status=skipping;
15226   mp->warning_info=mp_true_line(mp);
15227   while (1)  { 
15228     get_t_next;
15229     if ( mp->cur_cmd<=fi_or_else ) {
15230       if ( mp->cur_cmd<fi_or_else ) {
15231         incr(l);
15232       } else { 
15233         if ( l==0 ) break;
15234         if ( mp->cur_mod==fi_code ) decr(l);
15235       }
15236     } else {
15237       @<Decrease the string reference count,
15238        if the current token is a string@>;
15239     }
15240   }
15241   mp->scanner_status=normal;
15242 }
15243
15244 @ @<Decrease the string reference count...@>=
15245 if ( mp->cur_cmd==string_token ) { delete_str_ref(mp->cur_mod); }
15246
15247 @ When we begin to process a new \&{if}, we set |if_limit:=if_code|; then
15248 if \&{elseif} or \&{else} or \&{fi} occurs before the current \&{if}
15249 condition has been evaluated, a colon will be inserted.
15250 A construction like `\.{if fi}' would otherwise get \MP\ confused.
15251
15252 @<Push the condition stack@>=
15253 { p=mp_get_node(mp, if_node_size); link(p)=mp->cond_ptr; type(p)=mp->if_limit;
15254   name_type(p)=mp->cur_if; if_line_field(p)=mp->if_line;
15255   mp->cond_ptr=p; mp->if_limit=if_code; mp->if_line=mp_true_line(mp); 
15256   mp->cur_if=if_code;
15257 }
15258
15259 @ @<Pop the condition stack@>=
15260 { p=mp->cond_ptr; mp->if_line=if_line_field(p);
15261   mp->cur_if=name_type(p); mp->if_limit=type(p); mp->cond_ptr=link(p);
15262   mp_free_node(mp, p,if_node_size);
15263 }
15264
15265 @ Here's a procedure that changes the |if_limit| code corresponding to
15266 a given value of |cond_ptr|.
15267
15268 @c void mp_change_if_limit (MP mp,small_number l, pointer p) {
15269   pointer q;
15270   if ( p==mp->cond_ptr ) {
15271     mp->if_limit=l; /* that's the easy case */
15272   } else  { 
15273     q=mp->cond_ptr;
15274     while (1) { 
15275       if ( q==null ) mp_confusion(mp, "if");
15276 @:this can't happen if}{\quad if@>
15277       if ( link(q)==p ) { 
15278         type(q)=l; return;
15279       }
15280       q=link(q);
15281     }
15282   }
15283 }
15284
15285 @ The user is supposed to put colons into the proper parts of conditional
15286 statements. Therefore, \MP\ has to check for their presence.
15287
15288 @c 
15289 void mp_check_colon (MP mp) { 
15290   if ( mp->cur_cmd!=colon ) { 
15291     mp_missing_err(mp, ":");
15292 @.Missing `:'@>
15293     help2("There should've been a colon after the condition.")
15294          ("I shall pretend that one was there.");;
15295     mp_back_error(mp);
15296   }
15297 }
15298
15299 @ A condition is started when the |get_x_next| procedure encounters
15300 an |if_test| command; in that case |get_x_next| calls |conditional|,
15301 which is a recursive procedure.
15302 @^recursion@>
15303
15304 @c void mp_conditional (MP mp) {
15305   pointer save_cond_ptr; /* |cond_ptr| corresponding to this conditional */
15306   int new_if_limit; /* future value of |if_limit| */
15307   pointer p; /* temporary register */
15308   @<Push the condition stack@>; 
15309   save_cond_ptr=mp->cond_ptr;
15310 RESWITCH: 
15311   mp_get_boolean(mp); new_if_limit=else_if_code;
15312   if ( mp->internal[mp_tracing_commands]>unity ) {
15313     @<Display the boolean value of |cur_exp|@>;
15314   }
15315 FOUND: 
15316   mp_check_colon(mp);
15317   if ( mp->cur_exp==true_code ) {
15318     mp_change_if_limit(mp, new_if_limit,save_cond_ptr);
15319     return; /* wait for \&{elseif}, \&{else}, or \&{fi} */
15320   };
15321   @<Skip to \&{elseif} or \&{else} or \&{fi}, then |goto done|@>;
15322 DONE: 
15323   mp->cur_if=mp->cur_mod; mp->if_line=mp_true_line(mp);
15324   if ( mp->cur_mod==fi_code ) {
15325     @<Pop the condition stack@>
15326   } else if ( mp->cur_mod==else_if_code ) {
15327     goto RESWITCH;
15328   } else  { 
15329     mp->cur_exp=true_code; new_if_limit=fi_code; mp_get_x_next(mp); 
15330     goto FOUND;
15331   }
15332 }
15333
15334 @ In a construction like `\&{if} \&{if} \&{true}: $0=1$: \\{foo}
15335 \&{else}: \\{bar} \&{fi}', the first \&{else}
15336 that we come to after learning that the \&{if} is false is not the
15337 \&{else} we're looking for. Hence the following curious logic is needed.
15338
15339 @<Skip to \&{elseif}...@>=
15340 while (1) { 
15341   mp_pass_text(mp);
15342   if ( mp->cond_ptr==save_cond_ptr ) goto DONE;
15343   else if ( mp->cur_mod==fi_code ) @<Pop the condition stack@>;
15344 }
15345
15346
15347 @ @<Display the boolean value...@>=
15348 { mp_begin_diagnostic(mp);
15349   if ( mp->cur_exp==true_code ) mp_print(mp, "{true}");
15350   else mp_print(mp, "{false}");
15351   mp_end_diagnostic(mp, false);
15352 }
15353
15354 @ The processing of conditionals is complete except for the following
15355 code, which is actually part of |get_x_next|. It comes into play when
15356 \&{elseif}, \&{else}, or \&{fi} is scanned.
15357
15358 @<Terminate the current conditional and skip to \&{fi}@>=
15359 if ( mp->cur_mod>mp->if_limit ) {
15360   if ( mp->if_limit==if_code ) { /* condition not yet evaluated */
15361     mp_missing_err(mp, ":");
15362 @.Missing `:'@>
15363     mp_back_input(mp); mp->cur_sym=frozen_colon; mp_ins_error(mp);
15364   } else  { 
15365     print_err("Extra "); mp_print_cmd_mod(mp, fi_or_else,mp->cur_mod);
15366 @.Extra else@>
15367 @.Extra elseif@>
15368 @.Extra fi@>
15369     help1("I'm ignoring this; it doesn't match any if.");
15370     mp_error(mp);
15371   }
15372 } else  { 
15373   while ( mp->cur_mod!=fi_code ) mp_pass_text(mp); /* skip to \&{fi} */
15374   @<Pop the condition stack@>;
15375 }
15376
15377 @* \[34] Iterations.
15378 To bring our treatment of |get_x_next| to a close, we need to consider what
15379 \MP\ does when it sees \&{for}, \&{forsuffixes}, and \&{forever}.
15380
15381 There's a global variable |loop_ptr| that keeps track of the \&{for} loops
15382 that are currently active. If |loop_ptr=null|, no loops are in progress;
15383 otherwise |info(loop_ptr)| points to the iterative text of the current
15384 (innermost) loop, and |link(loop_ptr)| points to the data for any other
15385 loops that enclose the current one.
15386
15387 A loop-control node also has two other fields, called |loop_type| and
15388 |loop_list|, whose contents depend on the type of loop:
15389
15390 \yskip\indent|loop_type(loop_ptr)=null| means that |loop_list(loop_ptr)|
15391 points to a list of one-word nodes whose |info| fields point to the
15392 remaining argument values of a suffix list and expression list.
15393
15394 \yskip\indent|loop_type(loop_ptr)=mp_void| means that the current loop is
15395 `\&{forever}'.
15396
15397 \yskip\indent|loop_type(loop_ptr)=progression_flag| means that
15398 |p=loop_list(loop_ptr)| points to a ``progression node'' and |value(p)|,
15399 |step_size(p)|, and |final_value(p)| contain the data for an arithmetic
15400 progression.
15401
15402 \yskip\indent|loop_type(loop_ptr)=p>mp_void| means that |p| points to an edge
15403 header and |loop_list(loop_ptr)| points into the graphical object list for
15404 that edge header.
15405
15406 \yskip\noindent In the case of a progression node, the first word is not used
15407 because the link field of words in the dynamic memory area cannot be arbitrary.
15408
15409 @d loop_list_loc(A) ((A)+1) /* where the |loop_list| field resides */
15410 @d loop_type(A) info(loop_list_loc((A))) /* the type of \&{for} loop */
15411 @d loop_list(A) link(loop_list_loc((A))) /* the remaining list elements */
15412 @d loop_node_size 2 /* the number of words in a loop control node */
15413 @d progression_node_size 4 /* the number of words in a progression node */
15414 @d step_size(A) mp->mem[(A)+2].sc /* the step size in an arithmetic progression */
15415 @d final_value(A) mp->mem[(A)+3].sc /* the final value in an arithmetic progression */
15416 @d progression_flag (null+2)
15417   /* |loop_type| value when |loop_list| points to a progression node */
15418
15419 @<Glob...@>=
15420 pointer loop_ptr; /* top of the loop-control-node stack */
15421
15422 @ @<Set init...@>=
15423 mp->loop_ptr=null;
15424
15425 @ If the expressions that define an arithmetic progression in
15426 a \&{for} loop don't have known numeric values, the |bad_for|
15427 subroutine screams at the user.
15428
15429 @c void mp_bad_for (MP mp, const char * s) {
15430   mp_disp_err(mp, null,"Improper "); /* show the bad expression above the message */
15431 @.Improper...replaced by 0@>
15432   mp_print(mp, s); mp_print(mp, " has been replaced by 0");
15433   help4("When you say `for x=a step b until c',")
15434     ("the initial value `a' and the step size `b'")
15435     ("and the final value `c' must have known numeric values.")
15436     ("I'm zeroing this one. Proceed, with fingers crossed.");
15437   mp_put_get_flush_error(mp, 0);
15438 }
15439
15440 @ Here's what \MP\ does when \&{for}, \&{forsuffixes}, or \&{forever}
15441 has just been scanned. (This code requires slight familiarity with
15442 expression-parsing routines that we have not yet discussed; but it seems
15443 to belong in the present part of the program, even though the original author
15444 didn't write it until later. The reader may wish to come back to it.)
15445
15446 @c void mp_begin_iteration (MP mp) {
15447   halfword m; /* |expr_base| (\&{for}) or |suffix_base| (\&{forsuffixes}) */
15448   halfword n; /* hash address of the current symbol */
15449   pointer s; /* the new loop-control node */
15450   pointer p; /* substitution list for |scan_toks| */
15451   pointer q;  /* link manipulation register */
15452   pointer pp; /* a new progression node */
15453   m=mp->cur_mod; n=mp->cur_sym; s=mp_get_node(mp, loop_node_size);
15454   if ( m==start_forever ){ 
15455     loop_type(s)=mp_void; p=null; mp_get_x_next(mp);
15456   } else { 
15457     mp_get_symbol(mp); p=mp_get_node(mp, token_node_size);
15458     info(p)=mp->cur_sym; value(p)=m;
15459     mp_get_x_next(mp);
15460     if ( mp->cur_cmd==within_token ) {
15461       @<Set up a picture iteration@>;
15462     } else { 
15463       @<Check for the |"="| or |":="| in a loop header@>;
15464       @<Scan the values to be used in the loop@>;
15465     }
15466   }
15467   @<Check for the presence of a colon@>;
15468   @<Scan the loop text and put it on the loop control stack@>;
15469   mp_resume_iteration(mp);
15470 }
15471
15472 @ @<Check for the |"="| or |":="| in a loop header@>=
15473 if ( (mp->cur_cmd!=equals)&&(mp->cur_cmd!=assignment) ) { 
15474   mp_missing_err(mp, "=");
15475 @.Missing `='@>
15476   help3("The next thing in this loop should have been `=' or `:='.")
15477     ("But don't worry; I'll pretend that an equals sign")
15478     ("was present, and I'll look for the values next.");
15479   mp_back_error(mp);
15480 }
15481
15482 @ @<Check for the presence of a colon@>=
15483 if ( mp->cur_cmd!=colon ) { 
15484   mp_missing_err(mp, ":");
15485 @.Missing `:'@>
15486   help3("The next thing in this loop should have been a `:'.")
15487     ("So I'll pretend that a colon was present;")
15488     ("everything from here to `endfor' will be iterated.");
15489   mp_back_error(mp);
15490 }
15491
15492 @ We append a special |frozen_repeat_loop| token in place of the
15493 `\&{endfor}' at the end of the loop. This will come through \MP's scanner
15494 at the proper time to cause the loop to be repeated.
15495
15496 (If the user tries some shenanigan like `\&{for} $\ldots$ \&{let} \&{endfor}',
15497 he will be foiled by the |get_symbol| routine, which keeps frozen
15498 tokens unchanged. Furthermore the |frozen_repeat_loop| is an \&{outer}
15499 token, so it won't be lost accidentally.)
15500
15501 @ @<Scan the loop text...@>=
15502 q=mp_get_avail(mp); info(q)=frozen_repeat_loop;
15503 mp->scanner_status=loop_defining; mp->warning_info=n;
15504 info(s)=mp_scan_toks(mp, iteration,p,q,0); mp->scanner_status=normal;
15505 link(s)=mp->loop_ptr; mp->loop_ptr=s
15506
15507 @ @<Initialize table...@>=
15508 eq_type(frozen_repeat_loop)=repeat_loop+outer_tag;
15509 text(frozen_repeat_loop)=intern(" ENDFOR");
15510
15511 @ The loop text is inserted into \MP's scanning apparatus by the
15512 |resume_iteration| routine.
15513
15514 @c void mp_resume_iteration (MP mp) {
15515   pointer p,q; /* link registers */
15516   p=loop_type(mp->loop_ptr);
15517   if ( p==progression_flag ) { 
15518     p=loop_list(mp->loop_ptr); /* now |p| points to a progression node */
15519     mp->cur_exp=value(p);
15520     if ( @<The arithmetic progression has ended@> ) {
15521       mp_stop_iteration(mp);
15522       return;
15523     }
15524     mp->cur_type=mp_known; q=mp_stash_cur_exp(mp); /* make |q| an \&{expr} argument */
15525     value(p)=mp->cur_exp+step_size(p); /* set |value(p)| for the next iteration */
15526   } else if ( p==null ) { 
15527     p=loop_list(mp->loop_ptr);
15528     if ( p==null ) {
15529       mp_stop_iteration(mp);
15530       return;
15531     }
15532     loop_list(mp->loop_ptr)=link(p); q=info(p); free_avail(p);
15533   } else if ( p==mp_void ) { 
15534     mp_begin_token_list(mp, info(mp->loop_ptr),forever_text); return;
15535   } else {
15536     @<Make |q| a capsule containing the next picture component from
15537       |loop_list(loop_ptr)| or |goto not_found|@>;
15538   }
15539   mp_begin_token_list(mp, info(mp->loop_ptr),loop_text);
15540   mp_stack_argument(mp, q);
15541   if ( mp->internal[mp_tracing_commands]>unity ) {
15542      @<Trace the start of a loop@>;
15543   }
15544   return;
15545 NOT_FOUND:
15546   mp_stop_iteration(mp);
15547 }
15548
15549 @ @<The arithmetic progression has ended@>=
15550 ((step_size(p)>0)&&(mp->cur_exp>final_value(p)))||
15551  ((step_size(p)<0)&&(mp->cur_exp<final_value(p)))
15552
15553 @ @<Trace the start of a loop@>=
15554
15555   mp_begin_diagnostic(mp); mp_print_nl(mp, "{loop value=");
15556 @.loop value=n@>
15557   if ( (q!=null)&&(link(q)==mp_void) ) mp_print_exp(mp, q,1);
15558   else mp_show_token_list(mp, q,null,50,0);
15559   mp_print_char(mp, '}'); mp_end_diagnostic(mp, false);
15560 }
15561
15562 @ @<Make |q| a capsule containing the next picture component from...@>=
15563 { q=loop_list(mp->loop_ptr);
15564   if ( q==null ) goto NOT_FOUND;
15565   skip_component(q) goto NOT_FOUND;
15566   mp->cur_exp=mp_copy_objects(mp, loop_list(mp->loop_ptr),q);
15567   mp_init_bbox(mp, mp->cur_exp);
15568   mp->cur_type=mp_picture_type;
15569   loop_list(mp->loop_ptr)=q;
15570   q=mp_stash_cur_exp(mp);
15571 }
15572
15573 @ A level of loop control disappears when |resume_iteration| has decided
15574 not to resume, or when an \&{exitif} construction has removed the loop text
15575 from the input stack.
15576
15577 @c void mp_stop_iteration (MP mp) {
15578   pointer p,q; /* the usual */
15579   p=loop_type(mp->loop_ptr);
15580   if ( p==progression_flag )  {
15581     mp_free_node(mp, loop_list(mp->loop_ptr),progression_node_size);
15582   } else if ( p==null ){ 
15583     q=loop_list(mp->loop_ptr);
15584     while ( q!=null ) {
15585       p=info(q);
15586       if ( p!=null ) {
15587         if ( link(p)==mp_void ) { /* it's an \&{expr} parameter */
15588           mp_recycle_value(mp, p); mp_free_node(mp, p,value_node_size);
15589         } else {
15590           mp_flush_token_list(mp, p); /* it's a \&{suffix} or \&{text} parameter */
15591         }
15592       }
15593       p=q; q=link(q); free_avail(p);
15594     }
15595   } else if ( p>progression_flag ) {
15596     delete_edge_ref(p);
15597   }
15598   p=mp->loop_ptr; mp->loop_ptr=link(p); mp_flush_token_list(mp, info(p));
15599   mp_free_node(mp, p,loop_node_size);
15600 }
15601
15602 @ Now that we know all about loop control, we can finish up
15603 the missing portion of |begin_iteration| and we'll be done.
15604
15605 The following code is performed after the `\.=' has been scanned in
15606 a \&{for} construction (if |m=expr_base|) or a \&{forsuffixes} construction
15607 (if |m=suffix_base|).
15608
15609 @<Scan the values to be used in the loop@>=
15610 loop_type(s)=null; q=loop_list_loc(s); link(q)=null; /* |link(q)=loop_list(s)| */
15611 do {  
15612   mp_get_x_next(mp);
15613   if ( m!=expr_base ) {
15614     mp_scan_suffix(mp);
15615   } else { 
15616     if ( mp->cur_cmd>=colon ) if ( mp->cur_cmd<=comma ) 
15617           goto CONTINUE;
15618     mp_scan_expression(mp);
15619     if ( mp->cur_cmd==step_token ) if ( q==loop_list_loc(s) ) {
15620       @<Prepare for step-until construction and |break|@>;
15621     }
15622     mp->cur_exp=mp_stash_cur_exp(mp);
15623   }
15624   link(q)=mp_get_avail(mp); q=link(q); 
15625   info(q)=mp->cur_exp; mp->cur_type=mp_vacuous;
15626 CONTINUE:
15627   ;
15628 } while (mp->cur_cmd==comma)
15629
15630 @ @<Prepare for step-until construction and |break|@>=
15631
15632   if ( mp->cur_type!=mp_known ) mp_bad_for(mp, "initial value");
15633   pp=mp_get_node(mp, progression_node_size); value(pp)=mp->cur_exp;
15634   mp_get_x_next(mp); mp_scan_expression(mp);
15635   if ( mp->cur_type!=mp_known ) mp_bad_for(mp, "step size");
15636   step_size(pp)=mp->cur_exp;
15637   if ( mp->cur_cmd!=until_token ) { 
15638     mp_missing_err(mp, "until");
15639 @.Missing `until'@>
15640     help2("I assume you meant to say `until' after `step'.")
15641       ("So I'll look for the final value and colon next.");
15642     mp_back_error(mp);
15643   }
15644   mp_get_x_next(mp); mp_scan_expression(mp);
15645   if ( mp->cur_type!=mp_known ) mp_bad_for(mp, "final value");
15646   final_value(pp)=mp->cur_exp; loop_list(s)=pp;
15647   loop_type(s)=progression_flag; 
15648   break;
15649 }
15650
15651 @ The last case is when we have just seen ``\&{within}'', and we need to
15652 parse a picture expression and prepare to iterate over it.
15653
15654 @<Set up a picture iteration@>=
15655 { mp_get_x_next(mp);
15656   mp_scan_expression(mp);
15657   @<Make sure the current expression is a known picture@>;
15658   loop_type(s)=mp->cur_exp; mp->cur_type=mp_vacuous;
15659   q=link(dummy_loc(mp->cur_exp));
15660   if ( q!= null ) 
15661     if ( is_start_or_stop(q) )
15662       if ( mp_skip_1component(mp, q)==null ) q=link(q);
15663   loop_list(s)=q;
15664 }
15665
15666 @ @<Make sure the current expression is a known picture@>=
15667 if ( mp->cur_type!=mp_picture_type ) {
15668   mp_disp_err(mp, null,"Improper iteration spec has been replaced by nullpicture");
15669   help1("When you say `for x in p', p must be a known picture.");
15670   mp_put_get_flush_error(mp, mp_get_node(mp, edge_header_size));
15671   mp_init_edges(mp, mp->cur_exp); mp->cur_type=mp_picture_type;
15672 }
15673
15674 @* \[35] File names.
15675 It's time now to fret about file names.  Besides the fact that different
15676 operating systems treat files in different ways, we must cope with the
15677 fact that completely different naming conventions are used by different
15678 groups of people. The following programs show what is required for one
15679 particular operating system; similar routines for other systems are not
15680 difficult to devise.
15681 @^system dependencies@>
15682
15683 \MP\ assumes that a file name has three parts: the name proper; its
15684 ``extension''; and a ``file area'' where it is found in an external file
15685 system.  The extension of an input file is assumed to be
15686 `\.{.mp}' unless otherwise specified; it is `\.{.log}' on the
15687 transcript file that records each run of \MP; it is `\.{.tfm}' on the font
15688 metric files that describe characters in any fonts created by \MP; it is
15689 `\.{.ps}' or `.{\it nnn}' for some number {\it nnn} on the \ps\ output files;
15690 and it is `\.{.mem}' on the mem files written by \.{INIMP} to initialize \MP.
15691 The file area can be arbitrary on input files, but files are usually
15692 output to the user's current area.  If an input file cannot be
15693 found on the specified area, \MP\ will look for it on a special system
15694 area; this special area is intended for commonly used input files.
15695
15696 Simple uses of \MP\ refer only to file names that have no explicit
15697 extension or area. For example, a person usually says `\.{input} \.{cmr10}'
15698 instead of `\.{input} \.{cmr10.new}'. Simple file
15699 names are best, because they make the \MP\ source files portable;
15700 whenever a file name consists entirely of letters and digits, it should be
15701 treated in the same way by all implementations of \MP. However, users
15702 need the ability to refer to other files in their environment, especially
15703 when responding to error messages concerning unopenable files; therefore
15704 we want to let them use the syntax that appears in their favorite
15705 operating system.
15706
15707 @ \MP\ uses the same conventions that have proved to be satisfactory for
15708 \TeX\ and \MF. In order to isolate the system-dependent aspects of file names,
15709 @^system dependencies@>
15710 the system-independent parts of \MP\ are expressed in terms
15711 of three system-dependent
15712 procedures called |begin_name|, |more_name|, and |end_name|. In
15713 essence, if the user-specified characters of the file name are $c_1\ldots c_n$,
15714 the system-independent driver program does the operations
15715 $$|begin_name|;\,|more_name|(c_1);\,\ldots\,;\,|more_name|(c_n);
15716 \,|end_name|.$$
15717 These three procedures communicate with each other via global variables.
15718 Afterwards the file name will appear in the string pool as three strings
15719 called |cur_name|\penalty10000\hskip-.05em,
15720 |cur_area|, and |cur_ext|; the latter two are null (i.e.,
15721 |""|), unless they were explicitly specified by the user.
15722
15723 Actually the situation is slightly more complicated, because \MP\ needs
15724 to know when the file name ends. The |more_name| routine is a function
15725 (with side effects) that returns |true| on the calls |more_name|$(c_1)$,
15726 \dots, |more_name|$(c_{n-1})$. The final call |more_name|$(c_n)$
15727 returns |false|; or, it returns |true| and $c_n$ is the last character
15728 on the current input line. In other words,
15729 |more_name| is supposed to return |true| unless it is sure that the
15730 file name has been completely scanned; and |end_name| is supposed to be able
15731 to finish the assembly of |cur_name|, |cur_area|, and |cur_ext| regardless of
15732 whether $|more_name|(c_n)$ returned |true| or |false|.
15733
15734 @<Glob...@>=
15735 char * cur_name; /* name of file just scanned */
15736 char * cur_area; /* file area just scanned, or \.{""} */
15737 char * cur_ext; /* file extension just scanned, or \.{""} */
15738
15739 @ It is easier to maintain reference counts if we assign initial values.
15740
15741 @<Set init...@>=
15742 mp->cur_name=xstrdup(""); 
15743 mp->cur_area=xstrdup(""); 
15744 mp->cur_ext=xstrdup("");
15745
15746 @ @<Dealloc variables@>=
15747 xfree(mp->cur_area);
15748 xfree(mp->cur_name);
15749 xfree(mp->cur_ext);
15750
15751 @ The file names we shall deal with for illustrative purposes have the
15752 following structure:  If the name contains `\.>' or `\.:', the file area
15753 consists of all characters up to and including the final such character;
15754 otherwise the file area is null.  If the remaining file name contains
15755 `\..', the file extension consists of all such characters from the first
15756 remaining `\..' to the end, otherwise the file extension is null.
15757 @^system dependencies@>
15758
15759 We can scan such file names easily by using two global variables that keep track
15760 of the occurrences of area and extension delimiters.  Note that these variables
15761 cannot be of type |pool_pointer| because a string pool compaction could occur
15762 while scanning a file name.
15763
15764 @<Glob...@>=
15765 integer area_delimiter;
15766   /* most recent `\.>' or `\.:' relative to |str_start[str_ptr]| */
15767 integer ext_delimiter; /* the relevant `\..', if any */
15768
15769 @ Here now is the first of the system-dependent routines for file name scanning.
15770 @^system dependencies@>
15771
15772 The file name length is limited to |file_name_size|. That is good, because
15773 in the current configuration we cannot call |mp_do_compaction| while a name 
15774 is being scanned, |mp->area_delimiter| and |mp->ext_delimiter| are direct
15775 offsets into |mp->str_pool|. I am not in a great hurry to fix this, because 
15776 calling |str_room()| just once is more efficient anyway. TODO.
15777
15778 @<Declare subroutines for parsing file names@>=
15779 void mp_begin_name (MP mp) { 
15780   xfree(mp->cur_name); 
15781   xfree(mp->cur_area); 
15782   xfree(mp->cur_ext);
15783   mp->area_delimiter=-1; 
15784   mp->ext_delimiter=-1;
15785   str_room(file_name_size); 
15786 }
15787
15788 @ And here's the second.
15789 @^system dependencies@>
15790
15791 @<Declare subroutines for parsing file names@>=
15792 boolean mp_more_name (MP mp, ASCII_code c) {
15793   if (c==' ') {
15794     return false;
15795   } else { 
15796     if ( (c=='>')||(c==':') ) { 
15797       mp->area_delimiter=mp->pool_ptr; 
15798       mp->ext_delimiter=-1;
15799     } else if ( (c=='.')&&(mp->ext_delimiter<0) ) {
15800       mp->ext_delimiter=mp->pool_ptr;
15801     }
15802     append_char(c); /* contribute |c| to the current string */
15803     return true;
15804   }
15805 }
15806
15807 @ The third.
15808 @^system dependencies@>
15809
15810 @d copy_pool_segment(A,B,C) { 
15811       A = xmalloc(C+1,sizeof(char)); 
15812       strncpy(A,(char *)(mp->str_pool+B),C);  
15813       A[C] = 0;}
15814
15815 @<Declare subroutines for parsing file names@>=
15816 void mp_end_name (MP mp) {
15817   pool_pointer s; /* length of area, name, and extension */
15818   unsigned int len;
15819   /* "my/w.mp" */
15820   s = mp->str_start[mp->str_ptr];
15821   if ( mp->area_delimiter<0 ) {    
15822     mp->cur_area=xstrdup("");
15823   } else {
15824     len = mp->area_delimiter-s; 
15825     copy_pool_segment(mp->cur_area,s,len);
15826     s += len+1;
15827   }
15828   if ( mp->ext_delimiter<0 ) {
15829     mp->cur_ext=xstrdup("");
15830     len = mp->pool_ptr-s; 
15831   } else {
15832     copy_pool_segment(mp->cur_ext,mp->ext_delimiter,(mp->pool_ptr-mp->ext_delimiter));
15833     len = mp->ext_delimiter-s;
15834   }
15835   copy_pool_segment(mp->cur_name,s,len);
15836   mp->pool_ptr=s; /* don't need this partial string */
15837 }
15838
15839 @ Conversely, here is a routine that takes three strings and prints a file
15840 name that might have produced them. (The routine is system dependent, because
15841 some operating systems put the file area last instead of first.)
15842 @^system dependencies@>
15843
15844 @<Basic printing...@>=
15845 void mp_print_file_name (MP mp, char * n, char * a, char * e) { 
15846   mp_print(mp, a); mp_print(mp, n); mp_print(mp, e);
15847 }
15848
15849 @ Another system-dependent routine is needed to convert three internal
15850 \MP\ strings
15851 to the |name_of_file| value that is used to open files. The present code
15852 allows both lowercase and uppercase letters in the file name.
15853 @^system dependencies@>
15854
15855 @d append_to_name(A) { c=(A); 
15856   if ( k<file_name_size ) {
15857     mp->name_of_file[k]=xchr(c);
15858     incr(k);
15859   }
15860 }
15861
15862 @<Declare subroutines for parsing file names@>=
15863 void mp_pack_file_name (MP mp, const char *n, const char *a, const char *e) {
15864   integer k; /* number of positions filled in |name_of_file| */
15865   ASCII_code c; /* character being packed */
15866   const char *j; /* a character  index */
15867   k=0;
15868   assert(n);
15869   if (a!=NULL) {
15870     for (j=a;*j;j++) { append_to_name(*j); }
15871   }
15872   for (j=n;*j;j++) { append_to_name(*j); }
15873   if (e!=NULL) {
15874     for (j=e;*j;j++) { append_to_name(*j); }
15875   }
15876   mp->name_of_file[k]=0;
15877   mp->name_length=k; 
15878 }
15879
15880 @ @<Internal library declarations@>=
15881 void mp_pack_file_name (MP mp, const char *n, const char *a, const char *e) ;
15882
15883 @ A messier routine is also needed, since mem file names must be scanned
15884 before \MP's string mechanism has been initialized. We shall use the
15885 global variable |MP_mem_default| to supply the text for default system areas
15886 and extensions related to mem files.
15887 @^system dependencies@>
15888
15889 @d mem_default_length 9 /* length of the |MP_mem_default| string */
15890 @d mem_ext_length 4 /* length of its `\.{.mem}' part */
15891 @d mem_extension ".mem" /* the extension, as a \.{WEB} constant */
15892
15893 @<Glob...@>=
15894 char *MP_mem_default;
15895
15896 @ @<Option variables@>=
15897 char *mem_name; /* for commandline */
15898
15899 @ @<Allocate or initialize ...@>=
15900 mp->MP_mem_default = xstrdup("plain.mem");
15901 mp->mem_name = xstrdup(opt->mem_name);
15902 @.plain@>
15903 @^system dependencies@>
15904
15905 @ @<Dealloc variables@>=
15906 xfree(mp->MP_mem_default);
15907 xfree(mp->mem_name);
15908
15909 @ @<Check the ``constant'' values for consistency@>=
15910 if ( mem_default_length>file_name_size ) mp->bad=20;
15911
15912 @ Here is the messy routine that was just mentioned. It sets |name_of_file|
15913 from the first |n| characters of |MP_mem_default|, followed by
15914 |buffer[a..b-1]|, followed by the last |mem_ext_length| characters of
15915 |MP_mem_default|.
15916
15917 We dare not give error messages here, since \MP\ calls this routine before
15918 the |error| routine is ready to roll. Instead, we simply drop excess characters,
15919 since the error will be detected in another way when a strange file name
15920 isn't found.
15921 @^system dependencies@>
15922
15923 @c void mp_pack_buffered_name (MP mp,small_number n, integer a,
15924                                integer b) {
15925   integer k; /* number of positions filled in |name_of_file| */
15926   ASCII_code c; /* character being packed */
15927   integer j; /* index into |buffer| or |MP_mem_default| */
15928   if ( n+b-a+1+mem_ext_length>file_name_size )
15929     b=a+file_name_size-n-1-mem_ext_length;
15930   k=0;
15931   for (j=0;j<n;j++) {
15932     append_to_name(xord((int)mp->MP_mem_default[j]));
15933   }
15934   for (j=a;j<b;j++) {
15935     append_to_name(mp->buffer[j]);
15936   }
15937   for (j=mem_default_length-mem_ext_length;
15938       j<mem_default_length;j++) {
15939     append_to_name(xord((int)mp->MP_mem_default[j]));
15940   } 
15941   mp->name_of_file[k]=0;
15942   mp->name_length=k; 
15943 }
15944
15945 @ Here is the only place we use |pack_buffered_name|. This part of the program
15946 becomes active when a ``virgin'' \MP\ is trying to get going, just after
15947 the preliminary initialization, or when the user is substituting another
15948 mem file by typing `\.\&' after the initial `\.{**}' prompt.  The buffer
15949 contains the first line of input in |buffer[loc..(last-1)]|, where
15950 |loc<last| and |buffer[loc]<>" "|.
15951
15952 @<Declarations@>=
15953 boolean mp_open_mem_file (MP mp) ;
15954
15955 @ @c
15956 boolean mp_open_mem_file (MP mp) {
15957   int j; /* the first space after the file name */
15958   if (mp->mem_name!=NULL) {
15959     mp->mem_file = (mp->open_file)(mp,mp->mem_name, "r", mp_filetype_memfile);
15960     if ( mp->mem_file ) return true;
15961   }
15962   j=loc;
15963   if ( mp->buffer[loc]=='&' ) {
15964     incr(loc); j=loc; mp->buffer[mp->last]=' ';
15965     while ( mp->buffer[j]!=' ' ) incr(j);
15966     mp_pack_buffered_name(mp, 0,loc,j); /* try first without the system file area */
15967     if ( mp_w_open_in(mp, &mp->mem_file) ) goto FOUND;
15968     wake_up_terminal;
15969     wterm_ln("Sorry, I can\'t find that mem file; will try PLAIN.");
15970 @.Sorry, I can't find...@>
15971     update_terminal;
15972   }
15973   /* now pull out all the stops: try for the system \.{plain} file */
15974   mp_pack_buffered_name(mp, mem_default_length-mem_ext_length,0,0);
15975   if ( ! mp_w_open_in(mp, &mp->mem_file) ) {
15976     wake_up_terminal;
15977     wterm_ln("I can\'t find the PLAIN mem file!\n");
15978 @.I can't find PLAIN...@>
15979 @.plain@>
15980     return false;
15981   }
15982 FOUND:
15983   loc=j; return true;
15984 }
15985
15986 @ Operating systems often make it possible to determine the exact name (and
15987 possible version number) of a file that has been opened. The following routine,
15988 which simply makes a \MP\ string from the value of |name_of_file|, should
15989 ideally be changed to deduce the full name of file~|f|, which is the file
15990 most recently opened, if it is possible to do this.
15991 @^system dependencies@>
15992
15993 @<Declarations@>=
15994 #define mp_a_make_name_string(A,B)  mp_make_name_string(A)
15995 #define mp_b_make_name_string(A,B)  mp_make_name_string(A)
15996 #define mp_w_make_name_string(A,B)  mp_make_name_string(A)
15997
15998 @ @c 
15999 str_number mp_make_name_string (MP mp) {
16000   int k; /* index into |name_of_file| */
16001   str_room(mp->name_length);
16002   for (k=0;k<mp->name_length;k++) {
16003     append_char(xord((int)mp->name_of_file[k]));
16004   }
16005   return mp_make_string(mp);
16006 }
16007
16008 @ Now let's consider the ``driver''
16009 routines by which \MP\ deals with file names
16010 in a system-independent manner.  First comes a procedure that looks for a
16011 file name in the input by taking the information from the input buffer.
16012 (We can't use |get_next|, because the conversion to tokens would
16013 destroy necessary information.)
16014
16015 This procedure doesn't allow semicolons or percent signs to be part of
16016 file names, because of other conventions of \MP.
16017 {\sl The {\logos METAFONT\/}book} doesn't
16018 use semicolons or percents immediately after file names, but some users
16019 no doubt will find it natural to do so; therefore system-dependent
16020 changes to allow such characters in file names should probably
16021 be made with reluctance, and only when an entire file name that
16022 includes special characters is ``quoted'' somehow.
16023 @^system dependencies@>
16024
16025 @c void mp_scan_file_name (MP mp) { 
16026   mp_begin_name(mp);
16027   while ( mp->buffer[loc]==' ' ) incr(loc);
16028   while (1) { 
16029     if ( (mp->buffer[loc]==';')||(mp->buffer[loc]=='%') ) break;
16030     if ( ! mp_more_name(mp, mp->buffer[loc]) ) break;
16031     incr(loc);
16032   }
16033   mp_end_name(mp);
16034 }
16035
16036 @ Here is another version that takes its input from a string.
16037
16038 @<Declare subroutines for parsing file names@>=
16039 void mp_str_scan_file (MP mp,  str_number s) {
16040   pool_pointer p,q; /* current position and stopping point */
16041   mp_begin_name(mp);
16042   p=mp->str_start[s]; q=str_stop(s);
16043   while ( p<q ){ 
16044     if ( ! mp_more_name(mp, mp->str_pool[p]) ) break;
16045     incr(p);
16046   }
16047   mp_end_name(mp);
16048 }
16049
16050 @ And one that reads from a |char*|.
16051
16052 @<Declare subroutines for parsing file names@>=
16053 void mp_ptr_scan_file (MP mp,  char *s) {
16054   char *p, *q; /* current position and stopping point */
16055   mp_begin_name(mp);
16056   p=s; q=p+strlen(s);
16057   while ( p<q ){ 
16058     if ( ! mp_more_name(mp, *p)) break;
16059     p++;
16060   }
16061   mp_end_name(mp);
16062 }
16063
16064
16065 @ The global variable |job_name| contains the file name that was first
16066 \&{input} by the user. This name is extended by `\.{.log}' and `\.{ps}' and
16067 `\.{.mem}' and `\.{.tfm}' in order to make the names of \MP's output files.
16068
16069 @<Glob...@>=
16070 boolean log_opened; /* has the transcript file been opened? */
16071 char *log_name; /* full name of the log file */
16072
16073 @ @<Option variables@>=
16074 char *job_name; /* principal file name */
16075
16076 @ Initially |job_name=NULL|; it becomes nonzero as soon as the true name is known.
16077 We have |job_name=NULL| if and only if the `\.{log}' file has not been opened,
16078 except of course for a short time just after |job_name| has become nonzero.
16079
16080 @<Allocate or ...@>=
16081 mp->job_name=mp_xstrdup(mp, opt->job_name); 
16082 mp->log_opened=false;
16083
16084 @ @<Dealloc variables@>=
16085 xfree(mp->job_name);
16086
16087 @ Here is a routine that manufactures the output file names, assuming that
16088 |job_name<>0|. It ignores and changes the current settings of |cur_area|
16089 and |cur_ext|.
16090
16091 @d pack_cur_name mp_pack_file_name(mp, mp->cur_name,mp->cur_area,mp->cur_ext)
16092
16093 @<Declarations@>=
16094 void mp_pack_job_name (MP mp, const char *s) ;
16095
16096 @ @c 
16097 void mp_pack_job_name (MP mp, const char  *s) { /* |s = ".log"|, |".mem"|, |".ps"|, or .\\{nnn} */
16098   xfree(mp->cur_name); mp->cur_name=xstrdup(mp->job_name);
16099   xfree(mp->cur_area); mp->cur_area=xstrdup(""); 
16100   xfree(mp->cur_ext);  mp->cur_ext=xstrdup(s);
16101   pack_cur_name;
16102 }
16103
16104 @ If some trouble arises when \MP\ tries to open a file, the following
16105 routine calls upon the user to supply another file name. Parameter~|s|
16106 is used in the error message to identify the type of file; parameter~|e|
16107 is the default extension if none is given. Upon exit from the routine,
16108 variables |cur_name|, |cur_area|, |cur_ext|, and |name_of_file| are
16109 ready for another attempt at file opening.
16110
16111 @<Declarations@>=
16112 void mp_prompt_file_name (MP mp, const char * s, const char * e) ;
16113
16114 @ @c void mp_prompt_file_name (MP mp, const char * s, const char * e) {
16115   size_t k; /* index into |buffer| */
16116   char * saved_cur_name;
16117   if ( mp->interaction==mp_scroll_mode ) 
16118         wake_up_terminal;
16119   if (strcmp(s,"input file name")==0) {
16120         print_err("I can\'t find file `");
16121 @.I can't find file x@>
16122   } else {
16123         print_err("I can\'t write on file `");
16124   }
16125 @.I can't write on file x@>
16126   mp_print_file_name(mp, mp->cur_name,mp->cur_area,mp->cur_ext); 
16127   mp_print(mp, "'.");
16128   if (strcmp(e,"")==0) 
16129         mp_show_context(mp);
16130   mp_print_nl(mp, "Please type another "); mp_print(mp, s);
16131 @.Please type...@>
16132   if ( mp->interaction<mp_scroll_mode )
16133     mp_fatal_error(mp, "*** (job aborted, file error in nonstop mode)");
16134 @.job aborted, file error...@>
16135   saved_cur_name = xstrdup(mp->cur_name);
16136   clear_terminal; prompt_input(": "); @<Scan file name in the buffer@>;
16137   if (strcmp(mp->cur_ext,"")==0) 
16138         mp->cur_ext=xstrdup(e);
16139   if (strlen(mp->cur_name)==0) {
16140     mp->cur_name=saved_cur_name;
16141   } else {
16142     xfree(saved_cur_name);
16143   }
16144   pack_cur_name;
16145 }
16146
16147 @ @<Scan file name in the buffer@>=
16148
16149   mp_begin_name(mp); k=mp->first;
16150   while ( (mp->buffer[k]==' ')&&(k<mp->last) ) incr(k);
16151   while (1) { 
16152     if ( k==mp->last ) break;
16153     if ( ! mp_more_name(mp, mp->buffer[k]) ) break;
16154     incr(k);
16155   }
16156   mp_end_name(mp);
16157 }
16158
16159 @ The |open_log_file| routine is used to open the transcript file and to help
16160 it catch up to what has previously been printed on the terminal.
16161
16162 @c void mp_open_log_file (MP mp) {
16163   int old_setting; /* previous |selector| setting */
16164   int k; /* index into |months| and |buffer| */
16165   int l; /* end of first input line */
16166   integer m; /* the current month */
16167   const char *months="JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC"; 
16168     /* abbreviations of month names */
16169   old_setting=mp->selector;
16170   if ( mp->job_name==NULL ) {
16171      mp->job_name=xstrdup("mpout");
16172   }
16173   mp_pack_job_name(mp,".log");
16174   while ( ! mp_a_open_out(mp, &mp->log_file, mp_filetype_log) ) {
16175     @<Try to get a different log file name@>;
16176   }
16177   mp->log_name=xstrdup(mp->name_of_file);
16178   mp->selector=log_only; mp->log_opened=true;
16179   @<Print the banner line, including the date and time@>;
16180   mp->input_stack[mp->input_ptr]=mp->cur_input; 
16181     /* make sure bottom level is in memory */
16182 @.**@>
16183   if (!mp->noninteractive) {
16184     mp_print_nl(mp, "**");
16185     l=mp->input_stack[0].limit_field-1; /* last position of first line */
16186     for (k=0;k<=l;k++) mp_print_str(mp, mp->buffer[k]);
16187     mp_print_ln(mp); /* now the transcript file contains the first line of input */
16188   }
16189   mp->selector=old_setting+2; /* |log_only| or |term_and_log| */
16190 }
16191
16192 @ @<Dealloc variables@>=
16193 xfree(mp->log_name);
16194
16195 @ Sometimes |open_log_file| is called at awkward moments when \MP\ is
16196 unable to print error messages or even to |show_context|.
16197 The |prompt_file_name| routine can result in a |fatal_error|, but the |error|
16198 routine will not be invoked because |log_opened| will be false.
16199
16200 The normal idea of |mp_batch_mode| is that nothing at all should be written
16201 on the terminal. However, in the unusual case that
16202 no log file could be opened, we make an exception and allow
16203 an explanatory message to be seen.
16204
16205 Incidentally, the program always refers to the log file as a `\.{transcript
16206 file}', because some systems cannot use the extension `\.{.log}' for
16207 this file.
16208
16209 @<Try to get a different log file name@>=
16210 {  
16211   mp->selector=term_only;
16212   mp_prompt_file_name(mp, "transcript file name",".log");
16213 }
16214
16215 @ @<Print the banner...@>=
16216
16217   wlog(banner);
16218   mp_print(mp, mp->mem_ident); mp_print(mp, "  ");
16219   mp_print_int(mp, mp_round_unscaled(mp, mp->internal[mp_day])); 
16220   mp_print_char(mp, ' ');
16221   m=mp_round_unscaled(mp, mp->internal[mp_month]);
16222   for (k=3*m-3;k<3*m;k++) { wlog_chr(months[k]); }
16223   mp_print_char(mp, ' '); 
16224   mp_print_int(mp, mp_round_unscaled(mp, mp->internal[mp_year])); 
16225   mp_print_char(mp, ' ');
16226   m=mp_round_unscaled(mp, mp->internal[mp_time]);
16227   mp_print_dd(mp, m / 60); mp_print_char(mp, ':'); mp_print_dd(mp, m % 60);
16228 }
16229
16230 @ The |try_extension| function tries to open an input file determined by
16231 |cur_name|, |cur_area|, and the argument |ext|.  It returns |false| if it
16232 can't find the file in |cur_area| or the appropriate system area.
16233
16234 @c boolean mp_try_extension (MP mp, const char *ext) { 
16235   mp_pack_file_name(mp, mp->cur_name,mp->cur_area, ext);
16236   in_name=xstrdup(mp->cur_name); 
16237   in_area=xstrdup(mp->cur_area);
16238   if ( mp_a_open_in(mp, &cur_file, mp_filetype_program) ) {
16239     return true;
16240   } else { 
16241     mp_pack_file_name(mp, mp->cur_name,NULL,ext);
16242     return mp_a_open_in(mp, &cur_file, mp_filetype_program);
16243   }
16244 }
16245
16246 @ Let's turn now to the procedure that is used to initiate file reading
16247 when an `\.{input}' command is being processed.
16248
16249 @c void mp_start_input (MP mp) { /* \MP\ will \.{input} something */
16250   char *fname = NULL;
16251   @<Put the desired file name in |(cur_name,cur_ext,cur_area)|@>;
16252   while (1) { 
16253     mp_begin_file_reading(mp); /* set up |cur_file| and new level of input */
16254     if ( strlen(mp->cur_ext)==0 ) {
16255       if ( mp_try_extension(mp, ".mp") ) break;
16256       else if ( mp_try_extension(mp, "") ) break;
16257       else if ( mp_try_extension(mp, ".mf") ) break;
16258       /* |else do_nothing; | */
16259     } else if ( mp_try_extension(mp, mp->cur_ext) ) {
16260       break;
16261     }
16262     mp_end_file_reading(mp); /* remove the level that didn't work */
16263     mp_prompt_file_name(mp, "input file name","");
16264   }
16265   name=mp_a_make_name_string(mp, cur_file);
16266   fname = xstrdup(mp->name_of_file);
16267   if ( mp->job_name==NULL ) {
16268     mp->job_name=xstrdup(mp->cur_name); 
16269     mp_open_log_file(mp);
16270   } /* |open_log_file| doesn't |show_context|, so |limit|
16271         and |loc| needn't be set to meaningful values yet */
16272   if ( ((int)mp->term_offset+(int)strlen(fname)) > (mp->max_print_line-2)) mp_print_ln(mp);
16273   else if ( (mp->term_offset>0)||(mp->file_offset>0) ) mp_print_char(mp, ' ');
16274   mp_print_char(mp, '('); incr(mp->open_parens); mp_print(mp, fname); 
16275   xfree(fname);
16276   update_terminal;
16277   @<Flush |name| and replace it with |cur_name| if it won't be needed@>;
16278   @<Read the first line of the new file@>;
16279 }
16280
16281 @ This code should be omitted if |a_make_name_string| returns something other
16282 than just a copy of its argument and the full file name is needed for opening
16283 \.{MPX} files or implementing the switch-to-editor option.
16284 @^system dependencies@>
16285
16286 @<Flush |name| and replace it with |cur_name| if it won't be needed@>=
16287 mp_flush_string(mp, name); name=rts(mp->cur_name); xfree(mp->cur_name)
16288
16289 @ If the file is empty, it is considered to contain a single blank line,
16290 so there is no need to test the return value.
16291
16292 @<Read the first line...@>=
16293
16294   line=1;
16295   (void)mp_input_ln(mp, cur_file ); 
16296   mp_firm_up_the_line(mp);
16297   mp->buffer[limit]='%'; mp->first=limit+1; loc=start;
16298 }
16299
16300 @ @<Put the desired file name in |(cur_name,cur_ext,cur_area)|@>=
16301 while ( token_state &&(loc==null) ) mp_end_token_list(mp);
16302 if ( token_state ) { 
16303   print_err("File names can't appear within macros");
16304 @.File names can't...@>
16305   help3("Sorry...I've converted what follows to tokens,")
16306     ("possibly garbaging the name you gave.")
16307     ("Please delete the tokens and insert the name again.");
16308   mp_error(mp);
16309 }
16310 if ( file_state ) {
16311   mp_scan_file_name(mp);
16312 } else { 
16313    xfree(mp->cur_name); mp->cur_name=xstrdup(""); 
16314    xfree(mp->cur_ext);  mp->cur_ext =xstrdup(""); 
16315    xfree(mp->cur_area); mp->cur_area=xstrdup(""); 
16316 }
16317
16318 @ The following simple routine starts reading the \.{MPX} file associated
16319 with the current input file.
16320
16321 @c void mp_start_mpx_input (MP mp) {
16322   char *origname = NULL; /* a copy of nameoffile */
16323   mp_pack_file_name(mp, in_name, in_area, ".mpx");
16324   @<Try to make sure |name_of_file| refers to a valid \.{MPX} file and
16325     |goto not_found| if there is a problem@>;
16326   mp_begin_file_reading(mp);
16327   if ( ! mp_a_open_in(mp, &cur_file, mp_filetype_program) ) {
16328     mp_end_file_reading(mp);
16329     goto NOT_FOUND;
16330   }
16331   name=mp_a_make_name_string(mp, cur_file);
16332   mp->mpx_name[index]=name; add_str_ref(name);
16333   @<Read the first line of the new file@>;
16334   return;
16335 NOT_FOUND: 
16336     @<Explain that the \.{MPX} file can't be read and |succumb|@>;
16337   xfree(origname);
16338 }
16339
16340 @ This should ideally be changed to do whatever is necessary to create the
16341 \.{MPX} file given by |name_of_file| if it does not exist or if it is out
16342 of date.  This requires invoking \.{MPtoTeX} on the |origname| and passing
16343 the results through \TeX\ and \.{DVItoMP}.  (It is possible to use a
16344 completely different typesetting program if suitable postprocessor is
16345 available to perform the function of \.{DVItoMP}.)
16346 @^system dependencies@>
16347
16348 @ @<Exported types@>=
16349 typedef int (*mp_run_make_mpx_command)(MP mp, char *origname, char *mtxname);
16350
16351 @ @<Option variables@>=
16352 mp_run_make_mpx_command run_make_mpx;
16353
16354 @ @<Allocate or initialize ...@>=
16355 set_callback_option(run_make_mpx);
16356
16357 @ @<Internal library declarations@>=
16358 int mp_run_make_mpx (MP mp, char *origname, char *mtxname);
16359
16360 @ The default does nothing.
16361 @c 
16362 int mp_run_make_mpx (MP mp, char *origname, char *mtxname) {
16363   (void)mp;
16364   (void)origname;
16365   (void)mtxname;
16366   return false;
16367 }
16368
16369 @ @<Try to make sure |name_of_file| refers to a valid \.{MPX} file and
16370   |goto not_found| if there is a problem@>=
16371 origname = mp_xstrdup(mp,mp->name_of_file);
16372 *(origname+strlen(origname)-1)=0; /* drop the x */
16373 if (!(mp->run_make_mpx)(mp, origname, mp->name_of_file))
16374   goto NOT_FOUND 
16375
16376 @ @<Explain that the \.{MPX} file can't be read and |succumb|@>=
16377 if ( mp->interaction==mp_error_stop_mode ) wake_up_terminal;
16378 mp_print_nl(mp, ">> ");
16379 mp_print(mp, origname);
16380 mp_print_nl(mp, ">> ");
16381 mp_print(mp, mp->name_of_file);
16382 mp_print_nl(mp, "! Unable to make mpx file");
16383 help4("The two files given above are one of your source files")
16384   ("and an auxiliary file I need to read to find out what your")
16385   ("btex..etex blocks mean. If you don't know why I had trouble,")
16386   ("try running it manually through MPtoTeX, TeX, and DVItoMP");
16387 succumb;
16388
16389 @ The last file-opening commands are for files accessed via the \&{readfrom}
16390 @:read_from_}{\&{readfrom} primitive@>
16391 operator and the \&{write} command.  Such files are stored in separate arrays.
16392 @:write_}{\&{write} primitive@>
16393
16394 @<Types in the outer block@>=
16395 typedef unsigned int readf_index; /* |0..max_read_files| */
16396 typedef unsigned int write_index;  /* |0..max_write_files| */
16397
16398 @ @<Glob...@>=
16399 readf_index max_read_files; /* maximum number of simultaneously open \&{readfrom} files */
16400 void ** rd_file; /* \&{readfrom} files */
16401 char ** rd_fname; /* corresponding file name or 0 if file not open */
16402 readf_index read_files; /* number of valid entries in the above arrays */
16403 write_index max_write_files; /* maximum number of simultaneously open \&{write} */
16404 void ** wr_file; /* \&{write} files */
16405 char ** wr_fname; /* corresponding file name or 0 if file not open */
16406 write_index write_files; /* number of valid entries in the above arrays */
16407
16408 @ @<Allocate or initialize ...@>=
16409 mp->max_read_files=8;
16410 mp->rd_file = xmalloc((mp->max_read_files+1),sizeof(void *));
16411 mp->rd_fname = xmalloc((mp->max_read_files+1),sizeof(char *));
16412 memset(mp->rd_fname, 0, sizeof(char *)*(mp->max_read_files+1));
16413 mp->read_files=0;
16414 mp->max_write_files=8;
16415 mp->wr_file = xmalloc((mp->max_write_files+1),sizeof(void *));
16416 mp->wr_fname = xmalloc((mp->max_write_files+1),sizeof(char *));
16417 memset(mp->wr_fname, 0, sizeof(char *)*(mp->max_write_files+1));
16418 mp->write_files=0;
16419
16420
16421 @ This routine starts reading the file named by string~|s| without setting
16422 |loc|, |limit|, or |name|.  It returns |false| if the file is empty or cannot
16423 be opened.  Otherwise it updates |rd_file[n]| and |rd_fname[n]|.
16424
16425 @c boolean mp_start_read_input (MP mp,char *s, readf_index  n) {
16426   mp_ptr_scan_file(mp, s);
16427   pack_cur_name;
16428   mp_begin_file_reading(mp);
16429   if ( ! mp_a_open_in(mp, &mp->rd_file[n], (mp_filetype_text+n)) ) 
16430         goto NOT_FOUND;
16431   if ( ! mp_input_ln(mp, mp->rd_file[n] ) ) {
16432     (mp->close_file)(mp,mp->rd_file[n]); 
16433         goto NOT_FOUND; 
16434   }
16435   mp->rd_fname[n]=xstrdup(mp->name_of_file);
16436   return true;
16437 NOT_FOUND: 
16438   mp_end_file_reading(mp);
16439   return false;
16440 }
16441
16442 @ Open |wr_file[n]| using file name~|s| and update |wr_fname[n]|.
16443
16444 @<Declarations@>=
16445 void mp_open_write_file (MP mp, char *s, readf_index  n) ;
16446
16447 @ @c void mp_open_write_file (MP mp,char *s, readf_index  n) {
16448   mp_ptr_scan_file(mp, s);
16449   pack_cur_name;
16450   while ( ! mp_a_open_out(mp, &mp->wr_file[n], (mp_filetype_text+n)) )
16451     mp_prompt_file_name(mp, "file name for write output","");
16452   mp->wr_fname[n]=xstrdup(mp->name_of_file);
16453 }
16454
16455
16456 @* \[36] Introduction to the parsing routines.
16457 We come now to the central nervous system that sparks many of \MP's activities.
16458 By evaluating expressions, from their primary constituents to ever larger
16459 subexpressions, \MP\ builds the structures that ultimately define complete
16460 pictures or fonts of type.
16461
16462 Four mutually recursive subroutines are involved in this process: We call them
16463 $$\hbox{|scan_primary|, |scan_secondary|, |scan_tertiary|,
16464 and |scan_expression|.}$$
16465 @^recursion@>
16466 Each of them is parameterless and begins with the first token to be scanned
16467 already represented in |cur_cmd|, |cur_mod|, and |cur_sym|. After execution,
16468 the value of the primary or secondary or tertiary or expression that was
16469 found will appear in the global variables |cur_type| and |cur_exp|. The
16470 token following the expression will be represented in |cur_cmd|, |cur_mod|,
16471 and |cur_sym|.
16472
16473 Technically speaking, the parsing algorithms are ``LL(1),'' more or less;
16474 backup mechanisms have been added in order to provide reasonable error
16475 recovery.
16476
16477 @<Glob...@>=
16478 small_number cur_type; /* the type of the expression just found */
16479 integer cur_exp; /* the value of the expression just found */
16480
16481 @ @<Set init...@>=
16482 mp->cur_exp=0;
16483
16484 @ Many different kinds of expressions are possible, so it is wise to have
16485 precise descriptions of what |cur_type| and |cur_exp| mean in all cases:
16486
16487 \smallskip\hang
16488 |cur_type=mp_vacuous| means that this expression didn't turn out to have a
16489 value at all, because it arose from a \&{begingroup}$\,\ldots\,$\&{endgroup}
16490 construction in which there was no expression before the \&{endgroup}.
16491 In this case |cur_exp| has some irrelevant value.
16492
16493 \smallskip\hang
16494 |cur_type=mp_boolean_type| means that |cur_exp| is either |true_code|
16495 or |false_code|.
16496
16497 \smallskip\hang
16498 |cur_type=mp_unknown_boolean| means that |cur_exp| points to a capsule
16499 node that is in 
16500 a ring of equivalent booleans whose value has not yet been defined.
16501
16502 \smallskip\hang
16503 |cur_type=mp_string_type| means that |cur_exp| is a string number (i.e., an
16504 integer in the range |0<=cur_exp<str_ptr|). That string's reference count
16505 includes this particular reference.
16506
16507 \smallskip\hang
16508 |cur_type=mp_unknown_string| means that |cur_exp| points to a capsule
16509 node that is in
16510 a ring of equivalent strings whose value has not yet been defined.
16511
16512 \smallskip\hang
16513 |cur_type=mp_pen_type| means that |cur_exp| points to a node in a pen.  Nobody
16514 else points to any of the nodes in this pen.  The pen may be polygonal or
16515 elliptical.
16516
16517 \smallskip\hang
16518 |cur_type=mp_unknown_pen| means that |cur_exp| points to a capsule
16519 node that is in
16520 a ring of equivalent pens whose value has not yet been defined.
16521
16522 \smallskip\hang
16523 |cur_type=mp_path_type| means that |cur_exp| points to a the first node of
16524 a path; nobody else points to this particular path. The control points of
16525 the path will have been chosen.
16526
16527 \smallskip\hang
16528 |cur_type=mp_unknown_path| means that |cur_exp| points to a capsule
16529 node that is in
16530 a ring of equivalent paths whose value has not yet been defined.
16531
16532 \smallskip\hang
16533 |cur_type=mp_picture_type| means that |cur_exp| points to an edge header node.
16534 There may be other pointers to this particular set of edges.  The header node
16535 contains a reference count that includes this particular reference.
16536
16537 \smallskip\hang
16538 |cur_type=mp_unknown_picture| means that |cur_exp| points to a capsule
16539 node that is in
16540 a ring of equivalent pictures whose value has not yet been defined.
16541
16542 \smallskip\hang
16543 |cur_type=mp_transform_type| means that |cur_exp| points to a |mp_transform_type|
16544 capsule node. The |value| part of this capsule
16545 points to a transform node that contains six numeric values,
16546 each of which is |independent|, |dependent|, |mp_proto_dependent|, or |known|.
16547
16548 \smallskip\hang
16549 |cur_type=mp_color_type| means that |cur_exp| points to a |color_type|
16550 capsule node. The |value| part of this capsule
16551 points to a color node that contains three numeric values,
16552 each of which is |independent|, |dependent|, |mp_proto_dependent|, or |known|.
16553
16554 \smallskip\hang
16555 |cur_type=mp_cmykcolor_type| means that |cur_exp| points to a |mp_cmykcolor_type|
16556 capsule node. The |value| part of this capsule
16557 points to a color node that contains four numeric values,
16558 each of which is |independent|, |dependent|, |mp_proto_dependent|, or |known|.
16559
16560 \smallskip\hang
16561 |cur_type=mp_pair_type| means that |cur_exp| points to a capsule
16562 node whose type is |mp_pair_type|. The |value| part of this capsule
16563 points to a pair node that contains two numeric values,
16564 each of which is |independent|, |dependent|, |mp_proto_dependent|, or |known|.
16565
16566 \smallskip\hang
16567 |cur_type=mp_known| means that |cur_exp| is a |scaled| value.
16568
16569 \smallskip\hang
16570 |cur_type=mp_dependent| means that |cur_exp| points to a capsule node whose type
16571 is |dependent|. The |dep_list| field in this capsule points to the associated
16572 dependency list.
16573
16574 \smallskip\hang
16575 |cur_type=mp_proto_dependent| means that |cur_exp| points to a |mp_proto_dependent|
16576 capsule node. The |dep_list| field in this capsule
16577 points to the associated dependency list.
16578
16579 \smallskip\hang
16580 |cur_type=independent| means that |cur_exp| points to a capsule node
16581 whose type is |independent|. This somewhat unusual case can arise, for
16582 example, in the expression
16583 `$x+\&{begingroup}\penalty0\,\&{string}\,x; 0\,\&{endgroup}$'.
16584
16585 \smallskip\hang
16586 |cur_type=mp_token_list| means that |cur_exp| points to a linked list of
16587 tokens. 
16588
16589 \smallskip\noindent
16590 The possible settings of |cur_type| have been listed here in increasing
16591 numerical order. Notice that |cur_type| will never be |mp_numeric_type| or
16592 |suffixed_macro| or |mp_unsuffixed_macro|, although variables of those types
16593 are allowed.  Conversely, \MP\ has no variables of type |mp_vacuous| or
16594 |token_list|.
16595
16596 @ Capsules are two-word nodes that have a similar meaning
16597 to |cur_type| and |cur_exp|. Such nodes have |name_type=capsule|,
16598 and their |type| field is one of the possibilities for |cur_type| listed above.
16599 Also |link<=void| in capsules that aren't part of a token list.
16600
16601 The |value| field of a capsule is, in most cases, the value that
16602 corresponds to its |type|, as |cur_exp| corresponds to |cur_type|.
16603 However, when |cur_exp| would point to a capsule,
16604 no extra layer of indirection is present; the |value|
16605 field is what would have been called |value(cur_exp)| if it had not been
16606 encapsulated.  Furthermore, if the type is |dependent| or
16607 |mp_proto_dependent|, the |value| field of a capsule is replaced by
16608 |dep_list| and |prev_dep| fields, since dependency lists in capsules are
16609 always part of the general |dep_list| structure.
16610
16611 The |get_x_next| routine is careful not to change the values of |cur_type|
16612 and |cur_exp| when it gets an expanded token. However, |get_x_next| might
16613 call a macro, which might parse an expression, which might execute lots of
16614 commands in a group; hence it's possible that |cur_type| might change
16615 from, say, |mp_unknown_boolean| to |mp_boolean_type|, or from |dependent| to
16616 |known| or |independent|, during the time |get_x_next| is called. The
16617 programs below are careful to stash sensitive intermediate results in
16618 capsules, so that \MP's generality doesn't cause trouble.
16619
16620 Here's a procedure that illustrates these conventions. It takes
16621 the contents of $(|cur_type|\kern-.3pt,|cur_exp|\kern-.3pt)$
16622 and stashes them away in a
16623 capsule. It is not used when |cur_type=mp_token_list|.
16624 After the operation, |cur_type=mp_vacuous|; hence there is no need to
16625 copy path lists or to update reference counts, etc.
16626
16627 The special link |mp_void| is put on the capsule returned by
16628 |stash_cur_exp|, because this procedure is used to store macro parameters
16629 that must be easily distinguishable from token lists.
16630
16631 @<Declare the stashing/unstashing routines@>=
16632 pointer mp_stash_cur_exp (MP mp) {
16633   pointer p; /* the capsule that will be returned */
16634   switch (mp->cur_type) {
16635   case unknown_types:
16636   case mp_transform_type:
16637   case mp_color_type:
16638   case mp_pair_type:
16639   case mp_dependent:
16640   case mp_proto_dependent:
16641   case mp_independent: 
16642   case mp_cmykcolor_type:
16643     p=mp->cur_exp;
16644     break;
16645   default: 
16646     p=mp_get_node(mp, value_node_size); name_type(p)=mp_capsule;
16647     type(p)=mp->cur_type; value(p)=mp->cur_exp;
16648     break;
16649   }
16650   mp->cur_type=mp_vacuous; link(p)=mp_void; 
16651   return p;
16652 }
16653
16654 @ The inverse of |stash_cur_exp| is the following procedure, which
16655 deletes an unnecessary capsule and puts its contents into |cur_type|
16656 and |cur_exp|.
16657
16658 The program steps of \MP\ can be divided into two categories: those in
16659 which |cur_type| and |cur_exp| are ``alive'' and those in which they are
16660 ``dead,'' in the sense that |cur_type| and |cur_exp| contain relevant
16661 information or not. It's important not to ignore them when they're alive,
16662 and it's important not to pay attention to them when they're dead.
16663
16664 There's also an intermediate category: If |cur_type=mp_vacuous|, then
16665 |cur_exp| is irrelevant, hence we can proceed without caring if |cur_type|
16666 and |cur_exp| are alive or dead. In such cases we say that |cur_type|
16667 and |cur_exp| are {\sl dormant}. It is permissible to call |get_x_next|
16668 only when they are alive or dormant.
16669
16670 The \\{stash} procedure above assumes that |cur_type| and |cur_exp|
16671 are alive or dormant. The \\{unstash} procedure assumes that they are
16672 dead or dormant; it resuscitates them.
16673
16674 @<Declare the stashing/unstashing...@>=
16675 void mp_unstash_cur_exp (MP mp,pointer p) ;
16676
16677 @ @c
16678 void mp_unstash_cur_exp (MP mp,pointer p) { 
16679   mp->cur_type=type(p);
16680   switch (mp->cur_type) {
16681   case unknown_types:
16682   case mp_transform_type:
16683   case mp_color_type:
16684   case mp_pair_type:
16685   case mp_dependent: 
16686   case mp_proto_dependent:
16687   case mp_independent:
16688   case mp_cmykcolor_type: 
16689     mp->cur_exp=p;
16690     break;
16691   default:
16692     mp->cur_exp=value(p);
16693     mp_free_node(mp, p,value_node_size);
16694     break;
16695   }
16696 }
16697
16698 @ The following procedure prints the values of expressions in an
16699 abbreviated format. If its first parameter |p| is null, the value of
16700 |(cur_type,cur_exp)| is displayed; otherwise |p| should be a capsule
16701 containing the desired value. The second parameter controls the amount of
16702 output. If it is~0, dependency lists will be abbreviated to
16703 `\.{linearform}' unless they consist of a single term.  If it is greater
16704 than~1, complicated structures (pens, pictures, and paths) will be displayed
16705 in full.
16706 @.linearform@>
16707
16708 @<Declare subroutines for printing expressions@>=
16709 @<Declare the procedure called |print_dp|@>
16710 @<Declare the stashing/unstashing routines@>
16711 void mp_print_exp (MP mp,pointer p, small_number verbosity) {
16712   boolean restore_cur_exp; /* should |cur_exp| be restored? */
16713   small_number t; /* the type of the expression */
16714   pointer q; /* a big node being displayed */
16715   integer v=0; /* the value of the expression */
16716   if ( p!=null ) {
16717     restore_cur_exp=false;
16718   } else { 
16719     p=mp_stash_cur_exp(mp); restore_cur_exp=true;
16720   }
16721   t=type(p);
16722   if ( t<mp_dependent ) v=value(p); else if ( t<mp_independent ) v=dep_list(p);
16723   @<Print an abbreviated value of |v| with format depending on |t|@>;
16724   if ( restore_cur_exp ) mp_unstash_cur_exp(mp, p);
16725 }
16726
16727 @ @<Print an abbreviated value of |v| with format depending on |t|@>=
16728 switch (t) {
16729 case mp_vacuous:mp_print(mp, "mp_vacuous"); break;
16730 case mp_boolean_type:
16731   if ( v==true_code ) mp_print(mp, "true"); else mp_print(mp, "false");
16732   break;
16733 case unknown_types: case mp_numeric_type:
16734   @<Display a variable that's been declared but not defined@>;
16735   break;
16736 case mp_string_type:
16737   mp_print_char(mp, '"'); mp_print_str(mp, v); mp_print_char(mp, '"');
16738   break;
16739 case mp_pen_type: case mp_path_type: case mp_picture_type:
16740   @<Display a complex type@>;
16741   break;
16742 case mp_transform_type: case mp_color_type: case mp_pair_type: case mp_cmykcolor_type:
16743   if ( v==null ) mp_print_type(mp, t);
16744   else @<Display a big node@>;
16745   break;
16746 case mp_known:mp_print_scaled(mp, v); break;
16747 case mp_dependent: case mp_proto_dependent:
16748   mp_print_dp(mp, t,v,verbosity);
16749   break;
16750 case mp_independent:mp_print_variable_name(mp, p); break;
16751 default: mp_confusion(mp, "exp"); break;
16752 @:this can't happen exp}{\quad exp@>
16753 }
16754
16755 @ @<Display a big node@>=
16756
16757   mp_print_char(mp, '('); q=v+mp->big_node_size[t];
16758   do {  
16759     if ( type(v)==mp_known ) mp_print_scaled(mp, value(v));
16760     else if ( type(v)==mp_independent ) mp_print_variable_name(mp, v);
16761     else mp_print_dp(mp, type(v),dep_list(v),verbosity);
16762     v=v+2;
16763     if ( v!=q ) mp_print_char(mp, ',');
16764   } while (v!=q);
16765   mp_print_char(mp, ')');
16766 }
16767
16768 @ Values of type \&{picture}, \&{path}, and \&{pen} are displayed verbosely
16769 in the log file only, unless the user has given a positive value to
16770 \\{tracingonline}.
16771
16772 @<Display a complex type@>=
16773 if ( verbosity<=1 ) {
16774   mp_print_type(mp, t);
16775 } else { 
16776   if ( mp->selector==term_and_log )
16777    if ( mp->internal[mp_tracing_online]<=0 ) {
16778     mp->selector=term_only;
16779     mp_print_type(mp, t); mp_print(mp, " (see the transcript file)");
16780     mp->selector=term_and_log;
16781   };
16782   switch (t) {
16783   case mp_pen_type:mp_print_pen(mp, v,"",false); break;
16784   case mp_path_type:mp_print_path(mp, v,"",false); break;
16785   case mp_picture_type:mp_print_edges(mp, v,"",false); break;
16786   } /* there are no other cases */
16787 }
16788
16789 @ @<Declare the procedure called |print_dp|@>=
16790 void mp_print_dp (MP mp,small_number t, pointer p, 
16791                   small_number verbosity)  {
16792   pointer q; /* the node following |p| */
16793   q=link(p);
16794   if ( (info(q)==null) || (verbosity>0) ) mp_print_dependency(mp, p,t);
16795   else mp_print(mp, "linearform");
16796 }
16797
16798 @ The displayed name of a variable in a ring will not be a capsule unless
16799 the ring consists entirely of capsules.
16800
16801 @<Display a variable that's been declared but not defined@>=
16802 { mp_print_type(mp, t);
16803 if ( v!=null )
16804   { mp_print_char(mp, ' ');
16805   while ( (name_type(v)==mp_capsule) && (v!=p) ) v=value(v);
16806   mp_print_variable_name(mp, v);
16807   };
16808 }
16809
16810 @ When errors are detected during parsing, it is often helpful to
16811 display an expression just above the error message, using |exp_err|
16812 or |disp_err| instead of |print_err|.
16813
16814 @d exp_err(A) mp_disp_err(mp, null,(A)) /* displays the current expression */
16815
16816 @<Declare subroutines for printing expressions@>=
16817 void mp_disp_err (MP mp,pointer p, const char *s) { 
16818   if ( mp->interaction==mp_error_stop_mode ) wake_up_terminal;
16819   mp_print_nl(mp, ">> ");
16820 @.>>@>
16821   mp_print_exp(mp, p,1); /* ``medium verbose'' printing of the expression */
16822   if (strlen(s)) { 
16823     mp_print_nl(mp, "! "); mp_print(mp, s);
16824 @.!\relax@>
16825   }
16826 }
16827
16828 @ If |cur_type| and |cur_exp| contain relevant information that should
16829 be recycled, we will use the following procedure, which changes |cur_type|
16830 to |known| and stores a given value in |cur_exp|. We can think of |cur_type|
16831 and |cur_exp| as either alive or dormant after this has been done,
16832 because |cur_exp| will not contain a pointer value.
16833
16834 @ @c void mp_flush_cur_exp (MP mp,scaled v) { 
16835   switch (mp->cur_type) {
16836   case unknown_types: case mp_transform_type: case mp_color_type: case mp_pair_type:
16837   case mp_dependent: case mp_proto_dependent: case mp_independent: case mp_cmykcolor_type:
16838     mp_recycle_value(mp, mp->cur_exp); 
16839     mp_free_node(mp, mp->cur_exp,value_node_size);
16840     break;
16841   case mp_string_type:
16842     delete_str_ref(mp->cur_exp); break;
16843   case mp_pen_type: case mp_path_type: 
16844     mp_toss_knot_list(mp, mp->cur_exp); break;
16845   case mp_picture_type:
16846     delete_edge_ref(mp->cur_exp); break;
16847   default: 
16848     break;
16849   }
16850   mp->cur_type=mp_known; mp->cur_exp=v;
16851 }
16852
16853 @ There's a much more general procedure that is capable of releasing
16854 the storage associated with any two-word value packet.
16855
16856 @<Declare the recycling subroutines@>=
16857 void mp_recycle_value (MP mp,pointer p) ;
16858
16859 @ @c void mp_recycle_value (MP mp,pointer p) {
16860   small_number t; /* a type code */
16861   integer vv; /* another value */
16862   pointer q,r,s,pp; /* link manipulation registers */
16863   integer v=0; /* a value */
16864   t=type(p);
16865   if ( t<mp_dependent ) v=value(p);
16866   switch (t) {
16867   case undefined: case mp_vacuous: case mp_boolean_type: case mp_known:
16868   case mp_numeric_type:
16869     break;
16870   case unknown_types:
16871     mp_ring_delete(mp, p); break;
16872   case mp_string_type:
16873     delete_str_ref(v); break;
16874   case mp_path_type: case mp_pen_type:
16875     mp_toss_knot_list(mp, v); break;
16876   case mp_picture_type:
16877     delete_edge_ref(v); break;
16878   case mp_cmykcolor_type: case mp_pair_type: case mp_color_type:
16879   case mp_transform_type:
16880     @<Recycle a big node@>; break; 
16881   case mp_dependent: case mp_proto_dependent:
16882     @<Recycle a dependency list@>; break;
16883   case mp_independent:
16884     @<Recycle an independent variable@>; break;
16885   case mp_token_list: case mp_structured:
16886     mp_confusion(mp, "recycle"); break;
16887 @:this can't happen recycle}{\quad recycle@>
16888   case mp_unsuffixed_macro: case mp_suffixed_macro:
16889     mp_delete_mac_ref(mp, value(p)); break;
16890   } /* there are no other cases */
16891   type(p)=undefined;
16892 }
16893
16894 @ @<Recycle a big node@>=
16895 if ( v!=null ){ 
16896   q=v+mp->big_node_size[t];
16897   do {  
16898     q=q-2; mp_recycle_value(mp, q);
16899   } while (q!=v);
16900   mp_free_node(mp, v,mp->big_node_size[t]);
16901 }
16902
16903 @ @<Recycle a dependency list@>=
16904
16905   q=dep_list(p);
16906   while ( info(q)!=null ) q=link(q);
16907   link(prev_dep(p))=link(q);
16908   prev_dep(link(q))=prev_dep(p);
16909   link(q)=null; mp_flush_node_list(mp, dep_list(p));
16910 }
16911
16912 @ When an independent variable disappears, it simply fades away, unless
16913 something depends on it. In the latter case, a dependent variable whose
16914 coefficient of dependence is maximal will take its place.
16915 The relevant algorithm is due to Ignacio~A. Zabala, who implemented it
16916 as part of his Ph.D. thesis (Stanford University, December 1982).
16917 @^Zabala Salelles, Ignacio Andr\'es@>
16918
16919 For example, suppose that variable $x$ is being recycled, and that the
16920 only variables depending on~$x$ are $y=2x+a$ and $z=x+b$. In this case
16921 we want to make $y$ independent and $z=.5y-.5a+b$; no other variables
16922 will depend on~$y$. If $\\{tracingequations}>0$ in this situation,
16923 we will print `\.{\#\#\# -2x=-y+a}'.
16924
16925 There's a slight complication, however: An independent variable $x$
16926 can occur both in dependency lists and in proto-dependency lists.
16927 This makes it necessary to be careful when deciding which coefficient
16928 is maximal.
16929
16930 Furthermore, this complication is not so slight when
16931 a proto-dependent variable is chosen to become independent. For example,
16932 suppose that $y=2x+100a$ is proto-dependent while $z=x+b$ is dependent;
16933 then we must change $z=.5y-50a+b$ to a proto-dependency, because of the
16934 large coefficient `50'.
16935
16936 In order to deal with these complications without wasting too much time,
16937 we shall link together the occurrences of~$x$ among all the linear
16938 dependencies, maintaining separate lists for the dependent and
16939 proto-dependent cases.
16940
16941 @<Recycle an independent variable@>=
16942
16943   mp->max_c[mp_dependent]=0; mp->max_c[mp_proto_dependent]=0;
16944   mp->max_link[mp_dependent]=null; mp->max_link[mp_proto_dependent]=null;
16945   q=link(dep_head);
16946   while ( q!=dep_head ) { 
16947     s=value_loc(q); /* now |link(s)=dep_list(q)| */
16948     while (1) { 
16949       r=link(s);
16950       if ( info(r)==null ) break;
16951       if ( info(r)!=p ) { 
16952         s=r;
16953       } else  { 
16954         t=type(q); link(s)=link(r); info(r)=q;
16955         if ( abs(value(r))>mp->max_c[t] ) {
16956           @<Record a new maximum coefficient of type |t|@>;
16957         } else { 
16958           link(r)=mp->max_link[t]; mp->max_link[t]=r;
16959         }
16960       }
16961     } 
16962     q=link(r);
16963   }
16964   if ( (mp->max_c[mp_dependent]>0)||(mp->max_c[mp_proto_dependent]>0) ) {
16965     @<Choose a dependent variable to take the place of the disappearing
16966     independent variable, and change all remaining dependencies
16967     accordingly@>;
16968   }
16969 }
16970
16971 @ The code for independency removal makes use of three two-word arrays.
16972
16973 @<Glob...@>=
16974 integer max_c[mp_proto_dependent+1];  /* max coefficient magnitude */
16975 pointer max_ptr[mp_proto_dependent+1]; /* where |p| occurs with |max_c| */
16976 pointer max_link[mp_proto_dependent+1]; /* other occurrences of |p| */
16977
16978 @ @<Record a new maximum coefficient...@>=
16979
16980   if ( mp->max_c[t]>0 ) {
16981     link(mp->max_ptr[t])=mp->max_link[t]; mp->max_link[t]=mp->max_ptr[t];
16982   }
16983   mp->max_c[t]=abs(value(r)); mp->max_ptr[t]=r;
16984 }
16985
16986 @ @<Choose a dependent...@>=
16987
16988   if ( (mp->max_c[mp_dependent] / 010000) >= mp->max_c[mp_proto_dependent] )
16989     t=mp_dependent;
16990   else 
16991     t=mp_proto_dependent;
16992   @<Determine the dependency list |s| to substitute for the independent
16993     variable~|p|@>;
16994   t=mp_dependent+mp_proto_dependent-t; /* complement |t| */
16995   if ( mp->max_c[t]>0 ) { /* we need to pick up an unchosen dependency */ 
16996     link(mp->max_ptr[t])=mp->max_link[t]; mp->max_link[t]=mp->max_ptr[t];
16997   }
16998   if ( t!=mp_dependent ) { @<Substitute new dependencies in place of |p|@>; }
16999   else { @<Substitute new proto-dependencies in place of |p|@>;}
17000   mp_flush_node_list(mp, s);
17001   if ( mp->fix_needed ) mp_fix_dependencies(mp);
17002   check_arith;
17003 }
17004
17005 @ Let |s=max_ptr[t]|. At this point we have $|value|(s)=\pm|max_c|[t]$,
17006 and |info(s)| points to the dependent variable~|pp| of type~|t| from
17007 whose dependency list we have removed node~|s|. We must reinsert
17008 node~|s| into the dependency list, with coefficient $-1.0$, and with
17009 |pp| as the new independent variable. Since |pp| will have a larger serial
17010 number than any other variable, we can put node |s| at the head of the
17011 list.
17012
17013 @<Determine the dep...@>=
17014 s=mp->max_ptr[t]; pp=info(s); v=value(s);
17015 if ( t==mp_dependent ) value(s)=-fraction_one; else value(s)=-unity;
17016 r=dep_list(pp); link(s)=r;
17017 while ( info(r)!=null ) r=link(r);
17018 q=link(r); link(r)=null;
17019 prev_dep(q)=prev_dep(pp); link(prev_dep(pp))=q;
17020 new_indep(pp);
17021 if ( mp->cur_exp==pp ) if ( mp->cur_type==t ) mp->cur_type=mp_independent;
17022 if ( mp->internal[mp_tracing_equations]>0 ) { 
17023   @<Show the transformed dependency@>; 
17024 }
17025
17026 @ Now $(-v)$ times the formerly independent variable~|p| is being replaced
17027 by the dependency list~|s|.
17028
17029 @<Show the transformed...@>=
17030 if ( mp_interesting(mp, p) ) {
17031   mp_begin_diagnostic(mp); mp_print_nl(mp, "### ");
17032 @:]]]\#\#\#_}{\.{\#\#\#}@>
17033   if ( v>0 ) mp_print_char(mp, '-');
17034   if ( t==mp_dependent ) vv=mp_round_fraction(mp, mp->max_c[mp_dependent]);
17035   else vv=mp->max_c[mp_proto_dependent];
17036   if ( vv!=unity ) mp_print_scaled(mp, vv);
17037   mp_print_variable_name(mp, p);
17038   while ( value(p) % s_scale>0 ) {
17039     mp_print(mp, "*4"); value(p)=value(p)-2;
17040   }
17041   if ( t==mp_dependent ) mp_print_char(mp, '='); else mp_print(mp, " = ");
17042   mp_print_dependency(mp, s,t);
17043   mp_end_diagnostic(mp, false);
17044 }
17045
17046 @ Finally, there are dependent and proto-dependent variables whose
17047 dependency lists must be brought up to date.
17048
17049 @<Substitute new dependencies...@>=
17050 for (t=mp_dependent;t<=mp_proto_dependent;t++){ 
17051   r=mp->max_link[t];
17052   while ( r!=null ) {
17053     q=info(r);
17054     dep_list(q)=mp_p_plus_fq(mp, dep_list(q),
17055      mp_make_fraction(mp, value(r),-v),s,t,mp_dependent);
17056     if ( dep_list(q)==mp->dep_final ) mp_make_known(mp, q,mp->dep_final);
17057     q=r; r=link(r); mp_free_node(mp, q,dep_node_size);
17058   }
17059 }
17060
17061 @ @<Substitute new proto...@>=
17062 for (t=mp_dependent;t<=mp_proto_dependent;t++) {
17063   r=mp->max_link[t];
17064   while ( r!=null ) {
17065     q=info(r);
17066     if ( t==mp_dependent ) { /* for safety's sake, we change |q| to |mp_proto_dependent| */
17067       if ( mp->cur_exp==q ) if ( mp->cur_type==mp_dependent )
17068         mp->cur_type=mp_proto_dependent;
17069       dep_list(q)=mp_p_over_v(mp, dep_list(q),unity,
17070          mp_dependent,mp_proto_dependent);
17071       type(q)=mp_proto_dependent; 
17072       value(r)=mp_round_fraction(mp, value(r));
17073     }
17074     dep_list(q)=mp_p_plus_fq(mp, dep_list(q),
17075        mp_make_scaled(mp, value(r),-v),s,
17076        mp_proto_dependent,mp_proto_dependent);
17077     if ( dep_list(q)==mp->dep_final ) 
17078        mp_make_known(mp, q,mp->dep_final);
17079     q=r; r=link(r); mp_free_node(mp, q,dep_node_size);
17080   }
17081 }
17082
17083 @ Here are some routines that provide handy combinations of actions
17084 that are often needed during error recovery. For example,
17085 `|flush_error|' flushes the current expression, replaces it by
17086 a given value, and calls |error|.
17087
17088 Errors often are detected after an extra token has already been scanned.
17089 The `\\{put\_get}' routines put that token back before calling |error|;
17090 then they get it back again. (Or perhaps they get another token, if
17091 the user has changed things.)
17092
17093 @<Declarations@>=
17094 void mp_flush_error (MP mp,scaled v);
17095 void mp_put_get_error (MP mp);
17096 void mp_put_get_flush_error (MP mp,scaled v) ;
17097
17098 @ @c
17099 void mp_flush_error (MP mp,scaled v) { 
17100   mp_error(mp); mp_flush_cur_exp(mp, v); 
17101 }
17102 void mp_put_get_error (MP mp) { 
17103   mp_back_error(mp); mp_get_x_next(mp); 
17104 }
17105 void mp_put_get_flush_error (MP mp,scaled v) { 
17106   mp_put_get_error(mp);
17107   mp_flush_cur_exp(mp, v); 
17108 }
17109
17110 @ A global variable |var_flag| is set to a special command code
17111 just before \MP\ calls |scan_expression|, if the expression should be
17112 treated as a variable when this command code immediately follows. For
17113 example, |var_flag| is set to |assignment| at the beginning of a
17114 statement, because we want to know the {\sl location\/} of a variable at
17115 the left of `\.{:=}', not the {\sl value\/} of that variable.
17116
17117 The |scan_expression| subroutine calls |scan_tertiary|,
17118 which calls |scan_secondary|, which calls |scan_primary|, which sets
17119 |var_flag:=0|. In this way each of the scanning routines ``knows''
17120 when it has been called with a special |var_flag|, but |var_flag| is
17121 usually zero.
17122
17123 A variable preceding a command that equals |var_flag| is converted to a
17124 token list rather than a value. Furthermore, an `\.{=}' sign following an
17125 expression with |var_flag=assignment| is not considered to be a relation
17126 that produces boolean expressions.
17127
17128
17129 @<Glob...@>=
17130 int var_flag; /* command that wants a variable */
17131
17132 @ @<Set init...@>=
17133 mp->var_flag=0;
17134
17135 @* \[37] Parsing primary expressions.
17136 The first parsing routine, |scan_primary|, is also the most complicated one,
17137 since it involves so many different cases. But each case---with one
17138 exception---is fairly simple by itself.
17139
17140 When |scan_primary| begins, the first token of the primary to be scanned
17141 should already appear in |cur_cmd|, |cur_mod|, and |cur_sym|. The values
17142 of |cur_type| and |cur_exp| should be either dead or dormant, as explained
17143 earlier. If |cur_cmd| is not between |min_primary_command| and
17144 |max_primary_command|, inclusive, a syntax error will be signaled.
17145
17146 @<Declare the basic parsing subroutines@>=
17147 void mp_scan_primary (MP mp) {
17148   pointer p,q,r; /* for list manipulation */
17149   quarterword c; /* a primitive operation code */
17150   int my_var_flag; /* initial value of |my_var_flag| */
17151   pointer l_delim,r_delim; /* hash addresses of a delimiter pair */
17152   @<Other local variables for |scan_primary|@>;
17153   my_var_flag=mp->var_flag; mp->var_flag=0;
17154 RESTART:
17155   check_arith;
17156   @<Supply diagnostic information, if requested@>;
17157   switch (mp->cur_cmd) {
17158   case left_delimiter:
17159     @<Scan a delimited primary@>; break;
17160   case begin_group:
17161     @<Scan a grouped primary@>; break;
17162   case string_token:
17163     @<Scan a string constant@>; break;
17164   case numeric_token:
17165     @<Scan a primary that starts with a numeric token@>; break;
17166   case nullary:
17167     @<Scan a nullary operation@>; break;
17168   case unary: case type_name: case cycle: case plus_or_minus:
17169     @<Scan a unary operation@>; break;
17170   case primary_binary:
17171     @<Scan a binary operation with `\&{of}' between its operands@>; break;
17172   case str_op:
17173     @<Convert a suffix to a string@>; break;
17174   case internal_quantity:
17175     @<Scan an internal numeric quantity@>; break;
17176   case capsule_token:
17177     mp_make_exp_copy(mp, mp->cur_mod); break;
17178   case tag_token:
17179     @<Scan a variable primary; |goto restart| if it turns out to be a macro@>; break;
17180   default: 
17181     mp_bad_exp(mp, "A primary"); goto RESTART; break;
17182 @.A primary expression...@>
17183   }
17184   mp_get_x_next(mp); /* the routines |goto done| if they don't want this */
17185 DONE: 
17186   if ( mp->cur_cmd==left_bracket ) {
17187     if ( mp->cur_type>=mp_known ) {
17188       @<Scan a mediation construction@>;
17189     }
17190   }
17191 }
17192
17193
17194
17195 @ Errors at the beginning of expressions are flagged by |bad_exp|.
17196
17197 @c void mp_bad_exp (MP mp, const char * s) {
17198   int save_flag;
17199   print_err(s); mp_print(mp, " expression can't begin with `");
17200   mp_print_cmd_mod(mp, mp->cur_cmd,mp->cur_mod); 
17201   mp_print_char(mp, '\'');
17202   help4("I'm afraid I need some sort of value in order to continue,")
17203     ("so I've tentatively inserted `0'. You may want to")
17204     ("delete this zero and insert something else;")
17205     ("see Chapter 27 of The METAFONTbook for an example.");
17206 @:METAFONTbook}{\sl The {\logos METAFONT\/}book@>
17207   mp_back_input(mp); mp->cur_sym=0; mp->cur_cmd=numeric_token; 
17208   mp->cur_mod=0; mp_ins_error(mp);
17209   save_flag=mp->var_flag; mp->var_flag=0; mp_get_x_next(mp);
17210   mp->var_flag=save_flag;
17211 }
17212
17213 @ @<Supply diagnostic information, if requested@>=
17214 #ifdef DEBUG
17215 if ( mp->panicking ) mp_check_mem(mp, false);
17216 #endif
17217 if ( mp->interrupt!=0 ) if ( mp->OK_to_interrupt ) {
17218   mp_back_input(mp); check_interrupt; mp_get_x_next(mp);
17219 }
17220
17221 @ @<Scan a delimited primary@>=
17222
17223   l_delim=mp->cur_sym; r_delim=mp->cur_mod; 
17224   mp_get_x_next(mp); mp_scan_expression(mp);
17225   if ( (mp->cur_cmd==comma) && (mp->cur_type>=mp_known) ) {
17226     @<Scan the rest of a delimited set of numerics@>;
17227   } else {
17228     mp_check_delimiter(mp, l_delim,r_delim);
17229   }
17230 }
17231
17232 @ The |stash_in| subroutine puts the current (numeric) expression into a field
17233 within a ``big node.''
17234
17235 @c void mp_stash_in (MP mp,pointer p) {
17236   pointer q; /* temporary register */
17237   type(p)=mp->cur_type;
17238   if ( mp->cur_type==mp_known ) {
17239     value(p)=mp->cur_exp;
17240   } else { 
17241     if ( mp->cur_type==mp_independent ) {
17242       @<Stash an independent |cur_exp| into a big node@>;
17243     } else { 
17244       mp->mem[value_loc(p)]=mp->mem[value_loc(mp->cur_exp)];
17245       /* |dep_list(p):=dep_list(cur_exp)| and |prev_dep(p):=prev_dep(cur_exp)| */
17246       link(prev_dep(p))=p;
17247     }
17248     mp_free_node(mp, mp->cur_exp,value_node_size);
17249   }
17250   mp->cur_type=mp_vacuous;
17251 }
17252
17253 @ In rare cases the current expression can become |independent|. There
17254 may be many dependency lists pointing to such an independent capsule,
17255 so we can't simply move it into place within a big node. Instead,
17256 we copy it, then recycle it.
17257
17258 @ @<Stash an independent |cur_exp|...@>=
17259
17260   q=mp_single_dependency(mp, mp->cur_exp);
17261   if ( q==mp->dep_final ){ 
17262     type(p)=mp_known; value(p)=0; mp_free_node(mp, q,dep_node_size);
17263   } else { 
17264     type(p)=mp_dependent; mp_new_dep(mp, p,q);
17265   }
17266   mp_recycle_value(mp, mp->cur_exp);
17267 }
17268
17269 @ This code uses the fact that |red_part_loc| and |green_part_loc|
17270 are synonymous with |x_part_loc| and |y_part_loc|.
17271
17272 @<Scan the rest of a delimited set of numerics@>=
17273
17274 p=mp_stash_cur_exp(mp);
17275 mp_get_x_next(mp); mp_scan_expression(mp);
17276 @<Make sure the second part of a pair or color has a numeric type@>;
17277 q=mp_get_node(mp, value_node_size); name_type(q)=mp_capsule;
17278 if ( mp->cur_cmd==comma ) type(q)=mp_color_type;
17279 else type(q)=mp_pair_type;
17280 mp_init_big_node(mp, q); r=value(q);
17281 mp_stash_in(mp, y_part_loc(r));
17282 mp_unstash_cur_exp(mp, p);
17283 mp_stash_in(mp, x_part_loc(r));
17284 if ( mp->cur_cmd==comma ) {
17285   @<Scan the last of a triplet of numerics@>;
17286 }
17287 if ( mp->cur_cmd==comma ) {
17288   type(q)=mp_cmykcolor_type;
17289   mp_init_big_node(mp, q); t=value(q);
17290   mp->mem[cyan_part_loc(t)]=mp->mem[red_part_loc(r)];
17291   value(cyan_part_loc(t))=value(red_part_loc(r));
17292   mp->mem[magenta_part_loc(t)]=mp->mem[green_part_loc(r)];
17293   value(magenta_part_loc(t))=value(green_part_loc(r));
17294   mp->mem[yellow_part_loc(t)]=mp->mem[blue_part_loc(r)];
17295   value(yellow_part_loc(t))=value(blue_part_loc(r));
17296   mp_recycle_value(mp, r);
17297   r=t;
17298   @<Scan the last of a quartet of numerics@>;
17299 }
17300 mp_check_delimiter(mp, l_delim,r_delim);
17301 mp->cur_type=type(q);
17302 mp->cur_exp=q;
17303 }
17304
17305 @ @<Make sure the second part of a pair or color has a numeric type@>=
17306 if ( mp->cur_type<mp_known ) {
17307   exp_err("Nonnumeric ypart has been replaced by 0");
17308 @.Nonnumeric...replaced by 0@>
17309   help4("I've started to scan a pair `(a,b)' or a color `(a,b,c)';")
17310     ("but after finding a nice `a' I found a `b' that isn't")
17311     ("of numeric type. So I've changed that part to zero.")
17312     ("(The b that I didn't like appears above the error message.)");
17313   mp_put_get_flush_error(mp, 0);
17314 }
17315
17316 @ @<Scan the last of a triplet of numerics@>=
17317
17318   mp_get_x_next(mp); mp_scan_expression(mp);
17319   if ( mp->cur_type<mp_known ) {
17320     exp_err("Nonnumeric third part has been replaced by 0");
17321 @.Nonnumeric...replaced by 0@>
17322     help3("I've just scanned a color `(a,b,c)' or cmykcolor(a,b,c,d); but the `c'")
17323       ("isn't of numeric type. So I've changed that part to zero.")
17324       ("(The c that I didn't like appears above the error message.)");
17325     mp_put_get_flush_error(mp, 0);
17326   }
17327   mp_stash_in(mp, blue_part_loc(r));
17328 }
17329
17330 @ @<Scan the last of a quartet of numerics@>=
17331
17332   mp_get_x_next(mp); mp_scan_expression(mp);
17333   if ( mp->cur_type<mp_known ) {
17334     exp_err("Nonnumeric blackpart has been replaced by 0");
17335 @.Nonnumeric...replaced by 0@>
17336     help3("I've just scanned a cmykcolor `(c,m,y,k)'; but the `k' isn't")
17337       ("of numeric type. So I've changed that part to zero.")
17338       ("(The k that I didn't like appears above the error message.)");
17339     mp_put_get_flush_error(mp, 0);
17340   }
17341   mp_stash_in(mp, black_part_loc(r));
17342 }
17343
17344 @ The local variable |group_line| keeps track of the line
17345 where a \&{begingroup} command occurred; this will be useful
17346 in an error message if the group doesn't actually end.
17347
17348 @<Other local variables for |scan_primary|@>=
17349 integer group_line; /* where a group began */
17350
17351 @ @<Scan a grouped primary@>=
17352
17353   group_line=mp_true_line(mp);
17354   if ( mp->internal[mp_tracing_commands]>0 ) show_cur_cmd_mod;
17355   save_boundary_item(p);
17356   do {  
17357     mp_do_statement(mp); /* ends with |cur_cmd>=semicolon| */
17358   } while (mp->cur_cmd==semicolon);
17359   if ( mp->cur_cmd!=end_group ) {
17360     print_err("A group begun on line ");
17361 @.A group...never ended@>
17362     mp_print_int(mp, group_line);
17363     mp_print(mp, " never ended");
17364     help2("I saw a `begingroup' back there that hasn't been matched")
17365          ("by `endgroup'. So I've inserted `endgroup' now.");
17366     mp_back_error(mp); mp->cur_cmd=end_group;
17367   }
17368   mp_unsave(mp); 
17369     /* this might change |cur_type|, if independent variables are recycled */
17370   if ( mp->internal[mp_tracing_commands]>0 ) show_cur_cmd_mod;
17371 }
17372
17373 @ @<Scan a string constant@>=
17374
17375   mp->cur_type=mp_string_type; mp->cur_exp=mp->cur_mod;
17376 }
17377
17378 @ Later we'll come to procedures that perform actual operations like
17379 addition, square root, and so on; our purpose now is to do the parsing.
17380 But we might as well mention those future procedures now, so that the
17381 suspense won't be too bad:
17382
17383 \smallskip
17384 |do_nullary(c)| does primitive operations that have no operands (e.g.,
17385 `\&{true}' or `\&{pencircle}');
17386
17387 \smallskip
17388 |do_unary(c)| applies a primitive operation to the current expression;
17389
17390 \smallskip
17391 |do_binary(p,c)| applies a primitive operation to the capsule~|p|
17392 and the current expression.
17393
17394 @<Scan a nullary operation@>=mp_do_nullary(mp, mp->cur_mod)
17395
17396 @ @<Scan a unary operation@>=
17397
17398   c=mp->cur_mod; mp_get_x_next(mp); mp_scan_primary(mp); 
17399   mp_do_unary(mp, c); goto DONE;
17400 }
17401
17402 @ A numeric token might be a primary by itself, or it might be the
17403 numerator of a fraction composed solely of numeric tokens, or it might
17404 multiply the primary that follows (provided that the primary doesn't begin
17405 with a plus sign or a minus sign). The code here uses the facts that
17406 |max_primary_command=plus_or_minus| and
17407 |max_primary_command-1=numeric_token|. If a fraction is found that is less
17408 than unity, we try to retain higher precision when we use it in scalar
17409 multiplication.
17410
17411 @<Other local variables for |scan_primary|@>=
17412 scaled num,denom; /* for primaries that are fractions, like `1/2' */
17413
17414 @ @<Scan a primary that starts with a numeric token@>=
17415
17416   mp->cur_exp=mp->cur_mod; mp->cur_type=mp_known; mp_get_x_next(mp);
17417   if ( mp->cur_cmd!=slash ) { 
17418     num=0; denom=0;
17419   } else { 
17420     mp_get_x_next(mp);
17421     if ( mp->cur_cmd!=numeric_token ) { 
17422       mp_back_input(mp);
17423       mp->cur_cmd=slash; mp->cur_mod=over; mp->cur_sym=frozen_slash;
17424       goto DONE;
17425     }
17426     num=mp->cur_exp; denom=mp->cur_mod;
17427     if ( denom==0 ) { @<Protest division by zero@>; }
17428     else { mp->cur_exp=mp_make_scaled(mp, num,denom); }
17429     check_arith; mp_get_x_next(mp);
17430   }
17431   if ( mp->cur_cmd>=min_primary_command ) {
17432    if ( mp->cur_cmd<numeric_token ) { /* in particular, |cur_cmd<>plus_or_minus| */
17433      p=mp_stash_cur_exp(mp); mp_scan_primary(mp);
17434      if ( (abs(num)>=abs(denom))||(mp->cur_type<mp_color_type) ) {
17435        mp_do_binary(mp, p,times);
17436      } else {
17437        mp_frac_mult(mp, num,denom);
17438        mp_free_node(mp, p,value_node_size);
17439      }
17440     }
17441   }
17442   goto DONE;
17443 }
17444
17445 @ @<Protest division...@>=
17446
17447   print_err("Division by zero");
17448 @.Division by zero@>
17449   help1("I'll pretend that you meant to divide by 1."); mp_error(mp);
17450 }
17451
17452 @ @<Scan a binary operation with `\&{of}' between its operands@>=
17453
17454   c=mp->cur_mod; mp_get_x_next(mp); mp_scan_expression(mp);
17455   if ( mp->cur_cmd!=of_token ) {
17456     mp_missing_err(mp, "of"); mp_print(mp, " for "); 
17457     mp_print_cmd_mod(mp, primary_binary,c);
17458 @.Missing `of'@>
17459     help1("I've got the first argument; will look now for the other.");
17460     mp_back_error(mp);
17461   }
17462   p=mp_stash_cur_exp(mp); mp_get_x_next(mp); mp_scan_primary(mp); 
17463   mp_do_binary(mp, p,c); goto DONE;
17464 }
17465
17466 @ @<Convert a suffix to a string@>=
17467
17468   mp_get_x_next(mp); mp_scan_suffix(mp); 
17469   mp->old_setting=mp->selector; mp->selector=new_string;
17470   mp_show_token_list(mp, mp->cur_exp,null,100000,0); 
17471   mp_flush_token_list(mp, mp->cur_exp);
17472   mp->cur_exp=mp_make_string(mp); mp->selector=mp->old_setting; 
17473   mp->cur_type=mp_string_type;
17474   goto DONE;
17475 }
17476
17477 @ If an internal quantity appears all by itself on the left of an
17478 assignment, we return a token list of length one, containing the address
17479 of the internal quantity plus |hash_end|. (This accords with the conventions
17480 of the save stack, as described earlier.)
17481
17482 @<Scan an internal...@>=
17483
17484   q=mp->cur_mod;
17485   if ( my_var_flag==assignment ) {
17486     mp_get_x_next(mp);
17487     if ( mp->cur_cmd==assignment ) {
17488       mp->cur_exp=mp_get_avail(mp);
17489       info(mp->cur_exp)=q+hash_end; mp->cur_type=mp_token_list; 
17490       goto DONE;
17491     }
17492     mp_back_input(mp);
17493   }
17494   mp->cur_type=mp_known; mp->cur_exp=mp->internal[q];
17495 }
17496
17497 @ The most difficult part of |scan_primary| has been saved for last, since
17498 it was necessary to build up some confidence first. We can now face the task
17499 of scanning a variable.
17500
17501 As we scan a variable, we build a token list containing the relevant
17502 names and subscript values, simultaneously following along in the
17503 ``collective'' structure to see if we are actually dealing with a macro
17504 instead of a value.
17505
17506 The local variables |pre_head| and |post_head| will point to the beginning
17507 of the prefix and suffix lists; |tail| will point to the end of the list
17508 that is currently growing.
17509
17510 Another local variable, |tt|, contains partial information about the
17511 declared type of the variable-so-far. If |tt>=mp_unsuffixed_macro|, the
17512 relation |tt=type(q)| will always hold. If |tt=undefined|, the routine
17513 doesn't bother to update its information about type. And if
17514 |undefined<tt<mp_unsuffixed_macro|, the precise value of |tt| isn't critical.
17515
17516 @ @<Other local variables for |scan_primary|@>=
17517 pointer pre_head,post_head,tail;
17518   /* prefix and suffix list variables */
17519 small_number tt; /* approximation to the type of the variable-so-far */
17520 pointer t; /* a token */
17521 pointer macro_ref = 0; /* reference count for a suffixed macro */
17522
17523 @ @<Scan a variable primary...@>=
17524
17525   fast_get_avail(pre_head); tail=pre_head; post_head=null; tt=mp_vacuous;
17526   while (1) { 
17527     t=mp_cur_tok(mp); link(tail)=t;
17528     if ( tt!=undefined ) {
17529        @<Find the approximate type |tt| and corresponding~|q|@>;
17530       if ( tt>=mp_unsuffixed_macro ) {
17531         @<Either begin an unsuffixed macro call or
17532           prepare for a suffixed one@>;
17533       }
17534     }
17535     mp_get_x_next(mp); tail=t;
17536     if ( mp->cur_cmd==left_bracket ) {
17537       @<Scan for a subscript; replace |cur_cmd| by |numeric_token| if found@>;
17538     }
17539     if ( mp->cur_cmd>max_suffix_token ) break;
17540     if ( mp->cur_cmd<min_suffix_token ) break;
17541   } /* now |cur_cmd| is |internal_quantity|, |tag_token|, or |numeric_token| */
17542   @<Handle unusual cases that masquerade as variables, and |goto restart|
17543     or |goto done| if appropriate;
17544     otherwise make a copy of the variable and |goto done|@>;
17545 }
17546
17547 @ @<Either begin an unsuffixed macro call or...@>=
17548
17549   link(tail)=null;
17550   if ( tt>mp_unsuffixed_macro ) { /* |tt=mp_suffixed_macro| */
17551     post_head=mp_get_avail(mp); tail=post_head; link(tail)=t;
17552     tt=undefined; macro_ref=value(q); add_mac_ref(macro_ref);
17553   } else {
17554     @<Set up unsuffixed macro call and |goto restart|@>;
17555   }
17556 }
17557
17558 @ @<Scan for a subscript; replace |cur_cmd| by |numeric_token| if found@>=
17559
17560   mp_get_x_next(mp); mp_scan_expression(mp);
17561   if ( mp->cur_cmd!=right_bracket ) {
17562     @<Put the left bracket and the expression back to be rescanned@>;
17563   } else { 
17564     if ( mp->cur_type!=mp_known ) mp_bad_subscript(mp);
17565     mp->cur_cmd=numeric_token; mp->cur_mod=mp->cur_exp; mp->cur_sym=0;
17566   }
17567 }
17568
17569 @ The left bracket that we thought was introducing a subscript might have
17570 actually been the left bracket in a mediation construction like `\.{x[a,b]}'.
17571 So we don't issue an error message at this point; but we do want to back up
17572 so as to avoid any embarrassment about our incorrect assumption.
17573
17574 @<Put the left bracket and the expression back to be rescanned@>=
17575
17576   mp_back_input(mp); /* that was the token following the current expression */
17577   mp_back_expr(mp); mp->cur_cmd=left_bracket; 
17578   mp->cur_mod=0; mp->cur_sym=frozen_left_bracket;
17579 }
17580
17581 @ Here's a routine that puts the current expression back to be read again.
17582
17583 @c void mp_back_expr (MP mp) {
17584   pointer p; /* capsule token */
17585   p=mp_stash_cur_exp(mp); link(p)=null; back_list(p);
17586 }
17587
17588 @ Unknown subscripts lead to the following error message.
17589
17590 @c void mp_bad_subscript (MP mp) { 
17591   exp_err("Improper subscript has been replaced by zero");
17592 @.Improper subscript...@>
17593   help3("A bracketed subscript must have a known numeric value;")
17594     ("unfortunately, what I found was the value that appears just")
17595     ("above this error message. So I'll try a zero subscript.");
17596   mp_flush_error(mp, 0);
17597 }
17598
17599 @ Every time we call |get_x_next|, there's a chance that the variable we've
17600 been looking at will disappear. Thus, we cannot safely keep |q| pointing
17601 into the variable structure; we need to start searching from the root each time.
17602
17603 @<Find the approximate type |tt| and corresponding~|q|@>=
17604 @^inner loop@>
17605
17606   p=link(pre_head); q=info(p); tt=undefined;
17607   if ( eq_type(q) % outer_tag==tag_token ) {
17608     q=equiv(q);
17609     if ( q==null ) goto DONE2;
17610     while (1) { 
17611       p=link(p);
17612       if ( p==null ) {
17613         tt=type(q); goto DONE2;
17614       };
17615       if ( type(q)!=mp_structured ) goto DONE2;
17616       q=link(attr_head(q)); /* the |collective_subscript| attribute */
17617       if ( p>=mp->hi_mem_min ) { /* it's not a subscript */
17618         do {  q=link(q); } while (! (attr_loc(q)>=info(p)));
17619         if ( attr_loc(q)>info(p) ) goto DONE2;
17620       }
17621     }
17622   }
17623 DONE2:
17624   ;
17625 }
17626
17627 @ How do things stand now? Well, we have scanned an entire variable name,
17628 including possible subscripts and/or attributes; |cur_cmd|, |cur_mod|, and
17629 |cur_sym| represent the token that follows. If |post_head=null|, a
17630 token list for this variable name starts at |link(pre_head)|, with all
17631 subscripts evaluated. But if |post_head<>null|, the variable turned out
17632 to be a suffixed macro; |pre_head| is the head of the prefix list, while
17633 |post_head| is the head of a token list containing both `\.{\AT!}' and
17634 the suffix.
17635
17636 Our immediate problem is to see if this variable still exists. (Variable
17637 structures can change drastically whenever we call |get_x_next|; users
17638 aren't supposed to do this, but the fact that it is possible means that
17639 we must be cautious.)
17640
17641 The following procedure prints an error message when a variable
17642 unexpectedly disappears. Its help message isn't quite right for
17643 our present purposes, but we'll be able to fix that up.
17644
17645 @c 
17646 void mp_obliterated (MP mp,pointer q) { 
17647   print_err("Variable "); mp_show_token_list(mp, q,null,1000,0);
17648   mp_print(mp, " has been obliterated");
17649 @.Variable...obliterated@>
17650   help5("It seems you did a nasty thing---probably by accident,")
17651     ("but nevertheless you nearly hornswoggled me...")
17652     ("While I was evaluating the right-hand side of this")
17653     ("command, something happened, and the left-hand side")
17654     ("is no longer a variable! So I won't change anything.");
17655 }
17656
17657 @ If the variable does exist, we also need to check
17658 for a few other special cases before deciding that a plain old ordinary
17659 variable has, indeed, been scanned.
17660
17661 @<Handle unusual cases that masquerade as variables...@>=
17662 if ( post_head!=null ) {
17663   @<Set up suffixed macro call and |goto restart|@>;
17664 }
17665 q=link(pre_head); free_avail(pre_head);
17666 if ( mp->cur_cmd==my_var_flag ) { 
17667   mp->cur_type=mp_token_list; mp->cur_exp=q; goto DONE;
17668 }
17669 p=mp_find_variable(mp, q);
17670 if ( p!=null ) {
17671   mp_make_exp_copy(mp, p);
17672 } else { 
17673   mp_obliterated(mp, q);
17674   mp->help_line[2]="While I was evaluating the suffix of this variable,";
17675   mp->help_line[1]="something was redefined, and it's no longer a variable!";
17676   mp->help_line[0]="In order to get back on my feet, I've inserted `0' instead.";
17677   mp_put_get_flush_error(mp, 0);
17678 }
17679 mp_flush_node_list(mp, q); 
17680 goto DONE
17681
17682 @ The only complication associated with macro calling is that the prefix
17683 and ``at'' parameters must be packaged in an appropriate list of lists.
17684
17685 @<Set up unsuffixed macro call and |goto restart|@>=
17686
17687   p=mp_get_avail(mp); info(pre_head)=link(pre_head); link(pre_head)=p;
17688   info(p)=t; mp_macro_call(mp, value(q),pre_head,null);
17689   mp_get_x_next(mp); 
17690   goto RESTART;
17691 }
17692
17693 @ If the ``variable'' that turned out to be a suffixed macro no longer exists,
17694 we don't care, because we have reserved a pointer (|macro_ref|) to its
17695 token list.
17696
17697 @<Set up suffixed macro call and |goto restart|@>=
17698
17699   mp_back_input(mp); p=mp_get_avail(mp); q=link(post_head);
17700   info(pre_head)=link(pre_head); link(pre_head)=post_head;
17701   info(post_head)=q; link(post_head)=p; info(p)=link(q); link(q)=null;
17702   mp_macro_call(mp, macro_ref,pre_head,null); decr(ref_count(macro_ref));
17703   mp_get_x_next(mp); goto RESTART;
17704 }
17705
17706 @ Our remaining job is simply to make a copy of the value that has been
17707 found. Some cases are harder than others, but complexity arises solely
17708 because of the multiplicity of possible cases.
17709
17710 @<Declare the procedure called |make_exp_copy|@>=
17711 @<Declare subroutines needed by |make_exp_copy|@>
17712 void mp_make_exp_copy (MP mp,pointer p) {
17713   pointer q,r,t; /* registers for list manipulation */
17714 RESTART: 
17715   mp->cur_type=type(p);
17716   switch (mp->cur_type) {
17717   case mp_vacuous: case mp_boolean_type: case mp_known:
17718     mp->cur_exp=value(p); break;
17719   case unknown_types:
17720     mp->cur_exp=mp_new_ring_entry(mp, p);
17721     break;
17722   case mp_string_type: 
17723     mp->cur_exp=value(p); add_str_ref(mp->cur_exp);
17724     break;
17725   case mp_picture_type:
17726     mp->cur_exp=value(p);add_edge_ref(mp->cur_exp);
17727     break;
17728   case mp_pen_type:
17729     mp->cur_exp=copy_pen(value(p));
17730     break; 
17731   case mp_path_type:
17732     mp->cur_exp=mp_copy_path(mp, value(p));
17733     break;
17734   case mp_transform_type: case mp_color_type: 
17735   case mp_cmykcolor_type: case mp_pair_type:
17736     @<Copy the big node |p|@>;
17737     break;
17738   case mp_dependent: case mp_proto_dependent:
17739     mp_encapsulate(mp, mp_copy_dep_list(mp, dep_list(p)));
17740     break;
17741   case mp_numeric_type: 
17742     new_indep(p); goto RESTART;
17743     break;
17744   case mp_independent: 
17745     q=mp_single_dependency(mp, p);
17746     if ( q==mp->dep_final ){ 
17747       mp->cur_type=mp_known; mp->cur_exp=0; mp_free_node(mp, q,dep_node_size);
17748     } else { 
17749       mp->cur_type=mp_dependent; mp_encapsulate(mp, q);
17750     }
17751     break;
17752   default: 
17753     mp_confusion(mp, "copy");
17754 @:this can't happen copy}{\quad copy@>
17755     break;
17756   }
17757 }
17758
17759 @ The |encapsulate| subroutine assumes that |dep_final| is the
17760 tail of dependency list~|p|.
17761
17762 @<Declare subroutines needed by |make_exp_copy|@>=
17763 void mp_encapsulate (MP mp,pointer p) { 
17764   mp->cur_exp=mp_get_node(mp, value_node_size); type(mp->cur_exp)=mp->cur_type;
17765   name_type(mp->cur_exp)=mp_capsule; mp_new_dep(mp, mp->cur_exp,p);
17766 }
17767
17768 @ The most tedious case arises when the user refers to a
17769 \&{pair}, \&{color}, or \&{transform} variable; we must copy several fields,
17770 each of which can be |independent|, |dependent|, |mp_proto_dependent|,
17771 or |known|.
17772
17773 @<Copy the big node |p|@>=
17774
17775   if ( value(p)==null ) 
17776     mp_init_big_node(mp, p);
17777   t=mp_get_node(mp, value_node_size); name_type(t)=mp_capsule; type(t)=mp->cur_type;
17778   mp_init_big_node(mp, t);
17779   q=value(p)+mp->big_node_size[mp->cur_type]; 
17780   r=value(t)+mp->big_node_size[mp->cur_type];
17781   do {  
17782     q=q-2; r=r-2; mp_install(mp, r,q);
17783   } while (q!=value(p));
17784   mp->cur_exp=t;
17785 }
17786
17787 @ The |install| procedure copies a numeric field~|q| into field~|r| of
17788 a big node that will be part of a capsule.
17789
17790 @<Declare subroutines needed by |make_exp_copy|@>=
17791 void mp_install (MP mp,pointer r, pointer q) {
17792   pointer p; /* temporary register */
17793   if ( type(q)==mp_known ){ 
17794     value(r)=value(q); type(r)=mp_known;
17795   } else  if ( type(q)==mp_independent ) {
17796     p=mp_single_dependency(mp, q);
17797     if ( p==mp->dep_final ) {
17798       type(r)=mp_known; value(r)=0; mp_free_node(mp, p,dep_node_size);
17799     } else  { 
17800       type(r)=mp_dependent; mp_new_dep(mp, r,p);
17801     }
17802   } else {
17803     type(r)=type(q); mp_new_dep(mp, r,mp_copy_dep_list(mp, dep_list(q)));
17804   }
17805 }
17806
17807 @ Expressions of the form `\.{a[b,c]}' are converted into
17808 `\.{b+a*(c-b)}', without checking the types of \.b~or~\.c,
17809 provided that \.a is numeric.
17810
17811 @<Scan a mediation...@>=
17812
17813   p=mp_stash_cur_exp(mp); mp_get_x_next(mp); mp_scan_expression(mp);
17814   if ( mp->cur_cmd!=comma ) {
17815     @<Put the left bracket and the expression back...@>;
17816     mp_unstash_cur_exp(mp, p);
17817   } else { 
17818     q=mp_stash_cur_exp(mp); mp_get_x_next(mp); mp_scan_expression(mp);
17819     if ( mp->cur_cmd!=right_bracket ) {
17820       mp_missing_err(mp, "]");
17821 @.Missing `]'@>
17822       help3("I've scanned an expression of the form `a[b,c',")
17823       ("so a right bracket should have come next.")
17824       ("I shall pretend that one was there.");
17825       mp_back_error(mp);
17826     }
17827     r=mp_stash_cur_exp(mp); mp_make_exp_copy(mp, q);
17828     mp_do_binary(mp, r,minus); mp_do_binary(mp, p,times); 
17829     mp_do_binary(mp, q,plus); mp_get_x_next(mp);
17830   }
17831 }
17832
17833 @ Here is a comparatively simple routine that is used to scan the
17834 \&{suffix} parameters of a macro.
17835
17836 @<Declare the basic parsing subroutines@>=
17837 void mp_scan_suffix (MP mp) {
17838   pointer h,t; /* head and tail of the list being built */
17839   pointer p; /* temporary register */
17840   h=mp_get_avail(mp); t=h;
17841   while (1) { 
17842     if ( mp->cur_cmd==left_bracket ) {
17843       @<Scan a bracketed subscript and set |cur_cmd:=numeric_token|@>;
17844     }
17845     if ( mp->cur_cmd==numeric_token ) {
17846       p=mp_new_num_tok(mp, mp->cur_mod);
17847     } else if ((mp->cur_cmd==tag_token)||(mp->cur_cmd==internal_quantity) ) {
17848        p=mp_get_avail(mp); info(p)=mp->cur_sym;
17849     } else {
17850       break;
17851     }
17852     link(t)=p; t=p; mp_get_x_next(mp);
17853   }
17854   mp->cur_exp=link(h); free_avail(h); mp->cur_type=mp_token_list;
17855 }
17856
17857 @ @<Scan a bracketed subscript and set |cur_cmd:=numeric_token|@>=
17858
17859   mp_get_x_next(mp); mp_scan_expression(mp);
17860   if ( mp->cur_type!=mp_known ) mp_bad_subscript(mp);
17861   if ( mp->cur_cmd!=right_bracket ) {
17862      mp_missing_err(mp, "]");
17863 @.Missing `]'@>
17864     help3("I've seen a `[' and a subscript value, in a suffix,")
17865       ("so a right bracket should have come next.")
17866       ("I shall pretend that one was there.");
17867     mp_back_error(mp);
17868   }
17869   mp->cur_cmd=numeric_token; mp->cur_mod=mp->cur_exp;
17870 }
17871
17872 @* \[38] Parsing secondary and higher expressions.
17873
17874 After the intricacies of |scan_primary|\kern-1pt,
17875 the |scan_secondary| routine is
17876 refreshingly simple. It's not trivial, but the operations are relatively
17877 straightforward; the main difficulty is, again, that expressions and data
17878 structures might change drastically every time we call |get_x_next|, so a
17879 cautious approach is mandatory. For example, a macro defined by
17880 \&{primarydef} might have disappeared by the time its second argument has
17881 been scanned; we solve this by increasing the reference count of its token
17882 list, so that the macro can be called even after it has been clobbered.
17883
17884 @<Declare the basic parsing subroutines@>=
17885 void mp_scan_secondary (MP mp) {
17886   pointer p; /* for list manipulation */
17887   halfword c,d; /* operation codes or modifiers */
17888   pointer mac_name; /* token defined with \&{primarydef} */
17889 RESTART:
17890   if ((mp->cur_cmd<min_primary_command)||
17891       (mp->cur_cmd>max_primary_command) )
17892     mp_bad_exp(mp, "A secondary");
17893 @.A secondary expression...@>
17894   mp_scan_primary(mp);
17895 CONTINUE: 
17896   if ( mp->cur_cmd<=max_secondary_command &&
17897        mp->cur_cmd>=min_secondary_command ) {
17898     p=mp_stash_cur_exp(mp); 
17899     c=mp->cur_mod; d=mp->cur_cmd;
17900     if ( d==secondary_primary_macro ) { 
17901       mac_name=mp->cur_sym; 
17902       add_mac_ref(c);
17903     }
17904     mp_get_x_next(mp); 
17905     mp_scan_primary(mp);
17906     if ( d!=secondary_primary_macro ) {
17907       mp_do_binary(mp, p,c);
17908     } else { 
17909       mp_back_input(mp); 
17910       mp_binary_mac(mp, p,c,mac_name);
17911       decr(ref_count(c)); 
17912       mp_get_x_next(mp); 
17913       goto RESTART;
17914     }
17915     goto CONTINUE;
17916   }
17917 }
17918
17919 @ The following procedure calls a macro that has two parameters,
17920 |p| and |cur_exp|.
17921
17922 @c void mp_binary_mac (MP mp,pointer p, pointer c, pointer n) {
17923   pointer q,r; /* nodes in the parameter list */
17924   q=mp_get_avail(mp); r=mp_get_avail(mp); link(q)=r;
17925   info(q)=p; info(r)=mp_stash_cur_exp(mp);
17926   mp_macro_call(mp, c,q,n);
17927 }
17928
17929 @ The next procedure, |scan_tertiary|, is pretty much the same deal.
17930
17931 @<Declare the basic parsing subroutines@>=
17932 void mp_scan_tertiary (MP mp) {
17933   pointer p; /* for list manipulation */
17934   halfword c,d; /* operation codes or modifiers */
17935   pointer mac_name; /* token defined with \&{secondarydef} */
17936 RESTART:
17937   if ((mp->cur_cmd<min_primary_command)||
17938       (mp->cur_cmd>max_primary_command) )
17939     mp_bad_exp(mp, "A tertiary");
17940 @.A tertiary expression...@>
17941   mp_scan_secondary(mp);
17942 CONTINUE: 
17943   if ( mp->cur_cmd<=max_tertiary_command ) {
17944     if ( mp->cur_cmd>=min_tertiary_command ) {
17945       p=mp_stash_cur_exp(mp); c=mp->cur_mod; d=mp->cur_cmd;
17946       if ( d==tertiary_secondary_macro ) { 
17947         mac_name=mp->cur_sym; add_mac_ref(c);
17948       };
17949       mp_get_x_next(mp); mp_scan_secondary(mp);
17950       if ( d!=tertiary_secondary_macro ) {
17951         mp_do_binary(mp, p,c);
17952       } else { 
17953         mp_back_input(mp); mp_binary_mac(mp, p,c,mac_name);
17954         decr(ref_count(c)); mp_get_x_next(mp); 
17955         goto RESTART;
17956       }
17957       goto CONTINUE;
17958     }
17959   }
17960 }
17961
17962 @ Finally we reach the deepest level in our quartet of parsing routines.
17963 This one is much like the others; but it has an extra complication from
17964 paths, which materialize here.
17965
17966 @d continue_path 25 /* a label inside of |scan_expression| */
17967 @d finish_path 26 /* another */
17968
17969 @<Declare the basic parsing subroutines@>=
17970 void mp_scan_expression (MP mp) {
17971   pointer p,q,r,pp,qq; /* for list manipulation */
17972   halfword c,d; /* operation codes or modifiers */
17973   int my_var_flag; /* initial value of |var_flag| */
17974   pointer mac_name; /* token defined with \&{tertiarydef} */
17975   boolean cycle_hit; /* did a path expression just end with `\&{cycle}'? */
17976   scaled x,y; /* explicit coordinates or tension at a path join */
17977   int t; /* knot type following a path join */
17978   t=0; y=0; x=0;
17979   my_var_flag=mp->var_flag; mac_name=null;
17980 RESTART:
17981   if ((mp->cur_cmd<min_primary_command)||
17982       (mp->cur_cmd>max_primary_command) )
17983     mp_bad_exp(mp, "An");
17984 @.An expression...@>
17985   mp_scan_tertiary(mp);
17986 CONTINUE: 
17987   if ( mp->cur_cmd<=max_expression_command )
17988     if ( mp->cur_cmd>=min_expression_command ) {
17989       if ( (mp->cur_cmd!=equals)||(my_var_flag!=assignment) ) {
17990         p=mp_stash_cur_exp(mp); c=mp->cur_mod; d=mp->cur_cmd;
17991         if ( d==expression_tertiary_macro ) {
17992           mac_name=mp->cur_sym; add_mac_ref(c);
17993         }
17994         if ( (d<ampersand)||((d==ampersand)&&
17995              ((type(p)==mp_pair_type)||(type(p)==mp_path_type))) ) {
17996           @<Scan a path construction operation;
17997             but |return| if |p| has the wrong type@>;
17998         } else { 
17999           mp_get_x_next(mp); mp_scan_tertiary(mp);
18000           if ( d!=expression_tertiary_macro ) {
18001             mp_do_binary(mp, p,c);
18002           } else  { 
18003             mp_back_input(mp); mp_binary_mac(mp, p,c,mac_name);
18004             decr(ref_count(c)); mp_get_x_next(mp); 
18005             goto RESTART;
18006           }
18007         }
18008         goto CONTINUE;
18009      }
18010   }
18011 }
18012
18013 @ The reader should review the data structure conventions for paths before
18014 hoping to understand the next part of this code.
18015
18016 @<Scan a path construction operation...@>=
18017
18018   cycle_hit=false;
18019   @<Convert the left operand, |p|, into a partial path ending at~|q|;
18020     but |return| if |p| doesn't have a suitable type@>;
18021 CONTINUE_PATH: 
18022   @<Determine the path join parameters;
18023     but |goto finish_path| if there's only a direction specifier@>;
18024   if ( mp->cur_cmd==cycle ) {
18025     @<Get ready to close a cycle@>;
18026   } else { 
18027     mp_scan_tertiary(mp);
18028     @<Convert the right operand, |cur_exp|,
18029       into a partial path from |pp| to~|qq|@>;
18030   }
18031   @<Join the partial paths and reset |p| and |q| to the head and tail
18032     of the result@>;
18033   if ( mp->cur_cmd>=min_expression_command )
18034     if ( mp->cur_cmd<=ampersand ) if ( ! cycle_hit ) goto CONTINUE_PATH;
18035 FINISH_PATH:
18036   @<Choose control points for the path and put the result into |cur_exp|@>;
18037 }
18038
18039 @ @<Convert the left operand, |p|, into a partial path ending at~|q|...@>=
18040
18041   mp_unstash_cur_exp(mp, p);
18042   if ( mp->cur_type==mp_pair_type ) p=mp_new_knot(mp);
18043   else if ( mp->cur_type==mp_path_type ) p=mp->cur_exp;
18044   else return;
18045   q=p;
18046   while ( link(q)!=p ) q=link(q);
18047   if ( left_type(p)!=mp_endpoint ) { /* open up a cycle */
18048     r=mp_copy_knot(mp, p); link(q)=r; q=r;
18049   }
18050   left_type(p)=mp_open; right_type(q)=mp_open;
18051 }
18052
18053 @ A pair of numeric values is changed into a knot node for a one-point path
18054 when \MP\ discovers that the pair is part of a path.
18055
18056 @c @<Declare the procedure called |known_pair|@>
18057 pointer mp_new_knot (MP mp) { /* convert a pair to a knot with two endpoints */
18058   pointer q; /* the new node */
18059   q=mp_get_node(mp, knot_node_size); left_type(q)=mp_endpoint;
18060   right_type(q)=mp_endpoint; originator(q)=mp_metapost_user; link(q)=q;
18061   mp_known_pair(mp); x_coord(q)=mp->cur_x; y_coord(q)=mp->cur_y;
18062   return q;
18063 }
18064
18065 @ The |known_pair| subroutine sets |cur_x| and |cur_y| to the components
18066 of the current expression, assuming that the current expression is a
18067 pair of known numerics. Unknown components are zeroed, and the
18068 current expression is flushed.
18069
18070 @<Declare the procedure called |known_pair|@>=
18071 void mp_known_pair (MP mp) {
18072   pointer p; /* the pair node */
18073   if ( mp->cur_type!=mp_pair_type ) {
18074     exp_err("Undefined coordinates have been replaced by (0,0)");
18075 @.Undefined coordinates...@>
18076     help5("I need x and y numbers for this part of the path.")
18077       ("The value I found (see above) was no good;")
18078       ("so I'll try to keep going by using zero instead.")
18079       ("(Chapter 27 of The METAFONTbook explains that")
18080 @:METAFONTbook}{\sl The {\logos METAFONT\/}book@>
18081       ("you might want to type `I ??" "?' now.)");
18082     mp_put_get_flush_error(mp, 0); mp->cur_x=0; mp->cur_y=0;
18083   } else { 
18084     p=value(mp->cur_exp);
18085      @<Make sure that both |x| and |y| parts of |p| are known;
18086        copy them into |cur_x| and |cur_y|@>;
18087     mp_flush_cur_exp(mp, 0);
18088   }
18089 }
18090
18091 @ @<Make sure that both |x| and |y| parts of |p| are known...@>=
18092 if ( type(x_part_loc(p))==mp_known ) {
18093   mp->cur_x=value(x_part_loc(p));
18094 } else { 
18095   mp_disp_err(mp, x_part_loc(p),
18096     "Undefined x coordinate has been replaced by 0");
18097 @.Undefined coordinates...@>
18098   help5("I need a `known' x value for this part of the path.")
18099     ("The value I found (see above) was no good;")
18100     ("so I'll try to keep going by using zero instead.")
18101     ("(Chapter 27 of The METAFONTbook explains that")
18102 @:METAFONTbook}{\sl The {\logos METAFONT\/}book@>
18103     ("you might want to type `I ??" "?' now.)");
18104   mp_put_get_error(mp); mp_recycle_value(mp, x_part_loc(p)); mp->cur_x=0;
18105 }
18106 if ( type(y_part_loc(p))==mp_known ) {
18107   mp->cur_y=value(y_part_loc(p));
18108 } else { 
18109   mp_disp_err(mp, y_part_loc(p),
18110     "Undefined y coordinate has been replaced by 0");
18111   help5("I need a `known' y value for this part of the path.")
18112     ("The value I found (see above) was no good;")
18113     ("so I'll try to keep going by using zero instead.")
18114     ("(Chapter 27 of The METAFONTbook explains that")
18115     ("you might want to type `I ??" "?' now.)");
18116   mp_put_get_error(mp); mp_recycle_value(mp, y_part_loc(p)); mp->cur_y=0;
18117 }
18118
18119 @ At this point |cur_cmd| is either |ampersand|, |left_brace|, or |path_join|.
18120
18121 @<Determine the path join parameters...@>=
18122 if ( mp->cur_cmd==left_brace ) {
18123   @<Put the pre-join direction information into node |q|@>;
18124 }
18125 d=mp->cur_cmd;
18126 if ( d==path_join ) {
18127   @<Determine the tension and/or control points@>;
18128 } else if ( d!=ampersand ) {
18129   goto FINISH_PATH;
18130 }
18131 mp_get_x_next(mp);
18132 if ( mp->cur_cmd==left_brace ) {
18133   @<Put the post-join direction information into |x| and |t|@>;
18134 } else if ( right_type(q)!=mp_explicit ) {
18135   t=mp_open; x=0;
18136 }
18137
18138 @ The |scan_direction| subroutine looks at the directional information
18139 that is enclosed in braces, and also scans ahead to the following character.
18140 A type code is returned, either |open| (if the direction was $(0,0)$),
18141 or |curl| (if the direction was a curl of known value |cur_exp|), or
18142 |given| (if the direction is given by the |angle| value that now
18143 appears in |cur_exp|).
18144
18145 There's nothing difficult about this subroutine, but the program is rather
18146 lengthy because a variety of potential errors need to be nipped in the bud.
18147
18148 @c small_number mp_scan_direction (MP mp) {
18149   int t; /* the type of information found */
18150   scaled x; /* an |x| coordinate */
18151   mp_get_x_next(mp);
18152   if ( mp->cur_cmd==curl_command ) {
18153      @<Scan a curl specification@>;
18154   } else {
18155     @<Scan a given direction@>;
18156   }
18157   if ( mp->cur_cmd!=right_brace ) {
18158     mp_missing_err(mp, "}");
18159 @.Missing `\char`\}'@>
18160     help3("I've scanned a direction spec for part of a path,")
18161       ("so a right brace should have come next.")
18162       ("I shall pretend that one was there.");
18163     mp_back_error(mp);
18164   }
18165   mp_get_x_next(mp); 
18166   return t;
18167 }
18168
18169 @ @<Scan a curl specification@>=
18170 { mp_get_x_next(mp); mp_scan_expression(mp);
18171 if ( (mp->cur_type!=mp_known)||(mp->cur_exp<0) ){ 
18172   exp_err("Improper curl has been replaced by 1");
18173 @.Improper curl@>
18174   help1("A curl must be a known, nonnegative number.");
18175   mp_put_get_flush_error(mp, unity);
18176 }
18177 t=mp_curl;
18178 }
18179
18180 @ @<Scan a given direction@>=
18181 { mp_scan_expression(mp);
18182   if ( mp->cur_type>mp_pair_type ) {
18183     @<Get given directions separated by commas@>;
18184   } else {
18185     mp_known_pair(mp);
18186   }
18187   if ( (mp->cur_x==0)&&(mp->cur_y==0) )  t=mp_open;
18188   else  { t=mp_given; mp->cur_exp=mp_n_arg(mp, mp->cur_x,mp->cur_y);}
18189 }
18190
18191 @ @<Get given directions separated by commas@>=
18192
18193   if ( mp->cur_type!=mp_known ) {
18194     exp_err("Undefined x coordinate has been replaced by 0");
18195 @.Undefined coordinates...@>
18196     help5("I need a `known' x value for this part of the path.")
18197       ("The value I found (see above) was no good;")
18198       ("so I'll try to keep going by using zero instead.")
18199       ("(Chapter 27 of The METAFONTbook explains that")
18200 @:METAFONTbook}{\sl The {\logos METAFONT\/}book@>
18201       ("you might want to type `I ??" "?' now.)");
18202     mp_put_get_flush_error(mp, 0);
18203   }
18204   x=mp->cur_exp;
18205   if ( mp->cur_cmd!=comma ) {
18206     mp_missing_err(mp, ",");
18207 @.Missing `,'@>
18208     help2("I've got the x coordinate of a path direction;")
18209       ("will look for the y coordinate next.");
18210     mp_back_error(mp);
18211   }
18212   mp_get_x_next(mp); mp_scan_expression(mp);
18213   if ( mp->cur_type!=mp_known ) {
18214      exp_err("Undefined y coordinate has been replaced by 0");
18215     help5("I need a `known' y value for this part of the path.")
18216       ("The value I found (see above) was no good;")
18217       ("so I'll try to keep going by using zero instead.")
18218       ("(Chapter 27 of The METAFONTbook explains that")
18219       ("you might want to type `I ??" "?' now.)");
18220     mp_put_get_flush_error(mp, 0);
18221   }
18222   mp->cur_y=mp->cur_exp; mp->cur_x=x;
18223 }
18224
18225 @ At this point |right_type(q)| is usually |open|, but it may have been
18226 set to some other value by a previous operation. We must maintain
18227 the value of |right_type(q)| in cases such as
18228 `\.{..\{curl2\}z\{0,0\}..}'.
18229
18230 @<Put the pre-join...@>=
18231
18232   t=mp_scan_direction(mp);
18233   if ( t!=mp_open ) {
18234     right_type(q)=t; right_given(q)=mp->cur_exp;
18235     if ( left_type(q)==mp_open ) {
18236       left_type(q)=t; left_given(q)=mp->cur_exp;
18237     } /* note that |left_given(q)=left_curl(q)| */
18238   }
18239 }
18240
18241 @ Since |left_tension| and |left_y| share the same position in knot nodes,
18242 and since |left_given| is similarly equivalent to |left_x|, we use
18243 |x| and |y| to hold the given direction and tension information when
18244 there are no explicit control points.
18245
18246 @<Put the post-join...@>=
18247
18248   t=mp_scan_direction(mp);
18249   if ( right_type(q)!=mp_explicit ) x=mp->cur_exp;
18250   else t=mp_explicit; /* the direction information is superfluous */
18251 }
18252
18253 @ @<Determine the tension and/or...@>=
18254
18255   mp_get_x_next(mp);
18256   if ( mp->cur_cmd==tension ) {
18257     @<Set explicit tensions@>;
18258   } else if ( mp->cur_cmd==controls ) {
18259     @<Set explicit control points@>;
18260   } else  { 
18261     right_tension(q)=unity; y=unity; mp_back_input(mp); /* default tension */
18262     goto DONE;
18263   };
18264   if ( mp->cur_cmd!=path_join ) {
18265      mp_missing_err(mp, "..");
18266 @.Missing `..'@>
18267     help1("A path join command should end with two dots.");
18268     mp_back_error(mp);
18269   }
18270 DONE:
18271   ;
18272 }
18273
18274 @ @<Set explicit tensions@>=
18275
18276   mp_get_x_next(mp); y=mp->cur_cmd;
18277   if ( mp->cur_cmd==at_least ) mp_get_x_next(mp);
18278   mp_scan_primary(mp);
18279   @<Make sure that the current expression is a valid tension setting@>;
18280   if ( y==at_least ) negate(mp->cur_exp);
18281   right_tension(q)=mp->cur_exp;
18282   if ( mp->cur_cmd==and_command ) {
18283     mp_get_x_next(mp); y=mp->cur_cmd;
18284     if ( mp->cur_cmd==at_least ) mp_get_x_next(mp);
18285     mp_scan_primary(mp);
18286     @<Make sure that the current expression is a valid tension setting@>;
18287     if ( y==at_least ) negate(mp->cur_exp);
18288   }
18289   y=mp->cur_exp;
18290 }
18291
18292 @ @d min_tension three_quarter_unit
18293
18294 @<Make sure that the current expression is a valid tension setting@>=
18295 if ( (mp->cur_type!=mp_known)||(mp->cur_exp<min_tension) ) {
18296   exp_err("Improper tension has been set to 1");
18297 @.Improper tension@>
18298   help1("The expression above should have been a number >=3/4.");
18299   mp_put_get_flush_error(mp, unity);
18300 }
18301
18302 @ @<Set explicit control points@>=
18303
18304   right_type(q)=mp_explicit; t=mp_explicit; mp_get_x_next(mp); mp_scan_primary(mp);
18305   mp_known_pair(mp); right_x(q)=mp->cur_x; right_y(q)=mp->cur_y;
18306   if ( mp->cur_cmd!=and_command ) {
18307     x=right_x(q); y=right_y(q);
18308   } else { 
18309     mp_get_x_next(mp); mp_scan_primary(mp);
18310     mp_known_pair(mp); x=mp->cur_x; y=mp->cur_y;
18311   }
18312 }
18313
18314 @ @<Convert the right operand, |cur_exp|, into a partial path...@>=
18315
18316   if ( mp->cur_type!=mp_path_type ) pp=mp_new_knot(mp);
18317   else pp=mp->cur_exp;
18318   qq=pp;
18319   while ( link(qq)!=pp ) qq=link(qq);
18320   if ( left_type(pp)!=mp_endpoint ) { /* open up a cycle */
18321     r=mp_copy_knot(mp, pp); link(qq)=r; qq=r;
18322   }
18323   left_type(pp)=mp_open; right_type(qq)=mp_open;
18324 }
18325
18326 @ If a person tries to define an entire path by saying `\.{(x,y)\&cycle}',
18327 we silently change the specification to `\.{(x,y)..cycle}', since a cycle
18328 shouldn't have length zero.
18329
18330 @<Get ready to close a cycle@>=
18331
18332   cycle_hit=true; mp_get_x_next(mp); pp=p; qq=p;
18333   if ( d==ampersand ) if ( p==q ) {
18334     d=path_join; right_tension(q)=unity; y=unity;
18335   }
18336 }
18337
18338 @ @<Join the partial paths and reset |p| and |q|...@>=
18339
18340 if ( d==ampersand ) {
18341   if ( (x_coord(q)!=x_coord(pp))||(y_coord(q)!=y_coord(pp)) ) {
18342     print_err("Paths don't touch; `&' will be changed to `..'");
18343 @.Paths don't touch@>
18344     help3("When you join paths `p&q', the ending point of p")
18345       ("must be exactly equal to the starting point of q.")
18346       ("So I'm going to pretend that you said `p..q' instead.");
18347     mp_put_get_error(mp); d=path_join; right_tension(q)=unity; y=unity;
18348   }
18349 }
18350 @<Plug an opening in |right_type(pp)|, if possible@>;
18351 if ( d==ampersand ) {
18352   @<Splice independent paths together@>;
18353 } else  { 
18354   @<Plug an opening in |right_type(q)|, if possible@>;
18355   link(q)=pp; left_y(pp)=y;
18356   if ( t!=mp_open ) { left_x(pp)=x; left_type(pp)=t;  };
18357 }
18358 q=qq;
18359 }
18360
18361 @ @<Plug an opening in |right_type(q)|...@>=
18362 if ( right_type(q)==mp_open ) {
18363   if ( (left_type(q)==mp_curl)||(left_type(q)==mp_given) ) {
18364     right_type(q)=left_type(q); right_given(q)=left_given(q);
18365   }
18366 }
18367
18368 @ @<Plug an opening in |right_type(pp)|...@>=
18369 if ( right_type(pp)==mp_open ) {
18370   if ( (t==mp_curl)||(t==mp_given) ) {
18371     right_type(pp)=t; right_given(pp)=x;
18372   }
18373 }
18374
18375 @ @<Splice independent paths together@>=
18376
18377   if ( left_type(q)==mp_open ) if ( right_type(q)==mp_open ) {
18378     left_type(q)=mp_curl; left_curl(q)=unity;
18379   }
18380   if ( right_type(pp)==mp_open ) if ( t==mp_open ) {
18381     right_type(pp)=mp_curl; right_curl(pp)=unity;
18382   }
18383   right_type(q)=right_type(pp); link(q)=link(pp);
18384   right_x(q)=right_x(pp); right_y(q)=right_y(pp);
18385   mp_free_node(mp, pp,knot_node_size);
18386   if ( qq==pp ) qq=q;
18387 }
18388
18389 @ @<Choose control points for the path...@>=
18390 if ( cycle_hit ) { 
18391   if ( d==ampersand ) p=q;
18392 } else  { 
18393   left_type(p)=mp_endpoint;
18394   if ( right_type(p)==mp_open ) { 
18395     right_type(p)=mp_curl; right_curl(p)=unity;
18396   }
18397   right_type(q)=mp_endpoint;
18398   if ( left_type(q)==mp_open ) { 
18399     left_type(q)=mp_curl; left_curl(q)=unity;
18400   }
18401   link(q)=p;
18402 }
18403 mp_make_choices(mp, p);
18404 mp->cur_type=mp_path_type; mp->cur_exp=p
18405
18406 @ Finally, we sometimes need to scan an expression whose value is
18407 supposed to be either |true_code| or |false_code|.
18408
18409 @<Declare the basic parsing subroutines@>=
18410 void mp_get_boolean (MP mp) { 
18411   mp_get_x_next(mp); mp_scan_expression(mp);
18412   if ( mp->cur_type!=mp_boolean_type ) {
18413     exp_err("Undefined condition will be treated as `false'");
18414 @.Undefined condition...@>
18415     help2("The expression shown above should have had a definite")
18416       ("true-or-false value. I'm changing it to `false'.");
18417     mp_put_get_flush_error(mp, false_code); mp->cur_type=mp_boolean_type;
18418   }
18419 }
18420
18421 @* \[39] Doing the operations.
18422 The purpose of parsing is primarily to permit people to avoid piles of
18423 parentheses. But the real work is done after the structure of an expression
18424 has been recognized; that's when new expressions are generated. We
18425 turn now to the guts of \MP, which handles individual operators that
18426 have come through the parsing mechanism.
18427
18428 We'll start with the easy ones that take no operands, then work our way
18429 up to operators with one and ultimately two arguments. In other words,
18430 we will write the three procedures |do_nullary|, |do_unary|, and |do_binary|
18431 that are invoked periodically by the expression scanners.
18432
18433 First let's make sure that all of the primitive operators are in the
18434 hash table. Although |scan_primary| and its relatives made use of the
18435 \\{cmd} code for these operators, the \\{do} routines base everything
18436 on the \\{mod} code. For example, |do_binary| doesn't care whether the
18437 operation it performs is a |primary_binary| or |secondary_binary|, etc.
18438
18439 @<Put each...@>=
18440 mp_primitive(mp, "true",nullary,true_code);
18441 @:true_}{\&{true} primitive@>
18442 mp_primitive(mp, "false",nullary,false_code);
18443 @:false_}{\&{false} primitive@>
18444 mp_primitive(mp, "nullpicture",nullary,null_picture_code);
18445 @:null_picture_}{\&{nullpicture} primitive@>
18446 mp_primitive(mp, "nullpen",nullary,null_pen_code);
18447 @:null_pen_}{\&{nullpen} primitive@>
18448 mp_primitive(mp, "jobname",nullary,job_name_op);
18449 @:job_name_}{\&{jobname} primitive@>
18450 mp_primitive(mp, "readstring",nullary,read_string_op);
18451 @:read_string_}{\&{readstring} primitive@>
18452 mp_primitive(mp, "pencircle",nullary,pen_circle);
18453 @:pen_circle_}{\&{pencircle} primitive@>
18454 mp_primitive(mp, "normaldeviate",nullary,normal_deviate);
18455 @:normal_deviate_}{\&{normaldeviate} primitive@>
18456 mp_primitive(mp, "readfrom",unary,read_from_op);
18457 @:read_from_}{\&{readfrom} primitive@>
18458 mp_primitive(mp, "closefrom",unary,close_from_op);
18459 @:close_from_}{\&{closefrom} primitive@>
18460 mp_primitive(mp, "odd",unary,odd_op);
18461 @:odd_}{\&{odd} primitive@>
18462 mp_primitive(mp, "known",unary,known_op);
18463 @:known_}{\&{known} primitive@>
18464 mp_primitive(mp, "unknown",unary,unknown_op);
18465 @:unknown_}{\&{unknown} primitive@>
18466 mp_primitive(mp, "not",unary,not_op);
18467 @:not_}{\&{not} primitive@>
18468 mp_primitive(mp, "decimal",unary,decimal);
18469 @:decimal_}{\&{decimal} primitive@>
18470 mp_primitive(mp, "reverse",unary,reverse);
18471 @:reverse_}{\&{reverse} primitive@>
18472 mp_primitive(mp, "makepath",unary,make_path_op);
18473 @:make_path_}{\&{makepath} primitive@>
18474 mp_primitive(mp, "makepen",unary,make_pen_op);
18475 @:make_pen_}{\&{makepen} primitive@>
18476 mp_primitive(mp, "oct",unary,oct_op);
18477 @:oct_}{\&{oct} primitive@>
18478 mp_primitive(mp, "hex",unary,hex_op);
18479 @:hex_}{\&{hex} primitive@>
18480 mp_primitive(mp, "ASCII",unary,ASCII_op);
18481 @:ASCII_}{\&{ASCII} primitive@>
18482 mp_primitive(mp, "char",unary,char_op);
18483 @:char_}{\&{char} primitive@>
18484 mp_primitive(mp, "length",unary,length_op);
18485 @:length_}{\&{length} primitive@>
18486 mp_primitive(mp, "turningnumber",unary,turning_op);
18487 @:turning_number_}{\&{turningnumber} primitive@>
18488 mp_primitive(mp, "xpart",unary,x_part);
18489 @:x_part_}{\&{xpart} primitive@>
18490 mp_primitive(mp, "ypart",unary,y_part);
18491 @:y_part_}{\&{ypart} primitive@>
18492 mp_primitive(mp, "xxpart",unary,xx_part);
18493 @:xx_part_}{\&{xxpart} primitive@>
18494 mp_primitive(mp, "xypart",unary,xy_part);
18495 @:xy_part_}{\&{xypart} primitive@>
18496 mp_primitive(mp, "yxpart",unary,yx_part);
18497 @:yx_part_}{\&{yxpart} primitive@>
18498 mp_primitive(mp, "yypart",unary,yy_part);
18499 @:yy_part_}{\&{yypart} primitive@>
18500 mp_primitive(mp, "redpart",unary,red_part);
18501 @:red_part_}{\&{redpart} primitive@>
18502 mp_primitive(mp, "greenpart",unary,green_part);
18503 @:green_part_}{\&{greenpart} primitive@>
18504 mp_primitive(mp, "bluepart",unary,blue_part);
18505 @:blue_part_}{\&{bluepart} primitive@>
18506 mp_primitive(mp, "cyanpart",unary,cyan_part);
18507 @:cyan_part_}{\&{cyanpart} primitive@>
18508 mp_primitive(mp, "magentapart",unary,magenta_part);
18509 @:magenta_part_}{\&{magentapart} primitive@>
18510 mp_primitive(mp, "yellowpart",unary,yellow_part);
18511 @:yellow_part_}{\&{yellowpart} primitive@>
18512 mp_primitive(mp, "blackpart",unary,black_part);
18513 @:black_part_}{\&{blackpart} primitive@>
18514 mp_primitive(mp, "greypart",unary,grey_part);
18515 @:grey_part_}{\&{greypart} primitive@>
18516 mp_primitive(mp, "colormodel",unary,color_model_part);
18517 @:color_model_part_}{\&{colormodel} primitive@>
18518 mp_primitive(mp, "fontpart",unary,font_part);
18519 @:font_part_}{\&{fontpart} primitive@>
18520 mp_primitive(mp, "textpart",unary,text_part);
18521 @:text_part_}{\&{textpart} primitive@>
18522 mp_primitive(mp, "pathpart",unary,path_part);
18523 @:path_part_}{\&{pathpart} primitive@>
18524 mp_primitive(mp, "penpart",unary,pen_part);
18525 @:pen_part_}{\&{penpart} primitive@>
18526 mp_primitive(mp, "dashpart",unary,dash_part);
18527 @:dash_part_}{\&{dashpart} primitive@>
18528 mp_primitive(mp, "sqrt",unary,sqrt_op);
18529 @:sqrt_}{\&{sqrt} primitive@>
18530 mp_primitive(mp, "mexp",unary,m_exp_op);
18531 @:m_exp_}{\&{mexp} primitive@>
18532 mp_primitive(mp, "mlog",unary,m_log_op);
18533 @:m_log_}{\&{mlog} primitive@>
18534 mp_primitive(mp, "sind",unary,sin_d_op);
18535 @:sin_d_}{\&{sind} primitive@>
18536 mp_primitive(mp, "cosd",unary,cos_d_op);
18537 @:cos_d_}{\&{cosd} primitive@>
18538 mp_primitive(mp, "floor",unary,floor_op);
18539 @:floor_}{\&{floor} primitive@>
18540 mp_primitive(mp, "uniformdeviate",unary,uniform_deviate);
18541 @:uniform_deviate_}{\&{uniformdeviate} primitive@>
18542 mp_primitive(mp, "charexists",unary,char_exists_op);
18543 @:char_exists_}{\&{charexists} primitive@>
18544 mp_primitive(mp, "fontsize",unary,font_size);
18545 @:font_size_}{\&{fontsize} primitive@>
18546 mp_primitive(mp, "llcorner",unary,ll_corner_op);
18547 @:ll_corner_}{\&{llcorner} primitive@>
18548 mp_primitive(mp, "lrcorner",unary,lr_corner_op);
18549 @:lr_corner_}{\&{lrcorner} primitive@>
18550 mp_primitive(mp, "ulcorner",unary,ul_corner_op);
18551 @:ul_corner_}{\&{ulcorner} primitive@>
18552 mp_primitive(mp, "urcorner",unary,ur_corner_op);
18553 @:ur_corner_}{\&{urcorner} primitive@>
18554 mp_primitive(mp, "arclength",unary,arc_length);
18555 @:arc_length_}{\&{arclength} primitive@>
18556 mp_primitive(mp, "angle",unary,angle_op);
18557 @:angle_}{\&{angle} primitive@>
18558 mp_primitive(mp, "cycle",cycle,cycle_op);
18559 @:cycle_}{\&{cycle} primitive@>
18560 mp_primitive(mp, "stroked",unary,stroked_op);
18561 @:stroked_}{\&{stroked} primitive@>
18562 mp_primitive(mp, "filled",unary,filled_op);
18563 @:filled_}{\&{filled} primitive@>
18564 mp_primitive(mp, "textual",unary,textual_op);
18565 @:textual_}{\&{textual} primitive@>
18566 mp_primitive(mp, "clipped",unary,clipped_op);
18567 @:clipped_}{\&{clipped} primitive@>
18568 mp_primitive(mp, "bounded",unary,bounded_op);
18569 @:bounded_}{\&{bounded} primitive@>
18570 mp_primitive(mp, "+",plus_or_minus,plus);
18571 @:+ }{\.{+} primitive@>
18572 mp_primitive(mp, "-",plus_or_minus,minus);
18573 @:- }{\.{-} primitive@>
18574 mp_primitive(mp, "*",secondary_binary,times);
18575 @:* }{\.{*} primitive@>
18576 mp_primitive(mp, "/",slash,over); mp->eqtb[frozen_slash]=mp->eqtb[mp->cur_sym];
18577 @:/ }{\.{/} primitive@>
18578 mp_primitive(mp, "++",tertiary_binary,pythag_add);
18579 @:++_}{\.{++} primitive@>
18580 mp_primitive(mp, "+-+",tertiary_binary,pythag_sub);
18581 @:+-+_}{\.{+-+} primitive@>
18582 mp_primitive(mp, "or",tertiary_binary,or_op);
18583 @:or_}{\&{or} primitive@>
18584 mp_primitive(mp, "and",and_command,and_op);
18585 @:and_}{\&{and} primitive@>
18586 mp_primitive(mp, "<",expression_binary,less_than);
18587 @:< }{\.{<} primitive@>
18588 mp_primitive(mp, "<=",expression_binary,less_or_equal);
18589 @:<=_}{\.{<=} primitive@>
18590 mp_primitive(mp, ">",expression_binary,greater_than);
18591 @:> }{\.{>} primitive@>
18592 mp_primitive(mp, ">=",expression_binary,greater_or_equal);
18593 @:>=_}{\.{>=} primitive@>
18594 mp_primitive(mp, "=",equals,equal_to);
18595 @:= }{\.{=} primitive@>
18596 mp_primitive(mp, "<>",expression_binary,unequal_to);
18597 @:<>_}{\.{<>} primitive@>
18598 mp_primitive(mp, "substring",primary_binary,substring_of);
18599 @:substring_}{\&{substring} primitive@>
18600 mp_primitive(mp, "subpath",primary_binary,subpath_of);
18601 @:subpath_}{\&{subpath} primitive@>
18602 mp_primitive(mp, "directiontime",primary_binary,direction_time_of);
18603 @:direction_time_}{\&{directiontime} primitive@>
18604 mp_primitive(mp, "point",primary_binary,point_of);
18605 @:point_}{\&{point} primitive@>
18606 mp_primitive(mp, "precontrol",primary_binary,precontrol_of);
18607 @:precontrol_}{\&{precontrol} primitive@>
18608 mp_primitive(mp, "postcontrol",primary_binary,postcontrol_of);
18609 @:postcontrol_}{\&{postcontrol} primitive@>
18610 mp_primitive(mp, "penoffset",primary_binary,pen_offset_of);
18611 @:pen_offset_}{\&{penoffset} primitive@>
18612 mp_primitive(mp, "arctime",primary_binary,arc_time_of);
18613 @:arc_time_of_}{\&{arctime} primitive@>
18614 mp_primitive(mp, "mpversion",nullary,mp_version);
18615 @:mp_verison_}{\&{mpversion} primitive@>
18616 mp_primitive(mp, "&",ampersand,concatenate);
18617 @:!!!}{\.{\&} primitive@>
18618 mp_primitive(mp, "rotated",secondary_binary,rotated_by);
18619 @:rotated_}{\&{rotated} primitive@>
18620 mp_primitive(mp, "slanted",secondary_binary,slanted_by);
18621 @:slanted_}{\&{slanted} primitive@>
18622 mp_primitive(mp, "scaled",secondary_binary,scaled_by);
18623 @:scaled_}{\&{scaled} primitive@>
18624 mp_primitive(mp, "shifted",secondary_binary,shifted_by);
18625 @:shifted_}{\&{shifted} primitive@>
18626 mp_primitive(mp, "transformed",secondary_binary,transformed_by);
18627 @:transformed_}{\&{transformed} primitive@>
18628 mp_primitive(mp, "xscaled",secondary_binary,x_scaled);
18629 @:x_scaled_}{\&{xscaled} primitive@>
18630 mp_primitive(mp, "yscaled",secondary_binary,y_scaled);
18631 @:y_scaled_}{\&{yscaled} primitive@>
18632 mp_primitive(mp, "zscaled",secondary_binary,z_scaled);
18633 @:z_scaled_}{\&{zscaled} primitive@>
18634 mp_primitive(mp, "infont",secondary_binary,in_font);
18635 @:in_font_}{\&{infont} primitive@>
18636 mp_primitive(mp, "intersectiontimes",tertiary_binary,intersect);
18637 @:intersection_times_}{\&{intersectiontimes} primitive@>
18638 mp_primitive(mp, "envelope",primary_binary,envelope_of);
18639 @:envelope_}{\&{envelope} primitive@>
18640
18641 @ @<Cases of |print_cmd...@>=
18642 case nullary:
18643 case unary:
18644 case primary_binary:
18645 case secondary_binary:
18646 case tertiary_binary:
18647 case expression_binary:
18648 case cycle:
18649 case plus_or_minus:
18650 case slash:
18651 case ampersand:
18652 case equals:
18653 case and_command:
18654   mp_print_op(mp, m);
18655   break;
18656
18657 @ OK, let's look at the simplest \\{do} procedure first.
18658
18659 @c @<Declare nullary action procedure@>
18660 void mp_do_nullary (MP mp,quarterword c) { 
18661   check_arith;
18662   if ( mp->internal[mp_tracing_commands]>two )
18663     mp_show_cmd_mod(mp, nullary,c);
18664   switch (c) {
18665   case true_code: case false_code: 
18666     mp->cur_type=mp_boolean_type; mp->cur_exp=c;
18667     break;
18668   case null_picture_code: 
18669     mp->cur_type=mp_picture_type;
18670     mp->cur_exp=mp_get_node(mp, edge_header_size); 
18671     mp_init_edges(mp, mp->cur_exp);
18672     break;
18673   case null_pen_code: 
18674     mp->cur_type=mp_pen_type; mp->cur_exp=mp_get_pen_circle(mp, 0);
18675     break;
18676   case normal_deviate: 
18677     mp->cur_type=mp_known; mp->cur_exp=mp_norm_rand(mp);
18678     break;
18679   case pen_circle: 
18680     mp->cur_type=mp_pen_type; mp->cur_exp=mp_get_pen_circle(mp, unity);
18681     break;
18682   case job_name_op:  
18683     if ( mp->job_name==NULL ) mp_open_log_file(mp);
18684     mp->cur_type=mp_string_type; mp->cur_exp=rts(mp->job_name);
18685     break;
18686   case mp_version: 
18687     mp->cur_type=mp_string_type; 
18688     mp->cur_exp=intern(metapost_version) ;
18689     break;
18690   case read_string_op:
18691     @<Read a string from the terminal@>;
18692     break;
18693   } /* there are no other cases */
18694   check_arith;
18695 }
18696
18697 @ @<Read a string...@>=
18698
18699   if ( mp->interaction<=mp_nonstop_mode )
18700     mp_fatal_error(mp, "*** (cannot readstring in nonstop modes)");
18701   mp_begin_file_reading(mp); name=is_read;
18702   limit=start; prompt_input("");
18703   mp_finish_read(mp);
18704 }
18705
18706 @ @<Declare nullary action procedure@>=
18707 void mp_finish_read (MP mp) { /* copy |buffer| line to |cur_exp| */
18708   size_t k;
18709   str_room((int)mp->last-start);
18710   for (k=start;k<=mp->last-1;k++) {
18711    append_char(mp->buffer[k]);
18712   }
18713   mp_end_file_reading(mp); mp->cur_type=mp_string_type; 
18714   mp->cur_exp=mp_make_string(mp);
18715 }
18716
18717 @ Things get a bit more interesting when there's an operand. The
18718 operand to |do_unary| appears in |cur_type| and |cur_exp|.
18719
18720 @c @<Declare unary action procedures@>
18721 void mp_do_unary (MP mp,quarterword c) {
18722   pointer p,q,r; /* for list manipulation */
18723   integer x; /* a temporary register */
18724   check_arith;
18725   if ( mp->internal[mp_tracing_commands]>two )
18726     @<Trace the current unary operation@>;
18727   switch (c) {
18728   case plus:
18729     if ( mp->cur_type<mp_color_type ) mp_bad_unary(mp, plus);
18730     break;
18731   case minus:
18732     @<Negate the current expression@>;
18733     break;
18734   @<Additional cases of unary operators@>;
18735   } /* there are no other cases */
18736   check_arith;
18737 }
18738
18739 @ The |nice_pair| function returns |true| if both components of a pair
18740 are known.
18741
18742 @<Declare unary action procedures@>=
18743 boolean mp_nice_pair (MP mp,integer p, quarterword t) { 
18744   if ( t==mp_pair_type ) {
18745     p=value(p);
18746     if ( type(x_part_loc(p))==mp_known )
18747       if ( type(y_part_loc(p))==mp_known )
18748         return true;
18749   }
18750   return false;
18751 }
18752
18753 @ The |nice_color_or_pair| function is analogous except that it also accepts
18754 fully known colors.
18755
18756 @<Declare unary action procedures@>=
18757 boolean mp_nice_color_or_pair (MP mp,integer p, quarterword t) {
18758   pointer q,r; /* for scanning the big node */
18759   if ( (t!=mp_pair_type)&&(t!=mp_color_type)&&(t!=mp_cmykcolor_type) ) {
18760     return false;
18761   } else { 
18762     q=value(p);
18763     r=q+mp->big_node_size[type(p)];
18764     do {  
18765       r=r-2;
18766       if ( type(r)!=mp_known )
18767         return false;
18768     } while (r!=q);
18769     return true;
18770   }
18771 }
18772
18773 @ @<Declare unary action...@>=
18774 void mp_print_known_or_unknown_type (MP mp,small_number t, integer v) { 
18775   mp_print_char(mp, '(');
18776   if ( t>mp_known ) mp_print(mp, "unknown numeric");
18777   else { if ( (t==mp_pair_type)||(t==mp_color_type)||(t==mp_cmykcolor_type) )
18778     if ( ! mp_nice_color_or_pair(mp, v,t) ) mp_print(mp, "unknown ");
18779     mp_print_type(mp, t);
18780   }
18781   mp_print_char(mp, ')');
18782 }
18783
18784 @ @<Declare unary action...@>=
18785 void mp_bad_unary (MP mp,quarterword c) { 
18786   exp_err("Not implemented: "); mp_print_op(mp, c);
18787 @.Not implemented...@>
18788   mp_print_known_or_unknown_type(mp, mp->cur_type,mp->cur_exp);
18789   help3("I'm afraid I don't know how to apply that operation to that")
18790     ("particular type. Continue, and I'll simply return the")
18791     ("argument (shown above) as the result of the operation.");
18792   mp_put_get_error(mp);
18793 }
18794
18795 @ @<Trace the current unary operation@>=
18796
18797   mp_begin_diagnostic(mp); mp_print_nl(mp, "{"); 
18798   mp_print_op(mp, c); mp_print_char(mp, '(');
18799   mp_print_exp(mp, null,0); /* show the operand, but not verbosely */
18800   mp_print(mp, ")}"); mp_end_diagnostic(mp, false);
18801 }
18802
18803 @ Negation is easy except when the current expression
18804 is of type |independent|, or when it is a pair with one or more
18805 |independent| components.
18806
18807 It is tempting to argue that the negative of an independent variable
18808 is an independent variable, hence we don't have to do anything when
18809 negating it. The fallacy is that other dependent variables pointing
18810 to the current expression must change the sign of their
18811 coefficients if we make no change to the current expression.
18812
18813 Instead, we work around the problem by copying the current expression
18814 and recycling it afterwards (cf.~the |stash_in| routine).
18815
18816 @<Negate the current expression@>=
18817 switch (mp->cur_type) {
18818 case mp_color_type:
18819 case mp_cmykcolor_type:
18820 case mp_pair_type:
18821 case mp_independent: 
18822   q=mp->cur_exp; mp_make_exp_copy(mp, q);
18823   if ( mp->cur_type==mp_dependent ) {
18824     mp_negate_dep_list(mp, dep_list(mp->cur_exp));
18825   } else if ( mp->cur_type<=mp_pair_type ) { /* |mp_color_type| or |mp_pair_type| */
18826     p=value(mp->cur_exp);
18827     r=p+mp->big_node_size[mp->cur_type];
18828     do {  
18829       r=r-2;
18830       if ( type(r)==mp_known ) negate(value(r));
18831       else mp_negate_dep_list(mp, dep_list(r));
18832     } while (r!=p);
18833   } /* if |cur_type=mp_known| then |cur_exp=0| */
18834   mp_recycle_value(mp, q); mp_free_node(mp, q,value_node_size);
18835   break;
18836 case mp_dependent:
18837 case mp_proto_dependent:
18838   mp_negate_dep_list(mp, dep_list(mp->cur_exp));
18839   break;
18840 case mp_known:
18841   negate(mp->cur_exp);
18842   break;
18843 default:
18844   mp_bad_unary(mp, minus);
18845   break;
18846 }
18847
18848 @ @<Declare unary action...@>=
18849 void mp_negate_dep_list (MP mp,pointer p) { 
18850   while (1) { 
18851     negate(value(p));
18852     if ( info(p)==null ) return;
18853     p=link(p);
18854   }
18855 }
18856
18857 @ @<Additional cases of unary operators@>=
18858 case not_op: 
18859   if ( mp->cur_type!=mp_boolean_type ) mp_bad_unary(mp, not_op);
18860   else mp->cur_exp=true_code+false_code-mp->cur_exp;
18861   break;
18862
18863 @ @d three_sixty_units 23592960 /* that's |360*unity| */
18864 @d boolean_reset(A) if ( (A) ) mp->cur_exp=true_code; else mp->cur_exp=false_code
18865
18866 @<Additional cases of unary operators@>=
18867 case sqrt_op:
18868 case m_exp_op:
18869 case m_log_op:
18870 case sin_d_op:
18871 case cos_d_op:
18872 case floor_op:
18873 case  uniform_deviate:
18874 case odd_op:
18875 case char_exists_op:
18876   if ( mp->cur_type!=mp_known ) {
18877     mp_bad_unary(mp, c);
18878   } else {
18879     switch (c) {
18880     case sqrt_op:mp->cur_exp=mp_square_rt(mp, mp->cur_exp);break;
18881     case m_exp_op:mp->cur_exp=mp_m_exp(mp, mp->cur_exp);break;
18882     case m_log_op:mp->cur_exp=mp_m_log(mp, mp->cur_exp);break;
18883     case sin_d_op:
18884     case cos_d_op:
18885       mp_n_sin_cos(mp, (mp->cur_exp % three_sixty_units)*16);
18886       if ( c==sin_d_op ) mp->cur_exp=mp_round_fraction(mp, mp->n_sin);
18887       else mp->cur_exp=mp_round_fraction(mp, mp->n_cos);
18888       break;
18889     case floor_op:mp->cur_exp=mp_floor_scaled(mp, mp->cur_exp);break;
18890     case uniform_deviate:mp->cur_exp=mp_unif_rand(mp, mp->cur_exp);break;
18891     case odd_op: 
18892       boolean_reset(odd(mp_round_unscaled(mp, mp->cur_exp)));
18893       mp->cur_type=mp_boolean_type;
18894       break;
18895     case char_exists_op:
18896       @<Determine if a character has been shipped out@>;
18897       break;
18898     } /* there are no other cases */
18899   }
18900   break;
18901
18902 @ @<Additional cases of unary operators@>=
18903 case angle_op:
18904   if ( mp_nice_pair(mp, mp->cur_exp,mp->cur_type) ) {
18905     p=value(mp->cur_exp);
18906     x=mp_n_arg(mp, value(x_part_loc(p)),value(y_part_loc(p)));
18907     if ( x>=0 ) mp_flush_cur_exp(mp, (x+8)/ 16);
18908     else mp_flush_cur_exp(mp, -((-x+8)/ 16));
18909   } else {
18910     mp_bad_unary(mp, angle_op);
18911   }
18912   break;
18913
18914 @ If the current expression is a pair, but the context wants it to
18915 be a path, we call |pair_to_path|.
18916
18917 @<Declare unary action...@>=
18918 void mp_pair_to_path (MP mp) { 
18919   mp->cur_exp=mp_new_knot(mp); 
18920   mp->cur_type=mp_path_type;
18921 }
18922
18923
18924 @d pict_color_type(A) ((link(dummy_loc(mp->cur_exp))!=null) &&
18925                        (has_color(link(dummy_loc(mp->cur_exp)))) &&
18926                        ((color_model(link(dummy_loc(mp->cur_exp)))==A)
18927                         ||
18928                         (color_model(link(dummy_loc(mp->cur_exp)))==mp_uninitialized_model) &&
18929                         (mp->internal[mp_default_color_model]/unity)==(A)))
18930
18931 @<Additional cases of unary operators@>=
18932 case x_part:
18933 case y_part:
18934   if ( (mp->cur_type==mp_pair_type)||(mp->cur_type==mp_transform_type) )
18935     mp_take_part(mp, c);
18936   else if ( mp->cur_type==mp_picture_type ) mp_take_pict_part(mp, c);
18937   else mp_bad_unary(mp, c);
18938   break;
18939 case xx_part:
18940 case xy_part:
18941 case yx_part:
18942 case yy_part: 
18943   if ( mp->cur_type==mp_transform_type ) mp_take_part(mp, c);
18944   else if ( mp->cur_type==mp_picture_type ) mp_take_pict_part(mp, c);
18945   else mp_bad_unary(mp, c);
18946   break;
18947 case red_part:
18948 case green_part:
18949 case blue_part: 
18950   if ( mp->cur_type==mp_color_type ) mp_take_part(mp, c);
18951   else if ( mp->cur_type==mp_picture_type ) {
18952     if pict_color_type(mp_rgb_model) mp_take_pict_part(mp, c);
18953     else mp_bad_color_part(mp, c);
18954   }
18955   else mp_bad_unary(mp, c);
18956   break;
18957 case cyan_part:
18958 case magenta_part:
18959 case yellow_part:
18960 case black_part: 
18961   if ( mp->cur_type==mp_cmykcolor_type) mp_take_part(mp, c); 
18962   else if ( mp->cur_type==mp_picture_type ) {
18963     if pict_color_type(mp_cmyk_model) mp_take_pict_part(mp, c);
18964     else mp_bad_color_part(mp, c);
18965   }
18966   else mp_bad_unary(mp, c);
18967   break;
18968 case grey_part: 
18969   if ( mp->cur_type==mp_known ) mp->cur_exp=value(c);
18970   else if ( mp->cur_type==mp_picture_type ) {
18971     if pict_color_type(mp_grey_model) mp_take_pict_part(mp, c);
18972     else mp_bad_color_part(mp, c);
18973   }
18974   else mp_bad_unary(mp, c);
18975   break;
18976 case color_model_part: 
18977   if ( mp->cur_type==mp_picture_type ) mp_take_pict_part(mp, c);
18978   else mp_bad_unary(mp, c);
18979   break;
18980
18981 @ @<Declarations@>=
18982 void mp_bad_color_part(MP mp, quarterword c);
18983
18984 @ @c
18985 void mp_bad_color_part(MP mp, quarterword c) {
18986   pointer p; /* the big node */
18987   p=link(dummy_loc(mp->cur_exp));
18988   exp_err("Wrong picture color model: "); mp_print_op(mp, c);
18989 @.Wrong picture color model...@>
18990   if (color_model(p)==mp_grey_model)
18991     mp_print(mp, " of grey object");
18992   else if (color_model(p)==mp_cmyk_model)
18993     mp_print(mp, " of cmyk object");
18994   else if (color_model(p)==mp_rgb_model)
18995     mp_print(mp, " of rgb object");
18996   else if (color_model(p)==mp_no_model) 
18997     mp_print(mp, " of marking object");
18998   else 
18999     mp_print(mp," of defaulted object");
19000   help3("You can only ask for the redpart, greenpart, bluepart of a rgb object,")
19001     ("the cyanpart, magentapart, yellowpart or blackpart of a cmyk object, ")
19002     ("or the greypart of a grey object. No mixing and matching, please.");
19003   mp_error(mp);
19004   if (c==black_part)
19005     mp_flush_cur_exp(mp,unity);
19006   else
19007     mp_flush_cur_exp(mp,0);
19008 }
19009
19010 @ In the following procedure, |cur_exp| points to a capsule, which points to
19011 a big node. We want to delete all but one part of the big node.
19012
19013 @<Declare unary action...@>=
19014 void mp_take_part (MP mp,quarterword c) {
19015   pointer p; /* the big node */
19016   p=value(mp->cur_exp); value(temp_val)=p; type(temp_val)=mp->cur_type;
19017   link(p)=temp_val; mp_free_node(mp, mp->cur_exp,value_node_size);
19018   mp_make_exp_copy(mp, p+mp->sector_offset[c+mp_x_part_sector-x_part]);
19019   mp_recycle_value(mp, temp_val);
19020 }
19021
19022 @ @<Initialize table entries...@>=
19023 name_type(temp_val)=mp_capsule;
19024
19025 @ @<Additional cases of unary operators@>=
19026 case font_part:
19027 case text_part:
19028 case path_part:
19029 case pen_part:
19030 case dash_part:
19031   if ( mp->cur_type==mp_picture_type ) mp_take_pict_part(mp, c);
19032   else mp_bad_unary(mp, c);
19033   break;
19034
19035 @ @<Declarations@>=
19036 void mp_scale_edges (MP mp);
19037
19038 @ @<Declare unary action...@>=
19039 void mp_take_pict_part (MP mp,quarterword c) {
19040   pointer p; /* first graphical object in |cur_exp| */
19041   p=link(dummy_loc(mp->cur_exp));
19042   if ( p!=null ) {
19043     switch (c) {
19044     case x_part: case y_part: case xx_part:
19045     case xy_part: case yx_part: case yy_part:
19046       if ( type(p)==mp_text_code ) mp_flush_cur_exp(mp, text_trans_part(p+c));
19047       else goto NOT_FOUND;
19048       break;
19049     case red_part: case green_part: case blue_part:
19050       if ( has_color(p) ) mp_flush_cur_exp(mp, obj_color_part(p+c));
19051       else goto NOT_FOUND;
19052       break;
19053     case cyan_part: case magenta_part: case yellow_part:
19054     case black_part:
19055       if ( has_color(p) ) {
19056         if ( color_model(p)==mp_uninitialized_model && c==black_part)
19057           mp_flush_cur_exp(mp, unity);
19058         else
19059           mp_flush_cur_exp(mp, obj_color_part(p+c+(red_part-cyan_part)));
19060       } else goto NOT_FOUND;
19061       break;
19062     case grey_part:
19063       if ( has_color(p) )
19064           mp_flush_cur_exp(mp, obj_color_part(p+c+(red_part-grey_part)));
19065       else goto NOT_FOUND;
19066       break;
19067     case color_model_part:
19068       if ( has_color(p) ) {
19069         if ( color_model(p)==mp_uninitialized_model )
19070           mp_flush_cur_exp(mp, mp->internal[mp_default_color_model]);
19071         else
19072           mp_flush_cur_exp(mp, color_model(p)*unity);
19073       } else goto NOT_FOUND;
19074       break;
19075     @<Handle other cases in |take_pict_part| or |goto not_found|@>;
19076     } /* all cases have been enumerated */
19077     return;
19078   };
19079 NOT_FOUND:
19080   @<Convert the current expression to a null value appropriate
19081     for |c|@>;
19082 }
19083
19084 @ @<Handle other cases in |take_pict_part| or |goto not_found|@>=
19085 case text_part: 
19086   if ( type(p)!=mp_text_code ) goto NOT_FOUND;
19087   else { 
19088     mp_flush_cur_exp(mp, text_p(p));
19089     add_str_ref(mp->cur_exp);
19090     mp->cur_type=mp_string_type;
19091     };
19092   break;
19093 case font_part: 
19094   if ( type(p)!=mp_text_code ) goto NOT_FOUND;
19095   else { 
19096     mp_flush_cur_exp(mp, rts(mp->font_name[font_n(p)])); 
19097     add_str_ref(mp->cur_exp);
19098     mp->cur_type=mp_string_type;
19099   };
19100   break;
19101 case path_part:
19102   if ( type(p)==mp_text_code ) goto NOT_FOUND;
19103   else if ( is_stop(p) ) mp_confusion(mp, "pict");
19104 @:this can't happen pict}{\quad pict@>
19105   else { 
19106     mp_flush_cur_exp(mp, mp_copy_path(mp, path_p(p)));
19107     mp->cur_type=mp_path_type;
19108   }
19109   break;
19110 case pen_part: 
19111   if ( ! has_pen(p) ) goto NOT_FOUND;
19112   else {
19113     if ( pen_p(p)==null ) goto NOT_FOUND;
19114     else { mp_flush_cur_exp(mp, copy_pen(pen_p(p)));
19115       mp->cur_type=mp_pen_type;
19116     };
19117   }
19118   break;
19119 case dash_part: 
19120   if ( type(p)!=mp_stroked_code ) goto NOT_FOUND;
19121   else { if ( dash_p(p)==null ) goto NOT_FOUND;
19122     else { add_edge_ref(dash_p(p));
19123     mp->se_sf=dash_scale(p);
19124     mp->se_pic=dash_p(p);
19125     mp_scale_edges(mp);
19126     mp_flush_cur_exp(mp, mp->se_pic);
19127     mp->cur_type=mp_picture_type;
19128     };
19129   }
19130   break;
19131
19132 @ Since |scale_edges| had to be declared |forward|, it had to be declared as a
19133 parameterless procedure even though it really takes two arguments and updates
19134 one of them.  Hence the following globals are needed.
19135
19136 @<Global...@>=
19137 pointer se_pic;  /* edge header used and updated by |scale_edges| */
19138 scaled se_sf;  /* the scale factor argument to |scale_edges| */
19139
19140 @ @<Convert the current expression to a null value appropriate...@>=
19141 switch (c) {
19142 case text_part: case font_part: 
19143   mp_flush_cur_exp(mp, rts(""));
19144   mp->cur_type=mp_string_type;
19145   break;
19146 case path_part: 
19147   mp_flush_cur_exp(mp, mp_get_node(mp, knot_node_size));
19148   left_type(mp->cur_exp)=mp_endpoint;
19149   right_type(mp->cur_exp)=mp_endpoint;
19150   link(mp->cur_exp)=mp->cur_exp;
19151   x_coord(mp->cur_exp)=0;
19152   y_coord(mp->cur_exp)=0;
19153   originator(mp->cur_exp)=mp_metapost_user;
19154   mp->cur_type=mp_path_type;
19155   break;
19156 case pen_part: 
19157   mp_flush_cur_exp(mp, mp_get_pen_circle(mp, 0));
19158   mp->cur_type=mp_pen_type;
19159   break;
19160 case dash_part: 
19161   mp_flush_cur_exp(mp, mp_get_node(mp, edge_header_size));
19162   mp_init_edges(mp, mp->cur_exp);
19163   mp->cur_type=mp_picture_type;
19164   break;
19165 default: 
19166    mp_flush_cur_exp(mp, 0);
19167   break;
19168 }
19169
19170 @ @<Additional cases of unary...@>=
19171 case char_op: 
19172   if ( mp->cur_type!=mp_known ) { 
19173     mp_bad_unary(mp, char_op);
19174   } else { 
19175     mp->cur_exp=mp_round_unscaled(mp, mp->cur_exp) % 256; 
19176     mp->cur_type=mp_string_type;
19177     if ( mp->cur_exp<0 ) mp->cur_exp=mp->cur_exp+256;
19178   }
19179   break;
19180 case decimal: 
19181   if ( mp->cur_type!=mp_known ) {
19182      mp_bad_unary(mp, decimal);
19183   } else { 
19184     mp->old_setting=mp->selector; mp->selector=new_string;
19185     mp_print_scaled(mp, mp->cur_exp); mp->cur_exp=mp_make_string(mp);
19186     mp->selector=mp->old_setting; mp->cur_type=mp_string_type;
19187   }
19188   break;
19189 case oct_op:
19190 case hex_op:
19191 case ASCII_op: 
19192   if ( mp->cur_type!=mp_string_type ) mp_bad_unary(mp, c);
19193   else mp_str_to_num(mp, c);
19194   break;
19195 case font_size: 
19196   if ( mp->cur_type!=mp_string_type ) mp_bad_unary(mp, font_size);
19197   else @<Find the design size of the font whose name is |cur_exp|@>;
19198   break;
19199
19200 @ @<Declare unary action...@>=
19201 void mp_str_to_num (MP mp,quarterword c) { /* converts a string to a number */
19202   integer n; /* accumulator */
19203   ASCII_code m; /* current character */
19204   pool_pointer k; /* index into |str_pool| */
19205   int b; /* radix of conversion */
19206   boolean bad_char; /* did the string contain an invalid digit? */
19207   if ( c==ASCII_op ) {
19208     if ( length(mp->cur_exp)==0 ) n=-1;
19209     else n=mp->str_pool[mp->str_start[mp->cur_exp]];
19210   } else { 
19211     if ( c==oct_op ) b=8; else b=16;
19212     n=0; bad_char=false;
19213     for (k=mp->str_start[mp->cur_exp];k<=str_stop(mp->cur_exp)-1;k++) {
19214       m=mp->str_pool[k];
19215       if ( (m>='0')&&(m<='9') ) m=m-'0';
19216       else if ( (m>='A')&&(m<='F') ) m=m-'A'+10;
19217       else if ( (m>='a')&&(m<='f') ) m=m-'a'+10;
19218       else  { bad_char=true; m=0; };
19219       if ( m>=b ) { bad_char=true; m=0; };
19220       if ( n<32768 / b ) n=n*b+m; else n=32767;
19221     }
19222     @<Give error messages if |bad_char| or |n>=4096|@>;
19223   }
19224   mp_flush_cur_exp(mp, n*unity);
19225 }
19226
19227 @ @<Give error messages if |bad_char|...@>=
19228 if ( bad_char ) { 
19229   exp_err("String contains illegal digits");
19230 @.String contains illegal digits@>
19231   if ( c==oct_op ) {
19232     help1("I zeroed out characters that weren't in the range 0..7.");
19233   } else  {
19234     help1("I zeroed out characters that weren't hex digits.");
19235   }
19236   mp_put_get_error(mp);
19237 }
19238 if ( (n>4095) ) {
19239   if ( mp->internal[mp_warning_check]>0 ) {
19240     print_err("Number too large ("); 
19241     mp_print_int(mp, n); mp_print_char(mp, ')');
19242 @.Number too large@>
19243     help2("I have trouble with numbers greater than 4095; watch out.")
19244       ("(Set warningcheck:=0 to suppress this message.)");
19245     mp_put_get_error(mp);
19246   }
19247 }
19248
19249 @ The length operation is somewhat unusual in that it applies to a variety
19250 of different types of operands.
19251
19252 @<Additional cases of unary...@>=
19253 case length_op: 
19254   switch (mp->cur_type) {
19255   case mp_string_type: mp_flush_cur_exp(mp, length(mp->cur_exp)*unity); break;
19256   case mp_path_type: mp_flush_cur_exp(mp, mp_path_length(mp)); break;
19257   case mp_known: mp->cur_exp=abs(mp->cur_exp); break;
19258   case mp_picture_type: mp_flush_cur_exp(mp, mp_pict_length(mp)); break;
19259   default: 
19260     if ( mp_nice_pair(mp, mp->cur_exp,mp->cur_type) )
19261       mp_flush_cur_exp(mp, mp_pyth_add(mp, 
19262         value(x_part_loc(value(mp->cur_exp))),
19263         value(y_part_loc(value(mp->cur_exp)))));
19264     else mp_bad_unary(mp, c);
19265     break;
19266   }
19267   break;
19268
19269 @ @<Declare unary action...@>=
19270 scaled mp_path_length (MP mp) { /* computes the length of the current path */
19271   scaled n; /* the path length so far */
19272   pointer p; /* traverser */
19273   p=mp->cur_exp;
19274   if ( left_type(p)==mp_endpoint ) n=-unity; else n=0;
19275   do {  p=link(p); n=n+unity; } while (p!=mp->cur_exp);
19276   return n;
19277 }
19278
19279 @ @<Declare unary action...@>=
19280 scaled mp_pict_length (MP mp) { 
19281   /* counts interior components in picture |cur_exp| */
19282   scaled n; /* the count so far */
19283   pointer p; /* traverser */
19284   n=0;
19285   p=link(dummy_loc(mp->cur_exp));
19286   if ( p!=null ) {
19287     if ( is_start_or_stop(p) )
19288       if ( mp_skip_1component(mp, p)==null ) p=link(p);
19289     while ( p!=null )  { 
19290       skip_component(p) return n; 
19291       n=n+unity;   
19292     }
19293   }
19294   return n;
19295 }
19296
19297 @ Implement |turningnumber|
19298
19299 @<Additional cases of unary...@>=
19300 case turning_op:
19301   if ( mp->cur_type==mp_pair_type ) mp_flush_cur_exp(mp, 0);
19302   else if ( mp->cur_type!=mp_path_type ) mp_bad_unary(mp, turning_op);
19303   else if ( left_type(mp->cur_exp)==mp_endpoint )
19304      mp_flush_cur_exp(mp, 0); /* not a cyclic path */
19305   else
19306     mp_flush_cur_exp(mp, mp_turn_cycles_wrapper(mp, mp->cur_exp));
19307   break;
19308
19309 @ The function |an_angle| returns the value of the |angle| primitive, or $0$ if the
19310 argument is |origin|.
19311
19312 @<Declare unary action...@>=
19313 angle mp_an_angle (MP mp,scaled xpar, scaled ypar) {
19314   if ( (! ((xpar==0) && (ypar==0))) )
19315     return mp_n_arg(mp, xpar,ypar);
19316   return 0;
19317 }
19318
19319
19320 @ The actual turning number is (for the moment) computed in a C function
19321 that receives eight integers corresponding to the four controlling points,
19322 and returns a single angle.  Besides those, we have to account for discrete
19323 moves at the actual points.
19324
19325 @d floor(a) (a>=0 ? a : -(int)(-a))
19326 @d bezier_error (720<<20)+1
19327 @d sign(v) ((v)>0 ? 1 : ((v)<0 ? -1 : 0 ))
19328 @d print_roots(a) 
19329 @d out ((double)(xo>>20))
19330 @d mid ((double)(xm>>20))
19331 @d in  ((double)(xi>>20))
19332 @d divisor (256*256)
19333 @d double2angle(a) (int)floor(a*256.0*256.0*16.0)
19334
19335 @<Declare unary action...@>=
19336 angle mp_bezier_slope(MP mp, integer AX,integer AY,integer BX,integer BY,
19337             integer CX,integer CY,integer DX,integer DY);
19338
19339 @ @c 
19340 angle mp_bezier_slope(MP mp, integer AX,integer AY,integer BX,integer BY,
19341             integer CX,integer CY,integer DX,integer DY) {
19342   double a, b, c;
19343   integer deltax,deltay;
19344   double ax,ay,bx,by,cx,cy,dx,dy;
19345   angle xi = 0, xo = 0, xm = 0;
19346   double res = 0;
19347   ax=AX/divisor;  ay=AY/divisor;
19348   bx=BX/divisor;  by=BY/divisor;
19349   cx=CX/divisor;  cy=CY/divisor;
19350   dx=DX/divisor;  dy=DY/divisor;
19351
19352   deltax = (BX-AX); deltay = (BY-AY);
19353   if (deltax==0 && deltay == 0) { deltax=(CX-AX); deltay=(CY-AY); }
19354   if (deltax==0 && deltay == 0) { deltax=(DX-AX); deltay=(DY-AY); }
19355   xi = mp_an_angle(mp,deltax,deltay);
19356
19357   deltax = (CX-BX); deltay = (CY-BY);
19358   xm = mp_an_angle(mp,deltax,deltay);
19359
19360   deltax = (DX-CX); deltay = (DY-CY);
19361   if (deltax==0 && deltay == 0) { deltax=(DX-BX); deltay=(DY-BY); }
19362   if (deltax==0 && deltay == 0) { deltax=(DX-AX); deltay=(DY-AY); }
19363   xo = mp_an_angle(mp,deltax,deltay);
19364
19365   a = (bx-ax)*(cy-by) - (cx-bx)*(by-ay); /* a = (bp-ap)x(cp-bp); */
19366   b = (bx-ax)*(dy-cy) - (by-ay)*(dx-cx);; /* b = (bp-ap)x(dp-cp);*/
19367   c = (cx-bx)*(dy-cy) - (dx-cx)*(cy-by); /* c = (cp-bp)x(dp-cp);*/
19368
19369   if ((a==0)&&(c==0)) {
19370     res = (b==0 ?  0 :  (out-in)); 
19371     print_roots("no roots (a)");
19372   } else if ((a==0)||(c==0)) {
19373     if ((sign(b) == sign(a)) || (sign(b) == sign(c))) {
19374       res = out-in; /* ? */
19375       if (res<-180.0) 
19376         res += 360.0;
19377       else if (res>180.0)
19378         res -= 360.0;
19379       print_roots("no roots (b)");
19380     } else {
19381       res = out-in; /* ? */
19382       print_roots("one root (a)");
19383     }
19384   } else if ((sign(a)*sign(c))<0) {
19385     res = out-in; /* ? */
19386       if (res<-180.0) 
19387         res += 360.0;
19388       else if (res>180.0)
19389         res -= 360.0;
19390     print_roots("one root (b)");
19391   } else {
19392     if (sign(a) == sign(b)) {
19393       res = out-in; /* ? */
19394       if (res<-180.0) 
19395         res += 360.0;
19396       else if (res>180.0)
19397         res -= 360.0;
19398       print_roots("no roots (d)");
19399     } else {
19400       if ((b*b) == (4*a*c)) {
19401         res = bezier_error;
19402         print_roots("double root"); /* cusp */
19403       } else if ((b*b) < (4*a*c)) {
19404         res = out-in; /* ? */
19405         if (res<=0.0 &&res>-180.0) 
19406           res += 360.0;
19407         else if (res>=0.0 && res<180.0)
19408           res -= 360.0;
19409         print_roots("no roots (e)");
19410       } else {
19411         res = out-in;
19412         if (res<-180.0) 
19413           res += 360.0;
19414         else if (res>180.0)
19415           res -= 360.0;
19416         print_roots("two roots"); /* two inflections */
19417       }
19418     }
19419   }
19420   return double2angle(res);
19421 }
19422
19423 @
19424 @d p_nextnext link(link(p))
19425 @d p_next link(p)
19426 @d seven_twenty_deg 05500000000 /* $720\cdot2^{20}$, represents $720^\circ$ */
19427
19428 @<Declare unary action...@>=
19429 scaled mp_new_turn_cycles (MP mp,pointer c) {
19430   angle res,ang; /*  the angles of intermediate results  */
19431   scaled turns;  /*  the turn counter  */
19432   pointer p;     /*  for running around the path  */
19433   integer xp,yp;   /*  coordinates of next point  */
19434   integer x,y;   /*  helper coordinates  */
19435   angle in_angle,out_angle;     /*  helper angles */
19436   int old_setting; /* saved |selector| setting */
19437   res=0;
19438   turns= 0;
19439   p=c;
19440   old_setting = mp->selector; mp->selector=term_only;
19441   if ( mp->internal[mp_tracing_commands]>unity ) {
19442     mp_begin_diagnostic(mp);
19443     mp_print_nl(mp, "");
19444     mp_end_diagnostic(mp, false);
19445   }
19446   do { 
19447     xp = x_coord(p_next); yp = y_coord(p_next);
19448     ang  = mp_bezier_slope(mp,x_coord(p), y_coord(p), right_x(p), right_y(p),
19449              left_x(p_next), left_y(p_next), xp, yp);
19450     if ( ang>seven_twenty_deg ) {
19451       print_err("Strange path");
19452       mp_error(mp);
19453       mp->selector=old_setting;
19454       return 0;
19455     }
19456     res  = res + ang;
19457     if ( res > one_eighty_deg ) {
19458       res = res - three_sixty_deg;
19459       turns = turns + unity;
19460     }
19461     if ( res <= -one_eighty_deg ) {
19462       res = res + three_sixty_deg;
19463       turns = turns - unity;
19464     }
19465     /*  incoming angle at next point  */
19466     x = left_x(p_next);  y = left_y(p_next);
19467     if ( (xp==x)&&(yp==y) ) { x = right_x(p);  y = right_y(p);  };
19468     if ( (xp==x)&&(yp==y) ) { x = x_coord(p);  y = y_coord(p);  };
19469     in_angle = mp_an_angle(mp, xp - x, yp - y);
19470     /*  outgoing angle at next point  */
19471     x = right_x(p_next);  y = right_y(p_next);
19472     if ( (xp==x)&&(yp==y) ) { x = left_x(p_nextnext);  y = left_y(p_nextnext);  };
19473     if ( (xp==x)&&(yp==y) ) { x = x_coord(p_nextnext); y = y_coord(p_nextnext); };
19474     out_angle = mp_an_angle(mp, x - xp, y- yp);
19475     ang  = (out_angle - in_angle);
19476     reduce_angle(ang);
19477     if ( ang!=0 ) {
19478       res  = res + ang;
19479       if ( res >= one_eighty_deg ) {
19480         res = res - three_sixty_deg;
19481         turns = turns + unity;
19482       };
19483       if ( res <= -one_eighty_deg ) {
19484         res = res + three_sixty_deg;
19485         turns = turns - unity;
19486       };
19487     };
19488     p = link(p);
19489   } while (p!=c);
19490   mp->selector=old_setting;
19491   return turns;
19492 }
19493
19494
19495 @ This code is based on Bogus\l{}av Jackowski's
19496 |emergency_turningnumber| macro, with some minor changes by Taco
19497 Hoekwater. The macro code looked more like this:
19498 {\obeylines
19499 vardef turning\_number primary p =
19500 ~~save res, ang, turns;
19501 ~~res := 0;
19502 ~~if length p <= 2:
19503 ~~~~if Angle ((point 0 of p) - (postcontrol 0 of p)) >= 0:  1  else: -1 fi
19504 ~~else:
19505 ~~~~for t = 0 upto length p-1 :
19506 ~~~~~~angc := Angle ((point t+1 of p)  - (point t of p))
19507 ~~~~~~~~- Angle ((point t of p) - (point t-1 of p));
19508 ~~~~~~if angc > 180: angc := angc - 360; fi;
19509 ~~~~~~if angc < -180: angc := angc + 360; fi;
19510 ~~~~~~res  := res + angc;
19511 ~~~~endfor;
19512 ~~res/360
19513 ~~fi
19514 enddef;}
19515 The general idea is to calculate only the sum of the angles of
19516 straight lines between the points, of a path, not worrying about cusps
19517 or self-intersections in the segments at all. If the segment is not
19518 well-behaved, the result is not necesarily correct. But the old code
19519 was not always correct either, and worse, it sometimes failed for
19520 well-behaved paths as well. All known bugs that were triggered by the
19521 original code no longer occur with this code, and it runs roughly 3
19522 times as fast because the algorithm is much simpler.
19523
19524 @ It is possible to overflow the return value of the |turn_cycles|
19525 function when the path is sufficiently long and winding, but I am not
19526 going to bother testing for that. In any case, it would only return
19527 the looped result value, which is not a big problem.
19528
19529 The macro code for the repeat loop was a bit nicer to look
19530 at than the pascal code, because it could use |point -1 of p|. In
19531 pascal, the fastest way to loop around the path is not to look
19532 backward once, but forward twice. These defines help hide the trick.
19533
19534 @d p_to link(link(p))
19535 @d p_here link(p)
19536 @d p_from p
19537
19538 @<Declare unary action...@>=
19539 scaled mp_turn_cycles (MP mp,pointer c) {
19540   angle res,ang; /*  the angles of intermediate results  */
19541   scaled turns;  /*  the turn counter  */
19542   pointer p;     /*  for running around the path  */
19543   res=0;  turns= 0; p=c;
19544   do { 
19545     ang  = mp_an_angle (mp, x_coord(p_to) - x_coord(p_here), 
19546                             y_coord(p_to) - y_coord(p_here))
19547         - mp_an_angle (mp, x_coord(p_here) - x_coord(p_from), 
19548                            y_coord(p_here) - y_coord(p_from));
19549     reduce_angle(ang);
19550     res  = res + ang;
19551     if ( res >= three_sixty_deg )  {
19552       res = res - three_sixty_deg;
19553       turns = turns + unity;
19554     };
19555     if ( res <= -three_sixty_deg ) {
19556       res = res + three_sixty_deg;
19557       turns = turns - unity;
19558     };
19559     p = link(p);
19560   } while (p!=c);
19561   return turns;
19562 }
19563
19564 @ @<Declare unary action...@>=
19565 scaled mp_turn_cycles_wrapper (MP mp,pointer c) {
19566   scaled nval,oval;
19567   scaled saved_t_o; /* tracing\_online saved  */
19568   if ( (link(c)==c)||(link(link(c))==c) ) {
19569     if ( mp_an_angle (mp, x_coord(c) - right_x(c),  y_coord(c) - right_y(c)) > 0 )
19570       return unity;
19571     else
19572       return -unity;
19573   } else {
19574     nval = mp_new_turn_cycles(mp, c);
19575     oval = mp_turn_cycles(mp, c);
19576     if ( nval!=oval ) {
19577       saved_t_o=mp->internal[mp_tracing_online];
19578       mp->internal[mp_tracing_online]=unity;
19579       mp_begin_diagnostic(mp);
19580       mp_print_nl (mp, "Warning: the turningnumber algorithms do not agree."
19581                        " The current computed value is ");
19582       mp_print_scaled(mp, nval);
19583       mp_print(mp, ", but the 'connect-the-dots' algorithm returned ");
19584       mp_print_scaled(mp, oval);
19585       mp_end_diagnostic(mp, false);
19586       mp->internal[mp_tracing_online]=saved_t_o;
19587     }
19588     return nval;
19589   }
19590 }
19591
19592 @ @<Declare unary action...@>=
19593 scaled mp_count_turns (MP mp,pointer c) {
19594   pointer p; /* a knot in envelope spec |c| */
19595   integer t; /* total pen offset changes counted */
19596   t=0; p=c;
19597   do {  
19598     t=t+info(p)-zero_off;
19599     p=link(p);
19600   } while (p!=c);
19601   return ((t / 3)*unity);
19602 }
19603
19604 @ @d type_range(A,B) { 
19605   if ( (mp->cur_type>=(A)) && (mp->cur_type<=(B)) ) 
19606     mp_flush_cur_exp(mp, true_code);
19607   else mp_flush_cur_exp(mp, false_code);
19608   mp->cur_type=mp_boolean_type;
19609   }
19610 @d type_test(A) { 
19611   if ( mp->cur_type==(A) ) mp_flush_cur_exp(mp, true_code);
19612   else mp_flush_cur_exp(mp, false_code);
19613   mp->cur_type=mp_boolean_type;
19614   }
19615
19616 @<Additional cases of unary operators@>=
19617 case mp_boolean_type: 
19618   type_range(mp_boolean_type,mp_unknown_boolean); break;
19619 case mp_string_type: 
19620   type_range(mp_string_type,mp_unknown_string); break;
19621 case mp_pen_type: 
19622   type_range(mp_pen_type,mp_unknown_pen); break;
19623 case mp_path_type: 
19624   type_range(mp_path_type,mp_unknown_path); break;
19625 case mp_picture_type: 
19626   type_range(mp_picture_type,mp_unknown_picture); break;
19627 case mp_transform_type: case mp_color_type: case mp_cmykcolor_type:
19628 case mp_pair_type: 
19629   type_test(c); break;
19630 case mp_numeric_type: 
19631   type_range(mp_known,mp_independent); break;
19632 case known_op: case unknown_op: 
19633   mp_test_known(mp, c); break;
19634
19635 @ @<Declare unary action procedures@>=
19636 void mp_test_known (MP mp,quarterword c) {
19637   int b; /* is the current expression known? */
19638   pointer p,q; /* locations in a big node */
19639   b=false_code;
19640   switch (mp->cur_type) {
19641   case mp_vacuous: case mp_boolean_type: case mp_string_type:
19642   case mp_pen_type: case mp_path_type: case mp_picture_type:
19643   case mp_known: 
19644     b=true_code;
19645     break;
19646   case mp_transform_type:
19647   case mp_color_type: case mp_cmykcolor_type: case mp_pair_type: 
19648     p=value(mp->cur_exp);
19649     q=p+mp->big_node_size[mp->cur_type];
19650     do {  
19651       q=q-2;
19652       if ( type(q)!=mp_known ) 
19653        goto DONE;
19654     } while (q!=p);
19655     b=true_code;
19656   DONE:  
19657     break;
19658   default: 
19659     break;
19660   }
19661   if ( c==known_op ) mp_flush_cur_exp(mp, b);
19662   else mp_flush_cur_exp(mp, true_code+false_code-b);
19663   mp->cur_type=mp_boolean_type;
19664 }
19665
19666 @ @<Additional cases of unary operators@>=
19667 case cycle_op: 
19668   if ( mp->cur_type!=mp_path_type ) mp_flush_cur_exp(mp, false_code);
19669   else if ( left_type(mp->cur_exp)!=mp_endpoint ) mp_flush_cur_exp(mp, true_code);
19670   else mp_flush_cur_exp(mp, false_code);
19671   mp->cur_type=mp_boolean_type;
19672   break;
19673
19674 @ @<Additional cases of unary operators@>=
19675 case arc_length: 
19676   if ( mp->cur_type==mp_pair_type ) mp_pair_to_path(mp);
19677   if ( mp->cur_type!=mp_path_type ) mp_bad_unary(mp, arc_length);
19678   else mp_flush_cur_exp(mp, mp_get_arc_length(mp, mp->cur_exp));
19679   break;
19680
19681 @ Here we use the fact that |c-filled_op+fill_code| is the desired graphical
19682 object |type|.
19683 @^data structure assumptions@>
19684
19685 @<Additional cases of unary operators@>=
19686 case filled_op:
19687 case stroked_op:
19688 case textual_op:
19689 case clipped_op:
19690 case bounded_op:
19691   if ( mp->cur_type!=mp_picture_type ) mp_flush_cur_exp(mp, false_code);
19692   else if ( link(dummy_loc(mp->cur_exp))==null ) mp_flush_cur_exp(mp, false_code);
19693   else if ( type(link(dummy_loc(mp->cur_exp)))==c+mp_fill_code-filled_op )
19694     mp_flush_cur_exp(mp, true_code);
19695   else mp_flush_cur_exp(mp, false_code);
19696   mp->cur_type=mp_boolean_type;
19697   break;
19698
19699 @ @<Additional cases of unary operators@>=
19700 case make_pen_op: 
19701   if ( mp->cur_type==mp_pair_type ) mp_pair_to_path(mp);
19702   if ( mp->cur_type!=mp_path_type ) mp_bad_unary(mp, make_pen_op);
19703   else { 
19704     mp->cur_type=mp_pen_type;
19705     mp->cur_exp=mp_make_pen(mp, mp->cur_exp,true);
19706   };
19707   break;
19708 case make_path_op: 
19709   if ( mp->cur_type!=mp_pen_type ) mp_bad_unary(mp, make_path_op);
19710   else  { 
19711     mp->cur_type=mp_path_type;
19712     mp_make_path(mp, mp->cur_exp);
19713   };
19714   break;
19715 case reverse: 
19716   if ( mp->cur_type==mp_path_type ) {
19717     p=mp_htap_ypoc(mp, mp->cur_exp);
19718     if ( right_type(p)==mp_endpoint ) p=link(p);
19719     mp_toss_knot_list(mp, mp->cur_exp); mp->cur_exp=p;
19720   } else if ( mp->cur_type==mp_pair_type ) mp_pair_to_path(mp);
19721   else mp_bad_unary(mp, reverse);
19722   break;
19723
19724 @ The |pair_value| routine changes the current expression to a
19725 given ordered pair of values.
19726
19727 @<Declare unary action procedures@>=
19728 void mp_pair_value (MP mp,scaled x, scaled y) {
19729   pointer p; /* a pair node */
19730   p=mp_get_node(mp, value_node_size); 
19731   mp_flush_cur_exp(mp, p); mp->cur_type=mp_pair_type;
19732   type(p)=mp_pair_type; name_type(p)=mp_capsule; mp_init_big_node(mp, p);
19733   p=value(p);
19734   type(x_part_loc(p))=mp_known; value(x_part_loc(p))=x;
19735   type(y_part_loc(p))=mp_known; value(y_part_loc(p))=y;
19736 }
19737
19738 @ @<Additional cases of unary operators@>=
19739 case ll_corner_op: 
19740   if ( ! mp_get_cur_bbox(mp) ) mp_bad_unary(mp, ll_corner_op);
19741   else mp_pair_value(mp, minx,miny);
19742   break;
19743 case lr_corner_op: 
19744   if ( ! mp_get_cur_bbox(mp) ) mp_bad_unary(mp, lr_corner_op);
19745   else mp_pair_value(mp, maxx,miny);
19746   break;
19747 case ul_corner_op: 
19748   if ( ! mp_get_cur_bbox(mp) ) mp_bad_unary(mp, ul_corner_op);
19749   else mp_pair_value(mp, minx,maxy);
19750   break;
19751 case ur_corner_op: 
19752   if ( ! mp_get_cur_bbox(mp) ) mp_bad_unary(mp, ur_corner_op);
19753   else mp_pair_value(mp, maxx,maxy);
19754   break;
19755
19756 @ Here is a function that sets |minx|, |maxx|, |miny|, |maxy| to the bounding
19757 box of the current expression.  The boolean result is |false| if the expression
19758 has the wrong type.
19759
19760 @<Declare unary action procedures@>=
19761 boolean mp_get_cur_bbox (MP mp) { 
19762   switch (mp->cur_type) {
19763   case mp_picture_type: 
19764     mp_set_bbox(mp, mp->cur_exp,true);
19765     if ( minx_val(mp->cur_exp)>maxx_val(mp->cur_exp) ) {
19766       minx=0; maxx=0; miny=0; maxy=0;
19767     } else { 
19768       minx=minx_val(mp->cur_exp);
19769       maxx=maxx_val(mp->cur_exp);
19770       miny=miny_val(mp->cur_exp);
19771       maxy=maxy_val(mp->cur_exp);
19772     }
19773     break;
19774   case mp_path_type: 
19775     mp_path_bbox(mp, mp->cur_exp);
19776     break;
19777   case mp_pen_type: 
19778     mp_pen_bbox(mp, mp->cur_exp);
19779     break;
19780   default: 
19781     return false;
19782   }
19783   return true;
19784 }
19785
19786 @ @<Additional cases of unary operators@>=
19787 case read_from_op:
19788 case close_from_op: 
19789   if ( mp->cur_type!=mp_string_type ) mp_bad_unary(mp, c);
19790   else mp_do_read_or_close(mp,c);
19791   break;
19792
19793 @ Here is a routine that interprets |cur_exp| as a file name and tries to read
19794 a line from the file or to close the file.
19795
19796 @<Declare unary action procedures@>=
19797 void mp_do_read_or_close (MP mp,quarterword c) {
19798   readf_index n,n0; /* indices for searching |rd_fname| */
19799   @<Find the |n| where |rd_fname[n]=cur_exp|; if |cur_exp| must be inserted,
19800     call |start_read_input| and |goto found| or |not_found|@>;
19801   mp_begin_file_reading(mp);
19802   name=is_read;
19803   if ( mp_input_ln(mp, mp->rd_file[n] ) ) 
19804     goto FOUND;
19805   mp_end_file_reading(mp);
19806 NOT_FOUND:
19807   @<Record the end of file and set |cur_exp| to a dummy value@>;
19808   return;
19809 CLOSE_FILE:
19810   mp_flush_cur_exp(mp, 0); mp->cur_type=mp_vacuous; 
19811   return;
19812 FOUND:
19813   mp_flush_cur_exp(mp, 0);
19814   mp_finish_read(mp);
19815 }
19816
19817 @ Free slots in the |rd_file| and |rd_fname| arrays are marked with NULL's in
19818 |rd_fname|.
19819
19820 @<Find the |n| where |rd_fname[n]=cur_exp|...@>=
19821 {   
19822   char *fn;
19823   n=mp->read_files;
19824   n0=mp->read_files;
19825   fn = str(mp->cur_exp);
19826   while (mp_xstrcmp(fn,mp->rd_fname[n])!=0) { 
19827     if ( n>0 ) {
19828       decr(n);
19829     } else if ( c==close_from_op ) {
19830       goto CLOSE_FILE;
19831     } else {
19832       if ( n0==mp->read_files ) {
19833         if ( mp->read_files<mp->max_read_files ) {
19834           incr(mp->read_files);
19835         } else {
19836           void **rd_file;
19837           char **rd_fname;
19838               readf_index l,k;
19839           l = mp->max_read_files + (mp->max_read_files>>2);
19840           rd_file = xmalloc((l+1), sizeof(void *));
19841           rd_fname = xmalloc((l+1), sizeof(char *));
19842               for (k=0;k<=l;k++) {
19843             if (k<=mp->max_read_files) {
19844                   rd_file[k]=mp->rd_file[k]; 
19845               rd_fname[k]=mp->rd_fname[k];
19846             } else {
19847               rd_file[k]=0; 
19848               rd_fname[k]=NULL;
19849             }
19850           }
19851               xfree(mp->rd_file); xfree(mp->rd_fname);
19852           mp->max_read_files = l;
19853           mp->rd_file = rd_file;
19854           mp->rd_fname = rd_fname;
19855         }
19856       }
19857       n=n0;
19858       if ( mp_start_read_input(mp,fn,n) ) 
19859         goto FOUND;
19860       else 
19861         goto NOT_FOUND;
19862     }
19863     if ( mp->rd_fname[n]==NULL ) { n0=n; }
19864   } 
19865   if ( c==close_from_op ) { 
19866     (mp->close_file)(mp,mp->rd_file[n]); 
19867     goto NOT_FOUND; 
19868   }
19869 }
19870
19871 @ @<Record the end of file and set |cur_exp| to a dummy value@>=
19872 xfree(mp->rd_fname[n]);
19873 mp->rd_fname[n]=NULL;
19874 if ( n==mp->read_files-1 ) mp->read_files=n;
19875 if ( c==close_from_op ) 
19876   goto CLOSE_FILE;
19877 mp_flush_cur_exp(mp, mp->eof_line);
19878 mp->cur_type=mp_string_type
19879
19880 @ The string denoting end-of-file is a one-byte string at position zero, by definition
19881
19882 @<Glob...@>=
19883 str_number eof_line;
19884
19885 @ @<Set init...@>=
19886 mp->eof_line=0;
19887
19888 @ Finally, we have the operations that combine a capsule~|p|
19889 with the current expression.
19890
19891 @d binary_return  { mp_finish_binary(mp, old_p, old_exp); return; }
19892
19893 @c @<Declare binary action procedures@>
19894 void mp_finish_binary (MP mp, pointer old_p, pointer old_exp ){
19895   check_arith; 
19896   @<Recycle any sidestepped |independent| capsules@>;
19897 }
19898 void mp_do_binary (MP mp,pointer p, quarterword c) {
19899   pointer q,r,rr; /* for list manipulation */
19900   pointer old_p,old_exp; /* capsules to recycle */
19901   integer v; /* for numeric manipulation */
19902   check_arith;
19903   if ( mp->internal[mp_tracing_commands]>two ) {
19904     @<Trace the current binary operation@>;
19905   }
19906   @<Sidestep |independent| cases in capsule |p|@>;
19907   @<Sidestep |independent| cases in the current expression@>;
19908   switch (c) {
19909   case plus: case minus:
19910     @<Add or subtract the current expression from |p|@>;
19911     break;
19912   @<Additional cases of binary operators@>;
19913   }; /* there are no other cases */
19914   mp_recycle_value(mp, p); 
19915   mp_free_node(mp, p,value_node_size); /* |return| to avoid this */
19916   mp_finish_binary(mp, old_p, old_exp);
19917 }
19918
19919 @ @<Declare binary action...@>=
19920 void mp_bad_binary (MP mp,pointer p, quarterword c) { 
19921   mp_disp_err(mp, p,"");
19922   exp_err("Not implemented: ");
19923 @.Not implemented...@>
19924   if ( c>=min_of ) mp_print_op(mp, c);
19925   mp_print_known_or_unknown_type(mp, type(p),p);
19926   if ( c>=min_of ) mp_print(mp, "of"); else mp_print_op(mp, c);
19927   mp_print_known_or_unknown_type(mp, mp->cur_type,mp->cur_exp);
19928   help3("I'm afraid I don't know how to apply that operation to that")
19929        ("combination of types. Continue, and I'll return the second")
19930       ("argument (see above) as the result of the operation.");
19931   mp_put_get_error(mp);
19932 }
19933 void mp_bad_envelope_pen (MP mp) {
19934   mp_disp_err(mp, null,"");
19935   exp_err("Not implemented: envelope(elliptical pen)of(path)");
19936 @.Not implemented...@>
19937   help3("I'm afraid I don't know how to apply that operation to that")
19938        ("combination of types. Continue, and I'll return the second")
19939       ("argument (see above) as the result of the operation.");
19940   mp_put_get_error(mp);
19941 }
19942
19943 @ @<Trace the current binary operation@>=
19944
19945   mp_begin_diagnostic(mp); mp_print_nl(mp, "{(");
19946   mp_print_exp(mp,p,0); /* show the operand, but not verbosely */
19947   mp_print_char(mp,')'); mp_print_op(mp,c); mp_print_char(mp,'(');
19948   mp_print_exp(mp,null,0); mp_print(mp,")}"); 
19949   mp_end_diagnostic(mp, false);
19950 }
19951
19952 @ Several of the binary operations are potentially complicated by the
19953 fact that |independent| values can sneak into capsules. For example,
19954 we've seen an instance of this difficulty in the unary operation
19955 of negation. In order to reduce the number of cases that need to be
19956 handled, we first change the two operands (if necessary)
19957 to rid them of |independent| components. The original operands are
19958 put into capsules called |old_p| and |old_exp|, which will be
19959 recycled after the binary operation has been safely carried out.
19960
19961 @<Recycle any sidestepped |independent| capsules@>=
19962 if ( old_p!=null ) { 
19963   mp_recycle_value(mp, old_p); mp_free_node(mp, old_p,value_node_size);
19964 }
19965 if ( old_exp!=null ) {
19966   mp_recycle_value(mp, old_exp); mp_free_node(mp, old_exp,value_node_size);
19967 }
19968
19969 @ A big node is considered to be ``tarnished'' if it contains at least one
19970 independent component. We will define a simple function called `|tarnished|'
19971 that returns |null| if and only if its argument is not tarnished.
19972
19973 @<Sidestep |independent| cases in capsule |p|@>=
19974 switch (type(p)) {
19975 case mp_transform_type:
19976 case mp_color_type:
19977 case mp_cmykcolor_type:
19978 case mp_pair_type: 
19979   old_p=mp_tarnished(mp, p);
19980   break;
19981 case mp_independent: old_p=mp_void; break;
19982 default: old_p=null; break;
19983 }
19984 if ( old_p!=null ) {
19985   q=mp_stash_cur_exp(mp); old_p=p; mp_make_exp_copy(mp, old_p);
19986   p=mp_stash_cur_exp(mp); mp_unstash_cur_exp(mp, q);
19987 }
19988
19989 @ @<Sidestep |independent| cases in the current expression@>=
19990 switch (mp->cur_type) {
19991 case mp_transform_type:
19992 case mp_color_type:
19993 case mp_cmykcolor_type:
19994 case mp_pair_type: 
19995   old_exp=mp_tarnished(mp, mp->cur_exp);
19996   break;
19997 case mp_independent:old_exp=mp_void; break;
19998 default: old_exp=null; break;
19999 }
20000 if ( old_exp!=null ) {
20001   old_exp=mp->cur_exp; mp_make_exp_copy(mp, old_exp);
20002 }
20003
20004 @ @<Declare binary action...@>=
20005 pointer mp_tarnished (MP mp,pointer p) {
20006   pointer q; /* beginning of the big node */
20007   pointer r; /* current position in the big node */
20008   q=value(p); r=q+mp->big_node_size[type(p)];
20009   do {  
20010    r=r-2;
20011    if ( type(r)==mp_independent ) return mp_void; 
20012   } while (r!=q);
20013   return null;
20014 }
20015
20016 @ @<Add or subtract the current expression from |p|@>=
20017 if ( (mp->cur_type<mp_color_type)||(type(p)<mp_color_type) ) {
20018   mp_bad_binary(mp, p,c);
20019 } else  {
20020   if ((mp->cur_type>mp_pair_type)&&(type(p)>mp_pair_type) ) {
20021     mp_add_or_subtract(mp, p,null,c);
20022   } else {
20023     if ( mp->cur_type!=type(p) )  {
20024       mp_bad_binary(mp, p,c);
20025     } else { 
20026       q=value(p); r=value(mp->cur_exp);
20027       rr=r+mp->big_node_size[mp->cur_type];
20028       while ( r<rr ) { 
20029         mp_add_or_subtract(mp, q,r,c);
20030         q=q+2; r=r+2;
20031       }
20032     }
20033   }
20034 }
20035
20036 @ The first argument to |add_or_subtract| is the location of a value node
20037 in a capsule or pair node that will soon be recycled. The second argument
20038 is either a location within a pair or transform node of |cur_exp|,
20039 or it is null (which means that |cur_exp| itself should be the second
20040 argument).  The third argument is either |plus| or |minus|.
20041
20042 The sum or difference of the numeric quantities will replace the second
20043 operand.  Arithmetic overflow may go undetected; users aren't supposed to
20044 be monkeying around with really big values.
20045 @^overflow in arithmetic@>
20046
20047 @<Declare binary action...@>=
20048 @<Declare the procedure called |dep_finish|@>
20049 void mp_add_or_subtract (MP mp,pointer p, pointer q, quarterword c) {
20050   small_number s,t; /* operand types */
20051   pointer r; /* list traverser */
20052   integer v; /* second operand value */
20053   if ( q==null ) { 
20054     t=mp->cur_type;
20055     if ( t<mp_dependent ) v=mp->cur_exp; else v=dep_list(mp->cur_exp);
20056   } else { 
20057     t=type(q);
20058     if ( t<mp_dependent ) v=value(q); else v=dep_list(q);
20059   }
20060   if ( t==mp_known ) {
20061     if ( c==minus ) negate(v);
20062     if ( type(p)==mp_known ) {
20063       v=mp_slow_add(mp, value(p),v);
20064       if ( q==null ) mp->cur_exp=v; else value(q)=v;
20065       return;
20066     }
20067     @<Add a known value to the constant term of |dep_list(p)|@>;
20068   } else  { 
20069     if ( c==minus ) mp_negate_dep_list(mp, v);
20070     @<Add operand |p| to the dependency list |v|@>;
20071   }
20072 }
20073
20074 @ @<Add a known value to the constant term of |dep_list(p)|@>=
20075 r=dep_list(p);
20076 while ( info(r)!=null ) r=link(r);
20077 value(r)=mp_slow_add(mp, value(r),v);
20078 if ( q==null ) {
20079   q=mp_get_node(mp, value_node_size); mp->cur_exp=q; mp->cur_type=type(p);
20080   name_type(q)=mp_capsule;
20081 }
20082 dep_list(q)=dep_list(p); type(q)=type(p);
20083 prev_dep(q)=prev_dep(p); link(prev_dep(p))=q;
20084 type(p)=mp_known; /* this will keep the recycler from collecting non-garbage */
20085
20086 @ We prefer |dependent| lists to |mp_proto_dependent| ones, because it is
20087 nice to retain the extra accuracy of |fraction| coefficients.
20088 But we have to handle both kinds, and mixtures too.
20089
20090 @<Add operand |p| to the dependency list |v|@>=
20091 if ( type(p)==mp_known ) {
20092   @<Add the known |value(p)| to the constant term of |v|@>;
20093 } else { 
20094   s=type(p); r=dep_list(p);
20095   if ( t==mp_dependent ) {
20096     if ( s==mp_dependent ) {
20097       if ( mp_max_coef(mp, r)+mp_max_coef(mp, v)<coef_bound )
20098         v=mp_p_plus_q(mp, v,r,mp_dependent); goto DONE;
20099       } /* |fix_needed| will necessarily be false */
20100       t=mp_proto_dependent; 
20101       v=mp_p_over_v(mp, v,unity,mp_dependent,mp_proto_dependent);
20102     }
20103     if ( s==mp_proto_dependent ) v=mp_p_plus_q(mp, v,r,mp_proto_dependent);
20104     else v=mp_p_plus_fq(mp, v,unity,r,mp_proto_dependent,mp_dependent);
20105  DONE:  
20106     @<Output the answer, |v| (which might have become |known|)@>;
20107   }
20108
20109 @ @<Add the known |value(p)| to the constant term of |v|@>=
20110
20111   while ( info(v)!=null ) v=link(v);
20112   value(v)=mp_slow_add(mp, value(p),value(v));
20113 }
20114
20115 @ @<Output the answer, |v| (which might have become |known|)@>=
20116 if ( q!=null ) mp_dep_finish(mp, v,q,t);
20117 else  { mp->cur_type=t; mp_dep_finish(mp, v,null,t); }
20118
20119 @ Here's the current situation: The dependency list |v| of type |t|
20120 should either be put into the current expression (if |q=null|) or
20121 into location |q| within a pair node (otherwise). The destination (|cur_exp|
20122 or |q|) formerly held a dependency list with the same
20123 final pointer as the list |v|.
20124
20125 @<Declare the procedure called |dep_finish|@>=
20126 void mp_dep_finish (MP mp, pointer v, pointer q, small_number t) {
20127   pointer p; /* the destination */
20128   scaled vv; /* the value, if it is |known| */
20129   if ( q==null ) p=mp->cur_exp; else p=q;
20130   dep_list(p)=v; type(p)=t;
20131   if ( info(v)==null ) { 
20132     vv=value(v);
20133     if ( q==null ) { 
20134       mp_flush_cur_exp(mp, vv);
20135     } else  { 
20136       mp_recycle_value(mp, p); type(q)=mp_known; value(q)=vv; 
20137     }
20138   } else if ( q==null ) {
20139     mp->cur_type=t;
20140   }
20141   if ( mp->fix_needed ) mp_fix_dependencies(mp);
20142 }
20143
20144 @ Let's turn now to the six basic relations of comparison.
20145
20146 @<Additional cases of binary operators@>=
20147 case less_than: case less_or_equal: case greater_than:
20148 case greater_or_equal: case equal_to: case unequal_to:
20149   check_arith; /* at this point |arith_error| should be |false|? */
20150   if ( (mp->cur_type>mp_pair_type)&&(type(p)>mp_pair_type) ) {
20151     mp_add_or_subtract(mp, p,null,minus); /* |cur_exp:=(p)-cur_exp| */
20152   } else if ( mp->cur_type!=type(p) ) {
20153     mp_bad_binary(mp, p,c); goto DONE; 
20154   } else if ( mp->cur_type==mp_string_type ) {
20155     mp_flush_cur_exp(mp, mp_str_vs_str(mp, value(p),mp->cur_exp));
20156   } else if ((mp->cur_type==mp_unknown_string)||
20157            (mp->cur_type==mp_unknown_boolean) ) {
20158     @<Check if unknowns have been equated@>;
20159   } else if ( (mp->cur_type<=mp_pair_type)&&(mp->cur_type>=mp_transform_type)) {
20160     @<Reduce comparison of big nodes to comparison of scalars@>;
20161   } else if ( mp->cur_type==mp_boolean_type ) {
20162     mp_flush_cur_exp(mp, mp->cur_exp-value(p));
20163   } else { 
20164     mp_bad_binary(mp, p,c); goto DONE;
20165   }
20166   @<Compare the current expression with zero@>;
20167 DONE:  
20168   mp->arith_error=false; /* ignore overflow in comparisons */
20169   break;
20170
20171 @ @<Compare the current expression with zero@>=
20172 if ( mp->cur_type!=mp_known ) {
20173   if ( mp->cur_type<mp_known ) {
20174     mp_disp_err(mp, p,"");
20175     help1("The quantities shown above have not been equated.")
20176   } else  {
20177     help2("Oh dear. I can\'t decide if the expression above is positive,")
20178      ("negative, or zero. So this comparison test won't be `true'.");
20179   }
20180   exp_err("Unknown relation will be considered false");
20181 @.Unknown relation...@>
20182   mp_put_get_flush_error(mp, false_code);
20183 } else {
20184   switch (c) {
20185   case less_than: boolean_reset(mp->cur_exp<0); break;
20186   case less_or_equal: boolean_reset(mp->cur_exp<=0); break;
20187   case greater_than: boolean_reset(mp->cur_exp>0); break;
20188   case greater_or_equal: boolean_reset(mp->cur_exp>=0); break;
20189   case equal_to: boolean_reset(mp->cur_exp==0); break;
20190   case unequal_to: boolean_reset(mp->cur_exp!=0); break;
20191   }; /* there are no other cases */
20192 }
20193 mp->cur_type=mp_boolean_type
20194
20195 @ When two unknown strings are in the same ring, we know that they are
20196 equal. Otherwise, we don't know whether they are equal or not, so we
20197 make no change.
20198
20199 @<Check if unknowns have been equated@>=
20200
20201   q=value(mp->cur_exp);
20202   while ( (q!=mp->cur_exp)&&(q!=p) ) q=value(q);
20203   if ( q==p ) mp_flush_cur_exp(mp, 0);
20204 }
20205
20206 @ @<Reduce comparison of big nodes to comparison of scalars@>=
20207
20208   q=value(p); r=value(mp->cur_exp);
20209   rr=r+mp->big_node_size[mp->cur_type]-2;
20210   while (1) { mp_add_or_subtract(mp, q,r,minus);
20211     if ( type(r)!=mp_known ) break;
20212     if ( value(r)!=0 ) break;
20213     if ( r==rr ) break;
20214     q=q+2; r=r+2;
20215   }
20216   mp_take_part(mp, name_type(r)+x_part-mp_x_part_sector);
20217 }
20218
20219 @ Here we use the sneaky fact that |and_op-false_code=or_op-true_code|.
20220
20221 @<Additional cases of binary operators@>=
20222 case and_op:
20223 case or_op: 
20224   if ( (type(p)!=mp_boolean_type)||(mp->cur_type!=mp_boolean_type) )
20225     mp_bad_binary(mp, p,c);
20226   else if ( value(p)==c+false_code-and_op ) mp->cur_exp=value(p);
20227   break;
20228
20229 @ @<Additional cases of binary operators@>=
20230 case times: 
20231   if ( (mp->cur_type<mp_color_type)||(type(p)<mp_color_type) ) {
20232    mp_bad_binary(mp, p,times);
20233   } else if ( (mp->cur_type==mp_known)||(type(p)==mp_known) ) {
20234     @<Multiply when at least one operand is known@>;
20235   } else if ( (mp_nice_color_or_pair(mp, p,type(p))&&(mp->cur_type>mp_pair_type))
20236       ||(mp_nice_color_or_pair(mp, mp->cur_exp,mp->cur_type)&&
20237           (type(p)>mp_pair_type)) ) {
20238     mp_hard_times(mp, p); 
20239     binary_return;
20240   } else {
20241     mp_bad_binary(mp, p,times);
20242   }
20243   break;
20244
20245 @ @<Multiply when at least one operand is known@>=
20246
20247   if ( type(p)==mp_known ) {
20248     v=value(p); mp_free_node(mp, p,value_node_size); 
20249   } else {
20250     v=mp->cur_exp; mp_unstash_cur_exp(mp, p);
20251   }
20252   if ( mp->cur_type==mp_known ) {
20253     mp->cur_exp=mp_take_scaled(mp, mp->cur_exp,v);
20254   } else if ( (mp->cur_type==mp_pair_type)||
20255               (mp->cur_type==mp_color_type)||
20256               (mp->cur_type==mp_cmykcolor_type) ) {
20257     p=value(mp->cur_exp)+mp->big_node_size[mp->cur_type];
20258     do {  
20259        p=p-2; mp_dep_mult(mp, p,v,true);
20260     } while (p!=value(mp->cur_exp));
20261   } else {
20262     mp_dep_mult(mp, null,v,true);
20263   }
20264   binary_return;
20265 }
20266
20267 @ @<Declare binary action...@>=
20268 void mp_dep_mult (MP mp,pointer p, integer v, boolean v_is_scaled) {
20269   pointer q; /* the dependency list being multiplied by |v| */
20270   small_number s,t; /* its type, before and after */
20271   if ( p==null ) {
20272     q=mp->cur_exp;
20273   } else if ( type(p)!=mp_known ) {
20274     q=p;
20275   } else { 
20276     if ( v_is_scaled ) value(p)=mp_take_scaled(mp, value(p),v);
20277     else value(p)=mp_take_fraction(mp, value(p),v);
20278     return;
20279   };
20280   t=type(q); q=dep_list(q); s=t;
20281   if ( t==mp_dependent ) if ( v_is_scaled )
20282     if (mp_ab_vs_cd(mp, mp_max_coef(mp,q),abs(v),coef_bound-1,unity)>=0 ) 
20283       t=mp_proto_dependent;
20284   q=mp_p_times_v(mp, q,v,s,t,v_is_scaled); 
20285   mp_dep_finish(mp, q,p,t);
20286 }
20287
20288 @ Here is a routine that is similar to |times|; but it is invoked only
20289 internally, when |v| is a |fraction| whose magnitude is at most~1,
20290 and when |cur_type>=mp_color_type|.
20291
20292 @c void mp_frac_mult (MP mp,scaled n, scaled d) {
20293   /* multiplies |cur_exp| by |n/d| */
20294   pointer p; /* a pair node */
20295   pointer old_exp; /* a capsule to recycle */
20296   fraction v; /* |n/d| */
20297   if ( mp->internal[mp_tracing_commands]>two ) {
20298     @<Trace the fraction multiplication@>;
20299   }
20300   switch (mp->cur_type) {
20301   case mp_transform_type:
20302   case mp_color_type:
20303   case mp_cmykcolor_type:
20304   case mp_pair_type:
20305    old_exp=mp_tarnished(mp, mp->cur_exp);
20306    break;
20307   case mp_independent: old_exp=mp_void; break;
20308   default: old_exp=null; break;
20309   }
20310   if ( old_exp!=null ) { 
20311      old_exp=mp->cur_exp; mp_make_exp_copy(mp, old_exp);
20312   }
20313   v=mp_make_fraction(mp, n,d);
20314   if ( mp->cur_type==mp_known ) {
20315     mp->cur_exp=mp_take_fraction(mp, mp->cur_exp,v);
20316   } else if ( mp->cur_type<=mp_pair_type ) { 
20317     p=value(mp->cur_exp)+mp->big_node_size[mp->cur_type];
20318     do {  
20319       p=p-2;
20320       mp_dep_mult(mp, p,v,false);
20321     } while (p!=value(mp->cur_exp));
20322   } else {
20323     mp_dep_mult(mp, null,v,false);
20324   }
20325   if ( old_exp!=null ) {
20326     mp_recycle_value(mp, old_exp); 
20327     mp_free_node(mp, old_exp,value_node_size);
20328   }
20329 }
20330
20331 @ @<Trace the fraction multiplication@>=
20332
20333   mp_begin_diagnostic(mp); 
20334   mp_print_nl(mp, "{("); mp_print_scaled(mp,n); mp_print_char(mp,'/');
20335   mp_print_scaled(mp,d); mp_print(mp,")*("); mp_print_exp(mp,null,0); 
20336   mp_print(mp,")}");
20337   mp_end_diagnostic(mp, false);
20338 }
20339
20340 @ The |hard_times| routine multiplies a nice color or pair by a dependency list.
20341
20342 @<Declare binary action procedures@>=
20343 void mp_hard_times (MP mp,pointer p) {
20344   pointer q; /* a copy of the dependent variable |p| */
20345   pointer r; /* a component of the big node for the nice color or pair */
20346   scaled v; /* the known value for |r| */
20347   if ( type(p)<=mp_pair_type ) { 
20348      q=mp_stash_cur_exp(mp); mp_unstash_cur_exp(mp, p); p=q;
20349   }; /* now |cur_type=mp_pair_type| or |cur_type=mp_color_type| */
20350   r=value(mp->cur_exp)+mp->big_node_size[mp->cur_type];
20351   while (1) { 
20352     r=r-2;
20353     v=value(r);
20354     type(r)=type(p);
20355     if ( r==value(mp->cur_exp) ) 
20356       break;
20357     mp_new_dep(mp, r,mp_copy_dep_list(mp, dep_list(p)));
20358     mp_dep_mult(mp, r,v,true);
20359   }
20360   mp->mem[value_loc(r)]=mp->mem[value_loc(p)];
20361   link(prev_dep(p))=r;
20362   mp_free_node(mp, p,value_node_size);
20363   mp_dep_mult(mp, r,v,true);
20364 }
20365
20366 @ @<Additional cases of binary operators@>=
20367 case over: 
20368   if ( (mp->cur_type!=mp_known)||(type(p)<mp_color_type) ) {
20369     mp_bad_binary(mp, p,over);
20370   } else { 
20371     v=mp->cur_exp; mp_unstash_cur_exp(mp, p);
20372     if ( v==0 ) {
20373       @<Squeal about division by zero@>;
20374     } else { 
20375       if ( mp->cur_type==mp_known ) {
20376         mp->cur_exp=mp_make_scaled(mp, mp->cur_exp,v);
20377       } else if ( mp->cur_type<=mp_pair_type ) { 
20378         p=value(mp->cur_exp)+mp->big_node_size[mp->cur_type];
20379         do {  
20380           p=p-2;  mp_dep_div(mp, p,v);
20381         } while (p!=value(mp->cur_exp));
20382       } else {
20383         mp_dep_div(mp, null,v);
20384       }
20385     }
20386     binary_return;
20387   }
20388   break;
20389
20390 @ @<Declare binary action...@>=
20391 void mp_dep_div (MP mp,pointer p, scaled v) {
20392   pointer q; /* the dependency list being divided by |v| */
20393   small_number s,t; /* its type, before and after */
20394   if ( p==null ) q=mp->cur_exp;
20395   else if ( type(p)!=mp_known ) q=p;
20396   else { value(p)=mp_make_scaled(mp, value(p),v); return; };
20397   t=type(q); q=dep_list(q); s=t;
20398   if ( t==mp_dependent )
20399     if ( mp_ab_vs_cd(mp, mp_max_coef(mp,q),unity,coef_bound-1,abs(v))>=0 ) 
20400       t=mp_proto_dependent;
20401   q=mp_p_over_v(mp, q,v,s,t); 
20402   mp_dep_finish(mp, q,p,t);
20403 }
20404
20405 @ @<Squeal about division by zero@>=
20406
20407   exp_err("Division by zero");
20408 @.Division by zero@>
20409   help2("You're trying to divide the quantity shown above the error")
20410     ("message by zero. I'm going to divide it by one instead.");
20411   mp_put_get_error(mp);
20412 }
20413
20414 @ @<Additional cases of binary operators@>=
20415 case pythag_add:
20416 case pythag_sub: 
20417    if ( (mp->cur_type==mp_known)&&(type(p)==mp_known) ) {
20418      if ( c==pythag_add ) mp->cur_exp=mp_pyth_add(mp, value(p),mp->cur_exp);
20419      else mp->cur_exp=mp_pyth_sub(mp, value(p),mp->cur_exp);
20420    } else mp_bad_binary(mp, p,c);
20421    break;
20422
20423 @ The next few sections of the program deal with affine transformations
20424 of coordinate data.
20425
20426 @<Additional cases of binary operators@>=
20427 case rotated_by: case slanted_by:
20428 case scaled_by: case shifted_by: case transformed_by:
20429 case x_scaled: case y_scaled: case z_scaled:
20430   if ( type(p)==mp_path_type ) { 
20431     path_trans(c,p); binary_return;
20432   } else if ( type(p)==mp_pen_type ) { 
20433     pen_trans(c,p);
20434     mp->cur_exp=mp_convex_hull(mp, mp->cur_exp); 
20435       /* rounding error could destroy convexity */
20436     binary_return;
20437   } else if ( (type(p)==mp_pair_type)||(type(p)==mp_transform_type) ) {
20438     mp_big_trans(mp, p,c);
20439   } else if ( type(p)==mp_picture_type ) {
20440     mp_do_edges_trans(mp, p,c); binary_return;
20441   } else {
20442     mp_bad_binary(mp, p,c);
20443   }
20444   break;
20445
20446 @ Let |c| be one of the eight transform operators. The procedure call
20447 |set_up_trans(c)| first changes |cur_exp| to a transform that corresponds to
20448 |c| and the original value of |cur_exp|. (In particular, |cur_exp| doesn't
20449 change at all if |c=transformed_by|.)
20450
20451 Then, if all components of the resulting transform are |known|, they are
20452 moved to the global variables |txx|, |txy|, |tyx|, |tyy|, |tx|, |ty|;
20453 and |cur_exp| is changed to the known value zero.
20454
20455 @<Declare binary action...@>=
20456 void mp_set_up_trans (MP mp,quarterword c) {
20457   pointer p,q,r; /* list manipulation registers */
20458   if ( (c!=transformed_by)||(mp->cur_type!=mp_transform_type) ) {
20459     @<Put the current transform into |cur_exp|@>;
20460   }
20461   @<If the current transform is entirely known, stash it in global variables;
20462     otherwise |return|@>;
20463 }
20464
20465 @ @<Glob...@>=
20466 scaled txx;
20467 scaled txy;
20468 scaled tyx;
20469 scaled tyy;
20470 scaled tx;
20471 scaled ty; /* current transform coefficients */
20472
20473 @ @<Put the current transform...@>=
20474
20475   p=mp_stash_cur_exp(mp); 
20476   mp->cur_exp=mp_id_transform(mp); 
20477   mp->cur_type=mp_transform_type;
20478   q=value(mp->cur_exp);
20479   switch (c) {
20480   @<For each of the eight cases, change the relevant fields of |cur_exp|
20481     and |goto done|;
20482     but do nothing if capsule |p| doesn't have the appropriate type@>;
20483   }; /* there are no other cases */
20484   mp_disp_err(mp, p,"Improper transformation argument");
20485 @.Improper transformation argument@>
20486   help3("The expression shown above has the wrong type,")
20487        ("so I can\'t transform anything using it.")
20488        ("Proceed, and I'll omit the transformation.");
20489   mp_put_get_error(mp);
20490 DONE: 
20491   mp_recycle_value(mp, p); 
20492   mp_free_node(mp, p,value_node_size);
20493 }
20494
20495 @ @<If the current transform is entirely known, ...@>=
20496 q=value(mp->cur_exp); r=q+transform_node_size;
20497 do {  
20498   r=r-2;
20499   if ( type(r)!=mp_known ) return;
20500 } while (r!=q);
20501 mp->txx=value(xx_part_loc(q));
20502 mp->txy=value(xy_part_loc(q));
20503 mp->tyx=value(yx_part_loc(q));
20504 mp->tyy=value(yy_part_loc(q));
20505 mp->tx=value(x_part_loc(q));
20506 mp->ty=value(y_part_loc(q));
20507 mp_flush_cur_exp(mp, 0)
20508
20509 @ @<For each of the eight cases...@>=
20510 case rotated_by:
20511   if ( type(p)==mp_known )
20512     @<Install sines and cosines, then |goto done|@>;
20513   break;
20514 case slanted_by:
20515   if ( type(p)>mp_pair_type ) { 
20516    mp_install(mp, xy_part_loc(q),p); goto DONE;
20517   };
20518   break;
20519 case scaled_by:
20520   if ( type(p)>mp_pair_type ) { 
20521     mp_install(mp, xx_part_loc(q),p); mp_install(mp, yy_part_loc(q),p); 
20522     goto DONE;
20523   };
20524   break;
20525 case shifted_by:
20526   if ( type(p)==mp_pair_type ) {
20527     r=value(p); mp_install(mp, x_part_loc(q),x_part_loc(r));
20528     mp_install(mp, y_part_loc(q),y_part_loc(r)); goto DONE;
20529   };
20530   break;
20531 case x_scaled:
20532   if ( type(p)>mp_pair_type ) {
20533     mp_install(mp, xx_part_loc(q),p); goto DONE;
20534   };
20535   break;
20536 case y_scaled:
20537   if ( type(p)>mp_pair_type ) {
20538     mp_install(mp, yy_part_loc(q),p); goto DONE;
20539   };
20540   break;
20541 case z_scaled:
20542   if ( type(p)==mp_pair_type )
20543     @<Install a complex multiplier, then |goto done|@>;
20544   break;
20545 case transformed_by:
20546   break;
20547   
20548
20549 @ @<Install sines and cosines, then |goto done|@>=
20550 { mp_n_sin_cos(mp, (value(p) % three_sixty_units)*16);
20551   value(xx_part_loc(q))=mp_round_fraction(mp, mp->n_cos);
20552   value(yx_part_loc(q))=mp_round_fraction(mp, mp->n_sin);
20553   value(xy_part_loc(q))=-value(yx_part_loc(q));
20554   value(yy_part_loc(q))=value(xx_part_loc(q));
20555   goto DONE;
20556 }
20557
20558 @ @<Install a complex multiplier, then |goto done|@>=
20559
20560   r=value(p);
20561   mp_install(mp, xx_part_loc(q),x_part_loc(r));
20562   mp_install(mp, yy_part_loc(q),x_part_loc(r));
20563   mp_install(mp, yx_part_loc(q),y_part_loc(r));
20564   if ( type(y_part_loc(r))==mp_known ) negate(value(y_part_loc(r)));
20565   else mp_negate_dep_list(mp, dep_list(y_part_loc(r)));
20566   mp_install(mp, xy_part_loc(q),y_part_loc(r));
20567   goto DONE;
20568 }
20569
20570 @ Procedure |set_up_known_trans| is like |set_up_trans|, but it
20571 insists that the transformation be entirely known.
20572
20573 @<Declare binary action...@>=
20574 void mp_set_up_known_trans (MP mp,quarterword c) { 
20575   mp_set_up_trans(mp, c);
20576   if ( mp->cur_type!=mp_known ) {
20577     exp_err("Transform components aren't all known");
20578 @.Transform components...@>
20579     help3("I'm unable to apply a partially specified transformation")
20580       ("except to a fully known pair or transform.")
20581       ("Proceed, and I'll omit the transformation.");
20582     mp_put_get_flush_error(mp, 0);
20583     mp->txx=unity; mp->txy=0; mp->tyx=0; mp->tyy=unity; 
20584     mp->tx=0; mp->ty=0;
20585   }
20586 }
20587
20588 @ Here's a procedure that applies the transform |txx..ty| to a pair of
20589 coordinates in locations |p| and~|q|.
20590
20591 @<Declare binary action...@>= 
20592 void mp_trans (MP mp,pointer p, pointer q) {
20593   scaled v; /* the new |x| value */
20594   v=mp_take_scaled(mp, mp->mem[p].sc,mp->txx)+
20595   mp_take_scaled(mp, mp->mem[q].sc,mp->txy)+mp->tx;
20596   mp->mem[q].sc=mp_take_scaled(mp, mp->mem[p].sc,mp->tyx)+
20597   mp_take_scaled(mp, mp->mem[q].sc,mp->tyy)+mp->ty;
20598   mp->mem[p].sc=v;
20599 }
20600
20601 @ The simplest transformation procedure applies a transform to all
20602 coordinates of a path.  The |path_trans(c)(p)| macro applies
20603 a transformation defined by |cur_exp| and the transform operator |c|
20604 to the path~|p|.
20605
20606 @d path_trans(A,B) { mp_set_up_known_trans(mp, (A)); 
20607                      mp_unstash_cur_exp(mp, (B)); 
20608                      mp_do_path_trans(mp, mp->cur_exp); }
20609
20610 @<Declare binary action...@>=
20611 void mp_do_path_trans (MP mp,pointer p) {
20612   pointer q; /* list traverser */
20613   q=p;
20614   do { 
20615     if ( left_type(q)!=mp_endpoint ) 
20616       mp_trans(mp, q+3,q+4); /* that's |left_x| and |left_y| */
20617     mp_trans(mp, q+1,q+2); /* that's |x_coord| and |y_coord| */
20618     if ( right_type(q)!=mp_endpoint ) 
20619       mp_trans(mp, q+5,q+6); /* that's |right_x| and |right_y| */
20620 @^data structure assumptions@>
20621     q=link(q);
20622   } while (q!=p);
20623 }
20624
20625 @ Transforming a pen is very similar, except that there are no |left_type|
20626 and |right_type| fields.
20627
20628 @d pen_trans(A,B) { mp_set_up_known_trans(mp, (A)); 
20629                     mp_unstash_cur_exp(mp, (B)); 
20630                     mp_do_pen_trans(mp, mp->cur_exp); }
20631
20632 @<Declare binary action...@>=
20633 void mp_do_pen_trans (MP mp,pointer p) {
20634   pointer q; /* list traverser */
20635   if ( pen_is_elliptical(p) ) {
20636     mp_trans(mp, p+3,p+4); /* that's |left_x| and |left_y| */
20637     mp_trans(mp, p+5,p+6); /* that's |right_x| and |right_y| */
20638   };
20639   q=p;
20640   do { 
20641     mp_trans(mp, q+1,q+2); /* that's |x_coord| and |y_coord| */
20642 @^data structure assumptions@>
20643     q=link(q);
20644   } while (q!=p);
20645 }
20646
20647 @ The next transformation procedure applies to edge structures. It will do
20648 any transformation, but the results may be substandard if the picture contains
20649 text that uses downloaded bitmap fonts.  The binary action procedure is
20650 |do_edges_trans|, but we also need a function that just scales a picture.
20651 That routine is |scale_edges|.  Both it and the underlying routine |edges_trans|
20652 should be thought of as procedures that update an edge structure |h|, except
20653 that they have to return a (possibly new) structure because of the need to call
20654 |private_edges|.
20655
20656 @<Declare binary action...@>=
20657 pointer mp_edges_trans (MP mp, pointer h) {
20658   pointer q; /* the object being transformed */
20659   pointer r,s; /* for list manipulation */
20660   scaled sx,sy; /* saved transformation parameters */
20661   scaled sqdet; /* square root of determinant for |dash_scale| */
20662   integer sgndet; /* sign of the determinant */
20663   scaled v; /* a temporary value */
20664   h=mp_private_edges(mp, h);
20665   sqdet=mp_sqrt_det(mp, mp->txx,mp->txy,mp->tyx,mp->tyy);
20666   sgndet=mp_ab_vs_cd(mp, mp->txx,mp->tyy,mp->txy,mp->tyx);
20667   if ( dash_list(h)!=null_dash ) {
20668     @<Try to transform the dash list of |h|@>;
20669   }
20670   @<Make the bounding box of |h| unknown if it can't be updated properly
20671     without scanning the whole structure@>;  
20672   q=link(dummy_loc(h));
20673   while ( q!=null ) { 
20674     @<Transform graphical object |q|@>;
20675     q=link(q);
20676   }
20677   return h;
20678 }
20679 void mp_do_edges_trans (MP mp,pointer p, quarterword c) { 
20680   mp_set_up_known_trans(mp, c);
20681   value(p)=mp_edges_trans(mp, value(p));
20682   mp_unstash_cur_exp(mp, p);
20683 }
20684 void mp_scale_edges (MP mp) { 
20685   mp->txx=mp->se_sf; mp->tyy=mp->se_sf;
20686   mp->txy=0; mp->tyx=0; mp->tx=0; mp->ty=0;
20687   mp->se_pic=mp_edges_trans(mp, mp->se_pic);
20688 }
20689
20690 @ @<Try to transform the dash list of |h|@>=
20691 if ( (mp->txy!=0)||(mp->tyx!=0)||
20692      (mp->ty!=0)||(abs(mp->txx)!=abs(mp->tyy))) {
20693   mp_flush_dash_list(mp, h);
20694 } else { 
20695   if ( mp->txx<0 ) { @<Reverse the dash list of |h|@>; } 
20696   @<Scale the dash list by |txx| and shift it by |tx|@>;
20697   dash_y(h)=mp_take_scaled(mp, dash_y(h),abs(mp->tyy));
20698 }
20699
20700 @ @<Reverse the dash list of |h|@>=
20701
20702   r=dash_list(h);
20703   dash_list(h)=null_dash;
20704   while ( r!=null_dash ) {
20705     s=r; r=link(r);
20706     v=start_x(s); start_x(s)=stop_x(s); stop_x(s)=v;
20707     link(s)=dash_list(h);
20708     dash_list(h)=s;
20709   }
20710 }
20711
20712 @ @<Scale the dash list by |txx| and shift it by |tx|@>=
20713 r=dash_list(h);
20714 while ( r!=null_dash ) {
20715   start_x(r)=mp_take_scaled(mp, start_x(r),mp->txx)+mp->tx;
20716   stop_x(r)=mp_take_scaled(mp, stop_x(r),mp->txx)+mp->tx;
20717   r=link(r);
20718 }
20719
20720 @ @<Make the bounding box of |h| unknown if it can't be updated properly...@>=
20721 if ( (mp->txx==0)&&(mp->tyy==0) ) {
20722   @<Swap the $x$ and $y$ parameters in the bounding box of |h|@>;
20723 } else if ( (mp->txy!=0)||(mp->tyx!=0) ) {
20724   mp_init_bbox(mp, h);
20725   goto DONE1;
20726 }
20727 if ( minx_val(h)<=maxx_val(h) ) {
20728   @<Scale the bounding box by |txx+txy| and |tyx+tyy|; then shift by
20729    |(tx,ty)|@>;
20730 }
20731 DONE1:
20732
20733
20734
20735 @ @<Swap the $x$ and $y$ parameters in the bounding box of |h|@>=
20736
20737   v=minx_val(h); minx_val(h)=miny_val(h); miny_val(h)=v;
20738   v=maxx_val(h); maxx_val(h)=maxy_val(h); maxy_val(h)=v;
20739 }
20740
20741 @ The sum ``|txx+txy|'' is whichever of |txx| or |txy| is nonzero.  The other
20742 sum is similar.
20743
20744 @<Scale the bounding box by |txx+txy| and |tyx+tyy|; then shift...@>=
20745
20746   minx_val(h)=mp_take_scaled(mp, minx_val(h),mp->txx+mp->txy)+mp->tx;
20747   maxx_val(h)=mp_take_scaled(mp, maxx_val(h),mp->txx+mp->txy)+mp->tx;
20748   miny_val(h)=mp_take_scaled(mp, miny_val(h),mp->tyx+mp->tyy)+mp->ty;
20749   maxy_val(h)=mp_take_scaled(mp, maxy_val(h),mp->tyx+mp->tyy)+mp->ty;
20750   if ( mp->txx+mp->txy<0 ) {
20751     v=minx_val(h); minx_val(h)=maxx_val(h); maxx_val(h)=v;
20752   }
20753   if ( mp->tyx+mp->tyy<0 ) {
20754     v=miny_val(h); miny_val(h)=maxy_val(h); maxy_val(h)=v;
20755   }
20756 }
20757
20758 @ Now we ready for the main task of transforming the graphical objects in edge
20759 structure~|h|.
20760
20761 @<Transform graphical object |q|@>=
20762 switch (type(q)) {
20763 case mp_fill_code: case mp_stroked_code: 
20764   mp_do_path_trans(mp, path_p(q));
20765   @<Transform |pen_p(q)|, making sure polygonal pens stay counter-clockwise@>;
20766   break;
20767 case mp_start_clip_code: case mp_start_bounds_code: 
20768   mp_do_path_trans(mp, path_p(q));
20769   break;
20770 case mp_text_code: 
20771   r=text_tx_loc(q);
20772   @<Transform the compact transformation starting at |r|@>;
20773   break;
20774 case mp_stop_clip_code: case mp_stop_bounds_code: 
20775   break;
20776 } /* there are no other cases */
20777
20778 @ Note that the shift parameters |(tx,ty)| apply only to the path being stroked.
20779 The |dash_scale| has to be adjusted  to scale the dash lengths in |dash_p(q)|
20780 since the \ps\ output procedures will try to compensate for the transformation
20781 we are applying to |pen_p(q)|.  Since this compensation is based on the square
20782 root of the determinant, |sqdet| is the appropriate factor.
20783
20784 @<Transform |pen_p(q)|, making sure...@>=
20785 if ( pen_p(q)!=null ) {
20786   sx=mp->tx; sy=mp->ty;
20787   mp->tx=0; mp->ty=0;
20788   mp_do_pen_trans(mp, pen_p(q));
20789   if ( ((type(q)==mp_stroked_code)&&(dash_p(q)!=null)) )
20790     dash_scale(q)=mp_take_scaled(mp, dash_scale(q),sqdet);
20791   if ( ! pen_is_elliptical(pen_p(q)) )
20792     if ( sgndet<0 )
20793       pen_p(q)=mp_make_pen(mp, mp_copy_path(mp, pen_p(q)),true); 
20794          /* this unreverses the pen */
20795   mp->tx=sx; mp->ty=sy;
20796 }
20797
20798 @ This uses the fact that transformations are stored in the order
20799 |(tx,ty,txx,txy,tyx,tyy)|.
20800 @^data structure assumptions@>
20801
20802 @<Transform the compact transformation starting at |r|@>=
20803 mp_trans(mp, r,r+1);
20804 sx=mp->tx; sy=mp->ty;
20805 mp->tx=0; mp->ty=0;
20806 mp_trans(mp, r+2,r+4);
20807 mp_trans(mp, r+3,r+5);
20808 mp->tx=sx; mp->ty=sy
20809
20810 @ The hard cases of transformation occur when big nodes are involved,
20811 and when some of their components are unknown.
20812
20813 @<Declare binary action...@>=
20814 @<Declare subroutines needed by |big_trans|@>
20815 void mp_big_trans (MP mp,pointer p, quarterword c) {
20816   pointer q,r,pp,qq; /* list manipulation registers */
20817   small_number s; /* size of a big node */
20818   s=mp->big_node_size[type(p)]; q=value(p); r=q+s;
20819   do {  
20820     r=r-2;
20821     if ( type(r)!=mp_known ) {
20822       @<Transform an unknown big node and |return|@>;
20823     }
20824   } while (r!=q);
20825   @<Transform a known big node@>;
20826 } /* node |p| will now be recycled by |do_binary| */
20827
20828 @ @<Transform an unknown big node and |return|@>=
20829
20830   mp_set_up_known_trans(mp, c); mp_make_exp_copy(mp, p); 
20831   r=value(mp->cur_exp);
20832   if ( mp->cur_type==mp_transform_type ) {
20833     mp_bilin1(mp, yy_part_loc(r),mp->tyy,xy_part_loc(q),mp->tyx,0);
20834     mp_bilin1(mp, yx_part_loc(r),mp->tyy,xx_part_loc(q),mp->tyx,0);
20835     mp_bilin1(mp, xy_part_loc(r),mp->txx,yy_part_loc(q),mp->txy,0);
20836     mp_bilin1(mp, xx_part_loc(r),mp->txx,yx_part_loc(q),mp->txy,0);
20837   }
20838   mp_bilin1(mp, y_part_loc(r),mp->tyy,x_part_loc(q),mp->tyx,mp->ty);
20839   mp_bilin1(mp, x_part_loc(r),mp->txx,y_part_loc(q),mp->txy,mp->tx);
20840   return;
20841 }
20842
20843 @ Let |p| point to a two-word value field inside a big node of |cur_exp|,
20844 and let |q| point to a another value field. The |bilin1| procedure
20845 replaces |p| by $p\cdot t+q\cdot u+\delta$.
20846
20847 @<Declare subroutines needed by |big_trans|@>=
20848 void mp_bilin1 (MP mp, pointer p, scaled t, pointer q, 
20849                 scaled u, scaled delta) {
20850   pointer r; /* list traverser */
20851   if ( t!=unity ) mp_dep_mult(mp, p,t,true);
20852   if ( u!=0 ) {
20853     if ( type(q)==mp_known ) {
20854       delta+=mp_take_scaled(mp, value(q),u);
20855     } else { 
20856       @<Ensure that |type(p)=mp_proto_dependent|@>;
20857       dep_list(p)=mp_p_plus_fq(mp, dep_list(p),u,dep_list(q),
20858                                mp_proto_dependent,type(q));
20859     }
20860   }
20861   if ( type(p)==mp_known ) {
20862     value(p)+=delta;
20863   } else {
20864     r=dep_list(p);
20865     while ( info(r)!=null ) r=link(r);
20866     delta+=value(r);
20867     if ( r!=dep_list(p) ) value(r)=delta;
20868     else { mp_recycle_value(mp, p); type(p)=mp_known; value(p)=delta; };
20869   }
20870   if ( mp->fix_needed ) mp_fix_dependencies(mp);
20871 }
20872
20873 @ @<Ensure that |type(p)=mp_proto_dependent|@>=
20874 if ( type(p)!=mp_proto_dependent ) {
20875   if ( type(p)==mp_known ) 
20876     mp_new_dep(mp, p,mp_const_dependency(mp, value(p)));
20877   else 
20878     dep_list(p)=mp_p_times_v(mp, dep_list(p),unity,mp_dependent,
20879                              mp_proto_dependent,true);
20880   type(p)=mp_proto_dependent;
20881 }
20882
20883 @ @<Transform a known big node@>=
20884 mp_set_up_trans(mp, c);
20885 if ( mp->cur_type==mp_known ) {
20886   @<Transform known by known@>;
20887 } else { 
20888   pp=mp_stash_cur_exp(mp); qq=value(pp);
20889   mp_make_exp_copy(mp, p); r=value(mp->cur_exp);
20890   if ( mp->cur_type==mp_transform_type ) {
20891     mp_bilin2(mp, yy_part_loc(r),yy_part_loc(qq),
20892       value(xy_part_loc(q)),yx_part_loc(qq),null);
20893     mp_bilin2(mp, yx_part_loc(r),yy_part_loc(qq),
20894       value(xx_part_loc(q)),yx_part_loc(qq),null);
20895     mp_bilin2(mp, xy_part_loc(r),xx_part_loc(qq),
20896       value(yy_part_loc(q)),xy_part_loc(qq),null);
20897     mp_bilin2(mp, xx_part_loc(r),xx_part_loc(qq),
20898       value(yx_part_loc(q)),xy_part_loc(qq),null);
20899   };
20900   mp_bilin2(mp, y_part_loc(r),yy_part_loc(qq),
20901     value(x_part_loc(q)),yx_part_loc(qq),y_part_loc(qq));
20902   mp_bilin2(mp, x_part_loc(r),xx_part_loc(qq),
20903     value(y_part_loc(q)),xy_part_loc(qq),x_part_loc(qq));
20904   mp_recycle_value(mp, pp); mp_free_node(mp, pp,value_node_size);
20905 }
20906
20907 @ Let |p| be a |mp_proto_dependent| value whose dependency list ends
20908 at |dep_final|. The following procedure adds |v| times another
20909 numeric quantity to~|p|.
20910
20911 @<Declare subroutines needed by |big_trans|@>=
20912 void mp_add_mult_dep (MP mp,pointer p, scaled v, pointer r) { 
20913   if ( type(r)==mp_known ) {
20914     value(mp->dep_final)+=mp_take_scaled(mp, value(r),v);
20915   } else  { 
20916     dep_list(p)=mp_p_plus_fq(mp, dep_list(p),v,dep_list(r),
20917                                                          mp_proto_dependent,type(r));
20918     if ( mp->fix_needed ) mp_fix_dependencies(mp);
20919   }
20920 }
20921
20922 @ The |bilin2| procedure is something like |bilin1|, but with known
20923 and unknown quantities reversed. Parameter |p| points to a value field
20924 within the big node for |cur_exp|; and |type(p)=mp_known|. Parameters
20925 |t| and~|u| point to value fields elsewhere; so does parameter~|q|,
20926 unless it is |null| (which stands for zero). Location~|p| will be
20927 replaced by $p\cdot t+v\cdot u+q$.
20928
20929 @<Declare subroutines needed by |big_trans|@>=
20930 void mp_bilin2 (MP mp,pointer p, pointer t, scaled v, 
20931                 pointer u, pointer q) {
20932   scaled vv; /* temporary storage for |value(p)| */
20933   vv=value(p); type(p)=mp_proto_dependent;
20934   mp_new_dep(mp, p,mp_const_dependency(mp, 0)); /* this sets |dep_final| */
20935   if ( vv!=0 ) 
20936     mp_add_mult_dep(mp, p,vv,t); /* |dep_final| doesn't change */
20937   if ( v!=0 ) mp_add_mult_dep(mp, p,v,u);
20938   if ( q!=null ) mp_add_mult_dep(mp, p,unity,q);
20939   if ( dep_list(p)==mp->dep_final ) {
20940     vv=value(mp->dep_final); mp_recycle_value(mp, p);
20941     type(p)=mp_known; value(p)=vv;
20942   }
20943 }
20944
20945 @ @<Transform known by known@>=
20946
20947   mp_make_exp_copy(mp, p); r=value(mp->cur_exp);
20948   if ( mp->cur_type==mp_transform_type ) {
20949     mp_bilin3(mp, yy_part_loc(r),mp->tyy,value(xy_part_loc(q)),mp->tyx,0);
20950     mp_bilin3(mp, yx_part_loc(r),mp->tyy,value(xx_part_loc(q)),mp->tyx,0);
20951     mp_bilin3(mp, xy_part_loc(r),mp->txx,value(yy_part_loc(q)),mp->txy,0);
20952     mp_bilin3(mp, xx_part_loc(r),mp->txx,value(yx_part_loc(q)),mp->txy,0);
20953   }
20954   mp_bilin3(mp, y_part_loc(r),mp->tyy,value(x_part_loc(q)),mp->tyx,mp->ty);
20955   mp_bilin3(mp, x_part_loc(r),mp->txx,value(y_part_loc(q)),mp->txy,mp->tx);
20956 }
20957
20958 @ Finally, in |bilin3| everything is |known|.
20959
20960 @<Declare subroutines needed by |big_trans|@>=
20961 void mp_bilin3 (MP mp,pointer p, scaled t, 
20962                scaled v, scaled u, scaled delta) { 
20963   if ( t!=unity )
20964     delta+=mp_take_scaled(mp, value(p),t);
20965   else 
20966     delta+=value(p);
20967   if ( u!=0 ) value(p)=delta+mp_take_scaled(mp, v,u);
20968   else value(p)=delta;
20969 }
20970
20971 @ @<Additional cases of binary operators@>=
20972 case concatenate: 
20973   if ( (mp->cur_type==mp_string_type)&&(type(p)==mp_string_type) ) mp_cat(mp, p);
20974   else mp_bad_binary(mp, p,concatenate);
20975   break;
20976 case substring_of: 
20977   if ( mp_nice_pair(mp, p,type(p))&&(mp->cur_type==mp_string_type) )
20978     mp_chop_string(mp, value(p));
20979   else mp_bad_binary(mp, p,substring_of);
20980   break;
20981 case subpath_of: 
20982   if ( mp->cur_type==mp_pair_type ) mp_pair_to_path(mp);
20983   if ( mp_nice_pair(mp, p,type(p))&&(mp->cur_type==mp_path_type) )
20984     mp_chop_path(mp, value(p));
20985   else mp_bad_binary(mp, p,subpath_of);
20986   break;
20987
20988 @ @<Declare binary action...@>=
20989 void mp_cat (MP mp,pointer p) {
20990   str_number a,b; /* the strings being concatenated */
20991   pool_pointer k; /* index into |str_pool| */
20992   a=value(p); b=mp->cur_exp; str_room(length(a)+length(b));
20993   for (k=mp->str_start[a];k<=str_stop(a)-1;k++) {
20994     append_char(mp->str_pool[k]);
20995   }
20996   for (k=mp->str_start[b];k<=str_stop(b)-1;k++) {
20997     append_char(mp->str_pool[k]);
20998   }
20999   mp->cur_exp=mp_make_string(mp); delete_str_ref(b);
21000 }
21001
21002 @ @<Declare binary action...@>=
21003 void mp_chop_string (MP mp,pointer p) {
21004   integer a, b; /* start and stop points */
21005   integer l; /* length of the original string */
21006   integer k; /* runs from |a| to |b| */
21007   str_number s; /* the original string */
21008   boolean reversed; /* was |a>b|? */
21009   a=mp_round_unscaled(mp, value(x_part_loc(p)));
21010   b=mp_round_unscaled(mp, value(y_part_loc(p)));
21011   if ( a<=b ) reversed=false;
21012   else  { reversed=true; k=a; a=b; b=k; };
21013   s=mp->cur_exp; l=length(s);
21014   if ( a<0 ) { 
21015     a=0;
21016     if ( b<0 ) b=0;
21017   }
21018   if ( b>l ) { 
21019     b=l;
21020     if ( a>l ) a=l;
21021   }
21022   str_room(b-a);
21023   if ( reversed ) {
21024     for (k=mp->str_start[s]+b-1;k>=mp->str_start[s]+a;k--)  {
21025       append_char(mp->str_pool[k]);
21026     }
21027   } else  {
21028     for (k=mp->str_start[s]+a;k<=mp->str_start[s]+b-1;k++)  {
21029       append_char(mp->str_pool[k]);
21030     }
21031   }
21032   mp->cur_exp=mp_make_string(mp); delete_str_ref(s);
21033 }
21034
21035 @ @<Declare binary action...@>=
21036 void mp_chop_path (MP mp,pointer p) {
21037   pointer q; /* a knot in the original path */
21038   pointer pp,qq,rr,ss; /* link variables for copies of path nodes */
21039   scaled a,b,k,l; /* indices for chopping */
21040   boolean reversed; /* was |a>b|? */
21041   l=mp_path_length(mp); a=value(x_part_loc(p)); b=value(y_part_loc(p));
21042   if ( a<=b ) reversed=false;
21043   else  { reversed=true; k=a; a=b; b=k; };
21044   @<Dispense with the cases |a<0| and/or |b>l|@>;
21045   q=mp->cur_exp;
21046   while ( a>=unity ) {
21047     q=link(q); a=a-unity; b=b-unity;
21048   }
21049   if ( b==a ) {
21050     @<Construct a path from |pp| to |qq| of length zero@>; 
21051   } else { 
21052     @<Construct a path from |pp| to |qq| of length $\lceil b\rceil$@>; 
21053   }
21054   left_type(pp)=mp_endpoint; right_type(qq)=mp_endpoint; link(qq)=pp;
21055   mp_toss_knot_list(mp, mp->cur_exp);
21056   if ( reversed ) {
21057     mp->cur_exp=link(mp_htap_ypoc(mp, pp)); mp_toss_knot_list(mp, pp);
21058   } else {
21059     mp->cur_exp=pp;
21060   }
21061 }
21062
21063 @ @<Dispense with the cases |a<0| and/or |b>l|@>=
21064 if ( a<0 ) {
21065   if ( left_type(mp->cur_exp)==mp_endpoint ) {
21066     a=0; if ( b<0 ) b=0;
21067   } else  {
21068     do {  a=a+l; b=b+l; } while (a<0); /* a cycle always has length |l>0| */
21069   }
21070 }
21071 if ( b>l ) {
21072   if ( left_type(mp->cur_exp)==mp_endpoint ) {
21073     b=l; if ( a>l ) a=l;
21074   } else {
21075     while ( a>=l ) { 
21076       a=a-l; b=b-l;
21077     }
21078   }
21079 }
21080
21081 @ @<Construct a path from |pp| to |qq| of length $\lceil b\rceil$@>=
21082
21083   pp=mp_copy_knot(mp, q); qq=pp;
21084   do {  
21085     q=link(q); rr=qq; qq=mp_copy_knot(mp, q); link(rr)=qq; b=b-unity;
21086   } while (b>0);
21087   if ( a>0 ) {
21088     ss=pp; pp=link(pp);
21089     mp_split_cubic(mp, ss,a*010000); pp=link(ss);
21090     mp_free_node(mp, ss,knot_node_size);
21091     if ( rr==ss ) {
21092       b=mp_make_scaled(mp, b,unity-a); rr=pp;
21093     }
21094   }
21095   if ( b<0 ) {
21096     mp_split_cubic(mp, rr,(b+unity)*010000);
21097     mp_free_node(mp, qq,knot_node_size);
21098     qq=link(rr);
21099   }
21100 }
21101
21102 @ @<Construct a path from |pp| to |qq| of length zero@>=
21103
21104   if ( a>0 ) { mp_split_cubic(mp, q,a*010000); q=link(q); };
21105   pp=mp_copy_knot(mp, q); qq=pp;
21106 }
21107
21108 @ @<Additional cases of binary operators@>=
21109 case point_of: case precontrol_of: case postcontrol_of: 
21110   if ( mp->cur_type==mp_pair_type )
21111      mp_pair_to_path(mp);
21112   if ( (mp->cur_type==mp_path_type)&&(type(p)==mp_known) )
21113     mp_find_point(mp, value(p),c);
21114   else 
21115     mp_bad_binary(mp, p,c);
21116   break;
21117 case pen_offset_of: 
21118   if ( (mp->cur_type==mp_pen_type)&& mp_nice_pair(mp, p,type(p)) )
21119     mp_set_up_offset(mp, value(p));
21120   else 
21121     mp_bad_binary(mp, p,pen_offset_of);
21122   break;
21123 case direction_time_of: 
21124   if ( mp->cur_type==mp_pair_type ) mp_pair_to_path(mp);
21125   if ( (mp->cur_type==mp_path_type)&& mp_nice_pair(mp, p,type(p)) )
21126     mp_set_up_direction_time(mp, value(p));
21127   else 
21128     mp_bad_binary(mp, p,direction_time_of);
21129   break;
21130 case envelope_of:
21131   if ( (type(p) != mp_pen_type) || (mp->cur_type != mp_path_type) )
21132     mp_bad_binary(mp, p,envelope_of);
21133   else
21134     mp_set_up_envelope(mp, p);
21135   break;
21136
21137 @ @<Declare binary action...@>=
21138 void mp_set_up_offset (MP mp,pointer p) { 
21139   mp_find_offset(mp, value(x_part_loc(p)),value(y_part_loc(p)),mp->cur_exp);
21140   mp_pair_value(mp, mp->cur_x,mp->cur_y);
21141 }
21142 void mp_set_up_direction_time (MP mp,pointer p) { 
21143   mp_flush_cur_exp(mp, mp_find_direction_time(mp, value(x_part_loc(p)),
21144   value(y_part_loc(p)),mp->cur_exp));
21145 }
21146 void mp_set_up_envelope (MP mp,pointer p) {
21147   small_number ljoin, lcap;
21148   scaled miterlim;
21149   pointer q = mp_copy_path(mp, mp->cur_exp); /* the original path */
21150   /* TODO: accept elliptical pens for straight paths */
21151   if (pen_is_elliptical(value(p))) {
21152     mp_bad_envelope_pen(mp);
21153     mp->cur_exp = q;
21154     mp->cur_type = mp_path_type;
21155     return;
21156   }
21157   if ( mp->internal[mp_linejoin]>unity ) ljoin=2;
21158   else if ( mp->internal[mp_linejoin]>0 ) ljoin=1;
21159   else ljoin=0;
21160   if ( mp->internal[mp_linecap]>unity ) lcap=2;
21161   else if ( mp->internal[mp_linecap]>0 ) lcap=1;
21162   else lcap=0;
21163   if ( mp->internal[mp_miterlimit]<unity )
21164     miterlim=unity;
21165   else
21166     miterlim=mp->internal[mp_miterlimit];
21167   mp->cur_exp = mp_make_envelope(mp, q, value(p), ljoin,lcap,miterlim);
21168   mp->cur_type = mp_path_type;
21169 }
21170
21171 @ @<Declare binary action...@>=
21172 void mp_find_point (MP mp,scaled v, quarterword c) {
21173   pointer p; /* the path */
21174   scaled n; /* its length */
21175   p=mp->cur_exp;
21176   if ( left_type(p)==mp_endpoint ) n=-unity; else n=0;
21177   do {  p=link(p); n=n+unity; } while (p!=mp->cur_exp);
21178   if ( n==0 ) { 
21179     v=0; 
21180   } else if ( v<0 ) {
21181     if ( left_type(p)==mp_endpoint ) v=0;
21182     else v=n-1-((-v-1) % n);
21183   } else if ( v>n ) {
21184     if ( left_type(p)==mp_endpoint ) v=n;
21185     else v=v % n;
21186   }
21187   p=mp->cur_exp;
21188   while ( v>=unity ) { p=link(p); v=v-unity;  };
21189   if ( v!=0 ) {
21190      @<Insert a fractional node by splitting the cubic@>;
21191   }
21192   @<Set the current expression to the desired path coordinates@>;
21193 }
21194
21195 @ @<Insert a fractional node...@>=
21196 { mp_split_cubic(mp, p,v*010000); p=link(p); }
21197
21198 @ @<Set the current expression to the desired path coordinates...@>=
21199 switch (c) {
21200 case point_of: 
21201   mp_pair_value(mp, x_coord(p),y_coord(p));
21202   break;
21203 case precontrol_of: 
21204   if ( left_type(p)==mp_endpoint ) mp_pair_value(mp, x_coord(p),y_coord(p));
21205   else mp_pair_value(mp, left_x(p),left_y(p));
21206   break;
21207 case postcontrol_of: 
21208   if ( right_type(p)==mp_endpoint ) mp_pair_value(mp, x_coord(p),y_coord(p));
21209   else mp_pair_value(mp, right_x(p),right_y(p));
21210   break;
21211 } /* there are no other cases */
21212
21213 @ @<Additional cases of binary operators@>=
21214 case arc_time_of: 
21215   if ( mp->cur_type==mp_pair_type )
21216      mp_pair_to_path(mp);
21217   if ( (mp->cur_type==mp_path_type)&&(type(p)==mp_known) )
21218     mp_flush_cur_exp(mp, mp_get_arc_time(mp, mp->cur_exp,value(p)));
21219   else 
21220     mp_bad_binary(mp, p,c);
21221   break;
21222
21223 @ @<Additional cases of bin...@>=
21224 case intersect: 
21225   if ( type(p)==mp_pair_type ) {
21226     q=mp_stash_cur_exp(mp); mp_unstash_cur_exp(mp, p);
21227     mp_pair_to_path(mp); p=mp_stash_cur_exp(mp); mp_unstash_cur_exp(mp, q);
21228   };
21229   if ( mp->cur_type==mp_pair_type ) mp_pair_to_path(mp);
21230   if ( (mp->cur_type==mp_path_type)&&(type(p)==mp_path_type) ) {
21231     mp_path_intersection(mp, value(p),mp->cur_exp);
21232     mp_pair_value(mp, mp->cur_t,mp->cur_tt);
21233   } else {
21234     mp_bad_binary(mp, p,intersect);
21235   }
21236   break;
21237
21238 @ @<Additional cases of bin...@>=
21239 case in_font:
21240   if ( (mp->cur_type!=mp_string_type)||(type(p)!=mp_string_type)) 
21241     mp_bad_binary(mp, p,in_font);
21242   else { mp_do_infont(mp, p); binary_return; }
21243   break;
21244
21245 @ Function |new_text_node| owns the reference count for its second argument
21246 (the text string) but not its first (the font name).
21247
21248 @<Declare binary action...@>=
21249 void mp_do_infont (MP mp,pointer p) {
21250   pointer q;
21251   q=mp_get_node(mp, edge_header_size);
21252   mp_init_edges(mp, q);
21253   link(obj_tail(q))=mp_new_text_node(mp,str(mp->cur_exp),value(p));
21254   obj_tail(q)=link(obj_tail(q));
21255   mp_free_node(mp, p,value_node_size);
21256   mp_flush_cur_exp(mp, q);
21257   mp->cur_type=mp_picture_type;
21258 }
21259
21260 @* \[40] Statements and commands.
21261 The chief executive of \MP\ is the |do_statement| routine, which
21262 contains the master switch that causes all the various pieces of \MP\
21263 to do their things, in the right order.
21264
21265 In a sense, this is the grand climax of the program: It applies all the
21266 tools that we have worked so hard to construct. In another sense, this is
21267 the messiest part of the program: It necessarily refers to other pieces
21268 of code all over the place, so that a person can't fully understand what is
21269 going on without paging back and forth to be reminded of conventions that
21270 are defined elsewhere. We are now at the hub of the web.
21271
21272 The structure of |do_statement| itself is quite simple.  The first token
21273 of the statement is fetched using |get_x_next|.  If it can be the first
21274 token of an expression, we look for an equation, an assignment, or a
21275 title. Otherwise we use a \&{case} construction to branch at high speed to
21276 the appropriate routine for various and sundry other types of commands,
21277 each of which has an ``action procedure'' that does the necessary work.
21278
21279 The program uses the fact that
21280 $$\hbox{|min_primary_command=max_statement_command=type_name|}$$
21281 to interpret a statement that starts with, e.g., `\&{string}',
21282 as a type declaration rather than a boolean expression.
21283
21284 @c void mp_do_statement (MP mp) { /* governs \MP's activities */
21285   mp->cur_type=mp_vacuous; mp_get_x_next(mp);
21286   if ( mp->cur_cmd>max_primary_command ) {
21287     @<Worry about bad statement@>;
21288   } else if ( mp->cur_cmd>max_statement_command ) {
21289     @<Do an equation, assignment, title, or
21290      `$\langle\,$expression$\,\rangle\,$\&{endgroup}'@>;
21291   } else {
21292     @<Do a statement that doesn't begin with an expression@>;
21293   }
21294   if ( mp->cur_cmd<semicolon )
21295     @<Flush unparsable junk that was found after the statement@>;
21296   mp->error_count=0;
21297 }
21298
21299 @ @<Declarations@>=
21300 @<Declare action procedures for use by |do_statement|@>
21301
21302 @ The only command codes |>max_primary_command| that can be present
21303 at the beginning of a statement are |semicolon| and higher; these
21304 occur when the statement is null.
21305
21306 @<Worry about bad statement@>=
21307
21308   if ( mp->cur_cmd<semicolon ) {
21309     print_err("A statement can't begin with `");
21310 @.A statement can't begin with x@>
21311     mp_print_cmd_mod(mp, mp->cur_cmd,mp->cur_mod); mp_print_char(mp, '\'');
21312     help5("I was looking for the beginning of a new statement.")
21313       ("If you just proceed without changing anything, I'll ignore")
21314       ("everything up to the next `;'. Please insert a semicolon")
21315       ("now in front of anything that you don't want me to delete.")
21316       ("(See Chapter 27 of The METAFONTbook for an example.)");
21317 @:METAFONTbook}{\sl The {\logos METAFONT\/}book@>
21318     mp_back_error(mp); mp_get_x_next(mp);
21319   }
21320 }
21321
21322 @ The help message printed here says that everything is flushed up to
21323 a semicolon, but actually the commands |end_group| and |stop| will
21324 also terminate a statement.
21325
21326 @<Flush unparsable junk that was found after the statement@>=
21327
21328   print_err("Extra tokens will be flushed");
21329 @.Extra tokens will be flushed@>
21330   help6("I've just read as much of that statement as I could fathom,")
21331        ("so a semicolon should have been next. It's very puzzling...")
21332        ("but I'll try to get myself back together, by ignoring")
21333        ("everything up to the next `;'. Please insert a semicolon")
21334        ("now in front of anything that you don't want me to delete.")
21335        ("(See Chapter 27 of The METAFONTbook for an example.)");
21336 @:METAFONTbook}{\sl The {\logos METAFONT\/}book@>
21337   mp_back_error(mp); mp->scanner_status=flushing;
21338   do {  
21339     get_t_next;
21340     @<Decrease the string reference count...@>;
21341   } while (! end_of_statement); /* |cur_cmd=semicolon|, |end_group|, or |stop| */
21342   mp->scanner_status=normal;
21343 }
21344
21345 @ If |do_statement| ends with |cur_cmd=end_group|, we should have
21346 |cur_type=mp_vacuous| unless the statement was simply an expression;
21347 in the latter case, |cur_type| and |cur_exp| should represent that
21348 expression.
21349
21350 @<Do a statement that doesn't...@>=
21351
21352   if ( mp->internal[mp_tracing_commands]>0 ) 
21353     show_cur_cmd_mod;
21354   switch (mp->cur_cmd ) {
21355   case type_name:mp_do_type_declaration(mp); break;
21356   case macro_def:
21357     if ( mp->cur_mod>var_def ) mp_make_op_def(mp);
21358     else if ( mp->cur_mod>end_def ) mp_scan_def(mp);
21359      break;
21360   @<Cases of |do_statement| that invoke particular commands@>;
21361   } /* there are no other cases */
21362   mp->cur_type=mp_vacuous;
21363 }
21364
21365 @ The most important statements begin with expressions.
21366
21367 @<Do an equation, assignment, title, or...@>=
21368
21369   mp->var_flag=assignment; mp_scan_expression(mp);
21370   if ( mp->cur_cmd<end_group ) {
21371     if ( mp->cur_cmd==equals ) mp_do_equation(mp);
21372     else if ( mp->cur_cmd==assignment ) mp_do_assignment(mp);
21373     else if ( mp->cur_type==mp_string_type ) {@<Do a title@> ; }
21374     else if ( mp->cur_type!=mp_vacuous ){ 
21375       exp_err("Isolated expression");
21376 @.Isolated expression@>
21377       help3("I couldn't find an `=' or `:=' after the")
21378         ("expression that is shown above this error message,")
21379         ("so I guess I'll just ignore it and carry on.");
21380       mp_put_get_error(mp);
21381     }
21382     mp_flush_cur_exp(mp, 0); mp->cur_type=mp_vacuous;
21383   }
21384 }
21385
21386 @ @<Do a title@>=
21387
21388   if ( mp->internal[mp_tracing_titles]>0 ) {
21389     mp_print_nl(mp, "");  mp_print_str(mp, mp->cur_exp); update_terminal;
21390   }
21391 }
21392
21393 @ Equations and assignments are performed by the pair of mutually recursive
21394 @^recursion@>
21395 routines |do_equation| and |do_assignment|. These routines are called when
21396 |cur_cmd=equals| and when |cur_cmd=assignment|, respectively; the left-hand
21397 side is in |cur_type| and |cur_exp|, while the right-hand side is yet
21398 to be scanned. After the routines are finished, |cur_type| and |cur_exp|
21399 will be equal to the right-hand side (which will normally be equal
21400 to the left-hand side).
21401
21402 @<Declare action procedures for use by |do_statement|@>=
21403 @<Declare the procedure called |try_eq|@>
21404 @<Declare the procedure called |make_eq|@>
21405 void mp_do_equation (MP mp) ;
21406
21407 @ @c
21408 void mp_do_equation (MP mp) {
21409   pointer lhs; /* capsule for the left-hand side */
21410   pointer p; /* temporary register */
21411   lhs=mp_stash_cur_exp(mp); mp_get_x_next(mp); 
21412   mp->var_flag=assignment; mp_scan_expression(mp);
21413   if ( mp->cur_cmd==equals ) mp_do_equation(mp);
21414   else if ( mp->cur_cmd==assignment ) mp_do_assignment(mp);
21415   if ( mp->internal[mp_tracing_commands]>two ) 
21416     @<Trace the current equation@>;
21417   if ( mp->cur_type==mp_unknown_path ) if ( type(lhs)==mp_pair_type ) {
21418     p=mp_stash_cur_exp(mp); mp_unstash_cur_exp(mp, lhs); lhs=p;
21419   }; /* in this case |make_eq| will change the pair to a path */
21420   mp_make_eq(mp, lhs); /* equate |lhs| to |(cur_type,cur_exp)| */
21421 }
21422
21423 @ And |do_assignment| is similar to |do_equation|:
21424
21425 @<Declarations@>=
21426 void mp_do_assignment (MP mp);
21427
21428 @ @<Declare action procedures for use by |do_statement|@>=
21429 void mp_do_assignment (MP mp) ;
21430
21431 @ @c
21432 void mp_do_assignment (MP mp) {
21433   pointer lhs; /* token list for the left-hand side */
21434   pointer p; /* where the left-hand value is stored */
21435   pointer q; /* temporary capsule for the right-hand value */
21436   if ( mp->cur_type!=mp_token_list ) { 
21437     exp_err("Improper `:=' will be changed to `='");
21438 @.Improper `:='@>
21439     help2("I didn't find a variable name at the left of the `:=',")
21440       ("so I'm going to pretend that you said `=' instead.");
21441     mp_error(mp); mp_do_equation(mp);
21442   } else { 
21443     lhs=mp->cur_exp; mp->cur_type=mp_vacuous;
21444     mp_get_x_next(mp); mp->var_flag=assignment; mp_scan_expression(mp);
21445     if ( mp->cur_cmd==equals ) mp_do_equation(mp);
21446     else if ( mp->cur_cmd==assignment ) mp_do_assignment(mp);
21447     if ( mp->internal[mp_tracing_commands]>two ) 
21448       @<Trace the current assignment@>;
21449     if ( info(lhs)>hash_end ) {
21450       @<Assign the current expression to an internal variable@>;
21451     } else  {
21452       @<Assign the current expression to the variable |lhs|@>;
21453     }
21454     mp_flush_node_list(mp, lhs);
21455   }
21456 }
21457
21458 @ @<Trace the current equation@>=
21459
21460   mp_begin_diagnostic(mp); mp_print_nl(mp, "{("); mp_print_exp(mp,lhs,0);
21461   mp_print(mp,")=("); mp_print_exp(mp,null,0); 
21462   mp_print(mp,")}"); mp_end_diagnostic(mp, false);
21463 }
21464
21465 @ @<Trace the current assignment@>=
21466
21467   mp_begin_diagnostic(mp); mp_print_nl(mp, "{");
21468   if ( info(lhs)>hash_end ) 
21469      mp_print(mp, mp->int_name[info(lhs)-(hash_end)]);
21470   else 
21471      mp_show_token_list(mp, lhs,null,1000,0);
21472   mp_print(mp, ":="); mp_print_exp(mp, null,0); 
21473   mp_print_char(mp, '}'); mp_end_diagnostic(mp, false);
21474 }
21475
21476 @ @<Assign the current expression to an internal variable@>=
21477 if ( mp->cur_type==mp_known )  {
21478   mp->internal[info(lhs)-(hash_end)]=mp->cur_exp;
21479 } else { 
21480   exp_err("Internal quantity `");
21481 @.Internal quantity...@>
21482   mp_print(mp, mp->int_name[info(lhs)-(hash_end)]);
21483   mp_print(mp, "' must receive a known value");
21484   help2("I can\'t set an internal quantity to anything but a known")
21485     ("numeric value, so I'll have to ignore this assignment.");
21486   mp_put_get_error(mp);
21487 }
21488
21489 @ @<Assign the current expression to the variable |lhs|@>=
21490
21491   p=mp_find_variable(mp, lhs);
21492   if ( p!=null ) {
21493     q=mp_stash_cur_exp(mp); mp->cur_type=mp_und_type(mp, p); 
21494     mp_recycle_value(mp, p);
21495     type(p)=mp->cur_type; value(p)=null; mp_make_exp_copy(mp, p);
21496     p=mp_stash_cur_exp(mp); mp_unstash_cur_exp(mp, q); mp_make_eq(mp, p);
21497   } else  { 
21498     mp_obliterated(mp, lhs); mp_put_get_error(mp);
21499   }
21500 }
21501
21502
21503 @ And now we get to the nitty-gritty. The |make_eq| procedure is given
21504 a pointer to a capsule that is to be equated to the current expression.
21505
21506 @<Declare the procedure called |make_eq|@>=
21507 void mp_make_eq (MP mp,pointer lhs) ;
21508
21509
21510
21511 @c void mp_make_eq (MP mp,pointer lhs) {
21512   small_number t; /* type of the left-hand side */
21513   pointer p,q; /* pointers inside of big nodes */
21514   integer v=0; /* value of the left-hand side */
21515 RESTART: 
21516   t=type(lhs);
21517   if ( t<=mp_pair_type ) v=value(lhs);
21518   switch (t) {
21519   @<For each type |t|, make an equation and |goto done| unless |cur_type|
21520     is incompatible with~|t|@>;
21521   } /* all cases have been listed */
21522   @<Announce that the equation cannot be performed@>;
21523 DONE:
21524   check_arith; mp_recycle_value(mp, lhs); 
21525   mp_free_node(mp, lhs,value_node_size);
21526 }
21527
21528 @ @<Announce that the equation cannot be performed@>=
21529 mp_disp_err(mp, lhs,""); 
21530 exp_err("Equation cannot be performed (");
21531 @.Equation cannot be performed@>
21532 if ( type(lhs)<=mp_pair_type ) mp_print_type(mp, type(lhs));
21533 else mp_print(mp, "numeric");
21534 mp_print_char(mp, '=');
21535 if ( mp->cur_type<=mp_pair_type ) mp_print_type(mp, mp->cur_type);
21536 else mp_print(mp, "numeric");
21537 mp_print_char(mp, ')');
21538 help2("I'm sorry, but I don't know how to make such things equal.")
21539      ("(See the two expressions just above the error message.)");
21540 mp_put_get_error(mp)
21541
21542 @ @<For each type |t|, make an equation and |goto done| unless...@>=
21543 case mp_boolean_type: case mp_string_type: case mp_pen_type:
21544 case mp_path_type: case mp_picture_type:
21545   if ( mp->cur_type==t+unknown_tag ) { 
21546     mp_nonlinear_eq(mp, v,mp->cur_exp,false); 
21547     mp_unstash_cur_exp(mp, mp->cur_exp); goto DONE;
21548   } else if ( mp->cur_type==t ) {
21549     @<Report redundant or inconsistent equation and |goto done|@>;
21550   }
21551   break;
21552 case unknown_types:
21553   if ( mp->cur_type==t-unknown_tag ) { 
21554     mp_nonlinear_eq(mp, mp->cur_exp,lhs,true); goto DONE;
21555   } else if ( mp->cur_type==t ) { 
21556     mp_ring_merge(mp, lhs,mp->cur_exp); goto DONE;
21557   } else if ( mp->cur_type==mp_pair_type ) {
21558     if ( t==mp_unknown_path ) { 
21559      mp_pair_to_path(mp); goto RESTART;
21560     };
21561   }
21562   break;
21563 case mp_transform_type: case mp_color_type:
21564 case mp_cmykcolor_type: case mp_pair_type:
21565   if ( mp->cur_type==t ) {
21566     @<Do multiple equations and |goto done|@>;
21567   }
21568   break;
21569 case mp_known: case mp_dependent:
21570 case mp_proto_dependent: case mp_independent:
21571   if ( mp->cur_type>=mp_known ) { 
21572     mp_try_eq(mp, lhs,null); goto DONE;
21573   };
21574   break;
21575 case mp_vacuous:
21576   break;
21577
21578 @ @<Report redundant or inconsistent equation and |goto done|@>=
21579
21580   if ( mp->cur_type<=mp_string_type ) {
21581     if ( mp->cur_type==mp_string_type ) {
21582       if ( mp_str_vs_str(mp, v,mp->cur_exp)!=0 ) {
21583         goto NOT_FOUND;
21584       }
21585     } else if ( v!=mp->cur_exp ) {
21586       goto NOT_FOUND;
21587     }
21588     @<Exclaim about a redundant equation@>; goto DONE;
21589   }
21590   print_err("Redundant or inconsistent equation");
21591 @.Redundant or inconsistent equation@>
21592   help2("An equation between already-known quantities can't help.")
21593        ("But don't worry; continue and I'll just ignore it.");
21594   mp_put_get_error(mp); goto DONE;
21595 NOT_FOUND: 
21596   print_err("Inconsistent equation");
21597 @.Inconsistent equation@>
21598   help2("The equation I just read contradicts what was said before.")
21599        ("But don't worry; continue and I'll just ignore it.");
21600   mp_put_get_error(mp); goto DONE;
21601 }
21602
21603 @ @<Do multiple equations and |goto done|@>=
21604
21605   p=v+mp->big_node_size[t]; 
21606   q=value(mp->cur_exp)+mp->big_node_size[t];
21607   do {  
21608     p=p-2; q=q-2; mp_try_eq(mp, p,q);
21609   } while (p!=v);
21610   goto DONE;
21611 }
21612
21613 @ The first argument to |try_eq| is the location of a value node
21614 in a capsule that will soon be recycled. The second argument is
21615 either a location within a pair or transform node pointed to by
21616 |cur_exp|, or it is |null| (which means that |cur_exp| itself
21617 serves as the second argument). The idea is to leave |cur_exp| unchanged,
21618 but to equate the two operands.
21619
21620 @<Declare the procedure called |try_eq|@>=
21621 void mp_try_eq (MP mp,pointer l, pointer r) ;
21622
21623
21624 @c void mp_try_eq (MP mp,pointer l, pointer r) {
21625   pointer p; /* dependency list for right operand minus left operand */
21626   int t; /* the type of list |p| */
21627   pointer q; /* the constant term of |p| is here */
21628   pointer pp; /* dependency list for right operand */
21629   int tt; /* the type of list |pp| */
21630   boolean copied; /* have we copied a list that ought to be recycled? */
21631   @<Remove the left operand from its container, negate it, and
21632     put it into dependency list~|p| with constant term~|q|@>;
21633   @<Add the right operand to list |p|@>;
21634   if ( info(p)==null ) {
21635     @<Deal with redundant or inconsistent equation@>;
21636   } else { 
21637     mp_linear_eq(mp, p,t);
21638     if ( r==null ) if ( mp->cur_type!=mp_known ) {
21639       if ( type(mp->cur_exp)==mp_known ) {
21640         pp=mp->cur_exp; mp->cur_exp=value(mp->cur_exp); mp->cur_type=mp_known;
21641         mp_free_node(mp, pp,value_node_size);
21642       }
21643     }
21644   }
21645 }
21646
21647 @ @<Remove the left operand from its container, negate it, and...@>=
21648 t=type(l);
21649 if ( t==mp_known ) { 
21650   t=mp_dependent; p=mp_const_dependency(mp, -value(l)); q=p;
21651 } else if ( t==mp_independent ) {
21652   t=mp_dependent; p=mp_single_dependency(mp, l); negate(value(p));
21653   q=mp->dep_final;
21654 } else { 
21655   p=dep_list(l); q=p;
21656   while (1) { 
21657     negate(value(q));
21658     if ( info(q)==null ) break;
21659     q=link(q);
21660   }
21661   link(prev_dep(l))=link(q); prev_dep(link(q))=prev_dep(l);
21662   type(l)=mp_known;
21663 }
21664
21665 @ @<Deal with redundant or inconsistent equation@>=
21666
21667   if ( abs(value(p))>64 ) { /* off by .001 or more */
21668     print_err("Inconsistent equation");
21669 @.Inconsistent equation@>
21670     mp_print(mp, " (off by "); mp_print_scaled(mp, value(p)); 
21671     mp_print_char(mp, ')');
21672     help2("The equation I just read contradicts what was said before.")
21673       ("But don't worry; continue and I'll just ignore it.");
21674     mp_put_get_error(mp);
21675   } else if ( r==null ) {
21676     @<Exclaim about a redundant equation@>;
21677   }
21678   mp_free_node(mp, p,dep_node_size);
21679 }
21680
21681 @ @<Add the right operand to list |p|@>=
21682 if ( r==null ) {
21683   if ( mp->cur_type==mp_known ) {
21684     value(q)=value(q)+mp->cur_exp; goto DONE1;
21685   } else { 
21686     tt=mp->cur_type;
21687     if ( tt==mp_independent ) pp=mp_single_dependency(mp, mp->cur_exp);
21688     else pp=dep_list(mp->cur_exp);
21689   } 
21690 } else {
21691   if ( type(r)==mp_known ) {
21692     value(q)=value(q)+value(r); goto DONE1;
21693   } else { 
21694     tt=type(r);
21695     if ( tt==mp_independent ) pp=mp_single_dependency(mp, r);
21696     else pp=dep_list(r);
21697   }
21698 }
21699 if ( tt!=mp_independent ) copied=false;
21700 else  { copied=true; tt=mp_dependent; };
21701 @<Add dependency list |pp| of type |tt| to dependency list~|p| of type~|t|@>;
21702 if ( copied ) mp_flush_node_list(mp, pp);
21703 DONE1:
21704
21705 @ @<Add dependency list |pp| of type |tt| to dependency list~|p| of type~|t|@>=
21706 mp->watch_coefs=false;
21707 if ( t==tt ) {
21708   p=mp_p_plus_q(mp, p,pp,t);
21709 } else if ( t==mp_proto_dependent ) {
21710   p=mp_p_plus_fq(mp, p,unity,pp,mp_proto_dependent,mp_dependent);
21711 } else { 
21712   q=p;
21713   while ( info(q)!=null ) {
21714     value(q)=mp_round_fraction(mp, value(q)); q=link(q);
21715   }
21716   t=mp_proto_dependent; p=mp_p_plus_q(mp, p,pp,t);
21717 }
21718 mp->watch_coefs=true;
21719
21720 @ Our next goal is to process type declarations. For this purpose it's
21721 convenient to have a procedure that scans a $\langle\,$declared
21722 variable$\,\rangle$ and returns the corresponding token list. After the
21723 following procedure has acted, the token after the declared variable
21724 will have been scanned, so it will appear in |cur_cmd|, |cur_mod|,
21725 and~|cur_sym|.
21726
21727 @<Declare the function called |scan_declared_variable|@>=
21728 pointer mp_scan_declared_variable (MP mp) {
21729   pointer x; /* hash address of the variable's root */
21730   pointer h,t; /* head and tail of the token list to be returned */
21731   pointer l; /* hash address of left bracket */
21732   mp_get_symbol(mp); x=mp->cur_sym;
21733   if ( mp->cur_cmd!=tag_token ) mp_clear_symbol(mp, x,false);
21734   h=mp_get_avail(mp); info(h)=x; t=h;
21735   while (1) { 
21736     mp_get_x_next(mp);
21737     if ( mp->cur_sym==0 ) break;
21738     if ( mp->cur_cmd!=tag_token ) if ( mp->cur_cmd!=internal_quantity)  {
21739       if ( mp->cur_cmd==left_bracket ) {
21740         @<Descend past a collective subscript@>;
21741       } else {
21742         break;
21743       }
21744     }
21745     link(t)=mp_get_avail(mp); t=link(t); info(t)=mp->cur_sym;
21746   }
21747   if ( (eq_type(x)%outer_tag)!=tag_token ) mp_clear_symbol(mp, x,false);
21748   if ( equiv(x)==null ) mp_new_root(mp, x);
21749   return h;
21750 }
21751
21752 @ If the subscript isn't collective, we don't accept it as part of the
21753 declared variable.
21754
21755 @<Descend past a collective subscript@>=
21756
21757   l=mp->cur_sym; mp_get_x_next(mp);
21758   if ( mp->cur_cmd!=right_bracket ) {
21759     mp_back_input(mp); mp->cur_sym=l; mp->cur_cmd=left_bracket; break;
21760   } else {
21761     mp->cur_sym=collective_subscript;
21762   }
21763 }
21764
21765 @ Type declarations are introduced by the following primitive operations.
21766
21767 @<Put each...@>=
21768 mp_primitive(mp, "numeric",type_name,mp_numeric_type);
21769 @:numeric_}{\&{numeric} primitive@>
21770 mp_primitive(mp, "string",type_name,mp_string_type);
21771 @:string_}{\&{string} primitive@>
21772 mp_primitive(mp, "boolean",type_name,mp_boolean_type);
21773 @:boolean_}{\&{boolean} primitive@>
21774 mp_primitive(mp, "path",type_name,mp_path_type);
21775 @:path_}{\&{path} primitive@>
21776 mp_primitive(mp, "pen",type_name,mp_pen_type);
21777 @:pen_}{\&{pen} primitive@>
21778 mp_primitive(mp, "picture",type_name,mp_picture_type);
21779 @:picture_}{\&{picture} primitive@>
21780 mp_primitive(mp, "transform",type_name,mp_transform_type);
21781 @:transform_}{\&{transform} primitive@>
21782 mp_primitive(mp, "color",type_name,mp_color_type);
21783 @:color_}{\&{color} primitive@>
21784 mp_primitive(mp, "rgbcolor",type_name,mp_color_type);
21785 @:color_}{\&{rgbcolor} primitive@>
21786 mp_primitive(mp, "cmykcolor",type_name,mp_cmykcolor_type);
21787 @:color_}{\&{cmykcolor} primitive@>
21788 mp_primitive(mp, "pair",type_name,mp_pair_type);
21789 @:pair_}{\&{pair} primitive@>
21790
21791 @ @<Cases of |print_cmd...@>=
21792 case type_name: mp_print_type(mp, m); break;
21793
21794 @ Now we are ready to handle type declarations, assuming that a
21795 |type_name| has just been scanned.
21796
21797 @<Declare action procedures for use by |do_statement|@>=
21798 void mp_do_type_declaration (MP mp) ;
21799
21800 @ @c
21801 void mp_do_type_declaration (MP mp) {
21802   small_number t; /* the type being declared */
21803   pointer p; /* token list for a declared variable */
21804   pointer q; /* value node for the variable */
21805   if ( mp->cur_mod>=mp_transform_type ) 
21806     t=mp->cur_mod;
21807   else 
21808     t=mp->cur_mod+unknown_tag;
21809   do {  
21810     p=mp_scan_declared_variable(mp);
21811     mp_flush_variable(mp, equiv(info(p)),link(p),false);
21812     q=mp_find_variable(mp, p);
21813     if ( q!=null ) { 
21814       type(q)=t; value(q)=null; 
21815     } else  { 
21816       print_err("Declared variable conflicts with previous vardef");
21817 @.Declared variable conflicts...@>
21818       help2("You can't use, e.g., `numeric foo[]' after `vardef foo'.")
21819            ("Proceed, and I'll ignore the illegal redeclaration.");
21820       mp_put_get_error(mp);
21821     }
21822     mp_flush_list(mp, p);
21823     if ( mp->cur_cmd<comma ) {
21824       @<Flush spurious symbols after the declared variable@>;
21825     }
21826   } while (! end_of_statement);
21827 }
21828
21829 @ @<Flush spurious symbols after the declared variable@>=
21830
21831   print_err("Illegal suffix of declared variable will be flushed");
21832 @.Illegal suffix...flushed@>
21833   help5("Variables in declarations must consist entirely of")
21834     ("names and collective subscripts, e.g., `x[]a'.")
21835     ("Are you trying to use a reserved word in a variable name?")
21836     ("I'm going to discard the junk I found here,")
21837     ("up to the next comma or the end of the declaration.");
21838   if ( mp->cur_cmd==numeric_token )
21839     mp->help_line[2]="Explicit subscripts like `x15a' aren't permitted.";
21840   mp_put_get_error(mp); mp->scanner_status=flushing;
21841   do {  
21842     get_t_next;
21843     @<Decrease the string reference count...@>;
21844   } while (mp->cur_cmd<comma); /* either |end_of_statement| or |cur_cmd=comma| */
21845   mp->scanner_status=normal;
21846 }
21847
21848 @ \MP's |main_control| procedure just calls |do_statement| repeatedly
21849 until coming to the end of the user's program.
21850 Each execution of |do_statement| concludes with
21851 |cur_cmd=semicolon|, |end_group|, or |stop|.
21852
21853 @c void mp_main_control (MP mp) { 
21854   do {  
21855     mp_do_statement(mp);
21856     if ( mp->cur_cmd==end_group ) {
21857       print_err("Extra `endgroup'");
21858 @.Extra `endgroup'@>
21859       help2("I'm not currently working on a `begingroup',")
21860         ("so I had better not try to end anything.");
21861       mp_flush_error(mp, 0);
21862     }
21863   } while (mp->cur_cmd!=stop);
21864 }
21865 int __attribute__((noinline)) 
21866 mp_run (MP mp) {
21867   if (mp->history < mp_fatal_error_stop ) {
21868     @<Install and test the non-local jump buffer@>;
21869     mp_main_control(mp); /* come to life */
21870     mp_final_cleanup(mp); /* prepare for death */
21871     mp_close_files_and_terminate(mp);
21872   }
21873   return mp->history;
21874 }
21875 int __attribute__((noinline)) 
21876 mp_execute (MP mp) {
21877   if (mp->history < mp_fatal_error_stop ) {
21878     mp->history = mp_spotless;
21879     mp->file_offset = 0;
21880     mp->term_offset = 0;
21881     mp->tally = 0; 
21882     @<Install and test the non-local jump buffer@>;
21883         if (mp->run_state==0) {
21884       mp->run_state = 1;
21885     } else {
21886       mp_input_ln(mp,mp->term_in);
21887       mp_firm_up_the_line(mp);  
21888       mp->buffer[limit]='%';
21889       mp->first=limit+1; 
21890       loc=start;
21891     }
21892         do {  
21893       mp_do_statement(mp);
21894     } while (mp->cur_cmd!=stop);
21895   }
21896   return mp->history;
21897 }
21898 int __attribute__((noinline)) 
21899 mp_finish (MP mp) {
21900   if (mp->history < mp_fatal_error_stop ) {
21901     @<Install and test the non-local jump buffer@>;
21902     mp_final_cleanup(mp); /* prepare for death */
21903     mp_close_files_and_terminate(mp);
21904   }
21905   return mp->history;
21906 }
21907 const char * mp_mplib_version (MP mp) {
21908   (void)mp;
21909   return mplib_version;
21910 }
21911 const char * mp_metapost_version (MP mp) {
21912   (void)mp;
21913   return metapost_version;
21914 }
21915
21916 @ @<Exported function headers@>=
21917 int mp_run (MP mp);
21918 int mp_execute (MP mp);
21919 int mp_finish (MP mp);
21920 const char * mp_mplib_version (MP mp);
21921 const char * mp_metapost_version (MP mp);
21922
21923 @ @<Put each...@>=
21924 mp_primitive(mp, "end",stop,0);
21925 @:end_}{\&{end} primitive@>
21926 mp_primitive(mp, "dump",stop,1);
21927 @:dump_}{\&{dump} primitive@>
21928
21929 @ @<Cases of |print_cmd...@>=
21930 case stop:
21931   if ( m==0 ) mp_print(mp, "end");
21932   else mp_print(mp, "dump");
21933   break;
21934
21935 @* \[41] Commands.
21936 Let's turn now to statements that are classified as ``commands'' because
21937 of their imperative nature. We'll begin with simple ones, so that it
21938 will be clear how to hook command processing into the |do_statement| routine;
21939 then we'll tackle the tougher commands.
21940
21941 Here's one of the simplest:
21942
21943 @<Cases of |do_statement|...@>=
21944 case mp_random_seed: mp_do_random_seed(mp);  break;
21945
21946 @ @<Declare action procedures for use by |do_statement|@>=
21947 void mp_do_random_seed (MP mp) ;
21948
21949 @ @c void mp_do_random_seed (MP mp) { 
21950   mp_get_x_next(mp);
21951   if ( mp->cur_cmd!=assignment ) {
21952     mp_missing_err(mp, ":=");
21953 @.Missing `:='@>
21954     help1("Always say `randomseed:=<numeric expression>'.");
21955     mp_back_error(mp);
21956   };
21957   mp_get_x_next(mp); mp_scan_expression(mp);
21958   if ( mp->cur_type!=mp_known ) {
21959     exp_err("Unknown value will be ignored");
21960 @.Unknown value...ignored@>
21961     help2("Your expression was too random for me to handle,")
21962       ("so I won't change the random seed just now.");
21963     mp_put_get_flush_error(mp, 0);
21964   } else {
21965    @<Initialize the random seed to |cur_exp|@>;
21966   }
21967 }
21968
21969 @ @<Initialize the random seed to |cur_exp|@>=
21970
21971   mp_init_randoms(mp, mp->cur_exp);
21972   if ( mp->selector>=log_only && mp->selector<write_file) {
21973     mp->old_setting=mp->selector; mp->selector=log_only;
21974     mp_print_nl(mp, "{randomseed:="); 
21975     mp_print_scaled(mp, mp->cur_exp); 
21976     mp_print_char(mp, '}');
21977     mp_print_nl(mp, ""); mp->selector=mp->old_setting;
21978   }
21979 }
21980
21981 @ And here's another simple one (somewhat different in flavor):
21982
21983 @<Cases of |do_statement|...@>=
21984 case mode_command: 
21985   mp_print_ln(mp); mp->interaction=mp->cur_mod;
21986   @<Initialize the print |selector| based on |interaction|@>;
21987   if ( mp->log_opened ) mp->selector=mp->selector+2;
21988   mp_get_x_next(mp);
21989   break;
21990
21991 @ @<Put each...@>=
21992 mp_primitive(mp, "batchmode",mode_command,mp_batch_mode);
21993 @:mp_batch_mode_}{\&{batchmode} primitive@>
21994 mp_primitive(mp, "nonstopmode",mode_command,mp_nonstop_mode);
21995 @:mp_nonstop_mode_}{\&{nonstopmode} primitive@>
21996 mp_primitive(mp, "scrollmode",mode_command,mp_scroll_mode);
21997 @:mp_scroll_mode_}{\&{scrollmode} primitive@>
21998 mp_primitive(mp, "errorstopmode",mode_command,mp_error_stop_mode);
21999 @:mp_error_stop_mode_}{\&{errorstopmode} primitive@>
22000
22001 @ @<Cases of |print_cmd_mod|...@>=
22002 case mode_command: 
22003   switch (m) {
22004   case mp_batch_mode: mp_print(mp, "batchmode"); break;
22005   case mp_nonstop_mode: mp_print(mp, "nonstopmode"); break;
22006   case mp_scroll_mode: mp_print(mp, "scrollmode"); break;
22007   default: mp_print(mp, "errorstopmode"); break;
22008   }
22009   break;
22010
22011 @ The `\&{inner}' and `\&{outer}' commands are only slightly harder.
22012
22013 @<Cases of |do_statement|...@>=
22014 case protection_command: mp_do_protection(mp); break;
22015
22016 @ @<Put each...@>=
22017 mp_primitive(mp, "inner",protection_command,0);
22018 @:inner_}{\&{inner} primitive@>
22019 mp_primitive(mp, "outer",protection_command,1);
22020 @:outer_}{\&{outer} primitive@>
22021
22022 @ @<Cases of |print_cmd...@>=
22023 case protection_command: 
22024   if ( m==0 ) mp_print(mp, "inner");
22025   else mp_print(mp, "outer");
22026   break;
22027
22028 @ @<Declare action procedures for use by |do_statement|@>=
22029 void mp_do_protection (MP mp) ;
22030
22031 @ @c void mp_do_protection (MP mp) {
22032   int m; /* 0 to unprotect, 1 to protect */
22033   halfword t; /* the |eq_type| before we change it */
22034   m=mp->cur_mod;
22035   do {  
22036     mp_get_symbol(mp); t=eq_type(mp->cur_sym);
22037     if ( m==0 ) { 
22038       if ( t>=outer_tag ) 
22039         eq_type(mp->cur_sym)=t-outer_tag;
22040     } else if ( t<outer_tag ) {
22041       eq_type(mp->cur_sym)=t+outer_tag;
22042     }
22043     mp_get_x_next(mp);
22044   } while (mp->cur_cmd==comma);
22045 }
22046
22047 @ \MP\ never defines the tokens `\.(' and `\.)' to be primitives, but
22048 plain \MP\ begins with the declaration `\&{delimiters} \.{()}'. Such a
22049 declaration assigns the command code |left_delimiter| to `\.{(}' and
22050 |right_delimiter| to `\.{)}'; the |equiv| of each delimiter is the
22051 hash address of its mate.
22052
22053 @<Cases of |do_statement|...@>=
22054 case delimiters: mp_def_delims(mp); break;
22055
22056 @ @<Declare action procedures for use by |do_statement|@>=
22057 void mp_def_delims (MP mp) ;
22058
22059 @ @c void mp_def_delims (MP mp) {
22060   pointer l_delim,r_delim; /* the new delimiter pair */
22061   mp_get_clear_symbol(mp); l_delim=mp->cur_sym;
22062   mp_get_clear_symbol(mp); r_delim=mp->cur_sym;
22063   eq_type(l_delim)=left_delimiter; equiv(l_delim)=r_delim;
22064   eq_type(r_delim)=right_delimiter; equiv(r_delim)=l_delim;
22065   mp_get_x_next(mp);
22066 }
22067
22068 @ Here is a procedure that is called when \MP\ has reached a point
22069 where some right delimiter is mandatory.
22070
22071 @<Declare the procedure called |check_delimiter|@>=
22072 void mp_check_delimiter (MP mp,pointer l_delim, pointer r_delim) {
22073   if ( mp->cur_cmd==right_delimiter ) 
22074     if ( mp->cur_mod==l_delim ) 
22075       return;
22076   if ( mp->cur_sym!=r_delim ) {
22077      mp_missing_err(mp, str(text(r_delim)));
22078 @.Missing `)'@>
22079     help2("I found no right delimiter to match a left one. So I've")
22080       ("put one in, behind the scenes; this may fix the problem.");
22081     mp_back_error(mp);
22082   } else { 
22083     print_err("The token `"); mp_print_text(r_delim);
22084 @.The token...delimiter@>
22085     mp_print(mp, "' is no longer a right delimiter");
22086     help3("Strange: This token has lost its former meaning!")
22087       ("I'll read it as a right delimiter this time;")
22088       ("but watch out, I'll probably miss it later.");
22089     mp_error(mp);
22090   }
22091 }
22092
22093 @ The next four commands save or change the values associated with tokens.
22094
22095 @<Cases of |do_statement|...@>=
22096 case save_command: 
22097   do {  
22098     mp_get_symbol(mp); mp_save_variable(mp, mp->cur_sym); mp_get_x_next(mp);
22099   } while (mp->cur_cmd==comma);
22100   break;
22101 case interim_command: mp_do_interim(mp); break;
22102 case let_command: mp_do_let(mp); break;
22103 case new_internal: mp_do_new_internal(mp); break;
22104
22105 @ @<Declare action procedures for use by |do_statement|@>=
22106 void mp_do_statement (MP mp);
22107 void mp_do_interim (MP mp);
22108
22109 @ @c void mp_do_interim (MP mp) { 
22110   mp_get_x_next(mp);
22111   if ( mp->cur_cmd!=internal_quantity ) {
22112      print_err("The token `");
22113 @.The token...quantity@>
22114     if ( mp->cur_sym==0 ) mp_print(mp, "(%CAPSULE)");
22115     else mp_print_text(mp->cur_sym);
22116     mp_print(mp, "' isn't an internal quantity");
22117     help1("Something like `tracingonline' should follow `interim'.");
22118     mp_back_error(mp);
22119   } else { 
22120     mp_save_internal(mp, mp->cur_mod); mp_back_input(mp);
22121   }
22122   mp_do_statement(mp);
22123 }
22124
22125 @ The following procedure is careful not to undefine the left-hand symbol
22126 too soon, lest commands like `{\tt let x=x}' have a surprising effect.
22127
22128 @<Declare action procedures for use by |do_statement|@>=
22129 void mp_do_let (MP mp) ;
22130
22131 @ @c void mp_do_let (MP mp) {
22132   pointer l; /* hash location of the left-hand symbol */
22133   mp_get_symbol(mp); l=mp->cur_sym; mp_get_x_next(mp);
22134   if ( mp->cur_cmd!=equals ) if ( mp->cur_cmd!=assignment ) {
22135      mp_missing_err(mp, "=");
22136 @.Missing `='@>
22137     help3("You should have said `let symbol = something'.")
22138       ("But don't worry; I'll pretend that an equals sign")
22139       ("was present. The next token I read will be `something'.");
22140     mp_back_error(mp);
22141   }
22142   mp_get_symbol(mp);
22143   switch (mp->cur_cmd) {
22144   case defined_macro: case secondary_primary_macro:
22145   case tertiary_secondary_macro: case expression_tertiary_macro: 
22146     add_mac_ref(mp->cur_mod);
22147     break;
22148   default: 
22149     break;
22150   }
22151   mp_clear_symbol(mp, l,false); eq_type(l)=mp->cur_cmd;
22152   if ( mp->cur_cmd==tag_token ) equiv(l)=null;
22153   else equiv(l)=mp->cur_mod;
22154   mp_get_x_next(mp);
22155 }
22156
22157 @ @<Declarations@>=
22158 void mp_grow_internals (MP mp, int l);
22159 void mp_do_new_internal (MP mp) ;
22160
22161 @ @c
22162 void mp_grow_internals (MP mp, int l) {
22163   scaled *internal;
22164   char * *int_name; 
22165   int k;
22166   if ( hash_end+l>max_halfword ) {
22167     mp_confusion(mp, "out of memory space"); /* can't be reached */
22168   }
22169   int_name = xmalloc ((l+1),sizeof(char *));
22170   internal = xmalloc ((l+1),sizeof(scaled));
22171   for (k=0;k<=l; k++ ) { 
22172     if (k<=mp->max_internal) {
22173       internal[k]=mp->internal[k]; 
22174       int_name[k]=mp->int_name[k]; 
22175     } else {
22176       internal[k]=0; 
22177       int_name[k]=NULL; 
22178     }
22179   }
22180   xfree(mp->internal); xfree(mp->int_name);
22181   mp->int_name = int_name;
22182   mp->internal = internal;
22183   mp->max_internal = l;
22184 }
22185
22186
22187 void mp_do_new_internal (MP mp) { 
22188   do {  
22189     if ( mp->int_ptr==mp->max_internal ) {
22190       mp_grow_internals(mp, (mp->max_internal + (mp->max_internal>>2)));
22191     }
22192     mp_get_clear_symbol(mp); incr(mp->int_ptr);
22193     eq_type(mp->cur_sym)=internal_quantity; 
22194     equiv(mp->cur_sym)=mp->int_ptr;
22195     if(mp->int_name[mp->int_ptr]!=NULL)
22196       xfree(mp->int_name[mp->int_ptr]);
22197     mp->int_name[mp->int_ptr]=str(text(mp->cur_sym)); 
22198     mp->internal[mp->int_ptr]=0;
22199     mp_get_x_next(mp);
22200   } while (mp->cur_cmd==comma);
22201 }
22202
22203 @ @<Dealloc variables@>=
22204 for (k=0;k<=mp->max_internal;k++) {
22205    xfree(mp->int_name[k]);
22206 }
22207 xfree(mp->internal); 
22208 xfree(mp->int_name); 
22209
22210
22211 @ The various `\&{show}' commands are distinguished by modifier fields
22212 in the usual way.
22213
22214 @d show_token_code 0 /* show the meaning of a single token */
22215 @d show_stats_code 1 /* show current memory and string usage */
22216 @d show_code 2 /* show a list of expressions */
22217 @d show_var_code 3 /* show a variable and its descendents */
22218 @d show_dependencies_code 4 /* show dependent variables in terms of independents */
22219
22220 @<Put each...@>=
22221 mp_primitive(mp, "showtoken",show_command,show_token_code);
22222 @:show_token_}{\&{showtoken} primitive@>
22223 mp_primitive(mp, "showstats",show_command,show_stats_code);
22224 @:show_stats_}{\&{showstats} primitive@>
22225 mp_primitive(mp, "show",show_command,show_code);
22226 @:show_}{\&{show} primitive@>
22227 mp_primitive(mp, "showvariable",show_command,show_var_code);
22228 @:show_var_}{\&{showvariable} primitive@>
22229 mp_primitive(mp, "showdependencies",show_command,show_dependencies_code);
22230 @:show_dependencies_}{\&{showdependencies} primitive@>
22231
22232 @ @<Cases of |print_cmd...@>=
22233 case show_command: 
22234   switch (m) {
22235   case show_token_code:mp_print(mp, "showtoken"); break;
22236   case show_stats_code:mp_print(mp, "showstats"); break;
22237   case show_code:mp_print(mp, "show"); break;
22238   case show_var_code:mp_print(mp, "showvariable"); break;
22239   default: mp_print(mp, "showdependencies"); break;
22240   }
22241   break;
22242
22243 @ @<Cases of |do_statement|...@>=
22244 case show_command:mp_do_show_whatever(mp); break;
22245
22246 @ The value of |cur_mod| controls the |verbosity| in the |print_exp| routine:
22247 if it's |show_code|, complicated structures are abbreviated, otherwise
22248 they aren't.
22249
22250 @<Declare action procedures for use by |do_statement|@>=
22251 void mp_do_show (MP mp) ;
22252
22253 @ @c void mp_do_show (MP mp) { 
22254   do {  
22255     mp_get_x_next(mp); mp_scan_expression(mp);
22256     mp_print_nl(mp, ">> ");
22257 @.>>@>
22258     mp_print_exp(mp, null,2); mp_flush_cur_exp(mp, 0);
22259   } while (mp->cur_cmd==comma);
22260 }
22261
22262 @ @<Declare action procedures for use by |do_statement|@>=
22263 void mp_disp_token (MP mp) ;
22264
22265 @ @c void mp_disp_token (MP mp) { 
22266   mp_print_nl(mp, "> ");
22267 @.>\relax@>
22268   if ( mp->cur_sym==0 ) {
22269     @<Show a numeric or string or capsule token@>;
22270   } else { 
22271     mp_print_text(mp->cur_sym); mp_print_char(mp, '=');
22272     if ( eq_type(mp->cur_sym)>=outer_tag ) mp_print(mp, "(outer) ");
22273     mp_print_cmd_mod(mp, mp->cur_cmd,mp->cur_mod);
22274     if ( mp->cur_cmd==defined_macro ) {
22275       mp_print_ln(mp); mp_show_macro(mp, mp->cur_mod,null,100000);
22276     } /* this avoids recursion between |show_macro| and |print_cmd_mod| */
22277 @^recursion@>
22278   }
22279 }
22280
22281 @ @<Show a numeric or string or capsule token@>=
22282
22283   if ( mp->cur_cmd==numeric_token ) {
22284     mp_print_scaled(mp, mp->cur_mod);
22285   } else if ( mp->cur_cmd==capsule_token ) {
22286     mp_print_capsule(mp,mp->cur_mod);
22287   } else  { 
22288     mp_print_char(mp, '"'); 
22289     mp_print_str(mp, mp->cur_mod); mp_print_char(mp, '"');
22290     delete_str_ref(mp->cur_mod);
22291   }
22292 }
22293
22294 @ The following cases of |print_cmd_mod| might arise in connection
22295 with |disp_token|, although they don't necessarily correspond to
22296 primitive tokens.
22297
22298 @<Cases of |print_cmd_...@>=
22299 case left_delimiter:
22300 case right_delimiter: 
22301   if ( c==left_delimiter ) mp_print(mp, "left");
22302   else mp_print(mp, "right");
22303   mp_print(mp, " delimiter that matches "); 
22304   mp_print_text(m);
22305   break;
22306 case tag_token:
22307   if ( m==null ) mp_print(mp, "tag");
22308    else mp_print(mp, "variable");
22309    break;
22310 case defined_macro: 
22311    mp_print(mp, "macro:");
22312    break;
22313 case secondary_primary_macro:
22314 case tertiary_secondary_macro:
22315 case expression_tertiary_macro:
22316   mp_print_cmd_mod(mp, macro_def,c); 
22317   mp_print(mp, "'d macro:");
22318   mp_print_ln(mp); mp_show_token_list(mp, link(link(m)),null,1000,0);
22319   break;
22320 case repeat_loop:
22321   mp_print(mp, "[repeat the loop]");
22322   break;
22323 case internal_quantity:
22324   mp_print(mp, mp->int_name[m]);
22325   break;
22326
22327 @ @<Declare action procedures for use by |do_statement|@>=
22328 void mp_do_show_token (MP mp) ;
22329
22330 @ @c void mp_do_show_token (MP mp) { 
22331   do {  
22332     get_t_next; mp_disp_token(mp);
22333     mp_get_x_next(mp);
22334   } while (mp->cur_cmd==comma);
22335 }
22336
22337 @ @<Declare action procedures for use by |do_statement|@>=
22338 void mp_do_show_stats (MP mp) ;
22339
22340 @ @c void mp_do_show_stats (MP mp) { 
22341   mp_print_nl(mp, "Memory usage ");
22342 @.Memory usage...@>
22343   mp_print_int(mp, mp->var_used); mp_print_char(mp, '&'); mp_print_int(mp, mp->dyn_used);
22344   mp_print(mp, " ("); mp_print_int(mp, mp->hi_mem_min-mp->lo_mem_max-1);
22345   mp_print(mp, " still untouched)"); mp_print_ln(mp);
22346   mp_print_nl(mp, "String usage ");
22347   mp_print_int(mp, mp->strs_in_use-mp->init_str_use);
22348   mp_print_char(mp, '&'); mp_print_int(mp, mp->pool_in_use-mp->init_pool_ptr);
22349   mp_print(mp, " (");
22350   mp_print_int(mp, mp->max_strings-1-mp->strs_used_up); mp_print_char(mp, '&');
22351   mp_print_int(mp, mp->pool_size-mp->pool_ptr); 
22352   mp_print(mp, " now untouched)"); mp_print_ln(mp);
22353   mp_get_x_next(mp);
22354 }
22355
22356 @ Here's a recursive procedure that gives an abbreviated account
22357 of a variable, for use by |do_show_var|.
22358
22359 @<Declare action procedures for use by |do_statement|@>=
22360 void mp_disp_var (MP mp,pointer p) ;
22361
22362 @ @c void mp_disp_var (MP mp,pointer p) {
22363   pointer q; /* traverses attributes and subscripts */
22364   int n; /* amount of macro text to show */
22365   if ( type(p)==mp_structured )  {
22366     @<Descend the structure@>;
22367   } else if ( type(p)>=mp_unsuffixed_macro ) {
22368     @<Display a variable macro@>;
22369   } else if ( type(p)!=undefined ){ 
22370     mp_print_nl(mp, ""); mp_print_variable_name(mp, p); 
22371     mp_print_char(mp, '=');
22372     mp_print_exp(mp, p,0);
22373   }
22374 }
22375
22376 @ @<Descend the structure@>=
22377
22378   q=attr_head(p);
22379   do {  mp_disp_var(mp, q); q=link(q); } while (q!=end_attr);
22380   q=subscr_head(p);
22381   while ( name_type(q)==mp_subscr ) { 
22382     mp_disp_var(mp, q); q=link(q);
22383   }
22384 }
22385
22386 @ @<Display a variable macro@>=
22387
22388   mp_print_nl(mp, ""); mp_print_variable_name(mp, p);
22389   if ( type(p)>mp_unsuffixed_macro ) 
22390     mp_print(mp, "@@#"); /* |suffixed_macro| */
22391   mp_print(mp, "=macro:");
22392   if ( (int)mp->file_offset>=mp->max_print_line-20 ) n=5;
22393   else n=mp->max_print_line-mp->file_offset-15;
22394   mp_show_macro(mp, value(p),null,n);
22395 }
22396
22397 @ @<Declare action procedures for use by |do_statement|@>=
22398 void mp_do_show_var (MP mp) ;
22399
22400 @ @c void mp_do_show_var (MP mp) { 
22401   do {  
22402     get_t_next;
22403     if ( mp->cur_sym>0 ) if ( mp->cur_sym<=hash_end )
22404       if ( mp->cur_cmd==tag_token ) if ( mp->cur_mod!=null ) {
22405       mp_disp_var(mp, mp->cur_mod); goto DONE;
22406     }
22407    mp_disp_token(mp);
22408   DONE:
22409    mp_get_x_next(mp);
22410   } while (mp->cur_cmd==comma);
22411 }
22412
22413 @ @<Declare action procedures for use by |do_statement|@>=
22414 void mp_do_show_dependencies (MP mp) ;
22415
22416 @ @c void mp_do_show_dependencies (MP mp) {
22417   pointer p; /* link that runs through all dependencies */
22418   p=link(dep_head);
22419   while ( p!=dep_head ) {
22420     if ( mp_interesting(mp, p) ) {
22421       mp_print_nl(mp, ""); mp_print_variable_name(mp, p);
22422       if ( type(p)==mp_dependent ) mp_print_char(mp, '=');
22423       else mp_print(mp, " = "); /* extra spaces imply proto-dependency */
22424       mp_print_dependency(mp, dep_list(p),type(p));
22425     }
22426     p=dep_list(p);
22427     while ( info(p)!=null ) p=link(p);
22428     p=link(p);
22429   }
22430   mp_get_x_next(mp);
22431 }
22432
22433 @ Finally we are ready for the procedure that governs all of the
22434 show commands.
22435
22436 @<Declare action procedures for use by |do_statement|@>=
22437 void mp_do_show_whatever (MP mp) ;
22438
22439 @ @c void mp_do_show_whatever (MP mp) { 
22440   if ( mp->interaction==mp_error_stop_mode ) wake_up_terminal;
22441   switch (mp->cur_mod) {
22442   case show_token_code:mp_do_show_token(mp); break;
22443   case show_stats_code:mp_do_show_stats(mp); break;
22444   case show_code:mp_do_show(mp); break;
22445   case show_var_code:mp_do_show_var(mp); break;
22446   case show_dependencies_code:mp_do_show_dependencies(mp); break;
22447   } /* there are no other cases */
22448   if ( mp->internal[mp_showstopping]>0 ){ 
22449     print_err("OK");
22450 @.OK@>
22451     if ( mp->interaction<mp_error_stop_mode ) { 
22452       help0; decr(mp->error_count);
22453     } else {
22454       help1("This isn't an error message; I'm just showing something.");
22455     }
22456     if ( mp->cur_cmd==semicolon ) mp_error(mp);
22457      else mp_put_get_error(mp);
22458   }
22459 }
22460
22461 @ The `\&{addto}' command needs the following additional primitives:
22462
22463 @d double_path_code 0 /* command modifier for `\&{doublepath}' */
22464 @d contour_code 1 /* command modifier for `\&{contour}' */
22465 @d also_code 2 /* command modifier for `\&{also}' */
22466
22467 @ Pre and postscripts need two new identifiers:
22468
22469 @d with_pre_script 11
22470 @d with_post_script 13
22471
22472 @<Put each...@>=
22473 mp_primitive(mp, "doublepath",thing_to_add,double_path_code);
22474 @:double_path_}{\&{doublepath} primitive@>
22475 mp_primitive(mp, "contour",thing_to_add,contour_code);
22476 @:contour_}{\&{contour} primitive@>
22477 mp_primitive(mp, "also",thing_to_add,also_code);
22478 @:also_}{\&{also} primitive@>
22479 mp_primitive(mp, "withpen",with_option,mp_pen_type);
22480 @:with_pen_}{\&{withpen} primitive@>
22481 mp_primitive(mp, "dashed",with_option,mp_picture_type);
22482 @:dashed_}{\&{dashed} primitive@>
22483 mp_primitive(mp, "withprescript",with_option,with_pre_script);
22484 @:with_pre_script_}{\&{withprescript} primitive@>
22485 mp_primitive(mp, "withpostscript",with_option,with_post_script);
22486 @:with_post_script_}{\&{withpostscript} primitive@>
22487 mp_primitive(mp, "withoutcolor",with_option,mp_no_model);
22488 @:with_color_}{\&{withoutcolor} primitive@>
22489 mp_primitive(mp, "withgreyscale",with_option,mp_grey_model);
22490 @:with_color_}{\&{withgreyscale} primitive@>
22491 mp_primitive(mp, "withcolor",with_option,mp_uninitialized_model);
22492 @:with_color_}{\&{withcolor} primitive@>
22493 /*  \&{withrgbcolor} is an alias for \&{withcolor} */
22494 mp_primitive(mp, "withrgbcolor",with_option,mp_rgb_model);
22495 @:with_color_}{\&{withrgbcolor} primitive@>
22496 mp_primitive(mp, "withcmykcolor",with_option,mp_cmyk_model);
22497 @:with_color_}{\&{withcmykcolor} primitive@>
22498
22499 @ @<Cases of |print_cmd...@>=
22500 case thing_to_add:
22501   if ( m==contour_code ) mp_print(mp, "contour");
22502   else if ( m==double_path_code ) mp_print(mp, "doublepath");
22503   else mp_print(mp, "also");
22504   break;
22505 case with_option:
22506   if ( m==mp_pen_type ) mp_print(mp, "withpen");
22507   else if ( m==with_pre_script ) mp_print(mp, "withprescript");
22508   else if ( m==with_post_script ) mp_print(mp, "withpostscript");
22509   else if ( m==mp_no_model ) mp_print(mp, "withoutcolor");
22510   else if ( m==mp_rgb_model ) mp_print(mp, "withrgbcolor");
22511   else if ( m==mp_uninitialized_model ) mp_print(mp, "withcolor");
22512   else if ( m==mp_cmyk_model ) mp_print(mp, "withcmykcolor");
22513   else if ( m==mp_grey_model ) mp_print(mp, "withgreyscale");
22514   else mp_print(mp, "dashed");
22515   break;
22516
22517 @ The |scan_with_list| procedure parses a $\langle$with list$\rangle$ and
22518 updates the list of graphical objects starting at |p|.  Each $\langle$with
22519 clause$\rangle$ updates all graphical objects whose |type| is compatible.
22520 Other objects are ignored.
22521
22522 @<Declare action procedures for use by |do_statement|@>=
22523 void mp_scan_with_list (MP mp,pointer p) ;
22524
22525 @ @c void mp_scan_with_list (MP mp,pointer p) {
22526   small_number t; /* |cur_mod| of the |with_option| (should match |cur_type|) */
22527   pointer q; /* for list manipulation */
22528   int old_setting; /* saved |selector| setting */
22529   pointer k; /* for finding the near-last item in a list  */
22530   str_number s; /* for string cleanup after combining  */
22531   pointer cp,pp,dp,ap,bp;
22532     /* objects being updated; |void| initially; |null| to suppress update */
22533   cp=mp_void; pp=mp_void; dp=mp_void; ap=mp_void; bp=mp_void;
22534   k=0;
22535   while ( mp->cur_cmd==with_option ){ 
22536     t=mp->cur_mod;
22537     mp_get_x_next(mp);
22538     if ( t!=mp_no_model ) mp_scan_expression(mp);
22539     if (((t==with_pre_script)&&(mp->cur_type!=mp_string_type))||
22540      ((t==with_post_script)&&(mp->cur_type!=mp_string_type))||
22541      ((t==mp_uninitialized_model)&&
22542         ((mp->cur_type!=mp_cmykcolor_type)&&(mp->cur_type!=mp_color_type)
22543           &&(mp->cur_type!=mp_known)&&(mp->cur_type!=mp_boolean_type)))||
22544      ((t==mp_cmyk_model)&&(mp->cur_type!=mp_cmykcolor_type))||
22545      ((t==mp_rgb_model)&&(mp->cur_type!=mp_color_type))||
22546      ((t==mp_grey_model)&&(mp->cur_type!=mp_known))||
22547      ((t==mp_pen_type)&&(mp->cur_type!=t))||
22548      ((t==mp_picture_type)&&(mp->cur_type!=t)) ) {
22549       @<Complain about improper type@>;
22550     } else if ( t==mp_uninitialized_model ) {
22551       if ( cp==mp_void ) @<Make |cp| a colored object in object list~|p|@>;
22552       if ( cp!=null )
22553         @<Transfer a color from the current expression to object~|cp|@>;
22554       mp_flush_cur_exp(mp, 0);
22555     } else if ( t==mp_rgb_model ) {
22556       if ( cp==mp_void ) @<Make |cp| a colored object in object list~|p|@>;
22557       if ( cp!=null )
22558         @<Transfer a rgbcolor from the current expression to object~|cp|@>;
22559       mp_flush_cur_exp(mp, 0);
22560     } else if ( t==mp_cmyk_model ) {
22561       if ( cp==mp_void ) @<Make |cp| a colored object in object list~|p|@>;
22562       if ( cp!=null )
22563         @<Transfer a cmykcolor from the current expression to object~|cp|@>;
22564       mp_flush_cur_exp(mp, 0);
22565     } else if ( t==mp_grey_model ) {
22566       if ( cp==mp_void ) @<Make |cp| a colored object in object list~|p|@>;
22567       if ( cp!=null )
22568         @<Transfer a greyscale from the current expression to object~|cp|@>;
22569       mp_flush_cur_exp(mp, 0);
22570     } else if ( t==mp_no_model ) {
22571       if ( cp==mp_void ) @<Make |cp| a colored object in object list~|p|@>;
22572       if ( cp!=null )
22573         @<Transfer a noncolor from the current expression to object~|cp|@>;
22574     } else if ( t==mp_pen_type ) {
22575       if ( pp==mp_void ) @<Make |pp| an object in list~|p| that needs a pen@>;
22576       if ( pp!=null ) {
22577         if ( pen_p(pp)!=null ) mp_toss_knot_list(mp, pen_p(pp));
22578         pen_p(pp)=mp->cur_exp; mp->cur_type=mp_vacuous;
22579       }
22580     } else if ( t==with_pre_script ) {
22581       if ( ap==mp_void )
22582         ap=p;
22583       while ( (ap!=null)&&(! has_color(ap)) )
22584          ap=link(ap);
22585       if ( ap!=null ) {
22586         if ( pre_script(ap)!=null ) { /*  build a new,combined string  */
22587           s=pre_script(ap);
22588           old_setting=mp->selector;
22589               mp->selector=new_string;
22590           str_room(length(pre_script(ap))+length(mp->cur_exp)+2);
22591               mp_print_str(mp, mp->cur_exp);
22592           append_char(13);  /* a forced \ps\ newline  */
22593           mp_print_str(mp, pre_script(ap));
22594           pre_script(ap)=mp_make_string(mp);
22595           delete_str_ref(s);
22596           mp->selector=old_setting;
22597         } else {
22598           pre_script(ap)=mp->cur_exp;
22599         }
22600         mp->cur_type=mp_vacuous;
22601       }
22602     } else if ( t==with_post_script ) {
22603       if ( bp==mp_void )
22604         k=p; 
22605       bp=k;
22606       while ( link(k)!=null ) {
22607         k=link(k);
22608         if ( has_color(k) ) bp=k;
22609       }
22610       if ( bp!=null ) {
22611          if ( post_script(bp)!=null ) {
22612            s=post_script(bp);
22613            old_setting=mp->selector;
22614                mp->selector=new_string;
22615            str_room(length(post_script(bp))+length(mp->cur_exp)+2);
22616            mp_print_str(mp, post_script(bp));
22617            append_char(13); /* a forced \ps\ newline  */
22618            mp_print_str(mp, mp->cur_exp);
22619            post_script(bp)=mp_make_string(mp);
22620            delete_str_ref(s);
22621            mp->selector=old_setting;
22622          } else {
22623            post_script(bp)=mp->cur_exp;
22624          }
22625          mp->cur_type=mp_vacuous;
22626        }
22627     } else { 
22628       if ( dp==mp_void ) {
22629         @<Make |dp| a stroked node in list~|p|@>;
22630       }
22631       if ( dp!=null ) {
22632         if ( dash_p(dp)!=null ) delete_edge_ref(dash_p(dp));
22633         dash_p(dp)=mp_make_dashes(mp, mp->cur_exp);
22634         dash_scale(dp)=unity;
22635         mp->cur_type=mp_vacuous;
22636       }
22637     }
22638   }
22639   @<Copy the information from objects |cp|, |pp|, and |dp| into the rest
22640     of the list@>;
22641 }
22642
22643 @ @<Complain about improper type@>=
22644 { exp_err("Improper type");
22645 @.Improper type@>
22646 help2("Next time say `withpen <known pen expression>';")
22647   ("I'll ignore the bad `with' clause and look for another.");
22648 if ( t==with_pre_script )
22649   mp->help_line[1]="Next time say `withprescript <known string expression>';";
22650 else if ( t==with_post_script )
22651   mp->help_line[1]="Next time say `withpostscript <known string expression>';";
22652 else if ( t==mp_picture_type )
22653   mp->help_line[1]="Next time say `dashed <known picture expression>';";
22654 else if ( t==mp_uninitialized_model )
22655   mp->help_line[1]="Next time say `withcolor <known color expression>';";
22656 else if ( t==mp_rgb_model )
22657   mp->help_line[1]="Next time say `withrgbcolor <known color expression>';";
22658 else if ( t==mp_cmyk_model )
22659   mp->help_line[1]="Next time say `withcmykcolor <known cmykcolor expression>';";
22660 else if ( t==mp_grey_model )
22661   mp->help_line[1]="Next time say `withgreyscale <known numeric expression>';";;
22662 mp_put_get_flush_error(mp, 0);
22663 }
22664
22665 @ Forcing the color to be between |0| and |unity| here guarantees that no
22666 picture will ever contain a color outside the legal range for \ps\ graphics.
22667
22668 @<Transfer a color from the current expression to object~|cp|@>=
22669 { if ( mp->cur_type==mp_color_type )
22670    @<Transfer a rgbcolor from the current expression to object~|cp|@>
22671 else if ( mp->cur_type==mp_cmykcolor_type )
22672    @<Transfer a cmykcolor from the current expression to object~|cp|@>
22673 else if ( mp->cur_type==mp_known )
22674    @<Transfer a greyscale from the current expression to object~|cp|@>
22675 else if ( mp->cur_exp==false_code )
22676    @<Transfer a noncolor from the current expression to object~|cp|@>;
22677 }
22678
22679 @ @<Transfer a rgbcolor from the current expression to object~|cp|@>=
22680 { q=value(mp->cur_exp);
22681 cyan_val(cp)=0;
22682 magenta_val(cp)=0;
22683 yellow_val(cp)=0;
22684 black_val(cp)=0;
22685 red_val(cp)=value(red_part_loc(q));
22686 green_val(cp)=value(green_part_loc(q));
22687 blue_val(cp)=value(blue_part_loc(q));
22688 color_model(cp)=mp_rgb_model;
22689 if ( red_val(cp)<0 ) red_val(cp)=0;
22690 if ( green_val(cp)<0 ) green_val(cp)=0;
22691 if ( blue_val(cp)<0 ) blue_val(cp)=0;
22692 if ( red_val(cp)>unity ) red_val(cp)=unity;
22693 if ( green_val(cp)>unity ) green_val(cp)=unity;
22694 if ( blue_val(cp)>unity ) blue_val(cp)=unity;
22695 }
22696
22697 @ @<Transfer a cmykcolor from the current expression to object~|cp|@>=
22698 { q=value(mp->cur_exp);
22699 cyan_val(cp)=value(cyan_part_loc(q));
22700 magenta_val(cp)=value(magenta_part_loc(q));
22701 yellow_val(cp)=value(yellow_part_loc(q));
22702 black_val(cp)=value(black_part_loc(q));
22703 color_model(cp)=mp_cmyk_model;
22704 if ( cyan_val(cp)<0 ) cyan_val(cp)=0;
22705 if ( magenta_val(cp)<0 ) magenta_val(cp)=0;
22706 if ( yellow_val(cp)<0 ) yellow_val(cp)=0;
22707 if ( black_val(cp)<0 ) black_val(cp)=0;
22708 if ( cyan_val(cp)>unity ) cyan_val(cp)=unity;
22709 if ( magenta_val(cp)>unity ) magenta_val(cp)=unity;
22710 if ( yellow_val(cp)>unity ) yellow_val(cp)=unity;
22711 if ( black_val(cp)>unity ) black_val(cp)=unity;
22712 }
22713
22714 @ @<Transfer a greyscale from the current expression to object~|cp|@>=
22715 { q=mp->cur_exp;
22716 cyan_val(cp)=0;
22717 magenta_val(cp)=0;
22718 yellow_val(cp)=0;
22719 black_val(cp)=0;
22720 grey_val(cp)=q;
22721 color_model(cp)=mp_grey_model;
22722 if ( grey_val(cp)<0 ) grey_val(cp)=0;
22723 if ( grey_val(cp)>unity ) grey_val(cp)=unity;
22724 }
22725
22726 @ @<Transfer a noncolor from the current expression to object~|cp|@>=
22727 {
22728 cyan_val(cp)=0;
22729 magenta_val(cp)=0;
22730 yellow_val(cp)=0;
22731 black_val(cp)=0;
22732 grey_val(cp)=0;
22733 color_model(cp)=mp_no_model;
22734 }
22735
22736 @ @<Make |cp| a colored object in object list~|p|@>=
22737 { cp=p;
22738   while ( cp!=null ){ 
22739     if ( has_color(cp) ) break;
22740     cp=link(cp);
22741   }
22742 }
22743
22744 @ @<Make |pp| an object in list~|p| that needs a pen@>=
22745 { pp=p;
22746   while ( pp!=null ) {
22747     if ( has_pen(pp) ) break;
22748     pp=link(pp);
22749   }
22750 }
22751
22752 @ @<Make |dp| a stroked node in list~|p|@>=
22753 { dp=p;
22754   while ( dp!=null ) {
22755     if ( type(dp)==mp_stroked_code ) break;
22756     dp=link(dp);
22757   }
22758 }
22759
22760 @ @<Copy the information from objects |cp|, |pp|, and |dp| into...@>=
22761 @<Copy |cp|'s color into the colored objects linked to~|cp|@>;
22762 if ( pp>mp_void ) {
22763   @<Copy |pen_p(pp)| into stroked and filled nodes linked to |pp|@>;
22764 }
22765 if ( dp>mp_void ) {
22766   @<Make stroked nodes linked to |dp| refer to |dash_p(dp)|@>;
22767 }
22768
22769
22770 @ @<Copy |cp|'s color into the colored objects linked to~|cp|@>=
22771 { q=link(cp);
22772   while ( q!=null ) { 
22773     if ( has_color(q) ) {
22774       red_val(q)=red_val(cp);
22775       green_val(q)=green_val(cp);
22776       blue_val(q)=blue_val(cp);
22777       black_val(q)=black_val(cp);
22778       color_model(q)=color_model(cp);
22779     }
22780     q=link(q);
22781   }
22782 }
22783
22784 @ @<Copy |pen_p(pp)| into stroked and filled nodes linked to |pp|@>=
22785 { q=link(pp);
22786   while ( q!=null ) {
22787     if ( has_pen(q) ) {
22788       if ( pen_p(q)!=null ) mp_toss_knot_list(mp, pen_p(q));
22789       pen_p(q)=copy_pen(pen_p(pp));
22790     }
22791     q=link(q);
22792   }
22793 }
22794
22795 @ @<Make stroked nodes linked to |dp| refer to |dash_p(dp)|@>=
22796 { q=link(dp);
22797   while ( q!=null ) {
22798     if ( type(q)==mp_stroked_code ) {
22799       if ( dash_p(q)!=null ) delete_edge_ref(dash_p(q));
22800       dash_p(q)=dash_p(dp);
22801       dash_scale(q)=unity;
22802       if ( dash_p(q)!=null ) add_edge_ref(dash_p(q));
22803     }
22804     q=link(q);
22805   }
22806 }
22807
22808 @ One of the things we need to do when we've parsed an \&{addto} or
22809 similar command is find the header of a supposed \&{picture} variable, given
22810 a token list for that variable.  Since the edge structure is about to be
22811 updated, we use |private_edges| to make sure that this is possible.
22812
22813 @<Declare action procedures for use by |do_statement|@>=
22814 pointer mp_find_edges_var (MP mp, pointer t) ;
22815
22816 @ @c pointer mp_find_edges_var (MP mp, pointer t) {
22817   pointer p;
22818   pointer cur_edges; /* the return value */
22819   p=mp_find_variable(mp, t); cur_edges=null;
22820   if ( p==null ) { 
22821     mp_obliterated(mp, t); mp_put_get_error(mp);
22822   } else if ( type(p)!=mp_picture_type )  { 
22823     print_err("Variable "); mp_show_token_list(mp, t,null,1000,0);
22824 @.Variable x is the wrong type@>
22825     mp_print(mp, " is the wrong type ("); 
22826     mp_print_type(mp, type(p)); mp_print_char(mp, ')');
22827     help2("I was looking for a \"known\" picture variable.")
22828          ("So I'll not change anything just now."); 
22829     mp_put_get_error(mp);
22830   } else { 
22831     value(p)=mp_private_edges(mp, value(p));
22832     cur_edges=value(p);
22833   }
22834   mp_flush_node_list(mp, t);
22835   return cur_edges;
22836 }
22837
22838 @ @<Cases of |do_statement|...@>=
22839 case add_to_command: mp_do_add_to(mp); break;
22840 case bounds_command:mp_do_bounds(mp); break;
22841
22842 @ @<Put each...@>=
22843 mp_primitive(mp, "clip",bounds_command,mp_start_clip_code);
22844 @:clip_}{\&{clip} primitive@>
22845 mp_primitive(mp, "setbounds",bounds_command,mp_start_bounds_code);
22846 @:set_bounds_}{\&{setbounds} primitive@>
22847
22848 @ @<Cases of |print_cmd...@>=
22849 case bounds_command: 
22850   if ( m==mp_start_clip_code ) mp_print(mp, "clip");
22851   else mp_print(mp, "setbounds");
22852   break;
22853
22854 @ The following function parses the beginning of an \&{addto} or \&{clip}
22855 command: it expects a variable name followed by a token with |cur_cmd=sep|
22856 and then an expression.  The function returns the token list for the variable
22857 and stores the command modifier for the separator token in the global variable
22858 |last_add_type|.  We must be careful because this variable might get overwritten
22859 any time we call |get_x_next|.
22860
22861 @<Glob...@>=
22862 quarterword last_add_type;
22863   /* command modifier that identifies the last \&{addto} command */
22864
22865 @ @<Declare action procedures for use by |do_statement|@>=
22866 pointer mp_start_draw_cmd (MP mp,quarterword sep) ;
22867
22868 @ @c pointer mp_start_draw_cmd (MP mp,quarterword sep) {
22869   pointer lhv; /* variable to add to left */
22870   quarterword add_type=0; /* value to be returned in |last_add_type| */
22871   lhv=null;
22872   mp_get_x_next(mp); mp->var_flag=sep; mp_scan_primary(mp);
22873   if ( mp->cur_type!=mp_token_list ) {
22874     @<Abandon edges command because there's no variable@>;
22875   } else  { 
22876     lhv=mp->cur_exp; add_type=mp->cur_mod;
22877     mp->cur_type=mp_vacuous; mp_get_x_next(mp); mp_scan_expression(mp);
22878   }
22879   mp->last_add_type=add_type;
22880   return lhv;
22881 }
22882
22883 @ @<Abandon edges command because there's no variable@>=
22884 { exp_err("Not a suitable variable");
22885 @.Not a suitable variable@>
22886   help4("At this point I needed to see the name of a picture variable.")
22887     ("(Or perhaps you have indeed presented me with one; I might")
22888     ("have missed it, if it wasn't followed by the proper token.)")
22889     ("So I'll not change anything just now.");
22890   mp_put_get_flush_error(mp, 0);
22891 }
22892
22893 @ Here is an example of how to use |start_draw_cmd|.
22894
22895 @<Declare action procedures for use by |do_statement|@>=
22896 void mp_do_bounds (MP mp) ;
22897
22898 @ @c void mp_do_bounds (MP mp) {
22899   pointer lhv,lhe; /* variable on left, the corresponding edge structure */
22900   pointer p; /* for list manipulation */
22901   integer m; /* initial value of |cur_mod| */
22902   m=mp->cur_mod;
22903   lhv=mp_start_draw_cmd(mp, to_token);
22904   if ( lhv!=null ) {
22905     lhe=mp_find_edges_var(mp, lhv);
22906     if ( lhe==null ) {
22907       mp_flush_cur_exp(mp, 0);
22908     } else if ( mp->cur_type!=mp_path_type ) {
22909       exp_err("Improper `clip'");
22910 @.Improper `addto'@>
22911       help2("This expression should have specified a known path.")
22912         ("So I'll not change anything just now."); 
22913       mp_put_get_flush_error(mp, 0);
22914     } else if ( left_type(mp->cur_exp)==mp_endpoint ) {
22915       @<Complain about a non-cycle@>;
22916     } else {
22917       @<Make |cur_exp| into a \&{setbounds} or clipping path and add it to |lhe|@>;
22918     }
22919   }
22920 }
22921
22922 @ @<Complain about a non-cycle@>=
22923 { print_err("Not a cycle");
22924 @.Not a cycle@>
22925   help2("That contour should have ended with `..cycle' or `&cycle'.")
22926     ("So I'll not change anything just now."); mp_put_get_error(mp);
22927 }
22928
22929 @ @<Make |cur_exp| into a \&{setbounds} or clipping path and add...@>=
22930 { p=mp_new_bounds_node(mp, mp->cur_exp,m);
22931   link(p)=link(dummy_loc(lhe));
22932   link(dummy_loc(lhe))=p;
22933   if ( obj_tail(lhe)==dummy_loc(lhe) ) obj_tail(lhe)=p;
22934   p=mp_get_node(mp, mp->gr_object_size[stop_type(m)]);
22935   type(p)=stop_type(m);
22936   link(obj_tail(lhe))=p;
22937   obj_tail(lhe)=p;
22938   mp_init_bbox(mp, lhe);
22939 }
22940
22941 @ The |do_add_to| procedure is a little like |do_clip| but there are a lot more
22942 cases to deal with.
22943
22944 @<Declare action procedures for use by |do_statement|@>=
22945 void mp_do_add_to (MP mp) ;
22946
22947 @ @c void mp_do_add_to (MP mp) {
22948   pointer lhv,lhe; /* variable on left, the corresponding edge structure */
22949   pointer p; /* the graphical object or list for |scan_with_list| to update */
22950   pointer e; /* an edge structure to be merged */
22951   quarterword add_type; /* |also_code|, |contour_code|, or |double_path_code| */
22952   lhv=mp_start_draw_cmd(mp, thing_to_add); add_type=mp->last_add_type;
22953   if ( lhv!=null ) {
22954     if ( add_type==also_code ) {
22955       @<Make sure the current expression is a suitable picture and set |e| and |p|
22956        appropriately@>;
22957     } else {
22958       @<Create a graphical object |p| based on |add_type| and the current
22959         expression@>;
22960     }
22961     mp_scan_with_list(mp, p);
22962     @<Use |p|, |e|, and |add_type| to augment |lhv| as requested@>;
22963   }
22964 }
22965
22966 @ Setting |p:=null| causes the $\langle$with list$\rangle$ to be ignored;
22967 setting |e:=null| prevents anything from being added to |lhe|.
22968
22969 @ @<Make sure the current expression is a suitable picture and set |e|...@>=
22970
22971   p=null; e=null;
22972   if ( mp->cur_type!=mp_picture_type ) {
22973     exp_err("Improper `addto'");
22974 @.Improper `addto'@>
22975     help2("This expression should have specified a known picture.")
22976       ("So I'll not change anything just now."); mp_put_get_flush_error(mp, 0);
22977   } else { 
22978     e=mp_private_edges(mp, mp->cur_exp); mp->cur_type=mp_vacuous;
22979     p=link(dummy_loc(e));
22980   }
22981 }
22982
22983 @ In this case |add_type<>also_code| so setting |p:=null| suppresses future
22984 attempts to add to the edge structure.
22985
22986 @<Create a graphical object |p| based on |add_type| and the current...@>=
22987 { e=null; p=null;
22988   if ( mp->cur_type==mp_pair_type ) mp_pair_to_path(mp);
22989   if ( mp->cur_type!=mp_path_type ) {
22990     exp_err("Improper `addto'");
22991 @.Improper `addto'@>
22992     help2("This expression should have specified a known path.")
22993       ("So I'll not change anything just now."); 
22994     mp_put_get_flush_error(mp, 0);
22995   } else if ( add_type==contour_code ) {
22996     if ( left_type(mp->cur_exp)==mp_endpoint ) {
22997       @<Complain about a non-cycle@>;
22998     } else { 
22999       p=mp_new_fill_node(mp, mp->cur_exp);
23000       mp->cur_type=mp_vacuous;
23001     }
23002   } else { 
23003     p=mp_new_stroked_node(mp, mp->cur_exp);
23004     mp->cur_type=mp_vacuous;
23005   }
23006 }
23007
23008 @ @<Use |p|, |e|, and |add_type| to augment |lhv| as requested@>=
23009 lhe=mp_find_edges_var(mp, lhv);
23010 if ( lhe==null ) {
23011   if ( (e==null)&&(p!=null) ) e=mp_toss_gr_object(mp, p);
23012   if ( e!=null ) delete_edge_ref(e);
23013 } else if ( add_type==also_code ) {
23014   if ( e!=null ) {
23015     @<Merge |e| into |lhe| and delete |e|@>;
23016   } else { 
23017     do_nothing;
23018   }
23019 } else if ( p!=null ) {
23020   link(obj_tail(lhe))=p;
23021   obj_tail(lhe)=p;
23022   if ( add_type==double_path_code )
23023     if ( pen_p(p)==null ) 
23024       pen_p(p)=mp_get_pen_circle(mp, 0);
23025 }
23026
23027 @ @<Merge |e| into |lhe| and delete |e|@>=
23028 { if ( link(dummy_loc(e))!=null ) {
23029     link(obj_tail(lhe))=link(dummy_loc(e));
23030     obj_tail(lhe)=obj_tail(e);
23031     obj_tail(e)=dummy_loc(e);
23032     link(dummy_loc(e))=null;
23033     mp_flush_dash_list(mp, lhe);
23034   }
23035   mp_toss_edges(mp, e);
23036 }
23037
23038 @ @<Cases of |do_statement|...@>=
23039 case ship_out_command: mp_do_ship_out(mp); break;
23040
23041 @ @<Declare action procedures for use by |do_statement|@>=
23042 @<Declare the function called |tfm_check|@>
23043 @<Declare the \ps\ output procedures@>
23044 void mp_do_ship_out (MP mp) ;
23045
23046 @ @c void mp_do_ship_out (MP mp) {
23047   integer c; /* the character code */
23048   mp_get_x_next(mp); mp_scan_expression(mp);
23049   if ( mp->cur_type!=mp_picture_type ) {
23050     @<Complain that it's not a known picture@>;
23051   } else { 
23052     c=mp_round_unscaled(mp, mp->internal[mp_char_code]) % 256;
23053     if ( c<0 ) c=c+256;
23054     @<Store the width information for character code~|c|@>;
23055     mp_ship_out(mp, mp->cur_exp);
23056     mp_flush_cur_exp(mp, 0);
23057   }
23058 }
23059
23060 @ @<Complain that it's not a known picture@>=
23061
23062   exp_err("Not a known picture");
23063   help1("I can only output known pictures.");
23064   mp_put_get_flush_error(mp, 0);
23065 }
23066
23067 @ The \&{everyjob} command simply assigns a nonzero value to the global variable
23068 |start_sym|.
23069
23070 @<Cases of |do_statement|...@>=
23071 case every_job_command: 
23072   mp_get_symbol(mp); mp->start_sym=mp->cur_sym; mp_get_x_next(mp);
23073   break;
23074
23075 @ @<Glob...@>=
23076 halfword start_sym; /* a symbolic token to insert at beginning of job */
23077
23078 @ @<Set init...@>=
23079 mp->start_sym=0;
23080
23081 @ Finally, we have only the ``message'' commands remaining.
23082
23083 @d message_code 0
23084 @d err_message_code 1
23085 @d err_help_code 2
23086 @d filename_template_code 3
23087 @d print_with_leading_zeroes(A)  g = mp->pool_ptr;
23088               mp_print_int(mp, (A)); g = mp->pool_ptr-g;
23089               if ( f>g ) {
23090                 mp->pool_ptr = mp->pool_ptr - g;
23091                 while ( f>g ) {
23092                   mp_print_char(mp, '0');
23093                   decr(f);
23094                   };
23095                 mp_print_int(mp, (A));
23096               };
23097               f = 0
23098
23099 @<Put each...@>=
23100 mp_primitive(mp, "message",message_command,message_code);
23101 @:message_}{\&{message} primitive@>
23102 mp_primitive(mp, "errmessage",message_command,err_message_code);
23103 @:err_message_}{\&{errmessage} primitive@>
23104 mp_primitive(mp, "errhelp",message_command,err_help_code);
23105 @:err_help_}{\&{errhelp} primitive@>
23106 mp_primitive(mp, "filenametemplate",message_command,filename_template_code);
23107 @:filename_template_}{\&{filenametemplate} primitive@>
23108
23109 @ @<Cases of |print_cmd...@>=
23110 case message_command: 
23111   if ( m<err_message_code ) mp_print(mp, "message");
23112   else if ( m==err_message_code ) mp_print(mp, "errmessage");
23113   else if ( m==filename_template_code ) mp_print(mp, "filenametemplate");
23114   else mp_print(mp, "errhelp");
23115   break;
23116
23117 @ @<Cases of |do_statement|...@>=
23118 case message_command: mp_do_message(mp); break;
23119
23120 @ @<Declare action procedures for use by |do_statement|@>=
23121 @<Declare a procedure called |no_string_err|@>
23122 void mp_do_message (MP mp) ;
23123
23124
23125 @c void mp_do_message (MP mp) {
23126   int m; /* the type of message */
23127   m=mp->cur_mod; mp_get_x_next(mp); mp_scan_expression(mp);
23128   if ( mp->cur_type!=mp_string_type )
23129     mp_no_string_err(mp, "A message should be a known string expression.");
23130   else {
23131     switch (m) {
23132     case message_code: 
23133       mp_print_nl(mp, ""); mp_print_str(mp, mp->cur_exp);
23134       break;
23135     case err_message_code:
23136       @<Print string |cur_exp| as an error message@>;
23137       break;
23138     case err_help_code:
23139       @<Save string |cur_exp| as the |err_help|@>;
23140       break;
23141     case filename_template_code:
23142       @<Save the filename template@>;
23143       break;
23144     } /* there are no other cases */
23145   }
23146   mp_flush_cur_exp(mp, 0);
23147 }
23148
23149 @ @<Declare a procedure called |no_string_err|@>=
23150 void mp_no_string_err (MP mp, const char *s) { 
23151    exp_err("Not a string");
23152 @.Not a string@>
23153   help1(s);
23154   mp_put_get_error(mp);
23155 }
23156
23157 @ The global variable |err_help| is zero when the user has most recently
23158 given an empty help string, or if none has ever been given.
23159
23160 @<Save string |cur_exp| as the |err_help|@>=
23161
23162   if ( mp->err_help!=0 ) delete_str_ref(mp->err_help);
23163   if ( length(mp->cur_exp)==0 ) mp->err_help=0;
23164   else  { mp->err_help=mp->cur_exp; add_str_ref(mp->err_help); }
23165 }
23166
23167 @ If \&{errmessage} occurs often in |mp_scroll_mode|, without user-defined
23168 \&{errhelp}, we don't want to give a long help message each time. So we
23169 give a verbose explanation only once.
23170
23171 @<Glob...@>=
23172 boolean long_help_seen; /* has the long \.{\\errmessage} help been used? */
23173
23174 @ @<Set init...@>=mp->long_help_seen=false;
23175
23176 @ @<Print string |cur_exp| as an error message@>=
23177
23178   print_err(""); mp_print_str(mp, mp->cur_exp);
23179   if ( mp->err_help!=0 ) {
23180     mp->use_err_help=true;
23181   } else if ( mp->long_help_seen ) { 
23182     help1("(That was another `errmessage'.)") ; 
23183   } else  { 
23184    if ( mp->interaction<mp_error_stop_mode ) mp->long_help_seen=true;
23185     help4("This error message was generated by an `errmessage'")
23186      ("command, so I can\'t give any explicit help.")
23187      ("Pretend that you're Miss Marple: Examine all clues,")
23188 @^Marple, Jane@>
23189      ("and deduce the truth by inspired guesses.");
23190   }
23191   mp_put_get_error(mp); mp->use_err_help=false;
23192 }
23193
23194 @ @<Cases of |do_statement|...@>=
23195 case write_command: mp_do_write(mp); break;
23196
23197 @ @<Declare action procedures for use by |do_statement|@>=
23198 void mp_do_write (MP mp) ;
23199
23200 @ @c void mp_do_write (MP mp) {
23201   str_number t; /* the line of text to be written */
23202   write_index n,n0; /* for searching |wr_fname| and |wr_file| arrays */
23203   int old_setting; /* for saving |selector| during output */
23204   mp_get_x_next(mp);
23205   mp_scan_expression(mp);
23206   if ( mp->cur_type!=mp_string_type ) {
23207     mp_no_string_err(mp, "The text to be written should be a known string expression");
23208   } else if ( mp->cur_cmd!=to_token ) { 
23209     print_err("Missing `to' clause");
23210     help1("A write command should end with `to <filename>'");
23211     mp_put_get_error(mp);
23212   } else { 
23213     t=mp->cur_exp; mp->cur_type=mp_vacuous;
23214     mp_get_x_next(mp);
23215     mp_scan_expression(mp);
23216     if ( mp->cur_type!=mp_string_type )
23217       mp_no_string_err(mp, "I can\'t write to that file name.  It isn't a known string");
23218     else {
23219       @<Write |t| to the file named by |cur_exp|@>;
23220     }
23221     delete_str_ref(t);
23222   }
23223   mp_flush_cur_exp(mp, 0);
23224 }
23225
23226 @ @<Write |t| to the file named by |cur_exp|@>=
23227
23228   @<Find |n| where |wr_fname[n]=cur_exp| and call |open_write_file| if
23229     |cur_exp| must be inserted@>;
23230   if ( mp_str_vs_str(mp, t,mp->eof_line)==0 ) {
23231     @<Record the end of file on |wr_file[n]|@>;
23232   } else { 
23233     old_setting=mp->selector;
23234     mp->selector=n+write_file;
23235     mp_print_str(mp, t); mp_print_ln(mp);
23236     mp->selector = old_setting;
23237   }
23238 }
23239
23240 @ @<Find |n| where |wr_fname[n]=cur_exp| and call |open_write_file| if...@>=
23241 {
23242   char *fn = str(mp->cur_exp);
23243   n=mp->write_files;
23244   n0=mp->write_files;
23245   while (mp_xstrcmp(fn,mp->wr_fname[n])!=0) { 
23246     if ( n==0 ) { /* bottom reached */
23247           if ( n0==mp->write_files ) {
23248         if ( mp->write_files<mp->max_write_files ) {
23249           incr(mp->write_files);
23250         } else {
23251           void **wr_file;
23252           char **wr_fname;
23253               write_index l,k;
23254           l = mp->max_write_files + (mp->max_write_files>>2);
23255           wr_file = xmalloc((l+1),sizeof(void *));
23256           wr_fname = xmalloc((l+1),sizeof(char *));
23257               for (k=0;k<=l;k++) {
23258             if (k<=mp->max_write_files) {
23259                   wr_file[k]=mp->wr_file[k]; 
23260               wr_fname[k]=mp->wr_fname[k];
23261             } else {
23262                   wr_file[k]=0; 
23263               wr_fname[k]=NULL;
23264             }
23265           }
23266               xfree(mp->wr_file); xfree(mp->wr_fname);
23267           mp->max_write_files = l;
23268           mp->wr_file = wr_file;
23269           mp->wr_fname = wr_fname;
23270         }
23271       }
23272       n=n0;
23273       mp_open_write_file(mp, fn ,n);
23274     } else { 
23275       decr(n);
23276           if ( mp->wr_fname[n]==NULL )  n0=n; 
23277     }
23278   }
23279 }
23280
23281 @ @<Record the end of file on |wr_file[n]|@>=
23282 { (mp->close_file)(mp,mp->wr_file[n]);
23283   xfree(mp->wr_fname[n]);
23284   if ( n==mp->write_files-1 ) mp->write_files=n;
23285 }
23286
23287
23288 @* \[42] Writing font metric data.
23289 \TeX\ gets its knowledge about fonts from font metric files, also called
23290 \.{TFM} files; the `\.T' in `\.{TFM}' stands for \TeX,
23291 but other programs know about them too. One of \MP's duties is to
23292 write \.{TFM} files so that the user's fonts can readily be
23293 applied to typesetting.
23294 @:TFM files}{\.{TFM} files@>
23295 @^font metric files@>
23296
23297 The information in a \.{TFM} file appears in a sequence of 8-bit bytes.
23298 Since the number of bytes is always a multiple of~4, we could
23299 also regard the file as a sequence of 32-bit words, but \MP\ uses the
23300 byte interpretation. The format of \.{TFM} files was designed by
23301 Lyle Ramshaw in 1980. The intent is to convey a lot of different kinds
23302 @^Ramshaw, Lyle Harold@>
23303 of information in a compact but useful form.
23304
23305 @<Glob...@>=
23306 void * tfm_file; /* the font metric output goes here */
23307 char * metric_file_name; /* full name of the font metric file */
23308
23309 @ The first 24 bytes (6 words) of a \.{TFM} file contain twelve 16-bit
23310 integers that give the lengths of the various subsequent portions
23311 of the file. These twelve integers are, in order:
23312 $$\vbox{\halign{\hfil#&$\null=\null$#\hfil\cr
23313 |lf|&length of the entire file, in words;\cr
23314 |lh|&length of the header data, in words;\cr
23315 |bc|&smallest character code in the font;\cr
23316 |ec|&largest character code in the font;\cr
23317 |nw|&number of words in the width table;\cr
23318 |nh|&number of words in the height table;\cr
23319 |nd|&number of words in the depth table;\cr
23320 |ni|&number of words in the italic correction table;\cr
23321 |nl|&number of words in the lig/kern table;\cr
23322 |nk|&number of words in the kern table;\cr
23323 |ne|&number of words in the extensible character table;\cr
23324 |np|&number of font parameter words.\cr}}$$
23325 They are all nonnegative and less than $2^{15}$. We must have |bc-1<=ec<=255|,
23326 |ne<=256|, and
23327 $$\hbox{|lf=6+lh+(ec-bc+1)+nw+nh+nd+ni+nl+nk+ne+np|.}$$
23328 Note that a font may contain as many as 256 characters (if |bc=0| and |ec=255|),
23329 and as few as 0 characters (if |bc=ec+1|).
23330
23331 Incidentally, when two or more 8-bit bytes are combined to form an integer of
23332 16 or more bits, the most significant bytes appear first in the file.
23333 This is called BigEndian order.
23334 @^BigEndian order@>
23335
23336 @ The rest of the \.{TFM} file may be regarded as a sequence of ten data
23337 arrays.
23338
23339 The most important data type used here is a |fix_word|, which is
23340 a 32-bit representation of a binary fraction. A |fix_word| is a signed
23341 quantity, with the two's complement of the entire word used to represent
23342 negation. Of the 32 bits in a |fix_word|, exactly 12 are to the left of the
23343 binary point; thus, the largest |fix_word| value is $2048-2^{-20}$, and
23344 the smallest is $-2048$. We will see below, however, that all but two of
23345 the |fix_word| values must lie between $-16$ and $+16$.
23346
23347 @ The first data array is a block of header information, which contains
23348 general facts about the font. The header must contain at least two words,
23349 |header[0]| and |header[1]|, whose meaning is explained below.  Additional
23350 header information of use to other software routines might also be
23351 included, and \MP\ will generate it if the \.{headerbyte} command occurs.
23352 For example, 16 more words of header information are in use at the Xerox
23353 Palo Alto Research Center; the first ten specify the character coding
23354 scheme used (e.g., `\.{XEROX TEXT}' or `\.{TEX MATHSY}'), the next five
23355 give the font family name (e.g., `\.{HELVETICA}' or `\.{CMSY}'), and the
23356 last gives the ``face byte.''
23357
23358 \yskip\hang|header[0]| is a 32-bit check sum that \MP\ will copy into
23359 the \.{GF} output file. This helps ensure consistency between files,
23360 since \TeX\ records the check sums from the \.{TFM}'s it reads, and these
23361 should match the check sums on actual fonts that are used.  The actual
23362 relation between this check sum and the rest of the \.{TFM} file is not
23363 important; the check sum is simply an identification number with the
23364 property that incompatible fonts almost always have distinct check sums.
23365 @^check sum@>
23366
23367 \yskip\hang|header[1]| is a |fix_word| containing the design size of the
23368 font, in units of \TeX\ points. This number must be at least 1.0; it is
23369 fairly arbitrary, but usually the design size is 10.0 for a ``10 point''
23370 font, i.e., a font that was designed to look best at a 10-point size,
23371 whatever that really means. When a \TeX\ user asks for a font `\.{at}
23372 $\delta$ \.{pt}', the effect is to override the design size and replace it
23373 by $\delta$, and to multiply the $x$ and~$y$ coordinates of the points in
23374 the font image by a factor of $\delta$ divided by the design size.  {\sl
23375 All other dimensions in the\/ \.{TFM} file are |fix_word|\kern-1pt\
23376 numbers in design-size units.} Thus, for example, the value of |param[6]|,
23377 which defines the \.{em} unit, is often the |fix_word| value $2^{20}=1.0$,
23378 since many fonts have a design size equal to one em.  The other dimensions
23379 must be less than 16 design-size units in absolute value; thus,
23380 |header[1]| and |param[1]| are the only |fix_word| entries in the whole
23381 \.{TFM} file whose first byte might be something besides 0 or 255.
23382 @^design size@>
23383
23384 @ Next comes the |char_info| array, which contains one |char_info_word|
23385 per character. Each word in this part of the file contains six fields
23386 packed into four bytes as follows.
23387
23388 \yskip\hang first byte: |width_index| (8 bits)\par
23389 \hang second byte: |height_index| (4 bits) times 16, plus |depth_index|
23390   (4~bits)\par
23391 \hang third byte: |italic_index| (6 bits) times 4, plus |tag|
23392   (2~bits)\par
23393 \hang fourth byte: |remainder| (8 bits)\par
23394 \yskip\noindent
23395 The actual width of a character is \\{width}|[width_index]|, in design-size
23396 units; this is a device for compressing information, since many characters
23397 have the same width. Since it is quite common for many characters
23398 to have the same height, depth, or italic correction, the \.{TFM} format
23399 imposes a limit of 16 different heights, 16 different depths, and
23400 64 different italic corrections.
23401
23402 Incidentally, the relation $\\{width}[0]=\\{height}[0]=\\{depth}[0]=
23403 \\{italic}[0]=0$ should always hold, so that an index of zero implies a
23404 value of zero.  The |width_index| should never be zero unless the
23405 character does not exist in the font, since a character is valid if and
23406 only if it lies between |bc| and |ec| and has a nonzero |width_index|.
23407
23408 @ The |tag| field in a |char_info_word| has four values that explain how to
23409 interpret the |remainder| field.
23410
23411 \yskip\hang|tag=0| (|no_tag|) means that |remainder| is unused.\par
23412 \hang|tag=1| (|lig_tag|) means that this character has a ligature/kerning
23413 program starting at location |remainder| in the |lig_kern| array.\par
23414 \hang|tag=2| (|list_tag|) means that this character is part of a chain of
23415 characters of ascending sizes, and not the largest in the chain.  The
23416 |remainder| field gives the character code of the next larger character.\par
23417 \hang|tag=3| (|ext_tag|) means that this character code represents an
23418 extensible character, i.e., a character that is built up of smaller pieces
23419 so that it can be made arbitrarily large. The pieces are specified in
23420 |exten[remainder]|.\par
23421 \yskip\noindent
23422 Characters with |tag=2| and |tag=3| are treated as characters with |tag=0|
23423 unless they are used in special circumstances in math formulas. For example,
23424 \TeX's \.{\\sum} operation looks for a |list_tag|, and the \.{\\left}
23425 operation looks for both |list_tag| and |ext_tag|.
23426
23427 @d no_tag 0 /* vanilla character */
23428 @d lig_tag 1 /* character has a ligature/kerning program */
23429 @d list_tag 2 /* character has a successor in a charlist */
23430 @d ext_tag 3 /* character is extensible */
23431
23432 @ The |lig_kern| array contains instructions in a simple programming language
23433 that explains what to do for special letter pairs. Each word in this array is a
23434 |lig_kern_command| of four bytes.
23435
23436 \yskip\hang first byte: |skip_byte|, indicates that this is the final program
23437   step if the byte is 128 or more, otherwise the next step is obtained by
23438   skipping this number of intervening steps.\par
23439 \hang second byte: |next_char|, ``if |next_char| follows the current character,
23440   then perform the operation and stop, otherwise continue.''\par
23441 \hang third byte: |op_byte|, indicates a ligature step if less than~128,
23442   a kern step otherwise.\par
23443 \hang fourth byte: |remainder|.\par
23444 \yskip\noindent
23445 In a kern step, an
23446 additional space equal to |kern[256*(op_byte-128)+remainder]| is inserted
23447 between the current character and |next_char|. This amount is
23448 often negative, so that the characters are brought closer together
23449 by kerning; but it might be positive.
23450
23451 There are eight kinds of ligature steps, having |op_byte| codes $4a+2b+c$ where
23452 $0\le a\le b+c$ and $0\le b,c\le1$. The character whose code is
23453 |remainder| is inserted between the current character and |next_char|;
23454 then the current character is deleted if $b=0$, and |next_char| is
23455 deleted if $c=0$; then we pass over $a$~characters to reach the next
23456 current character (which may have a ligature/kerning program of its own).
23457
23458 If the very first instruction of the |lig_kern| array has |skip_byte=255|,
23459 the |next_char| byte is the so-called right boundary character of this font;
23460 the value of |next_char| need not lie between |bc| and~|ec|.
23461 If the very last instruction of the |lig_kern| array has |skip_byte=255|,
23462 there is a special ligature/kerning program for a left boundary character,
23463 beginning at location |256*op_byte+remainder|.
23464 The interpretation is that \TeX\ puts implicit boundary characters
23465 before and after each consecutive string of characters from the same font.
23466 These implicit characters do not appear in the output, but they can affect
23467 ligatures and kerning.
23468
23469 If the very first instruction of a character's |lig_kern| program has
23470 |skip_byte>128|, the program actually begins in location
23471 |256*op_byte+remainder|. This feature allows access to large |lig_kern|
23472 arrays, because the first instruction must otherwise
23473 appear in a location |<=255|.
23474
23475 Any instruction with |skip_byte>128| in the |lig_kern| array must satisfy
23476 the condition
23477 $$\hbox{|256*op_byte+remainder<nl|.}$$
23478 If such an instruction is encountered during
23479 normal program execution, it denotes an unconditional halt; no ligature
23480 command is performed.
23481
23482 @d stop_flag (128)
23483   /* value indicating `\.{STOP}' in a lig/kern program */
23484 @d kern_flag (128) /* op code for a kern step */
23485 @d skip_byte(A) mp->lig_kern[(A)].b0
23486 @d next_char(A) mp->lig_kern[(A)].b1
23487 @d op_byte(A) mp->lig_kern[(A)].b2
23488 @d rem_byte(A) mp->lig_kern[(A)].b3
23489
23490 @ Extensible characters are specified by an |extensible_recipe|, which
23491 consists of four bytes called |top|, |mid|, |bot|, and |rep| (in this
23492 order). These bytes are the character codes of individual pieces used to
23493 build up a large symbol.  If |top|, |mid|, or |bot| are zero, they are not
23494 present in the built-up result. For example, an extensible vertical line is
23495 like an extensible bracket, except that the top and bottom pieces are missing.
23496
23497 Let $T$, $M$, $B$, and $R$ denote the respective pieces, or an empty box
23498 if the piece isn't present. Then the extensible characters have the form
23499 $TR^kMR^kB$ from top to bottom, for some |k>=0|, unless $M$ is absent;
23500 in the latter case we can have $TR^kB$ for both even and odd values of~|k|.
23501 The width of the extensible character is the width of $R$; and the
23502 height-plus-depth is the sum of the individual height-plus-depths of the
23503 components used, since the pieces are butted together in a vertical list.
23504
23505 @d ext_top(A) mp->exten[(A)].b0 /* |top| piece in a recipe */
23506 @d ext_mid(A) mp->exten[(A)].b1 /* |mid| piece in a recipe */
23507 @d ext_bot(A) mp->exten[(A)].b2 /* |bot| piece in a recipe */
23508 @d ext_rep(A) mp->exten[(A)].b3 /* |rep| piece in a recipe */
23509
23510 @ The final portion of a \.{TFM} file is the |param| array, which is another
23511 sequence of |fix_word| values.
23512
23513 \yskip\hang|param[1]=slant| is the amount of italic slant, which is used
23514 to help position accents. For example, |slant=.25| means that when you go
23515 up one unit, you also go .25 units to the right. The |slant| is a pure
23516 number; it is the only |fix_word| other than the design size itself that is
23517 not scaled by the design size.
23518 @^design size@>
23519
23520 \hang|param[2]=space| is the normal spacing between words in text.
23521 Note that character 040 in the font need not have anything to do with
23522 blank spaces.
23523
23524 \hang|param[3]=space_stretch| is the amount of glue stretching between words.
23525
23526 \hang|param[4]=space_shrink| is the amount of glue shrinking between words.
23527
23528 \hang|param[5]=x_height| is the size of one ex in the font; it is also
23529 the height of letters for which accents don't have to be raised or lowered.
23530
23531 \hang|param[6]=quad| is the size of one em in the font.
23532
23533 \hang|param[7]=extra_space| is the amount added to |param[2]| at the
23534 ends of sentences.
23535
23536 \yskip\noindent
23537 If fewer than seven parameters are present, \TeX\ sets the missing parameters
23538 to zero.
23539
23540 @d slant_code 1
23541 @d space_code 2
23542 @d space_stretch_code 3
23543 @d space_shrink_code 4
23544 @d x_height_code 5
23545 @d quad_code 6
23546 @d extra_space_code 7
23547
23548 @ So that is what \.{TFM} files hold. One of \MP's duties is to output such
23549 information, and it does this all at once at the end of a job.
23550 In order to prepare for such frenetic activity, it squirrels away the
23551 necessary facts in various arrays as information becomes available.
23552
23553 Character dimensions (\&{charwd}, \&{charht}, \&{chardp}, and \&{charic})
23554 are stored respectively in |tfm_width|, |tfm_height|, |tfm_depth|, and
23555 |tfm_ital_corr|. Other information about a character (e.g., about
23556 its ligatures or successors) is accessible via the |char_tag| and
23557 |char_remainder| arrays. Other information about the font as a whole
23558 is kept in additional arrays called |header_byte|, |lig_kern|,
23559 |kern|, |exten|, and |param|.
23560
23561 @d max_tfm_int 32510
23562 @d undefined_label max_tfm_int /* an undefined local label */
23563
23564 @<Glob...@>=
23565 #define TFM_ITEMS 257
23566 eight_bits bc;
23567 eight_bits ec; /* smallest and largest character codes shipped out */
23568 scaled tfm_width[TFM_ITEMS]; /* \&{charwd} values */
23569 scaled tfm_height[TFM_ITEMS]; /* \&{charht} values */
23570 scaled tfm_depth[TFM_ITEMS]; /* \&{chardp} values */
23571 scaled tfm_ital_corr[TFM_ITEMS]; /* \&{charic} values */
23572 boolean char_exists[TFM_ITEMS]; /* has this code been shipped out? */
23573 int char_tag[TFM_ITEMS]; /* |remainder| category */
23574 int char_remainder[TFM_ITEMS]; /* the |remainder| byte */
23575 char *header_byte; /* bytes of the \.{TFM} header */
23576 int header_last; /* last initialized \.{TFM} header byte */
23577 int header_size; /* size of the \.{TFM} header */
23578 four_quarters *lig_kern; /* the ligature/kern table */
23579 short nl; /* the number of ligature/kern steps so far */
23580 scaled *kern; /* distinct kerning amounts */
23581 short nk; /* the number of distinct kerns so far */
23582 four_quarters exten[TFM_ITEMS]; /* extensible character recipes */
23583 short ne; /* the number of extensible characters so far */
23584 scaled *param; /* \&{fontinfo} parameters */
23585 short np; /* the largest \&{fontinfo} parameter specified so far */
23586 short nw;short nh;short nd;short ni; /* sizes of \.{TFM} subtables */
23587 short skip_table[TFM_ITEMS]; /* local label status */
23588 boolean lk_started; /* has there been a lig/kern step in this command yet? */
23589 integer bchar; /* right boundary character */
23590 short bch_label; /* left boundary starting location */
23591 short ll;short lll; /* registers used for lig/kern processing */
23592 short label_loc[257]; /* lig/kern starting addresses */
23593 eight_bits label_char[257]; /* characters for |label_loc| */
23594 short label_ptr; /* highest position occupied in |label_loc| */
23595
23596 @ @<Allocate or initialize ...@>=
23597 mp->header_last = 0; mp->header_size = 128; /* just for init */
23598 mp->header_byte = xmalloc(mp->header_size, sizeof(char));
23599 mp->lig_kern = NULL; /* allocated when needed */
23600 mp->kern = NULL; /* allocated when needed */ 
23601 mp->param = NULL; /* allocated when needed */
23602
23603 @ @<Dealloc variables@>=
23604 xfree(mp->header_byte);
23605 xfree(mp->lig_kern);
23606 xfree(mp->kern);
23607 xfree(mp->param);
23608
23609 @ @<Set init...@>=
23610 for (k=0;k<= 255;k++ ) {
23611   mp->tfm_width[k]=0; mp->tfm_height[k]=0; mp->tfm_depth[k]=0; mp->tfm_ital_corr[k]=0;
23612   mp->char_exists[k]=false; mp->char_tag[k]=no_tag; mp->char_remainder[k]=0;
23613   mp->skip_table[k]=undefined_label;
23614 }
23615 memset(mp->header_byte,0,mp->header_size);
23616 mp->bc=255; mp->ec=0; mp->nl=0; mp->nk=0; mp->ne=0; mp->np=0;
23617 mp->internal[mp_boundary_char]=-unity;
23618 mp->bch_label=undefined_label;
23619 mp->label_loc[0]=-1; mp->label_ptr=0;
23620
23621 @ @<Declarations@>=
23622 scaled mp_tfm_check (MP mp,small_number m) ;
23623
23624 @ @<Declare the function called |tfm_check|@>=
23625 scaled mp_tfm_check (MP mp,small_number m) {
23626   if ( abs(mp->internal[m])>=fraction_half ) {
23627     print_err("Enormous "); mp_print(mp, mp->int_name[m]);
23628 @.Enormous charwd...@>
23629 @.Enormous chardp...@>
23630 @.Enormous charht...@>
23631 @.Enormous charic...@>
23632 @.Enormous designsize...@>
23633     mp_print(mp, " has been reduced");
23634     help1("Font metric dimensions must be less than 2048pt.");
23635     mp_put_get_error(mp);
23636     if ( mp->internal[m]>0 ) return (fraction_half-1);
23637     else return (1-fraction_half);
23638   } else {
23639     return mp->internal[m];
23640   }
23641 }
23642
23643 @ @<Store the width information for character code~|c|@>=
23644 if ( c<mp->bc ) mp->bc=c;
23645 if ( c>mp->ec ) mp->ec=c;
23646 mp->char_exists[c]=true;
23647 mp->tfm_width[c]=mp_tfm_check(mp, mp_char_wd);
23648 mp->tfm_height[c]=mp_tfm_check(mp, mp_char_ht);
23649 mp->tfm_depth[c]=mp_tfm_check(mp, mp_char_dp);
23650 mp->tfm_ital_corr[c]=mp_tfm_check(mp, mp_char_ic)
23651
23652 @ Now let's consider \MP's special \.{TFM}-oriented commands.
23653
23654 @<Cases of |do_statement|...@>=
23655 case tfm_command: mp_do_tfm_command(mp); break;
23656
23657 @ @d char_list_code 0
23658 @d lig_table_code 1
23659 @d extensible_code 2
23660 @d header_byte_code 3
23661 @d font_dimen_code 4
23662
23663 @<Put each...@>=
23664 mp_primitive(mp, "charlist",tfm_command,char_list_code);
23665 @:char_list_}{\&{charlist} primitive@>
23666 mp_primitive(mp, "ligtable",tfm_command,lig_table_code);
23667 @:lig_table_}{\&{ligtable} primitive@>
23668 mp_primitive(mp, "extensible",tfm_command,extensible_code);
23669 @:extensible_}{\&{extensible} primitive@>
23670 mp_primitive(mp, "headerbyte",tfm_command,header_byte_code);
23671 @:header_byte_}{\&{headerbyte} primitive@>
23672 mp_primitive(mp, "fontdimen",tfm_command,font_dimen_code);
23673 @:font_dimen_}{\&{fontdimen} primitive@>
23674
23675 @ @<Cases of |print_cmd...@>=
23676 case tfm_command: 
23677   switch (m) {
23678   case char_list_code:mp_print(mp, "charlist"); break;
23679   case lig_table_code:mp_print(mp, "ligtable"); break;
23680   case extensible_code:mp_print(mp, "extensible"); break;
23681   case header_byte_code:mp_print(mp, "headerbyte"); break;
23682   default: mp_print(mp, "fontdimen"); break;
23683   }
23684   break;
23685
23686 @ @<Declare action procedures for use by |do_statement|@>=
23687 eight_bits mp_get_code (MP mp) ;
23688
23689 @ @c eight_bits mp_get_code (MP mp) { /* scans a character code value */
23690   integer c; /* the code value found */
23691   mp_get_x_next(mp); mp_scan_expression(mp);
23692   if ( mp->cur_type==mp_known ) { 
23693     c=mp_round_unscaled(mp, mp->cur_exp);
23694     if ( c>=0 ) if ( c<256 ) return c;
23695   } else if ( mp->cur_type==mp_string_type ) {
23696     if ( length(mp->cur_exp)==1 )  { 
23697       c=mp->str_pool[mp->str_start[mp->cur_exp]];
23698       return c;
23699     }
23700   }
23701   exp_err("Invalid code has been replaced by 0");
23702 @.Invalid code...@>
23703   help2("I was looking for a number between 0 and 255, or for a")
23704        ("string of length 1. Didn't find it; will use 0 instead.");
23705   mp_put_get_flush_error(mp, 0); c=0;
23706   return c;
23707 }
23708
23709 @ @<Declare action procedures for use by |do_statement|@>=
23710 void mp_set_tag (MP mp,halfword c, small_number t, halfword r) ;
23711
23712 @ @c void mp_set_tag (MP mp,halfword c, small_number t, halfword r) { 
23713   if ( mp->char_tag[c]==no_tag ) {
23714     mp->char_tag[c]=t; mp->char_remainder[c]=r;
23715     if ( t==lig_tag ){ 
23716       incr(mp->label_ptr); mp->label_loc[mp->label_ptr]=r; 
23717       mp->label_char[mp->label_ptr]=c;
23718     }
23719   } else {
23720     @<Complain about a character tag conflict@>;
23721   }
23722 }
23723
23724 @ @<Complain about a character tag conflict@>=
23725
23726   print_err("Character ");
23727   if ( (c>' ')&&(c<127) ) mp_print_char(mp,c);
23728   else if ( c==256 ) mp_print(mp, "||");
23729   else  { mp_print(mp, "code "); mp_print_int(mp, c); };
23730   mp_print(mp, " is already ");
23731 @.Character c is already...@>
23732   switch (mp->char_tag[c]) {
23733   case lig_tag: mp_print(mp, "in a ligtable"); break;
23734   case list_tag: mp_print(mp, "in a charlist"); break;
23735   case ext_tag: mp_print(mp, "extensible"); break;
23736   } /* there are no other cases */
23737   help2("It's not legal to label a character more than once.")
23738     ("So I'll not change anything just now.");
23739   mp_put_get_error(mp); 
23740 }
23741
23742 @ @<Declare action procedures for use by |do_statement|@>=
23743 void mp_do_tfm_command (MP mp) ;
23744
23745 @ @c void mp_do_tfm_command (MP mp) {
23746   int c,cc; /* character codes */
23747   int k; /* index into the |kern| array */
23748   int j; /* index into |header_byte| or |param| */
23749   switch (mp->cur_mod) {
23750   case char_list_code: 
23751     c=mp_get_code(mp);
23752      /* we will store a list of character successors */
23753     while ( mp->cur_cmd==colon )   { 
23754       cc=mp_get_code(mp); mp_set_tag(mp, c,list_tag,cc); c=cc;
23755     };
23756     break;
23757   case lig_table_code: 
23758     if (mp->lig_kern==NULL) 
23759        mp->lig_kern = xmalloc((max_tfm_int+1),sizeof(four_quarters));
23760     if (mp->kern==NULL) 
23761        mp->kern = xmalloc((max_tfm_int+1),sizeof(scaled));
23762     @<Store a list of ligature/kern steps@>;
23763     break;
23764   case extensible_code: 
23765     @<Define an extensible recipe@>;
23766     break;
23767   case header_byte_code: 
23768   case font_dimen_code: 
23769     c=mp->cur_mod; mp_get_x_next(mp);
23770     mp_scan_expression(mp);
23771     if ( (mp->cur_type!=mp_known)||(mp->cur_exp<half_unit) ) {
23772       exp_err("Improper location");
23773 @.Improper location@>
23774       help2("I was looking for a known, positive number.")
23775        ("For safety's sake I'll ignore the present command.");
23776       mp_put_get_error(mp);
23777     } else  { 
23778       j=mp_round_unscaled(mp, mp->cur_exp);
23779       if ( mp->cur_cmd!=colon ) {
23780         mp_missing_err(mp, ":");
23781 @.Missing `:'@>
23782         help1("A colon should follow a headerbyte or fontinfo location.");
23783         mp_back_error(mp);
23784       }
23785       if ( c==header_byte_code ) { 
23786         @<Store a list of header bytes@>;
23787       } else {     
23788         if (mp->param==NULL) 
23789           mp->param = xmalloc((max_tfm_int+1),sizeof(scaled));
23790         @<Store a list of font dimensions@>;
23791       }
23792     }
23793     break;
23794   } /* there are no other cases */
23795 }
23796
23797 @ @<Store a list of ligature/kern steps@>=
23798
23799   mp->lk_started=false;
23800 CONTINUE: 
23801   mp_get_x_next(mp);
23802   if ((mp->cur_cmd==skip_to)&& mp->lk_started )
23803     @<Process a |skip_to| command and |goto done|@>;
23804   if ( mp->cur_cmd==bchar_label ) { c=256; mp->cur_cmd=colon; }
23805   else { mp_back_input(mp); c=mp_get_code(mp); };
23806   if ((mp->cur_cmd==colon)||(mp->cur_cmd==double_colon)) {
23807     @<Record a label in a lig/kern subprogram and |goto continue|@>;
23808   }
23809   if ( mp->cur_cmd==lig_kern_token ) { 
23810     @<Compile a ligature/kern command@>; 
23811   } else  { 
23812     print_err("Illegal ligtable step");
23813 @.Illegal ligtable step@>
23814     help1("I was looking for `=:' or `kern' here.");
23815     mp_back_error(mp); next_char(mp->nl)=qi(0); 
23816     op_byte(mp->nl)=qi(0); rem_byte(mp->nl)=qi(0);
23817     skip_byte(mp->nl)=stop_flag+1; /* this specifies an unconditional stop */
23818   }
23819   if ( mp->nl==max_tfm_int) mp_fatal_error(mp, "ligtable too large");
23820   incr(mp->nl);
23821   if ( mp->cur_cmd==comma ) goto CONTINUE;
23822   if ( skip_byte(mp->nl-1)<stop_flag ) skip_byte(mp->nl-1)=stop_flag;
23823 }
23824 DONE:
23825
23826 @ @<Put each...@>=
23827 mp_primitive(mp, "=:",lig_kern_token,0);
23828 @:=:_}{\.{=:} primitive@>
23829 mp_primitive(mp, "=:|",lig_kern_token,1);
23830 @:=:/_}{\.{=:\char'174} primitive@>
23831 mp_primitive(mp, "=:|>",lig_kern_token,5);
23832 @:=:/>_}{\.{=:\char'174>} primitive@>
23833 mp_primitive(mp, "|=:",lig_kern_token,2);
23834 @:=:/_}{\.{\char'174=:} primitive@>
23835 mp_primitive(mp, "|=:>",lig_kern_token,6);
23836 @:=:/>_}{\.{\char'174=:>} primitive@>
23837 mp_primitive(mp, "|=:|",lig_kern_token,3);
23838 @:=:/_}{\.{\char'174=:\char'174} primitive@>
23839 mp_primitive(mp, "|=:|>",lig_kern_token,7);
23840 @:=:/>_}{\.{\char'174=:\char'174>} primitive@>
23841 mp_primitive(mp, "|=:|>>",lig_kern_token,11);
23842 @:=:/>_}{\.{\char'174=:\char'174>>} primitive@>
23843 mp_primitive(mp, "kern",lig_kern_token,128);
23844 @:kern_}{\&{kern} primitive@>
23845
23846 @ @<Cases of |print_cmd...@>=
23847 case lig_kern_token: 
23848   switch (m) {
23849   case 0:mp_print(mp, "=:"); break;
23850   case 1:mp_print(mp, "=:|"); break;
23851   case 2:mp_print(mp, "|=:"); break;
23852   case 3:mp_print(mp, "|=:|"); break;
23853   case 5:mp_print(mp, "=:|>"); break;
23854   case 6:mp_print(mp, "|=:>"); break;
23855   case 7:mp_print(mp, "|=:|>"); break;
23856   case 11:mp_print(mp, "|=:|>>"); break;
23857   default: mp_print(mp, "kern"); break;
23858   }
23859   break;
23860
23861 @ Local labels are implemented by maintaining the |skip_table| array,
23862 where |skip_table[c]| is either |undefined_label| or the address of the
23863 most recent lig/kern instruction that skips to local label~|c|. In the
23864 latter case, the |skip_byte| in that instruction will (temporarily)
23865 be zero if there were no prior skips to this label, or it will be the
23866 distance to the prior skip.
23867
23868 We may need to cancel skips that span more than 127 lig/kern steps.
23869
23870 @d cancel_skips(A) mp->ll=(A);
23871   do {  
23872     mp->lll=qo(skip_byte(mp->ll)); 
23873     skip_byte(mp->ll)=stop_flag; mp->ll=mp->ll-mp->lll;
23874   } while (mp->lll!=0)
23875 @d skip_error(A) { print_err("Too far to skip");
23876 @.Too far to skip@>
23877   help1("At most 127 lig/kern steps can separate skipto1 from 1::.");
23878   mp_error(mp); cancel_skips((A));
23879   }
23880
23881 @<Process a |skip_to| command and |goto done|@>=
23882
23883   c=mp_get_code(mp);
23884   if ( mp->nl-mp->skip_table[c]>128 ) {
23885     skip_error(mp->skip_table[c]); mp->skip_table[c]=undefined_label;
23886   }
23887   if ( mp->skip_table[c]==undefined_label ) skip_byte(mp->nl-1)=qi(0);
23888   else skip_byte(mp->nl-1)=qi(mp->nl-mp->skip_table[c]-1);
23889   mp->skip_table[c]=mp->nl-1; goto DONE;
23890 }
23891
23892 @ @<Record a label in a lig/kern subprogram and |goto continue|@>=
23893
23894   if ( mp->cur_cmd==colon ) {
23895     if ( c==256 ) mp->bch_label=mp->nl;
23896     else mp_set_tag(mp, c,lig_tag,mp->nl);
23897   } else if ( mp->skip_table[c]<undefined_label ) {
23898     mp->ll=mp->skip_table[c]; mp->skip_table[c]=undefined_label;
23899     do {  
23900       mp->lll=qo(skip_byte(mp->ll));
23901       if ( mp->nl-mp->ll>128 ) {
23902         skip_error(mp->ll); goto CONTINUE;
23903       }
23904       skip_byte(mp->ll)=qi(mp->nl-mp->ll-1); mp->ll=mp->ll-mp->lll;
23905     } while (mp->lll!=0);
23906   }
23907   goto CONTINUE;
23908 }
23909
23910 @ @<Compile a ligature/kern...@>=
23911
23912   next_char(mp->nl)=qi(c); skip_byte(mp->nl)=qi(0);
23913   if ( mp->cur_mod<128 ) { /* ligature op */
23914     op_byte(mp->nl)=qi(mp->cur_mod); rem_byte(mp->nl)=qi(mp_get_code(mp));
23915   } else { 
23916     mp_get_x_next(mp); mp_scan_expression(mp);
23917     if ( mp->cur_type!=mp_known ) {
23918       exp_err("Improper kern");
23919 @.Improper kern@>
23920       help2("The amount of kern should be a known numeric value.")
23921         ("I'm zeroing this one. Proceed, with fingers crossed.");
23922       mp_put_get_flush_error(mp, 0);
23923     }
23924     mp->kern[mp->nk]=mp->cur_exp;
23925     k=0; 
23926     while ( mp->kern[k]!=mp->cur_exp ) incr(k);
23927     if ( k==mp->nk ) {
23928       if ( mp->nk==max_tfm_int ) mp_fatal_error(mp, "too many TFM kerns");
23929       incr(mp->nk);
23930     }
23931     op_byte(mp->nl)=kern_flag+(k / 256);
23932     rem_byte(mp->nl)=qi((k % 256));
23933   }
23934   mp->lk_started=true;
23935 }
23936
23937 @ @d missing_extensible_punctuation(A) 
23938   { mp_missing_err(mp, (A));
23939 @.Missing `\char`\#'@>
23940   help1("I'm processing `extensible c: t,m,b,r'."); mp_back_error(mp);
23941   }
23942
23943 @<Define an extensible recipe@>=
23944
23945   if ( mp->ne==256 ) mp_fatal_error(mp, "too many extensible recipies");
23946   c=mp_get_code(mp); mp_set_tag(mp, c,ext_tag,mp->ne);
23947   if ( mp->cur_cmd!=colon ) missing_extensible_punctuation(":");
23948   ext_top(mp->ne)=qi(mp_get_code(mp));
23949   if ( mp->cur_cmd!=comma ) missing_extensible_punctuation(",");
23950   ext_mid(mp->ne)=qi(mp_get_code(mp));
23951   if ( mp->cur_cmd!=comma ) missing_extensible_punctuation(",");
23952   ext_bot(mp->ne)=qi(mp_get_code(mp));
23953   if ( mp->cur_cmd!=comma ) missing_extensible_punctuation(",");
23954   ext_rep(mp->ne)=qi(mp_get_code(mp));
23955   incr(mp->ne);
23956 }
23957
23958 @ The header could contain ASCII zeroes, so can't use |strdup|.
23959
23960 @<Store a list of header bytes@>=
23961 do {  
23962   if ( j>=mp->header_size ) {
23963     int l = mp->header_size + (mp->header_size >> 2);
23964     char *t = xmalloc(l,sizeof(char));
23965     memset(t,0,l); 
23966     memcpy(t,mp->header_byte,mp->header_size);
23967     xfree (mp->header_byte);
23968     mp->header_byte = t;
23969     mp->header_size = l;
23970   }
23971   mp->header_byte[j]=mp_get_code(mp); 
23972   incr(j); incr(mp->header_last);
23973 } while (mp->cur_cmd==comma)
23974
23975 @ @<Store a list of font dimensions@>=
23976 do {  
23977   if ( j>max_tfm_int ) mp_fatal_error(mp, "too many fontdimens");
23978   while ( j>mp->np ) { incr(mp->np); mp->param[mp->np]=0; };
23979   mp_get_x_next(mp); mp_scan_expression(mp);
23980   if ( mp->cur_type!=mp_known ){ 
23981     exp_err("Improper font parameter");
23982 @.Improper font parameter@>
23983     help1("I'm zeroing this one. Proceed, with fingers crossed.");
23984     mp_put_get_flush_error(mp, 0);
23985   }
23986   mp->param[j]=mp->cur_exp; incr(j);
23987 } while (mp->cur_cmd==comma)
23988
23989 @ OK: We've stored all the data that is needed for the \.{TFM} file.
23990 All that remains is to output it in the correct format.
23991
23992 An interesting problem needs to be solved in this connection, because
23993 the \.{TFM} format allows at most 256~widths, 16~heights, 16~depths,
23994 and 64~italic corrections. If the data has more distinct values than
23995 this, we want to meet the necessary restrictions by perturbing the
23996 given values as little as possible.
23997
23998 \MP\ solves this problem in two steps. First the values of a given
23999 kind (widths, heights, depths, or italic corrections) are sorted;
24000 then the list of sorted values is perturbed, if necessary.
24001
24002 The sorting operation is facilitated by having a special node of
24003 essentially infinite |value| at the end of the current list.
24004
24005 @<Initialize table entries...@>=
24006 value(inf_val)=fraction_four;
24007
24008 @ Straight linear insertion is good enough for sorting, since the lists
24009 are usually not terribly long. As we work on the data, the current list
24010 will start at |link(temp_head)| and end at |inf_val|; the nodes in this
24011 list will be in increasing order of their |value| fields.
24012
24013 Given such a list, the |sort_in| function takes a value and returns a pointer
24014 to where that value can be found in the list. The value is inserted in
24015 the proper place, if necessary.
24016
24017 At the time we need to do these operations, most of \MP's work has been
24018 completed, so we will have plenty of memory to play with. The value nodes
24019 that are allocated for sorting will never be returned to free storage.
24020
24021 @d clear_the_list link(temp_head)=inf_val
24022
24023 @c pointer mp_sort_in (MP mp,scaled v) {
24024   pointer p,q,r; /* list manipulation registers */
24025   p=temp_head;
24026   while (1) { 
24027     q=link(p);
24028     if ( v<=value(q) ) break;
24029     p=q;
24030   }
24031   if ( v<value(q) ) {
24032     r=mp_get_node(mp, value_node_size); value(r)=v; link(r)=q; link(p)=r;
24033   }
24034   return link(p);
24035 }
24036
24037 @ Now we come to the interesting part, where we reduce the list if necessary
24038 until it has the required size. The |min_cover| routine is basic to this
24039 process; it computes the minimum number~|m| such that the values of the
24040 current sorted list can be covered by |m|~intervals of width~|d|. It
24041 also sets the global value |perturbation| to the smallest value $d'>d$
24042 such that the covering found by this algorithm would be different.
24043
24044 In particular, |min_cover(0)| returns the number of distinct values in the
24045 current list and sets |perturbation| to the minimum distance between
24046 adjacent values.
24047
24048 @c integer mp_min_cover (MP mp,scaled d) {
24049   pointer p; /* runs through the current list */
24050   scaled l; /* the least element covered by the current interval */
24051   integer m; /* lower bound on the size of the minimum cover */
24052   m=0; p=link(temp_head); mp->perturbation=el_gordo;
24053   while ( p!=inf_val ){ 
24054     incr(m); l=value(p);
24055     do {  p=link(p); } while (value(p)<=l+d);
24056     if ( value(p)-l<mp->perturbation ) 
24057       mp->perturbation=value(p)-l;
24058   }
24059   return m;
24060 }
24061
24062 @ @<Glob...@>=
24063 scaled perturbation; /* quantity related to \.{TFM} rounding */
24064 integer excess; /* the list is this much too long */
24065
24066 @ The smallest |d| such that a given list can be covered with |m| intervals
24067 is determined by the |threshold| routine, which is sort of an inverse
24068 to |min_cover|. The idea is to increase the interval size rapidly until
24069 finding the range, then to go sequentially until the exact borderline has
24070 been discovered.
24071
24072 @c scaled mp_threshold (MP mp,integer m) {
24073   scaled d; /* lower bound on the smallest interval size */
24074   mp->excess=mp_min_cover(mp, 0)-m;
24075   if ( mp->excess<=0 ) {
24076     return 0;
24077   } else  { 
24078     do {  
24079       d=mp->perturbation;
24080     } while (mp_min_cover(mp, d+d)>m);
24081     while ( mp_min_cover(mp, d)>m ) 
24082       d=mp->perturbation;
24083     return d;
24084   }
24085 }
24086
24087 @ The |skimp| procedure reduces the current list to at most |m| entries,
24088 by changing values if necessary. It also sets |info(p):=k| if |value(p)|
24089 is the |k|th distinct value on the resulting list, and it sets
24090 |perturbation| to the maximum amount by which a |value| field has
24091 been changed. The size of the resulting list is returned as the
24092 value of |skimp|.
24093
24094 @c integer mp_skimp (MP mp,integer m) {
24095   scaled d; /* the size of intervals being coalesced */
24096   pointer p,q,r; /* list manipulation registers */
24097   scaled l; /* the least value in the current interval */
24098   scaled v; /* a compromise value */
24099   d=mp_threshold(mp, m); mp->perturbation=0;
24100   q=temp_head; m=0; p=link(temp_head);
24101   while ( p!=inf_val ) {
24102     incr(m); l=value(p); info(p)=m;
24103     if ( value(link(p))<=l+d ) {
24104       @<Replace an interval of values by its midpoint@>;
24105     }
24106     q=p; p=link(p);
24107   }
24108   return m;
24109 }
24110
24111 @ @<Replace an interval...@>=
24112
24113   do {  
24114     p=link(p); info(p)=m;
24115     decr(mp->excess); if ( mp->excess==0 ) d=0;
24116   } while (value(link(p))<=l+d);
24117   v=l+halfp(value(p)-l);
24118   if ( value(p)-v>mp->perturbation ) 
24119     mp->perturbation=value(p)-v;
24120   r=q;
24121   do {  
24122     r=link(r); value(r)=v;
24123   } while (r!=p);
24124   link(q)=p; /* remove duplicate values from the current list */
24125 }
24126
24127 @ A warning message is issued whenever something is perturbed by
24128 more than 1/16\thinspace pt.
24129
24130 @c void mp_tfm_warning (MP mp,small_number m) { 
24131   mp_print_nl(mp, "(some "); 
24132   mp_print(mp, mp->int_name[m]);
24133 @.some charwds...@>
24134 @.some chardps...@>
24135 @.some charhts...@>
24136 @.some charics...@>
24137   mp_print(mp, " values had to be adjusted by as much as ");
24138   mp_print_scaled(mp, mp->perturbation); mp_print(mp, "pt)");
24139 }
24140
24141 @ Here's an example of how we use these routines.
24142 The width data needs to be perturbed only if there are 256 distinct
24143 widths, but \MP\ must check for this case even though it is
24144 highly unusual.
24145
24146 An integer variable |k| will be defined when we use this code.
24147 The |dimen_head| array will contain pointers to the sorted
24148 lists of dimensions.
24149
24150 @<Massage the \.{TFM} widths@>=
24151 clear_the_list;
24152 for (k=mp->bc;k<=mp->ec;k++)  {
24153   if ( mp->char_exists[k] )
24154     mp->tfm_width[k]=mp_sort_in(mp, mp->tfm_width[k]);
24155 }
24156 mp->nw=mp_skimp(mp, 255)+1; mp->dimen_head[1]=link(temp_head);
24157 if ( mp->perturbation>=010000 ) mp_tfm_warning(mp, mp_char_wd)
24158
24159 @ @<Glob...@>=
24160 pointer dimen_head[5]; /* lists of \.{TFM} dimensions */
24161
24162 @ Heights, depths, and italic corrections are different from widths
24163 not only because their list length is more severely restricted, but
24164 also because zero values do not need to be put into the lists.
24165
24166 @<Massage the \.{TFM} heights, depths, and italic corrections@>=
24167 clear_the_list;
24168 for (k=mp->bc;k<=mp->ec;k++) {
24169   if ( mp->char_exists[k] ) {
24170     if ( mp->tfm_height[k]==0 ) mp->tfm_height[k]=zero_val;
24171     else mp->tfm_height[k]=mp_sort_in(mp, mp->tfm_height[k]);
24172   }
24173 }
24174 mp->nh=mp_skimp(mp, 15)+1; mp->dimen_head[2]=link(temp_head);
24175 if ( mp->perturbation>=010000 ) mp_tfm_warning(mp, mp_char_ht);
24176 clear_the_list;
24177 for (k=mp->bc;k<=mp->ec;k++) {
24178   if ( mp->char_exists[k] ) {
24179     if ( mp->tfm_depth[k]==0 ) mp->tfm_depth[k]=zero_val;
24180     else mp->tfm_depth[k]=mp_sort_in(mp, mp->tfm_depth[k]);
24181   }
24182 }
24183 mp->nd=mp_skimp(mp, 15)+1; mp->dimen_head[3]=link(temp_head);
24184 if ( mp->perturbation>=010000 ) mp_tfm_warning(mp, mp_char_dp);
24185 clear_the_list;
24186 for (k=mp->bc;k<=mp->ec;k++) {
24187   if ( mp->char_exists[k] ) {
24188     if ( mp->tfm_ital_corr[k]==0 ) mp->tfm_ital_corr[k]=zero_val;
24189     else mp->tfm_ital_corr[k]=mp_sort_in(mp, mp->tfm_ital_corr[k]);
24190   }
24191 }
24192 mp->ni=mp_skimp(mp, 63)+1; mp->dimen_head[4]=link(temp_head);
24193 if ( mp->perturbation>=010000 ) mp_tfm_warning(mp, mp_char_ic)
24194
24195 @ @<Initialize table entries...@>=
24196 value(zero_val)=0; info(zero_val)=0;
24197
24198 @ Bytes 5--8 of the header are set to the design size, unless the user has
24199 some crazy reason for specifying them differently.
24200 @^design size@>
24201
24202 Error messages are not allowed at the time this procedure is called,
24203 so a warning is printed instead.
24204
24205 The value of |max_tfm_dimen| is calculated so that
24206 $$\hbox{|make_scaled(16*max_tfm_dimen,internal[mp_design_size])|}
24207  < \\{three\_bytes}.$$
24208
24209 @d three_bytes 0100000000 /* $2^{24}$ */
24210
24211 @c 
24212 void mp_fix_design_size (MP mp) {
24213   scaled d; /* the design size */
24214   d=mp->internal[mp_design_size];
24215   if ( (d<unity)||(d>=fraction_half) ) {
24216     if ( d!=0 )
24217       mp_print_nl(mp, "(illegal design size has been changed to 128pt)");
24218 @.illegal design size...@>
24219     d=040000000; mp->internal[mp_design_size]=d;
24220   }
24221   if ( mp->header_byte[4]<0 ) if ( mp->header_byte[5]<0 )
24222     if ( mp->header_byte[6]<0 ) if ( mp->header_byte[7]<0 ) {
24223      mp->header_byte[4]=d / 04000000;
24224      mp->header_byte[5]=(d / 4096) % 256;
24225      mp->header_byte[6]=(d / 16) % 256;
24226      mp->header_byte[7]=(d % 16)*16;
24227   };
24228   mp->max_tfm_dimen=16*mp->internal[mp_design_size]-1-mp->internal[mp_design_size] / 010000000;
24229   if ( mp->max_tfm_dimen>=fraction_half ) mp->max_tfm_dimen=fraction_half-1;
24230 }
24231
24232 @ The |dimen_out| procedure computes a |fix_word| relative to the
24233 design size. If the data was out of range, it is corrected and the
24234 global variable |tfm_changed| is increased by~one.
24235
24236 @c integer mp_dimen_out (MP mp,scaled x) { 
24237   if ( abs(x)>mp->max_tfm_dimen ) {
24238     incr(mp->tfm_changed);
24239     if ( x>0 ) x=mp->max_tfm_dimen; else x=-mp->max_tfm_dimen;
24240   }
24241   x=mp_make_scaled(mp, x*16,mp->internal[mp_design_size]);
24242   return x;
24243 }
24244
24245 @ @<Glob...@>=
24246 scaled max_tfm_dimen; /* bound on widths, heights, kerns, etc. */
24247 integer tfm_changed; /* the number of data entries that were out of bounds */
24248
24249 @ If the user has not specified any of the first four header bytes,
24250 the |fix_check_sum| procedure replaces them by a ``check sum'' computed
24251 from the |tfm_width| data relative to the design size.
24252 @^check sum@>
24253
24254 @c void mp_fix_check_sum (MP mp) {
24255   eight_bits k; /* runs through character codes */
24256   eight_bits B1,B2,B3,B4; /* bytes of the check sum */
24257   integer x;  /* hash value used in check sum computation */
24258   if ( mp->header_byte[0]==0 && mp->header_byte[1]==0 &&
24259        mp->header_byte[2]==0 && mp->header_byte[3]==0 ) {
24260     @<Compute a check sum in |(b1,b2,b3,b4)|@>;
24261     mp->header_byte[0]=B1; mp->header_byte[1]=B2;
24262     mp->header_byte[2]=B3; mp->header_byte[3]=B4; 
24263     return;
24264   }
24265 }
24266
24267 @ @<Compute a check sum in |(b1,b2,b3,b4)|@>=
24268 B1=mp->bc; B2=mp->ec; B3=mp->bc; B4=mp->ec; mp->tfm_changed=0;
24269 for (k=mp->bc;k<=mp->ec;k++) { 
24270   if ( mp->char_exists[k] ) {
24271     x=mp_dimen_out(mp, value(mp->tfm_width[k]))+(k+4)*020000000; /* this is positive */
24272     B1=(B1+B1+x) % 255;
24273     B2=(B2+B2+x) % 253;
24274     B3=(B3+B3+x) % 251;
24275     B4=(B4+B4+x) % 247;
24276   }
24277 }
24278
24279 @ Finally we're ready to actually write the \.{TFM} information.
24280 Here are some utility routines for this purpose.
24281
24282 @d tfm_out(A) do { /* output one byte to |tfm_file| */
24283   unsigned char s=(A); 
24284   (mp->write_binary_file)(mp,mp->tfm_file,(void *)&s,1); 
24285   } while (0)
24286
24287 @c void mp_tfm_two (MP mp,integer x) { /* output two bytes to |tfm_file| */
24288   tfm_out(x / 256); tfm_out(x % 256);
24289 }
24290 void mp_tfm_four (MP mp,integer x) { /* output four bytes to |tfm_file| */
24291   if ( x>=0 ) tfm_out(x / three_bytes);
24292   else { 
24293     x=x+010000000000; /* use two's complement for negative values */
24294     x=x+010000000000;
24295     tfm_out((x / three_bytes) + 128);
24296   };
24297   x=x % three_bytes; tfm_out(x / unity);
24298   x=x % unity; tfm_out(x / 0400);
24299   tfm_out(x % 0400);
24300 }
24301 void mp_tfm_qqqq (MP mp,four_quarters x) { /* output four quarterwords to |tfm_file| */
24302   tfm_out(qo(x.b0)); tfm_out(qo(x.b1)); 
24303   tfm_out(qo(x.b2)); tfm_out(qo(x.b3));
24304 }
24305
24306 @ @<Finish the \.{TFM} file@>=
24307 if ( mp->job_name==NULL ) mp_open_log_file(mp);
24308 mp_pack_job_name(mp, ".tfm");
24309 while ( ! mp_b_open_out(mp, &mp->tfm_file, mp_filetype_metrics) )
24310   mp_prompt_file_name(mp, "file name for font metrics",".tfm");
24311 mp->metric_file_name=xstrdup(mp->name_of_file);
24312 @<Output the subfile sizes and header bytes@>;
24313 @<Output the character information bytes, then
24314   output the dimensions themselves@>;
24315 @<Output the ligature/kern program@>;
24316 @<Output the extensible character recipes and the font metric parameters@>;
24317   if ( mp->internal[mp_tracing_stats]>0 )
24318   @<Log the subfile sizes of the \.{TFM} file@>;
24319 mp_print_nl(mp, "Font metrics written on "); 
24320 mp_print(mp, mp->metric_file_name); mp_print_char(mp, '.');
24321 @.Font metrics written...@>
24322 (mp->close_file)(mp,mp->tfm_file)
24323
24324 @ Integer variables |lh|, |k|, and |lk_offset| will be defined when we use
24325 this code.
24326
24327 @<Output the subfile sizes and header bytes@>=
24328 k=mp->header_last;
24329 LH=(k+3) / 4; /* this is the number of header words */
24330 if ( mp->bc>mp->ec ) mp->bc=1; /* if there are no characters, |ec=0| and |bc=1| */
24331 @<Compute the ligature/kern program offset and implant the
24332   left boundary label@>;
24333 mp_tfm_two(mp,6+LH+(mp->ec-mp->bc+1)+mp->nw+mp->nh+mp->nd+mp->ni+mp->nl
24334      +lk_offset+mp->nk+mp->ne+mp->np);
24335   /* this is the total number of file words that will be output */
24336 mp_tfm_two(mp, LH); mp_tfm_two(mp, mp->bc); mp_tfm_two(mp, mp->ec); 
24337 mp_tfm_two(mp, mp->nw); mp_tfm_two(mp, mp->nh);
24338 mp_tfm_two(mp, mp->nd); mp_tfm_two(mp, mp->ni); mp_tfm_two(mp, mp->nl+lk_offset); 
24339 mp_tfm_two(mp, mp->nk); mp_tfm_two(mp, mp->ne);
24340 mp_tfm_two(mp, mp->np);
24341 for (k=0;k< 4*LH;k++)   { 
24342   tfm_out(mp->header_byte[k]);
24343 }
24344
24345 @ @<Output the character information bytes...@>=
24346 for (k=mp->bc;k<=mp->ec;k++) {
24347   if ( ! mp->char_exists[k] ) {
24348     mp_tfm_four(mp, 0);
24349   } else { 
24350     tfm_out(info(mp->tfm_width[k])); /* the width index */
24351     tfm_out((info(mp->tfm_height[k]))*16+info(mp->tfm_depth[k]));
24352     tfm_out((info(mp->tfm_ital_corr[k]))*4+mp->char_tag[k]);
24353     tfm_out(mp->char_remainder[k]);
24354   };
24355 }
24356 mp->tfm_changed=0;
24357 for (k=1;k<=4;k++) { 
24358   mp_tfm_four(mp, 0); p=mp->dimen_head[k];
24359   while ( p!=inf_val ) {
24360     mp_tfm_four(mp, mp_dimen_out(mp, value(p))); p=link(p);
24361   }
24362 }
24363
24364
24365 @ We need to output special instructions at the beginning of the
24366 |lig_kern| array in order to specify the right boundary character
24367 and/or to handle starting addresses that exceed 255. The |label_loc|
24368 and |label_char| arrays have been set up to record all the
24369 starting addresses; we have $-1=|label_loc|[0]<|label_loc|[1]\le\cdots
24370 \le|label_loc|[|label_ptr]|$.
24371
24372 @<Compute the ligature/kern program offset...@>=
24373 mp->bchar=mp_round_unscaled(mp, mp->internal[mp_boundary_char]);
24374 if ((mp->bchar<0)||(mp->bchar>255))
24375   { mp->bchar=-1; mp->lk_started=false; lk_offset=0; }
24376 else { mp->lk_started=true; lk_offset=1; };
24377 @<Find the minimum |lk_offset| and adjust all remainders@>;
24378 if ( mp->bch_label<undefined_label )
24379   { skip_byte(mp->nl)=qi(255); next_char(mp->nl)=qi(0);
24380   op_byte(mp->nl)=qi(((mp->bch_label+lk_offset)/ 256));
24381   rem_byte(mp->nl)=qi(((mp->bch_label+lk_offset)% 256));
24382   incr(mp->nl); /* possibly |nl=lig_table_size+1| */
24383   }
24384
24385 @ @<Find the minimum |lk_offset|...@>=
24386 k=mp->label_ptr; /* pointer to the largest unallocated label */
24387 if ( mp->label_loc[k]+lk_offset>255 ) {
24388   lk_offset=0; mp->lk_started=false; /* location 0 can do double duty */
24389   do {  
24390     mp->char_remainder[mp->label_char[k]]=lk_offset;
24391     while ( mp->label_loc[k-1]==mp->label_loc[k] ) {
24392        decr(k); mp->char_remainder[mp->label_char[k]]=lk_offset;
24393     }
24394     incr(lk_offset); decr(k);
24395   } while (! (lk_offset+mp->label_loc[k]<256));
24396     /* N.B.: |lk_offset=256| satisfies this when |k=0| */
24397 }
24398 if ( lk_offset>0 ) {
24399   while ( k>0 ) {
24400     mp->char_remainder[mp->label_char[k]]
24401      =mp->char_remainder[mp->label_char[k]]+lk_offset;
24402     decr(k);
24403   }
24404 }
24405
24406 @ @<Output the ligature/kern program@>=
24407 for (k=0;k<= 255;k++ ) {
24408   if ( mp->skip_table[k]<undefined_label ) {
24409      mp_print_nl(mp, "(local label "); mp_print_int(mp, k); mp_print(mp, ":: was missing)");
24410 @.local label l:: was missing@>
24411     cancel_skips(mp->skip_table[k]);
24412   }
24413 }
24414 if ( mp->lk_started ) { /* |lk_offset=1| for the special |bchar| */
24415   tfm_out(255); tfm_out(mp->bchar); mp_tfm_two(mp, 0);
24416 } else {
24417   for (k=1;k<=lk_offset;k++) {/* output the redirection specs */
24418     mp->ll=mp->label_loc[mp->label_ptr];
24419     if ( mp->bchar<0 ) { tfm_out(254); tfm_out(0);   }
24420     else { tfm_out(255); tfm_out(mp->bchar);   };
24421     mp_tfm_two(mp, mp->ll+lk_offset);
24422     do {  
24423       decr(mp->label_ptr);
24424     } while (! (mp->label_loc[mp->label_ptr]<mp->ll));
24425   }
24426 }
24427 for (k=0;k<=mp->nl-1;k++) mp_tfm_qqqq(mp, mp->lig_kern[k]);
24428 for (k=0;k<=mp->nk-1;k++) mp_tfm_four(mp, mp_dimen_out(mp, mp->kern[k]))
24429
24430 @ @<Output the extensible character recipes...@>=
24431 for (k=0;k<=mp->ne-1;k++) 
24432   mp_tfm_qqqq(mp, mp->exten[k]);
24433 for (k=1;k<=mp->np;k++) {
24434   if ( k==1 ) {
24435     if ( abs(mp->param[1])<fraction_half ) {
24436       mp_tfm_four(mp, mp->param[1]*16);
24437     } else  { 
24438       incr(mp->tfm_changed);
24439       if ( mp->param[1]>0 ) mp_tfm_four(mp, el_gordo);
24440       else mp_tfm_four(mp, -el_gordo);
24441     }
24442   } else {
24443     mp_tfm_four(mp, mp_dimen_out(mp, mp->param[k]));
24444   }
24445 }
24446 if ( mp->tfm_changed>0 )  { 
24447   if ( mp->tfm_changed==1 ) mp_print_nl(mp, "(a font metric dimension");
24448 @.a font metric dimension...@>
24449   else  { 
24450     mp_print_nl(mp, "("); mp_print_int(mp, mp->tfm_changed);
24451 @.font metric dimensions...@>
24452     mp_print(mp, " font metric dimensions");
24453   }
24454   mp_print(mp, " had to be decreased)");
24455 }
24456
24457 @ @<Log the subfile sizes of the \.{TFM} file@>=
24458
24459   char s[200];
24460   wlog_ln(" ");
24461   if ( mp->bch_label<undefined_label ) decr(mp->nl);
24462   snprintf(s,128,"(You used %iw,%ih,%id,%ii,%il,%ik,%ie,%ip metric file positions)",
24463                  mp->nw, mp->nh, mp->nd, mp->ni, mp->nl, mp->nk, mp->ne,mp->np);
24464   wlog_ln(s);
24465 }
24466
24467 @* \[43] Reading font metric data.
24468
24469 \MP\ isn't a typesetting program but it does need to find the bounding box
24470 of a sequence of typeset characters.  Thus it needs to read \.{TFM} files as
24471 well as write them.
24472
24473 @<Glob...@>=
24474 void * tfm_infile;
24475
24476 @ All the width, height, and depth information is stored in an array called
24477 |font_info|.  This array is allocated sequentially and each font is stored
24478 as a series of |char_info| words followed by the width, height, and depth
24479 tables.  Since |font_name| entries are permanent, their |str_ref| values are
24480 set to |max_str_ref|.
24481
24482 @<Types...@>=
24483 typedef unsigned int font_number; /* |0..font_max| */
24484
24485 @ The |font_info| array is indexed via a group directory arrays.
24486 For example, the |char_info| data for character~|c| in font~|f| will be
24487 in |font_info[char_base[f]+c].qqqq|.
24488
24489 @<Glob...@>=
24490 font_number font_max; /* maximum font number for included text fonts */
24491 size_t      font_mem_size; /* number of words for \.{TFM} information for text fonts */
24492 memory_word *font_info; /* height, width, and depth data */
24493 char        **font_enc_name; /* encoding names, if any */
24494 boolean     *font_ps_name_fixed; /* are the postscript names fixed already?  */
24495 int         next_fmem; /* next unused entry in |font_info| */
24496 font_number last_fnum; /* last font number used so far */
24497 scaled      *font_dsize;  /* 16 times the ``design'' size in \ps\ points */
24498 char        **font_name;  /* name as specified in the \&{infont} command */
24499 char        **font_ps_name;  /* PostScript name for use when |internal[mp_prologues]>0| */
24500 font_number last_ps_fnum; /* last valid |font_ps_name| index */
24501 eight_bits  *font_bc;
24502 eight_bits  *font_ec;  /* first and last character code */
24503 int         *char_base;  /* base address for |char_info| */
24504 int         *width_base; /* index for zeroth character width */
24505 int         *height_base; /* index for zeroth character height */
24506 int         *depth_base; /* index for zeroth character depth */
24507 pointer     *font_sizes;
24508
24509 @ @<Allocate or initialize ...@>=
24510 mp->font_mem_size = 10000; 
24511 mp->font_info = xmalloc ((mp->font_mem_size+1),sizeof(memory_word));
24512 memset (mp->font_info,0,sizeof(memory_word)*(mp->font_mem_size+1));
24513 mp->font_enc_name = NULL;
24514 mp->font_ps_name_fixed = NULL;
24515 mp->font_dsize = NULL;
24516 mp->font_name = NULL;
24517 mp->font_ps_name = NULL;
24518 mp->font_bc = NULL;
24519 mp->font_ec = NULL;
24520 mp->last_fnum = null_font;
24521 mp->char_base = NULL;
24522 mp->width_base = NULL;
24523 mp->height_base = NULL;
24524 mp->depth_base = NULL;
24525 mp->font_sizes = null;
24526
24527 @ @<Dealloc variables@>=
24528 for (k=1;k<=(int)mp->last_fnum;k++) {
24529   xfree(mp->font_enc_name[k]);
24530   xfree(mp->font_name[k]);
24531   xfree(mp->font_ps_name[k]);
24532 }
24533 xfree(mp->font_info);
24534 xfree(mp->font_enc_name);
24535 xfree(mp->font_ps_name_fixed);
24536 xfree(mp->font_dsize);
24537 xfree(mp->font_name);
24538 xfree(mp->font_ps_name);
24539 xfree(mp->font_bc);
24540 xfree(mp->font_ec);
24541 xfree(mp->char_base);
24542 xfree(mp->width_base);
24543 xfree(mp->height_base);
24544 xfree(mp->depth_base);
24545 xfree(mp->font_sizes);
24546
24547
24548 @c 
24549 void mp_reallocate_fonts (MP mp, font_number l) {
24550   font_number f;
24551   XREALLOC(mp->font_enc_name,      l, char *);
24552   XREALLOC(mp->font_ps_name_fixed, l, boolean);
24553   XREALLOC(mp->font_dsize,         l, scaled);
24554   XREALLOC(mp->font_name,          l, char *);
24555   XREALLOC(mp->font_ps_name,       l, char *);
24556   XREALLOC(mp->font_bc,            l, eight_bits);
24557   XREALLOC(mp->font_ec,            l, eight_bits);
24558   XREALLOC(mp->char_base,          l, int);
24559   XREALLOC(mp->width_base,         l, int);
24560   XREALLOC(mp->height_base,        l, int);
24561   XREALLOC(mp->depth_base,         l, int);
24562   XREALLOC(mp->font_sizes,         l, pointer);
24563   for (f=(mp->last_fnum+1);f<=l;f++) {
24564     mp->font_enc_name[f]=NULL;
24565     mp->font_ps_name_fixed[f] = false;
24566     mp->font_name[f]=NULL;
24567     mp->font_ps_name[f]=NULL;
24568     mp->font_sizes[f]=null;
24569   }
24570   mp->font_max = l;
24571 }
24572
24573 @ @<Declare |mp_reallocate| functions@>=
24574 void mp_reallocate_fonts (MP mp, font_number l);
24575
24576
24577 @ A |null_font| containing no characters is useful for error recovery.  Its
24578 |font_name| entry starts out empty but is reset each time an erroneous font is
24579 found.  This helps to cut down on the number of duplicate error messages without
24580 wasting a lot of space.
24581
24582 @d null_font 0 /* the |font_number| for an empty font */
24583
24584 @<Set initial...@>=
24585 mp->font_dsize[null_font]=0;
24586 mp->font_bc[null_font]=1;
24587 mp->font_ec[null_font]=0;
24588 mp->char_base[null_font]=0;
24589 mp->width_base[null_font]=0;
24590 mp->height_base[null_font]=0;
24591 mp->depth_base[null_font]=0;
24592 mp->next_fmem=0;
24593 mp->last_fnum=null_font;
24594 mp->last_ps_fnum=null_font;
24595 mp->font_name[null_font]=(char *)"nullfont";
24596 mp->font_ps_name[null_font]=(char *)"";
24597 mp->font_ps_name_fixed[null_font] = false;
24598 mp->font_enc_name[null_font]=NULL;
24599 mp->font_sizes[null_font]=null;
24600
24601 @ Each |char_info| word is of type |four_quarters|.  The |b0| field contains
24602 the |width index|; the |b1| field contains the height
24603 index; the |b2| fields contains the depth index, and the |b3| field used only
24604 for temporary storage. (It is used to keep track of which characters occur in
24605 an edge structure that is being shipped out.)
24606 The corresponding words in the width, height, and depth tables are stored as
24607 |scaled| values in units of \ps\ points.
24608
24609 With the macros below, the |char_info| word for character~|c| in font~|f| is
24610 |char_info(f)(c)| and the width is
24611 $$\hbox{|char_width(f)(char_info(f)(c)).sc|.}$$
24612
24613 @d char_info_end(A) (A)].qqqq
24614 @d char_info(A) mp->font_info[mp->char_base[(A)]+char_info_end
24615 @d char_width_end(A) (A).b0].sc
24616 @d char_width(A) mp->font_info[mp->width_base[(A)]+char_width_end
24617 @d char_height_end(A) (A).b1].sc
24618 @d char_height(A) mp->font_info[mp->height_base[(A)]+char_height_end
24619 @d char_depth_end(A) (A).b2].sc
24620 @d char_depth(A) mp->font_info[mp->depth_base[(A)]+char_depth_end
24621 @d ichar_exists(A) ((A).b0>0)
24622
24623 @ The |font_ps_name| for a built-in font should be what PostScript expects.
24624 A preliminary name is obtained here from the \.{TFM} name as given in the
24625 |fname| argument.  This gets updated later from an external table if necessary.
24626
24627 @<Declare text measuring subroutines@>=
24628 @<Declare subroutines for parsing file names@>
24629 font_number mp_read_font_info (MP mp, char *fname) {
24630   boolean file_opened; /* has |tfm_infile| been opened? */
24631   font_number n; /* the number to return */
24632   halfword lf,tfm_lh,bc,ec,nw,nh,nd; /* subfile size parameters */
24633   size_t whd_size; /* words needed for heights, widths, and depths */
24634   int i,ii; /* |font_info| indices */
24635   int jj; /* counts bytes to be ignored */
24636   scaled z; /* used to compute the design size */
24637   fraction d;
24638   /* height, width, or depth as a fraction of design size times $2^{-8}$ */
24639   eight_bits h_and_d; /* height and depth indices being unpacked */
24640   unsigned char tfbyte; /* a byte read from the file */
24641   n=null_font;
24642   @<Open |tfm_infile| for input@>;
24643   @<Read data from |tfm_infile|; if there is no room, say so and |goto done|;
24644     otherwise |goto bad_tfm| or |goto done| as appropriate@>;
24645 BAD_TFM:
24646   @<Complain that the \.{TFM} file is bad@>;
24647 DONE:
24648   if ( file_opened ) (mp->close_file)(mp,mp->tfm_infile);
24649   if ( n!=null_font ) { 
24650     mp->font_ps_name[n]=mp_xstrdup(mp,fname);
24651     mp->font_name[n]=mp_xstrdup(mp,fname);
24652   }
24653   return n;
24654 }
24655
24656 @ \MP\ doesn't bother to check the entire \.{TFM} file for errors or explain
24657 precisely what is wrong if it does find a problem.  Programs called \.{TFtoPL}
24658 @.TFtoPL@> @.PLtoTF@>
24659 and \.{PLtoTF} can be used to debug \.{TFM} files.
24660
24661 @<Complain that the \.{TFM} file is bad@>=
24662 print_err("Font ");
24663 mp_print(mp, fname);
24664 if ( file_opened ) mp_print(mp, " not usable: TFM file is bad");
24665 else mp_print(mp, " not usable: TFM file not found");
24666 help3("I wasn't able to read the size data for this font so this")
24667   ("`infont' operation won't produce anything. If the font name")
24668   ("is right, you might ask an expert to make a TFM file");
24669 if ( file_opened )
24670   mp->help_line[0]="is right, try asking an expert to fix the TFM file";
24671 mp_error(mp)
24672
24673 @ @<Read data from |tfm_infile|; if there is no room, say so...@>=
24674 @<Read the \.{TFM} size fields@>;
24675 @<Use the size fields to allocate space in |font_info|@>;
24676 @<Read the \.{TFM} header@>;
24677 @<Read the character data and the width, height, and depth tables and
24678   |goto done|@>
24679
24680 @ A bad \.{TFM} file can be shorter than it claims to be.  The code given here
24681 might try to read past the end of the file if this happens.  Changes will be
24682 needed if it causes a system error to refer to |tfm_infile^| or call
24683 |get_tfm_infile| when |eof(tfm_infile)| is true.  For example, the definition
24684 @^system dependencies@>
24685 of |tfget| could be changed to
24686 ``|begin get(tfm_infile); if eof(tfm_infile) then goto bad_tfm; end|.''
24687
24688 @d tfget do { 
24689   size_t wanted=1; 
24690   void *tfbyte_ptr = &tfbyte;
24691   (mp->read_binary_file)(mp,mp->tfm_infile,&tfbyte_ptr,&wanted); 
24692   if (wanted==0) goto BAD_TFM; 
24693 } while (0)
24694 @d read_two(A) { (A)=tfbyte;
24695   if ( (A)>127 ) goto BAD_TFM;
24696   tfget; (A)=(A)*0400+tfbyte;
24697 }
24698 @d tf_ignore(A) { for (jj=(A);jj>=1;jj--) tfget; }
24699
24700 @<Read the \.{TFM} size fields@>=
24701 tfget; read_two(lf);
24702 tfget; read_two(tfm_lh);
24703 tfget; read_two(bc);
24704 tfget; read_two(ec);
24705 if ( (bc>1+ec)||(ec>255) ) goto BAD_TFM;
24706 tfget; read_two(nw);
24707 tfget; read_two(nh);
24708 tfget; read_two(nd);
24709 whd_size=(ec+1-bc)+nw+nh+nd;
24710 if ( lf<(int)(6+tfm_lh+whd_size) ) goto BAD_TFM;
24711 tf_ignore(10)
24712
24713 @ Offsets are added to |char_base[n]| and |width_base[n]| so that is not
24714 necessary to apply the |so|  and |qo| macros when looking up the width of a
24715 character in the string pool.  In order to ensure nonnegative |char_base|
24716 values when |bc>0|, it may be necessary to reserve a few unused |font_info|
24717 elements.
24718
24719 @<Use the size fields to allocate space in |font_info|@>=
24720 if ( mp->next_fmem<bc) mp->next_fmem=bc;  /* ensure nonnegative |char_base| */
24721 if (mp->last_fnum==mp->font_max)
24722   mp_reallocate_fonts(mp,(mp->font_max+(mp->font_max>>2)));
24723 while (mp->next_fmem+whd_size>=mp->font_mem_size) {
24724   size_t l = mp->font_mem_size+(mp->font_mem_size>>2);
24725   memory_word *font_info;
24726   font_info = xmalloc ((l+1),sizeof(memory_word));
24727   memset (font_info,0,sizeof(memory_word)*(l+1));
24728   memcpy (font_info,mp->font_info,sizeof(memory_word)*(mp->font_mem_size+1));
24729   xfree(mp->font_info);
24730   mp->font_info = font_info;
24731   mp->font_mem_size = l;
24732 }
24733 incr(mp->last_fnum);
24734 n=mp->last_fnum;
24735 mp->font_bc[n]=bc;
24736 mp->font_ec[n]=ec;
24737 mp->char_base[n]=mp->next_fmem-bc;
24738 mp->width_base[n]=mp->next_fmem+ec-bc+1;
24739 mp->height_base[n]=mp->width_base[n]+nw;
24740 mp->depth_base[n]=mp->height_base[n]+nh;
24741 mp->next_fmem=mp->next_fmem+whd_size;
24742
24743
24744 @ @<Read the \.{TFM} header@>=
24745 if ( tfm_lh<2 ) goto BAD_TFM;
24746 tf_ignore(4);
24747 tfget; read_two(z);
24748 tfget; z=z*0400+tfbyte;
24749 tfget; z=z*0400+tfbyte; /* now |z| is 16 times the design size */
24750 mp->font_dsize[n]=mp_take_fraction(mp, z,267432584);
24751   /* times ${72\over72.27}2^{28}$ to convert from \TeX\ points */
24752 tf_ignore(4*(tfm_lh-2))
24753
24754 @ @<Read the character data and the width, height, and depth tables...@>=
24755 ii=mp->width_base[n];
24756 i=mp->char_base[n]+bc;
24757 while ( i<ii ) { 
24758   tfget; mp->font_info[i].qqqq.b0=qi(tfbyte);
24759   tfget; h_and_d=tfbyte;
24760   mp->font_info[i].qqqq.b1=h_and_d / 16;
24761   mp->font_info[i].qqqq.b2=h_and_d % 16;
24762   tfget; tfget;
24763   incr(i);
24764 }
24765 while ( i<mp->next_fmem ) {
24766   @<Read a four byte dimension, scale it by the design size, store it in
24767     |font_info[i]|, and increment |i|@>;
24768 }
24769 goto DONE
24770
24771 @ The raw dimension read into |d| should have magnitude at most $2^{24}$ when
24772 interpreted as an integer, and this includes a scale factor of $2^{20}$.  Thus
24773 we can multiply it by sixteen and think of it as a |fraction| that has been
24774 divided by sixteen.  This cancels the extra scale factor contained in
24775 |font_dsize[n|.
24776
24777 @<Read a four byte dimension, scale it by the design size, store it in...@>=
24778
24779 tfget; d=tfbyte;
24780 if ( d>=0200 ) d=d-0400;
24781 tfget; d=d*0400+tfbyte;
24782 tfget; d=d*0400+tfbyte;
24783 tfget; d=d*0400+tfbyte;
24784 mp->font_info[i].sc=mp_take_fraction(mp, d*16,mp->font_dsize[n]);
24785 incr(i);
24786 }
24787
24788 @ This function does no longer use the file name parser, because |fname| is
24789 a C string already.
24790 @<Open |tfm_infile| for input@>=
24791 file_opened=false;
24792 mp_ptr_scan_file(mp, fname);
24793 if ( strlen(mp->cur_area)==0 ) { xfree(mp->cur_area); }
24794 if ( strlen(mp->cur_ext)==0 )  { xfree(mp->cur_ext); mp->cur_ext=xstrdup(".tfm"); }
24795 pack_cur_name;
24796 mp->tfm_infile = (mp->open_file)(mp, mp->name_of_file, "r",mp_filetype_metrics);
24797 if ( !mp->tfm_infile  ) goto BAD_TFM;
24798 file_opened=true
24799
24800 @ When we have a font name and we don't know whether it has been loaded yet,
24801 we scan the |font_name| array before calling |read_font_info|.
24802
24803 @<Declare text measuring subroutines@>=
24804 font_number mp_find_font (MP mp, char *f) {
24805   font_number n;
24806   for (n=0;n<=mp->last_fnum;n++) {
24807     if (mp_xstrcmp(f,mp->font_name[n])==0 ) {
24808       mp_xfree(f);
24809       return n;
24810     }
24811   }
24812   n = mp_read_font_info(mp, f);
24813   mp_xfree(f);
24814   return n;
24815 }
24816
24817 @ One simple application of |find_font| is the implementation of the |font_size|
24818 operator that gets the design size for a given font name.
24819
24820 @<Find the design size of the font whose name is |cur_exp|@>=
24821 mp_flush_cur_exp(mp, (mp->font_dsize[mp_find_font(mp, str(mp->cur_exp))]+8) / 16)
24822
24823 @ If we discover that the font doesn't have a requested character, we omit it
24824 from the bounding box computation and expect the \ps\ interpreter to drop it.
24825 This routine issues a warning message if the user has asked for it.
24826
24827 @<Declare text measuring subroutines@>=
24828 void mp_lost_warning (MP mp,font_number f, pool_pointer k) { 
24829   if ( mp->internal[mp_tracing_lost_chars]>0 ) { 
24830     mp_begin_diagnostic(mp);
24831     if ( mp->selector==log_only ) incr(mp->selector);
24832     mp_print_nl(mp, "Missing character: There is no ");
24833 @.Missing character@>
24834     mp_print_str(mp, mp->str_pool[k]); 
24835     mp_print(mp, " in font ");
24836     mp_print(mp, mp->font_name[f]); mp_print_char(mp, '!'); 
24837     mp_end_diagnostic(mp, false);
24838   }
24839 }
24840
24841 @ The whole purpose of saving the height, width, and depth information is to be
24842 able to find the bounding box of an item of text in an edge structure.  The
24843 |set_text_box| procedure takes a text node and adds this information.
24844
24845 @<Declare text measuring subroutines@>=
24846 void mp_set_text_box (MP mp,pointer p) {
24847   font_number f; /* |font_n(p)| */
24848   ASCII_code bc,ec; /* range of valid characters for font |f| */
24849   pool_pointer k,kk; /* current character and character to stop at */
24850   four_quarters cc; /* the |char_info| for the current character */
24851   scaled h,d; /* dimensions of the current character */
24852   width_val(p)=0;
24853   height_val(p)=-el_gordo;
24854   depth_val(p)=-el_gordo;
24855   f=font_n(p);
24856   bc=mp->font_bc[f];
24857   ec=mp->font_ec[f];
24858   kk=str_stop(text_p(p));
24859   k=mp->str_start[text_p(p)];
24860   while ( k<kk ) {
24861     @<Adjust |p|'s bounding box to contain |str_pool[k]|; advance |k|@>;
24862   }
24863   @<Set the height and depth to zero if the bounding box is empty@>;
24864 }
24865
24866 @ @<Adjust |p|'s bounding box to contain |str_pool[k]|; advance |k|@>=
24867
24868   if ( (mp->str_pool[k]<bc)||(mp->str_pool[k]>ec) ) {
24869     mp_lost_warning(mp, f,k);
24870   } else { 
24871     cc=char_info(f)(mp->str_pool[k]);
24872     if ( ! ichar_exists(cc) ) {
24873       mp_lost_warning(mp, f,k);
24874     } else { 
24875       width_val(p)=width_val(p)+char_width(f)(cc);
24876       h=char_height(f)(cc);
24877       d=char_depth(f)(cc);
24878       if ( h>height_val(p) ) height_val(p)=h;
24879       if ( d>depth_val(p) ) depth_val(p)=d;
24880     }
24881   }
24882   incr(k);
24883 }
24884
24885 @ Let's hope modern compilers do comparisons correctly when the difference would
24886 overflow.
24887
24888 @<Set the height and depth to zero if the bounding box is empty@>=
24889 if ( height_val(p)<-depth_val(p) ) { 
24890   height_val(p)=0;
24891   depth_val(p)=0;
24892 }
24893
24894 @ The new primitives fontmapfile and fontmapline.
24895
24896 @<Declare action procedures for use by |do_statement|@>=
24897 void mp_do_mapfile (MP mp) ;
24898 void mp_do_mapline (MP mp) ;
24899
24900 @ @c void mp_do_mapfile (MP mp) { 
24901   mp_get_x_next(mp); mp_scan_expression(mp);
24902   if ( mp->cur_type!=mp_string_type ) {
24903     @<Complain about improper map operation@>;
24904   } else {
24905     mp_map_file(mp,mp->cur_exp);
24906   }
24907 }
24908 void mp_do_mapline (MP mp) { 
24909   mp_get_x_next(mp); mp_scan_expression(mp);
24910   if ( mp->cur_type!=mp_string_type ) {
24911      @<Complain about improper map operation@>;
24912   } else { 
24913      mp_map_line(mp,mp->cur_exp);
24914   }
24915 }
24916
24917 @ @<Complain about improper map operation@>=
24918
24919   exp_err("Unsuitable expression");
24920   help1("Only known strings can be map files or map lines.");
24921   mp_put_get_error(mp);
24922 }
24923
24924 @ To print |scaled| value to PDF output we need some subroutines to ensure
24925 accurary.
24926
24927 @d max_integer   0x7FFFFFFF /* $2^{31}-1$ */
24928
24929 @<Glob...@>=
24930 scaled one_bp; /* scaled value corresponds to 1bp */
24931 scaled one_hundred_bp; /* scaled value corresponds to 100bp */
24932 scaled one_hundred_inch; /* scaled value corresponds to 100in */
24933 integer ten_pow[10]; /* $10^0..10^9$ */
24934 integer scaled_out; /* amount of |scaled| that was taken out in |divide_scaled| */
24935
24936 @ @<Set init...@>=
24937 mp->one_bp = 65782; /* 65781.76 */
24938 mp->one_hundred_bp = 6578176;
24939 mp->one_hundred_inch = 473628672;
24940 mp->ten_pow[0] = 1;
24941 for (i = 1;i<= 9; i++ ) {
24942   mp->ten_pow[i] = 10*mp->ten_pow[i - 1];
24943 }
24944
24945 @ The following function divides |s| by |m|. |dd| is number of decimal digits.
24946
24947 @c scaled mp_divide_scaled (MP mp,scaled s, scaled m, integer  dd) {
24948   scaled q,r;
24949   integer sign,i;
24950   sign = 1;
24951   if ( s < 0 ) { sign = -sign; s = -s; }
24952   if ( m < 0 ) { sign = -sign; m = -m; }
24953   if ( m == 0 )
24954     mp_confusion(mp, "arithmetic: divided by zero");
24955   else if ( m >= (max_integer / 10) )
24956     mp_confusion(mp, "arithmetic: number too big");
24957   q = s / m;
24958   r = s % m;
24959   for (i = 1;i<=dd;i++) {
24960     q = 10*q + (10*r) / m;
24961     r = (10*r) % m;
24962   }
24963   if ( 2*r >= m ) { incr(q); r = r - m; }
24964   mp->scaled_out = sign*(s - (r / mp->ten_pow[dd]));
24965   return (sign*q);
24966 }
24967
24968 @* \[44] Shipping pictures out.
24969 The |ship_out| procedure, to be described below, is given a pointer to
24970 an edge structure. Its mission is to output a file containing the \ps\
24971 description of an edge structure.
24972
24973 @ Each time an edge structure is shipped out we write a new \ps\ output
24974 file named according to the current \&{charcode}.
24975 @:char_code_}{\&{charcode} primitive@>
24976
24977 This is the only backend function that remains in the main |mpost.w| file. 
24978 There are just too many variable accesses needed for status reporting 
24979 etcetera to make it worthwile to move the code to |psout.w|.
24980
24981 @<Internal library declarations@>=
24982 void mp_open_output_file (MP mp) ;
24983
24984 @ @c 
24985 char *mp_set_output_file_name (MP mp, integer c) {
24986   char *ss = NULL; /* filename extension proposal */  
24987   int old_setting; /* previous |selector| setting */
24988   pool_pointer i; /*  indexes into |filename_template|  */
24989   integer cc; /* a temporary integer for template building  */
24990   integer f,g=0; /* field widths */
24991   if ( mp->job_name==NULL ) mp_open_log_file(mp);
24992   if ( mp->filename_template==0 ) {
24993     char *s; /* a file extension derived from |c| */
24994     if ( c<0 ) 
24995       s=xstrdup(".ps");
24996     else 
24997       @<Use |c| to compute the file extension |s|@>;
24998     mp_pack_job_name(mp, s);
24999     ss = s ;
25000   } else { /* initializations */
25001     str_number s, n; /* a file extension derived from |c| */
25002     old_setting=mp->selector; 
25003     mp->selector=new_string;
25004     f = 0;
25005     i = mp->str_start[mp->filename_template];
25006     n = rts(""); /* initialize */
25007     while ( i<str_stop(mp->filename_template) ) {
25008        if ( mp->str_pool[i]=='%' ) {
25009       CONTINUE:
25010         incr(i);
25011         if ( i<str_stop(mp->filename_template) ) {
25012           if ( mp->str_pool[i]=='j' ) {
25013             mp_print(mp, mp->job_name);
25014           } else if ( mp->str_pool[i]=='d' ) {
25015              cc= mp_round_unscaled(mp, mp->internal[mp_day]);
25016              print_with_leading_zeroes(cc);
25017           } else if ( mp->str_pool[i]=='m' ) {
25018              cc= mp_round_unscaled(mp, mp->internal[mp_month]);
25019              print_with_leading_zeroes(cc);
25020           } else if ( mp->str_pool[i]=='y' ) {
25021              cc= mp_round_unscaled(mp, mp->internal[mp_year]);
25022              print_with_leading_zeroes(cc);
25023           } else if ( mp->str_pool[i]=='H' ) {
25024              cc= mp_round_unscaled(mp, mp->internal[mp_time]) / 60;
25025              print_with_leading_zeroes(cc);
25026           }  else if ( mp->str_pool[i]=='M' ) {
25027              cc= mp_round_unscaled(mp, mp->internal[mp_time]) % 60;
25028              print_with_leading_zeroes(cc);
25029           } else if ( mp->str_pool[i]=='c' ) {
25030             if ( c<0 ) mp_print(mp, "ps");
25031             else print_with_leading_zeroes(c);
25032           } else if ( (mp->str_pool[i]>='0') && 
25033                       (mp->str_pool[i]<='9') ) {
25034             if ( (f<10)  )
25035               f = (f*10) + mp->str_pool[i]-'0';
25036             goto CONTINUE;
25037           } else {
25038             mp_print_str(mp, mp->str_pool[i]);
25039           }
25040         }
25041       } else {
25042         if ( mp->str_pool[i]=='.' )
25043           if (length(n)==0)
25044             n = mp_make_string(mp);
25045         mp_print_str(mp, mp->str_pool[i]);
25046       };
25047       incr(i);
25048     };
25049     s = mp_make_string(mp);
25050     mp->selector= old_setting;
25051     if (length(n)==0) {
25052        n=s;
25053        s=rts("");
25054     };
25055     mp_pack_file_name(mp, str(n),"",str(s));
25056     delete_str_ref(n);
25057         ss = str(s);
25058     delete_str_ref(s);
25059   }
25060   return ss;
25061 }
25062
25063 char * mp_get_output_file_name (MP mp) {
25064   char *fname; /* return value */
25065   char *saved_name;  /* saved |name_of_file| */
25066   saved_name = mp_xstrdup(mp, mp->name_of_file);
25067   (void)mp_set_output_file_name(mp, mp_round_unscaled(mp, mp->internal[mp_char_code]));
25068   fname = mp_xstrdup(mp, mp->name_of_file);
25069   mp_pack_file_name(mp, saved_name,NULL,NULL);
25070   return fname;
25071 }
25072
25073 void mp_open_output_file (MP mp) {
25074   char *ss; /* filename extension proposal */
25075   integer c; /* \&{charcode} rounded to the nearest integer */
25076   c=mp_round_unscaled(mp, mp->internal[mp_char_code]);
25077   ss = mp_set_output_file_name(mp, c);
25078   while ( ! mp_a_open_out(mp, (void *)&mp->ps_file, mp_filetype_postscript) )
25079     mp_prompt_file_name(mp, "file name for output",ss);
25080   xfree(ss);
25081   @<Store the true output file name if appropriate@>;
25082 }
25083
25084 @ The file extension created here could be up to five characters long in
25085 extreme cases so it may have to be shortened on some systems.
25086 @^system dependencies@>
25087
25088 @<Use |c| to compute the file extension |s|@>=
25089
25090   s = xmalloc(7,1);
25091   snprintf(s,7,".%i",(int)c);
25092 }
25093
25094 @ The user won't want to see all the output file names so we only save the
25095 first and last ones and a count of how many there were.  For this purpose
25096 files are ordered primarily by \&{charcode} and secondarily by order of
25097 creation.
25098 @:char_code_}{\&{charcode} primitive@>
25099
25100 @<Store the true output file name if appropriate@>=
25101 if ((c<mp->first_output_code)&&(mp->first_output_code>=0)) {
25102   mp->first_output_code=c;
25103   xfree(mp->first_file_name);
25104   mp->first_file_name=xstrdup(mp->name_of_file);
25105 }
25106 if ( c>=mp->last_output_code ) {
25107   mp->last_output_code=c;
25108   xfree(mp->last_file_name);
25109   mp->last_file_name=xstrdup(mp->name_of_file);
25110 }
25111
25112 @ @<Glob...@>=
25113 char * first_file_name;
25114 char * last_file_name; /* full file names */
25115 integer first_output_code;integer last_output_code; /* rounded \&{charcode} values */
25116 @:char_code_}{\&{charcode} primitive@>
25117 integer total_shipped; /* total number of |ship_out| operations completed */
25118
25119 @ @<Set init...@>=
25120 mp->first_file_name=xstrdup("");
25121 mp->last_file_name=xstrdup("");
25122 mp->first_output_code=32768;
25123 mp->last_output_code=-32768;
25124 mp->total_shipped=0;
25125
25126 @ @<Dealloc variables@>=
25127 xfree(mp->first_file_name);
25128 xfree(mp->last_file_name);
25129
25130 @ @<Begin the progress report for the output of picture~|c|@>=
25131 if ( (int)mp->term_offset>mp->max_print_line-6 ) mp_print_ln(mp);
25132 else if ( (mp->term_offset>0)||(mp->file_offset>0) ) mp_print_char(mp, ' ');
25133 mp_print_char(mp, '[');
25134 if ( c>=0 ) mp_print_int(mp, c)
25135
25136 @ @<End progress report@>=
25137 mp_print_char(mp, ']');
25138 update_terminal;
25139 incr(mp->total_shipped)
25140
25141 @ @<Explain what output files were written@>=
25142 if ( mp->total_shipped>0 ) { 
25143   mp_print_nl(mp, "");
25144   mp_print_int(mp, mp->total_shipped);
25145   mp_print(mp, " output file");
25146   if ( mp->total_shipped>1 ) mp_print_char(mp, 's');
25147   mp_print(mp, " written: ");
25148   mp_print(mp, mp->first_file_name);
25149   if ( mp->total_shipped>1 ) {
25150     if ( 31+strlen(mp->first_file_name)+
25151          strlen(mp->last_file_name)> (unsigned)mp->max_print_line) 
25152       mp_print_ln(mp);
25153     mp_print(mp, " .. ");
25154     mp_print(mp, mp->last_file_name);
25155   }
25156 }
25157
25158 @ @<Internal library declarations@>=
25159 boolean mp_has_font_size(MP mp, font_number f );
25160
25161 @ @c 
25162 boolean mp_has_font_size(MP mp, font_number f ) {
25163   return (mp->font_sizes[f]!=null);
25164 }
25165
25166 @ The \&{special} command saves up lines of text to be printed during the next
25167 |ship_out| operation.  The saved items are stored as a list of capsule tokens.
25168
25169 @<Glob...@>=
25170 pointer last_pending; /* the last token in a list of pending specials */
25171
25172 @ @<Set init...@>=
25173 mp->last_pending=spec_head;
25174
25175 @ @<Cases of |do_statement|...@>=
25176 case special_command: 
25177   if ( mp->cur_mod==0 ) mp_do_special(mp); else 
25178   if ( mp->cur_mod==1 ) mp_do_mapfile(mp); else 
25179   mp_do_mapline(mp);
25180   break;
25181
25182 @ @<Declare action procedures for use by |do_statement|@>=
25183 void mp_do_special (MP mp) ;
25184
25185 @ @c void mp_do_special (MP mp) { 
25186   mp_get_x_next(mp); mp_scan_expression(mp);
25187   if ( mp->cur_type!=mp_string_type ) {
25188     @<Complain about improper special operation@>;
25189   } else { 
25190     link(mp->last_pending)=mp_stash_cur_exp(mp);
25191     mp->last_pending=link(mp->last_pending);
25192     link(mp->last_pending)=null;
25193   }
25194 }
25195
25196 @ @<Complain about improper special operation@>=
25197
25198   exp_err("Unsuitable expression");
25199   help1("Only known strings are allowed for output as specials.");
25200   mp_put_get_error(mp);
25201 }
25202
25203 @ On the export side, we need an extra object type for special strings.
25204
25205 @<Graphical object codes@>=
25206 mp_special_code=8, 
25207
25208 @ @<Export pending specials@>=
25209 p=link(spec_head);
25210 while ( p!=null ) {
25211   mp_special_object *tp;
25212   tp = (mp_special_object *)mp_new_graphic_object(mp,mp_special_code);  
25213   gr_pre_script(tp)  = str(value(p));
25214   if (hh->body==NULL) hh->body = (mp_graphic_object *)tp; 
25215   else gr_link(hp) = (mp_graphic_object *)tp;
25216   hp = (mp_graphic_object *)tp;
25217   p=link(p);
25218 }
25219 mp_flush_token_list(mp, link(spec_head));
25220 link(spec_head)=null;
25221 mp->last_pending=spec_head
25222
25223 @ We are now ready for the main output procedure.  Note that the |selector|
25224 setting is saved in a global variable so that |begin_diagnostic| can access it.
25225
25226 @<Declare the \ps\ output procedures@>=
25227 void mp_ship_out (MP mp, pointer h) ;
25228
25229 @ Once again, the |gr_XXXX| macros are defined in |mppsout.h|
25230
25231 @d export_color(q,p) 
25232   if ( color_model(p)==mp_uninitialized_model ) {
25233     gr_color_model(q)  = (mp->internal[mp_default_color_model]>>16);
25234     gr_cyan_val(q)     = 0;
25235         gr_magenta_val(q)  = 0;
25236         gr_yellow_val(q)   = 0;
25237         gr_black_val(q)    = (gr_color_model(q)==mp_cmyk_model ? unity : 0);
25238   } else {
25239     gr_color_model(q)  = color_model(p);
25240     gr_cyan_val(q)     = cyan_val(p);
25241     gr_magenta_val(q)  = magenta_val(p);
25242     gr_yellow_val(q)   = yellow_val(p);
25243     gr_black_val(q)    = black_val(p);
25244   }
25245
25246 @d export_scripts(q,p)
25247   if (pre_script(p)!=null)  gr_pre_script(q)   = str(pre_script(p));
25248   if (post_script(p)!=null) gr_post_script(q)  = str(post_script(p));
25249
25250 @c
25251 struct mp_edge_object *mp_gr_export(MP mp, pointer h) {
25252   pointer p; /* the current graphical object */
25253   integer t; /* a temporary value */
25254   scaled d_width; /* the current pen width */
25255   mp_edge_object *hh; /* the first graphical object */
25256   struct mp_graphic_object *hq; /* something |hp| points to  */
25257   struct mp_text_object    *tt;
25258   struct mp_fill_object    *tf;
25259   struct mp_stroked_object *ts;
25260   struct mp_clip_object    *tc;
25261   struct mp_bounds_object  *tb;
25262   struct mp_graphic_object *hp = NULL; /* the current graphical object */
25263   mp_set_bbox(mp, h, true);
25264   hh = mp_xmalloc(mp,1,sizeof(mp_edge_object));
25265   hh->body = NULL;
25266   hh->_next = NULL;
25267   hh->_parent = mp;
25268   hh->_minx = minx_val(h);
25269   hh->_miny = miny_val(h);
25270   hh->_maxx = maxx_val(h);
25271   hh->_maxy = maxy_val(h);
25272   hh->_filename = mp_get_output_file_name(mp);
25273   @<Export pending specials@>;
25274   p=link(dummy_loc(h));
25275   while ( p!=null ) { 
25276     hq = mp_new_graphic_object(mp,type(p));
25277     switch (type(p)) {
25278     case mp_fill_code:
25279       tf = (mp_fill_object *)hq;
25280       gr_pen_p(tf)        = mp_export_knot_list(mp,pen_p(p));
25281       d_width = mp_get_pen_scale(mp, pen_p(p));
25282       if ((pen_p(p)==null) || pen_is_elliptical(pen_p(p)))  {
25283             gr_path_p(tf)       = mp_export_knot_list(mp,path_p(p));
25284       } else {
25285         pointer pc, pp;
25286         pc = mp_copy_path(mp, path_p(p));
25287         pp = mp_make_envelope(mp, pc, pen_p(p),ljoin_val(p),0,miterlim_val(p));
25288         gr_path_p(tf)       = mp_export_knot_list(mp,pp);
25289         mp_toss_knot_list(mp, pp);
25290         pc = mp_htap_ypoc(mp, path_p(p));
25291         pp = mp_make_envelope(mp, pc, pen_p(p),ljoin_val(p),0,miterlim_val(p));
25292         gr_htap_p(tf)       = mp_export_knot_list(mp,pp);
25293         mp_toss_knot_list(mp, pp);
25294       }
25295       export_color(tf,p) ;
25296       export_scripts(tf,p);
25297       gr_ljoin_val(tf)    = ljoin_val(p);
25298       gr_miterlim_val(tf) = miterlim_val(p);
25299       break;
25300     case mp_stroked_code:
25301       ts = (mp_stroked_object *)hq;
25302       gr_pen_p(ts)        = mp_export_knot_list(mp,pen_p(p));
25303       d_width = mp_get_pen_scale(mp, pen_p(p));
25304       if (pen_is_elliptical(pen_p(p)))  {
25305               gr_path_p(ts)       = mp_export_knot_list(mp,path_p(p));
25306       } else {
25307         pointer pc;
25308         pc=mp_copy_path(mp, path_p(p));
25309         t=lcap_val(p);
25310         if ( left_type(pc)!=mp_endpoint ) { 
25311           left_type(mp_insert_knot(mp, pc,x_coord(pc),y_coord(pc)))=mp_endpoint;
25312           right_type(pc)=mp_endpoint;
25313           pc=link(pc);
25314           t=1;
25315         }
25316         pc=mp_make_envelope(mp,pc,pen_p(p),ljoin_val(p),t,miterlim_val(p));
25317         gr_path_p(ts)       = mp_export_knot_list(mp,pc);
25318         mp_toss_knot_list(mp, pc);
25319       }
25320       export_color(ts,p) ;
25321       export_scripts(ts,p);
25322       gr_ljoin_val(ts)    = ljoin_val(p);
25323       gr_miterlim_val(ts) = miterlim_val(p);
25324       gr_lcap_val(ts)     = lcap_val(p);
25325       gr_dash_p(ts)       = mp_export_dashes(mp,p,&d_width);
25326       break;
25327     case mp_text_code:
25328       tt = (mp_text_object *)hq;
25329       gr_text_p(tt)       = str(text_p(p));
25330       gr_font_n(tt)       = font_n(p);
25331       gr_font_name(tt)    = mp_xstrdup(mp,mp->font_name[font_n(p)]);
25332       gr_font_dsize(tt)   = mp->font_dsize[font_n(p)];
25333       export_color(tt,p) ;
25334       export_scripts(tt,p);
25335       gr_width_val(tt)    = width_val(p);
25336       gr_height_val(tt)   = height_val(p);
25337       gr_depth_val(tt)    = depth_val(p);
25338       gr_tx_val(tt)       = tx_val(p);
25339       gr_ty_val(tt)       = ty_val(p);
25340       gr_txx_val(tt)      = txx_val(p);
25341       gr_txy_val(tt)      = txy_val(p);
25342       gr_tyx_val(tt)      = tyx_val(p);
25343       gr_tyy_val(tt)      = tyy_val(p);
25344       break;
25345     case mp_start_clip_code: 
25346       tc = (mp_clip_object *)hq;
25347       gr_path_p(tc) = mp_export_knot_list(mp,path_p(p));
25348       break;
25349     case mp_start_bounds_code:
25350       tb = (mp_bounds_object *)hq;
25351       gr_path_p(tb) = mp_export_knot_list(mp,path_p(p));
25352       break;
25353     case mp_stop_clip_code: 
25354     case mp_stop_bounds_code:
25355       /* nothing to do here */
25356       break;
25357     } 
25358     if (hh->body==NULL) hh->body=hq; else  gr_link(hp) = hq;
25359     hp = hq;
25360     p=link(p);
25361   }
25362   return hh;
25363 }
25364
25365 @ @<Exported function ...@>=
25366 struct mp_edge_object *mp_gr_export(MP mp, int h);
25367
25368 @ This function is now nearly trivial.
25369
25370 @c
25371 void mp_ship_out (MP mp, pointer h) { /* output edge structure |h| */
25372   integer c; /* \&{charcode} rounded to the nearest integer */
25373   c=mp_round_unscaled(mp, mp->internal[mp_char_code]);
25374   @<Begin the progress report for the output of picture~|c|@>;
25375   (mp->shipout_backend) (mp, h);
25376   @<End progress report@>;
25377   if ( mp->internal[mp_tracing_output]>0 ) 
25378    mp_print_edges(mp, h," (just shipped out)",true);
25379 }
25380
25381 @ @<Declarations@>=
25382 void mp_shipout_backend (MP mp, pointer h);
25383
25384 @ @c
25385 void mp_shipout_backend (MP mp, pointer h) {
25386   mp_edge_object *hh; /* the first graphical object */
25387   hh = mp_gr_export(mp,h);
25388   mp_gr_ship_out (hh,
25389                  (mp->internal[mp_prologues]>>16),
25390                  (mp->internal[mp_procset]>>16));
25391   mp_gr_toss_objects(hh);
25392 }
25393
25394 @ @<Exported types@>=
25395 typedef void (*mp_backend_writer)(MP, int);
25396
25397 @ @<Option variables@>=
25398 mp_backend_writer shipout_backend;
25399
25400 @ @<Allocate or initialize ...@>=
25401 set_callback_option(shipout_backend);
25402
25403 @ Now that we've finished |ship_out|, let's look at the other commands
25404 by which a user can send things to the \.{GF} file.
25405
25406 @ @<Determine if a character has been shipped out@>=
25407
25408   mp->cur_exp=mp_round_unscaled(mp, mp->cur_exp) % 256;
25409   if ( mp->cur_exp<0 ) mp->cur_exp=mp->cur_exp+256;
25410   boolean_reset(mp->char_exists[mp->cur_exp]);
25411   mp->cur_type=mp_boolean_type;
25412 }
25413
25414 @ @<Glob...@>=
25415 psout_data ps;
25416
25417 @ @<Allocate or initialize ...@>=
25418 mp_backend_initialize(mp);
25419
25420 @ @<Dealloc...@>=
25421 mp_backend_free(mp);
25422
25423
25424 @* \[45] Dumping and undumping the tables.
25425 After \.{INIMP} has seen a collection of macros, it
25426 can write all the necessary information on an auxiliary file so
25427 that production versions of \MP\ are able to initialize their
25428 memory at high speed. The present section of the program takes
25429 care of such output and input. We shall consider simultaneously
25430 the processes of storing and restoring,
25431 so that the inverse relation between them is clear.
25432 @.INIMP@>
25433
25434 The global variable |mem_ident| is a string that is printed right
25435 after the |banner| line when \MP\ is ready to start. For \.{INIMP} this
25436 string says simply `\.{(INIMP)}'; for other versions of \MP\ it says,
25437 for example, `\.{(mem=plain 1990.4.14)}', showing the year,
25438 month, and day that the mem file was created. We have |mem_ident=0|
25439 before \MP's tables are loaded.
25440
25441 @<Glob...@>=
25442 char * mem_ident;
25443
25444 @ @<Set init...@>=
25445 mp->mem_ident=NULL;
25446
25447 @ @<Initialize table entries...@>=
25448 mp->mem_ident=xstrdup(" (INIMP)");
25449
25450 @ @<Declare act...@>=
25451 void mp_store_mem_file (MP mp) ;
25452
25453 @ @c void mp_store_mem_file (MP mp) {
25454   integer k;  /* all-purpose index */
25455   pointer p,q; /* all-purpose pointers */
25456   integer x; /* something to dump */
25457   four_quarters w; /* four ASCII codes */
25458   memory_word WW;
25459   @<Create the |mem_ident|, open the mem file,
25460     and inform the user that dumping has begun@>;
25461   @<Dump constants for consistency check@>;
25462   @<Dump the string pool@>;
25463   @<Dump the dynamic memory@>;
25464   @<Dump the table of equivalents and the hash table@>;
25465   @<Dump a few more things and the closing check word@>;
25466   @<Close the mem file@>;
25467 }
25468
25469 @ Corresponding to the procedure that dumps a mem file, we also have a function
25470 that reads~one~in. The function returns |false| if the dumped mem is
25471 incompatible with the present \MP\ table sizes, etc.
25472
25473 @d off_base 6666 /* go here if the mem file is unacceptable */
25474 @d too_small(A) { wake_up_terminal;
25475   wterm_ln("---! Must increase the "); wterm((A));
25476 @.Must increase the x@>
25477   goto OFF_BASE;
25478   }
25479
25480 @c 
25481 boolean mp_load_mem_file (MP mp) {
25482   integer k; /* all-purpose index */
25483   pointer p,q; /* all-purpose pointers */
25484   integer x; /* something undumped */
25485   str_number s; /* some temporary string */
25486   four_quarters w; /* four ASCII codes */
25487   memory_word WW;
25488   @<Undump constants for consistency check@>;
25489   @<Undump the string pool@>;
25490   @<Undump the dynamic memory@>;
25491   @<Undump the table of equivalents and the hash table@>;
25492   @<Undump a few more things and the closing check word@>;
25493   return true; /* it worked! */
25494 OFF_BASE: 
25495   wake_up_terminal;
25496   wterm_ln("(Fatal mem file error; I'm stymied)\n");
25497 @.Fatal mem file error@>
25498    return false;
25499 }
25500
25501 @ @<Declarations@>=
25502 boolean mp_load_mem_file (MP mp) ;
25503
25504 @ Mem files consist of |memory_word| items, and we use the following
25505 macros to dump words of different types:
25506
25507 @d dump_wd(A)   { WW=(A);       (mp->write_binary_file)(mp,mp->mem_file,&WW,sizeof(WW)); }
25508 @d dump_int(A)  { int cint=(A); (mp->write_binary_file)(mp,mp->mem_file,&cint,sizeof(cint)); }
25509 @d dump_hh(A)   { WW.hh=(A);    (mp->write_binary_file)(mp,mp->mem_file,&WW,sizeof(WW)); }
25510 @d dump_qqqq(A) { WW.qqqq=(A);  (mp->write_binary_file)(mp,mp->mem_file,&WW,sizeof(WW)); }
25511 @d dump_string(A) { dump_int(strlen(A)+1);
25512                     (mp->write_binary_file)(mp,mp->mem_file,A,strlen(A)+1); }
25513
25514 @<Glob...@>=
25515 void * mem_file; /* for input or output of mem information */
25516
25517 @ The inverse macros are slightly more complicated, since we need to check
25518 the range of the values we are reading in. We say `|undump(a)(b)(x)|' to
25519 read an integer value |x| that is supposed to be in the range |a<=x<=b|.
25520
25521 @d mgeti(A) do {
25522   size_t wanted = sizeof(A);
25523   void *A_ptr = &A;
25524   (mp->read_binary_file)(mp, mp->mem_file,&A_ptr,&wanted);
25525   if (wanted!=sizeof(A)) goto OFF_BASE;
25526 } while (0)
25527
25528 @d mgetw(A) do {
25529   size_t wanted = sizeof(A);
25530   void *A_ptr = &A;
25531   (mp->read_binary_file)(mp, mp->mem_file,&A_ptr,&wanted);
25532   if (wanted!=sizeof(A)) goto OFF_BASE;
25533 } while (0)
25534
25535 @d undump_wd(A)   { mgetw(WW); A=WW; }
25536 @d undump_int(A)  { int cint; mgeti(cint); A=cint; }
25537 @d undump_hh(A)   { mgetw(WW); A=WW.hh; }
25538 @d undump_qqqq(A) { mgetw(WW); A=WW.qqqq; }
25539 @d undump_strings(A,B,C) { 
25540    undump_int(x); if ( (x<(A)) || (x>(B)) ) goto OFF_BASE; else C=str(x); }
25541 @d undump(A,B,C) { undump_int(x); if ( (x<(A)) || (x>(int)(B)) ) goto OFF_BASE; else C=x; }
25542 @d undump_size(A,B,C,D) { undump_int(x);
25543                           if (x<(A)) goto OFF_BASE; 
25544                           if (x>(B)) { too_small((C)); } else { D=x;} }
25545 @d undump_string(A) do { 
25546   size_t the_wanted; 
25547   void *the_string;
25548   integer XX=0; 
25549   undump_int(XX);
25550   the_wanted = XX;
25551   the_string = xmalloc(XX,sizeof(char));
25552   (mp->read_binary_file)(mp,mp->mem_file,&the_string,&the_wanted);
25553   A = (char *)the_string;
25554   if (the_wanted!=(size_t)XX) goto OFF_BASE;
25555 } while (0)
25556
25557 @ The next few sections of the program should make it clear how we use the
25558 dump/undump macros.
25559
25560 @<Dump constants for consistency check@>=
25561 dump_int(mp->mem_top);
25562 dump_int(mp->hash_size);
25563 dump_int(mp->hash_prime)
25564 dump_int(mp->param_size);
25565 dump_int(mp->max_in_open);
25566
25567 @ Sections of a \.{WEB} program that are ``commented out'' still contribute
25568 strings to the string pool; therefore \.{INIMP} and \MP\ will have
25569 the same strings. (And it is, of course, a good thing that they do.)
25570 @.WEB@>
25571 @^string pool@>
25572
25573 @<Undump constants for consistency check@>=
25574 undump_int(x); mp->mem_top = x;
25575 undump_int(x); if (mp->hash_size != x) goto OFF_BASE;
25576 undump_int(x); if (mp->hash_prime != x) goto OFF_BASE;
25577 undump_int(x); if (mp->param_size != x) goto OFF_BASE;
25578 undump_int(x); if (mp->max_in_open != x) goto OFF_BASE
25579
25580 @ We do string pool compaction to avoid dumping unused strings.
25581
25582 @d dump_four_ASCII 
25583   w.b0=qi(mp->str_pool[k]); w.b1=qi(mp->str_pool[k+1]);
25584   w.b2=qi(mp->str_pool[k+2]); w.b3=qi(mp->str_pool[k+3]);
25585   dump_qqqq(w)
25586
25587 @<Dump the string pool@>=
25588 mp_do_compaction(mp, mp->pool_size);
25589 dump_int(mp->pool_ptr);
25590 dump_int(mp->max_str_ptr);
25591 dump_int(mp->str_ptr);
25592 k=0;
25593 while ( (mp->next_str[k]==k+1) && (k<=mp->max_str_ptr) ) 
25594   incr(k);
25595 dump_int(k);
25596 while ( k<=mp->max_str_ptr ) { 
25597   dump_int(mp->next_str[k]); incr(k);
25598 }
25599 k=0;
25600 while (1)  { 
25601   dump_int(mp->str_start[k]); /* TODO: valgrind warning here */
25602   if ( k==mp->str_ptr ) {
25603     break;
25604   } else { 
25605     k=mp->next_str[k]; 
25606   }
25607 }
25608 k=0;
25609 while (k+4<mp->pool_ptr ) { 
25610   dump_four_ASCII; k=k+4; 
25611 }
25612 k=mp->pool_ptr-4; dump_four_ASCII;
25613 mp_print_ln(mp); mp_print(mp, "at most "); mp_print_int(mp, mp->max_str_ptr);
25614 mp_print(mp, " strings of total length ");
25615 mp_print_int(mp, mp->pool_ptr)
25616
25617 @ @d undump_four_ASCII 
25618   undump_qqqq(w);
25619   mp->str_pool[k]=qo(w.b0); mp->str_pool[k+1]=qo(w.b1);
25620   mp->str_pool[k+2]=qo(w.b2); mp->str_pool[k+3]=qo(w.b3)
25621
25622 @<Undump the string pool@>=
25623 undump_int(mp->pool_ptr);
25624 mp_reallocate_pool(mp, mp->pool_ptr) ;
25625 undump_int(mp->max_str_ptr);
25626 mp_reallocate_strings (mp,mp->max_str_ptr) ;
25627 undump(0,mp->max_str_ptr,mp->str_ptr);
25628 undump(0,mp->max_str_ptr+1,s);
25629 for (k=0;k<=s-1;k++) 
25630   mp->next_str[k]=k+1;
25631 for (k=s;k<=mp->max_str_ptr;k++) 
25632   undump(s+1,mp->max_str_ptr+1,mp->next_str[k]);
25633 mp->fixed_str_use=0;
25634 k=0;
25635 while (1) { 
25636   undump(0,mp->pool_ptr,mp->str_start[k]);
25637   if ( k==mp->str_ptr ) break;
25638   mp->str_ref[k]=max_str_ref;
25639   incr(mp->fixed_str_use);
25640   mp->last_fixed_str=k; k=mp->next_str[k];
25641 }
25642 k=0;
25643 while ( k+4<mp->pool_ptr ) { 
25644   undump_four_ASCII; k=k+4;
25645 }
25646 k=mp->pool_ptr-4; undump_four_ASCII;
25647 mp->init_str_use=mp->fixed_str_use; mp->init_pool_ptr=mp->pool_ptr;
25648 mp->max_pool_ptr=mp->pool_ptr;
25649 mp->strs_used_up=mp->fixed_str_use;
25650 mp->pool_in_use=mp->str_start[mp->str_ptr]; mp->strs_in_use=mp->fixed_str_use;
25651 mp->max_pl_used=mp->pool_in_use; mp->max_strs_used=mp->strs_in_use;
25652 mp->pact_count=0; mp->pact_chars=0; mp->pact_strs=0;
25653
25654 @ By sorting the list of available spaces in the variable-size portion of
25655 |mem|, we are usually able to get by without having to dump very much
25656 of the dynamic memory.
25657
25658 We recompute |var_used| and |dyn_used|, so that \.{INIMP} dumps valid
25659 information even when it has not been gathering statistics.
25660
25661 @<Dump the dynamic memory@>=
25662 mp_sort_avail(mp); mp->var_used=0;
25663 dump_int(mp->lo_mem_max); dump_int(mp->rover);
25664 p=0; q=mp->rover; x=0;
25665 do {  
25666   for (k=p;k<= q+1;k++) 
25667     dump_wd(mp->mem[k]);
25668   x=x+q+2-p; mp->var_used=mp->var_used+q-p;
25669   p=q+node_size(q); q=rlink(q);
25670 } while (q!=mp->rover);
25671 mp->var_used=mp->var_used+mp->lo_mem_max-p; 
25672 mp->dyn_used=mp->mem_end+1-mp->hi_mem_min;
25673 for (k=p;k<= mp->lo_mem_max;k++ ) 
25674   dump_wd(mp->mem[k]);
25675 x=x+mp->lo_mem_max+1-p;
25676 dump_int(mp->hi_mem_min); dump_int(mp->avail);
25677 for (k=mp->hi_mem_min;k<=mp->mem_end;k++ ) 
25678   dump_wd(mp->mem[k]);
25679 x=x+mp->mem_end+1-mp->hi_mem_min;
25680 p=mp->avail;
25681 while ( p!=null ) { 
25682   decr(mp->dyn_used); p=link(p);
25683 }
25684 dump_int(mp->var_used); dump_int(mp->dyn_used);
25685 mp_print_ln(mp); mp_print_int(mp, x);
25686 mp_print(mp, " memory locations dumped; current usage is ");
25687 mp_print_int(mp, mp->var_used); mp_print_char(mp, '&'); mp_print_int(mp, mp->dyn_used)
25688
25689 @ @<Undump the dynamic memory@>=
25690 undump(lo_mem_stat_max+1000,hi_mem_stat_min-1,mp->lo_mem_max);
25691 undump(lo_mem_stat_max+1,mp->lo_mem_max,mp->rover);
25692 p=0; q=mp->rover;
25693 do {  
25694   for (k=p;k<= q+1; k++) 
25695     undump_wd(mp->mem[k]);
25696   p=q+node_size(q);
25697   if ( (p>mp->lo_mem_max)||((q>=rlink(q))&&(rlink(q)!=mp->rover)) ) 
25698     goto OFF_BASE;
25699   q=rlink(q);
25700 } while (q!=mp->rover);
25701 for (k=p;k<=mp->lo_mem_max;k++ ) 
25702   undump_wd(mp->mem[k]);
25703 undump(mp->lo_mem_max+1,hi_mem_stat_min,mp->hi_mem_min);
25704 undump(null,mp->mem_top,mp->avail); mp->mem_end=mp->mem_top;
25705 mp->last_pending=spec_head;
25706 for (k=mp->hi_mem_min;k<= mp->mem_end;k++) 
25707   undump_wd(mp->mem[k]);
25708 undump_int(mp->var_used); undump_int(mp->dyn_used)
25709
25710 @ A different scheme is used to compress the hash table, since its lower region
25711 is usually sparse. When |text(p)<>0| for |p<=hash_used|, we output three
25712 words: |p|, |hash[p]|, and |eqtb[p]|. The hash table is, of course, densely
25713 packed for |p>=hash_used|, so the remaining entries are output in~a~block.
25714
25715 @<Dump the table of equivalents and the hash table@>=
25716 dump_int(mp->hash_used); 
25717 mp->st_count=frozen_inaccessible-1-mp->hash_used;
25718 for (p=1;p<=mp->hash_used;p++) {
25719   if ( text(p)!=0 ) {
25720      dump_int(p); dump_hh(mp->hash[p]); dump_hh(mp->eqtb[p]); incr(mp->st_count);
25721   }
25722 }
25723 for (p=mp->hash_used+1;p<=(int)hash_end;p++) {
25724   dump_hh(mp->hash[p]); dump_hh(mp->eqtb[p]);
25725 }
25726 dump_int(mp->st_count);
25727 mp_print_ln(mp); mp_print_int(mp, mp->st_count); mp_print(mp, " symbolic tokens")
25728
25729 @ @<Undump the table of equivalents and the hash table@>=
25730 undump(1,frozen_inaccessible,mp->hash_used); 
25731 p=0;
25732 do {  
25733   undump(p+1,mp->hash_used,p); 
25734   undump_hh(mp->hash[p]); undump_hh(mp->eqtb[p]);
25735 } while (p!=mp->hash_used);
25736 for (p=mp->hash_used+1;p<=(int)hash_end;p++ )  { 
25737   undump_hh(mp->hash[p]); undump_hh(mp->eqtb[p]);
25738 }
25739 undump_int(mp->st_count)
25740
25741 @ We have already printed a lot of statistics, so we set |mp_tracing_stats:=0|
25742 to prevent them appearing again.
25743
25744 @<Dump a few more things and the closing check word@>=
25745 dump_int(mp->max_internal);
25746 dump_int(mp->int_ptr);
25747 for (k=1;k<= mp->int_ptr;k++ ) { 
25748   dump_int(mp->internal[k]); 
25749   dump_string(mp->int_name[k]);
25750 }
25751 dump_int(mp->start_sym); 
25752 dump_int(mp->interaction); 
25753 dump_string(mp->mem_ident);
25754 dump_int(mp->bg_loc); dump_int(mp->eg_loc); dump_int(mp->serial_no); dump_int(69073);
25755 mp->internal[mp_tracing_stats]=0
25756
25757 @ @<Undump a few more things and the closing check word@>=
25758 undump_int(x);
25759 if (x>mp->max_internal) mp_grow_internals(mp,x);
25760 undump_int(mp->int_ptr);
25761 for (k=1;k<= mp->int_ptr;k++) { 
25762   undump_int(mp->internal[k]);
25763   undump_string(mp->int_name[k]);
25764 }
25765 undump(0,frozen_inaccessible,mp->start_sym);
25766 if (mp->interaction==mp_unspecified_mode) {
25767   undump(mp_unspecified_mode,mp_error_stop_mode,mp->interaction);
25768 } else {
25769   undump(mp_unspecified_mode,mp_error_stop_mode,x);
25770 }
25771 undump_string(mp->mem_ident);
25772 undump(1,hash_end,mp->bg_loc);
25773 undump(1,hash_end,mp->eg_loc);
25774 undump_int(mp->serial_no);
25775 undump_int(x); 
25776 if (x!=69073) goto OFF_BASE
25777
25778 @ @<Create the |mem_ident|...@>=
25779
25780   xfree(mp->mem_ident);
25781   mp->mem_ident = xmalloc(256,1);
25782   snprintf(mp->mem_ident,256," (mem=%s %i.%i.%i)", 
25783            mp->job_name,
25784            (int)(mp_round_unscaled(mp, mp->internal[mp_year]) % 100),
25785            (int)mp_round_unscaled(mp, mp->internal[mp_month]),
25786            (int)mp_round_unscaled(mp, mp->internal[mp_day]));
25787   mp_pack_job_name(mp, mem_extension);
25788   while (! mp_w_open_out(mp, &mp->mem_file) )
25789     mp_prompt_file_name(mp, "mem file name", mem_extension);
25790   mp_print_nl(mp, "Beginning to dump on file ");
25791 @.Beginning to dump...@>
25792   mp_print(mp, mp->name_of_file); 
25793   mp_print_nl(mp, mp->mem_ident);
25794 }
25795
25796 @ @<Dealloc variables@>=
25797 xfree(mp->mem_ident);
25798
25799 @ @<Close the mem file@>=
25800 (mp->close_file)(mp,mp->mem_file)
25801
25802 @* \[46] The main program.
25803 This is it: the part of \MP\ that executes all those procedures we have
25804 written.
25805
25806 Well---almost. We haven't put the parsing subroutines into the
25807 program yet; and we'd better leave space for a few more routines that may
25808 have been forgotten.
25809
25810 @c @<Declare the basic parsing subroutines@>
25811 @<Declare miscellaneous procedures that were declared |forward|@>
25812 @<Last-minute procedures@>
25813
25814 @ We've noted that there are two versions of \MP. One, called \.{INIMP},
25815 @.INIMP@>
25816 has to be run first; it initializes everything from scratch, without
25817 reading a mem file, and it has the capability of dumping a mem file.
25818 The other one is called `\.{VIRMP}'; it is a ``virgin'' program that needs
25819 @.VIRMP@>
25820 to input a mem file in order to get started. \.{VIRMP} typically has
25821 a bit more memory capacity than \.{INIMP}, because it does not need the
25822 space consumed by the dumping/undumping routines and the numerous calls on
25823 |primitive|, etc.
25824
25825 The \.{VIRMP} program cannot read a mem file instantaneously, of course;
25826 the best implementations therefore allow for production versions of \MP\ that
25827 not only avoid the loading routine for object code, they also have
25828 a mem file pre-loaded. 
25829
25830 @ @<Option variables@>=
25831 int ini_version; /* are we iniMP? */
25832
25833 @ @<Set |ini_version|@>=
25834 mp->ini_version = (opt->ini_version ? true : false);
25835
25836 @ Here we do whatever is needed to complete \MP's job gracefully on the
25837 local operating system. The code here might come into play after a fatal
25838 error; it must therefore consist entirely of ``safe'' operations that
25839 cannot produce error messages. For example, it would be a mistake to call
25840 |str_room| or |make_string| at this time, because a call on |overflow|
25841 might lead to an infinite loop.
25842 @^system dependencies@>
25843
25844 This program doesn't bother to close the input files that may still be open.
25845
25846 @<Last-minute...@>=
25847 void mp_close_files_and_terminate (MP mp) {
25848   integer k; /* all-purpose index */
25849   integer LH; /* the length of the \.{TFM} header, in words */
25850   int lk_offset; /* extra words inserted at beginning of |lig_kern| array */
25851   pointer p; /* runs through a list of \.{TFM} dimensions */
25852   @<Close all open files in the |rd_file| and |wr_file| arrays@>;
25853   if ( mp->internal[mp_tracing_stats]>0 )
25854     @<Output statistics about this job@>;
25855   wake_up_terminal; 
25856   @<Do all the finishing work on the \.{TFM} file@>;
25857   @<Explain what output files were written@>;
25858   if ( mp->log_opened ){ 
25859     wlog_cr;
25860     (mp->close_file)(mp,mp->log_file); 
25861     mp->selector=mp->selector-2;
25862     if ( mp->selector==term_only ) {
25863       mp_print_nl(mp, "Transcript written on ");
25864 @.Transcript written...@>
25865       mp_print(mp, mp->log_name); mp_print_char(mp, '.');
25866     }
25867   }
25868   mp_print_ln(mp);
25869   t_close_out;
25870   t_close_in;
25871 }
25872
25873 @ @<Declarations@>=
25874 void mp_close_files_and_terminate (MP mp) ;
25875
25876 @ @<Close all open files in the |rd_file| and |wr_file| arrays@>=
25877 if (mp->rd_fname!=NULL) {
25878   for (k=0;k<=(int)mp->read_files-1;k++ ) {
25879     if ( mp->rd_fname[k]!=NULL ) {
25880       (mp->close_file)(mp,mp->rd_file[k]);
25881       xfree(mp->rd_fname[k]);      
25882    }
25883  }
25884 }
25885 if (mp->wr_fname!=NULL) {
25886   for (k=0;k<=(int)mp->write_files-1;k++) {
25887     if ( mp->wr_fname[k]!=NULL ) {
25888      (mp->close_file)(mp,mp->wr_file[k]);
25889       xfree(mp->wr_fname[k]); 
25890     }
25891   }
25892 }
25893
25894 @ @<Dealloc ...@>=
25895 for (k=0;k<(int)mp->max_read_files;k++ ) {
25896   if ( mp->rd_fname[k]!=NULL ) {
25897     (mp->close_file)(mp,mp->rd_file[k]);
25898     xfree(mp->rd_fname[k]); 
25899   }
25900 }
25901 xfree(mp->rd_file);
25902 xfree(mp->rd_fname);
25903 for (k=0;k<(int)mp->max_write_files;k++) {
25904   if ( mp->wr_fname[k]!=NULL ) {
25905     (mp->close_file)(mp,mp->wr_file[k]);
25906     xfree(mp->wr_fname[k]); 
25907   }
25908 }
25909 xfree(mp->wr_file);
25910 xfree(mp->wr_fname);
25911
25912
25913 @ We want to produce a \.{TFM} file if and only if |mp_fontmaking| is positive.
25914
25915 We reclaim all of the variable-size memory at this point, so that
25916 there is no chance of another memory overflow after the memory capacity
25917 has already been exceeded.
25918
25919 @<Do all the finishing work on the \.{TFM} file@>=
25920 if ( mp->internal[mp_fontmaking]>0 ) {
25921   @<Make the dynamic memory into one big available node@>;
25922   @<Massage the \.{TFM} widths@>;
25923   mp_fix_design_size(mp); mp_fix_check_sum(mp);
25924   @<Massage the \.{TFM} heights, depths, and italic corrections@>;
25925   mp->internal[mp_fontmaking]=0; /* avoid loop in case of fatal error */
25926   @<Finish the \.{TFM} file@>;
25927 }
25928
25929 @ @<Make the dynamic memory into one big available node@>=
25930 mp->rover=lo_mem_stat_max+1; link(mp->rover)=empty_flag; mp->lo_mem_max=mp->hi_mem_min-1;
25931 if ( mp->lo_mem_max-mp->rover>max_halfword ) mp->lo_mem_max=max_halfword+mp->rover;
25932 node_size(mp->rover)=mp->lo_mem_max-mp->rover; 
25933 llink(mp->rover)=mp->rover; rlink(mp->rover)=mp->rover;
25934 link(mp->lo_mem_max)=null; info(mp->lo_mem_max)=null
25935
25936 @ The present section goes directly to the log file instead of using
25937 |print| commands, because there's no need for these strings to take
25938 up |str_pool| memory when a non-{\bf stat} version of \MP\ is being used.
25939
25940 @<Output statistics...@>=
25941 if ( mp->log_opened ) { 
25942   char s[128];
25943   wlog_ln(" ");
25944   wlog_ln("Here is how much of MetaPost's memory you used:");
25945 @.Here is how much...@>
25946   snprintf(s,128," %i string%s out of %i",(int)mp->max_strs_used-mp->init_str_use,
25947           (mp->max_strs_used!=mp->init_str_use+1 ? "s" : ""),
25948           (int)(mp->max_strings-1-mp->init_str_use));
25949   wlog_ln(s);
25950   snprintf(s,128," %i string characters out of %i",
25951            (int)mp->max_pl_used-mp->init_pool_ptr,
25952            (int)mp->pool_size-mp->init_pool_ptr);
25953   wlog_ln(s);
25954   snprintf(s,128," %i words of memory out of %i",
25955            (int)mp->lo_mem_max+mp->mem_end-mp->hi_mem_min+2,
25956            (int)mp->mem_end);
25957   wlog_ln(s);
25958   snprintf(s,128," %i symbolic tokens out of %i", (int)mp->st_count, (int)mp->hash_size);
25959   wlog_ln(s);
25960   snprintf(s,128," %ii,%in,%ip,%ib stack positions out of %ii,%in,%ip,%ib",
25961            (int)mp->max_in_stack,(int)mp->int_ptr,
25962            (int)mp->max_param_stack,(int)mp->max_buf_stack+1,
25963            (int)mp->stack_size,(int)mp->max_internal,(int)mp->param_size,(int)mp->buf_size);
25964   wlog_ln(s);
25965   snprintf(s,128," %i string compactions (moved %i characters, %i strings)",
25966           (int)mp->pact_count,(int)mp->pact_chars,(int)mp->pact_strs);
25967   wlog_ln(s);
25968 }
25969
25970 @ It is nice to have have some of the stats available from the API.
25971
25972 @<Exported function ...@>=
25973 int mp_memory_usage (MP mp );
25974 int mp_hash_usage (MP mp );
25975 int mp_param_usage (MP mp );
25976 int mp_open_usage (MP mp );
25977
25978 @ @c
25979 int mp_memory_usage (MP mp ) {
25980         return (int)mp->lo_mem_max+mp->mem_end-mp->hi_mem_min+2;
25981 }
25982 int mp_hash_usage (MP mp ) {
25983   return (int)mp->st_count;
25984 }
25985 int mp_param_usage (MP mp ) {
25986         return (int)mp->max_param_stack;
25987 }
25988 int mp_open_usage (MP mp ) {
25989         return (int)mp->max_in_stack;
25990 }
25991
25992 @ We get to the |final_cleanup| routine when \&{end} or \&{dump} has
25993 been scanned.
25994
25995 @<Last-minute...@>=
25996 void mp_final_cleanup (MP mp) {
25997   small_number c; /* 0 for \&{end}, 1 for \&{dump} */
25998   c=mp->cur_mod;
25999   if ( mp->job_name==NULL ) mp_open_log_file(mp);
26000   while ( mp->input_ptr>0 ) {
26001     if ( token_state ) mp_end_token_list(mp);
26002     else  mp_end_file_reading(mp);
26003   }
26004   while ( mp->loop_ptr!=null ) mp_stop_iteration(mp);
26005   while ( mp->open_parens>0 ) { 
26006     mp_print(mp, " )"); decr(mp->open_parens);
26007   };
26008   while ( mp->cond_ptr!=null ) {
26009     mp_print_nl(mp, "(end occurred when ");
26010 @.end occurred...@>
26011     mp_print_cmd_mod(mp, fi_or_else,mp->cur_if);
26012     /* `\.{if}' or `\.{elseif}' or `\.{else}' */
26013     if ( mp->if_line!=0 ) {
26014       mp_print(mp, " on line "); mp_print_int(mp, mp->if_line);
26015     }
26016     mp_print(mp, " was incomplete)");
26017     mp->if_line=if_line_field(mp->cond_ptr);
26018     mp->cur_if=name_type(mp->cond_ptr); mp->cond_ptr=link(mp->cond_ptr);
26019   }
26020   if ( mp->history!=mp_spotless )
26021     if ( ((mp->history==mp_warning_issued)||(mp->interaction<mp_error_stop_mode)) )
26022       if ( mp->selector==term_and_log ) {
26023     mp->selector=term_only;
26024     mp_print_nl(mp, "(see the transcript file for additional information)");
26025 @.see the transcript file...@>
26026     mp->selector=term_and_log;
26027   }
26028   if ( c==1 ) {
26029     if (mp->ini_version) {
26030       mp_store_mem_file(mp); return;
26031     }
26032     mp_print_nl(mp, "(dump is performed only by INIMP)"); return;
26033 @.dump...only by INIMP@>
26034   }
26035 }
26036
26037 @ @<Declarations@>=
26038 void mp_final_cleanup (MP mp) ;
26039 void mp_init_prim (MP mp) ;
26040 void mp_init_tab (MP mp) ;
26041
26042 @ @<Last-minute...@>=
26043 void mp_init_prim (MP mp) { /* initialize all the primitives */
26044   @<Put each...@>;
26045 }
26046 @#
26047 void mp_init_tab (MP mp) { /* initialize other tables */
26048   integer k; /* all-purpose index */
26049   @<Initialize table entries (done by \.{INIMP} only)@>;
26050 }
26051
26052
26053 @ When we begin the following code, \MP's tables may still contain garbage;
26054 the strings might not even be present. Thus we must proceed cautiously to get
26055 bootstrapped in.
26056
26057 But when we finish this part of the program, \MP\ is ready to call on the
26058 |main_control| routine to do its work.
26059
26060 @<Get the first line...@>=
26061
26062   @<Initialize the input routines@>;
26063   if ( (mp->mem_ident==NULL)||(mp->buffer[loc]=='&') ) {
26064     if ( mp->mem_ident!=NULL ) {
26065       mp_do_initialize(mp); /* erase preloaded mem */
26066     }
26067     if ( ! mp_open_mem_file(mp) ) return mp_fatal_error_stop;
26068     if ( ! mp_load_mem_file(mp) ) {
26069       (mp->close_file)(mp, mp->mem_file); 
26070       return mp_fatal_error_stop;
26071     }
26072     (mp->close_file)(mp, mp->mem_file);
26073     while ( (loc<limit)&&(mp->buffer[loc]==' ') ) incr(loc);
26074   }
26075   mp->buffer[limit]='%';
26076   mp_fix_date_and_time(mp);
26077   if (mp->random_seed==0)
26078     mp->random_seed = (mp->internal[mp_time] / unity)+mp->internal[mp_day];
26079   mp_init_randoms(mp, mp->random_seed);
26080   @<Initialize the print |selector|...@>;
26081   if ( loc<limit ) if ( mp->buffer[loc]!='\\' ) 
26082     mp_start_input(mp); /* \&{input} assumed */
26083 }
26084
26085 @ @<Run inimpost commands@>=
26086 {
26087   mp_get_strings_started(mp);
26088   mp_init_tab(mp); /* initialize the tables */
26089   mp_init_prim(mp); /* call |primitive| for each primitive */
26090   mp->init_str_use=mp->str_ptr; mp->init_pool_ptr=mp->pool_ptr;
26091   mp->max_str_ptr=mp->str_ptr; mp->max_pool_ptr=mp->pool_ptr;
26092   mp_fix_date_and_time(mp);
26093 }
26094
26095
26096 @* \[47] Debugging.
26097 Once \MP\ is working, you should be able to diagnose most errors with
26098 the \.{show} commands and other diagnostic features. But for the initial
26099 stages of debugging, and for the revelation of really deep mysteries, you
26100 can compile \MP\ with a few more aids. An additional routine called |debug_help|
26101 will also come into play when you type `\.D' after an error message;
26102 |debug_help| also occurs just before a fatal error causes \MP\ to succumb.
26103 @^debugging@>
26104 @^system dependencies@>
26105
26106 The interface to |debug_help| is primitive, but it is good enough when used
26107 with a debugger that allows you to set breakpoints and to read
26108 variables and change their values. After getting the prompt `\.{debug \#}', you
26109 type either a negative number (this exits |debug_help|), or zero (this
26110 goes to a location where you can set a breakpoint, thereby entering into
26111 dialog with the debugger), or a positive number |m| followed by
26112 an argument |n|. The meaning of |m| and |n| will be clear from the
26113 program below. (If |m=13|, there is an additional argument, |l|.)
26114 @.debug \#@>
26115
26116 @<Last-minute...@>=
26117 void mp_debug_help (MP mp) { /* routine to display various things */
26118   integer k;
26119   int l,m,n;
26120   char *aline;
26121   size_t len;
26122   while (1) { 
26123     wake_up_terminal;
26124     mp_print_nl(mp, "debug # (-1 to exit):"); update_terminal;
26125 @.debug \#@>
26126     m = 0;
26127     aline = (mp->read_ascii_file)(mp,mp->term_in, &len);
26128     if (len) { sscanf(aline,"%i",&m); xfree(aline); }
26129     if ( m<=0 )
26130       return;
26131     n = 0 ;
26132     aline = (mp->read_ascii_file)(mp,mp->term_in, &len);
26133     if (len) { sscanf(aline,"%i",&n); xfree(aline); }
26134     switch (m) {
26135     @<Numbered cases for |debug_help|@>;
26136     default: mp_print(mp, "?"); break;
26137     }
26138   }
26139 }
26140
26141 @ @<Numbered cases...@>=
26142 case 1: mp_print_word(mp, mp->mem[n]); /* display |mem[n]| in all forms */
26143   break;
26144 case 2: mp_print_int(mp, info(n));
26145   break;
26146 case 3: mp_print_int(mp, link(n));
26147   break;
26148 case 4: mp_print_int(mp, eq_type(n)); mp_print_char(mp, ':'); mp_print_int(mp, equiv(n));
26149   break;
26150 case 5: mp_print_variable_name(mp, n);
26151   break;
26152 case 6: mp_print_int(mp, mp->internal[n]);
26153   break;
26154 case 7: mp_do_show_dependencies(mp);
26155   break;
26156 case 9: mp_show_token_list(mp, n,null,100000,0);
26157   break;
26158 case 10: mp_print_str(mp, n);
26159   break;
26160 case 11: mp_check_mem(mp, n>0); /* check wellformedness; print new busy locations if |n>0| */
26161   break;
26162 case 12: mp_search_mem(mp, n); /* look for pointers to |n| */
26163   break;
26164 case 13: 
26165   l = 0;  
26166   aline = (mp->read_ascii_file)(mp,mp->term_in, &len);
26167   if (len) { sscanf(aline,"%i",&l); xfree(aline); }
26168   mp_print_cmd_mod(mp, n,l); 
26169   break;
26170 case 14: for (k=0;k<=n;k++) mp_print_str(mp, mp->buffer[k]);
26171   break;
26172 case 15: mp->panicking=! mp->panicking;
26173   break;
26174
26175
26176 @ Saving the filename template
26177
26178 @<Save the filename template@>=
26179
26180   if ( mp->filename_template!=0 ) delete_str_ref(mp->filename_template);
26181   if ( length(mp->cur_exp)==0 ) mp->filename_template=0;
26182   else { 
26183     mp->filename_template=mp->cur_exp; add_str_ref(mp->filename_template);
26184   }
26185 }
26186
26187 @* \[48] System-dependent changes.
26188 This section should be replaced, if necessary, by any special
26189 modification of the program
26190 that are necessary to make \MP\ work at a particular installation.
26191 It is usually best to design your change file so that all changes to
26192 previous sections preserve the section numbering; then everybody's version
26193 will be consistent with the published program. More extensive changes,
26194 which introduce new sections, can be inserted here; then only the index
26195 itself will get a new section number.
26196 @^system dependencies@>
26197
26198 @* \[49] Index.
26199 Here is where you can find all uses of each identifier in the program,
26200 with underlined entries pointing to where the identifier was defined.
26201 If the identifier is only one letter long, however, you get to see only
26202 the underlined entries. {\sl All references are to section numbers instead of
26203 page numbers.}
26204
26205 This index also lists error messages and other aspects of the program
26206 that you might want to look up some day. For example, the entry
26207 for ``system dependencies'' lists all sections that should receive
26208 special attention from people who are installing \MP\ in a new
26209 operating environment. A list of various things that can't happen appears
26210 under ``this can't happen''.
26211 Approximately 25 sections are listed under ``inner loop''; these account
26212 for more than 60\pct! of \MP's running time, exclusive of input and output.