2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
9 static char sccsid
[] = "$Id: ex_read.c,v 8.14 1993/11/28 12:37:18 bostic Exp $ (Berkeley) $Date: 1993/11/28 12:37:18 $";
12 #include <sys/types.h>
24 * ex_read -- :read [file]
26 * Read from a file or utility.
43 * If "read !", it's a pipe from a utility.
46 * Historical vi wouldn't undo a filter read, for no apparent
49 if (F_ISSET(cmdp
, E_FORCE
)) {
50 if (cmdp
->argv
[0][0] == '\0') {
51 msgq(sp
, M_ERR
, "Usage: %s.", cmdp
->cmd
->usage
);
54 if (argv_exp1(sp
, ep
, cmdp
, cmdp
->argv
[0], 0))
56 if (F_ISSET(cmdp
, E_MODIFY
) && IN_VI_MODE(sp
)) {
57 len
= strlen(cmdp
->argv
[0]);
58 GET_SPACE(sp
, bp
, blen
, len
+ 2);
60 memmove(bp
+ 1, cmdp
->argv
[0], len
+ 1);
61 (void)sp
->s_busy(sp
, bp
);
62 FREE_SPACE(sp
, bp
, blen
);
65 &cmdp
->addr1
, NULL
, &rm
, cmdp
->argv
[0], FILTER_READ
))
71 /* If no file arguments, read the alternate file. */
72 if (cmdp
->argv
[0][0] == '\0') {
73 if (sp
->alt_name
== NULL
) {
75 "No default filename from which to read.");
80 if (argv_exp2(sp
, ep
, cmdp
, cmdp
->argv
[0], 0))
85 name
= FILENAME(sp
->frp
);
88 name
= (char *)cmdp
->argv
[0];
89 set_alt_name(sp
, name
);
92 msgq(sp
, M_ERR
, "Usage: %s.", cmdp
->cmd
->usage
);
99 * Historically, vi did not permit reads from non-regular files,
100 * nor did it distinguish between "read !" and "read!", so there
101 * was no way to "force" it.
103 if ((fp
= fopen(name
, "r")) == NULL
|| fstat(fileno(fp
), &sb
)) {
104 msgq(sp
, M_SYSERR
, name
);
107 if (!S_ISREG(sb
.st_mode
)) {
109 msgq(sp
, M_ERR
, "Only regular files may be read.");
113 rval
= ex_readfp(sp
, ep
, name
, fp
, &cmdp
->addr1
, &nlines
, 1);
116 * Set the cursor to the first line read in, if anything read
117 * in, otherwise, the address. (Historic vi set it to the
118 * line after the address regardless, but since that line may
119 * not exist we don't bother.)
121 sp
->lno
= cmdp
->addr1
.lno
;
126 F_SET(sp
, S_AUTOPRINT
);
133 * Read lines into the file.
136 ex_readfp(sp
, ep
, name
, fp
, fm
, nlinesp
, success_msg
)
152 * Add in the lines from the output. Insertion starts at the line
153 * following the address.
158 for (lno
= fm
->lno
; !ex_getline(sp
, fp
, &len
); ++lno
) {
159 if (file_aline(sp
, ep
, 1, lno
, exp
->ibp
, len
)) {
167 msgq(sp
, M_SYSERR
, name
);
172 msgq(sp
, M_SYSERR
, name
);
179 /* Return the number of lines read in. */
180 nlines
= lno
- fm
->lno
;
185 msgq(sp
, M_INFO
, "%s: %lu line%s, %lu characters.",
186 name
, nlines
, nlines
== 1 ? "" : "s", ccnt
);