PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/App/UnxUtilsSrc/unxutils/gawk-3.0.4/test/funstack.awk

https://bitbucket.org/dabomb69/bash-portable
AWK | 977 lines | 678 code | 142 blank | 157 comment | 0 complexity | c220cce63839178ed27a18ef84698c29 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, CC-BY-SA-3.0, Unlicense, AGPL-1.0
  1. ### ====================================================================
  2. ### @Awk-file{
  3. ### author = "Nelson H. F. Beebe",
  4. ### version = "1.00",
  5. ### date = "09 October 1996",
  6. ### time = "15:57:06 MDT",
  7. ### filename = "journal-toc.awk",
  8. ### address = "Center for Scientific Computing
  9. ### Department of Mathematics
  10. ### University of Utah
  11. ### Salt Lake City, UT 84112
  12. ### USA",
  13. ### telephone = "+1 801 581 5254",
  14. ### FAX = "+1 801 581 4148",
  15. ### URL = "http://www.math.utah.edu/~beebe",
  16. ### checksum = "25092 977 3357 26493",
  17. ### email = "beebe@math.utah.edu (Internet)",
  18. ### codetable = "ISO/ASCII",
  19. ### keywords = "BibTeX, bibliography, HTML, journal table of
  20. ### contents",
  21. ### supported = "yes",
  22. ### docstring = "Create a journal cover table of contents from
  23. ### <at>Article{...} entries in a journal BibTeX
  24. ### .bib file for checking the bibliography
  25. ### database against the actual journal covers.
  26. ### The output can be either plain text, or HTML.
  27. ###
  28. ### Usage:
  29. ### bibclean -max-width 0 BibTeX-file(s) | \
  30. ### bibsort -byvolume | \
  31. ### awk -f journal-toc.awk \
  32. ### [-v HTML=nnn] [-v INDENT=nnn] \
  33. ### [-v BIBFILEURL=url] >foo.toc
  34. ###
  35. ### or if the bibliography is already sorted
  36. ### by volume,
  37. ###
  38. ### bibclean -max-width 0 BibTeX-file(s) | \
  39. ### awk -f journal-toc.awk \
  40. ### [-v HTML=nnn] [-v INDENT=nnn] \
  41. ### [-v BIBFILEURL=url] >foo.toc
  42. ###
  43. ### A non-zero value of the command-line option,
  44. ### HTML=nnn, results in HTML output instead of
  45. ### the default plain ASCII text (corresponding
  46. ### to HTML=0). The
  47. ###
  48. ### The INDENT=nnn command-line option specifies
  49. ### the number of blanks to indent each logical
  50. ### level of HTML. The default is INDENT=4.
  51. ### INDENT=0 suppresses indentation. The INDENT
  52. ### option has no effect when the default HTML=0
  53. ### (plain text output) option is in effect.
  54. ###
  55. ### When HTML output is selected, the
  56. ### BIBFILEURL=url command-line option provides a
  57. ### way to request hypertext links from table of
  58. ### contents page numbers to the complete BibTeX
  59. ### entry for the article. These links are
  60. ### created by appending a sharp (#) and the
  61. ### citation label to the BIBFILEURL value, which
  62. ### conforms with the practice of
  63. ### bibtex-to-html.awk.
  64. ###
  65. ### The HTML output form may be useful as a more
  66. ### compact representation of journal article
  67. ### bibliography data than the original BibTeX
  68. ### file provides. Of course, the
  69. ### table-of-contents format provides less
  70. ### information, and is considerably more
  71. ### troublesome for a computer program to parse.
  72. ###
  73. ### When URL key values are provided, they will
  74. ### be used to create hypertext links around
  75. ### article titles. This supports journals that
  76. ### provide article contents on the World-Wide
  77. ### Web.
  78. ###
  79. ### For parsing simplicity, this program requires
  80. ### that BibTeX
  81. ###
  82. ### key = "value"
  83. ###
  84. ### and
  85. ###
  86. ### @String{name = "value"}
  87. ###
  88. ### specifications be entirely contained on
  89. ### single lines, which is readily provided by
  90. ### the `bibclean -max-width 0' filter. It also
  91. ### requires that bibliography entries begin and
  92. ### end at the start of a line, and that
  93. ### quotation marks, rather than balanced braces,
  94. ### delimit string values. This is a
  95. ### conventional format that again can be
  96. ### guaranteed by bibclean.
  97. ###
  98. ### This program requires `new' awk, as described
  99. ### in the book
  100. ###
  101. ### Alfred V. Aho, Brian W. Kernighan, and
  102. ### Peter J. Weinberger,
  103. ### ``The AWK Programming Language'',
  104. ### Addison-Wesley (1988), ISBN
  105. ### 0-201-07981-X,
  106. ###
  107. ### such as provided by programs named (GNU)
  108. ### gawk, nawk, and recent AT&T awk.
  109. ###
  110. ### The checksum field above contains a CRC-16
  111. ### checksum as the first value, followed by the
  112. ### equivalent of the standard UNIX wc (word
  113. ### count) utility output of lines, words, and
  114. ### characters. This is produced by Robert
  115. ### Solovay's checksum utility.",
  116. ### }
  117. ### ====================================================================
  118. BEGIN { initialize() }
  119. /^ *@ *[Ss][Tt][Rr][Ii][Nn][Gg] *{/ { do_String(); next }
  120. /^ *@ *[Pp][Rr][Ee][Aa][Mm][Bb][Ll][Ee]/ { next }
  121. /^ *@ *[Aa][Rr][Tt][Ii][Cc][Ll][Ee]/ { do_Article(); next }
  122. /^ *@/ { do_Other(); next }
  123. /^ *author *= *\"/ { do_author(); next }
  124. /^ *journal *= */ { do_journal(); next }
  125. /^ *volume *= *\"/ { do_volume(); next }
  126. /^ *number *= *\"/ { do_number(); next }
  127. /^ *year *= *\"/ { do_year(); next }
  128. /^ *month *= */ { do_month(); next }
  129. /^ *title *= *\"/ { do_title(); next }
  130. /^ *pages *= *\"/ { do_pages(); next }
  131. /^ *URL *= *\"/ { do_URL(); next }
  132. /^ *} *$/ { if (In_Article) do_end_entry(); next }
  133. END { terminate() }
  134. ########################################################################
  135. # NB: The programming conventions for variables in this program are: #
  136. # UPPERCASE global constants and user options #
  137. # Initialuppercase global variables #
  138. # lowercase local variables #
  139. # Any deviation is an error! #
  140. ########################################################################
  141. function do_Article()
  142. {
  143. In_Article = 1
  144. Citation_label = $0
  145. sub(/^[^\{]*{/,"",Citation_label)
  146. sub(/ *, *$/,"",Citation_label)
  147. Author = ""
  148. Title = ""
  149. Journal = ""
  150. Volume = ""
  151. Number = ""
  152. Month = ""
  153. Year = ""
  154. Pages = ""
  155. Url = ""
  156. }
  157. function do_author()
  158. {
  159. Author = TeX_to_HTML(get_value($0))
  160. }
  161. function do_end_entry( k,n,parts)
  162. {
  163. n = split(Author,parts," and ")
  164. if (Last_number != Number)
  165. do_new_issue()
  166. for (k = 1; k < n; ++k)
  167. print_toc_line(parts[k] " and", "", "")
  168. Title_prefix = html_begin_title()
  169. Title_suffix = html_end_title()
  170. if (html_length(Title) <= (MAX_TITLE_CHARS + MIN_LEADERS)) # complete title fits on line
  171. print_toc_line(parts[n], Title, html_begin_pages() Pages html_end_pages())
  172. else # need to split long title over multiple lines
  173. do_long_title(parts[n], Title, html_begin_pages() Pages html_end_pages())
  174. }
  175. function do_journal()
  176. {
  177. if ($0 ~ /[=] *"/) # have journal = "quoted journal name",
  178. Journal = get_value($0)
  179. else # have journal = journal-abbreviation,
  180. {
  181. Journal = get_abbrev($0)
  182. if (Journal in String) # replace abbrev by its expansion
  183. Journal = String[Journal]
  184. }
  185. gsub(/\\-/,"",Journal) # remove discretionary hyphens
  186. }
  187. function do_long_title(author,title,pages, last_title,n)
  188. {
  189. title = trim(title) # discard leading and trailing space
  190. while (length(title) > 0)
  191. {
  192. n = html_breakpoint(title,MAX_TITLE_CHARS+MIN_LEADERS)
  193. last_title = substr(title,1,n)
  194. title = substr(title,n+1)
  195. sub(/^ +/,"",title) # discard any leading space
  196. print_toc_line(author, last_title, (length(title) == 0) ? pages : "")
  197. author = ""
  198. }
  199. }
  200. function do_month( k,n,parts)
  201. {
  202. Month = ($0 ~ /[=] *"/) ? get_value($0) : get_abbrev($0)
  203. gsub(/[\"]/,"",Month)
  204. gsub(/ *# *\\slash *# */," / ",Month)
  205. gsub(/ *# *-+ *# */," / ",Month)
  206. n = split(Month,parts," */ *")
  207. Month = ""
  208. for (k = 1; k <= n; ++k)
  209. Month = Month ((k > 1) ? " / " : "") \
  210. ((parts[k] in Month_expansion) ? Month_expansion[parts[k]] : parts[k])
  211. }
  212. function do_new_issue()
  213. {
  214. Last_number = Number
  215. if (HTML)
  216. {
  217. if (Last_volume != Volume)
  218. {
  219. Last_volume = Volume
  220. print_line(prefix(2) "<BR>")
  221. }
  222. html_end_toc()
  223. html_begin_issue()
  224. print_line(prefix(2) Journal "<BR>")
  225. }
  226. else
  227. {
  228. print_line("")
  229. print_line(Journal)
  230. }
  231. print_line(strip_html(vol_no_month_year()))
  232. if (HTML)
  233. {
  234. html_end_issue()
  235. html_toc_entry()
  236. html_begin_toc()
  237. }
  238. else
  239. print_line("")
  240. }
  241. function do_number()
  242. {
  243. Number = get_value($0)
  244. }
  245. function do_Other()
  246. {
  247. In_Article = 0
  248. }
  249. function do_pages()
  250. {
  251. Pages = get_value($0)
  252. sub(/--[?][?]/,"",Pages)
  253. }
  254. function do_String()
  255. {
  256. sub(/^[^\{]*\{/,"",$0) # discard up to and including open brace
  257. sub(/\} *$/,"",$0) # discard from optional whitespace and trailing brace to end of line
  258. String[get_key($0)] = get_value($0)
  259. }
  260. function do_title()
  261. {
  262. Title = TeX_to_HTML(get_value($0))
  263. }
  264. function do_URL( parts)
  265. {
  266. Url = get_value($0)
  267. split(Url,parts,"[,;]") # in case we have multiple URLs
  268. Url = trim(parts[1])
  269. }
  270. function do_volume()
  271. {
  272. Volume = get_value($0)
  273. }
  274. function do_year()
  275. {
  276. Year = get_value($0)
  277. }
  278. function get_abbrev(s)
  279. { # return abbrev from ``key = abbrev,''
  280. sub(/^[^=]*= */,"",s) # discard text up to start of non-blank value
  281. sub(/ *,? *$/,"",s) # discard trailing optional whitspace, quote,
  282. # optional comma, and optional space
  283. return (s)
  284. }
  285. function get_key(s)
  286. { # return kay from ``key = "value",''
  287. sub(/^ */,"",s) # discard leading space
  288. sub(/ *=.*$/,"",s) # discard everthing after key
  289. return (s)
  290. }
  291. function get_value(s)
  292. { # return value from ``key = "value",''
  293. sub(/^[^\"]*\" */,"",s) # discard text up to start of non-blank value
  294. sub(/ *\",? *$/,"",s) # discard trailing optional whitspace, quote,
  295. # optional comma, and optional space
  296. return (s)
  297. }
  298. function html_accents(s)
  299. {
  300. if (index(s,"\\") > 0) # important optimization
  301. {
  302. # Convert common lower-case accented letters according to the
  303. # table on p. 169 of in Peter Flynn's ``The World Wide Web
  304. # Handbook'', International Thomson Computer Press, 1995, ISBN
  305. # 1-85032-205-8. The official table of ISO Latin 1 SGML
  306. # entities used in HTML can be found in the file
  307. # /usr/local/lib/html-check/lib/ISOlat1.sgml (your path
  308. # may differ).
  309. gsub(/{\\\a}/, "\\&agrave;", s)
  310. gsub(/{\\'a}/, "\\&aacute;", s)
  311. gsub(/{\\[\^]a}/,"\\&acirc;", s)
  312. gsub(/{\\~a}/, "\\&atilde;", s)
  313. gsub(/{\\\"a}/, "\\&auml;", s)
  314. gsub(/{\\aa}/, "\\&aring;", s)
  315. gsub(/{\\ae}/, "\\&aelig;", s)
  316. gsub(/{\\c{c}}/,"\\&ccedil;", s)
  317. gsub(/{\\\e}/, "\\&egrave;", s)
  318. gsub(/{\\'e}/, "\\&eacute;", s)
  319. gsub(/{\\[\^]e}/,"\\&ecirc;", s)
  320. gsub(/{\\\"e}/, "\\&euml;", s)
  321. gsub(/{\\\i}/, "\\&igrave;", s)
  322. gsub(/{\\'i}/, "\\&iacute;", s)
  323. gsub(/{\\[\^]i}/,"\\&icirc;", s)
  324. gsub(/{\\\"i}/, "\\&iuml;", s)
  325. # ignore eth and thorn
  326. gsub(/{\\~n}/, "\\&ntilde;", s)
  327. gsub(/{\\\o}/, "\\&ograve;", s)
  328. gsub(/{\\'o}/, "\\&oacute;", s)
  329. gsub(/{\\[\^]o}/, "\\&ocirc;", s)
  330. gsub(/{\\~o}/, "\\&otilde;", s)
  331. gsub(/{\\\"o}/, "\\&ouml;", s)
  332. gsub(/{\\o}/, "\\&oslash;", s)
  333. gsub(/{\\\u}/, "\\&ugrave;", s)
  334. gsub(/{\\'u}/, "\\&uacute;", s)
  335. gsub(/{\\[\^]u}/,"\\&ucirc;", s)
  336. gsub(/{\\\"u}/, "\\&uuml;", s)
  337. gsub(/{\\'y}/, "\\&yacute;", s)
  338. gsub(/{\\\"y}/, "\\&yuml;", s)
  339. # Now do the same for upper-case accents
  340. gsub(/{\\\A}/, "\\&Agrave;", s)
  341. gsub(/{\\'A}/, "\\&Aacute;", s)
  342. gsub(/{\\[\^]A}/, "\\&Acirc;", s)
  343. gsub(/{\\~A}/, "\\&Atilde;", s)
  344. gsub(/{\\\"A}/, "\\&Auml;", s)
  345. gsub(/{\\AA}/, "\\&Aring;", s)
  346. gsub(/{\\AE}/, "\\&AElig;", s)
  347. gsub(/{\\c{C}}/,"\\&Ccedil;", s)
  348. gsub(/{\\\e}/, "\\&Egrave;", s)
  349. gsub(/{\\'E}/, "\\&Eacute;", s)
  350. gsub(/{\\[\^]E}/, "\\&Ecirc;", s)
  351. gsub(/{\\\"E}/, "\\&Euml;", s)
  352. gsub(/{\\\I}/, "\\&Igrave;", s)
  353. gsub(/{\\'I}/, "\\&Iacute;", s)
  354. gsub(/{\\[\^]I}/, "\\&Icirc;", s)
  355. gsub(/{\\\"I}/, "\\&Iuml;", s)
  356. # ignore eth and thorn
  357. gsub(/{\\~N}/, "\\&Ntilde;", s)
  358. gsub(/{\\\O}/, "\\&Ograve;", s)
  359. gsub(/{\\'O}/, "\\&Oacute;", s)
  360. gsub(/{\\[\^]O}/, "\\&Ocirc;", s)
  361. gsub(/{\\~O}/, "\\&Otilde;", s)
  362. gsub(/{\\\"O}/, "\\&Ouml;", s)
  363. gsub(/{\\O}/, "\\&Oslash;", s)
  364. gsub(/{\\\U}/, "\\&Ugrave;", s)
  365. gsub(/{\\'U}/, "\\&Uacute;", s)
  366. gsub(/{\\[\^]U}/, "\\&Ucirc;", s)
  367. gsub(/{\\\"U}/, "\\&Uuml;", s)
  368. gsub(/{\\'Y}/, "\\&Yacute;", s)
  369. gsub(/{\\ss}/, "\\&szlig;", s)
  370. # Others not mentioned in Flynn's book
  371. gsub(/{\\'\\i}/,"\\&iacute;", s)
  372. gsub(/{\\'\\j}/,"j", s)
  373. }
  374. return (s)
  375. }
  376. function html_begin_issue()
  377. {
  378. print_line("")
  379. print_line(prefix(2) "<HR>")
  380. print_line("")
  381. print_line(prefix(2) "<H1>")
  382. print_line(prefix(3) "<A NAME=\"" html_label() "\">")
  383. }
  384. function html_begin_pages()
  385. {
  386. return ((HTML && (BIBFILEURL != "")) ? ("<A HREF=\"" BIBFILEURL "#" Citation_label "\">") : "")
  387. }
  388. function html_begin_pre()
  389. {
  390. In_PRE = 1
  391. print_line("<PRE>")
  392. }
  393. function html_begin_title()
  394. {
  395. return ((HTML && (Url != "")) ? ("<A HREF=\"" Url "\">") : "")
  396. }
  397. function html_begin_toc()
  398. {
  399. html_end_toc()
  400. html_begin_pre()
  401. }
  402. function html_body( k)
  403. {
  404. for (k = 1; k <= BodyLines; ++k)
  405. print Body[k]
  406. }
  407. function html_breakpoint(title,maxlength, break_after,k)
  408. {
  409. # Return the largest character position in title AFTER which we
  410. # can break the title across lines, without exceeding maxlength
  411. # visible characters.
  412. if (html_length(title) > maxlength) # then need to split title across lines
  413. {
  414. # In the presence of HTML markup, the initialization of
  415. # k here is complicated, because we need to advance it
  416. # until html_length(title) is at least maxlength,
  417. # without invoking the expensive html_length() function
  418. # too frequently. The need to split the title makes the
  419. # alternative of delayed insertion of HTML markup much
  420. # more complicated.
  421. break_after = 0
  422. for (k = min(maxlength,length(title)); k < length(title); ++k)
  423. {
  424. if (substr(title,k+1,1) == " ")
  425. { # could break after position k
  426. if (html_length(substr(title,1,k)) <= maxlength)
  427. break_after = k
  428. else # advanced too far, retreat back to last break_after
  429. break
  430. }
  431. }
  432. if (break_after == 0) # no breakpoint found by forward scan
  433. { # so switch to backward scan
  434. for (k = min(maxlength,length(title)) - 1; \
  435. (k > 0) && (substr(title,k+1,1) != " "); --k)
  436. ; # find space at which to break title
  437. if (k < 1) # no break point found
  438. k = length(title) # so must print entire string
  439. }
  440. else
  441. k = break_after
  442. }
  443. else # title fits on one line
  444. k = length(title)
  445. return (k)
  446. }
  447. function html_end_issue()
  448. {
  449. print_line(prefix(3) "</A>")
  450. print_line(prefix(2) "</H1>")
  451. }
  452. function html_end_pages()
  453. {
  454. return ((HTML && (BIBFILEURL != "")) ? "</A>" : "")
  455. }
  456. function html_end_pre()
  457. {
  458. if (In_PRE)
  459. {
  460. print_line("</PRE>")
  461. In_PRE = 0
  462. }
  463. }
  464. function html_end_title()
  465. {
  466. return ((HTML && (Url != "")) ? "</A>" : "")
  467. }
  468. function html_end_toc()
  469. {
  470. html_end_pre()
  471. }
  472. function html_fonts(s, arg,control_word,k,level,n,open_brace)
  473. {
  474. open_brace = index(s,"{")
  475. if (open_brace > 0) # important optimization
  476. {
  477. level = 1
  478. for (k = open_brace + 1; (level != 0) && (k <= length(s)); ++k)
  479. {
  480. if (substr(s,k,1) == "{")
  481. level++
  482. else if (substr(s,k,1) == "}")
  483. level--
  484. }
  485. # {...} is now found at open_brace ... (k-1)
  486. for (control_word in Font_decl_map) # look for {\xxx ...}
  487. {
  488. if (substr(s,open_brace+1,length(control_word)+1) ~ \
  489. ("\\" control_word "[^A-Za-z]"))
  490. {
  491. n = open_brace + 1 + length(control_word)
  492. arg = trim(substr(s,n,k - n))
  493. if (Font_decl_map[control_word] == "toupper") # arg -> ARG
  494. arg = toupper(arg)
  495. else if (Font_decl_map[control_word] != "") # arg -> <TAG>arg</TAG>
  496. arg = "<" Font_decl_map[control_word] ">" arg "</" Font_decl_map[control_word] ">"
  497. return (substr(s,1,open_brace-1) arg html_fonts(substr(s,k)))
  498. }
  499. }
  500. for (control_word in Font_cmd_map) # look for \xxx{...}
  501. {
  502. if (substr(s,open_brace - length(control_word),length(control_word)) ~ \
  503. ("\\" control_word))
  504. {
  505. n = open_brace + 1
  506. arg = trim(substr(s,n,k - n))
  507. if (Font_cmd_map[control_word] == "toupper") # arg -> ARG
  508. arg = toupper(arg)
  509. else if (Font_cmd_map[control_word] != "") # arg -> <TAG>arg</TAG>
  510. arg = "<" Font_cmd_map[control_word] ">" arg "</" Font_cmd_map[control_word] ">"
  511. n = open_brace - length(control_word) - 1
  512. return (substr(s,1,n) arg html_fonts(substr(s,k)))
  513. }
  514. }
  515. }
  516. return (s)
  517. }
  518. function html_header()
  519. {
  520. USER = ENVIRON["USER"]
  521. if (USER == "")
  522. USER = ENVIRON["LOGNAME"]
  523. if (USER == "")
  524. USER = "????"
  525. "hostname" | getline HOSTNAME
  526. "date" | getline DATE
  527. ("ypcat passwd | grep '^" USER ":' | awk -F: '{print $5}'") | getline PERSONAL_NAME
  528. if (PERSONAL_NAME == "")
  529. ("grep '^" USER ":' /etc/passwd | awk -F: '{print $5}'") | getline PERSONAL_NAME
  530. print "<!-- WARNING: Do NOT edit this file. It was converted from -->"
  531. print "<!-- BibTeX format to HTML by journal-toc.awk version " VERSION_NUMBER " " VERSION_DATE " -->"
  532. print "<!-- on " DATE " -->"
  533. print "<!-- for " PERSONAL_NAME " (" USER "@" HOSTNAME ") -->"
  534. print ""
  535. print ""
  536. print "<!DOCTYPE HTML public \"-//IETF//DTD HTML//EN\">"
  537. print ""
  538. print "<HTML>"
  539. print prefix(1) "<HEAD>"
  540. print prefix(2) "<TITLE>"
  541. print prefix(3) Journal
  542. print prefix(2) "</TITLE>"
  543. print prefix(2) "<LINK REV=\"made\" HREF=\"mailto:" USER "@" HOSTNAME "\">"
  544. print prefix(1) "</HEAD>"
  545. print ""
  546. print prefix(1) "<BODY>"
  547. }
  548. function html_label( label)
  549. {
  550. label = Volume "(" Number "):" Month ":" Year
  551. gsub(/[^A-Za-z0-9():,;.\/\-]/,"",label)
  552. return (label)
  553. }
  554. function html_length(s)
  555. { # Return visible length of s, ignoring any HTML markup
  556. if (HTML)
  557. {
  558. gsub(/<\/?[^>]*>/,"",s) # remove SGML tags
  559. gsub(/&[A-Za-z0-9]+;/,"",s) # remove SGML entities
  560. }
  561. return (length(s))
  562. }
  563. function html_toc()
  564. {
  565. print prefix(2) "<H1>"
  566. print prefix(3) "Table of contents for issues of " Journal
  567. print prefix(2) "</H1>"
  568. print HTML_TOC
  569. }
  570. function html_toc_entry()
  571. {
  572. HTML_TOC = HTML_TOC " <A HREF=\"#" html_label() "\">"
  573. HTML_TOC = HTML_TOC vol_no_month_year()
  574. HTML_TOC = HTML_TOC "</A><BR>" "\n"
  575. }
  576. function html_trailer()
  577. {
  578. html_end_pre()
  579. print prefix(1) "</BODY>"
  580. print "</HTML>"
  581. }
  582. function initialize()
  583. {
  584. # NB: Update these when the program changes
  585. VERSION_DATE = "[09-Oct-1996]"
  586. VERSION_NUMBER = "1.00"
  587. HTML = (HTML == "") ? 0 : (0 + HTML)
  588. if (INDENT == "")
  589. INDENT = 4
  590. if (HTML == 0)
  591. INDENT = 0 # indentation suppressed in ASCII mode
  592. LEADERS = " . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ."
  593. MAX_TITLE_CHARS = 36 # 36 produces a 79-char output line when there is
  594. # just an initial page number. If this is
  595. # increased, the LEADERS string may need to be
  596. # lengthened.
  597. MIN_LEADERS = 4 # Minimum number of characters from LEADERS
  598. # required when leaders are used. The total
  599. # number of characters that can appear in a
  600. # title line is MAX_TITLE_CHARS + MIN_LEADERS.
  601. # Leaders are omitted when the title length is
  602. # between MAX_TITLE_CHARS and this sum.
  603. MIN_LEADERS_SPACE = " " # must be at least MIN_LEADERS characters long
  604. Month_expansion["jan"] = "January"
  605. Month_expansion["feb"] = "February"
  606. Month_expansion["mar"] = "March"
  607. Month_expansion["apr"] = "April"
  608. Month_expansion["may"] = "May"
  609. Month_expansion["jun"] = "June"
  610. Month_expansion["jul"] = "July"
  611. Month_expansion["aug"] = "August"
  612. Month_expansion["sep"] = "September"
  613. Month_expansion["oct"] = "October"
  614. Month_expansion["nov"] = "November"
  615. Month_expansion["dec"] = "December"
  616. Font_cmd_map["\\emph"] = "EM"
  617. Font_cmd_map["\\textbf"] = "B"
  618. Font_cmd_map["\\textit"] = "I"
  619. Font_cmd_map["\\textmd"] = ""
  620. Font_cmd_map["\\textrm"] = ""
  621. Font_cmd_map["\\textsc"] = "toupper"
  622. Font_cmd_map["\\textsl"] = "I"
  623. Font_cmd_map["\\texttt"] = "t"
  624. Font_cmd_map["\\textup"] = ""
  625. Font_decl_map["\\bf"] = "B"
  626. Font_decl_map["\\em"] = "EM"
  627. Font_decl_map["\\it"] = "I"
  628. Font_decl_map["\\rm"] = ""
  629. Font_decl_map["\\sc"] = "toupper"
  630. Font_decl_map["\\sf"] = ""
  631. Font_decl_map["\\tt"] = "TT"
  632. Font_decl_map["\\itshape"] = "I"
  633. Font_decl_map["\\upshape"] = ""
  634. Font_decl_map["\\slshape"] = "I"
  635. Font_decl_map["\\scshape"] = "toupper"
  636. Font_decl_map["\\mdseries"] = ""
  637. Font_decl_map["\\bfseries"] = "B"
  638. Font_decl_map["\\rmfamily"] = ""
  639. Font_decl_map["\\sffamily"] = ""
  640. Font_decl_map["\\ttfamily"] = "TT"
  641. }
  642. function min(a,b)
  643. {
  644. return (a < b) ? a : b
  645. }
  646. function prefix(level)
  647. {
  648. # Return a prefix of up to 60 blanks
  649. if (In_PRE)
  650. return ("")
  651. else
  652. return (substr(" ", \
  653. 1, INDENT * level))
  654. }
  655. function print_line(line)
  656. {
  657. if (HTML) # must buffer in memory so that we can accumulate TOC
  658. Body[++BodyLines] = line
  659. else
  660. print line
  661. }
  662. function print_toc_line(author,title,pages, extra,leaders,n,t)
  663. {
  664. # When we have a multiline title, the hypertext link goes only
  665. # on the first line. A multiline hypertext link looks awful
  666. # because of long underlines under the leading indentation.
  667. if (pages == "") # then no leaders needed in title lines other than last one
  668. t = sprintf("%31s %s%s%s", author, Title_prefix, title, Title_suffix)
  669. else # last title line, with page number
  670. {
  671. n = html_length(title) # potentially expensive
  672. extra = n % 2 # extra space for aligned leader dots
  673. if (n <= MAX_TITLE_CHARS) # then need leaders
  674. leaders = substr(LEADERS, 1, MAX_TITLE_CHARS + MIN_LEADERS - extra - \
  675. min(MAX_TITLE_CHARS,n))
  676. else # title (almost) fills line, so no leaders
  677. leaders = substr(MIN_LEADERS_SPACE,1, \
  678. (MAX_TITLE_CHARS + MIN_LEADERS - extra - n))
  679. t = sprintf("%31s %s%s%s%s%s %4s", \
  680. author, Title_prefix, title, Title_suffix, \
  681. (extra ? " " : ""), leaders, pages)
  682. }
  683. Title_prefix = "" # forget any hypertext
  684. Title_suffix = "" # link material
  685. # Efficency note: an earlier version accumulated the body in a
  686. # single scalar like this: "Body = Body t". Profiling revealed
  687. # this statement as the major hot spot, and the change to array
  688. # storage made the program more than twice as fast. This
  689. # suggests that awk might benefit from an optimization of
  690. # "s = s t" that uses realloc() instead of malloc().
  691. if (HTML)
  692. Body[++BodyLines] = t
  693. else
  694. print t
  695. }
  696. function protect_SGML_characters(s)
  697. {
  698. gsub(/&/,"\\&amp;",s) # NB: this one MUST be first
  699. gsub(/</,"\\&lt;",s)
  700. gsub(/>/,"\\&gt;",s)
  701. gsub(/\"/,"\\&quot;",s)
  702. return (s)
  703. }
  704. function strip_braces(s, k)
  705. { # strip non-backslashed braces from s and return the result
  706. return (strip_char(strip_char(s,"{"),"}"))
  707. }
  708. function strip_char(s,c, k)
  709. { # strip non-backslashed instances of c from s, and return the result
  710. k = index(s,c)
  711. if (k > 0) # then found the character
  712. {
  713. if (substr(s,k-1,1) != "\\") # then not backslashed char
  714. s = substr(s,1,k-1) strip_char(substr(s,k+1),c) # so remove it (recursively)
  715. else # preserve backslashed char
  716. s = substr(s,1,k) strip_char(s,k+1,c)
  717. }
  718. return (s)
  719. }
  720. function strip_html(s)
  721. {
  722. gsub(/<\/?[^>]*>/,"",s)
  723. return (s)
  724. }
  725. function terminate()
  726. {
  727. if (HTML)
  728. {
  729. html_end_pre()
  730. HTML = 0 # NB: stop line buffering
  731. html_header()
  732. html_toc()
  733. html_body()
  734. html_trailer()
  735. }
  736. }
  737. function TeX_to_HTML(s, k,n,parts)
  738. {
  739. # First convert the four SGML reserved characters to SGML entities
  740. if (HTML)
  741. {
  742. gsub(/>/, "\\&gt;", s)
  743. gsub(/</, "\\&lt;", s)
  744. gsub(/"/, "\\&quot;", s)
  745. }
  746. gsub(/[$][$]/,"$$",s) # change display math to triple dollars for split
  747. n = split(s,parts,/[$]/)# split into non-math (odd) and math (even) parts
  748. s = ""
  749. for (k = 1; k <= n; ++k) # unbrace non-math part, leaving math mode intact
  750. s = s ((k > 1) ? "$" : "") \
  751. ((k % 2) ? strip_braces(TeX_to_HTML_nonmath(parts[k])) : \
  752. TeX_to_HTML_math(parts[k]))
  753. gsub(/[$][$][$]/,"$$",s) # restore display math
  754. return (s)
  755. }
  756. function TeX_to_HTML_math(s)
  757. {
  758. # Mostly a dummy for now, but HTML 3 could support some math translation
  759. gsub(/\\&/,"\\&amp;",s) # reduce TeX ampersands to SGML entities
  760. return (s)
  761. }
  762. function TeX_to_HTML_nonmath(s)
  763. {
  764. if (index(s,"\\") > 0) # important optimization
  765. {
  766. gsub(/\\slash +/,"/",s) # replace TeX slashes with conventional ones
  767. gsub(/ *\\emdash +/," --- ",s) # replace BibNet emdashes with conventional ones
  768. gsub(/\\%/,"%",s) # reduce TeX percents to conventional ones
  769. gsub(/\\[$]/,"$",s) # reduce TeX dollars to conventional ones
  770. gsub(/\\#/,"#",s) # reduce TeX sharps to conventional ones
  771. if (HTML) # translate TeX markup to HTML
  772. {
  773. gsub(/\\&/,"\\&amp;",s) # reduce TeX ampersands to SGML entities
  774. s = html_accents(s)
  775. s = html_fonts(s)
  776. }
  777. else # plain ASCII text output: discard all TeX markup
  778. {
  779. gsub(/\\\&/, "\\&", s) # reduce TeX ampersands to conventional ones
  780. gsub(/\\[a-z][a-z] +/,"",s) # remove TeX font changes
  781. gsub(/\\[^A-Za-z]/,"",s) # remove remaining TeX control symbols
  782. }
  783. }
  784. return (s)
  785. }
  786. function trim(s)
  787. {
  788. gsub(/^[ \t]+/,"",s)
  789. gsub(/[ \t]+$/,"",s)
  790. return (s)
  791. }
  792. function vol_no_month_year()
  793. {
  794. return ("Volume " wrap(Volume) ", Number " wrap(Number) ", " wrap(Month) ", " wrap(Year))
  795. }
  796. function wrap(value)
  797. {
  798. return (HTML ? ("<STRONG>" value "</STRONG>") : value)
  799. }