home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / groff / troff / div.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-30  |  25.7 KB  |  1,106 lines

  1. // -*- C++ -*-
  2. /* Copyright (C) 1989, 1990 Free Software Foundation, Inc.
  3.      Written by James Clark (jjc@jclark.uucp)
  4.  
  5. This file is part of groff.
  6.  
  7. groff is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 1, or (at your option) any later
  10. version.
  11.  
  12. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with groff; see the file LICENSE.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21.  
  22. // diversions
  23.  
  24. #include "groff.h"
  25. #include "symbol.h"
  26. #include "dictionary.h"
  27. #include "hvunits.h"
  28. #include "env.h"
  29. #include "request.h"
  30. #include "node.h"
  31. #include "token.h"
  32. #include "div.h"
  33. #include "reg.h"
  34.  
  35. static int last_post_line_extra_space = 0; // needed for \n(.a
  36. static int nl_reg_contents = -1;
  37. static int dl_reg_contents = 0;
  38. static int dn_reg_contents = 0;
  39. static int vertical_position_traps_flag = 1;
  40. static vunits truncated_space;
  41. static vunits needed_space;
  42.  
  43. diversion::diversion(symbol s) 
  44. : nm(s), prev(0), vertical_position(V0), marked_place(V0), high_water_mark(V0)
  45. {
  46. }
  47.  
  48. struct vertical_size {
  49.   vunits pre_extra, post_extra, pre, post;
  50.   vertical_size(vunits vs, int ls);
  51. };
  52.  
  53. vertical_size::vertical_size(vunits vs, int ls)
  54. : pre_extra(V0), post_extra(V0), pre(vs)
  55. {
  56.   if (ls > 1)
  57.     post = vs*(ls - 1);
  58.   else
  59.     post = V0;
  60. }
  61.  
  62. void node::set_vertical_size(vertical_size *v)
  63. {
  64. }
  65.  
  66. void extra_size_node::set_vertical_size(vertical_size *v)
  67. {
  68.   if (n < V0) {
  69.     if (-n > v->pre_extra)
  70.       v->pre_extra = -n;
  71.   }
  72.   else if (n > v->post_extra)
  73.     v->post_extra = n;
  74. }
  75.  
  76. void vertical_size_node::set_vertical_size(vertical_size *v)
  77. {
  78.   if (n < V0)
  79.     v->pre = -n;
  80.   else
  81.     v->post = n;
  82. }
  83.  
  84. top_level_diversion *topdiv;
  85.  
  86. diversion *curdiv;
  87.  
  88. void do_divert(int append)
  89. {
  90.   tok.skip();
  91.   symbol nm = get_name();
  92.   if (nm.is_null()) {
  93.     if (curdiv->prev) {
  94.       diversion *temp = curdiv;
  95.       curdiv = curdiv->prev;
  96.       delete temp;
  97.     }
  98.     else
  99.       warning(WARN_DI, "diversion stack underflow");
  100.   }
  101.   else {
  102.     macro_diversion *md = new macro_diversion(nm, append);
  103.     md->prev = curdiv;
  104.     curdiv = md;
  105.   }
  106.   skip_line();
  107. }
  108.  
  109. void divert()
  110. {
  111.   do_divert(0);
  112. }
  113.  
  114. void divert_append()
  115. {
  116.   do_divert(1);
  117. }
  118.   
  119. void diversion::need(vunits n)
  120. {
  121.   vunits d = distance_to_next_trap();
  122.   if (d < n) {
  123.     space(d, 1);
  124.     truncated_space = -d;
  125.     needed_space = n;
  126.   }
  127. }
  128.  
  129. macro_diversion::macro_diversion(symbol s, int append)
  130. : diversion(s), max_width(H0)
  131. {
  132. #if 0
  133.   if (append) {
  134.     /* We don't allow recursive appends eg:
  135.  
  136.       .da a
  137.       .a
  138.       .di
  139.       
  140.       This causes an infinite loop in troff anyway.
  141.       This is because the user could do
  142.  
  143.       .as a foo
  144.  
  145.       in the diversion, and this would mess things up royally,
  146.       since there would be two things appending to the same
  147.       macro_header.
  148.       To make it work, we would have to copy the _contents_
  149.       of the macro into which we were diverting; this doesn't
  150.       strike me as worthwhile.
  151.       However,
  152.  
  153.       .di a
  154.       .a
  155.       .a
  156.       .di
  157.  
  158.        will work and will make `a' contain two copies of what it contained
  159.        before; in troff, `a' would contain nothing. */
  160.     request_or_macro *rm 
  161.       = (request_or_macro *)request_dictionary.remove(s);
  162.     if (!rm || (mac = rm->to_macro()) == 0)
  163.       mac = new macro;
  164.   }
  165.   else
  166.     mac = new macro;
  167. #endif
  168.   // We can now catch the situation described above by comparing
  169.   // the length of the charlist in the macro_header with the length
  170.   // stored in the macro. When we detect this, we copy the contents.
  171.   mac = new macro;
  172.   if (append) {
  173.     request_or_macro *rm 
  174.       = (request_or_macro *)request_dictionary.lookup(s);
  175.     if (rm) {
  176.       macro *m = rm->to_macro();
  177.       if (m)
  178.     *mac = *m;
  179.     }
  180.   }
  181. }
  182.  
  183. macro_diversion::~macro_diversion()
  184. {
  185.   request_or_macro *rm = (request_or_macro *)request_dictionary.lookup(nm);
  186.   macro *m = rm ? rm->to_macro() : 0;
  187.   if (m) {
  188.     *m = *mac;
  189.     delete mac;
  190.   }
  191.   else
  192.     request_dictionary.define(nm, mac);
  193.   mac = 0;
  194.   dl_reg_contents = max_width.to_units();
  195.   dn_reg_contents = vertical_position.to_units();
  196. }
  197.  
  198. vunits macro_diversion::distance_to_next_trap()
  199. {
  200.   if (!diversion_trap.is_null() && diversion_trap_pos > vertical_position)
  201.     return diversion_trap_pos - vertical_position;
  202.   else
  203.     // Substract vresolution so that vunits::vunits does not overflow.
  204.     return vunits(INT_MAX - vresolution);
  205. }
  206.  
  207. void macro_diversion::transparent_output(unsigned char c)
  208. {
  209.   mac->append(c);
  210. }
  211.  
  212. void macro_diversion::transparent_output(node *n)
  213. {
  214.   mac->append(n);
  215. }
  216.  
  217. void macro_diversion::output(node *nd, int retain_size,
  218.                  vunits vs, int ls, hunits width)
  219. {
  220.   vertical_size v(vs, ls);
  221.   while (nd != 0) {
  222.     nd->set_vertical_size(&v);
  223.     node *temp = nd;
  224.     nd = nd->next;
  225.     if (temp->interpret(mac)) {
  226.       delete temp;
  227.     }
  228.     else {
  229. #if 1
  230.       temp->freeze_space();
  231. #endif
  232.       mac->append(temp);
  233.     }
  234.   }
  235.   if (!v.post_extra.is_zero())
  236.     last_post_line_extra_space = v.post_extra.to_units();
  237.   if (!retain_size) {
  238.     v.pre = vs;
  239.     v.post = (ls > 1) ? vs*(ls - 1) : V0;
  240.   }
  241.   if (width > max_width)
  242.     max_width = width;
  243.   vunits x = v.pre + v.pre_extra + v.post + v.post_extra;
  244.   if (vertical_position_traps_flag
  245.       && !diversion_trap.is_null() && diversion_trap_pos > vertical_position
  246.       && diversion_trap_pos <= vertical_position + x) {
  247.     vunits trunc = vertical_position + x - diversion_trap_pos;
  248.     if (trunc > v.post)
  249.       trunc = v.post;
  250.     v.post -= trunc;
  251.     x -= trunc;
  252.     truncated_space = trunc;
  253.     spring_trap(diversion_trap);
  254.   }
  255.   mac->append(new vertical_size_node(-v.pre));
  256.   mac->append(new vertical_size_node(v.post));
  257.   mac->append('\n');
  258.   vertical_position += x;
  259.   if (vertical_position > high_water_mark)
  260.     high_water_mark = vertical_position;
  261. }
  262.  
  263. void macro_diversion::space(vunits n, int)
  264. {
  265.   if (vertical_position_traps_flag
  266.       && !diversion_trap.is_null() && diversion_trap_pos > vertical_position
  267.       && diversion_trap_pos <= vertical_position + n) {
  268.     truncated_space = vertical_position + n - diversion_trap_pos;
  269.     n = diversion_trap_pos - vertical_position;
  270.     spring_trap(diversion_trap);
  271.   }
  272.   else if (n + vertical_position < V0)
  273.     n = -vertical_position;
  274.   mac->append(new diverted_space_node(n));
  275.   vertical_position += n;
  276.   if (vertical_position > high_water_mark)
  277.     high_water_mark = vertical_position;
  278. }
  279.  
  280. void macro_diversion::copy_file(const char *filename)
  281. {
  282.   mac->append(new diverted_copy_file_node(filename));
  283. }
  284.  
  285. top_level_diversion::top_level_diversion()
  286. : page_count(0), have_next_page_number(0), page_length(units_per_inch*11), 
  287.   page_offset(units_per_inch), prev_page_offset(units_per_inch), 
  288.   ejecting_page(0), page_trap_list(0), first_page_begun(0), no_space_mode(0),
  289.   page_number(0)
  290. {
  291. }
  292.  
  293. // find the next trap after pos
  294.  
  295. trap *top_level_diversion::find_next_trap(vunits *next_trap_pos)
  296. {
  297.   trap *next_trap = 0;
  298.   for (trap *pt = page_trap_list; pt != 0; pt = pt->next)
  299.     if (!pt->nm.is_null()) {
  300.       if (pt->position >= V0) {
  301.     if (pt->position > vertical_position 
  302.         && pt->position < page_length
  303.         && (next_trap == 0 || pt->position < *next_trap_pos)) {
  304.           next_trap = pt;
  305.           *next_trap_pos = pt->position;
  306.         }
  307.       }
  308.       else {
  309.     vunits pos = pt->position;
  310.     pos += page_length;
  311.     if (pos > 0 && pos > vertical_position && (next_trap == 0 || pos < *next_trap_pos)) {
  312.       next_trap = pt;
  313.       *next_trap_pos = pos;
  314.     }
  315.       }
  316.     }
  317.   return next_trap;
  318. }
  319.  
  320. vunits top_level_diversion::distance_to_next_trap()
  321. {
  322.   vunits d;
  323.   if (!find_next_trap(&d))
  324.     return page_length - vertical_position;
  325.   else
  326.     return d - vertical_position;
  327. }
  328.  
  329. void top_level_diversion::output(node *nd, int retain_size,
  330.                  vunits vs, int ls, hunits /*width*/)
  331. {
  332.   no_space_mode = 0;
  333.   vunits next_trap_pos;
  334.   trap *next_trap = find_next_trap(&next_trap_pos);
  335.   if (!first_page_begun && begin_page()) 
  336.     fatal("sorry, I didn't manage to begin the first page in time: use an explicit .br request");
  337.   vertical_size v(vs, ls);
  338.   for (node *tem = nd; tem != 0; tem = tem->next)
  339.     tem->set_vertical_size(&v);
  340.   if (!v.post_extra.is_zero())
  341.     last_post_line_extra_space = v.post_extra.to_units();
  342.   if (!retain_size) {
  343.     v.pre = vs;
  344.     v.post = (ls > 1) ? vs*(ls - 1) : V0;
  345.   }
  346.   vertical_position += v.pre;
  347.   vertical_position += v.pre_extra;
  348.   the_output->print_line(page_offset, vertical_position, nd,
  349.              v.pre + v.pre_extra, v.post_extra);
  350.   vertical_position += v.post_extra;
  351.   if (vertical_position_traps_flag && vertical_position >= page_length)
  352.     begin_page();
  353.   else if (vertical_position_traps_flag
  354.        && next_trap != 0 && vertical_position >= next_trap_pos) {
  355.     nl_reg_contents = vertical_position.to_units();
  356.     truncated_space = v.post;
  357.     if (vertical_position > high_water_mark)
  358.       high_water_mark = vertical_position;
  359.     spring_trap(next_trap->nm);
  360.   }
  361.   else if (v.post > V0) {
  362.     vertical_position += v.post;
  363.     if (vertical_position_traps_flag
  364.     && next_trap != 0 && vertical_position >= next_trap_pos) {
  365.       truncated_space = vertical_position - next_trap_pos;
  366.       vertical_position = next_trap_pos;
  367.       nl_reg_contents = vertical_position.to_units();
  368.       if (vertical_position > high_water_mark)
  369.     high_water_mark = vertical_position;
  370.       spring_trap(next_trap->nm);
  371.     }
  372.     else if (vertical_position_traps_flag && vertical_position >= page_length)
  373.       begin_page();
  374.     else {
  375.       nl_reg_contents = vertical_position.to_units();
  376.       if (vertical_position > high_water_mark)
  377.     high_water_mark = vertical_position;
  378.     }
  379.   }
  380.   else {
  381.     nl_reg_contents = vertical_position.to_units();
  382.     if (vertical_position > high_water_mark)
  383.       high_water_mark = vertical_position;
  384.   }
  385. }
  386.  
  387. void top_level_diversion::transparent_output(unsigned char c)
  388. {
  389.   if (!first_page_begun && begin_page())
  390.     // This can only happen with the transparent() request.
  391.     fatal("sorry, I didn't manage to begin the first page in time: use an explicit .br request");
  392.   const char *s = asciify(c);
  393.   while (*s)
  394.     the_output->transparent_char(*s++);
  395. }
  396.  
  397. void top_level_diversion::transparent_output(node * /*n*/)
  398. {
  399.   error("can't transparently output node at top level");
  400. }
  401.  
  402. void top_level_diversion::copy_file(const char *filename)
  403. {
  404.   if (!first_page_begun && begin_page())
  405.     fatal("sorry, I didn't manage to begin the first page in time: use an explicit .br request");
  406.   the_output->copy_file(page_offset, vertical_position, filename);
  407. }
  408.  
  409. void top_level_diversion::space(vunits n, int forced)
  410. {
  411.   if (no_space_mode) {
  412.     if (!forced)
  413.       return;
  414.     else
  415.       no_space_mode = 0;
  416.   }
  417.   if (!first_page_begun) {
  418.     if (begin_page()) {
  419.       // This happens if there's a top of page trap, and the first-page
  420.       // transition is caused by `'sp'.
  421.       truncated_space = n > V0 ? n : V0;
  422.       return;
  423.     }
  424.   }
  425.   vunits next_trap_pos;
  426.   trap *next_trap = find_next_trap(&next_trap_pos);
  427.   vunits y = vertical_position + n;
  428.   if (vertical_position_traps_flag && next_trap != 0 && y >= next_trap_pos) {
  429.     vertical_position = next_trap_pos;
  430.     nl_reg_contents = vertical_position.to_units();
  431.     truncated_space = y - vertical_position;
  432.     if (vertical_position > high_water_mark)
  433.       high_water_mark = vertical_position;
  434.     spring_trap(next_trap->nm);
  435.   }
  436.   else if (y < V0) {
  437.     vertical_position = V0;
  438.     nl_reg_contents = vertical_position.to_units();
  439.   }
  440.   else if (vertical_position_traps_flag && y >= page_length && n >= V0)
  441.     begin_page();
  442.   else {
  443.     vertical_position = y;
  444.     nl_reg_contents = vertical_position.to_units();
  445.     if (vertical_position > high_water_mark)
  446.       high_water_mark = vertical_position;
  447.   }
  448. }
  449.  
  450. trap::trap(symbol s, vunits n, trap *p)
  451.      : nm(s), next(p), position(n)
  452. {
  453. }
  454.  
  455. void top_level_diversion::add_trap(symbol nm, vunits pos)
  456. {
  457.   trap *first_free_slot = 0;
  458.   for (trap **p = &page_trap_list; *p; p = &(*p)->next) {
  459.     if ((*p)->position == pos) {
  460.       (*p)->nm = nm;
  461.       return;
  462.     }
  463.     else if ((*p)->nm.is_null() && first_free_slot == 0)
  464.       first_free_slot = *p;
  465.   }
  466.   if (first_free_slot) {
  467.     first_free_slot->nm = nm;
  468.     first_free_slot->position = pos;
  469.   }
  470.   else
  471.     *p = new trap(nm, pos, 0);
  472. }  
  473.  
  474. void top_level_diversion::remove_trap(symbol nm)
  475. {
  476.   for (trap *p = page_trap_list; p; p = p->next)
  477.     if (p->nm == nm) {
  478.       p->nm = NULL_SYMBOL;
  479.       return;
  480.     }
  481. }
  482.  
  483. void top_level_diversion::remove_trap_at(vunits pos)
  484. {
  485.   for (trap *p = page_trap_list; p; p = p->next)
  486.     if (p->position == pos) {
  487.       p->nm = NULL_SYMBOL;
  488.       return;
  489.     }
  490. }
  491.       
  492. void top_level_diversion::change_trap(symbol nm, vunits pos)
  493. {
  494.   for (trap *p = page_trap_list; p; p = p->next)
  495.     if (p->nm == nm) {
  496.       p->position = pos;
  497.       return;
  498.     }
  499. }
  500.  
  501. void top_level_diversion::print_traps()
  502. {
  503.   for (trap *p = page_trap_list; p; p = p->next)
  504.     if (p->nm.is_null())
  505.       fprintf(stderr, "  empty\n");
  506.     else
  507.       fprintf(stderr, "%s\t%d\n", p->nm.contents(), p->position.to_units());
  508.   fflush(stderr);
  509. }
  510.  
  511. void end_diversions()
  512. {
  513.   while (curdiv != topdiv) {
  514.     error("automatically ending diversion `%1' on exit",
  515.         curdiv->nm.contents());
  516.     diversion *tem = curdiv;
  517.     curdiv = curdiv->prev;
  518.     delete tem;
  519.   }
  520. }
  521.  
  522. NO_RETURN void cleanup_and_exit(int exit_code)
  523. {
  524.   if (the_output)
  525.     delete the_output;
  526.   exit(exit_code);
  527. }
  528.  
  529. int exit_flag = 0;
  530.  
  531. // returns non-zero if it sprung a top of page trap
  532.  
  533. int top_level_diversion::begin_page()
  534. {
  535.   if (exit_flag == 2 || (exit_flag == 1 && curenv->is_empty()))
  536.     cleanup_and_exit(0);
  537.   if (!the_output)
  538.     init_output();
  539.   ++page_count;
  540.   if (have_next_page_number) {
  541.     page_number = next_page_number;
  542.     have_next_page_number = 0;
  543.   }
  544.   else
  545.     page_number++;
  546.   // spring the top of page trap if there is one
  547.   vunits next_trap_pos;
  548.   vertical_position = -vresolution;
  549.   trap *next_trap = find_next_trap(&next_trap_pos);
  550.   vertical_position = V0;
  551.   first_page_begun = 1;
  552.   ejecting_page = 0;
  553.   the_output->begin_page(page_number, page_length);
  554.   nl_reg_contents = vertical_position.to_units();
  555.   if (vertical_position_traps_flag && next_trap != 0 && next_trap_pos == V0) {
  556.     truncated_space = V0;
  557.     spring_trap(next_trap->nm);
  558.     return 1;
  559.   }
  560.   else
  561.     return 0;
  562. }
  563.  
  564. void continue_page_eject()
  565. {
  566.   if (topdiv->get_ejecting()) {
  567.     if (curdiv != topdiv)
  568.       error("can't continue page ejection because of current diversion");
  569.     else if (!vertical_position_traps_flag)
  570.       error("can't continue page ejection because vertical position traps disabled");
  571.     else {
  572.       push_page_ejector();
  573.       topdiv->space(topdiv->get_page_length(), 1);
  574.     }
  575.   }
  576. }
  577.  
  578. void top_level_diversion::set_next_page_number(int n)
  579. {
  580.   next_page_number= n;
  581.   have_next_page_number = 1;
  582. }
  583.  
  584. int top_level_diversion::get_next_page_number()
  585. {
  586.   return have_next_page_number ? next_page_number : page_number + 1;
  587. }
  588.  
  589. void top_level_diversion::set_page_length(vunits n)
  590. {
  591.   page_length = n;
  592. }
  593.  
  594. diversion::~diversion()
  595. {
  596. }
  597.  
  598. void page_offset()
  599. {
  600.   hunits n;
  601.   if (!has_arg()) {
  602.     hunits temp = topdiv->page_offset;
  603.     topdiv->page_offset = topdiv->prev_page_offset;
  604.     topdiv->prev_page_offset = temp;
  605.   }
  606.   else if (get_hunits(&n, 'v', topdiv->page_offset)) {
  607.     topdiv->prev_page_offset = topdiv->page_offset;
  608.     topdiv->page_offset = n;
  609.   }
  610.   skip_line();
  611. }
  612.  
  613. void page_length()
  614. {
  615.   vunits n;
  616.   if (!has_arg())
  617.     topdiv->set_page_length(11*units_per_inch);
  618.   else if (get_vunits(&n, 'v', topdiv->get_page_length()))
  619.     topdiv->set_page_length(n);
  620.   skip_line();
  621. }
  622.  
  623. void when_request()
  624. {
  625.   vunits n;
  626.   if (get_vunits(&n, 'v')) {
  627.     symbol s = get_name();
  628.     if (s.is_null())
  629.       topdiv->remove_trap_at(n);
  630.     else
  631.       topdiv->add_trap(s, n);
  632.   }
  633.   skip_line();
  634. }
  635.  
  636. void begin_page()
  637. {
  638.   int got_arg = 0;
  639.   int n;
  640.   if (has_arg() && get_integer(&n, topdiv->get_page_number()))
  641.     got_arg = 1;
  642.   while (!tok.newline() && !tok.eof())
  643.     tok.next();
  644.   if (curdiv == topdiv) {
  645.     if (!topdiv->first_page_begun) {
  646.       if (topdiv->no_space_mode && !got_arg) {
  647.     topdiv->begin_page();
  648.       }
  649.       else {
  650.     /* Given this
  651.  
  652.          .wh 0 x
  653.      .de x
  654.      .tm \\n%
  655.      ..
  656.      .bp 3
  657.  
  658.      troff prints
  659.  
  660.      1
  661.      3
  662.  
  663.      This code makes groff do the same. */
  664.  
  665.     push_page_ejector();
  666.     topdiv->begin_page();
  667.     if (got_arg)
  668.       topdiv->set_next_page_number(n);
  669.     topdiv->set_ejecting();
  670.       }
  671.     }
  672.     else {
  673.       push_page_ejector();
  674.       if (break_flag)
  675.     curenv->do_break();
  676.       if (got_arg)
  677.     topdiv->set_next_page_number(n);
  678.       if (!(topdiv->no_space_mode && !got_arg))
  679.     topdiv->set_ejecting();
  680.     }
  681.   }
  682.   tok.next();
  683. }
  684.  
  685. void no_space()
  686. {
  687.   if (curdiv == topdiv)
  688.     topdiv->no_space_mode = 1;
  689.   skip_line();
  690. }
  691.  
  692. void restore_spacing()
  693. {
  694.   if (curdiv == topdiv)
  695.     topdiv->no_space_mode = 0;
  696.   skip_line();
  697. }
  698.  
  699. /* It is necessary to generate a break before before reading the argument,
  700. because otherwise arguments using | will be wrong. But if we just
  701. generate a break as usual, then the line forced out may spring a trap
  702. and thus push a macro onto the input stack before we have had a chance
  703. to read the argument to the sp request. We resolve this dilemma by
  704. setting, before generating the break, a flag which will postpone the
  705. actual pushing of the macro associated with the trap sprung by the
  706. outputting of the line forced out by the break till after we have read
  707. the argument to the request.  If the break did cause a trap to be
  708. sprung, then we don't actually do the space. */
  709.  
  710. void space_request()
  711. {
  712.   postpone_traps();
  713.   if (break_flag)
  714.     curenv->do_break();
  715.   int err = 0;
  716.   vunits n;
  717.   if (!has_arg())
  718.     n = curenv->get_vertical_spacing();
  719.   else if (!get_vunits(&n, 'v'))
  720.     err = 1;
  721.   while (!tok.newline() && !tok.eof())
  722.     tok.next();
  723.   if (!unpostpone_traps()) {
  724.     if (!err)
  725.       curdiv->space(n);
  726.   }
  727.   else {
  728.     // The line might have had line spacing that was truncated.
  729.     if (!err)
  730.       truncated_space += n;
  731.   }
  732.   tok.next();
  733. }
  734.  
  735. void blank_line()
  736. {
  737.   curenv->do_break();
  738.   if (!trap_sprung_flag)
  739.     curdiv->space(curenv->get_vertical_spacing());
  740.   else
  741.     truncated_space += curenv->get_vertical_spacing();
  742. }
  743.  
  744. /* need_space might spring a trap and so we must be careful that the
  745. BEGIN_TRAP token is not skipped over. */
  746.  
  747. void need_space()
  748. {
  749.   int err = 0;
  750.   vunits n;
  751.   if (!has_arg())
  752.     n = curenv->get_vertical_spacing();
  753.   else if (!get_vunits(&n, 'v'))
  754.     err = 1;
  755.   while (!tok.newline() && !tok.eof())
  756.     tok.next();
  757.   if (!err)
  758.     curdiv->need(n);
  759.   tok.next();
  760. }
  761.  
  762. void page_number()
  763. {
  764.   int n;
  765.   if (has_arg() && get_integer(&n, topdiv->get_page_number()))
  766.     topdiv->set_next_page_number(n);
  767.   skip_line();
  768. }
  769.  
  770. vunits saved_space;
  771.  
  772. void save_vertical_space()
  773. {
  774.   vunits x;
  775.   if (get_vunits(&x, 'v')) {
  776.     if (curdiv->distance_to_next_trap() > x)
  777.       curdiv->space(x, 1);
  778.     else
  779.       saved_space = x;
  780.   }
  781.   skip_line();
  782. }
  783.  
  784. void output_saved_vertical_space()
  785. {
  786.   while (!tok.newline() && !tok.eof())
  787.     tok.next();
  788.   if (saved_space > V0)
  789.     curdiv->space(saved_space, 1);
  790.   saved_space = V0;
  791.   tok.next();
  792. }
  793.  
  794. void flush_output()
  795. {
  796.   while (!tok.newline() && !tok.eof())
  797.     tok.next();
  798.   if (break_flag)
  799.     curenv->do_break();
  800.   if (the_output)
  801.     the_output->flush();
  802.   tok.next();
  803. }
  804.  
  805. void macro_diversion::set_diversion_trap(symbol s, vunits n)
  806. {
  807.   diversion_trap = s;
  808.   diversion_trap_pos = n;
  809. }
  810.  
  811. void macro_diversion::clear_diversion_trap()
  812. {
  813.   diversion_trap = NULL_SYMBOL;
  814. }
  815.  
  816. void top_level_diversion::set_diversion_trap(symbol, vunits)
  817. {
  818.   error("can't set diversion trap when no current diversion");
  819. }
  820.  
  821. void top_level_diversion::clear_diversion_trap()
  822. {
  823.   error("can't set diversion trap when no current diversion");
  824. }
  825.  
  826. void diversion_trap()
  827. {
  828.   vunits n;
  829.   if (has_arg() && get_vunits(&n, 'v')) {
  830.     symbol s = get_name();
  831.     if (!s.is_null())
  832.       curdiv->set_diversion_trap(s, n);
  833.     else
  834.       curdiv->clear_diversion_trap();
  835.   }
  836.   else
  837.     curdiv->clear_diversion_trap();
  838.   skip_line();
  839. }
  840.  
  841. void change_trap()
  842. {
  843.   symbol s = get_name(1);
  844.   if (!s.is_null()) {
  845.     vunits x;
  846.     if (has_arg() && get_vunits(&x, 'v'))
  847.       topdiv->change_trap(s, x);
  848.     else
  849.       topdiv->remove_trap(s);
  850.   }
  851.   skip_line();
  852. }
  853.  
  854. void print_traps()
  855. {
  856.   topdiv->print_traps();
  857.   skip_line();
  858. }
  859.  
  860. void mark()
  861. {
  862.   symbol s = get_name();
  863.   if (s.is_null())
  864.     curdiv->marked_place = curdiv->get_vertical_position();
  865.   else
  866.     set_number_reg(s, curdiv->get_vertical_position().to_units());
  867.   skip_line();
  868. }
  869.  
  870. void return_request()
  871. {
  872.   vunits dist = V0;
  873.   // This is truly bizarre.  It is documented in the SQ manual.
  874.   if (has_arg()) {
  875.     if (tok.ch() == '-') {
  876.       tok.next();
  877.       vunits x;
  878.       if (get_vunits(&x, 'v'))
  879.     dist = -x;
  880.     }
  881.     else {
  882.       vunits x;
  883.       if (get_vunits(&x, 'v') && x >= V0)
  884.     dist = x - curdiv->get_vertical_position();
  885.     }
  886.   }
  887.   else
  888.     dist = curdiv->marked_place - curdiv->get_vertical_position();
  889.   if (dist < V0)
  890.     curdiv->space(dist);
  891.   skip_line();
  892. }
  893.  
  894. void vertical_position_traps()
  895. {
  896.   int n;
  897.   if (!has_arg())
  898.     vertical_position_traps_flag = 1;
  899.   else if (get_integer(&n))
  900.     vertical_position_traps_flag = (n != 0);
  901.   skip_line();
  902. }
  903.  
  904. class page_offset_reg : public reg {
  905. public:
  906.   int get_value(units *);
  907.   const char *get_string();
  908. };
  909.   
  910. int page_offset_reg::get_value(units *res)
  911. {
  912.   *res = topdiv->get_page_offset().to_units();
  913.   return 1;
  914. }
  915.  
  916. const char *page_offset_reg::get_string()
  917. {
  918.   return itoa(topdiv->get_page_offset().to_units());
  919. }
  920.  
  921. class page_length_reg : public reg {
  922. public:
  923.   int get_value(units *);
  924.   const char *get_string();
  925. };
  926.   
  927. int page_length_reg::get_value(units *res)
  928. {
  929.   *res = topdiv->get_page_length().to_units();
  930.   return 1;
  931. }
  932.  
  933. const char *page_length_reg::get_string()
  934. {
  935.   return itoa(topdiv->get_page_length().to_units());
  936. }
  937.  
  938. class vertical_position_reg : public reg {
  939. public:
  940.   int get_value(units *);
  941.   const char *get_string();
  942. };
  943.   
  944. int vertical_position_reg::get_value(units *res)
  945. {
  946.   if (curdiv == topdiv && !topdiv->first_page_begun)
  947.     *res = -1;
  948.   else
  949.     *res = curdiv->get_vertical_position().to_units();
  950.   return 1;
  951. }
  952.  
  953. const char *vertical_position_reg::get_string()
  954. {
  955.   if (curdiv == topdiv && !topdiv->first_page_begun)
  956.     return "-1";
  957.   else
  958.     return itoa(curdiv->get_vertical_position().to_units());
  959. }
  960.  
  961. class high_water_mark_reg : public reg {
  962. public:
  963.   int get_value(units *);
  964.   const char *get_string();
  965. };
  966.   
  967. int high_water_mark_reg::get_value(units *res)
  968. {
  969.   *res = curdiv->get_high_water_mark().to_units();
  970.   return 1;
  971. }
  972.  
  973. const char *high_water_mark_reg::get_string()
  974. {
  975.   return itoa(curdiv->get_high_water_mark().to_units());
  976. }
  977.  
  978. class distance_to_next_trap_reg : public reg {
  979. public:
  980.   int get_value(units *);
  981.   const char *get_string();
  982. };
  983.   
  984. int distance_to_next_trap_reg::get_value(units *res)
  985. {
  986.   *res = curdiv->distance_to_next_trap().to_units();
  987.   return 1;
  988. }
  989.  
  990. const char *distance_to_next_trap_reg::get_string()
  991. {
  992.   return itoa(curdiv->distance_to_next_trap().to_units());
  993. }
  994.  
  995. class diversion_name_reg : public reg {
  996. public:
  997.   const char *get_string();
  998. };
  999.  
  1000. const char *diversion_name_reg::get_string()
  1001. {
  1002.   return curdiv->get_diversion_name();
  1003. }
  1004.  
  1005. class page_number_reg : public general_reg {
  1006. public:
  1007.   page_number_reg();
  1008.   int get_value(units *);
  1009.   void set_value(units);
  1010. };
  1011.  
  1012. page_number_reg::page_number_reg()
  1013. {
  1014. }
  1015.  
  1016. void page_number_reg::set_value(units n)
  1017. {
  1018.   topdiv->set_page_number(n);
  1019. }
  1020.  
  1021. int page_number_reg::get_value(units *res)
  1022. {
  1023.   *res = topdiv->get_page_number();
  1024.   return 1;
  1025. }
  1026.  
  1027. class next_page_number_reg : public reg {
  1028. public:
  1029.   const char *get_string();
  1030. };
  1031.  
  1032. const char *next_page_number_reg::get_string()
  1033. {
  1034.   return itoa(topdiv->get_next_page_number());
  1035. }
  1036.  
  1037. class page_ejecting_reg : public reg {
  1038. public:
  1039.   const char *get_string();
  1040. };
  1041.  
  1042. const char *page_ejecting_reg::get_string()
  1043. {
  1044.   return itoa(topdiv->get_ejecting());
  1045. }
  1046.  
  1047. class constant_vunits_reg : public reg {
  1048.   vunits *p;
  1049. public:
  1050.   constant_vunits_reg(vunits *);
  1051.   const char *get_string();
  1052. };
  1053.  
  1054. constant_vunits_reg::constant_vunits_reg(vunits *q) : p(q)
  1055. {
  1056. }
  1057.  
  1058. const char *constant_vunits_reg::get_string()
  1059. {
  1060.   return itoa(p->to_units());
  1061. }
  1062.  
  1063. void init_div_requests()
  1064. {
  1065.   init_request("wh", when_request);
  1066.   init_request("ch", change_trap);
  1067.   init_request("pl", page_length);
  1068.   init_request("po", page_offset);
  1069.   init_request("rs", restore_spacing);
  1070.   init_request("ns", no_space);
  1071.   init_request("sp", space_request);
  1072.   init_request("di", divert);
  1073.   init_request("da", divert_append);
  1074.   init_request("bp", begin_page);
  1075.   init_request("ne", need_space);
  1076.   init_request("pn", page_number);
  1077.   init_request("dt", diversion_trap);
  1078.   init_request("rt", return_request);
  1079.   init_request("mk", mark);
  1080.   init_request("sv", save_vertical_space);
  1081.   init_request("os", output_saved_vertical_space);
  1082.   init_request("fl", flush_output);
  1083.   init_request("vpt", vertical_position_traps);
  1084.   init_request("ptr", print_traps);
  1085.   number_reg_dictionary.define(".a",
  1086.                new constant_int_reg(&last_post_line_extra_space));
  1087.   number_reg_dictionary.define(".z", new diversion_name_reg);
  1088.   number_reg_dictionary.define(".o", new page_offset_reg);
  1089.   number_reg_dictionary.define(".p", new page_length_reg);
  1090.   number_reg_dictionary.define(".d", new vertical_position_reg);
  1091.   number_reg_dictionary.define(".h", new high_water_mark_reg);
  1092.   number_reg_dictionary.define(".t", new distance_to_next_trap_reg);
  1093.   number_reg_dictionary.define("dl", new variable_reg(&dl_reg_contents));
  1094.   number_reg_dictionary.define("dn", new variable_reg(&dn_reg_contents));
  1095.   number_reg_dictionary.define("nl", new variable_reg(&nl_reg_contents));
  1096.   number_reg_dictionary.define(".vpt", 
  1097.                new constant_int_reg(&vertical_position_traps_flag));
  1098.   number_reg_dictionary.define("%", new page_number_reg);
  1099.   number_reg_dictionary.define(".pn", new next_page_number_reg);
  1100.   number_reg_dictionary.define(".trunc",
  1101.                    new constant_vunits_reg(&truncated_space));
  1102.   number_reg_dictionary.define(".ne",
  1103.                    new constant_vunits_reg(&needed_space));
  1104.   number_reg_dictionary.define(".pe", new page_ejecting_reg);
  1105. }
  1106.