Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / ex / ex_source.c
blob9267096eced0bdcf3966747df3f586c456dbdba2
1 /* $NetBSD: ex_source.c,v 1.1.1.2 2008/05/18 14:31:18 aymeric Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/stat.h>
22 #include <bitstring.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "../common/common.h"
34 * ex_source -- :source file
35 * Execute ex commands from a file.
37 * PUBLIC: int ex_source __P((SCR *, EXCMD *));
39 int
40 ex_source(SCR *sp, EXCMD *cmdp)
42 struct stat sb;
43 int fd, len;
44 char *bp;
45 const char *name;
46 size_t nlen;
47 const CHAR_T *wp;
48 CHAR_T *dp;
49 size_t wlen;
51 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1, name, nlen);
52 if ((fd = open(name, O_RDONLY, 0)) < 0 || fstat(fd, &sb))
53 goto err;
56 * XXX
57 * I'd like to test to see if the file is too large to malloc. Since
58 * we don't know what size or type off_t's or size_t's are, what the
59 * largest unsigned integral type is, or what random insanity the local
60 * C compiler will perpetrate, doing the comparison in a portable way
61 * is flatly impossible. So, put an fairly unreasonable limit on it,
62 * I don't want to be dropping core here.
64 #define MEGABYTE 1048576
65 if (sb.st_size > MEGABYTE) {
66 errno = ENOMEM;
67 goto err;
70 MALLOC(sp, bp, char *, (size_t)sb.st_size + 1);
71 if (bp == NULL) {
72 (void)close(fd);
73 return (1);
75 bp[sb.st_size] = '\0';
77 /* Read the file into memory. */
78 len = read(fd, bp, (int)sb.st_size);
79 (void)close(fd);
80 if (len == -1 || len != sb.st_size) {
81 if (len != sb.st_size)
82 errno = EIO;
83 free(bp);
84 err: msgq_str(sp, M_SYSERR, name, "%s");
85 return (1);
88 if (CHAR2INT(sp, bp, (size_t)sb.st_size + 1, wp, wlen))
89 msgq(sp, M_ERR, "323|Invalid input. Truncated.");
90 dp = v_wstrdup(sp, wp, wlen - 1);
91 free(bp);
92 /* Put it on the ex queue. */
93 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1, name, nlen);
94 return (ex_run_str(sp, name, dp, wlen - 1, 1, 1));