move configure.in to configure.ac
[nvi.git] / ex / ex_source.c
blob368b01f35a0568759840864e3b81577383f5227c
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_source.c,v 10.16 2001/08/18 21:49:58 skimo Exp $ (Berkeley) $Date: 2001/08/18 21:49:58 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <bitstring.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "../common/common.h"
32 * ex_source -- :source file
33 * Execute ex commands from a file.
35 * PUBLIC: int ex_source __P((SCR *, EXCMD *));
37 int
38 ex_source(SCR *sp, EXCMD *cmdp)
40 struct stat sb;
41 int fd, len;
42 char *bp;
43 char *name;
44 size_t nlen;
45 CHAR_T *wp, *dp;
46 size_t wlen;
48 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1, name, nlen);
49 if ((fd = open(name, O_RDONLY, 0)) < 0 || fstat(fd, &sb))
50 goto err;
53 * XXX
54 * I'd like to test to see if the file is too large to malloc. Since
55 * we don't know what size or type off_t's or size_t's are, what the
56 * largest unsigned integral type is, or what random insanity the local
57 * C compiler will perpetrate, doing the comparison in a portable way
58 * is flatly impossible. So, put an fairly unreasonable limit on it,
59 * I don't want to be dropping core here.
61 #define MEGABYTE 1048576
62 if (sb.st_size > MEGABYTE) {
63 errno = ENOMEM;
64 goto err;
67 MALLOC(sp, bp, char *, (size_t)sb.st_size + 1);
68 if (bp == NULL) {
69 (void)close(fd);
70 return (1);
72 bp[sb.st_size] = '\0';
74 /* Read the file into memory. */
75 len = read(fd, bp, (int)sb.st_size);
76 (void)close(fd);
77 if (len == -1 || len != sb.st_size) {
78 if (len != sb.st_size)
79 errno = EIO;
80 free(bp);
81 err: msgq_str(sp, M_SYSERR, name, "%s");
82 return (1);
85 if (CHAR2INT(sp, bp, (size_t)sb.st_size + 1, wp, wlen))
86 msgq(sp, M_ERR, "323|Invalid input. Truncated.");
87 dp = v_wstrdup(sp, wp, wlen - 1);
88 free(bp);
89 /* Put it on the ex queue. */
90 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1, name, nlen);
91 return (ex_run_str(sp, name, dp, wlen - 1, 1, 1));