xref: /original-bsd/usr.bin/vis/foldit.c (revision 817cfbae)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)foldit.c	5.1 (Berkeley) 04/18/91";
10 #endif /* not lint */
11 
12 foldit(chunk, col, max)
13 	char *chunk;
14 {
15 	register char *cp;
16 
17 	/*
18 	 * Keep track of column position. Insert hidden newline
19 	 * if this chunk puts us over the limit.
20 	 */
21 again:
22 	cp = chunk;
23 	while (*cp) {
24 		switch(*cp) {
25 		case '\n':
26 		case '\r':
27 			col = 0;
28 			break;
29 		case '\t':
30 			col = col + 8 &~ 07;
31 			break;
32 		case '\b':
33 			col = col ? col - 1 : 0;
34 			break;
35 		default:
36 			col++;
37 		}
38 		if (col > (max - 2)) {
39 			printf("\\\n");
40 			col = 0;
41 			goto again;
42 		}
43 		cp++;
44 	}
45 	return (col);
46 }
47