protex 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. #!/usr/bin/perl
  2. #BOP
  3. #
  4. # !ROUTINE: ProTeX v. 2.00 - Translates DAO Prologues to LaTeX
  5. #
  6. # !INTERFACE:
  7. # protex [-hbACFS] ] [+-nlsxf] [src_file(s)]
  8. #
  9. # !DESCRIPTION:
  10. # Perl filter to produce a \LaTeX compatible document
  11. # from a DAO Fortran source code with standard Pro\TeX
  12. # prologues. If source files are not specified it
  13. # reads from stdin; output is always to stdout.
  14. #
  15. # \noindent
  16. # {\bf Command Line Switches:} \vspace{0.2cm}
  17. #
  18. # \begin{center}
  19. # \begin{tabular}{|c|l|} \hline \hline
  20. # -h & Help mode: list command line options \\ \hline
  21. # -b & Bare mode, meaning no preamble, etc. \\ \hline
  22. # +/-n & New Page for each subsection (wastes paper) \\ \hline
  23. # +/-l & Listing mode, default is prologues only \\ \hline
  24. # +/-s & Shut-up mode, i.e., ignore any code from BOC to EOC \\ \hline
  25. # +/-x & No LaTeX mode, i.e., put !DESCRIPTION: in verbatim mode \\ \hline
  26. # +/-f & No source file info \\ \hline
  27. # -A & Ada code \\ \hline
  28. # -C & C++ code \\ \hline
  29. # -F & F90 code (default) \\ \hline
  30. # -S & Shell script \\ \hline \hline
  31. # \end{tabular}
  32. # \end{center}
  33. #
  34. # The options can appear in any order. The options, -h and -b, affect
  35. # the input from all files listed on command-line input. Each of the
  36. # remaining options effects only the input from the files listed after
  37. # the option and prior to any overriding option. The plus sign
  38. # turns off the option. For example, the command-line input,
  39. # \bv
  40. # protex -bnS File1 -F File2.f +n File3.f
  41. # \ev
  42. # will cause the option, {\tt -n} to affect the input from the files,
  43. # {\tt File} and {\tt File2.f}, but not from {\tt File3.f}. The
  44. # {\tt -S} option is implemented for {\tt File1} but is overridden by
  45. # the {\tt -F} for files {\tt File2.f} and {\tt File3.f}.
  46. #
  47. #
  48. # !SEE ALSO:
  49. # For a more detailed description of ProTeX functionality,
  50. # DAO Prologue and other conventions, consult:
  51. #
  52. # Sawyer, W., and A. da Silva, 1997: ProTeX: A Sample
  53. # Fortran 90 Source Code Documentation System.
  54. # DAO Office Note 97-11
  55. #
  56. #
  57. # !REVISION HISTORY:
  58. #
  59. # 20Dec1995 da Silva First experimental version
  60. # 10Nov1996 da Silva First internal release (v1.01)
  61. # 28Jun1997 da Silva Modified so that !DESCRIPTION can appear after
  62. # !INTERFACE, and !INPUT PARAMETERS etc. changed to italics.
  63. # 02Jul1997 Sawyer Added shut-up mode
  64. # 20Oct1997 Sawyer Added support for shell scripts
  65. # 11Mar1998 Sawyer Added: file name, date in header, C, script support
  66. # 05Aug1998 Sawyer Fixed LPChang-bug-support-for-files-with-underscores
  67. # 10Oct1998 da Silva Introduced -f option for removing source file info
  68. # from subsection, etc. Added help (WS).
  69. # 06Dec1999 C. Redder Added LaTeX command "\label{sec:prologues}" just
  70. # after the beginning of the proglogue section.
  71. # 13Dec1999 C. Redder Increased flexbility in command-line
  72. # interface. The options can appear in any
  73. # order which will allow the user to implement
  74. # options for select files.
  75. # 01Feb1999 C. Redder Added \usepackage commands to preamble of latex
  76. # document to include the packages amsmath, epsfig
  77. # and hangcaption.
  78. # 10May2000 C. Redder Revised LaTeX command "\label{sec:prologues}"
  79. # to "\label{app:ProLogues}"
  80. # 24May2001 da Silva Added !PARAMETERS/!REURN VALUE: keywords for CAM.
  81. #
  82. #EOP
  83. #----------------------------------------------------------------------------
  84. # Keep this if you don't know what it does...
  85. # -------------------------------------------
  86. $[ = 1; # set array base to 1
  87. $, = ' '; # set output field separator
  88. $\ = "\n"; # set output record separator
  89. # Set valid options lists
  90. # -----------------------
  91. $GlobOptions = 'hb'; # Global options (i.e for all files)
  92. $LangOptions = 'ACFS'; # Options for setting programming languages
  93. $SwOptions = 'flnsx'; # Options that can change for each input
  94. # file
  95. $RegOptions = "$GlobOptions$LangOptions";
  96. # Scan for global options until first first
  97. # file is processed.
  98. # Scan for global options
  99. # -----------------------
  100. $NFiles = 0;
  101. Arg:
  102. foreach $arg (@ARGV) {
  103. $option = &CheckOpts ( $arg, $RegOptions, $SwOptions ) + 1;
  104. if ( $option ) {
  105. $rc = &GetOpts ( $arg, $GlobOptions );
  106. next Arg; }
  107. else { $NFiles++;
  108. }# end if
  109. }# end foreach
  110. # If all inut arguments are options, then assume the
  111. # filename, "-", for the standard input
  112. # --------------------------------------------------
  113. if ( $NFiles == 0 ) { push (@ARGV, "-"); }
  114. # Implement help option
  115. # ---------------------
  116. if ( $opt_h ) {
  117. &print_help();
  118. exit();
  119. }#end if
  120. # Optional Prologue Keywords
  121. # --------------------------
  122. @keys = ( "!INTERFACE:",
  123. "!USES:",
  124. "!PUBLIC TYPES:",
  125. "!PUBLIC MEMBER FUNCTIONS:",
  126. "!PUBLIC DATA MEMBERS:",
  127. "!DEFINED PARAMETERS:",
  128. "!PARAMETERS:",
  129. "!INPUT PARAMETERS:",
  130. "!INPUT/OUTPUT PARAMETERS:",
  131. "!OUTPUT PARAMETERS:",
  132. "!RETURN VALUE:",
  133. "!REVISION HISTORY:",
  134. "!BUGS:",
  135. "!SEE ALSO:",
  136. "!SYSTEM ROUTINES:",
  137. "!FILES USED:",
  138. "!REMARKS:",
  139. "!TO DO:",
  140. "!CALLING SEQUENCE:",
  141. "!AUTHOR:",
  142. "!CALLED FROM:",
  143. "!LOCAL VARIABLES:" );
  144. # Initialize these for clarity
  145. # ----------------------------
  146. $intro = 0; # doing introduction?
  147. $prologue = 0; # doing prologue?
  148. $first = 1; # first prologue?
  149. $source = 0; # source code mode?
  150. $verb = 0; # verbatim mode?
  151. $tpage = 0; # title page?
  152. $begdoc = 0; # has \begin{document} been written?
  153. # Initial LaTeX stuff
  154. # -------------------
  155. &print_notice();
  156. &print_preamble(); # \documentclass, text dimensions, etc.
  157. &print_macros(); # short-hand LaTeX macros
  158. # Main loop -- for each command-line argument
  159. # -------------------------------------------
  160. ARG:
  161. foreach $arg (@ARGV) {
  162. # Scan for non-global command-line options
  163. # ----------------------------------------
  164. $option = &CheckOpts ( $arg, $RegOptions, $SwOptions, "quiet" ) + 1;
  165. if ( $option ) {
  166. &GetOpts ( $arg, $SwOptions );
  167. &SetOpt ( $arg, $LangOptions );
  168. next ARG;
  169. }# end if
  170. # Determine the type of code, set corresponding search strings
  171. # ------------------------------------------------------------
  172. # if ( $opt_F ) { # FORTRAN
  173. $comment_string = '!'; # -------
  174. $boi_string = '!BOI';
  175. $eoi_string = '!EOI';
  176. $bop_string = '!BOP';
  177. $eop_string = '!EOP';
  178. $boc_string = '!BOC';
  179. $eoc_string = '!EOC';
  180. #}# end if
  181. if ( $opt_A ) { # ADA
  182. $comment_string = '--'; # ---
  183. $boi_string = '--BOI';
  184. $eoi_string = '--EOI';
  185. $bop_string = '--BOP';
  186. $eop_string = '--EOP';
  187. $boc_string = '--BOC';
  188. $eoc_string = '--EOC';
  189. }# end if
  190. if ( $opt_C ) {
  191. $comment_string = '//'; # C
  192. $boi_string = '//BOI'; # -
  193. $eoi_string = '//EOI';
  194. $bop_string = '//BOP';
  195. $eop_string = '//EOP';
  196. $boc_string = '//BOC';
  197. $eoc_string = '//EOC';
  198. }# end if
  199. if ( $opt_S ) { # Script
  200. $comment_string = '#'; # ------
  201. $boi_string = '#BOI';
  202. $eoi_string = '#EOI';
  203. $bop_string = '#BOP';
  204. $eop_string = '#EOP';
  205. $boc_string = '#BOC';
  206. $eoc_string = '#EOC';
  207. }# end if
  208. # Set file name parameters
  209. # ------------------------
  210. $InputFile = $arg;
  211. @all_path_components = split( /\//, $InputFile );
  212. $FileBaseName = pop ( @all_path_components );
  213. $FileBaseName =~ s/_/\\_/g;
  214. if ( $InputFile eq "-" ) {$FileBaseName = "Standard Input";}
  215. # Set date
  216. # --------
  217. $Date = `date`;
  218. # Open current file
  219. # -----------------
  220. open ( InputFile, "$InputFile" )
  221. or print STDERR "Unable to open $InputFile: $!";
  222. # Print page header
  223. # -----------------
  224. printf "\n\\markboth{Left}{Source File: %s, Date: %s}\n\n",
  225. $FileBaseName, $Date;
  226. LINE:
  227. # Inner loop --- for processing each line of the input file
  228. # ---------------------------------------------------------
  229. while ( <InputFile> ) {
  230. chop; # strip record separator
  231. @Fld = split(' ', $_, 9999);
  232. # Straight quote
  233. # --------------
  234. if ($Fld[1] eq '!QUOTE:') {
  235. for ($i = 2; $i <= $#Fld; $i++) {
  236. printf '%s ', $Fld[$i];
  237. }# end for
  238. print " ";
  239. next LINE;
  240. }# end if
  241. # Handle optional Title Page and Introduction
  242. # -------------------------------------------
  243. if ($Fld[1] eq $boi_string) {
  244. print ' ';
  245. $intro = 1;
  246. next LINE;
  247. }# end if
  248. if ($Fld[2] eq '!TITLE:') {
  249. if ( $intro ) {
  250. shift @Fld;
  251. shift @Fld;
  252. @title = @Fld;
  253. $tpage = 1;
  254. next LINE;
  255. }# end if
  256. }# end if
  257. if ($Fld[2] eq '!AUTHORS:') {
  258. if ( $intro ) {
  259. shift @Fld;
  260. shift @Fld;
  261. @author = @Fld;
  262. $tpage = 1;
  263. next LINE;
  264. }# end if
  265. }# end if
  266. if ($Fld[2] eq '!AFFILIATION:') {
  267. if ( $intro ) {
  268. shift @Fld;
  269. shift @Fld;
  270. @affiliation = @Fld;
  271. $tpage = 1;
  272. next LINE;
  273. }# end if
  274. }# end if
  275. if ($Fld[2] eq '!DATE:') {
  276. if ( $intro ) {
  277. shift @Fld;
  278. shift @Fld;
  279. @date = @Fld;
  280. $tpage = 1;
  281. next LINE;
  282. }# end if
  283. }# end if
  284. if ($Fld[2] eq '!INTRODUCTION:') {
  285. if ( $intro ) {
  286. &do_beg();
  287. print ' ';
  288. print '%..............................................';
  289. shift @Fld;
  290. shift @Fld;
  291. print "\\section{@Fld}";
  292. next LINE;
  293. }# end if
  294. }# end if
  295. # End of introduction
  296. # -------------------
  297. if ($Fld[1] eq $eoi_string) {
  298. print ' ';
  299. print '%/////////////////////////////////////////////////////////////';
  300. print "\\newpage";
  301. $intro = 0;
  302. next LINE;
  303. }# end if
  304. # Beginning of prologue
  305. # ---------------------
  306. if ($Fld[1] eq $bop_string) {
  307. if ( $source ) { &do_eoc(); }
  308. print ' ';
  309. print '%/////////////////////////////////////////////////////////////';
  310. &do_beg();
  311. if ($first == 0) {
  312. ### print "\\newpage";
  313. print " ";
  314. print "\\mbox{}\\hrulefill\\ ";
  315. print " ";}
  316. else {
  317. unless($opt_b){print "\\section{Routine/Function Prologues} \\label{app:ProLogues}";}
  318. }# end if
  319. $first = 0;
  320. $prologue = 1;
  321. $verb = 0;
  322. $source = 0;
  323. &set_missing(); # no required keyword yet
  324. next LINE;
  325. }# end if
  326. # A new subroutine/function
  327. # -------------------------
  328. if ($Fld[2] eq '!ROUTINE:' ) {
  329. if ($prologue) {
  330. shift @Fld;
  331. shift @Fld;
  332. $_ = join(' ', @Fld);
  333. $name_is = $_;
  334. s/_/\\_/g; # Replace "_" with "\_"
  335. if ( $opt_n && $not_first ) { printf "\\newpage\n"; }
  336. unless ($opt_f) {printf "\\subsection{%s (Source File: %s)}\n\n", $_, $FileBaseName;}
  337. else {printf "\\subsection{%s }\n\n", $_;}
  338. $have_name = 1;
  339. $not_first = 1;
  340. next LINE;
  341. }# end if
  342. }# end if
  343. # A new Module
  344. # ------------
  345. if ($Fld[2] eq '!MODULE:' ) {
  346. if ($prologue) {
  347. shift @Fld;
  348. shift @Fld;
  349. $_ = join(' ', @Fld);
  350. $name_is = $_;
  351. s/_/\\_/g; # Replace "_" with "\_"
  352. if ( $opt_n && $not_first ) { printf "\\newpage\n"; }
  353. unless($opt_f) {printf "\\subsection{Module %s (Source File: %s)}\n\n", $_, $FileBaseName;}
  354. else {printf "\\subsection{Module %s }\n\n", $_;}
  355. $have_name = 1;
  356. $have_intf = 1; # fake it, it does not need one.
  357. $not_first = 1;
  358. next LINE;
  359. }# end if
  360. }# end if
  361. # A new include file
  362. # ------------------
  363. if ($Fld[2] eq '!INCLUDE:' ) {
  364. if ($prologue) {
  365. shift @Fld;
  366. shift @Fld;
  367. $_ = join(' ', @Fld);
  368. $name_is = $_;
  369. s/_/\\_/g; # Replace "_" with "\_"
  370. if ( $opt_n && $not_first ) { printf "\\newpage\n"; }
  371. unless($opt_f) {printf "\\subsection{Include File %s (Source File: %s)}\n\n", $_, $FileBaseName;}
  372. else {printf "\\subsection{Include File %s }\n\n", $_;}
  373. $have_name = 1;
  374. $have_intf = 1; # fake it, it does not need one.
  375. $not_first = 1;
  376. next LINE;
  377. }# end if
  378. }# end if
  379. # A new INTERNAL subroutine/function
  380. # ----------------------------------
  381. if ($Fld[2] eq '!IROUTINE:') { # Internal routine
  382. if ($prologue) {
  383. shift @Fld;
  384. shift @Fld;
  385. $_ = join(' ', @Fld);
  386. $name_is = $_;
  387. s/_/\\_/g; # Replace "_" with "\_"
  388. printf "\\subsubsection{%s}\n\n", $_;
  389. $have_name = 1;
  390. next LINE;
  391. }# end if
  392. }# end if
  393. # Description: what follows will be regular LaTeX (no verbatim)
  394. # -------------------------------------------------------------
  395. if (/!DESCRIPTION:/) {
  396. if ($prologue) {
  397. if ($verb) {
  398. printf "\\end{verbatim}";
  399. printf "\n{\\sf DESCRIPTION:\\\\ }\n\n";
  400. $verb = 0; }
  401. else { # probably never occurs
  402. }# end if
  403. if ($opt_x) {
  404. printf "\\begin{verbatim} ";
  405. $verb = 1;
  406. $first_verb = 1; }
  407. else {
  408. for ($i = 3; $i <= $#Fld; $i++) {
  409. printf '%s ', $Fld[$i];
  410. }# end for
  411. }# end if
  412. ### print " ";
  413. $have_desc = 1;
  414. next LINE;
  415. }# end if
  416. }# end if
  417. # Handle optional keywords (these will appear as verbatim)
  418. # --------------------------------------------------------
  419. if ($prologue) {
  420. KEY: foreach $key ( @keys ) {
  421. if ( /$key/ ) {
  422. if ($verb) {
  423. printf "\\end{verbatim}";
  424. $verb = 0; }
  425. else {
  426. printf "\n\\bigskip";
  427. }# end if
  428. $k = sprintf('%s', $key);
  429. $ln = length($k);
  430. ###printf "\\subsubsection*{%s}\n", substr($k, 2, $ln - 1);
  431. ###printf "{\\Large \\em %s}\n", ucfirst lc substr($k, 2, $ln - 1);
  432. $_ = $key;
  433. if( /USES/ || /INPUT/ || /OUTPUT/ || /PARAMETERS/ || /VALUE/ ) {
  434. printf "{\\em %s}\n", substr($k, 2, $ln - 1); } # italics
  435. else {
  436. printf "{\\sf %s}\n", substr($k, 2, $ln - 1); # san serif
  437. }# end if
  438. printf "\\begin{verbatim} ";
  439. $verb = 1;
  440. $first_verb = 1;
  441. if ( $key eq "!INTERFACE:" ) { $have_intf = 1; }
  442. if ( $key eq "!CALLING SEQUENCE:" ) { $have_intf = 1; }
  443. if ( $key eq "!REVISION HISTORY:" ) { $have_hist = 1; }
  444. next LINE;
  445. }# end if
  446. }# end foreach
  447. }# end if
  448. # End of prologue
  449. # ---------------
  450. if ($Fld[1] eq $eop_string) {
  451. if ($verb) {
  452. print "\\end{verbatim}";
  453. $verb = 0;
  454. }# end if
  455. $prologue = 0;
  456. &check_if_all_there(); # check if all required keyword are there.
  457. if ( $opt_l ) {
  458. $Fld[1] = $boc_string;}
  459. else { next LINE; }
  460. }# end if
  461. unless ( $opt_s ) {
  462. #
  463. # Beginning of source code section
  464. # --------------------------------
  465. if ($Fld[1] eq $boc_string) {
  466. print ' ';
  467. print '%/////////////////////////////////////////////////////////////';
  468. $first = 0;
  469. $prologue = 0;
  470. $source = 1;
  471. ### printf "\\subsubsection*{CONTENTS:}\n\n", $Fld[3];
  472. printf "{\\sf CONTENTS:}";
  473. printf "\n \\begin{verbatim}\n";
  474. $verb = 1;
  475. next LINE;
  476. }# end if
  477. # End of source code
  478. # ------------------
  479. if ($Fld[1] eq $eoc_string) {
  480. &do_eoc();
  481. $prologue = 0;
  482. next LINE;
  483. }# end if
  484. }# end unless
  485. # Prologue or Introduction, print regular line (except for !)
  486. # -----------------------------------------------------------
  487. if ($prologue||$intro) {
  488. if ( $verb && $#Fld == 1 && ( $Fld[1] eq $comment_string ) ) {
  489. next LINE; # to eliminate excessive blanks
  490. }# end if
  491. if ( $Fld[2] eq "\\ev" ) { # special handling
  492. $_ = $comment_string . " \\end{verbatim}";
  493. }# end if
  494. s/^$comment_string/ /; # replace comment string with blank
  495. # $line = sprintf('%s', $_); # not necessary -- comment str is absent
  496. # $ln = length($line); # not necessary -- comment str is absent
  497. unless ( $first_verb ) { printf "\n "; }
  498. printf '%s', $_;
  499. # printf '%s', substr($line, 1, $ln - 1); # comment str is absent
  500. $first_verb = 0;
  501. next LINE;
  502. }# end if
  503. # Source code: print the full line
  504. # --------------------------------
  505. if ($source) {
  506. print $_;
  507. next LINE;
  508. }# end if
  509. }# end inner loop for processing each line of the input file
  510. # ---------------------------------------------------------
  511. }# end main loop for each command-line argument
  512. # --------------------------------------------
  513. print $_;
  514. if ( $source ) { &do_eoc(); }
  515. print '%...............................................................';
  516. unless ( $opt_b ) {
  517. print "\\end{document}";
  518. }#end unless
  519. #----------------------------------------------------------------------
  520. sub CheckOpts
  521. # Checks options against a given list. Outputs error message
  522. # for any invalid option.
  523. #
  524. # Usage:
  525. # $rc = &CheckOpts ( options, valid_reg_options,
  526. # valid_sw_options,
  527. # quiet_mode )
  528. #
  529. # character: options - options to be checked. (e.g. -df+x) The
  530. # list must begin with a positive or
  531. # negative sign. If no sign appears at the
  532. # beginning or by itself, then the argument
  533. # is not recognized as a list of options.
  534. # character: valid_reg_options - list of valid regular options.
  535. # (i.e. options that are associated only
  536. # eith negative sign.)
  537. # character: valid_sw_options - list of valid switch options.
  538. # (i.e. options that can be associated with
  539. # either a positive or negative sign.
  540. # logical: quiet mode (optional) If true then print no error
  541. # messages.
  542. # integer: rc - return code
  543. # = -1 if the arguement, options, is
  544. # not recognized as a list of options
  545. # = 0 if all options are valid.
  546. # > 0 for the number of invalid options.
  547. #
  548. { local($options,
  549. $valid_reg_options,
  550. $valid_sw_options,
  551. $quiet_mode ) = @_;
  552. if ( $options eq "+" ||
  553. $options eq "-" ) {return -1}
  554. local(@Options) = split( / */, $options );
  555. if ( $Options[ $[ ] ne "-" &&
  556. $Options[ $[ ] ne "+" ) {return -1;}
  557. local($option, $option_sign, $valid_list, $pos);
  558. local($errs) = 0;
  559. foreach $option ( @Options ) {
  560. if ( $option eq "-" ||
  561. $option eq "+" ) {$option_sign = $option;}
  562. else {
  563. if ( $option_sign eq "-" )
  564. { $valid_list = $valid_reg_options
  565. . $valid_sw_options; }
  566. else
  567. { $valid_list = $valid_sw_options; }
  568. $pos = index ($valid_list,$option);
  569. if ( $pos < $[ &&
  570. $quiet_mode ) {
  571. $errs++;
  572. print STDERR "Invalid option: $option_sign$option \n";
  573. }# end if
  574. }# end if
  575. }# end foreach
  576. return $errs;
  577. }#end sub GetOpts
  578. sub GetOpts
  579. # Gets options. If an option is valid, then opt_[option] is
  580. # set to 0 or 1 as a side effect if the option is preceeded by
  581. # a positive or negative sign.
  582. #
  583. # Usage:
  584. # $rc = &GetOpts ( options, valid_options )
  585. #
  586. # character: options - options to be checked. (e.g. -df+x) The
  587. # list must begin with a positive or
  588. # negative sign. If no sign appears at the
  589. # beginning or by itself, then the argument
  590. # is not recognized as a list of options.
  591. # character: valid_options - list of valid options (e.g. dfhx)
  592. # integer: rc - return code
  593. # = -1 if the arguement, options, is
  594. # not recognized as a list of options.
  595. # = 0 otherwise
  596. #
  597. { local($options,$valid_options) = @_;
  598. if ( $options eq "+" ||
  599. $options eq "-" ) {return -1}
  600. local(@Options) = split( / */, $options );
  601. if ( $Options[ $[ ] ne "-" &&
  602. $Options[ $[ ] ne "+" ) {return -1;}
  603. local($option, $option_sign);
  604. foreach $option ( @Options ) {
  605. if ( $option eq "-" ||
  606. $option eq "+" ) {
  607. $option_sign = $option; }
  608. else {
  609. if ( index ($valid_options,$option) >= $[ ) {
  610. if ( $option_sign eq "-" ) {${"opt_$option"} = 1;}
  611. if ( $option_sign eq "+" ) {${"opt_$option"} = 0;};
  612. }# end if
  613. }# end if
  614. }# end foreach
  615. return 0;
  616. }#end sub GetOpts
  617. sub SetOpt
  618. # Sets option flags. For the last input option that is in a
  619. # list, the flag opt_[option] is set to 1 as a side effect.
  620. # For all other options in the list, opt_[option] is set to 0.
  621. #
  622. # Usage:
  623. # $rc = &SetOpt ( options, valid_options )
  624. #
  625. # character: options - options to be checked. (e.g. -df+x) The
  626. # list must begin with a positive or
  627. # negative sign. If no sign appears at the
  628. # beginning or by itself, then the argument
  629. # is not recognized as a list of options.
  630. # character: valid_options - list of valid options (e.g. def )
  631. # integer: rc - return code
  632. # = -1 if the arguement, options, is
  633. # not recognized as a list of options.
  634. # = 0 otherwise
  635. # Note: For the examples provided for the input arguments,
  636. # $opt_d = 0, $opt_e = 0, and $opt_f = 1, since the
  637. # input option, -f, was the last in the argument,
  638. # option.
  639. #
  640. { local($options,$valid_options) = @_;
  641. if ( $options eq "+" ||
  642. $options eq "-" ) {return -1}
  643. local(@Options) = split( / */, $options );
  644. local(@ValidOptions) = split( / */, $valid_options );
  645. if ( $Options[ $[ ] ne "-" &&
  646. $Options[ $[ ] ne "+" ) {return -1;}
  647. local($option, $option_sign);
  648. foreach $option ( @Options ) {
  649. if ( $option ne "-" &&
  650. $option ne "+" ) {
  651. if ( index ($valid_options,$option) >= $[ ) {
  652. foreach $valid_option (@ValidOptions ) {
  653. ${"opt_$valid_option"} = 0;
  654. }# end foreach
  655. ${"opt_$option"} = 1;
  656. }# end if
  657. }# end if
  658. }# end foreach
  659. return 0;
  660. }#end sub SetOpt
  661. sub print_help {
  662. print "Usage: protex [-hbACFS] [+-nlsxf] [src_file(s)]";
  663. print " ";
  664. print " Options:";
  665. print " -h Help mode: list command line options";
  666. print " -b Bare mode, meaning no preamble, etc.";
  667. print " +-n New Page for each subsection (wastes paper)";
  668. print " +-l Listing mode, default is prologues only";
  669. print " +-s Shut-up mode, i.e., ignore any code from BOC to EOC";
  670. print " +-x No LaTeX mode, i.e., put !DESCRIPTION: in verbatim mode";
  671. print " +-f No source file info";
  672. print " -A Ada code";
  673. print " -C C++ code";
  674. print " -F F90 code";
  675. print " -S Shell script";
  676. print " ";
  677. print " The options can appear in any order. The options, -h and -b,";
  678. print " affect the input from all files listed on command-line input.";
  679. print " Each of the remaining options effects only the input from the";
  680. print " files listed after the option and prior to any overriding";
  681. print " option. The plus sign turns off the option.";
  682. }# end sub print_help
  683. sub print_notice {
  684. print "% **** IMPORTANT NOTICE *****" ;
  685. print "% This LaTeX file has been automatically produced by ProTeX v. 1.1";
  686. print "% Any changes made to this file will likely be lost next time";
  687. print "% this file is regenerated from its source. Send questions ";
  688. print "% to Arlindo da Silva, dasilva\@gsfc.nasa.gov";
  689. print " ";
  690. }# sub print_notice
  691. sub print_preamble {
  692. unless ( $opt_b ) {
  693. print "%------------------------ PREAMBLE --------------------------";
  694. print "\\documentclass[11pt]{article}";
  695. print "\\usepackage{amsmath}";
  696. print "\\usepackage{epsfig}";
  697. print "\\usepackage{hangcaption}";
  698. print "\\textheight 9in";
  699. print "\\topmargin 0pt";
  700. print "\\headsep 1cm";
  701. print "\\headheight 0pt";
  702. print "\\textwidth 6in";
  703. print "\\oddsidemargin 0in";
  704. print "\\evensidemargin 0in";
  705. print "\\marginparpush 0pt";
  706. print "\\pagestyle{myheadings}";
  707. print "\\markboth{}{}";
  708. print "%-------------------------------------------------------------";
  709. }#end unless
  710. print "\\parskip 0pt";
  711. print "\\parindent 0pt";
  712. print "\\baselineskip 11pt";
  713. }# end sub print_preamble
  714. sub print_macros {
  715. print " ";
  716. print "%--------------------- SHORT-HAND MACROS ----------------------";
  717. print "\\def\\bv{\\begin{verbatim}}";
  718. print "\\def\\ev\{\\end\{verbatim}}";
  719. print "\\def\\be{\\begin{equation}}";
  720. print "\\def\\ee{\\end{equation}}";
  721. print "\\def\\bea{\\begin{eqnarray}}";
  722. print "\\def\\eea{\\end{eqnarray}}";
  723. print "\\def\\bi{\\begin{itemize}}";
  724. print "\\def\\ei{\\end{itemize}}";
  725. print "\\def\\bn{\\begin{enumerate}}";
  726. print "\\def\\en{\\end{enumerate}}";
  727. print "\\def\\bd{\\begin{description}}";
  728. print "\\def\\ed{\\end{description}}";
  729. print "\\def\\({\\left (}";
  730. print "\\def\\){\\right )}";
  731. print "\\def\\[{\\left [}";
  732. print "\\def\\]{\\right ]}";
  733. print "\\def\\<{\\left \\langle}";
  734. print "\\def\\>{\\right \\rangle}";
  735. print "\\def\\cI{{\\cal I}}";
  736. print "\\def\\diag{\\mathop{\\rm diag}}";
  737. print "\\def\\tr{\\mathop{\\rm tr}}";
  738. print "%-------------------------------------------------------------";
  739. }# end sub print_macros
  740. sub do_beg {
  741. unless ( $opt_b ) {
  742. if ( $begdoc == 0 ) {
  743. if ( $tpage ) {
  744. print "\\title{@title}";
  745. print "\\author{{\\sc @author}\\\\ {\\em @affiliation}}";
  746. print "\\date{@date}";
  747. }
  748. print "\\begin{document}";
  749. if ( $tpage ) {
  750. print "\\maketitle";
  751. }
  752. print "\\tableofcontents";
  753. print "\\newpage";
  754. $begdoc = 1;
  755. }
  756. }
  757. }# end sub do_beg
  758. sub do_eoc {
  759. print ' ';
  760. if ($verb) {
  761. print "\\end{verbatim}";
  762. $verb = 0;
  763. }
  764. $source = 0;
  765. }# end sub do_eoc
  766. sub set_missing {
  767. $have_name = 0; # have routine name?
  768. $have_desc = 0; # have description?
  769. $have_intf = 0; # have interface?
  770. $have_hist = 0; # have revision history?
  771. $name_is = "UNKNOWN";
  772. }# end sub set_missing
  773. sub check_if_all_there {
  774. $have_name ||
  775. die "ProTeX: invalid prologue, missing !ROUTINE: or !IROUTINE: in <$name_is>";
  776. $have_desc ||
  777. die "ProTeX: invalid prologue, missing !DESCRIPTION: in <$name_is>";
  778. $have_intf ||
  779. die "ProTeX: invalid prologue, missing !INTERFACE: in <$name_is>";
  780. $have_hist ||
  781. die "ProTeX: invalid prologue, missing !REVISION HISTORY: in <$name_is>";
  782. }# end sub check_if_all_there