home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / net / adjust next >
Internet Message Format  |  1987-04-09  |  7KB

  1. From amos@instable.UUCP Thu Apr  9 10:31:23 1987
  2. Path: seismo!lll-lcc!styx!ames!ucbcad!ucbvax!decvax!decwrl!nsc!nsta!instable!amos
  3. From: amos@instable.UUCP (Amos Shapir)
  4. Newsgroups: net.sources
  5. Subject: Text line adjuster
  6. Keywords: text, adjust, center
  7. Message-ID: <741@instable.UUCP>
  8. Date: 9 Apr 87 14:31:23 GMT
  9. Organization: National Semiconductor (Israel) Ltd.
  10. Lines: 315
  11.  
  12.  
  13.                     Have    you    ever
  14.                  wished  you had  something
  15.               to  replace   nroff?  Something
  16.             small, fast  and versatile,  yet at
  17.            the same time  flexible and powerful?
  18.           Well, your troubles  are over. 'Adj' is
  19.           not  as powerful  as nroff  or TeX,  but
  20.           it's ok for small portions of text, (and
  21.           much  more  useful than  4BSD's  'fmt').
  22.           Combined  with  the  trivial  centering
  23.            filter 'ctr',  it can do  fancy stuff
  24.             like  this  text.   This  text  was
  25.                   produced by the command:
  26.                               
  27. adj -w 19,26,31,35,37,39,40,40,40,39,37,35,31,26,19 | ctr -w 60
  28.  
  29.         o /        o /        o /        o /
  30. -----------------X---------------X---------------X---------------X----
  31.         o \        o \        o \        o \
  32.  
  33. #!/bin/sh
  34. : "This is a shell archive, meaning:                              "
  35. : "1. Remove everything above the #! /bin/sh line.                "
  36. : "2. Save the rest in a file.                                    "
  37. : "3. Execute the file with /bin/sh (not csh) to create the files:"
  38. : "    adj.6"
  39. : "    adj.c"
  40. : "    ctr.6"
  41. : "    ctr.c"
  42. : "This archive created:  Thu Mar 19 12:31:56 EET 1987 "
  43. if [ -f adj.6 ]
  44. then
  45. echo file adj.6 exists
  46. else
  47. echo extracting file: adj.6
  48. sed 's/^X//' >adj.6 << 'END-of-adj.6'
  49. X.TH ADJ 6 Local
  50. X.UC 4
  51. X.SH NAME
  52. Xadj \- fill and adjust text lines
  53. X.SH SYNOPSIS
  54. X.B adj
  55. X[
  56. X.B \-h
  57. X] [
  58. X.B \-n
  59. X] [
  60. X.B \-p
  61. X] [
  62. X.B \-w
  63. X.I n1,...
  64. X] [ file ]
  65. X.SH DESCRIPTION
  66. X.I Adj
  67. Xcopies the given file (standard input if none is given) to the standard output,
  68. Xfilling lines and adjusting the right margin. Indentation and empty lines
  69. Xare preserved.
  70. X.PP
  71. XNormally, words (uninterrupted sequences of non-blank characters) are not
  72. Xbroken, unless the
  73. X.B \-h
  74. X(hyphen) flag is given.
  75. X.PP
  76. XThe
  77. X.B \-n
  78. X(no adjust) prevents
  79. X.I adj
  80. Xfrom adjusting the right margin.
  81. X.PP
  82. XIf the 
  83. X.B \-p
  84. X(paragraph) option is specified, indentation is preserved only on
  85. Xthe first line of a paragraph, assuming paragraphs are separated by empty lines.
  86. X.PP
  87. XThe
  88. X.B \-w
  89. X.I n
  90. X(width) option sets the line width to
  91. X.I n
  92. X(default 65).
  93. XWhen given as
  94. X.B \-w
  95. X.I n1,n2,...
  96. X(a comma-separated sequence of numbers) the output lines will be
  97. X.I "n1, n2"
  98. Xetc. characters long, respectively.
  99. XWhen the sequence is exhausted, it is restarted.
  100. X.SH "SEE ALSO"
  101. Xctr(6)
  102. X.SH BUGS
  103. X.B \-n
  104. Xdoes not break words intelligently.
  105. X.PP
  106. XChange of indentation is done ungracefully.
  107. X.PP
  108. XThere's probably a better way tp specify line lengths.
  109. END-of-adj.6
  110. fi
  111. if [ -f adj.c ]
  112. then
  113. echo file adj.c exists
  114. else
  115. echo extracting file: adj.c
  116. sed 's/^X//' >adj.c << 'END-of-adj.c'
  117. X/*
  118. X * Trivial line-fill filter
  119. X * white space at col 1 and empty lines
  120. X * are preserved.
  121. X * flags: -n - dont adjust right margin
  122. X *      -w {n} line is {n} chars wide
  123. X *      -w {n1,n2,...} line is cyclically n1, n2... chars wide
  124. X *      -h - break words with a hyphen
  125. X *      -p - indent only 1st line of a paragraph
  126. X */
  127. X#define MAXB 1024
  128. X#define MAXW 100    /* words in a line */
  129. X#define NLL  50        /* line lengths vector size */
  130. X#define EOF (-1)
  131. X/* flags */
  132. X#define NOADJ 01    /* no adjust */
  133. X#define HYPHN 02    /* hyphenate */
  134. X#define PARAG 04    /* indent 1 line */
  135. Xint ll[NLL] = { 65 };    /* line lengths */
  136. Xint nll;        /* current line length index */
  137. Xint nw;        /* current no. of words */
  138. Xint ind;    /* current indentation */
  139. Xstatic char buf[MAXB];
  140. Xmain(argc, argv)
  141. X    char **argv;
  142. X{
  143. X    register char *pb, *pw, *pe;
  144. X    register c, flags, nl;
  145. X
  146. X    while(--argc > 0)
  147. X        if(**++argv == '-')
  148. X            switch(argv[0][1]) {
  149. X            case 'n':
  150. X                flags = NOADJ;
  151. X                break;
  152. X            case 'h':
  153. X                flags = HYPHN;
  154. X                break;
  155. X            case 'p':
  156. X                flags |= PARAG;
  157. X                break;
  158. X            case 'w':
  159. X                --argc;
  160. X                pb = *++argv;
  161. X                nl=0;
  162. X                do {
  163. X                    for(ll[nl]=0; *pb>='0' && *pb<='9'; pb++)
  164. X                        ll[nl] = ll[nl]*10+*pb-'0';
  165. X                    nl++;
  166. X                }while(*pb++);
  167. X            }
  168. X        else {
  169. X            close(0);
  170. X            open(*argv, 0, 0);
  171. X        }
  172. X    nl = 2;
  173. X    pb = pw = buf-1;
  174. X    while((c = getchar()) != EOF) {
  175. X        if(c==' ' || c=='\t' || c=='\n') {
  176. X            if(pb>=buf && *pb!=' ') {
  177. X                *++pb = ' ';
  178. X                nw++;
  179. X                pw = pb;    /* word marker in buf */
  180. X            }
  181. X            if(c == '\n') {
  182. X                if(++nl > 1) {
  183. X                    if(nw > 0) {
  184. X                        putline(pb, NOADJ);
  185. X                        pb = pw = buf-1;
  186. X                        nw = 0;
  187. X                    }
  188. X                    putchar('\n');
  189. X                    ind = 0;
  190. X                }
  191. X            } else if(nl > 1) {
  192. X                if(c == '\t')
  193. X                    ind = (ind/8+1)*8;
  194. X                else
  195. X                    ind++;
  196. X            }
  197. X        } else if(c>' ' && c<0177) {
  198. X            *++pb = c;
  199. X            if(nl) {
  200. X                nl = 0;
  201. X                pe = &buf[ll[nll]-ind];
  202. X            }
  203. X        }
  204. X        if(pb == pe) {    /* line overflow */
  205. X            if((flags&HYPHN) && pb-pw>2) {    /* insert hyphen */
  206. X                *++pb = pe[-1];
  207. X                pe[-1] = '-';
  208. X                *++pb = c;
  209. X                pw = pe;
  210. X                nw++;
  211. X            } else if(nw == 0) {    /* insert blank */
  212. X                *++pb = c;
  213. X                pw = pe;
  214. X                nw++;
  215. X            }
  216. X            putline(pw, flags);
  217. X            for(pe=buf, ++pw; pw<=pb; )
  218. X                *pe++ = *pw++;
  219. X            pb = pe-1;
  220. X            pw = buf-1;
  221. X            pe = &buf[ll[nll]-ind];
  222. X            nw = 0;
  223. X        }
  224. X    }
  225. X    if(nw > 0)
  226. X        putline(pb, NOADJ);
  227. X    return(0);
  228. X}
  229. X
  230. X/*
  231. X * adjust & print line
  232. X */
  233. Xputline(pw, flags)
  234. X    register char *pw;
  235. X{
  236. X    register char *pb;
  237. X    register i, n, b, m;
  238. X
  239. X    for(i=ind; i>=8; i-=8)
  240. X        putchar('\t');
  241. X    for(; i>0; i--)
  242. X        putchar(' ');
  243. X    n = nw-1;
  244. X    b = buf+ll[nll]-ind-pw;    /* no. of blanks to distribute */
  245. X    m = n/2;
  246. X    for(pb=buf; pb<pw; pb++) {
  247. X        putchar(*pb);
  248. X        if(!(flags&NOADJ) && *pb==' ') {
  249. X            m += b;
  250. X            for(i=m/n; i>0; i--)
  251. X                putchar(' ');
  252. X            m %= n;
  253. X        }
  254. X    }
  255. X    putchar('\n');
  256. X    /* cyclic increment line lengths vector */
  257. X    if(ll[++nll] == 0)
  258. X        nll = 0;
  259. X    if(flags&PARAG)
  260. X        ind = 0;
  261. X}
  262. END-of-adj.c
  263. fi
  264. if [ -f ctr.6 ]
  265. then
  266. echo file ctr.6 exists
  267. else
  268. echo extracting file: ctr.6
  269. sed 's/^X//' >ctr.6 << 'END-of-ctr.6'
  270. X.TH CTR 6 Local
  271. X.UC 4
  272. X.SH NAME
  273. Xctr \- center text lines
  274. X.SH SYNOPSIS
  275. X.B ctr
  276. X[
  277. X.B \-w
  278. X.I n
  279. X]
  280. X.SH DESCRIPTION
  281. X.I Ctr
  282. Xcopies its standard input to its standard output,
  283. Xcentering text in the output lines.
  284. X.PP
  285. XThe
  286. X.B \-w
  287. X.I n
  288. X(width) option sets the output line width to
  289. X.I n
  290. X(default 65).
  291. X.SH "SEE ALSO"
  292. Xadj(6)
  293. X.SH BUGS
  294. END-of-ctr.6
  295. fi
  296. if [ -f ctr.c ]
  297. then
  298. echo file ctr.c exists
  299. else
  300. echo extracting file: ctr.c
  301. sed 's/^X//' >ctr.c << 'END-of-ctr.c'
  302. X#define NULL ((char *)0)
  303. Xmain(argc, argv)
  304. X    char **argv;
  305. X{
  306. X    static int ll = 65;    /* line length */
  307. X    static buf[100];
  308. X
  309. X    if(argc > 1 && **++argv == '-' && argv[0][1] == 'w')
  310. X            ll = atoi(*++argv);
  311. X    while(gets(buf) != NULL)
  312. X        printf("%*s%s\n", (ll-strlen(buf))/2, "", buf);
  313. X}
  314. END-of-ctr.c
  315. fi
  316. exit 0
  317.  
  318.         o /        o /        o /        o /
  319. -----------------X---------------X---------------X---------------X----
  320.         o \        o \        o \        o \
  321. May the Source be with you, always...
  322. -- 
  323.     Amos Shapir
  324. National Semiconductor (Israel)
  325. 6 Maskit st. P.O.B. 3007, Herzlia 46104, Israel  Tel. (972)52-522261
  326. amos%nsta@nsc.com {hplabs,pyramid,sun,decwrl} 34.48'E 32.10'N
  327.  
  328.  
  329.