From owner-freebsd-bugs@FreeBSD.ORG Thu Jul 15 05:10:18 2004 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 0D1C916A4D1 for ; Thu, 15 Jul 2004 05:10:18 +0000 (GMT) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id D8D9943D5E for ; Thu, 15 Jul 2004 05:10:17 +0000 (GMT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.12.11/8.12.11) with ESMTP id i6F5AH0V091630 for ; Thu, 15 Jul 2004 05:10:17 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.12.11/8.12.11/Submit) id i6F5AHmN091629; Thu, 15 Jul 2004 05:10:17 GMT (envelope-from gnats) Resent-Date: Thu, 15 Jul 2004 05:10:17 GMT Resent-Message-Id: <200407150510.i6F5AHmN091629@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, =?iso-8859-1?Q?Jos=E9?= de Paula Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id B7D1016A4CE for ; Thu, 15 Jul 2004 05:08:18 +0000 (GMT) Received: from smtp-29.ig.com.br (smtp-29.ig.com.br [200.226.132.157]) by mx1.FreeBSD.org (Postfix) with SMTP id CBFF643D46 for ; Thu, 15 Jul 2004 05:08:17 +0000 (GMT) (envelope-from jose_de_paula@ig.com.br) Received: (qmail 15008 invoked from network); 15 Jul 2004 05:07:06 -0000 Received: from unknown (HELO luna.pinguim) (200.181.94.155) by smtp-29.ig.com.br with SMTP; 15 Jul 2004 05:07:06 -0000 Received: from sombra by luna.pinguim with local (Exim 4.34; FreeBSD) id 1BkyS2-000H7A-Ac for FreeBSD-gnats-submit@freebsd.org; Thu, 15 Jul 2004 02:06:34 -0300 Message-Id: <20040715050634.GA65786@luna.pinguim> Date: Thu, 15 Jul 2004 02:06:34 -0300 From: =?iso-8859-1?Q?Jos=E9?= de Paula To: FreeBSD-gnats-submit@FreeBSD.org Subject: bin/69085: [patch] basic modelines for contrib/nvi X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: =?iso-8859-1?Q?Jos=E9?= de Paula List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 15 Jul 2004 05:10:18 -0000 >Number: 69085 >Category: bin >Synopsis: [patch] basic modelines for contrib/nvi >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Thu Jul 15 05:10:17 GMT 2004 >Closed-Date: >Last-Modified: >Originator: José de Paula >Release: FreeBSD 5.2.1-RELEASE-p9 i386 >Organization: >Environment: System: FreeBSD luna.pinguim 5.2.1-RELEASE-p9 FreeBSD 5.2.1-RELEASE-p9 #2: Wed Jul 7 21:58:30 BRT 2004 sombra@luna.pinguim:/usr/obj/usr/src/sys/ESPINAFRE i386 >Description: I hacked together this little patch to contrib/nvi to make it support simple modelines. The original author of nvi (Keith Bostic) says in the sources that modelines are not to be implemented because of security concerns, but I believe that implementing them after vim's style (i.e., only allowing arguments to the 'set' option in modelines) doesn't pose any security threat. Besides, I find them quite useful when editing some specific files, for which I want specific settings. Acceptable modelines are in the form: (ex/vi regex) .*[[:blank:]]\+\(vi\)\|\(ex\):options where 'options' are space- or colon-separated arguments to 'set'. >How-To-Repeat: Start nvi with 'set modeline' on your .nexrc >Fix: See patch below. --- nvi-modeline.patch begins here --- diff -Nru a/common/exf.c b/common/exf.c --- a/common/exf.c Wed Jul 14 22:22:19 2004 +++ b/common/exf.c Wed Jul 14 23:33:17 2004 @@ -42,6 +42,83 @@ #include "common.h" +char * parse_modeline(sp, line, size) + SCR *sp; + char *line; + int size; +{ + char *cmd, *ex; + int i; + + cmd = malloc(sizeof (*cmd) * size); + + if ( !(ex = strstr(line, "vi:")) && !(ex = strstr(line, "ex:")) ) + return NULL; /* no modelines here */ + if ( (ex != line) && !isblank(*(ex - 1))) + return NULL; /* no modelines here either */ + + /* What we have to do is initialize *cmd with 'set ', + * change colons to spaces and append the resulting mess + * to cmd. + */ + strcpy(cmd, "set "); + i = strlen(cmd); + ex += 3; + while(*ex != '\n' && *ex != '\r' && i < size) /* run till end of line */ + { + if(*ex == ':') + cmd[i] = ' '; + else + cmd[i] = *ex; + i++; + ex++; + } + cmd[i] = '\0'; + + return cmd; +} + +/* + * We look for a modeline, and return a cooked string ready to be + * run by ex_run_str. + * the first one is found; if there is no modeline, return NULL. + */ +char * get_modeline(sp) + SCR * sp; +{ + char *cmd, *line; + int i, lnop, linesz, lineempty; + + line = malloc(sizeof(*line) * 255); /* shall we have a modeline longer than that? */ + + if (db_last(sp, &lnop)) + return NULL; + if (lnop == 0) + return NULL; + + for (i = 1; i <= lnop && i <= 5; i++) + { + db_eget(sp, i, &line, &linesz, &lineempty); + if(lineempty) + continue; + if ( (cmd = parse_modeline(sp, line, linesz)) != NULL) + return cmd; + } + + /* just like before, but counting lines from bottom up */ + for (i = lnop; i > 0 && i >= (lnop - 5); i--) + { + db_eget(sp, i, &line, &linesz, &lineempty); + if(lineempty) + continue; + if ( (cmd = parse_modeline(sp, line, linesz)) != NULL) + return cmd; + } + + /* If we reached this point, there is nothing to return. Therefore, */ + return NULL; +} + static int file_backup __P((SCR *, char *, char *)); static void file_cinit __P((SCR *)); static void file_comment __P((SCR *)); @@ -520,6 +597,7 @@ MARK m; size_t len; int nb; + char *cmd; /* Set some basic defaults. */ sp->lno = 1; @@ -612,6 +690,14 @@ */ m.lno = sp->lno; m.cno = sp->cno; + + if (O_ISSET(sp, O_MODELINE)) { + cmd = get_modeline(sp); + if (cmd != NULL) + if (ex_run_str(sp, "modeline", cmd, strlen(cmd), 0, 0)) + return; + } + (void)mark_set(sp, ABSMARK1, &m, 0); } diff -Nru a/common/options.c b/common/options.c --- a/common/options.c Wed Jul 14 22:22:19 2004 +++ b/common/options.c Wed Jul 14 22:32:54 2004 @@ -124,7 +124,7 @@ * 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}, + {"modeline", NULL, OPT_0BOOL, 0}, /* O_MSGCAT 4.4BSD */ {"msgcat", f_msgcat, OPT_STR, 0}, /* O_NOPRINT 4.4BSD */ --- nvi-modeline.patch ends here --- >Release-Note: >Audit-Trail: >Unformatted: To: FreeBSD-gnats-submit@freebsd.org Subject: [patch] basic modelines for contrib/nvi From: José de Paula Reply-To: José de Paula Cc: X-send-pr-version: 3.113 X-GNATS-Notify: