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