#include <sys/queue.h>
#include <ctype.h>
#include <limits.h>
#include <regex.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "def.h"
#include "funmap.h"
#ifdef MGLOG
#include "kbd.h"
#include "log.h"
#endif
static int multiarg(char *, char *, int);
static int isvar(char **, char **, int);
static int founddef(char *, int, int, int, int);
static int foundlst(char *, int, int, int);
static int expandvals(char *, char *, char *);
static int foundfun(char *, int);
static int doregex(char *, char *);
static void clearexp(void);
static int parse(char *, const char *, const char *, int, int, int, int);
static int parsdef(char *, const char *, const char *, int, int, int);
static int parsval(char *, const char *, const char *, int, int, int);
static int parsexp(char *, const char *, const char *, int, int, int);
static int exitinterpreter(char *, char *, int);
TAILQ_HEAD(exphead, expentry) ehead;
struct expentry {
TAILQ_ENTRY(expentry) eentry;
char *fun;
char funbuf[BUFSIZE];
const char *par1;
const char *par2;
int expctr;
int blkid;
};
#define NUMSCHKEYS 4
#define MAXLENSCHKEYS 17
char scharkey[NUMSCHKEYS][MAXLENSCHKEYS] =
{
"define",
"list",
"if",
"lambda"
};
static const char lp = '(';
static const char rp = ')';
static char *defnam = NULL;
static int lnm;
int
foundparen(char *funstr, int llen, int lnum)
{
const char *lrp = NULL;
char *p, *begp = NULL, *endp = NULL, *prechr;
char *lastchr = NULL;
int i, ret, pctr, expctr, blkid, inquote, esc;
int elen, spc, ns;
pctr = expctr = inquote = esc = elen = spc = ns = 0;
blkid = 1;
lnm = lnum;
TAILQ_INIT(&ehead);
p = funstr;
for (i = 0; i < llen; ++i, p++) {
if (pctr == 0 && *p != ' ' && *p != '\t' && *p != '(') {
if (*p == ')')
return(dobeep_num("Extra ')' found on line:",
lnm));
return(dobeep_num("Error line:", lnm));
}
if (begp != NULL)
elen++;
if (*p == '\\') {
esc = 1;
} else if (*p == '(') {
if (lastchr != NULL && *lastchr == '(')
return(dobeep_num("Multiple consecutive "\
"left parantheses line", lnm));
if (inquote == 0) {
if (begp != NULL) {
if (*prechr == ' ')
ns--;
if (endp == NULL)
*p = '\0';
else
*endp = '\0';
ret = parse(begp, lrp, &lp, blkid,
++expctr, elen - spc, ns);
if (!ret) {
cleanup();
return(ret);
}
elen = 0;
}
lrp = &lp;
begp = endp = NULL;
pctr++;
} else if (inquote != 1) {
cleanup();
return(dobeep_num("Opening and closing quote "\
"char error line:", lnm));
}
esc = spc = 0;
} else if (*p == ')') {
if (lastchr != NULL && *lastchr == '(')
return(dobeep_num("Empty parenthesis "\
"not supported line", lnm));
if (inquote == 0) {
if (begp != NULL) {
if (*prechr == ' ')
ns--;
if (endp == NULL)
*p = '\0';
else
*endp = '\0';
ret = parse(begp, lrp, &rp, blkid,
++expctr, elen - spc, ns);
if (!ret) {
cleanup();
return(ret);
}
elen = 0;
}
lrp = &rp;
begp = endp = NULL;
pctr--;
} else if (inquote != 1) {
cleanup();
return(dobeep_num("Opening and closing quote "\
"char error line:", lnm));
}
esc = spc = 0;
} else if (*p != ' ' && *p != '\t') {
if (begp == NULL) {
begp = p;
if (*begp == '"' || isdigit(*begp))
return(dobeep_num("First char of "\
"expression error line:", lnm));
}
if (*p == '"') {
if (inquote == 0 && esc == 0) {
if (*prechr != ' ' && *prechr != '\t')
return(dobeep_num("Parse error"\
" line:", lnm));
inquote++;
} else if (inquote > 0 && esc == 1)
esc = 0;
else
inquote--;
} else if (*prechr == '"' && inquote == 0) {
return(dobeep_num("Parse error line:", lnm));
}
endp = NULL;
spc = 0;
} else if (endp == NULL && (*p == ' ' || *p == '\t')) {
if (inquote == 0) {
*p = ' ';
endp = p;
spc++;
if (begp != NULL)
ns++;
}
esc = 0;
} else if (*p == '\t' || *p == ' ') {
if (inquote == 0) {
*p = ' ';
spc++;
}
esc = 0;
}
if (*p != '\t' && *p != ' ' && inquote == 0)
lastchr = p;
if (pctr == 0) {
blkid++;
expctr = 0;
defnam = NULL;
}
prechr = p;
}
if (pctr != 0) {
cleanup();
return(dobeep_num("Opening and closing parentheses error line:",
lnm));
}
if (ret == FALSE)
cleanup();
else
clearexp();
return (ret);
}
static int
parse(char *begp, const char *par1, const char *par2, int blkid, int expctr,
int elen, int ns)
{
char *regs;
int ret = FALSE;
if (strncmp(begp, "define", 6) == 0) {
ret = parsdef(begp, par1, par2, blkid, expctr, elen);
if (ret == TRUE || ret == FALSE)
return (ret);
} else if (strncmp(begp, "list", 4) == 0)
return(parsval(begp, par1, par2, blkid, expctr, elen));
regs = "^exit$";
if (doregex(regs, begp))
return(exitinterpreter(NULL, NULL, FALSE));
regs = "^[A-Za-z-]+$";
if (doregex(regs, begp))
return(excline(begp, 0, 0));
if (strncmp(begp, "global-set-key ", 15) == 0)
return(excline(begp, 0, 0));
if (strncmp(begp, "define-key ", 11) == 0)
return(excline(begp, 0, 0));
return (parsexp(begp, par1, par2, blkid, expctr, elen));
}
static int
parsdef(char *begp, const char *par1, const char *par2, int blkid, int expctr,
int elen)
{
char *regs;
if ((defnam == NULL) && (expctr != 1))
return(dobeep_num("'define' incorrectly used line:", lnm));
regs = "^define[ ]+[A-Za-z][.0-9_A-Z+a-z-]*[ ]+.+[ ]+.+$";
if (doregex(regs, begp))
return(dobeep_num("Invalid use of define line:", lnm));
regs = "^define[ ]+[A-Za-z][.0-9_A-Z+a-z-]*[ ]+.*$";
if (doregex(regs, begp)) {
if (par1 == &lp && par2 == &rp && expctr == 1)
return(founddef(begp, blkid, expctr, 1, elen));
return(dobeep_num("Invalid use of define line:", lnm));
}
regs = "^define[ ]+[A-Za-z][.0-9_A-Z+a-z-]*[ ]*$";
if (doregex(regs, begp)) {
if (par1 == &lp && par2 == &lp && expctr == 1)
return(founddef(begp, blkid, expctr, 0, elen));
return(dobeep_num("Invalid use of 'define' line:", lnm));
}
regs = "^define$";
if (doregex(regs, begp)) {
if (par1 == &lp && par2 == &lp && expctr == 1)
return(foundfun(begp, expctr));
return(dobeep_num("Invalid use of 'define' line:", lnm));
}
return (ABORT);
}
static int
parsval(char *begp, const char *par1, const char *par2, int blkid, int expctr,
int elen)
{
char *regs;
regs = "^list$";
if (doregex(regs, begp))
return(dobeep_num("Invalid use of list line:", lnm));
regs = "^list[ ]+.*$";
if (doregex(regs, begp)) {
if (expctr == 1)
return(dobeep_num("list with no-where to go.", lnm));
if (par1 == &lp && expctr > 1)
return(foundlst(begp, blkid, expctr, elen));
return(dobeep_num("Invalid use of list line:", lnm));
}
return (FALSE);
}
static int
parsexp(char *begp, const char *par1, const char *par2, int blkid, int expctr,
int elen)
{
struct expentry *e1 = NULL;
PF funcp;
char *cmdp, *fendp, *valp, *fname, *funb = NULL;
int numparams, ret;
cmdp = begp;
fendp = strchr(cmdp, ' ');
*fendp = '\0';
if ((funcp = name_function(cmdp)) == NULL)
return (dobeep_msgs("Unknown command:", cmdp));
numparams = numparams_function(funcp);
if (numparams == 0)
return (dobeep_msgs("Command takes no arguments:", cmdp));
if (numparams == -1)
return (dobeep_msgs("Interactive command found:", cmdp));
if ((e1 = malloc(sizeof(struct expentry))) == NULL) {
cleanup();
return (dobeep_msg("malloc Error"));
}
TAILQ_INSERT_HEAD(&ehead, e1, eentry);
if ((e1->fun = strndup(cmdp, BUFSIZE)) == NULL) {
cleanup();
return(dobeep_msg("strndup error"));
}
cmdp = e1->fun;
fname = e1->fun;
e1->funbuf[0] = '\0';
funb = e1->funbuf;
e1->expctr = expctr;
e1->blkid = blkid;
e1->par1 = par1;
e1->par2 = par2;
*fendp = ' ';
valp = fendp + 1;
ret = expandvals(cmdp, valp, funb);
if (!ret)
return (ret);
return (multiarg(fname, funb, numparams));
}
static int
multiarg(char *cmdp, char *argbuf, int numparams)
{
char excbuf[BUFSIZE];
char *argp, *p, *s = " ";
char *regs;
int spc, numspc;
int fin, inquote;
argp = argbuf;
spc = 1;
numspc = fin = inquote = 0;
for (p = argbuf; *p != '\0'; p++) {
if (*(p + 1) == '\0')
fin = 1;
if (*p != ' ') {
if (*p == '"') {
if (inquote == 1)
inquote = 0;
else
inquote = 1;
}
if (spc == 1)
if ((numspc % numparams) == 0) {
argp = p;
}
spc = 0;
}
if ((*p == ' ' && inquote == 0) || fin) {
if (spc == 1)
continue;
if ((numspc % numparams) != (numparams - 1)) {
numspc++;
continue;
}
if (*p == ' ') {
*p = '\0';
}
excbuf[0] = '\0';
regs = "[\"]+.*[\"]+";
if (!doregex(regs, argp)) {
const char *errstr;
strtonum(argp, 0, INT_MAX, &errstr);
if (errstr != NULL)
return (dobeep_msgs("Var not found:",
argp));
}
if (strlcpy(excbuf, cmdp, sizeof(excbuf))
>= sizeof(excbuf))
return (dobeep_msg("strlcpy error"));
if (strlcat(excbuf, s, sizeof(excbuf))
>= sizeof(excbuf))
return (dobeep_msg("strlcat error"));
if (strlcat(excbuf, argp, sizeof(excbuf))
>= sizeof(excbuf))
return (dobeep_msg("strlcat error"));
excline(excbuf, 0, 0);
if (fin)
break;
*p = ' ';
numspc++;
spc = 1;
}
}
return (TRUE);
}
static int
isvar(char **argp, char **varbuf, int sizof)
{
struct varentry *v1 = NULL;
if (SLIST_EMPTY(&varhead))
return (FALSE);
#ifdef MGLOG
mglog_isvar(*varbuf, *argp, sizof);
#endif
SLIST_FOREACH(v1, &varhead, entry) {
if (strcmp(*argp, v1->v_name) == 0) {
(void)(strlcpy(*varbuf, v1->v_buf, sizof) >= sizof);
return (TRUE);
}
}
return (FALSE);
}
static int
foundfun(char *defstr, int expctr)
{
return (TRUE);
}
static int
foundlst(char *defstr, int blkid, int expctr, int elen)
{
char *p;
p = strstr(defstr, " ");
p = skipwhite(p);
expandvals(NULL, p, defnam);
return (TRUE);
}
static int
founddef(char *defstr, int blkid, int expctr, int hasval, int elen)
{
struct varentry *vt, *v1 = NULL;
char *p, *vnamep, *vendp = NULL, *valp;
p = strstr(defstr, " ");
vnamep = skipwhite(p);
vendp = vnamep;
while (1) {
++vendp;
if (*vendp == ' ')
break;
}
*vendp = '\0';
if (name_function(vnamep) != NULL)
return(dobeep_msgs("Variable/function name clash:", vnamep));
if (!SLIST_EMPTY(&varhead)) {
SLIST_FOREACH_SAFE(v1, &varhead, entry, vt) {
if (strcmp(vnamep, v1->v_name) == 0) {
SLIST_REMOVE(&varhead, v1, varentry, entry);
free(v1->v_name);
free(v1);
}
}
}
if ((v1 = malloc(sizeof(struct varentry))) == NULL)
return (ABORT);
if ((v1->v_name = strndup(vnamep, BUFSIZE)) == NULL) {
free(v1);
return(dobeep_msg("strndup error"));
}
SLIST_INSERT_HEAD(&varhead, v1, entry);
vnamep = v1->v_name;
v1->v_count = 0;
v1->v_vals = NULL;
v1->v_buf[0] = '\0';
defnam = v1->v_buf;
if (hasval) {
valp = skipwhite(vendp + 1);
expandvals(NULL, valp, defnam);
defnam = NULL;
}
*vendp = ' ';
return (TRUE);
}
static int
expandvals(char *cmdp, char *valp, char *bp)
{
char excbuf[BUFSIZE], argbuf[BUFSIZE];
char contbuf[BUFSIZE], varbuf[BUFSIZE];
char *argp, *endp, *p, *v, *s = " ";
char *regs;
int spc, cnt;
int sizof, fin, inquote;
p = skipwhite(valp);
if (strlcpy(argbuf, p, sizeof(argbuf)) >= sizeof(argbuf))
return (dobeep_msg("strlcpy error"));
argp = argbuf;
spc = 1;
fin = inquote = cnt = spc = 0;
for (p = argbuf; *p != '\0'; p++) {
if (*(p + 1) == '\0')
fin = 1;
if (*p != ' ') {
if (*p == '"') {
if (inquote == 1)
inquote = 0;
else
inquote = 1;
}
if (spc == 1)
argp = p;
spc = 0;
}
if ((*p == ' ' && inquote == 0) || fin) {
if (spc == 1)
continue;
if (*p == ' ') {
*p = '\0';
}
endp = p + 1;
excbuf[0] = '\0';
varbuf[0] = '\0';
contbuf[0] = '\0';
sizof = sizeof(varbuf);
v = varbuf;
regs = "[\"]+.*[\"]+";
if (doregex(regs, argp))
;
else if (isvar(&argp, &v, sizof)) {
(void)(strlcat(varbuf, " ",
sizof) >= sizof);
*p = ' ';
(void)(strlcpy(contbuf, endp,
sizeof(contbuf)) >= sizeof(contbuf));
(void)(strlcat(varbuf, contbuf,
sizof) >= sizof);
argbuf[0] = ' ';
argbuf[1] = '\0';
(void)(strlcat(argbuf, varbuf,
sizof) >= sizof);
p = argp = argbuf;
spc = 1;
fin = 0;
continue;
} else {
const char *errstr;
strtonum(argp, 0, INT_MAX, &errstr);
if (errstr != NULL)
return (dobeep_msgs("Var not found:",
argp));
}
#ifdef MGLOG
mglog_misc("x|%s|%p|%d|\n", bp, defnam, BUFSIZE);
#endif
if (*bp != '\0') {
if (strlcat(bp, s, BUFSIZE) >= BUFSIZE)
return (dobeep_msg("strlcat error"));
}
if (strlcat(bp, argp, BUFSIZE) >= BUFSIZE) {
return (dobeep_msg("strlcat error"));
}
if (fin)
break;
*p = ' ';
spc = 1;
}
}
return (TRUE);
}
static void
clearexp(void)
{
struct expentry *e1 = NULL;
while (!TAILQ_EMPTY(&ehead)) {
e1 = TAILQ_FIRST(&ehead);
TAILQ_REMOVE(&ehead, e1, eentry);
free(e1->fun);
free(e1);
}
return;
}
void
cleanup(void)
{
defnam = NULL;
clearexp();
}
static int
doregex(char *r, char *e)
{
regex_t regex_buff;
if (regcomp(®ex_buff, r, REG_EXTENDED)) {
regfree(®ex_buff);
return(dobeep_num("Regex compilation error line:", lnm));
}
if (!regexec(®ex_buff, e, 0, NULL, 0)) {
regfree(®ex_buff);
return(TRUE);
}
regfree(®ex_buff);
return(FALSE);
}
static int
exitinterpreter(char *ptr, char *dobuf, int dosiz)
{
cleanup();
if (batch == 0)
return(dobeep_msg("Interpreter exited via exit command."));
return(FALSE);
}