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.
13 static const char sccsid
[] = "$Id: ex_read.c,v 10.38 1996/08/12 20:36:59 bostic Exp $ (Berkeley) $Date: 1996/08/12 20:36:59 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
21 #include <bitstring.h>
29 #include "../common/common.h"
33 * ex_read -- :read [file]
35 * Read from a file or utility.
38 * Historical vi wouldn't undo a filter read, for no apparent reason.
40 * PUBLIC: int ex_read __P((SCR *, EXCMD *));
47 enum { R_ARG
, R_EXPANDARG
, R_FILTER
} which
;
63 * 0 args: read the current pathname.
64 * 1 args: check for "read !arg".
71 arg
= cmdp
->argv
[0]->bp
;
72 arglen
= cmdp
->argv
[0]->len
;
78 /* Secure means no shell access. */
79 if (O_ISSET(sp
, O_SECURE
)) {
80 ex_emsg(sp
, cmdp
->cmd
->name
, EXM_SECURE_F
);
91 /* Load a temporary file if no file being edited. */
93 if ((frp
= file_add(sp
, NULL
)) == NULL
)
95 if (file_init(sp
, frp
, NULL
, 0))
102 * File name and bang expand the user's argument. If
103 * we don't get an additional argument, it's illegal.
106 if (argv_exp1(sp
, cmdp
, arg
, arglen
, 1))
108 if (argc
== cmdp
->argc
) {
109 ex_emsg(sp
, cmdp
->cmd
->usage
, EXM_USAGE
);
112 argc
= cmdp
->argc
- 1;
114 /* Set the last bang command. */
116 if (exp
->lastbcomm
!= NULL
)
117 free(exp
->lastbcomm
);
118 if ((exp
->lastbcomm
=
119 strdup(cmdp
->argv
[argc
]->bp
)) == NULL
) {
120 msgq(sp
, M_SYSERR
, NULL
);
125 * Vi redisplayed the user's argument if it changed, ex
126 * always displayed a !, plus the user's argument if it
129 if (F_ISSET(sp
, SC_VI
)) {
130 if (F_ISSET(cmdp
, E_MODIFY
))
131 (void)vs_update(sp
, "!", cmdp
->argv
[argc
]->bp
);
133 if (F_ISSET(cmdp
, E_MODIFY
))
135 "!%s\n", cmdp
->argv
[argc
]->bp
);
137 (void)ex_puts(sp
, "!\n");
142 * Historically, filter reads as the first ex command didn't
143 * wait for the user. If SC_SCR_EXWROTE not already set, set
144 * the don't-wait flag.
146 if (!F_ISSET(sp
, SC_SCR_EXWROTE
))
147 F_SET(sp
, SC_EX_WAIT_NO
);
150 * Switch into ex canonical mode. The reason to restore the
151 * original terminal modes for read filters is so that users
152 * can do things like ":r! cat /dev/tty".
155 * We do not output an extra <newline>, so that we don't touch
156 * the screen on a normal read.
158 if (F_ISSET(sp
, SC_VI
)) {
159 if (gp
->scr_screen(sp
, SC_EX
)) {
160 ex_emsg(sp
, cmdp
->cmd
->name
, EXM_NOCANON_F
);
165 * Historically, the read command doesn't switch to
166 * the alternate X11 xterm screen, if doing a filter
167 * read -- don't set SA_ALTERNATE.
169 F_SET(sp
, SC_SCR_EX
| SC_SCR_EXWROTE
);
172 if (ex_filter(sp
, cmdp
, &cmdp
->addr1
,
173 NULL
, &rm
, cmdp
->argv
[argc
]->bp
, FILTER_READ
))
176 /* The filter version of read set the autoprint flag. */
177 F_SET(cmdp
, E_AUTOPRINT
);
180 * If in vi mode, move to the first nonblank. Might have
181 * switched into ex mode, so saved the original SC_VI value.
184 if (F_ISSET(sp
, SC_VI
)) {
186 (void)nonblank(sp
, sp
->lno
, &sp
->cno
);
190 name
= sp
->frp
->name
;
193 if (argv_exp2(sp
, cmdp
, arg
, arglen
))
196 * 0 args: impossible.
197 * 1 args: impossible (I hope).
199 * >2 args: object, too many args.
201 * The 1 args case depends on the argv_sexp() function refusing
202 * to return success without at least one non-blank character.
204 switch (cmdp
->argc
) {
210 name
= cmdp
->argv
[1]->bp
;
213 * Historically, the read and write commands renamed
214 * "unnamed" files, or, if the file had a name, set
215 * the alternate file name.
217 if (F_ISSET(sp
->frp
, FR_TMPFILE
) &&
218 !F_ISSET(sp
->frp
, FR_EXNAMED
)) {
219 if ((p
= v_strdup(sp
, cmdp
->argv
[1]->bp
,
220 cmdp
->argv
[1]->len
)) != NULL
) {
225 * The file has a real name, it's no longer a
226 * temporary, clear the temporary file flags.
228 F_CLR(sp
->frp
, FR_TMPEXIT
| FR_TMPFILE
);
229 F_SET(sp
->frp
, FR_NAMECHANGE
| FR_EXNAMED
);
231 /* Notify the screen. */
232 (void)sp
->gp
->scr_rename(sp
, sp
->frp
->name
, 1);
234 set_alt_name(sp
, name
);
237 ex_emsg(sp
, cmdp
->argv
[0]->bp
, EXM_FILECOUNT
);
246 * Historically, vi did not permit reads from non-regular files, nor
247 * did it distinguish between "read !" and "read!", so there was no
248 * way to "force" it. We permit reading from named pipes too, since
249 * they didn't exist when the original implementation of vi was done
250 * and they seem a reasonable addition.
252 if ((fp
= fopen(name
, "r")) == NULL
|| fstat(fileno(fp
), &sb
)) {
253 msgq_str(sp
, M_SYSERR
, name
, "%s");
256 if (!S_ISFIFO(sb
.st_mode
) && !S_ISREG(sb
.st_mode
)) {
259 "145|Only regular files and named pipes may be read");
263 /* Try and get a lock. */
264 if (file_lock(sp
, NULL
, NULL
, fileno(fp
), 0) == LOCK_UNAVAIL
)
265 msgq(sp
, M_ERR
, "146|%s: read lock was unavailable", name
);
267 rval
= ex_readfp(sp
, name
, fp
, &cmdp
->addr1
, &nlines
, 0);
270 * In vi, set the cursor to the first line read in, if anything read
271 * in, otherwise, the address. (Historic vi set it to the line after
272 * the address regardless, but since that line may not exist we don't
275 * In ex, set the cursor to the last line read in, if anything read in,
276 * otherwise, the address.
278 if (F_ISSET(sp
, SC_VI
)) {
279 sp
->lno
= cmdp
->addr1
.lno
;
283 sp
->lno
= cmdp
->addr1
.lno
+ nlines
;
289 * Read lines into the file.
291 * PUBLIC: int ex_readfp __P((SCR *, char *, FILE *, MARK *, recno_t *, int));
294 ex_readfp(sp
, name
, fp
, fm
, nlinesp
, silent
)
306 u_long ccnt
; /* XXX: can't print off_t portably. */
314 * Add in the lines from the output. Insertion starts at the line
315 * following the address.
319 p
= "147|Reading...";
320 for (lno
= fm
->lno
; !ex_getline(sp
, fp
, &len
); ++lno
, ++lcnt
) {
321 if ((lcnt
+ 1) % INTERRUPT_CHECK
== 0) {
326 p
== NULL
? BUSY_UPDATE
: BUSY_ON
);
330 if (db_append(sp
, 1, lno
, exp
->ibp
, len
))
335 if (ferror(fp
) || fclose(fp
))
338 /* Return the number of lines read in. */
343 p
= msg_print(sp
, name
, &nf
);
345 "148|%s: %lu lines, %lu characters", p
, lcnt
, ccnt
);
347 FREE_SPACE(sp
, p
, 0);
352 err
: msgq_str(sp
, M_SYSERR
, name
, "%s");
358 gp
->scr_busy(sp
, NULL
, BUSY_OFF
);