xref: /minix3/usr.bin/vis/foldit.c (revision 06f01a55d2b92b3b7d83b77547fb614e1fed0d9d)
1*06f01a55SThomas Cort /*	$NetBSD: foldit.c,v 1.7 2009/02/10 23:06:31 christos Exp $	*/
2*06f01a55SThomas Cort 
3*06f01a55SThomas Cort /*-
4*06f01a55SThomas Cort  * Copyright (c) 1990, 1993
5*06f01a55SThomas Cort  *	The Regents of the University of California.  All rights reserved.
6*06f01a55SThomas Cort  *
7*06f01a55SThomas Cort  * Redistribution and use in source and binary forms, with or without
8*06f01a55SThomas Cort  * modification, are permitted provided that the following conditions
9*06f01a55SThomas Cort  * are met:
10*06f01a55SThomas Cort  * 1. Redistributions of source code must retain the above copyright
11*06f01a55SThomas Cort  *    notice, this list of conditions and the following disclaimer.
12*06f01a55SThomas Cort  * 2. Redistributions in binary form must reproduce the above copyright
13*06f01a55SThomas Cort  *    notice, this list of conditions and the following disclaimer in the
14*06f01a55SThomas Cort  *    documentation and/or other materials provided with the distribution.
15*06f01a55SThomas Cort  * 3. Neither the name of the University nor the names of its contributors
16*06f01a55SThomas Cort  *    may be used to endorse or promote products derived from this software
17*06f01a55SThomas Cort  *    without specific prior written permission.
18*06f01a55SThomas Cort  *
19*06f01a55SThomas Cort  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*06f01a55SThomas Cort  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*06f01a55SThomas Cort  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*06f01a55SThomas Cort  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*06f01a55SThomas Cort  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*06f01a55SThomas Cort  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*06f01a55SThomas Cort  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*06f01a55SThomas Cort  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*06f01a55SThomas Cort  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*06f01a55SThomas Cort  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*06f01a55SThomas Cort  * SUCH DAMAGE.
30*06f01a55SThomas Cort  */
31*06f01a55SThomas Cort 
32*06f01a55SThomas Cort #include <sys/cdefs.h>
33*06f01a55SThomas Cort #ifndef lint
34*06f01a55SThomas Cort #if 0
35*06f01a55SThomas Cort static char sccsid[] = "@(#)foldit.c	8.1 (Berkeley) 6/6/93";
36*06f01a55SThomas Cort #endif
37*06f01a55SThomas Cort __RCSID("$NetBSD: foldit.c,v 1.7 2009/02/10 23:06:31 christos Exp $");
38*06f01a55SThomas Cort #endif /* not lint */
39*06f01a55SThomas Cort 
40*06f01a55SThomas Cort #include <stdio.h>
41*06f01a55SThomas Cort #include <vis.h>
42*06f01a55SThomas Cort #include "extern.h"
43*06f01a55SThomas Cort 
44*06f01a55SThomas Cort int
foldit(const char * chunk,int col,int max,int flags)45*06f01a55SThomas Cort foldit(const char *chunk, int col, int max, int flags)
46*06f01a55SThomas Cort {
47*06f01a55SThomas Cort 	const char *cp;
48*06f01a55SThomas Cort 
49*06f01a55SThomas Cort 	/*
50*06f01a55SThomas Cort 	 * Keep track of column position. Insert hidden newline
51*06f01a55SThomas Cort 	 * if this chunk puts us over the limit.
52*06f01a55SThomas Cort 	 */
53*06f01a55SThomas Cort again:
54*06f01a55SThomas Cort 	cp = chunk;
55*06f01a55SThomas Cort 	while (*cp) {
56*06f01a55SThomas Cort 		switch(*cp) {
57*06f01a55SThomas Cort 		case '\n':
58*06f01a55SThomas Cort 		case '\r':
59*06f01a55SThomas Cort 			col = 0;
60*06f01a55SThomas Cort 			break;
61*06f01a55SThomas Cort 		case '\t':
62*06f01a55SThomas Cort 			col = (col + 8) &~ 07;
63*06f01a55SThomas Cort 			break;
64*06f01a55SThomas Cort 		case '\b':
65*06f01a55SThomas Cort 			col = col ? col - 1 : 0;
66*06f01a55SThomas Cort 			break;
67*06f01a55SThomas Cort 		default:
68*06f01a55SThomas Cort 			col++;
69*06f01a55SThomas Cort 		}
70*06f01a55SThomas Cort 		if (col > (max - 2)) {
71*06f01a55SThomas Cort 			printf(flags & VIS_MIMESTYLE ? "=\n" : "\\\n");
72*06f01a55SThomas Cort 			col = 0;
73*06f01a55SThomas Cort 			goto again;
74*06f01a55SThomas Cort 		}
75*06f01a55SThomas Cort 		cp++;
76*06f01a55SThomas Cort 	}
77*06f01a55SThomas Cort 	return (col);
78*06f01a55SThomas Cort }
79