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