xref: /minix3/external/bsd/nvi/dist/ex/ex_source.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ex_source.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994
484d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
584d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
784d9c625SLionel Sambuc  *
884d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc  */
1084d9c625SLionel Sambuc 
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc 
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc 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 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ex_source.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc 
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc #include <sys/stat.h>
2584d9c625SLionel Sambuc 
2684d9c625SLionel Sambuc #include <bitstring.h>
2784d9c625SLionel Sambuc #include <errno.h>
2884d9c625SLionel Sambuc #include <fcntl.h>
2984d9c625SLionel Sambuc #include <limits.h>
3084d9c625SLionel Sambuc #include <stdio.h>
3184d9c625SLionel Sambuc #include <stdlib.h>
3284d9c625SLionel Sambuc #include <string.h>
3384d9c625SLionel Sambuc #include <unistd.h>
3484d9c625SLionel Sambuc 
3584d9c625SLionel Sambuc #include "../common/common.h"
3684d9c625SLionel Sambuc 
3784d9c625SLionel Sambuc /*
3884d9c625SLionel Sambuc  * ex_source -- :source file
3984d9c625SLionel Sambuc  *	Execute ex commands from a file.
4084d9c625SLionel Sambuc  *
4184d9c625SLionel Sambuc  * PUBLIC: int ex_source __P((SCR *, EXCMD *));
4284d9c625SLionel Sambuc  */
4384d9c625SLionel Sambuc int
ex_source(SCR * sp,EXCMD * cmdp)4484d9c625SLionel Sambuc ex_source(SCR *sp, EXCMD *cmdp)
4584d9c625SLionel Sambuc {
4684d9c625SLionel Sambuc 	struct stat sb;
4784d9c625SLionel Sambuc 	int fd, len;
4884d9c625SLionel Sambuc 	char *bp;
4984d9c625SLionel Sambuc 	const char *name;
5084d9c625SLionel Sambuc 	size_t nlen;
5184d9c625SLionel Sambuc 	const CHAR_T *wp;
5284d9c625SLionel Sambuc 	CHAR_T *dp;
5384d9c625SLionel Sambuc 	size_t wlen;
5484d9c625SLionel Sambuc 
5584d9c625SLionel Sambuc 	INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1, name, nlen);
5684d9c625SLionel Sambuc 	if ((fd = open(name, O_RDONLY, 0)) < 0 || fstat(fd, &sb))
5784d9c625SLionel Sambuc 		goto err;
5884d9c625SLionel Sambuc 
5984d9c625SLionel Sambuc 	/*
6084d9c625SLionel Sambuc 	 * XXX
6184d9c625SLionel Sambuc 	 * I'd like to test to see if the file is too large to malloc.  Since
6284d9c625SLionel Sambuc 	 * we don't know what size or type off_t's or size_t's are, what the
6384d9c625SLionel Sambuc 	 * largest unsigned integral type is, or what random insanity the local
6484d9c625SLionel Sambuc 	 * C compiler will perpetrate, doing the comparison in a portable way
6584d9c625SLionel Sambuc 	 * is flatly impossible.  So, put an fairly unreasonable limit on it,
6684d9c625SLionel Sambuc 	 * I don't want to be dropping core here.
6784d9c625SLionel Sambuc 	 */
6884d9c625SLionel Sambuc #define	MEGABYTE	1048576
6984d9c625SLionel Sambuc 	if (sb.st_size > MEGABYTE) {
7084d9c625SLionel Sambuc 		errno = ENOMEM;
7184d9c625SLionel Sambuc 		goto err;
7284d9c625SLionel Sambuc 	}
7384d9c625SLionel Sambuc 
7484d9c625SLionel Sambuc 	MALLOC(sp, bp, char *, (size_t)sb.st_size + 1);
7584d9c625SLionel Sambuc 	if (bp == NULL) {
7684d9c625SLionel Sambuc 		(void)close(fd);
7784d9c625SLionel Sambuc 		return (1);
7884d9c625SLionel Sambuc 	}
7984d9c625SLionel Sambuc 	bp[sb.st_size] = '\0';
8084d9c625SLionel Sambuc 
8184d9c625SLionel Sambuc 	/* Read the file into memory. */
8284d9c625SLionel Sambuc 	len = read(fd, bp, (int)sb.st_size);
8384d9c625SLionel Sambuc 	(void)close(fd);
8484d9c625SLionel Sambuc 	if (len == -1 || len != sb.st_size) {
8584d9c625SLionel Sambuc 		if (len != sb.st_size)
8684d9c625SLionel Sambuc 			errno = EIO;
8784d9c625SLionel Sambuc 		free(bp);
8884d9c625SLionel Sambuc err:		msgq_str(sp, M_SYSERR, name, "%s");
8984d9c625SLionel Sambuc 		return (1);
9084d9c625SLionel Sambuc 	}
9184d9c625SLionel Sambuc 
9284d9c625SLionel Sambuc 	if (CHAR2INT(sp, bp, (size_t)sb.st_size + 1, wp, wlen))
9384d9c625SLionel Sambuc 		msgq(sp, M_ERR, "323|Invalid input. Truncated.");
9484d9c625SLionel Sambuc 	dp = v_wstrdup(sp, wp, wlen - 1);
9584d9c625SLionel Sambuc 	free(bp);
9684d9c625SLionel Sambuc 	/* Put it on the ex queue. */
9784d9c625SLionel Sambuc 	INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1, name, nlen);
9884d9c625SLionel Sambuc 	return (ex_run_str(sp, name, dp, wlen - 1, 1, 1));
9984d9c625SLionel Sambuc }
100