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.43 2001/06/10 10:23:44 skimo Exp $ (Berkeley) $Date: 2001/06/10 10:23:44 $";
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
;
66 * 0 args: read the current pathname.
67 * 1 args: check for "read !arg".
74 arg
= cmdp
->argv
[0]->bp
;
75 arglen
= cmdp
->argv
[0]->len
;
81 /* Secure means no shell access. */
82 if (O_ISSET(sp
, O_SECURE
)) {
83 ex_wemsg(sp
, cmdp
->cmd
->name
, EXM_SECURE_F
);
94 /* Load a temporary file if no file being edited. */
96 if ((frp
= file_add(sp
, NULL
)) == NULL
)
98 if (file_init(sp
, frp
, NULL
, 0))
105 * File name and bang expand the user's argument. If
106 * we don't get an additional argument, it's illegal.
109 if (argv_exp1(sp
, cmdp
, arg
, arglen
, 1))
111 if (argc
== cmdp
->argc
) {
112 ex_emsg(sp
, cmdp
->cmd
->usage
, EXM_USAGE
);
115 argc
= cmdp
->argc
- 1;
117 /* Set the last bang command. */
119 if (exp
->lastbcomm
!= NULL
)
120 free(exp
->lastbcomm
);
121 if ((exp
->lastbcomm
=
122 v_wstrdup(sp
, cmdp
->argv
[argc
]->bp
,
123 cmdp
->argv
[argc
]->len
)) == NULL
) {
124 msgq(sp
, M_SYSERR
, NULL
);
129 * Vi redisplayed the user's argument if it changed, ex
130 * always displayed a !, plus the user's argument if it
133 if (F_ISSET(sp
, SC_VI
)) {
134 if (F_ISSET(cmdp
, E_MODIFY
))
135 (void)vs_update(sp
, "!", cmdp
->argv
[argc
]->bp
);
137 if (F_ISSET(cmdp
, E_MODIFY
))
139 "!%s\n", cmdp
->argv
[argc
]->bp
);
141 (void)ex_puts(sp
, "!\n");
146 * Historically, filter reads as the first ex command didn't
147 * wait for the user. If SC_SCR_EXWROTE not already set, set
148 * the don't-wait flag.
150 if (!F_ISSET(sp
, SC_SCR_EXWROTE
))
151 F_SET(sp
, SC_EX_WAIT_NO
);
154 * Switch into ex canonical mode. The reason to restore the
155 * original terminal modes for read filters is so that users
156 * can do things like ":r! cat /dev/tty".
159 * We do not output an extra <newline>, so that we don't touch
160 * the screen on a normal read.
162 if (F_ISSET(sp
, SC_VI
)) {
163 if (gp
->scr_screen(sp
, SC_EX
)) {
164 ex_wemsg(sp
, cmdp
->cmd
->name
, EXM_NOCANON_F
);
169 * Historically, the read command doesn't switch to
170 * the alternate X11 xterm screen, if doing a filter
171 * read -- don't set SA_ALTERNATE.
173 F_SET(sp
, SC_SCR_EX
| SC_SCR_EXWROTE
);
176 if (ex_filter(sp
, cmdp
, &cmdp
->addr1
,
177 NULL
, &rm
, cmdp
->argv
[argc
]->bp
, FILTER_READ
))
180 /* The filter version of read set the autoprint flag. */
181 F_SET(cmdp
, E_AUTOPRINT
);
184 * If in vi mode, move to the first nonblank. Might have
185 * switched into ex mode, so saved the original SC_VI value.
188 if (F_ISSET(sp
, SC_VI
)) {
190 (void)nonblank(sp
, sp
->lno
, &sp
->cno
);
194 name
= sp
->frp
->name
;
197 if (argv_exp2(sp
, cmdp
, arg
, arglen
))
200 * 0 args: impossible.
201 * 1 args: impossible (I hope).
203 * >2 args: object, too many args.
205 * The 1 args case depends on the argv_sexp() function refusing
206 * to return success without at least one non-blank character.
208 switch (cmdp
->argc
) {
214 INT2CHAR(sp
, cmdp
->argv
[1]->bp
, cmdp
->argv
[1]->len
+ 1,
218 * Historically, the read and write commands renamed
219 * "unnamed" files, or, if the file had a name, set
220 * the alternate file name.
222 if (F_ISSET(sp
->frp
, FR_TMPFILE
) &&
223 !F_ISSET(sp
->frp
, FR_EXNAMED
)) {
224 if ((p
= strdup(name
)) != NULL
) {
229 * The file has a real name, it's no longer a
230 * temporary, clear the temporary file flags.
232 F_CLR(sp
->frp
, FR_TMPEXIT
| FR_TMPFILE
);
233 F_SET(sp
->frp
, FR_NAMECHANGE
| FR_EXNAMED
);
235 /* Notify the screen. */
236 (void)sp
->gp
->scr_rename(sp
, sp
->frp
->name
, 1);
237 name
= sp
->frp
->name
;
239 set_alt_name(sp
, name
);
244 ex_wemsg(sp
, cmdp
->argv
[0]->bp
, EXM_FILECOUNT
);
253 * Historically, vi did not permit reads from non-regular files, nor
254 * did it distinguish between "read !" and "read!", so there was no
255 * way to "force" it. We permit reading from named pipes too, since
256 * they didn't exist when the original implementation of vi was done
257 * and they seem a reasonable addition.
259 if ((fp
= fopen(name
, "r")) == NULL
|| fstat(fileno(fp
), &sb
)) {
260 msgq_str(sp
, M_SYSERR
, name
, "%s");
263 if (!S_ISFIFO(sb
.st_mode
) && !S_ISREG(sb
.st_mode
)) {
266 "145|Only regular files and named pipes may be read");
270 /* Try and get a lock. */
271 if (file_lock(sp
, NULL
, NULL
, fileno(fp
), 0) == LOCK_UNAVAIL
)
272 msgq(sp
, M_ERR
, "146|%s: read lock was unavailable", name
);
274 rval
= ex_readfp(sp
, name
, fp
, &cmdp
->addr1
, &nlines
, 0);
277 * In vi, set the cursor to the first line read in, if anything read
278 * in, otherwise, the address. (Historic vi set it to the line after
279 * the address regardless, but since that line may not exist we don't
282 * In ex, set the cursor to the last line read in, if anything read in,
283 * otherwise, the address.
285 if (F_ISSET(sp
, SC_VI
)) {
286 sp
->lno
= cmdp
->addr1
.lno
;
290 sp
->lno
= cmdp
->addr1
.lno
+ nlines
;
296 * Read lines into the file.
298 * PUBLIC: int ex_readfp __P((SCR *, char *, FILE *, MARK *, db_recno_t *, int));
301 ex_readfp(sp
, name
, fp
, fm
, nlinesp
, silent
)
311 db_recno_t lcnt
, lno
;
313 u_long ccnt
; /* XXX: can't print off_t portably. */
321 * Add in the lines from the output. Insertion starts at the line
322 * following the address.
326 p
= "147|Reading...";
327 for (lno
= fm
->lno
; !ex_getline(sp
, fp
, &len
); ++lno
, ++lcnt
) {
328 if ((lcnt
+ 1) % INTERRUPT_CHECK
== 0) {
333 p
== NULL
? BUSY_UPDATE
: BUSY_ON
);
337 if (db_append(sp
, 1, lno
, exp
->ibp
, len
))
342 if (ferror(fp
) || fclose(fp
))
345 /* Return the number of lines read in. */
350 p
= msg_print(sp
, name
, &nf
);
352 "148|%s: %lu lines, %lu characters", p
, lcnt
, ccnt
);
354 FREE_SPACE(sp
, p
, 0);
359 err
: msgq_str(sp
, M_SYSERR
, name
, "%s");
365 gp
->scr_busy(sp
, NULL
, BUSY_OFF
);