#include "config.h"
#include <sys/mman.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <bitstring.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../common/common.h"
#include "../vi/vi.h"
#include "tag.h"
static char *binary_search(char *, char *, char *);
static int compare(char *, char *, char *);
static void ctag_file(SCR *, TAGF *, char *, char **, size_t *);
static int ctag_search(SCR *, char *, size_t, char *);
static int ctag_sfile(SCR *, TAGF *, TAGQ *, char *);
static TAGQ *ctag_slist(SCR *, char *);
static char *linear_search(char *, char *, char *);
static int tag_copy(SCR *, TAG *, TAG **);
static int tag_pop(SCR *, TAGQ *, int);
static int tagf_copy(SCR *, TAGF *, TAGF **);
static int tagf_free(SCR *, TAGF *);
static int tagq_copy(SCR *, TAGQ *, TAGQ **);
int
ex_tag_first(SCR *sp, char *tagarg)
{
ARGS *ap[2], a;
EXCMD cmd;
ex_cinit(&cmd, C_TAG, 0, OOBLNO, OOBLNO, 0, ap);
ex_cadd(&cmd, &a, tagarg, strlen(tagarg));
if (ex_tag_push(sp, &cmd))
return (0);
F_CLR(sp, SC_SCR_TOP);
F_SET(sp, SC_SCR_CENTER);
return (0);
}
int
ex_tag_push(SCR *sp, EXCMD *cmdp)
{
EX_PRIVATE *exp;
FREF *frp;
TAG *rtp;
TAGQ *rtqp, *tqp;
recno_t lno;
size_t cno;
long tl;
int force, istmp;
exp = EXP(sp);
switch (cmdp->argc) {
case 1:
free(exp->tag_last);
if ((exp->tag_last = strdup(cmdp->argv[0]->bp)) == NULL) {
msgq(sp, M_SYSERR, NULL);
return (1);
}
if ((tl =
O_VAL(sp, O_TAGLENGTH)) != 0 && strlen(exp->tag_last) > tl)
exp->tag_last[tl] = '\0';
break;
case 0:
if (exp->tag_last == NULL) {
msgq(sp, M_ERR, "No previous tag entered");
return (1);
}
break;
default:
abort();
}
if ((tqp = ctag_slist(sp, exp->tag_last)) == NULL)
return (1);
rtp = NULL;
rtqp = NULL;
if (TAILQ_EMPTY(&exp->tq)) {
CALLOC_GOTO(sp, rtqp, 1, sizeof(TAGQ));
TAILQ_INIT(&rtqp->tagq);
CALLOC_GOTO(sp, rtp, 1, sizeof(TAG));
TAILQ_INSERT_HEAD(&rtqp->tagq, rtp, q);
rtqp->current = rtp;
}
frp = sp->frp;
lno = sp->lno;
cno = sp->cno;
istmp = frp == NULL ||
(F_ISSET(frp, FR_TMPFILE) && !F_ISSET(cmdp, E_NEWSCREEN));
force = FL_ISSET(cmdp->iflags, E_C_FORCE);
if (F_ISSET(cmdp, E_NEWSCREEN)) {
if (ex_tag_Nswitch(sp, TAILQ_FIRST(&tqp->tagq), force))
goto err;
sp = sp->nextdisp;
exp = EXP(sp);
} else
if (ex_tag_nswitch(sp, TAILQ_FIRST(&tqp->tagq), force))
goto err;
if (TAILQ_EMPTY(&exp->tq)) {
TAILQ_INSERT_HEAD(&exp->tq, rtqp, q);
} else
rtqp = TAILQ_FIRST(&exp->tq);
TAILQ_INSERT_HEAD(&exp->tq, tqp, q);
(void)ctag_search(sp,
tqp->current->search, tqp->current->slen, tqp->tag);
if (istmp) {
rtqp->current->frp = sp->frp;
rtqp->current->lno = sp->lno;
rtqp->current->cno = sp->cno;
} else {
rtqp->current->frp = frp;
rtqp->current->lno = lno;
rtqp->current->cno = cno;
}
return (0);
err:
alloc_err:
free(rtqp);
free(rtp);
tagq_free(sp, tqp);
return (1);
}
int
ex_tag_next(SCR *sp, EXCMD *cmdp)
{
EX_PRIVATE *exp;
TAG *tp;
TAGQ *tqp;
exp = EXP(sp);
if ((tqp = TAILQ_FIRST(&exp->tq)) == NULL) {
tag_msg(sp, TAG_EMPTY, NULL);
return (1);
}
if ((tp = TAILQ_NEXT(tqp->current, q)) == NULL) {
msgq(sp, M_ERR, "Already at the last tag of this group");
return (1);
}
if (ex_tag_nswitch(sp, tp, FL_ISSET(cmdp->iflags, E_C_FORCE)))
return (1);
tqp->current = tp;
(void)ctag_search(sp, tp->search, tp->slen, tqp->tag);
return (0);
}
int
ex_tag_prev(SCR *sp, EXCMD *cmdp)
{
EX_PRIVATE *exp;
TAG *tp;
TAGQ *tqp;
exp = EXP(sp);
if ((tqp = TAILQ_FIRST(&exp->tq)) == NULL) {
tag_msg(sp, TAG_EMPTY, NULL);
return (0);
}
if ((tp = TAILQ_PREV(tqp->current, _tagqh, q)) == NULL) {
msgq(sp, M_ERR, "Already at the first tag of this group");
return (1);
}
if (ex_tag_nswitch(sp, tp, FL_ISSET(cmdp->iflags, E_C_FORCE)))
return (1);
tqp->current = tp;
(void)ctag_search(sp, tp->search, tp->slen, tqp->tag);
return (0);
}
int
ex_tag_nswitch(SCR *sp, TAG *tp, int force)
{
if (tp->frp == NULL && (tp->frp = file_add(sp, tp->fname)) == NULL)
return (1);
if (tp->frp == sp->frp)
return (0);
if (file_m1(sp, force, FS_ALL | FS_POSSIBLE))
return (1);
if (file_init(sp, tp->frp, NULL, FS_SETALT))
return (1);
F_CLR(sp, SC_SCR_TOP);
F_SET(sp, SC_SCR_CENTER);
F_SET(sp, SC_FSWITCH);
return (0);
}
int
ex_tag_Nswitch(SCR *sp, TAG *tp, int force)
{
SCR *new;
if (tp->frp == NULL && (tp->frp = file_add(sp, tp->fname)) == NULL)
return (1);
if (screen_init(sp->gp, sp, &new))
return (1);
if (vs_split(sp, new, 0)) {
(void)file_end(new, new->ep, 1);
(void)screen_end(new);
return (1);
}
if (tp->frp == sp->frp) {
new->ep = sp->ep;
++new->ep->refcnt;
new->frp = tp->frp;
new->frp->flags = sp->frp->flags;
} else if (file_init(new, tp->frp, NULL, force)) {
(void)vs_discard(new, NULL);
(void)screen_end(new);
return (1);
}
new->cargv = new->argv = ex_buildargv(sp, NULL, tp->frp->name);
F_CLR(new, SC_SCR_TOP);
F_SET(new, SC_SCR_CENTER);
sp->nextdisp = new;
F_SET(sp, SC_SSWITCH);
return (0);
}
int
ex_tag_pop(SCR *sp, EXCMD *cmdp)
{
EX_PRIVATE *exp;
TAGQ *tqp, *dtqp = NULL;
size_t arglen;
long off;
char *arg, *p, *t;
exp = EXP(sp);
if (TAILQ_EMPTY(&exp->tq)) {
tag_msg(sp, TAG_EMPTY, NULL);
return (1);
}
switch (cmdp->argc) {
case 0:
dtqp = TAILQ_FIRST(&exp->tq);
break;
case 1:
arg = cmdp->argv[0]->bp;
off = strtol(arg, &p, 10);
if (*p != '\0')
goto filearg;
if (off < 1)
return (0);
TAILQ_FOREACH(tqp, &exp->tq, q) {
if (--off <= 1)
break;
}
if (tqp == NULL) {
msgq(sp, M_ERR,
"Less than %s entries on the tags stack; use :display t[ags]",
arg);
return (1);
}
dtqp = tqp;
break;
filearg: arglen = strlen(arg);
for (tqp = TAILQ_FIRST(&exp->tq); tqp;
dtqp = tqp, tqp = TAILQ_NEXT(tqp, q)) {
if (tqp == TAILQ_FIRST(&exp->tq))
continue;
p = tqp->current->frp->name;
if ((t = strrchr(p, '/')) == NULL)
t = p;
else
++t;
if (!strncmp(arg, t, arglen))
break;
}
if (tqp == NULL) {
msgq_str(sp, M_ERR, arg,
"No file %s on the tags stack to return to; use :display t[ags]");
return (1);
}
if (tqp == TAILQ_FIRST(&exp->tq))
return (0);
break;
default:
abort();
}
return (tag_pop(sp, dtqp, FL_ISSET(cmdp->iflags, E_C_FORCE)));
}
int
ex_tag_top(SCR *sp, EXCMD *cmdp)
{
EX_PRIVATE *exp;
exp = EXP(sp);
if (TAILQ_EMPTY(&exp->tq)) {
tag_msg(sp, TAG_EMPTY, NULL);
return (1);
}
return (tag_pop(sp,
TAILQ_PREV(TAILQ_LAST(&exp->tq, _tqh), _tqh, q),
FL_ISSET(cmdp->iflags, E_C_FORCE)));
}
static int
tag_pop(SCR *sp, TAGQ *dtqp, int force)
{
EX_PRIVATE *exp;
TAG *tp;
TAGQ *tqp;
exp = EXP(sp);
tp = TAILQ_NEXT(dtqp, q)->current;
if (tp->frp == sp->frp) {
sp->lno = tp->lno;
sp->cno = tp->cno;
} else {
if (file_m1(sp, force, FS_ALL | FS_POSSIBLE))
return (1);
tp->frp->lno = tp->lno;
tp->frp->cno = tp->cno;
F_SET(sp->frp, FR_CURSORSET);
if (file_init(sp, tp->frp, NULL, FS_SETALT))
return (1);
F_SET(sp, SC_FSWITCH);
}
do {
tqp = TAILQ_FIRST(&exp->tq);
if (tagq_free(sp, tqp))
return (0);
} while (tqp != dtqp);
if (TAILQ_NEXT(TAILQ_FIRST(&exp->tq), q) == NULL)
tagq_free(sp, TAILQ_FIRST(&exp->tq));
return (0);
}
int
ex_tag_display(SCR *sp)
{
EX_PRIVATE *exp;
TAG *tp;
TAGQ *tqp;
int cnt;
size_t len;
char *p;
exp = EXP(sp);
if (TAILQ_EMPTY(&exp->tq)) {
tag_msg(sp, TAG_EMPTY, NULL);
return (0);
}
tqp = TAILQ_FIRST(&exp->tq);
#define L_NAME 30
#define L_SLOP 4
#define L_SPACE 5
#define L_TAG 20
if (sp->cols <= L_NAME + L_SLOP) {
msgq(sp, M_ERR, "Display too small.");
return (0);
}
cnt = 0;
TAILQ_FOREACH(tqp, &exp->tq, q) {
if (INTERRUPTED(sp))
break;
++cnt;
TAILQ_FOREACH(tp, &tqp->tagq, q) {
if (tp == TAILQ_FIRST(&tqp->tagq))
(void)ex_printf(sp, "%2d ", cnt);
else
(void)ex_printf(sp, " ");
p = tp->frp == NULL ? tp->fname : tp->frp->name;
if ((len = strlen(p)) > L_NAME) {
len = len - (L_NAME - 4);
(void)ex_printf(sp, " ... %*.*s",
L_NAME - 4, L_NAME - 4, p + len);
} else
(void)ex_printf(sp,
" %*.*s", L_NAME, L_NAME, p);
if (tqp->current == tp)
(void)ex_printf(sp, "*");
if (tp == TAILQ_FIRST(&tqp->tagq) && tqp->tag != NULL &&
(sp->cols - L_NAME) >= L_TAG + L_SPACE) {
len = strlen(tqp->tag);
if (len > sp->cols - (L_NAME + L_SPACE))
len = sp->cols - (L_NAME + L_SPACE);
(void)ex_printf(sp, "%s%.*s",
tqp->current == tp ? " " : " ",
(int)len, tqp->tag);
}
(void)ex_printf(sp, "\n");
}
}
return (0);
}
int
ex_tag_copy(SCR *orig, SCR *sp)
{
EX_PRIVATE *oexp, *nexp;
TAGQ *aqp, *tqp;
TAG *ap, *tp;
TAGF *atfp, *tfp;
oexp = EXP(orig);
nexp = EXP(sp);
TAILQ_FOREACH(aqp, &oexp->tq, q) {
if (tagq_copy(sp, aqp, &tqp))
return (1);
TAILQ_FOREACH(ap, &aqp->tagq, q) {
if (tag_copy(sp, ap, &tp))
return (1);
if (aqp->current == ap)
tqp->current = tp;
TAILQ_INSERT_TAIL(&tqp->tagq, tp, q);
}
TAILQ_INSERT_TAIL(&nexp->tq, tqp, q);
}
TAILQ_FOREACH(atfp, &oexp->tagfq, q) {
if (tagf_copy(sp, atfp, &tfp))
return (1);
TAILQ_INSERT_TAIL(&nexp->tagfq, tfp, q);
}
if (oexp->tag_last != NULL &&
(nexp->tag_last = strdup(oexp->tag_last)) == NULL) {
msgq(sp, M_SYSERR, NULL);
return (1);
}
return (0);
}
static int
tagf_copy(SCR *sp, TAGF *otfp, TAGF **tfpp)
{
TAGF *tfp;
MALLOC_RET(sp, tfp, sizeof(TAGF));
*tfp = *otfp;
if ((tfp->name = strdup(otfp->name)) == NULL) {
free(tfp);
return (1);
}
*tfpp = tfp;
return (0);
}
static int
tagq_copy(SCR *sp, TAGQ *otqp, TAGQ **tqpp)
{
TAGQ *tqp;
size_t len;
len = sizeof(TAGQ);
if (otqp->tag != NULL)
len += otqp->tlen + 1;
MALLOC_RET(sp, tqp, len);
memcpy(tqp, otqp, len);
TAILQ_INIT(&tqp->tagq);
tqp->current = NULL;
if (otqp->tag != NULL)
tqp->tag = tqp->buf;
*tqpp = tqp;
return (0);
}
static int
tag_copy(SCR *sp, TAG *otp, TAG **tpp)
{
TAG *tp;
size_t len;
len = sizeof(TAG);
if (otp->fname != NULL)
len += otp->fnlen + 1;
if (otp->search != NULL)
len += otp->slen + 1;
MALLOC_RET(sp, tp, len);
memcpy(tp, otp, len);
if (otp->fname != NULL)
tp->fname = tp->buf;
if (otp->search != NULL)
tp->search = tp->fname + otp->fnlen + 1;
*tpp = tp;
return (0);
}
static int
tagf_free(SCR *sp, TAGF *tfp)
{
EX_PRIVATE *exp;
exp = EXP(sp);
TAILQ_REMOVE(&exp->tagfq, tfp, q);
free(tfp->name);
free(tfp);
return (0);
}
int
tagq_free(SCR *sp, TAGQ *tqp)
{
EX_PRIVATE *exp;
TAGQ *ttqp;
TAG *tp;
exp = EXP(sp);
while ((tp = TAILQ_FIRST(&tqp->tagq))) {
TAILQ_REMOVE(&tqp->tagq, tp, q);
free(tp);
}
TAILQ_FOREACH(ttqp, &exp->tq, q) {
if (ttqp == tqp) {
TAILQ_REMOVE(&exp->tq, tqp, q);
break;
}
}
free(tqp);
return (0);
}
void
tag_msg(SCR *sp, tagmsg_t msg, char *tag)
{
switch (msg) {
case TAG_BADLNO:
msgq_str(sp, M_ERR, tag,
"%s: the tag's line number is past the end of the file");
break;
case TAG_EMPTY:
msgq(sp, M_INFO, "The tags stack is empty");
break;
case TAG_SEARCH:
msgq_str(sp, M_ERR, tag, "%s: search pattern not found");
break;
default:
abort();
}
}
int
ex_tagf_alloc(SCR *sp, char *str)
{
EX_PRIVATE *exp;
TAGF *tfp;
size_t len;
char *p, *t;
exp = EXP(sp);
while ((tfp = TAILQ_FIRST(&exp->tagfq)) != NULL)
tagf_free(sp, tfp);
for (p = t = str;; ++p) {
if (*p == '\0' || isblank(*p)) {
if ((len = p - t) > 1) {
MALLOC_RET(sp, tfp, sizeof(TAGF));
MALLOC(sp, tfp->name, len + 1);
if (tfp->name == NULL) {
free(tfp);
return (1);
}
memcpy(tfp->name, t, len);
tfp->name[len] = '\0';
tfp->flags = 0;
TAILQ_INSERT_TAIL(&exp->tagfq, tfp, q);
}
t = p + 1;
}
if (*p == '\0')
break;
}
return (0);
}
int
ex_tag_free(SCR *sp)
{
EX_PRIVATE *exp;
TAGF *tfp;
TAGQ *tqp;
exp = EXP(sp);
while ((tqp = TAILQ_FIRST(&exp->tq)))
tagq_free(sp, tqp);
while ((tfp = TAILQ_FIRST(&exp->tagfq)) != NULL)
tagf_free(sp, tfp);
free(exp->tag_last);
return (0);
}
static int
ctag_search(SCR *sp, char *search, size_t slen, char *tag)
{
MARK m;
char *p;
if (isdigit(search[0])) {
m.lno = atoi(search);
if (!db_exist(sp, m.lno)) {
tag_msg(sp, TAG_BADLNO, tag);
return (1);
}
} else {
m.lno = 1;
m.cno = 0;
if (f_search(sp, &m, &m,
search, slen, NULL, SEARCH_FILE | SEARCH_TAG)) {
if ((p = strrchr(search, '(')) != NULL) {
slen = p - search;
if (f_search(sp, &m, &m, search, slen,
NULL, SEARCH_FILE | SEARCH_TAG))
goto notfound;
} else {
notfound: tag_msg(sp, TAG_SEARCH, tag);
return (1);
}
}
if (sp->searchdir == NOTSET)
sp->searchdir = FORWARD;
}
sp->lno = m.lno;
sp->cno = 0;
(void)nonblank(sp, sp->lno, &sp->cno);
return (0);
}
static TAGQ *
ctag_slist(SCR *sp, char *tag)
{
EX_PRIVATE *exp;
TAGF *tfp;
TAGQ *tqp;
size_t len;
int echk;
exp = EXP(sp);
len = strlen(tag);
CALLOC_GOTO(sp, tqp, 1, sizeof(TAGQ) + len + 1);
TAILQ_INIT(&tqp->tagq);
tqp->tag = tqp->buf;
memcpy(tqp->tag, tag, (tqp->tlen = len) + 1);
echk = 0;
TAILQ_FOREACH(tfp, &exp->tagfq, q)
if (ctag_sfile(sp, tfp, tqp, tag)) {
echk = 1;
F_SET(tfp, TAGF_ERR);
} else
F_CLR(tfp, TAGF_ERR | TAGF_ERR_WARN);
if (TAILQ_EMPTY(&tqp->tagq)) {
msgq_str(sp, M_ERR, tag, "%s: tag not found");
if (echk)
TAILQ_FOREACH(tfp, &exp->tagfq, q)
if (F_ISSET(tfp, TAGF_ERR) &&
!F_ISSET(tfp, TAGF_ERR_WARN)) {
errno = tfp->errnum;
msgq_str(sp, M_SYSERR, tfp->name, "%s");
F_SET(tfp, TAGF_ERR_WARN);
}
free(tqp);
return (NULL);
}
tqp->current = TAILQ_FIRST(&tqp->tagq);
return (tqp);
alloc_err:
return (NULL);
}
static int
ctag_sfile(SCR *sp, TAGF *tfp, TAGQ *tqp, char *tname)
{
struct stat sb;
TAG *tp;
size_t dlen, nlen, slen;
int fd, i, nf1, nf2;
char *back, *cname, *dname, *front, *map, *name, *p, *search, *t;
if ((fd = open(tfp->name, O_RDONLY)) < 0) {
tfp->errnum = errno;
return (1);
}
if (fstat(fd, &sb) != 0 ||
(map = mmap(NULL, (size_t)sb.st_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE, fd, (off_t)0)) == MAP_FAILED) {
tfp->errnum = errno;
(void)close(fd);
return (1);
}
front = map;
back = front + sb.st_size;
front = binary_search(tname, front, back);
front = linear_search(tname, front, back);
if (front == NULL)
goto done;
for (;;) {
for (p = front; p < back && *p != '\n'; ++p);
if (p == back || *p != '\n')
break;
*p = '\0';
t = p + 1;
p = front;
front = t;
for (i = 0; i < 2 && (t = strsep(&p, "\t ")) != NULL; ++i)
switch (i) {
case 0:
cname = t;
break;
case 1:
name = t;
nlen = strlen(name);
break;
}
if (i != 2 || p == NULL || t == NULL)
goto corrupt;
search = p;
if ((slen = strlen(p)) == 0) {
corrupt: p = msg_print(sp, tname, &nf1);
t = msg_print(sp, tfp->name, &nf2);
msgq(sp, M_ERR, "%s: corrupted tag in %s", p, t);
if (nf1)
FREE_SPACE(sp, p, 0);
if (nf2)
FREE_SPACE(sp, t, 0);
continue;
}
if (strcmp(tname, cname))
break;
ctag_file(sp, tfp, name, &dname, &dlen);
CALLOC_GOTO(sp, tp,
1, sizeof(TAG) + dlen + 2 + nlen + 1 + slen + 1);
tp->fname = tp->buf;
if (dlen != 0) {
memcpy(tp->fname, dname, dlen);
tp->fname[dlen] = '/';
++dlen;
}
memcpy(tp->fname + dlen, name, nlen + 1);
tp->fnlen = dlen + nlen;
tp->search = tp->fname + tp->fnlen + 1;
memcpy(tp->search, search, (tp->slen = slen) + 1);
TAILQ_INSERT_TAIL(&tqp->tagq, tp, q);
}
alloc_err:
done: if (munmap(map, (size_t)sb.st_size))
msgq(sp, M_SYSERR, "munmap");
if (close(fd))
msgq(sp, M_SYSERR, "close");
return (0);
}
static void
ctag_file(SCR *sp, TAGF *tfp, char *name, char **dirp, size_t *dlenp)
{
struct stat sb;
char *p, buf[PATH_MAX];
*dlenp = 0;
if (name[0] != '/' &&
stat(name, &sb) && (p = strrchr(tfp->name, '/')) != NULL) {
*p = '\0';
(void)snprintf(buf, sizeof(buf), "%s/%s", tfp->name, name);
if (stat(buf, &sb) == 0) {
*dirp = tfp->name;
*dlenp = strlen(*dirp);
}
*p = '/';
}
}
#define EQUAL 0
#define GREATER 1
#define LESS (-1)
#define SKIP_PAST_NEWLINE(p, back) while ((p) < (back) && *(p)++ != '\n');
static char *
binary_search(char *string, char *front, char *back)
{
char *p;
p = front + (back - front) / 2;
SKIP_PAST_NEWLINE(p, back);
while (p != back) {
if (compare(string, p, back) == GREATER)
front = p;
else
back = p;
p = front + (back - front) / 2;
SKIP_PAST_NEWLINE(p, back);
}
return (front);
}
static char *
linear_search(char *string, char *front, char *back)
{
while (front < back) {
switch (compare(string, front, back)) {
case EQUAL:
return (front);
case LESS:
return (NULL);
case GREATER:
break;
}
SKIP_PAST_NEWLINE(front, back);
}
return (NULL);
}
static int
compare(char *s1, char *s2, char *back)
{
for (; *s1 && s2 < back && (*s2 != '\t' && *s2 != ' '); ++s1, ++s2)
if (*s1 != *s2)
return (*s1 < *s2 ? LESS : GREATER);
return (*s1 ? GREATER : s2 < back &&
(*s2 != '\t' && *s2 != ' ') ? LESS : EQUAL);
}