home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / util / gnu / groff_src.lha / groff-1.10src / troff / div.cc < prev    next >
C/C++ Source or Header  |  1995-06-22  |  27KB  |  1,129 lines

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