[comp.editors] comments on vi's modeline variable in SunOS 4.0.3

maart@cs.vu.nl (Maarten Litmaath) (11/21/89)

Relevant portions of ex_io.c:
--
	/*
	 *	if the modeline variable is set,
	 *	check the first and last five lines of the file
	 *	for a mode line.
	 */
	if (value(MODELINE)) {
		for (a=first; a<=last; a++) {
			if (a==first+5 && last-first > 10)
				a = last - 4;
			getline(*a);
			checkmodeline(linebuf);
		}
	}

/* ... */

checkmodeline(line)
char *line;
{
	char *beg, *end;
	char cmdbuf[1024];
	char *index(), *rindex();

	beg = index(line, ':');
	if (beg == NULL)
		return;
	if (&beg[-3] < line)
		return;
	if (!(  ( (beg[-3] == ' ' || beg[-3] == '\t')
	        && beg[-2] == 'e'
		&& beg[-1] == 'x')
	     || ( (beg[-3] == ' ' || beg[-3] == '\t')
	        && beg[-2] == 'v'
		&& beg[-1] == 'i'))) return;
	strncpy(cmdbuf, beg+1, sizeof cmdbuf);
	end = rindex(cmdbuf, ':');
	if (end == NULL)
		return;
	*end = 0;
	globp = cmdbuf;
	commands(1, 1);
}
--
So if the modeline variable is set and the file would look like below, we
would expect things to work:
--
 ex:se nu ai:
 vi:se sh=/bin/sh|map q ZZ:
text
text
text
	vi:!date^M:
--
However, under SunOS 4.0.3 this file causes vi to hang!  The map command seems
to be the source of trouble, and the shell escape is handled incorrectly too.
If you delete the second and the last line, it works.

The danger: consider someone editing the passwd file, which has a line
(in 1 of the intervals) containing the string "ex:" and terminated by ":" ->
unexpected side effects!

BTW, many vi versions don't have this modeline feature.
-- 
[...] like a roving gang.  "Ah, here's a NEW almost-empty group to post train
schedules and core dumps in!"  (Joe Buck) | maart@cs.vu.nl, mcsun!botter!maart