| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
# cexcerpt <file.c> <dir> |
| 4 |
# |
| 5 |
# Extract tagged code chunks from a C source file for verbatim |
| 6 |
# inclusion in LaTeX documentation. |
| 7 |
# |
| 8 |
# Processes C source file <file.c>; extracts tagged excerpts, |
| 9 |
# and puts them in a file in directory <dir>. |
| 10 |
# |
| 11 |
# An excerpt is marked with special comments in the C file: |
| 12 |
# |
| 13 |
# /*::cexcerpt::my_example::begin::*/ |
| 14 |
# while (esl_sq_Read(sqfp, sq) == eslOK) |
| 15 |
# { n++; } |
| 16 |
# /*::cexcerpt::my_example::end::*/ |
| 17 |
# |
| 18 |
# The tag's format is "::cexcerpt::<tag>::begin::" (or end). |
| 19 |
# We match it with: |
| 20 |
# ^\s*\/\*::cexcerpt::(\S+)::begin::\*\/ |
| 21 |
# |
| 22 |
# The tag is used to construct the file name, as <tag>.tex. |
| 23 |
# In the example, the tag my_example creates a file my_example.tex |
| 24 |
# in <dir>. |
| 25 |
# |
| 26 |
# All the text between the cexcerpt tags is put in the file. |
| 27 |
# In addition, this text is wrapped in a {cchunk} environment. |
| 28 |
# So in the example above, my_example.tex will contain: |
| 29 |
# \begin{cchunk} |
| 30 |
# while (esl_sq_Read(sqfp, sq) == eslOK) |
| 31 |
# { n++; } |
| 32 |
# \end{cchunk} |
| 33 |
# |
| 34 |
# This file can then be included in a LaTeX file, with |
| 35 |
# \input{<dir>/<tag>} |
| 36 |
# |
| 37 |
# For best results, the C source should be free of TAB characters. |
| 38 |
# "M-x untabify" on the region to clean them out. |
| 39 |
# |
| 40 |
# Cexcerpts can't overlap or nest in any way in the C file; only |
| 41 |
# one can be active at any given time. |
| 42 |
# |
| 43 |
# SRE, Fri Feb 25 08:40:19 2005 |
| 44 |
# SVN $Id: cexcerpt 1531 2005-12-13 20:53:46Z eddy $ |
| 45 |
|
| 46 |
|
| 47 |
$usage = "cexcerpt <file.c> <dir>"; |
| 48 |
die("Wrong number of command line arguments.\nUsage: $usage\n") unless ($#ARGV+1 == 2); |
| 49 |
|
| 50 |
$cfile = shift; |
| 51 |
$dir = shift; |
| 52 |
die("C source file $cfile doesn't exist.\n") unless -e $cfile; |
| 53 |
die("C source file $cfile isn't readable.\n") unless -r $cfile; |
| 54 |
die("Directory $dir doesn't exist.\n") unless -e $dir; |
| 55 |
die("$dir isn't a directory.\n") unless -d $dir; |
| 56 |
die("Can't write files to directory $dir.\n") unless -w $dir; |
| 57 |
|
| 58 |
open(CFILE,$cfile) || die("Couldn't open C file $cfile.\n"); |
| 59 |
$in_cexcerpt = 0; |
| 60 |
$linenumber = 1; |
| 61 |
while (<CFILE>) |
| 62 |
{ |
| 63 |
if (/^\s*\/\*::cexcerpt::(\S+)::begin::\*\//) |
| 64 |
{ |
| 65 |
if ($in_cexcerpt) { |
| 66 |
die("Can't start $1 at line $linenumber; $tag started at line $startline.\n"); |
| 67 |
} |
| 68 |
if (($n = grep(/$1/, @taglist)) > 0) { |
| 69 |
die("Already saw tag $1 in this C file ($n); tags must be unique.\n"); |
| 70 |
} |
| 71 |
|
| 72 |
$tag = $1; |
| 73 |
$in_cexcerpt = 1; |
| 74 |
$startline = $linenumber; |
| 75 |
$outfile = "$dir/$tag.tex"; |
| 76 |
push(@taglist, $tag); |
| 77 |
print (" extracting $tag.tex...\n"); |
| 78 |
open(OUTFILE,">$outfile") || die("Couldn't open $outfile for writing."); |
| 79 |
print OUTFILE "\\begin{cchunk}\n"; |
| 80 |
} |
| 81 |
elsif (/^\s*\/\*::cexcerpt::(\S+)::end::\*\//) |
| 82 |
{ |
| 83 |
if (!$in_cexcerpt) { |
| 84 |
die("cexcerpt $1 can't end (line $linenumber) because it never started.\n"); |
| 85 |
} |
| 86 |
if ($tag ne $1) { |
| 87 |
die("tried to end $1 at line $linenumber, but $tag is active (from line $startline).\n"); |
| 88 |
} |
| 89 |
|
| 90 |
$in_cexcerpt = 0; |
| 91 |
print OUTFILE "\\end{cchunk}\n"; |
| 92 |
close(OUTFILE); |
| 93 |
} |
| 94 |
elsif ($in_cexcerpt) |
| 95 |
{ |
| 96 |
print OUTFILE $_; |
| 97 |
} |
| 98 |
$linenumber++; |
| 99 |
} |
| 100 |
close(CFILE); |