home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume8 / lit < prev    next >
Text File  |  1989-10-03  |  10KB  |  466 lines

  1. Newsgroups: comp.sources.misc
  2. subject: v08i091: Literate Programming aid
  3. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  4. Reply-To: bba@mtuxo.UUCP (Binayak Banerjee +1 201 957 2109)
  5.  
  6. Posting-number: Volume 8, Issue 91
  7. Submitted-by: bba@mtuxo.UUCP (Binayak Banerjee +1 201 957 2109)
  8. Archive-name: lit
  9.  
  10. #!/bin/sh
  11. # shar:    Shell Archiver  (v1.22)
  12. #
  13. #    Run the following text with /bin/sh to create:
  14. #      README
  15. #      lit.1
  16. #      lit.sh
  17. #
  18. echo "x - extracting README (Text)"
  19. sed 's/^X//' << 'SHAR_EOF' > README &&
  20. XA Request
  21. X---------
  22. XThis  code  is  placed  in   the  public  domain.  The  following
  23. Xcourtesies are requested:
  24. X
  25. Xa. Don't pretend you wrote it.
  26. Xb. If you  modify it, identify your changes. I  want no credit or
  27. X   blame for your code.
  28. Xc. If you extend the functionality  of the code, please send me a
  29. X   copy.
  30. X
  31. XBinayak Banerjee (Mon Oct  2 13:33:45 EDT 1989)
  32. XBinayak_Banerjee@ATT.COM
  33. X------------------------
  34. X
  35. XLit is  my apology  for a literate  programming device.  I really
  36. Xwanted something  similar to cweb,  without having to pay  for it
  37. Xout of  my own pocket. As  no one was  rushing to buy me  cweb, I
  38. Xwrote Lit.
  39. X
  40. XAlthough it is very crude, it is much better than nothing. I have
  41. Xused it for two rather substantial projects -- and it worked very
  42. Xwell.
  43. X
  44. XIt  works best  when used  in an  environment with  the following
  45. Xfacilities:
  46. X
  47. X    1. troff
  48. X    2. vgrind
  49. X    3. Nawk (new awk)
  50. X
  51. XIf  you don't  have these  -- don't  panic. It  will still  work,
  52. Xthough not as nicely.
  53. X
  54. XINSTALLATION:
  55. X
  56. Xa.  Copy lit.sh to lit (or whatever name you wish to use)
  57. X
  58. Xb.  Edit the  definitions at  the top  of lit.sh  make sure  that
  59. XTROFF, VGRIND, and  AWK point to the appropriate  places for you.
  60. XAlso make  sure that TROFFARG  and VGRINDARG are  the appropriate
  61. Xdefault options to troff and VGRIND.
  62. X
  63. XPORTING
  64. X
  65. XHere are some hints on adapting this to your environment:
  66. X
  67. Xa.  No troff
  68. X
  69. X    i.  Have nroff
  70. X
  71. X    Simple -- just set TROFF=nroff in lit.sh
  72. X
  73. X    ii. Some other markup language
  74. X
  75. X    Find  the comments  BEGIN CODE  and END  CODE in  lit.sh.
  76. X    Change the  directives to something appropriate  for your
  77. X    system. set TROFF=your-system as well.
  78. X
  79. Xb.  No vgrind
  80. X
  81. X    Set  VGRIND=/bin/cat. You  won't get  the nice  boldfaced
  82. X    keywords,  but  it will  still  be  much nicer  than  raw
  83. X    listings. You may  also wish to redefine vS and  vE as DS
  84. X    and DE.
  85. X
  86. Xc.  No nawk.
  87. X
  88. X    Change  all occurrences  of  FNR in  lit.sh  to just  NR.
  89. X    Change all  occurrences of FILENAME to  "input file". Set
  90. X    awk  to  point  to  your  old awk.  You  will  lose  some
  91. X    debugging info -- but no real functionality.
  92. SHAR_EOF
  93. chmod 0600 README || echo "restore of README fails"
  94. echo "x - extracting lit.1 (Text)"
  95. sed 's/^X//' << 'SHAR_EOF' > lit.1 &&
  96. X.\" A Request
  97. X.\" ---------
  98. X.\" This  code  is  placed  in   the  public  domain.  The  following
  99. X.\" courtesies are requested:
  100. X.\" 
  101. X.\" a. Don't pretend you wrote it.
  102. X.\" b. If you  modify it, identify your changes. I  want no credit or
  103. X.\"    blame for your code.
  104. X.\" c. If you extend the functionality  of the code, please send me a
  105. X.\"    copy.
  106. X.\" 
  107. X.\" Binayak Banerjee (Mon Oct  2 13:33:45 EDT 1989)
  108. X.\" Binayak_Banerjee@ATT.COM
  109. X.\" ------------------------
  110. X.\" 
  111. X.tr ~
  112. X.TH LIT 1L "Unsupported"
  113. X.UC 4
  114. X.SH NAME
  115. Xlit \- literate programming
  116. X.SH SYNOPSIS
  117. X.B "lit -v"
  118. Xfile ...
  119. X.br
  120. X.B "lit -c"
  121. X[
  122. X.B \-l
  123. X\fIlanguage\fP ] file ...
  124. X.br
  125. X.B "lit -t"
  126. X[
  127. X.B \-p
  128. X\fIpass\fP] [
  129. X.B \-T
  130. X\fItroff-args\fP] [
  131. X.B \-V
  132. X\fIvgrind-args\fP
  133. X] file ...
  134. X.SH DESCRIPTION
  135. X.I Lit
  136. XTakes an input file in a format that interleaves text and code.  It outputs
  137. Xeither the code or the text as requested.
  138. X.PP
  139. XThe
  140. X.B \-v
  141. Xoption checks the input file for correct syntax.  It reports
  142. Xon unbalanced pairs of `.vS' and `.vE'.
  143. X.PP
  144. XThe
  145. X.B \-c
  146. Xoption copies the code into the specied files.  If the
  147. X.B \-l
  148. Xoption is given, then language specific processing is done.
  149. XAt present, only \fB-l\fPC has an effect.  This option adds
  150. X``\fB#line\fP'' directives to the generated code.
  151. X.PP
  152. XThe
  153. X.B \-t
  154. Xoption will process the input files in order to generate the
  155. Xprinted textual representation of the input.  It preprocesses
  156. Xthe input, and then filters it through
  157. X\fIvgrind\fP and \fItroff\fP.  The
  158. X.B \-p
  159. Xoption controls the pipeline. E.g. -p~1 yields the
  160. Xpreprocessed input, -p~2 yields the output of \fIvgrind(1),\fP and -p~3
  161. X(The default) passes it through \fItroff(1)\fP as well.
  162. XArguments to \fItroff\fP and \fIvgrind\fP can be supplied via
  163. Xthe
  164. X.B \-T
  165. Xand the
  166. X.B \-V
  167. Xoptions.  Multipart arguments must be quoted,
  168. XE.g.~\fB-T"-man~-o2-7".\fP  Notice that the leading dash is
  169. Xretained.
  170. X.SH "INPUT FORMAT"
  171. XThe input format consists of \fItroff(1)\fP input.  Segments
  172. Xof embedded code are marked off as follows:
  173. X.sp
  174. X.RS
  175. X.nf
  176. X\&.vS \fBfilename\fP
  177. X\&\fI... Embedded code fragments.\fP
  178. X\&.vE
  179. X.RE
  180. X.fi
  181. X.sp
  182. XThe code fragments between the \fB.vS\fP and \fB.vE\fP are copied
  183. Xto specified file.  If \fBfilename\fP is not specified, the last
  184. Xspecified \fBfilename\fP will be used.
  185. X.SH "MAKING AN INDEX"
  186. XThis facility is due to \fIvgrind,\fP rather than to \fIlit\fP.
  187. XVgrind generates troff input that optionally yields an index
  188. Xof functions.  One can also write a troff function that takes
  189. Xadvantage of this:
  190. X.sp
  191. X.RS
  192. X.nf
  193. X\&.de IX
  194. X\&.if \\\\nx .tm \\\\$1 \\n%
  195. X\&..
  196. X.RE
  197. X.fi
  198. X.sp
  199. XAn item would be entered in the index by adding a line similar to
  200. Xthe following one, anyplace in the text.
  201. X.sp
  202. X.RS
  203. X\&.IX "\fIIndex Item\fP"
  204. X.RE
  205. X.sp
  206. X.PP
  207. XThe index is run off as follows:
  208. X.sp
  209. X.RS
  210. X$ lit -t -T"-mm -rx1" > /dev/null 2> Index
  211. X.br
  212. X$ vgrind Index
  213. X.RE
  214. X.sp
  215. X.SH "SEE ALSO"
  216. Xweb(6), tangle(1), weave(1) from the \fBTeX\fP distribution.
  217. X.br
  218. Xvgrind(1), and troff(1).
  219. X.SH "AUTHOR"
  220. X.RS
  221. XBinayak Banerjee
  222. X.br
  223. XAT&T Bell Labs
  224. X.br
  225. XMiddletown, NJ 07748
  226. X.br
  227. X\fIBinayak_Banerjee@ATT.COM\fP
  228. X.RE
  229. SHAR_EOF
  230. chmod 0700 lit.1 || echo "restore of lit.1 fails"
  231. echo "x - extracting lit.sh (Text)"
  232. sed 's/^X//' << 'SHAR_EOF' > lit.sh &&
  233. X#! /bin/sh
  234. X# A Request
  235. X# ---------
  236. X# This  code  is  placed  in   the  public  domain.  The  following
  237. X# courtesies are requested:
  238. X# 
  239. X# a. Don't pretend you wrote it.
  240. X# b. If you  modify it, identify your changes. I  want no credit or
  241. X#    blame for your code.
  242. X# c. If you extend the functionality  of the code, please send me a
  243. X#    copy.
  244. X# 
  245. X# Binayak Banerjee (Mon Oct  2 13:33:45 EDT 1989)
  246. X# Binayak_Banerjee@ATT.COM
  247. X# ------------------------
  248. X
  249. X
  250. X# Modify these to suit.
  251. X
  252. XTROFF=/usr/bin/troff
  253. XVGRIND=${HOME}/bin/vgrind
  254. XAWK=$TOOLS/bin/nawk
  255. X
  256. X# Default values of arguments
  257. X
  258. XPASS=3            # Default for -p
  259. XTROFFARG="-mm -Tpost"    # Default for -T
  260. XVGRINDARG="-lc"        # Default for -V
  261. X
  262. X# Temp files
  263. Xtmpawk=/tmp/lita$$
  264. Xwhat=
  265. X
  266. X# Usage Info
  267. X
  268. XU1="-c [-l language] file ..."
  269. XU2="-t [-T troff-args] [-V vgrind-args] [-p 1|2|3] file ..."
  270. XU3="-v file ..."
  271. XUSAGE="USAGE:\n\t$0 $U1\n\t$0 $U2\n\t$0 $U3"
  272. X
  273. Xtrap "rm -f $tmpawk" 0
  274. X
  275. Xif [ $# -lt 2 ]; then
  276. X    echo "$USAGE"
  277. X    exit 2
  278. Xfi
  279. X
  280. Xif [ "$1" = "-c" ]; then
  281. Xcat <<'Stinky' > $tmpawk
  282. XBEGIN {
  283. X    state = "ignore"
  284. X}
  285. X
  286. X/^\.vS/ {
  287. X    if ($2 == "") {
  288. X        if (outfile == "") {
  289. X            print "# Didn't supply filename"
  290. X            printf("echo 'ERROR(%s,%d):No filename given' >&2\n",FILENAME,FNR)
  291. X            outfile="/dev/null"
  292. X            isseen["/dev/null"] = "yes"
  293. X        }
  294. X    } else outfile = $2
  295. X
  296. X    if (isseen[outfile] == "yes") {
  297. X        print "# Appending to",outfile
  298. X        printf "echo '*%s(%d)\\c'\n", FILENAME,NR
  299. X        print "cat - << 'END-OF-INPUT' >>",outfile
  300. X        if (lang ~ /^[cC]$/) {
  301. X            printf "# line %d \"%s\"\n",FNR+1,FILENAME
  302. X        }
  303. X    } else {
  304. X        isseen[outfile] = "yes"
  305. X        print "# Dumping to",outfile
  306. X        printf "echo '*%s(%d)\\c'\n", FILENAME,NR
  307. X        print "cat - << 'END-OF-INPUT' >",outfile
  308. X        if (lang ~ /^[cC]$/) {
  309. X            printf "# line %d \"%s\"\n",FNR+1,FILENAME
  310. X        }
  311. X    }
  312. X    state = "echo"
  313. X    next
  314. X}
  315. X
  316. X/^\.vE/ {
  317. X    state = "ignore"
  318. X    print "END-OF-INPUT"
  319. X    next
  320. X}
  321. X
  322. Xstate == "echo" {
  323. X    print
  324. X    next
  325. X}
  326. X
  327. X{ next }
  328. XEND { print "echo" }
  329. XStinky
  330. X    what="code"
  331. Xelif [ "$1" = "-t" ]; then
  332. Xcat <<'Stinky' > $tmpawk
  333. X$1 == ".vS" {
  334. X        # BEGIN CODE
  335. X        print ".sp"
  336. X        print ".nf"
  337. X        print $0
  338. X        next
  339. X}
  340. X$1 == ".vE" {
  341. X        # END CODE
  342. X        print $0
  343. X        print ".sp"
  344. X        print ".fi"
  345. X        next
  346. X}
  347. X{ print $0 }
  348. XStinky
  349. X    what="text"
  350. Xelif [ "$1" = "-v" ]; then
  351. Xcat <<'Stinky' > $tmpawk
  352. XBEGIN {
  353. X    print "Verifying...."
  354. X    stktop = 0;
  355. X}
  356. X/^\.vS/ {
  357. X        file[++stktop] = FILENAME
  358. X        linenum[stktop] = FNR
  359. X}
  360. X/^\.vE/ {
  361. X        if (stktop <= 0) {
  362. X            printf("vE with no vS in %s, line %d\n",FILENAME,FNR)
  363. X            stktop = 0
  364. X        } else {
  365. X            stktop--
  366. X        }
  367. X}
  368. X{ next }
  369. XEND {
  370. X    if (stktop > 0) {
  371. X        print "vS without corresponding vE"
  372. X        while (stktop > 0) {
  373. X            print file[stktop],linenum[stktop]
  374. X            stktop--
  375. X        }
  376. X    }
  377. X}
  378. XStinky
  379. X    # This is easy enough to do here
  380. X    shift
  381. X    $AWK -f $tmpawk $*
  382. X    exit
  383. Xelse
  384. X    echo "$USAGE"
  385. X    exit 2
  386. Xfi
  387. X
  388. X# DEBUGGING
  389. X# cp $tmpawk AWK.TMP
  390. X
  391. X# Parse arguments
  392. X
  393. X# Getopt is dumb.  Do it by hand.
  394. X# set -- `getopt "l:T:V:p:" $*`
  395. X
  396. X# if [ $? != 0 ]
  397. X#     then
  398. X#     echo $USAGE
  399. X#     exit 2
  400. X# fi
  401. X
  402. Xshift
  403. Xwhile [ -n "$1" ]; do
  404. X    case $1 in
  405. X    -l*)
  406. X        LANG=`expr "$1" : '-l\(.*\)' `
  407. X        if [ "$?" != 0 ]; then
  408. X            LANG=$2; shift
  409. X        fi
  410. X        shift;;
  411. X    -p*)
  412. X        PASS=`expr "$1" : '-p\(.*\)' `
  413. X        if [ "$?" != 0 ]; then
  414. X            PASS=$2; shift
  415. X        fi
  416. X        shift;;
  417. X    -T*)
  418. X        TROFFARG=`expr "$1" : '-T\(.*\)' `
  419. X        if [ "$?" != 0 ]; then
  420. X            TROFFARG=$2; shift
  421. X        fi
  422. X        shift;;
  423. X    -V*)
  424. X        VGRINDARG=`expr "$1" : '-T\(.*\)' `
  425. X        if [ "$?" != 0 ]; then
  426. X            VGRINDARG=$2; shift
  427. X        fi
  428. X        shift;;
  429. X     -*)
  430. X        echo "$USAGE"
  431. X        exit 1;;
  432. X      *)
  433. X        break;;
  434. X    esac
  435. Xdone
  436. X
  437. Xtroff="$TROFF $TROFFARG"
  438. Xvgrind="$VGRIND -ft $VGRINDARG -"
  439. X
  440. X# Now handle case where text is desired.
  441. X
  442. Xif [ "$what" = "text" ]; then
  443. X    case "$PASS" in
  444. X        1) troff="/bin/cat";vgrind="/bin/cat";;
  445. X        2) troff="/bin/cat";;
  446. X        *) : ;;
  447. X    esac
  448. X
  449. X    $AWK -f "$tmpawk" $* |
  450. X            $vgrind |
  451. X            $troff
  452. X
  453. Xelse
  454. X    if [ "$LANG" ]; then
  455. X        LANG="lang=$LANG"
  456. X    fi
  457. X
  458. X    $AWK -f "$tmpawk" $LANG $* | /bin/sh
  459. Xfi
  460. X
  461. Xexit 0
  462. SHAR_EOF
  463. chmod 0755 lit.sh || echo "restore of lit.sh fails"
  464. exit 0
  465.  
  466.