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>
25 #include "../common/common.h"
28 * ex_source -- :source file
29 * Execute ex commands from a file.
31 * PUBLIC: int ex_source(SCR *, EXCMD *);
34 ex_source(SCR
*sp
, EXCMD
*cmdp
)
45 INT2CHAR(sp
, cmdp
->argv
[0]->bp
, cmdp
->argv
[0]->len
+ 1, name
, nlen
);
46 if ((fd
= open(name
, O_RDONLY
, 0)) < 0 || fstat(fd
, &sb
))
51 * I'd like to test to see if the file is too large to malloc. Since
52 * we don't know what size or type off_t's or size_t's are, what the
53 * largest unsigned integral type is, or what random insanity the local
54 * C compiler will perpetrate, doing the comparison in a portable way
55 * is flatly impossible. So, put an fairly unreasonable limit on it,
56 * I don't want to be dropping core here.
58 #define MEGABYTE 1048576
59 if (sb
.st_size
> MEGABYTE
) {
64 MALLOC(sp
, bp
, (size_t)sb
.st_size
+ 1);
69 bp
[sb
.st_size
] = '\0';
71 /* Read the file into memory. */
72 len
= read(fd
, bp
, (int)sb
.st_size
);
74 if (len
== -1 || len
!= sb
.st_size
) {
75 if (len
!= sb
.st_size
)
78 err
: msgq_str(sp
, M_SYSERR
, name
, "%s");
83 if (CHAR2INT(sp
, bp
, (size_t)sb
.st_size
+ 1, wp
, wlen
))
84 msgq(sp
, M_ERR
, "323|Invalid input. Truncated.");
85 /* Put it on the ex queue. */
86 rc
= ex_run_str(sp
, np
, wp
, wlen
- 1, 1, 0);