use three-argument AC_DEFINE instead of acconfig.h
[nvi.git] / ex / ex_read.c
blobe55eee0c1180c2d0f43d4c5a2fe682f8fdf18d08
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.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_read.c,v 10.44 2001/06/25 15:19:19 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:19 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
21 #include <bitstring.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "../common/common.h"
30 #include "../vi/vi.h"
33 * ex_read -- :read [file]
34 * :read [!cmd]
35 * Read from a file or utility.
37 * !!!
38 * Historical vi wouldn't undo a filter read, for no apparent reason.
40 * PUBLIC: int ex_read __P((SCR *, EXCMD *));
42 int
43 ex_read(SCR *sp, EXCMD *cmdp)
45 enum { R_ARG, R_EXPANDARG, R_FILTER } which;
46 struct stat sb;
47 CHAR_T *arg;
48 char *name;
49 size_t nlen;
50 EX_PRIVATE *exp;
51 FILE *fp;
52 FREF *frp;
53 GS *gp;
54 MARK rm;
55 db_recno_t nlines;
56 size_t arglen;
57 int argc, rval;
58 char *p;
59 char *np;
61 gp = sp->gp;
64 * 0 args: read the current pathname.
65 * 1 args: check for "read !arg".
67 switch (cmdp->argc) {
68 case 0:
69 which = R_ARG;
70 break;
71 case 1:
72 arg = cmdp->argv[0]->bp;
73 arglen = cmdp->argv[0]->len;
74 if (*arg == '!') {
75 ++arg;
76 --arglen;
77 which = R_FILTER;
79 /* Secure means no shell access. */
80 if (O_ISSET(sp, O_SECURE)) {
81 ex_wemsg(sp, cmdp->cmd->name, EXM_SECURE_F);
82 return (1);
84 } else
85 which = R_EXPANDARG;
86 break;
87 default:
88 abort();
89 /* NOTREACHED */
92 /* Load a temporary file if no file being edited. */
93 if (sp->ep == NULL) {
94 if ((frp = file_add(sp, NULL)) == NULL)
95 return (1);
96 if (file_init(sp, frp, NULL, 0))
97 return (1);
100 switch (which) {
101 case R_FILTER:
103 * File name and bang expand the user's argument. If
104 * we don't get an additional argument, it's illegal.
106 argc = cmdp->argc;
107 if (argv_exp1(sp, cmdp, arg, arglen, 1))
108 return (1);
109 if (argc == cmdp->argc) {
110 ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
111 return (1);
113 argc = cmdp->argc - 1;
115 /* Set the last bang command. */
116 exp = EXP(sp);
117 if (exp->lastbcomm != NULL)
118 free(exp->lastbcomm);
119 if ((exp->lastbcomm =
120 v_wstrdup(sp, cmdp->argv[argc]->bp,
121 cmdp->argv[argc]->len)) == NULL) {
122 msgq(sp, M_SYSERR, NULL);
123 return (1);
127 * Vi redisplayed the user's argument if it changed, ex
128 * always displayed a !, plus the user's argument if it
129 * changed.
131 if (F_ISSET(sp, SC_VI)) {
132 if (F_ISSET(cmdp, E_MODIFY))
133 (void)vs_update(sp, "!", cmdp->argv[argc]->bp);
134 } else {
135 if (F_ISSET(cmdp, E_MODIFY))
136 (void)ex_printf(sp,
137 "!%s\n", cmdp->argv[argc]->bp);
138 else
139 (void)ex_puts(sp, "!\n");
140 (void)ex_fflush(sp);
144 * Historically, filter reads as the first ex command didn't
145 * wait for the user. If SC_SCR_EXWROTE not already set, set
146 * the don't-wait flag.
148 if (!F_ISSET(sp, SC_SCR_EXWROTE))
149 F_SET(sp, SC_EX_WAIT_NO);
152 * Switch into ex canonical mode. The reason to restore the
153 * original terminal modes for read filters is so that users
154 * can do things like ":r! cat /dev/tty".
156 * !!!
157 * We do not output an extra <newline>, so that we don't touch
158 * the screen on a normal read.
160 if (F_ISSET(sp, SC_VI)) {
161 if (gp->scr_screen(sp, SC_EX)) {
162 ex_wemsg(sp, cmdp->cmd->name, EXM_NOCANON_F);
163 return (1);
166 * !!!
167 * Historically, the read command doesn't switch to
168 * the alternate X11 xterm screen, if doing a filter
169 * read -- don't set SA_ALTERNATE.
171 F_SET(sp, SC_SCR_EX | SC_SCR_EXWROTE);
174 if (ex_filter(sp, cmdp, &cmdp->addr1,
175 NULL, &rm, cmdp->argv[argc]->bp, FILTER_READ))
176 return (1);
178 /* The filter version of read set the autoprint flag. */
179 F_SET(cmdp, E_AUTOPRINT);
182 * If in vi mode, move to the first nonblank. Might have
183 * switched into ex mode, so saved the original SC_VI value.
185 sp->lno = rm.lno;
186 if (F_ISSET(sp, SC_VI)) {
187 sp->cno = 0;
188 (void)nonblank(sp, sp->lno, &sp->cno);
190 return (0);
191 case R_ARG:
192 name = sp->frp->name;
193 break;
194 case R_EXPANDARG:
195 if (argv_exp2(sp, cmdp, arg, arglen))
196 return (1);
198 * 0 args: impossible.
199 * 1 args: impossible (I hope).
200 * 2 args: read it.
201 * >2 args: object, too many args.
203 * The 1 args case depends on the argv_sexp() function refusing
204 * to return success without at least one non-blank character.
206 switch (cmdp->argc) {
207 case 0:
208 case 1:
209 abort();
210 /* NOTREACHED */
211 case 2:
212 INT2CHAR(sp, cmdp->argv[1]->bp, cmdp->argv[1]->len + 1,
213 name, nlen);
215 * !!!
216 * Historically, the read and write commands renamed
217 * "unnamed" files, or, if the file had a name, set
218 * the alternate file name.
220 if (F_ISSET(sp->frp, FR_TMPFILE) &&
221 !F_ISSET(sp->frp, FR_EXNAMED)) {
222 if ((p = strdup(name)) != NULL) {
223 free(sp->frp->name);
224 sp->frp->name = p;
227 * The file has a real name, it's no longer a
228 * temporary, clear the temporary file flags.
230 F_CLR(sp->frp, FR_TMPEXIT | FR_TMPFILE);
231 F_SET(sp->frp, FR_NAMECHANGE | FR_EXNAMED);
233 /* Notify the screen. */
234 (void)sp->gp->scr_rename(sp, sp->frp->name, 1);
235 name = sp->frp->name;
236 } else {
237 set_alt_name(sp, name);
238 name = sp->alt_name;
240 break;
241 default:
242 ex_wemsg(sp, cmdp->argv[0]->bp, EXM_FILECOUNT);
243 return (1);
246 break;
250 * !!!
251 * Historically, vi did not permit reads from non-regular files, nor
252 * did it distinguish between "read !" and "read!", so there was no
253 * way to "force" it. We permit reading from named pipes too, since
254 * they didn't exist when the original implementation of vi was done
255 * and they seem a reasonable addition.
257 if ((fp = fopen(name, "r")) == NULL || fstat(fileno(fp), &sb)) {
258 msgq_str(sp, M_SYSERR, name, "%s");
259 return (1);
261 if (!S_ISFIFO(sb.st_mode) && !S_ISREG(sb.st_mode)) {
262 (void)fclose(fp);
263 msgq(sp, M_ERR,
264 "145|Only regular files and named pipes may be read");
265 return (1);
268 /* Try and get a lock. */
269 if (file_lock(sp, NULL, NULL, fileno(fp), 0) == LOCK_UNAVAIL)
270 msgq(sp, M_ERR, "146|%s: read lock was unavailable", name);
272 rval = ex_readfp(sp, name, fp, &cmdp->addr1, &nlines, 0);
275 * In vi, set the cursor to the first line read in, if anything read
276 * in, otherwise, the address. (Historic vi set it to the line after
277 * the address regardless, but since that line may not exist we don't
278 * bother.)
280 * In ex, set the cursor to the last line read in, if anything read in,
281 * otherwise, the address.
283 if (F_ISSET(sp, SC_VI)) {
284 sp->lno = cmdp->addr1.lno;
285 if (nlines)
286 ++sp->lno;
287 } else
288 sp->lno = cmdp->addr1.lno + nlines;
289 return (rval);
293 * ex_readfp --
294 * Read lines into the file.
296 * PUBLIC: int ex_readfp __P((SCR *, char *, FILE *, MARK *, db_recno_t *, int));
299 ex_readfp(SCR *sp, char *name, FILE *fp, MARK *fm, db_recno_t *nlinesp, int silent)
301 EX_PRIVATE *exp;
302 GS *gp;
303 db_recno_t lcnt, lno;
304 size_t len;
305 u_long ccnt; /* XXX: can't print off_t portably. */
306 int nf, rval;
307 char *p;
308 size_t wlen;
309 CHAR_T *wp;
311 gp = sp->gp;
312 exp = EXP(sp);
315 * Add in the lines from the output. Insertion starts at the line
316 * following the address.
318 ccnt = 0;
319 lcnt = 0;
320 p = "147|Reading...";
321 for (lno = fm->lno; !ex_getline(sp, fp, &len); ++lno, ++lcnt) {
322 if ((lcnt + 1) % INTERRUPT_CHECK == 0) {
323 if (INTERRUPTED(sp))
324 break;
325 if (!silent) {
326 gp->scr_busy(sp, p,
327 p == NULL ? BUSY_UPDATE : BUSY_ON);
328 p = NULL;
331 FILE2INT5(sp, exp->ibcw, exp->ibp, len, wp, wlen);
332 if (db_append(sp, 1, lno, wp, wlen))
333 goto err;
334 ccnt += len;
337 if (ferror(fp) || fclose(fp))
338 goto err;
340 /* Return the number of lines read in. */
341 if (nlinesp != NULL)
342 *nlinesp = lcnt;
344 if (!silent) {
345 p = msg_print(sp, name, &nf);
346 msgq(sp, M_INFO,
347 "148|%s: %lu lines, %lu characters", p, lcnt, ccnt);
348 if (nf)
349 FREE_SPACE(sp, p, 0);
352 rval = 0;
353 if (0) {
354 err: msgq_str(sp, M_SYSERR, name, "%s");
355 (void)fclose(fp);
356 rval = 1;
359 if (!silent)
360 gp->scr_busy(sp, NULL, BUSY_OFF);
361 return (rval);