blob: 5d2c86963fa42e068f4030874e2f3665994c0c42 [file] [log] [blame]
Elliott Hughes50012062015-03-10 22:22:24 -07001/* $OpenBSD: expr.c,v 1.24 2014/12/08 14:26:31 otto Exp $ */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002
3/*-
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00004 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
Elliott Hughesdd4abe02018-02-05 15:55:19 -08005 * 2011, 2012, 2013, 2014, 2016, 2017, 2018
Elliott Hughesfc0307d2016-02-02 15:26:47 -08006 * mirabilos <m@mirbsd.org>
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07007 *
8 * Provided that these terms and disclaimer and all copyright notices
9 * are retained or reproduced in an accompanying document, permission
10 * is granted to deal in this work without restriction, including un-
11 * limited rights to use, publicly perform, distribute, sell, modify,
12 * merge, give away, or sublicence.
13 *
14 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
15 * the utmost extent permitted by applicable law, neither express nor
16 * implied; without malicious intent or gross negligence. In no event
17 * may a licensor, author or contributor be held liable for indirect,
18 * direct, other damage, loss, or other issues arising in any way out
19 * of dealing in the work, even if advised of the possibility of such
20 * damage or existence of a defect, except proven that it results out
21 * of said person's immediate fault when using the work as intended.
22 */
23
24#include "sh.h"
25
Elliott Hughes47086262019-03-26 12:34:31 -070026__RCSID("$MirOS: src/bin/mksh/expr.c,v 1.105 2018/08/10 02:53:33 tg Exp $");
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000027
Elliott Hughes77740fc2016-08-12 15:06:53 -070028#define EXPRTOK_DEFNS
29#include "exprtok.h"
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070030
31/* precisions; used to be enum prec but we do arithmetics on it */
Thorsten Glaser811a5752013-07-25 14:24:45 +000032#define P_PRIMARY 0 /* VAR, LIT, (), ! ~ ++ -- */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070033#define P_MULT 1 /* * / % */
34#define P_ADD 2 /* + - */
Elliott Hughes77740fc2016-08-12 15:06:53 -070035#define P_SHIFT 3 /* ^< ^> << >> */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070036#define P_RELATION 4 /* < <= > >= */
37#define P_EQUALITY 5 /* == != */
38#define P_BAND 6 /* & */
39#define P_BXOR 7 /* ^ */
40#define P_BOR 8 /* | */
41#define P_LAND 9 /* && */
42#define P_LOR 10 /* || */
43#define P_TERN 11 /* ?: */
Elliott Hughes77740fc2016-08-12 15:06:53 -070044 /* = += -= *= /= %= ^<= ^>= <<= >>= &= ^= |= */
Thorsten Glaser811a5752013-07-25 14:24:45 +000045#define P_ASSIGN 12
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070046#define P_COMMA 13 /* , */
47#define MAX_PREC P_COMMA
48
Elliott Hughes77740fc2016-08-12 15:06:53 -070049enum token {
50#define EXPRTOK_ENUM
51#include "exprtok.h"
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070052};
53
Elliott Hughes77740fc2016-08-12 15:06:53 -070054static const char opname[][4] = {
55#define EXPRTOK_NAME
56#include "exprtok.h"
57};
58
59static const uint8_t oplen[] = {
60#define EXPRTOK_LEN
61#include "exprtok.h"
62};
63
64static const uint8_t opprec[] = {
65#define EXPRTOK_PREC
66#include "exprtok.h"
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070067};
68
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000069typedef struct expr_state {
70 /* expression being evaluated */
71 const char *expression;
72 /* lexical position */
73 const char *tokp;
74 /* value from token() */
75 struct tbl *val;
76 /* variable that is being recursively expanded (EXPRINEVAL flag set) */
77 struct tbl *evaling;
78 /* token from token() */
79 enum token tok;
80 /* don't do assignments (for ?:, &&, ||) */
Thorsten Glaser811a5752013-07-25 14:24:45 +000081 uint8_t noassign;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000082 /* evaluating an $(()) expression? */
83 bool arith;
84 /* unsigned arithmetic calculation */
85 bool natural;
86} Expr_state;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070087
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070088enum error_type {
89 ET_UNEXPECTED, ET_BADLIT, ET_RECURSIVE,
90 ET_LVALUE, ET_RDONLY, ET_STR
91};
92
93static void evalerr(Expr_state *, enum error_type, const char *)
94 MKSH_A_NORETURN;
Thorsten Glaser811a5752013-07-25 14:24:45 +000095static struct tbl *evalexpr(Expr_state *, unsigned int);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070096static void exprtoken(Expr_state *);
97static struct tbl *do_ppmm(Expr_state *, enum token, struct tbl *, bool);
98static void assign_check(Expr_state *, enum token, struct tbl *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070099static struct tbl *intvar(Expr_state *, struct tbl *);
100
101/*
102 * parse and evaluate expression
103 */
104int
105evaluate(const char *expr, mksh_ari_t *rval, int error_ok, bool arith)
106{
107 struct tbl v;
108 int ret;
109
Thorsten Glaser811a5752013-07-25 14:24:45 +0000110 v.flag = DEFINED | INTEGER;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700111 v.type = 0;
112 ret = v_evaluate(&v, expr, error_ok, arith);
113 *rval = v.val.i;
114 return (ret);
115}
116
117/*
118 * parse and evaluate expression, storing result in vp.
119 */
120int
121v_evaluate(struct tbl *vp, const char *expr, volatile int error_ok,
122 bool arith)
123{
124 struct tbl *v;
125 Expr_state curstate;
126 Expr_state * const es = &curstate;
127 int i;
128
129 /* save state to allow recursive calls */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000130 memset(&curstate, 0, sizeof(curstate));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700131 curstate.expression = curstate.tokp = expr;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000132 curstate.tok = BAD;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700133 curstate.arith = arith;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700134
135 newenv(E_ERRH);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000136 if ((i = kshsetjmp(e->jbuf))) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700137 /* Clear EXPRINEVAL in of any variables we were playing with */
138 if (curstate.evaling)
139 curstate.evaling->flag &= ~EXPRINEVAL;
140 quitenv(NULL);
141 if (i == LAEXPR) {
142 if (error_ok == KSH_RETURN_ERROR)
143 return (0);
144 errorfz();
145 }
146 unwind(i);
147 /* NOTREACHED */
148 }
149
150 exprtoken(es);
151 if (es->tok == END) {
152 es->tok = LIT;
Elliott Hughes77740fc2016-08-12 15:06:53 -0700153 es->val = tempvar("");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700154 }
155 v = intvar(es, evalexpr(es, MAX_PREC));
156
157 if (es->tok != END)
158 evalerr(es, ET_UNEXPECTED, NULL);
159
160 if (es->arith && es->natural)
161 vp->flag |= INT_U;
162 if (vp->flag & INTEGER)
163 setint_v(vp, v, es->arith);
164 else
165 /* can fail if readonly */
166 setstr(vp, str_val(v), error_ok);
167
168 quitenv(NULL);
169
170 return (1);
171}
172
173static void
174evalerr(Expr_state *es, enum error_type type, const char *str)
175{
176 char tbuf[2];
177 const char *s;
178
179 es->arith = false;
180 switch (type) {
181 case ET_UNEXPECTED:
182 switch (es->tok) {
183 case VAR:
184 s = es->val->name;
185 break;
186 case LIT:
187 s = str_val(es->val);
188 break;
189 case END:
190 s = "end of expression";
191 break;
192 case BAD:
193 tbuf[0] = *es->tokp;
194 tbuf[1] = '\0';
195 s = tbuf;
196 break;
197 default:
Elliott Hughes77740fc2016-08-12 15:06:53 -0700198 s = opname[(int)es->tok];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700199 }
Elliott Hughes77740fc2016-08-12 15:06:53 -0700200 warningf(true, Tf_sD_s_qs, es->expression,
201 Tunexpected, s);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700202 break;
203
204 case ET_BADLIT:
Elliott Hughes77740fc2016-08-12 15:06:53 -0700205 warningf(true, Tf_sD_s_qs, es->expression,
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700206 Tbadnum, str);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700207 break;
208
209 case ET_RECURSIVE:
Elliott Hughes77740fc2016-08-12 15:06:53 -0700210 warningf(true, Tf_sD_s_qs, es->expression,
Geremy Condra03ebf062011-10-12 18:17:24 -0700211 "expression recurses on parameter", str);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700212 break;
213
214 case ET_LVALUE:
Elliott Hughes77740fc2016-08-12 15:06:53 -0700215 warningf(true, Tf_sD_s_s,
Geremy Condra03ebf062011-10-12 18:17:24 -0700216 es->expression, str, "requires lvalue");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700217 break;
218
219 case ET_RDONLY:
Elliott Hughes77740fc2016-08-12 15:06:53 -0700220 warningf(true, Tf_sD_s_s,
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000221 es->expression, str, "applied to read-only variable");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700222 break;
223
224 default: /* keep gcc happy */
225 case ET_STR:
Elliott Hughes77740fc2016-08-12 15:06:53 -0700226 warningf(true, Tf_sD_s, es->expression, str);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700227 break;
228 }
229 unwind(LAEXPR);
230}
231
Thorsten Glaser811a5752013-07-25 14:24:45 +0000232/* do a ++ or -- operation */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700233static struct tbl *
Thorsten Glaser811a5752013-07-25 14:24:45 +0000234do_ppmm(Expr_state *es, enum token op, struct tbl *vasn, bool is_prefix)
235{
236 struct tbl *vl;
237 mksh_uari_t oval;
238
239 assign_check(es, op, vasn);
240
241 vl = intvar(es, vasn);
242 oval = vl->val.u;
243 if (op == O_PLUSPLUS)
244 ++vl->val.u;
245 else
246 --vl->val.u;
247 if (!es->noassign) {
248 if (vasn->flag & INTEGER)
249 setint_v(vasn, vl, es->arith);
250 else
251 setint(vasn, vl->val.i);
252 }
253 if (!is_prefix)
254 /* undo the increment/decrement */
255 vl->val.u = oval;
256
257 return (vl);
258}
259
260static struct tbl *
261evalexpr(Expr_state *es, unsigned int prec)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700262{
263 struct tbl *vl, *vr = NULL, *vasn;
264 enum token op;
Thorsten Glaser811a5752013-07-25 14:24:45 +0000265 mksh_uari_t res = 0, t1, t2, t3;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700266
267 if (prec == P_PRIMARY) {
Thorsten Glaser811a5752013-07-25 14:24:45 +0000268 switch ((int)(op = es->tok)) {
269 case O_BNOT:
270 case O_LNOT:
271 case O_MINUS:
272 case O_PLUS:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700273 exprtoken(es);
274 vl = intvar(es, evalexpr(es, P_PRIMARY));
Thorsten Glaser811a5752013-07-25 14:24:45 +0000275 switch ((int)op) {
276 case O_BNOT:
277 vl->val.u = ~vl->val.u;
278 break;
279 case O_LNOT:
280 vl->val.u = !vl->val.u;
281 break;
282 case O_MINUS:
283 vl->val.u = -vl->val.u;
284 break;
285 case O_PLUS:
286 /* nop */
287 break;
288 }
289 break;
290
291 case OPEN_PAREN:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700292 exprtoken(es);
293 vl = evalexpr(es, MAX_PREC);
294 if (es->tok != CLOSE_PAREN)
295 evalerr(es, ET_STR, "missing )");
296 exprtoken(es);
Thorsten Glaser811a5752013-07-25 14:24:45 +0000297 break;
298
299 case O_PLUSPLUS:
300 case O_MINUSMINUS:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700301 exprtoken(es);
302 vl = do_ppmm(es, op, es->val, true);
303 exprtoken(es);
Thorsten Glaser811a5752013-07-25 14:24:45 +0000304 break;
305
306 case VAR:
307 case LIT:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700308 vl = es->val;
309 exprtoken(es);
Thorsten Glaser811a5752013-07-25 14:24:45 +0000310 break;
311
312 default:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700313 evalerr(es, ET_UNEXPECTED, NULL);
314 /* NOTREACHED */
315 }
Thorsten Glaser811a5752013-07-25 14:24:45 +0000316
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700317 if (es->tok == O_PLUSPLUS || es->tok == O_MINUSMINUS) {
318 vl = do_ppmm(es, es->tok, vl, false);
319 exprtoken(es);
320 }
Thorsten Glaser811a5752013-07-25 14:24:45 +0000321
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700322 return (vl);
Thorsten Glaser811a5752013-07-25 14:24:45 +0000323 /* prec == P_PRIMARY */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700324 }
Thorsten Glaser811a5752013-07-25 14:24:45 +0000325
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700326 vl = evalexpr(es, prec - 1);
Thorsten Glaser811a5752013-07-25 14:24:45 +0000327 while ((int)(op = es->tok) >= (int)O_EQ && (int)op <= (int)O_COMMA &&
Elliott Hughes77740fc2016-08-12 15:06:53 -0700328 opprec[(int)op] == prec) {
Elliott Hughes966dd552016-12-08 15:56:04 -0800329 switch ((int)op) {
330 case O_TERN:
331 case O_LAND:
332 case O_LOR:
333 break;
334 default:
335 exprtoken(es);
336 }
337
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700338 vasn = vl;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000339 if (op != O_ASN)
340 /* vl may not have a value yet */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700341 vl = intvar(es, vl);
342 if (IS_ASSIGNOP(op)) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000343 if (!es->noassign)
344 assign_check(es, op, vasn);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700345 vr = intvar(es, evalexpr(es, P_ASSIGN));
Thorsten Glaser811a5752013-07-25 14:24:45 +0000346 } else if (op == O_TERN) {
347 bool ev = vl->val.u != 0;
348
349 if (!ev)
350 es->noassign++;
Elliott Hughes966dd552016-12-08 15:56:04 -0800351 exprtoken(es);
Thorsten Glaser811a5752013-07-25 14:24:45 +0000352 vl = evalexpr(es, MAX_PREC);
353 if (!ev)
354 es->noassign--;
355 if (es->tok != CTERN)
356 evalerr(es, ET_STR, "missing :");
Thorsten Glaser811a5752013-07-25 14:24:45 +0000357 if (ev)
358 es->noassign++;
Elliott Hughes966dd552016-12-08 15:56:04 -0800359 exprtoken(es);
Thorsten Glaser811a5752013-07-25 14:24:45 +0000360 vr = evalexpr(es, P_TERN);
361 if (ev)
362 es->noassign--;
363 vl = ev ? vl : vr;
364 continue;
365 } else if (op != O_LAND && op != O_LOR)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700366 vr = intvar(es, evalexpr(es, prec - 1));
Thorsten Glaser811a5752013-07-25 14:24:45 +0000367
368 /* common ops setup */
369 switch ((int)op) {
370 case O_DIV:
371 case O_DIVASN:
372 case O_MOD:
373 case O_MODASN:
374 if (vr->val.u == 0) {
375 if (!es->noassign)
376 evalerr(es, ET_STR, "zero divisor");
377 vr->val.u = 1;
378 }
379 /* calculate the absolute values */
380 t1 = vl->val.i < 0 ? -vl->val.u : vl->val.u;
381 t2 = vr->val.i < 0 ? -vr->val.u : vr->val.u;
382 break;
383#ifndef MKSH_LEGACY_MODE
384 case O_LSHIFT:
385 case O_LSHIFTASN:
386 case O_RSHIFT:
387 case O_RSHIFTASN:
388 case O_ROL:
389 case O_ROLASN:
390 case O_ROR:
391 case O_RORASN:
392 t1 = vl->val.u;
393 t2 = vr->val.u & 31;
394 break;
395#endif
396 case O_LAND:
397 case O_LOR:
398 t1 = vl->val.u;
399 t2 = 0; /* gcc */
400 break;
401 default:
402 t1 = vl->val.u;
403 t2 = vr->val.u;
404 break;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700405 }
Thorsten Glaser811a5752013-07-25 14:24:45 +0000406
407#define cmpop(op) (es->natural ? \
408 (mksh_uari_t)(vl->val.u op vr->val.u) : \
409 (mksh_uari_t)(vl->val.i op vr->val.i) \
410)
411
412 /* op calculation */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700413 switch ((int)op) {
414 case O_TIMES:
415 case O_TIMESASN:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000416 res = t1 * t2;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700417 break;
418 case O_MOD:
419 case O_MODASN:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000420 if (es->natural) {
421 res = vl->val.u % vr->val.u;
422 break;
423 }
424 goto signed_division;
425 case O_DIV:
426 case O_DIVASN:
427 if (es->natural) {
428 res = vl->val.u / vr->val.u;
429 break;
430 }
431 signed_division:
432 /*
433 * a / b = abs(a) / abs(b) * sgn((u)a^(u)b)
434 */
435 t3 = t1 / t2;
436#ifndef MKSH_LEGACY_MODE
437 res = ((vl->val.u ^ vr->val.u) & 0x80000000) ? -t3 : t3;
438#else
439 res = ((t1 == vl->val.u ? 0 : 1) ^
440 (t2 == vr->val.u ? 0 : 1)) ? -t3 : t3;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000441#endif
Thorsten Glaser811a5752013-07-25 14:24:45 +0000442 if (op == O_MOD || op == O_MODASN) {
443 /*
444 * primitive modulo, to get the sign of
445 * the result correct:
446 * (a % b) = a - ((a / b) * b)
447 * the subtraction and multiplication
448 * are, amazingly enough, sign ignorant
449 */
450 res = vl->val.u - (res * vr->val.u);
451 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700452 break;
453 case O_PLUS:
454 case O_PLUSASN:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000455 res = t1 + t2;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700456 break;
457 case O_MINUS:
458 case O_MINUSASN:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000459 res = t1 - t2;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700460 break;
Thorsten Glaser811a5752013-07-25 14:24:45 +0000461#ifndef MKSH_LEGACY_MODE
462 case O_ROL:
463 case O_ROLASN:
464 res = (t1 << t2) | (t1 >> (32 - t2));
465 break;
466 case O_ROR:
467 case O_RORASN:
468 res = (t1 >> t2) | (t1 << (32 - t2));
469 break;
470#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700471 case O_LSHIFT:
472 case O_LSHIFTASN:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000473 res = t1 << t2;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700474 break;
475 case O_RSHIFT:
476 case O_RSHIFTASN:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000477 res = es->natural || vl->val.i >= 0 ?
478 t1 >> t2 :
479 ~(~t1 >> t2);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700480 break;
481 case O_LT:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000482 res = cmpop(<);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700483 break;
484 case O_LE:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000485 res = cmpop(<=);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700486 break;
487 case O_GT:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000488 res = cmpop(>);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700489 break;
490 case O_GE:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000491 res = cmpop(>=);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700492 break;
493 case O_EQ:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000494 res = t1 == t2;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700495 break;
496 case O_NE:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000497 res = t1 != t2;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700498 break;
499 case O_BAND:
500 case O_BANDASN:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000501 res = t1 & t2;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700502 break;
503 case O_BXOR:
504 case O_BXORASN:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000505 res = t1 ^ t2;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700506 break;
507 case O_BOR:
508 case O_BORASN:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000509 res = t1 | t2;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700510 break;
511 case O_LAND:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000512 if (!t1)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700513 es->noassign++;
Elliott Hughes966dd552016-12-08 15:56:04 -0800514 exprtoken(es);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700515 vr = intvar(es, evalexpr(es, prec - 1));
Thorsten Glaser811a5752013-07-25 14:24:45 +0000516 res = t1 && vr->val.u;
517 if (!t1)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700518 es->noassign--;
519 break;
520 case O_LOR:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000521 if (t1)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700522 es->noassign++;
Elliott Hughes966dd552016-12-08 15:56:04 -0800523 exprtoken(es);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700524 vr = intvar(es, evalexpr(es, prec - 1));
Thorsten Glaser811a5752013-07-25 14:24:45 +0000525 res = t1 || vr->val.u;
526 if (t1)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700527 es->noassign--;
528 break;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700529 case O_ASN:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700530 case O_COMMA:
Thorsten Glaser811a5752013-07-25 14:24:45 +0000531 res = t2;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700532 break;
533 }
Thorsten Glaser811a5752013-07-25 14:24:45 +0000534
535#undef cmpop
536
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700537 if (IS_ASSIGNOP(op)) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000538 vr->val.u = res;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700539 if (!es->noassign) {
540 if (vasn->flag & INTEGER)
541 setint_v(vasn, vr, es->arith);
542 else
Thorsten Glaser811a5752013-07-25 14:24:45 +0000543 setint(vasn, vr->val.i);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700544 }
545 vl = vr;
Thorsten Glaser811a5752013-07-25 14:24:45 +0000546 } else
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000547 vl->val.u = res;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700548 }
549 return (vl);
550}
551
552static void
553exprtoken(Expr_state *es)
554{
555 const char *cp = es->tokp;
556 int c;
557 char *tvar;
558
Thorsten Glaser811a5752013-07-25 14:24:45 +0000559 /* skip whitespace */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700560 skip_spaces:
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800561 --cp;
562 do {
563 c = ord(*++cp);
564 } while (ctype(c, C_SPACE));
565 if (es->tokp == es->expression && (unsigned int)c == ORD('#')) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700566 /* expression begins with # */
Thorsten Glaser811a5752013-07-25 14:24:45 +0000567 /* switch to unsigned */
568 es->natural = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700569 ++cp;
570 goto skip_spaces;
571 }
572 es->tokp = cp;
573
574 if (c == '\0')
575 es->tok = END;
Elliott Hughes23925bb2017-09-22 16:04:20 -0700576 else if (ctype(c, C_ALPHX)) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700577 do {
Elliott Hughes23925bb2017-09-22 16:04:20 -0700578 c = ord(*++cp);
579 } while (ctype(c, C_ALNUX));
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800580 if ((unsigned int)c == ORD('[')) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700581 size_t len;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700582
583 len = array_ref_len(cp);
584 if (len == 0)
585 evalerr(es, ET_STR, "missing ]");
586 cp += len;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700587 }
588 if (es->noassign) {
Elliott Hughes77740fc2016-08-12 15:06:53 -0700589 es->val = tempvar("");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700590 es->val->flag |= EXPRLVALUE;
591 } else {
592 strndupx(tvar, es->tokp, cp - es->tokp, ATEMP);
593 es->val = global(tvar);
594 afree(tvar, ATEMP);
595 }
596 es->tok = VAR;
597 } else if (c == '1' && cp[1] == '#') {
598 cp += 2;
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800599 if (*cp)
600 cp += utf_ptradj(cp);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700601 strndupx(tvar, es->tokp, cp - es->tokp, ATEMP);
602 goto process_tvar;
603#ifndef MKSH_SMALL
604 } else if (c == '\'') {
Elliott Hughes77740fc2016-08-12 15:06:53 -0700605 if (*++cp == '\0') {
606 es->tok = END;
607 evalerr(es, ET_UNEXPECTED, NULL);
608 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700609 cp += utf_ptradj(cp);
610 if (*cp++ != '\'')
611 evalerr(es, ET_STR,
612 "multi-character character constant");
613 /* 'x' -> 1#x (x = one multibyte character) */
614 c = cp - es->tokp;
615 tvar = alloc(c + /* NUL */ 1, ATEMP);
616 tvar[0] = '1';
617 tvar[1] = '#';
618 memcpy(tvar + 2, es->tokp + 1, c - 2);
619 tvar[c] = '\0';
620 goto process_tvar;
621#endif
Elliott Hughes23925bb2017-09-22 16:04:20 -0700622 } else if (ctype(c, C_DIGIT)) {
623 while (ctype(c, C_ALNUM | C_HASH))
624 c = ord(*cp++);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700625 strndupx(tvar, es->tokp, --cp - es->tokp, ATEMP);
626 process_tvar:
Elliott Hughes77740fc2016-08-12 15:06:53 -0700627 es->val = tempvar("");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700628 es->val->flag &= ~INTEGER;
629 es->val->type = 0;
630 es->val->val.s = tvar;
631 if (setint_v(es->val, es->val, es->arith) == NULL)
632 evalerr(es, ET_BADLIT, tvar);
633 afree(tvar, ATEMP);
634 es->tok = LIT;
635 } else {
636 int i, n0;
637
Elliott Hughes23925bb2017-09-22 16:04:20 -0700638 for (i = 0; (n0 = ord(opname[i][0])); i++)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700639 if (c == n0 && strncmp(cp, opname[i],
640 (size_t)oplen[i]) == 0) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700641 es->tok = (enum token)i;
Elliott Hughes77740fc2016-08-12 15:06:53 -0700642 cp += oplen[i];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700643 break;
644 }
645 if (!n0)
646 es->tok = BAD;
647 }
648 es->tokp = cp;
649}
650
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700651static void
652assign_check(Expr_state *es, enum token op, struct tbl *vasn)
653{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000654 if (es->tok == END || !vasn ||
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700655 (vasn->name[0] == '\0' && !(vasn->flag & EXPRLVALUE)))
Elliott Hughes77740fc2016-08-12 15:06:53 -0700656 evalerr(es, ET_LVALUE, opname[(int)op]);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700657 else if (vasn->flag & RDONLY)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700658 evalerr(es, ET_RDONLY, opname[(int)op]);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700659}
660
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000661struct tbl *
Elliott Hughes77740fc2016-08-12 15:06:53 -0700662tempvar(const char *vname)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700663{
664 struct tbl *vp;
Elliott Hughes77740fc2016-08-12 15:06:53 -0700665 size_t vsize;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700666
Elliott Hughes77740fc2016-08-12 15:06:53 -0700667 vsize = strlen(vname) + 1;
668 vp = alloc(offsetof(struct tbl, name[0]) + vsize, ATEMP);
669 memcpy(vp->name, vname, vsize);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700670 vp->flag = ISSET|INTEGER;
671 vp->type = 0;
672 vp->areap = ATEMP;
673 vp->ua.hval = 0;
674 vp->val.i = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700675 return (vp);
676}
677
678/* cast (string) variable to temporary integer variable */
679static struct tbl *
680intvar(Expr_state *es, struct tbl *vp)
681{
682 struct tbl *vq;
683
684 /* try to avoid replacing a temp var with another temp var */
685 if (vp->name[0] == '\0' &&
686 (vp->flag & (ISSET|INTEGER|EXPRLVALUE)) == (ISSET|INTEGER))
687 return (vp);
688
Elliott Hughes77740fc2016-08-12 15:06:53 -0700689 vq = tempvar("");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700690 if (setint_v(vq, vp, es->arith) == NULL) {
691 if (vp->flag & EXPRINEVAL)
692 evalerr(es, ET_RECURSIVE, vp->name);
693 es->evaling = vp;
694 vp->flag |= EXPRINEVAL;
695 v_evaluate(vq, str_val(vp), KSH_UNWIND_ERROR, es->arith);
696 vp->flag &= ~EXPRINEVAL;
697 es->evaling = NULL;
698 }
699 return (vq);
700}
701
702
703/*
704 * UTF-8 support code: high-level functions
705 */
706
707int
708utf_widthadj(const char *src, const char **dst)
709{
710 size_t len;
711 unsigned int wc;
712 int width;
713
714 if (!UTFMODE || (len = utf_mbtowc(&wc, src)) == (size_t)-1 ||
715 wc == 0)
716 len = width = 1;
717 else if ((width = utf_wcwidth(wc)) < 0)
718 /* XXX use 2 for x_zotc3 here? */
719 width = 1;
720
721 if (dst)
722 *dst = src + len;
723 return (width);
724}
725
Geremy Condra03ebf062011-10-12 18:17:24 -0700726size_t
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700727utf_mbswidth(const char *s)
728{
Geremy Condra03ebf062011-10-12 18:17:24 -0700729 size_t len, width = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700730 unsigned int wc;
Geremy Condra03ebf062011-10-12 18:17:24 -0700731 int cw;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700732
733 if (!UTFMODE)
734 return (strlen(s));
735
736 while (*s)
737 if (((len = utf_mbtowc(&wc, s)) == (size_t)-1) ||
738 ((cw = utf_wcwidth(wc)) == -1)) {
739 s++;
740 width += 1;
741 } else {
742 s += len;
743 width += cw;
744 }
745 return (width);
746}
747
748const char *
Elliott Hughes77740fc2016-08-12 15:06:53 -0700749utf_skipcols(const char *p, int cols, int *colp)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700750{
751 int c = 0;
Elliott Hughes77740fc2016-08-12 15:06:53 -0700752 const char *q;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700753
754 while (c < cols) {
Elliott Hughes77740fc2016-08-12 15:06:53 -0700755 if (!*p) {
756 /* end of input; special handling for edit.c */
757 if (!colp)
758 return (p + cols - c);
759 *colp = c;
760 return (p);
761 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700762 c += utf_widthadj(p, &p);
763 }
Elliott Hughes77740fc2016-08-12 15:06:53 -0700764 if (UTFMODE)
765 while (utf_widthadj(p, &q) == 0)
766 p = q;
767 if (colp)
768 *colp = c;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700769 return (p);
770}
771
772size_t
773utf_ptradj(const char *src)
774{
775 register size_t n;
776
Elliott Hughes23925bb2017-09-22 16:04:20 -0700777 if (!UTFMODE || rtt2asc(*src) < 0xC2 ||
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700778 (n = utf_mbtowc(NULL, src)) == (size_t)-1)
779 n = 1;
780 return (n);
781}
782
783/*
784 * UTF-8 support code: low-level functions
785 */
786
787/* CESU-8 multibyte and wide character conversion crafted for mksh */
788
789size_t
790utf_mbtowc(unsigned int *dst, const char *src)
791{
792 const unsigned char *s = (const unsigned char *)src;
793 unsigned int c, wc;
794
Elliott Hughes23925bb2017-09-22 16:04:20 -0700795 if ((wc = ord(rtt2asc(*s++))) < 0x80) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700796 out:
797 if (dst != NULL)
798 *dst = wc;
799 return (wc ? ((const char *)s - src) : 0);
800 }
801 if (wc < 0xC2 || wc >= 0xF0)
802 /* < 0xC0: spurious second byte */
803 /* < 0xC2: non-minimalistic mapping error in 2-byte seqs */
804 /* > 0xEF: beyond BMP */
805 goto ilseq;
806
807 if (wc < 0xE0) {
808 wc = (wc & 0x1F) << 6;
Elliott Hughes23925bb2017-09-22 16:04:20 -0700809 if (((c = ord(rtt2asc(*s++))) & 0xC0) != 0x80)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700810 goto ilseq;
811 wc |= c & 0x3F;
812 goto out;
813 }
814
815 wc = (wc & 0x0F) << 12;
816
Elliott Hughes23925bb2017-09-22 16:04:20 -0700817 if (((c = ord(rtt2asc(*s++))) & 0xC0) != 0x80)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700818 goto ilseq;
819 wc |= (c & 0x3F) << 6;
820
Elliott Hughes23925bb2017-09-22 16:04:20 -0700821 if (((c = ord(rtt2asc(*s++))) & 0xC0) != 0x80)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700822 goto ilseq;
823 wc |= c & 0x3F;
824
825 /* Check for non-minimalistic mapping error in 3-byte seqs */
826 if (wc >= 0x0800 && wc <= 0xFFFD)
827 goto out;
828 ilseq:
829 return ((size_t)(-1));
830}
831
832size_t
833utf_wctomb(char *dst, unsigned int wc)
834{
835 unsigned char *d;
836
837 if (wc < 0x80) {
Elliott Hughes23925bb2017-09-22 16:04:20 -0700838 *dst = asc2rtt(wc);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700839 return (1);
840 }
841
842 d = (unsigned char *)dst;
843 if (wc < 0x0800)
Elliott Hughes23925bb2017-09-22 16:04:20 -0700844 *d++ = asc2rtt((wc >> 6) | 0xC0);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700845 else {
Elliott Hughes23925bb2017-09-22 16:04:20 -0700846 *d++ = asc2rtt(((wc = wc > 0xFFFD ? 0xFFFD : wc) >> 12) | 0xE0);
847 *d++ = asc2rtt(((wc >> 6) & 0x3F) | 0x80);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700848 }
Elliott Hughes23925bb2017-09-22 16:04:20 -0700849 *d++ = asc2rtt((wc & 0x3F) | 0x80);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700850 return ((char *)d - dst);
851}
852
Geremy Condra03ebf062011-10-12 18:17:24 -0700853/*
854 * Wrapper around access(2) because it says root can execute everything
855 * on some operating systems. Does not set errno, no user needs it. Use
856 * this iff mode can have the X_OK bit set, access otherwise.
857 */
858int
859ksh_access(const char *fn, int mode)
860{
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700861#ifdef __OS2__
862 return (access_ex(access, fn, mode));
863#else
Geremy Condra03ebf062011-10-12 18:17:24 -0700864 int rv;
865 struct stat sb;
866
867 if ((rv = access(fn, mode)) == 0 && kshuid == 0 && (mode & X_OK) &&
868 (rv = stat(fn, &sb)) == 0 && !S_ISDIR(sb.st_mode) &&
869 (sb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
870 rv = -1;
871
872 return (rv);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700873#endif
Geremy Condra03ebf062011-10-12 18:17:24 -0700874}
Thorsten Glaser811a5752013-07-25 14:24:45 +0000875
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800876#ifndef MIRBSD_BOOTFLOPPY
Elliott Hughes23925bb2017-09-22 16:04:20 -0700877/* From: X11/xc/programs/xterm/wcwidth.c,v 1.10 */
Thorsten Glaser811a5752013-07-25 14:24:45 +0000878
879struct mb_ucsrange {
880 unsigned short beg;
881 unsigned short end;
882};
883
884static int mb_ucsbsearch(const struct mb_ucsrange arr[], size_t elems,
Elliott Hughes737fdce2014-08-07 12:59:26 -0700885 unsigned int val) MKSH_A_PURE;
Thorsten Glaser811a5752013-07-25 14:24:45 +0000886
887/*
Elliott Hughes47086262019-03-26 12:34:31 -0700888 * Generated from the UCD 11.0.0 by
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800889 * MirOS: contrib/code/Snippets/eawparse,v 1.12 2017/09/06 16:05:45 tg Exp $
Thorsten Glaser811a5752013-07-25 14:24:45 +0000890 */
891
892static const struct mb_ucsrange mb_ucs_combining[] = {
893 { 0x0300, 0x036F },
894 { 0x0483, 0x0489 },
895 { 0x0591, 0x05BD },
896 { 0x05BF, 0x05BF },
897 { 0x05C1, 0x05C2 },
898 { 0x05C4, 0x05C5 },
899 { 0x05C7, 0x05C7 },
Thorsten Glaser811a5752013-07-25 14:24:45 +0000900 { 0x0610, 0x061A },
Elliott Hughes737fdce2014-08-07 12:59:26 -0700901 { 0x061C, 0x061C },
Thorsten Glaser811a5752013-07-25 14:24:45 +0000902 { 0x064B, 0x065F },
903 { 0x0670, 0x0670 },
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800904 { 0x06D6, 0x06DC },
Thorsten Glaser811a5752013-07-25 14:24:45 +0000905 { 0x06DF, 0x06E4 },
906 { 0x06E7, 0x06E8 },
907 { 0x06EA, 0x06ED },
Thorsten Glaser811a5752013-07-25 14:24:45 +0000908 { 0x0711, 0x0711 },
909 { 0x0730, 0x074A },
910 { 0x07A6, 0x07B0 },
911 { 0x07EB, 0x07F3 },
Elliott Hughes47086262019-03-26 12:34:31 -0700912 { 0x07FD, 0x07FD },
Thorsten Glaser811a5752013-07-25 14:24:45 +0000913 { 0x0816, 0x0819 },
914 { 0x081B, 0x0823 },
915 { 0x0825, 0x0827 },
916 { 0x0829, 0x082D },
917 { 0x0859, 0x085B },
Elliott Hughes47086262019-03-26 12:34:31 -0700918 { 0x08D3, 0x08E1 },
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800919 { 0x08E3, 0x0902 },
Thorsten Glaser811a5752013-07-25 14:24:45 +0000920 { 0x093A, 0x093A },
921 { 0x093C, 0x093C },
922 { 0x0941, 0x0948 },
923 { 0x094D, 0x094D },
924 { 0x0951, 0x0957 },
925 { 0x0962, 0x0963 },
926 { 0x0981, 0x0981 },
927 { 0x09BC, 0x09BC },
928 { 0x09C1, 0x09C4 },
929 { 0x09CD, 0x09CD },
930 { 0x09E2, 0x09E3 },
Elliott Hughes47086262019-03-26 12:34:31 -0700931 { 0x09FE, 0x09FE },
Thorsten Glaser811a5752013-07-25 14:24:45 +0000932 { 0x0A01, 0x0A02 },
933 { 0x0A3C, 0x0A3C },
934 { 0x0A41, 0x0A42 },
935 { 0x0A47, 0x0A48 },
936 { 0x0A4B, 0x0A4D },
937 { 0x0A51, 0x0A51 },
938 { 0x0A70, 0x0A71 },
939 { 0x0A75, 0x0A75 },
940 { 0x0A81, 0x0A82 },
941 { 0x0ABC, 0x0ABC },
942 { 0x0AC1, 0x0AC5 },
943 { 0x0AC7, 0x0AC8 },
944 { 0x0ACD, 0x0ACD },
945 { 0x0AE2, 0x0AE3 },
Elliott Hughes23925bb2017-09-22 16:04:20 -0700946 { 0x0AFA, 0x0AFF },
Thorsten Glaser811a5752013-07-25 14:24:45 +0000947 { 0x0B01, 0x0B01 },
948 { 0x0B3C, 0x0B3C },
949 { 0x0B3F, 0x0B3F },
950 { 0x0B41, 0x0B44 },
951 { 0x0B4D, 0x0B4D },
952 { 0x0B56, 0x0B56 },
953 { 0x0B62, 0x0B63 },
954 { 0x0B82, 0x0B82 },
955 { 0x0BC0, 0x0BC0 },
956 { 0x0BCD, 0x0BCD },
Elliott Hughes737fdce2014-08-07 12:59:26 -0700957 { 0x0C00, 0x0C00 },
Elliott Hughes47086262019-03-26 12:34:31 -0700958 { 0x0C04, 0x0C04 },
Thorsten Glaser811a5752013-07-25 14:24:45 +0000959 { 0x0C3E, 0x0C40 },
960 { 0x0C46, 0x0C48 },
961 { 0x0C4A, 0x0C4D },
962 { 0x0C55, 0x0C56 },
963 { 0x0C62, 0x0C63 },
Elliott Hughes737fdce2014-08-07 12:59:26 -0700964 { 0x0C81, 0x0C81 },
Thorsten Glaser811a5752013-07-25 14:24:45 +0000965 { 0x0CBC, 0x0CBC },
966 { 0x0CBF, 0x0CBF },
967 { 0x0CC6, 0x0CC6 },
968 { 0x0CCC, 0x0CCD },
969 { 0x0CE2, 0x0CE3 },
Elliott Hughes23925bb2017-09-22 16:04:20 -0700970 { 0x0D00, 0x0D01 },
971 { 0x0D3B, 0x0D3C },
Thorsten Glaser811a5752013-07-25 14:24:45 +0000972 { 0x0D41, 0x0D44 },
973 { 0x0D4D, 0x0D4D },
974 { 0x0D62, 0x0D63 },
975 { 0x0DCA, 0x0DCA },
976 { 0x0DD2, 0x0DD4 },
977 { 0x0DD6, 0x0DD6 },
978 { 0x0E31, 0x0E31 },
979 { 0x0E34, 0x0E3A },
980 { 0x0E47, 0x0E4E },
981 { 0x0EB1, 0x0EB1 },
982 { 0x0EB4, 0x0EB9 },
983 { 0x0EBB, 0x0EBC },
984 { 0x0EC8, 0x0ECD },
985 { 0x0F18, 0x0F19 },
986 { 0x0F35, 0x0F35 },
987 { 0x0F37, 0x0F37 },
988 { 0x0F39, 0x0F39 },
989 { 0x0F71, 0x0F7E },
990 { 0x0F80, 0x0F84 },
991 { 0x0F86, 0x0F87 },
992 { 0x0F8D, 0x0F97 },
993 { 0x0F99, 0x0FBC },
994 { 0x0FC6, 0x0FC6 },
995 { 0x102D, 0x1030 },
996 { 0x1032, 0x1037 },
997 { 0x1039, 0x103A },
998 { 0x103D, 0x103E },
999 { 0x1058, 0x1059 },
1000 { 0x105E, 0x1060 },
1001 { 0x1071, 0x1074 },
1002 { 0x1082, 0x1082 },
1003 { 0x1085, 0x1086 },
1004 { 0x108D, 0x108D },
1005 { 0x109D, 0x109D },
1006 { 0x1160, 0x11FF },
1007 { 0x135D, 0x135F },
1008 { 0x1712, 0x1714 },
1009 { 0x1732, 0x1734 },
1010 { 0x1752, 0x1753 },
1011 { 0x1772, 0x1773 },
1012 { 0x17B4, 0x17B5 },
1013 { 0x17B7, 0x17BD },
1014 { 0x17C6, 0x17C6 },
1015 { 0x17C9, 0x17D3 },
1016 { 0x17DD, 0x17DD },
Elliott Hughes737fdce2014-08-07 12:59:26 -07001017 { 0x180B, 0x180E },
Elliott Hughes966dd552016-12-08 15:56:04 -08001018 { 0x1885, 0x1886 },
Thorsten Glaser811a5752013-07-25 14:24:45 +00001019 { 0x18A9, 0x18A9 },
1020 { 0x1920, 0x1922 },
1021 { 0x1927, 0x1928 },
1022 { 0x1932, 0x1932 },
1023 { 0x1939, 0x193B },
1024 { 0x1A17, 0x1A18 },
Elliott Hughes737fdce2014-08-07 12:59:26 -07001025 { 0x1A1B, 0x1A1B },
Thorsten Glaser811a5752013-07-25 14:24:45 +00001026 { 0x1A56, 0x1A56 },
1027 { 0x1A58, 0x1A5E },
1028 { 0x1A60, 0x1A60 },
1029 { 0x1A62, 0x1A62 },
1030 { 0x1A65, 0x1A6C },
1031 { 0x1A73, 0x1A7C },
1032 { 0x1A7F, 0x1A7F },
Elliott Hughes737fdce2014-08-07 12:59:26 -07001033 { 0x1AB0, 0x1ABE },
Thorsten Glaser811a5752013-07-25 14:24:45 +00001034 { 0x1B00, 0x1B03 },
1035 { 0x1B34, 0x1B34 },
1036 { 0x1B36, 0x1B3A },
1037 { 0x1B3C, 0x1B3C },
1038 { 0x1B42, 0x1B42 },
1039 { 0x1B6B, 0x1B73 },
1040 { 0x1B80, 0x1B81 },
1041 { 0x1BA2, 0x1BA5 },
1042 { 0x1BA8, 0x1BA9 },
Elliott Hughes737fdce2014-08-07 12:59:26 -07001043 { 0x1BAB, 0x1BAD },
Thorsten Glaser811a5752013-07-25 14:24:45 +00001044 { 0x1BE6, 0x1BE6 },
1045 { 0x1BE8, 0x1BE9 },
1046 { 0x1BED, 0x1BED },
1047 { 0x1BEF, 0x1BF1 },
1048 { 0x1C2C, 0x1C33 },
1049 { 0x1C36, 0x1C37 },
1050 { 0x1CD0, 0x1CD2 },
1051 { 0x1CD4, 0x1CE0 },
1052 { 0x1CE2, 0x1CE8 },
1053 { 0x1CED, 0x1CED },
1054 { 0x1CF4, 0x1CF4 },
Elliott Hughes737fdce2014-08-07 12:59:26 -07001055 { 0x1CF8, 0x1CF9 },
Elliott Hughes23925bb2017-09-22 16:04:20 -07001056 { 0x1DC0, 0x1DF9 },
Elliott Hughes966dd552016-12-08 15:56:04 -08001057 { 0x1DFB, 0x1DFF },
Thorsten Glaser811a5752013-07-25 14:24:45 +00001058 { 0x200B, 0x200F },
1059 { 0x202A, 0x202E },
1060 { 0x2060, 0x2064 },
Elliott Hughes737fdce2014-08-07 12:59:26 -07001061 { 0x2066, 0x206F },
Thorsten Glaser811a5752013-07-25 14:24:45 +00001062 { 0x20D0, 0x20F0 },
1063 { 0x2CEF, 0x2CF1 },
1064 { 0x2D7F, 0x2D7F },
1065 { 0x2DE0, 0x2DFF },
1066 { 0x302A, 0x302D },
1067 { 0x3099, 0x309A },
1068 { 0xA66F, 0xA672 },
1069 { 0xA674, 0xA67D },
Elliott Hughes966dd552016-12-08 15:56:04 -08001070 { 0xA69E, 0xA69F },
Thorsten Glaser811a5752013-07-25 14:24:45 +00001071 { 0xA6F0, 0xA6F1 },
1072 { 0xA802, 0xA802 },
1073 { 0xA806, 0xA806 },
1074 { 0xA80B, 0xA80B },
1075 { 0xA825, 0xA826 },
Elliott Hughes966dd552016-12-08 15:56:04 -08001076 { 0xA8C4, 0xA8C5 },
Thorsten Glaser811a5752013-07-25 14:24:45 +00001077 { 0xA8E0, 0xA8F1 },
Elliott Hughes47086262019-03-26 12:34:31 -07001078 { 0xA8FF, 0xA8FF },
Thorsten Glaser811a5752013-07-25 14:24:45 +00001079 { 0xA926, 0xA92D },
1080 { 0xA947, 0xA951 },
1081 { 0xA980, 0xA982 },
1082 { 0xA9B3, 0xA9B3 },
1083 { 0xA9B6, 0xA9B9 },
1084 { 0xA9BC, 0xA9BC },
Elliott Hughes737fdce2014-08-07 12:59:26 -07001085 { 0xA9E5, 0xA9E5 },
Thorsten Glaser811a5752013-07-25 14:24:45 +00001086 { 0xAA29, 0xAA2E },
1087 { 0xAA31, 0xAA32 },
1088 { 0xAA35, 0xAA36 },
1089 { 0xAA43, 0xAA43 },
1090 { 0xAA4C, 0xAA4C },
Elliott Hughes737fdce2014-08-07 12:59:26 -07001091 { 0xAA7C, 0xAA7C },
Thorsten Glaser811a5752013-07-25 14:24:45 +00001092 { 0xAAB0, 0xAAB0 },
1093 { 0xAAB2, 0xAAB4 },
1094 { 0xAAB7, 0xAAB8 },
1095 { 0xAABE, 0xAABF },
1096 { 0xAAC1, 0xAAC1 },
1097 { 0xAAEC, 0xAAED },
1098 { 0xAAF6, 0xAAF6 },
1099 { 0xABE5, 0xABE5 },
1100 { 0xABE8, 0xABE8 },
1101 { 0xABED, 0xABED },
1102 { 0xFB1E, 0xFB1E },
1103 { 0xFE00, 0xFE0F },
Elliott Hughes966dd552016-12-08 15:56:04 -08001104 { 0xFE20, 0xFE2F },
Thorsten Glaser811a5752013-07-25 14:24:45 +00001105 { 0xFEFF, 0xFEFF },
1106 { 0xFFF9, 0xFFFB }
1107};
1108
1109static const struct mb_ucsrange mb_ucs_fullwidth[] = {
1110 { 0x1100, 0x115F },
Elliott Hughes966dd552016-12-08 15:56:04 -08001111 { 0x231A, 0x231B },
Thorsten Glaser811a5752013-07-25 14:24:45 +00001112 { 0x2329, 0x232A },
Elliott Hughes966dd552016-12-08 15:56:04 -08001113 { 0x23E9, 0x23EC },
1114 { 0x23F0, 0x23F0 },
1115 { 0x23F3, 0x23F3 },
1116 { 0x25FD, 0x25FE },
1117 { 0x2614, 0x2615 },
1118 { 0x2648, 0x2653 },
1119 { 0x267F, 0x267F },
1120 { 0x2693, 0x2693 },
1121 { 0x26A1, 0x26A1 },
1122 { 0x26AA, 0x26AB },
1123 { 0x26BD, 0x26BE },
1124 { 0x26C4, 0x26C5 },
1125 { 0x26CE, 0x26CE },
1126 { 0x26D4, 0x26D4 },
1127 { 0x26EA, 0x26EA },
1128 { 0x26F2, 0x26F3 },
1129 { 0x26F5, 0x26F5 },
1130 { 0x26FA, 0x26FA },
1131 { 0x26FD, 0x26FD },
1132 { 0x2705, 0x2705 },
1133 { 0x270A, 0x270B },
1134 { 0x2728, 0x2728 },
1135 { 0x274C, 0x274C },
1136 { 0x274E, 0x274E },
1137 { 0x2753, 0x2755 },
1138 { 0x2757, 0x2757 },
1139 { 0x2795, 0x2797 },
1140 { 0x27B0, 0x27B0 },
1141 { 0x27BF, 0x27BF },
1142 { 0x2B1B, 0x2B1C },
1143 { 0x2B50, 0x2B50 },
1144 { 0x2B55, 0x2B55 },
Elliott Hughes23925bb2017-09-22 16:04:20 -07001145 { 0x2E80, 0x3029 },
1146 { 0x302E, 0x303E },
1147 { 0x3040, 0x3098 },
1148 { 0x309B, 0xA4CF },
Thorsten Glaser811a5752013-07-25 14:24:45 +00001149 { 0xA960, 0xA97F },
1150 { 0xAC00, 0xD7A3 },
1151 { 0xF900, 0xFAFF },
1152 { 0xFE10, 0xFE19 },
1153 { 0xFE30, 0xFE6F },
Elliott Hughes23925bb2017-09-22 16:04:20 -07001154 { 0xFF01, 0xFF60 },
Thorsten Glaser811a5752013-07-25 14:24:45 +00001155 { 0xFFE0, 0xFFE6 }
1156};
1157
1158/* simple binary search in ranges, with bounds optimisation */
1159static int
1160mb_ucsbsearch(const struct mb_ucsrange arr[], size_t elems, unsigned int val)
1161{
1162 size_t min = 0, mid, max = elems;
1163
1164 if (val < arr[min].beg || val > arr[max - 1].end)
1165 return (0);
1166
1167 while (min < max) {
1168 mid = (min + max) / 2;
1169
1170 if (val < arr[mid].beg)
1171 max = mid;
1172 else if (val > arr[mid].end)
1173 min = mid + 1;
1174 else
1175 return (1);
1176 }
1177 return (0);
1178}
1179
Elliott Hughes47086262019-03-26 12:34:31 -07001180/* Unix column width of a wide character (UCS code point, really) */
Thorsten Glaser811a5752013-07-25 14:24:45 +00001181int
1182utf_wcwidth(unsigned int wc)
1183{
1184 /* except NUL, C0/C1 control characters and DEL yield -1 */
1185 if (wc < 0x20 || (wc >= 0x7F && wc < 0xA0))
1186 return (wc ? -1 : 0);
1187
1188 /* combining characters use 0 screen columns */
1189 if (mb_ucsbsearch(mb_ucs_combining, NELEM(mb_ucs_combining), wc))
1190 return (0);
1191
1192 /* all others use 1 or 2 screen columns */
1193 if (mb_ucsbsearch(mb_ucs_fullwidth, NELEM(mb_ucs_fullwidth), wc))
1194 return (2);
1195 return (1);
1196}
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001197#endif