xref: /netbsd-src/external/bsd/nvi/dist/ex/ex_tcl.c (revision 2f698edb5c1cb2dcd9e762b0abb50c41dde8b6b7)
1 /*	$NetBSD: ex_tcl.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
2 /*-
3  * Copyright (c) 1992, 1993, 1994
4  *	The Regents of the University of California.  All rights reserved.
5  * Copyright (c) 1992, 1993, 1994, 1995, 1996
6  *	Keith Bostic.  All rights reserved.
7  * Copyright (c) 1995
8  *	George V. Neville-Neil.  All rights reserved.
9  *
10  * See the LICENSE file for redistribution information.
11  */
12 
13 #include "config.h"
14 
15 #include <sys/cdefs.h>
16 #if 0
17 #ifndef lint
18 static const char sccsid[] = "Id: ex_tcl.c,v 8.11 2001/06/25 15:19:21 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:21 ";
19 #endif /* not lint */
20 #else
21 __RCSID("$NetBSD: ex_tcl.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
22 #endif
23 
24 #include <sys/types.h>
25 #include <sys/queue.h>
26 
27 #include <bitstring.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <termios.h>
32 #include <unistd.h>
33 
34 #include "../common/common.h"
35 
36 #ifdef HAVE_TCL_INTERP
37 #include <tcl.h>
38 #endif
39 
40 /*
41  * ex_tcl -- :[line [,line]] tcl [command]
42  *	Run a command through the tcl interpreter.
43  *
44  * PUBLIC: int ex_tcl __P((SCR*, EXCMD *));
45  */
46 int
ex_tcl(SCR * sp,EXCMD * cmdp)47 ex_tcl(SCR *sp, EXCMD *cmdp)
48 {
49 #ifdef HAVE_TCL_INTERP
50 	CHAR_T *p;
51 	GS *gp;
52 	size_t len;
53 	char buf[128];
54 
55 	/* Initialize the interpreter. */
56 	gp = sp->gp;
57 	if (gp->tcl_interp == NULL && tcl_init(gp))
58 		return (1);
59 
60 	/* Skip leading white space. */
61 	if (cmdp->argc != 0)
62 		for (p = cmdp->argv[0]->bp,
63 		    len = cmdp->argv[0]->len; len > 0; --len, ++p)
64 			if (!ISBLANK((UCHAR_T)*p))
65 				break;
66 	if (cmdp->argc == 0 || len == 0) {
67 		ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
68 		return (1);
69 	}
70 
71 	(void)snprintf(buf, sizeof(buf),
72 	    "set viScreenId %d\nset viStartLine %lu\nset viStopLine %lu",
73 	    sp->id, cmdp->addr1.lno, cmdp->addr2.lno);
74 	if (Tcl_Eval(gp->tcl_interp, buf) == TCL_OK &&
75 	    Tcl_Eval(gp->tcl_interp, cmdp->argv[0]->bp) == TCL_OK)
76 		return (0);
77 
78 	msgq(sp, M_ERR, "Tcl: %s", ((Tcl_Interp *)gp->tcl_interp)->result);
79 	return (1);
80 #else
81 	msgq(sp, M_ERR, "302|Vi was not loaded with a Tcl interpreter");
82 	return (1);
83 #endif /* HAVE_TCL_INTERP */
84 }
85