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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
24 #include "../common/common.h"
28 * ex_read -- :read [file]
30 * Read from a file or utility.
33 * Historical vi wouldn't undo a filter read, for no apparent reason.
35 * PUBLIC: int ex_read(SCR *, EXCMD *);
38 ex_read(SCR
*sp
, EXCMD
*cmdp
)
40 enum { R_ARG
, R_EXPANDARG
, R_FILTER
} which
;
58 * 0 args: read the current pathname.
59 * 1 args: check for "read !arg".
66 arg
= cmdp
->argv
[0]->bp
;
67 arglen
= cmdp
->argv
[0]->len
;
73 /* Secure means no shell access. */
74 if (O_ISSET(sp
, O_SECURE
)) {
75 ex_wemsg(sp
, cmdp
->cmd
->name
, EXM_SECURE_F
);
86 /* Load a temporary file if no file being edited. */
88 if ((frp
= file_add(sp
, NULL
)) == NULL
)
90 if (file_init(sp
, frp
, NULL
, 0))
97 * File name and bang expand the user's argument. If
98 * we don't get an additional argument, it's illegal.
101 if (argv_exp1(sp
, cmdp
, arg
, arglen
, 1))
103 if (argc
== cmdp
->argc
) {
104 ex_emsg(sp
, cmdp
->cmd
->usage
, EXM_USAGE
);
107 argc
= cmdp
->argc
- 1;
109 /* Set the last bang command. */
111 free(exp
->lastbcomm
);
112 if ((exp
->lastbcomm
=
113 v_wstrdup(sp
, cmdp
->argv
[argc
]->bp
,
114 cmdp
->argv
[argc
]->len
)) == NULL
) {
115 msgq(sp
, M_SYSERR
, NULL
);
120 * Vi redisplayed the user's argument if it changed, ex
121 * always displayed a !, plus the user's argument if it
124 if (F_ISSET(sp
, SC_VI
)) {
125 if (F_ISSET(cmdp
, E_MODIFY
))
126 (void)vs_update(sp
, "!", cmdp
->argv
[argc
]->bp
);
128 if (F_ISSET(cmdp
, E_MODIFY
))
130 "!"WS
"\n", cmdp
->argv
[argc
]->bp
);
132 (void)ex_puts(sp
, "!\n");
137 * Historically, filter reads as the first ex command didn't
138 * wait for the user. If SC_SCR_EXWROTE not already set, set
139 * the don't-wait flag.
141 if (!F_ISSET(sp
, SC_SCR_EXWROTE
))
142 F_SET(sp
, SC_EX_WAIT_NO
);
145 * Switch into ex canonical mode. The reason to restore the
146 * original terminal modes for read filters is so that users
147 * can do things like ":r! cat /dev/tty".
150 * We do not output an extra <newline>, so that we don't touch
151 * the screen on a normal read.
153 if (F_ISSET(sp
, SC_VI
)) {
154 if (gp
->scr_screen(sp
, SC_EX
)) {
155 ex_wemsg(sp
, cmdp
->cmd
->name
, EXM_NOCANON_F
);
160 * Historically, the read command doesn't switch to
161 * the alternate X11 xterm screen, if doing a filter
162 * read -- don't set SA_ALTERNATE.
164 F_SET(sp
, SC_SCR_EX
| SC_SCR_EXWROTE
);
167 if (ex_filter(sp
, cmdp
, &cmdp
->addr1
,
168 NULL
, &rm
, cmdp
->argv
[argc
]->bp
, FILTER_READ
))
171 /* The filter version of read set the autoprint flag. */
172 F_SET(cmdp
, E_AUTOPRINT
);
175 * If in vi mode, move to the first nonblank. Might have
176 * switched into ex mode, so saved the original SC_VI value.
179 if (F_ISSET(sp
, SC_VI
)) {
181 (void)nonblank(sp
, sp
->lno
, &sp
->cno
);
185 name
= sp
->frp
->name
;
188 if (argv_exp2(sp
, cmdp
, arg
, arglen
))
191 * 0 args: impossible.
192 * 1 args: impossible (I hope).
194 * >2 args: object, too many args.
196 * The 1 args case depends on the argv_sexp() function refusing
197 * to return success without at least one non-blank character.
199 switch (cmdp
->argc
) {
205 INT2CHAR(sp
, cmdp
->argv
[1]->bp
, cmdp
->argv
[1]->len
+ 1,
209 * Historically, the read and write commands renamed
210 * "unnamed" files, or, if the file had a name, set
211 * the alternate file name.
213 if (F_ISSET(sp
->frp
, FR_TMPFILE
) &&
214 !F_ISSET(sp
->frp
, FR_EXNAMED
)) {
215 if ((p
= strdup(name
)) != NULL
) {
220 * The file has a real name, it's no longer a
221 * temporary, clear the temporary file flags.
223 F_CLR(sp
->frp
, FR_TMPEXIT
| FR_TMPFILE
);
224 F_SET(sp
->frp
, FR_NAMECHANGE
| FR_EXNAMED
);
226 /* Notify the screen. */
227 (void)sp
->gp
->scr_rename(sp
, sp
->frp
->name
, 1);
228 name
= sp
->frp
->name
;
230 set_alt_name(sp
, name
);
235 ex_wemsg(sp
, cmdp
->argv
[0]->bp
, EXM_FILECOUNT
);
244 * Historically, vi did not permit reads from non-regular files, nor
245 * did it distinguish between "read !" and "read!", so there was no
246 * way to "force" it. We permit reading from named pipes too, since
247 * they didn't exist when the original implementation of vi was done
248 * and they seem a reasonable addition.
250 if ((fp
= fopen(name
, "r")) == NULL
|| fstat(fileno(fp
), &sb
)) {
251 msgq_str(sp
, M_SYSERR
, name
, "%s");
254 if (!S_ISFIFO(sb
.st_mode
) && !S_ISREG(sb
.st_mode
)) {
257 "145|Only regular files and named pipes may be read");
261 /* Try and get a lock. */
262 if (file_lock(sp
, NULL
, fileno(fp
), 0) == LOCK_UNAVAIL
)
263 msgq(sp
, M_ERR
, "146|%s: read lock was unavailable", name
);
265 rval
= ex_readfp(sp
, name
, fp
, &cmdp
->addr1
, &nlines
, 0);
268 * In vi, set the cursor to the first line read in, if anything read
269 * in, otherwise, the address. (Historic vi set it to the line after
270 * the address regardless, but since that line may not exist we don't
273 * In ex, set the cursor to the last line read in, if anything read in,
274 * otherwise, the address.
276 if (F_ISSET(sp
, SC_VI
)) {
277 sp
->lno
= cmdp
->addr1
.lno
;
281 sp
->lno
= cmdp
->addr1
.lno
+ nlines
;
287 * Read lines into the file.
289 * PUBLIC: int ex_readfp(SCR *, char *, FILE *, MARK *, recno_t *, int);
292 ex_readfp(SCR
*sp
, char *name
, FILE *fp
, MARK
*fm
, recno_t
*nlinesp
, int silent
)
298 u_long ccnt
; /* XXX: can't print off_t portably. */
308 * Add in the lines from the output. Insertion starts at the line
309 * following the address.
313 p
= "147|Reading...";
314 for (lno
= fm
->lno
; !ex_getline(sp
, fp
, &len
); ++lno
, ++lcnt
) {
315 if ((lcnt
+ 1) % INTERRUPT_CHECK
== 0) {
320 p
== NULL
? BUSY_UPDATE
: BUSY_ON
);
324 FILE2INT5(sp
, exp
->ibcw
, exp
->ibp
, len
, wp
, wlen
);
325 if (db_append(sp
, 1, lno
, wp
, wlen
))
330 if (ferror(fp
) || fclose(fp
))
333 /* Return the number of lines read in. */
338 p
= msg_print(sp
, name
, &nf
);
340 "148|%s: %lu lines, %lu characters", p
,
343 FREE_SPACE(sp
, p
, 0);
348 err
: msgq_str(sp
, M_SYSERR
, name
, "%s");
354 gp
->scr_busy(sp
, NULL
, BUSY_OFF
);