1*0a6a1f1dSLionel Sambuc /* $NetBSD: ex_tcl.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc * Copyright (c) 1992, 1993, 1994
484d9c625SLionel Sambuc * The Regents of the University of California. All rights reserved.
584d9c625SLionel Sambuc * Copyright (c) 1992, 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc * Keith Bostic. All rights reserved.
784d9c625SLionel Sambuc * Copyright (c) 1995
884d9c625SLionel Sambuc * George V. Neville-Neil. All rights reserved.
984d9c625SLionel Sambuc *
1084d9c625SLionel Sambuc * See the LICENSE file for redistribution information.
1184d9c625SLionel Sambuc */
1284d9c625SLionel Sambuc
1384d9c625SLionel Sambuc #include "config.h"
1484d9c625SLionel Sambuc
15*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
16*0a6a1f1dSLionel Sambuc #if 0
1784d9c625SLionel Sambuc #ifndef lint
1884d9c625SLionel Sambuc 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 ";
1984d9c625SLionel Sambuc #endif /* not lint */
20*0a6a1f1dSLionel Sambuc #else
21*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ex_tcl.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
22*0a6a1f1dSLionel Sambuc #endif
2384d9c625SLionel Sambuc
2484d9c625SLionel Sambuc #include <sys/types.h>
2584d9c625SLionel Sambuc #include <sys/queue.h>
2684d9c625SLionel Sambuc
2784d9c625SLionel Sambuc #include <bitstring.h>
2884d9c625SLionel Sambuc #include <limits.h>
2984d9c625SLionel Sambuc #include <stdio.h>
3084d9c625SLionel Sambuc #include <string.h>
3184d9c625SLionel Sambuc #include <termios.h>
3284d9c625SLionel Sambuc #include <unistd.h>
3384d9c625SLionel Sambuc
3484d9c625SLionel Sambuc #include "../common/common.h"
3584d9c625SLionel Sambuc
3684d9c625SLionel Sambuc #ifdef HAVE_TCL_INTERP
3784d9c625SLionel Sambuc #include <tcl.h>
3884d9c625SLionel Sambuc #endif
3984d9c625SLionel Sambuc
4084d9c625SLionel Sambuc /*
4184d9c625SLionel Sambuc * ex_tcl -- :[line [,line]] tcl [command]
4284d9c625SLionel Sambuc * Run a command through the tcl interpreter.
4384d9c625SLionel Sambuc *
4484d9c625SLionel Sambuc * PUBLIC: int ex_tcl __P((SCR*, EXCMD *));
4584d9c625SLionel Sambuc */
4684d9c625SLionel Sambuc int
ex_tcl(SCR * sp,EXCMD * cmdp)4784d9c625SLionel Sambuc ex_tcl(SCR *sp, EXCMD *cmdp)
4884d9c625SLionel Sambuc {
4984d9c625SLionel Sambuc #ifdef HAVE_TCL_INTERP
5084d9c625SLionel Sambuc CHAR_T *p;
5184d9c625SLionel Sambuc GS *gp;
5284d9c625SLionel Sambuc size_t len;
5384d9c625SLionel Sambuc char buf[128];
5484d9c625SLionel Sambuc
5584d9c625SLionel Sambuc /* Initialize the interpreter. */
5684d9c625SLionel Sambuc gp = sp->gp;
5784d9c625SLionel Sambuc if (gp->tcl_interp == NULL && tcl_init(gp))
5884d9c625SLionel Sambuc return (1);
5984d9c625SLionel Sambuc
6084d9c625SLionel Sambuc /* Skip leading white space. */
6184d9c625SLionel Sambuc if (cmdp->argc != 0)
6284d9c625SLionel Sambuc for (p = cmdp->argv[0]->bp,
6384d9c625SLionel Sambuc len = cmdp->argv[0]->len; len > 0; --len, ++p)
6484d9c625SLionel Sambuc if (!ISBLANK((UCHAR_T)*p))
6584d9c625SLionel Sambuc break;
6684d9c625SLionel Sambuc if (cmdp->argc == 0 || len == 0) {
6784d9c625SLionel Sambuc ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
6884d9c625SLionel Sambuc return (1);
6984d9c625SLionel Sambuc }
7084d9c625SLionel Sambuc
7184d9c625SLionel Sambuc (void)snprintf(buf, sizeof(buf),
7284d9c625SLionel Sambuc "set viScreenId %d\nset viStartLine %lu\nset viStopLine %lu",
7384d9c625SLionel Sambuc sp->id, cmdp->addr1.lno, cmdp->addr2.lno);
7484d9c625SLionel Sambuc if (Tcl_Eval(gp->tcl_interp, buf) == TCL_OK &&
7584d9c625SLionel Sambuc Tcl_Eval(gp->tcl_interp, cmdp->argv[0]->bp) == TCL_OK)
7684d9c625SLionel Sambuc return (0);
7784d9c625SLionel Sambuc
7884d9c625SLionel Sambuc msgq(sp, M_ERR, "Tcl: %s", ((Tcl_Interp *)gp->tcl_interp)->result);
7984d9c625SLionel Sambuc return (1);
8084d9c625SLionel Sambuc #else
8184d9c625SLionel Sambuc msgq(sp, M_ERR, "302|Vi was not loaded with a Tcl interpreter");
8284d9c625SLionel Sambuc return (1);
8384d9c625SLionel Sambuc #endif /* HAVE_TCL_INTERP */
8484d9c625SLionel Sambuc }
85