ack/util/grind/commands.g

739 lines
13 KiB
Plaintext
Raw Normal View History

1994-06-24 11:31:16 +00:00
/* $Id$ */
1990-08-31 18:22:53 +00:00
/* Command grammar */
{
#include <stdio.h>
#include <alloc.h>
#include <signal.h>
#include "ops.h"
#include "class.h"
#include "position.h"
#include "file.h"
#include "idf.h"
#include "symbol.h"
#include "tree.h"
1990-09-07 14:56:24 +00:00
#include "langdep.h"
#include "token.h"
1990-09-12 16:13:59 +00:00
#include "expr.h"
1993-11-10 15:11:28 +00:00
#include "misc.h"
1990-08-31 18:22:53 +00:00
extern char *Salloc();
extern char *strindex();
extern char *strcpy();
1990-11-12 13:46:31 +00:00
extern void signal_child();
1990-08-31 18:22:53 +00:00
extern FILE *db_in;
extern int disable_intr;
extern p_tree run_command, print_command;
1990-08-31 18:22:53 +00:00
1990-11-12 13:46:31 +00:00
struct token tok, aside;
int errorgiven = 0;
int child_interrupted = 0;
int interrupted = 0;
int eof_seen = 0;
1990-11-12 13:46:31 +00:00
static int shellescape();
1990-09-07 14:56:24 +00:00
static int extended_charset = 0;
static int in_expression = 0;
1990-08-31 18:22:53 +00:00
1990-09-19 14:31:12 +00:00
#define binprio(op) ((*(currlang->binop_prio))(op))
#define unprio(op) ((*(currlang->unop_prio))(op))
1990-08-31 18:22:53 +00:00
}
%start Commands, commands;
%lexical LLlex;
commands
{ p_tree com, lastcom = 0;
int give_prompt;
1990-08-31 18:22:53 +00:00
}
:
1990-10-31 15:56:51 +00:00
{ errorgiven = 0; }
1990-08-31 18:22:53 +00:00
[ %persistent command_line(&com)
[ '\n' { give_prompt = 1; }
| %default ';' { give_prompt = 0; }
]
1990-08-31 18:22:53 +00:00
{ if (com) {
if (lastcom) {
1990-08-31 18:22:53 +00:00
freenode(lastcom);
lastcom = 0;
}
1990-10-17 17:00:03 +00:00
if (errorgiven) {
1990-11-06 12:23:41 +00:00
if (com != run_command) freenode(com);
1990-10-17 17:00:03 +00:00
com = 0;
}
else {
1990-11-12 13:46:31 +00:00
enterlog(com);
eval(com);
1990-08-31 18:22:53 +00:00
if (repeatable(com)) {
lastcom = com;
}
else if (! in_status(com) &&
com != run_command &&
com != print_command) {
1990-08-31 18:22:53 +00:00
freenode(com);
1990-10-17 17:00:03 +00:00
com = 0;
1990-08-31 18:22:53 +00:00
}
}
} else if (lastcom && ! errorgiven) {
1990-11-12 13:46:31 +00:00
enterlog(lastcom);
eval(lastcom);
}
if (give_prompt) {
errorgiven = 0;
interrupted = 0;
prompt();
}
1990-08-31 18:22:53 +00:00
}
]*
;
command_line(p_tree *p;)
:
{ *p = 0; }
[
1990-08-31 18:22:53 +00:00
list_command(p)
| file_command(p)
| run_command(p)
| stop_command(p)
| when_command(p)
| continue_command(p)
| step_command(p)
| next_command(p)
| regs_command(p)
1990-09-20 17:51:14 +00:00
| where_command(p)
1990-08-31 18:22:53 +00:00
| STATUS { *p = mknode(OP_STATUS); }
| DUMP { *p = mknode(OP_DUMP); }
1990-11-12 13:46:31 +00:00
| RESTORE opt_num(p) { *p = mknode(OP_RESTORE, *p); }
1990-08-31 18:22:53 +00:00
| delete_command(p)
| print_command(p)
1990-09-20 17:51:14 +00:00
| display_command(p)
1990-08-31 18:22:53 +00:00
| trace_command(p)
1990-09-19 14:31:12 +00:00
| set_command(p)
| help_command(p)
1990-09-20 17:51:14 +00:00
| FIND qualified_name(p){ *p = mknode(OP_FIND, *p); }
| WHICH qualified_name(p){ *p = mknode(OP_WHICH, *p); }
| able_command(p)
1990-11-12 13:46:31 +00:00
| '!' { (void) shellescape();
1990-11-06 12:23:41 +00:00
*p = mknode(OP_SHELL);
1990-10-31 15:56:51 +00:00
}
1990-11-06 12:23:41 +00:00
| source_command(p)
| log_command(p)
| frame_command(p)
|
]
1990-08-31 18:22:53 +00:00
;
1990-11-06 12:23:41 +00:00
frame_command(p_tree *p;)
:
FRAME
[ { *p = mknode(OP_FRAME, (p_tree) 0); }
| count(p) { *p = mknode(OP_FRAME, *p); }
| '-' count(p) { *p = mknode(OP_DOWN, *p); }
| '+' count(p) { *p = mknode(OP_UP, *p); }
]
;
1990-10-17 17:00:03 +00:00
1990-11-06 12:23:41 +00:00
source_command(p_tree *p;)
:
SOURCE { extended_charset = 1; }
name(p) { (*p)->t_idf = str2idf((*p)->t_str, 0); }
{ *p = mknode(OP_SOURCE, *p);
extended_charset = 0;
}
;
log_command(p_tree *p;)
1990-09-20 17:51:14 +00:00
:
1990-11-06 12:23:41 +00:00
LOG { extended_charset = 1; }
[ name(p) { (*p)->t_idf = str2idf((*p)->t_str, 0); }
| { *p = 0; }
1990-10-17 17:00:03 +00:00
]
1990-11-06 12:23:41 +00:00
{ *p = mknode(OP_LOG, *p);
extended_charset = 0;
}
;
where_command(p_tree *p;)
:
WHERE opt_num(p) { *p = mknode(OP_WHERE, *p); }
1990-09-20 17:51:14 +00:00
;
1990-08-31 18:22:53 +00:00
list_command(p_tree *p;)
{ p_tree t1 = 0, t2 = 0; }
:
LIST
[
| position(&t1)
1990-09-21 16:58:20 +00:00
| qualified_name(&t1)
]
[ ',' count(&t2)
| '-'
[ count(&t2) { t2->t_ival = - t2->t_ival; }
| { t2 = mknode(OP_INTEGER, -100000000L); }
]
|
]
{ *p = mknode(OP_LIST, t1, t2); }
1990-08-31 18:22:53 +00:00
;
file_command(p_tree *p;)
:
XFILE { extended_charset = 1; }
[ { *p = 0; }
| name(p) { (*p)->t_idf = str2idf((*p)->t_str, 0); }
] { *p = mknode(OP_FILE, *p);
extended_charset = 0;
}
;
help_command(p_tree *p;)
:
[ HELP | '?' ]
1990-10-17 17:00:03 +00:00
{ *p = mknode(OP_HELP, (p_tree) 0); }
[ name(&(*p)->t_args[0])?
| '?' { (*p)->t_args[0] = mknode(OP_NAME, str2idf("help",0), (char *) 0); }
1990-11-06 12:23:41 +00:00
| '!' { (*p)->t_args[0] = mknode(OP_NAME, (struct idf *) 0, "!"); }
]
;
1990-08-31 18:22:53 +00:00
run_command(p_tree *p;)
:
RUN { extended_charset = 1; }
1990-08-31 18:22:53 +00:00
args(p) { *p = mknode(OP_RUN, *p);
extended_charset = 0;
}
| RERUN { if (! run_command) {
error("no run command given yet");
}
else *p = run_command;
}
1990-11-06 12:23:41 +00:00
[ '?' { *p = mknode(OP_PRCOMM, *p); }
|
]
1990-08-31 18:22:53 +00:00
;
stop_command(p_tree *p;)
{ p_tree whr = 0, cond = 0; }
:
STOP
where(&whr)?
condition(&cond)? { if (! whr && ! cond) {
error("no position or condition");
*p = 0;
}
else *p = mknode(OP_STOP, whr, cond);
}
;
trace_command(p_tree *p;)
{ p_tree whr = 0, cond = 0, exp = 0; }
:
TRACE
1990-11-12 13:46:31 +00:00
[ ON format_expression(&exp) ]?
1990-08-31 18:22:53 +00:00
where(&whr)?
condition(&cond)? { *p = mknode(OP_TRACE, whr, cond, exp); }
;
continue_command(p_tree *p;)
{ long l; p_tree pos = 0; }
:
CONT
[ INTEGER { l = tok.ival; }
| { l = 1; }
]
[ AT position(&pos) ]?
1990-08-31 18:22:53 +00:00
{ *p = mknode(OP_CONT, mknode(OP_INTEGER, l), pos); }
;
when_command(p_tree *p;)
{ p_tree whr = 0, cond = 0; }
:
WHEN
where(&whr)?
1990-10-17 17:00:03 +00:00
condition(&cond)? { *p = mknode(OP_WHEN, whr, cond, (p_tree) 0);
p = &(*p)->t_args[2];
}
1990-08-31 18:22:53 +00:00
'{'
command_line(p)
[ ';' { if (*p) {
*p = mknode(OP_LINK, *p, (p_tree) 0);
p = &((*p)->t_args[1]);
}
1990-08-31 18:22:53 +00:00
}
command_line(p)
]*
'}'
{ if (! whr && ! cond) {
error("no position or condition");
}
else if (! *p) {
error("no commands given");
}
1990-08-31 18:22:53 +00:00
}
;
step_command(p_tree *p;)
:
1990-10-17 17:00:03 +00:00
STEP { *p = mknode(OP_STEP, (p_tree) 0); }
count(&(*p)->t_args[0])?
1990-08-31 18:22:53 +00:00
;
next_command(p_tree *p;)
:
1990-10-17 17:00:03 +00:00
NEXT { *p = mknode(OP_NEXT, (p_tree) 0); }
count(&(*p)->t_args[0])?
1990-08-31 18:22:53 +00:00
;
regs_command(p_tree *p;)
:
1990-10-17 17:00:03 +00:00
REGS { *p = mknode(OP_REGS, (p_tree) 0); }
count(&(*p)->t_args[0])?
1990-08-31 18:22:53 +00:00
;
delete_command(p_tree *p;)
:
1990-11-12 13:46:31 +00:00
DELETE num_list(p)? { *p = mknode(OP_DELETE, *p); }
1990-08-31 18:22:53 +00:00
;
print_command(p_tree *p;)
:
PRINT
[ format_expression_list(p)
1990-09-20 17:51:14 +00:00
{ *p = mknode(OP_PRINT, *p); }
|
{ *p = mknode(OP_PRINT, (p_tree) 0); }
]
1990-09-20 17:51:14 +00:00
;
display_command(p_tree *p;)
:
DISPLAY format_expression_list(p)
1990-09-20 17:51:14 +00:00
{ *p = mknode(OP_DISPLAY, *p); }
;
format_expression_list(p_tree *p;)
1990-09-20 17:51:14 +00:00
:
format_expression(p)
1990-08-31 18:22:53 +00:00
[ ',' { *p = mknode(OP_LINK, *p, (p_tree) 0);
p = &((*p)->t_args[1]);
}
format_expression(p)
1990-08-31 18:22:53 +00:00
]*
;
format_expression(p_tree *p;)
{ p_tree p1; }
:
expression(p, 0)
1990-11-15 17:00:39 +00:00
[ '\\' name(&p1) { register char *c = p1->t_str;
while (*c) {
if (! strindex("doshcax", *c)) {
error("illegal format: %c", *c);
break;
}
c++;
}
*p = mknode(OP_FORMAT, *p, p1);
}
|
]
;
1990-09-19 14:31:12 +00:00
set_command(p_tree *p;)
:
SET expression(p, 0) { *p = mknode(OP_SET, *p, (p_tree) 0); }
TO expression(&((*p)->t_args[1]), 0)
;
able_command(p_tree *p;)
:
[ ENABLE { *p = mknode(OP_ENABLE, (p_tree) 0); }
| DISABLE { *p = mknode(OP_DISABLE, (p_tree) 0); }
]
1990-11-12 13:46:31 +00:00
num_list(&(*p)->t_args[0])?
;
1990-11-12 13:46:31 +00:00
num_list(p_tree *p;)
:
1990-11-12 13:46:31 +00:00
num(p)
1990-10-17 17:00:03 +00:00
[ ',' { *p = mknode(OP_LINK, *p, (p_tree) 0); }
1990-11-12 13:46:31 +00:00
num(&(*p)->t_args[1])
]*
1990-09-19 14:31:12 +00:00
;
1990-08-31 18:22:53 +00:00
condition(p_tree *p;)
:
IF expression(p, 0)
1990-08-31 18:22:53 +00:00
;
where(p_tree *p;)
:
1990-10-17 17:00:03 +00:00
IN qualified_name(p) { *p = mknode(OP_IN, *p, (p_tree) 0); }
[ AT position(&((*p)->t_args[1])) ]?
1990-08-31 18:22:53 +00:00
|
AT position(p)
1990-08-31 18:22:53 +00:00
;
1990-09-07 14:56:24 +00:00
expression(p_tree *p; int level;)
{ int currprio, currop; }
: { in_expression++; }
factor(p)
1990-09-19 14:31:12 +00:00
[ %while ((currprio = binprio(currop = (int) tok.ival)) > level)
1990-09-07 14:56:24 +00:00
[ BIN_OP | PREF_OR_BIN_OP ]
{ *p = mknode(OP_BINOP, *p, (p_tree) 0);
(*p)->t_whichoper = currop;
}
expression(&((*p)->t_args[1]), currprio)
|
1990-09-07 14:56:24 +00:00
SEL_OP { *p = mknode(OP_BINOP, *p, (p_tree) 0);
(*p)->t_whichoper = (int) tok.ival;
}
name(&(*p)->t_args[1])
|
'[' { *p = mknode(OP_BINOP, *p, (p_tree) 0);
1990-09-12 16:13:59 +00:00
(*p)->t_whichoper = E_ARRAY;
1990-09-07 14:56:24 +00:00
}
expression(&(*p)->t_args[1], 0)
[ ',' { *p = mknode(OP_BINOP, *p, (p_tree) 0);
(*p)->t_whichoper = E_ARRAY;
}
expression(&(*p)->t_args[1], 0)
]*
1990-09-07 14:56:24 +00:00
']'
]*
{ in_expression--; }
;
factor(p_tree *p;)
:
[
%default EXPRESSION /* lexical analyzer will never return this token */
{ *p = mknode(OP_INTEGER, 0L); }
|
'(' expression(p, 0) ')'
1990-09-07 14:56:24 +00:00
|
INTEGER { *p = mknode(OP_INTEGER, tok.ival); }
|
REAL { *p = mknode(OP_REAL, tok.fval); }
|
STRING { *p = mknode(OP_STRING, tok.str); }
|
qualified_name(p)
|
{ *p = mknode(OP_UNOP, (p_tree) 0);
(*p)->t_whichoper = (int) tok.ival;
}
[ PREF_OP
| PREF_OR_BIN_OP
{ (*currlang->fix_bin_to_pref)(*p); }
]
expression(&(*p)->t_args[0], unprio((*p)->t_whichoper))
]
[ %while(1)
1990-09-07 14:56:24 +00:00
POST_OP { *p = mknode(OP_UNOP, *p);
(*p)->t_whichoper = (int) tok.ival;
}
]*
1990-08-31 18:22:53 +00:00
;
position(p_tree *p;)
{ p_tree lin;
char *str;
}
:
[ STRING { str = tok.str; }
':'
1990-09-21 16:58:20 +00:00
| { if (! listfile) str = 0;
else str = listfile->sy_idf->id_text;
1990-08-31 18:22:53 +00:00
}
]
count(&lin) { *p = mknode(OP_AT, lin->t_ival, str);
1990-08-31 18:22:53 +00:00
freenode(lin);
}
;
args(p_tree *p;)
{ int first_time = 1; }
:
[ { if (! first_time) {
*p = mknode(OP_LINK, *p, (p_tree) 0);
p = &((*p)->t_args[1]);
}
first_time = 0;
}
arg(p)
]*
;
arg(p_tree *p;)
:
name(p)
|
'>' name(p) { (*p)->t_oper = OP_OUTPUT; }
|
'<' name(p) { (*p)->t_oper = OP_INPUT; }
;
count(p_tree *p;)
1990-08-31 18:22:53 +00:00
:
INTEGER { *p = mknode(OP_INTEGER, tok.ival); }
;
1990-11-06 12:23:41 +00:00
opt_num(p_tree *p;)
1990-11-12 13:46:31 +00:00
:
num(p)
|
{ *p = 0; }
;
num(p_tree *p;)
1990-11-06 12:23:41 +00:00
:
count(p)
|
'-' count(p) { (*p)->t_ival = - (*p)->t_ival; }
;
1990-08-31 18:22:53 +00:00
qualified_name(p_tree *p;)
:
name(p)
[ '`' { *p = mknode(OP_SELECT, *p, (p_tree) 0); }
name(&((*p)->t_args[1]))
]*
;
name(p_tree *p;)
:
[ XFILE
| LIST
| RUN
| RERUN
| STOP
| WHEN
| AT
| IN
| IF
| %default NAME
1990-08-31 18:22:53 +00:00
| CONT
| STEP
| NEXT
| REGS
| WHERE
| STATUS
| PRINT
| DELETE
| DUMP
| RESTORE
| TRACE
| ON
1990-09-19 14:31:12 +00:00
| SET
| TO
1990-09-20 17:51:14 +00:00
| FIND
| DISPLAY
| WHICH
| HELP
| DISABLE
| ENABLE
1990-11-06 12:23:41 +00:00
| SOURCE
| FRAME
| LOG
1990-08-31 18:22:53 +00:00
] { *p = mknode(OP_NAME, tok.idf, tok.str); }
;
{
int
LLlex()
{
register int c;
if (ASIDE) {
tok = aside;
ASIDE = 0;
return TOK;
}
do {
c = getc(db_in);
if (interrupted && c == EOF) {
c = ' ';
interrupted = 0;
continue;
}
1990-08-31 18:22:53 +00:00
} while (c != EOF && class(c) == STSKIP);
if (c == EOF) {
eof_seen = 1;
return c;
}
1990-10-01 11:44:27 +00:00
if (extended_charset && in_ext(c)) {
TOK = get_name(c);
return TOK;
}
1990-08-31 18:22:53 +00:00
switch(class(c)) {
case STSTR:
1990-09-07 14:56:24 +00:00
TOK = (*currlang->get_string)(c);
1990-08-31 18:22:53 +00:00
break;
case STIDF:
1990-09-07 14:56:24 +00:00
if (in_expression) TOK = (*currlang->get_name)(c);
else TOK = get_name(c);
1990-08-31 18:22:53 +00:00
break;
case STNUM:
1990-09-07 14:56:24 +00:00
TOK = (*currlang->get_number)(c);
1990-08-31 18:22:53 +00:00
break;
case STNL:
TOK = c;
break;
1990-09-07 14:56:24 +00:00
case STSIMP:
if (! in_expression) {
TOK = c;
break;
}
/* Fall through */
1990-08-31 18:22:53 +00:00
default:
1990-09-07 14:56:24 +00:00
TOK = (*currlang->get_token)(c);
break;
1990-08-31 18:22:53 +00:00
}
return TOK;
}
int
get_name(c)
register int c;
{
char buf[512+1];
register char *p = &buf[0];
register struct idf *id;
do {
if (p - buf < 512) *p++ = c;
c = getc(db_in);
} while ((extended_charset && in_ext(c)) || in_idf(c));
ungetc(c, db_in);
*p++ = 0;
1990-08-31 18:22:53 +00:00
if (extended_charset) {
tok.idf = 0;
tok.str = Salloc(buf, (unsigned) (p - buf));
return NAME;
}
id = str2idf(buf, 1);
tok.idf = id;
tok.str = id->id_text;
return id->id_reserved ? id->id_reserved : NAME;
}
1990-11-12 13:46:31 +00:00
extern char *symbol2str();
1990-08-31 18:22:53 +00:00
LLmessage(t)
{
if (t > 0) {
if (! errorgiven) {
error("%s missing before %s", symbol2str(t), symbol2str(TOK));
}
aside = tok;
}
else if (t == 0) {
if (! errorgiven) {
error("%s unexpected", symbol2str(TOK));
}
}
else if (! errorgiven) {
error("EOF expected");
}
errorgiven = 1;
}
1990-11-12 13:46:31 +00:00
static void
1990-08-31 18:22:53 +00:00
catch_del()
{
signal(SIGINT, catch_del);
if (! disable_intr) {
1995-08-15 09:10:39 +00:00
signal_child(7);
child_interrupted = 1;
}
interrupted = 1;
1990-08-31 18:22:53 +00:00
}
1990-11-12 13:46:31 +00:00
void
1990-08-31 18:22:53 +00:00
init_del()
{
signal(SIGINT, catch_del);
}
1990-11-12 13:46:31 +00:00
static void
ctch()
{
/* Only for shell escapes ... */
signal(SIGINT, ctch);
}
#define SHBUFSIZ 512
1990-11-12 13:46:31 +00:00
static int
shellescape()
{
register char *p; /* walks through command */
static char previous[SHBUFSIZ]; /* previous command */
char comm[SHBUFSIZ]; /* space for command */
register int cnt; /* prevent array bound errors */
register int c; /* current char */
register int lastc = 0; /* will contain the previous char */
p = comm;
cnt = SHBUFSIZ-2;
while (c = getc(db_in), c != '\n') {
switch(c) {
case '!':
/*
* An unescaped ! expands to the previous
* command, but disappears if there is none
*/
if (lastc != '\\') {
if (*previous) {
int len = strlen(previous);
if ((cnt -= len) <= 0) break;
strcpy(p,previous);
p += len;
}
}
else {
*p++ = c;
}
continue;
case '%':
/*
* An unescaped % will expand to the current
* filename, but disappears is there is none
*/
if (lastc != '\\') {
if (listfile) {
int len = strlen(listfile->sy_idf->id_text);
if ((cnt -= len) <= 0) break;
strcpy(p,listfile->sy_idf->id_text);
p += len;
}
}
else {
*p++ = c;
}
continue;
default:
lastc = c;
if (cnt-- <= 0) break;
*p++ = c;
continue;
}
break;
}
*p = '\0';
if (c != '\n') {
warning("shell command too long");
while (c != '\n') c = getc(db_in);
}
ungetc(c, db_in);
strcpy(previous, comm);
signal(SIGINT, ctch);
cnt = system(comm);
signal(SIGINT, catch_del);
return cnt;
}
1990-08-31 18:22:53 +00:00
}