home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume39 / par / part02 < prev    next >
Encoding:
Text File  |  1993-08-21  |  41.7 KB  |  1,620 lines

  1. Newsgroups: comp.sources.misc
  2. From: amc@wuecl.wustl.edu (Adam Costello)
  3. Subject: v39i041:  par - paragraph reformatter, v1.30, Part02/03
  4. Message-ID: <1993Aug22.014902.29581@sparky.sterling.com>
  5. X-Md4-Signature: e666e0037263feb1153e34077ce65809
  6. Sender: kent@sparky.sterling.com (Kent Landfield)
  7. Organization: Sterling Software
  8. Date: Sun, 22 Aug 1993 01:49:02 GMT
  9. Approved: kent@sparky.sterling.com
  10.  
  11. Submitted-by: amc@wuecl.wustl.edu (Adam Costello)
  12. Posting-number: Volume 39, Issue 41
  13. Archive-name: par/part02
  14. Environment: ANSI-C
  15. Supersedes: par: Volume 38, Issue 114-116
  16.  
  17. #! /bin/sh
  18. # This is a shell archive.  Remove anything before this line, then unpack
  19. # it by saving it into a file and typing "sh file".  To overwrite existing
  20. # files, type "sh file -c".  You can also feed this as standard input via
  21. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  22. # will see the following message at the end:
  23. #        "End of shell archive."
  24. # Contents:  Par130 Par130/par.1 Par130/buffer.h Par130/buffer.c
  25. # Wrapped by amc@wuecl on Fri Aug 20 19:16:23 1993
  26. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  27. if test ! -d 'Par130' ; then
  28.     echo shar: Creating directory \"'Par130'\"
  29.     mkdir 'Par130'
  30. fi
  31. if test -f 'Par130/par.1' -a "${1}" != "-c" ; then 
  32.   echo shar: Will not clobber existing file \"'Par130/par.1'\"
  33. else
  34. echo shar: Extracting \"'Par130/par.1'\" \(31620 characters\)
  35. sed "s/^X//" >'Par130/par.1' <<'END_OF_FILE'
  36. X.\"*********************
  37. X.\"* par.1             *
  38. X.\"* for Par 1.30      *
  39. X.\"* Copyright 1993 by *
  40. X.\"* Adam M. Costello  *
  41. X.\"*********************
  42. X.\"
  43. X.\" This is nroff -man (or troff -man) code.
  44. X.\"
  45. X.TH par 1 "18 August 1993" "Par 1.30" "USER COMMANDS"
  46. X.SH NAME
  47. Xpar \- filter for reformatting paragraphs
  48. X.SH SYNOPSIS
  49. X.ds O \fR[\fP
  50. X.ds C \fR]\fP
  51. X.de OP
  52. X.BI \*O\ \\$1 \\$2\ \*C
  53. X..
  54. X.TP .5i
  55. X.B par
  56. X.na
  57. X.OP version
  58. X.OP h \*Ohang\*C
  59. X.OP p prefix
  60. X.OP s suffix
  61. X.OP w width
  62. X.OP d \*Odiv\*C
  63. X.OP f \*Ofit\*C
  64. X.OP g \*Oguess\*C
  65. X.OP j \*Ojust\*C
  66. X.OP l \*Olast\*C
  67. X.OP q \*Oquote\*C
  68. X.OP R \*OReport\*C
  69. X.OP t \*Otouch\*C
  70. X.br
  71. X.ad
  72. X.SH DESCRIPTION
  73. X.ie t .ds Q ``
  74. X.el .ds Q ""
  75. X.ie t .ds U ''
  76. X.el .ds U ""
  77. X.de IT
  78. X.LP
  79. X\h'-\w"\\$1\ "u'\\$1\ \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9
  80. X..
  81. X.LP
  82. X.B par
  83. Xis a filter which copies its input to its output,
  84. Xchanging all white characters (except newlines) to
  85. Xspaces, and reformatting each paragraph.  Paragraphs are
  86. Xdelimited by protected, blank, and vacant lines, (see the
  87. X.SM TERMINOLOGY
  88. Xsection for definitions), and
  89. Xoptionally by indentation (see the
  90. X.B d
  91. Xoption in the
  92. X.SM OPTIONS
  93. Xsection).
  94. X.LP
  95. XEach output paragraph is generated from the
  96. Xcorresponding input paragraph as follows:
  97. X.RS
  98. X.LP
  99. X.IT 1) An optional prefix and/or suffix
  100. Xis removed from each input line.
  101. X.IT 2) The remainder is divided into
  102. Xwords (delimited by spaces).
  103. X.IT 3) The words are joined into lines
  104. Xto make an eye-pleasing paragraph.
  105. X.IT 4) The prefixes and suffixes are reattached.
  106. X.RE
  107. X.LP
  108. XIf there are suffixes, spaces are inserted before
  109. Xthem so that they all end in the same column.
  110. X.SH TERMINOLOGY
  111. X.LP
  112. XMiscellaneous terms:
  113. X.RS
  114. X.IP error
  115. XA condition which causes
  116. X.B par
  117. Xto abort.  See the
  118. X.SM DIAGNOSTICS
  119. Xsection.
  120. X.IP IP
  121. XInput paragraph.
  122. X.IP OP
  123. XOutput paragraph.
  124. X.IP parameter
  125. XA symbol which may take on unsigned integral values.  There
  126. Xare several parameters whose values affect the behavior of
  127. X.BR par .
  128. XParameters can be assigned values
  129. Xusing command line options.
  130. X.RE
  131. X.LP
  132. XTypes of characters:
  133. X.RS
  134. X.IP "alphanumeric character"
  135. XAn upper case letter, lower case letter, or decimal digit.
  136. X.IP "body character"
  137. XA member of the set of characters defined by the
  138. X.SM PARBODY
  139. Xenvironment variable (see the
  140. X.SM ENVIRONMENT
  141. Xsection).
  142. X.IP "protective character"
  143. XA member  of the set of characters defined by the
  144. X.SM PARPROTECT
  145. Xenvironment variable (see the
  146. X.SM ENVIRONMENT
  147. Xsection).
  148. X.IP "quote character"
  149. XA member of the set of characters defined by the
  150. X.SM PARQUOTE
  151. Xenvironment variable (see the
  152. X.SM ENVIRONMENT
  153. Xsection).
  154. X.IP "terminal character"
  155. XA period, question mark, exclamation point, or colon.
  156. X.IP "white character"
  157. XA space, formfeed, newline, carriage
  158. Xreturn, tab, or vertical tab.
  159. X.RE
  160. X.LP
  161. XFunctions:
  162. X.RS
  163. X.IP comprelen
  164. XThe comprelen of a non-empty set of lines is the
  165. Xlength of the longest string of non-body characters
  166. Xappearing at the beginning of every line in the set.
  167. X.IP comsuflen
  168. XGiven a non-empty set
  169. X.I S
  170. Xof lines, let
  171. X.I p
  172. Xbe the comprelen of
  173. X.IR S .
  174. XLet
  175. X.I T
  176. Xbe the set of lines which result from stripping the first
  177. X.I p
  178. Xcharacters from each line in
  179. X.IR S .
  180. XLet
  181. X.I c
  182. Xbe the longest string of non-body characters
  183. Xappearing at the end of every line in
  184. X.IR T .
  185. XStrip
  186. X.I c
  187. Xof all initial spaces except the last.  The length of
  188. X.I c
  189. Xis the comsuflen of
  190. X.IR S .
  191. X.IP quoteprefix
  192. XThe quoteprefix of a line is the longest string of quote
  193. Xcharacters appearing at the beginning of the line, after
  194. Xthis string has been stripped of any trailing spaces.
  195. X.RE
  196. X.LP
  197. XTypes of lines:
  198. X.RS
  199. X.IP "blank line"
  200. XAn empty line, or a line whose first character is
  201. Xnot protective and which contains only spaces.
  202. X.IP "protected line"
  203. XAn input line whose first character is protective.
  204. X.IP "vacant line"
  205. XAny line which can be shown to be vacant by a finite number
  206. Xof applications of the following recursive rule:  Suppose
  207. X.I S
  208. Xis a subsequence of a segment (see below)
  209. Xbounded above and below by vacant lines or
  210. Xby the beginning/end of the segment.  Let
  211. X.I p
  212. Xand
  213. X.I s
  214. Xbe the comprelen and comsuflen of
  215. X.IR S .
  216. XAny member of
  217. X.I S
  218. Xwhich, if stripped of its first
  219. X.I p
  220. Xcharacters and last
  221. X.I s
  222. Xcharacters, would be blank, is vacant.
  223. X.RE
  224. X.LP
  225. XGroups of lines:
  226. X.RS
  227. X.IP segment
  228. XA contiguous sequence of input lines containing no protected
  229. Xor blank lines, bounded above and below by protected
  230. Xlines, blank lines, and/or the beginning/end of the input.
  231. X.IP block
  232. XA contiguous subsequence of a segment containing
  233. Xno vacant lines, bounded above and below by vacant
  234. Xlines and/or the beginning/end of the segment.
  235. X.RE
  236. X.LP
  237. XTypes of words:
  238. X.RS
  239. X.IP "capitalized word"
  240. XA word which contains at least one alphanumeric
  241. Xcharacter, whose first alphanumeric
  242. Xcharacter is not a lower case letter.
  243. X.IP "curious word"
  244. XA word which contains a terminal character
  245. X.I c
  246. Xsuch that there are no alphanumeric
  247. Xcharacters in the word after
  248. X.IR c ,
  249. Xbut there is at least one alphanumeric character in the word before
  250. X.IR c .
  251. X.RE
  252. X.SH OPTIONS
  253. X.LP
  254. XAny command line argument may begin with one minus
  255. Xsign (\-) which is ignored.  Generally, more
  256. Xthan one option may appear in a single command
  257. Xline argument, but there is one exception:  The
  258. X.B version
  259. Xoption must have a whole argument all to itself.
  260. X.TP 1i
  261. X.B version
  262. XCauses all remaining arguments to be ignored.  No input
  263. Xis read.  \*Qpar 1.30\*U is printed on the output.  Of
  264. Xcourse, this will change in future releases of Par.
  265. X.LP
  266. XAll remaining options are used to set values of
  267. Xparameters.  Values set by command line options hold
  268. Xfor all paragraphs.  Unset parameters are given default
  269. Xvalues.  Any unset parameters whose default values depend
  270. Xon the IP are recomputed separately for each paragraph.
  271. X.LP
  272. XThe approximate role of each
  273. Xvariable is described here.  See the
  274. X.SM DETAILS
  275. Xsection for the rest of the story.
  276. X.LP
  277. XThe first four parameters,
  278. X.IR hang ,
  279. X.IR prefix ,
  280. X.IR suffix ,
  281. Xand
  282. X.IR width ,
  283. Xmay be set to any unsigned decimal integer less than 10000.
  284. X.TP 1i
  285. X.BI h\fR[ hang\fR]
  286. XMainly affects the default values of
  287. X.I prefix
  288. Xand
  289. X.IR suffix .
  290. XDefaults to 0.  If the
  291. X.B h
  292. Xoption is given without a number, the
  293. Xvalue 1 is inferred.  (See also the
  294. X.B p
  295. Xand
  296. X.B s
  297. Xoptions.)
  298. X.TP
  299. X.BI p prefix
  300. XThe first
  301. X.I prefix
  302. Xcharacters of each line of the OP are copied from the first
  303. X.I prefix
  304. Xcharacters of the corresponding line
  305. Xof the IP.  If there are more than
  306. X.IR hang \ +\ 1
  307. Xlines in the IP, the default value is the comprelen
  308. Xof all the lines in the IP except the first
  309. X.I hang
  310. Xof them.  If there are exactly
  311. X.IR hang \ +\ 1
  312. Xlines in the IP and
  313. X.I quote
  314. Xis 1, the default value is the number of
  315. Xleading quote characters in the last line.
  316. XOtherwise, the default value is 0.  (See also the
  317. X.B h
  318. Xand
  319. X.B q
  320. Xoptions.)
  321. X.TP
  322. X.BI s suffix
  323. XThe last
  324. X.I suffix
  325. Xcharacters of each line of the OP are copied from the last
  326. X.I suffix
  327. Xcharacters of the corresponding line
  328. Xof the IP.  If there are more than
  329. X.IR hang \ +\ 1
  330. Xlines in the IP, the default value is the comsuflen
  331. Xof all the lines of the IP except the first
  332. X.I hang
  333. Xof them.  Otherwise the default value is 0.  (See also the
  334. X.B h
  335. Xoption.)
  336. X.TP
  337. X.BI w width
  338. XNo line in the OP will contain more than
  339. X.I width
  340. Xcharacters, not including the
  341. Xtrailing newlines.  Defaults to 72.
  342. X.LP
  343. XThe remaining eight parameters,
  344. X.IR div,
  345. X.IR fit,
  346. X.IR guess,
  347. X.IR just,
  348. X.IR last,
  349. X.IR quote,
  350. X.IR Report,
  351. Xand
  352. X.IR touch,
  353. Xmay be set to either 0 or 1.  If the number is
  354. Xabsent in the option, the value 1 is inferred.
  355. X.TP 1i
  356. X.BI d\fR[ div\fR]
  357. XIf
  358. X.I div
  359. Xis 0, then each block becames an IP.  If
  360. X.I div
  361. Xis 1, then each block is subdivided into IPs as follows:  Let
  362. X.I p
  363. Xbe the comprelen of the block.
  364. XLet a line's status be 1 if its
  365. X.RI ( p \ +\ 1)st
  366. Xcharacter is a space, 0 otherwise.  Every line in the
  367. Xblock whose status is the same as the status of the
  368. Xfirst line will begin a new paragraph.  Defaults to 0.
  369. X.TP
  370. X.BI f\fR[ fit\fR]
  371. XIf
  372. X.I fit
  373. Xis 1 and
  374. X.I just
  375. Xis 0,
  376. X.B par
  377. Xwill try to make the lines in the OP as nearly the
  378. Xsame length as possible, even if it means making
  379. Xthe OP narrower.  Defaults to 0.  (See also the
  380. X.B j
  381. Xoption.)
  382. X.TP
  383. X.BI g\fR[ guess\fR]
  384. XIf
  385. X.I guess
  386. Xis 1, then when
  387. X.B par
  388. Xis choosing line breaks, whenever it encounters a curious
  389. Xword followed by a capitalized word, it take one of two
  390. Xspecial actions.  If the two words are separated by a single
  391. Xspace in the input, they will be merged into one word with
  392. Xan embedded non-breaking space.  If the two words are
  393. Xseparated by more than one space, or by a line break, then
  394. X.B par
  395. Xwill insure that they are separated by two spaces,
  396. Xor by a line break, in the output.  Defaults to 0.
  397. X.TP
  398. X.BI j\fR[ just\fR]
  399. XIf
  400. X.I just
  401. Xis 1,
  402. X.B par
  403. Xjustifies the OP, inserting spaces between words
  404. Xso that all lines in the OP have length
  405. X.I width
  406. X(except the last, if
  407. X.I last
  408. Xis 0).
  409. X.I fit
  410. Xhas no effect if
  411. X.I just
  412. Xis 1.  Defaults to 0.  (See also the
  413. X.BR w ,
  414. X.BR l ,
  415. Xand
  416. X.B f
  417. Xoptions.)
  418. X.TP
  419. X.BI l\fR[ last\fR]
  420. XIf
  421. X.I last
  422. Xis 1,
  423. X.B par
  424. Xtries to make the last line of the OP about
  425. Xthe same length as the others.  Defaults to 0.
  426. X.TP
  427. X.BI q\fR[ quote\fR]
  428. XIf
  429. X.I quote
  430. Xis 1, then before each segment is scanned for vacant lines,
  431. X.B par
  432. Xwill insert some new lines as follows:  For each pair of
  433. Xadjacent lines in the segment, if the quoteprefix of one
  434. Xis a prefix of (but not the same as) the quoteprefix of
  435. Xthe other, and each of the two lines contains at least
  436. Xone non-quote character, then a line consisting of the
  437. Xsmaller quoteprefix will be inserted between the two lines.
  438. X.I quote
  439. Xalso affects the default value of
  440. X.IR prefix .
  441. XDefaults to 0.  (See also the
  442. X.B p
  443. Xoption.)
  444. X.TP
  445. X.BI R\fR[ Report\fR]
  446. XIf
  447. X.I Report
  448. Xis 1, then it will be considered an error
  449. Xfor an input word to contain more than
  450. X.IR L \ =
  451. X.RI ( width \ -
  452. X.IR prefix \ -
  453. X.IR suffix )
  454. Xcharacters.  Otherwise, such
  455. Xwords will be chopped after each
  456. X.IR L th
  457. Xcharacter into shorter words.  Defaults to 0.  It
  458. Xis recommended that this option be included in
  459. X.SM PARINIT
  460. X(see the
  461. X.SM ENVIRONMENT
  462. Xsection).
  463. X.TP
  464. X.BI t\fR[ touch\fR]
  465. XHas no effect if
  466. X.I suffix
  467. Xis 0 or
  468. X.I just
  469. Xis 1.  Otherwise, if
  470. X.I touch
  471. Xis 0, all lines in the OP have length
  472. X.IR width .
  473. XIf
  474. X.I touch
  475. Xis 1, then the length of the lines is decreased until the
  476. Xsuffixes touch the body of the OP.  Defaults to the logical
  477. X.SM OR
  478. Xof
  479. X.I fit
  480. Xand
  481. X.IR last .
  482. X(See also the
  483. X.BR s ,
  484. X.BR j ,
  485. X.BR w ,
  486. X.BR f ,
  487. Xand
  488. X.B l
  489. Xoptions.)
  490. X.LP
  491. XIf the value of any parameter is set more
  492. Xthan once, the last value is used.  When
  493. Xunset parameters are assigned default values,
  494. X.I hang
  495. Xand
  496. X.I quote
  497. Xare assigned before
  498. X.IR prefix ,
  499. Xand
  500. X.I fit
  501. Xand
  502. X.I last
  503. Xare assigned before
  504. X.I touch
  505. X(because of the dependencies).
  506. X.LP
  507. XIt is an error if
  508. X.I width
  509. X<=
  510. X.I prefix
  511. X+
  512. X.IR suffix .
  513. X.SH ENVIRONMENT
  514. X.TP 1i
  515. X.SM PARBODY
  516. XAll and only the characters appearing in the value of
  517. X.SM PARBODY
  518. Xcount as body characters (which are used for determining
  519. Xcomprelens and comsuflens).  The underscore is an escape
  520. Xcharacter, which may begin the following escape sequences:
  521. X.RS 2i
  522. X.IT __\ = an underscore
  523. X.IT _s\ = a space
  524. X.IT _A\ = all upper case letters
  525. X.IT _a\ = all lower case letters
  526. X.IT _0\ = all decimal digits
  527. X.IT _x\fIhh\fP\ = the character represented
  528. Xby the two hexadecimal digits
  529. X.I hh
  530. X(which may be upper or lower case)
  531. X.RE
  532. X.RS 1i
  533. X.LP
  534. XIf
  535. X.SM PARBODY
  536. Xis not set, there are no body characters.
  537. X.LP
  538. XA good value for
  539. X.SM PARBODY
  540. Xmight be \*Q_A_a.\*U, but it depends on the application.
  541. X.RE
  542. X.TP
  543. X.SM PARINIT
  544. XIf set,
  545. X.B par
  546. Xwill read command line options from
  547. X.SM PARINIT
  548. Xbefore it reads them from the command line.
  549. X.TP
  550. X.SM PARPROTECT
  551. XDetermines the set of protective
  552. Xcharacters, using the same syntax as
  553. X.SM PARBODY\s0.
  554. XIf
  555. X.SM PARPROTECT
  556. Xis not set, there are no protective characters.
  557. X.TP
  558. X.SM PARQUOTE
  559. XDetermines the set of quote characters, using the same syntax as
  560. X.SM PARBODY\s0.
  561. XIf
  562. X.SM PARQUOTE
  563. Xis not set, then the quote characters
  564. Xare the greater-than sign and the space.
  565. X.LP
  566. XIf a
  567. X.SM NUL
  568. Xcharacter appears in the value of an environment variable,
  569. Xit and the rest of the string will not be seen by
  570. X.BR par .
  571. X.SH DETAILS
  572. X.LP
  573. XLines are terminated by newline characters, but the
  574. Xnewlines are not considered to be included in the lines.
  575. XIf the last character of the input is a non-newline,
  576. Xthen a newline will be inferred immediately after
  577. Xit (but if the input is empty, no newline will be
  578. Xinferred; the number of input lines will be 0).  Thus,
  579. Xthe input can always be viewed as a sequence of lines.
  580. X.LP
  581. XProtected lines are copied unchanged from the input to the
  582. Xoutput.  All other input lines, as they are read, have any
  583. X.SM NUL
  584. Xcharacters removed, and every white character
  585. X(except newlines) turned into a space.
  586. X.LP
  587. XBlank lines in the input are transformed into empty
  588. Xlines in the output.  Vacant lines in the input are
  589. Xstripped of trailing spaces before being output.
  590. X.LP
  591. XThe input is divided into segments, which are
  592. Xdivided into blocks, which are divided into
  593. XIPs.  The exact process depends on the values of
  594. X.I quote
  595. Xand
  596. X.I div
  597. X(see
  598. X.B q
  599. Xand
  600. X.B d
  601. Xin the
  602. X.SM OPTIONS
  603. Xsection).  The remainder of this section describes
  604. Xthe process which is applied independently to
  605. Xeach IP to construct the corresponding OP.
  606. X.LP
  607. XAfter the values of the parameters are determined (see the
  608. X.SM OPTIONS
  609. Xsection), the first
  610. X.I prefix
  611. Xcharacters and the last
  612. X.I suffix
  613. Xcharacters of each input line are removed and remembered.
  614. XIt is an error for any line to contain fewer than
  615. X.IR prefix \ +\  suffix
  616. Xcharacters.
  617. X.LP
  618. XThe remaining text is treated as a sequence of
  619. Xcharacters, not lines.  The text is broken into
  620. Xwords, which are delimited by spaces.  That is, a
  621. Xword
  622. Xis a maximal sub-sequence of non-spaces.  If
  623. X.I guess
  624. Xis 1, then some words might be merged (see
  625. X.B g
  626. Xin the
  627. X.SM OPTIONS
  628. Xsection).  The first word includes any
  629. Xspaces that preceed it on the same line.
  630. X.LP
  631. XLet
  632. X.I L
  633. X=
  634. X.I width
  635. X\-
  636. X.I prefix
  637. X\-
  638. X.IR suffix .
  639. X.LP
  640. XIf
  641. X.I Report
  642. Xis 0, then some words may get chopped up at this point (see
  643. X.B R
  644. Xin the
  645. X.SM
  646. XOPTIONS
  647. Xsection).
  648. X.LP
  649. XThe words are reassembled, preserving
  650. Xtheir order, into lines.  If
  651. X.I just
  652. Xis 0, adjacent words within a line are separated
  653. Xby a single space, (or sometimes two if
  654. X.I guess
  655. Xis 1), and line breaks are chosen so that
  656. Xthe paragraph satisfies the following properties:
  657. X.RS 1i
  658. X.IT 1) No line contains more than
  659. X.I L
  660. Xcharacters.
  661. X.IT 2) If
  662. X.I fit
  663. Xis 1, the difference between the lengths of the
  664. Xshortest and longest lines is as small as possible.
  665. X.IT 3) The shortest line is as long as
  666. Xpossible, subject to properties 1 and 2.
  667. X.IT 4) Let
  668. X.I target
  669. Xbe
  670. X.I L
  671. Xif
  672. X.I fit
  673. Xis 0, or the length of the longest line if
  674. X.I fit
  675. Xis 1.  The sum of the squares of the differences between
  676. X.I target
  677. Xand the lengths of the lines is as small as
  678. Xpossible, subject to properties 1, 2, and 3.
  679. X.RE
  680. X.RS .5i
  681. X.LP
  682. XIf
  683. X.I last
  684. Xis 0, then the last line does not count as a line
  685. Xfor the purposes of properties 2, 3, and 4 above.
  686. X.LP
  687. XIf all the words fit on a single line, then
  688. Xthe properties as worded above don't make much
  689. Xsense.  In that case, no line breaks are inserted.
  690. X.RE
  691. X.LP
  692. XIf
  693. X.I just
  694. Xis 1, then adjacent words within a line are
  695. Xseparated by one space (or sometimes two if
  696. X.I guess
  697. Xis 1) plus zero or more extra spaces.  The value of
  698. X.I fit
  699. Xis disregarded, and line breaks are chosen so that
  700. Xthe paragraph satisfies the following properties:
  701. X.RS 1i
  702. X.IT 1) Every line contains exactly
  703. X.I L
  704. Xcharacters.
  705. X.IT 2) The largest inter-word gap is as small as
  706. Xpossible, subject to property 1.  (An inter-word gap
  707. Xconsists only of the extra spaces, not the regular spaces.)
  708. X.IT 3) The sum of the squares of the lengths
  709. Xof the inter-word gaps is as small as
  710. Xpossible, subject to properties 1 and 2.
  711. X.RE
  712. X.RS .5i
  713. X.LP
  714. XIf
  715. X.I last
  716. Xis 0, then the last line does not count as a
  717. Xline for the purposes of property 1, and it
  718. Xdoes not require or contain any extra spaces.
  719. X.LP
  720. XExtra spaces are distributed as uniformly as
  721. Xpossible among the inter-word gaps in each line.
  722. X.LP
  723. XIn a justified paragraph, every line must
  724. Xcontain at least two words, but that's not
  725. Xalways possible to accomplish.  If the paragraph
  726. Xcannot be justified, it is considered an error.
  727. X.RE
  728. X.LP
  729. XIf the number of lines in the
  730. Xresulting paragraph is less than
  731. X.IR hang ,
  732. Xthen empty lines are added at the end
  733. Xto bring the number of lines up to
  734. X.IR hang .
  735. X.LP
  736. XIf
  737. X.I just
  738. Xis 0 and
  739. X.I touch
  740. Xis 1, then
  741. X.I L
  742. Xis changed to be the length of the longest line.
  743. X.LP
  744. XIf
  745. X.I suffix
  746. Xis not 0, then each line is padded at the
  747. Xend with spaces to bring its length up to
  748. X.IR L .
  749. X.LP
  750. XTo each line is prepended
  751. X.I prefix
  752. Xcharacters.  Let
  753. X.I n
  754. Xbe the number of lines in the IP.  The
  755. Xcharacters which are prepended to the
  756. X.IR i th
  757. Xline are chosen as follows:
  758. X.RS
  759. X.LP
  760. X.IT 1) If
  761. X.I i
  762. X<=
  763. X.IR n ,
  764. Xthen the characters are copied from the ones
  765. Xthat were removed from the beginning of the
  766. X.IR n th
  767. Xinput line.
  768. X.IT 2) If
  769. X.I i
  770. X>
  771. X.I n
  772. X>
  773. X.IR hang ,
  774. Xthen the characters are copied from the ones that were
  775. Xremoved from the beginning of the last input line.
  776. X.IT 3) If
  777. X.I i
  778. X>
  779. X.I n
  780. Xand
  781. X.I n
  782. X<=
  783. X.IR hang ,
  784. Xthen the characters are all spaces.
  785. X.RE
  786. X.LP
  787. XThen to each line is appended
  788. X.I suffix
  789. Xcharacters.  The characters which are appended to the
  790. X.IR i th
  791. Xline are chosen as follows:
  792. X.RS
  793. X.LP
  794. X.IT 1) If
  795. X.I i
  796. X<=
  797. X.IR n ,
  798. Xthen the characters are copied from the
  799. Xones that were removed from the end of the
  800. X.IR n th
  801. Xinput line.
  802. X.IT 2) If
  803. X.I i
  804. X>
  805. X.I n
  806. X> 0, then the characters are copied from the ones that
  807. Xwere removed from the end of the last input line.
  808. X.IT 3) If
  809. X.I n
  810. X= 0, then the characters are all spaces.
  811. X.RE
  812. X.LP
  813. XFinally, the lines are printed to the output as the OP.
  814. X.SH DIAGNOSTICS
  815. X.LP
  816. XIf there are no errors,
  817. X.B par
  818. Xreturns
  819. X.SM EXIT_SUCCESS
  820. X(see
  821. X.BR <stdlib.h> ).
  822. X.LP
  823. XIf there is an error, then an error
  824. Xmessage will be printed to the output, and
  825. X.B par
  826. Xwill return
  827. X.SM EXIT_FAILURE\s0\.
  828. XIf the error is local to a single paragraph, then the
  829. Xpreceeding paragraphs will have been output before the
  830. Xerror was detected.  Line numbers in error messages are
  831. Xlocal to the IP in which the error occurred.  All error
  832. Xmessages begin with \*Qpar error:\*U on a line by itself.
  833. X.LP
  834. XOf course, trying to print an error message would be
  835. Xfutile if an error resulted from an output function, so
  836. X.B par
  837. Xdoesn't bother doing any error checking on output functions.
  838. X.SH EXAMPLES
  839. X.de VS
  840. X.RS -.5i
  841. X.LP
  842. X.nf
  843. X.ps -1p
  844. X.vs -2p
  845. X.cs R 20
  846. X..
  847. X.de VE
  848. X.cs R
  849. X.vs
  850. X.ps
  851. X.fi
  852. X.RE
  853. X..
  854. X.de CM
  855. X\&\*Q\fB\\$1\fP\\*U:
  856. X..
  857. X.LP
  858. XThe superiority of
  859. X.BR par 's
  860. Xdynamic programming algorithm over a
  861. Xgreedy algorithm (such as the one used by
  862. X.BR fmt )
  863. Xcan be seen in the following example:
  864. X.LP
  865. XOriginal paragraph (note that
  866. Xeach line begins with 8 spaces):
  867. X.VS
  868. X        We the people of the United States,
  869. X        in order to form a more perfect union,
  870. X        establish justice,
  871. X        insure domestic tranquility,
  872. X        provide for the common defense,
  873. X        promote the general welfare,
  874. X        and secure the blessing of liberty
  875. X        to ourselves and our posterity,
  876. X        do ordain and establish the Constitution
  877. X        of the United States of America.
  878. X.VE
  879. X.LP
  880. XAfter a greedy algorithm with width = 39:
  881. X.VS
  882. X        We the people of the United
  883. X        States, in order to form a more
  884. X        perfect union, establish
  885. X        establish justice, insure
  886. X        domestic tranquility, provide
  887. X        for the common defense, promote
  888. X        the general welfare, and secure
  889. X        the blessing of liberty to
  890. X        ourselves and our posterity, do
  891. X        ordain and establish the
  892. X        Constitution of the United
  893. X        States of America.
  894. X.VE
  895. X.LP
  896. XAfter
  897. X.CM "par 39"
  898. X.VS
  899. X        We the people of the United
  900. X        States, in order to form a
  901. X        more perfect union, establish
  902. X        justice, insure domestic
  903. X        tranquility, provide for the
  904. X        common defense, promote the
  905. X        general welfare, and secure
  906. X        the blessing of liberty to
  907. X        ourselves and our posterity,
  908. X        do ordain and establish the
  909. X        Constitution of the United
  910. X        States of America.
  911. X.VE
  912. X.LP
  913. XThe line breaks chosen by
  914. X.B par
  915. Xare clearly more eye-pleasing.
  916. X.LP
  917. X.B par
  918. Xis most useful in conjunction with the text-filtering
  919. Xfeatures of an editor, such as the ! commands of
  920. X.BR vi .
  921. X.LP
  922. XThe rest of this section is a series of
  923. Xbefore-and-after pictures showing some typical uses of
  924. X.BR par .
  925. X.LP
  926. XBefore:
  927. X.VS
  928. X        /*   We the people of the United States, */
  929. X        /* in order to form a more perfect union, */
  930. X        /* establish justice, */
  931. X        /* insure domestic tranquility, */
  932. X        /* provide for the common defense, */
  933. X        /* promote the general welfare, */
  934. X        /* and secure the blessing of liberty */
  935. X        /* to ourselves and our posterity, */
  936. X        /* do ordain and establish the Constitution */
  937. X        /* of the United States of America. */
  938. X.VE
  939. X.LP
  940. XAfter
  941. X.CM "par 59"
  942. X.VS
  943. X        /*   We the people of the United States, in      */
  944. X        /* order to form a more perfect union, establish */
  945. X        /* justice, insure domestic tranquility, provide */
  946. X        /* for the common defense, promote the general   */
  947. X        /* welfare, and secure the blessing of liberty   */
  948. X        /* to ourselves and our posterity, do ordain     */
  949. X        /* and establish the Constitution of the United  */
  950. X        /* States of America.                            */
  951. X.VE
  952. X.LP
  953. XOr after
  954. X.CM "par 59f"
  955. X.VS
  956. X        /*   We the people of the United States,  */
  957. X        /* in order to form a more perfect union, */
  958. X        /* establish justice, insure domestic     */
  959. X        /* tranquility, provide for the common    */
  960. X        /* defense, promote the general welfare,  */
  961. X        /* and secure the blessing of liberty to  */
  962. X        /* ourselves and our posterity, do ordain */
  963. X        /* and establish the Constitution of the  */
  964. X        /* United States of America.              */
  965. X.VE
  966. X.LP
  967. XOr after
  968. X.CM "par 59l"
  969. X.VS
  970. X        /*   We the people of the United States, in      */
  971. X        /* order to form a more perfect union, establish */
  972. X        /* justice, insure domestic tranquility,         */
  973. X        /* provide for the common defense, promote       */
  974. X        /* the general welfare, and secure the           */
  975. X        /* blessing of liberty to ourselves and our      */
  976. X        /* posterity, do ordain and establish the        */
  977. X        /* Constitution of the United States of America. */
  978. X.VE
  979. X.LP
  980. XOr after
  981. X.CM "par 59lf"
  982. X.VS
  983. X        /*   We the people of the United States,  */
  984. X        /* in order to form a more perfect union, */
  985. X        /* establish justice, insure domestic     */
  986. X        /* tranquility, provide for the common    */
  987. X        /* defense, promote the general welfare,  */
  988. X        /* and secure the blessing of liberty     */
  989. X        /* to ourselves and our posterity, do     */
  990. X        /* ordain and establish the Constitution  */
  991. X        /* of the United States of America.       */
  992. X.VE
  993. X.LP
  994. XOr after
  995. X.CM "par 59lft0"
  996. X.VS
  997. X        /*   We the people of the United States,         */
  998. X        /* in order to form a more perfect union,        */
  999. X        /* establish justice, insure domestic            */
  1000. X        /* tranquility, provide for the common           */
  1001. X        /* defense, promote the general welfare,         */
  1002. X        /* and secure the blessing of liberty            */
  1003. X        /* to ourselves and our posterity, do            */
  1004. X        /* ordain and establish the Constitution         */
  1005. X        /* of the United States of America.              */
  1006. X.VE
  1007. X.LP
  1008. XOr after
  1009. X.CM "par 59j"
  1010. X.VS
  1011. X        /*   We  the people  of  the  United States,  in */
  1012. X        /* order to form a more perfect union, establish */
  1013. X        /* justice, insure domestic tranquility, provide */
  1014. X        /* for the  common defense, promote  the general */
  1015. X        /* welfare, and  secure the blessing  of liberty */
  1016. X        /* to ourselves and our posterity, do ordain and */
  1017. X        /* establish  the  Constitution  of  the  United */
  1018. X        /* States of America.                            */
  1019. X.VE
  1020. X.LP
  1021. XOr after
  1022. X.CM "par 59jl"
  1023. X.VS
  1024. X        /*   We  the   people  of  the   United  States, */
  1025. X        /* in   order    to   form   a    more   perfect */
  1026. X        /* union,  establish  justice,  insure  domestic */
  1027. X        /* tranquility, provide for  the common defense, */
  1028. X        /* promote  the  general   welfare,  and  secure */
  1029. X        /* the  blessing  of  liberty to  ourselves  and */
  1030. X        /* our  posterity, do  ordain and  establish the */
  1031. X        /* Constitution of the United States of America. */
  1032. X.VE
  1033. X.LP
  1034. XBefore:
  1035. X.VS
  1036. X        Preamble      We the people of the United States,
  1037. X        to the US     in order to form
  1038. X        Constitution  a more perfect union,
  1039. X                      establish justice,
  1040. X                      insure domestic tranquility,
  1041. X                      provide for the common defense,
  1042. X                      promote the general welfare,
  1043. X                      and secure the blessing of liberty
  1044. X                      to ourselves and our posterity,
  1045. X                      do ordain and establish
  1046. X                      the Constitution
  1047. X                      of the United States of America.
  1048. X.VE
  1049. X.LP
  1050. XAfter
  1051. X.CM "par 52h3"
  1052. X.VS
  1053. X        Preamble      We the people of the United
  1054. X        to the US     States, in order to form a
  1055. X        Constitution  more perfect union, establish
  1056. X                      justice, insure domestic
  1057. X                      tranquility, provide for the
  1058. X                      common defense, promote the
  1059. X                      general welfare, and secure
  1060. X                      the blessing of liberty to
  1061. X                      ourselves and our posterity,
  1062. X                      do ordain and establish the
  1063. X                      Constitution of the United
  1064. X                      States of America.
  1065. X.VE
  1066. X.LP
  1067. XBefore:
  1068. X.VS
  1069. X         1  We the people of the United States,
  1070. X         2  in order to form a more perfect union,
  1071. X         3  establish justice,
  1072. X         4  insure domestic tranquility,
  1073. X         5  provide for the common defense,
  1074. X         6  promote the general welfare,
  1075. X         7  and secure the blessing of liberty
  1076. X         8  to ourselves and our posterity,
  1077. X         9  do ordain and establish the Constitution
  1078. X        10  of the United States of America.
  1079. X.VE
  1080. X.LP
  1081. XAfter
  1082. X.CM "par 59p12l"
  1083. X.VS
  1084. X         1  We the people of the United States, in order to
  1085. X         2  form a more perfect union, establish justice,
  1086. X         3  insure domestic tranquility, provide for the
  1087. X         4  common defense, promote the general welfare,
  1088. X         5  and secure the blessing of liberty to ourselves
  1089. X         6  and our posterity, do ordain and establish the
  1090. X         7  Constitution of the United States of America.
  1091. X.VE
  1092. X.LP
  1093. XBefore:
  1094. X.VS
  1095. X        > > We the people
  1096. X        > > of the United States,
  1097. X        > > in order to form a more perfect union,
  1098. X        > > establish justice,
  1099. X        > > ensure domestic tranquility,
  1100. X        > > provide for the common defense,
  1101. X        >
  1102. X        > Promote the general welfare,
  1103. X        > and secure the blessing of liberty
  1104. X        > to ourselves and our posterity,
  1105. X        > do ordain and establish
  1106. X        > the Constitution of the United States of America.
  1107. X.VE
  1108. X.LP
  1109. XAfter
  1110. X.CM "par 52"
  1111. X.VS
  1112. X        > > We the people of the United States, in
  1113. X        > > order to form a more perfect union,
  1114. X        > > establish justice, ensure domestic
  1115. X        > > tranquility, provide for the common
  1116. X        > > defense,
  1117. X        >
  1118. X        > Promote the general welfare, and secure
  1119. X        > the blessing of liberty to ourselves and
  1120. X        > our posterity, do ordain and establish
  1121. X        > the Constitution of the United States of
  1122. X        > America.
  1123. X.VE
  1124. X.LP
  1125. XBefore:
  1126. X.VS
  1127. X        >   We the people
  1128. X        > of the United States,
  1129. X        > in order to form a more perfect union,
  1130. X        > establish justice,
  1131. X        > ensure domestic tranquility,
  1132. X        > provide for the common defense,
  1133. X        >   Promote the general welfare,
  1134. X        > and secure the blessing of liberty
  1135. X        > to ourselves and our posterity,
  1136. X        > do ordain and establish
  1137. X        > the Constitution of the United States of America.
  1138. X.VE
  1139. X.LP
  1140. XAfter
  1141. X.CM "par 52d"
  1142. X.VS
  1143. X        >   We the people of the United States,
  1144. X        > in order to form a more perfect union,
  1145. X        > establish justice, ensure domestic
  1146. X        > tranquility, provide for the common
  1147. X        > defense,
  1148. X        >   Promote the general welfare, and secure
  1149. X        > the blessing of liberty to ourselves and
  1150. X        > our posterity, do ordain and establish
  1151. X        > the Constitution of the United States of
  1152. X        > America.
  1153. X.VE
  1154. X.LP
  1155. XBefore:
  1156. X.VS
  1157. X        Joe Public writes:
  1158. X        > Jane Doe writes:
  1159. X        > > I can't find the source for uncompress.
  1160. X        > Oh no, not again!!!
  1161. X        >
  1162. X        > Isn't there a FAQ for this?
  1163. X        That wasn't very helpful, Joe. Jane,
  1164. X        just make a link from uncompress to compress.
  1165. X.VE
  1166. X.LP
  1167. XAfter
  1168. X.CM "par 40q"
  1169. X.VS
  1170. X        Joe Public writes:
  1171. X
  1172. X        > Jane Doe writes:
  1173. X        >
  1174. X        > > I can't find the source for
  1175. X        > > uncompress.
  1176. X        >
  1177. X        > Oh no, not again!!!
  1178. X        >
  1179. X        > Isn't there a FAQ for this?
  1180. X
  1181. X        That wasn't very helpful, Joe.
  1182. X        Jane, just make a link from
  1183. X        uncompress to compress.
  1184. X.VE
  1185. X.LP
  1186. XBefore:
  1187. X.VS
  1188. X        I sure hope there's still room
  1189. X        in Dr. Jones' section of archaeology.
  1190. X        I've heard he's the best.
  1191. X.VE
  1192. X.LP
  1193. XAfter
  1194. X.CM "par 50g"
  1195. X.VS
  1196. X        I sure hope there's still room in
  1197. X        Dr. Jones' section of archaeology.  I've
  1198. X        heard he's the best.
  1199. X.VE
  1200. X.SH SEE ALSO
  1201. X.LP
  1202. X.B par.doc
  1203. X.SH LIMITATIONS
  1204. X.LP
  1205. XThe
  1206. X.I guess
  1207. Xfeature guesses wrong in cases like the following:
  1208. X.VS
  1209. X        I calc'd the approx.
  1210. X        Fermi level to 3 sig. digits.
  1211. X.VE
  1212. X.LP
  1213. XWith
  1214. X.I guess
  1215. X= 1,
  1216. X.B par
  1217. Xwill incorrectly assume that \*Qapprox.\*U
  1218. Xends a sentence.  If the input were:
  1219. X.VS
  1220. X        I calc'd the approx. Fermi
  1221. X        level to 3 sig. digits.
  1222. X.VE
  1223. X.LP
  1224. Xthen
  1225. X.B par
  1226. Xwould refuse to put a line break between
  1227. X\*Qapprox.\*U and \*QFermi\*U in the output,
  1228. Xmainly to avoid creating the first situation (in
  1229. Xcase the paragraph were to be fed back through
  1230. X.B par
  1231. Xagain).  This non-breaking space policy does come in handy
  1232. Xfor cases like \*QMr.\ Johnson\*U and \*QJan.\ 1\*U, though.
  1233. X.LP
  1234. XThe
  1235. X.I guess
  1236. Xfeature only goes one way.
  1237. X.B par
  1238. Xcan preserve wide sentence breaks in a
  1239. Xparagraph, or remove them, but it can't insert
  1240. Xthem if they aren't already in the input.
  1241. X.LP
  1242. XIf you use tabs, you probably won't like the way
  1243. X.B par
  1244. Xhandles (or doesn't handle) them.  It turns them into
  1245. Xspaces.  I didn't bother trying to make sense of tabs
  1246. Xbecause they don't make sense to begin with.  Not everyone's
  1247. Xterminal has the same tab settings, so text files containing
  1248. Xtabs are sometimes mangled.  In fact, almost every text
  1249. Xfile containing tabs gets mangled when something is
  1250. Xinserted at the beginning of each line (when quoting
  1251. Xe-mail or commenting out a section of a shell script, for
  1252. Xexample), making them a pain to edit.  In my opinion, the
  1253. Xworld would be a nicer place if everyone stopped using
  1254. Xtabs (so I'm doing my part by not supporting them in
  1255. X.BR par .)
  1256. X(Thanks to ets1@cs.wustl.edu (Eric T. Stuebe)
  1257. Xfor showing me the light about tabs.)
  1258. X.LP
  1259. XThere is currently no way for the length of the
  1260. Xoutput prefix to differ from the length of the
  1261. Xinput prefix.  Ditto for the suffix.  I may consider
  1262. Xadding this capability in a future release, but
  1263. Xright now I'm not sure how I'd want it to work.
  1264. X.SH BUGS
  1265. X.LP
  1266. XIf I knew of any bugs, I wouldn't release the package.  Of
  1267. Xcourse, there may be bugs that I haven't yet discovered.
  1268. X.LP
  1269. XIf you find any bugs (in the program or
  1270. Xin the documentation), or if you have
  1271. Xany suggestions, please send e-mail to:
  1272. X.RS
  1273. X.LP
  1274. Xamc@ecl.wustl.edu
  1275. X.RE
  1276. X.LP
  1277. Xor send paper mail to:
  1278. X.RS
  1279. X.LP
  1280. X.nf
  1281. XAdam M. Costello
  1282. XCampus Box 1045
  1283. XWashington University
  1284. XOne Brookings Dr.
  1285. XSt. Louis, MO 63130
  1286. XUSA
  1287. X.fi
  1288. X.RE
  1289. X.LP
  1290. XNote that both addresses could
  1291. Xchange anytime after June 1994.
  1292. X.LP
  1293. XWhen reporting a bug, please include the exact input and
  1294. Xcommand line options used, and the version number of
  1295. X.BR par ,
  1296. Xso that I can reproduce it.
  1297. END_OF_FILE
  1298. if test 31620 -ne `wc -c <'Par130/par.1'`; then
  1299.     echo shar: \"'Par130/par.1'\" unpacked with wrong size!
  1300. fi
  1301. # end of 'Par130/par.1'
  1302. fi
  1303. if test -f 'Par130/buffer.h' -a "${1}" != "-c" ; then 
  1304.   echo shar: Will not clobber existing file \"'Par130/buffer.h'\"
  1305. else
  1306. echo shar: Extracting \"'Par130/buffer.h'\" \(2446 characters\)
  1307. sed "s/^X//" >'Par130/buffer.h' <<'END_OF_FILE'
  1308. X/*********************/
  1309. X/* buffer.h          */
  1310. X/* for Par 1.30      */
  1311. X/* Copyright 1993 by */
  1312. X/* Adam M. Costello  */
  1313. X/*********************/
  1314. X
  1315. X/* This is ANSI C code. */
  1316. X
  1317. X
  1318. X/* Note: Those functions declared here which do not use errmsg    */
  1319. X/* always succeed, provided that they are passed valid arguments. */
  1320. X
  1321. X
  1322. X#include "errmsg.h"
  1323. X
  1324. X#include <stddef.h>
  1325. X
  1326. X
  1327. Xstruct buffer;
  1328. X
  1329. X
  1330. Xstruct buffer *newbuffer(size_t itemsize, errmsg_t errmsg);
  1331. X
  1332. X  /* newbuffer(itemsize,errmsg) returns a pointer to a */
  1333. X  /* new empty struct buffer which holds items of size */
  1334. X  /* itemsize.  Any struct buffer *buf passed to any   */
  1335. X  /* function declared in this header must have been   */
  1336. X  /* obtained from this function.  itemsize must not   */
  1337. X  /* be 0.  Returns NULL on failure.                   */
  1338. X
  1339. X
  1340. Xvoid freebuffer(struct buffer *buf);
  1341. X
  1342. X  /* freebuffer(buf) frees the memory associated with */
  1343. X  /* *buf.  buf may not be used after this call.      */
  1344. X
  1345. X
  1346. Xvoid clearbuffer(struct buffer *buf);
  1347. X
  1348. X  /* clearbuffer(buf) removes  */
  1349. X  /* all items from *buf, but  */
  1350. X  /* does not free any memory. */
  1351. X
  1352. X
  1353. Xvoid additem(struct buffer *buf, const void *item, errmsg_t errmsg);
  1354. X
  1355. X  /* additem(buf,item,errmsg) copies *item to the end of     */
  1356. X  /* *buf.  item must point to an object of the proper size  */
  1357. X  /* for *buf.  If additem() fails, *buf will be unaffected. */
  1358. X
  1359. X
  1360. Xint numitems(struct buffer *buf);
  1361. X
  1362. X  /* numitems(buf) returns the number of items in *buf. */
  1363. X
  1364. X
  1365. Xvoid *copyitems(struct buffer *buf, errmsg_t errmsg);
  1366. X
  1367. X  /* copyitems(buf,errmsg) returns an array of objects of the proper size */
  1368. X  /* for *buf, one for each item in *buf, or (void *) 0 if there are no   */
  1369. X  /* items in buf.  The elements of the array are copied from the items   */
  1370. X  /* in *buf, in order.  The array is allocated with malloc(), so it may  */
  1371. X  /* be freed with free().  Returns NULL on failure.                      */
  1372. X
  1373. X
  1374. Xvoid *nextitem(struct buffer *buf);
  1375. X
  1376. X  /* When buf was created by newbuffer, a pointer associated with buf   */
  1377. X  /* was initialized to point at the first slot in buf.  If there is an */
  1378. X  /* item in this slot, nextitem(buf) advances the pointer to the next  */
  1379. X  /* slot and returns the old value.  If there is no item in the slot,  */
  1380. X  /* nextitem(buf) leaves the pointer where it is and returns NULL.     */
  1381. X
  1382. X
  1383. Xvoid rewindbuffer(struct buffer *buf);
  1384. X
  1385. X  /* rewindbuffer(buf) resets the pointer used by  */
  1386. X  /* nextitem() to point at the first slot in buf. */
  1387. END_OF_FILE
  1388. if test 2446 -ne `wc -c <'Par130/buffer.h'`; then
  1389.     echo shar: \"'Par130/buffer.h'\" unpacked with wrong size!
  1390. fi
  1391. # end of 'Par130/buffer.h'
  1392. fi
  1393. if test -f 'Par130/buffer.c' -a "${1}" != "-c" ; then 
  1394.   echo shar: Will not clobber existing file \"'Par130/buffer.c'\"
  1395. else
  1396. echo shar: Extracting \"'Par130/buffer.c'\" \(4689 characters\)
  1397. sed "s/^X//" >'Par130/buffer.c' <<'END_OF_FILE'
  1398. X/*********************/
  1399. X/* buffer.c          */
  1400. X/* for Par 1.30      */
  1401. X/* Copyright 1993 by */
  1402. X/* Adam M. Costello  */
  1403. X/*********************/
  1404. X
  1405. X/* This is ANSI C code. */
  1406. X
  1407. X
  1408. X/* additem(), copyitems(), and nextitem() rely on the fact that */
  1409. X/* sizeof (char) is 1.  See section A7.4.8 of The C Programming */
  1410. X/* Language, Second Edition, by Kerninghan and Ritchie.         */
  1411. X
  1412. X
  1413. X#include "buffer.h"  /* Makes sure we're consistent with the prototypes. */
  1414. X                     /* Also includes <stddef.h> and "errmsg.h".         */
  1415. X
  1416. X#include <stdlib.h>
  1417. X#include <string.h>
  1418. X
  1419. X#undef NULL
  1420. X#define NULL ((void *) 0)
  1421. X
  1422. X#ifdef DONTFREE
  1423. X#define free(ptr)
  1424. X#endif
  1425. X
  1426. X
  1427. Xstruct buffer {
  1428. X  struct block *firstblk, /* The first block.                    */
  1429. X               *current,  /* The last non-empty block, or        */
  1430. X                          /* firstblk if all are empty.          */
  1431. X               *nextblk;  /* The block containing the item to be */
  1432. X                          /* returned by nextitem(), or NULL.    */
  1433. X  int nextindex;          /* Index of item in nextblock->items.  */
  1434. X  size_t itemsize;        /* The size of an item.                */
  1435. X};
  1436. X
  1437. Xstruct block {
  1438. X  struct block *next;  /* The next block, or NULL if none.              */
  1439. X  void *items;         /* Storage for the items in this block.          */
  1440. X  int maxhere,         /* Number of items that fit in *items.           */
  1441. X      numprevious,     /* Total of numhere for all previous blocks.     */
  1442. X      numhere;         /* The first numhere slots in *items are filled. */
  1443. X};
  1444. X
  1445. X
  1446. Xstruct buffer *newbuffer(size_t itemsize, errmsg_t errmsg)
  1447. X{
  1448. X  struct buffer *buf;
  1449. X  struct block *blk;
  1450. X  void *items;
  1451. X  int maxhere;
  1452. X
  1453. X  maxhere = 124 / itemsize;
  1454. X  if (maxhere < 4) maxhere = 4;
  1455. X
  1456. X  buf = (struct buffer *) malloc(sizeof (struct buffer));
  1457. X  blk = (struct block *) malloc(sizeof (struct block));
  1458. X  items = malloc(maxhere * itemsize);
  1459. X  if (!buf || !blk || !items) {
  1460. X    strcpy(errmsg,outofmem);
  1461. X    goto nberror;
  1462. X  }
  1463. X
  1464. X  buf->itemsize = itemsize;
  1465. X  buf->firstblk = buf->current = buf->nextblk = blk;
  1466. X  buf->nextindex = 0;
  1467. X  blk->next = NULL;
  1468. X  blk->numprevious = blk->numhere = 0;
  1469. X  blk->maxhere = maxhere;
  1470. X  blk->items = items;
  1471. X
  1472. X  *errmsg = '\0';
  1473. X  return buf;
  1474. X
  1475. X  nberror:
  1476. X  if (buf) free(buf);
  1477. X  if (blk) free(blk);
  1478. X  if (items) free(items);
  1479. X  return NULL;
  1480. X}
  1481. X
  1482. X
  1483. Xvoid freebuffer(struct buffer *buf)
  1484. X{
  1485. X  struct block *blk, *tmp;
  1486. X
  1487. X  blk = buf->firstblk;
  1488. X  while (blk) {
  1489. X    tmp = blk;
  1490. X    blk = blk->next;
  1491. X    if (tmp->items) free(tmp->items);
  1492. X    free(tmp);
  1493. X  }
  1494. X
  1495. X  free(buf);
  1496. X}
  1497. X
  1498. X
  1499. Xvoid clearbuffer(struct buffer *buf)
  1500. X{
  1501. X  struct block *blk;
  1502. X
  1503. X  for (blk = buf->firstblk;  blk;  blk = blk->next)
  1504. X    blk->numhere = 0;
  1505. X
  1506. X  buf->current = buf->firstblk;
  1507. X}
  1508. X
  1509. X
  1510. Xvoid additem(struct buffer *buf, const void *item, errmsg_t errmsg)
  1511. X{
  1512. X  struct block *blk, *new;
  1513. X  void *items;
  1514. X  int maxhere;
  1515. X  size_t itemsize = buf->itemsize;
  1516. X
  1517. X  blk = buf->current;
  1518. X
  1519. X  if (blk->numhere == blk->maxhere) {
  1520. X    new = blk->next;
  1521. X    if (!new) {
  1522. X      maxhere = 2 * blk->maxhere;
  1523. X      new = (struct block * ) malloc(sizeof (struct block));
  1524. X      items = malloc(maxhere * itemsize);
  1525. X      if (!new || !items) {
  1526. X        strcpy(errmsg,outofmem);
  1527. X        goto aierror;
  1528. X      }
  1529. X      blk->next = new;
  1530. X      new->next = NULL;
  1531. X      new->maxhere = maxhere;
  1532. X      new->numprevious = blk->numprevious + blk->numhere;
  1533. X      new->numhere = 0;
  1534. X      new->items = items;
  1535. X    }
  1536. X    blk = buf->current = new;
  1537. X  }
  1538. X
  1539. X  memcpy( ((char *) blk->items) + (blk->numhere * itemsize), item, itemsize );
  1540. X
  1541. X  ++blk->numhere;
  1542. X
  1543. X  *errmsg = '\0';
  1544. X  return;
  1545. X
  1546. X  aierror:
  1547. X  if (new) free(new);
  1548. X  if (items) free(items);
  1549. X}
  1550. X
  1551. X
  1552. Xint numitems(struct buffer *buf)
  1553. X{
  1554. X  struct block *blk = buf->current;
  1555. X  return blk->numprevious + blk->numhere;
  1556. X}
  1557. X
  1558. X
  1559. Xvoid *copyitems(struct buffer *buf, errmsg_t errmsg)
  1560. X{
  1561. X  int n;
  1562. X  void *r;
  1563. X  struct block *blk, *b;
  1564. X  size_t itemsize = buf->itemsize;
  1565. X
  1566. X  b = buf->current;
  1567. X  n = b->numprevious + b->numhere;
  1568. X  if (!n) return NULL;
  1569. X
  1570. X  r = malloc(n * itemsize);
  1571. X  if (!r) {
  1572. X    strcpy(errmsg,outofmem);
  1573. X    return NULL;
  1574. X  }
  1575. X
  1576. X  b = b->next;
  1577. X
  1578. X  for (blk = buf->firstblk;  blk != b;  blk = blk->next)
  1579. X    memcpy( ((char *) r) + (blk->numprevious * itemsize),
  1580. X            blk->items, blk->numhere * itemsize);
  1581. X
  1582. X  *errmsg = '\0';
  1583. X  return r;
  1584. X}
  1585. X
  1586. X
  1587. Xvoid rewindbuffer(struct buffer *buf)
  1588. X{
  1589. X  buf->nextblk = buf->firstblk;
  1590. X  buf->nextindex = 0;
  1591. X}
  1592. X
  1593. X
  1594. Xvoid *nextitem(struct buffer *buf)
  1595. X{
  1596. X  void *r;
  1597. X
  1598. X  if (!buf->nextblk || buf->nextindex >= buf->nextblk->numhere)
  1599. X    return NULL;
  1600. X
  1601. X  r = ((char *) buf->nextblk->items) + (buf->nextindex * buf->itemsize);
  1602. X
  1603. X  if (++buf->nextindex >= buf->nextblk->maxhere) {
  1604. X    buf->nextblk = buf->nextblk->next;
  1605. X    buf->nextindex = 0;
  1606. X  }
  1607. X
  1608. X  return r;
  1609. X}
  1610. END_OF_FILE
  1611. if test 4689 -ne `wc -c <'Par130/buffer.c'`; then
  1612.     echo shar: \"'Par130/buffer.c'\" unpacked with wrong size!
  1613. fi
  1614. # end of 'Par130/buffer.c'
  1615. fi
  1616. echo shar: End of shell archive.
  1617. exit 0
  1618.  
  1619. exit 0 # Just in case...
  1620.