/contrib/groff/src/roff/troff/div.cpp

https://bitbucket.org/freebsd/freebsd-head/ · C++ · 1198 lines · 983 code · 116 blank · 99 comment · 229 complexity · eb69496b2bb3cfde7585d2f527d8e50c MD5 · raw file

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