#include "config.h"
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <bitstring.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "common.h"
#include "../vi/vi.h"
#ifdef DEBUG
static void attach(GS *);
#endif
static int v_obsolete(char *[]);
enum pmode pmode;
int
editor(GS *gp, int argc, char *argv[])
{
extern int optind;
extern char *optarg;
const char *p;
EVENT ev;
FREF *frp;
SCR *sp;
size_t len;
u_int flags;
int ch, flagchk, secure, startup, readonly, rval, silent;
char *tag_f, *wsizearg, path[256];
static const char *optstr[3] = {
#ifdef DEBUG
"c:D:FlRrSsT:t:vw:",
"c:D:eFlRrST:t:w:",
"c:D:eFlrST:t:w:"
#else
"c:FlRrSst:vw:",
"c:eFlRrSt:w:",
"c:eFlrSt:w:"
#endif
};
if (pledge("stdio rpath wpath cpath fattr flock getpw tty proc exec",
NULL) == -1) {
perror("pledge");
goto err;
}
if (gp->scr_busy == NULL)
gp->scr_busy = vs_busy;
if (gp->scr_msg == NULL)
gp->scr_msg = vs_msg;
TAILQ_INIT(&gp->dq);
TAILQ_INIT(&gp->hq);
LIST_INIT(&gp->ecq);
LIST_INSERT_HEAD(&gp->ecq, &gp->excmd, q);
gp->noprint = DEFAULT_NOPRINT;
TAILQ_INIT(&gp->frefq);
TAILQ_INIT(&gp->dcb_store.textq);
LIST_INIT(&gp->cutq);
LIST_INIT(&gp->seqq);
readonly = 0;
if (!strcmp(getprogname(), "ex") || !strcmp(getprogname(), "nex"))
LF_INIT(SC_EX);
else {
if (!strcmp(getprogname(), "nview") ||
!strcmp(getprogname(), "view"))
readonly = 1;
LF_INIT(SC_VI);
}
if (v_obsolete(argv))
return (1);
flagchk = '\0';
tag_f = wsizearg = NULL;
secure = silent = 0;
startup = 1;
F_SET(gp, G_SNAPSHOT);
pmode = MODE_EX;
if (!strcmp(getprogname(), "ex"))
pmode = MODE_EX;
else if (!strcmp(getprogname(), "vi"))
pmode = MODE_VI;
else if (!strcmp(getprogname(), "view"))
pmode = MODE_VIEW;
while ((ch = getopt(argc, argv, optstr[pmode])) != -1)
switch (ch) {
case 'c':
if (gp->c_option != NULL) {
warnx("only one -c command may be specified.");
return (1);
}
gp->c_option = optarg;
break;
#ifdef DEBUG
case 'D':
switch (optarg[0]) {
case 's':
startup = 0;
break;
case 'w':
attach(gp);
break;
default:
warnx("-D requires s or w argument.");
return (1);
}
break;
#endif
case 'e':
LF_CLR(SC_VI);
LF_SET(SC_EX);
break;
case 'F':
F_CLR(gp, G_SNAPSHOT);
break;
case 'R':
readonly = 1;
break;
case 'r':
if (flagchk == 't') {
warnx(
"only one of -r and -t may be specified.");
return (1);
}
flagchk = 'r';
break;
case 'S':
secure = 1;
break;
case 's':
silent = 1;
break;
#ifdef DEBUG
case 'T':
if ((gp->tracefp = fopen(optarg, "w")) == NULL) {
warn("%s", optarg);
goto err;
}
(void)fprintf(gp->tracefp,
"\n===\ntrace: open %s\n", optarg);
fflush(gp->tracefp);
break;
#endif
case 't':
if (flagchk == 'r') {
warnx(
"only one of -r and -t may be specified.");
return (1);
}
if (flagchk == 't') {
warnx("only one tag file may be specified.");
return (1);
}
flagchk = 't';
tag_f = optarg;
break;
case 'v':
LF_CLR(SC_EX);
LF_SET(SC_VI);
break;
case 'w':
wsizearg = optarg;
break;
case '?':
default:
(void)gp->scr_usage();
return (1);
}
argc -= optind;
argv += optind;
if (secure)
if (pledge("stdio rpath wpath cpath fattr flock getpw tty", NULL) == -1) {
perror("pledge");
goto err;
}
if (silent && !LF_ISSET(SC_EX)) {
warnx("-s option is only applicable to ex.");
goto err;
}
if (LF_ISSET(SC_EX) && F_ISSET(gp, G_SCRIPTED))
silent = 1;
if (screen_init(gp, NULL, &sp)) {
if (sp != NULL)
TAILQ_INSERT_HEAD(&gp->dq, sp, q);
goto err;
}
F_SET(sp, SC_EX);
TAILQ_INSERT_HEAD(&gp->dq, sp, q);
if (v_key_init(sp))
goto err;
{ int oargs[5], *oargp = oargs;
if (readonly)
*oargp++ = O_READONLY;
if (secure)
*oargp++ = O_SECURE;
*oargp = -1;
if (opts_init(sp, oargs))
goto err;
}
if (wsizearg != NULL) {
ARGS *av[2], a, b;
(void)snprintf(path, sizeof(path), "window=%s", wsizearg);
a.bp = (CHAR_T *)path;
a.len = strlen(path);
b.bp = NULL;
b.len = 0;
av[0] = &a;
av[1] = &b;
(void)opts_set(sp, av, NULL);
}
if (silent) {
O_CLR(sp, O_AUTOPRINT);
O_CLR(sp, O_PROMPT);
O_CLR(sp, O_VERBOSE);
O_CLR(sp, O_WARN);
F_SET(sp, SC_EX_SILENT);
}
sp->rows = O_VAL(sp, O_LINES);
sp->cols = O_VAL(sp, O_COLUMNS);
if (!silent && startup) {
if (ex_exrc(sp))
goto err;
if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
if (screen_end(sp))
goto err;
goto done;
}
}
if (flagchk == 'r' && argv[0] == NULL) {
if (rcv_list(sp))
goto err;
if (screen_end(sp))
goto err;
goto done;
}
sp->defscroll = (O_VAL(sp, O_WINDOW) + 1) / 2;
if (gp->c_option == NULL) {
F_CLR(sp, SC_EX | SC_VI);
F_SET(sp, LF_ISSET(SC_EX | SC_VI));
}
if (tag_f != NULL && ex_tag_first(sp, tag_f))
goto err;
if (*argv != NULL) {
if (sp->frp != NULL) {
size_t l;
l = strlen(sp->frp->name) + 1;
if ((*--argv = malloc(l)) == NULL) {
warn(NULL);
goto err;
}
(void)strlcpy(*argv, sp->frp->name, l);
}
sp->argv = sp->cargv = argv;
F_SET(sp, SC_ARGNOFREE);
if (flagchk == 'r')
F_SET(sp, SC_ARGRECOVER);
}
if (sp->frp == NULL) {
if (sp->argv == NULL) {
if ((frp = file_add(sp, NULL)) == NULL)
goto err;
} else {
if ((frp = file_add(sp, (CHAR_T *)sp->argv[0])) == NULL)
goto err;
if (F_ISSET(sp, SC_ARGRECOVER))
F_SET(frp, FR_RECOVER);
}
if (file_init(sp, frp, NULL, 0))
goto err;
if (EXCMD_RUNNING(gp)) {
(void)ex_cmd(sp);
if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
if (screen_end(sp))
goto err;
goto done;
}
}
}
if (F_ISSET(sp, SC_SCR_EX)) {
p = msg_cmsg(sp, CMSG_CONT_R, &len);
(void)write(STDOUT_FILENO, p, len);
for (;;) {
if (v_event_get(sp, &ev, 0, 0))
goto err;
if (ev.e_event == E_INTERRUPT ||
(ev.e_event == E_CHARACTER &&
(ev.e_value == K_CR || ev.e_value == K_NL)))
break;
(void)gp->scr_bell(sp);
}
}
F_CLR(sp, SC_EX | SC_VI);
F_SET(sp, LF_ISSET(SC_EX | SC_VI) | SC_STATUS_CNT);
while (sp != NULL)
if (F_ISSET(sp, SC_EX) ? ex(&sp) : vi(&sp))
goto err;
done: rval = 0;
if (0)
err: rval = 1;
v_end(gp);
return (rval);
}
void
v_end(GS *gp)
{
MSGS *mp;
SCR *sp;
if (gp->ccl_sp != NULL) {
(void)file_end(gp->ccl_sp, NULL, 1);
(void)screen_end(gp->ccl_sp);
}
while ((sp = TAILQ_FIRST(&gp->dq)))
(void)screen_end(sp);
while ((sp = TAILQ_FIRST(&gp->hq)))
(void)screen_end(sp);
#if defined(DEBUG) || defined(PURIFY)
{ FREF *frp;
while ((frp = TAILQ_FIRST(&gp->frefq))) {
TAILQ_REMOVE(&gp->frefq, frp, q);
free(frp->name);
free(frp->tname);
free(frp);
}
}
free(gp->i_event);
cut_close(gp);
seq_close(gp);
(void)text_lfree(&gp->dcb_store.textq);
#endif
if (F_ISSET(gp, G_BELLSCHED))
(void)fprintf(stderr, "\07");
while ((mp = LIST_FIRST(&gp->msgq)) != NULL) {
(void)fprintf(stderr, "%s%.*s",
mp->mtype == M_ERR ? "ex/vi: " : "", (int)mp->len, mp->buf);
LIST_REMOVE(mp, q);
#if defined(DEBUG) || defined(PURIFY)
free(mp->buf);
free(mp);
#endif
}
#if defined(DEBUG) || defined(PURIFY)
free(gp->tmp_bp);
#if defined(DEBUG)
if (gp->tracefp != NULL)
(void)fclose(gp->tracefp);
#endif
#endif
}
static int
v_obsolete(char *argv[])
{
size_t len;
char *p;
while (*++argv && strcmp(argv[0], "--"))
if (argv[0][0] == '+') {
if (argv[0][1] == '\0') {
argv[0] = strdup("-c$");
if (argv[0] == NULL)
goto nomem;
} else {
p = argv[0];
len = strlen(argv[0]);
if ((argv[0] = malloc(len + 2)) == NULL)
goto nomem;
argv[0][0] = '-';
argv[0][1] = 'c';
(void)strlcpy(argv[0] + 2, p + 1, len);
}
} else if (argv[0][0] == '-') {
if (argv[0][1] == '\0') {
argv[0] = strdup("-s");
if (argv[0] == NULL) {
nomem: warn(NULL);
return (1);
}
} else
if ((argv[0][1] == 'c' || argv[0][1] == 'T' ||
argv[0][1] == 't' || argv[0][1] == 'w') &&
argv[0][2] == '\0')
++argv;
}
return (0);
}
#ifdef DEBUG
static void
attach(GS *gp)
{
int fd;
char ch;
if ((fd = open(_PATH_TTY, O_RDONLY)) < 0) {
warn("%s", _PATH_TTY);
return;
}
(void)printf("process %ld waiting, enter <CR> to continue: ",
(long)getpid());
(void)fflush(stdout);
do {
if (read(fd, &ch, 1) != 1) {
(void)close(fd);
return;
}
} while (ch != '\n' && ch != '\r');
(void)close(fd);
}
#endif