. . . . . . . . . ( end of section Introduction) <<Contents | End>>
Requirements
user
|w
V
in->(br)->out
The program reads text input and attempts to reformat it so that no line is longer than 'w' characters by breaking lines at the first block of whitespace after the last word that fits on the line. The break is done by removing all the whitespace characters after the last word that fits on the output line and replacing them by CR. Assume that there are no BS chars in the file. If w>250 or <10 then the program should do nothing and report an abnormal termination("error"). Default width is 79. If a single word is too long to fit on a line by itself then it will have to be broken up - Hyphenate words whose length is>w-1.
Data dictionary
Input lines (ilines) must be broken up if they are too long. So
each iline is split into a group of zero or more shorter output lines followed
by part of a line that does not need breaking. The short lines end in
the last word that can fit on that line. After the broken lines there
is a last part of the iline which is less than 'w' in length,
This is ok when each word is smaller than the output line width. Longer
words are broken into hyphenated parts.
The length of words, wsb and last word in each 'line' add up to less than w and HT (tab) equals HTWIDTH characters in width.
. . . . . . . . . ( end of section Requirements) <<Contents | End>>
Design
Operations List
The notation is inspired by Tony Hoare's CSP and ":+" operator.
Table
Symbol | Function | When |
---|---|---|
ch | Character | |
!ch | Output ch | Each character output |
!eoln | Output end of line | End of each output end of line |
?ch | Input ch | Each ch of input |
n | Number of charcters | |
n:+1 | add 1 to n | When character is output onto line. |
n:+HTWIDTH | add HTWIDTH to n | each horizontal tab |
n'=0 | set n to 0 | start of a line |
w?:user | get width from the user | Start of program(or given) |
!"-" | Output a hyphen | when word has to be broken |
C_user_C_in_P_out
________________________________________________________________________
| w?:user; ?ch |
|________________________________________________________________________|
/| C_iline |\
| | ________________________________________________________________ | |
| | /| P_line |\ | |
| | | | ___________________________________________________________ | | | |
| | | | | n'=0 | CP_pt_long_wd | | | | |
| | | | |------------------------------------------| ---------- | | | | |
| | | | | ______________________ | | /| CP_pt_wd|\ | | | | |
| | | | | /|CP_word |\ | CP_wsb | | | -------| || | | | |
| | | | | | | ________________ | | | | | | |!ch;?ch| || | | | |
| | | | | | | /| CP_non_ws |\ | | | | | | -------| || | | | |
| | | | | | || | -------------- | || | | | \|_________|/ | | | | |
| | | | | | || ||n:+1;!ch; ?ch || || | | | | !"-" | | | | | |
| | | | | | | \| -------------- |/ | | | | |---------| | | | | |
| | | | | | | ---------------- | | | | | !eoln | | | | | |
| | | | | \|______________________|/ | | |_________| | | | | |
| | | | |-----------------------------------------------------------| | | | |
| | | | | CP_last_word | | | | |
| | | | | ----------------- | | | | |
| | | | | /| CP_last_non_ws |\ | | | | |
| | | | | | | --------- | | | | | | |
| | | | | | | |!ch; ?ch | | | | | | | |
| | | | | \| --------- |/ | | | | |
| | | | | ----------------- | | | | |
| | | | |-----------------------------------------------------------| | | | |
| | | | | C_wsb_P_break | | | | |
| | | | | ----------- | | | | |
| | | | | /| C_ws |\ | | | | |
| | | | | | | ------ | | | | | | |
| | | | | | | | ?ch | | | | | | | |
| | | | | | | ------ | | | | | | |
| | | | | \|-----------|/ | | | | |
| | | | | | !eoln | | | | | |
| | | | | ----------- | | | | |
| | | | |___________________________________________________________| | | | |
| | \|________________________________________________________________|/ | |
| | | CP_ok_line | | |
| | | ---------------- | | |
| | | /| CP_non_eoln |\ | | |
| | || | _________ | | | | |
| | || | |!ch; ?ch | | | | | |
| | | \| |_________| |/ | | |
| | | |----------------| | | |
| | | | CP_eoln | | | |
| | | | ---------- | | | |
| | | | |!eoln; ?ch| | | | |
| | | | ---------- | | | |
| | | |________________| | | |
| | |________________________________________________________________| | |
\|________________________________________________________________________|/
CP_wsb
-------------------------------
(|CP_HT|CP_non_HT_ws|CP_non_print |)
-------------------------------
. . . . . . . . . ( end of section Design) <<Contents | End>>
Implementation
DChart Schematic Logic
wd and ws are buffers
C_user_C_in_P_out
\
| w?:user; ?ch
| #C_iline
| \
| | not end(in)
| | #P_line(posit iline will be broken after word)
| | \2_______________________________________1
| | |admit word not hyphenated |posit word hyphenated
| | | push ch & wd back out; n'=0 |CP_pt_long_wd
| | | # | \
| | | \ | |wd!new; n'=0
| | | | | |#CP_pt_wd
| | | |posit room for next word or wsb | | \
| | | | | | |n<w;Quit if ch in ws
| | | | | | \|wd!ch;n:+1;?ch
| | | | | |wd!end;!wd
| | | |\ _____________________ | | !'_';!eoln
| | | | |ch in wdchs | else |
| | | | |CP_word | CP_wsb (3)
| | | | | \ | \
| | | | | | wd!new | |ws!new
| | | | | | | |#
| | | | | | #CP_non_ws | | \
| | | | | | \ | | |Quit will be broken if ch=eoln
| | | | | | | | | |
| | | | | | | | | |\ _________________
| | | | | | |ch not ws | | | | | |
| | | | | | |Quit if | | | |ch=HT |ch=SP |ch not SP
| | | | | | | n=w | | | | | |ch not HT
| | | | | | |n:+1;wd!ch | | \| |CP_HT |CP_non_HT |CP_non_print
| | | | | | \|?ch | |Quit next fits if n>w-1
| | | | | | | |ws!end; !ws
| | | | |wd!end; !wd |
| | | \|
| | |
| | |admit next did not fit (3)
| | | CP_last_word |
| | | \ |
| | | side-effects |
| | | |
| | |push ch & wd back out (in!b) |
| | | |
| | | C_wsb_P_break |
| | | \ |
| | | | #C_ws |
| | | | \ |
| | | | beneficent side-effects |
| | | | |
| | | |!eoln |
| | | \| |
| | \|______________________________________|
| |
| |admit no more breaks needed
| |CP_ok_line
| | \
| | | #CP_non_eoln
| | | \
| | | Beneficient side-effects
| | |
| | |
| | | CP_eoln
| | | \
| | | |!eoln; ?ch
. . . . . . . . . ( end of section Implementation) <<Contents | End>>
Source Code
[ br.c ]
[ br.ANSI.c ]
UNIX Manual
[ br.l ]