head	1.13;
access;
symbols
	perseant-exfatfs-base-20250801:1.13
	perseant-exfatfs-base-20240630:1.13
	perseant-exfatfs:1.13.0.36
	perseant-exfatfs-base:1.13
	cjep_sun2x:1.13.0.34
	cjep_sun2x-base:1.13
	cjep_staticlib_x-base1:1.13
	cjep_staticlib_x:1.13.0.32
	cjep_staticlib_x-base:1.13
	phil-wifi-20200421:1.13
	phil-wifi-20200411:1.13
	phil-wifi-20200406:1.13
	pgoyette-compat-merge-20190127:1.13
	pgoyette-compat-20190127:1.13
	pgoyette-compat-20190118:1.13
	pgoyette-compat-1226:1.13
	pgoyette-compat-1126:1.13
	pgoyette-compat-1020:1.13
	pgoyette-compat-0930:1.13
	pgoyette-compat-0906:1.13
	pgoyette-compat-0728:1.13
	pgoyette-compat-0625:1.13
	pgoyette-compat-0521:1.13
	pgoyette-compat-0502:1.13
	pgoyette-compat-0422:1.13
	pgoyette-compat-0415:1.13
	pgoyette-compat-0407:1.13
	pgoyette-compat-0330:1.13
	pgoyette-compat-0322:1.13
	pgoyette-compat-0315:1.13
	pgoyette-compat:1.13.0.30
	pgoyette-compat-base:1.13
	perseant-stdc-iso10646:1.13.0.28
	perseant-stdc-iso10646-base:1.13
	prg-localcount2-base3:1.13
	prg-localcount2-base2:1.13
	prg-localcount2-base1:1.13
	prg-localcount2:1.13.0.26
	prg-localcount2-base:1.13
	pgoyette-localcount-20170426:1.13
	bouyer-socketcan-base1:1.13
	pgoyette-localcount-20170320:1.13
	bouyer-socketcan:1.13.0.24
	bouyer-socketcan-base:1.13
	pgoyette-localcount-20170107:1.13
	pgoyette-localcount-20161104:1.13
	localcount-20160914:1.13
	pgoyette-localcount-20160806:1.13
	pgoyette-localcount-20160726:1.13
	pgoyette-localcount:1.13.0.22
	pgoyette-localcount-base:1.13
	netbsd-5-2-3-RELEASE:1.13
	netbsd-5-1-5-RELEASE:1.13
	yamt-pagecache-base9:1.13
	yamt-pagecache-tag8:1.13
	tls-earlyentropy:1.13.0.18
	tls-earlyentropy-base:1.13
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.13
	riastradh-drm2-base3:1.13
	netbsd-5-2-2-RELEASE:1.13
	netbsd-5-1-4-RELEASE:1.13
	netbsd-5-2-1-RELEASE:1.13
	netbsd-5-1-3-RELEASE:1.13
	agc-symver:1.13.0.20
	agc-symver-base:1.13
	tls-maxphys-base:1.13
	yamt-pagecache-base8:1.13
	netbsd-5-2:1.13.0.16
	yamt-pagecache-base7:1.13
	netbsd-5-2-RELEASE:1.13
	netbsd-5-2-RC1:1.13
	yamt-pagecache-base6:1.13
	yamt-pagecache-base5:1.13
	yamt-pagecache-base4:1.13
	netbsd-5-1-2-RELEASE:1.13
	netbsd-5-1-1-RELEASE:1.13
	yamt-pagecache-base3:1.13
	yamt-pagecache-base2:1.13
	yamt-pagecache:1.13.0.14
	yamt-pagecache-base:1.13
	bouyer-quota2-nbase:1.13
	bouyer-quota2:1.13.0.12
	bouyer-quota2-base:1.13
	matt-nb5-pq3:1.13.0.10
	matt-nb5-pq3-base:1.13
	netbsd-5-1:1.13.0.8
	netbsd-5-1-RELEASE:1.13
	netbsd-5-1-RC4:1.13
	netbsd-5-1-RC3:1.13
	netbsd-5-1-RC2:1.13
	netbsd-5-1-RC1:1.13
	netbsd-5-0-2-RELEASE:1.13
	netbsd-5-0-1-RELEASE:1.13
	jym-xensuspend-nbase:1.13
	netbsd-5-0:1.13.0.6
	netbsd-5-0-RELEASE:1.13
	netbsd-5-0-RC4:1.13
	netbsd-5-0-RC3:1.13
	netbsd-5-0-RC2:1.13
	jym-xensuspend:1.13.0.4
	jym-xensuspend-base:1.13
	netbsd-5-0-RC1:1.13
	netbsd-5:1.13.0.2
	netbsd-5-base:1.13
	matt-mips64-base2:1.12
	matt-mips64:1.12.0.26
	mjf-devfs2:1.12.0.24
	mjf-devfs2-base:1.12
	netbsd-4-0-1-RELEASE:1.12
	wrstuden-revivesa-base-3:1.12
	wrstuden-revivesa-base-2:1.12
	wrstuden-fixsa-newbase:1.12
	wrstuden-revivesa-base-1:1.12
	yamt-pf42-base4:1.12
	yamt-pf42-base3:1.12
	hpcarm-cleanup-nbase:1.12
	yamt-pf42-baseX:1.12
	yamt-pf42-base2:1.12
	wrstuden-revivesa:1.12.0.22
	wrstuden-revivesa-base:1.12
	yamt-pf42:1.12.0.20
	yamt-pf42-base:1.12
	keiichi-mipv6-nbase:1.12
	keiichi-mipv6:1.12.0.18
	keiichi-mipv6-base:1.12
	matt-armv6-nbase:1.12
	matt-armv6-prevmlocking:1.12
	wrstuden-fixsa-base-1:1.12
	netbsd-4-0:1.12.0.16
	netbsd-4-0-RELEASE:1.12
	cube-autoconf:1.12.0.14
	cube-autoconf-base:1.12
	netbsd-4-0-RC5:1.12
	netbsd-4-0-RC4:1.12
	netbsd-4-0-RC3:1.12
	netbsd-4-0-RC2:1.12
	netbsd-4-0-RC1:1.12
	matt-armv6:1.12.0.12
	matt-armv6-base:1.12
	matt-mips64-base:1.12
	hpcarm-cleanup:1.12.0.10
	hpcarm-cleanup-base:1.12
	netbsd-3-1-1-RELEASE:1.11
	netbsd-3-0-3-RELEASE:1.11
	wrstuden-fixsa:1.12.0.8
	wrstuden-fixsa-base:1.12
	abandoned-netbsd-4-base:1.12
	abandoned-netbsd-4:1.12.0.4
	netbsd-3-1:1.11.0.6
	netbsd-3-1-RELEASE:1.11
	netbsd-3-0-2-RELEASE:1.11
	netbsd-3-1-RC4:1.11
	netbsd-3-1-RC3:1.11
	netbsd-3-1-RC2:1.11
	netbsd-3-1-RC1:1.11
	netbsd-4:1.12.0.6
	netbsd-4-base:1.12
	chap-midi-nbase:1.12
	netbsd-3-0-1-RELEASE:1.11
	chap-midi:1.12.0.2
	chap-midi-base:1.12
	netbsd-3-0:1.11.0.4
	netbsd-3-0-RELEASE:1.11
	netbsd-3-0-RC6:1.11
	netbsd-3-0-RC5:1.11
	netbsd-3-0-RC4:1.11
	netbsd-3-0-RC3:1.11
	netbsd-3-0-RC2:1.11
	netbsd-3-0-RC1:1.11
	netbsd-2-0-3-RELEASE:1.9.2.1
	netbsd-2-1:1.9.2.1.0.4
	netbsd-2-1-RELEASE:1.9.2.1
	netbsd-2-1-RC6:1.9.2.1
	netbsd-2-1-RC5:1.9.2.1
	netbsd-2-1-RC4:1.9.2.1
	netbsd-2-1-RC3:1.9.2.1
	netbsd-2-1-RC2:1.9.2.1
	netbsd-2-1-RC1:1.9.2.1
	netbsd-2-0-2-RELEASE:1.9.2.1
	netbsd-3:1.11.0.2
	netbsd-3-base:1.11
	netbsd-2-0-1-RELEASE:1.9.2.1
	netbsd-2:1.9.2.1.0.2
	netbsd-2-base:1.9.2.1
	netbsd-2-0-RELEASE:1.9.2.1
	netbsd-2-0-RC5:1.9.2.1
	netbsd-2-0-RC4:1.9.2.1
	netbsd-2-0-RC3:1.9.2.1
	netbsd-2-0-RC2:1.9.2.1
	netbsd-2-0-RC1:1.9.2.1
	netbsd-2-0:1.9.0.2
	netbsd-2-0-base:1.9
	netbsd-1-6-PATCH002-RELEASE:1.6
	netbsd-1-6-PATCH002:1.6
	netbsd-1-6-PATCH002-RC4:1.6
	netbsd-1-6-PATCH002-RC3:1.6
	netbsd-1-6-PATCH002-RC2:1.6
	netbsd-1-6-PATCH002-RC1:1.6
	netbsd-1-6-PATCH001:1.6
	netbsd-1-6-PATCH001-RELEASE:1.6
	netbsd-1-6-PATCH001-RC3:1.6
	netbsd-1-6-PATCH001-RC2:1.6
	netbsd-1-6-PATCH001-RC1:1.6
	fvdl_fs64_base:1.6
	netbsd-1-6-RELEASE:1.6
	netbsd-1-6-RC3:1.6
	netbsd-1-6-RC2:1.6
	netbsd-1-6-RC1:1.6
	netbsd-1-6:1.6.0.2
	netbsd-1-6-base:1.6
	netbsd-1-5-PATCH003:1.4
	netbsd-1-5-PATCH002:1.4
	netbsd-1-5-PATCH001:1.4
	nvi_1_79:1.1.1.4
	netbsd-1-5-RELEASE:1.4
	netbsd-1-5-BETA2:1.4
	netbsd-1-5-BETA:1.4
	netbsd-1-4-PATCH003:1.3
	netbsd-1-5-ALPHA2:1.4
	netbsd-1-5:1.4.0.2
	netbsd-1-5-base:1.4
	minoura-xpg4dl-base:1.3
	minoura-xpg4dl:1.3.0.8
	netbsd-1-4-PATCH002:1.3
	wrstuden-devbsize-19991221:1.3
	wrstuden-devbsize:1.3.0.6
	wrstuden-devbsize-base:1.3
	comdex-fall-1999:1.3.0.4
	comdex-fall-1999-base:1.3
	netbsd-1-4-PATCH001:1.3
	netbsd-1-4-RELEASE:1.3
	netbsd-1-4:1.3.0.2
	netbsd-1-4-base:1.3
	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.13
date	2008.10.29.16.49.35;	author christos;	state dead;
branches;
next	1.12;

1.12
date	2005.06.02.04.25.16;	author lukem;	state Exp;
branches;
next	1.11;

1.11
date	2004.11.05.19.50.12;	author dsl;	state Exp;
branches;
next	1.10;

1.10
date	2004.04.05.06.59.15;	author aymeric;	state Exp;
branches;
next	1.9;

1.9
date	2003.08.28.01.55.17;	author mrg;	state Exp;
branches
	1.9.2.1;
next	1.8;

1.8
date	2003.08.27.15.15.16;	author dsl;	state Exp;
branches;
next	1.7;

1.7
date	2003.08.27.13.47.51;	author dsl;	state Exp;
branches;
next	1.6;

1.6
date	2002.04.09.01.47.31;	author thorpej;	state Exp;
branches;
next	1.5;

1.5
date	2001.03.31.11.37.46;	author aymeric;	state Exp;
branches;
next	1.4;

1.4
date	2000.06.19.20.09.37;	author aymeric;	state Exp;
branches;
next	1.3;

1.3
date	99.01.08.06.16.54;	author abs;	state Exp;
branches
	1.3.8.1;
next	1.2;

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

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

1.9.2.1
date	2004.04.08.21.19.46;	author jdc;	state Exp;
branches;
next	;

1.3.8.1
date	2000.06.23.16.40.09;	author minoura;	state Exp;
branches;
next	;

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

1.1.1.2
date	94.08.17.19.25.30;	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.05.06;	author mrg;	state Exp;
branches;
next	1.1.1.4;

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

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

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


desc
@@


1.13
log
@bye old vi!
@
text
@/*	$NetBSD: options.c,v 1.12 2005/06/02 04:25:16 lukem 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[] = "@@(#)options.c	10.51 (Berkeley) 10/14/96";
#else
__RCSID("$NetBSD: options.c,v 1.12 2005/06/02 04:25:16 lukem Exp $");
#endif
#endif /* not lint */

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

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

#include "common.h"
#include "../vi/vi.h"
#include "pathnames.h"

static int	 	 opts_abbcmp __P((const void *, const void *));
static int	 	 opts_cmp __P((const void *, const void *));
static int	 	 opts_print __P((SCR *, OPTLIST const *));

/*
 * O'Reilly noted options and abbreviations are from "Learning the VI Editor",
 * Fifth Edition, May 1992.  There's no way of knowing what systems they are
 * actually from.
 *
 * HPUX noted options and abbreviations are from "The Ultimate Guide to the
 * VI and EX Text Editors", 1990.
 *
 * This list must be sorted...
 */
OPTLIST const optlist[] = {
/* O_ALTWERASE	  4.4BSD */
	{"altwerase",	f_altwerase,	OPT_0BOOL,	0},
/* O_AUTOINDENT	    4BSD */
	{"autoindent",	NULL,		OPT_0BOOL,	0},
/* O_AUTOPRINT	    4BSD */
	{"autoprint",	NULL,		OPT_1BOOL,	0},
/* O_AUTOWRITE	    4BSD */
	{"autowrite",	NULL,		OPT_0BOOL,	0},
/* O_BACKUP	  4.4BSD */
	{"backup",	NULL,		OPT_STR,	0},
/* O_BEAUTIFY	    4BSD */
	{"beautify",	NULL,		OPT_0BOOL,	0},
/* O_CDPATH	  4.4BSD */
	{"cdpath",	NULL,		OPT_STR,	0},
/* O_CEDIT	  4.4BSD */
	{"cedit",	NULL,		OPT_STR,	0},
/* O_COLUMNS	  4.4BSD */
	{"columns",	f_columns,	OPT_NUM,	OPT_NOSAVE},
/* O_COMMENT	  4.4BSD */
	{"comment",	NULL,		OPT_0BOOL,	0},
/* O_DIRECTORY	    4BSD */
	{"directory",	NULL,		OPT_STR,	0},
/* O_EDCOMPATIBLE   4BSD */
	{"edcompatible",NULL,		OPT_0BOOL,	0},
/* O_ERRORBELLS	    4BSD */
	{"errorbells",	NULL,		OPT_0BOOL,	0},
/* O_ESCAPETIME	  4.4BSD */
	{"escapetime",	NULL,		OPT_NUM,	0},
/* O_EXRC	System V (undocumented) */
	{"exrc",	NULL,		OPT_0BOOL,	0},
/* O_EXTENDED	  4.4BSD */
	{"extended",	f_recompile,	OPT_0BOOL,	0},
/* O_FILEC	  4.4BSD */
	{"filec",	NULL,		OPT_STR,	0},
/* O_FLASH	    HPUX */
	{"flash",	NULL,		OPT_1BOOL,	0},
#ifdef GTAGS
/* O_GTAGSMODE	    FreeBSD, NetBSD */
	{"gtagsmode",	NULL,		OPT_0BOOL,	0},
#endif
/* O_HARDTABS	    4BSD */
	{"hardtabs",	NULL,		OPT_NUM,	0},
/* O_ICLOWER	  4.4BSD */
	{"iclower",	f_recompile,	OPT_0BOOL,	0},
/* O_IGNORECASE	    4BSD */
	{"ignorecase",	f_recompile,	OPT_0BOOL,	0},
/* O_KEYTIME	  4.4BSD */
	{"keytime",	NULL,		OPT_NUM,	0},
/* O_LEFTRIGHT	  4.4BSD */
	{"leftright",	f_reformat,	OPT_0BOOL,	0},
/* O_LINES	  4.4BSD */
	{"lines",	f_lines,	OPT_NUM,	OPT_NOSAVE},
/* O_LISP	    4BSD
 *	XXX
 *	When the lisp option is implemented, delete the OPT_NOSAVE flag,
 *	so that :mkexrc dumps it.
 */
	{"lisp",	f_lisp,		OPT_0BOOL,	OPT_NOSAVE},
/* O_LIST	    4BSD */
	{"list",	f_reformat,	OPT_0BOOL,	0},
/* O_LOCKFILES	  4.4BSD
 *	XXX
 *	Locking isn't reliable enough over NFS to require it, in addition,
 *	it's a serious startup performance problem over some remote links.
 */
	{"lock",	NULL,		OPT_1BOOL,	0},
/* O_MAGIC	    4BSD */
	{"magic",	NULL,		OPT_1BOOL,	0},
/* O_MATCHCHARS	  netbsd 2.0 */
	{"matchchars",	NULL,		OPT_STR,	OPT_PAIRS},
/* O_MATCHTIME	  4.4BSD */
	{"matchtime",	NULL,		OPT_NUM,	0},
/* O_MESG	    4BSD */
	{"mesg",	NULL,		OPT_1BOOL,	0},
/* O_MODELINE	    4BSD
 *	!!!
 *	This has been documented in historical systems as both "modeline"
 *	and as "modelines".  Regardless of the name, this option represents
 *	a security problem of mammoth proportions, not to mention a stunning
 *	example of what your intro CS professor referred to as the perils of
 *	mixing code and data.  Don't add it, or I will kill you.
 */
	{"modeline",	NULL,		OPT_0BOOL,	OPT_NOSET},
/* O_MSGCAT	  4.4BSD */
	{"msgcat",	f_msgcat,	OPT_STR,	0},
/* O_NOPRINT	  4.4BSD */
	{"noprint",	f_print,	OPT_STR,	OPT_EARLYSET},
/* O_NUMBER	    4BSD */
	{"number",	f_reformat,	OPT_0BOOL,	0},
/* O_OCTAL	  4.4BSD */
	{"octal",	f_print,	OPT_0BOOL,	OPT_EARLYSET},
/* O_OPEN	    4BSD */
	{"open",	NULL,		OPT_1BOOL,	0},
/* O_OPTIMIZE	    4BSD */
	{"optimize",	NULL,		OPT_1BOOL,	0},
/* O_PARAGRAPHS	    4BSD */
	{"paragraphs",	NULL,		OPT_STR,	OPT_PAIRS},
/* O_PATH	  4.4BSD */
	{"path",	NULL,		OPT_STR,	0},
/* O_PRINT	  4.4BSD */
	{"print",	f_print,	OPT_STR,	OPT_EARLYSET},
/* O_PROMPT	    4BSD */
	{"prompt",	NULL,		OPT_1BOOL,	0},
/* O_READONLY	    4BSD (undocumented) */
	{"readonly",	f_readonly,	OPT_0BOOL,	OPT_ALWAYS},
/* O_RECDIR	  4.4BSD */
	{"recdir",	NULL,		OPT_STR,	0},
/* O_REDRAW	    4BSD */
	{"redraw",	NULL,		OPT_0BOOL,	0},
/* O_REMAP	    4BSD */
	{"remap",	NULL,		OPT_1BOOL,	0},
/* O_REPORT	    4BSD */
	{"report",	NULL,		OPT_NUM,	0},
/* O_RULER	  4.4BSD */
	{"ruler",	NULL,		OPT_0BOOL,	0},
/* O_SCROLL	    4BSD */
	{"scroll",	NULL,		OPT_NUM,	0},
/* O_SEARCHINCR	  4.4BSD */
	{"searchincr",	NULL,		OPT_0BOOL,	0},
/* O_SECTIONS	    4BSD */
	{"sections",	NULL,		OPT_STR,	OPT_PAIRS},
/* O_SECURE	  4.4BSD */
	{"secure",	NULL,		OPT_0BOOL,	OPT_NOUNSET},
/* O_SHELL	    4BSD */
	{"shell",	NULL,		OPT_STR,	0},
/* O_SHELLMETA	  4.4BSD */
	{"shellmeta",	NULL,		OPT_STR,	0},
/* O_SHIFTWIDTH	    4BSD */
	{"shiftwidth",	NULL,		OPT_NUM,	OPT_NOZERO},
/* O_SHOWMATCH	    4BSD */
	{"showmatch",	NULL,		OPT_0BOOL,	0},
/* O_SHOWMODE	  4.4BSD */
	{"showmode",	NULL,		OPT_0BOOL,	0},
/* O_SIDESCROLL	  4.4BSD */
	{"sidescroll",	NULL,		OPT_NUM,	OPT_NOZERO},
/* O_SLOWOPEN	    4BSD  */
	{"slowopen",	NULL,		OPT_0BOOL,	0},
/* O_SOURCEANY	    4BSD (undocumented)
 *	!!!
 *	Historic vi, on startup, source'd $HOME/.exrc and ./.exrc, if they
 *	were owned by the user.  The sourceany option was an undocumented
 *	feature of historic vi which permitted the startup source'ing of
 *	.exrc files the user didn't own.  This is an obvious security problem,
 *	and we ignore the option.
 */
	{"sourceany",	NULL,		OPT_0BOOL,	OPT_NOSET},
/* O_TABSTOP	    4BSD */
	{"tabstop",	f_reformat,	OPT_NUM,	OPT_NOZERO},
/* O_TAGLENGTH	    4BSD */
	{"taglength",	NULL,		OPT_NUM,	0},
/* O_TAGS	    4BSD */
	{"tags",	NULL,		OPT_STR,	0},
/* O_TERM	    4BSD
 *	!!!
 *	By default, the historic vi always displayed information about two
 *	options, redraw and term.  Term seems sufficient.
 */
	{"term",	NULL,		OPT_STR,	OPT_ADISP|OPT_NOSAVE},
/* O_TERSE	    4BSD */
	{"terse",	NULL,		OPT_0BOOL,	0},
/* O_TILDEOP      4.4BSD */
	{"tildeop",	NULL,		OPT_0BOOL,	0},
/* O_TIMEOUT	    4BSD (undocumented) */
	{"timeout",	NULL,		OPT_1BOOL,	0},
/* O_TTYWERASE	  4.4BSD */
	{"ttywerase",	f_ttywerase,	OPT_0BOOL,	0},
/* O_VERBOSE	  4.4BSD */
	{"verbose",	NULL,		OPT_0BOOL,	0},
/* O_W1200	    4BSD */
	{"w1200",	f_w1200,	OPT_NUM,	OPT_NDISP|OPT_NOSAVE},
/* O_W300	    4BSD */
	{"w300",	f_w300,		OPT_NUM,	OPT_NDISP|OPT_NOSAVE},
/* O_W9600	    4BSD */
	{"w9600",	f_w9600,	OPT_NUM,	OPT_NDISP|OPT_NOSAVE},
/* O_WARN	    4BSD */
	{"warn",	NULL,		OPT_1BOOL,	0},
/* O_WINDOW	    4BSD */
	{"window",	f_window,	OPT_NUM,	0},
/* O_WINDOWNAME	    4BSD */
	{"windowname",	NULL,		OPT_0BOOL,	0},
/* O_WRAPLEN	  4.4BSD */
	{"wraplen",	NULL,		OPT_NUM,	0},
/* O_WRAPMARGIN	    4BSD */
	{"wrapmargin",	NULL,		OPT_NUM,	0},
/* O_WRAPSCAN	    4BSD */
	{"wrapscan",	NULL,		OPT_1BOOL,	0},
/* O_WRITEANY	    4BSD */
	{"writeany",	NULL,		OPT_0BOOL,	0},
	{NULL},
};

typedef struct abbrev {
        char *name;
        int offset;
} OABBREV;

static OABBREV const abbrev[] = {
	{"ai",		O_AUTOINDENT},		/*     4BSD */
	{"ap",		O_AUTOPRINT},		/*     4BSD */
	{"aw",		O_AUTOWRITE},		/*     4BSD */
	{"bf",		O_BEAUTIFY},		/*     4BSD */
	{"co",		O_COLUMNS},		/*   4.4BSD */
	{"dir",		O_DIRECTORY},		/*     4BSD */
	{"eb",		O_ERRORBELLS},		/*     4BSD */
	{"ed",		O_EDCOMPATIBLE},	/*     4BSD */
	{"ex",		O_EXRC},		/* System V (undocumented) */
#ifdef GTAGS
	{"gt",		O_GTAGSMODE},		/* FreeBSD, NetBSD */
#endif
	{"ht",		O_HARDTABS},		/*     4BSD */
	{"ic",		O_IGNORECASE},		/*     4BSD */
	{"li",		O_LINES},		/*   4.4BSD */
	{"modelines",	O_MODELINE},		/*     HPUX */
	{"nu",		O_NUMBER},		/*     4BSD */
	{"opt",		O_OPTIMIZE},		/*     4BSD */
	{"para",	O_PARAGRAPHS},		/*     4BSD */
	{"re",		O_REDRAW},		/* O'Reilly */
	{"ro",		O_READONLY},		/*     4BSD (undocumented) */
	{"scr",		O_SCROLL},		/*     4BSD (undocumented) */
	{"sect",	O_SECTIONS},		/* O'Reilly */
	{"sh",		O_SHELL},		/*     4BSD */
	{"slow",	O_SLOWOPEN},		/*     4BSD */
	{"sm",		O_SHOWMATCH},		/*     4BSD */
	{"smd",		O_SHOWMODE},		/*     4BSD */
	{"sw",		O_SHIFTWIDTH},		/*     4BSD */
	{"tag",		O_TAGS},		/*     4BSD (undocumented) */
	{"tl",		O_TAGLENGTH},		/*     4BSD */
	{"to",		O_TIMEOUT},		/*     4BSD (undocumented) */
	{"ts",		O_TABSTOP},		/*     4BSD */
	{"tty",		O_TERM},		/*     4BSD (undocumented) */
	{"ttytype",	O_TERM},		/*     4BSD (undocumented) */
	{"w",		O_WINDOW},		/* O'Reilly */
	{"wa",		O_WRITEANY},		/*     4BSD */
	{"wi",		O_WINDOW},		/*     4BSD (undocumented) */
	{"wl",		O_WRAPLEN},		/*   4.4BSD */
	{"wm",		O_WRAPMARGIN},		/*     4BSD */
	{"ws",		O_WRAPSCAN},		/*     4BSD */
	{NULL},
};

/*
 * opts_init --
 *	Initialize some of the options.
 *
 * PUBLIC: int opts_init __P((SCR *, int *));
 */
int
opts_init(sp, oargs)
	SCR *sp;
	int *oargs;
{
	ARGS *argv[2], a, b;
	OPTLIST const *op;
	u_long v;
	int cnt, optindx;
	char *s, b1[1024];

	if (sizeof optlist / sizeof optlist[0] - 1 != O_OPTIONCOUNT) {
		fprintf(stderr, "vi: option table size error (%d != %d)\n",
		    (int)(sizeof optlist / sizeof optlist[0] - 1),
		    O_OPTIONCOUNT);
		exit(1);
	}

	a.bp = b1;
	b.bp = NULL;
	a.len = b.len = 0;
	argv[0] = &a;
	argv[1] = &b;
	optindx = 0;	/* XXXGCC -Wuninitialized */

	/* Set numeric and string default values. */
#define	OI(indx, str) {							\
	if (str != b1)		/* GCC puts strings in text-space. */	\
		(void)strcpy(b1, str);					\
	a.len = strlen(b1);						\
	if (opts_set(sp, argv, NULL)) {					\
		 optindx = indx;					\
		goto err;						\
	}								\
}
	/*
	 * Indirect global options to global space.  Specifically, set up
	 * terminal, lines, columns first, they're used by other options.
	 * Note, don't set the flags until we've set up the indirection.
	 */
	if (o_set(sp, O_TERM, 0, NULL, GO_TERM))
		goto err;
	F_SET(&sp->opts[O_TERM], OPT_GLOBAL);
	if (o_set(sp, O_LINES, 0, NULL, GO_LINES))
		goto err;
	F_SET(&sp->opts[O_LINES], OPT_GLOBAL);
	if (o_set(sp, O_COLUMNS, 0, NULL, GO_COLUMNS))
		goto err;
	F_SET(&sp->opts[O_COLUMNS], OPT_GLOBAL);
	if (o_set(sp, O_SECURE, 0, NULL, GO_SECURE))
		goto err;
	F_SET(&sp->opts[O_SECURE], OPT_GLOBAL);

	/* Initialize string values. */
	(void)snprintf(b1, sizeof(b1),
	    "cdpath=%s", (s = getenv("CDPATH")) == NULL ? ":" : s);
	OI(O_CDPATH, b1);

	/*
	 * !!!
	 * Vi historically stored temporary files in /var/tmp.  We store them
	 * in /tmp by default, hoping it's a memory based file system.  There
	 * are two ways to change this -- the user can set either the directory
	 * option or the TMPDIR environmental variable.
	 */
	(void)snprintf(b1, sizeof(b1),
	    "directory=%s", (s = getenv("TMPDIR")) == NULL ? _PATH_TMP : s);
	OI(O_DIRECTORY, b1);
	OI(O_ESCAPETIME, "escapetime=1");
	OI(O_KEYTIME, "keytime=6");
	OI(O_MATCHTIME, "matchtime=7");
	(void)snprintf(b1, sizeof(b1), "msgcat=%s", _PATH_MSGCAT);
	OI(O_MSGCAT, b1);
	OI(O_REPORT, "report=5");
	OI(O_PARAGRAPHS, "paragraphs=IPLPPPQPP LIpplpipbp");
	(void)snprintf(b1, sizeof(b1), "path=%s", "");
	OI(O_PATH, b1);
	(void)snprintf(b1, sizeof(b1), "recdir=%s", _PATH_PRESERVE);
	OI(O_RECDIR, b1);
	OI(O_SECTIONS, "sections=NHSHH HUnhsh");
	(void)snprintf(b1, sizeof(b1),
	    "shell=%s", (s = getenv("SHELL")) == NULL ? _PATH_BSHELL : s);
	OI(O_SHELL, b1);
	OI(O_SHELLMETA, "shellmeta=~{[*?$`'\"\\");
	OI(O_SHIFTWIDTH, "shiftwidth=8");
	OI(O_SIDESCROLL, "sidescroll=16");
	OI(O_TABSTOP, "tabstop=8");
	(void)snprintf(b1, sizeof(b1), "tags=%s", _PATH_TAGS);
	OI(O_TAGS, b1);
	OI(O_MATCHCHARS, "matchchars=()[]{}<>");

	/*
	 * XXX
	 * Initialize O_SCROLL here, after term; initializing term should
	 * have created a LINES/COLUMNS value.
	 */
	if ((v = (O_VAL(sp, O_LINES) - 1) / 2) == 0)
		v = 1;
	(void)snprintf(b1, sizeof(b1), "scroll=%ld", v);
	OI(O_SCROLL, b1);

	/*
	 * The default window option values are:
	 *		8 if baud rate <=  600
	 *	       16 if baud rate <= 1200
	 *	LINES - 1 if baud rate  > 1200
	 *
	 * Note, the windows option code will correct any too-large value
	 * or when the O_LINES value is 1.
	 */
	if (sp->gp->scr_baud(sp, &v))
		return (1);
	if (v <= 600)
		v = 8;
	else if (v <= 1200)
		v = 16;
	else
		v = O_VAL(sp, O_LINES) - 1;
	(void)snprintf(b1, sizeof(b1), "window=%lu", v);
	OI(O_WINDOW, b1);

	/*
	 * Set boolean default values, and copy all settings into the default
	 * information.  OS_NOFREE is set, we're copying, not replacing.
	 */
	for (op = optlist, cnt = 0; op->name != NULL; ++op, ++cnt)
		switch (op->type) {
		case OPT_0BOOL:
			break;
		case OPT_1BOOL:
			O_SET(sp, cnt);
			O_D_SET(sp, cnt);
			break;
		case OPT_NUM:
			o_set(sp, cnt, OS_DEF, NULL, O_VAL(sp, cnt));
			break;
		case OPT_STR:
			if (O_STR(sp, cnt) != NULL && o_set(sp, cnt,
			    OS_DEF | OS_NOFREE | OS_STRDUP, O_STR(sp, cnt), 0))
				goto err;
			break;
		default:
			abort();
		}

	/*
	 * !!!
	 * Some options can be initialized by the command name or the
	 * command-line arguments.  They don't set the default values,
	 * it's historic practice.
	 */
	for (; *oargs != -1; ++oargs)
		OI(*oargs, optlist[*oargs].name);
	return (0);
#undef OI

err:	msgq(sp, M_ERR,
	    "031|Unable to set default %s option", optlist[optindx].name);
	return (1);
}

/*
 * opts_set --
 *	Change the values of one or more options.
 *
 * PUBLIC: int opts_set __P((SCR *, ARGS *[], char *));
 */
int
opts_set(sp, argv, usage)
	SCR *sp;
	ARGS *argv[];
	char *usage;
{
	enum optdisp disp;
	enum nresult nret;
	OPTLIST const *op;
	OPTION *spo;
	u_long value, turnoff;
	int ch, equals, nf, nf2, offset, qmark, rval;
	char *endp, *name, *p, *sep, *t;

	disp = NO_DISPLAY;
	for (rval = 0; argv[0]->len != 0; ++argv) {
		/*
		 * The historic vi dumped the options for each occurrence of
		 * "all" in the set list.  Puhleeze.
		 */
		if (!strcmp(argv[0]->bp, "all")) {
			disp = ALL_DISPLAY;
			continue;
		}

		/* Find equals sign or question mark. */
		for (sep = NULL, equals = qmark = 0,
		    p = name = argv[0]->bp; (ch = *p) != '\0'; ++p)
			if (ch == '=' || ch == '?') {
				if (p == name) {
					if (usage != NULL)
						msgq(sp, M_ERR,
						    "032|Usage: %s", usage);
					return (1);
				}
				sep = p;
				if (ch == '=')
					equals = 1;
				else
					qmark = 1;
				break;
			}

		turnoff = 0;
		op = NULL;
		if (sep != NULL)
			*sep++ = '\0';

		/* Search for the name, then name without any leading "no". */
		if ((op = opts_search(name)) == NULL &&
		    name[0] == 'n' && name[1] == 'o') {
			turnoff = 1;
			name += 2;
			op = opts_search(name);
		}
		if (op == NULL) {
			opts_nomatch(sp, name);
			rval = 1;
			continue;
		}

		/* Find current option values. */
		offset = op - optlist;
		spo = sp->opts + offset;

		/*
		 * !!!
		 * Historically, the question mark could be a separate
		 * argument.
		 */
		if (!equals && !qmark &&
		    argv[1]->len == 1 && argv[1]->bp[0] == '?') {
			++argv;
			qmark = 1;
		}

		/* Set name, value. */
		switch (op->type) {
		case OPT_0BOOL:
		case OPT_1BOOL:
			/* Some options may not be reset. */
			if (F_ISSET(op, OPT_NOUNSET) && turnoff) {
				msgq_str(sp, M_ERR, name,
			    "291|set: the %s option may not be turned off");
				rval = 1;
				break;
			}

			/* Some options may not be set. */
			if (F_ISSET(op, OPT_NOSET) && !turnoff) {
				msgq_str(sp, M_ERR, name,
			    "313|set: the %s option may never be turned on");
				rval = 1;
				break;
			}

			if (equals) {
				msgq_str(sp, M_ERR, name,
			    "034|set: [no]%s option doesn't take a value");
				rval = 1;
				break;
			}
			if (qmark) {
				if (!disp)
					disp = SELECT_DISPLAY;
				F_SET(spo, OPT_SELECTED);
				break;
			}

			/*
			 * Do nothing if the value is unchanged, the underlying
			 * functions can be expensive.
			 */
			if (!F_ISSET(op, OPT_ALWAYS))
				if (turnoff) {
					if (!O_ISSET(sp, offset))
						break;
				} else {
					if (O_ISSET(sp, offset))
						break;
				}

			if (F_ISSET(op, OPT_EARLYSET)) {
			    /* Set the value. */
			    if (turnoff)
				O_CLR(sp, offset);
			    else
				O_SET(sp, offset);
			}

			/* Report to subsystems. */
			if (op->func != NULL &&
			    op->func(sp, spo, NULL, &turnoff) ||
			    ex_optchange(sp, offset, NULL, &turnoff) ||
			    v_optchange(sp, offset, NULL, &turnoff) ||
			    sp->gp->scr_optchange(sp, offset, NULL, &turnoff)) {
				rval = 1;
				break;
			}

			if (!F_ISSET(op, OPT_EARLYSET)) {
			    /* Set the value. */
			    if (turnoff)
				O_CLR(sp, offset);
			    else
				O_SET(sp, offset);
			}
			break;
		case OPT_NUM:
			if (turnoff) {
				msgq_str(sp, M_ERR, name,
				    "035|set: %s option isn't a boolean");
				rval = 1;
				break;
			}
			if (qmark || !equals) {
				if (!disp)
					disp = SELECT_DISPLAY;
				F_SET(spo, OPT_SELECTED);
				break;
			}

			if (!isdigit((unsigned char)sep[0]))
				goto badnum;
			if ((nret =
			    nget_uslong(&value, sep, &endp, 10)) != NUM_OK) {
				p = msg_print(sp, name, &nf);
				t = msg_print(sp, sep, &nf2);
				switch (nret) {
				case NUM_ERR:
					msgq(sp, M_SYSERR,
					    "036|set: %s option: %s", p, t);
					break;
				case NUM_OVER:
					msgq(sp, M_ERR,
			    "037|set: %s option: %s: value overflow", p, t);
					break;
				case NUM_OK:
				case NUM_UNDER:
					abort();
				}
				if (nf)
					FREE_SPACE(sp, p, 0);
				if (nf2)
					FREE_SPACE(sp, t, 0);
				rval = 1;
				break;
			}
			if (*endp && !isblank(*endp)) {
badnum:				p = msg_print(sp, name, &nf);
				t = msg_print(sp, sep, &nf2);
				msgq(sp, M_ERR,
		    "038|set: %s option: %s is an illegal number", p, t);
				if (nf)
					FREE_SPACE(sp, p, 0);
				if (nf2)
					FREE_SPACE(sp, t, 0);
				rval = 1;
				break;
			}

			/* Some options may never be set to zero. */
			if (F_ISSET(op, OPT_NOZERO) && value == 0) {
				msgq_str(sp, M_ERR, name,
			    "314|set: the %s option may never be set to 0");
				rval = 1;
				break;
			}

			/*
			 * Do nothing if the value is unchanged, the underlying
			 * functions can be expensive.
			 */
			if (!F_ISSET(op, OPT_ALWAYS) &&
			    O_VAL(sp, offset) == value)
				break;

			if (F_ISSET(op, OPT_EARLYSET)) {
			    /* Set the value. */
			    if (o_set(sp, offset, 0, NULL, value)) {
				rval = 1;
				break;
			    }
			}

			/* Report to subsystems. */
			if (op->func != NULL &&
			    op->func(sp, spo, sep, &value) ||
			    ex_optchange(sp, offset, sep, &value) ||
			    v_optchange(sp, offset, sep, &value) ||
			    sp->gp->scr_optchange(sp, offset, sep, &value)) {
				rval = 1;
				break;
			}

			if (!F_ISSET(op, OPT_EARLYSET)) {
			    /* Set the value. */
			    if (o_set(sp, offset, 0, NULL, value))
				rval = 1;
			}
			break;
		case OPT_STR:
			if (turnoff) {
				msgq_str(sp, M_ERR, name,
				    "039|set: %s option isn't a boolean");
				rval = 1;
				break;
			}
			if (qmark || !equals) {
				if (!disp)
					disp = SELECT_DISPLAY;
				F_SET(spo, OPT_SELECTED);
				break;
			}

			/* Check for strings that must have even length */
			if (F_ISSET(op, OPT_PAIRS) && strlen(sep) & 1) {
				msgq_str(sp, M_ERR, name,
				    "047|set: the %s option must be in two character groups");
				rval = 1;
				break;
			}

			/*
			 * Do nothing if the value is unchanged, the underlying
			 * functions can be expensive.
			 */
			if (!F_ISSET(op, OPT_ALWAYS) &&
			    O_STR(sp, offset) != NULL &&
			    !strcmp(O_STR(sp, offset), sep))
				break;

			if (F_ISSET(op, OPT_EARLYSET)) {
			    /* Set the value. */
			    if (o_set(sp, offset, OS_STRDUP, sep, 0)) {
				rval = 1;
				break;
			    }
			}

			/* Report to subsystems. */
			if (op->func != NULL &&
			    op->func(sp, spo, sep, NULL) ||
			    ex_optchange(sp, offset, sep, NULL) ||
			    v_optchange(sp, offset, sep, NULL) ||
			    sp->gp->scr_optchange(sp, offset, sep, NULL)) {
				rval = 1;
				break;
			}

			if (!F_ISSET(op, OPT_EARLYSET)) {
			    /* Set the value. */
			    if (o_set(sp, offset, OS_STRDUP, sep, 0))
				rval = 1;
			}
			break;
		default:
			abort();
		}
	}
	if (disp != NO_DISPLAY)
		opts_dump(sp, disp);
	return (rval);
}

/*
 * o_set --
 *	Set an option's value.
 *
 * PUBLIC: int o_set __P((SCR *, int, u_int, char *, u_long));
 */
int
o_set(sp, opt, flags, str, val)
	SCR *sp;
	int opt;
	u_int flags;
	char *str;
	u_long val;
{
	OPTION *op;

	/* Set a pointer to the options area. */
	op = F_ISSET(&sp->opts[opt], OPT_GLOBAL) ?
	    &sp->gp->opts[sp->opts[opt].o_cur.val] : &sp->opts[opt];

	/* Copy the string, if requested. */
	if (LF_ISSET(OS_STRDUP) && (str = strdup(str)) == NULL) {
		msgq(sp, M_SYSERR, NULL);
		return (1);
	}

	/* Free the previous string, if requested, and set the value. */
	if LF_ISSET(OS_DEF)
		if (LF_ISSET(OS_STR | OS_STRDUP)) {
			if (!LF_ISSET(OS_NOFREE) && op->o_def.str != NULL)
				free(op->o_def.str);
			op->o_def.str = str;
		} else
			op->o_def.val = val;
	else
		if (LF_ISSET(OS_STR | OS_STRDUP)) {
			if (!LF_ISSET(OS_NOFREE) && op->o_cur.str != NULL)
				free(op->o_cur.str);
			op->o_cur.str = str;
		} else
			op->o_cur.val = val;
	return (0);
}

/*
 * opts_empty --
 *	Return 1 if the string option is invalid, 0 if it's OK.
 *
 * PUBLIC: int opts_empty __P((SCR *, int, int));
 */
int
opts_empty(sp, off, silent)
	SCR *sp;
	int off, silent;
{
	char *p;

	if ((p = O_STR(sp, off)) == NULL || p[0] == '\0') {
		if (!silent)
			msgq_str(sp, M_ERR, optlist[off].name,
			    "305|No %s edit option specified");
		return (1);
	}
	return (0);
}

/*
 * opts_dump --
 *	List the current values of selected options.
 *
 * PUBLIC: void opts_dump __P((SCR *, enum optdisp));
 */
void
opts_dump(sp, type)
	SCR *sp;
	enum optdisp type;
{
	OPTLIST const *op;
	int base, b_num, cnt, col, colwidth, curlen, s_num;
	int numcols, numrows, row;
	int b_op[O_OPTIONCOUNT], s_op[O_OPTIONCOUNT];
	char nbuf[20];

	/*
	 * Options are output in two groups -- those that fit in a column and
	 * those that don't.  Output is done on 6 character "tab" boundaries
	 * for no particular reason.  (Since we don't output tab characters,
	 * we can ignore the terminal's tab settings.)  Ignore the user's tab
	 * setting because we have no idea how reasonable it is.
	 *
	 * Find a column width we can live with, testing from 10 columns to 1.
	 */
	for (numcols = 10; numcols > 1; --numcols) {
		colwidth = sp->cols / numcols & ~(STANDARD_TAB - 1);
		if (colwidth >= 10) {
			colwidth =
			    (colwidth + STANDARD_TAB) & ~(STANDARD_TAB - 1);
			numcols = sp->cols / colwidth;
			break;
		}
		colwidth = 0;
	}

	/*
	 * Get the set of options to list, entering them into
	 * the column list or the overflow list.
	 */
	for (b_num = s_num = 0, op = optlist; op->name != NULL; ++op) {
		cnt = op - optlist;

		/* If OPT_NDISP set, it's never displayed. */
		if (F_ISSET(op, OPT_NDISP))
			continue;

		switch (type) {
		case ALL_DISPLAY:		/* Display all. */
			break;
		case CHANGED_DISPLAY:		/* Display changed. */
			/* If OPT_ADISP set, it's always "changed". */
			if (F_ISSET(op, OPT_ADISP))
				break;
			switch (op->type) {
			case OPT_0BOOL:
			case OPT_1BOOL:
			case OPT_NUM:
				if (O_VAL(sp, cnt) == O_D_VAL(sp, cnt))
					continue;
				break;
			case OPT_STR:
				if (O_STR(sp, cnt) == O_D_STR(sp, cnt) ||
				    O_D_STR(sp, cnt) != NULL &&
				    !strcmp(O_STR(sp, cnt), O_D_STR(sp, cnt)))
					continue;
				break;
			}
			break;
		case SELECT_DISPLAY:		/* Display selected. */
			if (!F_ISSET(&sp->opts[cnt], OPT_SELECTED))
				continue;
			break;
		default:
		case NO_DISPLAY:
			abort();
		}
		F_CLR(&sp->opts[cnt], OPT_SELECTED);

		curlen = strlen(op->name);
		switch (op->type) {
		case OPT_0BOOL:
		case OPT_1BOOL:
			if (!O_ISSET(sp, cnt))
				curlen += 2;
			break;
		case OPT_NUM:
			(void)snprintf(nbuf,
			    sizeof(nbuf), "%ld", O_VAL(sp, cnt));
			curlen += strlen(nbuf);
			break;
		case OPT_STR:
			if (O_STR(sp, cnt) != NULL)
				curlen += strlen(O_STR(sp, cnt));
			curlen += 3;
			break;
		}
		/* Offset by 2 so there's a gap. */
		if (curlen <= colwidth - 2)
			s_op[s_num++] = cnt;
		else
			b_op[b_num++] = cnt;
	}

	if (s_num > 0) {
		/* Figure out the number of rows. */
		if (s_num > numcols) {
			numrows = s_num / numcols;
			if (s_num % numcols)
				++numrows;
		} else
			numrows = 1;

		/* Display the options in sorted order. */
		for (row = 0; row < numrows;) {
			for (base = row, col = 0; col < numcols; ++col) {
				cnt = opts_print(sp, &optlist[s_op[base]]);
				if ((base += numrows) >= s_num)
					break;
				(void)ex_printf(sp, "%*s",
				    (int)(colwidth - cnt), "");
			}
			if (++row < numrows || b_num)
				(void)ex_puts(sp, "\n");
		}
	}

	for (row = 0; row < b_num;) {
		(void)opts_print(sp, &optlist[b_op[row]]);
		if (++row < b_num)
			(void)ex_puts(sp, "\n");
	}
	(void)ex_puts(sp, "\n");
}

/*
 * opts_print --
 *	Print out an option.
 */
static int
opts_print(sp, op)
	SCR *sp;
	OPTLIST const *op;
{
	int curlen, offset;

	curlen = 0;
	offset = op - optlist;
	switch (op->type) {
	case OPT_0BOOL:
	case OPT_1BOOL:
		curlen += ex_printf(sp,
		    "%s%s", O_ISSET(sp, offset) ? "" : "no", op->name);
		break;
	case OPT_NUM:
		curlen += ex_printf(sp, "%s=%ld", op->name, O_VAL(sp, offset));
		break;
	case OPT_STR:
		curlen += ex_printf(sp, "%s=\"%s\"", op->name,
		    O_STR(sp, offset) == NULL ? "" : O_STR(sp, offset));
		break;
	}
	return (curlen);
}

/*
 * opts_save --
 *	Write the current configuration to a file.
 *
 * PUBLIC: int opts_save __P((SCR *, FILE *));
 */
int
opts_save(sp, fp)
	SCR *sp;
	FILE *fp;
{
	OPTLIST const *op;
	int ch, cnt;
	char *p;

	for (op = optlist; op->name != NULL; ++op) {
		if (F_ISSET(op, OPT_NOSAVE))
			continue;
		cnt = op - optlist;
		switch (op->type) {
		case OPT_0BOOL:
		case OPT_1BOOL:
			if (O_ISSET(sp, cnt))
				(void)fprintf(fp, "set %s\n", op->name);
			else
				(void)fprintf(fp, "set no%s\n", op->name);
			break;
		case OPT_NUM:
			(void)fprintf(fp,
			    "set %s=%-3ld\n", op->name, O_VAL(sp, cnt));
			break;
		case OPT_STR:
			if (O_STR(sp, cnt) == NULL)
				break;
			(void)fprintf(fp, "set ");
			for (p = op->name; (ch = *p) != '\0'; ++p) {
				if (isblank(ch) || ch == '\\')
					(void)putc('\\', fp);
				(void)putc(ch, fp);
			}
			(void)putc('=', fp);
			for (p = O_STR(sp, cnt); (ch = *p) != '\0'; ++p) {
				if (isblank(ch) || ch == '\\')
					(void)putc('\\', fp);
				(void)putc(ch, fp);
			}
			(void)putc('\n', fp);
			break;
		}
		if (ferror(fp)) {
			msgq(sp, M_SYSERR, NULL);
			return (1);
		}
	}
	return (0);
}

/* 
 * opts_search --
 *	Search for an option.
 *
 * PUBLIC: OPTLIST const *opts_search __P((char *));
 */
OPTLIST const *
opts_search(name)
	char *name;
{
	OPTLIST const *op, *found;
	OABBREV atmp, *ap;
	OPTLIST otmp;
	size_t len;

	/* Check list of abbreviations. */
	atmp.name = name;
	if ((ap = bsearch(&atmp, abbrev, sizeof(abbrev) / sizeof(OABBREV) - 1,
	    sizeof(OABBREV), opts_abbcmp)) != NULL)
		return (optlist + ap->offset);

	/* Check list of options. */
	otmp.name = name;
	if ((op = bsearch(&otmp, optlist, sizeof(optlist) / sizeof(OPTLIST) - 1,
	    sizeof(OPTLIST), opts_cmp)) != NULL)
		return (op);
		
	/*
	 * Check to see if the name is the prefix of one (and only one)
	 * option.  If so, return the option.
	 */
	len = strlen(name);
	for (found = NULL, op = optlist; op->name != NULL; ++op) {
		if (op->name[0] < name[0])
			continue;
		if (op->name[0] > name[0])
			break;
		if (!memcmp(op->name, name, len)) {
			if (found != NULL)
				return (NULL);
			found = op;
		}
	}
	return (found);
}

/* 
 * opts_nomatch --
 *	Standard nomatch error message for options.
 *
 * PUBLIC: void opts_nomatch __P((SCR *, char *));
 */
void
opts_nomatch(sp, name)
	SCR *sp;
	char *name;
{
	msgq_str(sp, M_ERR, name,
	    "033|set: no %s option: 'set all' gives all option values");
}

static int
opts_abbcmp(a, b)
        const void *a, *b;
{
        return(strcmp(((OABBREV *)a)->name, ((OABBREV *)b)->name));
}

static int
opts_cmp(a, b)
        const void *a, *b;
{
        return(strcmp(((OPTLIST *)a)->name, ((OPTLIST *)b)->name));
}

/*
 * opts_copy --
 *	Copy a screen's OPTION array.
 *
 * PUBLIC: int opts_copy __P((SCR *, SCR *));
 */
int
opts_copy(orig, sp)
	SCR *orig, *sp;
{
	int cnt, rval;

	/* Copy most everything without change. */
	memcpy(sp->opts, orig->opts, sizeof(orig->opts));

	/* Copy the string edit options. */
	for (cnt = rval = 0; cnt < O_OPTIONCOUNT; ++cnt) {
		if (optlist[cnt].type != OPT_STR ||
		    F_ISSET(&sp->opts[cnt], OPT_GLOBAL))
			continue;
		/*
		 * If never set, or already failed, NULL out the entries --
		 * have to continue after failure, otherwise would have two
		 * screens referencing the same memory.
		 */
		if (rval || O_STR(sp, cnt) == NULL) {
			o_set(sp, cnt, OS_NOFREE | OS_STR, NULL, 0);
			o_set(sp, cnt, OS_DEF | OS_NOFREE | OS_STR, NULL, 0);
			continue;
		}

		/* Copy the current string. */
		if (o_set(sp, cnt, OS_NOFREE | OS_STRDUP, O_STR(sp, cnt), 0)) {
			o_set(sp, cnt, OS_DEF | OS_NOFREE | OS_STR, NULL, 0);
			goto nomem;
		}

		/* Copy the default string. */
		if (O_D_STR(sp, cnt) != NULL && o_set(sp, cnt,
		    OS_DEF | OS_NOFREE | OS_STRDUP, O_D_STR(sp, cnt), 0)) {
nomem:			msgq(orig, M_SYSERR, NULL);
			rval = 1;
		}
	}
	return (rval);
}

/*
 * opts_free --
 *	Free all option strings
 *
 * PUBLIC: void opts_free __P((SCR *));
 */
void
opts_free(sp)
	SCR *sp;
{
	int cnt;

	for (cnt = 0; cnt < O_OPTIONCOUNT; ++cnt) {
		if (optlist[cnt].type != OPT_STR ||
		    F_ISSET(&sp->opts[cnt], OPT_GLOBAL))
			continue;
		if (O_STR(sp, cnt) != NULL)
			free(O_STR(sp, cnt));
		if (O_D_STR(sp, cnt) != NULL)
			free(O_D_STR(sp, cnt));
	}
}
@


1.12
log
@appease gcc -Wuninitialized.
Each is marked with XXXGCC, because in some cases it looks like gcc
isn't correctly detecting that
	for ( ; xxx ; yyy)
		foo = 0;
always sets foo ...
@
text
@d1 1
a1 1
/*	$NetBSD: options.c,v 1.11 2004/11/05 19:50:12 dsl Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD: options.c,v 1.11 2004/11/05 19:50:12 dsl Exp $");
@


1.11
log
@Add (unsigned char) cast to ctype functions
@
text
@d1 1
a1 1
/*	$NetBSD: options.c,v 1.10 2004/04/05 06:59:15 aymeric Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD: options.c,v 1.10 2004/04/05 06:59:15 aymeric Exp $");
d325 1
@


1.10
log
@fix copying and freeing of options, patch from yamt@@ in PR bin/25050. Thanks!
@
text
@d1 1
a1 1
/*	$NetBSD: options.c,v 1.9 2003/08/28 01:55:17 mrg Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD: options.c,v 1.9 2003/08/28 01:55:17 mrg Exp $");
d630 1
a630 1
			if (!isdigit(sep[0]))
@


1.9
log
@can't print size_t with %d
@
text
@d1 1
a1 1
/*	$NetBSD: options.c,v 1.8 2003/08/27 15:15:16 dsl Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD: options.c,v 1.8 2003/08/27 15:15:16 dsl Exp $");
d1155 1
a1155 1
		    F_ISSET(&optlist[cnt], OPT_GLOBAL))
d1198 1
a1198 1
		    F_ISSET(&optlist[cnt], OPT_GLOBAL))
@


1.9.2.1
log
@Pull up revision 1.10 (requested by aymeric in ticket #99)

fix copying and freeing of options, patch from yamt@@ in PR bin/25050.
Thanks!
@
text
@d1 1
a1 1
/*	$NetBSD: options.c,v 1.9 2003/08/28 01:55:17 mrg Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD: options.c,v 1.9 2003/08/28 01:55:17 mrg Exp $");
d1155 1
a1155 1
		    F_ISSET(&sp->opts[cnt], OPT_GLOBAL))
d1198 1
a1198 1
		    F_ISSET(&sp->opts[cnt], OPT_GLOBAL))
@


1.8
log
@Increase maximum terminal size from 500x500 to 4000x4000.
Add a 'matchchars' option to set the character pairs that % looks for.
Traditionally this was []{}() but someone added <> to nvi - probably
for editing html, but it is a pain for C. :set matchars=[]{}() will
restore the traditional behaviour.
If the 'open' and 'close' characters are the same (eg "" or '') then % will
alternate forwards and backwards searches.
Use common code to detect options that must have an even number of characters.
@
text
@d1 1
a1 1
/*	$NetBSD: options.c,v 1.6 2002/04/09 01:47:31 thorpej Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD: options.c,v 1.6 2002/04/09 01:47:31 thorpej Exp $");
d315 2
a316 1
		    sizeof optlist / sizeof optlist[0] - 1, O_OPTIONCOUNT);
@


1.7
log
@Coorectly sort options table (it is searched with a binary chop).
@
text
@d124 2
d152 1
a152 1
	{"paragraphs",	f_paragraph,	OPT_STR,	0},
d176 1
a176 1
	{"sections",	f_section,	OPT_STR,	0},
d313 6
d389 1
d719 8
@


1.6
log
@Use __RCSID() and __COPYRIGHT().
@
text
@d1 1
a1 1
/*	$NetBSD: options.c,v 1.5 2001/03/31 11:37:46 aymeric Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD$");
d52 2
d80 2
a83 2
/* O_ERRORBELLS	    4BSD */
	{"errorbells",	NULL,		OPT_0BOOL,	0},
@


1.5
log
@merge changes after import of nvi 1.79
@
text
@d1 1
a1 1
/*	$NetBSD: options.c,v 1.4 2000/06/19 20:09:37 aymeric Exp $	*/
d14 1
d16 1
d18 3
@


1.4
log
@Correct behavior of options octal, print and noprint
This fixes PR #6800
@
text
@d1 1
a1 1
/*	$NetBSD: options.c,v 1.3 1999/01/08 06:16:54 abs Exp $	*/
d15 1
a15 1
static const char sccsid[] = "@@(#)options.c	10.43 (Berkeley) 5/16/96";
d86 2
a87 2
/* O_GTAGSMODE      FreeBSD, NetBSD */
	{"gtagsmode",   NULL,           OPT_0BOOL,      0},
d144 2
d225 2
d270 1
d360 2
d406 2
a407 2
	 * Set boolean default values, and copy all settings into the
	 * the default information.
d421 2
a422 2
			if (O_STR(sp, cnt) != NULL && o_set(sp,
			    cnt, OS_DEF | OS_STRDUP, O_STR(sp, cnt), 0))
a505 1

d507 1
a507 2
			msgq_str(sp, M_ERR, name,
		    "033|set: no %s option: 'set all' gives all option values");
d717 1
a717 2
			    if (o_set(sp, offset,
					OS_FREE | OS_STRDUP, sep, 0)) {
d735 1
a735 1
			    if (o_set(sp, offset, OS_FREE | OS_STRDUP, sep, 0))
a773 1

d775 4
a778 4
	if LF_ISSET(OS_DEF) {
		if (LF_ISSET(OS_FREE) && op->o_def.str != NULL)
			free(op->o_def.str);
		if (LF_ISSET(OS_STR | OS_STRDUP))
d780 1
a780 1
		else
d782 4
a785 4
	} else {
		if (LF_ISSET(OS_FREE) && op->o_cur.str != NULL)
			free(op->o_cur.str);
		if (LF_ISSET(OS_STR | OS_STRDUP))
d787 1
a787 1
		else
a788 1
	}
d1083 15
d1125 1
a1125 1
	memmove(sp->opts, orig->opts, sizeof(orig->opts));
d1138 2
a1139 2
			o_set(sp, cnt, OS_STR, NULL, 0);
			o_set(sp, cnt, OS_DEF | OS_STR, NULL, 0);
d1144 2
a1145 2
		if (o_set(sp, cnt, OS_STRDUP, O_STR(sp, cnt), 0)) {
			o_set(sp, cnt, OS_DEF | OS_STR, NULL, 0);
d1150 2
a1151 2
		if (O_D_STR(sp, cnt) != NULL &&
		    o_set(sp, cnt, OS_DEF | OS_STRDUP, O_D_STR(sp, cnt), 0)) {
@


1.3
log
@Add Gtags support from shigio@@wafu.netgate.net (PR 5978). Gtags are produced
by global (http://wafu.netgate.net/tama/unix/global.html)
@
text
@d1 1
a1 1
/*	$NetBSD: options.c,v 1.2 1998/01/09 08:06:58 perry Exp $	*/
d133 1
a133 1
	{"noprint",	f_print,	OPT_STR,	0},
d137 1
a137 1
	{"octal",	f_print,	OPT_0BOOL,	0},
d145 1
a145 1
	{"print",	f_print,	OPT_STR,	0},
d568 8
d586 3
a588 2
			/* Set the value. */
			if (turnoff)
d590 1
a590 1
			else
d592 1
d663 8
d681 3
a683 2
			/* Set the value. */
			if (o_set(sp, offset, 0, NULL, value))
d685 1
d710 9
d729 3
a731 2
			/* Set the value. */
			if (o_set(sp, offset, OS_FREE | OS_STRDUP, sep, 0))
d733 1
@


1.3.8.1
log
@Sync w/ netbsd-1-5-base.
@
text
@d1 1
a1 1
/*	$NetBSD: options.c,v 1.4 2000/06/19 20:09:37 aymeric Exp $	*/
d133 1
a133 1
	{"noprint",	f_print,	OPT_STR,	OPT_EARLYSET},
d137 1
a137 1
	{"octal",	f_print,	OPT_0BOOL,	OPT_EARLYSET},
d145 1
a145 1
	{"print",	f_print,	OPT_STR,	OPT_EARLYSET},
a567 8
			if (F_ISSET(op, OPT_EARLYSET)) {
			    /* Set the value. */
			    if (turnoff)
				O_CLR(sp, offset);
			    else
				O_SET(sp, offset);
			}

d578 2
a579 3
			if (!F_ISSET(op, OPT_EARLYSET)) {
			    /* Set the value. */
			    if (turnoff)
d581 1
a581 1
			    else
a582 1
			}
a652 8
			if (F_ISSET(op, OPT_EARLYSET)) {
			    /* Set the value. */
			    if (o_set(sp, offset, 0, NULL, value)) {
				rval = 1;
				break;
			    }
			}

d663 2
a664 3
			if (!F_ISSET(op, OPT_EARLYSET)) {
			    /* Set the value. */
			    if (o_set(sp, offset, 0, NULL, value))
a665 1
			}
a689 9
			if (F_ISSET(op, OPT_EARLYSET)) {
			    /* Set the value. */
			    if (o_set(sp, offset,
					OS_FREE | OS_STRDUP, sep, 0)) {
				rval = 1;
				break;
			    }
			}

d700 2
a701 3
			if (!F_ISSET(op, OPT_EARLYSET)) {
			    /* Set the value. */
			    if (o_set(sp, offset, OS_FREE | OS_STRDUP, sep, 0))
a702 1
			}
@


1.2
log
@RCS Id Police.
@
text
@d1 1
a1 1
/*	$NetBSD$	*/
d85 4
d249 3
@


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[] = "@@(#)options.c	8.65 (Berkeley) 8/17/94";
a26 1
#include <signal.h>
a29 1
#include <termios.h>
d32 3
a34 7
#include "compat.h"
#include <db.h>
#include <regex.h>
#include <pathnames.h>

#include "vi.h"
#include "excmd.h"
a37 1
static OPTLIST const	*opts_prefix __P((char *));
d48 1
a48 1
static OPTLIST const optlist[] = {
d57 2
d62 3
a64 1
	{"cdpath",	f_cdpath,	OPT_STR,	0},
a68 2
/* O_DIGRAPH	  XXX: Elvis */
	{"digraph",	NULL,		OPT_0BOOL,	0},
d73 2
d80 3
a82 1
	{"extended",	NULL,		OPT_0BOOL,	0},
d87 2
d90 1
a90 1
	{"ignorecase",	NULL,		OPT_0BOOL,	0},
d94 1
a94 1
	{"leftright",	f_leftright,	OPT_0BOOL,	0},
d97 4
a100 5
/* O_LISP	    4BSD */
/*
 * XXX
 * When the lisp option is implemented, delete
 * the OPT_NOSAVE flag, so that :mkexrc dumps it.
d104 7
a110 1
	{"list",	f_list,		OPT_0BOOL,	0},
d116 14
a129 5
	{"mesg",	f_mesg,		OPT_1BOOL,	0},
/* O_META	  4.4BSD */
	{"meta",	NULL,		OPT_STR,	0},
/* O_MODELINE	    4BSD */
	{"modeline",	f_modeline,	OPT_0BOOL,	0},
d131 1
a131 1
	{"number",	f_number,	OPT_0BOOL,	0},
d133 1
a133 1
	{"octal",	f_octal,	OPT_0BOOL,	0},
d140 2
d145 1
a145 1
	{"readonly",	f_readonly,	OPT_0BOOL,	0},
d153 1
a153 1
	{"report",	NULL,		OPT_NUM,	OPT_NOSTR},
d158 2
d162 2
d166 2
d169 1
a169 3
	{"shiftwidth",	f_shiftwidth,	OPT_NUM,	0},
/* O_SHOWDIRTY	  4.4BSD */
	{"showdirty",	NULL,		OPT_0BOOL,	0},
d175 1
a175 1
	{"sidescroll",	NULL,		OPT_NUM,	0},
d178 9
a186 2
/* O_SOURCEANY	    4BSD (undocumented) */
	{"sourceany",	f_sourceany,	OPT_0BOOL,	0},
d188 1
a188 1
	{"tabstop",	f_tabstop,	OPT_NUM,	0},
d190 1
a190 1
	{"taglength",	NULL,		OPT_NUM,	OPT_NOSTR},
d192 7
a198 3
	{"tags",	f_tags,		OPT_STR,	0},
/* O_TERM	    4BSD */
	{"term",	f_term,		OPT_STR,	OPT_NOSAVE},
d210 1
a210 1
	{"w1200",	f_w1200,	OPT_NUM,	OPT_NEVER|OPT_NOSAVE},
d212 1
a212 1
	{"w300",	f_w300,		OPT_NUM,	OPT_NEVER|OPT_NOSAVE},
d214 1
a214 1
	{"w9600",	f_w9600,	OPT_NUM,	OPT_NEVER|OPT_NOSAVE},
d219 2
d222 1
a222 1
	{"wrapmargin",	NULL,		OPT_NUM,	OPT_NOSTR},
d269 1
d277 3
a279 2
 *	Initialize some of the options.  Since the user isn't really
 *	"setting" these variables, don't set their OPT_SET bits.
d282 1
a282 1
opts_init(sp)
d284 1
d289 1
a289 1
	int cnt;
a292 1
	a.len = 0;
d294 1
a294 1
	b.len = 0;
d298 2
a299 1
#define	SET_DEF(opt, str) {						\
d303 3
a305 4
	if (opts_set(sp, NULL, argv)) {					\
		msgq(sp, M_ERR,						\
		    "Unable to set default %s option", optlist[opt]);	\
		return (1);						\
a306 1
	F_CLR(&sp->opts[opt], OPT_SET);					\
d308 22
a329 10
	/* Set default values. */
	for (op = optlist, cnt = 0; op->name != NULL; ++op, ++cnt)
		if (op->type == OPT_0BOOL)
			O_CLR(sp, cnt);
		else if (op->type == OPT_1BOOL)
			O_SET(sp, cnt);

	(void)snprintf(b1, sizeof(b1), "cdpath=%s",
	    (s = getenv("CDPATH")) == NULL ? ":" : s);
	SET_DEF(O_CDPATH, b1);
d338 10
a347 8
	(void)snprintf(b1, sizeof(b1), "directory=%s",
	    (s = getenv("TMPDIR")) == NULL ? _PATH_TMP : s);
	SET_DEF(O_DIRECTORY, b1);
	SET_DEF(O_KEYTIME, "keytime=6");
	SET_DEF(O_MATCHTIME, "matchtime=7");
	SET_DEF(O_META, "meta=~{[*?$`'\"\\");
	SET_DEF(O_REPORT, "report=5");
	SET_DEF(O_PARAGRAPHS, "paragraphs=IPLPPPQPP LIpplpipbp");
d349 9
a357 8
	SET_DEF(O_RECDIR, b1);
	SET_DEF(O_SECTIONS, "sections=NHSHH HUnhsh");
	(void)snprintf(b1, sizeof(b1), "shell=%s",
	    (s = getenv("SHELL")) == NULL ? _PATH_BSHELL : s);
	SET_DEF(O_SHELL, b1);
	SET_DEF(O_SHIFTWIDTH, "shiftwidth=8");
	SET_DEF(O_SIDESCROLL, "sidescroll=16");
	SET_DEF(O_TABSTOP, "tabstop=8");
d359 1
a359 4
	SET_DEF(O_TAGS, b1);
	(void)snprintf(b1, sizeof(b1), "term=%s",
	    (s = getenv("TERM")) == NULL ? "unknown" : s);
	SET_DEF(O_TERM, b1);
d363 2
a364 3
	 * Initialize ^D, ^U scrolling value here, after TERM.  (We didn't
	 * have the options information when the screen was initialized.)
	 * Initializing term should have created a LINES/COLUMNS value.
d366 4
a369 3
	sp->defscroll = O_VAL(sp, O_LINES) / 2;
	(void)snprintf(b1, sizeof(b1), "scroll=%ld", sp->defscroll);
	SET_DEF(O_SCROLL, b1);
d376 3
d380 2
a381 1
	v = baud_from_bval(sp);
d389 1
a389 1
	SET_DEF(O_WINDOW, b1);
d391 23
a413 1
	SET_DEF(O_WRAPMARGIN, "wrapmargin=0");
d416 4
a419 2
	 * By default, the historic vi always displayed information
	 * about two options, redraw and term.  Term seems sufficient.
d421 2
a422 1
	F_SET(&sp->opts[O_TERM], OPT_SET);
d424 5
d434 2
d438 1
a438 1
opts_set(sp, usage, argv)
d440 1
a441 1
	ARGS *argv[];
d444 1
a444 1
	OABBREV atmp, *ap;
a445 1
	OPTLIST otmp;
d448 2
a449 2
	int ch, equals, offset, qmark, rval;
	char *endp, *name, *p, *sep; 
d468 2
a469 2
						msgq(sp,
						    M_ERR, "Usage: %s", usage);
d485 3
a487 18
		/* Check list of abbreviations. */
		atmp.name = name;
		if ((ap = bsearch(&atmp, abbrev,
		    sizeof(abbrev) / sizeof(OABBREV) - 1,
		    sizeof(OABBREV), opts_abbcmp)) != NULL) {
			op = optlist + ap->offset;
			goto found;
		}

		/* Check list of options. */
		otmp.name = name;
		if ((op = bsearch(&otmp, optlist,
		    sizeof(optlist) / sizeof(OPTLIST) - 1,
		    sizeof(OPTLIST), opts_cmp)) != NULL)
			goto found;

		/* Try the name without any leading "no". */
		if (name[0] == 'n' && name[1] == 'o') {
d490 2
a491 2
		} else
			goto prefix;
d493 4
a496 23
		/* Check list of abbreviations. */
		atmp.name = name;
		if ((ap = bsearch(&atmp, abbrev,
		    sizeof(abbrev) / sizeof(OABBREV) - 1,
		    sizeof(OABBREV), opts_abbcmp)) != NULL) {
			op = optlist + ap->offset;
			goto found;
		}

		/* Check list of options. */
		otmp.name = name;
		if ((op = bsearch(&otmp, optlist,
		    sizeof(optlist) / sizeof(OPTLIST) - 1,
		    sizeof(OPTLIST), opts_cmp)) != NULL)
			goto found;

		/* Check for prefix match. */
prefix:		op = opts_prefix(name);

found:		if (op == NULL) {
			msgq(sp, M_ERR,
			    "no %s option: 'set all' gives all option values",
			    name);
d519 16
d536 3
a538 3
				msgq(sp, M_ERR,
				    "set: [no]%s option doesn't take a value",
				    name);
d547 12
a558 4
			if (op->func != NULL) {
				if (op->func(sp, spo, NULL, turnoff)) {
					rval = 1;
					break;
d560 13
a572 1
			} else if (turnoff)
d576 1
a576 1
			goto change;
a577 7
			/*
			 * !!!
			 * Extension to historic vi.  If the OPT_NOSTR flag is
			 * set, a numeric option may be turned off by using a
			 * "no" prefix, e.g. "nowrapmargin".  (We assume that
			 * setting the value to 0 turns a numeric option off.)
			 */
d579 3
a581 6
				if (F_ISSET(op, OPT_NOSTR)) {
					value = 0;
					goto nostr;
				}
				msgq(sp, M_ERR,
				    "set: %s option isn't a boolean", name);
d590 27
a616 1
			value = strtol(sep, &endp, 10);
d618 2
d621 14
a634 1
				    "set %s: illegal number %s", name, sep);
d637 23
a659 8
nostr:			if (op->func != NULL) {
				if (op->func(sp, spo, sep, value)) {
					rval = 1;
					break;
				}
			} else
				O_VAL(sp, offset) = value;
			goto change;
d662 3
a664 2
				msgq(sp, M_ERR,
				    "set: %s option isn't a boolean", name);
d673 18
a690 14
			if (op->func != NULL) {
				if (op->func(sp, spo, sep, (u_long)0)) {
					rval = 1;
					break;
				}
			} else {
				if (F_ISSET(&sp->opts[offset], OPT_ALLOCATED))
					free(O_STR(sp, offset));
				if ((O_STR(sp, offset) = strdup(sep)) == NULL) {
					msgq(sp, M_SYSERR, NULL);
					rval = 1;
					break;
				} else
					F_SET(&sp->opts[offset], OPT_ALLOCATED);
d692 4
a695 3
change:			if (sp->s_optchange != NULL)
				(void)sp->s_optchange(sp, offset);
			F_SET(&sp->opts[offset], OPT_SET);
d707 68
d777 2
a791 12
	 * XXX
	 * It's possible to get here by putting "set option" in the
	 * .exrc file.  I can't think of a clean way to layer this,
	 * or a reasonable check to make, so we block it here.
	 */
	if (sp->stdfp == NULL) {
		msgq(sp, M_ERR,
		    "Option display requires that the screen be initialized");
		return;
	}

	/*
d798 1
a798 1
	 * Find a column width we can live with.
d800 2
a801 2
	for (cnt = 6; cnt > 1; --cnt) {
		colwidth = (sp->cols - 1) / cnt & ~(STANDARD_TAB - 1);
d805 1
d818 2
a819 2
		/* If OPT_NEVER set, it's never displayed. */
		if (F_ISSET(op, OPT_NEVER))
d826 17
a842 2
			if (!F_ISSET(&sp->opts[cnt], OPT_SET))
				continue;
a850 1
			/* NOTREACHED */
d867 3
a869 1
			curlen += strlen(O_STR(sp, cnt)) + 3;
d872 2
a873 2
		/* Offset by two so there's a gap. */
		if (curlen < colwidth - 2)
d880 1
a880 2
		/* Figure out the number of columns. */
		numcols = (sp->cols - 1) / colwidth;
d894 2
a895 2
				(void)ex_printf(EXCOOKIE,
				    "%*s", (int)(colwidth - cnt), "");
d898 1
a898 1
				(void)ex_printf(EXCOOKIE, "\n");
d905 1
a905 1
			(void)ex_printf(EXCOOKIE, "\n");
d907 1
a907 1
	(void)ex_printf(EXCOOKIE, "\n");
d926 1
a926 1
		curlen += ex_printf(EXCOOKIE,
d930 1
a930 2
		curlen += ex_printf(EXCOOKIE,
		     "%s=%ld", op->name, O_VAL(sp, offset));
d933 2
a934 2
		curlen += ex_printf(EXCOOKIE,
		    "%s=\"%s\"", op->name, O_STR(sp, offset));
d943 2
d969 1
a969 1
			    "set %s=%-3d\n", op->name, O_VAL(sp, cnt));
d972 2
d990 1
a990 1
			msgq(sp, M_ERR, "I/O error: %s", strerror(errno));
d997 5
a1001 4
/*
 * opts_prefix --
 *	Check to see if the name is the prefix of one (and only one)
 *	option.  If so, return the option.
d1003 2
a1004 2
static OPTLIST const *
opts_prefix(name)
d1007 3
a1009 1
	OPTLIST const *op, *save_op;
d1012 16
a1027 1
	save_op = NULL;
d1029 1
a1029 1
	for (op = optlist; op->name != NULL; ++op) {
d1035 1
a1035 1
			if (save_op != NULL)
d1037 1
a1037 1
			save_op = op;
d1040 1
a1040 1
	return (save_op);
a1057 18
 * opts_free --
 *	Free all option strings
 */
void
opts_free(sp)
	SCR *sp;
{
	int cnt;
	char *p;

	for (cnt = 0; cnt < O_OPTIONCOUNT; ++cnt)
		if (F_ISSET(&sp->opts[cnt], OPT_ALLOCATED)) {
			p = O_STR(sp, cnt);
			FREE(p, strlen(p) + 1);
		}
}

/*
d1060 2
d1067 1
a1067 2
	OPTION *op;
	int cnt;
d1072 27
a1098 10
	/*
	 * Allocate copies of the strings -- keep trying to reallocate
	 * after ENOMEM failure, otherwise end up with more than one
	 * screen referencing the original memory.
	 */
	for (op = sp->opts, cnt = 0; cnt < O_OPTIONCOUNT; ++cnt, ++op)
		if (F_ISSET(&sp->opts[cnt], OPT_ALLOCATED) &&
		    (O_STR(sp, cnt) = strdup(O_STR(sp, cnt))) == NULL) {
			msgq(orig, M_SYSERR, NULL);
			return (1);
d1100 25
a1124 1
	return (0);
@


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[] = "@@(#)options.c	8.66 (Berkeley) 8/17/94";
@


1.1.1.2.2.1
log
@file options.c was added on branch netbsd-1-0 on 1994-08-17 19:25:31 +0000
@
text
@d1 890
@


1.1.1.2.2.2
log
@new public version of nvi
@
text
@a0 890
/*-
 * 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[] = "@@(#)options.c	8.66 (Berkeley) 8/17/94";
#endif /* not lint */

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

#include <bitstring.h>
#include <ctype.h>
#include <errno.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 <db.h>
#include <regex.h>
#include <pathnames.h>

#include "vi.h"
#include "excmd.h"

static int	 	 opts_abbcmp __P((const void *, const void *));
static int	 	 opts_cmp __P((const void *, const void *));
static OPTLIST const	*opts_prefix __P((char *));
static int	 	 opts_print __P((SCR *, OPTLIST const *));

/*
 * O'Reilly noted options and abbreviations are from "Learning the VI Editor",
 * Fifth Edition, May 1992.  There's no way of knowing what systems they are
 * actually from.
 *
 * HPUX noted options and abbreviations are from "The Ultimate Guide to the
 * VI and EX Text Editors", 1990.
 */
static OPTLIST const optlist[] = {
/* O_ALTWERASE	  4.4BSD */
	{"altwerase",	f_altwerase,	OPT_0BOOL,	0},
/* O_AUTOINDENT	    4BSD */
	{"autoindent",	NULL,		OPT_0BOOL,	0},
/* O_AUTOPRINT	    4BSD */
	{"autoprint",	NULL,		OPT_1BOOL,	0},
/* O_AUTOWRITE	    4BSD */
	{"autowrite",	NULL,		OPT_0BOOL,	0},
/* O_BEAUTIFY	    4BSD */
	{"beautify",	NULL,		OPT_0BOOL,	0},
/* O_CDPATH	  4.4BSD */
	{"cdpath",	f_cdpath,	OPT_STR,	0},
/* O_COLUMNS	  4.4BSD */
	{"columns",	f_columns,	OPT_NUM,	OPT_NOSAVE},
/* O_COMMENT	  4.4BSD */
	{"comment",	NULL,		OPT_0BOOL,	0},
/* O_DIGRAPH	  XXX: Elvis */
	{"digraph",	NULL,		OPT_0BOOL,	0},
/* O_DIRECTORY	    4BSD */
	{"directory",	NULL,		OPT_STR,	0},
/* O_EDCOMPATIBLE   4BSD */
	{"edcompatible",NULL,		OPT_0BOOL,	0},
/* O_ERRORBELLS	    4BSD */
	{"errorbells",	NULL,		OPT_0BOOL,	0},
/* O_EXRC	System V (undocumented) */
	{"exrc",	NULL,		OPT_0BOOL,	0},
/* O_EXTENDED	  4.4BSD */
	{"extended",	NULL,		OPT_0BOOL,	0},
/* O_FLASH	    HPUX */
	{"flash",	NULL,		OPT_1BOOL,	0},
/* O_HARDTABS	    4BSD */
	{"hardtabs",	NULL,		OPT_NUM,	0},
/* O_IGNORECASE	    4BSD */
	{"ignorecase",	NULL,		OPT_0BOOL,	0},
/* O_KEYTIME	  4.4BSD */
	{"keytime",	NULL,		OPT_NUM,	0},
/* O_LEFTRIGHT	  4.4BSD */
	{"leftright",	f_leftright,	OPT_0BOOL,	0},
/* O_LINES	  4.4BSD */
	{"lines",	f_lines,	OPT_NUM,	OPT_NOSAVE},
/* O_LISP	    4BSD */
/*
 * XXX
 * When the lisp option is implemented, delete
 * the OPT_NOSAVE flag, so that :mkexrc dumps it.
 */
	{"lisp",	f_lisp,		OPT_0BOOL,	OPT_NOSAVE},
/* O_LIST	    4BSD */
	{"list",	f_list,		OPT_0BOOL,	0},
/* O_MAGIC	    4BSD */
	{"magic",	NULL,		OPT_1BOOL,	0},
/* O_MATCHTIME	  4.4BSD */
	{"matchtime",	NULL,		OPT_NUM,	0},
/* O_MESG	    4BSD */
	{"mesg",	f_mesg,		OPT_1BOOL,	0},
/* O_META	  4.4BSD */
	{"meta",	NULL,		OPT_STR,	0},
/* O_MODELINE	    4BSD */
	{"modeline",	f_modeline,	OPT_0BOOL,	0},
/* O_NUMBER	    4BSD */
	{"number",	f_number,	OPT_0BOOL,	0},
/* O_OCTAL	  4.4BSD */
	{"octal",	f_octal,	OPT_0BOOL,	0},
/* O_OPEN	    4BSD */
	{"open",	NULL,		OPT_1BOOL,	0},
/* O_OPTIMIZE	    4BSD */
	{"optimize",	NULL,		OPT_1BOOL,	0},
/* O_PARAGRAPHS	    4BSD */
	{"paragraphs",	f_paragraph,	OPT_STR,	0},
/* O_PROMPT	    4BSD */
	{"prompt",	NULL,		OPT_1BOOL,	0},
/* O_READONLY	    4BSD (undocumented) */
	{"readonly",	f_readonly,	OPT_0BOOL,	0},
/* O_RECDIR	  4.4BSD */
	{"recdir",	NULL,		OPT_STR,	0},
/* O_REDRAW	    4BSD */
	{"redraw",	NULL,		OPT_0BOOL,	0},
/* O_REMAP	    4BSD */
	{"remap",	NULL,		OPT_1BOOL,	0},
/* O_REPORT	    4BSD */
	{"report",	NULL,		OPT_NUM,	OPT_NOSTR},
/* O_RULER	  4.4BSD */
	{"ruler",	NULL,		OPT_0BOOL,	0},
/* O_SCROLL	    4BSD */
	{"scroll",	NULL,		OPT_NUM,	0},
/* O_SECTIONS	    4BSD */
	{"sections",	f_section,	OPT_STR,	0},
/* O_SHELL	    4BSD */
	{"shell",	NULL,		OPT_STR,	0},
/* O_SHIFTWIDTH	    4BSD */
	{"shiftwidth",	f_shiftwidth,	OPT_NUM,	0},
/* O_SHOWDIRTY	  4.4BSD */
	{"showdirty",	NULL,		OPT_0BOOL,	0},
/* O_SHOWMATCH	    4BSD */
	{"showmatch",	NULL,		OPT_0BOOL,	0},
/* O_SHOWMODE	  4.4BSD */
	{"showmode",	NULL,		OPT_0BOOL,	0},
/* O_SIDESCROLL	  4.4BSD */
	{"sidescroll",	NULL,		OPT_NUM,	0},
/* O_SLOWOPEN	    4BSD  */
	{"slowopen",	NULL,		OPT_0BOOL,	0},
/* O_SOURCEANY	    4BSD (undocumented) */
	{"sourceany",	f_sourceany,	OPT_0BOOL,	0},
/* O_TABSTOP	    4BSD */
	{"tabstop",	f_tabstop,	OPT_NUM,	0},
/* O_TAGLENGTH	    4BSD */
	{"taglength",	NULL,		OPT_NUM,	OPT_NOSTR},
/* O_TAGS	    4BSD */
	{"tags",	f_tags,		OPT_STR,	0},
/* O_TERM	    4BSD */
	{"term",	f_term,		OPT_STR,	OPT_NOSAVE},
/* O_TERSE	    4BSD */
	{"terse",	NULL,		OPT_0BOOL,	0},
/* O_TILDEOP      4.4BSD */
	{"tildeop",	NULL,		OPT_0BOOL,	0},
/* O_TIMEOUT	    4BSD (undocumented) */
	{"timeout",	NULL,		OPT_1BOOL,	0},
/* O_TTYWERASE	  4.4BSD */
	{"ttywerase",	f_ttywerase,	OPT_0BOOL,	0},
/* O_VERBOSE	  4.4BSD */
	{"verbose",	NULL,		OPT_0BOOL,	0},
/* O_W1200	    4BSD */
	{"w1200",	f_w1200,	OPT_NUM,	OPT_NEVER|OPT_NOSAVE},
/* O_W300	    4BSD */
	{"w300",	f_w300,		OPT_NUM,	OPT_NEVER|OPT_NOSAVE},
/* O_W9600	    4BSD */
	{"w9600",	f_w9600,	OPT_NUM,	OPT_NEVER|OPT_NOSAVE},
/* O_WARN	    4BSD */
	{"warn",	NULL,		OPT_1BOOL,	0},
/* O_WINDOW	    4BSD */
	{"window",	f_window,	OPT_NUM,	0},
/* O_WRAPMARGIN	    4BSD */
	{"wrapmargin",	NULL,		OPT_NUM,	OPT_NOSTR},
/* O_WRAPSCAN	    4BSD */
	{"wrapscan",	NULL,		OPT_1BOOL,	0},
/* O_WRITEANY	    4BSD */
	{"writeany",	NULL,		OPT_0BOOL,	0},
	{NULL},
};

typedef struct abbrev {
        char *name;
        int offset;
} OABBREV;

static OABBREV const abbrev[] = {
	{"ai",		O_AUTOINDENT},		/*     4BSD */
	{"ap",		O_AUTOPRINT},		/*     4BSD */
	{"aw",		O_AUTOWRITE},		/*     4BSD */
	{"bf",		O_BEAUTIFY},		/*     4BSD */
	{"co",		O_COLUMNS},		/*   4.4BSD */
	{"dir",		O_DIRECTORY},		/*     4BSD */
	{"eb",		O_ERRORBELLS},		/*     4BSD */
	{"ed",		O_EDCOMPATIBLE},	/*     4BSD */
	{"ex",		O_EXRC},		/* System V (undocumented) */
	{"ht",		O_HARDTABS},		/*     4BSD */
	{"ic",		O_IGNORECASE},		/*     4BSD */
	{"li",		O_LINES},		/*   4.4BSD */
	{"modelines",	O_MODELINE},		/*     HPUX */
	{"nu",		O_NUMBER},		/*     4BSD */
	{"opt",		O_OPTIMIZE},		/*     4BSD */
	{"para",	O_PARAGRAPHS},		/*     4BSD */
	{"re",		O_REDRAW},		/* O'Reilly */
	{"ro",		O_READONLY},		/*     4BSD (undocumented) */
	{"scr",		O_SCROLL},		/*     4BSD (undocumented) */
	{"sect",	O_SECTIONS},		/* O'Reilly */
	{"sh",		O_SHELL},		/*     4BSD */
	{"slow",	O_SLOWOPEN},		/*     4BSD */
	{"sm",		O_SHOWMATCH},		/*     4BSD */
	{"sw",		O_SHIFTWIDTH},		/*     4BSD */
	{"tag",		O_TAGS},		/*     4BSD (undocumented) */
	{"tl",		O_TAGLENGTH},		/*     4BSD */
	{"to",		O_TIMEOUT},		/*     4BSD (undocumented) */
	{"ts",		O_TABSTOP},		/*     4BSD */
	{"tty",		O_TERM},		/*     4BSD (undocumented) */
	{"ttytype",	O_TERM},		/*     4BSD (undocumented) */
	{"w",		O_WINDOW},		/* O'Reilly */
	{"wa",		O_WRITEANY},		/*     4BSD */
	{"wi",		O_WINDOW},		/*     4BSD (undocumented) */
	{"wm",		O_WRAPMARGIN},		/*     4BSD */
	{"ws",		O_WRAPSCAN},		/*     4BSD */
	{NULL},
};

/*
 * opts_init --
 *	Initialize some of the options.  Since the user isn't really
 *	"setting" these variables, don't set their OPT_SET bits.
 */
int
opts_init(sp)
	SCR *sp;
{
	ARGS *argv[2], a, b;
	OPTLIST const *op;
	u_long v;
	int cnt;
	char *s, b1[1024];

	a.bp = b1;
	a.len = 0;
	b.bp = NULL;
	b.len = 0;
	argv[0] = &a;
	argv[1] = &b;

#define	SET_DEF(opt, str) {						\
	if (str != b1)		/* GCC puts strings in text-space. */	\
		(void)strcpy(b1, str);					\
	a.len = strlen(b1);						\
	if (opts_set(sp, NULL, argv)) {					\
		msgq(sp, M_ERR,						\
		    "Unable to set default %s option", optlist[opt]);	\
		return (1);						\
	}								\
	F_CLR(&sp->opts[opt], OPT_SET);					\
}
	/* Set default values. */
	for (op = optlist, cnt = 0; op->name != NULL; ++op, ++cnt)
		if (op->type == OPT_0BOOL)
			O_CLR(sp, cnt);
		else if (op->type == OPT_1BOOL)
			O_SET(sp, cnt);

	(void)snprintf(b1, sizeof(b1), "cdpath=%s",
	    (s = getenv("CDPATH")) == NULL ? ":" : s);
	SET_DEF(O_CDPATH, b1);

	/*
	 * !!!
	 * Vi historically stored temporary files in /var/tmp.  We store them
	 * in /tmp by default, hoping it's a memory based file system.  There
	 * are two ways to change this -- the user can set either the directory
	 * option or the TMPDIR environmental variable.
	 */
	(void)snprintf(b1, sizeof(b1), "directory=%s",
	    (s = getenv("TMPDIR")) == NULL ? _PATH_TMP : s);
	SET_DEF(O_DIRECTORY, b1);
	SET_DEF(O_KEYTIME, "keytime=6");
	SET_DEF(O_MATCHTIME, "matchtime=7");
	SET_DEF(O_META, "meta=~{[*?$`'\"\\");
	SET_DEF(O_REPORT, "report=5");
	SET_DEF(O_PARAGRAPHS, "paragraphs=IPLPPPQPP LIpplpipbp");
	(void)snprintf(b1, sizeof(b1), "recdir=%s", _PATH_PRESERVE);
	SET_DEF(O_RECDIR, b1);
	SET_DEF(O_SECTIONS, "sections=NHSHH HUnhsh");
	(void)snprintf(b1, sizeof(b1), "shell=%s",
	    (s = getenv("SHELL")) == NULL ? _PATH_BSHELL : s);
	SET_DEF(O_SHELL, b1);
	SET_DEF(O_SHIFTWIDTH, "shiftwidth=8");
	SET_DEF(O_SIDESCROLL, "sidescroll=16");
	SET_DEF(O_TABSTOP, "tabstop=8");
	(void)snprintf(b1, sizeof(b1), "tags=%s", _PATH_TAGS);
	SET_DEF(O_TAGS, b1);
	(void)snprintf(b1, sizeof(b1), "term=%s",
	    (s = getenv("TERM")) == NULL ? "unknown" : s);
	SET_DEF(O_TERM, b1);

	/*
	 * XXX
	 * Initialize ^D, ^U scrolling value here, after TERM.  (We didn't
	 * have the options information when the screen was initialized.)
	 * Initializing term should have created a LINES/COLUMNS value.
	 */
	sp->defscroll = O_VAL(sp, O_LINES) / 2;
	(void)snprintf(b1, sizeof(b1), "scroll=%ld", sp->defscroll);
	SET_DEF(O_SCROLL, b1);

	/*
	 * The default window option values are:
	 *		8 if baud rate <=  600
	 *	       16 if baud rate <= 1200
	 *	LINES - 1 if baud rate  > 1200
	 */
	v = baud_from_bval(sp);
	if (v <= 600)
		v = 8;
	else if (v <= 1200)
		v = 16;
	else
		v = O_VAL(sp, O_LINES) - 1;
	(void)snprintf(b1, sizeof(b1), "window=%lu", v);
	SET_DEF(O_WINDOW, b1);

	SET_DEF(O_WRAPMARGIN, "wrapmargin=0");

	/*
	 * By default, the historic vi always displayed information
	 * about two options, redraw and term.  Term seems sufficient.
	 */
	F_SET(&sp->opts[O_TERM], OPT_SET);
	return (0);
}

/*
 * opts_set --
 *	Change the values of one or more options.
 */
int
opts_set(sp, usage, argv)
	SCR *sp;
	char *usage;
	ARGS *argv[];
{
	enum optdisp disp;
	OABBREV atmp, *ap;
	OPTLIST const *op;
	OPTLIST otmp;
	OPTION *spo;
	u_long value, turnoff;
	int ch, equals, offset, qmark, rval;
	char *endp, *name, *p, *sep; 

	disp = NO_DISPLAY;
	for (rval = 0; argv[0]->len != 0; ++argv) {
		/*
		 * The historic vi dumped the options for each occurrence of
		 * "all" in the set list.  Puhleeze.
		 */
		if (!strcmp(argv[0]->bp, "all")) {
			disp = ALL_DISPLAY;
			continue;
		}

		/* Find equals sign or question mark. */
		for (sep = NULL, equals = qmark = 0,
		    p = name = argv[0]->bp; (ch = *p) != '\0'; ++p)
			if (ch == '=' || ch == '?') {
				if (p == name) {
					if (usage != NULL)
						msgq(sp,
						    M_ERR, "Usage: %s", usage);
					return (1);
				}
				sep = p;
				if (ch == '=')
					equals = 1;
				else
					qmark = 1;
				break;
			}

		turnoff = 0;
		op = NULL;
		if (sep != NULL)
			*sep++ = '\0';

		/* Check list of abbreviations. */
		atmp.name = name;
		if ((ap = bsearch(&atmp, abbrev,
		    sizeof(abbrev) / sizeof(OABBREV) - 1,
		    sizeof(OABBREV), opts_abbcmp)) != NULL) {
			op = optlist + ap->offset;
			goto found;
		}

		/* Check list of options. */
		otmp.name = name;
		if ((op = bsearch(&otmp, optlist,
		    sizeof(optlist) / sizeof(OPTLIST) - 1,
		    sizeof(OPTLIST), opts_cmp)) != NULL)
			goto found;

		/* Try the name without any leading "no". */
		if (name[0] == 'n' && name[1] == 'o') {
			turnoff = 1;
			name += 2;
		} else
			goto prefix;

		/* Check list of abbreviations. */
		atmp.name = name;
		if ((ap = bsearch(&atmp, abbrev,
		    sizeof(abbrev) / sizeof(OABBREV) - 1,
		    sizeof(OABBREV), opts_abbcmp)) != NULL) {
			op = optlist + ap->offset;
			goto found;
		}

		/* Check list of options. */
		otmp.name = name;
		if ((op = bsearch(&otmp, optlist,
		    sizeof(optlist) / sizeof(OPTLIST) - 1,
		    sizeof(OPTLIST), opts_cmp)) != NULL)
			goto found;

		/* Check for prefix match. */
prefix:		op = opts_prefix(name);

found:		if (op == NULL) {
			msgq(sp, M_ERR,
			    "no %s option: 'set all' gives all option values",
			    name);
			continue;
		}

		/* Find current option values. */
		offset = op - optlist;
		spo = sp->opts + offset;

		/*
		 * !!!
		 * Historically, the question mark could be a separate
		 * argument.
		 */
		if (!equals && !qmark &&
		    argv[1]->len == 1 && argv[1]->bp[0] == '?') {
			++argv;
			qmark = 1;
		}

		/* Set name, value. */
		switch (op->type) {
		case OPT_0BOOL:
		case OPT_1BOOL:
			if (equals) {
				msgq(sp, M_ERR,
				    "set: [no]%s option doesn't take a value",
				    name);
				break;
			}
			if (qmark) {
				if (!disp)
					disp = SELECT_DISPLAY;
				F_SET(spo, OPT_SELECTED);
				break;
			}
			if (op->func != NULL) {
				if (op->func(sp, spo, NULL, turnoff)) {
					rval = 1;
					break;
				}
			} else if (turnoff)
				O_CLR(sp, offset);
			else
				O_SET(sp, offset);
			goto change;
		case OPT_NUM:
			/*
			 * !!!
			 * Extension to historic vi.  If the OPT_NOSTR flag is
			 * set, a numeric option may be turned off by using a
			 * "no" prefix, e.g. "nowrapmargin".  (We assume that
			 * setting the value to 0 turns a numeric option off.)
			 */
			if (turnoff) {
				if (F_ISSET(op, OPT_NOSTR)) {
					value = 0;
					goto nostr;
				}
				msgq(sp, M_ERR,
				    "set: %s option isn't a boolean", name);
				break;
			}
			if (qmark || !equals) {
				if (!disp)
					disp = SELECT_DISPLAY;
				F_SET(spo, OPT_SELECTED);
				break;
			}
			value = strtol(sep, &endp, 10);
			if (*endp && !isblank(*endp)) {
				msgq(sp, M_ERR,
				    "set %s: illegal number %s", name, sep);
				break;
			}
nostr:			if (op->func != NULL) {
				if (op->func(sp, spo, sep, value)) {
					rval = 1;
					break;
				}
			} else
				O_VAL(sp, offset) = value;
			goto change;
		case OPT_STR:
			if (turnoff) {
				msgq(sp, M_ERR,
				    "set: %s option isn't a boolean", name);
				break;
			}
			if (qmark || !equals) {
				if (!disp)
					disp = SELECT_DISPLAY;
				F_SET(spo, OPT_SELECTED);
				break;
			}
			if (op->func != NULL) {
				if (op->func(sp, spo, sep, (u_long)0)) {
					rval = 1;
					break;
				}
			} else {
				if (F_ISSET(&sp->opts[offset], OPT_ALLOCATED))
					free(O_STR(sp, offset));
				if ((O_STR(sp, offset) = strdup(sep)) == NULL) {
					msgq(sp, M_SYSERR, NULL);
					rval = 1;
					break;
				} else
					F_SET(&sp->opts[offset], OPT_ALLOCATED);
			}
change:			if (sp->s_optchange != NULL)
				(void)sp->s_optchange(sp, offset);
			F_SET(&sp->opts[offset], OPT_SET);
			break;
		default:
			abort();
		}
	}
	if (disp != NO_DISPLAY)
		opts_dump(sp, disp);
	return (rval);
}

/*
 * opts_dump --
 *	List the current values of selected options.
 */
void
opts_dump(sp, type)
	SCR *sp;
	enum optdisp type;
{
	OPTLIST const *op;
	int base, b_num, cnt, col, colwidth, curlen, s_num;
	int numcols, numrows, row;
	int b_op[O_OPTIONCOUNT], s_op[O_OPTIONCOUNT];
	char nbuf[20];

	/*
	 * XXX
	 * It's possible to get here by putting "set option" in the
	 * .exrc file.  I can't think of a clean way to layer this,
	 * or a reasonable check to make, so we block it here.
	 */
	if (sp->stdfp == NULL) {
		msgq(sp, M_ERR,
		    "Option display requires that the screen be initialized");
		return;
	}

	/*
	 * Options are output in two groups -- those that fit in a column and
	 * those that don't.  Output is done on 6 character "tab" boundaries
	 * for no particular reason.  (Since we don't output tab characters,
	 * we can ignore the terminal's tab settings.)  Ignore the user's tab
	 * setting because we have no idea how reasonable it is.
	 *
	 * Find a column width we can live with.
	 */
	for (cnt = 6; cnt > 1; --cnt) {
		colwidth = (sp->cols - 1) / cnt & ~(STANDARD_TAB - 1);
		if (colwidth >= 10) {
			colwidth =
			    (colwidth + STANDARD_TAB) & ~(STANDARD_TAB - 1);
			break;
		}
		colwidth = 0;
	}

	/*
	 * Get the set of options to list, entering them into
	 * the column list or the overflow list.
	 */
	for (b_num = s_num = 0, op = optlist; op->name != NULL; ++op) {
		cnt = op - optlist;

		/* If OPT_NEVER set, it's never displayed. */
		if (F_ISSET(op, OPT_NEVER))
			continue;

		switch (type) {
		case ALL_DISPLAY:		/* Display all. */
			break;
		case CHANGED_DISPLAY:		/* Display changed. */
			if (!F_ISSET(&sp->opts[cnt], OPT_SET))
				continue;
			break;
		case SELECT_DISPLAY:		/* Display selected. */
			if (!F_ISSET(&sp->opts[cnt], OPT_SELECTED))
				continue;
			break;
		default:
		case NO_DISPLAY:
			abort();
			/* NOTREACHED */
		}
		F_CLR(&sp->opts[cnt], OPT_SELECTED);

		curlen = strlen(op->name);
		switch (op->type) {
		case OPT_0BOOL:
		case OPT_1BOOL:
			if (!O_ISSET(sp, cnt))
				curlen += 2;
			break;
		case OPT_NUM:
			(void)snprintf(nbuf,
			    sizeof(nbuf), "%ld", O_VAL(sp, cnt));
			curlen += strlen(nbuf);
			break;
		case OPT_STR:
			curlen += strlen(O_STR(sp, cnt)) + 3;
			break;
		}
		/* Offset by two so there's a gap. */
		if (curlen < colwidth - 2)
			s_op[s_num++] = cnt;
		else
			b_op[b_num++] = cnt;
	}

	if (s_num > 0) {
		/* Figure out the number of columns. */
		numcols = (sp->cols - 1) / colwidth;
		if (s_num > numcols) {
			numrows = s_num / numcols;
			if (s_num % numcols)
				++numrows;
		} else
			numrows = 1;

		/* Display the options in sorted order. */
		for (row = 0; row < numrows;) {
			for (base = row, col = 0; col < numcols; ++col) {
				cnt = opts_print(sp, &optlist[s_op[base]]);
				if ((base += numrows) >= s_num)
					break;
				(void)ex_printf(EXCOOKIE,
				    "%*s", (int)(colwidth - cnt), "");
			}
			if (++row < numrows || b_num)
				(void)ex_printf(EXCOOKIE, "\n");
		}
	}

	for (row = 0; row < b_num;) {
		(void)opts_print(sp, &optlist[b_op[row]]);
		if (++row < b_num)
			(void)ex_printf(EXCOOKIE, "\n");
	}
	(void)ex_printf(EXCOOKIE, "\n");
}

/*
 * opts_print --
 *	Print out an option.
 */
static int
opts_print(sp, op)
	SCR *sp;
	OPTLIST const *op;
{
	int curlen, offset;

	curlen = 0;
	offset = op - optlist;
	switch (op->type) {
	case OPT_0BOOL:
	case OPT_1BOOL:
		curlen += ex_printf(EXCOOKIE,
		    "%s%s", O_ISSET(sp, offset) ? "" : "no", op->name);
		break;
	case OPT_NUM:
		curlen += ex_printf(EXCOOKIE,
		     "%s=%ld", op->name, O_VAL(sp, offset));
		break;
	case OPT_STR:
		curlen += ex_printf(EXCOOKIE,
		    "%s=\"%s\"", op->name, O_STR(sp, offset));
		break;
	}
	return (curlen);
}

/*
 * opts_save --
 *	Write the current configuration to a file.
 */
int
opts_save(sp, fp)
	SCR *sp;
	FILE *fp;
{
	OPTLIST const *op;
	int ch, cnt;
	char *p;

	for (op = optlist; op->name != NULL; ++op) {
		if (F_ISSET(op, OPT_NOSAVE))
			continue;
		cnt = op - optlist;
		switch (op->type) {
		case OPT_0BOOL:
		case OPT_1BOOL:
			if (O_ISSET(sp, cnt))
				(void)fprintf(fp, "set %s\n", op->name);
			else
				(void)fprintf(fp, "set no%s\n", op->name);
			break;
		case OPT_NUM:
			(void)fprintf(fp,
			    "set %s=%-3d\n", op->name, O_VAL(sp, cnt));
			break;
		case OPT_STR:
			(void)fprintf(fp, "set ");
			for (p = op->name; (ch = *p) != '\0'; ++p) {
				if (isblank(ch) || ch == '\\')
					(void)putc('\\', fp);
				(void)putc(ch, fp);
			}
			(void)putc('=', fp);
			for (p = O_STR(sp, cnt); (ch = *p) != '\0'; ++p) {
				if (isblank(ch) || ch == '\\')
					(void)putc('\\', fp);
				(void)putc(ch, fp);
			}
			(void)putc('\n', fp);
			break;
		}
		if (ferror(fp)) {
			msgq(sp, M_ERR, "I/O error: %s", strerror(errno));
			return (1);
		}
	}
	return (0);
}

/*
 * opts_prefix --
 *	Check to see if the name is the prefix of one (and only one)
 *	option.  If so, return the option.
 */
static OPTLIST const *
opts_prefix(name)
	char *name;
{
	OPTLIST const *op, *save_op;
	size_t len;

	save_op = NULL;
	len = strlen(name);
	for (op = optlist; op->name != NULL; ++op) {
		if (op->name[0] < name[0])
			continue;
		if (op->name[0] > name[0])
			break;
		if (!memcmp(op->name, name, len)) {
			if (save_op != NULL)
				return (NULL);
			save_op = op;
		}
	}
	return (save_op);
}

static int
opts_abbcmp(a, b)
        const void *a, *b;
{
        return(strcmp(((OABBREV *)a)->name, ((OABBREV *)b)->name));
}

static int
opts_cmp(a, b)
        const void *a, *b;
{
        return(strcmp(((OPTLIST *)a)->name, ((OPTLIST *)b)->name));
}

/*
 * opts_free --
 *	Free all option strings
 */
void
opts_free(sp)
	SCR *sp;
{
	int cnt;
	char *p;

	for (cnt = 0; cnt < O_OPTIONCOUNT; ++cnt)
		if (F_ISSET(&sp->opts[cnt], OPT_ALLOCATED)) {
			p = O_STR(sp, cnt);
			FREE(p, strlen(p) + 1);
		}
}

/*
 * opts_copy --
 *	Copy a screen's OPTION array.
 */
int
opts_copy(orig, sp)
	SCR *orig, *sp;
{
	OPTION *op;
	int cnt;

	/* Copy most everything without change. */
	memmove(sp->opts, orig->opts, sizeof(orig->opts));

	/*
	 * Allocate copies of the strings -- keep trying to reallocate
	 * after ENOMEM failure, otherwise end up with more than one
	 * screen referencing the original memory.
	 */
	for (op = sp->opts, cnt = 0; cnt < O_OPTIONCOUNT; ++cnt, ++op)
		if (F_ISSET(&sp->opts[cnt], OPT_ALLOCATED) &&
		    (O_STR(sp, cnt) = strdup(O_STR(sp, cnt))) == NULL) {
			msgq(orig, M_SYSERR, NULL);
			return (1);
		}
	return (0);
}
@


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[] = "@@(#)options.c	10.43 (Berkeley) 5/16/96";
d47 1
d51 1
d54 7
a60 3
#include "common.h"
#include "../vi/vi.h"
#include "pathnames.h"
d64 1
d75 1
a75 1
OPTLIST const optlist[] = {
a83 2
/* O_BACKUP	  4.4BSD */
	{"backup",	NULL,		OPT_STR,	0},
d87 1
a87 3
	{"cdpath",	NULL,		OPT_STR,	0},
/* O_CEDIT	  4.4BSD */
	{"cedit",	NULL,		OPT_STR,	0},
d92 2
a97 2
/* O_ESCAPETIME	  4.4BSD */
	{"escapetime",	NULL,		OPT_NUM,	0},
d103 1
a103 3
	{"extended",	f_recompile,	OPT_0BOOL,	0},
/* O_FILEC	  4.4BSD */
	{"filec",	NULL,		OPT_STR,	0},
a107 2
/* O_ICLOWER	  4.4BSD */
	{"iclower",	f_recompile,	OPT_0BOOL,	0},
d109 1
a109 1
	{"ignorecase",	f_recompile,	OPT_0BOOL,	0},
d113 1
a113 1
	{"leftright",	f_reformat,	OPT_0BOOL,	0},
d116 5
a120 4
/* O_LISP	    4BSD
 *	XXX
 *	When the lisp option is implemented, delete the OPT_NOSAVE flag,
 *	so that :mkexrc dumps it.
d124 1
a124 7
	{"list",	f_reformat,	OPT_0BOOL,	0},
/* O_LOCKFILES	  4.4BSD
 *	XXX
 *	Locking isn't reliable enough over NFS to require it, in addition,
 *	it's a serious startup performance problem over some remote links.
 */
	{"lock",	NULL,		OPT_1BOOL,	0},
d130 5
a134 14
	{"mesg",	NULL,		OPT_1BOOL,	0},
/* O_MODELINE	    4BSD
 *	!!!
 *	This has been documented in historical systems as both "modeline"
 *	and as "modelines".  Regardless of the name, this option represents
 *	a security problem of mammoth proportions, not to mention a stunning
 *	example of what your intro CS professor referred to as the perils of
 *	mixing code and data.  Don't add it, or I will kill you.
 */
	{"modeline",	NULL,		OPT_0BOOL,	OPT_NOSET},
/* O_MSGCAT	  4.4BSD */
	{"msgcat",	f_msgcat,	OPT_STR,	0},
/* O_NOPRINT	  4.4BSD */
	{"noprint",	f_print,	OPT_STR,	0},
d136 1
a136 1
	{"number",	f_reformat,	OPT_0BOOL,	0},
d138 1
a138 1
	{"octal",	f_print,	OPT_0BOOL,	0},
a144 2
/* O_PRINT	  4.4BSD */
	{"print",	f_print,	OPT_STR,	0},
d148 1
a148 1
	{"readonly",	f_readonly,	OPT_0BOOL,	OPT_ALWAYS},
d156 1
a156 1
	{"report",	NULL,		OPT_NUM,	0},
a160 2
/* O_SEARCHINCR	  4.4BSD */
	{"searchincr",	NULL,		OPT_0BOOL,	0},
a162 2
/* O_SECURE	  4.4BSD */
	{"secure",	NULL,		OPT_0BOOL,	OPT_NOUNSET},
a164 2
/* O_SHELLMETA	  4.4BSD */
	{"shellmeta",	NULL,		OPT_STR,	0},
d166 3
a168 1
	{"shiftwidth",	NULL,		OPT_NUM,	OPT_NOZERO},
d174 1
a174 1
	{"sidescroll",	NULL,		OPT_NUM,	OPT_NOZERO},
d177 2
a178 9
/* O_SOURCEANY	    4BSD (undocumented)
 *	!!!
 *	Historic vi, on startup, source'd $HOME/.exrc and ./.exrc, if they
 *	were owned by the user.  The sourceany option was an undocumented
 *	feature of historic vi which permitted the startup source'ing of
 *	.exrc files the user didn't own.  This is an obvious security problem,
 *	and we ignore the option.
 */
	{"sourceany",	NULL,		OPT_0BOOL,	OPT_NOSET},
d180 1
a180 1
	{"tabstop",	f_reformat,	OPT_NUM,	OPT_NOZERO},
d182 1
a182 1
	{"taglength",	NULL,		OPT_NUM,	0},
d184 3
a186 7
	{"tags",	NULL,		OPT_STR,	0},
/* O_TERM	    4BSD
 *	!!!
 *	By default, the historic vi always displayed information about two
 *	options, redraw and term.  Term seems sufficient.
 */
	{"term",	NULL,		OPT_STR,	OPT_ADISP|OPT_NOSAVE},
d198 1
a198 1
	{"w1200",	f_w1200,	OPT_NUM,	OPT_NDISP|OPT_NOSAVE},
d200 1
a200 1
	{"w300",	f_w300,		OPT_NUM,	OPT_NDISP|OPT_NOSAVE},
d202 1
a202 1
	{"w9600",	f_w9600,	OPT_NUM,	OPT_NDISP|OPT_NOSAVE},
a206 2
/* O_WRAPLEN	  4.4BSD */
	{"wraplen",	NULL,		OPT_NUM,	0},
d208 1
a208 1
	{"wrapmargin",	NULL,		OPT_NUM,	0},
a254 1
	{"wl",		O_WRAPLEN},		/*   4.4BSD */
d262 2
a263 3
 *	Initialize some of the options.
 *
 * PUBLIC: int opts_init __P((SCR *, int *));
d266 1
a266 1
opts_init(sp, oargs)
a267 1
	int *oargs;
d272 1
a272 1
	int cnt, optindx;
d276 1
d278 1
a278 1
	a.len = b.len = 0;
d282 1
a282 2
	/* Set numeric and string default values. */
#define	OI(indx, str) {							\
d286 4
a289 3
	if (opts_set(sp, argv, NULL)) {					\
		 optindx = indx;					\
		goto err;						\
d291 1
d293 10
a302 22
	/*
	 * Indirect global options to global space.  Specifically, set up
	 * terminal, lines, columns first, they're used by other options.
	 * Note, don't set the flags until we've set up the indirection.
	 */
	if (o_set(sp, O_TERM, 0, NULL, GO_TERM))
		goto err;
	F_SET(&sp->opts[O_TERM], OPT_GLOBAL);
	if (o_set(sp, O_LINES, 0, NULL, GO_LINES))
		goto err;
	F_SET(&sp->opts[O_LINES], OPT_GLOBAL);
	if (o_set(sp, O_COLUMNS, 0, NULL, GO_COLUMNS))
		goto err;
	F_SET(&sp->opts[O_COLUMNS], OPT_GLOBAL);
	if (o_set(sp, O_SECURE, 0, NULL, GO_SECURE))
		goto err;
	F_SET(&sp->opts[O_SECURE], OPT_GLOBAL);

	/* Initialize string values. */
	(void)snprintf(b1, sizeof(b1),
	    "cdpath=%s", (s = getenv("CDPATH")) == NULL ? ":" : s);
	OI(O_CDPATH, b1);
d311 8
a318 10
	(void)snprintf(b1, sizeof(b1),
	    "directory=%s", (s = getenv("TMPDIR")) == NULL ? _PATH_TMP : s);
	OI(O_DIRECTORY, b1);
	OI(O_ESCAPETIME, "escapetime=1");
	OI(O_KEYTIME, "keytime=6");
	OI(O_MATCHTIME, "matchtime=7");
	(void)snprintf(b1, sizeof(b1), "msgcat=%s", _PATH_MSGCAT);
	OI(O_MSGCAT, b1);
	OI(O_REPORT, "report=5");
	OI(O_PARAGRAPHS, "paragraphs=IPLPPPQPP LIpplpipbp");
d320 8
a327 9
	OI(O_RECDIR, b1);
	OI(O_SECTIONS, "sections=NHSHH HUnhsh");
	(void)snprintf(b1, sizeof(b1),
	    "shell=%s", (s = getenv("SHELL")) == NULL ? _PATH_BSHELL : s);
	OI(O_SHELL, b1);
	OI(O_SHELLMETA, "shellmeta=~{[*?$`'\"\\");
	OI(O_SHIFTWIDTH, "shiftwidth=8");
	OI(O_SIDESCROLL, "sidescroll=16");
	OI(O_TABSTOP, "tabstop=8");
d329 4
a332 1
	OI(O_TAGS, b1);
d336 3
a338 2
	 * Initialize O_SCROLL here, after term; initializing term should
	 * have created a LINES/COLUMNS value.
d340 3
a342 4
	if ((v = (O_VAL(sp, O_LINES) - 1) / 2) == 0)
		v = 1;
	(void)snprintf(b1, sizeof(b1), "scroll=%ld", v);
	OI(O_SCROLL, b1);
a348 3
	 *
	 * Note, the windows option code will correct any too-large value
	 * or when the O_LINES value is 1.
d350 1
a350 2
	if (sp->gp->scr_baud(sp, &v))
		return (1);
d358 1
a358 1
	OI(O_WINDOW, b1);
d360 1
a360 23
	/*
	 * Set boolean default values, and copy all settings into the
	 * the default information.
	 */
	for (op = optlist, cnt = 0; op->name != NULL; ++op, ++cnt)
		switch (op->type) {
		case OPT_0BOOL:
			break;
		case OPT_1BOOL:
			O_SET(sp, cnt);
			O_D_SET(sp, cnt);
			break;
		case OPT_NUM:
			o_set(sp, cnt, OS_DEF, NULL, O_VAL(sp, cnt));
			break;
		case OPT_STR:
			if (O_STR(sp, cnt) != NULL && o_set(sp,
			    cnt, OS_DEF | OS_STRDUP, O_STR(sp, cnt), 0))
				goto err;
			break;
		default:
			abort();
		}
d363 2
a364 4
	 * !!!
	 * Some options can be initialized by the command name or the
	 * command-line arguments.  They don't set the default values,
	 * it's historic practice.
d366 1
a366 2
	for (; *oargs != -1; ++oargs)
		OI(*oargs, optlist[*oargs].name);
a367 5
#undef OI

err:	msgq(sp, M_ERR,
	    "031|Unable to set default %s option", optlist[optindx].name);
	return (1);
a372 2
 *
 * PUBLIC: int opts_set __P((SCR *, ARGS *[], char *));
d375 1
a375 1
opts_set(sp, argv, usage)
d377 1
a378 1
	char *usage;
d381 1
a381 1
	enum nresult nret;
d383 1
d386 2
a387 2
	int ch, equals, nf, nf2, offset, qmark, rval;
	char *endp, *name, *p, *sep, *t;
d406 2
a407 2
						msgq(sp, M_ERR,
						    "032|Usage: %s", usage);
d423 18
a440 3
		/* Search for the name, then name without any leading "no". */
		if ((op = opts_search(name)) == NULL &&
		    name[0] == 'n' && name[1] == 'o') {
d443 2
a444 2
			op = opts_search(name);
		}
d446 23
a468 4
		if (op == NULL) {
			msgq_str(sp, M_ERR, name,
		    "033|set: no %s option: 'set all' gives all option values");
			rval = 1;
a490 16
			/* Some options may not be reset. */
			if (F_ISSET(op, OPT_NOUNSET) && turnoff) {
				msgq_str(sp, M_ERR, name,
			    "291|set: the %s option may not be turned off");
				rval = 1;
				break;
			}

			/* Some options may not be set. */
			if (F_ISSET(op, OPT_NOSET) && !turnoff) {
				msgq_str(sp, M_ERR, name,
			    "313|set: the %s option may never be turned on");
				rval = 1;
				break;
			}

d492 3
a494 3
				msgq_str(sp, M_ERR, name,
			    "034|set: [no]%s option doesn't take a value");
				rval = 1;
d503 4
a506 12

			/*
			 * Do nothing if the value is unchanged, the underlying
			 * functions can be expensive.
			 */
			if (!F_ISSET(op, OPT_ALWAYS))
				if (turnoff) {
					if (!O_ISSET(sp, offset))
						break;
				} else {
					if (O_ISSET(sp, offset))
						break;
d508 1
a508 13

			/* Report to subsystems. */
			if (op->func != NULL &&
			    op->func(sp, spo, NULL, &turnoff) ||
			    ex_optchange(sp, offset, NULL, &turnoff) ||
			    v_optchange(sp, offset, NULL, &turnoff) ||
			    sp->gp->scr_optchange(sp, offset, NULL, &turnoff)) {
				rval = 1;
				break;
			}

			/* Set the value. */
			if (turnoff)
d512 1
a512 1
			break;
d514 7
d522 6
a527 3
				msgq_str(sp, M_ERR, name,
				    "035|set: %s option isn't a boolean");
				rval = 1;
d536 1
a536 27

			if (!isdigit(sep[0]))
				goto badnum;
			if ((nret =
			    nget_uslong(&value, sep, &endp, 10)) != NUM_OK) {
				p = msg_print(sp, name, &nf);
				t = msg_print(sp, sep, &nf2);
				switch (nret) {
				case NUM_ERR:
					msgq(sp, M_SYSERR,
					    "036|set: %s option: %s", p, t);
					break;
				case NUM_OVER:
					msgq(sp, M_ERR,
			    "037|set: %s option: %s: value overflow", p, t);
					break;
				case NUM_OK:
				case NUM_UNDER:
					abort();
				}
				if (nf)
					FREE_SPACE(sp, p, 0);
				if (nf2)
					FREE_SPACE(sp, t, 0);
				rval = 1;
				break;
			}
a537 2
badnum:				p = msg_print(sp, name, &nf);
				t = msg_print(sp, sep, &nf2);
d539 1
a539 6
		    "038|set: %s option: %s is an illegal number", p, t);
				if (nf)
					FREE_SPACE(sp, p, 0);
				if (nf2)
					FREE_SPACE(sp, t, 0);
				rval = 1;
d542 8
a549 31

			/* Some options may never be set to zero. */
			if (F_ISSET(op, OPT_NOZERO) && value == 0) {
				msgq_str(sp, M_ERR, name,
			    "314|set: the %s option may never be set to 0");
				rval = 1;
				break;
			}

			/*
			 * Do nothing if the value is unchanged, the underlying
			 * functions can be expensive.
			 */
			if (!F_ISSET(op, OPT_ALWAYS) &&
			    O_VAL(sp, offset) == value)
				break;

			/* Report to subsystems. */
			if (op->func != NULL &&
			    op->func(sp, spo, sep, &value) ||
			    ex_optchange(sp, offset, sep, &value) ||
			    v_optchange(sp, offset, sep, &value) ||
			    sp->gp->scr_optchange(sp, offset, sep, &value)) {
				rval = 1;
				break;
			}

			/* Set the value. */
			if (o_set(sp, offset, 0, NULL, value))
				rval = 1;
			break;
d552 2
a553 3
				msgq_str(sp, M_ERR, name,
				    "039|set: %s option isn't a boolean");
				rval = 1;
d562 14
a575 18

			/*
			 * Do nothing if the value is unchanged, the underlying
			 * functions can be expensive.
			 */
			if (!F_ISSET(op, OPT_ALWAYS) &&
			    O_STR(sp, offset) != NULL &&
			    !strcmp(O_STR(sp, offset), sep))
				break;

			/* Report to subsystems. */
			if (op->func != NULL &&
			    op->func(sp, spo, sep, NULL) ||
			    ex_optchange(sp, offset, sep, NULL) ||
			    v_optchange(sp, offset, sep, NULL) ||
			    sp->gp->scr_optchange(sp, offset, sep, NULL)) {
				rval = 1;
				break;
d577 3
a579 4

			/* Set the value. */
			if (o_set(sp, offset, OS_FREE | OS_STRDUP, sep, 0))
				rval = 1;
a590 68
 * o_set --
 *	Set an option's value.
 *
 * PUBLIC: int o_set __P((SCR *, int, u_int, char *, u_long));
 */
int
o_set(sp, opt, flags, str, val)
	SCR *sp;
	int opt;
	u_int flags;
	char *str;
	u_long val;
{
	OPTION *op;

	/* Set a pointer to the options area. */
	op = F_ISSET(&sp->opts[opt], OPT_GLOBAL) ?
	    &sp->gp->opts[sp->opts[opt].o_cur.val] : &sp->opts[opt];

	/* Copy the string, if requested. */
	if (LF_ISSET(OS_STRDUP) && (str = strdup(str)) == NULL) {
		msgq(sp, M_SYSERR, NULL);
		return (1);
	}


	/* Free the previous string, if requested, and set the value. */
	if LF_ISSET(OS_DEF) {
		if (LF_ISSET(OS_FREE) && op->o_def.str != NULL)
			free(op->o_def.str);
		if (LF_ISSET(OS_STR | OS_STRDUP))
			op->o_def.str = str;
		else
			op->o_def.val = val;
	} else {
		if (LF_ISSET(OS_FREE) && op->o_cur.str != NULL)
			free(op->o_cur.str);
		if (LF_ISSET(OS_STR | OS_STRDUP))
			op->o_cur.str = str;
		else
			op->o_cur.val = val;
	}
	return (0);
}

/*
 * opts_empty --
 *	Return 1 if the string option is invalid, 0 if it's OK.
 *
 * PUBLIC: int opts_empty __P((SCR *, int, int));
 */
int
opts_empty(sp, off, silent)
	SCR *sp;
	int off, silent;
{
	char *p;

	if ((p = O_STR(sp, off)) == NULL || p[0] == '\0') {
		if (!silent)
			msgq_str(sp, M_ERR, optlist[off].name,
			    "305|No %s edit option specified");
		return (1);
	}
	return (0);
}

/*
a592 2
 *
 * PUBLIC: void opts_dump __P((SCR *, enum optdisp));
d606 12
d624 1
a624 1
	 * Find a column width we can live with, testing from 10 columns to 1.
d626 2
a627 2
	for (numcols = 10; numcols > 1; --numcols) {
		colwidth = sp->cols / numcols & ~(STANDARD_TAB - 1);
a630 1
			numcols = sp->cols / colwidth;
d643 2
a644 2
		/* If OPT_NDISP set, it's never displayed. */
		if (F_ISSET(op, OPT_NDISP))
d651 2
a652 17
			/* If OPT_ADISP set, it's always "changed". */
			if (F_ISSET(op, OPT_ADISP))
				break;
			switch (op->type) {
			case OPT_0BOOL:
			case OPT_1BOOL:
			case OPT_NUM:
				if (O_VAL(sp, cnt) == O_D_VAL(sp, cnt))
					continue;
				break;
			case OPT_STR:
				if (O_STR(sp, cnt) == O_D_STR(sp, cnt) ||
				    O_D_STR(sp, cnt) != NULL &&
				    !strcmp(O_STR(sp, cnt), O_D_STR(sp, cnt)))
					continue;
				break;
			}
d661 1
d678 1
a678 3
			if (O_STR(sp, cnt) != NULL)
				curlen += strlen(O_STR(sp, cnt));
			curlen += 3;
d681 2
a682 2
		/* Offset by 2 so there's a gap. */
		if (curlen <= colwidth - 2)
d689 2
a690 1
		/* Figure out the number of rows. */
d704 2
a705 2
				(void)ex_printf(sp, "%*s",
				    (int)(colwidth - cnt), "");
d708 1
a708 1
				(void)ex_puts(sp, "\n");
d715 1
a715 1
			(void)ex_puts(sp, "\n");
d717 1
a717 1
	(void)ex_puts(sp, "\n");
d736 1
a736 1
		curlen += ex_printf(sp,
d740 2
a741 1
		curlen += ex_printf(sp, "%s=%ld", op->name, O_VAL(sp, offset));
d744 2
a745 2
		curlen += ex_printf(sp, "%s=\"%s\"", op->name,
		    O_STR(sp, offset) == NULL ? "" : O_STR(sp, offset));
a753 2
 *
 * PUBLIC: int opts_save __P((SCR *, FILE *));
d778 1
a778 1
			    "set %s=%-3ld\n", op->name, O_VAL(sp, cnt));
a780 2
			if (O_STR(sp, cnt) == NULL)
				break;
d797 1
a797 1
			msgq(sp, M_SYSERR, NULL);
d804 4
a807 5
/* 
 * opts_search --
 *	Search for an option.
 *
 * PUBLIC: OPTLIST const *opts_search __P((char *));
d809 2
a810 2
OPTLIST const *
opts_search(name)
d813 1
a813 3
	OPTLIST const *op, *found;
	OABBREV atmp, *ap;
	OPTLIST otmp;
d816 1
a816 16
	/* Check list of abbreviations. */
	atmp.name = name;
	if ((ap = bsearch(&atmp, abbrev, sizeof(abbrev) / sizeof(OABBREV) - 1,
	    sizeof(OABBREV), opts_abbcmp)) != NULL)
		return (optlist + ap->offset);

	/* Check list of options. */
	otmp.name = name;
	if ((op = bsearch(&otmp, optlist, sizeof(optlist) / sizeof(OPTLIST) - 1,
	    sizeof(OPTLIST), opts_cmp)) != NULL)
		return (op);
		
	/*
	 * Check to see if the name is the prefix of one (and only one)
	 * option.  If so, return the option.
	 */
d818 1
a818 1
	for (found = NULL, op = optlist; op->name != NULL; ++op) {
d824 1
a824 1
			if (found != NULL)
d826 1
a826 1
			found = op;
d829 1
a829 1
	return (found);
d847 18
a866 2
 *
 * PUBLIC: int opts_copy __P((SCR *, SCR *));
d872 2
a873 1
	int cnt, rval;
d878 10
a887 14
	/* Copy the string edit options. */
	for (cnt = rval = 0; cnt < O_OPTIONCOUNT; ++cnt) {
		if (optlist[cnt].type != OPT_STR ||
		    F_ISSET(&optlist[cnt], OPT_GLOBAL))
			continue;
		/*
		 * If never set, or already failed, NULL out the entries --
		 * have to continue after failure, otherwise would have two
		 * screens referencing the same memory.
		 */
		if (rval || O_STR(sp, cnt) == NULL) {
			o_set(sp, cnt, OS_STR, NULL, 0);
			o_set(sp, cnt, OS_DEF | OS_STR, NULL, 0);
			continue;
d889 1
a889 38

		/* Copy the current string. */
		if (o_set(sp, cnt, OS_STRDUP, O_STR(sp, cnt), 0)) {
			o_set(sp, cnt, OS_DEF | OS_STR, NULL, 0);
			goto nomem;
		}

		/* Copy the default string. */
		if (O_D_STR(sp, cnt) != NULL &&
		    o_set(sp, cnt, OS_DEF | OS_STRDUP, O_D_STR(sp, cnt), 0)) {
nomem:			msgq(orig, M_SYSERR, NULL);
			rval = 1;
		}
	}
	return (rval);
}

/*
 * opts_free --
 *	Free all option strings
 *
 * PUBLIC: void opts_free __P((SCR *));
 */
void
opts_free(sp)
	SCR *sp;
{
	int cnt;

	for (cnt = 0; cnt < O_OPTIONCOUNT; ++cnt) {
		if (optlist[cnt].type != OPT_STR ||
		    F_ISSET(&optlist[cnt], OPT_GLOBAL))
			continue;
		if (O_STR(sp, cnt) != NULL)
			free(O_STR(sp, cnt));
		if (O_D_STR(sp, cnt) != NULL)
			free(O_D_STR(sp, cnt));
	}
@


1.1.1.4
log
@import of nvi 1.79
@
text
@d13 1
a13 1
static const char sccsid[] = "@@(#)options.c	10.51 (Berkeley) 10/14/96";
a137 2
/* O_PATH	  4.4BSD */
	{"path",	NULL,		OPT_STR,	0},
a216 2
/* O_WINDOWNAME	    4BSD */
	{"windowname",	NULL,		OPT_0BOOL,	0},
a256 1
	{"smd",		O_SHOWMODE},		/*     4BSD */
a345 2
	(void)snprintf(b1, sizeof(b1), "path=%s", "");
	OI(O_PATH, b1);
d390 2
a391 2
	 * Set boolean default values, and copy all settings into the default
	 * information.  OS_NOFREE is set, we're copying, not replacing.
d405 2
a406 2
			if (O_STR(sp, cnt) != NULL && o_set(sp, cnt,
			    OS_DEF | OS_NOFREE | OS_STRDUP, O_STR(sp, cnt), 0))
d490 1
d492 2
a493 1
			opts_nomatch(sp, name);
d692 1
a692 1
			if (o_set(sp, offset, OS_STRDUP, sep, 0))
d730 1
d732 4
a735 4
	if LF_ISSET(OS_DEF)
		if (LF_ISSET(OS_STR | OS_STRDUP)) {
			if (!LF_ISSET(OS_NOFREE) && op->o_def.str != NULL)
				free(op->o_def.str);
d737 1
a737 1
		} else
d739 4
a742 4
	else
		if (LF_ISSET(OS_STR | OS_STRDUP)) {
			if (!LF_ISSET(OS_NOFREE) && op->o_cur.str != NULL)
				free(op->o_cur.str);
d744 1
a744 1
		} else
d746 1
a1040 15
/* 
 * opts_nomatch --
 *	Standard nomatch error message for options.
 *
 * PUBLIC: void opts_nomatch __P((SCR *, char *));
 */
void
opts_nomatch(sp, name)
	SCR *sp;
	char *name;
{
	msgq_str(sp, M_ERR, name,
	    "033|set: no %s option: 'set all' gives all option values");
}

d1068 1
a1068 1
	memcpy(sp->opts, orig->opts, sizeof(orig->opts));
d1081 2
a1082 2
			o_set(sp, cnt, OS_NOFREE | OS_STR, NULL, 0);
			o_set(sp, cnt, OS_DEF | OS_NOFREE | OS_STR, NULL, 0);
d1087 2
a1088 2
		if (o_set(sp, cnt, OS_NOFREE | OS_STRDUP, O_STR(sp, cnt), 0)) {
			o_set(sp, cnt, OS_DEF | OS_NOFREE | OS_STR, NULL, 0);
d1093 2
a1094 2
		if (O_D_STR(sp, cnt) != NULL && o_set(sp, cnt,
		    OS_DEF | OS_NOFREE | OS_STRDUP, O_D_STR(sp, cnt), 0)) {
@

