1 /* $OpenBSD: sh.h,v 1.31 2012/09/10 01:25:30 tedu Exp $ */ 2 /* $OpenBSD: shf.h,v 1.6 2005/12/11 18:53:51 deraadt Exp $ */ 3 /* $OpenBSD: table.h,v 1.8 2012/02/19 07:52:30 otto Exp $ */ 4 /* $OpenBSD: tree.h,v 1.10 2005/03/28 21:28:22 deraadt Exp $ */ 5 /* $OpenBSD: expand.h,v 1.6 2005/03/30 17:16:37 deraadt Exp $ */ 6 /* $OpenBSD: lex.h,v 1.13 2013/03/03 19:11:34 guenther Exp $ */ 7 /* $OpenBSD: proto.h,v 1.34 2012/06/27 07:17:19 otto Exp $ */ 8 /* $OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $ */ 9 /* $OpenBSD: tty.h,v 1.5 2004/12/20 11:34:26 otto Exp $ */ 10 11 /*- 12 * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 13 * 2011, 2012, 2013 14 * Thorsten Glaser <tg (at) mirbsd.org> 15 * 16 * Provided that these terms and disclaimer and all copyright notices 17 * are retained or reproduced in an accompanying document, permission 18 * is granted to deal in this work without restriction, including un 19 * limited rights to use, publicly perform, distribute, sell, modify, 20 * merge, give away, or sublicence. 21 * 22 * This work is provided AS IS and WITHOUT WARRANTY of any kind, to 23 * the utmost extent permitted by applicable law, neither express nor 24 * implied; without malicious intent or gross negligence. In no event 25 * may a licensor, author or contributor be held liable for indirect, 26 * direct, other damage, loss, or other issues arising in any way out 27 * of dealing in the work, even if advised of the possibility of such 28 * damage or existence of a defect, except proven that it results out 29 * of said persons immediate fault when using the work as intended. 30 */ 31 32 #ifdef __dietlibc__ 33 /* XXX imake style */ 34 #define _BSD_SOURCE /* live, BSD, live */ 35 #endif 36 37 #if HAVE_SYS_PARAM_H 38 #include <sys/param.h> 39 #endif 40 #include <sys/types.h> 41 #if HAVE_BOTH_TIME_H 42 #include <sys/time.h> 43 #include <time.h> 44 #elif HAVE_SYS_TIME_H 45 #include <sys/time.h> 46 #elif HAVE_TIME_H 47 #include <time.h> 48 #endif 49 #include <sys/ioctl.h> 50 #if HAVE_SYS_SYSMACROS_H 51 #include <sys/sysmacros.h> 52 #endif 53 #if HAVE_SYS_MKDEV_H 54 #include <sys/mkdev.h> 55 #endif 56 #if HAVE_SYS_MMAN_H 57 #include <sys/mman.h> 58 #endif 59 #if HAVE_SYS_RESOURCE_H 60 #include <sys/resource.h> 61 #endif 62 #include <sys/stat.h> 63 #include <sys/wait.h> 64 #include <dirent.h> 65 #include <errno.h> 66 #include <fcntl.h> 67 #if HAVE_LIBGEN_H 68 #include <libgen.h> 69 #endif 70 #if HAVE_LIBUTIL_H 71 #include <libutil.h> 72 #endif 73 #include <limits.h> 74 #if HAVE_PATHS_H 75 #include <paths.h> 76 #endif 77 #include <pwd.h> 78 #include <setjmp.h> 79 #include <signal.h> 80 #include <stdarg.h> 81 #include <stddef.h> 82 #if HAVE_STDINT_H 83 #include <stdint.h> 84 #endif 85 #include <stdio.h> 86 #include <stdlib.h> 87 #include <string.h> 88 #if HAVE_STRINGS_H 89 #include <strings.h> 90 #endif 91 #if HAVE_TERMIOS_H 92 #include <termios.h> 93 #else 94 /* shudder */ 95 #include <termio.h> 96 #endif 97 #ifdef _ISC_UNIX 98 /* XXX imake style */ 99 #include <sys/sioctl.h> 100 #endif 101 #if HAVE_ULIMIT_H 102 #include <ulimit.h> 103 #endif 104 #include <unistd.h> 105 #if HAVE_VALUES_H 106 #include <values.h> 107 #endif 108 109 #undef __attribute__ 110 #if HAVE_ATTRIBUTE_BOUNDED 111 #define MKSH_A_BOUNDED(x,y,z) __attribute__((__bounded__ (x, y, z))) 112 #else 113 #define MKSH_A_BOUNDED(x,y,z) /* nothing */ 114 #endif 115 #if HAVE_ATTRIBUTE_FORMAT 116 #define MKSH_A_FORMAT(x,y,z) __attribute__((__format__ (x, y, z))) 117 #else 118 #define MKSH_A_FORMAT(x,y,z) /* nothing */ 119 #endif 120 #if HAVE_ATTRIBUTE_NORETURN 121 #define MKSH_A_NORETURN __attribute__((__noreturn__)) 122 #else 123 #define MKSH_A_NORETURN /* nothing */ 124 #endif 125 #if HAVE_ATTRIBUTE_UNUSED 126 #define MKSH_A_UNUSED __attribute__((__unused__)) 127 #else 128 #define MKSH_A_UNUSED /* nothing */ 129 #endif 130 #if HAVE_ATTRIBUTE_USED 131 #define MKSH_A_USED __attribute__((__used__)) 132 #else 133 #define MKSH_A_USED /* nothing */ 134 #endif 135 136 #if defined(MirBSD) && (MirBSD >= 0x09A1) && \ 137 defined(__ELF__) && defined(__GNUC__) && \ 138 !defined(__llvm__) && !defined(__NWCC__) 139 /* 140 * We got usable __IDSTRING __COPYRIGHT __RCSID __SCCSID macros 141 * which work for all cases; no need to redefine them using the 142 * "portable" macros from below when we might have the "better" 143 * gcc+ELF specific macros or other system dependent ones. 144 */ 145 #else 146 #undef __IDSTRING 147 #undef __IDSTRING_CONCAT 148 #undef __IDSTRING_EXPAND 149 #undef __COPYRIGHT 150 #undef __RCSID 151 #undef __SCCSID 152 #define __IDSTRING_CONCAT(l,p) __LINTED__ ## l ## _ ## p 153 #define __IDSTRING_EXPAND(l,p) __IDSTRING_CONCAT(l,p) 154 #ifdef MKSH_DONT_EMIT_IDSTRING 155 #define __IDSTRING(prefix, string) /* nothing */ 156 #else 157 #define __IDSTRING(prefix, string) \ 158 static const char __IDSTRING_EXPAND(__LINE__,prefix) [] \ 159 MKSH_A_USED = "@(""#)" #prefix ": " string 160 #endif 161 #define __COPYRIGHT(x) __IDSTRING(copyright,x) 162 #define __RCSID(x) __IDSTRING(rcsid,x) 163 #define __SCCSID(x) __IDSTRING(sccsid,x) 164 #endif 165 166 #ifdef EXTERN 167 __RCSID("$MirOS: src/bin/mksh/sh.h,v 1.667 2013/08/14 20:26:19 tg Exp $"); 168 #endif 169 #define MKSH_VERSION "R48 2013/08/14" 170 171 /* arithmetic types: C implementation */ 172 #if !HAVE_CAN_INTTYPES 173 #if !HAVE_CAN_UCBINTS 174 typedef signed int int32_t; 175 typedef unsigned int uint32_t; 176 #else 177 typedef u_int32_t uint32_t; 178 #endif 179 #endif 180 181 /* arithmetic types: shell arithmetics */ 182 #ifdef MKSH_LEGACY_MODE 183 /* 184 * POSIX demands these to be the C environment's long type 185 */ 186 typedef long mksh_ari_t; 187 typedef unsigned long mksh_uari_t; 188 #else 189 /* 190 * These types are exactly 32 bit wide; signed and unsigned 191 * integer wraparound, even across division and modulo, for 192 * any shell code using them, is guaranteed. 193 */ 194 typedef int32_t mksh_ari_t; 195 typedef uint32_t mksh_uari_t; 196 #endif 197 198 /* boolean type (no <stdbool.h> deliberately) */ 199 typedef unsigned char mksh_bool; 200 #undef bool 201 /* false MUST equal the same 0 as written by static storage initialisation */ 202 #undef false 203 #undef true 204 /* access macros for boolean type */ 205 #define bool mksh_bool 206 /* values must have identity mapping between mksh_bool and short */ 207 #define false 0 208 #define true 1 209 /* make any-type into bool or short */ 210 #define tobool(cond) ((cond) ? true : false) 211 212 /* char (octet) type: C implementation */ 213 #if !HAVE_CAN_INT8TYPE 214 #if !HAVE_CAN_UCBINT8 215 typedef unsigned char uint8_t; 216 #else 217 typedef u_int8_t uint8_t; 218 #endif 219 #endif 220 221 /* other standard types */ 222 223 #if !HAVE_RLIM_T 224 typedef unsigned long rlim_t; 225 #endif 226 227 #if !HAVE_SIG_T 228 #undef sig_t 229 typedef void (*sig_t)(int); 230 #endif 231 232 #ifdef MKSH_TYPEDEF_SIG_ATOMIC_T 233 typedef MKSH_TYPEDEF_SIG_ATOMIC_T sig_atomic_t; 234 #endif 235 236 #ifdef MKSH_TYPEDEF_SSIZE_T 237 typedef MKSH_TYPEDEF_SSIZE_T ssize_t; 238 #endif 239 240 /* un-do vendor damage */ 241 242 #undef BAD /* AIX defines that somewhere */ 243 #undef PRINT /* LynxOS defines that somewhere */ 244 #undef flock /* SCO UnixWare defines that to flock64 but ENOENT */ 245 246 247 #ifndef MKSH_INCLUDES_ONLY 248 249 /* extra types */ 250 251 #if !HAVE_GETRUSAGE 252 #undef rusage 253 #undef RUSAGE_SELF 254 #undef RUSAGE_CHILDREN 255 #define rusage mksh_rusage 256 #define RUSAGE_SELF 0 257 #define RUSAGE_CHILDREN -1 258 259 struct rusage { 260 struct timeval ru_utime; 261 struct timeval ru_stime; 262 }; 263 #endif 264 265 /* extra macros */ 266 267 #ifndef timerclear 268 #define timerclear(tvp) \ 269 do { \ 270 (tvp)->tv_sec = (tvp)->tv_usec = 0; \ 271 } while (/* CONSTCOND */ 0) 272 #endif 273 #ifndef timeradd 274 #define timeradd(tvp, uvp, vvp) \ 275 do { \ 276 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ 277 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ 278 if ((vvp)->tv_usec >= 1000000) { \ 279 (vvp)->tv_sec++; \ 280 (vvp)->tv_usec -= 1000000; \ 281 } \ 282 } while (/* CONSTCOND */ 0) 283 #endif 284 #ifndef timersub 285 #define timersub(tvp, uvp, vvp) \ 286 do { \ 287 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 288 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 289 if ((vvp)->tv_usec < 0) { \ 290 (vvp)->tv_sec--; \ 291 (vvp)->tv_usec += 1000000; \ 292 } \ 293 } while (/* CONSTCOND */ 0) 294 #endif 295 296 #define ksh_isdigit(c) (((c) >= '0') && ((c) <= '9')) 297 #define ksh_islower(c) (((c) >= 'a') && ((c) <= 'z')) 298 #define ksh_isupper(c) (((c) >= 'A') && ((c) <= 'Z')) 299 #define ksh_tolower(c) (((c) >= 'A') && ((c) <= 'Z') ? (c) - 'A' + 'a' : (c)) 300 #define ksh_toupper(c) (((c) >= 'a') && ((c) <= 'z') ? (c) - 'a' + 'A' : (c)) 301 #define ksh_isdash(s) (((s) != NULL) && ((s)[0] == '-') && ((s)[1] == '\0')) 302 #define ksh_isspace(c) ((((c) >= 0x09) && ((c) <= 0x0D)) || ((c) == 0x20)) 303 #define ksh_min(x,y) ((x) < (y) ? (x) : (y)) 304 #define ksh_max(x,y) ((x) > (y) ? (x) : (y)) 305 306 #ifdef MKSH__NO_PATH_MAX 307 #undef PATH_MAX 308 #else 309 #ifndef PATH_MAX 310 #define PATH_MAX 1024 311 #endif 312 #endif 313 #ifndef SIZE_MAX 314 #ifdef SIZE_T_MAX 315 #define SIZE_MAX SIZE_T_MAX 316 #else 317 #define SIZE_MAX ((size_t)-1) 318 #endif 319 #endif 320 #ifndef S_ISLNK 321 #define S_ISLNK(m) ((m & 0170000) == 0120000) 322 #endif 323 #ifndef S_ISSOCK 324 #define S_ISSOCK(m) ((m & 0170000) == 0140000) 325 #endif 326 #if !defined(S_ISCDF) && defined(S_CDF) 327 #define S_ISCDF(m) (S_ISDIR(m) && ((m) & S_CDF)) 328 #endif 329 #ifndef DEFFILEMODE 330 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) 331 #endif 332 333 #ifndef NSIG 334 #if defined(_NSIG) 335 #define NSIG _NSIG 336 #elif defined(SIGMAX) 337 #define NSIG (SIGMAX+1) 338 #elif defined(_SIGMAX) 339 #define NSIG (_SIGMAX+1) 340 #endif 341 #endif 342 343 344 /* OS-dependent additions (functions, variables, by OS) */ 345 346 #if !HAVE_FLOCK_DECL 347 extern int flock(int, int); 348 #endif 349 350 #if !HAVE_GETTIMEOFDAY 351 #define mksh_TIME(tv) do { \ 352 (tv).tv_usec = 0; \ 353 (tv).tv_sec = time(NULL); \ 354 } while (/* CONSTCOND */ 0) 355 #else 356 #define mksh_TIME(tv) gettimeofday(&(tv), NULL) 357 #endif 358 359 #if !HAVE_GETRUSAGE 360 extern int getrusage(int, struct rusage *); 361 #endif 362 363 #if !HAVE_MEMMOVE 364 /* we assume either memmove or bcopy exist, at the moment */ 365 #define memmove(dst, src, len) bcopy((src), (dst), (len)) 366 #endif 367 368 #if !HAVE_REVOKE_DECL 369 extern int revoke(const char *); 370 #endif 371 372 #if defined(DEBUG) || !HAVE_STRERROR 373 #undef strerror 374 #define strerror /* poisoned */ dontuse_strerror 375 #define cstrerror /* replaced */ cstrerror 376 extern const char *cstrerror(int); 377 #else 378 #define cstrerror(errnum) ((const char *)strerror(errnum)) 379 #endif 380 381 #if !HAVE_STRLCPY 382 size_t strlcpy(char *, const char *, size_t); 383 #endif 384 385 #ifdef __INTERIX 386 /* XXX imake style */ 387 #define makedev mkdev 388 extern int __cdecl seteuid(uid_t); 389 extern int __cdecl setegid(gid_t); 390 #endif 391 392 #if defined(__COHERENT__) 393 #ifndef O_ACCMODE 394 /* this need not work everywhere, take care */ 395 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR) 396 #endif 397 #endif 398 399 #ifdef MKSH__NO_SYMLINK 400 #undef S_ISLNK 401 #define S_ISLNK(m) (/* CONSTCOND */ 0) 402 #define mksh_lstat stat 403 #else 404 #define mksh_lstat lstat 405 #endif 406 407 #if HAVE_TERMIOS_H 408 #define mksh_ttyst struct termios 409 #define mksh_tcget(fd,st) tcgetattr((fd), (st)) 410 #define mksh_tcset(fd,st) tcsetattr((fd), TCSADRAIN, (st)) 411 #else 412 #define mksh_ttyst struct termio 413 #define mksh_tcget(fd,st) ioctl((fd), TCGETA, (st)) 414 #define mksh_tcset(fd,st) ioctl((fd), TCSETAW, (st)) 415 #endif 416 417 /* remove redundancies */ 418 419 #if defined(MirBSD) && (MirBSD >= 0x0AB3) && !defined(MKSH_OPTSTATIC) 420 #define MKSH_mirbsd_wcwidth 421 #define utf_wcwidth(i) wcwidth((__WCHAR_TYPE__)i) 422 extern int wcwidth(__WCHAR_TYPE__); 423 #endif 424 425 426 /* some useful #defines */ 427 #ifdef EXTERN 428 # define E_INIT(i) = i 429 #else 430 # define E_INIT(i) 431 # define EXTERN extern 432 # define EXTERN_DEFINED 433 #endif 434 435 /* define bit in flag */ 436 #define BIT(i) (1 << (i)) 437 #define NELEM(a) (sizeof(a) / sizeof((a)[0])) 438 439 /* 440 * Make MAGIC a char that might be printed to make bugs more obvious, but 441 * not a char that is used often. Also, can't use the high bit as it causes 442 * portability problems (calling strchr(x, 0x80 | 'x') is error prone). 443 */ 444 #define MAGIC (7) /* prefix for *?[!{,} during expand */ 445 #define ISMAGIC(c) ((unsigned char)(c) == MAGIC) 446 447 EXTERN const char *safe_prompt; /* safe prompt if PS1 substitution fails */ 448 449 #ifdef MKSH_LEGACY_MODE 450 #define KSH_VERSIONNAME "LEGACY" 451 #else 452 #define KSH_VERSIONNAME "MIRBSD" 453 #endif 454 EXTERN const char initvsn[] E_INIT("KSH_VERSION=@(#)" KSH_VERSIONNAME \ 455 " KSH " MKSH_VERSION); 456 #define KSH_VERSION (initvsn + /* "KSH_VERSION=@(#)" */ 16) 457 458 EXTERN const char digits_uc[] E_INIT("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 459 EXTERN const char digits_lc[] E_INIT("0123456789abcdefghijklmnopqrstuvwxyz"); 460 461 /* 462 * Evil hack for const correctness due to API brokenness 463 */ 464 union mksh_cchack { 465 char *rw; 466 const char *ro; 467 }; 468 union mksh_ccphack { 469 char **rw; 470 const char **ro; 471 }; 472 473 /* 474 * Evil hack since casting uint to sint is implementation-defined 475 */ 476 typedef union { 477 mksh_ari_t i; 478 mksh_uari_t u; 479 } mksh_ari_u; 480 481 /* for const debugging */ 482 #if defined(DEBUG) && defined(__GNUC__) && !defined(__ICC) && \ 483 !defined(__INTEL_COMPILER) && !defined(__SUNPRO_C) 484 char *ucstrchr(char *, int); 485 char *ucstrstr(char *, const char *); 486 #undef strchr 487 #define strchr ucstrchr 488 #define strstr ucstrstr 489 #define cstrchr(s,c) ({ \ 490 union mksh_cchack in, out; \ 491 \ 492 in.ro = (s); \ 493 out.rw = ucstrchr(in.rw, (c)); \ 494 (out.ro); \ 495 }) 496 #define cstrstr(b,l) ({ \ 497 union mksh_cchack in, out; \ 498 \ 499 in.ro = (b); \ 500 out.rw = ucstrstr(in.rw, (l)); \ 501 (out.ro); \ 502 }) 503 #define vstrchr(s,c) (cstrchr((s), (c)) != NULL) 504 #define vstrstr(b,l) (cstrstr((b), (l)) != NULL) 505 #else /* !DEBUG, !gcc */ 506 #define cstrchr(s,c) ((const char *)strchr((s), (c))) 507 #define cstrstr(s,c) ((const char *)strstr((s), (c))) 508 #define vstrchr(s,c) (strchr((s), (c)) != NULL) 509 #define vstrstr(b,l) (strstr((b), (l)) != NULL) 510 #endif 511 512 #if defined(DEBUG) || defined(__COVERITY__) 513 #define mkssert(e) do { if (!(e)) exit(255); } while (/* CONSTCOND */ 0) 514 #ifndef DEBUG_LEAKS 515 #define DEBUG_LEAKS 516 #endif 517 #else 518 #define mkssert(e) do { } while (/* CONSTCOND */ 0) 519 #endif 520 521 #if (!defined(MKSH_BUILDMAKEFILE4BSD) && !defined(MKSH_BUILDSH)) || (MKSH_BUILD_R != 481) 522 #error Must run Build.sh to compile this. 523 extern void thiswillneverbedefinedIhope(void); 524 int 525 im_sorry_dave(void) 526 { 527 /* Im sorry, Dave. Im afraid I cant do that. */ 528 return (thiswillneverbedefinedIhope()); 529 } 530 #endif 531 532 /* use this ipv strchr(s, 0) but no side effects in s! */ 533 #define strnul(s) ((s) + strlen(s)) 534 535 #define utf_ptradjx(src, dst) do { \ 536 (dst) = (src) + utf_ptradj(src); \ 537 } while (/* CONSTCOND */ 0) 538 539 #if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST) 540 #define strdupx(d, s, ap) do { \ 541 (d) = strdup_i((s), (ap)); \ 542 } while (/* CONSTCOND */ 0) 543 #define strndupx(d, s, n, ap) do { \ 544 (d) = strndup_i((s), (n), (ap)); \ 545 } while (/* CONSTCOND */ 0) 546 #else 547 /* be careful to evaluate arguments only once! */ 548 #define strdupx(d, s, ap) do { \ 549 const char *strdup_src = (s); \ 550 char *strdup_dst = NULL; \ 551 \ 552 if (strdup_src != NULL) { \ 553 size_t strdup_len = strlen(strdup_src) + 1; \ 554 strdup_dst = alloc(strdup_len, (ap)); \ 555 memcpy(strdup_dst, strdup_src, strdup_len); \ 556 } \ 557 (d) = strdup_dst; \ 558 } while (/* CONSTCOND */ 0) 559 #define strndupx(d, s, n, ap) do { \ 560 const char *strdup_src = (s); \ 561 char *strdup_dst = NULL; \ 562 \ 563 if (strdup_src != NULL) { \ 564 size_t strndup_len = (n); \ 565 strdup_dst = alloc(strndup_len + 1, (ap)); \ 566 memcpy(strdup_dst, strdup_src, strndup_len); \ 567 strdup_dst[strndup_len] = '\0'; \ 568 } \ 569 (d) = strdup_dst; \ 570 } while (/* CONSTCOND */ 0) 571 #endif 572 573 #ifdef MKSH_LEGACY_MODE 574 #ifndef MKSH_NO_CMDLINE_EDITING 575 #define MKSH_NO_CMDLINE_EDITING /* defined */ 576 #endif 577 #ifndef MKSH_CONSERVATIVE_FDS 578 #define MKSH_CONSERVATIVE_FDS /* defined */ 579 #endif 580 #undef MKSH_S_NOVI 581 #define MKSH_S_NOVI 1 582 #endif 583 584 #ifdef MKSH_SMALL 585 #ifndef MKSH_CONSERVATIVE_FDS 586 #define MKSH_CONSERVATIVE_FDS /* defined */ 587 #endif 588 #ifndef MKSH_NOPWNAM 589 #define MKSH_NOPWNAM /* defined */ 590 #endif 591 #ifndef MKSH_S_NOVI 592 #define MKSH_S_NOVI 1 593 #endif 594 #endif 595 596 #ifndef MKSH_S_NOVI 597 #define MKSH_S_NOVI 0 598 #endif 599 600 #if defined(MKSH_NOPROSPECTOFWORK) && !defined(MKSH_UNEMPLOYED) 601 #define MKSH_UNEMPLOYED 1 602 #endif 603 604 /* these shall be smaller than 100 */ 605 #ifdef MKSH_CONSERVATIVE_FDS 606 #define NUFILE 32 /* Number of user-accessible files */ 607 #define FDBASE 10 /* First file usable by Shell */ 608 #else 609 #define NUFILE 56 /* Number of user-accessible files */ 610 #define FDBASE 24 /* First file usable by Shell */ 611 #endif 612 613 /* 614 * simple grouping allocator 615 */ 616 617 618 /* 0. OS API: where to get memory from and how to free it (grouped) */ 619 620 /* malloc(3)/realloc(3) -> free(3) for use by the memory allocator */ 621 #define malloc_osi(sz) malloc(sz) 622 #define realloc_osi(p,sz) realloc((p), (sz)) 623 #define free_osimalloc(p) free(p) 624 625 /* malloc(3)/realloc(3) -> free(3) for use by mksh code */ 626 #define malloc_osfunc(sz) malloc(sz) 627 #define realloc_osfunc(p,sz) realloc((p), (sz)) 628 #define free_osfunc(p) free(p) 629 630 #if HAVE_MKNOD 631 /* setmode(3) -> free(3) */ 632 #define free_ossetmode(p) free(p) 633 #endif 634 635 #ifdef MKSH__NO_PATH_MAX 636 /* GNU libc: get_current_dir_name(3) -> free(3) */ 637 #define free_gnu_gcdn(p) free(p) 638 #endif 639 640 641 /* 1. internal structure */ 642 struct lalloc { 643 struct lalloc *next; 644 }; 645 646 /* 2. sizes */ 647 #define ALLOC_ITEM struct lalloc 648 #define ALLOC_SIZE (sizeof(ALLOC_ITEM)) 649 650 /* 3. group structure (only the same for lalloc.c) */ 651 typedef struct lalloc Area; 652 653 654 EXTERN Area aperm; /* permanent object space */ 655 #define APERM &aperm 656 #define ATEMP &e->area 657 658 /* 659 * flags (the order of these enums MUST match the order in misc.c(options[])) 660 */ 661 enum sh_flag { 662 #define SHFLAGS_ENUMS 663 #include "sh_flags.h" 664 FNFLAGS /* (place holder: how many flags are there) */ 665 }; 666 667 #define Flag(f) (shell_flags[(int)(f)]) 668 #define UTFMODE Flag(FUNICODE) 669 670 /* 671 * parsing & execution environment 672 * 673 * note that kshlongjmp MUST NOT be passed 0 as second argument! 674 */ 675 #ifdef MKSH_NO_SIGSETJMP 676 #define kshjmp_buf jmp_buf 677 #define kshsetjmp(jbuf) _setjmp(jbuf) 678 #define kshlongjmp _longjmp 679 #else 680 #define kshjmp_buf sigjmp_buf 681 #define kshsetjmp(jbuf) sigsetjmp((jbuf), 0) 682 #define kshlongjmp siglongjmp 683 #endif 684 685 struct sretrace_info; 686 struct yyrecursive_state; 687 688 EXTERN struct sretrace_info *retrace_info E_INIT(NULL); 689 EXTERN int subshell_nesting_type E_INIT(0); 690 691 extern struct env { 692 ALLOC_ITEM alloc_INT; /* internal, do not touch */ 693 Area area; /* temporary allocation area */ 694 struct env *oenv; /* link to previous environment */ 695 struct block *loc; /* local variables and functions */ 696 short *savefd; /* original redirected fds */ 697 struct temp *temps; /* temp files */ 698 /* saved parser recursion state */ 699 struct yyrecursive_state *yyrecursive_statep; 700 kshjmp_buf jbuf; /* long jump back to env creator */ 701 uint8_t type; /* environment type - see below */ 702 uint8_t flags; /* EF_* */ 703 } *e; 704 705 /* struct env.type values */ 706 #define E_NONE 0 /* dummy environment */ 707 #define E_PARSE 1 /* parsing command # */ 708 #define E_FUNC 2 /* executing function # */ 709 #define E_INCL 3 /* including a file via . # */ 710 #define E_EXEC 4 /* executing command tree */ 711 #define E_LOOP 5 /* executing for/while # */ 712 #define E_ERRH 6 /* general error handler # */ 713 #define E_GONE 7 /* hidden in child */ 714 /* # indicates env has valid jbuf (see unwind()) */ 715 716 /* struct env.flag values */ 717 #define EF_BRKCONT_PASS BIT(1) /* set if E_LOOP must pass break/continue on */ 718 #define EF_FAKE_SIGDIE BIT(2) /* hack to get info from unwind to quitenv */ 719 720 /* Do breaks/continues stop at env type e? */ 721 #define STOP_BRKCONT(t) ((t) == E_NONE || (t) == E_PARSE || \ 722 (t) == E_FUNC || (t) == E_INCL) 723 /* Do returns stop at env type e? */ 724 #define STOP_RETURN(t) ((t) == E_FUNC || (t) == E_INCL) 725 726 /* values for kshlongjmp(e->jbuf, i) */ 727 /* note that i MUST NOT be zero */ 728 #define LRETURN 1 /* return statement */ 729 #define LEXIT 2 /* exit statement */ 730 #define LERROR 3 /* errorf() called */ 731 #define LLEAVE 4 /* untrappable exit/error */ 732 #define LINTR 5 /* ^C noticed */ 733 #define LBREAK 6 /* break statement */ 734 #define LCONTIN 7 /* continue statement */ 735 #define LSHELL 8 /* return to interactive shell() */ 736 #define LAEXPR 9 /* error in arithmetic expression */ 737 738 /* sort of shell global state */ 739 EXTERN pid_t procpid; /* PID of executing process */ 740 EXTERN int exstat; /* exit status */ 741 EXTERN int subst_exstat; /* exit status of last $(..)/`..` */ 742 EXTERN struct tbl *vp_pipest; /* global PIPESTATUS array */ 743 EXTERN short trap_exstat; /* exit status before running a trap */ 744 EXTERN uint8_t trap_nested; /* running nested traps */ 745 EXTERN uint8_t shell_flags[FNFLAGS]; 746 EXTERN const char *kshname; /* $0 */ 747 EXTERN struct { 748 uid_t kshuid_v; /* real UID of shell */ 749 uid_t ksheuid_v; /* effective UID of shell */ 750 gid_t kshgid_v; /* real GID of shell */ 751 gid_t kshegid_v; /* effective GID of shell */ 752 pid_t kshpgrp_v; /* process group of shell */ 753 pid_t kshppid_v; /* PID of parent of shell */ 754 pid_t kshpid_v; /* $$, shell PID */ 755 } rndsetupstate; 756 757 #define kshpid rndsetupstate.kshpid_v 758 #define kshpgrp rndsetupstate.kshpgrp_v 759 #define kshuid rndsetupstate.kshuid_v 760 #define ksheuid rndsetupstate.ksheuid_v 761 #define kshgid rndsetupstate.kshgid_v 762 #define kshegid rndsetupstate.kshegid_v 763 #define kshppid rndsetupstate.kshppid_v 764 765 766 /* option processing */ 767 #define OF_CMDLINE 0x01 /* command line */ 768 #define OF_SET 0x02 /* set builtin */ 769 #define OF_SPECIAL 0x04 /* a special variable changing */ 770 #define OF_INTERNAL 0x08 /* set internally by shell */ 771 #define OF_FIRSTTIME 0x10 /* as early as possible, once */ 772 #define OF_ANY (OF_CMDLINE | OF_SET | OF_SPECIAL | OF_INTERNAL) 773 774 /* null value for variable; comparison pointer for unset */ 775 EXTERN char null[] E_INIT(""); 776 /* helpers for string pooling */ 777 EXTERN const char Tintovfl[] E_INIT("integer overflow %zu %c %zu prevented"); 778 EXTERN const char Toomem[] E_INIT("can't allocate %zu data bytes"); 779 #if defined(__GNUC__) 780 /* trust this to have string pooling; -Wformat bitches otherwise */ 781 #define Tsynerr "syntax error" 782 #else 783 EXTERN const char Tsynerr[] E_INIT("syntax error"); 784 #endif 785 EXTERN const char Tselect[] E_INIT("select"); 786 EXTERN const char Tr_fc_e_dash[] E_INIT("r=fc -e -"); 787 #define Tfc_e_dash (Tr_fc_e_dash + 2) /* "fc -e -" */ 788 #define Zfc_e_dash 7 /* strlen(Tfc_e_dash) */ 789 EXTERN const char Tlocal_typeset[] E_INIT("local=typeset"); 790 #define T_typeset (Tlocal_typeset + 5) /* "=typeset" */ 791 #define Ttypeset (Tlocal_typeset + 6) /* "typeset" */ 792 EXTERN const char Talias[] E_INIT("alias"); 793 EXTERN const char Tunalias[] E_INIT("unalias"); 794 EXTERN const char Tsgset[] E_INIT("*=set"); 795 #define Tset (Tsgset + 2) /* "set" */ 796 EXTERN const char Tsgunset[] E_INIT("*=unset"); 797 #define Tunset (Tsgunset + 2) /* "unset" */ 798 EXTERN const char Tsgexport[] E_INIT("*=export"); 799 #define Texport (Tsgexport + 2) /* "export" */ 800 EXTERN const char Tsgreadonly[] E_INIT("*=readonly"); 801 #define Treadonly (Tsgreadonly + 2) /* "readonly" */ 802 EXTERN const char Tgbuiltin[] E_INIT("=builtin"); 803 #define Tbuiltin (Tgbuiltin + 1) /* "builtin" */ 804 EXTERN const char T_function[] E_INIT(" function"); 805 #define Tfunction (T_function + 1) /* "function" */ 806 EXTERN const char TC_LEX1[] E_INIT("|&;<>() \t\n"); 807 #define TC_IFSWS (TC_LEX1 + 7) /* space tab newline */ 808 809 typedef uint8_t Temp_type; 810 /* expanded heredoc */ 811 #define TT_HEREDOC_EXP 0 812 /* temporary file used for history editing (fc -e) */ 813 #define TT_HIST_EDIT 1 814 /* temporary file used during in-situ command substitution */ 815 #define TT_FUNSUB 2 816 817 /* temp/heredoc files. The file is removed when the struct is freed. */ 818 struct temp { 819 struct temp *next; 820 struct shf *shf; 821 /* pid of process parsed here-doc */ 822 pid_t pid; 823 Temp_type type; 824 /* actually longer: name (variable length) */ 825 char tffn[3]; 826 }; 827 828 /* 829 * stdio and our IO routines 830 */ 831 832 #define shl_xtrace (&shf_iob[0]) /* for set -x */ 833 #define shl_stdout (&shf_iob[1]) 834 #define shl_out (&shf_iob[2]) 835 #ifdef DF 836 #define shl_dbg (&shf_iob[3]) /* for DF() */ 837 #endif 838 EXTERN bool shl_stdout_ok; 839 840 /* 841 * trap handlers 842 */ 843 typedef struct trap { 844 const char *name; /* short name */ 845 const char *mess; /* descriptive name */ 846 char *trap; /* trap command */ 847 sig_t cursig; /* current handler (valid if TF_ORIG_* set) */ 848 sig_t shtrap; /* shell signal handler */ 849 int signal; /* signal number */ 850 int flags; /* TF_* */ 851 volatile sig_atomic_t set; /* trap pending */ 852 } Trap; 853 854 /* values for Trap.flags */ 855 #define TF_SHELL_USES BIT(0) /* shell uses signal, user can't change */ 856 #define TF_USER_SET BIT(1) /* user has (tried to) set trap */ 857 #define TF_ORIG_IGN BIT(2) /* original action was SIG_IGN */ 858 #define TF_ORIG_DFL BIT(3) /* original action was SIG_DFL */ 859 #define TF_EXEC_IGN BIT(4) /* restore SIG_IGN just before exec */ 860 #define TF_EXEC_DFL BIT(5) /* restore SIG_DFL just before exec */ 861 #define TF_DFL_INTR BIT(6) /* when received, default action is LINTR */ 862 #define TF_TTY_INTR BIT(7) /* tty generated signal (see j_waitj) */ 863 #define TF_CHANGED BIT(8) /* used by runtrap() to detect trap changes */ 864 #define TF_FATAL BIT(9) /* causes termination if not trapped */ 865 866 /* values for setsig()/setexecsig() flags argument */ 867 #define SS_RESTORE_MASK 0x3 /* how to restore a signal before an exec() */ 868 #define SS_RESTORE_CURR 0 /* leave current handler in place */ 869 #define SS_RESTORE_ORIG 1 /* restore original handler */ 870 #define SS_RESTORE_DFL 2 /* restore to SIG_DFL */ 871 #define SS_RESTORE_IGN 3 /* restore to SIG_IGN */ 872 #define SS_FORCE BIT(3) /* set signal even if original signal ignored */ 873 #define SS_USER BIT(4) /* user is doing the set (ie, trap command) */ 874 #define SS_SHTRAP BIT(5) /* trap for internal use (ALRM, CHLD, WINCH) */ 875 876 #define ksh_SIGEXIT 0 /* for trap EXIT */ 877 #define ksh_SIGERR NSIG /* for trap ERR */ 878 879 EXTERN volatile sig_atomic_t trap; /* traps pending? */ 880 EXTERN volatile sig_atomic_t intrsig; /* pending trap interrupts command */ 881 EXTERN volatile sig_atomic_t fatal_trap; /* received a fatal signal */ 882 extern Trap sigtraps[NSIG+1]; 883 884 /* got_winch = 1 when we need to re-adjust the window size */ 885 #ifdef SIGWINCH 886 EXTERN volatile sig_atomic_t got_winch E_INIT(1); 887 #else 888 #define got_winch true 889 #endif 890 891 /* 892 * TMOUT support 893 */ 894 /* values for ksh_tmout_state */ 895 enum tmout_enum { 896 TMOUT_EXECUTING = 0, /* executing commands */ 897 TMOUT_READING, /* waiting for input */ 898 TMOUT_LEAVING /* have timed out */ 899 }; 900 EXTERN unsigned int ksh_tmout; 901 EXTERN enum tmout_enum ksh_tmout_state E_INIT(TMOUT_EXECUTING); 902 903 /* For "You have stopped jobs" message */ 904 EXTERN bool really_exit; 905 906 /* 907 * fast character classes 908 */ 909 #define C_ALPHA BIT(0) /* a-z_A-Z */ 910 #define C_DIGIT BIT(1) /* 0-9 */ 911 #define C_LEX1 BIT(2) /* \t \n\0|&;<>() */ 912 #define C_VAR1 BIT(3) /* *@#!$-? */ 913 #define C_IFSWS BIT(4) /* \t \n (IFS white space) */ 914 #define C_SUBOP1 BIT(5) /* "=-+?" */ 915 #define C_QUOTE BIT(6) /* \t\n "#$&'()*;<=>?[\]`| (needing quoting) */ 916 #define C_IFS BIT(7) /* $IFS */ 917 #define C_SUBOP2 BIT(8) /* "#%" (magic, see below) */ 918 919 extern unsigned char chtypes[]; 920 921 #define ctype(c, t) tobool( ((t) == C_SUBOP2) ? \ 922 (((c) == '#' || (c) == '%') ? 1 : 0) : \ 923 (chtypes[(unsigned char)(c)] & (t)) ) 924 #define ksh_isalphx(c) ctype((c), C_ALPHA) 925 #define ksh_isalnux(c) ctype((c), C_ALPHA | C_DIGIT) 926 927 EXTERN int ifs0 E_INIT(' '); /* for "$*" */ 928 929 /* Argument parsing for built-in commands and getopts command */ 930 931 /* Values for Getopt.flags */ 932 #define GF_ERROR BIT(0) /* call errorf() if there is an error */ 933 #define GF_PLUSOPT BIT(1) /* allow +c as an option */ 934 #define GF_NONAME BIT(2) /* don't print argv[0] in errors */ 935 936 /* Values for Getopt.info */ 937 #define GI_MINUS BIT(0) /* an option started with -... */ 938 #define GI_PLUS BIT(1) /* an option started with +... */ 939 #define GI_MINUSMINUS BIT(2) /* arguments were ended with -- */ 940 941 /* in case some OS defines these */ 942 #undef optarg 943 #undef optind 944 945 typedef struct { 946 const char *optarg; 947 int optind; 948 int uoptind; /* what user sees in $OPTIND */ 949 int flags; /* see GF_* */ 950 int info; /* see GI_* */ 951 unsigned int p; /* 0 or index into argv[optind - 1] */ 952 char buf[2]; /* for bad option OPTARG value */ 953 } Getopt; 954 955 EXTERN Getopt builtin_opt; /* for shell builtin commands */ 956 EXTERN Getopt user_opt; /* parsing state for getopts builtin command */ 957 958 /* This for co-processes */ 959 960 /* something that won't (realisticly) wrap */ 961 typedef int32_t Coproc_id; 962 963 struct coproc { 964 void *job; /* 0 or job of co-process using input pipe */ 965 int read; /* pipe from co-process's stdout */ 966 int readw; /* other side of read (saved temporarily) */ 967 int write; /* pipe to co-process's stdin */ 968 int njobs; /* number of live jobs using output pipe */ 969 Coproc_id id; /* id of current output pipe */ 970 }; 971 EXTERN struct coproc coproc; 972 973 #ifndef MKSH_NOPROSPECTOFWORK 974 /* used in jobs.c and by coprocess stuff in exec.c and select() calls */ 975 EXTERN sigset_t sm_default, sm_sigchld; 976 #endif 977 978 /* name of called builtin function (used by error functions) */ 979 EXTERN const char *builtin_argv0; 980 /* flags of called builtin (SPEC_BI, etc.) */ 981 EXTERN uint32_t builtin_flag; 982 983 /* current working directory */ 984 EXTERN char *current_wd; 985 986 /* input line size */ 987 #define LINE (4096 - ALLOC_SIZE) 988 /* 989 * Minimum required space to work with on a line - if the prompt leaves 990 * less space than this on a line, the prompt is truncated. 991 */ 992 #define MIN_EDIT_SPACE 7 993 /* 994 * Minimum allowed value for x_cols: 2 for prompt, 3 for " < " at end of line 995 */ 996 #define MIN_COLS (2 + MIN_EDIT_SPACE + 3) 997 #define MIN_LINS 3 998 EXTERN mksh_ari_t x_cols E_INIT(80); /* tty columns */ 999 EXTERN mksh_ari_t x_lins E_INIT(24); /* tty lines */ 1000 1001 1002 /* Determine the location of the system (common) profile */ 1003 1004 #ifndef MKSH_DEFAULT_PROFILEDIR 1005 #if defined(ANDROID) 1006 #define MKSH_DEFAULT_PROFILEDIR "/system/etc" 1007 #else 1008 #define MKSH_DEFAULT_PROFILEDIR "/etc" 1009 #endif 1010 #endif 1011 1012 #define MKSH_SYSTEM_PROFILE MKSH_DEFAULT_PROFILEDIR "/profile" 1013 #define MKSH_SUID_PROFILE MKSH_DEFAULT_PROFILEDIR "/suid_profile" 1014 1015 1016 /* Used by v_evaluate() and setstr() to control action when error occurs */ 1017 #define KSH_UNWIND_ERROR 0 /* unwind the stack (kshlongjmp) */ 1018 #define KSH_RETURN_ERROR 1 /* return 1/0 for success/failure */ 1019 1020 /* 1021 * Shell file I/O routines 1022 */ 1023 1024 #define SHF_BSIZE 512 1025 1026 #define shf_fileno(shf) ((shf)->fd) 1027 #define shf_setfileno(shf,nfd) ((shf)->fd = (nfd)) 1028 #define shf_getc_i(shf) ((shf)->rnleft > 0 ? \ 1029 (shf)->rnleft--, *(shf)->rp++ : \ 1030 shf_getchar(shf)) 1031 #define shf_putc_i(c, shf) ((shf)->wnleft == 0 ? \ 1032 shf_putchar((c), (shf)) : \ 1033 ((shf)->wnleft--, *(shf)->wp++ = (c))) 1034 #define shf_eof(shf) ((shf)->flags & SHF_EOF) 1035 #define shf_error(shf) ((shf)->flags & SHF_ERROR) 1036 #define shf_errno(shf) ((shf)->errnosv) 1037 #define shf_clearerr(shf) ((shf)->flags &= ~(SHF_EOF | SHF_ERROR)) 1038 1039 /* Flags passed to shf_*open() */ 1040 #define SHF_RD 0x0001 1041 #define SHF_WR 0x0002 1042 #define SHF_RDWR (SHF_RD|SHF_WR) 1043 #define SHF_ACCMODE 0x0003 /* mask */ 1044 #define SHF_GETFL 0x0004 /* use fcntl() to figure RD/WR flags */ 1045 #define SHF_UNBUF 0x0008 /* unbuffered I/O */ 1046 #define SHF_CLEXEC 0x0010 /* set close on exec flag */ 1047 #define SHF_MAPHI 0x0020 /* make fd > FDBASE (and close orig) 1048 * (shf_open() only) */ 1049 #define SHF_DYNAMIC 0x0040 /* string: increase buffer as needed */ 1050 #define SHF_INTERRUPT 0x0080 /* EINTR in read/write causes error */ 1051 /* Flags used internally */ 1052 #define SHF_STRING 0x0100 /* a string, not a file */ 1053 #define SHF_ALLOCS 0x0200 /* shf and shf->buf were alloc()ed */ 1054 #define SHF_ALLOCB 0x0400 /* shf->buf was alloc()ed */ 1055 #define SHF_ERROR 0x0800 /* read()/write() error */ 1056 #define SHF_EOF 0x1000 /* read eof (sticky) */ 1057 #define SHF_READING 0x2000 /* currently reading: rnleft,rp valid */ 1058 #define SHF_WRITING 0x4000 /* currently writing: wnleft,wp valid */ 1059 1060 1061 struct shf { 1062 Area *areap; /* area shf/buf were allocated in */ 1063 unsigned char *rp; /* read: current position in buffer */ 1064 unsigned char *wp; /* write: current position in buffer */ 1065 unsigned char *buf; /* buffer */ 1066 ssize_t bsize; /* actual size of buf */ 1067 ssize_t rbsize; /* size of buffer (1 if SHF_UNBUF) */ 1068 ssize_t rnleft; /* read: how much data left in buffer */ 1069 ssize_t wbsize; /* size of buffer (0 if SHF_UNBUF) */ 1070 ssize_t wnleft; /* write: how much space left in buffer */ 1071 int flags; /* see SHF_* */ 1072 int fd; /* file descriptor */ 1073 int errnosv; /* saved value of errno after error */ 1074 }; 1075 1076 extern struct shf shf_iob[]; 1077 1078 struct table { 1079 Area *areap; /* area to allocate entries */ 1080 struct tbl **tbls; /* hashed table items */ 1081 size_t nfree; /* free table entries */ 1082 uint8_t tshift; /* table size (2^tshift) */ 1083 }; 1084 1085 /* table item */ 1086 struct tbl { 1087 /* Area to allocate from */ 1088 Area *areap; 1089 /* value */ 1090 union { 1091 char *s; /* string */ 1092 mksh_ari_t i; /* integer */ 1093 mksh_uari_t u; /* unsigned integer */ 1094 int (*f)(const char **); /* built-in command */ 1095 struct op *t; /* "function" tree */ 1096 } val; 1097 union { 1098 struct tbl *array; /* array values */ 1099 const char *fpath; /* temporary path to undef function */ 1100 } u; 1101 union { 1102 int field; /* field with for -L/-R/-Z */ 1103 int errnov; /* CEXEC/CTALIAS */ 1104 } u2; 1105 union { 1106 uint32_t hval; /* hash(name) */ 1107 uint32_t index; /* index for an array */ 1108 } ua; 1109 /* 1110 * command type (see below), base (if INTEGER), 1111 * offset from val.s of value (if EXPORT) 1112 */ 1113 int type; 1114 /* flags (see below) */ 1115 uint32_t flag; 1116 1117 /* actually longer: name (variable length) */ 1118 char name[4]; 1119 }; 1120 1121 EXTERN struct tbl vtemp; 1122 1123 /* common flag bits */ 1124 #define ALLOC BIT(0) /* val.s has been allocated */ 1125 #define DEFINED BIT(1) /* is defined in block */ 1126 #define ISSET BIT(2) /* has value, vp->val.[si] */ 1127 #define EXPORT BIT(3) /* exported variable/function */ 1128 #define TRACE BIT(4) /* var: user flagged, func: execution tracing */ 1129 /* (start non-common flags at 8) */ 1130 /* flag bits used for variables */ 1131 #define SPECIAL BIT(8) /* PATH, IFS, SECONDS, etc */ 1132 #define INTEGER BIT(9) /* val.i contains integer value */ 1133 #define RDONLY BIT(10) /* read-only variable */ 1134 #define LOCAL BIT(11) /* for local typeset() */ 1135 #define ARRAY BIT(13) /* array */ 1136 #define LJUST BIT(14) /* left justify */ 1137 #define RJUST BIT(15) /* right justify */ 1138 #define ZEROFIL BIT(16) /* 0 filled if RJUSTIFY, strip 0s if LJUSTIFY */ 1139 #define LCASEV BIT(17) /* convert to lower case */ 1140 #define UCASEV_AL BIT(18) /* convert to upper case / autoload function */ 1141 #define INT_U BIT(19) /* unsigned integer */ 1142 #define INT_L BIT(20) /* long integer (no-op but used as magic) */ 1143 #define IMPORT BIT(21) /* flag to typeset(): no arrays, must have = */ 1144 #define LOCAL_COPY BIT(22) /* with LOCAL - copy attrs from existing var */ 1145 #define EXPRINEVAL BIT(23) /* contents currently being evaluated */ 1146 #define EXPRLVALUE BIT(24) /* useable as lvalue (temp flag) */ 1147 #define AINDEX BIT(25) /* array index >0 = ua.index filled in */ 1148 #define ASSOC BIT(26) /* ARRAY ? associative : reference */ 1149 /* flag bits used for taliases/builtins/aliases/keywords/functions */ 1150 #define KEEPASN BIT(8) /* keep command assignments (eg, var=x cmd) */ 1151 #define FINUSE BIT(9) /* function being executed */ 1152 #define FDELETE BIT(10) /* function deleted while it was executing */ 1153 #define FKSH BIT(11) /* function defined with function x (vs x()) */ 1154 #define SPEC_BI BIT(12) /* a POSIX special builtin */ 1155 /* 1156 * Attributes that can be set by the user (used to decide if an unset 1157 * param should be repoted by set/typeset). Does not include ARRAY or 1158 * LOCAL. 1159 */ 1160 #define USERATTRIB (EXPORT|INTEGER|RDONLY|LJUST|RJUST|ZEROFIL|\ 1161 LCASEV|UCASEV_AL|INT_U|INT_L) 1162 1163 #define arrayindex(vp) ((unsigned long)((vp)->flag & AINDEX ? \ 1164 (vp)->ua.index : 0)) 1165 1166 EXTERN enum { 1167 SRF_NOP, 1168 SRF_ENABLE, 1169 SRF_DISABLE 1170 } set_refflag E_INIT(SRF_NOP); 1171 1172 /* command types */ 1173 #define CNONE 0 /* undefined */ 1174 #define CSHELL 1 /* built-in */ 1175 #define CFUNC 2 /* function */ 1176 #define CEXEC 4 /* executable command */ 1177 #define CALIAS 5 /* alias */ 1178 #define CKEYWD 6 /* keyword */ 1179 #define CTALIAS 7 /* tracked alias */ 1180 1181 /* Flags for findcom()/comexec() */ 1182 #define FC_SPECBI BIT(0) /* special builtin */ 1183 #define FC_FUNC BIT(1) /* function */ 1184 #define FC_NORMBI BIT(2) /* not special builtin */ 1185 #define FC_BI (FC_SPECBI | FC_NORMBI) 1186 #define FC_PATH BIT(3) /* do path search */ 1187 #define FC_DEFPATH BIT(4) /* use default path in path search */ 1188 1189 1190 #define AF_ARGV_ALLOC 0x1 /* argv[] array allocated */ 1191 #define AF_ARGS_ALLOCED 0x2 /* argument strings allocated */ 1192 #define AI_ARGV(a, i) ((i) == 0 ? (a).argv[0] : (a).argv[(i) - (a).skip]) 1193 #define AI_ARGC(a) ((a).ai_argc - (a).skip) 1194 1195 /* Argument info. Used for $#, $* for shell, functions, includes, etc. */ 1196 struct arg_info { 1197 const char **argv; 1198 int flags; /* AF_* */ 1199 int ai_argc; 1200 int skip; /* first arg is argv[0], second is argv[1 + skip] */ 1201 }; 1202 1203 /* 1204 * activation record for function blocks 1205 */ 1206 struct block { 1207 Area area; /* area to allocate things */ 1208 const char **argv; 1209 char *error; /* error handler */ 1210 char *exit; /* exit handler */ 1211 struct block *next; /* enclosing block */ 1212 struct table vars; /* local variables */ 1213 struct table funs; /* local functions */ 1214 Getopt getopts_state; 1215 int argc; 1216 int flags; /* see BF_* */ 1217 }; 1218 1219 /* Values for struct block.flags */ 1220 #define BF_DOGETOPTS BIT(0) /* save/restore getopts state */ 1221 1222 /* 1223 * Used by ktwalk() and ktnext() routines. 1224 */ 1225 struct tstate { 1226 struct tbl **next; 1227 ssize_t left; 1228 }; 1229 1230 EXTERN struct table taliases; /* tracked aliases */ 1231 EXTERN struct table builtins; /* built-in commands */ 1232 EXTERN struct table aliases; /* aliases */ 1233 EXTERN struct table keywords; /* keywords */ 1234 #ifndef MKSH_NOPWNAM 1235 EXTERN struct table homedirs; /* homedir() cache */ 1236 #endif 1237 1238 struct builtin { 1239 const char *name; 1240 int (*func)(const char **); 1241 }; 1242 1243 extern const struct builtin mkshbuiltins[]; 1244 1245 /* values for set_prompt() */ 1246 #define PS1 0 /* command */ 1247 #define PS2 1 /* command continuation */ 1248 1249 EXTERN char *path; /* copy of either PATH or def_path */ 1250 EXTERN const char *def_path; /* path to use if PATH not set */ 1251 EXTERN char *tmpdir; /* TMPDIR value */ 1252 EXTERN const char *prompt; 1253 EXTERN int cur_prompt; /* PS1 or PS2 */ 1254 EXTERN int current_lineno; /* LINENO value */ 1255 1256 /* 1257 * Description of a command or an operation on commands. 1258 */ 1259 struct op { 1260 const char **args; /* arguments to a command */ 1261 char **vars; /* variable assignments */ 1262 struct ioword **ioact; /* IO actions (eg, < > >>) */ 1263 struct op *left, *right; /* descendents */ 1264 char *str; /* word for case; identifier for for, 1265 * select, and functions; 1266 * path to execute for TEXEC; 1267 * time hook for TCOM. 1268 */ 1269 int lineno; /* TCOM/TFUNC: LINENO for this */ 1270 short type; /* operation type, see below */ 1271 /* WARNING: newtp(), tcopy() use evalflags = 0 to clear union */ 1272 union { 1273 /* TCOM: arg expansion eval() flags */ 1274 short evalflags; 1275 /* TFUNC: function x (vs x()) */ 1276 short ksh_func; 1277 /* TPAT: termination character */ 1278 char charflag; 1279 } u; 1280 }; 1281 1282 /* Tree.type values */ 1283 #define TEOF 0 1284 #define TCOM 1 /* command */ 1285 #define TPAREN 2 /* (c-list) */ 1286 #define TPIPE 3 /* a | b */ 1287 #define TLIST 4 /* a ; b */ 1288 #define TOR 5 /* || */ 1289 #define TAND 6 /* && */ 1290 #define TBANG 7 /* ! */ 1291 #define TDBRACKET 8 /* [[ .. ]] */ 1292 #define TFOR 9 1293 #define TSELECT 10 1294 #define TCASE 11 1295 #define TIF 12 1296 #define TWHILE 13 1297 #define TUNTIL 14 1298 #define TELIF 15 1299 #define TPAT 16 /* pattern in case */ 1300 #define TBRACE 17 /* {c-list} */ 1301 #define TASYNC 18 /* c & */ 1302 #define TFUNCT 19 /* function name { command; } */ 1303 #define TTIME 20 /* time pipeline */ 1304 #define TEXEC 21 /* fork/exec eval'd TCOM */ 1305 #define TCOPROC 22 /* coprocess |& */ 1306 1307 /* 1308 * prefix codes for words in command tree 1309 */ 1310 #define EOS 0 /* end of string */ 1311 #define CHAR 1 /* unquoted character */ 1312 #define QCHAR 2 /* quoted character */ 1313 #define COMSUB 3 /* $() substitution (0 terminated) */ 1314 #define EXPRSUB 4 /* $(()) substitution (0 terminated) */ 1315 #define OQUOTE 5 /* opening " or ' */ 1316 #define CQUOTE 6 /* closing " or ' */ 1317 #define OSUBST 7 /* opening ${ subst (followed by { or X) */ 1318 #define CSUBST 8 /* closing } of above (followed by } or X) */ 1319 #define OPAT 9 /* open pattern: *(, @(, etc. */ 1320 #define SPAT 10 /* separate pattern: | */ 1321 #define CPAT 11 /* close pattern: ) */ 1322 #define ADELIM 12 /* arbitrary delimiter: ${foo:2:3} ${foo/bar/baz} */ 1323 #define FUNSUB 14 /* ${ foo;} substitution (NUL terminated) */ 1324 #define VALSUB 15 /* ${|foo;} substitution (NUL terminated) */ 1325 1326 /* 1327 * IO redirection 1328 */ 1329 struct ioword { 1330 int unit; /* unit affected */ 1331 int flag; /* action (below) */ 1332 char *name; /* file name (unused if heredoc) */ 1333 char *delim; /* delimiter for <<,<<- */ 1334 char *heredoc; /* content of heredoc */ 1335 }; 1336 1337 /* ioword.flag - type of redirection */ 1338 #define IOTYPE 0xF /* type: bits 0:3 */ 1339 #define IOREAD 0x1 /* < */ 1340 #define IOWRITE 0x2 /* > */ 1341 #define IORDWR 0x3 /* <>: todo */ 1342 #define IOHERE 0x4 /* << (here file) */ 1343 #define IOCAT 0x5 /* >> */ 1344 #define IODUP 0x6 /* <&/>& */ 1345 #define IOEVAL BIT(4) /* expand in << */ 1346 #define IOSKIP BIT(5) /* <<-, skip ^\t* */ 1347 #define IOCLOB BIT(6) /* >|, override -o noclobber */ 1348 #define IORDUP BIT(7) /* x<&y (as opposed to x>&y) */ 1349 #define IONAMEXP BIT(8) /* name has been expanded */ 1350 #define IOBASH BIT(9) /* &> etc. */ 1351 #define IOHERESTR BIT(10) /* <<< (here string) */ 1352 #define IONDELIM BIT(11) /* null delimiter (<<) */ 1353 1354 /* execute/exchild flags */ 1355 #define XEXEC BIT(0) /* execute without forking */ 1356 #define XFORK BIT(1) /* fork before executing */ 1357 #define XBGND BIT(2) /* command & */ 1358 #define XPIPEI BIT(3) /* input is pipe */ 1359 #define XPIPEO BIT(4) /* output is pipe */ 1360 #define XXCOM BIT(5) /* `...` command */ 1361 #define XPCLOSE BIT(6) /* exchild: close close_fd in parent */ 1362 #define XCCLOSE BIT(7) /* exchild: close close_fd in child */ 1363 #define XERROK BIT(8) /* non-zero exit ok (for set -e) */ 1364 #define XCOPROC BIT(9) /* starting a co-process */ 1365 #define XTIME BIT(10) /* timing TCOM command */ 1366 #define XPIPEST BIT(11) /* want PIPESTATUS */ 1367 1368 /* 1369 * flags to control expansion of words (assumed by t->evalflags to fit 1370 * in a short) 1371 */ 1372 #define DOBLANK BIT(0) /* perform blank interpretation */ 1373 #define DOGLOB BIT(1) /* expand [?* */ 1374 #define DOPAT BIT(2) /* quote *?[ */ 1375 #define DOTILDE BIT(3) /* normal ~ expansion (first char) */ 1376 #define DONTRUNCOMMAND BIT(4) /* do not run $(command) things */ 1377 #define DOASNTILDE BIT(5) /* assignment ~ expansion (after =, :) */ 1378 #define DOBRACE BIT(6) /* used by expand(): do brace expansion */ 1379 #define DOMAGIC BIT(7) /* used by expand(): string contains MAGIC */ 1380 #define DOTEMP BIT(8) /* dito: in word part of ${..[%#=?]..} */ 1381 #define DOVACHECK BIT(9) /* var assign check (for typeset, set, etc) */ 1382 #define DOMARKDIRS BIT(10) /* force markdirs behaviour */ 1383 #if !defined(MKSH_SMALL) 1384 #define DOTCOMEXEC BIT(11) /* not an eval flag, used by sh -c hack */ 1385 #endif 1386 1387 /* 1388 * The arguments of [[ .. ]] expressions are kept in t->args[] and flags 1389 * indicating how the arguments have been munged are kept in t->vars[]. 1390 * The contents of t->vars[] are stuffed strings (so they can be treated 1391 * like all other t->vars[]) in which the second character is the one that 1392 * is examined. The DB_* defines are the values for these second characters. 1393 */ 1394 #define DB_NORM 1 /* normal argument */ 1395 #define DB_OR 2 /* || -> -o conversion */ 1396 #define DB_AND 3 /* && -> -a conversion */ 1397 #define DB_BE 4 /* an inserted -BE */ 1398 #define DB_PAT 5 /* a pattern argument */ 1399 1400 #define X_EXTRA 20 /* this many extra bytes in X string */ 1401 1402 typedef struct XString { 1403 char *end, *beg; /* end, begin of string */ 1404 size_t len; /* length */ 1405 Area *areap; /* area to allocate/free from */ 1406 } XString; 1407 1408 typedef char *XStringP; 1409 1410 /* initialise expandable string */ 1411 #define XinitN(xs, length, area) do { \ 1412 (xs).len = (length); \ 1413 (xs).areap = (area); \ 1414 (xs).beg = alloc((xs).len + X_EXTRA, (xs).areap); \ 1415 (xs).end = (xs).beg + (xs).len; \ 1416 } while (/* CONSTCOND */ 0) 1417 #define Xinit(xs, xp, length, area) do { \ 1418 XinitN((xs), (length), (area)); \ 1419 (xp) = (xs).beg; \ 1420 } while (/* CONSTCOND */ 0) 1421 1422 /* stuff char into string */ 1423 #define Xput(xs, xp, c) (*xp++ = (c)) 1424 1425 /* check if there are at least n bytes left */ 1426 #define XcheckN(xs, xp, n) do { \ 1427 ssize_t more = ((xp) + (n)) - (xs).end; \ 1428 if (more > 0) \ 1429 (xp) = Xcheck_grow(&(xs), (xp), more); \ 1430 } while (/* CONSTCOND */ 0) 1431 1432 /* check for overflow, expand string */ 1433 #define Xcheck(xs, xp) XcheckN((xs), (xp), 1) 1434 1435 /* free string */ 1436 #define Xfree(xs, xp) afree((xs).beg, (xs).areap) 1437 1438 /* close, return string */ 1439 #define Xclose(xs, xp) aresize((xs).beg, (xp) - (xs).beg, (xs).areap) 1440 1441 /* begin of string */ 1442 #define Xstring(xs, xp) ((xs).beg) 1443 1444 #define Xnleft(xs, xp) ((xs).end - (xp)) /* may be less than 0 */ 1445 #define Xlength(xs, xp) ((xp) - (xs).beg) 1446 #define Xsize(xs, xp) ((xs).end - (xs).beg) 1447 #define Xsavepos(xs, xp) ((xp) - (xs).beg) 1448 #define Xrestpos(xs, xp, n) ((xs).beg + (n)) 1449 1450 char *Xcheck_grow(XString *, const char *, size_t); 1451 1452 /* 1453 * expandable vector of generic pointers 1454 */ 1455 1456 typedef struct { 1457 /* begin of allocated area */ 1458 void **beg; 1459 /* currently used number of entries */ 1460 size_t len; 1461 /* allocated number of entries */ 1462 size_t siz; 1463 } XPtrV; 1464 1465 #define XPinit(x, n) do { \ 1466 (x).siz = (n); \ 1467 (x).len = 0; \ 1468 (x).beg = alloc2((x).siz, sizeof(void *), ATEMP); \ 1469 } while (/* CONSTCOND */ 0) \ 1470 1471 #define XPput(x, p) do { \ 1472 if ((x).len == (x).siz) { \ 1473 (x).beg = aresize2((x).beg, (x).siz, \ 1474 2 * sizeof(void *), ATEMP); \ 1475 (x).siz <<= 1; \ 1476 } \ 1477 (x).beg[(x).len++] = (p); \ 1478 } while (/* CONSTCOND */ 0) 1479 1480 #define XPptrv(x) ((x).beg) 1481 #define XPsize(x) ((x).len) 1482 #define XPclose(x) aresize2((x).beg, XPsize(x), sizeof(void *), ATEMP) 1483 #define XPfree(x) afree((x).beg, ATEMP) 1484 1485 /* 1486 * Lexer internals 1487 */ 1488 1489 typedef struct source Source; 1490 struct source { 1491 const char *str; /* input pointer */ 1492 const char *start; /* start of current buffer */ 1493 union { 1494 const char **strv; /* string [] */ 1495 struct shf *shf; /* shell file */ 1496 struct tbl *tblp; /* alias (SF_HASALIAS) */ 1497 char *freeme; /* also for SREREAD */ 1498 } u; 1499 const char *file; /* input file name */ 1500 int type; /* input type */ 1501 int line; /* line number */ 1502 int errline; /* line the error occurred on (0 if not set) */ 1503 int flags; /* SF_* */ 1504 Area *areap; 1505 Source *next; /* stacked source */ 1506 XString xs; /* input buffer */ 1507 char ugbuf[2]; /* buffer for ungetsc() (SREREAD) and 1508 * alias (SALIAS) */ 1509 }; 1510 1511 /* Source.type values */ 1512 #define SEOF 0 /* input EOF */ 1513 #define SFILE 1 /* file input */ 1514 #define SSTDIN 2 /* read stdin */ 1515 #define SSTRING 3 /* string */ 1516 #define SWSTR 4 /* string without \n */ 1517 #define SWORDS 5 /* string[] */ 1518 #define SWORDSEP 6 /* string[] separator */ 1519 #define SALIAS 7 /* alias expansion */ 1520 #define SREREAD 8 /* read ahead to be re-scanned */ 1521 #define SSTRINGCMDLINE 9 /* string from "mksh -c ..." */ 1522 1523 /* Source.flags values */ 1524 #define SF_ECHO BIT(0) /* echo input to shlout */ 1525 #define SF_ALIAS BIT(1) /* faking space at end of alias */ 1526 #define SF_ALIASEND BIT(2) /* faking space at end of alias */ 1527 #define SF_TTY BIT(3) /* type == SSTDIN & it is a tty */ 1528 #define SF_HASALIAS BIT(4) /* u.tblp valid (SALIAS, SEOF) */ 1529 #if !defined(MKSH_SMALL) 1530 #define SF_MAYEXEC BIT(5) /* special sh -c optimisation hack */ 1531 #endif 1532 1533 typedef union { 1534 int i; 1535 char *cp; 1536 char **wp; 1537 struct op *o; 1538 struct ioword *iop; 1539 } YYSTYPE; 1540 1541 /* If something is added here, add it to tokentab[] in syn.c as well */ 1542 #define LWORD 256 1543 #define LOGAND 257 /* && */ 1544 #define LOGOR 258 /* || */ 1545 #define BREAK 259 /* ;; */ 1546 #define IF 260 1547 #define THEN 261 1548 #define ELSE 262 1549 #define ELIF 263 1550 #define FI 264 1551 #define CASE 265 1552 #define ESAC 266 1553 #define FOR 267 1554 #define SELECT 268 1555 #define WHILE 269 1556 #define UNTIL 270 1557 #define DO 271 1558 #define DONE 272 1559 #define IN 273 1560 #define FUNCTION 274 1561 #define TIME 275 1562 #define REDIR 276 1563 #define MDPAREN 277 /* (( )) */ 1564 #define BANG 278 /* ! */ 1565 #define DBRACKET 279 /* [[ .. ]] */ 1566 #define COPROC 280 /* |& */ 1567 #define BRKEV 281 /* ;| */ 1568 #define BRKFT 282 /* ;& */ 1569 #define YYERRCODE 300 1570 1571 /* flags to yylex */ 1572 #define CONTIN BIT(0) /* skip new lines to complete command */ 1573 #define ONEWORD BIT(1) /* single word for substitute() */ 1574 #define ALIAS BIT(2) /* recognise alias */ 1575 #define KEYWORD BIT(3) /* recognise keywords */ 1576 #define LETEXPR BIT(4) /* get expression inside (( )) */ 1577 #define VARASN BIT(5) /* check for var=word */ 1578 #define ARRAYVAR BIT(6) /* parse x[1 & 2] as one word */ 1579 #define ESACONLY BIT(7) /* only accept esac keyword */ 1580 #define CMDWORD BIT(8) /* parsing simple command (alias related) */ 1581 #define HEREDELIM BIT(9) /* parsing <<,<<- delimiter */ 1582 #define LQCHAR BIT(10) /* source string contains QCHAR */ 1583 #define HEREDOC BIT(11) /* parsing a here document body */ 1584 1585 #define HERES 10 /* max number of << in line */ 1586 1587 #undef CTRL 1588 #define CTRL(x) ((x) == '?' ? 0x7F : (x) & 0x1F) /* ASCII */ 1589 #define UNCTRL(x) ((x) ^ 0x40) /* ASCII */ 1590 1591 #define IDENT 64 1592 1593 EXTERN Source *source; /* yyparse/yylex source */ 1594 EXTERN YYSTYPE yylval; /* result from yylex */ 1595 EXTERN struct ioword *heres[HERES], **herep; 1596 EXTERN char ident[IDENT + 1]; 1597 1598 EXTERN char **history; /* saved commands */ 1599 EXTERN char **histptr; /* last history item */ 1600 EXTERN mksh_ari_t histsize; /* history size */ 1601 1602 /* user and system time of last j_waitjed job */ 1603 EXTERN struct timeval j_usrtime, j_systime; 1604 1605 #define notok2mul(max, val, c) (((val) != 0) && ((c) != 0) && \ 1606 (((max) / (c)) < (val))) 1607 #define notok2add(max, val, c) ((val) > ((max) - (c))) 1608 #define notoktomul(val, cnst) notok2mul(SIZE_MAX, (val), (cnst)) 1609 #define notoktoadd(val, cnst) notok2add(SIZE_MAX, (val), (cnst)) 1610 #define checkoktoadd(val, cnst) do { \ 1611 if (notoktoadd((val), (cnst))) \ 1612 internal_errorf(Tintovfl, (size_t)(val), \ 1613 '+', (size_t)(cnst)); \ 1614 } while (/* CONSTCOND */ 0) 1615 1616 1617 /* NZAAT hash based on Bob Jenkins' one-at-a-time hash */ 1618 1619 /* From: src/kern/include/nzat.h,v 1.2 2011/07/18 00:35:40 tg Exp $ */ 1620 1621 #define NZATInit(h) do { \ 1622 (h) = 0; \ 1623 } while (/* CONSTCOND */ 0) 1624 1625 #define NZATUpdateByte(h,b) do { \ 1626 (h) += (uint8_t)(b); \ 1627 ++(h); \ 1628 (h) += (h) << 10; \ 1629 (h) ^= (h) >> 6; \ 1630 } while (/* CONSTCOND */ 0) 1631 1632 #define NZATUpdateMem(h,p,z) do { \ 1633 register const uint8_t *NZATUpdateMem_p; \ 1634 register size_t NZATUpdateMem_z = (z); \ 1635 \ 1636 NZATUpdateMem_p = (const void *)(p); \ 1637 while (NZATUpdateMem_z--) \ 1638 NZATUpdateByte((h), *NZATUpdateMem_p++); \ 1639 } while (/* CONSTCOND */ 0) 1640 1641 #define NZATUpdateString(h,s) do { \ 1642 register const char *NZATUpdateString_s; \ 1643 register uint8_t NZATUpdateString_c; \ 1644 \ 1645 NZATUpdateString_s = (const void *)(s); \ 1646 while ((NZATUpdateString_c = *NZATUpdateString_s++)) \ 1647 NZATUpdateByte((h), NZATUpdateString_c); \ 1648 } while (/* CONSTCOND */ 0) 1649 1650 #define NZAATFinish(h) do { \ 1651 (h) += (h) << 10; \ 1652 (h) ^= (h) >> 6; \ 1653 (h) += (h) << 3; \ 1654 (h) ^= (h) >> 11; \ 1655 (h) += (h) << 15; \ 1656 } while (/* CONSTCOND */ 0) 1657 1658 1659 /* lalloc.c */ 1660 void ainit(Area *); 1661 void afreeall(Area *); 1662 /* these cannot fail and can take NULL (not for ap) */ 1663 #define alloc(n, ap) aresize(NULL, (n), (ap)) 1664 #define alloc2(m, n, ap) aresize2(NULL, (m), (n), (ap)) 1665 void *aresize(void *, size_t, Area *); 1666 void *aresize2(void *, size_t, size_t, Area *); 1667 void afree(void *, Area *); /* can take NULL */ 1668 /* edit.c */ 1669 #ifndef MKSH_NO_CMDLINE_EDITING 1670 #ifndef MKSH_SMALL 1671 int x_bind(const char *, const char *, bool, bool); 1672 #else 1673 int x_bind(const char *, const char *, bool); 1674 #endif 1675 void x_init(void); 1676 #ifdef DEBUG_LEAKS 1677 void x_done(void); 1678 #endif 1679 int x_read(char *); 1680 #endif 1681 void x_mkraw(int, mksh_ttyst *, bool); 1682 /* eval.c */ 1683 char *substitute(const char *, int); 1684 char **eval(const char **, int); 1685 char *evalstr(const char *cp, int); 1686 char *evalonestr(const char *cp, int); 1687 char *debunk(char *, const char *, size_t); 1688 void expand(const char *, XPtrV *, int); 1689 int glob_str(char *, XPtrV *, bool); 1690 char *tilde(char *); 1691 /* exec.c */ 1692 int execute(struct op * volatile, volatile int, volatile int * volatile); 1693 int shcomexec(const char **); 1694 struct tbl *findfunc(const char *, uint32_t, bool); 1695 int define(const char *, struct op *); 1696 const char *builtin(const char *, int (*)(const char **)); 1697 struct tbl *findcom(const char *, int); 1698 void flushcom(bool); 1699 const char *search_path(const char *, const char *, int, int *); 1700 void pr_menu(const char * const *); 1701 void pr_list(char * const *); 1702 /* expr.c */ 1703 int evaluate(const char *, mksh_ari_t *, int, bool); 1704 int v_evaluate(struct tbl *, const char *, volatile int, bool); 1705 /* UTF-8 stuff */ 1706 size_t utf_mbtowc(unsigned int *, const char *); 1707 size_t utf_wctomb(char *, unsigned int); 1708 int utf_widthadj(const char *, const char **); 1709 size_t utf_mbswidth(const char *); 1710 const char *utf_skipcols(const char *, int); 1711 size_t utf_ptradj(const char *); 1712 #ifndef MKSH_mirbsd_wcwidth 1713 int utf_wcwidth(unsigned int); 1714 #endif 1715 int ksh_access(const char *, int); 1716 struct tbl *tempvar(void); 1717 /* funcs.c */ 1718 int c_hash(const char **); 1719 int c_pwd(const char **); 1720 int c_print(const char **); 1721 #ifdef MKSH_PRINTF_BUILTIN 1722 int c_printf(const char **); 1723 #endif 1724 int c_whence(const char **); 1725 int c_command(const char **); 1726 int c_typeset(const char **); 1727 int c_alias(const char **); 1728 int c_unalias(const char **); 1729 int c_let(const char **); 1730 int c_jobs(const char **); 1731 #ifndef MKSH_UNEMPLOYED 1732 int c_fgbg(const char **); 1733 #endif 1734 int c_kill(const char **); 1735 void getopts_reset(int); 1736 int c_getopts(const char **); 1737 #ifndef MKSH_NO_CMDLINE_EDITING 1738 int c_bind(const char **); 1739 #endif 1740 int c_shift(const char **); 1741 int c_umask(const char **); 1742 int c_dot(const char **); 1743 int c_wait(const char **); 1744 int c_read(const char **); 1745 int c_eval(const char **); 1746 int c_trap(const char **); 1747 int c_brkcont(const char **); 1748 int c_exitreturn(const char **); 1749 int c_set(const char **); 1750 int c_unset(const char **); 1751 int c_ulimit(const char **); 1752 int c_times(const char **); 1753 int timex(struct op *, int, volatile int *); 1754 void timex_hook(struct op *, char ** volatile *); 1755 int c_exec(const char **); 1756 /* dummy function (just need pointer value), special case in comexec() */ 1757 #define c_builtin shcomexec 1758 int c_test(const char **); 1759 #if HAVE_MKNOD 1760 int c_mknod(const char **); 1761 #endif 1762 int c_realpath(const char **); 1763 int c_rename(const char **); 1764 int c_cat(const char **); 1765 int c_sleep(const char **); 1766 /* histrap.c */ 1767 void init_histvec(void); 1768 void hist_init(Source *); 1769 #if HAVE_PERSISTENT_HISTORY 1770 void hist_finish(void); 1771 #endif 1772 void histsave(int *, const char *, bool, bool); 1773 #if !defined(MKSH_SMALL) && HAVE_PERSISTENT_HISTORY 1774 bool histsync(void); 1775 #endif 1776 int c_fc(const char **); 1777 void sethistsize(mksh_ari_t); 1778 #if HAVE_PERSISTENT_HISTORY 1779 void sethistfile(const char *); 1780 #endif 1781 #if !defined(MKSH_NO_CMDLINE_EDITING) && !MKSH_S_NOVI 1782 char **histpos(void); 1783 int histnum(int); 1784 #endif 1785 int findhist(int, int, const char *, int); 1786 char **hist_get_newest(bool); 1787 void inittraps(void); 1788 void alarm_init(void); 1789 Trap *gettrap(const char *, bool); 1790 void trapsig(int); 1791 void intrcheck(void); 1792 int fatal_trap_check(void); 1793 int trap_pending(void); 1794 void runtraps(int intr); 1795 void runtrap(Trap *, bool); 1796 void cleartraps(void); 1797 void restoresigs(void); 1798 void settrap(Trap *, const char *); 1799 int block_pipe(void); 1800 void restore_pipe(int); 1801 int setsig(Trap *, sig_t, int); 1802 void setexecsig(Trap *, int); 1803 #if HAVE_FLOCK || HAVE_LOCK_FCNTL 1804 void mksh_lockfd(int); 1805 void mksh_unlkfd(int); 1806 #endif 1807 /* jobs.c */ 1808 void j_init(void); 1809 void j_exit(void); 1810 #ifndef MKSH_UNEMPLOYED 1811 void j_change(void); 1812 #endif 1813 int exchild(struct op *, int, volatile int *, int); 1814 void startlast(void); 1815 int waitlast(void); 1816 int waitfor(const char *, int *); 1817 int j_kill(const char *, int); 1818 #ifndef MKSH_UNEMPLOYED 1819 int j_resume(const char *, int); 1820 #endif 1821 int j_jobs(const char *, int, int); 1822 void j_notify(void); 1823 pid_t j_async(void); 1824 int j_stopped_running(void); 1825 /* lex.c */ 1826 int yylex(int); 1827 void yyskiputf8bom(void); 1828 void yyerror(const char *, ...) 1829 MKSH_A_NORETURN 1830 MKSH_A_FORMAT(__printf__, 1, 2); 1831 Source *pushs(int, Area *); 1832 void set_prompt(int, Source *); 1833 int pprompt(const char *, int); 1834 /* main.c */ 1835 int include(const char *, int, const char **, bool); 1836 int command(const char *, int); 1837 int shell(Source * volatile, volatile bool); 1838 /* argument MUST NOT be 0 */ 1839 void unwind(int) MKSH_A_NORETURN; 1840 void newenv(int); 1841 void quitenv(struct shf *); 1842 void cleanup_parents_env(void); 1843 void cleanup_proc_env(void); 1844 void errorf(const char *, ...) 1845 MKSH_A_NORETURN 1846 MKSH_A_FORMAT(__printf__, 1, 2); 1847 void errorfx(int, const char *, ...) 1848 MKSH_A_NORETURN 1849 MKSH_A_FORMAT(__printf__, 2, 3); 1850 void warningf(bool, const char *, ...) 1851 MKSH_A_FORMAT(__printf__, 2, 3); 1852 void bi_errorf(const char *, ...) 1853 MKSH_A_FORMAT(__printf__, 1, 2); 1854 #define errorfz() errorf(NULL) 1855 #define errorfxz(rc) errorfx((rc), NULL) 1856 #define bi_errorfz() bi_errorf(NULL) 1857 void internal_errorf(const char *, ...) 1858 MKSH_A_NORETURN 1859 MKSH_A_FORMAT(__printf__, 1, 2); 1860 void internal_warningf(const char *, ...) 1861 MKSH_A_FORMAT(__printf__, 1, 2); 1862 void error_prefix(bool); 1863 void shellf(const char *, ...) 1864 MKSH_A_FORMAT(__printf__, 1, 2); 1865 void shprintf(const char *, ...) 1866 MKSH_A_FORMAT(__printf__, 1, 2); 1867 int can_seek(int); 1868 void initio(void); 1869 int ksh_dup2(int, int, bool); 1870 short savefd(int); 1871 void restfd(int, int); 1872 void openpipe(int *); 1873 void closepipe(int *); 1874 int check_fd(const char *, int, const char **); 1875 void coproc_init(void); 1876 void coproc_read_close(int); 1877 void coproc_readw_close(int); 1878 void coproc_write_close(int); 1879 int coproc_getfd(int, const char **); 1880 void coproc_cleanup(int); 1881 struct temp *maketemp(Area *, Temp_type, struct temp **); 1882 void ktinit(Area *, struct table *, uint8_t); 1883 struct tbl *ktscan(struct table *, const char *, uint32_t, struct tbl ***); 1884 /* table, name (key) to search for, hash(n) */ 1885 #define ktsearch(tp, s, h) ktscan((tp), (s), (h), NULL) 1886 struct tbl *ktenter(struct table *, const char *, uint32_t); 1887 #define ktdelete(p) do { p->flag = 0; } while (/* CONSTCOND */ 0) 1888 void ktwalk(struct tstate *, struct table *); 1889 struct tbl *ktnext(struct tstate *); 1890 struct tbl **ktsort(struct table *); 1891 #ifdef DF 1892 void DF(const char *, ...) 1893 MKSH_A_FORMAT(__printf__, 1, 2); 1894 #endif 1895 /* misc.c */ 1896 void setctypes(const char *, int); 1897 void initctypes(void); 1898 size_t option(const char *); 1899 char *getoptions(void); 1900 void change_flag(enum sh_flag, int, bool); 1901 void change_xtrace(unsigned char, bool); 1902 int parse_args(const char **, int, bool *); 1903 int getn(const char *, int *); 1904 int gmatchx(const char *, const char *, bool); 1905 int has_globbing(const char *, const char *); 1906 int xstrcmp(const void *, const void *); 1907 void ksh_getopt_reset(Getopt *, int); 1908 int ksh_getopt(const char **, Getopt *, const char *); 1909 void print_value_quoted(struct shf *, const char *); 1910 char *quote_value(const char *); 1911 void print_columns(struct shf *, unsigned int, 1912 char *(*)(char *, size_t, unsigned int, const void *), 1913 const void *, size_t, size_t, bool); 1914 void strip_nuls(char *, int); 1915 ssize_t blocking_read(int, char *, size_t) 1916 MKSH_A_BOUNDED(__buffer__, 2, 3); 1917 int reset_nonblock(int); 1918 char *ksh_get_wd(void); 1919 char *do_realpath(const char *); 1920 void simplify_path(char *); 1921 void set_current_wd(const char *); 1922 int c_cd(const char **); 1923 #if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST) 1924 char *strdup_i(const char *, Area *); 1925 char *strndup_i(const char *, size_t, Area *); 1926 #endif 1927 int unbksl(bool, int (*)(void), void (*)(int)); 1928 /* shf.c */ 1929 struct shf *shf_open(const char *, int, int, int); 1930 struct shf *shf_fdopen(int, int, struct shf *); 1931 struct shf *shf_reopen(int, int, struct shf *); 1932 struct shf *shf_sopen(char *, ssize_t, int, struct shf *); 1933 int shf_close(struct shf *); 1934 int shf_fdclose(struct shf *); 1935 char *shf_sclose(struct shf *); 1936 int shf_flush(struct shf *); 1937 ssize_t shf_read(char *, ssize_t, struct shf *); 1938 char *shf_getse(char *, ssize_t, struct shf *); 1939 int shf_getchar(struct shf *s); 1940 int shf_ungetc(int, struct shf *); 1941 #if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST) 1942 int shf_getc(struct shf *); 1943 int shf_putc(int, struct shf *); 1944 #else 1945 #define shf_getc shf_getc_i 1946 #define shf_putc shf_putc_i 1947 #endif 1948 int shf_putchar(int, struct shf *); 1949 ssize_t shf_puts(const char *, struct shf *); 1950 ssize_t shf_write(const char *, ssize_t, struct shf *); 1951 ssize_t shf_fprintf(struct shf *, const char *, ...) 1952 MKSH_A_FORMAT(__printf__, 2, 3); 1953 ssize_t shf_snprintf(char *, ssize_t, const char *, ...) 1954 MKSH_A_FORMAT(__printf__, 3, 4) 1955 MKSH_A_BOUNDED(__string__, 1, 2); 1956 char *shf_smprintf(const char *, ...) 1957 MKSH_A_FORMAT(__printf__, 1, 2); 1958 ssize_t shf_vfprintf(struct shf *, const char *, va_list) 1959 MKSH_A_FORMAT(__printf__, 2, 0); 1960 /* syn.c */ 1961 void initkeywords(void); 1962 struct op *compile(Source *, bool); 1963 bool parse_usec(const char *, struct timeval *); 1964 char *yyrecursive(int); 1965 void yyrecursive_pop(bool); 1966 /* tree.c */ 1967 void fptreef(struct shf *, int, const char *, ...); 1968 char *snptreef(char *, ssize_t, const char *, ...); 1969 struct op *tcopy(struct op *, Area *); 1970 char *wdcopy(const char *, Area *); 1971 const char *wdscan(const char *, int); 1972 #define WDS_TPUTS BIT(0) /* tputS (dumpwdvar) mode */ 1973 #define WDS_KEEPQ BIT(1) /* keep quote characters */ 1974 #define WDS_MAGIC BIT(2) /* make MAGIC */ 1975 char *wdstrip(const char *, int); 1976 void tfree(struct op *, Area *); 1977 void dumpchar(struct shf *, int); 1978 void dumptree(struct shf *, struct op *); 1979 void dumpwdvar(struct shf *, const char *); 1980 void dumpioact(struct shf *shf, struct op *t); 1981 void vistree(char *, size_t, struct op *) 1982 MKSH_A_BOUNDED(__string__, 1, 2); 1983 void fpFUNCTf(struct shf *, int, bool, const char *, struct op *); 1984 /* var.c */ 1985 void newblock(void); 1986 void popblock(void); 1987 void initvar(void); 1988 struct block *varsearch(struct block *, struct tbl **, const char *, uint32_t); 1989 struct tbl *global(const char *); 1990 struct tbl *local(const char *, bool); 1991 char *str_val(struct tbl *); 1992 int setstr(struct tbl *, const char *, int); 1993 struct tbl *setint_v(struct tbl *, struct tbl *, bool); 1994 void setint(struct tbl *, mksh_ari_t); 1995 void setint_n(struct tbl *, mksh_ari_t, int); 1996 struct tbl *typeset(const char *, uint32_t, uint32_t, int, int); 1997 void unset(struct tbl *, int); 1998 const char *skip_varname(const char *, int); 1999 const char *skip_wdvarname(const char *, bool); 2000 int is_wdvarname(const char *, bool); 2001 int is_wdvarassign(const char *); 2002 struct tbl *arraysearch(struct tbl *, uint32_t); 2003 char **makenv(void); 2004 void change_winsz(void); 2005 size_t array_ref_len(const char *); 2006 char *arrayname(const char *); 2007 mksh_uari_t set_array(const char *, bool, const char **); 2008 uint32_t hash(const void *); 2009 mksh_ari_t rndget(void); 2010 void rndset(unsigned long); 2011 2012 enum Test_op { 2013 /* non-operator */ 2014 TO_NONOP = 0, 2015 /* unary operators */ 2016 TO_STNZE, TO_STZER, TO_OPTION, 2017 TO_FILAXST, 2018 TO_FILEXST, 2019 TO_FILREG, TO_FILBDEV, TO_FILCDEV, TO_FILSYM, TO_FILFIFO, TO_FILSOCK, 2020 TO_FILCDF, TO_FILID, TO_FILGID, TO_FILSETG, TO_FILSTCK, TO_FILUID, 2021 TO_FILRD, TO_FILGZ, TO_FILTT, TO_FILSETU, TO_FILWR, TO_FILEX, 2022 /* binary operators */ 2023 TO_STEQL, TO_STNEQ, TO_STLT, TO_STGT, TO_INTEQ, TO_INTNE, TO_INTGT, 2024 TO_INTGE, TO_INTLT, TO_INTLE, TO_FILEQ, TO_FILNT, TO_FILOT, 2025 /* not an operator */ 2026 TO_NONNULL /* !TO_NONOP */ 2027 }; 2028 typedef enum Test_op Test_op; 2029 2030 /* Used by Test_env.isa() (order important - used to index *_tokens[] arrays) */ 2031 enum Test_meta { 2032 TM_OR, /* -o or || */ 2033 TM_AND, /* -a or && */ 2034 TM_NOT, /* ! */ 2035 TM_OPAREN, /* ( */ 2036 TM_CPAREN, /* ) */ 2037 TM_UNOP, /* unary operator */ 2038 TM_BINOP, /* binary operator */ 2039 TM_END /* end of input */ 2040 }; 2041 typedef enum Test_meta Test_meta; 2042 2043 #define TEF_ERROR BIT(0) /* set if we've hit an error */ 2044 #define TEF_DBRACKET BIT(1) /* set if [[ .. ]] test */ 2045 2046 typedef struct test_env { 2047 union { 2048 const char **wp; /* used by ptest_* */ 2049 XPtrV *av; /* used by dbtestp_* */ 2050 } pos; 2051 const char **wp_end; /* used by ptest_* */ 2052 Test_op (*isa)(struct test_env *, Test_meta); 2053 const char *(*getopnd) (struct test_env *, Test_op, bool); 2054 int (*eval)(struct test_env *, Test_op, const char *, const char *, bool); 2055 void (*error)(struct test_env *, int, const char *); 2056 int flags; /* TEF_* */ 2057 } Test_env; 2058 2059 extern const char * const dbtest_tokens[]; 2060 2061 Test_op test_isop(Test_meta, const char *); 2062 int test_eval(Test_env *, Test_op, const char *, const char *, bool); 2063 int test_parse(Test_env *); 2064 2065 EXTERN int tty_fd E_INIT(-1); /* dup'd tty file descriptor */ 2066 EXTERN bool tty_devtty; /* true if tty_fd is from /dev/tty */ 2067 EXTERN mksh_ttyst tty_state; /* saved tty state */ 2068 EXTERN bool tty_hasstate; /* true if tty_state is valid */ 2069 2070 extern int tty_init_fd(void); /* initialise tty_fd, tty_devtty */ 2071 2072 /* be sure not to interfere with anyone else's idea about EXTERN */ 2073 #ifdef EXTERN_DEFINED 2074 # undef EXTERN_DEFINED 2075 # undef EXTERN 2076 #endif 2077 #undef E_INIT 2078 2079 #endif /* !MKSH_INCLUDES_ONLY */ 2080