2013-03-29 17:54:05 +01:00
|
|
|
/* $OpenBSD: expr.c,v 1.22 2013/03/28 08:39:28 nicm Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-05-16 18:59:42 +02:00
|
|
|
/*-
|
2012-03-29 21:23:01 +02:00
|
|
|
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
|
2013-02-15 19:36:48 +01:00
|
|
|
* 2011, 2012, 2013
|
2009-05-16 18:59:42 +02:00
|
|
|
* Thorsten Glaser <tg@mirbsd.org>
|
|
|
|
*
|
|
|
|
* Provided that these terms and disclaimer and all copyright notices
|
|
|
|
* are retained or reproduced in an accompanying document, permission
|
|
|
|
* is granted to deal in this work without restriction, including un-
|
|
|
|
* limited rights to use, publicly perform, distribute, sell, modify,
|
|
|
|
* merge, give away, or sublicence.
|
|
|
|
*
|
|
|
|
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
|
|
|
|
* the utmost extent permitted by applicable law, neither express nor
|
|
|
|
* implied; without malicious intent or gross negligence. In no event
|
|
|
|
* may a licensor, author or contributor be held liable for indirect,
|
|
|
|
* direct, other damage, loss, or other issues arising in any way out
|
|
|
|
* of dealing in the work, even if advised of the possibility of such
|
|
|
|
* damage or existence of a defect, except proven that it results out
|
|
|
|
* of said person's immediate fault when using the work as intended.
|
|
|
|
*/
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
#include "sh.h"
|
|
|
|
|
2013-07-21 20:38:56 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/expr.c,v 1.72 2013/07/21 18:38:56 tg Exp $");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2013-04-14 15:36:53 +02:00
|
|
|
/* the order of these enums is constrained by the order of opinfo[] */
|
2005-05-23 05:06:10 +02:00
|
|
|
enum token {
|
|
|
|
/* some (long) unary operators */
|
|
|
|
O_PLUSPLUS = 0, O_MINUSMINUS,
|
|
|
|
/* binary operators */
|
|
|
|
O_EQ, O_NE,
|
|
|
|
/* assignments are assumed to be in range O_ASN .. O_BORASN */
|
|
|
|
O_ASN, O_TIMESASN, O_DIVASN, O_MODASN, O_PLUSASN, O_MINUSASN,
|
2013-04-14 15:36:53 +02:00
|
|
|
#ifndef MKSH_LEGACY_MODE
|
|
|
|
O_ROLASN, O_RORASN,
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
O_LSHIFTASN, O_RSHIFTASN, O_BANDASN, O_BXORASN, O_BORASN,
|
2013-04-14 15:36:53 +02:00
|
|
|
/* binary non-assignment operators */
|
|
|
|
#ifndef MKSH_LEGACY_MODE
|
|
|
|
O_ROL, O_ROR,
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
O_LSHIFT, O_RSHIFT,
|
|
|
|
O_LE, O_GE, O_LT, O_GT,
|
|
|
|
O_LAND,
|
|
|
|
O_LOR,
|
|
|
|
O_TIMES, O_DIV, O_MOD,
|
|
|
|
O_PLUS, O_MINUS,
|
|
|
|
O_BAND,
|
|
|
|
O_BXOR,
|
|
|
|
O_BOR,
|
|
|
|
O_TERN,
|
|
|
|
O_COMMA,
|
|
|
|
/* things after this aren't used as binary operators */
|
|
|
|
/* unary that are not also binaries */
|
|
|
|
O_BNOT, O_LNOT,
|
|
|
|
/* misc */
|
|
|
|
OPEN_PAREN, CLOSE_PAREN, CTERN,
|
|
|
|
/* things that don't appear in the opinfo[] table */
|
|
|
|
VAR, LIT, END, BAD
|
|
|
|
};
|
|
|
|
#define IS_ASSIGNOP(op) ((int)(op) >= (int)O_ASN && (int)(op) <= (int)O_BORASN)
|
|
|
|
|
2008-03-28 14:33:37 +01:00
|
|
|
/* precisions; used to be enum prec but we do arithmetics on it */
|
2013-04-14 15:36:53 +02:00
|
|
|
#define P_PRIMARY 0 /* VAR, LIT, (), ! ~ ++ -- */
|
2008-03-28 14:33:37 +01:00
|
|
|
#define P_MULT 1 /* * / % */
|
|
|
|
#define P_ADD 2 /* + - */
|
2013-04-14 15:36:53 +02:00
|
|
|
#define P_SHIFT 3 /* <<< >>> << >> */
|
2008-03-28 14:33:37 +01:00
|
|
|
#define P_RELATION 4 /* < <= > >= */
|
|
|
|
#define P_EQUALITY 5 /* == != */
|
|
|
|
#define P_BAND 6 /* & */
|
|
|
|
#define P_BXOR 7 /* ^ */
|
|
|
|
#define P_BOR 8 /* | */
|
|
|
|
#define P_LAND 9 /* && */
|
|
|
|
#define P_LOR 10 /* || */
|
|
|
|
#define P_TERN 11 /* ?: */
|
2013-04-14 15:36:53 +02:00
|
|
|
/* = += -= *= /= %= <<<= >>>= <<= >>= &= ^= |= */
|
|
|
|
#define P_ASSIGN 12
|
2008-03-28 14:33:37 +01:00
|
|
|
#define P_COMMA 13 /* , */
|
2005-05-23 05:06:10 +02:00
|
|
|
#define MAX_PREC P_COMMA
|
|
|
|
|
|
|
|
struct opinfo {
|
2013-04-14 15:36:53 +02:00
|
|
|
char name[5];
|
|
|
|
/* name length */
|
|
|
|
uint8_t len;
|
|
|
|
/* precedence: lower is higher */
|
|
|
|
uint8_t prec;
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
2013-04-14 15:36:53 +02:00
|
|
|
/*
|
|
|
|
* Tokens in this table must be ordered so the longest are first
|
2009-06-10 20:12:51 +02:00
|
|
|
* (eg, += before +). If you change something, change the order
|
2005-05-23 05:06:10 +02:00
|
|
|
* of enum token too.
|
|
|
|
*/
|
|
|
|
static const struct opinfo opinfo[] = {
|
2013-04-14 15:36:53 +02:00
|
|
|
{ "++", 2, P_PRIMARY }, /* before + */
|
|
|
|
{ "--", 2, P_PRIMARY }, /* before - */
|
|
|
|
{ "==", 2, P_EQUALITY }, /* before = */
|
|
|
|
{ "!=", 2, P_EQUALITY }, /* before ! */
|
|
|
|
{ "=", 1, P_ASSIGN }, /* keep assigns in a block */
|
|
|
|
{ "*=", 2, P_ASSIGN },
|
|
|
|
{ "/=", 2, P_ASSIGN },
|
|
|
|
{ "%=", 2, P_ASSIGN },
|
|
|
|
{ "+=", 2, P_ASSIGN },
|
|
|
|
{ "-=", 2, P_ASSIGN },
|
|
|
|
#ifndef MKSH_LEGACY_MODE
|
|
|
|
{ "<<<=", 4, P_ASSIGN }, /* before <<< */
|
|
|
|
{ ">>>=", 4, P_ASSIGN }, /* before >>> */
|
|
|
|
#endif
|
|
|
|
{ "<<=", 3, P_ASSIGN },
|
|
|
|
{ ">>=", 3, P_ASSIGN },
|
|
|
|
{ "&=", 2, P_ASSIGN },
|
|
|
|
{ "^=", 2, P_ASSIGN },
|
|
|
|
{ "|=", 2, P_ASSIGN },
|
|
|
|
#ifndef MKSH_LEGACY_MODE
|
|
|
|
{ "<<<", 3, P_SHIFT }, /* before << */
|
|
|
|
{ ">>>", 3, P_SHIFT }, /* before >> */
|
|
|
|
#endif
|
|
|
|
{ "<<", 2, P_SHIFT },
|
|
|
|
{ ">>", 2, P_SHIFT },
|
|
|
|
{ "<=", 2, P_RELATION },
|
|
|
|
{ ">=", 2, P_RELATION },
|
|
|
|
{ "<", 1, P_RELATION },
|
|
|
|
{ ">", 1, P_RELATION },
|
|
|
|
{ "&&", 2, P_LAND },
|
|
|
|
{ "||", 2, P_LOR },
|
|
|
|
{ "*", 1, P_MULT },
|
|
|
|
{ "/", 1, P_MULT },
|
|
|
|
{ "%", 1, P_MULT },
|
|
|
|
{ "+", 1, P_ADD },
|
|
|
|
{ "-", 1, P_ADD },
|
|
|
|
{ "&", 1, P_BAND },
|
|
|
|
{ "^", 1, P_BXOR },
|
|
|
|
{ "|", 1, P_BOR },
|
|
|
|
{ "?", 1, P_TERN },
|
|
|
|
{ ",", 1, P_COMMA },
|
|
|
|
{ "~", 1, P_PRIMARY },
|
|
|
|
{ "!", 1, P_PRIMARY },
|
|
|
|
{ "(", 1, P_PRIMARY },
|
|
|
|
{ ")", 1, P_PRIMARY },
|
|
|
|
{ ":", 1, P_PRIMARY },
|
|
|
|
{ "", 0, P_PRIMARY }
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
2013-02-15 19:36:48 +01:00
|
|
|
typedef struct expr_state {
|
|
|
|
/* expression being evaluated */
|
|
|
|
const char *expression;
|
|
|
|
/* lexical position */
|
|
|
|
const char *tokp;
|
|
|
|
/* value from token() */
|
|
|
|
struct tbl *val;
|
|
|
|
/* variable that is being recursively expanded (EXPRINEVAL flag set) */
|
|
|
|
struct tbl *evaling;
|
|
|
|
/* token from token() */
|
|
|
|
enum token tok;
|
|
|
|
/* don't do assignments (for ?:, &&, ||) */
|
2013-04-26 21:10:58 +02:00
|
|
|
uint8_t noassign;
|
2013-02-15 19:36:48 +01:00
|
|
|
/* evaluating an $(()) expression? */
|
|
|
|
bool arith;
|
|
|
|
/* unsigned arithmetic calculation */
|
|
|
|
bool natural;
|
|
|
|
} Expr_state;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
enum error_type {
|
|
|
|
ET_UNEXPECTED, ET_BADLIT, ET_RECURSIVE,
|
|
|
|
ET_LVALUE, ET_RDONLY, ET_STR
|
|
|
|
};
|
|
|
|
|
2007-01-17 23:51:47 +01:00
|
|
|
static void evalerr(Expr_state *, enum error_type, const char *)
|
2009-12-12 23:27:10 +01:00
|
|
|
MKSH_A_NORETURN;
|
2013-04-14 15:36:53 +02:00
|
|
|
static struct tbl *evalexpr(Expr_state *, unsigned int);
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
static void exprtoken(Expr_state *);
|
2005-05-23 05:06:10 +02:00
|
|
|
static struct tbl *do_ppmm(Expr_state *, enum token, struct tbl *, bool);
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
static void assign_check(Expr_state *, enum token, struct tbl *);
|
2005-05-23 05:06:10 +02:00
|
|
|
static struct tbl *intvar(Expr_state *, struct tbl *);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse and evaluate expression
|
|
|
|
*/
|
|
|
|
int
|
2009-03-14 19:12:55 +01:00
|
|
|
evaluate(const char *expr, mksh_ari_t *rval, int error_ok, bool arith)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct tbl v;
|
|
|
|
int ret;
|
|
|
|
|
2013-04-14 15:36:53 +02:00
|
|
|
v.flag = DEFINED | INTEGER;
|
2005-05-23 05:06:10 +02:00
|
|
|
v.type = 0;
|
|
|
|
ret = v_evaluate(&v, expr, error_ok, arith);
|
|
|
|
*rval = v.val.i;
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (ret);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse and evaluate expression, storing result in vp.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
v_evaluate(struct tbl *vp, const char *expr, volatile int error_ok,
|
|
|
|
bool arith)
|
|
|
|
{
|
|
|
|
struct tbl *v;
|
|
|
|
Expr_state curstate;
|
|
|
|
Expr_state * const es = &curstate;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* save state to allow recursive calls */
|
2013-02-15 19:36:48 +01:00
|
|
|
memset(&curstate, 0, sizeof(curstate));
|
2005-05-23 05:06:10 +02:00
|
|
|
curstate.expression = curstate.tokp = expr;
|
2013-02-15 19:36:48 +01:00
|
|
|
curstate.tok = BAD;
|
2005-05-23 05:06:10 +02:00
|
|
|
curstate.arith = arith;
|
|
|
|
|
|
|
|
newenv(E_ERRH);
|
2012-03-31 19:30:00 +02:00
|
|
|
if ((i = kshsetjmp(e->jbuf))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
/* Clear EXPRINEVAL in of any variables we were playing with */
|
|
|
|
if (curstate.evaling)
|
|
|
|
curstate.evaling->flag &= ~EXPRINEVAL;
|
|
|
|
quitenv(NULL);
|
|
|
|
if (i == LAEXPR) {
|
|
|
|
if (error_ok == KSH_RETURN_ERROR)
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (0);
|
2007-07-22 16:01:50 +02:00
|
|
|
errorfz();
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
unwind(i);
|
2006-05-10 20:54:13 +02:00
|
|
|
/* NOTREACHED */
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (es->tok == END) {
|
|
|
|
es->tok = LIT;
|
|
|
|
es->val = tempvar();
|
|
|
|
}
|
|
|
|
v = intvar(es, evalexpr(es, MAX_PREC));
|
|
|
|
|
|
|
|
if (es->tok != END)
|
|
|
|
evalerr(es, ET_UNEXPECTED, NULL);
|
|
|
|
|
2008-12-17 20:39:23 +01:00
|
|
|
if (es->arith && es->natural)
|
|
|
|
vp->flag |= INT_U;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (vp->flag & INTEGER)
|
|
|
|
setint_v(vp, v, es->arith);
|
|
|
|
else
|
|
|
|
/* can fail if readonly */
|
|
|
|
setstr(vp, str_val(v), error_ok);
|
|
|
|
|
|
|
|
quitenv(NULL);
|
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
evalerr(Expr_state *es, enum error_type type, const char *str)
|
|
|
|
{
|
|
|
|
char tbuf[2];
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
es->arith = false;
|
|
|
|
switch (type) {
|
|
|
|
case ET_UNEXPECTED:
|
|
|
|
switch (es->tok) {
|
|
|
|
case VAR:
|
|
|
|
s = es->val->name;
|
|
|
|
break;
|
|
|
|
case LIT:
|
|
|
|
s = str_val(es->val);
|
|
|
|
break;
|
|
|
|
case END:
|
|
|
|
s = "end of expression";
|
|
|
|
break;
|
|
|
|
case BAD:
|
|
|
|
tbuf[0] = *es->tokp;
|
|
|
|
tbuf[1] = '\0';
|
|
|
|
s = tbuf;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s = opinfo[(int)es->tok].name;
|
|
|
|
}
|
2010-08-28 20:50:58 +02:00
|
|
|
warningf(true, "%s: %s '%s'", es->expression,
|
|
|
|
"unexpected", s);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ET_BADLIT:
|
2010-08-28 20:50:58 +02:00
|
|
|
warningf(true, "%s: %s '%s'", es->expression,
|
|
|
|
"bad number", str);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ET_RECURSIVE:
|
2010-08-28 20:50:58 +02:00
|
|
|
warningf(true, "%s: %s '%s'", es->expression,
|
|
|
|
"expression recurses on parameter", str);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ET_LVALUE:
|
2010-08-28 20:50:58 +02:00
|
|
|
warningf(true, "%s: %s %s",
|
|
|
|
es->expression, str, "requires lvalue");
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ET_RDONLY:
|
2010-08-28 20:50:58 +02:00
|
|
|
warningf(true, "%s: %s %s",
|
2011-12-16 21:03:02 +01:00
|
|
|
es->expression, str, "applied to read-only variable");
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* keep gcc happy */
|
|
|
|
case ET_STR:
|
|
|
|
warningf(true, "%s: %s", es->expression, str);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
unwind(LAEXPR);
|
|
|
|
}
|
|
|
|
|
2013-03-31 20:33:13 +02:00
|
|
|
/* do a ++ or -- operation */
|
|
|
|
static struct tbl *
|
|
|
|
do_ppmm(Expr_state *es, enum token op, struct tbl *vasn, bool is_prefix)
|
|
|
|
{
|
|
|
|
struct tbl *vl;
|
|
|
|
mksh_uari_t oval;
|
|
|
|
|
|
|
|
assign_check(es, op, vasn);
|
|
|
|
|
|
|
|
vl = intvar(es, vasn);
|
|
|
|
oval = vl->val.u;
|
|
|
|
if (op == O_PLUSPLUS)
|
|
|
|
++vl->val.u;
|
|
|
|
else
|
|
|
|
--vl->val.u;
|
2013-07-21 20:38:56 +02:00
|
|
|
if (!es->noassign) {
|
|
|
|
if (vasn->flag & INTEGER)
|
|
|
|
setint_v(vasn, vl, es->arith);
|
|
|
|
else
|
|
|
|
setint(vasn, vl->val.i);
|
|
|
|
}
|
2013-03-31 20:33:13 +02:00
|
|
|
if (!is_prefix)
|
|
|
|
/* undo the increment/decrement */
|
|
|
|
vl->val.u = oval;
|
|
|
|
|
|
|
|
return (vl);
|
|
|
|
}
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
static struct tbl *
|
2013-04-14 15:36:53 +02:00
|
|
|
evalexpr(Expr_state *es, unsigned int prec)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct tbl *vl, *vr = NULL, *vasn;
|
|
|
|
enum token op;
|
2013-04-14 15:36:53 +02:00
|
|
|
mksh_uari_t res = 0, t1, t2, t3;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (prec == P_PRIMARY) {
|
2013-03-31 20:33:13 +02:00
|
|
|
switch ((int)(op = es->tok)) {
|
|
|
|
case O_BNOT:
|
|
|
|
case O_LNOT:
|
|
|
|
case O_MINUS:
|
|
|
|
case O_PLUS:
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2005-05-23 05:06:10 +02:00
|
|
|
vl = intvar(es, evalexpr(es, P_PRIMARY));
|
2013-03-31 20:33:13 +02:00
|
|
|
switch ((int)op) {
|
|
|
|
case O_BNOT:
|
|
|
|
vl->val.u = ~vl->val.u;
|
|
|
|
break;
|
|
|
|
case O_LNOT:
|
|
|
|
vl->val.u = !vl->val.u;
|
|
|
|
break;
|
|
|
|
case O_MINUS:
|
|
|
|
vl->val.u = -vl->val.u;
|
|
|
|
break;
|
|
|
|
case O_PLUS:
|
|
|
|
/* nop */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPEN_PAREN:
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2005-05-23 05:06:10 +02:00
|
|
|
vl = evalexpr(es, MAX_PREC);
|
|
|
|
if (es->tok != CLOSE_PAREN)
|
|
|
|
evalerr(es, ET_STR, "missing )");
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2013-03-31 20:33:13 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case O_PLUSPLUS:
|
|
|
|
case O_MINUSMINUS:
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2005-05-23 05:06:10 +02:00
|
|
|
vl = do_ppmm(es, op, es->val, true);
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2013-03-31 20:33:13 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VAR:
|
|
|
|
case LIT:
|
2005-05-23 05:06:10 +02:00
|
|
|
vl = es->val;
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2013-03-31 20:33:13 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2005-05-23 05:06:10 +02:00
|
|
|
evalerr(es, ET_UNEXPECTED, NULL);
|
2006-05-10 20:54:13 +02:00
|
|
|
/* NOTREACHED */
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2013-03-31 20:33:13 +02:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
if (es->tok == O_PLUSPLUS || es->tok == O_MINUSMINUS) {
|
|
|
|
vl = do_ppmm(es, es->tok, vl, false);
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2013-03-31 20:33:13 +02:00
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (vl);
|
2013-03-31 20:33:13 +02:00
|
|
|
/* prec == P_PRIMARY */
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2013-03-31 20:33:13 +02:00
|
|
|
|
2008-03-28 14:33:37 +01:00
|
|
|
vl = evalexpr(es, prec - 1);
|
2013-03-31 20:33:13 +02:00
|
|
|
while ((int)(op = es->tok) >= (int)O_EQ && (int)op <= (int)O_COMMA &&
|
|
|
|
opinfo[(int)op].prec == prec) {
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2005-05-23 05:06:10 +02:00
|
|
|
vasn = vl;
|
2011-12-11 02:56:43 +01:00
|
|
|
if (op != O_ASN)
|
|
|
|
/* vl may not have a value yet */
|
2005-05-23 05:06:10 +02:00
|
|
|
vl = intvar(es, vl);
|
|
|
|
if (IS_ASSIGNOP(op)) {
|
2011-12-16 21:03:02 +01:00
|
|
|
if (!es->noassign)
|
|
|
|
assign_check(es, op, vasn);
|
2005-05-23 05:06:10 +02:00
|
|
|
vr = intvar(es, evalexpr(es, P_ASSIGN));
|
2013-03-31 20:33:13 +02:00
|
|
|
} else if (op == O_TERN) {
|
|
|
|
bool ev = vl->val.u != 0;
|
|
|
|
|
|
|
|
if (!ev)
|
|
|
|
es->noassign++;
|
|
|
|
vl = evalexpr(es, MAX_PREC);
|
|
|
|
if (!ev)
|
|
|
|
es->noassign--;
|
|
|
|
if (es->tok != CTERN)
|
|
|
|
evalerr(es, ET_STR, "missing :");
|
|
|
|
exprtoken(es);
|
|
|
|
if (ev)
|
|
|
|
es->noassign++;
|
|
|
|
vr = evalexpr(es, P_TERN);
|
|
|
|
if (ev)
|
|
|
|
es->noassign--;
|
|
|
|
vl = ev ? vl : vr;
|
|
|
|
continue;
|
|
|
|
} else if (op != O_LAND && op != O_LOR)
|
2008-03-28 14:33:37 +01:00
|
|
|
vr = intvar(es, evalexpr(es, prec - 1));
|
2013-03-31 20:33:13 +02:00
|
|
|
|
2013-04-14 15:36:53 +02:00
|
|
|
/* common ops setup */
|
2008-12-17 20:39:23 +01:00
|
|
|
switch ((int)op) {
|
2005-05-23 05:06:10 +02:00
|
|
|
case O_DIV:
|
|
|
|
case O_DIVASN:
|
|
|
|
case O_MOD:
|
|
|
|
case O_MODASN:
|
2013-04-14 15:36:53 +02:00
|
|
|
if (vr->val.u == 0) {
|
|
|
|
if (!es->noassign)
|
|
|
|
evalerr(es, ET_STR, "zero divisor");
|
|
|
|
vr->val.u = 1;
|
2013-03-31 20:33:13 +02:00
|
|
|
}
|
2013-04-14 15:36:53 +02:00
|
|
|
/* calculate the absolute values */
|
|
|
|
t1 = vl->val.i < 0 ? -vl->val.u : vl->val.u;
|
|
|
|
t2 = vr->val.i < 0 ? -vr->val.u : vr->val.u;
|
|
|
|
break;
|
|
|
|
#ifndef MKSH_LEGACY_MODE
|
|
|
|
case O_LSHIFT:
|
|
|
|
case O_LSHIFTASN:
|
|
|
|
case O_RSHIFT:
|
|
|
|
case O_RSHIFTASN:
|
|
|
|
case O_ROL:
|
|
|
|
case O_ROLASN:
|
|
|
|
case O_ROR:
|
|
|
|
case O_RORASN:
|
|
|
|
t1 = vl->val.u;
|
|
|
|
t2 = vr->val.u & 31;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case O_LAND:
|
|
|
|
case O_LOR:
|
|
|
|
t1 = vl->val.u;
|
|
|
|
t2 = 0; /* gcc */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
t1 = vl->val.u;
|
|
|
|
t2 = vr->val.u;
|
2013-03-31 20:33:13 +02:00
|
|
|
break;
|
2013-04-14 15:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define cmpop(op) (es->natural ? \
|
|
|
|
(mksh_uari_t)(vl->val.u op vr->val.u) : \
|
|
|
|
(mksh_uari_t)(vl->val.i op vr->val.i) \
|
|
|
|
)
|
|
|
|
|
|
|
|
/* op calculation */
|
|
|
|
switch ((int)op) {
|
2013-03-31 20:33:13 +02:00
|
|
|
case O_TIMES:
|
|
|
|
case O_TIMESASN:
|
2013-04-14 15:36:53 +02:00
|
|
|
res = t1 * t2;
|
|
|
|
break;
|
|
|
|
case O_MOD:
|
|
|
|
case O_MODASN:
|
|
|
|
if (es->natural) {
|
|
|
|
res = vl->val.u % vr->val.u;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto signed_division;
|
|
|
|
case O_DIV:
|
|
|
|
case O_DIVASN:
|
|
|
|
if (es->natural) {
|
|
|
|
res = vl->val.u / vr->val.u;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
signed_division:
|
|
|
|
/*
|
|
|
|
* a / b = abs(a) / abs(b) * sgn((u)a^(u)b)
|
|
|
|
*/
|
|
|
|
t3 = t1 / t2;
|
|
|
|
#ifndef MKSH_LEGACY_MODE
|
|
|
|
res = ((vl->val.u ^ vr->val.u) & 0x80000000) ? -t3 : t3;
|
|
|
|
#else
|
|
|
|
res = ((t1 == vl->val.u ? 0 : 1) ^
|
|
|
|
(t2 == vr->val.u ? 0 : 1)) ? -t3 : t3;
|
|
|
|
#endif
|
|
|
|
if (op == O_MOD || op == O_MODASN) {
|
|
|
|
/*
|
|
|
|
* primitive modulo, to get the sign of
|
|
|
|
* the result correct:
|
|
|
|
* (a % b) = a - ((a / b) * b)
|
|
|
|
* the subtraction and multiplication
|
|
|
|
* are, amazingly enough, sign ignorant
|
|
|
|
*/
|
|
|
|
res = vl->val.u - (res * vr->val.u);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_PLUS:
|
|
|
|
case O_PLUSASN:
|
2013-04-14 15:36:53 +02:00
|
|
|
res = t1 + t2;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_MINUS:
|
|
|
|
case O_MINUSASN:
|
2013-04-14 15:36:53 +02:00
|
|
|
res = t1 - t2;
|
|
|
|
break;
|
|
|
|
#ifndef MKSH_LEGACY_MODE
|
|
|
|
case O_ROL:
|
|
|
|
case O_ROLASN:
|
|
|
|
res = (t1 << t2) | (t1 >> (32 - t2));
|
|
|
|
break;
|
|
|
|
case O_ROR:
|
|
|
|
case O_RORASN:
|
|
|
|
res = (t1 >> t2) | (t1 << (32 - t2));
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2013-04-14 15:36:53 +02:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
case O_LSHIFT:
|
|
|
|
case O_LSHIFTASN:
|
2013-04-14 15:36:53 +02:00
|
|
|
res = t1 << t2;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_RSHIFT:
|
|
|
|
case O_RSHIFTASN:
|
2013-04-01 04:28:35 +02:00
|
|
|
res = es->natural || vl->val.i >= 0 ?
|
2013-04-14 15:36:53 +02:00
|
|
|
t1 >> t2 :
|
|
|
|
~(~t1 >> t2);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_LT:
|
2013-04-14 15:36:53 +02:00
|
|
|
res = cmpop(<);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_LE:
|
2013-04-14 15:36:53 +02:00
|
|
|
res = cmpop(<=);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_GT:
|
2013-04-14 15:36:53 +02:00
|
|
|
res = cmpop(>);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_GE:
|
2013-04-14 15:36:53 +02:00
|
|
|
res = cmpop(>=);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_EQ:
|
2013-04-14 15:36:53 +02:00
|
|
|
res = t1 == t2;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_NE:
|
2013-04-14 15:36:53 +02:00
|
|
|
res = t1 != t2;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_BAND:
|
|
|
|
case O_BANDASN:
|
2013-04-14 15:36:53 +02:00
|
|
|
res = t1 & t2;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_BXOR:
|
|
|
|
case O_BXORASN:
|
2013-04-14 15:36:53 +02:00
|
|
|
res = t1 ^ t2;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_BOR:
|
|
|
|
case O_BORASN:
|
2013-04-14 15:36:53 +02:00
|
|
|
res = t1 | t2;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_LAND:
|
2013-04-14 15:36:53 +02:00
|
|
|
if (!t1)
|
2005-05-23 05:06:10 +02:00
|
|
|
es->noassign++;
|
2008-03-28 14:33:37 +01:00
|
|
|
vr = intvar(es, evalexpr(es, prec - 1));
|
2013-04-14 15:36:53 +02:00
|
|
|
res = t1 && vr->val.u;
|
|
|
|
if (!t1)
|
2005-05-23 05:06:10 +02:00
|
|
|
es->noassign--;
|
|
|
|
break;
|
|
|
|
case O_LOR:
|
2013-04-14 15:36:53 +02:00
|
|
|
if (t1)
|
2005-05-23 05:06:10 +02:00
|
|
|
es->noassign++;
|
2008-03-28 14:33:37 +01:00
|
|
|
vr = intvar(es, evalexpr(es, prec - 1));
|
2013-04-14 15:36:53 +02:00
|
|
|
res = t1 || vr->val.u;
|
|
|
|
if (t1)
|
2005-05-23 05:06:10 +02:00
|
|
|
es->noassign--;
|
|
|
|
break;
|
|
|
|
case O_ASN:
|
|
|
|
case O_COMMA:
|
2013-04-14 15:36:53 +02:00
|
|
|
res = t2;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-03-31 20:33:13 +02:00
|
|
|
|
2013-04-14 15:36:53 +02:00
|
|
|
#undef cmpop
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
if (IS_ASSIGNOP(op)) {
|
2012-10-03 19:24:23 +02:00
|
|
|
vr->val.u = res;
|
2009-10-04 15:19:33 +02:00
|
|
|
if (!es->noassign) {
|
|
|
|
if (vasn->flag & INTEGER)
|
|
|
|
setint_v(vasn, vr, es->arith);
|
|
|
|
else
|
2013-04-01 04:37:53 +02:00
|
|
|
setint(vasn, vr->val.i);
|
2009-10-04 15:19:33 +02:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
vl = vr;
|
2013-03-31 20:33:13 +02:00
|
|
|
} else
|
2012-10-03 19:24:23 +02:00
|
|
|
vl->val.u = res;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (vl);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(Expr_state *es)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2008-12-17 20:39:23 +01:00
|
|
|
const char *cp = es->tokp;
|
2005-05-23 05:06:10 +02:00
|
|
|
int c;
|
|
|
|
char *tvar;
|
|
|
|
|
2013-04-14 15:36:53 +02:00
|
|
|
/* skip whitespace */
|
2008-12-17 20:39:23 +01:00
|
|
|
skip_spaces:
|
|
|
|
while ((c = *cp), ksh_isspace(c))
|
|
|
|
++cp;
|
|
|
|
if (es->tokp == es->expression && c == '#') {
|
|
|
|
/* expression begins with # */
|
2013-04-14 15:36:53 +02:00
|
|
|
/* switch to unsigned */
|
|
|
|
es->natural = true;
|
2008-12-17 20:39:23 +01:00
|
|
|
++cp;
|
|
|
|
goto skip_spaces;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
es->tokp = cp;
|
|
|
|
|
|
|
|
if (c == '\0')
|
|
|
|
es->tok = END;
|
2006-11-10 08:52:04 +01:00
|
|
|
else if (ksh_isalphx(c)) {
|
|
|
|
for (; ksh_isalnux(c); c = *cp)
|
2005-05-23 05:06:10 +02:00
|
|
|
cp++;
|
|
|
|
if (c == '[') {
|
2011-08-27 20:06:52 +02:00
|
|
|
size_t len;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
len = array_ref_len(cp);
|
|
|
|
if (len == 0)
|
|
|
|
evalerr(es, ET_STR, "missing ]");
|
|
|
|
cp += len;
|
|
|
|
}
|
|
|
|
if (es->noassign) {
|
|
|
|
es->val = tempvar();
|
|
|
|
es->val->flag |= EXPRLVALUE;
|
|
|
|
} else {
|
2008-10-28 15:32:43 +01:00
|
|
|
strndupx(tvar, es->tokp, cp - es->tokp, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
es->val = global(tvar);
|
|
|
|
afree(tvar, ATEMP);
|
|
|
|
}
|
|
|
|
es->tok = VAR;
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
} else if (c == '1' && cp[1] == '#') {
|
2008-10-28 15:32:43 +01:00
|
|
|
cp += 2;
|
2009-05-16 17:09:07 +02:00
|
|
|
cp += utf_ptradj(cp);
|
2008-10-28 15:32:43 +01:00
|
|
|
strndupx(tvar, es->tokp, cp - es->tokp, ATEMP);
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
goto process_tvar;
|
2009-09-23 20:04:58 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2009-09-06 19:55:55 +02:00
|
|
|
} else if (c == '\'') {
|
|
|
|
++cp;
|
|
|
|
cp += utf_ptradj(cp);
|
|
|
|
if (*cp++ != '\'')
|
|
|
|
evalerr(es, ET_STR,
|
|
|
|
"multi-character character constant");
|
|
|
|
/* 'x' -> 1#x (x = one multibyte character) */
|
|
|
|
c = cp - es->tokp;
|
|
|
|
tvar = alloc(c + /* NUL */ 1, ATEMP);
|
|
|
|
tvar[0] = '1';
|
|
|
|
tvar[1] = '#';
|
|
|
|
memcpy(tvar + 2, es->tokp + 1, c - 2);
|
|
|
|
tvar[c] = '\0';
|
|
|
|
goto process_tvar;
|
2009-09-23 20:04:58 +02:00
|
|
|
#endif
|
2006-11-10 08:52:04 +01:00
|
|
|
} else if (ksh_isdigit(c)) {
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
while (c != '_' && (ksh_isalnux(c) || c == '#'))
|
|
|
|
c = *cp++;
|
2008-10-28 15:32:43 +01:00
|
|
|
strndupx(tvar, es->tokp, --cp - es->tokp, ATEMP);
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
process_tvar:
|
2005-05-23 05:06:10 +02:00
|
|
|
es->val = tempvar();
|
|
|
|
es->val->flag &= ~INTEGER;
|
|
|
|
es->val->type = 0;
|
|
|
|
es->val->val.s = tvar;
|
|
|
|
if (setint_v(es->val, es->val, es->arith) == NULL)
|
|
|
|
evalerr(es, ET_BADLIT, tvar);
|
|
|
|
afree(tvar, ATEMP);
|
|
|
|
es->tok = LIT;
|
|
|
|
} else {
|
|
|
|
int i, n0;
|
|
|
|
|
|
|
|
for (i = 0; (n0 = opinfo[i].name[0]); i++)
|
2009-08-08 15:08:53 +02:00
|
|
|
if (c == n0 && strncmp(cp, opinfo[i].name,
|
|
|
|
(size_t)opinfo[i].len) == 0) {
|
2008-12-17 20:39:23 +01:00
|
|
|
es->tok = (enum token)i;
|
2005-05-23 05:06:10 +02:00
|
|
|
cp += opinfo[i].len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!n0)
|
|
|
|
es->tok = BAD;
|
|
|
|
}
|
|
|
|
es->tokp = cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
assign_check(Expr_state *es, enum token op, struct tbl *vasn)
|
|
|
|
{
|
2013-02-15 19:36:48 +01:00
|
|
|
if (es->tok == END || !vasn ||
|
2009-06-08 22:13:07 +02:00
|
|
|
(vasn->name[0] == '\0' && !(vasn->flag & EXPRLVALUE)))
|
2008-12-17 20:39:23 +01:00
|
|
|
evalerr(es, ET_LVALUE, opinfo[(int)op].name);
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (vasn->flag & RDONLY)
|
2008-12-17 20:39:23 +01:00
|
|
|
evalerr(es, ET_RDONLY, opinfo[(int)op].name);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2012-06-28 22:02:29 +02:00
|
|
|
struct tbl *
|
2005-05-23 05:06:10 +02:00
|
|
|
tempvar(void)
|
|
|
|
{
|
|
|
|
struct tbl *vp;
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
vp = alloc(sizeof(struct tbl), ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
vp->flag = ISSET|INTEGER;
|
|
|
|
vp->type = 0;
|
|
|
|
vp->areap = ATEMP;
|
2009-08-28 23:01:27 +02:00
|
|
|
vp->ua.hval = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
vp->val.i = 0;
|
|
|
|
vp->name[0] = '\0';
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (vp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* cast (string) variable to temporary integer variable */
|
|
|
|
static struct tbl *
|
|
|
|
intvar(Expr_state *es, struct tbl *vp)
|
|
|
|
{
|
|
|
|
struct tbl *vq;
|
|
|
|
|
|
|
|
/* try to avoid replacing a temp var with another temp var */
|
|
|
|
if (vp->name[0] == '\0' &&
|
|
|
|
(vp->flag & (ISSET|INTEGER|EXPRLVALUE)) == (ISSET|INTEGER))
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (vp);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
vq = tempvar();
|
|
|
|
if (setint_v(vq, vp, es->arith) == NULL) {
|
|
|
|
if (vp->flag & EXPRINEVAL)
|
|
|
|
evalerr(es, ET_RECURSIVE, vp->name);
|
|
|
|
es->evaling = vp;
|
|
|
|
vp->flag |= EXPRINEVAL;
|
|
|
|
v_evaluate(vq, str_val(vp), KSH_UNWIND_ERROR, es->arith);
|
|
|
|
vp->flag &= ~EXPRINEVAL;
|
|
|
|
es->evaling = NULL;
|
|
|
|
}
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (vq);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-09-26 06:01:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* UTF-8 support code: high-level functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
utf_widthadj(const char *src, const char **dst)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
unsigned int wc;
|
|
|
|
int width;
|
|
|
|
|
|
|
|
if (!UTFMODE || (len = utf_mbtowc(&wc, src)) == (size_t)-1 ||
|
|
|
|
wc == 0)
|
|
|
|
len = width = 1;
|
|
|
|
else if ((width = utf_wcwidth(wc)) < 0)
|
|
|
|
/* XXX use 2 for x_zotc3 here? */
|
|
|
|
width = 1;
|
|
|
|
|
|
|
|
if (dst)
|
|
|
|
*dst = src + len;
|
|
|
|
return (width);
|
|
|
|
}
|
|
|
|
|
2011-08-27 20:06:52 +02:00
|
|
|
size_t
|
2009-11-28 15:28:03 +01:00
|
|
|
utf_mbswidth(const char *s)
|
2009-09-26 06:01:34 +02:00
|
|
|
{
|
2011-08-27 20:06:52 +02:00
|
|
|
size_t len, width = 0;
|
2009-09-26 06:01:34 +02:00
|
|
|
unsigned int wc;
|
2011-08-27 20:06:52 +02:00
|
|
|
int cw;
|
2009-09-26 06:01:34 +02:00
|
|
|
|
|
|
|
if (!UTFMODE)
|
|
|
|
return (strlen(s));
|
|
|
|
|
|
|
|
while (*s)
|
2009-11-28 15:28:03 +01:00
|
|
|
if (((len = utf_mbtowc(&wc, s)) == (size_t)-1) ||
|
|
|
|
((cw = utf_wcwidth(wc)) == -1)) {
|
2009-09-26 06:01:34 +02:00
|
|
|
s++;
|
|
|
|
width += 1;
|
|
|
|
} else {
|
|
|
|
s += len;
|
|
|
|
width += cw;
|
|
|
|
}
|
|
|
|
return (width);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
utf_skipcols(const char *p, int cols)
|
|
|
|
{
|
|
|
|
int c = 0;
|
|
|
|
|
2009-12-05 21:17:59 +01:00
|
|
|
while (c < cols) {
|
|
|
|
if (!*p)
|
|
|
|
return (p + cols - c);
|
2009-09-26 06:01:34 +02:00
|
|
|
c += utf_widthadj(p, &p);
|
2009-12-05 21:17:59 +01:00
|
|
|
}
|
2009-09-26 06:01:34 +02:00
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
utf_ptradj(const char *src)
|
|
|
|
{
|
|
|
|
register size_t n;
|
|
|
|
|
|
|
|
if (!UTFMODE ||
|
|
|
|
*(const unsigned char *)(src) < 0xC2 ||
|
|
|
|
(n = utf_mbtowc(NULL, src)) == (size_t)-1)
|
|
|
|
n = 1;
|
|
|
|
return (n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* UTF-8 support code: low-level functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* CESU-8 multibyte and wide character conversion crafted for mksh */
|
|
|
|
|
|
|
|
size_t
|
|
|
|
utf_mbtowc(unsigned int *dst, const char *src)
|
|
|
|
{
|
|
|
|
const unsigned char *s = (const unsigned char *)src;
|
|
|
|
unsigned int c, wc;
|
|
|
|
|
|
|
|
if ((wc = *s++) < 0x80) {
|
|
|
|
out:
|
|
|
|
if (dst != NULL)
|
|
|
|
*dst = wc;
|
|
|
|
return (wc ? ((const char *)s - src) : 0);
|
|
|
|
}
|
|
|
|
if (wc < 0xC2 || wc >= 0xF0)
|
|
|
|
/* < 0xC0: spurious second byte */
|
|
|
|
/* < 0xC2: non-minimalistic mapping error in 2-byte seqs */
|
|
|
|
/* > 0xEF: beyond BMP */
|
|
|
|
goto ilseq;
|
|
|
|
|
|
|
|
if (wc < 0xE0) {
|
|
|
|
wc = (wc & 0x1F) << 6;
|
|
|
|
if (((c = *s++) & 0xC0) != 0x80)
|
|
|
|
goto ilseq;
|
|
|
|
wc |= c & 0x3F;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
wc = (wc & 0x0F) << 12;
|
|
|
|
|
|
|
|
if (((c = *s++) & 0xC0) != 0x80)
|
|
|
|
goto ilseq;
|
|
|
|
wc |= (c & 0x3F) << 6;
|
|
|
|
|
|
|
|
if (((c = *s++) & 0xC0) != 0x80)
|
|
|
|
goto ilseq;
|
|
|
|
wc |= c & 0x3F;
|
|
|
|
|
|
|
|
/* Check for non-minimalistic mapping error in 3-byte seqs */
|
|
|
|
if (wc >= 0x0800 && wc <= 0xFFFD)
|
|
|
|
goto out;
|
|
|
|
ilseq:
|
|
|
|
return ((size_t)(-1));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
utf_wctomb(char *dst, unsigned int wc)
|
|
|
|
{
|
|
|
|
unsigned char *d;
|
|
|
|
|
|
|
|
if (wc < 0x80) {
|
|
|
|
*dst = wc;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
d = (unsigned char *)dst;
|
|
|
|
if (wc < 0x0800)
|
|
|
|
*d++ = (wc >> 6) | 0xC0;
|
|
|
|
else {
|
|
|
|
*d++ = ((wc = wc > 0xFFFD ? 0xFFFD : wc) >> 12) | 0xE0;
|
|
|
|
*d++ = ((wc >> 6) & 0x3F) | 0x80;
|
|
|
|
}
|
|
|
|
*d++ = (wc & 0x3F) | 0x80;
|
|
|
|
return ((char *)d - dst);
|
|
|
|
}
|
|
|
|
|
2011-09-07 17:24:22 +02:00
|
|
|
/*
|
|
|
|
* Wrapper around access(2) because it says root can execute everything
|
|
|
|
* on some operating systems. Does not set errno, no user needs it. Use
|
|
|
|
* this iff mode can have the X_OK bit set, access otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ksh_access(const char *fn, int mode)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if ((rv = access(fn, mode)) == 0 && kshuid == 0 && (mode & X_OK) &&
|
|
|
|
(rv = stat(fn, &sb)) == 0 && !S_ISDIR(sb.st_mode) &&
|
|
|
|
(sb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
|
|
|
|
rv = -1;
|
|
|
|
|
|
|
|
return (rv);
|
|
|
|
}
|
2013-06-01 01:27:14 +02:00
|
|
|
|
|
|
|
#ifndef MKSH_mirbsd_wcwidth
|
|
|
|
/* From: X11/xc/programs/xterm/wcwidth.c,v 1.6 2013/05/31 23:27:09 tg Exp $ */
|
|
|
|
|
|
|
|
struct mb_ucsrange {
|
|
|
|
unsigned short beg;
|
|
|
|
unsigned short end;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int mb_ucsbsearch(const struct mb_ucsrange arr[], size_t elems,
|
|
|
|
unsigned int val);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generated by MirOS: contrib/code/Snippets/eawparse,v 1.1 2013/05/31 23:27:16 tg Exp $
|
|
|
|
* from Unicode 6.2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const struct mb_ucsrange mb_ucs_combining[] = {
|
|
|
|
{ 0x0300, 0x036F },
|
|
|
|
{ 0x0483, 0x0489 },
|
|
|
|
{ 0x0591, 0x05BD },
|
|
|
|
{ 0x05BF, 0x05BF },
|
|
|
|
{ 0x05C1, 0x05C2 },
|
|
|
|
{ 0x05C4, 0x05C5 },
|
|
|
|
{ 0x05C7, 0x05C7 },
|
|
|
|
{ 0x0600, 0x0604 },
|
|
|
|
{ 0x0610, 0x061A },
|
|
|
|
{ 0x064B, 0x065F },
|
|
|
|
{ 0x0670, 0x0670 },
|
|
|
|
{ 0x06D6, 0x06DD },
|
|
|
|
{ 0x06DF, 0x06E4 },
|
|
|
|
{ 0x06E7, 0x06E8 },
|
|
|
|
{ 0x06EA, 0x06ED },
|
|
|
|
{ 0x070F, 0x070F },
|
|
|
|
{ 0x0711, 0x0711 },
|
|
|
|
{ 0x0730, 0x074A },
|
|
|
|
{ 0x07A6, 0x07B0 },
|
|
|
|
{ 0x07EB, 0x07F3 },
|
|
|
|
{ 0x0816, 0x0819 },
|
|
|
|
{ 0x081B, 0x0823 },
|
|
|
|
{ 0x0825, 0x0827 },
|
|
|
|
{ 0x0829, 0x082D },
|
|
|
|
{ 0x0859, 0x085B },
|
|
|
|
{ 0x08E4, 0x08FE },
|
|
|
|
{ 0x0900, 0x0902 },
|
|
|
|
{ 0x093A, 0x093A },
|
|
|
|
{ 0x093C, 0x093C },
|
|
|
|
{ 0x0941, 0x0948 },
|
|
|
|
{ 0x094D, 0x094D },
|
|
|
|
{ 0x0951, 0x0957 },
|
|
|
|
{ 0x0962, 0x0963 },
|
|
|
|
{ 0x0981, 0x0981 },
|
|
|
|
{ 0x09BC, 0x09BC },
|
|
|
|
{ 0x09C1, 0x09C4 },
|
|
|
|
{ 0x09CD, 0x09CD },
|
|
|
|
{ 0x09E2, 0x09E3 },
|
|
|
|
{ 0x0A01, 0x0A02 },
|
|
|
|
{ 0x0A3C, 0x0A3C },
|
|
|
|
{ 0x0A41, 0x0A42 },
|
|
|
|
{ 0x0A47, 0x0A48 },
|
|
|
|
{ 0x0A4B, 0x0A4D },
|
|
|
|
{ 0x0A51, 0x0A51 },
|
|
|
|
{ 0x0A70, 0x0A71 },
|
|
|
|
{ 0x0A75, 0x0A75 },
|
|
|
|
{ 0x0A81, 0x0A82 },
|
|
|
|
{ 0x0ABC, 0x0ABC },
|
|
|
|
{ 0x0AC1, 0x0AC5 },
|
|
|
|
{ 0x0AC7, 0x0AC8 },
|
|
|
|
{ 0x0ACD, 0x0ACD },
|
|
|
|
{ 0x0AE2, 0x0AE3 },
|
|
|
|
{ 0x0B01, 0x0B01 },
|
|
|
|
{ 0x0B3C, 0x0B3C },
|
|
|
|
{ 0x0B3F, 0x0B3F },
|
|
|
|
{ 0x0B41, 0x0B44 },
|
|
|
|
{ 0x0B4D, 0x0B4D },
|
|
|
|
{ 0x0B56, 0x0B56 },
|
|
|
|
{ 0x0B62, 0x0B63 },
|
|
|
|
{ 0x0B82, 0x0B82 },
|
|
|
|
{ 0x0BC0, 0x0BC0 },
|
|
|
|
{ 0x0BCD, 0x0BCD },
|
|
|
|
{ 0x0C3E, 0x0C40 },
|
|
|
|
{ 0x0C46, 0x0C48 },
|
|
|
|
{ 0x0C4A, 0x0C4D },
|
|
|
|
{ 0x0C55, 0x0C56 },
|
|
|
|
{ 0x0C62, 0x0C63 },
|
|
|
|
{ 0x0CBC, 0x0CBC },
|
|
|
|
{ 0x0CBF, 0x0CBF },
|
|
|
|
{ 0x0CC6, 0x0CC6 },
|
|
|
|
{ 0x0CCC, 0x0CCD },
|
|
|
|
{ 0x0CE2, 0x0CE3 },
|
|
|
|
{ 0x0D41, 0x0D44 },
|
|
|
|
{ 0x0D4D, 0x0D4D },
|
|
|
|
{ 0x0D62, 0x0D63 },
|
|
|
|
{ 0x0DCA, 0x0DCA },
|
|
|
|
{ 0x0DD2, 0x0DD4 },
|
|
|
|
{ 0x0DD6, 0x0DD6 },
|
|
|
|
{ 0x0E31, 0x0E31 },
|
|
|
|
{ 0x0E34, 0x0E3A },
|
|
|
|
{ 0x0E47, 0x0E4E },
|
|
|
|
{ 0x0EB1, 0x0EB1 },
|
|
|
|
{ 0x0EB4, 0x0EB9 },
|
|
|
|
{ 0x0EBB, 0x0EBC },
|
|
|
|
{ 0x0EC8, 0x0ECD },
|
|
|
|
{ 0x0F18, 0x0F19 },
|
|
|
|
{ 0x0F35, 0x0F35 },
|
|
|
|
{ 0x0F37, 0x0F37 },
|
|
|
|
{ 0x0F39, 0x0F39 },
|
|
|
|
{ 0x0F71, 0x0F7E },
|
|
|
|
{ 0x0F80, 0x0F84 },
|
|
|
|
{ 0x0F86, 0x0F87 },
|
|
|
|
{ 0x0F8D, 0x0F97 },
|
|
|
|
{ 0x0F99, 0x0FBC },
|
|
|
|
{ 0x0FC6, 0x0FC6 },
|
|
|
|
{ 0x102D, 0x1030 },
|
|
|
|
{ 0x1032, 0x1037 },
|
|
|
|
{ 0x1039, 0x103A },
|
|
|
|
{ 0x103D, 0x103E },
|
|
|
|
{ 0x1058, 0x1059 },
|
|
|
|
{ 0x105E, 0x1060 },
|
|
|
|
{ 0x1071, 0x1074 },
|
|
|
|
{ 0x1082, 0x1082 },
|
|
|
|
{ 0x1085, 0x1086 },
|
|
|
|
{ 0x108D, 0x108D },
|
|
|
|
{ 0x109D, 0x109D },
|
|
|
|
{ 0x1160, 0x11FF },
|
|
|
|
{ 0x135D, 0x135F },
|
|
|
|
{ 0x1712, 0x1714 },
|
|
|
|
{ 0x1732, 0x1734 },
|
|
|
|
{ 0x1752, 0x1753 },
|
|
|
|
{ 0x1772, 0x1773 },
|
|
|
|
{ 0x17B4, 0x17B5 },
|
|
|
|
{ 0x17B7, 0x17BD },
|
|
|
|
{ 0x17C6, 0x17C6 },
|
|
|
|
{ 0x17C9, 0x17D3 },
|
|
|
|
{ 0x17DD, 0x17DD },
|
|
|
|
{ 0x180B, 0x180D },
|
|
|
|
{ 0x18A9, 0x18A9 },
|
|
|
|
{ 0x1920, 0x1922 },
|
|
|
|
{ 0x1927, 0x1928 },
|
|
|
|
{ 0x1932, 0x1932 },
|
|
|
|
{ 0x1939, 0x193B },
|
|
|
|
{ 0x1A17, 0x1A18 },
|
|
|
|
{ 0x1A56, 0x1A56 },
|
|
|
|
{ 0x1A58, 0x1A5E },
|
|
|
|
{ 0x1A60, 0x1A60 },
|
|
|
|
{ 0x1A62, 0x1A62 },
|
|
|
|
{ 0x1A65, 0x1A6C },
|
|
|
|
{ 0x1A73, 0x1A7C },
|
|
|
|
{ 0x1A7F, 0x1A7F },
|
|
|
|
{ 0x1B00, 0x1B03 },
|
|
|
|
{ 0x1B34, 0x1B34 },
|
|
|
|
{ 0x1B36, 0x1B3A },
|
|
|
|
{ 0x1B3C, 0x1B3C },
|
|
|
|
{ 0x1B42, 0x1B42 },
|
|
|
|
{ 0x1B6B, 0x1B73 },
|
|
|
|
{ 0x1B80, 0x1B81 },
|
|
|
|
{ 0x1BA2, 0x1BA5 },
|
|
|
|
{ 0x1BA8, 0x1BA9 },
|
|
|
|
{ 0x1BAB, 0x1BAB },
|
|
|
|
{ 0x1BE6, 0x1BE6 },
|
|
|
|
{ 0x1BE8, 0x1BE9 },
|
|
|
|
{ 0x1BED, 0x1BED },
|
|
|
|
{ 0x1BEF, 0x1BF1 },
|
|
|
|
{ 0x1C2C, 0x1C33 },
|
|
|
|
{ 0x1C36, 0x1C37 },
|
|
|
|
{ 0x1CD0, 0x1CD2 },
|
|
|
|
{ 0x1CD4, 0x1CE0 },
|
|
|
|
{ 0x1CE2, 0x1CE8 },
|
|
|
|
{ 0x1CED, 0x1CED },
|
|
|
|
{ 0x1CF4, 0x1CF4 },
|
|
|
|
{ 0x1DC0, 0x1DE6 },
|
|
|
|
{ 0x1DFC, 0x1DFF },
|
|
|
|
{ 0x200B, 0x200F },
|
|
|
|
{ 0x202A, 0x202E },
|
|
|
|
{ 0x2060, 0x2064 },
|
|
|
|
{ 0x206A, 0x206F },
|
|
|
|
{ 0x20D0, 0x20F0 },
|
|
|
|
{ 0x2CEF, 0x2CF1 },
|
|
|
|
{ 0x2D7F, 0x2D7F },
|
|
|
|
{ 0x2DE0, 0x2DFF },
|
|
|
|
{ 0x302A, 0x302D },
|
|
|
|
{ 0x3099, 0x309A },
|
|
|
|
{ 0xA66F, 0xA672 },
|
|
|
|
{ 0xA674, 0xA67D },
|
|
|
|
{ 0xA69F, 0xA69F },
|
|
|
|
{ 0xA6F0, 0xA6F1 },
|
|
|
|
{ 0xA802, 0xA802 },
|
|
|
|
{ 0xA806, 0xA806 },
|
|
|
|
{ 0xA80B, 0xA80B },
|
|
|
|
{ 0xA825, 0xA826 },
|
|
|
|
{ 0xA8C4, 0xA8C4 },
|
|
|
|
{ 0xA8E0, 0xA8F1 },
|
|
|
|
{ 0xA926, 0xA92D },
|
|
|
|
{ 0xA947, 0xA951 },
|
|
|
|
{ 0xA980, 0xA982 },
|
|
|
|
{ 0xA9B3, 0xA9B3 },
|
|
|
|
{ 0xA9B6, 0xA9B9 },
|
|
|
|
{ 0xA9BC, 0xA9BC },
|
|
|
|
{ 0xAA29, 0xAA2E },
|
|
|
|
{ 0xAA31, 0xAA32 },
|
|
|
|
{ 0xAA35, 0xAA36 },
|
|
|
|
{ 0xAA43, 0xAA43 },
|
|
|
|
{ 0xAA4C, 0xAA4C },
|
|
|
|
{ 0xAAB0, 0xAAB0 },
|
|
|
|
{ 0xAAB2, 0xAAB4 },
|
|
|
|
{ 0xAAB7, 0xAAB8 },
|
|
|
|
{ 0xAABE, 0xAABF },
|
|
|
|
{ 0xAAC1, 0xAAC1 },
|
|
|
|
{ 0xAAEC, 0xAAED },
|
|
|
|
{ 0xAAF6, 0xAAF6 },
|
|
|
|
{ 0xABE5, 0xABE5 },
|
|
|
|
{ 0xABE8, 0xABE8 },
|
|
|
|
{ 0xABED, 0xABED },
|
|
|
|
{ 0xFB1E, 0xFB1E },
|
|
|
|
{ 0xFE00, 0xFE0F },
|
|
|
|
{ 0xFE20, 0xFE26 },
|
|
|
|
{ 0xFEFF, 0xFEFF },
|
|
|
|
{ 0xFFF9, 0xFFFB }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct mb_ucsrange mb_ucs_fullwidth[] = {
|
|
|
|
{ 0x1100, 0x115F },
|
|
|
|
{ 0x2329, 0x232A },
|
|
|
|
{ 0x2E80, 0x303E },
|
|
|
|
{ 0x3040, 0xA4CF },
|
|
|
|
{ 0xA960, 0xA97F },
|
|
|
|
{ 0xAC00, 0xD7A3 },
|
|
|
|
{ 0xF900, 0xFAFF },
|
|
|
|
{ 0xFE10, 0xFE19 },
|
|
|
|
{ 0xFE30, 0xFE6F },
|
|
|
|
{ 0xFF00, 0xFF60 },
|
|
|
|
{ 0xFFE0, 0xFFE6 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* simple binary search in ranges, with bounds optimisation */
|
|
|
|
static int
|
|
|
|
mb_ucsbsearch(const struct mb_ucsrange arr[], size_t elems, unsigned int val)
|
|
|
|
{
|
|
|
|
size_t min = 0, mid, max = elems;
|
|
|
|
|
|
|
|
if (val < arr[min].beg || val > arr[max - 1].end)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
while (min < max) {
|
|
|
|
mid = (min + max) / 2;
|
|
|
|
|
|
|
|
if (val < arr[mid].beg)
|
|
|
|
max = mid;
|
|
|
|
else if (val > arr[mid].end)
|
|
|
|
min = mid + 1;
|
|
|
|
else
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unix column width of a wide character (Unicode code point, really) */
|
|
|
|
int
|
|
|
|
utf_wcwidth(unsigned int wc)
|
|
|
|
{
|
|
|
|
/* except NUL, C0/C1 control characters and DEL yield -1 */
|
|
|
|
if (wc < 0x20 || (wc >= 0x7F && wc < 0xA0))
|
|
|
|
return (wc ? -1 : 0);
|
|
|
|
|
|
|
|
/* combining characters use 0 screen columns */
|
|
|
|
if (mb_ucsbsearch(mb_ucs_combining, NELEM(mb_ucs_combining), wc))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/* all others use 1 or 2 screen columns */
|
|
|
|
if (mb_ucsbsearch(mb_ucs_fullwidth, NELEM(mb_ucs_fullwidth), wc))
|
|
|
|
return (2);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
#endif
|