#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/tree.h>
#include <sys/types.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
#include "def.h"
struct ctag;
static int addctag(char *);
static int atbow(void);
void closetags(void);
static int ctagcmp(struct ctag *, struct ctag *);
static int loadbuffer(char *);
static int loadtags(const char *);
static int pushtag(char *);
static int searchpat(char *);
static struct ctag *searchtag(char *);
static char *strip(char *, size_t);
static void unloadtags(void);
#define DEFAULTFN "tags"
struct ctag {
RB_ENTRY(ctag) entry;
char *tag;
char *fname;
char *pat;
};
RB_HEAD(tagtree, ctag) tags = RB_INITIALIZER(&tags);
RB_GENERATE(tagtree, ctag, entry, ctagcmp);
struct tagpos {
SLIST_ENTRY(tagpos) entry;
int doto;
int dotline;
char *bname;
};
SLIST_HEAD(tagstack, tagpos) shead = SLIST_HEAD_INITIALIZER(shead);
int
ctagcmp(struct ctag *s, struct ctag *t)
{
return strcmp(s->tag, t->tag);
}
int
tagsvisit(int f, int n)
{
char fname[NFILEN], *bufp, *temp;
if (getbufcwd(fname, sizeof(fname)) == FALSE)
fname[0] = '\0';
if (strlcat(fname, DEFAULTFN, sizeof(fname)) >= sizeof(fname)) {
dobeep();
ewprintf("Filename too long");
return (FALSE);
}
bufp = eread("Visit tags table (default %s): ", fname,
NFILEN, EFFILE | EFCR | EFNEW | EFDEF, DEFAULTFN);
if (bufp == NULL)
return (ABORT);
if (!RB_EMPTY(&tags)) {
if (eyorn("Keep current list of tags table also") == FALSE) {
ewprintf("Starting a new list of tags table");
unloadtags();
}
}
temp = bufp;
if (temp[0] == '\0')
temp = fname;
return (loadtags(temp));
}
int
findtag(int f, int n)
{
char utok[MAX_TOKEN], dtok[MAX_TOKEN];
char *tok, *bufp;
int ret;
if (curtoken(f, n, dtok) == FALSE) {
dtok[0] = '\0';
bufp = eread("Find tag: ", utok, MAX_TOKEN, EFNUL | EFNEW);
} else
bufp = eread("Find tag (default %s): ", utok, MAX_TOKEN,
EFNUL | EFNEW, dtok);
if (bufp == NULL)
return (ABORT);
else if (bufp[0] == '\0')
tok = dtok;
else
tok = utok;
if (tok[0] == '\0') {
dobeep();
ewprintf("There is no default tag");
return (FALSE);
}
if (RB_EMPTY(&tags))
if ((ret = tagsvisit(f, n)) != TRUE)
return (ret);
return pushtag(tok);
}
void
unloadtags(void)
{
struct ctag *var, *nxt;
for (var = RB_MIN(tagtree, &tags); var != NULL; var = nxt) {
nxt = RB_NEXT(tagtree, &tags, var);
RB_REMOVE(tagtree, &tags, var);
free(var->tag);
free(var);
}
}
int
pushtag(char *tok)
{
struct ctag *res;
struct tagpos *s;
char bname[NFILEN];
int doto, dotline;
if ((res = searchtag(tok)) == NULL)
return (FALSE);
doto = curwp->w_doto;
dotline = curwp->w_dotline;
if (strlcpy(bname, curbp->b_cwd, sizeof(bname)) >= sizeof(bname)) {
dobeep();
ewprintf("filename too long");
return (FALSE);
}
if (strlcat(bname, curbp->b_bname, sizeof(bname)) >= sizeof(bname)) {
dobeep();
ewprintf("filename too long");
return (FALSE);
}
if (loadbuffer(res->fname) == FALSE)
return (FALSE);
if (searchpat(res->pat) == TRUE) {
if ((s = malloc(sizeof(struct tagpos))) == NULL) {
dobeep();
ewprintf("Out of memory");
return (FALSE);
}
if ((s->bname = strdup(bname)) == NULL) {
dobeep();
ewprintf("Out of memory");
free(s);
return (FALSE);
}
s->doto = doto;
s->dotline = dotline;
SLIST_INSERT_HEAD(&shead, s, entry);
return (TRUE);
} else {
dobeep();
ewprintf("%s: pattern not found", res->tag);
return (FALSE);
}
return (FALSE);
}
int
poptag(int f, int n)
{
struct line *dotp;
struct tagpos *s;
if (SLIST_EMPTY(&shead)) {
dobeep();
ewprintf("No previous location for find-tag invocation");
return (FALSE);
}
s = SLIST_FIRST(&shead);
SLIST_REMOVE_HEAD(&shead, entry);
if (loadbuffer(s->bname) == FALSE) {
free(s->bname);
free(s);
return (FALSE);
}
curwp->w_dotline = s->dotline;
curwp->w_doto = s->doto;
dotp = curwp->w_bufp->b_headp;
while (s->dotline--)
dotp = dotp->l_fp;
curwp->w_dotp = dotp;
free(s->bname);
free(s);
return (TRUE);
}
int
loadtags(const char *fn)
{
struct stat sb;
char *l;
FILE *fd;
if ((fd = fopen(fn, "r")) == NULL) {
dobeep();
ewprintf("Unable to open tags file: %s", fn);
return (FALSE);
}
if (fstat(fileno(fd), &sb) == -1) {
dobeep();
ewprintf("fstat: %s", strerror(errno));
fclose(fd);
return (FALSE);
}
if (!S_ISREG(sb.st_mode)) {
dobeep();
ewprintf("Not a regular file");
fclose(fd);
return (FALSE);
}
while ((l = fparseln(fd, NULL, NULL, "\\\\\0",
FPARSELN_UNESCCONT | FPARSELN_UNESCREST)) != NULL) {
if (addctag(l) == FALSE) {
fclose(fd);
return (FALSE);
}
}
fclose(fd);
return (TRUE);
}
void
closetags(void)
{
struct tagpos *s;
while (!SLIST_EMPTY(&shead)) {
s = SLIST_FIRST(&shead);
SLIST_REMOVE_HEAD(&shead, entry);
free(s->bname);
free(s);
}
unloadtags();
}
char *
strip(char *s, size_t len)
{
s[len - 1] = '\0';
if (s[len - 2] == '$')
s[len - 2] = '\0';
s++;
if (*s == '^')
s++;
return s;
}
int
addctag(char *s)
{
struct ctag *t = NULL;
char *l, *c;
if ((t = malloc(sizeof(struct ctag))) == NULL) {
dobeep();
ewprintf("Out of memory");
goto cleanup;
}
t->tag = s;
if ((l = strchr(s, '\t')) == NULL)
goto cleanup;
*l++ = '\0';
t->fname = l;
if ((l = strchr(l, '\t')) == NULL)
goto cleanup;
*l++ = '\0';
if (*l == '\0')
goto cleanup;
if ((c = strstr(l, ";\"")) != NULL)
*c = '\0';
t->pat = strip(l, strlen(l));
if (RB_INSERT(tagtree, &tags, t) != NULL) {
free(t);
free(s);
}
return (TRUE);
cleanup:
free(t);
free(s);
return (FALSE);
}
int
searchpat(char *s_pat)
{
struct line *lp;
int dotline;
size_t plen;
plen = strlen(s_pat);
dotline = 1;
lp = lforw(curbp->b_headp);
while (lp != curbp->b_headp) {
if (ltext(lp) != NULL && plen <= llength(lp) &&
(strncmp(s_pat, ltext(lp), plen) == 0)) {
curwp->w_doto = 0;
curwp->w_dotp = lp;
curwp->w_dotline = dotline;
return (TRUE);
} else {
lp = lforw(lp);
dotline++;
}
}
return (FALSE);
}
int
atbow(void)
{
if (curwp->w_doto == 0)
return (TRUE);
if (ISWORD(curwp->w_dotp->l_text[curwp->w_doto]) &&
!ISWORD(curwp->w_dotp->l_text[curwp->w_doto - 1]))
return (TRUE);
return (FALSE);
}
int
curtoken(int f, int n, char *token)
{
struct line *odotp;
int odoto, tdoto, odotline, size, r;
char c;
c = cinfo['_'];
cinfo['_'] = _MG_W;
odotp = curwp->w_dotp;
odoto = curwp->w_doto;
odotline = curwp->w_dotline;
if (!atbow())
if ((r = backword(f, n)) == FALSE)
goto cleanup;
tdoto = curwp->w_doto;
if ((r = forwword(f, n)) == FALSE)
goto cleanup;
while (ltext(curwp->w_dotp) &&
isspace(lgetc(curwp->w_dotp, tdoto)))
tdoto++;
size = curwp->w_doto - tdoto;
if (size <= 0 || size >= MAX_TOKEN ||
ltext(curwp->w_dotp) == NULL) {
r = FALSE;
goto cleanup;
}
strncpy(token, ltext(curwp->w_dotp) + tdoto, size);
token[size] = '\0';
r = TRUE;
cleanup:
cinfo['_'] = c;
curwp->w_dotp = odotp;
curwp->w_doto = odoto;
curwp->w_dotline = odotline;
return (r);
}
struct ctag *
searchtag(char *tok)
{
struct ctag t, *res;
t.tag = tok;
if ((res = RB_FIND(tagtree, &tags, &t)) == NULL) {
dobeep();
ewprintf("No tag containing %s", tok);
return (NULL);
}
return res;
}
int
loadbuffer(char *bname)
{
struct buffer *bufp;
char *adjf;
if (bname[0] == '*') {
if ((bufp = bfind(bname, FALSE)) != NULL) {
curbp = bufp;
return (showbuffer(bufp, curwp, WFFULL));
} else {
return (FALSE);
}
} else {
if ((adjf = adjustname(bname, TRUE)) == NULL)
return (FALSE);
if ((bufp = findbuffer(adjf)) == NULL)
return (FALSE);
}
curbp = bufp;
if (showbuffer(bufp, curwp, WFFULL) != TRUE)
return (FALSE);
if (bufp->b_fname[0] == '\0') {
if (readin(adjf) != TRUE) {
killbuffer(bufp);
return (FALSE);
}
}
return (TRUE);
}