#include "config.h"
#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>
#include "../common/common.h"
#include "../vi/vi.h"
int
ex_read(SCR *sp, EXCMD *cmdp)
{
enum { R_ARG, R_EXPANDARG, R_FILTER } which;
struct stat sb;
CHAR_T *arg, *name;
EX_PRIVATE *exp;
FILE *fp;
FREF *frp;
GS *gp;
MARK rm;
recno_t nlines;
size_t arglen;
int argc, rval;
char *p;
gp = sp->gp;
switch (cmdp->argc) {
case 0:
which = R_ARG;
arg = NULL;
arglen = 0;
break;
case 1:
arg = cmdp->argv[0]->bp;
arglen = cmdp->argv[0]->len;
if (*arg == '!') {
++arg;
--arglen;
which = R_FILTER;
if (O_ISSET(sp, O_SECURE)) {
ex_emsg(sp, cmdp->cmd->name, EXM_SECURE_F);
return (1);
}
} else
which = R_EXPANDARG;
break;
default:
abort();
}
if (sp->ep == NULL) {
if ((frp = file_add(sp, NULL)) == NULL)
return (1);
if (file_init(sp, frp, NULL, 0))
return (1);
}
switch (which) {
case R_FILTER:
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;
exp = EXP(sp);
free(exp->lastbcomm);
if ((exp->lastbcomm =
strdup(cmdp->argv[argc]->bp)) == NULL) {
msgq(sp, M_SYSERR, NULL);
return (1);
}
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);
}
if (!F_ISSET(sp, SC_SCR_EXWROTE))
F_SET(sp, SC_EX_WAIT_NO);
if (F_ISSET(sp, SC_VI)) {
if (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);
}
if (ex_filter(sp, cmdp, &cmdp->addr1,
NULL, &rm, cmdp->argv[argc]->bp, FILTER_READ))
return (1);
F_SET(cmdp, E_AUTOPRINT);
sp->lno = rm.lno;
if (F_ISSET(sp, SC_VI)) {
sp->cno = 0;
(void)nonblank(sp, sp->lno, &sp->cno);
}
return (0);
case R_ARG:
name = sp->frp->name;
break;
case R_EXPANDARG:
if (argv_exp2(sp, cmdp, arg, arglen))
return (1);
switch (cmdp->argc) {
case 0:
case 1:
abort();
case 2:
name = cmdp->argv[1]->bp;
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;
}
F_CLR(sp->frp, FR_TMPEXIT | FR_TMPFILE);
F_SET(sp->frp, FR_NAMECHANGE | FR_EXNAMED);
(void)sp->gp->scr_rename(sp, sp->frp->name, 1);
} else
set_alt_name(sp, name);
break;
default:
ex_emsg(sp, cmdp->argv[0]->bp, EXM_FILECOUNT);
return (1);
}
break;
default:
abort();
}
if ((fp = fopen(name, "r")) == NULL || fstat(fileno(fp), &sb)) {
msgq_str(sp, M_SYSERR, name, "%s");
return (1);
}
if (!S_ISFIFO(sb.st_mode) && !S_ISREG(sb.st_mode)) {
(void)fclose(fp);
msgq(sp, M_ERR,
"Only regular files and named pipes may be read");
return (1);
}
if (file_lock(sp, NULL, NULL, fileno(fp), 0) == LOCK_UNAVAIL)
msgq(sp, M_ERR, "%s: read lock was unavailable", name);
rval = ex_readfp(sp, name, fp, &cmdp->addr1, &nlines, 0);
if (F_ISSET(sp, SC_VI)) {
sp->lno = cmdp->addr1.lno;
if (nlines)
++sp->lno;
} else
sp->lno = cmdp->addr1.lno + nlines;
return (rval);
}
int
ex_readfp(SCR *sp, char *name, FILE *fp, MARK *fm, recno_t *nlinesp,
int silent)
{
EX_PRIVATE *exp;
GS *gp;
recno_t lcnt, lno;
size_t len;
u_long ccnt;
int nf, rval;
char *p;
gp = sp->gp;
exp = EXP(sp);
ccnt = 0;
lcnt = 0;
p = "Reading...";
for (lno = fm->lno; !ex_getline(sp, fp, &len); ++lno, ++lcnt) {
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;
}
}
if (db_append(sp, 1, lno, exp->ibp, len))
goto err;
ccnt += len;
}
if (ferror(fp) || fclose(fp))
goto err;
if (nlinesp != NULL)
*nlinesp = lcnt;
if (!silent) {
p = msg_print(sp, name, &nf);
msgq(sp, M_INFO,
"%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;
}
if (!silent)
gp->scr_busy(sp, NULL, BUSY_OFF);
return (rval);
}