jrtangle 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. #! /usr/local/bin/gawk -f
  2. #
  3. # Copyright (C) 2013, 2014, 2015 Arnold David Robbins
  4. #
  5. # This file is part of TexiWeb Jr., a literate programming system.
  6. #
  7. # TexiWeb Jr. is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # TexiWeb Jr. is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. #
  21. # Up-to-date source code for TexiWeb Jr. can be obtained via
  22. # Git from github:
  23. #
  24. # git clone http://github.com/arnoldrobbins/texiwebjr
  25. #
  26. BEGIN {
  27. TRUE = 1
  28. FALSE = 0
  29. File_chunk_pattern = "^@\\(([^)]+)@\\)[[:space:]]*=[[:space:]]*$"
  30. Code_chunk_pattern = "^@" "<(.+)" "@>[[:space:]]*=[[:space:]]*$"
  31. Chunk_name_pattern = "@<[^>\n]+@>"
  32. }
  33. # Error checking:
  34. # Use brackets to avoid triggering the warning on ourselves!
  35. /(^<[@])|(>[@]([[:space:]]*=[[:space:]]*)?$)/ {
  36. # Ditto, with concatenation
  37. warning("<" "@ or >" "@ used instead of @" "< or @" ">\n\t%s\n",
  38. $0)
  39. }
  40. END {
  41. check_unfinished()
  42. }
  43. # check_unfinished --- print a fatal error when an unfinished code or
  44. # file chunk is detected. Also ifweave / iftangle.
  45. function check_unfinished()
  46. {
  47. if (Flags["file chunk"])
  48. fatal(_"unfinished file chunk (started at %s)\n",
  49. Line_numbers["file chunk"])
  50. else if (Flags["code chunk"])
  51. fatal(_"unfinished code chunk (started at %s)\n",
  52. Line_numbers["code chunk"])
  53. if ("ifweave" in Line_numbers)
  54. fatal(_"unfinished @ifweave section (started at %s)\n",
  55. Line_numbers["ifweave"])
  56. if ("iftangle" in Line_numbers)
  57. fatal(_"unfinished @iftangle section (started at %s)\n",
  58. Line_numbers["iftangle"])
  59. }
  60. # strip_out_name --- get the name from name
  61. function strip_out_name(name, l)
  62. {
  63. l = length(name)
  64. name = substr(name, 3, l - 4)
  65. return name
  66. }
  67. # Helper functions
  68. # message --- write a particular kind of message out to stderr
  69. function message(msg, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
  70. {
  71. printf("%s:%d: %s: " format, FILENAME, FNR, msg,
  72. a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) > "/dev/stderr"
  73. if (substr(format, length(format), 1) != "\n")
  74. printf("\n") > "/dev/stderr"
  75. }
  76. # fatal --- print a fatal error message and exit.
  77. # No varargs, so fake it with lots of parameters.
  78. function fatal(format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
  79. {
  80. message(_"fatal", format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
  81. exit 1
  82. }
  83. # warning --- print a warning message to stderr
  84. # No varargs, so fake it with lots of parameters.
  85. function warning(format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
  86. {
  87. message(_"warning", format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
  88. }
  89. # join.awk --- join an array into a string
  90. #
  91. # Arnold Robbins, arnold@skeeve.com, Public Domain
  92. # May 1993
  93. function join(array, start, end, sep, result, i)
  94. {
  95. if (sep == "")
  96. sep = " "
  97. else if (sep == SUBSEP) # magic value
  98. sep = ""
  99. result = array[start]
  100. for (i = start + 1; i <= end; i++)
  101. result = result sep array[i]
  102. return result
  103. }
  104. # ifweave lines should be removed
  105. /^@ifweave[[:space:]]*$/, /^@end ifweave[[:space:]]*$/ {
  106. if ("iftangle" in Line_numbers)
  107. fatal(_"cannot nest @ifweave inside @iftangle\n")
  108. # start of construct, save line number
  109. if (/^@ifweave[[:space:]]*$/)
  110. Line_numbers["ifweave"] = (FILENAME ":" FNR)
  111. # end of construct, delete line number
  112. if (/^@end ifweave[[:space:]]*$/)
  113. delete Line_numbers["ifweave"]
  114. # simply skip these lines, this is tangling
  115. next
  116. }
  117. # For tangling we remove the bracketing control lines and let anything
  118. # in between fall through.
  119. /^@iftangle[[:space:]]*$/, /^@end iftangle[[:space:]]*$/ {
  120. if ("ifweave" in Line_numbers)
  121. fatal(_"cannot nest @iftangle inside @ifweave\n")
  122. # start of construct, save line number, skip this line
  123. if (/^@iftangle[[:space:]]*$/) {
  124. Line_numbers["iftangle"] = (FILENAME ":" FNR)
  125. next
  126. }
  127. # end of construct, delete line number, skip this line
  128. if (/^@end iftangle[[:space:]]*$/) {
  129. delete Line_numbers["iftangle"]
  130. next
  131. }
  132. # otherwise fall through into the rest of the code
  133. }
  134. $0 ~ Code_chunk_pattern {
  135. Chunk_type = "code chunk"
  136. Pattern = Code_chunk_pattern
  137. Debug_pat = "code"
  138. new_chunk = gensub(Pattern, "\\1", 1)
  139. if (Flags[Chunk_type]) {
  140. fatal(_"%s start of %s found while still collecting %s\n",
  141. Chunk_type, new_chunk, Current_chunk)
  142. }
  143. check_unfinished()
  144. Flags[Chunk_type] = TRUE
  145. Line_numbers[Chunk_type] = (FILENAME ":" FNR)
  146. Current_chunk = new_chunk
  147. Chunk_info[Current_chunk]["type"] = Chunk_type
  148. if (Debug ~ Debug_pat)
  149. printf("saw new %s %s\n", Debug_pat, Current_chunk) > "/dev/stderr"
  150. next
  151. }
  152. $0 ~ File_chunk_pattern {
  153. Chunk_type = "file chunk"
  154. Pattern = File_chunk_pattern
  155. Debug_pat = "filename"
  156. new_chunk = gensub(Pattern, "\\1", 1)
  157. if (Flags[Chunk_type]) {
  158. fatal(_"%s start of %s found while still collecting %s\n",
  159. Chunk_type, new_chunk, Current_chunk)
  160. }
  161. check_unfinished()
  162. Flags[Chunk_type] = TRUE
  163. Line_numbers[Chunk_type] = (FILENAME ":" FNR)
  164. Current_chunk = new_chunk
  165. Chunk_info[Current_chunk]["type"] = Chunk_type
  166. if (Debug ~ Debug_pat)
  167. printf("saw new %s %s\n", Debug_pat, Current_chunk) > "/dev/stderr"
  168. next
  169. }
  170. /^@[[:space:]]*$/ {
  171. if (Flags["file chunk"])
  172. end_file_gathering()
  173. else if (Flags["code chunk"])
  174. end_code_gathering()
  175. else
  176. warning(_"unmatched terminating @-sign: ignored\n")
  177. Chunk_lines = ""
  178. Flags[Chunk_type] = FALSE
  179. Line_numbers[Chunk_type] = ""
  180. Chunk_type = ""
  181. next
  182. }
  183. Flags["file chunk"] || Flags["code chunk"] {
  184. if (Chunk_lines == "")
  185. Chunk_lines = $0
  186. else
  187. Chunk_lines = Chunk_lines "\n" $0
  188. next
  189. }
  190. # end_file_gathering ---finish up collecting a file
  191. function end_file_gathering()
  192. {
  193. if (Current_chunk in File_contents)
  194. File_contents[Current_chunk] = \
  195. File_contents[Current_chunk] "\n" Chunk_lines
  196. else
  197. File_contents[Current_chunk] = Chunk_lines
  198. if (Debug ~ /filename/)
  199. printf("finished collecting file %s\n",
  200. Current_chunk) > "/dev/stderr"
  201. }
  202. # end_code_gathering --- complete collecting lines of the code chunk
  203. function end_code_gathering()
  204. {
  205. if (Current_chunk in Code_contents)
  206. Code_contents[Current_chunk] = \
  207. Code_contents[Current_chunk] "\n" Chunk_lines
  208. else
  209. Code_contents[Current_chunk] = Chunk_lines
  210. if (Debug ~ /code/)
  211. printf("finished collecting %s\n",
  212. Current_chunk) > "/dev/stderr"
  213. }
  214. # Finishing off code is easy, at least from 10,000 feet.
  215. END {
  216. if (length(Initial_setup) > 0) {
  217. Initial_setup = Initial_setup "\n"
  218. system(Shell_debug Initial_setup)
  219. }
  220. dump_files()
  221. if (Debug ~ /code/)
  222. dump_chunks()
  223. }
  224. # dump_files --- create files and update them as needed
  225. function dump_files( i, s, update_recipe)
  226. {
  227. for (i in File_contents) {
  228. s = expand_code_chunks(File_contents[i])
  229. create_update_recipe(i, update_recipe)
  230. printf("%s\n", s) > update_recipe["output"]
  231. close(update_recipe["output"])
  232. # update the file if necessary
  233. update_recipe["recipe"] = (Shell_debug update_recipe["recipe"])
  234. system(update_recipe["recipe"])
  235. # run any "post create" command (such as changing mode)
  236. if (i in Post_create_commands)
  237. system(Shell_debug Post_create_commands[i])
  238. }
  239. }
  240. # expand_code_chunks --- expand embedded code chunks.
  241. function expand_code_chunks(contents)
  242. {
  243. delete Currently_expanding
  244. return do_expand_code_chunks(contents)
  245. }
  246. # do_expand_code_chunks --- do the actual work to expand code chunks
  247. function do_expand_code_chunks(contents,
  248. nlines, lines, results, i, j) # locals
  249. {
  250. nlines = split(contents, lines, "\n")
  251. for (i = 1; i <= nlines; i++) {
  252. if (lines[i] !~ Chunk_name_pattern) {
  253. results[i] = lines[i]
  254. } else
  255. results[i] = expand_one_line(lines[i])
  256. }
  257. return join(results, 1, nlines, "\n")
  258. }
  259. # expand_one_line --- expand a line with code chunk references
  260. function expand_one_line(input_line, parts, chunk_names, nparts,
  261. num_names, code_lines, i, j)
  262. {
  263. nparts = split(input_line, parts, Chunk_name_pattern, chunk_names)
  264. num_names = length(chunk_names)
  265. # check for recursive expansion
  266. for (i = 1; i <= num_names; i++) {
  267. j = strip_out_name(chunk_names[i])
  268. if (! (j in Code_contents))
  269. fatal(_"expand_one_line: code chunk `%s' used but not defined\n",
  270. chunk_names[i])
  271. if (j in Currently_expanding)
  272. fatal(_"expand_one_line: code chunk `%s' expands itself recursively\n",
  273. chunk_names[i])
  274. else
  275. Currently_expanding[j] = TRUE
  276. }
  277. code_lines = code_body(chunk_names[1])
  278. delete Currently_expanding[strip_out_name(chunk_names[1])]
  279. if (input_line !~ ("^" Chunk_name_pattern)) {
  280. if (parts[1] ~ /^[[:space:]]+$/) {
  281. # insert leading white space on all lines to get indentation
  282. gsub(/^|\n/, ("&" parts[1]), code_lines)
  283. } else {
  284. code_lines = parts[1] code_lines
  285. }
  286. }
  287. for (i = 2; i <= nparts; i++) {
  288. code_lines = code_lines parts[i]
  289. if (i in chunk_names) {
  290. code_lines = code_lines code_body(chunk_names[i])
  291. delete Currently_expanding[strip_out_name(chunk_names[i])]
  292. }
  293. }
  294. if (code_lines ~ Chunk_name_pattern) {
  295. # get any further code chunks, recursively
  296. return do_expand_code_chunks(code_lines)
  297. } else
  298. return code_lines
  299. }
  300. # code_body --- get the code body associated with name
  301. function code_body(name)
  302. {
  303. return Code_contents[strip_out_name(name)]
  304. }
  305. /^@initial_setup[[:space:]]*$/, /^@end initial_setup[[:space:]]*$/ {
  306. if (/^@initial_setup[[:space:]]*$/) {
  307. # start of construct, save line number, skip this line
  308. if ("initial_setup" in Line_numbers)
  309. fatal(_"cannot nest one @initial_setup inside another\n")
  310. Line_numbers["initial_setup"] = (FILENAME ":" FNR)
  311. Initial_setup = ""
  312. } else if (/^@end initial_setup[[:space:]]*$/) {
  313. # end of construct, delete line number, skip this line
  314. delete Line_numbers["initial_setup"]
  315. } else
  316. Initial_setup = Initial_setup "\n" $0
  317. next
  318. }
  319. BEGIN {
  320. # This condition should distinguish between the native and
  321. # MSYS (POSIX-ish) Gawk. We rely on the fact that MSYS mounts
  322. # /tmp and sets $TEMP to point to it, but resets the value
  323. # back to the native C:/Foo/Bar value when invoking native
  324. # programs.
  325. if (ENVIRON["TEMP"] ~ /^[A-Z]:[\\\/]/) {
  326. # MS-Windows recipe
  327. Update_recipe = \
  328. "fc /B \"@DIR@\\@FILE@\" \"@DIR@\\@FILE@@SUFFIX@\" >nul 2>&1 || move /Y \"@DIR@\\@FILE@\" \"@DIR@\\@FILE@@SUFFIX@\" >nul"
  329. } else {
  330. # POSIX recipe
  331. Update_recipe = \
  332. "PATH=/bin:/usr/bin\n" \
  333. "export PATH\n" \
  334. "destname=\"@DIR@/@FILE@\"\n" \
  335. "outname=\"${destname}@SUFFIX@\"\n" \
  336. "if [ ! -f \"$destname\" ]\n" \
  337. "then\n" \
  338. "\tmv \"$outname\" \"$destname\"\n" \
  339. "elif cmp -s \"$outname\" \"$destname\" > /dev/null\n" \
  340. "then\n" \
  341. "\trm \"$outname\"\n" \
  342. "else\n" \
  343. "\tmv \"$outname\" \"$destname\"\n" \
  344. "fi\n"
  345. }
  346. }
  347. /^@file_update_recipe[[:space:]]*$/,
  348. /^@end file_update_recipe[[:space:]]*$/ {
  349. if (/^@file_update_recipe[[:space:]]*$/) {
  350. # start of construct, save line number, skip this line
  351. if ("file_update_recipe" in Line_numbers)
  352. fatal(_"cannot nest one @file_update_recipe inside another\n")
  353. Line_numbers["file_update_recipe"] = (FILENAME ":" FNR)
  354. Update_recipe = ""
  355. } else if (/^@end file_update_recipe[[:space:]]*$/) {
  356. # end of construct, delete line number, skip this line
  357. delete Line_numbers["file_update_recipe"]
  358. } else
  359. Update_recipe = Update_recipe "\n" $0
  360. next
  361. }
  362. BEGIN {
  363. # @default is an unlikely name for a real file
  364. Update_value["@default"]["DIR"] = "." # current directory
  365. Update_value["@default"]["SUFFIX"] = ".tangle_tmp" # unlikely suffix
  366. # No default for filename, jrtangle knows it!
  367. }
  368. /^@file_update[[:space:]]/ {
  369. if (NF != 4)
  370. fatal(_"usage: @file_update name dir suffix\n")
  371. if ($4 == "\"\"")
  372. $4 = ""
  373. Update_value[$2]["DIR"] = $3
  374. Update_value[$2]["SUFFIX"] = $4
  375. next
  376. }
  377. # create_update_recipe --- create update recipe per file,
  378. # return dest filename
  379. function create_update_recipe(file, results,
  380. source_file, dir, suffix, recipe) # locals
  381. {
  382. if (file in Update_value)
  383. source_file = file
  384. else
  385. source_file = "@default"
  386. dir = Update_value[source_file]["DIR"]
  387. suffix = Update_value[source_file]["SUFFIX"]
  388. delete results
  389. recipe = Update_recipe
  390. gsub(/@DIR@/, dir, recipe)
  391. gsub(/@FILE@/, file, recipe)
  392. gsub(/@SUFFIX@/, suffix, recipe)
  393. results["recipe"] = recipe
  394. results["output"] = (dir "/" file suffix)
  395. }
  396. BEGIN {
  397. if (Debug ~ /shell/ && ENVIRON["TEMP"] !~ /^[A-Z]:[\\\/]/)
  398. if (length(Shell_debug) == 0)
  399. Shell_debug = "set -x; "
  400. }
  401. /^@post_create[[:space:]]+/ {
  402. if (NF < 3)
  403. fatal(_"usage: @post_create filename command\n")
  404. check_unfinished()
  405. name = $2
  406. $1 = $2 = ""
  407. $0 = $0
  408. Post_create_commands[name] = $0
  409. }
  410. # dump_chunks --- print out all the chunks
  411. function dump_chunks( i, format)
  412. {
  413. format = "@<%s"
  414. format = format "@>=\n%s@\n\n"
  415. for (i in Code_contents)
  416. printf(format, i, Code_contents[i]) > "/dev/stderr"
  417. }