1/*-
2 * Copyright (c) 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 *	Keith Bostic.  All rights reserved.
6 *
7 * See the LICENSE file for redistribution information.
8 */
9
10#include "config.h"
11
12#include <sys/types.h>
13#include <sys/queue.h>
14#include <sys/stat.h>
15
16#include <bitstring.h>
17#include <errno.h>
18#include <limits.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
24#include "../common/common.h"
25
26/*
27 * ex_cinit --
28 *	Create an EX command structure.
29 *
30 * PUBLIC: void ex_cinit(SCR *, EXCMD *, int, int, recno_t, recno_t, int);
31 */
32void
33ex_cinit(SCR *sp, EXCMD *cmdp, int cmd_id, int naddr, recno_t lno1, recno_t lno2, int force)
34{
35	memset(cmdp, 0, sizeof(EXCMD));
36	cmdp->cmd = &cmds[cmd_id];
37	cmdp->addrcnt = naddr;
38	cmdp->addr1.lno = lno1;
39	cmdp->addr2.lno = lno2;
40	cmdp->addr1.cno = cmdp->addr2.cno = 1;
41	if (force)
42		cmdp->iflags |= E_C_FORCE;
43	(void)argv_init(sp, cmdp);
44}
45
46/*
47 * ex_getline --
48 *	Return a line from the file.
49 *
50 * PUBLIC: int ex_getline(SCR *, FILE *, size_t *);
51 */
52int
53ex_getline(SCR *sp, FILE *fp, size_t *lenp)
54{
55	EX_PRIVATE *exp;
56	size_t off;
57	int ch;
58	char *p;
59
60	exp = EXP(sp);
61	for (errno = 0, off = 0, p = exp->ibp;;) {
62		if (off >= exp->ibp_len) {
63			BINC_RETC(sp, exp->ibp, exp->ibp_len, off + 1);
64			p = exp->ibp + off;
65		}
66		if ((ch = getc(fp)) == EOF && !feof(fp)) {
67			if (errno == EINTR) {
68				errno = 0;
69				clearerr(fp);
70				continue;
71			}
72			return (1);
73		}
74		if (ch == EOF || ch == '\n') {
75			if (ch == EOF && !off)
76				return (1);
77			*lenp = off;
78			return (0);
79		}
80		*p++ = ch;
81		++off;
82	}
83	/* NOTREACHED */
84}
85
86/*
87 * ex_ncheck --
88 *	Check for more files to edit.
89 *
90 * PUBLIC: int ex_ncheck(SCR *, int);
91 */
92int
93ex_ncheck(SCR *sp, int force)
94{
95	char **ap;
96
97	/*
98	 * !!!
99	 * Historic practice: quit! or two quit's done in succession
100	 * (where ZZ counts as a quit) didn't check for other files.
101	 */
102	if (!force && sp->ccnt != sp->q_ccnt + 1 &&
103	    sp->cargv != NULL && sp->cargv[1] != NULL) {
104		sp->q_ccnt = sp->ccnt;
105
106		for (ap = sp->cargv + 1; *ap != NULL; ++ap);
107		msgq(sp, M_ERR,
108		    "167|%d more files to edit", (int)(ap - sp->cargv) - 1);
109
110		return (1);
111	}
112	return (0);
113}
114
115/*
116 * ex_init --
117 *	Init the screen for ex.
118 *
119 * PUBLIC: int ex_init(SCR *);
120 */
121int
122ex_init(SCR *sp)
123{
124	GS *gp;
125
126	gp = sp->gp;
127
128	if (gp->scr_screen(sp, SC_EX))
129		return (1);
130	(void)gp->scr_attr(sp, SA_ALTERNATE, 0);
131
132	sp->rows = O_VAL(sp, O_LINES);
133	sp->cols = O_VAL(sp, O_COLUMNS);
134
135	F_CLR(sp, SC_VI);
136	F_SET(sp, SC_EX | SC_SCR_EX);
137	return (0);
138}
139
140/*
141 * ex_emsg --
142 *	Display a few common ex and vi error messages.
143 *
144 * PUBLIC: void ex_wemsg(SCR *, CHAR_T *, exm_t);
145 */
146void
147ex_wemsg(SCR* sp, CHAR_T *p, exm_t which)
148{
149	char *np;
150	size_t nlen;
151
152	if (p) INT2CHAR(sp, p, STRLEN(p), np, nlen);
153	else np = NULL;
154	ex_emsg(sp, np, which);
155}
156
157/*
158 * ex_emsg --
159 *	Display a few common ex and vi error messages.
160 *
161 * PUBLIC: void ex_emsg(SCR *, char *, exm_t);
162 */
163void
164ex_emsg(SCR *sp, char *p, exm_t which)
165{
166	switch (which) {
167	case EXM_EMPTYBUF:
168		msgq(sp, M_ERR, "168|Buffer %s is empty", p);
169		break;
170	case EXM_FILECOUNT:
171		 msgq_str(sp, M_ERR, p,
172		     "144|%s: expanded into too many file names");
173		break;
174	case EXM_NOCANON:
175		msgq(sp, M_ERR,
176		    "283|The %s command requires the ex terminal interface", p);
177		break;
178	case EXM_NOCANON_F:
179		msgq(sp, M_ERR,
180		    "272|That form of %s requires the ex terminal interface",
181		    p);
182		break;
183	case EXM_NOFILEYET:
184		if (p == NULL)
185			msgq(sp, M_ERR,
186			    "274|Command failed, no file read in yet.");
187		else
188			msgq(sp, M_ERR,
189	"173|The %s command requires that a file have already been read in", p);
190		break;
191	case EXM_NOPREVBUF:
192		msgq(sp, M_ERR, "171|No previous buffer to execute");
193		break;
194	case EXM_NOPREVRE:
195		msgq(sp, M_ERR, "172|No previous regular expression");
196		break;
197	case EXM_NOSUSPEND:
198		msgq(sp, M_ERR, "230|This screen may not be suspended");
199		break;
200	case EXM_SECURE:
201		msgq(sp, M_ERR,
202"290|The %s command is not supported when the secure edit option is set", p);
203		break;
204	case EXM_SECURE_F:
205		msgq(sp, M_ERR,
206"284|That form of %s is not supported when the secure edit option is set", p);
207		break;
208	case EXM_USAGE:
209		msgq(sp, M_ERR, "174|Usage: %s", p);
210		break;
211	}
212}
213