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