home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume08 / lit < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  10.2 KB

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