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.18 1993/12/19 12:59:19 bostic Exp $ (Berkeley) $Date: 1993/12/19 12:59:19 $";
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 /* Expand the user's argument. */
52 cmdp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
, 0))
55 /* If argc still 1, there wasn't anything to expand. */
56 if (cmdp
->argc
== 1) {
57 msgq(sp
, M_ERR
, "Usage: %s.", cmdp
->cmd
->usage
);
61 /* Redisplay the user's argument if it's changed. */
62 if (F_ISSET(cmdp
, E_MODIFY
) && IN_VI_MODE(sp
)) {
63 len
= cmdp
->argv
[1]->len
;
64 GET_SPACE_RET(sp
, bp
, blen
, len
+ 2);
66 memmove(bp
+ 1, cmdp
->argv
[1], cmdp
->argv
[1]->len
+ 1);
67 (void)sp
->s_busy(sp
, bp
);
68 FREE_SPACE(sp
, bp
, blen
);
72 &cmdp
->addr1
, NULL
, &rm
, cmdp
->argv
[1]->bp
, FILTER_READ
))
78 /* Expand the user's argument. */
80 cmdp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
, 0))
85 /* Nothing to expand, read the current file. */
86 name
= FILENAME(sp
->frp
);
87 if (sp
->alt_name
== NULL
)
88 set_alt_name(sp
, name
);
91 /* One new argument, read it. */
92 name
= cmdp
->argv
[1]->bp
;
95 /* If expanded to more than one argument, object. */
96 msgq(sp
, M_ERR
, "%s expanded into too many file names",
98 msgq(sp
, M_ERR
, "Usage: %s.", cmdp
->cmd
->usage
);
104 * Historically, vi did not permit reads from non-regular files,
105 * nor did it distinguish between "read !" and "read!", so there
106 * was no way to "force" it.
108 if ((fp
= fopen(name
, "r")) == NULL
|| fstat(fileno(fp
), &sb
)) {
109 msgq(sp
, M_SYSERR
, name
);
112 if (!S_ISREG(sb
.st_mode
)) {
114 msgq(sp
, M_ERR
, "Only regular files may be read.");
118 rval
= ex_readfp(sp
, ep
, name
, fp
, &cmdp
->addr1
, &nlines
, 1);
121 * Set the cursor to the first line read in, if anything read
122 * in, otherwise, the address. (Historic vi set it to the
123 * line after the address regardless, but since that line may
124 * not exist we don't bother.)
126 sp
->lno
= cmdp
->addr1
.lno
;
131 F_SET(sp
, S_AUTOPRINT
);
138 * Read lines into the file.
141 ex_readfp(sp
, ep
, name
, fp
, fm
, nlinesp
, success_msg
)
157 * Add in the lines from the output. Insertion starts at the line
158 * following the address.
163 for (lno
= fm
->lno
; !ex_getline(sp
, fp
, &len
); ++lno
) {
164 if (file_aline(sp
, ep
, 1, lno
, exp
->ibp
, len
)) {
172 msgq(sp
, M_SYSERR
, name
);
177 msgq(sp
, M_SYSERR
, name
);
184 /* Return the number of lines read in. */
185 nlines
= lno
- fm
->lno
;
190 msgq(sp
, M_INFO
, "%s: %lu line%s, %lu characters.",
191 name
, nlines
, nlines
== 1 ? "" : "s", ccnt
);