NetBSD/usr.bin/vi/ex/ex_read.c

349 lines
7.5 KiB
C
Raw Normal View History

1998-01-09 11:03:16 +03:00
/* $NetBSD: ex_read.c,v 1.7 1998/01/09 08:07:59 perry Exp $ */
/*-
* Copyright (c) 1992, 1993, 1994
* The Regents of the University of California. All rights reserved.
1996-05-20 07:47:00 +04:00
* Copyright (c) 1992, 1993, 1994, 1995, 1996
* Keith Bostic. All rights reserved.
*
1996-05-20 07:47:00 +04:00
* See the LICENSE file for redistribution information.
*/
1996-05-20 07:47:00 +04:00
#include "config.h"
#ifndef lint
1996-05-20 07:47:00 +04:00
static const char sccsid[] = "@(#)ex_read.c 10.31 (Berkeley) 5/8/96";
#endif /* not lint */
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <bitstring.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
1996-05-20 07:47:00 +04:00
#include "../common/common.h"
#include "../vi/vi.h"
/*
* ex_read -- :read [file]
* :read [!cmd]
1996-05-20 07:47:00 +04:00
* Read from a file or utility.
*
* !!!
* Historical vi wouldn't undo a filter read, for no apparent reason.
1996-05-20 07:47:00 +04:00
*
* PUBLIC: int ex_read __P((SCR *, EXCMD *));
*/
int
1996-05-20 07:47:00 +04:00
ex_read(sp, cmdp)
SCR *sp;
1996-05-20 07:47:00 +04:00
EXCMD *cmdp;
{
1996-05-20 07:47:00 +04:00
enum { R_ARG, R_EXPANDARG, R_FILTER } which;
struct stat sb;
CHAR_T *arg, *name;
EX_PRIVATE *exp;
FILE *fp;
1996-05-20 07:47:00 +04:00
FREF *frp;
GS *gp;
MARK rm;
recno_t nlines;
1996-05-20 07:47:00 +04:00
size_t arglen;
int argc, rval;
char *p;
1996-05-20 07:47:00 +04:00
gp = sp->gp;
/*
1996-05-20 07:47:00 +04:00
* 0 args: read the current pathname.
* 1 args: check for "read !arg".
*/
switch (cmdp->argc) {
case 0:
1996-05-20 07:47:00 +04:00
which = R_ARG;
break;
case 1:
arg = cmdp->argv[0]->bp;
arglen = cmdp->argv[0]->len;
if (*arg == '!') {
++arg;
--arglen;
1996-05-20 07:47:00 +04:00
which = R_FILTER;
/* Secure means no shell access. */
if (O_ISSET(sp, O_SECURE)) {
ex_emsg(sp, cmdp->cmd->name, EXM_SECURE_F);
return (1);
}
} else
which = R_EXPANDARG;
break;
default:
1996-05-20 07:47:00 +04:00
abort();
/* NOTREACHED */
}
1996-05-20 07:47:00 +04:00
/* Load a temporary file if no file being edited. */
if (sp->ep == NULL) {
if ((frp = file_add(sp, NULL)) == NULL)
return (1);
if (file_init(sp, frp, NULL, 0))
return (1);
1996-05-20 07:47:00 +04:00
}
1996-05-20 07:47:00 +04:00
switch (which) {
case R_FILTER:
/*
* File name and bang expand the user's argument. If
* we don't get an additional argument, it's illegal.
*/
argc = cmdp->argc;
if (argv_exp1(sp, cmdp, arg, arglen, 1))
return (1);
if (argc == cmdp->argc) {
ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
return (1);
}
argc = cmdp->argc - 1;
/* Set the last bang command. */
exp = EXP(sp);
if (exp->lastbcomm != NULL)
free(exp->lastbcomm);
1996-05-20 07:47:00 +04:00
if ((exp->lastbcomm =
strdup(cmdp->argv[argc]->bp)) == NULL) {
msgq(sp, M_SYSERR, NULL);
return (1);
}
1996-05-20 07:47:00 +04:00
/*
* Vi redisplayed the user's argument if it changed, ex
* always displayed a !, plus the user's argument if it
* changed.
*/
if (F_ISSET(sp, SC_VI)) {
if (F_ISSET(cmdp, E_MODIFY))
(void)vs_update(sp, "!", cmdp->argv[argc]->bp);
} else {
if (F_ISSET(cmdp, E_MODIFY))
(void)ex_printf(sp,
"!%s\n", cmdp->argv[argc]->bp);
else
(void)ex_puts(sp, "!\n");
(void)ex_fflush(sp);
}
/*
* Historically, filter reads as the first ex command didn't
* wait for the user. If SC_SCR_EXWROTE not already set, set
* the don't-wait flag.
*/
if (!F_ISSET(sp, SC_SCR_EXWROTE))
F_SET(sp, SC_EX_DONTWAIT);
/*
* Switch into ex canonical mode. The reason to restore the
* original terminal modes for read filters is so that users
* can do things like ":r! cat /dev/tty".
*
* !!!
* We do not output an extra <newline>, so that we don't touch
* the screen on a normal read.
*/
if (F_ISSET(sp, SC_VI)) {
if (sp->gp->scr_screen(sp, SC_EX)) {
ex_emsg(sp, cmdp->cmd->name, EXM_NOCANON_F);
return (1);
}
F_SET(sp, SC_SCR_EX | SC_SCR_EXWROTE);
}
1996-05-20 07:47:00 +04:00
if (ex_filter(sp, cmdp, &cmdp->addr1,
NULL, &rm, cmdp->argv[argc]->bp, FILTER_READ))
return (1);
/* The filter version of read set the autoprint flag. */
1996-05-20 07:47:00 +04:00
F_SET(cmdp, E_AUTOPRINT);
1996-05-20 07:47:00 +04:00
/*
* If in vi mode, move to the first nonblank. Might have
* switched into ex mode, so saved the original SC_VI value.
*/
sp->lno = rm.lno;
1996-05-20 07:47:00 +04:00
if (F_ISSET(sp, SC_VI)) {
sp->cno = 0;
1996-05-20 07:47:00 +04:00
(void)nonblank(sp, sp->lno, &sp->cno);
}
return (0);
1996-05-20 07:47:00 +04:00
case R_ARG:
name = sp->frp->name;
break;
1996-05-20 07:47:00 +04:00
case R_EXPANDARG:
if (argv_exp2(sp, cmdp, arg, arglen))
return (1);
/*
1996-05-20 07:47:00 +04:00
* 0 args: impossible.
* 1 args: impossible (I hope).
* 2 args: read it.
* >2 args: object, too many args.
*
* The 1 args case depends on the argv_sexp() function refusing
* to return success without at least one non-blank character.
*/
1996-05-20 07:47:00 +04:00
switch (cmdp->argc) {
case 0:
case 1:
abort();
/* NOTREACHED */
case 2:
name = cmdp->argv[1]->bp;
/*
* !!!
* Historically, the read and write commands renamed
* "unnamed" files, or, if the file had a name, set
* the alternate file name.
*/
if (F_ISSET(sp->frp, FR_TMPFILE) &&
!F_ISSET(sp->frp, FR_EXNAMED)) {
if ((p = v_strdup(sp, cmdp->argv[1]->bp,
cmdp->argv[1]->len)) != NULL) {
free(sp->frp->name);
sp->frp->name = p;
}
/*
* The file has a real name, it's no longer a
* temporary, clear the temporary file flags.
*/
F_CLR(sp->frp, FR_TMPEXIT | FR_TMPFILE);
F_SET(sp->frp, FR_NAMECHANGE | FR_EXNAMED);
/* Notify the screen. */
(void)gp->scr_rename(sp);
} else
set_alt_name(sp, name);
break;
default:
ex_emsg(sp, cmdp->argv[0]->bp, EXM_FILECOUNT);
return (1);
}
break;
}
/*
* !!!
* Historically, vi did not permit reads from non-regular files,
* nor did it distinguish between "read !" and "read!", so there
* was no way to "force" it.
*/
if ((fp = fopen(name, "r")) == NULL || fstat(fileno(fp), &sb)) {
1996-05-20 07:47:00 +04:00
msgq_str(sp, M_SYSERR, name, "%s");
return (1);
}
if (!S_ISREG(sb.st_mode)) {
(void)fclose(fp);
1996-05-20 07:47:00 +04:00
msgq(sp, M_ERR, "145|Only regular files may be read");
return (1);
}
1996-05-20 07:47:00 +04:00
/* Try and get a lock. */
if (file_lock(sp, NULL, NULL, fileno(fp), 0) == LOCK_UNAVAIL)
msgq(sp, M_ERR, "146|%s: read lock was unavailable", name);
rval = ex_readfp(sp, name, fp, &cmdp->addr1, &nlines, 0);
/*
* Set the cursor to the first line read in, if anything read
* in, otherwise, the address. (Historic vi set it to the
* line after the address regardless, but since that line may
* not exist we don't bother.)
*/
sp->lno = cmdp->addr1.lno;
if (nlines)
++sp->lno;
return (rval);
}
/*
* ex_readfp --
* Read lines into the file.
1996-05-20 07:47:00 +04:00
*
* PUBLIC: int ex_readfp __P((SCR *, char *, FILE *, MARK *, recno_t *, int));
*/
int
1996-05-20 07:47:00 +04:00
ex_readfp(sp, name, fp, fm, nlinesp, silent)
SCR *sp;
char *name;
FILE *fp;
MARK *fm;
recno_t *nlinesp;
1996-05-20 07:47:00 +04:00
int silent;
{
EX_PRIVATE *exp;
1996-05-20 07:47:00 +04:00
GS *gp;
recno_t lcnt, lno;
size_t len;
u_long ccnt; /* XXX: can't print off_t portably. */
1996-05-20 07:47:00 +04:00
int nf, rval;
char *p;
1996-05-20 07:47:00 +04:00
gp = sp->gp;
exp = EXP(sp);
/*
* Add in the lines from the output. Insertion starts at the line
* following the address.
*/
ccnt = 0;
lcnt = 0;
1996-05-20 07:47:00 +04:00
p = "147|Reading...";
for (lno = fm->lno; !ex_getline(sp, fp, &len); ++lno, ++lcnt) {
1996-05-20 07:47:00 +04:00
if ((lcnt + 1) % INTERRUPT_CHECK == 0) {
if (INTERRUPTED(sp))
break;
if (!silent) {
gp->scr_busy(sp, p,
p == NULL ? BUSY_UPDATE : BUSY_ON);
p = NULL;
}
}
1996-05-20 07:47:00 +04:00
if (db_append(sp, 1, lno, exp->ibp, len))
goto err;
ccnt += len;
}
1996-05-20 07:47:00 +04:00
if (ferror(fp) || fclose(fp))
goto err;
/* Return the number of lines read in. */
if (nlinesp != NULL)
*nlinesp = lcnt;
1996-05-20 07:47:00 +04:00
if (!silent) {
p = msg_print(sp, name, &nf);
msgq(sp, M_INFO,
"148|%s: %lu lines, %lu characters", p, lcnt, ccnt);
if (nf)
FREE_SPACE(sp, p, 0);
}
rval = 0;
if (0) {
err: msgq_str(sp, M_SYSERR, name, "%s");
(void)fclose(fp);
rval = 1;
}
1996-05-20 07:47:00 +04:00
if (!silent)
gp->scr_busy(sp, NULL, BUSY_OFF);
return (rval);
}