xref: /minix3/external/bsd/nvi/dist/ex/ex_util.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ex_util.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 1993, 1994
484d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
584d9c625SLionel Sambuc  * Copyright (c) 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
784d9c625SLionel Sambuc  *
884d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc  */
1084d9c625SLionel Sambuc 
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc 
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc static const char sccsid[] = "Id: ex_util.c,v 10.32 2001/06/25 15:19:21 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:21 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ex_util.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc 
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc #include <sys/stat.h>
2584d9c625SLionel Sambuc 
2684d9c625SLionel Sambuc #include <bitstring.h>
2784d9c625SLionel Sambuc #include <errno.h>
2884d9c625SLionel Sambuc #include <limits.h>
2984d9c625SLionel Sambuc #include <stdio.h>
3084d9c625SLionel Sambuc #include <stdlib.h>
3184d9c625SLionel Sambuc #include <string.h>
3284d9c625SLionel Sambuc #include <unistd.h>
3384d9c625SLionel Sambuc 
3484d9c625SLionel Sambuc #include "../common/common.h"
3584d9c625SLionel Sambuc 
3684d9c625SLionel Sambuc /*
3784d9c625SLionel Sambuc  * ex_cinit --
3884d9c625SLionel Sambuc  *	Create an EX command structure.
3984d9c625SLionel Sambuc  *
4084d9c625SLionel Sambuc  * PUBLIC: void ex_cinit __P((SCR *, EXCMD *, int, int, db_recno_t, db_recno_t, int));
4184d9c625SLionel Sambuc  */
4284d9c625SLionel Sambuc void
ex_cinit(SCR * sp,EXCMD * cmdp,int cmd_id,int naddr,db_recno_t lno1,db_recno_t lno2,int force)4384d9c625SLionel Sambuc ex_cinit(SCR *sp, EXCMD *cmdp, int cmd_id, int naddr, db_recno_t lno1, db_recno_t lno2, int force)
4484d9c625SLionel Sambuc {
4584d9c625SLionel Sambuc 	memset(cmdp, 0, sizeof(EXCMD));
4684d9c625SLionel Sambuc 	cmdp->cmd = &cmds[cmd_id];
4784d9c625SLionel Sambuc 	cmdp->addrcnt = naddr;
4884d9c625SLionel Sambuc 	cmdp->addr1.lno = lno1;
4984d9c625SLionel Sambuc 	cmdp->addr2.lno = lno2;
5084d9c625SLionel Sambuc 	cmdp->addr1.cno = cmdp->addr2.cno = 1;
5184d9c625SLionel Sambuc 	if (force)
5284d9c625SLionel Sambuc 		cmdp->iflags |= E_C_FORCE;
5384d9c625SLionel Sambuc 	(void)argv_init(sp, cmdp);
5484d9c625SLionel Sambuc }
5584d9c625SLionel Sambuc 
5684d9c625SLionel Sambuc /*
5784d9c625SLionel Sambuc  * ex_getline --
5884d9c625SLionel Sambuc  *	Return a line from the file.
5984d9c625SLionel Sambuc  *
6084d9c625SLionel Sambuc  * PUBLIC: int ex_getline __P((SCR *, FILE *, size_t *));
6184d9c625SLionel Sambuc  */
6284d9c625SLionel Sambuc int
ex_getline(SCR * sp,FILE * fp,size_t * lenp)6384d9c625SLionel Sambuc ex_getline(SCR *sp, FILE *fp, size_t *lenp)
6484d9c625SLionel Sambuc {
6584d9c625SLionel Sambuc 	EX_PRIVATE *exp;
6684d9c625SLionel Sambuc 	size_t off;
6784d9c625SLionel Sambuc 	int ch;
6884d9c625SLionel Sambuc 	char *p;
6984d9c625SLionel Sambuc 
7084d9c625SLionel Sambuc 	exp = EXP(sp);
7184d9c625SLionel Sambuc 	for (errno = 0, off = 0, p = exp->ibp;;) {
7284d9c625SLionel Sambuc 		if (off >= exp->ibp_len) {
7384d9c625SLionel Sambuc 			BINC_RETC(sp, exp->ibp, exp->ibp_len, off + 1);
7484d9c625SLionel Sambuc 			p = exp->ibp + off;
7584d9c625SLionel Sambuc 		}
7684d9c625SLionel Sambuc 		if ((ch = getc(fp)) == EOF && !feof(fp)) {
7784d9c625SLionel Sambuc 			if (errno == EINTR) {
7884d9c625SLionel Sambuc 				errno = 0;
7984d9c625SLionel Sambuc 				clearerr(fp);
8084d9c625SLionel Sambuc 				continue;
8184d9c625SLionel Sambuc 			}
8284d9c625SLionel Sambuc 			return (1);
8384d9c625SLionel Sambuc 		}
8484d9c625SLionel Sambuc 		if (ch == EOF || ch == '\n') {
8584d9c625SLionel Sambuc 			if (ch == EOF && !off)
8684d9c625SLionel Sambuc 				return (1);
8784d9c625SLionel Sambuc 			*lenp = off;
8884d9c625SLionel Sambuc 			return (0);
8984d9c625SLionel Sambuc 		}
9084d9c625SLionel Sambuc 		*p++ = ch;
9184d9c625SLionel Sambuc 		++off;
9284d9c625SLionel Sambuc 	}
9384d9c625SLionel Sambuc 	/* NOTREACHED */
9484d9c625SLionel Sambuc }
9584d9c625SLionel Sambuc 
9684d9c625SLionel Sambuc /*
9784d9c625SLionel Sambuc  * ex_ncheck --
9884d9c625SLionel Sambuc  *	Check for more files to edit.
9984d9c625SLionel Sambuc  *
10084d9c625SLionel Sambuc  * PUBLIC: int ex_ncheck __P((SCR *, int));
10184d9c625SLionel Sambuc  */
10284d9c625SLionel Sambuc int
ex_ncheck(SCR * sp,int force)10384d9c625SLionel Sambuc ex_ncheck(SCR *sp, int force)
10484d9c625SLionel Sambuc {
10584d9c625SLionel Sambuc 	char **ap;
10684d9c625SLionel Sambuc 
10784d9c625SLionel Sambuc 	/*
10884d9c625SLionel Sambuc 	 * !!!
10984d9c625SLionel Sambuc 	 * Historic practice: quit! or two quit's done in succession
11084d9c625SLionel Sambuc 	 * (where ZZ counts as a quit) didn't check for other files.
11184d9c625SLionel Sambuc 	 */
11284d9c625SLionel Sambuc 	if (!force && sp->ccnt != sp->q_ccnt + 1 &&
11384d9c625SLionel Sambuc 	    sp->cargv != NULL && sp->cargv[1] != NULL) {
11484d9c625SLionel Sambuc 		sp->q_ccnt = sp->ccnt;
11584d9c625SLionel Sambuc 
11684d9c625SLionel Sambuc 		for (ap = sp->cargv + 1; *ap != NULL; ++ap);
11784d9c625SLionel Sambuc 		msgq(sp, M_ERR,
11884d9c625SLionel Sambuc 		    "167|%zd more files to edit", (ap - sp->cargv) - 1);
11984d9c625SLionel Sambuc 
12084d9c625SLionel Sambuc 		return (1);
12184d9c625SLionel Sambuc 	}
12284d9c625SLionel Sambuc 	return (0);
12384d9c625SLionel Sambuc }
12484d9c625SLionel Sambuc 
12584d9c625SLionel Sambuc /*
12684d9c625SLionel Sambuc  * ex_init --
12784d9c625SLionel Sambuc  *	Init the screen for ex.
12884d9c625SLionel Sambuc  *
12984d9c625SLionel Sambuc  * PUBLIC: int ex_init __P((SCR *));
13084d9c625SLionel Sambuc  */
13184d9c625SLionel Sambuc int
ex_init(SCR * sp)13284d9c625SLionel Sambuc ex_init(SCR *sp)
13384d9c625SLionel Sambuc {
13484d9c625SLionel Sambuc 	GS *gp;
13584d9c625SLionel Sambuc 
13684d9c625SLionel Sambuc 	gp = sp->gp;
13784d9c625SLionel Sambuc 
13884d9c625SLionel Sambuc 	if (gp->scr_screen(sp, SC_EX))
13984d9c625SLionel Sambuc 		return (1);
14084d9c625SLionel Sambuc 	(void)gp->scr_attr(sp, SA_ALTERNATE, 0);
14184d9c625SLionel Sambuc 
14284d9c625SLionel Sambuc 	sp->rows = O_VAL(sp, O_LINES);
14384d9c625SLionel Sambuc 	sp->cols = O_VAL(sp, O_COLUMNS);
14484d9c625SLionel Sambuc 
14584d9c625SLionel Sambuc 	F_CLR(sp, SC_VI);
14684d9c625SLionel Sambuc 	F_SET(sp, SC_EX | SC_SCR_EX);
14784d9c625SLionel Sambuc 	return (0);
14884d9c625SLionel Sambuc }
14984d9c625SLionel Sambuc 
15084d9c625SLionel Sambuc /*
15184d9c625SLionel Sambuc  * ex_emsg --
15284d9c625SLionel Sambuc  *	Display a few common ex and vi error messages.
15384d9c625SLionel Sambuc  *
15484d9c625SLionel Sambuc  * PUBLIC: void ex_wemsg __P((SCR *, const CHAR_T *, exm_t));
15584d9c625SLionel Sambuc  */
15684d9c625SLionel Sambuc void
ex_wemsg(SCR * sp,const CHAR_T * p,exm_t which)15784d9c625SLionel Sambuc ex_wemsg(SCR* sp, const CHAR_T *p, exm_t which)
15884d9c625SLionel Sambuc {
15984d9c625SLionel Sambuc 	const char *np;
16084d9c625SLionel Sambuc 	size_t nlen;
16184d9c625SLionel Sambuc 
16284d9c625SLionel Sambuc 	if (p) INT2CHAR(sp, p, STRLEN(p), np, nlen);
16384d9c625SLionel Sambuc 	else np = NULL;
16484d9c625SLionel Sambuc 	ex_emsg(sp, np, which);
16584d9c625SLionel Sambuc }
16684d9c625SLionel Sambuc 
16784d9c625SLionel Sambuc /*
16884d9c625SLionel Sambuc  * ex_emsg --
16984d9c625SLionel Sambuc  *	Display a few common ex and vi error messages.
17084d9c625SLionel Sambuc  *
17184d9c625SLionel Sambuc  * PUBLIC: void ex_emsg __P((SCR *, const char *, exm_t));
17284d9c625SLionel Sambuc  */
17384d9c625SLionel Sambuc void
ex_emsg(SCR * sp,const char * p,exm_t which)17484d9c625SLionel Sambuc ex_emsg(SCR *sp, const char *p, exm_t which)
17584d9c625SLionel Sambuc {
17684d9c625SLionel Sambuc 	switch (which) {
17784d9c625SLionel Sambuc 	case EXM_EMPTYBUF:
17884d9c625SLionel Sambuc 		msgq(sp, M_ERR, "168|Buffer %s is empty", p);
17984d9c625SLionel Sambuc 		break;
18084d9c625SLionel Sambuc 	case EXM_FILECOUNT:
18184d9c625SLionel Sambuc 		 msgq_str(sp, M_ERR, p,
18284d9c625SLionel Sambuc 		     "144|%s: expanded into too many file names");
18384d9c625SLionel Sambuc 		break;
18484d9c625SLionel Sambuc 	case EXM_LOCKED:
18584d9c625SLionel Sambuc 		msgq(sp, M_ERR,
18684d9c625SLionel Sambuc 		    "Command failed, buffer is locked");
18784d9c625SLionel Sambuc 		break;
18884d9c625SLionel Sambuc 	case EXM_NOCANON:
18984d9c625SLionel Sambuc 		msgq(sp, M_ERR,
19084d9c625SLionel Sambuc 		    "283|The %s command requires the ex terminal interface", p);
19184d9c625SLionel Sambuc 		break;
19284d9c625SLionel Sambuc 	case EXM_NOCANON_F:
19384d9c625SLionel Sambuc 		msgq(sp, M_ERR,
19484d9c625SLionel Sambuc 		    "272|That form of %s requires the ex terminal interface",
19584d9c625SLionel Sambuc 		    p);
19684d9c625SLionel Sambuc 		break;
19784d9c625SLionel Sambuc 	case EXM_NOFILEYET:
19884d9c625SLionel Sambuc 		if (p == NULL)
19984d9c625SLionel Sambuc 			msgq(sp, M_ERR,
20084d9c625SLionel Sambuc 			    "274|Command failed, no file read in yet.");
20184d9c625SLionel Sambuc 		else
20284d9c625SLionel Sambuc 			msgq(sp, M_ERR,
20384d9c625SLionel Sambuc 	"173|The %s command requires that a file have already been read in", p);
20484d9c625SLionel Sambuc 		break;
20584d9c625SLionel Sambuc 	case EXM_NOPREVBUF:
20684d9c625SLionel Sambuc 		msgq(sp, M_ERR, "171|No previous buffer to execute");
20784d9c625SLionel Sambuc 		break;
20884d9c625SLionel Sambuc 	case EXM_NOPREVRE:
20984d9c625SLionel Sambuc 		msgq(sp, M_ERR, "172|No previous regular expression");
21084d9c625SLionel Sambuc 		break;
21184d9c625SLionel Sambuc 	case EXM_NOSUSPEND:
21284d9c625SLionel Sambuc 		msgq(sp, M_ERR, "230|This screen may not be suspended");
21384d9c625SLionel Sambuc 		break;
21484d9c625SLionel Sambuc 	case EXM_SECURE:
21584d9c625SLionel Sambuc 		msgq(sp, M_ERR,
21684d9c625SLionel Sambuc "290|The %s command is not supported when the secure edit option is set", p);
21784d9c625SLionel Sambuc 		break;
21884d9c625SLionel Sambuc 	case EXM_SECURE_F:
21984d9c625SLionel Sambuc 		msgq(sp, M_ERR,
22084d9c625SLionel Sambuc "284|That form of %s is not supported when the secure edit option is set", p);
22184d9c625SLionel Sambuc 		break;
22284d9c625SLionel Sambuc 	case EXM_USAGE:
22384d9c625SLionel Sambuc 		msgq(sp, M_ERR, "174|Usage: %s", p);
22484d9c625SLionel Sambuc 		break;
22584d9c625SLionel Sambuc 	}
22684d9c625SLionel Sambuc }
227