1247128Sbrooks/*	$NetBSD: foldit.c,v 1.7 2009/02/10 23:06:31 christos Exp $	*/
2247128Sbrooks
3247128Sbrooks/*-
4247128Sbrooks * Copyright (c) 1990, 1993
5247128Sbrooks *	The Regents of the University of California.  All rights reserved.
6247128Sbrooks *
7247128Sbrooks * Redistribution and use in source and binary forms, with or without
8247128Sbrooks * modification, are permitted provided that the following conditions
9247128Sbrooks * are met:
10247128Sbrooks * 1. Redistributions of source code must retain the above copyright
11247128Sbrooks *    notice, this list of conditions and the following disclaimer.
12247128Sbrooks * 2. Redistributions in binary form must reproduce the above copyright
13247128Sbrooks *    notice, this list of conditions and the following disclaimer in the
14247128Sbrooks *    documentation and/or other materials provided with the distribution.
15247128Sbrooks * 3. Neither the name of the University nor the names of its contributors
16247128Sbrooks *    may be used to endorse or promote products derived from this software
17247128Sbrooks *    without specific prior written permission.
18247128Sbrooks *
19247128Sbrooks * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20247128Sbrooks * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21247128Sbrooks * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22247128Sbrooks * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23247128Sbrooks * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24247128Sbrooks * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25247128Sbrooks * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26247128Sbrooks * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27247128Sbrooks * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28247128Sbrooks * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29247128Sbrooks * SUCH DAMAGE.
30247128Sbrooks */
31247128Sbrooks
32247128Sbrooks#include <sys/cdefs.h>
33247128Sbrooks#ifndef lint
34247128Sbrooks#if 0
35247128Sbrooksstatic char sccsid[] = "@(#)foldit.c	8.1 (Berkeley) 6/6/93";
36247128Sbrooks#endif
37247128Sbrooks__RCSID("$NetBSD: foldit.c,v 1.7 2009/02/10 23:06:31 christos Exp $");
38247128Sbrooks#endif /* not lint */
39247128Sbrooks
40247128Sbrooks#include <stdio.h>
41247128Sbrooks#include <vis.h>
42247128Sbrooks#include "extern.h"
43247128Sbrooks
44247128Sbrooksint
45247128Sbrooksfoldit(const char *chunk, int col, int max, int flags)
46247128Sbrooks{
47247128Sbrooks	const char *cp;
48247128Sbrooks
49247128Sbrooks	/*
50247128Sbrooks	 * Keep track of column position. Insert hidden newline
51247128Sbrooks	 * if this chunk puts us over the limit.
52247128Sbrooks	 */
53247128Sbrooksagain:
54247128Sbrooks	cp = chunk;
55247128Sbrooks	while (*cp) {
56247128Sbrooks		switch(*cp) {
57247128Sbrooks		case '\n':
58247128Sbrooks		case '\r':
59247128Sbrooks			col = 0;
60247128Sbrooks			break;
61247128Sbrooks		case '\t':
62247128Sbrooks			col = (col + 8) &~ 07;
63247128Sbrooks			break;
64247128Sbrooks		case '\b':
65247128Sbrooks			col = col ? col - 1 : 0;
66247128Sbrooks			break;
67247128Sbrooks		default:
68247128Sbrooks			col++;
69247128Sbrooks		}
70247128Sbrooks		if (col > (max - 2)) {
71247128Sbrooks			printf(flags & VIS_MIMESTYLE ? "=\n" : "\\\n");
72247128Sbrooks			col = 0;
73247128Sbrooks			goto again;
74247128Sbrooks		}
75247128Sbrooks		cp++;
76247128Sbrooks	}
77247128Sbrooks	return (col);
78247128Sbrooks}
79