head	1.5;
access;
symbols
	perseant-exfatfs-base-20250801:1.5
	perseant-exfatfs-base-20240630:1.5
	perseant-exfatfs:1.5.0.36
	perseant-exfatfs-base:1.5
	cjep_sun2x:1.5.0.34
	cjep_sun2x-base:1.5
	cjep_staticlib_x-base1:1.5
	cjep_staticlib_x:1.5.0.32
	cjep_staticlib_x-base:1.5
	phil-wifi-20200421:1.5
	phil-wifi-20200411:1.5
	phil-wifi-20200406:1.5
	pgoyette-compat-merge-20190127:1.5
	pgoyette-compat-20190127:1.5
	pgoyette-compat-20190118:1.5
	pgoyette-compat-1226:1.5
	pgoyette-compat-1126:1.5
	pgoyette-compat-1020:1.5
	pgoyette-compat-0930:1.5
	pgoyette-compat-0906:1.5
	pgoyette-compat-0728:1.5
	pgoyette-compat-0625:1.5
	pgoyette-compat-0521:1.5
	pgoyette-compat-0502:1.5
	pgoyette-compat-0422:1.5
	pgoyette-compat-0415:1.5
	pgoyette-compat-0407:1.5
	pgoyette-compat-0330:1.5
	pgoyette-compat-0322:1.5
	pgoyette-compat-0315:1.5
	pgoyette-compat:1.5.0.30
	pgoyette-compat-base:1.5
	perseant-stdc-iso10646:1.5.0.28
	perseant-stdc-iso10646-base:1.5
	prg-localcount2-base3:1.5
	prg-localcount2-base2:1.5
	prg-localcount2-base1:1.5
	prg-localcount2:1.5.0.26
	prg-localcount2-base:1.5
	pgoyette-localcount-20170426:1.5
	bouyer-socketcan-base1:1.5
	pgoyette-localcount-20170320:1.5
	bouyer-socketcan:1.5.0.24
	bouyer-socketcan-base:1.5
	pgoyette-localcount-20170107:1.5
	pgoyette-localcount-20161104:1.5
	localcount-20160914:1.5
	pgoyette-localcount-20160806:1.5
	pgoyette-localcount-20160726:1.5
	pgoyette-localcount:1.5.0.22
	pgoyette-localcount-base:1.5
	netbsd-5-2-3-RELEASE:1.5
	netbsd-5-1-5-RELEASE:1.5
	yamt-pagecache-base9:1.5
	yamt-pagecache-tag8:1.5
	tls-earlyentropy:1.5.0.18
	tls-earlyentropy-base:1.5
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.5
	riastradh-drm2-base3:1.5
	netbsd-5-2-2-RELEASE:1.5
	netbsd-5-1-4-RELEASE:1.5
	netbsd-5-2-1-RELEASE:1.5
	netbsd-5-1-3-RELEASE:1.5
	agc-symver:1.5.0.20
	agc-symver-base:1.5
	tls-maxphys-base:1.5
	yamt-pagecache-base8:1.5
	netbsd-5-2:1.5.0.16
	yamt-pagecache-base7:1.5
	netbsd-5-2-RELEASE:1.5
	netbsd-5-2-RC1:1.5
	yamt-pagecache-base6:1.5
	yamt-pagecache-base5:1.5
	yamt-pagecache-base4:1.5
	netbsd-5-1-2-RELEASE:1.5
	netbsd-5-1-1-RELEASE:1.5
	yamt-pagecache-base3:1.5
	yamt-pagecache-base2:1.5
	yamt-pagecache:1.5.0.14
	yamt-pagecache-base:1.5
	bouyer-quota2-nbase:1.5
	bouyer-quota2:1.5.0.12
	bouyer-quota2-base:1.5
	matt-nb5-pq3:1.5.0.10
	matt-nb5-pq3-base:1.5
	netbsd-5-1:1.5.0.8
	netbsd-5-1-RELEASE:1.5
	netbsd-5-1-RC4:1.5
	netbsd-5-1-RC3:1.5
	netbsd-5-1-RC2:1.5
	netbsd-5-1-RC1:1.5
	netbsd-5-0-2-RELEASE:1.5
	netbsd-5-0-1-RELEASE:1.5
	jym-xensuspend-nbase:1.5
	netbsd-5-0:1.5.0.6
	netbsd-5-0-RELEASE:1.5
	netbsd-5-0-RC4:1.5
	netbsd-5-0-RC3:1.5
	netbsd-5-0-RC2:1.5
	jym-xensuspend:1.5.0.4
	jym-xensuspend-base:1.5
	netbsd-5-0-RC1:1.5
	netbsd-5:1.5.0.2
	netbsd-5-base:1.5
	matt-mips64-base2:1.4
	matt-mips64:1.4.0.40
	mjf-devfs2:1.4.0.38
	mjf-devfs2-base:1.4
	netbsd-4-0-1-RELEASE:1.4
	wrstuden-revivesa-base-3:1.4
	wrstuden-revivesa-base-2:1.4
	wrstuden-fixsa-newbase:1.4
	wrstuden-revivesa-base-1:1.4
	yamt-pf42-base4:1.4
	yamt-pf42-base3:1.4
	hpcarm-cleanup-nbase:1.4
	yamt-pf42-baseX:1.4
	yamt-pf42-base2:1.4
	wrstuden-revivesa:1.4.0.36
	wrstuden-revivesa-base:1.4
	yamt-pf42:1.4.0.34
	yamt-pf42-base:1.4
	keiichi-mipv6-nbase:1.4
	keiichi-mipv6:1.4.0.32
	keiichi-mipv6-base:1.4
	matt-armv6-nbase:1.4
	matt-armv6-prevmlocking:1.4
	wrstuden-fixsa-base-1:1.4
	netbsd-4-0:1.4.0.30
	netbsd-4-0-RELEASE:1.4
	cube-autoconf:1.4.0.28
	cube-autoconf-base:1.4
	netbsd-4-0-RC5:1.4
	netbsd-4-0-RC4:1.4
	netbsd-4-0-RC3:1.4
	netbsd-4-0-RC2:1.4
	netbsd-4-0-RC1:1.4
	matt-armv6:1.4.0.26
	matt-armv6-base:1.4
	matt-mips64-base:1.4
	hpcarm-cleanup:1.4.0.24
	hpcarm-cleanup-base:1.4
	netbsd-3-1-1-RELEASE:1.4
	netbsd-3-0-3-RELEASE:1.4
	wrstuden-fixsa:1.4.0.22
	wrstuden-fixsa-base:1.4
	abandoned-netbsd-4-base:1.4
	abandoned-netbsd-4:1.4.0.16
	netbsd-3-1:1.4.0.18
	netbsd-3-1-RELEASE:1.4
	netbsd-3-0-2-RELEASE:1.4
	netbsd-3-1-RC4:1.4
	netbsd-3-1-RC3:1.4
	netbsd-3-1-RC2:1.4
	netbsd-3-1-RC1:1.4
	netbsd-4:1.4.0.20
	netbsd-4-base:1.4
	chap-midi-nbase:1.4
	netbsd-3-0-1-RELEASE:1.4
	chap-midi:1.4.0.14
	chap-midi-base:1.4
	netbsd-3-0:1.4.0.12
	netbsd-3-0-RELEASE:1.4
	netbsd-3-0-RC6:1.4
	netbsd-3-0-RC5:1.4
	netbsd-3-0-RC4:1.4
	netbsd-3-0-RC3:1.4
	netbsd-3-0-RC2:1.4
	netbsd-3-0-RC1:1.4
	netbsd-2-0-3-RELEASE:1.4
	netbsd-2-1:1.4.0.10
	netbsd-2-1-RELEASE:1.4
	netbsd-2-1-RC6:1.4
	netbsd-2-1-RC5:1.4
	netbsd-2-1-RC4:1.4
	netbsd-2-1-RC3:1.4
	netbsd-2-1-RC2:1.4
	netbsd-2-1-RC1:1.4
	netbsd-2-0-2-RELEASE:1.4
	netbsd-3:1.4.0.8
	netbsd-3-base:1.4
	netbsd-2-0-1-RELEASE:1.4
	netbsd-2:1.4.0.6
	netbsd-2-base:1.4
	netbsd-2-0-RELEASE:1.4
	netbsd-2-0-RC5:1.4
	netbsd-2-0-RC4:1.4
	netbsd-2-0-RC3:1.4
	netbsd-2-0-RC2:1.4
	netbsd-2-0-RC1:1.4
	netbsd-2-0:1.4.0.4
	netbsd-2-0-base:1.4
	netbsd-1-6-PATCH002-RELEASE:1.4
	netbsd-1-6-PATCH002:1.4
	netbsd-1-6-PATCH002-RC4:1.4
	netbsd-1-6-PATCH002-RC3:1.4
	netbsd-1-6-PATCH002-RC2:1.4
	netbsd-1-6-PATCH002-RC1:1.4
	netbsd-1-6-PATCH001:1.4
	netbsd-1-6-PATCH001-RELEASE:1.4
	netbsd-1-6-PATCH001-RC3:1.4
	netbsd-1-6-PATCH001-RC2:1.4
	netbsd-1-6-PATCH001-RC1:1.4
	fvdl_fs64_base:1.4
	netbsd-1-6-RELEASE:1.4
	netbsd-1-6-RC3:1.4
	netbsd-1-6-RC2:1.4
	netbsd-1-6-RC1:1.4
	netbsd-1-6:1.4.0.2
	netbsd-1-6-base:1.4
	netbsd-1-5-PATCH003:1.2
	netbsd-1-5-PATCH002:1.2
	netbsd-1-5-PATCH001:1.2
	nvi_1_79:1.1.1.4
	netbsd-1-5-RELEASE:1.2
	netbsd-1-5-BETA2:1.2
	netbsd-1-5-BETA:1.2
	netbsd-1-4-PATCH003:1.2
	netbsd-1-5-ALPHA2:1.2
	netbsd-1-5:1.2.0.10
	netbsd-1-5-base:1.2
	minoura-xpg4dl-base:1.2
	minoura-xpg4dl:1.2.0.8
	netbsd-1-4-PATCH002:1.2
	wrstuden-devbsize-19991221:1.2
	wrstuden-devbsize:1.2.0.6
	wrstuden-devbsize-base:1.2
	comdex-fall-1999:1.2.0.4
	comdex-fall-1999-base:1.2
	netbsd-1-4-PATCH001:1.2
	netbsd-1-4-RELEASE:1.2
	netbsd-1-4:1.2.0.2
	netbsd-1-4-base:1.2
	netbsd-1-3-PATCH003:1.1.1.3
	netbsd-1-3-PATCH003-CANDIDATE2:1.1.1.3
	netbsd-1-3-PATCH003-CANDIDATE1:1.1.1.3
	netbsd-1-3-PATCH003-CANDIDATE0:1.1.1.3
	netbsd-1-3-PATCH002:1.1.1.3
	netbsd-1-3-PATCH001:1.1.1.3
	netbsd-1-3-RELEASE:1.1.1.3
	netbsd-1-3-BETA:1.1.1.3
	netbsd-1-3:1.1.1.3.0.2
	netbsd-1-3-base:1.1.1.3
	netbsd-1-2-PATCH001:1.1.1.3
	netbsd-1-2-RELEASE:1.1.1.3
	netbsd-1-2-BETA:1.1.1.3
	netbsd-1-2:1.1.1.3.0.4
	netbsd-1-2-base:1.1.1.3
	nvi_1_66:1.1.1.3
	BOSTIC:1.1.1
	netbsd-1-1-PATCH001:1.1.1.2
	netbsd-1-1-RELEASE:1.1.1.2
	netbsd-1-1:1.1.1.2.0.4
	netbsd-1-1-base:1.1.1.2
	netbsd-1-0-PATCH06:1.1.1.2
	netbsd-1-0-PATCH05:1.1.1.2
	netbsd-1-0-PATCH04:1.1.1.2
	netbsd-1-0-PATCH03:1.1.1.2
	netbsd-1-0-PATCH02:1.1.1.2
	netbsd-1-0-PATCH1:1.1.1.2
	netbsd-1-0-PATCH0:1.1.1.2
	netbsd-1-0-RELEASE:1.1.1.2
	netbsd-1-0:1.1.1.2.0.2
	nvi-1-34b:1.1.1.2
	nvi-1-33b:1.1.1.1
	bostic-nvi:1.1.1;
locks; strict;
comment	@ * @;


1.5
date	2008.10.29.16.49.35;	author christos;	state dead;
branches;
next	1.4;

1.4
date	2002.04.09.01.47.32;	author thorpej;	state Exp;
branches;
next	1.3;

1.3
date	2001.03.31.11.37.47;	author aymeric;	state Exp;
branches;
next	1.2;

1.2
date	98.01.09.08.07.11;	author perry;	state Exp;
branches;
next	1.1;

1.1
date	94.08.17.16.19.06;	author cgd;	state Exp;
branches
	1.1.1.1;
next	;

1.1.1.1
date	94.08.17.16.19.07;	author cgd;	state Exp;
branches;
next	1.1.1.2;

1.1.1.2
date	94.08.17.19.26.16;	author cgd;	state Exp;
branches
	1.1.1.2.2.1;
next	1.1.1.3;

1.1.1.3
date	96.05.20.02.06.08;	author mrg;	state Exp;
branches;
next	1.1.1.4;

1.1.1.4
date	2001.03.31.11.30.03;	author aymeric;	state Exp;
branches;
next	;

1.1.1.2.2.1
date	94.08.17.19.26.16;	author cgd;	state dead;
branches;
next	1.1.1.2.2.2;

1.1.1.2.2.2
date	94.08.17.19.26.17;	author cgd;	state Exp;
branches;
next	;


desc
@@


1.5
log
@bye old vi!
@
text
@/*	$NetBSD: util.c,v 1.4 2002/04/09 01:47:32 thorpej Exp $	*/

/*-
 * Copyright (c) 1991, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 1991, 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
 *
 * See the LICENSE file for redistribution information.
 */

#include "config.h"

#include <sys/cdefs.h>
#ifndef lint
#if 0
static const char sccsid[] = "@@(#)util.c	10.11 (Berkeley) 9/15/96";
#else
__RCSID("$NetBSD: util.c,v 1.4 2002/04/09 01:47:32 thorpej Exp $");
#endif
#endif /* not lint */

#include <sys/types.h>
#include <sys/queue.h>

#include <bitstring.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "common.h"

/*
 * binc --
 *	Increase the size of a buffer.
 *
 * PUBLIC: void *binc __P((SCR *, void *, size_t *, size_t));
 */
void *
binc(sp, bp, bsizep, min)
	SCR *sp;			/* sp MAY BE NULL!!! */
	void *bp;
	size_t *bsizep, min;
{
	size_t csize;

	/* If already larger than the minimum, just return. */
	if (min && *bsizep >= min)
		return (bp);

	csize = *bsizep + MAX(min, 256);
	REALLOC(sp, bp, void *, csize);

	if (bp == NULL) {
		/*
		 * Theoretically, realloc is supposed to leave any already
		 * held memory alone if it can't get more.  Don't trust it.
		 */
		*bsizep = 0;
		return (NULL);
	}
	/*
	 * Memory is guaranteed to be zero-filled, various parts of
	 * nvi depend on this.
	 */
	memset((char *)bp + *bsizep, 0, csize - *bsizep);
	*bsizep = csize;
	return (bp);
}

/*
 * nonblank --
 *	Set the column number of the first non-blank character
 *	including or after the starting column.  On error, set
 *	the column to 0, it's safest.
 *
 * PUBLIC: int nonblank __P((SCR *, recno_t, size_t *));
 */
int
nonblank(sp, lno, cnop)
	SCR *sp;
	recno_t lno;
	size_t *cnop;
{
	char *p;
	size_t cnt, len, off;
	int isempty;

	/* Default. */
	off = *cnop;
	*cnop = 0;

	/* Get the line, succeeding in an empty file. */
	if (db_eget(sp, lno, &p, &len, &isempty))
		return (!isempty);

	/* Set the offset. */
	if (len == 0 || off >= len)
		return (0);

	for (cnt = off, p = &p[off],
	    len -= off; len && isblank(*p); ++cnt, ++p, --len);

	/* Set the return. */
	*cnop = len ? cnt : cnt - 1;
	return (0);
}

/*
 * tail --
 *	Return tail of a path.
 *
 * PUBLIC: char *tail __P((char *));
 */
char *
tail(path)
	char *path;
{
	char *p;

	if ((p = strrchr(path, '/')) == NULL)
		return (path);
	return (p + 1);
}

/*
 * v_strdup --
 *	Strdup for wide character strings with an associated length.
 *
 * PUBLIC: CHAR_T *v_strdup __P((SCR *, const CHAR_T *, size_t));
 */
CHAR_T *
v_strdup(sp, str, len)
	SCR *sp;
	const CHAR_T *str;
	size_t len;
{
	CHAR_T *copy;

	MALLOC(sp, copy, CHAR_T *, len + 1);
	if (copy == NULL)
		return (NULL);
	memcpy(copy, str, len * sizeof(CHAR_T));
	copy[len] = '\0';
	return (copy);
}

/*
 * nget_uslong --
 *      Get an unsigned long, checking for overflow.
 *
 * PUBLIC: enum nresult nget_uslong __P((u_long *, const char *, char **, int));
 */
enum nresult
nget_uslong(valp, p, endp, base)
	u_long *valp;
	const char *p;
	char **endp;
	int base;
{
	errno = 0;
	*valp = strtoul(p, endp, base);
	if (errno == 0)
		return (NUM_OK);
	if (errno == ERANGE && *valp == ULONG_MAX)
		return (NUM_OVER);
	return (NUM_ERR);
}

/*
 * nget_slong --
 *      Convert a signed long, checking for overflow and underflow.
 *
 * PUBLIC: enum nresult nget_slong __P((long *, const char *, char **, int));
 */
enum nresult
nget_slong(valp, p, endp, base)
	long *valp;
	const char *p;
	char **endp;
	int base;
{
	errno = 0;
	*valp = strtol(p, endp, base);
	if (errno == 0)
		return (NUM_OK);
	if (errno == ERANGE) {
		if (*valp == LONG_MAX)
			return (NUM_OVER);
		if (*valp == LONG_MIN)
			return (NUM_UNDER);
	}
	return (NUM_ERR);
}

#ifdef DEBUG
#ifdef __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif

/*
 * TRACE --
 *	debugging trace routine.
 *
 * PUBLIC: void TRACE __P((SCR *, const char *, ...));
 */
void
#ifdef __STDC__
TRACE(SCR *sp, const char *fmt, ...)
#else
TRACE(sp, fmt, va_alist)
	SCR *sp;
	char *fmt;
	va_dcl
#endif
{
	FILE *tfp;
	va_list ap;

	if ((tfp = sp->gp->tracefp) == NULL)
		return;
#ifdef __STDC__
	va_start(ap, fmt);
#else
	va_start(ap);
#endif
	(void)vfprintf(tfp, fmt, ap);
	va_end(ap);

	(void)fflush(tfp);
}
#endif
@


1.4
log
@Use __RCSID() and __COPYRIGHT().
@
text
@d1 1
a1 1
/*	$NetBSD: util.c,v 1.3 2001/03/31 11:37:47 aymeric Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD$");
@


1.3
log
@merge changes after import of nvi 1.79
@
text
@d1 1
a1 1
/*	$NetBSD: util.c,v 1.2 1998/01/09 08:07:11 perry Exp $	*/
d14 1
d16 1
d18 3
@


1.2
log
@RCS Id Police.
@
text
@d1 1
a1 1
/*	$NetBSD$	*/
d15 1
a15 1
static const char sccsid[] = "@@(#)util.c	10.10 (Berkeley) 3/6/96";
d141 1
a141 1
	memmove(copy, str, len * sizeof(CHAR_T));
@


1.1
log
@Initial revision
@
text
@d1 2
d6 2
d9 1
a9 27
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
d12 2
d15 1
a15 1
static const char sccsid[] = "@@(#)util.c	8.73 (Berkeley) 8/17/94";
a19 1
#include <sys/time.h>
d22 1
a23 1
#include <signal.h>
a26 1
#include <termios.h>
d29 1
a29 6
#include "compat.h"
#include <curses.h>
#include <db.h>
#include <regex.h>

#include "vi.h"
d34 2
d74 2
d78 1
a78 1
nonblank(sp, ep, lno, cnop)
a79 1
	EXF *ep;
d85 1
d91 3
a93 9
	/* Get the line. */
	if ((p = file_gline(sp, ep, lno, &len)) == NULL) {
		if (file_lline(sp, ep, &lno))
			return (1);
		if (lno == 0)
			return (0);
		GETLINE_ERR(sp, lno);
		return (1);
	}
d110 2
a124 33
 * set_alt_name --
 *	Set the alternate file name.
 *
 * Swap the alternate file name.  It's a routine because I wanted some place
 * to hang this comment.  The alternate file name (normally referenced using
 * the special character '#' during file expansion) is set by many
 * operations.  In the historic vi, the commands "ex", and "edit" obviously
 * set the alternate file name because they switched the underlying file.
 * Less obviously, the "read", "file", "write" and "wq" commands set it as
 * well.  In this implementation, some new commands have been added to the
 * list.  Where it gets interesting is that the alternate file name is set
 * multiple times by some commands.  If an edit attempt fails (for whatever
 * reason, like the current file is modified but as yet unwritten), it is
 * set to the file name that the user was unable to edit.  If the edit
 * succeeds, it is set to the last file name that was edited.  Good fun.
 *
 * If the user edits a temporary file, there are time when there isn't an
 * alternative file name.  A name argument of NULL turns it off.
 */
void
set_alt_name(sp, name)
	SCR *sp;
	char *name;
{
	if (sp->alt_name != NULL)
		free(sp->alt_name);
	if (name == NULL)
		sp->alt_name = NULL;
	else if ((sp->alt_name = strdup(name)) == NULL)
		msgq(sp, M_SYSERR, NULL);
}

/*
d127 2
d133 1
a133 1
	CHAR_T *str;
d147 59
a205 2
 * vi_putchar --
 *	Functional version of putchar, for tputs.
d208 23
a230 4
vi_putchar(ch)
	int ch;
{
	(void)putchar(ch);
d232 1
@


1.1.1.1
log
@new public version of nvi
@
text
@@


1.1.1.2
log
@new public version of nvi
@
text
@d35 1
a35 1
static char sccsid[] = "@@(#)util.c	8.74 (Berkeley) 8/17/94";
@


1.1.1.2.2.1
log
@file util.c was added on branch netbsd-1-0 on 1994-08-17 19:26:17 +0000
@
text
@d1 213
@


1.1.1.2.2.2
log
@new public version of nvi
@
text
@a0 213
/*-
 * Copyright (c) 1991, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
static char sccsid[] = "@@(#)util.c	8.74 (Berkeley) 8/17/94";
#endif /* not lint */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/time.h>

#include <bitstring.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#include "compat.h"
#include <curses.h>
#include <db.h>
#include <regex.h>

#include "vi.h"

/*
 * binc --
 *	Increase the size of a buffer.
 */
void *
binc(sp, bp, bsizep, min)
	SCR *sp;			/* sp MAY BE NULL!!! */
	void *bp;
	size_t *bsizep, min;
{
	size_t csize;

	/* If already larger than the minimum, just return. */
	if (min && *bsizep >= min)
		return (bp);

	csize = *bsizep + MAX(min, 256);
	REALLOC(sp, bp, void *, csize);

	if (bp == NULL) {
		/*
		 * Theoretically, realloc is supposed to leave any already
		 * held memory alone if it can't get more.  Don't trust it.
		 */
		*bsizep = 0;
		return (NULL);
	}
	/*
	 * Memory is guaranteed to be zero-filled, various parts of
	 * nvi depend on this.
	 */
	memset((char *)bp + *bsizep, 0, csize - *bsizep);
	*bsizep = csize;
	return (bp);
}

/*
 * nonblank --
 *	Set the column number of the first non-blank character
 *	including or after the starting column.  On error, set
 *	the column to 0, it's safest.
 */
int
nonblank(sp, ep, lno, cnop)
	SCR *sp;
	EXF *ep;
	recno_t lno;
	size_t *cnop;
{
	char *p;
	size_t cnt, len, off;

	/* Default. */
	off = *cnop;
	*cnop = 0;

	/* Get the line. */
	if ((p = file_gline(sp, ep, lno, &len)) == NULL) {
		if (file_lline(sp, ep, &lno))
			return (1);
		if (lno == 0)
			return (0);
		GETLINE_ERR(sp, lno);
		return (1);
	}

	/* Set the offset. */
	if (len == 0 || off >= len)
		return (0);

	for (cnt = off, p = &p[off],
	    len -= off; len && isblank(*p); ++cnt, ++p, --len);

	/* Set the return. */
	*cnop = len ? cnt : cnt - 1;
	return (0);
}

/*
 * tail --
 *	Return tail of a path.
 */
char *
tail(path)
	char *path;
{
	char *p;

	if ((p = strrchr(path, '/')) == NULL)
		return (path);
	return (p + 1);
}

/*
 * set_alt_name --
 *	Set the alternate file name.
 *
 * Swap the alternate file name.  It's a routine because I wanted some place
 * to hang this comment.  The alternate file name (normally referenced using
 * the special character '#' during file expansion) is set by many
 * operations.  In the historic vi, the commands "ex", and "edit" obviously
 * set the alternate file name because they switched the underlying file.
 * Less obviously, the "read", "file", "write" and "wq" commands set it as
 * well.  In this implementation, some new commands have been added to the
 * list.  Where it gets interesting is that the alternate file name is set
 * multiple times by some commands.  If an edit attempt fails (for whatever
 * reason, like the current file is modified but as yet unwritten), it is
 * set to the file name that the user was unable to edit.  If the edit
 * succeeds, it is set to the last file name that was edited.  Good fun.
 *
 * If the user edits a temporary file, there are time when there isn't an
 * alternative file name.  A name argument of NULL turns it off.
 */
void
set_alt_name(sp, name)
	SCR *sp;
	char *name;
{
	if (sp->alt_name != NULL)
		free(sp->alt_name);
	if (name == NULL)
		sp->alt_name = NULL;
	else if ((sp->alt_name = strdup(name)) == NULL)
		msgq(sp, M_SYSERR, NULL);
}

/*
 * v_strdup --
 *	Strdup for wide character strings with an associated length.
 */
CHAR_T *
v_strdup(sp, str, len)
	SCR *sp;
	CHAR_T *str;
	size_t len;
{
	CHAR_T *copy;

	MALLOC(sp, copy, CHAR_T *, len + 1);
	if (copy == NULL)
		return (NULL);
	memmove(copy, str, len * sizeof(CHAR_T));
	copy[len] = '\0';
	return (copy);
}

/*
 * vi_putchar --
 *	Functional version of putchar, for tputs.
 */
void
vi_putchar(ch)
	int ch;
{
	(void)putchar(ch);
}
@


1.1.1.3
log
@import of nvi 1.66
@
text
@a3 2
 * Copyright (c) 1991, 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
d5 27
a31 1
 * See the LICENSE file for redistribution information.
a33 2
#include "config.h"

d35 1
a35 1
static const char sccsid[] = "@@(#)util.c	10.10 (Berkeley) 3/6/96";
d40 1
a42 1
#include <errno.h>
d44 1
d48 1
d51 6
a56 1
#include "common.h"
a60 2
 *
 * PUBLIC: void *binc __P((SCR *, void *, size_t *, size_t));
a98 2
 *
 * PUBLIC: int nonblank __P((SCR *, recno_t, size_t *));
d101 1
a101 1
nonblank(sp, lno, cnop)
d103 1
a108 1
	int isempty;
d114 9
a122 3
	/* Get the line, succeeding in an empty file. */
	if (db_eget(sp, lno, &p, &len, &isempty))
		return (!isempty);
a138 2
 *
 * PUBLIC: char *tail __P((char *));
d152 33
a186 2
 *
 * PUBLIC: CHAR_T *v_strdup __P((SCR *, const CHAR_T *, size_t));
d191 1
a191 1
	const CHAR_T *str;
d205 2
a206 59
 * nget_uslong --
 *      Get an unsigned long, checking for overflow.
 *
 * PUBLIC: enum nresult nget_uslong __P((u_long *, const char *, char **, int));
 */
enum nresult
nget_uslong(valp, p, endp, base)
	u_long *valp;
	const char *p;
	char **endp;
	int base;
{
	errno = 0;
	*valp = strtoul(p, endp, base);
	if (errno == 0)
		return (NUM_OK);
	if (errno == ERANGE && *valp == ULONG_MAX)
		return (NUM_OVER);
	return (NUM_ERR);
}

/*
 * nget_slong --
 *      Convert a signed long, checking for overflow and underflow.
 *
 * PUBLIC: enum nresult nget_slong __P((long *, const char *, char **, int));
 */
enum nresult
nget_slong(valp, p, endp, base)
	long *valp;
	const char *p;
	char **endp;
	int base;
{
	errno = 0;
	*valp = strtol(p, endp, base);
	if (errno == 0)
		return (NUM_OK);
	if (errno == ERANGE) {
		if (*valp == LONG_MAX)
			return (NUM_OVER);
		if (*valp == LONG_MIN)
			return (NUM_UNDER);
	}
	return (NUM_ERR);
}

#ifdef DEBUG
#ifdef __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif

/*
 * TRACE --
 *	debugging trace routine.
 *
 * PUBLIC: void TRACE __P((SCR *, const char *, ...));
d209 4
a212 23
#ifdef __STDC__
TRACE(SCR *sp, const char *fmt, ...)
#else
TRACE(sp, fmt, va_alist)
	SCR *sp;
	char *fmt;
	va_dcl
#endif
{
	FILE *tfp;
	va_list ap;

	if ((tfp = sp->gp->tracefp) == NULL)
		return;
#ifdef __STDC__
	va_start(ap, fmt);
#else
	va_start(ap);
#endif
	(void)vfprintf(tfp, fmt, ap);
	va_end(ap);

	(void)fflush(tfp);
a213 1
#endif
@


1.1.1.4
log
@import of nvi 1.79
@
text
@d13 1
a13 1
static const char sccsid[] = "@@(#)util.c	10.11 (Berkeley) 9/15/96";
d139 1
a139 1
	memcpy(copy, str, len * sizeof(CHAR_T));
@

