2013-09-23 08:55:59 -04:00
|
|
|
/* eval.c -- implements eval.h */
|
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
|
|
|
|
*/
|
|
|
|
|
2015-02-15 01:41:35 -05:00
|
|
|
#include <assert.h>
|
2010-06-03 19:36:01 -04:00
|
|
|
#include <stdio.h>
|
2013-10-08 21:43:15 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-05-18 23:19:13 -04:00
|
|
|
#include <time.h>
|
2010-11-14 21:10:03 -05:00
|
|
|
|
2013-05-28 03:10:16 -04:00
|
|
|
#include "basic.h"
|
2013-05-28 01:04:39 -04:00
|
|
|
#include "bind.h"
|
2013-05-28 03:10:16 -04:00
|
|
|
#include "buffer.h"
|
2013-05-28 02:57:03 -04:00
|
|
|
#include "display.h"
|
2010-06-03 19:36:01 -04:00
|
|
|
#include "estruct.h"
|
2013-05-25 00:37:03 -04:00
|
|
|
#include "exec.h"
|
2013-10-08 04:39:15 -04:00
|
|
|
#include "execute.h"
|
2013-06-10 06:14:09 -04:00
|
|
|
#include "flook.h"
|
2013-05-28 02:46:44 -04:00
|
|
|
#include "input.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"
|
2013-09-25 09:45:05 -04:00
|
|
|
#include "terminal.h"
|
2013-05-28 03:17:15 -04:00
|
|
|
#include "termio.h"
|
2019-06-19 07:49:20 -04:00
|
|
|
#include "util.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]))
|
|
|
|
|
2015-01-09 04:53:29 -05:00
|
|
|
/* Macro argument token types */
|
|
|
|
|
|
|
|
#define TKNUL 0 /* end-of-string */
|
|
|
|
#define TKARG 1 /* interactive argument */
|
|
|
|
#define TKBUF 2 /* buffer argument */
|
|
|
|
#define TKVAR 3 /* user variables */
|
|
|
|
#define TKENV 4 /* environment variables */
|
|
|
|
#define TKFUN 5 /* function.... */
|
|
|
|
#define TKDIR 6 /* directive */
|
|
|
|
#define TKLBL 7 /* line label */
|
|
|
|
#define TKLIT 8 /* numeric literal */
|
|
|
|
#define TKSTR 9 /* quoted string literal */
|
|
|
|
#define TKCMD 10 /* command name */
|
|
|
|
|
|
|
|
static int gettyp( char *token) ;
|
|
|
|
|
2015-01-10 03:23:19 -05:00
|
|
|
/* Emacs global flag bit definitions (for gflags). */
|
|
|
|
/* if GFREAD is set, current buffer will be set on first file (read in) */
|
|
|
|
#define GFREAD 1
|
|
|
|
|
|
|
|
static int gflags = GFREAD ; /* global control flag */
|
|
|
|
|
|
|
|
int readfirst_f( void) {
|
|
|
|
return GFREAD == (gflags & GFREAD) ;
|
|
|
|
}
|
|
|
|
|
2013-10-08 04:24:11 -04:00
|
|
|
int macbug = FALSE ; /* macro debuging flag */
|
|
|
|
int cmdstatus = TRUE ; /* last command status */
|
2015-02-15 22:39:16 -05:00
|
|
|
static int flickcode = FALSE ; /* do flicker supression? */
|
2013-10-09 23:42:33 -04:00
|
|
|
int rval = 0 ; /* return value of a subprocess */
|
|
|
|
|
2013-10-08 04:24:11 -04:00
|
|
|
|
|
|
|
static int saveflag = 0 ; /* Flags, saved with the $target var */
|
|
|
|
|
2013-10-08 03:53:27 -04:00
|
|
|
|
2013-10-09 02:38:55 -04:00
|
|
|
long envram = 0l ; /* # of bytes current in use by malloc */
|
|
|
|
|
|
|
|
|
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) */
|
|
|
|
};
|
|
|
|
|
2013-09-29 02:07:53 -04:00
|
|
|
static char errorm[] = "ERROR" ; /* error literal */
|
|
|
|
|
|
|
|
static int seed = 0 ; /* random number seed */
|
|
|
|
|
|
|
|
static char *ltos( int val) ;
|
2015-01-17 01:45:50 -05:00
|
|
|
static char *mkupper( char *dst, char *src) ;
|
2013-09-29 02:07:53 -04:00
|
|
|
|
2013-05-17 09:50:01 -04:00
|
|
|
/* 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 */
|
2016-02-24 08:17:45 -05:00
|
|
|
"tab", /* tab width, 1... */
|
|
|
|
"hardtab", /* TRUE for hard coded tab, FALSE for soft ones */
|
2013-05-17 09:50:01 -04:00
|
|
|
"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
|
2016-02-24 08:17:45 -05:00
|
|
|
#define EVHARDTAB 38
|
|
|
|
#define EVOVERLAP 39
|
|
|
|
#define EVSCROLLCOUNT 40
|
|
|
|
#define EVSCROLL 41
|
2013-05-17 09:50:01 -04:00
|
|
|
|
|
|
|
enum function_type {
|
2015-03-19 10:37:41 -04:00
|
|
|
NILNAMIC = 0,
|
|
|
|
MONAMIC = (1 << 6),
|
|
|
|
DYNAMIC = (2 << 6),
|
2015-11-24 21:42:33 -05:00
|
|
|
TRINAMIC = (3 << 6)
|
2015-03-19 10:37:41 -04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
enum function_code {
|
|
|
|
UFADD = 0, UFSUB, UFTIMES, UFDIV, UFMOD, UFNEG,
|
|
|
|
UFCAT, UFLEFT, UFRIGHT, UFMID, UFNOT, UFEQUAL,
|
|
|
|
UFLESS, UFGREATER, UFSEQUAL, UFSLESS, UFSGREAT, UFIND,
|
|
|
|
UFAND, UFOR, UFLENGTH, UFUPPER, UFLOWER, UFTRUTH,
|
|
|
|
UFASCII, UFCHR, UFGTKEY, UFRND, UFABS, UFSINDEX,
|
|
|
|
UFENV, UFBIND, UFEXIST, UFFIND, UFBAND, UFBOR,
|
2015-11-24 21:42:33 -05:00
|
|
|
UFBXOR, UFBNOT, UFXLATE
|
2015-03-19 10:37:41 -04:00
|
|
|
} ;
|
2013-05-17 09:50:01 -04:00
|
|
|
|
|
|
|
/* List of recognized user functions. */
|
|
|
|
static struct {
|
|
|
|
const char f_name[ 4] ;
|
2015-03-19 10:37:41 -04:00
|
|
|
const int f_type ;
|
2013-05-17 09:50:01 -04:00
|
|
|
} funcs[] = {
|
2015-03-20 03:07:47 -04:00
|
|
|
{ "abs", UFABS | MONAMIC }, /* absolute value of a number */
|
2015-03-19 10:37:41 -04:00
|
|
|
{ "add", UFADD | DYNAMIC }, /* add two numbers together */
|
2015-03-20 03:07:47 -04:00
|
|
|
{ "and", UFAND | DYNAMIC }, /* logical and */
|
|
|
|
{ "asc", UFASCII | MONAMIC }, /* char to integer conversion */
|
|
|
|
{ "ban", UFBAND | DYNAMIC }, /* bitwise and 9-10-87 jwm */
|
|
|
|
{ "bin", UFBIND | MONAMIC }, /* loopup what function name is bound to a key */
|
|
|
|
{ "bno", UFBNOT | MONAMIC }, /* bitwise not */
|
|
|
|
{ "bor", UFBOR | DYNAMIC }, /* bitwise or 9-10-87 jwm */
|
|
|
|
{ "bxo", UFBXOR | DYNAMIC }, /* bitwise xor 9-10-87 jwm */
|
2015-03-19 10:37:41 -04:00
|
|
|
{ "cat", UFCAT | DYNAMIC }, /* concatinate string */
|
2015-03-20 03:07:47 -04:00
|
|
|
{ "chr", UFCHR | MONAMIC }, /* integer to char conversion */
|
|
|
|
{ "div", UFDIV | DYNAMIC }, /* division */
|
|
|
|
{ "env", UFENV | MONAMIC }, /* retrieve a system environment var */
|
2015-03-19 10:37:41 -04:00
|
|
|
{ "equ", UFEQUAL | DYNAMIC }, /* logical equality check */
|
2015-03-20 03:07:47 -04:00
|
|
|
{ "exi", UFEXIST | MONAMIC }, /* check if a file exists */
|
|
|
|
{ "fin", UFFIND | MONAMIC }, /* look for a file on the path... */
|
2015-03-19 10:37:41 -04:00
|
|
|
{ "gre", UFGREATER | DYNAMIC }, /* logical greater than */
|
2015-03-20 03:07:47 -04:00
|
|
|
{ "gtk", UFGTKEY | NILNAMIC }, /* get 1 charater */
|
2015-03-19 10:37:41 -04:00
|
|
|
{ "ind", UFIND | MONAMIC }, /* evaluate indirect value */
|
2015-03-20 03:07:47 -04:00
|
|
|
{ "lef", UFLEFT | DYNAMIC }, /* left string(string, len) */
|
2015-03-19 10:37:41 -04:00
|
|
|
{ "len", UFLENGTH | MONAMIC }, /* string length */
|
2015-03-20 03:07:47 -04:00
|
|
|
{ "les", UFLESS | DYNAMIC }, /* logical less than */
|
2015-03-19 10:37:41 -04:00
|
|
|
{ "low", UFLOWER | MONAMIC }, /* lower case string */
|
2015-03-20 03:07:47 -04:00
|
|
|
{ "mid", UFMID | TRINAMIC }, /* mid string(string, pos, len) */
|
|
|
|
{ "mod", UFMOD | DYNAMIC }, /* mod */
|
|
|
|
{ "neg", UFNEG | MONAMIC }, /* negate */
|
|
|
|
{ "not", UFNOT | MONAMIC }, /* logical not */
|
|
|
|
{ "or", UFOR | DYNAMIC }, /* logical or */
|
|
|
|
{ "rig", UFRIGHT | DYNAMIC }, /* right string(string, pos) */
|
2015-03-19 10:37:41 -04:00
|
|
|
{ "rnd", UFRND | MONAMIC }, /* get a random number */
|
2015-03-20 03:07:47 -04:00
|
|
|
{ "seq", UFSEQUAL | DYNAMIC }, /* string logical equality check */
|
|
|
|
{ "sgr", UFSGREAT | DYNAMIC }, /* string logical greater than */
|
2015-03-19 10:37:41 -04:00
|
|
|
{ "sin", UFSINDEX | DYNAMIC }, /* find the index of one string in another */
|
2015-03-20 03:07:47 -04:00
|
|
|
{ "sle", UFSLESS | DYNAMIC }, /* string logical less than */
|
|
|
|
{ "sub", UFSUB | DYNAMIC }, /* subtraction */
|
|
|
|
{ "tim", UFTIMES | DYNAMIC }, /* multiplication */
|
|
|
|
{ "tru", UFTRUTH | MONAMIC }, /* Truth of the universe logical test */
|
|
|
|
{ "upp", UFUPPER | MONAMIC }, /* uppercase string */
|
2015-03-19 10:37:41 -04:00
|
|
|
{ "xla", UFXLATE | TRINAMIC }, /* XLATE character string translation */
|
|
|
|
} ;
|
2013-05-17 09:50:01 -04:00
|
|
|
|
|
|
|
|
2010-11-23 17:41:25 -05:00
|
|
|
/* User variables */
|
|
|
|
static struct user_variable uv[MAXVARS + 1];
|
|
|
|
|
2013-09-23 08:55:59 -04:00
|
|
|
/* When emacs' command interpetor needs to get a variable's name,
|
|
|
|
* rather than it's value, it is passed back as a variable description
|
|
|
|
* structure. The v_num field is a index into the appropriate variable table.
|
|
|
|
*/
|
|
|
|
struct variable_description {
|
|
|
|
int v_type; /* Type of variable. */
|
|
|
|
int v_num; /* Ordinal pointer to variable in list. */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void findvar( char *var, struct variable_description *vd, int size) ;
|
|
|
|
static int svar( struct variable_description *var, char *value) ;
|
2015-06-12 05:05:43 -04:00
|
|
|
static char *i_to_a( int i) ;
|
2013-06-13 23:53:59 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* putctext:
|
|
|
|
* replace the current line with the passed in text
|
|
|
|
*
|
|
|
|
* char *iline; contents of new line
|
|
|
|
*/
|
|
|
|
static int putctext( char *iline)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* delete the current line */
|
|
|
|
curwp->w_doto = 0; /* starting at the beginning of the line */
|
|
|
|
if ((status = killtext(TRUE, 1)) != TRUE)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
/* insert the new line */
|
|
|
|
if ((status = linstr(iline)) != TRUE)
|
|
|
|
return status;
|
|
|
|
status = lnewline();
|
|
|
|
backline(TRUE, 1);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-09 02:35:03 -05:00
|
|
|
static char *result ; /* string result */
|
|
|
|
static int ressize = 0 ; /* mark result as uninitialized */
|
|
|
|
|
2015-10-27 00:51:40 -04:00
|
|
|
static int ernd( int i) ;
|
2015-01-09 03:00:11 -05:00
|
|
|
static int sindex( char *source, char *pattern) ;
|
|
|
|
static char *xlat( char *source, char *lookup, char *trans) ;
|
|
|
|
|
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;
|
2015-01-09 02:35:03 -05:00
|
|
|
|
|
|
|
if( ressize == 0) {
|
|
|
|
result = malloc( NSTRING) ;
|
|
|
|
ressize = NSTRING ;
|
|
|
|
}
|
2017-05-18 23:19:13 -04:00
|
|
|
|
|
|
|
seed = time( NULL) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2015-01-09 02:35:03 -05:00
|
|
|
static char *gtfun( char *fname) {
|
2015-02-28 07:29:00 -05:00
|
|
|
unsigned fnum ; /* index to function to eval */
|
2015-01-08 01:16:15 -05:00
|
|
|
char *arg1 ; /* value of first argument */
|
|
|
|
char *arg2 ; /* value of second argument */
|
2015-09-27 09:11:51 -04:00
|
|
|
char *arg3 ; /* last argument */
|
2015-01-08 01:16:15 -05:00
|
|
|
char *retstr ; /* return value */
|
2015-03-20 03:07:47 -04:00
|
|
|
int low, high ; /* binary search indexes */
|
2015-01-07 01:11:58 -05:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* 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 */
|
2015-03-20 03:07:47 -04:00
|
|
|
fnum = ARRAY_SIZE( funcs) ;
|
|
|
|
low = 0 ;
|
|
|
|
high = fnum - 1 ;
|
2015-07-23 22:21:37 -04:00
|
|
|
do {
|
2015-03-20 03:07:47 -04:00
|
|
|
int s, cur ;
|
|
|
|
|
2015-07-23 22:21:37 -04:00
|
|
|
cur = (high + low) / 2 ;
|
2015-03-20 03:07:47 -04:00
|
|
|
s = strcmp( fname, funcs[ cur].f_name) ;
|
|
|
|
if( s == 0) {
|
|
|
|
fnum = cur ;
|
|
|
|
break ;
|
|
|
|
} else if( s < 0)
|
|
|
|
high = cur - 1 ;
|
|
|
|
else
|
|
|
|
low = cur + 1 ;
|
2015-07-23 22:21:37 -04:00
|
|
|
} while( low <= high) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
2015-09-27 09:11:51 -04:00
|
|
|
arg1 = arg2 = arg3 = NULL ;
|
2015-08-31 23:29:08 -04:00
|
|
|
assert( clexec == TRUE) ; /* means macarg can be replaced by gettokval */
|
2005-05-31 11:50:56 -04:00
|
|
|
/* if needed, retrieve the first argument */
|
|
|
|
if (funcs[fnum].f_type >= MONAMIC) {
|
2015-09-27 09:11:51 -04:00
|
|
|
arg1 = getnewtokval() ;
|
|
|
|
if( arg1 == NULL)
|
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) {
|
2015-09-27 09:11:51 -04:00
|
|
|
arg2 = getnewtokval() ;
|
|
|
|
if( arg2 == NULL) {
|
2015-01-08 04:36:25 -05:00
|
|
|
free( arg1) ;
|
2010-08-29 06:03:55 -04:00
|
|
|
return errorm;
|
2015-01-08 04:36:25 -05:00
|
|
|
}
|
2005-09-30 18:26:09 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* if needed, retrieve the third argument */
|
2015-01-08 01:16:15 -05:00
|
|
|
if (funcs[fnum].f_type >= TRINAMIC) {
|
2015-09-27 09:11:51 -04:00
|
|
|
arg3 = getnewtokval() ;
|
|
|
|
if( arg3 == NULL) {
|
2015-01-08 04:36:25 -05:00
|
|
|
free( arg1) ;
|
|
|
|
free( arg2) ;
|
2010-08-29 06:03:55 -04:00
|
|
|
return errorm;
|
2015-01-08 04:36:25 -05:00
|
|
|
}
|
2015-01-08 01:16:15 -05:00
|
|
|
}
|
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! */
|
2015-03-19 10:37:41 -04:00
|
|
|
switch( funcs[ fnum].f_type) {
|
2015-01-08 04:36:25 -05:00
|
|
|
int sz ;
|
|
|
|
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFADD | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = i_to_a( atoi( arg1) + atoi( arg2)) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFSUB | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = i_to_a( atoi( arg1) - atoi( arg2)) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFTIMES | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = i_to_a( atoi( arg1) * atoi( arg2)) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFDIV | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
sz = atoi( arg2) ;
|
2015-03-16 21:04:22 -04:00
|
|
|
retstr = (sz == 0) ? errorm : i_to_a( atoi( arg1) / sz) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFMOD | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
sz = atoi( arg2) ;
|
2015-03-16 21:04:22 -04:00
|
|
|
retstr = (sz == 0) ? errorm : i_to_a( atoi( arg1) % sz) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFNEG | MONAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = i_to_a( -atoi( arg1)) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFCAT | DYNAMIC: {
|
2015-01-08 04:36:25 -05:00
|
|
|
int sz1 ;
|
2015-01-07 01:11:58 -05:00
|
|
|
|
|
|
|
sz1 = strlen( arg1) ;
|
2015-09-27 09:11:51 -04:00
|
|
|
sz = sz1 + strlen( arg2) + 1 ;
|
2015-01-07 01:11:58 -05:00
|
|
|
if( sz > ressize) {
|
|
|
|
free( result) ;
|
|
|
|
result = malloc( sz) ;
|
|
|
|
ressize = sz ;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy( result, arg1) ;
|
2015-09-27 09:11:51 -04:00
|
|
|
strcpy( &result[ sz1], arg2) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
retstr = result ;
|
2015-01-07 01:11:58 -05:00
|
|
|
}
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2017-05-16 00:13:12 -04:00
|
|
|
case UFLEFT | DYNAMIC: {
|
|
|
|
int sz1, i ;
|
|
|
|
|
|
|
|
sz1 = strlen( arg1) ;
|
|
|
|
sz = 0 ;
|
|
|
|
for( i = atoi( arg2) ; i > 0 ; i -= 1) {
|
|
|
|
unicode_t c ;
|
|
|
|
int bytc ;
|
|
|
|
|
|
|
|
bytc = utf8_to_unicode( arg1, sz, sz1, &c) ;
|
|
|
|
if( bytc == 0)
|
|
|
|
break ;
|
|
|
|
else
|
|
|
|
sz += bytc ;
|
|
|
|
}
|
|
|
|
|
2015-01-07 01:47:13 -05:00
|
|
|
if( sz >= ressize) {
|
|
|
|
free( result) ;
|
|
|
|
result = malloc( sz + 1) ;
|
|
|
|
ressize = sz + 1 ;
|
|
|
|
}
|
|
|
|
|
2019-06-19 07:49:20 -04:00
|
|
|
mystrscpy( result, arg1, sz + 1) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
retstr = result ;
|
2017-05-16 00:13:12 -04:00
|
|
|
}
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFRIGHT | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
sz = atoi( arg2) ;
|
2015-01-07 01:47:13 -05:00
|
|
|
if( sz >= ressize) {
|
|
|
|
free( result) ;
|
|
|
|
result = malloc( sz + 1) ;
|
|
|
|
ressize = sz + 1 ;
|
|
|
|
}
|
|
|
|
|
2015-01-08 01:16:15 -05:00
|
|
|
retstr = strcpy( result, &arg1[ strlen( arg1) - sz]) ;
|
|
|
|
break ;
|
2017-05-16 00:13:12 -04:00
|
|
|
case UFMID | TRINAMIC: {
|
|
|
|
int sz1, start, i, bytc ;
|
|
|
|
unicode_t c ;
|
|
|
|
|
|
|
|
sz1 = strlen( arg1) ;
|
|
|
|
start = 0 ;
|
|
|
|
for( i = atoi( arg2) - 1 ; i > 0 ; i -= 1) {
|
|
|
|
bytc = utf8_to_unicode( arg1, start, sz1, &c) ;
|
|
|
|
if( bytc == 0)
|
|
|
|
break ;
|
|
|
|
else
|
|
|
|
start += bytc ;
|
|
|
|
}
|
|
|
|
|
|
|
|
sz = start ;
|
|
|
|
for( i = atoi( arg3) ; i > 0 ; i -= 1) {
|
|
|
|
bytc = utf8_to_unicode( arg1, sz, sz1, &c) ;
|
|
|
|
if( bytc == 0)
|
|
|
|
break ;
|
|
|
|
else
|
|
|
|
sz += bytc ;
|
|
|
|
}
|
|
|
|
|
|
|
|
sz -= start ;
|
2015-01-07 01:47:13 -05:00
|
|
|
if( sz >= ressize) {
|
|
|
|
free( result) ;
|
|
|
|
result = malloc( sz + 1) ;
|
|
|
|
ressize = sz + 1 ;
|
|
|
|
}
|
|
|
|
|
2019-06-19 07:49:20 -04:00
|
|
|
mystrscpy( result, &arg1[ start], sz + 1) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
retstr = result ;
|
2017-05-16 00:13:12 -04:00
|
|
|
}
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFNOT | MONAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = ltos( stol( arg1) == FALSE) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFEQUAL | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = ltos( atoi( arg1) == atoi( arg2)) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFLESS | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = ltos( atoi( arg1) < atoi( arg2)) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFGREATER | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = ltos( atoi( arg1) > atoi( arg2)) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFSEQUAL | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = ltos( strcmp( arg1, arg2) == 0) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFSLESS | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = ltos( strcmp( arg1, arg2) < 0) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFSGREAT | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = ltos( strcmp( arg1, arg2) > 0) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFIND | MONAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = getval( arg1) ;
|
2015-03-19 10:37:41 -04:00
|
|
|
sz = strlen( retstr) + 1 ;
|
|
|
|
if( sz > ressize) {
|
|
|
|
free( result) ;
|
|
|
|
result = malloc( sz) ;
|
|
|
|
ressize = sz ;
|
|
|
|
}
|
|
|
|
|
|
|
|
retstr = strcpy( result, retstr) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFAND | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = ltos( stol( arg1) && stol( arg2)) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFOR | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = ltos( stol( arg1) || stol( arg2)) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFLENGTH | MONAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = i_to_a( strlen( arg1)) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFUPPER | MONAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
sz = strlen( arg1) ;
|
2015-01-08 20:46:03 -05:00
|
|
|
if( sz >= ressize) {
|
|
|
|
free( result) ;
|
|
|
|
result = malloc( sz + 1) ;
|
|
|
|
ressize = sz + 1 ;
|
|
|
|
}
|
|
|
|
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = mkupper( result, arg1) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFLOWER | MONAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
sz = strlen( arg1) ;
|
2015-01-08 20:46:03 -05:00
|
|
|
if( sz >= ressize) {
|
|
|
|
free( result) ;
|
|
|
|
result = malloc( sz + 1) ;
|
|
|
|
ressize = sz + 1 ;
|
|
|
|
}
|
|
|
|
|
2015-09-27 09:11:51 -04:00
|
|
|
strcpy( result, arg1) ; /* result is at least as long as arg1 */
|
2015-01-08 20:46:03 -05:00
|
|
|
retstr = mklower( result) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFTRUTH | MONAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = ltos( atoi( arg1) == 42) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFASCII | MONAMIC: {
|
2015-02-03 23:37:57 -05:00
|
|
|
unicode_t c ;
|
|
|
|
|
2015-09-27 09:11:51 -04:00
|
|
|
utf8_to_unicode( arg1, 0, 4, &c) ;
|
2015-02-03 23:37:57 -05:00
|
|
|
retstr = i_to_a( c) ;
|
|
|
|
}
|
|
|
|
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFCHR | MONAMIC: {
|
2015-02-08 01:26:07 -05:00
|
|
|
unicode_t c ;
|
|
|
|
|
2015-09-27 09:11:51 -04:00
|
|
|
c = atoi( arg1) ;
|
2015-02-08 01:26:07 -05:00
|
|
|
if( c > 0x10FFFF)
|
|
|
|
retstr = errorm ;
|
|
|
|
else {
|
|
|
|
sz = unicode_to_utf8( c, result) ;
|
|
|
|
result[ sz] = 0 ;
|
|
|
|
retstr = result ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFGTKEY | NILNAMIC:
|
2005-09-30 18:26:09 -04:00
|
|
|
result[0] = tgetc();
|
|
|
|
result[1] = 0;
|
2015-01-08 01:16:15 -05:00
|
|
|
retstr = result ;
|
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFRND | MONAMIC:
|
2015-10-27 00:51:40 -04:00
|
|
|
retstr = i_to_a( ernd( atoi( arg1))) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFABS | MONAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = i_to_a( abs( atoi( arg1))) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFSINDEX | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = i_to_a( sindex( arg1, arg2)) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFENV | MONAMIC:
|
2005-05-31 11:50:56 -04:00
|
|
|
#if ENVFUNC
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = getenv( arg1) ;
|
2015-01-08 04:36:25 -05:00
|
|
|
if( retstr == NULL)
|
|
|
|
retstr = "" ;
|
2005-05-31 11:50:56 -04:00
|
|
|
#else
|
2015-01-08 01:16:15 -05:00
|
|
|
retstr = "" ;
|
2005-05-31 11:50:56 -04:00
|
|
|
#endif
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFBIND | MONAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = transbind( arg1) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFEXIST | MONAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = ltos( fexist( arg1)) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFFIND | MONAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = flook( arg1, TRUE) ;
|
2015-01-08 04:36:25 -05:00
|
|
|
if( retstr == NULL)
|
|
|
|
retstr = "" ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFBAND | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = i_to_a( atoi( arg1) & atoi( arg2)) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFBOR | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = i_to_a( atoi( arg1) | atoi( arg2)) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFBXOR | DYNAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = i_to_a( atoi( arg1) ^ atoi( arg2)) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-09-27 09:11:51 -04:00
|
|
|
case UFBNOT | MONAMIC:
|
|
|
|
retstr = i_to_a( ~atoi( arg1)) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
2015-03-19 10:37:41 -04:00
|
|
|
case UFXLATE | TRINAMIC:
|
2015-09-27 09:11:51 -04:00
|
|
|
retstr = xlat( arg1, arg2, arg3) ;
|
2015-01-08 01:16:15 -05:00
|
|
|
break ;
|
|
|
|
default:
|
2015-02-15 01:41:35 -05:00
|
|
|
assert( FALSE) ; /* never should get here */
|
|
|
|
retstr = errorm ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2015-09-27 09:11:51 -04:00
|
|
|
if( arg3)
|
|
|
|
free( arg3) ;
|
|
|
|
|
2015-01-09 02:35:03 -05:00
|
|
|
if( arg2)
|
|
|
|
free( arg2) ;
|
|
|
|
|
2015-01-08 01:16:15 -05:00
|
|
|
if( arg1)
|
|
|
|
free( arg1) ;
|
|
|
|
|
|
|
|
return retstr ;
|
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
|
|
|
|
*/
|
2015-01-09 02:35:03 -05:00
|
|
|
static char *gtusr( char *vname) {
|
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)
|
2015-03-19 08:52:55 -04:00
|
|
|
break ;
|
|
|
|
|
|
|
|
if( strncmp( vname, uv[ vnum].u_name, NVSIZE) == 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
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* gtenv()
|
|
|
|
*
|
|
|
|
* char *vname; name of environment variable to retrieve
|
|
|
|
*/
|
2015-01-09 02:35:03 -05:00
|
|
|
static char *gtenv( char *vname) {
|
2015-02-28 07:29:00 -05:00
|
|
|
unsigned vnum ; /* ordinal number of var referenced */
|
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:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(fillcol);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVPAGELEN:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(term.t_nrow + 1);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVCURCOL:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(getccol(FALSE));
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVCURLINE:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(getcline());
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVRAM:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a((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:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(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);
|
2013-09-29 02:07:53 -04:00
|
|
|
case EVPALETTE: {
|
|
|
|
static char palstr[ 49] = "" ; /* palette string */
|
|
|
|
|
2010-08-29 06:03:55 -04:00
|
|
|
return palstr;
|
2013-09-29 02:07:53 -04:00
|
|
|
}
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVASAVE:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(gasave);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVACOUNT:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(gacount);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVLASTKEY:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(lastkey);
|
2017-05-04 22:49:02 -04:00
|
|
|
case EVCURCHAR: {
|
|
|
|
unicode_t c ;
|
|
|
|
|
|
|
|
lgetchar( &c) ;
|
|
|
|
return i_to_a( c) ;
|
|
|
|
}
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
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:
|
2019-08-12 21:14:08 -04:00
|
|
|
return PROGRAM_NAME_UTF8 ;
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVSEED:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(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:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(curwp->w_ntrows);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVCWLINE:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(getwpos());
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVTARGET:
|
|
|
|
saveflag = lastflag;
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(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:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(curbp->b_mode);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVGMODE:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(gmode);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVTPAUSE:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(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:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(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:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(gflags);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVRVAL:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(rval);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVTAB:
|
2016-02-24 00:22:54 -05:00
|
|
|
return i_to_a( tabwidth) ;
|
2016-02-24 08:17:45 -05:00
|
|
|
case EVHARDTAB:
|
|
|
|
return ltos( hardtab) ;
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVOVERLAP:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(overlap);
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVSCROLLCOUNT:
|
2014-06-18 02:59:40 -04:00
|
|
|
return i_to_a(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
|
|
|
|
}
|
2015-02-15 01:41:35 -05:00
|
|
|
|
|
|
|
assert( FALSE) ; /* again, we should never get here */
|
|
|
|
return errorm ;
|
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 */
|
2010-11-06 12:09:44 -04:00
|
|
|
struct variable_description vd; /* variable num/type */
|
2015-03-19 08:52:55 -04:00
|
|
|
char var[NVSIZE + 2]; /* name of variable to fetch %1234567890\0 */
|
2015-09-28 23:43:30 -04:00
|
|
|
char *value ; /* value to set variable to */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* first get the variable to set.. */
|
|
|
|
if (clexec == FALSE) {
|
2015-08-25 04:34:15 -04:00
|
|
|
status = getstring( "Variable to set: ", var, sizeof var, nlc) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
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 */
|
2015-01-13 03:42:14 -05:00
|
|
|
gettoken( var, sizeof var) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check the legality and find the var */
|
2015-03-19 08:52:55 -04:00
|
|
|
findvar( var, &vd, sizeof var) ;
|
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 */
|
2015-03-19 00:42:47 -04:00
|
|
|
if( f == TRUE) {
|
2015-09-28 23:43:30 -04:00
|
|
|
value = malloc( NSTRING) ;
|
|
|
|
if( value == NULL)
|
|
|
|
return FALSE ;
|
|
|
|
|
2019-06-19 07:49:20 -04:00
|
|
|
/* a bit overcautious here */
|
|
|
|
mystrscpy( value, i_to_a( n), NSTRING) ;
|
2015-03-19 00:42:47 -04:00
|
|
|
} else {
|
2015-09-28 23:43:30 -04:00
|
|
|
status = newmlarg( &value, "Value: ", 0) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
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
|
|
|
|
2015-01-15 01:36:19 -05:00
|
|
|
if( macbug)
|
|
|
|
if( abortc == mdbugout( "(((%s:%s:%s)))", ltos( status), var, value))
|
|
|
|
status = FALSE ;
|
|
|
|
#endif
|
2013-10-07 22:20:01 -04:00
|
|
|
|
2015-01-15 01:36:19 -05:00
|
|
|
/* and return it */
|
2015-09-28 23:43:30 -04:00
|
|
|
free( value) ;
|
2015-01-15 01:36:19 -05:00
|
|
|
return status;
|
|
|
|
}
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2015-02-13 04:46:42 -05:00
|
|
|
static void mlforce( char *s) ;
|
|
|
|
|
2015-01-15 01:36:19 -05:00
|
|
|
#if DEBUGM
|
2015-06-12 05:05:43 -04:00
|
|
|
int mdbugout( char *fmt, ...) {
|
2015-02-15 03:10:44 -05:00
|
|
|
int c ; /* input from kbd, output to terminal */
|
|
|
|
int savediscmd ;
|
2015-06-12 05:05:43 -04:00
|
|
|
va_list ap ;
|
2015-01-15 03:46:45 -05:00
|
|
|
|
2015-01-15 01:36:19 -05:00
|
|
|
/* assignment status ; variable name ; value we tried to assign */
|
|
|
|
/* write out the debug line */
|
2015-02-15 03:10:44 -05:00
|
|
|
savediscmd = discmd ;
|
|
|
|
discmd = TRUE ;
|
2015-06-12 05:05:43 -04:00
|
|
|
va_start( ap, fmt) ;
|
|
|
|
vmlwrite( fmt, ap) ;
|
|
|
|
va_end( ap) ;
|
2015-02-15 03:10:44 -05:00
|
|
|
discmd = savediscmd ;
|
2015-01-15 04:57:12 -05:00
|
|
|
update( TRUE) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2015-01-15 01:36:19 -05:00
|
|
|
/* and get the keystroke to hold the output */
|
|
|
|
c = get1key() ;
|
|
|
|
if( c == abortc)
|
2015-01-15 04:57:12 -05:00
|
|
|
mlforce( "(Macro aborted)") ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2015-01-15 01:36:19 -05:00
|
|
|
return c ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
2015-01-15 01:36:19 -05:00
|
|
|
#endif
|
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
|
|
|
*/
|
2013-09-23 08:55:59 -04:00
|
|
|
static void findvar(char *var, struct variable_description *vd, int size)
|
2005-05-31 11:50:56 -04:00
|
|
|
{
|
2015-03-19 08:52:55 -04:00
|
|
|
unsigned vnum = 0 ; /* subscript in variable arrays */
|
2010-02-27 05:38:22 -05:00
|
|
|
int vtype; /* type to return */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2006-11-19 14:52:18 -05:00
|
|
|
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;
|
2019-06-19 07:49:20 -04:00
|
|
|
mystrscpy( uv[ vnum].u_name, &var[ 1], NVSIZE + 1) ;
|
2005-09-30 18:26:09 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '&': /* indirect operator? */
|
|
|
|
var[4] = 0;
|
|
|
|
if (strcmp(&var[1], "ind") == 0) {
|
|
|
|
/* grab token, and eval it */
|
2015-08-25 04:34:15 -04:00
|
|
|
if( TRUE == gettokval( var, size))
|
|
|
|
goto fvar ;
|
2005-09-30 18:26:09 -04:00
|
|
|
}
|
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
|
|
|
*/
|
2013-09-23 08:55:59 -04:00
|
|
|
static 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:
|
2015-03-16 01:47:03 -04:00
|
|
|
return putctext( value) ;
|
2005-09-30 18:26:09 -04:00
|
|
|
case EVGFLAGS:
|
|
|
|
gflags = atoi(value);
|
|
|
|
break;
|
|
|
|
case EVRVAL:
|
|
|
|
break;
|
|
|
|
case EVTAB:
|
2016-02-24 00:22:54 -05:00
|
|
|
c = atoi( value) ;
|
|
|
|
if( c > 0) {
|
|
|
|
tabwidth = c ;
|
|
|
|
curwp->w_flag |= WFHARD;
|
|
|
|
} else
|
|
|
|
status = FALSE ;
|
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
break;
|
2016-02-24 08:17:45 -05:00
|
|
|
case EVHARDTAB:
|
|
|
|
hardtab = stol( value) ;
|
|
|
|
break ;
|
2005-09-30 18:26:09 -04:00
|
|
|
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
|
|
|
/*
|
2014-06-18 02:59:40 -04:00
|
|
|
* i_to_a:
|
2005-10-01 01:52:45 -04:00
|
|
|
* integer to ascii string.......... This is too
|
|
|
|
* inconsistant to use the system's
|
|
|
|
*
|
|
|
|
* int i; integer to translate to a string
|
|
|
|
*/
|
2015-06-12 05:05:43 -04:00
|
|
|
static char *i_to_a( int i) {
|
2015-06-05 20:25:44 -04:00
|
|
|
unsigned u ;
|
|
|
|
int sign ; /* sign of resulting number */
|
|
|
|
/* returns result string: sign digits null */
|
|
|
|
static char result[ 1 + (sizeof i * 5 + 1) / 2 + 1] ;
|
|
|
|
char *sp = &result[ sizeof result - 1] ; /* points on result's last byte */
|
2013-10-10 23:20:06 -04:00
|
|
|
|
2015-06-05 20:25:44 -04:00
|
|
|
*sp = 0 ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2005-09-30 18:26:09 -04:00
|
|
|
/* record the sign... */
|
2015-06-05 20:25:44 -04:00
|
|
|
sign = i < 0 ;
|
|
|
|
u = sign ? -i : i ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* and build the string (backwards!) */
|
|
|
|
do {
|
2015-06-05 20:25:44 -04:00
|
|
|
*(--sp) = '0' + u % 10 ; /* install the new digit */
|
|
|
|
u = u / 10 ;
|
|
|
|
} while( u) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* and fix the sign */
|
2015-06-05 20:25:44 -04:00
|
|
|
if( sign)
|
2005-05-31 11:50:56 -04:00
|
|
|
*(--sp) = '-'; /* and install the minus sign */
|
|
|
|
|
2015-06-05 20:25:44 -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
|
|
|
|
*/
|
2015-01-09 04:53:29 -05:00
|
|
|
static int gettyp( char *token) {
|
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;
|
|
|
|
|
|
|
|
switch (c) {
|
2015-08-19 03:46:40 -04:00
|
|
|
case 0: /* no blanks!!! */
|
|
|
|
return TKNUL ;
|
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;
|
2015-08-19 03:46:40 -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:
|
2015-08-19 03:46:40 -04:00
|
|
|
/* a numeric literal? */
|
|
|
|
if( (c >= '0' && c <= '9') || c == '-')
|
|
|
|
return TKLIT;
|
|
|
|
else
|
|
|
|
return TKCMD;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 04:53:29 -05:00
|
|
|
int is_it_cmd( char *token) {
|
|
|
|
return TKCMD == gettyp( token) ;
|
|
|
|
}
|
|
|
|
|
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 */
|
2015-02-28 07:29:00 -05:00
|
|
|
unsigned blen ; /* length of buffer argument */
|
2010-02-27 05:38:22 -05:00
|
|
|
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;
|
2015-08-24 21:17:41 -04:00
|
|
|
status = getstring( token, buf, NSTRING, nlc) ;
|
2005-09-30 18:26:09 -04:00
|
|
|
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;
|
2015-02-22 01:58:25 -05:00
|
|
|
if( blen >= sizeof buf)
|
|
|
|
blen = sizeof buf - 1 ;
|
2019-06-19 07:49:20 -04:00
|
|
|
|
|
|
|
mystrscpy( buf, bp->b_dotp->l_text + bp->b_doto, blen + 1) ;
|
2005-09-30 18:26:09 -04:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
2015-01-17 01:45:50 -05:00
|
|
|
static char *ltos( int val) {
|
|
|
|
static char *boolm[] = { "TRUE", "FALSE" } ;
|
|
|
|
|
|
|
|
return boolm[ !val] ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2005-10-01 01:52:45 -04:00
|
|
|
/*
|
|
|
|
* make a string upper case
|
|
|
|
*
|
2015-01-17 01:45:50 -05:00
|
|
|
* char *src ; string to upper case
|
|
|
|
* char *dst ; where to store
|
|
|
|
* dst must be at least as long as src.
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2015-01-17 01:45:50 -05:00
|
|
|
static char *mkupper( char *dst, char *src) {
|
|
|
|
char c, *sp ;
|
|
|
|
|
|
|
|
sp = dst ;
|
|
|
|
while( (c = *src++)) {
|
|
|
|
if( 'a' <= c && c <= 'z')
|
|
|
|
c += 'A' - 'a' ;
|
|
|
|
|
|
|
|
*sp++ = c ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
2015-01-17 01:45:50 -05:00
|
|
|
|
|
|
|
*sp = 0 ;
|
|
|
|
return dst ;
|
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
|
|
|
/*
|
|
|
|
* returns a random integer
|
2017-05-14 03:18:28 -04:00
|
|
|
* ernd( 0) [ 0 .. 2147483647]
|
|
|
|
* ernd( -2147483648) [ 0 .. 2147483647]
|
|
|
|
* ernd( 1) [ 1]
|
|
|
|
* ernd( i) [ 1 .. abs( i)]
|
2005-10-01 01:52:45 -04:00
|
|
|
*/
|
2015-10-27 00:51:40 -04:00
|
|
|
static int ernd( int i) {
|
2017-05-14 03:18:28 -04:00
|
|
|
int s ;
|
2015-10-27 00:51:40 -04:00
|
|
|
|
2017-05-14 03:18:28 -04:00
|
|
|
seed = seed * 1721 + 10007 ;
|
|
|
|
s = ((seed >> 16) & 0x0000FFFF) | (seed << 16) ;
|
|
|
|
s &= ~(1 << 31) ; /* avoid abs() */
|
2015-10-27 00:51:40 -04:00
|
|
|
i = i < 0 ? -i : i ; /* abs( i) */
|
2017-05-14 03:18:28 -04:00
|
|
|
return (i <= 0) ? s : s % i + 1 ;
|
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
|
|
|
|
*/
|
2015-01-09 03:00:11 -05:00
|
|
|
static int sindex( char *source, char *pattern) {
|
2005-09-30 18:26:09 -04:00
|
|
|
char *sp; /* ptr to current position to scan */
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* scanning through the source string */
|
|
|
|
sp = source;
|
2020-06-25 03:50:12 -04:00
|
|
|
int idx = 1 ;
|
|
|
|
int pos = 0 ;
|
|
|
|
int len = strlen( source) ;
|
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
while (*sp) {
|
2015-02-22 01:58:25 -05:00
|
|
|
char *csp; /* ptr to source string during comparison */
|
|
|
|
char *cp; /* ptr to place to check for equality */
|
2020-06-25 03:50:12 -04:00
|
|
|
char c ;
|
|
|
|
unicode_t uc ;
|
2015-02-22 01:58:25 -05:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
/* scan through the pattern */
|
|
|
|
cp = pattern;
|
|
|
|
csp = sp;
|
2020-06-25 03:50:12 -04:00
|
|
|
|
|
|
|
while( (c = *cp++) && eq( c, *csp))
|
|
|
|
csp++ ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
|
|
|
/* was it a match? */
|
2020-06-25 03:50:12 -04:00
|
|
|
if( c == 0)
|
|
|
|
return idx ;
|
|
|
|
|
|
|
|
idx += 1 ;
|
|
|
|
pos += utf8_to_unicode( source, pos, len, &uc) ;
|
|
|
|
sp = &source[ pos] ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
2015-01-09 03:00:11 -05:00
|
|
|
static char *xlat( char *source, char *lookup, char *trans) {
|
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
|
|
|
}
|
2015-02-13 04:46:42 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Force a string out to the message line regardless of the
|
|
|
|
* current $discmd setting. This is needed when $debug is TRUE
|
|
|
|
* and for the write-message and clear-message-line commands
|
|
|
|
*
|
|
|
|
* char *s; string to force out
|
|
|
|
*/
|
|
|
|
static void mlforce( char *s) {
|
|
|
|
int oldcmd; /* original command display flag */
|
|
|
|
|
|
|
|
oldcmd = discmd; /* save the discmd value */
|
|
|
|
discmd = TRUE; /* and turn display on */
|
|
|
|
mlwrite( (*s) ? "%s" : "", s) ; /* write the string out or erase line */
|
|
|
|
discmd = oldcmd; /* and restore the original setting */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function simply clears the message line,
|
|
|
|
* mainly for macro usage
|
|
|
|
*
|
|
|
|
* int f, n; arguments ignored
|
|
|
|
*/
|
|
|
|
int clrmes( int f, int n) {
|
|
|
|
mlforce( "") ;
|
|
|
|
return TRUE ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function writes a string on the message line
|
|
|
|
* mainly for macro usage
|
|
|
|
*
|
|
|
|
* int f, n; arguments ignored
|
|
|
|
*/
|
|
|
|
int writemsg( int f, int n) {
|
2015-09-18 05:55:35 -04:00
|
|
|
int status ;
|
|
|
|
char *buf ; /* buffer to receive message into */
|
2015-02-13 04:46:42 -05:00
|
|
|
|
2015-09-28 23:43:30 -04:00
|
|
|
status = newmlarg( &buf, "Message to write: ", 0) ;
|
|
|
|
if( status == TRUE) {
|
2015-02-13 04:46:42 -05:00
|
|
|
/* write the message out */
|
2015-09-18 05:55:35 -04:00
|
|
|
mlforce( buf) ;
|
2015-09-28 23:43:30 -04:00
|
|
|
free( buf) ;
|
|
|
|
}
|
2015-09-18 05:55:35 -04:00
|
|
|
|
|
|
|
return status ;
|
2015-02-13 04:46:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-18 05:55:35 -04:00
|
|
|
/* end of eval.c */
|