2013-05-20 01:16:08 -04:00
|
|
|
#include "eval.h"
|
|
|
|
|
2010-08-14 23:11:27 -04:00
|
|
|
/* eval.c
|
2005-05-31 11:50:56 -04:00
|
|
|
*
|
|
|
|
* Expression evaluation functions
|
|
|
|
*
|
|
|
|
* written 1986 by Daniel Lawrence
|
|
|
|
* modified by Petri Kutvonen
|
|
|
|
*/
|
|
|
|
|
2010-06-03 19:36:01 -04:00
|
|
|
#include <stdio.h>
|
2010-11-14 21:10:03 -05:00
|
|
|
|
2010-06-03 19:36:01 -04:00
|
|
|
#include "estruct.h"
|
|
|
|
#include "edef.h"
|
2005-10-01 01:52:45 -04:00
|
|
|
#include "efunc.h"
|
2013-05-25 00:37:03 -04:00
|
|
|
#include "exec.h"
|
2013-05-17 23:42:16 -04:00
|
|
|
#include "fileio.h"
|
2010-11-14 21:10:03 -05:00
|
|
|
#include "line.h"
|
2013-05-28 00:37:04 -04:00
|
|
|
#include "random.h"
|
2013-05-28 00:22:31 -04:00
|
|
|
#include "search.h"
|
2010-08-14 23:11:27 -04:00
|
|
|
#include "version.h"
|
2013-05-28 00:47:48 -04:00
|
|
|
#include "window.h"
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2010-11-23 17:41:25 -05:00
|
|
|
#define MAXVARS 255
|
|
|
|
|
2013-05-17 09:17:16 -04:00
|
|
|
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
|
|
|
|
2013-05-17 09:50:01 -04:00
|
|
|
/* Max #chars in a var name. */
|
|
|
|
#define NVSIZE 10
|
|
|
|
|
|
|
|
/* Structure to hold user variables and their definitions. */
|
|
|
|
struct user_variable {
|
|
|
|
char u_name[NVSIZE + 1]; /* name of user variable */
|
|
|
|
char *u_value; /* value (string) */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* List of recognized environment variables. */
|
|
|
|
|
|
|
|
static const char *envars[] = {
|
|
|
|
"fillcol", /* current fill column */
|
|
|
|
"pagelen", /* number of lines used by editor */
|
|
|
|
"curcol", /* current column pos of cursor */
|
|
|
|
"curline", /* current line in file */
|
|
|
|
"ram", /* ram in use by malloc */
|
|
|
|
"flicker", /* flicker supression */
|
|
|
|
"curwidth", /* current screen width */
|
|
|
|
"cbufname", /* current buffer name */
|
|
|
|
"cfname", /* current file name */
|
|
|
|
"sres", /* current screen resolution */
|
|
|
|
"debug", /* macro debugging */
|
|
|
|
"status", /* returns the status of the last command */
|
|
|
|
"palette", /* current palette string */
|
|
|
|
"asave", /* # of chars between auto-saves */
|
|
|
|
"acount", /* # of chars until next auto-save */
|
|
|
|
"lastkey", /* last keyboard char struck */
|
|
|
|
"curchar", /* current character under the cursor */
|
|
|
|
"discmd", /* display commands on command line */
|
|
|
|
"version", /* current version number */
|
|
|
|
"progname", /* returns current prog name - "MicroEMACS" */
|
|
|
|
"seed", /* current random number seed */
|
|
|
|
"disinp", /* display command line input characters */
|
|
|
|
"wline", /* # of lines in current window */
|
|
|
|
"cwline", /* current screen line in window */
|
|
|
|
"target", /* target for line moves */
|
|
|
|
"search", /* search pattern */
|
|
|
|
"replace", /* replacement pattern */
|
|
|
|
"match", /* last matched magic pattern */
|
|
|
|
"kill", /* kill buffer (read only) */
|
|
|
|
"cmode", /* mode of current buffer */
|
|
|
|
"gmode", /* global modes */
|
|
|
|
"tpause", /* length to pause for paren matching */
|
|
|
|
"pending", /* type ahead pending flag */
|
|
|
|
"lwidth", /* width of current line */
|
|
|
|
"line", /* text of current line */
|
|
|
|
"gflags", /* global internal emacs flags */
|
|
|
|
"rval", /* child process return value */
|
|
|
|
"tab", /* tab 4 or 8 */
|
|
|
|
"overlap",
|
|
|
|
"jump",
|
|
|
|
#if SCROLLCODE
|
|
|
|
"scroll", /* scroll enabled */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/* And its preprocesor definitions. */
|
|
|
|
|
|
|
|
#define EVFILLCOL 0
|
|
|
|
#define EVPAGELEN 1
|
|
|
|
#define EVCURCOL 2
|
|
|
|
#define EVCURLINE 3
|
|
|
|
#define EVRAM 4
|
|
|
|
#define EVFLICKER 5
|
|
|
|
#define EVCURWIDTH 6
|
|
|
|
#define EVCBUFNAME 7
|
|
|
|
#define EVCFNAME 8
|
|
|
|
#define EVSRES 9
|
|
|
|
#define EVDEBUG 10
|
|
|
|
#define EVSTATUS 11
|
|
|
|
#define EVPALETTE 12
|
|
|
|
#define EVASAVE 13
|
|
|
|
#define EVACOUNT 14
|
|
|
|
#define EVLASTKEY 15
|
|
|
|
#define EVCURCHAR 16
|
|
|
|
#define EVDISCMD 17
|
|
|
|
#define EVVERSION 18
|
|
|
|
#define EVPROGNAME 19
|
|
|
|
#define EVSEED 20
|
|
|
|
#define EVDISINP 21
|
|
|
|
#define EVWLINE 22
|
|
|
|
#define EVCWLINE 23
|
|
|
|
#define EVTARGET 24
|
|
|
|
#define EVSEARCH 25
|
|
|
|
#define EVREPLACE 26
|
|
|
|
#define EVMATCH 27
|
|
|
|
#define EVKILL 28
|
|
|
|
#define EVCMODE 29
|
|
|
|
#define EVGMODE 30
|
|
|
|
#define EVTPAUSE 31
|
|
|
|
#define EVPENDING 32
|
|
|
|
#define EVLWIDTH 33
|
|
|
|
#define EVLINE 34
|
|
|
|
#define EVGFLAGS 35
|
|
|
|
#define EVRVAL 36
|
|
|
|
#define EVTAB 37
|
|
|
|
#define EVOVERLAP 38
|
|
|
|
#define EVSCROLLCOUNT 39
|
|
|
|
#define EVSCROLL 40
|
|
|
|
|
|
|
|
enum function_type {
|
|
|
|
NILNAMIC = 0,
|
|
|
|
MONAMIC,
|
|
|
|
DYNAMIC,
|
|
|
|
TRINAMIC,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* List of recognized user functions. */
|
|
|
|
static struct {
|
|
|
|
const char f_name[ 4] ;
|
|
|
|
const enum function_type f_type ;
|
|
|
|
} funcs[] = {
|
|
|
|
{ "add", DYNAMIC }, /* add two numbers together */
|
|
|
|
{ "sub", DYNAMIC }, /* subtraction */
|
|
|
|
{ "tim", DYNAMIC }, /* multiplication */
|
|
|
|
{ "div", DYNAMIC }, /* division */
|
|
|
|
{ "mod", DYNAMIC }, /* mod */
|
|
|
|
{ "neg", MONAMIC }, /* negate */
|
|
|
|
{ "cat", DYNAMIC }, /* concatinate string */
|
|
|
|
{ "lef", DYNAMIC }, /* left string(string, len) */
|
|
|
|
{ "rig", DYNAMIC }, /* right string(string, pos) */
|
|
|
|
{ "mid", TRINAMIC }, /* mid string(string, pos, len) */
|
|
|
|
{ "not", MONAMIC }, /* logical not */
|
|
|
|
{ "equ", DYNAMIC }, /* logical equality check */
|
|
|
|
{ "les", DYNAMIC }, /* logical less than */
|
|
|
|
{ "gre", DYNAMIC }, /* logical greater than */
|
|
|
|
{ "seq", DYNAMIC }, /* string logical equality check */
|
|
|
|
{ "sle", DYNAMIC }, /* string logical less than */
|
|
|
|
{ "sgr", DYNAMIC }, /* string logical greater than */
|
|
|
|
{ "ind", MONAMIC }, /* evaluate indirect value */
|
|
|
|
{ "and", DYNAMIC }, /* logical and */
|
|
|
|
{ "or", DYNAMIC }, /* logical or */
|
|
|
|
{ "len", MONAMIC }, /* string length */
|
|
|
|
{ "upp", MONAMIC }, /* uppercase string */
|
|
|
|
{ "low", MONAMIC }, /* lower case string */
|
|
|
|
{ "tru", MONAMIC }, /* Truth of the universe logical test */
|
|
|
|
{ "asc", MONAMIC }, /* char to integer conversion */
|
|
|
|
{ "chr", MONAMIC }, /* integer to char conversion */
|
|
|
|
{ "gtk", NILNAMIC }, /* get 1 charater */
|
|
|
|
{ "rnd", MONAMIC }, /* get a random number */
|
|
|
|
{ "abs", MONAMIC }, /* absolute value of a number */
|
|
|
|
{ "sin", DYNAMIC }, /* find the index of one string in another */
|
|
|
|
{ "env", MONAMIC }, /* retrieve a system environment var */
|
|
|
|
{ "bin", MONAMIC }, /* loopup what function name is bound to a key */
|
|
|
|
{ "exi", MONAMIC }, /* check if a file exists */
|
|
|
|
{ "fin", MONAMIC }, /* look for a file on the path... */
|
|
|
|
{ "ban", DYNAMIC }, /* bitwise and 9-10-87 jwm */
|
|
|
|
{ "bor", DYNAMIC }, /* bitwise or 9-10-87 jwm */
|
|
|
|
{ "bxo", DYNAMIC }, /* bitwise xor 9-10-87 jwm */
|
|
|
|
{ "bno", MONAMIC }, /* bitwise not */
|
|
|
|
{ "xla", TRINAMIC }, /* XLATE character string translation */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* And its preprocesor definitions. */
|
|
|
|
|
|
|
|
#define UFADD 0
|
|
|
|
#define UFSUB 1
|
|
|
|
#define UFTIMES 2
|
|
|
|
#define UFDIV 3
|
|
|
|
#define UFMOD 4
|
|
|
|
#define UFNEG 5
|
|
|
|
#define UFCAT 6
|
|
|
|
#define UFLEFT 7
|
|
|
|
#define UFRIGHT 8
|
|
|
|
#define UFMID 9
|
|
|
|
#define UFNOT 10
|
|
|
|
#define UFEQUAL 11
|
|
|
|
#define UFLESS 12
|
|
|
|
#define UFGREATER 13
|
|
|
|
#define UFSEQUAL 14
|
|
|
|
#define UFSLESS 15
|
|
|
|
#define UFSGREAT 16
|
|
|
|
#define UFIND 17
|
|
|
|
#define UFAND 18
|
|
|
|
#define UFOR 19
|
|
|
|
#define UFLENGTH 20
|
|
|
|
#define UFUPPER 21
|
|
|
|
#define UFLOWER 22
|
|
|
|
#define UFTRUTH 23
|
|
|
|
#define UFASCII 24
|
|
|
|
#define UFCHR 25
|
|
|
|
#define UFGTKEY 26
|
|
|
|
#define UFRND 27
|
|
|
|
#define UFABS 28
|
|
|
|
#define UFSINDEX 29
|
|
|
|
#define UFENV 30
|
|
|
|
#define UFBIND 31
|
|
|
|
#define UFEXIST 32
|
|
|
|
#define UFFIND 33
|
|
|
|
#define UFBAND 34
|
|
|
|
#define UFBOR 35
|
|
|
|
#define UFBXOR 36
|
|
|
|
#define UFBNOT 37
|
|
|
|
#define UFXLATE 38
|
|
|
|
|
2010-11-23 17:41:25 -05:00
|
|
|
/* User variables */
|
|
|
|
static struct user_variable uv[MAXVARS + 1];
|
|
|
|
|
2010-08-14 23:11:27 -04:00
|
|
|
/* Initialize the user variable list. */
|
2005-10-01 01:52:45 -04:00
|
|
|
void varinit(void)
|
2010-08-14 23:11:27 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int i;
|
2005-09-30 18:26:09 -04:00
|
|
|
for (i = 0; i < MAXVARS; i++)
|
2005-05-31 11:50:56 -04:00
|
|
|
uv[i].u_name[0] = 0;
|
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
2010-08-14 23:11:27 -04:00
|
|
|
* Evaluate a function.
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2010-08-14 23:11:27 -04:00
|
|
|
* @fname: name of function to evaluate.
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
|
|
|
char *gtfun(char *fname)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int fnum; /* index to function to eval */
|
|
|
|
int status; /* return status */
|
|
|
|
char *tsp; /* temporary string pointer */
|
2005-09-30 18:26:09 -04:00
|
|
|
char arg1[NSTRING]; /* value of first argument */
|
|
|
|
char arg2[NSTRING]; /* value of second argument */
|
|
|
|
char arg3[NSTRING]; /* value of third argument */
|
2005-05-31 11:50:56 -04:00
|
|
|
static char result[2 * NSTRING]; /* string result */
|
|
|
|
|
|
|
|
/* look the function up in the function table */
|
2005-09-30 18:26:09 -04:00
|
|
|
fname[3] = 0; /* only first 3 chars significant */
|
|
|
|
mklower(fname); /* and let it be upper or lower case */
|
2010-06-11 00:32:18 -04:00
|
|
|
for (fnum = 0; fnum < ARRAY_SIZE(funcs); fnum++)
|
2005-05-31 11:50:56 -04:00
|
|
|
if (strcmp(fname, funcs[fnum].f_name) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* return errorm on a bad reference */
|
2010-06-11 00:32:18 -04:00
|
|
|
if (fnum == ARRAY_SIZE(funcs))
|
2010-08-29 06:03:55 -04:00
|
|
|
return errorm;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* if needed, retrieve the first argument */
|
|
|
|
if (funcs[fnum].f_type >= MONAMIC) {
|
|
|
|
if ((status = macarg(arg1)) != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return errorm;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* if needed, retrieve the second argument */
|
|
|
|
if (funcs[fnum].f_type >= DYNAMIC) {
|
|
|
|
if ((status = macarg(arg2)) != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return errorm;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* if needed, retrieve the third argument */
|
|
|
|
if (funcs[fnum].f_type >= TRINAMIC)
|
|
|
|
if ((status = macarg(arg3)) != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return errorm;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
}
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* and now evaluate it! */
|
|
|
|
switch (fnum) {
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFADD:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(atoi(arg1) + atoi(arg2));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFSUB:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(atoi(arg1) - atoi(arg2));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFTIMES:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(atoi(arg1) * atoi(arg2));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFDIV:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(atoi(arg1) / atoi(arg2));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFMOD:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(atoi(arg1) % atoi(arg2));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFNEG:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(-atoi(arg1));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFCAT:
|
|
|
|
strcpy(result, arg1);
|
2010-08-29 06:03:55 -04:00
|
|
|
return strcat(result, arg2);
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFLEFT:
|
2010-08-29 06:03:55 -04:00
|
|
|
return strncpy(result, arg1, atoi(arg2));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFRIGHT:
|
|
|
|
return (strcpy(result,
|
|
|
|
&arg1[(strlen(arg1) - atoi(arg2))]));
|
|
|
|
case UFMID:
|
|
|
|
return (strncpy(result, &arg1[atoi(arg2) - 1],
|
|
|
|
atoi(arg3)));
|
|
|
|
case UFNOT:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(stol(arg1) == FALSE);
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFEQUAL:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(atoi(arg1) == atoi(arg2));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFLESS:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(atoi(arg1) < atoi(arg2));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFGREATER:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(atoi(arg1) > atoi(arg2));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFSEQUAL:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(strcmp(arg1, arg2) == 0);
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFSLESS:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(strcmp(arg1, arg2) < 0);
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFSGREAT:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(strcmp(arg1, arg2) > 0);
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFIND:
|
2010-08-29 06:03:55 -04:00
|
|
|
return strcpy(result, getval(arg1));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFAND:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(stol(arg1) && stol(arg2));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFOR:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(stol(arg1) || stol(arg2));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFLENGTH:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(strlen(arg1));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFUPPER:
|
2010-08-29 06:03:55 -04:00
|
|
|
return mkupper(arg1);
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFLOWER:
|
2010-08-29 06:03:55 -04:00
|
|
|
return mklower(arg1);
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFTRUTH:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(atoi(arg1) == 42);
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFASCII:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa((int) arg1[0]);
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFCHR:
|
|
|
|
result[0] = atoi(arg1);
|
|
|
|
result[1] = 0;
|
2010-08-29 06:03:55 -04:00
|
|
|
return result;
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFGTKEY:
|
|
|
|
result[0] = tgetc();
|
|
|
|
result[1] = 0;
|
2010-08-29 06:03:55 -04:00
|
|
|
return result;
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFRND:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa((ernd() % abs(atoi(arg1))) + 1);
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFABS:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(abs(atoi(arg1)));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFSINDEX:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(sindex(arg1, arg2));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFENV:
|
2005-05-31 11:50:56 -04:00
|
|
|
#if ENVFUNC
|
2005-09-30 18:26:09 -04:00
|
|
|
tsp = getenv(arg1);
|
2010-08-29 06:03:55 -04:00
|
|
|
return tsp == NULL ? "" : tsp;
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
2010-08-29 06:03:55 -04:00
|
|
|
return "";
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFBIND:
|
2010-08-29 06:03:55 -04:00
|
|
|
return transbind(arg1);
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFEXIST:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(fexist(arg1));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFFIND:
|
|
|
|
tsp = flook(arg1, TRUE);
|
2010-08-29 06:03:55 -04:00
|
|
|
return tsp == NULL ? "" : tsp;
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFBAND:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(atoi(arg1) & atoi(arg2));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFBOR:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(atoi(arg1) | atoi(arg2));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFBXOR:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(atoi(arg1) ^ atoi(arg2));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFBNOT:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(~atoi(arg1));
|
2005-09-30 18:26:09 -04:00
|
|
|
case UFXLATE:
|
2010-08-29 06:03:55 -04:00
|
|
|
return xlat(arg1, arg2, arg3);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
exit(-11); /* never should get here */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* look up a user var's value
|
|
|
|
*
|
|
|
|
* char *vname; name of user variable to fetch
|
|
|
|
*/
|
|
|
|
char *gtusr(char *vname)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
|
2010-02-27 05:38:22 -05:00
|
|
|
int vnum; /* ordinal number of user var */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* scan the list looking for the user var name */
|
|
|
|
for (vnum = 0; vnum < MAXVARS; vnum++) {
|
|
|
|
if (uv[vnum].u_name[0] == 0)
|
2010-08-29 06:03:55 -04:00
|
|
|
return errorm;
|
2005-05-31 11:50:56 -04:00
|
|
|
if (strcmp(vname, uv[vnum].u_name) == 0)
|
2010-08-29 06:03:55 -04:00
|
|
|
return uv[vnum].u_value;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return errorm if we run off the end */
|
2010-08-29 06:03:55 -04:00
|
|
|
return errorm;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2010-02-27 05:38:24 -05:00
|
|
|
extern char *getkill(void);
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* gtenv()
|
|
|
|
*
|
|
|
|
* char *vname; name of environment variable to retrieve
|
|
|
|
*/
|
|
|
|
char *gtenv(char *vname)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int vnum; /* ordinal number of var refrenced */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* scan the list, looking for the referenced name */
|
2010-06-03 19:36:01 -04:00
|
|
|
for (vnum = 0; vnum < ARRAY_SIZE(envars); vnum++)
|
2005-05-31 11:50:56 -04:00
|
|
|
if (strcmp(vname, envars[vnum]) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* return errorm on a bad reference */
|
2010-06-03 19:36:01 -04:00
|
|
|
if (vnum == ARRAY_SIZE(envars))
|
|
|
|
#if ENVFUNC
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
char *ename = getenv(vname);
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
if (ename != NULL)
|
2010-08-29 06:03:55 -04:00
|
|
|
return ename;
|
2005-05-31 11:50:56 -04:00
|
|
|
else
|
2010-08-29 06:03:55 -04:00
|
|
|
return errorm;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
2010-08-29 06:03:55 -04:00
|
|
|
return errorm;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* otherwise, fetch the appropriate value */
|
|
|
|
switch (vnum) {
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVFILLCOL:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(fillcol);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVPAGELEN:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(term.t_nrow + 1);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVCURCOL:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(getccol(FALSE));
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVCURLINE:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(getcline());
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVRAM:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa((int) (envram / 1024l));
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVFLICKER:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(flickcode);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVCURWIDTH:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(term.t_ncol);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVCBUFNAME:
|
2010-08-29 06:03:55 -04:00
|
|
|
return curbp->b_bname;
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVCFNAME:
|
2010-08-29 06:03:55 -04:00
|
|
|
return curbp->b_fname;
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVSRES:
|
2010-08-29 06:03:55 -04:00
|
|
|
return sres;
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVDEBUG:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(macbug);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVSTATUS:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(cmdstatus);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVPALETTE:
|
2010-08-29 06:03:55 -04:00
|
|
|
return palstr;
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVASAVE:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(gasave);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVACOUNT:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(gacount);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVLASTKEY:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(lastkey);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVCURCHAR:
|
|
|
|
return (curwp->w_dotp->l_used ==
|
|
|
|
curwp->w_doto ? itoa('\n') :
|
|
|
|
itoa(lgetc(curwp->w_dotp, curwp->w_doto)));
|
|
|
|
case EVDISCMD:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(discmd);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVVERSION:
|
2010-08-29 06:03:55 -04:00
|
|
|
return VERSION;
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVPROGNAME:
|
2010-08-29 06:03:55 -04:00
|
|
|
return PROGRAM_NAME_LONG;
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVSEED:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(seed);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVDISINP:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(disinp);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVWLINE:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(curwp->w_ntrows);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVCWLINE:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(getwpos());
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVTARGET:
|
|
|
|
saveflag = lastflag;
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(curgoal);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVSEARCH:
|
2010-08-29 06:03:55 -04:00
|
|
|
return pat;
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVREPLACE:
|
2010-08-29 06:03:55 -04:00
|
|
|
return rpat;
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVMATCH:
|
2010-08-29 06:03:55 -04:00
|
|
|
return (patmatch == NULL) ? "" : patmatch;
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVKILL:
|
2010-08-29 06:03:55 -04:00
|
|
|
return getkill();
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVCMODE:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(curbp->b_mode);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVGMODE:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(gmode);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVTPAUSE:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(term.t_pause);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVPENDING:
|
2005-05-31 11:50:56 -04:00
|
|
|
#if TYPEAH
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(typahead());
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
2010-08-29 06:03:55 -04:00
|
|
|
return falsem;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVLWIDTH:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(llength(curwp->w_dotp));
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVLINE:
|
2010-08-29 06:03:55 -04:00
|
|
|
return getctext();
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVGFLAGS:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(gflags);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVRVAL:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(rval);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVTAB:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(tabmask + 1);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVOVERLAP:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(overlap);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVSCROLLCOUNT:
|
2010-08-29 06:03:55 -04:00
|
|
|
return itoa(scrollcount);
|
2005-05-31 11:50:56 -04:00
|
|
|
#if SCROLLCODE
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVSCROLL:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(term.t_scroll != NULL);
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVSCROLL:
|
2010-08-29 06:03:55 -04:00
|
|
|
return ltos(0);
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
|
|
|
}
|
2005-09-30 18:26:09 -04:00
|
|
|
exit(-12); /* again, we should never get here */
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* return some of the contents of the kill buffer
|
|
|
|
*/
|
|
|
|
char *getkill(void)
|
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int size; /* max number of chars to return */
|
2005-05-31 11:50:56 -04:00
|
|
|
static char value[NSTRING]; /* temp buffer for value */
|
|
|
|
|
|
|
|
if (kbufh == NULL)
|
|
|
|
/* no kill buffer....just a null string */
|
|
|
|
value[0] = 0;
|
|
|
|
else {
|
|
|
|
/* copy in the contents... */
|
|
|
|
if (kused < NSTRING)
|
|
|
|
size = kused;
|
|
|
|
else
|
|
|
|
size = NSTRING - 1;
|
|
|
|
strncpy(value, kbufh->d_chunk, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and return the constructed value */
|
2010-08-29 06:03:55 -04:00
|
|
|
return value;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* set a variable
|
|
|
|
*
|
|
|
|
* int f; default flag
|
|
|
|
* int n; numeric arg (can overide prompted value)
|
|
|
|
*/
|
|
|
|
int setvar(int f, int n)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int status; /* status return */
|
2005-05-31 11:50:56 -04:00
|
|
|
#if DEBUGM
|
2010-02-27 05:38:22 -05:00
|
|
|
char *sp; /* temp string pointer */
|
|
|
|
char *ep; /* ptr to end of outline */
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2010-11-06 12:09:44 -04:00
|
|
|
struct variable_description vd; /* variable num/type */
|
2005-09-30 18:26:09 -04:00
|
|
|
char var[NVSIZE + 1]; /* name of variable to fetch */
|
2005-05-31 11:50:56 -04:00
|
|
|
char value[NSTRING]; /* value to set variable to */
|
|
|
|
|
|
|
|
/* first get the variable to set.. */
|
|
|
|
if (clexec == FALSE) {
|
|
|
|
status = mlreply("Variable to set: ", &var[0], NVSIZE);
|
|
|
|
if (status != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-09-30 18:26:09 -04:00
|
|
|
} else { /* macro line argument */
|
2005-05-31 11:50:56 -04:00
|
|
|
/* grab token and skip it */
|
|
|
|
execstr = token(execstr, var, NVSIZE + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check the legality and find the var */
|
|
|
|
findvar(var, &vd, NVSIZE + 1);
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* if its not legal....bitch */
|
|
|
|
if (vd.v_type == -1) {
|
|
|
|
mlwrite("%%No such variable as '%s'", var);
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get the value for that variable */
|
|
|
|
if (f == TRUE)
|
|
|
|
strcpy(value, itoa(n));
|
|
|
|
else {
|
|
|
|
status = mlreply("Value: ", &value[0], NSTRING);
|
|
|
|
if (status != TRUE)
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* and set the appropriate value */
|
|
|
|
status = svar(&vd, value);
|
|
|
|
|
|
|
|
#if DEBUGM
|
|
|
|
/* if $debug == TRUE, every assignment will echo a statment to
|
|
|
|
that effect here. */
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
if (macbug) {
|
|
|
|
strcpy(outline, "(((");
|
|
|
|
|
|
|
|
/* assignment status */
|
|
|
|
strcat(outline, ltos(status));
|
|
|
|
strcat(outline, ":");
|
|
|
|
|
|
|
|
/* variable name */
|
|
|
|
strcat(outline, var);
|
|
|
|
strcat(outline, ":");
|
|
|
|
|
|
|
|
/* and lastly the value we tried to assign */
|
|
|
|
strcat(outline, value);
|
|
|
|
strcat(outline, ")))");
|
|
|
|
|
|
|
|
/* expand '%' to "%%" so mlwrite wont bitch */
|
|
|
|
sp = outline;
|
|
|
|
while (*sp)
|
|
|
|
if (*sp++ == '%') {
|
|
|
|
/* advance to the end */
|
|
|
|
ep = --sp;
|
2005-09-30 18:26:09 -04:00
|
|
|
while (*ep++);
|
2005-05-31 11:50:56 -04:00
|
|
|
/* null terminate the string one out */
|
|
|
|
*(ep + 1) = 0;
|
|
|
|
/* copy backwards */
|
2005-09-30 18:26:09 -04:00
|
|
|
while (ep-- > sp)
|
2005-05-31 11:50:56 -04:00
|
|
|
*(ep + 1) = *ep;
|
|
|
|
|
|
|
|
/* and advance sp past the new % */
|
2005-09-30 18:26:09 -04:00
|
|
|
sp += 2;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* write out the debug line */
|
|
|
|
mlforce(outline);
|
|
|
|
update(TRUE);
|
|
|
|
|
|
|
|
/* and get the keystroke to hold the output */
|
|
|
|
if (get1key() == abortc) {
|
|
|
|
mlforce("(Macro aborted)");
|
|
|
|
status = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* and return it */
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
2010-11-06 12:09:44 -04:00
|
|
|
* Find a variables type and name.
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2010-11-06 12:09:44 -04:00
|
|
|
* @var: name of variable to get.
|
|
|
|
* @vd: structure to hold type and pointer.
|
|
|
|
* @size: size of variable array.
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2010-11-06 12:09:44 -04:00
|
|
|
void findvar(char *var, struct variable_description *vd, int size)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int vnum; /* subscript in variable arrays */
|
|
|
|
int vtype; /* type to return */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2006-11-19 14:52:18 -05:00
|
|
|
vnum = -1;
|
|
|
|
fvar:
|
2010-06-03 19:36:01 -04:00
|
|
|
vtype = -1;
|
2005-05-31 11:50:56 -04:00
|
|
|
switch (var[0]) {
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
case '$': /* check for legal enviromnent var */
|
2010-06-03 19:36:01 -04:00
|
|
|
for (vnum = 0; vnum < ARRAY_SIZE(envars); vnum++)
|
2005-09-30 18:26:09 -04:00
|
|
|
if (strcmp(&var[1], envars[vnum]) == 0) {
|
|
|
|
vtype = TKENV;
|
2005-05-31 11:50:56 -04:00
|
|
|
break;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
|
|
|
break;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
case '%': /* check for existing legal user variable */
|
|
|
|
for (vnum = 0; vnum < MAXVARS; vnum++)
|
|
|
|
if (strcmp(&var[1], uv[vnum].u_name) == 0) {
|
|
|
|
vtype = TKVAR;
|
|
|
|
break;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
2005-09-30 18:26:09 -04:00
|
|
|
if (vnum < MAXVARS)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* create a new one??? */
|
|
|
|
for (vnum = 0; vnum < MAXVARS; vnum++)
|
|
|
|
if (uv[vnum].u_name[0] == 0) {
|
|
|
|
vtype = TKVAR;
|
|
|
|
strcpy(uv[vnum].u_name, &var[1]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '&': /* indirect operator? */
|
|
|
|
var[4] = 0;
|
|
|
|
if (strcmp(&var[1], "ind") == 0) {
|
|
|
|
/* grab token, and eval it */
|
|
|
|
execstr = token(execstr, var, size);
|
|
|
|
strcpy(var, getval(var));
|
|
|
|
goto fvar;
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return the results */
|
|
|
|
vd->v_num = vnum;
|
|
|
|
vd->v_type = vtype;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
2010-11-06 12:09:44 -04:00
|
|
|
* Set a variable.
|
2005-10-01 01:52:45 -04:00
|
|
|
*
|
2010-11-06 12:09:44 -04:00
|
|
|
* @var: variable to set.
|
|
|
|
* @value: value to set to.
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2010-11-06 12:09:44 -04:00
|
|
|
int svar(struct variable_description *var, char *value)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int vnum; /* ordinal number of var refrenced */
|
|
|
|
int vtype; /* type of variable to set */
|
|
|
|
int status; /* status return */
|
|
|
|
int c; /* translated character */
|
|
|
|
char *sp; /* scratch string pointer */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* simplify the vd structure (we are gonna look at it a lot) */
|
|
|
|
vnum = var->v_num;
|
|
|
|
vtype = var->v_type;
|
|
|
|
|
|
|
|
/* and set the appropriate value */
|
|
|
|
status = TRUE;
|
|
|
|
switch (vtype) {
|
2005-09-30 18:26:09 -04:00
|
|
|
case TKVAR: /* set a user variable */
|
2005-05-31 11:50:56 -04:00
|
|
|
if (uv[vnum].u_value != NULL)
|
|
|
|
free(uv[vnum].u_value);
|
|
|
|
sp = malloc(strlen(value) + 1);
|
|
|
|
if (sp == NULL)
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
strcpy(sp, value);
|
|
|
|
uv[vnum].u_value = sp;
|
|
|
|
break;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
case TKENV: /* set an environment variable */
|
2005-05-31 11:50:56 -04:00
|
|
|
status = TRUE; /* by default */
|
|
|
|
switch (vnum) {
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVFILLCOL:
|
|
|
|
fillcol = atoi(value);
|
|
|
|
break;
|
|
|
|
case EVPAGELEN:
|
|
|
|
status = newsize(TRUE, atoi(value));
|
|
|
|
break;
|
|
|
|
case EVCURCOL:
|
|
|
|
status = setccol(atoi(value));
|
|
|
|
break;
|
|
|
|
case EVCURLINE:
|
|
|
|
status = gotoline(TRUE, atoi(value));
|
|
|
|
break;
|
|
|
|
case EVRAM:
|
|
|
|
break;
|
|
|
|
case EVFLICKER:
|
|
|
|
flickcode = stol(value);
|
|
|
|
break;
|
|
|
|
case EVCURWIDTH:
|
|
|
|
status = newwidth(TRUE, atoi(value));
|
|
|
|
break;
|
|
|
|
case EVCBUFNAME:
|
|
|
|
strcpy(curbp->b_bname, value);
|
|
|
|
curwp->w_flag |= WFMODE;
|
|
|
|
break;
|
|
|
|
case EVCFNAME:
|
|
|
|
strcpy(curbp->b_fname, value);
|
|
|
|
curwp->w_flag |= WFMODE;
|
|
|
|
break;
|
|
|
|
case EVSRES:
|
|
|
|
status = TTrez(value);
|
|
|
|
break;
|
|
|
|
case EVDEBUG:
|
|
|
|
macbug = stol(value);
|
|
|
|
break;
|
|
|
|
case EVSTATUS:
|
|
|
|
cmdstatus = stol(value);
|
|
|
|
break;
|
|
|
|
case EVASAVE:
|
|
|
|
gasave = atoi(value);
|
|
|
|
break;
|
|
|
|
case EVACOUNT:
|
|
|
|
gacount = atoi(value);
|
|
|
|
break;
|
|
|
|
case EVLASTKEY:
|
|
|
|
lastkey = atoi(value);
|
|
|
|
break;
|
|
|
|
case EVCURCHAR:
|
2012-07-11 13:43:16 -04:00
|
|
|
ldelchar(1, FALSE); /* delete 1 char */
|
2005-09-30 18:26:09 -04:00
|
|
|
c = atoi(value);
|
|
|
|
if (c == '\n')
|
2005-09-30 19:34:11 -04:00
|
|
|
lnewline();
|
2005-09-30 18:26:09 -04:00
|
|
|
else
|
|
|
|
linsert(1, c);
|
|
|
|
backchar(FALSE, 1);
|
|
|
|
break;
|
|
|
|
case EVDISCMD:
|
|
|
|
discmd = stol(value);
|
|
|
|
break;
|
|
|
|
case EVVERSION:
|
|
|
|
break;
|
|
|
|
case EVPROGNAME:
|
|
|
|
break;
|
|
|
|
case EVSEED:
|
|
|
|
seed = atoi(value);
|
|
|
|
break;
|
|
|
|
case EVDISINP:
|
|
|
|
disinp = stol(value);
|
|
|
|
break;
|
|
|
|
case EVWLINE:
|
|
|
|
status = resize(TRUE, atoi(value));
|
|
|
|
break;
|
|
|
|
case EVCWLINE:
|
|
|
|
status = forwline(TRUE, atoi(value) - getwpos());
|
|
|
|
break;
|
|
|
|
case EVTARGET:
|
|
|
|
curgoal = atoi(value);
|
|
|
|
thisflag = saveflag;
|
|
|
|
break;
|
|
|
|
case EVSEARCH:
|
|
|
|
strcpy(pat, value);
|
|
|
|
rvstrcpy(tap, pat);
|
2005-05-31 11:50:56 -04:00
|
|
|
#if MAGIC
|
2005-09-30 18:26:09 -04:00
|
|
|
mcclear();
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
break;
|
|
|
|
case EVREPLACE:
|
|
|
|
strcpy(rpat, value);
|
|
|
|
break;
|
|
|
|
case EVMATCH:
|
|
|
|
break;
|
|
|
|
case EVKILL:
|
|
|
|
break;
|
|
|
|
case EVCMODE:
|
|
|
|
curbp->b_mode = atoi(value);
|
|
|
|
curwp->w_flag |= WFMODE;
|
|
|
|
break;
|
|
|
|
case EVGMODE:
|
|
|
|
gmode = atoi(value);
|
|
|
|
break;
|
|
|
|
case EVTPAUSE:
|
|
|
|
term.t_pause = atoi(value);
|
|
|
|
break;
|
|
|
|
case EVPENDING:
|
|
|
|
break;
|
|
|
|
case EVLWIDTH:
|
|
|
|
break;
|
|
|
|
case EVLINE:
|
|
|
|
putctext(value);
|
|
|
|
case EVGFLAGS:
|
|
|
|
gflags = atoi(value);
|
|
|
|
break;
|
|
|
|
case EVRVAL:
|
|
|
|
break;
|
|
|
|
case EVTAB:
|
|
|
|
tabmask = atoi(value) - 1;
|
|
|
|
if (tabmask != 0x07 && tabmask != 0x03)
|
|
|
|
tabmask = 0x07;
|
|
|
|
curwp->w_flag |= WFHARD;
|
|
|
|
break;
|
|
|
|
case EVOVERLAP:
|
|
|
|
overlap = atoi(value);
|
|
|
|
break;
|
2005-05-31 11:50:56 -04:00
|
|
|
case EVSCROLLCOUNT:
|
2005-09-30 18:26:09 -04:00
|
|
|
scrollcount = atoi(value);
|
|
|
|
break;
|
2005-05-31 11:50:56 -04:00
|
|
|
case EVSCROLL:
|
|
|
|
#if SCROLLCODE
|
2005-09-30 18:26:09 -04:00
|
|
|
if (!stol(value))
|
|
|
|
term.t_scroll = NULL;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2005-09-30 18:26:09 -04:00
|
|
|
break;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return status;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* itoa:
|
|
|
|
* integer to ascii string.......... This is too
|
|
|
|
* inconsistant to use the system's
|
|
|
|
*
|
|
|
|
* int i; integer to translate to a string
|
|
|
|
*/
|
|
|
|
char *itoa(int i)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int digit; /* current digit being used */
|
|
|
|
char *sp; /* pointer into result */
|
|
|
|
int sign; /* sign of resulting number */
|
2005-09-30 18:26:09 -04:00
|
|
|
static char result[INTWIDTH + 1]; /* resulting string */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
/* record the sign... */
|
2005-05-31 11:50:56 -04:00
|
|
|
sign = 1;
|
|
|
|
if (i < 0) {
|
|
|
|
sign = -1;
|
|
|
|
i = -i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and build the string (backwards!) */
|
|
|
|
sp = result + INTWIDTH;
|
|
|
|
*sp = 0;
|
|
|
|
do {
|
|
|
|
digit = i % 10;
|
|
|
|
*(--sp) = '0' + digit; /* and install the new digit */
|
|
|
|
i = i / 10;
|
|
|
|
} while (i);
|
|
|
|
|
|
|
|
/* and fix the sign */
|
|
|
|
if (sign == -1) {
|
|
|
|
*(--sp) = '-'; /* and install the minus sign */
|
|
|
|
}
|
|
|
|
|
2010-08-29 06:03:55 -04:00
|
|
|
return sp;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* find the type of a passed token
|
|
|
|
*
|
|
|
|
* char *token; token to analyze
|
|
|
|
*/
|
|
|
|
int gettyp(char *token)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
char c; /* first char in token */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* grab the first char (this is all we need) */
|
|
|
|
c = *token;
|
|
|
|
|
|
|
|
/* no blanks!!! */
|
|
|
|
if (c == 0)
|
2010-08-29 06:03:55 -04:00
|
|
|
return TKNUL;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* a numeric literal? */
|
|
|
|
if (c >= '0' && c <= '9')
|
2010-08-29 06:03:55 -04:00
|
|
|
return TKLIT;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
switch (c) {
|
2005-09-30 18:26:09 -04:00
|
|
|
case '"':
|
2010-08-29 06:03:55 -04:00
|
|
|
return TKSTR;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
case '!':
|
2010-08-29 06:03:55 -04:00
|
|
|
return TKDIR;
|
2005-09-30 18:26:09 -04:00
|
|
|
case '@':
|
2010-08-29 06:03:55 -04:00
|
|
|
return TKARG;
|
2005-09-30 18:26:09 -04:00
|
|
|
case '#':
|
2010-08-29 06:03:55 -04:00
|
|
|
return TKBUF;
|
2005-09-30 18:26:09 -04:00
|
|
|
case '$':
|
2010-08-29 06:03:55 -04:00
|
|
|
return TKENV;
|
2005-09-30 18:26:09 -04:00
|
|
|
case '%':
|
2010-08-29 06:03:55 -04:00
|
|
|
return TKVAR;
|
2005-09-30 18:26:09 -04:00
|
|
|
case '&':
|
2010-08-29 06:03:55 -04:00
|
|
|
return TKFUN;
|
2005-09-30 18:26:09 -04:00
|
|
|
case '*':
|
2010-08-29 06:03:55 -04:00
|
|
|
return TKLBL;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
default:
|
2010-08-29 06:03:55 -04:00
|
|
|
return TKCMD;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* find the value of a token
|
|
|
|
*
|
|
|
|
* char *token; token to evaluate
|
|
|
|
*/
|
|
|
|
char *getval(char *token)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
int status; /* error return */
|
|
|
|
struct buffer *bp; /* temp buffer pointer */
|
|
|
|
int blen; /* length of buffer argument */
|
|
|
|
int distmp; /* temporary discmd flag */
|
2005-09-30 18:26:09 -04:00
|
|
|
static char buf[NSTRING]; /* string buffer for some returns */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
switch (gettyp(token)) {
|
2005-09-30 18:26:09 -04:00
|
|
|
case TKNUL:
|
2010-08-29 06:03:55 -04:00
|
|
|
return "";
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
case TKARG: /* interactive argument */
|
|
|
|
strcpy(token, getval(&token[1]));
|
|
|
|
distmp = discmd; /* echo it always! */
|
|
|
|
discmd = TRUE;
|
|
|
|
status = getstring(token, buf, NSTRING, ctoec('\n'));
|
|
|
|
discmd = distmp;
|
|
|
|
if (status == ABORT)
|
2010-08-29 06:03:55 -04:00
|
|
|
return errorm;
|
|
|
|
return buf;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
case TKBUF: /* buffer contents fetch */
|
|
|
|
|
|
|
|
/* grab the right buffer */
|
|
|
|
strcpy(token, getval(&token[1]));
|
|
|
|
bp = bfind(token, FALSE, 0);
|
|
|
|
if (bp == NULL)
|
2010-08-29 06:03:55 -04:00
|
|
|
return errorm;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
/* if the buffer is displayed, get the window
|
|
|
|
vars instead of the buffer vars */
|
|
|
|
if (bp->b_nwnd > 0) {
|
|
|
|
curbp->b_dotp = curwp->w_dotp;
|
|
|
|
curbp->b_doto = curwp->w_doto;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure we are not at the end */
|
|
|
|
if (bp->b_linep == bp->b_dotp)
|
2010-08-29 06:03:55 -04:00
|
|
|
return errorm;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
/* grab the line as an argument */
|
|
|
|
blen = bp->b_dotp->l_used - bp->b_doto;
|
|
|
|
if (blen > NSTRING)
|
|
|
|
blen = NSTRING;
|
|
|
|
strncpy(buf, bp->b_dotp->l_text + bp->b_doto, blen);
|
|
|
|
buf[blen] = 0;
|
|
|
|
|
|
|
|
/* and step the buffer's line ptr ahead a line */
|
|
|
|
bp->b_dotp = bp->b_dotp->l_fp;
|
|
|
|
bp->b_doto = 0;
|
|
|
|
|
|
|
|
/* if displayed buffer, reset window ptr vars */
|
|
|
|
if (bp->b_nwnd > 0) {
|
|
|
|
curwp->w_dotp = curbp->b_dotp;
|
|
|
|
curwp->w_doto = 0;
|
|
|
|
curwp->w_flag |= WFMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and return the spoils */
|
2010-08-29 06:03:55 -04:00
|
|
|
return buf;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
case TKVAR:
|
2010-08-29 06:03:55 -04:00
|
|
|
return gtusr(token + 1);
|
2005-09-30 18:26:09 -04:00
|
|
|
case TKENV:
|
2010-08-29 06:03:55 -04:00
|
|
|
return gtenv(token + 1);
|
2005-09-30 18:26:09 -04:00
|
|
|
case TKFUN:
|
2010-08-29 06:03:55 -04:00
|
|
|
return gtfun(token + 1);
|
2005-09-30 18:26:09 -04:00
|
|
|
case TKDIR:
|
2010-08-29 06:03:55 -04:00
|
|
|
return errorm;
|
2005-09-30 18:26:09 -04:00
|
|
|
case TKLBL:
|
2010-08-29 06:03:55 -04:00
|
|
|
return errorm;
|
2005-09-30 18:26:09 -04:00
|
|
|
case TKLIT:
|
2010-08-29 06:03:55 -04:00
|
|
|
return token;
|
2005-09-30 18:26:09 -04:00
|
|
|
case TKSTR:
|
2010-08-29 06:03:55 -04:00
|
|
|
return token + 1;
|
2005-09-30 18:26:09 -04:00
|
|
|
case TKCMD:
|
2010-08-29 06:03:55 -04:00
|
|
|
return token;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return errorm;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* convert a string to a numeric logical
|
|
|
|
*
|
|
|
|
* char *val; value to check for stol
|
|
|
|
*/
|
|
|
|
int stol(char *val)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
/* check for logical values */
|
|
|
|
if (val[0] == 'F')
|
2010-08-29 06:03:55 -04:00
|
|
|
return FALSE;
|
2005-05-31 11:50:56 -04:00
|
|
|
if (val[0] == 'T')
|
2010-08-29 06:03:55 -04:00
|
|
|
return TRUE;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* check for numeric truth (!= 0) */
|
2010-08-29 06:03:55 -04:00
|
|
|
return (atoi(val) != 0);
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* numeric logical to string logical
|
|
|
|
*
|
|
|
|
* int val; value to translate
|
|
|
|
*/
|
|
|
|
char *ltos(int val)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
if (val)
|
2010-08-29 06:03:55 -04:00
|
|
|
return truem;
|
2005-05-31 11:50:56 -04:00
|
|
|
else
|
2010-08-29 06:03:55 -04:00
|
|
|
return falsem;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* make a string upper case
|
|
|
|
*
|
|
|
|
* char *str; string to upper case
|
|
|
|
*/
|
|
|
|
char *mkupper(char *str)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
char *sp;
|
|
|
|
|
|
|
|
sp = str;
|
|
|
|
while (*sp) {
|
|
|
|
if ('a' <= *sp && *sp <= 'z')
|
|
|
|
*sp += 'A' - 'a';
|
|
|
|
++sp;
|
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return str;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* make a string lower case
|
|
|
|
*
|
|
|
|
* char *str; string to lower case
|
|
|
|
*/
|
|
|
|
char *mklower(char *str)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
|
|
|
char *sp;
|
|
|
|
|
|
|
|
sp = str;
|
|
|
|
while (*sp) {
|
|
|
|
if ('A' <= *sp && *sp <= 'Z')
|
|
|
|
*sp += 'a' - 'A';
|
|
|
|
++sp;
|
|
|
|
}
|
2010-08-29 06:03:55 -04:00
|
|
|
return str;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* take the absolute value of an integer
|
|
|
|
*/
|
|
|
|
int abs(int x)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-08-29 06:03:55 -04:00
|
|
|
return x < 0 ? -x : x;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* returns a random integer
|
|
|
|
*/
|
|
|
|
int ernd(void)
|
|
|
|
{
|
2005-05-31 11:50:56 -04:00
|
|
|
seed = abs(seed * 1721 + 10007);
|
2010-08-29 06:03:55 -04:00
|
|
|
return seed;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* find pattern within source
|
|
|
|
*
|
|
|
|
* char *source; source string to search
|
|
|
|
* char *pattern; string to look for
|
|
|
|
*/
|
|
|
|
int sindex(char *source, char *pattern)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2005-09-30 18:26:09 -04:00
|
|
|
char *sp; /* ptr to current position to scan */
|
|
|
|
char *csp; /* ptr to source string during comparison */
|
|
|
|
char *cp; /* ptr to place to check for equality */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* scanning through the source string */
|
|
|
|
sp = source;
|
|
|
|
while (*sp) {
|
|
|
|
/* scan through the pattern */
|
|
|
|
cp = pattern;
|
|
|
|
csp = sp;
|
|
|
|
while (*cp) {
|
|
|
|
if (!eq(*cp, *csp))
|
|
|
|
break;
|
|
|
|
++cp;
|
|
|
|
++csp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* was it a match? */
|
|
|
|
if (*cp == 0)
|
2010-08-29 06:03:55 -04:00
|
|
|
return (int) (sp - source) + 1;
|
2005-05-31 11:50:56 -04:00
|
|
|
++sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no match at all.. */
|
2010-08-29 06:03:55 -04:00
|
|
|
return 0;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* Filter a string through a translation table
|
|
|
|
*
|
|
|
|
* char *source; string to filter
|
|
|
|
* char *lookup; characters to translate
|
|
|
|
* char *trans; resulting translated characters
|
|
|
|
*/
|
|
|
|
char *xlat(char *source, char *lookup, char *trans)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2010-02-27 05:38:22 -05:00
|
|
|
char *sp; /* pointer into source table */
|
|
|
|
char *lp; /* pointer into lookup table */
|
|
|
|
char *rp; /* pointer into result */
|
2005-05-31 11:50:56 -04:00
|
|
|
static char result[NSTRING]; /* temporary result */
|
|
|
|
|
|
|
|
/* scan source string */
|
|
|
|
sp = source;
|
|
|
|
rp = result;
|
|
|
|
while (*sp) {
|
|
|
|
/* scan lookup table for a match */
|
|
|
|
lp = lookup;
|
|
|
|
while (*lp) {
|
|
|
|
if (*sp == *lp) {
|
|
|
|
*rp++ = trans[lp - lookup];
|
|
|
|
goto xnext;
|
|
|
|
}
|
|
|
|
++lp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no match, copy in the source char untranslated */
|
|
|
|
*rp++ = *sp;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
xnext:++sp;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* terminate and return the result */
|
|
|
|
*rp = 0;
|
2010-08-29 06:03:55 -04:00
|
|
|
return result;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|