home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume26 / c++2latex / part03 < prev    next >
Encoding:
Text File  |  1991-12-14  |  50.1 KB  |  1,694 lines

  1. Newsgroups: comp.sources.misc
  2. From: heitkoet@gorbi.informatik.uni-dortmund.de (Joerg Heitkoetter)
  3. Subject:  v26i115:  c++2latex - a C++ to LaTeX converter, Part03/03
  4. Message-ID: <1991Dec15.033520.23840@sparky.imd.sterling.com>
  5. X-Md4-Signature: 6e412fa8ce4800b1da4909a72b7e89f6
  6. Date: Sun, 15 Dec 1991 03:35:20 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: heitkoet@gorbi.informatik.uni-dortmund.de (Joerg Heitkoetter)
  10. Posting-number: Volume 26, Issue 115
  11. Archive-name: c++2latex/part03
  12. Environment: C++, LaTeX
  13.  
  14. #!/bin/sh
  15. # do not concatenate these parts, unpack them in order with /bin/sh
  16. # file demo.c continued
  17. #
  18. if test ! -r _shar_seq_.tmp; then
  19.     echo 'Please unpack part 1 first!'
  20.     exit 1
  21. fi
  22. (read Scheck
  23.  if test "$Scheck" != 3; then
  24.     echo Please unpack part "$Scheck" next!
  25.     exit 1
  26.  else
  27.     exit 0
  28.  fi
  29. ) < _shar_seq_.tmp || exit 1
  30. if test ! -f _shar_wnt_.tmp; then
  31.     echo 'x - still skipping demo.c'
  32. else
  33. echo 'x - continuing file demo.c'
  34. sed 's/^X//' << 'SHAR_EOF' >> 'demo.c' &&
  35. //  the suitability of this software for any purpose.  It is provided "as is"
  36. //  without express or implied warranty.
  37. //
  38. //  UNIDO DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  39. //  INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  40. //  IN NO EVENT SHALL UNIDO BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  41. //  CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  42. //  FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  43. //  CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  44. //  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  45. //
  46. X
  47. /*
  48. X *    this is a comment
  49. X */
  50. X
  51. /* NOARGS */
  52. main (argc, argv)
  53. int argc;
  54. char **argv;
  55. {
  56. X    int a; // this is a variable
  57. X    int b; /* another variable */
  58. X
  59. X    // busy doing nothing...
  60. X    int c;
  61. X
  62. X        /* no more */
  63. X    int d;
  64. X
  65. X    switch (something) {
  66. X        case THISCASE:
  67. X            something = 99; // who knows why?
  68. X            break;
  69. X        case default:
  70. X            somethingelse = 100; // completetly arbitrary
  71. X            break;
  72. X    } // end of switch
  73. X
  74. X    for (i=0; i<j; i++) {
  75. X    } /* loop */
  76. X
  77. X    return (0);
  78. } /* end of main */
  79. SHAR_EOF
  80. echo 'File demo.c is complete' &&
  81. chmod 0640 demo.c ||
  82. echo 'restore of demo.c failed'
  83. Wc_c="`wc -c < 'demo.c'`"
  84. test 1621 -eq "$Wc_c" ||
  85.     echo 'demo.c: original size 1621, current size' "$Wc_c"
  86. rm -f _shar_wnt_.tmp
  87. fi
  88. # ============= fancyheadings.doc ==============
  89. if test -f 'fancyheadings.doc' -a X"$1" != X"-c"; then
  90.     echo 'x - skipping fancyheadings.doc (File already exists)'
  91.     rm -f _shar_wnt_.tmp
  92. else
  93. > _shar_wnt_.tmp
  94. echo 'x - extracting fancyheadings.doc (Text)'
  95. sed 's/^X//' << 'SHAR_EOF' > 'fancyheadings.doc' &&
  96. Return-Path: piet@cs.ruu.nl
  97. Received: from unido.informatik.uni-dortmund.de
  98. X    by gorbi.informatik.uni-dortmund.de id AA04891; Mon, 7 Oct 91 11:35:20 +0100
  99. Received: from ruuinf.cs.ruu.nl 
  100. X    by unido.informatik.uni-dortmund.de with SMTP (5.65+/UNIDO-2.0.4.d)
  101. X    via EUnet for gorbi.informatik.uni-dortmund.de
  102. X    id AA12353; Mon, 7 Oct 91 11:31:05 +0100
  103. Received: from gnu.cs.ruu.nl by ruuinf.cs.ruu.nl with SMTP
  104. X    (5.61+/IDA-1.2.8) id AA25657; Mon, 7 Oct 91 12:15:53 +0100
  105. Received: by alchemy.cs.ruu.nl
  106. X    (15.11/15.6) id AA20318; Mon, 7 Oct 91 11:34:15 -0100
  107. Date: Mon, 7 Oct 91 11:34:15 -0100
  108. XFrom: Piet van Oostrum <piet@cs.ruu.nl>
  109. Message-Id: <9110071034.AA20318@alchemy.cs.ruu.nl>
  110. To: heitkoet (Joerg Heitkoetter)
  111. Subject: Re: Fancy Latex Headers - Please Help
  112. Newsgroups: comp.text.tex
  113. In-Reply-To: <3853@laura.UUCP>
  114. Organization: Dept of Computer Science, Utrecht University, The Netherlands
  115. Cc: 
  116. X
  117. Here is the (new) doc file for fancyheadings.sty:
  118. X
  119. Here is a documentstylestyle option that allows you to customize your
  120. page headers and footers in an easy way. It combines features that were
  121. separately available in other pagestyles, without introducing much
  122. complexity. You can define:
  123. X    - three-part headers and footers
  124. X    - rules in header and footer
  125. X    - headers and footers wider than \textwidth
  126. X    - multiline headers and footers
  127. X    - separate headers and footers for even and odd pages
  128. X    - separate headers and footers for chapter pages
  129. X
  130. To use this pagestyle, you must include the ``fancyheadings'' style
  131. option in your \documentstyle, and issue the \pagestyle{fancy} command.
  132. The \pagestyle{fancy} command should be issued after any changes made to
  133. \textwidth.
  134. X
  135. The page layout will be as follows:
  136. X
  137. X        LHEAD          CHEAD          RHEAD
  138. X        ----------------------------------- (rule)
  139. X
  140. X                 page body
  141. X
  142. X
  143. X        ----------------------------------- (rule)
  144. X        LFOOT           CFOOT          RFOOT
  145. X
  146. The L-fields will be leftadjusted, the C-fields centered and the
  147. R-fields rightadjusted.
  148. Each of the six fields and the two rules can be defined separately.
  149. X
  150. Simple use:
  151. X
  152. The header and footer fields can be defined by commands \lhead{LHEAD}
  153. and so on for the other fields. If the field depends on something in the
  154. document (e.g. section titles) you must in general use the \markboth and
  155. \markright commands, otherwise a title may end on the wrong page. You
  156. can do this e.g. by redefining the commands \chaptermark, \sectionmark
  157. and so on (see example below). The defaults for these marks are as in
  158. the standard pagestyles. The marks can be put into a header or footer
  159. field by referencing \leftmark and \rightmark.
  160. X
  161. Rules in header and footer
  162. X
  163. The thickness of the rules below the header and above the footer can be
  164. changed by redefining the length parameters \headrulewidth (default
  165. 0.4pt) and \footrulewidth (default 0). These may be redefined by the
  166. \setlength command. A thickness of 0pt makes the rule invisible.
  167. If you want to make more complicated changes, you have to redefine the
  168. commands \headrule and/or \footrule.
  169. X
  170. Headers and footers wider than \textwidth
  171. X
  172. The headers and footers are set in a box of width \headwidth. The
  173. default for this is the value of \textwidth. You can make it wider (or
  174. smaller) by redefining \headwidth with the \setlength or \addtolength
  175. command. The headers and footers will stick out the page on the same
  176. side as the marginal notes. For example to include the marginal notes,
  177. add both \marginparsep and \marginparwidth to \headwidth (see also the
  178. example below).
  179. X
  180. Multiline headers and footers
  181. X
  182. Each of the six fields is set in an appropriate parbox, so you can put a
  183. multiline part in it with the \\ command. It is also possible to put
  184. extra space in it with the \vspace command. Note that if you do this you
  185. will probably have to increase the \headheight or \footskip lengths.
  186. X
  187. Separate headers and footers for even and odd pages
  188. X
  189. If you want the headers and footers to be different on even- and
  190. odd-numbered pages in the ``twoside'' style, the field-defining macros
  191. can be given an optional argument, to be used on the even-numbered
  192. pages, like \lhead[EVEN-LHEAD]{ODD-LHEAD}.
  193. X
  194. Separate headers and footers for chapter pages
  195. X
  196. LaTeX gives a \thispagestyle{plain} command for the first page of the
  197. document, the first page of each chapter and a couple of other pages. It
  198. might be incompatible with your pagestyle. In this case you can use a
  199. slightly different version of the pagestyle, called \pagestyle{fancyplain}. 
  200. This pagestyle redefines the pagestyle ``plain'' to also use pagestyle
  201. ``fancy'' with the following modifications:
  202. X    - the thicknesses of the rules is defined by \plainheadrulewidth and
  203. X      \plainfootrulewidth (both default 0).
  204. X    - the 6 fields may be defined separately for the plain pages by
  205. X      giving them the value \fancyplain{PLAIN-VALUE}{NORMAL-VALUE}. This
  206. X      construct may be used in both the optional argument and the normal
  207. X      argument. Thus \lhead[\fancyplain{F1}{F2}]{\fancyplain{F3}{F4}}
  208. X      specifies the LHEAD value in a two-sided document:
  209. X    F1 on an even-numbered ``plain'' page
  210. X    F2 on an even-numbered normal page
  211. X    F3 on an odd-numbered ``plain'' page
  212. X    F4 on an odd-numbered normal page.
  213. X
  214. Defaults:
  215. X
  216. \headrulewidth           0.4pt
  217. \footrulewidth        0pt
  218. \plainheadrulewidth    0pt
  219. \plainfootrulewidth    0pt
  220. X
  221. \lhead[\fancyplain{}{\sl\rightmark}]{\fancyplain{}{\sl\leftmark}}
  222. %  i.e. empty on ``plain'' pages \rightmark on even, \leftmark on odd pages
  223. \chead{}
  224. \rhead[\fancyplain{}{\sl\leftmark}]{\fancyplain{}{\sl\rightmark}}
  225. %  i.e. empty on ``plain'' pages \leftmark on even, \rightmark on odd pages
  226. \lfoot{}
  227. \cfoot{\rm\thepage} % page number
  228. \rfoot{}
  229. X
  230. Examples:
  231. X
  232. To put two lines containing the section title and the subsection title
  233. in the righthandside corner, use:
  234. X
  235. \documentstyle[fancyheadings]{article}
  236. \pagestyle{fancy}
  237. \renewcommand{\sectionmark}[1]{\markboth{#1}{}}
  238. \renewcommand{\subsectionmark}[1]{\markright{#1}}
  239. \rfoot{\leftmark\\\rightmark}
  240. X
  241. The following definitions give an approximation of the style used in the
  242. LaTeX book:
  243. X
  244. \documentstyle[fancyheadings]{book}
  245. \pagestyle{fancyplain}
  246. \addtolength{\headwidth}{\marginparsep}
  247. \addtolength{\headwidth}{\marginparwidth}
  248. \renewcommand{\chaptermark}[1]{\markboth{#1}{#1}} % remember chapter title
  249. \renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
  250. X                                                % section number and title
  251. \lhead[\fancyplain{}{\bf\thepage}]{\fancyplain{}{\bf\rightmark}}
  252. \rhead[\fancyplain{}{\bf\leftmark}]{\fancyplain{}{\bf\thepage}}
  253. \cfoot{}
  254. X
  255. Using section titles etc. in the headers and/or footers:
  256. X
  257. You can't just change the header and/or footer fields in the middle of some
  258. text (e.g. after a section header). This is because TeX may have processed
  259. a bit more text before deciding to make up the page. It may have passed a
  260. section beginning, causing the wrong title on the page.  TeX has a
  261. mechanism called 'marks' to solve this problem.  There is in LaTeX a
  262. \leftmark and a \rightmark. Usually \leftmark is a chapter title and
  263. \rightmark is a section title. To set the marks there are two commands:
  264. \markboth{L}{R} sets the \leftmark to L and the rightmark to R, and
  265. \rightmark{R} sets only the rightmark to R.
  266. The default definitions of \section etc. do this already for you.
  267. X
  268. An example follows:
  269. X
  270. X    left page                        right page
  271. X    ---------                        ----------
  272. X
  273. 2         CHAPTER 1. Introduction | 1.2 Some section                        3
  274. ------------------------------------ | -----------------------------------------
  275. X                                     |
  276. Text                                 | more text
  277. Text                                 | more text
  278. Text                                 | more text
  279. X                                     |
  280. X                                     |
  281. X                                     |
  282. X
  283. This can be easily done with fancyheadings as follows:
  284. X
  285. \pagestyle{fancy}
  286. \setlength{\headrulewidth}{1pt}
  287. \lhead[\rm\thepage]{\sl\rightmark}
  288. \rhead[\sl\leftmark]{\rm\thepage}
  289. X
  290. This specifies that on even pages (the [] parts) the leftheadpart is page
  291. number and rightheadpart is \leftmark, which is the chapter title (because that
  292. is given as the left argument of \markboth (see page 162 of the LaTeX book)
  293. X
  294. On odd pages (the parts between {}) the leftheadpart is \rightmark (which
  295. is the last section title because that is given as argument to \markright
  296. (see the same page), and the rightheadpart is the page no.
  297. X
  298. Now suppose you don't want the section number and you want the section
  299. title in upper case:
  300. You add the following to your preamble:
  301. X
  302. \renewcommand{\sectionmark}[1]{\markright{\uppercase{#1}}}
  303. X
  304. Or if you don't want the chapter number but only the chapter title (not in
  305. uppercase):
  306. X
  307. \renewcommand{\chaptermark}[1]{\markboth{#1}{ }}
  308. X
  309. Note: the parameter in both cases is the (section|chapter) title.
  310. X
  311. KNOWN PROBLEMS:
  312. X
  313. Sometimes you will get a warning message from LaTeX concerning ``overfull
  314. vbox during output''. In this case you have to increase the \headheight or
  315. \footskip lengths or both (with \addtolength or \setlength).
  316. -- 
  317. Piet* van Oostrum, Dept of Computer Science, Utrecht University,
  318. Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands.
  319. Telephone: +31 30 531806   Uucp:   uunet!mcsun!ruuinf!piet
  320. Telefax:   +31 30 513791   Internet:  piet@cs.ruu.nl   (*`Pete')
  321. SHAR_EOF
  322. chmod 0600 fancyheadings.doc ||
  323. echo 'restore of fancyheadings.doc failed'
  324. Wc_c="`wc -c < 'fancyheadings.doc'`"
  325. test 9284 -eq "$Wc_c" ||
  326.     echo 'fancyheadings.doc: original size 9284, current size' "$Wc_c"
  327. rm -f _shar_wnt_.tmp
  328. fi
  329. # ============= fancyheadings.sty ==============
  330. if test -f 'fancyheadings.sty' -a X"$1" != X"-c"; then
  331.     echo 'x - skipping fancyheadings.sty (File already exists)'
  332.     rm -f _shar_wnt_.tmp
  333. else
  334. > _shar_wnt_.tmp
  335. echo 'x - extracting fancyheadings.sty (Text)'
  336. sed 's/^X//' << 'SHAR_EOF' > 'fancyheadings.sty' &&
  337. % fancyheadings.sty version 1.0
  338. % Fancy headers and footers.
  339. % Piet van Oostrum, Dept of Computer Science, University of Utrecht
  340. % Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands
  341. % Telephone: +31-30-531806. piet@cs.ruu.nl (mcvax!hp4nl!ruuinf!piet)
  342. % March, 1989.
  343. X
  344. \def\lhead{\@ifnextchar[{\@xlhead}{\@ylhead}}
  345. \def\@xlhead[#1]#2{\gdef\@elhead{#1}\gdef\@olhead{#2}}
  346. \def\@ylhead#1{\gdef\@elhead{#1}\gdef\@olhead{#1}}
  347. X
  348. \def\chead{\@ifnextchar[{\@xchead}{\@ychead}}
  349. \def\@xchead[#1]#2{\gdef\@echead{#1}\gdef\@ochead{#2}}
  350. \def\@ychead#1{\gdef\@echead{#1}\gdef\@ochead{#1}}
  351. X
  352. \def\rhead{\@ifnextchar[{\@xrhead}{\@yrhead}}
  353. \def\@xrhead[#1]#2{\gdef\@erhead{#1}\gdef\@orhead{#2}}
  354. \def\@yrhead#1{\gdef\@erhead{#1}\gdef\@orhead{#1}}
  355. X
  356. \def\lfoot{\@ifnextchar[{\@xlfoot}{\@ylfoot}}
  357. \def\@xlfoot[#1]#2{\gdef\@elfoot{#1}\gdef\@olfoot{#2}}
  358. \def\@ylfoot#1{\gdef\@elfoot{#1}\gdef\@olfoot{#1}}
  359. X
  360. \def\cfoot{\@ifnextchar[{\@xcfoot}{\@ycfoot}}
  361. \def\@xcfoot[#1]#2{\gdef\@ecfoot{#1}\gdef\@ocfoot{#2}}
  362. \def\@ycfoot#1{\gdef\@ecfoot{#1}\gdef\@ocfoot{#1}}
  363. X
  364. \def\rfoot{\@ifnextchar[{\@xrfoot}{\@yrfoot}}
  365. \def\@xrfoot[#1]#2{\gdef\@erfoot{#1}\gdef\@orfoot{#2}}
  366. \def\@yrfoot#1{\gdef\@erfoot{#1}\gdef\@orfoot{#1}}
  367. X
  368. \newdimen\headrulewidth
  369. \newdimen\footrulewidth
  370. \newdimen\plainheadrulewidth
  371. \newdimen\plainfootrulewidth
  372. \newdimen\headwidth
  373. \newif\if@fancyplain \@fancyplainfalse
  374. \def\fancyplain#1#2{\if@fancyplain#1\else#2\fi}
  375. X
  376. % Initialization of the head and foot text.
  377. X
  378. \headrulewidth 0.4pt
  379. \footrulewidth\z@
  380. \plainheadrulewidth\z@
  381. \plainfootrulewidth\z@
  382. X
  383. \lhead[\fancyplain{}{\sl\rightmark}]{\fancyplain{}{\sl\leftmark}}
  384. %  i.e. empty on ``plain'' pages \rightmark on even, \leftmark on odd pages
  385. \chead{}
  386. \rhead[\fancyplain{}{\sl\leftmark}]{\fancyplain{}{\sl\rightmark}}
  387. %  i.e. empty on ``plain'' pages \leftmark on even, \rightmark on odd pages
  388. \lfoot{}
  389. \cfoot{\rm\thepage} % page number
  390. \rfoot{}
  391. X
  392. % Put together a header or footer given the left, center and
  393. % right text, fillers at left and right and a rule.
  394. % The \lap commands put the text into an hbox of zero size,
  395. % so overlapping text does not generate an errormessage.
  396. X
  397. \def\@fancyhead#1#2#3#4#5{#1\hbox to\headwidth{\vbox{\hbox
  398. {\rlap{\parbox[b]{\headwidth}{\raggedright#2\strut}}\hfill
  399. \parbox[b]{\headwidth}{\centering#3\strut}\hfill
  400. \llap{\parbox[b]{\headwidth}{\raggedleft#4\strut}}}\headrule}}#5}
  401. X
  402. X
  403. \def\@fancyfoot#1#2#3#4#5{#1\hbox to\headwidth{\vbox{\footrule
  404. \hbox{\rlap{\parbox[t]{\headwidth}{\raggedright#2\strut}}\hfill
  405. \parbox[t]{\headwidth}{\centering#3\strut}\hfill
  406. \llap{\parbox[t]{\headwidth}{\raggedleft#4\strut}}}}}#5}
  407. X
  408. \def\headrule{{\if@fancyplain\headrulewidth\plainheadrulewidth\fi
  409. \hrule\@height\headrulewidth\@width\headwidth \vskip-\headrulewidth}}
  410. X
  411. \def\footrule{{\if@fancyplain\footrulewidth\plainfootrulewidth\fi
  412. \vskip-0.3\normalbaselineskip\vskip-\footrulewidth
  413. \hrule\@width\headwidth\@height\footrulewidth\vskip0.3\normalbaselineskip}}
  414. X
  415. \def\ps@fancy{
  416. \let\@mkboth\markboth
  417. \@ifundefined{chapter}{\def\sectionmark##1{\markboth
  418. {\uppercase{\ifnum \c@secnumdepth>\z@
  419. X \thesection\hskip 1em\relax \fi ##1}}{}}
  420. \def\subsectionmark##1{\markright {\ifnum \c@secnumdepth >\@ne
  421. X \thesubsection\hskip 1em\relax \fi ##1}}}
  422. {\def\chaptermark##1{\markboth {\uppercase{\ifnum \c@secnumdepth>\m@ne
  423. X \@chapapp\ \thechapter. \ \fi ##1}}{}}
  424. \def\sectionmark##1{\markright{\uppercase{\ifnum \c@secnumdepth >\z@
  425. X \thesection. \ \fi ##1}}}}
  426. \def\@oddhead{\@fancyhead\relax\@olhead\@ochead\@orhead\hss}
  427. \def\@oddfoot{\@fancyfoot\relax\@olfoot\@ocfoot\@orfoot\hss}
  428. \def\@evenhead{\@fancyhead\hss\@elhead\@echead\@erhead\relax}
  429. \def\@evenfoot{\@fancyfoot\hss\@elfoot\@ecfoot\@erfoot\relax}
  430. \headwidth\textwidth}
  431. \def\ps@fancyplain{\ps@fancy \let\ps@plain\ps@plain@fancy}
  432. \def\ps@plain@fancy{\@fancyplaintrue\ps@fancy}
  433. SHAR_EOF
  434. chmod 0640 fancyheadings.sty ||
  435. echo 'restore of fancyheadings.sty failed'
  436. Wc_c="`wc -c < 'fancyheadings.sty'`"
  437. test 3778 -eq "$Wc_c" ||
  438.     echo 'fancyheadings.sty: original size 3778, current size' "$Wc_c"
  439. rm -f _shar_wnt_.tmp
  440. fi
  441. # ============= getopt.c ==============
  442. if test -f 'getopt.c' -a X"$1" != X"-c"; then
  443.     echo 'x - skipping getopt.c (File already exists)'
  444.     rm -f _shar_wnt_.tmp
  445. else
  446. > _shar_wnt_.tmp
  447. echo 'x - extracting getopt.c (Text)'
  448. sed 's/^X//' << 'SHAR_EOF' > 'getopt.c' &&
  449. /* Getopt for GNU.
  450. X   Copyright (C) 1987, 1989 Free Software Foundation, Inc.
  451. X
  452. X   This program is free software; you can redistribute it and/or modify
  453. X   it under the terms of the GNU General Public License as published by
  454. X   the Free Software Foundation; either version 1, or (at your option)
  455. X   any later version.
  456. X
  457. X   This program is distributed in the hope that it will be useful,
  458. X   but WITHOUT ANY WARRANTY; without even the implied warranty of
  459. X   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  460. X   GNU General Public License for more details.
  461. X
  462. X   You should have received a copy of the GNU General Public License
  463. X   along with this program; if not, write to the Free Software
  464. X   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  465. X
  466. /* This version of `getopt' appears to the caller like standard Unix `getopt'
  467. X   but it behaves differently for the user, since it allows the user
  468. X   to intersperse the options with the other arguments.
  469. X
  470. X   As `getopt' works, it permutes the elements of `argv' so that,
  471. X   when it is done, all the options precede everything else.  Thus
  472. X   all application programs are extended to handle flexible argument order.
  473. X
  474. X   Setting the environment variable _POSIX_OPTION_ORDER disables permutation.
  475. X   Then the behavior is completely standard.
  476. X
  477. X   GNU application programs can use a third alternative mode in which
  478. X   they can distinguish the relative order of options and other arguments.  */
  479. X
  480. #include <stdio.h>
  481. X
  482. /* If compiled with GNU C, use the built-in alloca */
  483. #ifdef __GNUC__
  484. #define alloca __builtin_alloca
  485. #else /* not __GNUC__ */
  486. #ifdef sparc
  487. #include <alloca.h>
  488. #else
  489. char *alloca ();
  490. #endif
  491. #endif /* not __GNUC__ */
  492. X
  493. #ifdef USG
  494. #define bcopy(s, d, l) memcpy((d), (s), (l))
  495. #define index strchr
  496. #endif
  497. X
  498. char *getenv ();
  499. char *index ();
  500. char *malloc ();
  501. X
  502. /* For communication from `getopt' to the caller.
  503. X   When `getopt' finds an option that takes an argument,
  504. X   the argument value is returned here.
  505. X   Also, when `ordering' is RETURN_IN_ORDER,
  506. X   each non-option ARGV-element is returned here.  */
  507. X
  508. char *optarg = 0;
  509. X
  510. /* Index in ARGV of the next element to be scanned.
  511. X   This is used for communication to and from the caller
  512. X   and for communication between successive calls to `getopt'.
  513. X
  514. X   On entry to `getopt', zero means this is the first call; initialize.
  515. X
  516. X   When `getopt' returns EOF, this is the index of the first of the
  517. X   non-option elements that the caller should itself scan.
  518. X
  519. X   Otherwise, `optind' communicates from one call to the next
  520. X   how much of ARGV has been scanned so far.  */
  521. X
  522. int optind = 0;
  523. X
  524. /* The next char to be scanned in the option-element
  525. X   in which the last option character we returned was found.
  526. X   This allows us to pick up the scan where we left off.
  527. X
  528. X   If this is zero, or a null string, it means resume the scan
  529. X   by advancing to the next ARGV-element.  */
  530. X
  531. static char *nextchar;
  532. X
  533. /* Callers store zero here to inhibit the error message
  534. X   for unrecognized options.  */
  535. X
  536. int opterr = 1;
  537. X
  538. /* Describe how to deal with options that follow non-option ARGV-elements.
  539. X
  540. X   If the caller did not specify anything,
  541. X   the default is REQUIRE_ORDER if the environment variable
  542. X   _POSIX_OPTION_ORDER is defined, PERMUTE otherwise.
  543. X
  544. X   REQUIRE_ORDER means don't recognize them as options.
  545. X   Stop option processing when the first non-option is seen.
  546. X   This is what Unix does.
  547. X
  548. X   PERMUTE is the default.  We permute the contents of ARGV as we scan,
  549. X   so that eventually all the options are at the end.  This allows options
  550. X   to be given in any order, even with programs that were not written to
  551. X   expect this.
  552. X
  553. X   RETURN_IN_ORDER is an option available to programs that were written
  554. X   to expect options and other ARGV-elements in any order and that care about
  555. X   the ordering of the two.  We describe each non-option ARGV-element
  556. X   as if it were the argument of an option with character code one.
  557. X   Using `-' as the first character of the list of option characters
  558. X   requests this mode of operation.
  559. X
  560. X   The special argument `--' forces an end of option-scanning regardless
  561. X   of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
  562. X   `--' can cause `getopt' to return EOF with `optind' != ARGC.  */
  563. X
  564. static enum { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER } ordering;
  565. X
  566. /* Describe the long-named options requested by the application.
  567. X   _GETOPT_LONG_OPTIONS is a vector of `struct option' terminated by an
  568. X   element containing a name which is zero.
  569. X   The field `has_arg' is 1 if the option takes an argument, 
  570. X   2 if it takes an optional argument.  */
  571. X
  572. struct option
  573. {
  574. X  char *name;
  575. X  int has_arg;
  576. X  int *flag;
  577. X  int val;
  578. };
  579. X
  580. struct option *_getopt_long_options;
  581. X
  582. int _getopt_long_only = 0;
  583. X
  584. #if 0
  585. /* This is an ugly kludge.  Programs should use the opt_index argument
  586. X   to getopt_long instead.  */
  587. /* Name of long-named option actually found.  */
  588. X
  589. char *_getopt_option_name;
  590. #endif
  591. X
  592. /* Index in _GETOPT_LONG_OPTIONS of the long-named option actually found.
  593. X   Only valid when a long-named option was found. */
  594. X
  595. int option_index;
  596. X
  597. /* Handle permutation of arguments.  */
  598. X
  599. /* Describe the part of ARGV that contains non-options that have
  600. X   been skipped.  `first_nonopt' is the index in ARGV of the first of them;
  601. X   `last_nonopt' is the index after the last of them.  */
  602. X
  603. static int first_nonopt;
  604. static int last_nonopt;
  605. X
  606. /* Exchange two adjacent subsequences of ARGV.
  607. X   One subsequence is elements [first_nonopt,last_nonopt)
  608. X    which contains all the non-options that have been skipped so far.
  609. X   The other is elements [last_nonopt,optind), which contains all
  610. X    the options processed since those non-options were skipped.
  611. X
  612. X   `first_nonopt' and `last_nonopt' are relocated so that they describe
  613. X    the new indices of the non-options in ARGV after they are moved.  */
  614. X
  615. static void
  616. exchange (argv)
  617. X     char **argv;
  618. {
  619. X  int nonopts_size
  620. X    = (last_nonopt - first_nonopt) * sizeof (char *);
  621. X  char **temp = (char **) alloca (nonopts_size);
  622. X
  623. X  /* Interchange the two blocks of data in argv.  */
  624. X
  625. X  bcopy (&argv[first_nonopt], temp, nonopts_size);
  626. X  bcopy (&argv[last_nonopt], &argv[first_nonopt],
  627. X     (optind - last_nonopt) * sizeof (char *));
  628. X  bcopy (temp, &argv[first_nonopt + optind - last_nonopt],
  629. X     nonopts_size);
  630. X
  631. X  /* Update records for the slots the non-options now occupy.  */
  632. X
  633. X  first_nonopt += (optind - last_nonopt);
  634. X  last_nonopt = optind;
  635. }
  636. X
  637. /* Scan elements of ARGV (whose length is ARGC) for option characters
  638. X   given in OPTSTRING.
  639. X
  640. X   If an element of ARGV starts with '-', and is not exactly "-" or "--",
  641. X   then it is an option element.  The characters of this element
  642. X   (aside from the initial '-') are option characters.  If `getopt'
  643. X   is called repeatedly, it returns successively each of the option characters
  644. X   from each of the option elements.
  645. X
  646. X   If `getopt' finds another option character, it returns that character,
  647. X   updating `optind' and `nextchar' so that the next call to `getopt' can
  648. X   resume the scan with the following option character or ARGV-element.
  649. X
  650. X   If there are no more option characters, `getopt' returns `EOF'.
  651. X   Then `optind' is the index in ARGV of the first ARGV-element
  652. X   that is not an option.  (The ARGV-elements have been permuted
  653. X   so that those that are not options now come last.)
  654. X
  655. X   OPTSTRING is a string containing the legitimate option characters.
  656. X   If an option character is seen that is not listed in OPTSTRING,
  657. X   return '?' after printing an error message.  If you set `opterr' to
  658. X   zero, the error message is suppressed but we still return '?'.
  659. X
  660. X   If a char in OPTSTRING is followed by a colon, that means it wants an arg,
  661. X   so the following text in the same ARGV-element, or the text of the following
  662. X   ARGV-element, is returned in `optarg'.  Two colons mean an option that
  663. X   wants an optional arg; if there is text in the current ARGV-element,
  664. X   it is returned in `optarg', otherwise `optarg' is set to zero.
  665. X
  666. X   If OPTSTRING starts with `-', it requests a different method of handling the
  667. X   non-option ARGV-elements.  See the comments about RETURN_IN_ORDER, above.
  668. X
  669. X   Long-named options begin with `+' instead of `-'.
  670. X   Their names may be abbreviated as long as the abbreviation is unique
  671. X   or is an exact match for some defined option.  If they have an
  672. X   argument, it follows the option name in the same ARGV-element, separated
  673. X   from the option name by a `=', or else the in next ARGV-element.
  674. X   `getopt' returns 0 when it finds a long-named option.  */
  675. X
  676. int
  677. getopt (argc, argv, optstring)
  678. X     int argc;
  679. X     char **argv;
  680. X     char *optstring;
  681. {
  682. X  optarg = 0;
  683. X
  684. X  /* Initialize the internal data when the first call is made.
  685. X     Start processing options with ARGV-element 1 (since ARGV-element 0
  686. X     is the program name); the sequence of previously skipped
  687. X     non-option ARGV-elements is empty.  */
  688. X
  689. X  if (optind == 0)
  690. X    {
  691. X      first_nonopt = last_nonopt = optind = 1;
  692. X
  693. X      nextchar = 0;
  694. X
  695. X      /* Determine how to handle the ordering of options and nonoptions.  */
  696. X
  697. X      if (optstring[0] == '-')
  698. X    ordering = RETURN_IN_ORDER;
  699. X      else if (getenv ("_POSIX_OPTION_ORDER") != 0)
  700. X    ordering = REQUIRE_ORDER;
  701. X      else
  702. X    ordering = PERMUTE;
  703. X    }
  704. X
  705. X  if (nextchar == 0 || *nextchar == 0)
  706. X    {
  707. X      if (ordering == PERMUTE)
  708. X    {
  709. X      /* If we have just processed some options following some non-options,
  710. X         exchange them so that the options come first.  */
  711. X
  712. X      if (first_nonopt != last_nonopt && last_nonopt != optind)
  713. X        exchange (argv);
  714. X      else if (last_nonopt != optind)
  715. X        first_nonopt = optind;
  716. X
  717. X      /* Now skip any additional non-options
  718. X         and extend the range of non-options previously skipped.  */
  719. X
  720. X      while (optind < argc
  721. X         && (argv[optind][0] != '-'
  722. X             || argv[optind][1] == 0)
  723. X         && (_getopt_long_options == 0
  724. X             || argv[optind][0] != '+'
  725. X             || argv[optind][1] == 0))
  726. X        optind++;
  727. X      last_nonopt = optind;
  728. X    }
  729. X
  730. X      /* Special ARGV-element `--' means premature end of options.
  731. X     Skip it like a null option,
  732. X     then exchange with previous non-options as if it were an option,
  733. X     then skip everything else like a non-option.  */
  734. X
  735. X      if (optind != argc && !strcmp (argv[optind], "--"))
  736. X    {
  737. X      optind++;
  738. X
  739. X      if (first_nonopt != last_nonopt && last_nonopt != optind)
  740. X        exchange (argv);
  741. X      else if (first_nonopt == last_nonopt)
  742. X        first_nonopt = optind;
  743. X      last_nonopt = argc;
  744. X
  745. X      optind = argc;
  746. X    }
  747. X
  748. X      /* If we have done all the ARGV-elements, stop the scan
  749. X     and back over any non-options that we skipped and permuted.  */
  750. X
  751. X      if (optind == argc)
  752. X    {
  753. X      /* Set the next-arg-index to point at the non-options
  754. X         that we previously skipped, so the caller will digest them.  */
  755. X      if (first_nonopt != last_nonopt)
  756. X        optind = first_nonopt;
  757. X      return EOF;
  758. X    }
  759. X     
  760. X      /* If we have come to a non-option and did not permute it,
  761. X     either stop the scan or describe it to the caller and pass it by.  */
  762. X
  763. X      if ((argv[optind][0] != '-' || argv[optind][1] == 0)
  764. X      && (_getopt_long_options == 0
  765. X          || argv[optind][0] != '+' || argv[optind][1] == 0))
  766. X    {
  767. X      if (ordering == REQUIRE_ORDER)
  768. X        return EOF;
  769. X      optarg = argv[optind++];
  770. #if 0
  771. X      _getopt_option_name = 0;
  772. #endif
  773. X      return 1;
  774. X    }
  775. X
  776. X      /* We have found another option-ARGV-element.
  777. X     Start decoding its characters.  */
  778. X
  779. X      nextchar = argv[optind] + 1;
  780. X    }
  781. X
  782. X  if (_getopt_long_options != 0
  783. X      && (argv[optind][0] == '+'
  784. X      || (_getopt_long_only && argv[optind][0] == '-'))
  785. X      )
  786. X    {
  787. X      struct option *p;
  788. X      char *s = nextchar;
  789. X      int exact = 0;
  790. X      int ambig = 0;
  791. X      struct option *pfound = 0;
  792. X      int indfound;
  793. X
  794. X      while (*s && *s != '=') s++;
  795. X
  796. X      /* Test all options for either exact match or abbreviated matches.  */
  797. X      for (p = _getopt_long_options, option_index = 0; p->name; 
  798. X       p++, option_index++)
  799. X    if (!strncmp (p->name, nextchar, s - nextchar))
  800. X      {
  801. X        if (s - nextchar == strlen (p->name))
  802. X          {
  803. X        /* Exact match found.  */
  804. X        pfound = p;
  805. X        indfound = option_index;
  806. X        exact = 1;
  807. X        break;
  808. X          }
  809. X        else if (pfound == 0)
  810. X          {
  811. X        /* First nonexact match found.  */
  812. X        pfound = p;
  813. X        indfound = option_index;
  814. X          }
  815. X        else
  816. X          /* Second nonexact match found.  */
  817. X          ambig = 1;
  818. X      }
  819. X
  820. X      if (ambig && !exact)
  821. X    {
  822. X      fprintf (stderr, "%s: option `%s' is ambiguous\n",
  823. X           argv[0], argv[optind]);
  824. X      nextchar += strlen (nextchar);               
  825. X      return '?';
  826. X    }
  827. X
  828. X      if (pfound != 0)
  829. X    {
  830. X      option_index = indfound;
  831. X      optind++;
  832. X      if (*s)
  833. X        {
  834. X          if (pfound->has_arg > 0)
  835. X        optarg = s + 1;
  836. X          else
  837. X        {
  838. X          fprintf (stderr,
  839. X               "%s: option `%c%s' doesn't allow an argument\n",
  840. X               argv[0], argv[optind - 1][0], pfound->name);
  841. X          nextchar += strlen (nextchar);               
  842. X          return '?';
  843. X        }
  844. X        }
  845. X      else if (pfound->has_arg)
  846. X        {
  847. X          if (optind < argc)
  848. X        optarg = argv[optind++];
  849. X          else if (pfound->has_arg != 2)
  850. X        {
  851. X          fprintf (stderr, "%s: option `%s' requires an argument\n",
  852. X               argv[0], argv[optind - 1]);
  853. X          nextchar += strlen (nextchar);           
  854. X          return '?';
  855. X        }
  856. X        }
  857. #if 0
  858. X      _getopt_option_name = pfound->name;
  859. #endif
  860. X      nextchar += strlen (nextchar);
  861. X      if (pfound->flag)
  862. X        *(pfound->flag) = pfound->val;
  863. X      return 0;
  864. X    }
  865. X      /* Can't find it as a long option.  If this is getopt_long_only,
  866. X     and the option starts with '-' and is a valid short
  867. X     option, then interpret it as a short option.  Otherwise it's
  868. X     an error.  */
  869. X      if (_getopt_long_only == 0 || argv[optind][0] == '+' ||
  870. X      index (optstring, *nextchar) == 0)
  871. X    {
  872. X      if (opterr != 0)
  873. X        fprintf (stderr, "%s: unrecognized option `%c%s'\n",
  874. X             argv[0], argv[optind][0], nextchar);
  875. X      nextchar += strlen (nextchar);           
  876. X      return '?';
  877. X    }
  878. X    }
  879. X  /* Look at and handle the next option-character.  */
  880. X
  881. X  {
  882. X    char c = *nextchar++;
  883. X    char *temp = index (optstring, c);
  884. X
  885. X    /* Increment `optind' when we start to process its last character.  */
  886. X    if (*nextchar == 0)
  887. X      optind++;
  888. X
  889. X    if (temp == 0 || c == ':')
  890. X      {
  891. X    if (opterr != 0)
  892. X      {
  893. X        if (c < 040 || c >= 0177)
  894. X          fprintf (stderr, "%s: unrecognized option, character code 0%o\n",
  895. X               argv[0], c);
  896. X        else
  897. X          fprintf (stderr, "%s: unrecognized option `-%c'\n",
  898. X               argv[0], c);
  899. X      }
  900. X    return '?';
  901. X      }
  902. X    if (temp[1] == ':')
  903. X      {
  904. X    if (temp[2] == ':')
  905. X      {
  906. X        /* This is an option that accepts an argument optionally.  */
  907. X        if (*nextchar != 0)
  908. X          {
  909. X            optarg = nextchar;
  910. X        optind++;
  911. X          }
  912. X        else
  913. X          optarg = 0;
  914. X        nextchar = 0;
  915. X      }
  916. X    else
  917. X      {
  918. X        /* This is an option that requires an argument.  */
  919. X        if (*nextchar != 0)
  920. X          {
  921. X        optarg = nextchar;
  922. X        /* If we end this ARGV-element by taking the rest as an arg,
  923. X           we must advance to the next element now.  */
  924. X        optind++;
  925. X          }
  926. X        else if (optind == argc)
  927. X          {
  928. X        if (opterr != 0)
  929. X          fprintf (stderr, "%s: option `-%c' requires an argument\n",
  930. X               argv[0], c);
  931. X        c = '?';
  932. X          }
  933. X        else
  934. X          /* We already incremented `optind' once;
  935. X         increment it again when taking next ARGV-elt as argument.  */
  936. X          optarg = argv[optind++];
  937. X        nextchar = 0;
  938. X      }
  939. X      }
  940. X    return c;
  941. X  }
  942. }
  943. X
  944. #ifdef TEST
  945. X
  946. /* Compile with -DTEST to make an executable for use in testing
  947. X   the above definition of `getopt'.  */
  948. X
  949. int
  950. main (argc, argv)
  951. X     int argc;
  952. X     char **argv;
  953. {
  954. X  char c;
  955. X  int digit_optind = 0;
  956. X
  957. X  while (1)
  958. X    {
  959. X      int this_option_optind = optind;
  960. X      if ((c = getopt (argc, argv, "abc:d:0123456789")) == EOF)
  961. X    break;
  962. X
  963. X      switch (c)
  964. X    {
  965. X    case '0':
  966. X    case '1':
  967. X    case '2':
  968. X    case '3':
  969. X    case '4':
  970. X    case '5':
  971. X    case '6':
  972. X    case '7':
  973. X    case '8':
  974. X    case '9':
  975. X      if (digit_optind != 0 && digit_optind != this_option_optind)
  976. X        printf ("digits occur in two different argv-elements.\n");
  977. X      digit_optind = this_option_optind;
  978. X      printf ("option %c\n", c);
  979. X      break;
  980. X
  981. X    case 'a':
  982. X      printf ("option a\n");
  983. X      break;
  984. X
  985. X    case 'b':
  986. X      printf ("option b\n");
  987. X      break;
  988. X
  989. X    case 'c':
  990. X      printf ("option c with value `%s'\n", optarg);
  991. X      break;
  992. X
  993. X    case '?':
  994. X      break;
  995. X
  996. X    default:
  997. X      printf ("?? getopt returned character code 0%o ??\n", c);
  998. X    }
  999. X    }
  1000. X
  1001. X  if (optind < argc)
  1002. X    {
  1003. X      printf ("non-option ARGV-elements: ");
  1004. X      while (optind < argc)
  1005. X    printf ("%s ", argv[optind++]);
  1006. X      printf ("\n");
  1007. X    }
  1008. X
  1009. X  return 0;
  1010. }
  1011. X
  1012. #endif /* TEST */
  1013. SHAR_EOF
  1014. chmod 0640 getopt.c ||
  1015. echo 'restore of getopt.c failed'
  1016. Wc_c="`wc -c < 'getopt.c'`"
  1017. test 16224 -eq "$Wc_c" ||
  1018.     echo 'getopt.c: original size 16224, current size' "$Wc_c"
  1019. rm -f _shar_wnt_.tmp
  1020. fi
  1021. # ============= getopt.h ==============
  1022. if test -f 'getopt.h' -a X"$1" != X"-c"; then
  1023.     echo 'x - skipping getopt.h (File already exists)'
  1024.     rm -f _shar_wnt_.tmp
  1025. else
  1026. > _shar_wnt_.tmp
  1027. echo 'x - extracting getopt.h (Text)'
  1028. sed 's/^X//' << 'SHAR_EOF' > 'getopt.h' &&
  1029. /* declarations for getopt
  1030. X   Copyright (C) 1989 Free Software Foundation, Inc.
  1031. X
  1032. X   This program is free software; you can redistribute it and/or modify
  1033. X   it under the terms of the GNU General Public License as published by
  1034. X   the Free Software Foundation; either version 1, or (at your option)
  1035. X   any later version.
  1036. X
  1037. X   This program is distributed in the hope that it will be useful,
  1038. X   but WITHOUT ANY WARRANTY; without even the implied warranty of
  1039. X   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1040. X   GNU General Public License for more details.
  1041. X
  1042. X   You should have received a copy of the GNU General Public License
  1043. X   along with this program; if not, write to the Free Software
  1044. X   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  1045. X
  1046. /* For communication from `getopt' to the caller.
  1047. X   When `getopt' finds an option that takes an argument,
  1048. X   the argument value is returned here.
  1049. X   Also, when `ordering' is RETURN_IN_ORDER,
  1050. X   each non-option ARGV-element is returned here.  */
  1051. X
  1052. extern char *optarg;
  1053. X
  1054. /* Index in ARGV of the next element to be scanned.
  1055. X   This is used for communication to and from the caller
  1056. X   and for communication between successive calls to `getopt'.
  1057. X
  1058. X   On entry to `getopt', zero means this is the first call; initialize.
  1059. X
  1060. X   When `getopt' returns EOF, this is the index of the first of the
  1061. X   non-option elements that the caller should itself scan.
  1062. X
  1063. X   Otherwise, `optind' communicates from one call to the next
  1064. X   how much of ARGV has been scanned so far.  */
  1065. X
  1066. extern int optind;
  1067. X
  1068. /* Callers store zero here to inhibit the error message `getopt' prints
  1069. X   for unrecognized options.  */
  1070. X
  1071. extern int opterr;
  1072. X
  1073. /* Describe the long-named options requested by the application.
  1074. X   _GETOPT_LONG_OPTIONS is a vector of `struct option' terminated by an
  1075. X   element containing a name which is zero.
  1076. X   The field `has_arg' is:
  1077. X   0 if the option does not take an argument,
  1078. X   1 if the option requires an argument,
  1079. X   2 if the option takes an optional argument.
  1080. X   If the field `flag' is nonzero, it points to a variable that is set to
  1081. X   the value given in the field `val' when the option is found, but
  1082. X   left unchanged if the option is not found.  */
  1083. X
  1084. struct option
  1085. {
  1086. X  char *name;
  1087. X  int has_arg;
  1088. X  int *flag;
  1089. X  int val;
  1090. };
  1091. X
  1092. extern struct option *_getopt_long_options;
  1093. X
  1094. /* If nonzero, tell getopt that '-' means a long option.
  1095. X   Set by getopt_long_only.  */
  1096. extern int _getopt_long_only;
  1097. X
  1098. #if 0
  1099. /* Name of long-named option actually found.
  1100. X   Only changed when a long-named option is found.
  1101. X   Set to zero when returning a non-option arg in `optarg'.  */
  1102. X
  1103. extern char *_getopt_option_name;
  1104. #endif
  1105. X
  1106. /* The index in GETOPT_LONG_OPTIONS of the long-named option found.
  1107. X   Only valid when a long-named option has been found by the most
  1108. X   recent call to `getopt'.  */
  1109. X
  1110. extern int option_index;
  1111. X
  1112. #ifdef __STDC__
  1113. int getopt (int, char **, char *);
  1114. int getopt_long (int, char **, char *, struct option *, int *);
  1115. int getopt_long_only (int, char **, char *, struct option *, int *);
  1116. #else
  1117. int getopt ();
  1118. int getopt_long ();
  1119. int getopt_long_only ();
  1120. #endif
  1121. SHAR_EOF
  1122. chmod 0640 getopt.h ||
  1123. echo 'restore of getopt.h failed'
  1124. Wc_c="`wc -c < 'getopt.h'`"
  1125. test 3097 -eq "$Wc_c" ||
  1126.     echo 'getopt.h: original size 3097, current size' "$Wc_c"
  1127. rm -f _shar_wnt_.tmp
  1128. fi
  1129. # ============= getopt1.c ==============
  1130. if test -f 'getopt1.c' -a X"$1" != X"-c"; then
  1131.     echo 'x - skipping getopt1.c (File already exists)'
  1132.     rm -f _shar_wnt_.tmp
  1133. else
  1134. > _shar_wnt_.tmp
  1135. echo 'x - extracting getopt1.c (Text)'
  1136. sed 's/^X//' << 'SHAR_EOF' > 'getopt1.c' &&
  1137. /* Getopt for GNU.
  1138. X   Copyright (C) 1987, 1989 Free Software Foundation, Inc.
  1139. X
  1140. X   This program is free software; you can redistribute it and/or modify
  1141. X   it under the terms of the GNU General Public License as published by
  1142. X   the Free Software Foundation; either version 1, or (at your option)
  1143. X   any later version.
  1144. X
  1145. X   This program is distributed in the hope that it will be useful,
  1146. X   but WITHOUT ANY WARRANTY; without even the implied warranty of
  1147. X   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1148. X   GNU General Public License for more details.
  1149. X
  1150. X   You should have received a copy of the GNU General Public License
  1151. X   along with this program; if not, write to the Free Software
  1152. X   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  1153. X
  1154. X
  1155. X
  1156. #include "getopt.h"
  1157. X
  1158. int
  1159. getopt_long (argc, argv, options, long_options, opt_index)
  1160. X     int argc;
  1161. X     char **argv;
  1162. X     char *options;
  1163. X     struct option *long_options;
  1164. X     int *opt_index;
  1165. {
  1166. X  int val;
  1167. X  _getopt_long_options = long_options;
  1168. X  val = getopt (argc, argv, options);
  1169. X  if (val == 0)
  1170. X    *opt_index = option_index;
  1171. X  return val;
  1172. }
  1173. X
  1174. /* Like getopt_long, but there are no short options.  That is,
  1175. X   '-' as well as '+' indicates a long option.  Of course, long_options
  1176. X   can contain single character options but '-ab' is not the same as
  1177. X   '-a -b'.  */
  1178. int getopt_long_only (argc, argv, options, long_options, opt_index)
  1179. X     int argc;
  1180. X     char **argv;
  1181. X     char *options;
  1182. X     struct option *long_options;
  1183. X     int *opt_index;
  1184. {
  1185. X  int val;
  1186. X  _getopt_long_options = long_options;
  1187. X  _getopt_long_only = 1;
  1188. X  val = getopt (argc, argv, options);
  1189. X  if (val == 0)
  1190. X    *opt_index = option_index;
  1191. X  return val;
  1192. }
  1193. X     
  1194. X
  1195. #ifdef TEST
  1196. X
  1197. #include <stdio.h>
  1198. X
  1199. int
  1200. main (argc, argv)
  1201. X     int argc;
  1202. X     char **argv;
  1203. {
  1204. X  char c;
  1205. X  int digit_optind = 0;
  1206. X
  1207. X  while (1)
  1208. X    {
  1209. X      int this_option_optind = optind;
  1210. X      char *name = '\0';
  1211. X      int option_index = 0;
  1212. X      static struct option long_options[]
  1213. X    = {{ "add", 1, 0, 0 },
  1214. X       { "append", 0, 0, 0 },
  1215. X       { "delete", 1, 0, 0 },
  1216. X       { "verbose", 0, 0, 0 },
  1217. X       { "create", 0, 0, 0 },
  1218. X       { "file", 1, 0, 0 },
  1219. X       { 0, 0, 0, 0}};
  1220. X
  1221. X      c = getopt_long (argc, argv, "abc:d:0123456789",
  1222. X               long_options, &option_index);
  1223. X      if (c == EOF)
  1224. X    break;
  1225. X    switch (c)
  1226. X      {
  1227. X      case 0:
  1228. X        printf ("option %s", (long_options[option_index]).name);
  1229. X        if (optarg)
  1230. X          printf (" with arg %s", optarg);
  1231. X        printf ("\n");
  1232. X        break;
  1233. X
  1234. X      case '0':
  1235. X      case '1':
  1236. X      case '2':
  1237. X      case '3':
  1238. X      case '4':
  1239. X      case '5':
  1240. X      case '6':
  1241. X      case '7':
  1242. X      case '8':
  1243. X      case '9':
  1244. X        if (digit_optind != 0 && digit_optind != this_option_optind)
  1245. X          printf ("digits occur in two different argv-elements.\n");
  1246. X        digit_optind = this_option_optind;
  1247. X        printf ("option %c\n", c);
  1248. X        break;
  1249. X
  1250. X      case 'a':
  1251. X        printf ("option a\n");
  1252. X        break;
  1253. X
  1254. X      case 'b':
  1255. X        printf ("option b\n");
  1256. X        break;
  1257. X
  1258. X      case 'c':
  1259. X        printf ("option c with value `%s'\n", optarg);
  1260. X        break;
  1261. X
  1262. X      case '?':
  1263. X        break;
  1264. X
  1265. X      default:
  1266. X        printf ("?? getopt returned character code 0%o ??\n", c);
  1267. X      }
  1268. X    }
  1269. X
  1270. X  if (optind < argc)
  1271. X    {
  1272. X      printf ("non-option ARGV-elements: ");
  1273. X      while (optind < argc)
  1274. X    printf ("%s ", argv[optind++]);
  1275. X      printf ("\n");
  1276. X    }
  1277. X
  1278. X  return 0;
  1279. }
  1280. X
  1281. #endif /* TEST */
  1282. SHAR_EOF
  1283. chmod 0640 getopt1.c ||
  1284. echo 'restore of getopt1.c failed'
  1285. Wc_c="`wc -c < 'getopt1.c'`"
  1286. test 3291 -eq "$Wc_c" ||
  1287.     echo 'getopt1.c: original size 3291, current size' "$Wc_c"
  1288. rm -f _shar_wnt_.tmp
  1289. fi
  1290. # ============= main.c ==============
  1291. if test -f 'main.c' -a X"$1" != X"-c"; then
  1292.     echo 'x - skipping main.c (File already exists)'
  1293.     rm -f _shar_wnt_.tmp
  1294. else
  1295. > _shar_wnt_.tmp
  1296. echo 'x - extracting main.c (Text)'
  1297. sed 's/^X//' << 'SHAR_EOF' > 'main.c' &&
  1298. /*
  1299. X *  This is a flex input file but should be edited in -*-C-*- mode
  1300. X *
  1301. X *  C++2LaTeX: Produce prettyprinted LaTeX files from  C++ or C sources.
  1302. X *  Copyright (C) 1990 Norbert Kiesel
  1303. X *
  1304. X *  This program is free software; you can redistribute it and/or modify
  1305. X *  it under the terms of the GNU General Public License as published by
  1306. X *  the Free Software Foundation; either version 1, or (at your option)
  1307. X *  any later version.
  1308. X *
  1309. X *  This program is distributed in the hope that it will be useful,
  1310. X *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  1311. X *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1312. X *  GNU General Public License for more details.
  1313. X *
  1314. X *  You should have received a copy of the GNU General Public License
  1315. X *  along with this program; if not, write to the Free Software
  1316. X *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1317. X *
  1318. X *  Norbert Kiesel
  1319. X *  RWTH Aachen / Institut f. Informatik III
  1320. X *  Ahornstr. 55
  1321. X *  D-5100 Aachen
  1322. X *  West Germany
  1323. X *
  1324. X *  Phone:  +49 241 80-7266
  1325. X *  EUNET:  norbert@rwthi3.uucp
  1326. X *  USENET: ...!mcvax!unido!rwthi3!norbert
  1327. X *  X.400:  norbert@rwthi3.informatik.rwth-aachen.de
  1328. X *
  1329. X *  Please contact me for any bugs you find in this code or any
  1330. X *  improvements! I'd also be very happy to get feedback where and
  1331. X *  how frequently this program is used (just drop a little mail :-).
  1332. X *
  1333. X *  ---------------------------------------------------------------------------------
  1334. X *
  1335. X *  C++2LaTeX 2.0: Produce even more prettyprinted LaTeX files from C++ or C sources.
  1336. X *
  1337. X *  Copyright (C) 1991 Joerg Heitkoetter
  1338. X *  Systems Analysis Research Group, University of Dortmund
  1339. X *  (heitkoet@gorbi.informatik.uni-dortmund.de).
  1340. X *
  1341. X */
  1342. X
  1343. void
  1344. substitute (input)
  1345. char   *input;
  1346. {
  1347. X    while (*input) {
  1348. X        switch (*input) {
  1349. X            case '_':
  1350. X            case '&':
  1351. X            case '#':
  1352. X            case '$':
  1353. X            case '%':
  1354. X            case '{':
  1355. X            case '}':
  1356. X                printf ("\\%c", *input);
  1357. X                break;
  1358. X            case '+':
  1359. X            case '=':
  1360. X            case '<':
  1361. X            case '>':
  1362. X                printf ("$%c$", *input);
  1363. X                break;
  1364. X            case '*':
  1365. X                printf ("$\\ast$");
  1366. X                break;
  1367. X            case '|':
  1368. X                printf ("$\\mid$");
  1369. X                break;
  1370. X            case '\\':
  1371. X                printf ("$\\backslash$");
  1372. X                break;
  1373. X            case '^':
  1374. X                printf ("$\\wedge$");
  1375. X                break;
  1376. X            case '~':
  1377. X                printf ("$\\sim$");
  1378. X                break;
  1379. X            default:
  1380. X                printf ("%c", *input);
  1381. X                break;
  1382. X        }
  1383. X        input++;
  1384. X    }
  1385. }
  1386. X
  1387. void
  1388. indent (blanks)
  1389. char   *blanks;
  1390. {
  1391. X    int     i;
  1392. X
  1393. X    i = 0;
  1394. X    while (*blanks) {
  1395. X        if (*blanks == ' ') {
  1396. X            i++;
  1397. X        } else {           /* *blanks == '\t' */
  1398. X            while (++i % tabtotab);
  1399. X        }
  1400. X        blanks++;
  1401. X    }
  1402. X    printf ("\\hspace*{%d\\indentation}", i);
  1403. }
  1404. X
  1405. #include "getopt.h"
  1406. #include <string.h>
  1407. #include <fcntl.h>
  1408. #include <ctype.h>
  1409. #include <time.h>
  1410. X
  1411. extern char *version_string;
  1412. X
  1413. static struct option opts[] =
  1414. {
  1415. X    {"ansi-c", 0, 0, 'a'},
  1416. X    {"complete-file", 0, 0, 'c'},
  1417. X    {"font-size", 1, 0, 's'},
  1418. X    {"indentation", 1, 0, 'i'},
  1419. X    {"header", 0, 0, 'h'},
  1420. X    {"piped", 0, 0, 'p'},
  1421. X    {"alignment", 0, 0, 'n'},      /* turn off comment alignment  -joke */
  1422. X    {"output", 1, 0, 'o'},
  1423. X    {"tabstop", 1, 0, 'T'},
  1424. X    {"brace-tab", 1, 0, 'b'},      /* added new tabtobrace  -joke */
  1425. X    {"comment-font", 1, 0, 'C'},
  1426. X    {"string-font", 1, 0, 'S'},
  1427. X    {"keyword-font", 1, 0, 'K'},
  1428. X    {"header-font", 1, 0, 'H'},
  1429. X    {"cpp-font", 1, 0, 'P'},
  1430. X    {"version", 0, 0, 'V'},
  1431. X    {0, 0, 0, 0}
  1432. };
  1433. X
  1434. X
  1435. main (argc, argv)
  1436. int     argc;
  1437. char  **argv;
  1438. {
  1439. X    int     c;
  1440. X    int     index;
  1441. X    int     i;
  1442. X    int     has_filename;
  1443. X    char   *input_name;
  1444. X    char   *output_name;
  1445. X    char   *program_name;
  1446. X    long    now;
  1447. X    char   *today;
  1448. X    char   *malloc ();
  1449. X
  1450. X    input_name = "Standard Input";
  1451. X    output_name = 0;
  1452. X
  1453. X    now = time (0);
  1454. X    today = ctime (&now);
  1455. X
  1456. X    program_name = strrchr (argv[0], '/');
  1457. X    if (program_name == NULL) {    /* no pathname */
  1458. X        program_name = argv[0];
  1459. X    } else {
  1460. X        program_name++;
  1461. X    }
  1462. X
  1463. X /* simple heuristic: '+' in name means C++ */
  1464. X    cplusplus_mode = (strchr (program_name, '+') != 0);
  1465. X
  1466. X    if (argc == 1)
  1467. X        usage (program_name);  /* added exit with usage  -joke */
  1468. X
  1469. X    while ((c = getopt_long (argc, argv,
  1470. X                 "acpno:s:i:b:hT:C:H:S:K:P:V", opts, &index))
  1471. X            != EOF) {
  1472. X        if (c == 0) {           /* Long option */
  1473. X            c = opts[index].val;
  1474. X        }
  1475. X        switch (c) {
  1476. X            case 'a':
  1477. X                cplusplus_mode = 0;
  1478. X                break;
  1479. X            case 'c':
  1480. X                complete_file = 1;
  1481. X                break;
  1482. X            case 'o':
  1483. X                if (piped) {
  1484. X                    fprintf (stderr,
  1485. X                         "%s: Can't use {-p,+pipe} and {-o,+output} together\n",
  1486. X                         program_name);
  1487. X                    exit (5);
  1488. X                }
  1489. X                output_name = optarg;
  1490. X                break;
  1491. X            case 'n':
  1492. X                aligntoright = 0;
  1493. X                break;
  1494. X            case 's':
  1495. X                font_size = optarg;
  1496. X                break;
  1497. X            case 'i':
  1498. X                indentation = optarg;
  1499. X                break;
  1500. X            case 'b':
  1501. X                tabtobrace = atoi (optarg);
  1502. X                break;
  1503. X            case 'T':
  1504. X                tabtotab = atoi (optarg);
  1505. X                break;
  1506. X            case 'p':
  1507. X                if (output_name != 0) {
  1508. X                    fprintf (stderr,
  1509. X                         "%s: Can't use {-p,+pipe} and {-o,+output} together\n",
  1510. X                         program_name);
  1511. X                    exit (5);
  1512. X                }
  1513. X                piped = 1;
  1514. X                break;
  1515. X            case 'h':
  1516. X                header = 1;
  1517. X                complete_file = 1;    /* header implies
  1518. X                             * complete-file */
  1519. X                break;
  1520. X            case 'C':
  1521. X                comment_font = optarg;
  1522. X                break;
  1523. X            case 'H':
  1524. X                header_font = optarg;
  1525. X                break;
  1526. X            case 'P':
  1527. X                cpp_font = optarg;
  1528. X                break;
  1529. X            case 'S':
  1530. X                string_font = optarg;
  1531. X                break;
  1532. X            case 'K':
  1533. X                keyword_font = optarg;
  1534. X                break;
  1535. X            case 'V':
  1536. X                fprintf (stderr, "%s\n", version_string);
  1537. X                break;
  1538. X            default:
  1539. X                usage (program_name);
  1540. X        }
  1541. X    }
  1542. X    has_filename = (argc - optind == 1);
  1543. X    if (has_filename) {           /* last argument is input file name */
  1544. X        input_name = argv[optind];
  1545. X        if (freopen (input_name, "r", stdin) == NULL) {
  1546. X            fprintf (stderr, "%s: Can't open `%s' for reading\n",
  1547. X                 program_name, input_name);
  1548. X            exit (2);
  1549. X        }
  1550. X    }
  1551. X    if ((output_name == 0) && !piped) {
  1552. X        char   *tmp;
  1553. X        if (has_filename) {
  1554. X            tmp = strrchr (input_name, '/');
  1555. X            if (tmp == 0) {    /* plain filename */
  1556. X                tmp = input_name;
  1557. X            } else {
  1558. X                tmp++;
  1559. X            }
  1560. X        } else {
  1561. X            tmp = program_name;
  1562. X        }
  1563. X        output_name = malloc (strlen (tmp) + 4);
  1564. X        if (output_name == 0) {
  1565. X            fprintf (stderr, "%s: Virtual memory exhausted\n", program_name);
  1566. X            exit (3);
  1567. X        }
  1568. X        strcpy (output_name, tmp);
  1569. X        strcat (output_name, ".tex");
  1570. X    }
  1571. X    if (!piped) {
  1572. X        if (freopen (output_name, "w", stdout) == NULL) {
  1573. X            fprintf (stderr, "%s: Can't open `%s' for writing\n",
  1574. X                 program_name, output_name);
  1575. X            exit (3);
  1576. X        }
  1577. X    }
  1578. X    printf ("\
  1579. %%\n\
  1580. %% This file was automatically produced at %.24s by\n\
  1581. %% %s", today, program_name);
  1582. X    for (i = 1; i < argc; i++) {
  1583. X        printf (" %s", argv[i]);
  1584. X    }
  1585. X    if (!has_filename) {
  1586. X        printf (" (from Standard Input)");
  1587. X    }
  1588. X    printf ("\n%%\n");
  1589. X    if (complete_file) {
  1590. X        if (header) {
  1591. X            if (strcmp (font_size, "10") == 0) {
  1592. X                printf ("\\documentstyle[fancyheadings]{article}\n");
  1593. X            } else {
  1594. X                printf ("\\documentstyle[%spt,fancyheadings]{article}\n",
  1595. X                    font_size);
  1596. X            }
  1597. X        } else {
  1598. X            if (strcmp (font_size, "10") == 0) {
  1599. X                printf ("\\documentstyle{article}\n");
  1600. X            } else {
  1601. X                printf ("\\documentstyle[%spt]{article}\n", font_size);
  1602. X            }
  1603. X        }
  1604. X        printf ("\\setlength{\\textwidth}{16cm}\n");
  1605. X        printf ("\\setlength{\\textheight}{23cm}\n");
  1606. X        printf ("\\setlength{\\hoffset}{-2cm}\n");
  1607. X        printf ("\\setlength{\\voffset}{-2cm}\n");
  1608. X        if (header) {
  1609. X            printf ("\\lhead{\\%s ", header_font);
  1610. X            substitute (input_name);
  1611. X            printf ("}");
  1612. X            printf ("\\rhead{\\rm\\thepage}\n");
  1613. X            printf ("\\cfoot{}\n");
  1614. X            printf ("\\addtolength{\\headheight}{14pt}\n");
  1615. X            printf ("\\pagestyle{fancy}\n");
  1616. X        }
  1617. X        printf ("\\begin{document}\n");
  1618. X    }
  1619. X    printf ("\\expandafter\\ifx\\csname indentation\\endcsname\\relax%\n");
  1620. X    printf ("\\newlength{\\indentation}\\fi\n");
  1621. X    printf ("\\setlength{\\indentation}{%s}\n", indentation);
  1622. X    printf ("\\begin{flushleft}\n");
  1623. X    yylex ();
  1624. X    printf ("\\end{flushleft}\n");
  1625. X    if (complete_file) {
  1626. X        printf ("\\end{document}\n");
  1627. X    }
  1628. X    exit (0);
  1629. }
  1630. X
  1631. void
  1632. usage (name)
  1633. char   *name;
  1634. {
  1635. X    fprintf (stderr, "%s\n", version_string);
  1636. X    fprintf (stderr, "\
  1637. Usage: %s [options] file\n\n\
  1638. Options:\n\
  1639. X    [-a]            [-b]\n\
  1640. X    [-c]            [-h]\n\
  1641. X    [-i length]        [-n]\n\
  1642. X    [-o file]        [-p]\n\
  1643. X    [-s fontsize]        [-C font]\n\
  1644. X    [-H font]        [-K font]\n\
  1645. X    [-P font]        [-S font]\n\
  1646. X    [-T tabulatorwidth]    [-V]\n\
  1647. X    \n\
  1648. X    [+ansi-c]        [+brace-tab indentation]\n\
  1649. X    [+complete-file]    [+header]\n\
  1650. X    [+indentation length]    [+no-alignment]\n\
  1651. X    [+output file]        [+pipe]\n\
  1652. X    [+font-size size]    [+comment-font font]\n\
  1653. X    [+keyword-font font]    [+cpp-font font]\n\
  1654. X    [+header-font font]    [+string-font font]\n\
  1655. X    [+tabstop width]    [+version]\n", name);
  1656. X    exit (1);
  1657. }
  1658. SHAR_EOF
  1659. chmod 0640 main.c ||
  1660. echo 'restore of main.c failed'
  1661. Wc_c="`wc -c < 'main.c'`"
  1662. test 8338 -eq "$Wc_c" ||
  1663.     echo 'main.c: original size 8338, current size' "$Wc_c"
  1664. rm -f _shar_wnt_.tmp
  1665. fi
  1666. # ============= version.c ==============
  1667. if test -f 'version.c' -a X"$1" != X"-c"; then
  1668.     echo 'x - skipping version.c (File already exists)'
  1669.     rm -f _shar_wnt_.tmp
  1670. else
  1671. > _shar_wnt_.tmp
  1672. echo 'x - extracting version.c (Text)'
  1673. sed 's/^X//' << 'SHAR_EOF' > 'version.c' &&
  1674. char * version_string = "c++2latex Version 2.0, Patchlevel 0";
  1675. /* used to be Version 1.1 Patchlevel 4 */
  1676. SHAR_EOF
  1677. chmod 0640 version.c ||
  1678. echo 'restore of version.c failed'
  1679. Wc_c="`wc -c < 'version.c'`"
  1680. test 105 -eq "$Wc_c" ||
  1681.     echo 'version.c: original size 105, current size' "$Wc_c"
  1682. rm -f _shar_wnt_.tmp
  1683. fi
  1684. rm -f _shar_seq_.tmp
  1685. echo You have unpacked the last part
  1686. exit 0
  1687. exit 0 # Just in case...
  1688. -- 
  1689. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1690. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1691. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1692. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1693.