xref: /onnv-gate/usr/src/cmd/vi/misc/fold.c (revision 802:73b56fb6544b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*802Scf46844 /*
23*802Scf46844  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*802Scf46844  * Use is subject to license terms.
25*802Scf46844  */
26*802Scf46844 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  * fold - fold long lines for finite output devices
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate int	fold =  80;
490Sstevel@tonic-gate 
50*802Scf46844 void putch(int c);
51*802Scf46844 
52*802Scf46844 int
main(int argc,char * argv[])53*802Scf46844 main(int argc, char *argv[])
540Sstevel@tonic-gate {
55*802Scf46844 	int c;
560Sstevel@tonic-gate 	FILE *f;
570Sstevel@tonic-gate 	char obuf[BUFSIZ];
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	argc--, argv++;
600Sstevel@tonic-gate 	setbuf(stdout, obuf);
610Sstevel@tonic-gate 	if (argc > 0 && argv[0][0] == '-') {
620Sstevel@tonic-gate 		fold = 0;
630Sstevel@tonic-gate 		argv[0]++;
640Sstevel@tonic-gate 		while (*argv[0] >= '0' && *argv[0] <= '9')
650Sstevel@tonic-gate 			fold *= 10, fold += *argv[0]++ - '0';
660Sstevel@tonic-gate 		if (*argv[0]) {
670Sstevel@tonic-gate 			printf("Bad number for fold\n");
680Sstevel@tonic-gate 			exit(1);
690Sstevel@tonic-gate 		}
700Sstevel@tonic-gate 		argc--, argv++;
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate 	do {
730Sstevel@tonic-gate 		if (argc > 0) {
740Sstevel@tonic-gate 			if (freopen(argv[0], "r", stdin) == NULL) {
750Sstevel@tonic-gate 				perror(argv[0]);
760Sstevel@tonic-gate 				exit(1);
770Sstevel@tonic-gate 			}
780Sstevel@tonic-gate 			argc--, argv++;
790Sstevel@tonic-gate 		}
800Sstevel@tonic-gate 		for (;;) {
810Sstevel@tonic-gate 			c = getc(stdin);
820Sstevel@tonic-gate 			if (c == -1)
830Sstevel@tonic-gate 				break;
840Sstevel@tonic-gate 			putch(c);
850Sstevel@tonic-gate 		}
860Sstevel@tonic-gate 	} while (argc > 0);
87*802Scf46844 	return (0);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate int	col;
910Sstevel@tonic-gate 
92*802Scf46844 void
putch(int c)93*802Scf46844 putch(int c)
940Sstevel@tonic-gate {
95*802Scf46844 	int ncol;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	switch (c) {
980Sstevel@tonic-gate 		case '\n':
990Sstevel@tonic-gate 			ncol = 0;
1000Sstevel@tonic-gate 			break;
1010Sstevel@tonic-gate 		case '\t':
1020Sstevel@tonic-gate 			ncol = (col + 8) &~ 7;
1030Sstevel@tonic-gate 			break;
1040Sstevel@tonic-gate 		case '\b':
1050Sstevel@tonic-gate 			ncol = col ? col - 1 : 0;
1060Sstevel@tonic-gate 			break;
1070Sstevel@tonic-gate 		case '\r':
1080Sstevel@tonic-gate 			ncol = 0;
1090Sstevel@tonic-gate 			break;
1100Sstevel@tonic-gate 		default:
1110Sstevel@tonic-gate 			ncol = col + 1;
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 	if (ncol > fold)
1140Sstevel@tonic-gate 		putchar('\n'), col = 0;
1150Sstevel@tonic-gate 	putchar(c);
1160Sstevel@tonic-gate 	switch (c) {
1170Sstevel@tonic-gate 		case '\n':
1180Sstevel@tonic-gate 			col = 0;
1190Sstevel@tonic-gate 			break;
1200Sstevel@tonic-gate 		case '\t':
1210Sstevel@tonic-gate 			col += 8;
1220Sstevel@tonic-gate 			col &= ~7;
1230Sstevel@tonic-gate 			break;
1240Sstevel@tonic-gate 		case '\b':
1250Sstevel@tonic-gate 			if (col)
1260Sstevel@tonic-gate 				col--;
1270Sstevel@tonic-gate 			break;
1280Sstevel@tonic-gate 		case '\r':
1290Sstevel@tonic-gate 			col = 0;
1300Sstevel@tonic-gate 			break;
1310Sstevel@tonic-gate 		default:
1320Sstevel@tonic-gate 			col++;
1330Sstevel@tonic-gate 			break;
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate }
136