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.44 2001/06/25 15:19:19 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:19 $";
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 *));
43 ex_read(SCR
*sp
, EXCMD
*cmdp
)
45 enum { R_ARG
, R_EXPANDARG
, R_FILTER
} which
;
64 * 0 args: read the current pathname.
65 * 1 args: check for "read !arg".
72 arg
= cmdp
->argv
[0]->bp
;
73 arglen
= cmdp
->argv
[0]->len
;
79 /* Secure means no shell access. */
80 if (O_ISSET(sp
, O_SECURE
)) {
81 ex_wemsg(sp
, cmdp
->cmd
->name
, EXM_SECURE_F
);
92 /* Load a temporary file if no file being edited. */
94 if ((frp
= file_add(sp
, NULL
)) == NULL
)
96 if (file_init(sp
, frp
, NULL
, 0))
103 * File name and bang expand the user's argument. If
104 * we don't get an additional argument, it's illegal.
107 if (argv_exp1(sp
, cmdp
, arg
, arglen
, 1))
109 if (argc
== cmdp
->argc
) {
110 ex_emsg(sp
, cmdp
->cmd
->usage
, EXM_USAGE
);
113 argc
= cmdp
->argc
- 1;
115 /* Set the last bang command. */
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
);
127 * Vi redisplayed the user's argument if it changed, ex
128 * always displayed a !, plus the user's argument if it
131 if (F_ISSET(sp
, SC_VI
)) {
132 if (F_ISSET(cmdp
, E_MODIFY
))
133 (void)vs_update(sp
, "!", cmdp
->argv
[argc
]->bp
);
135 if (F_ISSET(cmdp
, E_MODIFY
))
137 "!%s\n", cmdp
->argv
[argc
]->bp
);
139 (void)ex_puts(sp
, "!\n");
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".
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
);
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
))
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.
186 if (F_ISSET(sp
, SC_VI
)) {
188 (void)nonblank(sp
, sp
->lno
, &sp
->cno
);
192 name
= sp
->frp
->name
;
195 if (argv_exp2(sp
, cmdp
, arg
, arglen
))
198 * 0 args: impossible.
199 * 1 args: impossible (I hope).
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
) {
212 INT2CHAR(sp
, cmdp
->argv
[1]->bp
, cmdp
->argv
[1]->len
+ 1,
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
) {
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
;
237 set_alt_name(sp
, name
);
242 ex_wemsg(sp
, cmdp
->argv
[0]->bp
, EXM_FILECOUNT
);
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");
261 if (!S_ISFIFO(sb
.st_mode
) && !S_ISREG(sb
.st_mode
)) {
264 "145|Only regular files and named pipes may be read");
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
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
;
288 sp
->lno
= cmdp
->addr1
.lno
+ nlines
;
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
)
303 db_recno_t lcnt
, lno
;
305 u_long ccnt
; /* XXX: can't print off_t portably. */
313 * Add in the lines from the output. Insertion starts at the line
314 * following the address.
318 p
= "147|Reading...";
319 for (lno
= fm
->lno
; !ex_getline(sp
, fp
, &len
); ++lno
, ++lcnt
) {
320 if ((lcnt
+ 1) % INTERRUPT_CHECK
== 0) {
325 p
== NULL
? BUSY_UPDATE
: BUSY_ON
);
329 if (db_append(sp
, 1, lno
, exp
->ibp
, len
))
334 if (ferror(fp
) || fclose(fp
))
337 /* Return the number of lines read in. */
342 p
= msg_print(sp
, name
, &nf
);
344 "148|%s: %lu lines, %lu characters", p
, lcnt
, ccnt
);
346 FREE_SPACE(sp
, p
, 0);
351 err
: msgq_str(sp
, M_SYSERR
, name
, "%s");
357 gp
->scr_busy(sp
, NULL
, BUSY_OFF
);