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