1 /* 2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 3 * Copyright 2019 OmniOS Community Edition (OmniOSce) Association. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/types.h> 29 #include <sys/stat.h> 30 #include <fcntl.h> 31 #include <errno.h> 32 #include <stdlib.h> 33 #include <stdio.h> 34 #include <string.h> 35 #include <strings.h> 36 #include <limits.h> 37 #include <unistd.h> 38 #include <dirent.h> 39 #include <macros.h> 40 #include <sys/systeminfo.h> 41 #include <sys/queue.h> 42 #include <sys/mnttab.h> 43 #include "gfx_fb.h" 44 #include "ficl.h" 45 46 /* Commands and return values; nonzero return sets command_errmsg != NULL */ 47 typedef int (bootblk_cmd_t)(int argc, char *argv[]); 48 #define CMD_OK 0 49 #define CMD_ERROR 1 50 51 /* 52 * Support for commands 53 */ 54 struct bootblk_command 55 { 56 const char *c_name; 57 const char *c_desc; 58 bootblk_cmd_t *c_fn; 59 STAILQ_ENTRY(bootblk_command) next; 60 }; 61 62 #define MDIR_REMOVED 0x0001 63 #define MDIR_NOHINTS 0x0002 64 65 struct moduledir { 66 char *d_path; /* path of modules directory */ 67 uchar_t *d_hints; /* content of linker.hints file */ 68 int d_hintsz; /* size of hints data */ 69 int d_flags; 70 STAILQ_ENTRY(moduledir) d_link; 71 }; 72 static STAILQ_HEAD(, moduledir) moduledir_list = 73 STAILQ_HEAD_INITIALIZER(moduledir_list); 74 75 static const char *default_searchpath = "/platform/i86pc"; 76 77 static char typestr[] = "?fc?d?b? ?l?s?w"; 78 static int ls_getdir(char **pathp); 79 extern char **_environ; 80 81 char *command_errmsg; 82 char command_errbuf[256]; 83 84 extern void pager_open(void); 85 extern void pager_close(void); 86 extern int pager_output(const char *); 87 extern int pager_file(const char *); 88 static int page_file(char *); 89 static int include(const char *); 90 91 static int command_help(int argc, char *argv[]); 92 static int command_commandlist(int argc, char *argv[]); 93 static int command_show(int argc, char *argv[]); 94 static int command_set(int argc, char *argv[]); 95 static int command_setprop(int argc, char *argv[]); 96 static int command_unset(int argc, char *argv[]); 97 static int command_echo(int argc, char *argv[]); 98 static int command_read(int argc, char *argv[]); 99 static int command_more(int argc, char *argv[]); 100 static int command_ls(int argc, char *argv[]); 101 static int command_include(int argc, char *argv[]); 102 static int command_autoboot(int argc, char *argv[]); 103 static int command_boot(int argc, char *argv[]); 104 static int command_unload(int argc, char *argv[]); 105 static int command_load(int argc, char *argv[]); 106 static int command_reboot(int argc, char *argv[]); 107 static int command_sifting(int argc, char *argv[]); 108 static int command_framebuffer(int argc, char *argv[]); 109 110 #define BF_PARSE 100 111 #define BF_DICTSIZE 30000 112 113 /* update when loader version will change */ 114 static const char bootprog_rev[] = "1.1"; 115 STAILQ_HEAD(cmdh, bootblk_command) commands; 116 117 /* 118 * BootForth Interface to Ficl Forth interpreter. 119 */ 120 121 ficlSystem *bf_sys; 122 ficlVm *bf_vm; 123 124 /* 125 * Redistribution and use in source and binary forms, with or without 126 * modification, are permitted provided that the following conditions 127 * are met: 128 * 1. Redistributions of source code must retain the above copyright 129 * notice, this list of conditions and the following disclaimer. 130 * 2. Redistributions in binary form must reproduce the above copyright 131 * notice, this list of conditions and the following disclaimer in the 132 * documentation and/or other materials provided with the distribution. 133 * 134 * Jordan K. Hubbard 135 * 29 August 1998 136 * 137 * The meat of the simple parser. 138 */ 139 140 static void clean(void); 141 static int insert(int *argcp, char *buf); 142 143 #define PARSE_BUFSIZE 1024 /* maximum size of one element */ 144 #define MAXARGS 20 /* maximum number of elements */ 145 static char *args[MAXARGS]; 146 147 #define DIGIT(x) \ 148 (isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 149 150 /* 151 * backslash: Return malloc'd copy of str with all standard "backslash 152 * processing" done on it. Original can be free'd if desired. 153 */ 154 char * 155 backslash(char *str) 156 { 157 /* 158 * Remove backslashes from the strings. Turn \040 etc. into a single 159 * character (we allow eight bit values). Currently NUL is not 160 * allowed. 161 * 162 * Turn "\n" and "\t" into '\n' and '\t' characters. Etc. 163 */ 164 char *new_str; 165 int seenbs = 0; 166 int i = 0; 167 168 if ((new_str = strdup(str)) == NULL) 169 return (NULL); 170 171 while (*str) { 172 if (seenbs) { 173 seenbs = 0; 174 switch (*str) { 175 case '\\': 176 new_str[i++] = '\\'; 177 str++; 178 break; 179 180 /* preserve backslashed quotes, dollar signs */ 181 case '\'': 182 case '"': 183 case '$': 184 new_str[i++] = '\\'; 185 new_str[i++] = *str++; 186 break; 187 188 case 'b': 189 new_str[i++] = '\b'; 190 str++; 191 break; 192 193 case 'f': 194 new_str[i++] = '\f'; 195 str++; 196 break; 197 198 case 'r': 199 new_str[i++] = '\r'; 200 str++; 201 break; 202 203 case 'n': 204 new_str[i++] = '\n'; 205 str++; 206 break; 207 208 case 's': 209 new_str[i++] = ' '; 210 str++; 211 break; 212 213 case 't': 214 new_str[i++] = '\t'; 215 str++; 216 break; 217 218 case 'v': 219 new_str[i++] = '\13'; 220 str++; 221 break; 222 223 case 'z': 224 str++; 225 break; 226 227 case '0': case '1': case '2': case '3': case '4': 228 case '5': case '6': case '7': case '8': case '9': { 229 char val; 230 231 /* Three digit octal constant? */ 232 if (*str >= '0' && *str <= '3' && 233 *(str + 1) >= '0' && *(str + 1) <= '7' && 234 *(str + 2) >= '0' && *(str + 2) <= '7') { 235 236 val = (DIGIT(*str) << 6) + 237 (DIGIT(*(str + 1)) << 3) + 238 DIGIT(*(str + 2)); 239 240 /* 241 * Allow null value if user really 242 * wants to shoot at feet, but beware! 243 */ 244 new_str[i++] = val; 245 str += 3; 246 break; 247 } 248 249 /* 250 * One or two digit hex constant? 251 * If two are there they will both be taken. 252 * Use \z to split them up if this is not 253 * wanted. 254 */ 255 if (*str == '0' && 256 (*(str + 1) == 'x' || *(str + 1) == 'X') && 257 isxdigit(*(str + 2))) { 258 val = DIGIT(*(str + 2)); 259 if (isxdigit(*(str + 3))) { 260 val = (val << 4) + 261 DIGIT(*(str + 3)); 262 str += 4; 263 } else 264 str += 3; 265 /* Yep, allow null value here too */ 266 new_str[i++] = val; 267 break; 268 } 269 } 270 break; 271 272 default: 273 new_str[i++] = *str++; 274 break; 275 } 276 } else { 277 if (*str == '\\') { 278 seenbs = 1; 279 str++; 280 } else 281 new_str[i++] = *str++; 282 } 283 } 284 285 if (seenbs) { 286 /* 287 * The final character was a '\'. 288 * Put it in as a single backslash. 289 */ 290 new_str[i++] = '\\'; 291 } 292 new_str[i] = '\0'; 293 return (new_str); 294 } 295 296 /* 297 * parse: accept a string of input and "parse" it for backslash 298 * substitutions and environment variable expansions (${var}), 299 * returning an argc/argv style vector of whitespace separated 300 * arguments. Returns 0 on success, 1 on failure (ok, ok, so I 301 * wimped-out on the error codes! :). 302 * 303 * Note that the argv array returned must be freed by the caller, but 304 * we own the space allocated for arguments and will free that on next 305 * invocation. This allows argv consumers to modify the array if 306 * required. 307 * 308 * NB: environment variables that expand to more than one whitespace 309 * separated token will be returned as a single argv[] element, not 310 * split in turn. Expanded text is also immune to further backslash 311 * elimination or expansion since this is a one-pass, non-recursive 312 * parser. You didn't specify more than this so if you want more, ask 313 * me. - jkh 314 */ 315 316 #define PARSE_FAIL(expr) \ 317 if (expr) { \ 318 printf("fail at line %d\n", __LINE__); \ 319 clean(); \ 320 free(copy); \ 321 free(buf); \ 322 return (1); \ 323 } 324 325 /* Accept the usual delimiters for a variable, returning counterpart */ 326 static char 327 isdelim(int ch) 328 { 329 if (ch == '{') 330 return ('}'); 331 else if (ch == '(') 332 return (')'); 333 return ('\0'); 334 } 335 336 static int 337 isquote(int ch) 338 { 339 return (ch == '\''); 340 } 341 342 static int 343 isdquote(int ch) 344 { 345 return (ch == '"'); 346 } 347 348 int 349 parse(int *argc, char ***argv, char *str) 350 { 351 int ac; 352 char *val, *p, *q, *copy = NULL; 353 size_t i = 0; 354 char token, tmp, quote, dquote, *buf; 355 enum { STR, VAR, WHITE } state; 356 357 ac = *argc = 0; 358 dquote = quote = 0; 359 if (!str || (p = copy = backslash(str)) == NULL) 360 return (1); 361 362 /* Initialize vector and state */ 363 clean(); 364 state = STR; 365 buf = (char *)malloc(PARSE_BUFSIZE); 366 token = 0; 367 368 /* And awaaaaaaaaay we go! */ 369 while (*p) { 370 switch (state) { 371 case STR: 372 if ((*p == '\\') && p[1]) { 373 p++; 374 PARSE_FAIL(i == (PARSE_BUFSIZE - 1)); 375 buf[i++] = *p++; 376 } else if (isquote(*p)) { 377 quote = quote ? 0 : *p; 378 if (dquote) { /* keep quote */ 379 PARSE_FAIL(i == (PARSE_BUFSIZE - 1)); 380 buf[i++] = *p++; 381 } else 382 ++p; 383 } else if (isdquote(*p)) { 384 dquote = dquote ? 0 : *p; 385 if (quote) { /* keep dquote */ 386 PARSE_FAIL(i == (PARSE_BUFSIZE - 1)); 387 buf[i++] = *p++; 388 } else 389 ++p; 390 } else if (isspace(*p) && !quote && !dquote) { 391 state = WHITE; 392 if (i) { 393 buf[i] = '\0'; 394 PARSE_FAIL(insert(&ac, buf)); 395 i = 0; 396 } 397 ++p; 398 } else if (*p == '$' && !quote) { 399 token = isdelim(*(p + 1)); 400 if (token) 401 p += 2; 402 else 403 ++p; 404 state = VAR; 405 } else { 406 PARSE_FAIL(i == (PARSE_BUFSIZE - 1)); 407 buf[i++] = *p++; 408 } 409 break; 410 411 case WHITE: 412 if (isspace(*p)) 413 ++p; 414 else 415 state = STR; 416 break; 417 418 case VAR: 419 if (token) { 420 PARSE_FAIL((q = strchr(p, token)) == NULL); 421 } else { 422 q = p; 423 while (*q && !isspace(*q)) 424 ++q; 425 } 426 tmp = *q; 427 *q = '\0'; 428 if ((val = getenv(p)) != NULL) { 429 size_t len = strlen(val); 430 431 strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1)); 432 i += min(len, PARSE_BUFSIZE - 1); 433 } 434 *q = tmp; /* restore value */ 435 p = q + (token ? 1 : 0); 436 state = STR; 437 break; 438 } 439 } 440 /* missing terminating ' or " */ 441 PARSE_FAIL(quote || dquote); 442 /* If at end of token, add it */ 443 if (i && state == STR) { 444 buf[i] = '\0'; 445 PARSE_FAIL(insert(&ac, buf)); 446 } 447 args[ac] = NULL; 448 *argc = ac; 449 *argv = (char **)malloc((sizeof (char *) * ac + 1)); 450 bcopy(args, *argv, sizeof (char *) * ac + 1); 451 free(buf); 452 free(copy); 453 return (0); 454 } 455 456 #define MAXARGS 20 457 458 /* Clean vector space */ 459 static void 460 clean(void) 461 { 462 int i; 463 464 for (i = 0; i < MAXARGS; i++) { 465 if (args[i] != NULL) { 466 free(args[i]); 467 args[i] = NULL; 468 } 469 } 470 } 471 472 static int 473 insert(int *argcp, char *buf) 474 { 475 if (*argcp >= MAXARGS) 476 return (1); 477 args[(*argcp)++] = strdup(buf); 478 return (0); 479 } 480 481 static char * 482 isadir(void) 483 { 484 char *buf; 485 size_t bufsize = 20; 486 int ret; 487 488 if ((buf = malloc(bufsize)) == NULL) 489 return (NULL); 490 ret = sysinfo(SI_ARCHITECTURE_K, buf, bufsize); 491 if (ret == -1) { 492 free(buf); 493 return (NULL); 494 } 495 return (buf); 496 } 497 498 /* 499 * Shim for taking commands from BF and passing them out to 'standard' 500 * argv/argc command functions. 501 */ 502 static void 503 bf_command(ficlVm *vm) 504 { 505 char *name, *line, *tail, *cp; 506 size_t len; 507 struct bootblk_command *cmdp; 508 bootblk_cmd_t *cmd; 509 int nstrings, i; 510 int argc, result; 511 char **argv; 512 513 /* Get the name of the current word */ 514 name = vm->runningWord->name; 515 516 /* Find our command structure */ 517 cmd = NULL; 518 STAILQ_FOREACH(cmdp, &commands, next) { 519 if ((cmdp->c_name != NULL) && strcmp(name, cmdp->c_name) == 0) 520 cmd = cmdp->c_fn; 521 } 522 if (cmd == NULL) 523 printf("callout for unknown command '%s'\n", name); 524 525 /* Check whether we have been compiled or are being interpreted */ 526 if (ficlStackPopInteger(ficlVmGetDataStack(vm))) { 527 /* 528 * Get parameters from stack, in the format: 529 * an un ... a2 u2 a1 u1 n -- 530 * Where n is the number of strings, a/u are pairs of 531 * address/size for strings, and they will be concatenated 532 * in LIFO order. 533 */ 534 nstrings = ficlStackPopInteger(ficlVmGetDataStack(vm)); 535 for (i = 0, len = 0; i < nstrings; i++) 536 len += ficlStackFetch(ficlVmGetDataStack(vm), i * 2).i + 1; 537 line = malloc(strlen(name) + len + 1); 538 strcpy(line, name); 539 540 if (nstrings) 541 for (i = 0; i < nstrings; i++) { 542 len = ficlStackPopInteger( 543 ficlVmGetDataStack(vm)); 544 cp = ficlStackPopPointer( 545 ficlVmGetDataStack(vm)); 546 strcat(line, " "); 547 strncat(line, cp, len); 548 } 549 } else { 550 /* Get remainder of invocation */ 551 tail = ficlVmGetInBuf(vm); 552 for (cp = tail, len = 0; 553 cp != vm->tib.end && *cp != 0 && *cp != '\n'; cp++, len++) 554 ; 555 556 line = malloc(strlen(name) + len + 2); 557 strcpy(line, name); 558 if (len > 0) { 559 strcat(line, " "); 560 strncat(line, tail, len); 561 ficlVmUpdateTib(vm, tail + len); 562 } 563 } 564 565 command_errmsg = command_errbuf; 566 command_errbuf[0] = 0; 567 if (!parse(&argc, &argv, line)) { 568 result = (cmd)(argc, argv); 569 free(argv); 570 } else { 571 result = BF_PARSE; 572 } 573 free(line); 574 /* 575 * If there was error during nested ficlExec(), we may no longer have 576 * valid environment to return. Throw all exceptions from here. 577 */ 578 if (result != 0) 579 ficlVmThrow(vm, result); 580 /* This is going to be thrown!!! */ 581 ficlStackPushInteger(ficlVmGetDataStack(vm), result); 582 } 583 584 static char * 585 get_currdev(void) 586 { 587 int ret; 588 char *currdev; 589 FILE *fp; 590 struct mnttab mpref = {0}; 591 struct mnttab mp = {0}; 592 593 mpref.mnt_mountp = "/"; 594 fp = fopen(MNTTAB, "r"); 595 596 /* do the best we can to return something... */ 597 if (fp == NULL) 598 return (strdup(":")); 599 600 ret = getmntany(fp, &mp, &mpref); 601 (void) fclose(fp); 602 if (ret == 0) 603 (void) asprintf(&currdev, "zfs:%s:", mp.mnt_special); 604 else 605 return (strdup(":")); 606 607 return (currdev); 608 } 609 610 /* 611 * Replace a word definition (a builtin command) with another 612 * one that: 613 * 614 * - Throw error results instead of returning them on the stack 615 * - Pass a flag indicating whether the word was compiled or is 616 * being interpreted. 617 * 618 * There is one major problem with builtins that cannot be overcome 619 * in anyway, except by outlawing it. We want builtins to behave 620 * differently depending on whether they have been compiled or they 621 * are being interpreted. Notice that this is *not* the interpreter's 622 * current state. For example: 623 * 624 * : example ls ; immediate 625 * : problem example ; \ "ls" gets executed while compiling 626 * example \ "ls" gets executed while interpreting 627 * 628 * Notice that, though the current state is different in the two 629 * invocations of "example", in both cases "ls" has been 630 * *compiled in*, which is what we really want. 631 * 632 * The problem arises when you tick the builtin. For example: 633 * 634 * : example-1 ['] ls postpone literal ; immediate 635 * : example-2 example-1 execute ; immediate 636 * : problem example-2 ; 637 * example-2 638 * 639 * We have no way, when we get EXECUTEd, of knowing what our behavior 640 * should be. Thus, our only alternative is to "outlaw" this. See RFI 641 * 0007, and ANS Forth Standard's appendix D, item 6.7 for a related 642 * problem, concerning compile semantics. 643 * 644 * The problem is compounded by the fact that "' builtin CATCH" is valid 645 * and desirable. The only solution is to create an intermediary word. 646 * For example: 647 * 648 * : my-ls ls ; 649 * : example ['] my-ls catch ; 650 * 651 * So, with the below implementation, here is a summary of the behavior 652 * of builtins: 653 * 654 * ls -l \ "interpret" behavior, ie, 655 * \ takes parameters from TIB 656 * : ex-1 s" -l" 1 ls ; \ "compile" behavior, ie, 657 * \ takes parameters from the stack 658 * : ex-2 ['] ls catch ; immediate \ undefined behavior 659 * : ex-3 ['] ls catch ; \ undefined behavior 660 * ex-2 ex-3 \ "interpret" behavior, 661 * \ catch works 662 * : ex-4 ex-2 ; \ "compile" behavior, 663 * \ catch does not work 664 * : ex-5 ex-3 ; immediate \ same as ex-2 665 * : ex-6 ex-3 ; \ same as ex-3 666 * : ex-7 ['] ex-1 catch ; \ "compile" behavior, 667 * \ catch works 668 * : ex-8 postpone ls ; immediate \ same as ex-2 669 * : ex-9 postpone ls ; \ same as ex-3 670 * 671 * As the definition below is particularly tricky, and it's side effects 672 * must be well understood by those playing with it, I'll be heavy on 673 * the comments. 674 * 675 * (if you edit this definition, pay attention to trailing spaces after 676 * each word -- I warned you! :-) ) 677 */ 678 #define BUILTIN_CONSTRUCTOR \ 679 ": builtin: " \ 680 ">in @ " /* save the tib index pointer */ \ 681 "' " /* get next word's xt */ \ 682 "swap >in ! " /* point again to next word */ \ 683 "create " /* create a new definition of the next word */ \ 684 ", " /* save previous definition's xt */ \ 685 "immediate " /* make the new definition an immediate word */ \ 686 \ 687 "does> " /* Now, the *new* definition will: */ \ 688 "state @ if " /* if in compiling state: */ \ 689 "1 postpone literal " /* pass 1 flag to indicate compile */ \ 690 "@ compile, " /* compile in previous definition */ \ 691 "postpone throw " /* throw stack-returned result */ \ 692 "else " /* if in interpreting state: */ \ 693 "0 swap " /* pass 0 flag to indicate interpret */ \ 694 "@ execute " /* call previous definition */ \ 695 "throw " /* throw stack-returned result */ \ 696 "then ; " 697 698 extern int ficlExecFD(ficlVm *, int); 699 #define COMMAND_SET(ptr, name, desc, fn) \ 700 ptr = malloc(sizeof (struct bootblk_command)); \ 701 ptr->c_name = (name); \ 702 ptr->c_desc = (desc); \ 703 ptr->c_fn = (fn); 704 705 /* 706 * Initialise the Forth interpreter, create all our commands as words. 707 */ 708 ficlVm * 709 bf_init(const char *rc, ficlOutputFunction out) 710 { 711 struct bootblk_command *cmdp; 712 char create_buf[41]; /* 31 characters-long builtins */ 713 char *buf; 714 int fd, rv; 715 ficlSystemInformation *fsi; 716 ficlDictionary *dict; 717 ficlDictionary *env; 718 719 /* set up commands list */ 720 STAILQ_INIT(&commands); 721 COMMAND_SET(cmdp, "help", "detailed help", command_help); 722 STAILQ_INSERT_TAIL(&commands, cmdp, next); 723 COMMAND_SET(cmdp, "?", "list commands", command_commandlist); 724 STAILQ_INSERT_TAIL(&commands, cmdp, next); 725 COMMAND_SET(cmdp, "show", "show variable(s)", command_show); 726 STAILQ_INSERT_TAIL(&commands, cmdp, next); 727 COMMAND_SET(cmdp, "printenv", "show variable(s)", command_show); 728 STAILQ_INSERT_TAIL(&commands, cmdp, next); 729 COMMAND_SET(cmdp, "set", "set a variable", command_set); 730 STAILQ_INSERT_TAIL(&commands, cmdp, next); 731 COMMAND_SET(cmdp, "setprop", "set a variable", command_setprop); 732 STAILQ_INSERT_TAIL(&commands, cmdp, next); 733 COMMAND_SET(cmdp, "unset", "unset a variable", command_unset); 734 STAILQ_INSERT_TAIL(&commands, cmdp, next); 735 COMMAND_SET(cmdp, "echo", "echo arguments", command_echo); 736 STAILQ_INSERT_TAIL(&commands, cmdp, next); 737 COMMAND_SET(cmdp, "read", "read input from the terminal", command_read); 738 STAILQ_INSERT_TAIL(&commands, cmdp, next); 739 COMMAND_SET(cmdp, "more", "show contents of a file", command_more); 740 STAILQ_INSERT_TAIL(&commands, cmdp, next); 741 COMMAND_SET(cmdp, "ls", "list files", command_ls); 742 STAILQ_INSERT_TAIL(&commands, cmdp, next); 743 COMMAND_SET(cmdp, "include", "read commands from a file", 744 command_include); 745 STAILQ_INSERT_TAIL(&commands, cmdp, next); 746 COMMAND_SET(cmdp, "boot", "boot a file or loaded kernel", command_boot); 747 STAILQ_INSERT_TAIL(&commands, cmdp, next); 748 COMMAND_SET(cmdp, "autoboot", "boot automatically after a delay", 749 command_autoboot); 750 STAILQ_INSERT_TAIL(&commands, cmdp, next); 751 COMMAND_SET(cmdp, "load", "load a kernel or module", command_load); 752 STAILQ_INSERT_TAIL(&commands, cmdp, next); 753 COMMAND_SET(cmdp, "unload", "unload all modules", command_unload); 754 STAILQ_INSERT_TAIL(&commands, cmdp, next); 755 COMMAND_SET(cmdp, "reboot", "reboot the system", command_reboot); 756 STAILQ_INSERT_TAIL(&commands, cmdp, next); 757 COMMAND_SET(cmdp, "sifting", "find words", command_sifting); 758 COMMAND_SET(cmdp, "framebuffer", "framebuffer mode management", 759 command_framebuffer); 760 STAILQ_INSERT_TAIL(&commands, cmdp, next); 761 762 fsi = malloc(sizeof (ficlSystemInformation)); 763 ficlSystemInformationInitialize(fsi); 764 fsi->textOut = out; 765 fsi->dictionarySize = BF_DICTSIZE; 766 767 bf_sys = ficlSystemCreate(fsi); 768 free(fsi); 769 ficlSystemCompileExtras(bf_sys); 770 bf_vm = ficlSystemCreateVm(bf_sys); 771 772 buf = isadir(); 773 if (buf == NULL || strcmp(buf, "amd64") != 0) { 774 (void) setenv("ISADIR", "", 1); 775 } else { 776 (void) setenv("ISADIR", buf, 1); 777 } 778 if (buf != NULL) 779 free(buf); 780 buf = get_currdev(); 781 (void) setenv("currdev", buf, 1); 782 free(buf); 783 784 /* Put all private definitions in a "builtins" vocabulary */ 785 rv = ficlVmEvaluate(bf_vm, 786 "vocabulary builtins also builtins definitions"); 787 if (rv != FICL_VM_STATUS_OUT_OF_TEXT) { 788 printf("error interpreting forth: %d\n", rv); 789 exit(1); 790 } 791 792 /* Builtin constructor word */ 793 rv = ficlVmEvaluate(bf_vm, BUILTIN_CONSTRUCTOR); 794 if (rv != FICL_VM_STATUS_OUT_OF_TEXT) { 795 printf("error interpreting forth: %d\n", rv); 796 exit(1); 797 } 798 799 /* make all commands appear as Forth words */ 800 dict = ficlSystemGetDictionary(bf_sys); 801 cmdp = NULL; 802 STAILQ_FOREACH(cmdp, &commands, next) { 803 ficlDictionaryAppendPrimitive(dict, (char *)cmdp->c_name, 804 bf_command, FICL_WORD_DEFAULT); 805 rv = ficlVmEvaluate(bf_vm, "forth definitions builtins"); 806 if (rv != FICL_VM_STATUS_OUT_OF_TEXT) { 807 printf("error interpreting forth: %d\n", rv); 808 exit(1); 809 } 810 snprintf(create_buf, sizeof (create_buf), "builtin: %s", 811 cmdp->c_name); 812 rv = ficlVmEvaluate(bf_vm, create_buf); 813 if (rv != FICL_VM_STATUS_OUT_OF_TEXT) { 814 printf("error interpreting forth: %d\n", rv); 815 exit(1); 816 } 817 rv = ficlVmEvaluate(bf_vm, "builtins definitions"); 818 if (rv != FICL_VM_STATUS_OUT_OF_TEXT) { 819 printf("error interpreting forth: %d\n", rv); 820 exit(1); 821 } 822 } 823 rv = ficlVmEvaluate(bf_vm, "only forth definitions"); 824 if (rv != FICL_VM_STATUS_OUT_OF_TEXT) { 825 printf("error interpreting forth: %d\n", rv); 826 exit(1); 827 } 828 829 /* 830 * Export some version numbers so that code can detect the 831 * loader/host version 832 */ 833 env = ficlSystemGetEnvironment(bf_sys); 834 ficlDictionarySetConstant(env, "loader_version", 835 (bootprog_rev[0] - '0') * 10 + (bootprog_rev[2] - '0')); 836 837 /* try to load and run init file if present */ 838 if (rc == NULL) 839 rc = "/boot/forth/boot.4th"; 840 if (*rc != '\0') { 841 fd = open(rc, O_RDONLY); 842 if (fd != -1) { 843 (void) ficlExecFD(bf_vm, fd); 844 close(fd); 845 } 846 } 847 848 gfx_framework_init(); 849 return (bf_vm); 850 } 851 852 void 853 bf_fini(void) 854 { 855 ficlSystemDestroy(bf_sys); 856 gfx_framework_fini(); 857 } 858 859 /* 860 * Feed a line of user input to the Forth interpreter 861 */ 862 int 863 bf_run(char *line) 864 { 865 int result; 866 ficlString s; 867 868 FICL_STRING_SET_FROM_CSTRING(s, line); 869 result = ficlVmExecuteString(bf_vm, s); 870 871 switch (result) { 872 case FICL_VM_STATUS_OUT_OF_TEXT: 873 case FICL_VM_STATUS_ABORTQ: 874 case FICL_VM_STATUS_QUIT: 875 case FICL_VM_STATUS_ERROR_EXIT: 876 break; 877 case FICL_VM_STATUS_USER_EXIT: 878 break; 879 case FICL_VM_STATUS_ABORT: 880 printf("Aborted!\n"); 881 break; 882 case BF_PARSE: 883 printf("Parse error!\n"); 884 break; 885 default: 886 if (command_errmsg != NULL) { 887 printf("%s\n", command_errmsg); 888 command_errmsg = NULL; 889 } 890 } 891 892 setenv("interpret", bf_vm->state ? "" : "ok", 1); 893 894 return (result); 895 } 896 897 char * 898 get_dev(const char *path) 899 { 900 FILE *fp; 901 struct mnttab mpref = {0}; 902 struct mnttab mp = {0}; 903 char *currdev; 904 int ret; 905 char *buf; 906 char *tmppath; 907 char *tmpdev; 908 char *cwd = NULL; 909 910 fp = fopen(MNTTAB, "r"); 911 912 /* do the best we can to return something... */ 913 if (fp == NULL) 914 return (strdup(path)); 915 916 /* 917 * the path can have device provided, check for it 918 * and extract it. 919 */ 920 buf = strrchr(path, ':'); 921 if (buf != NULL) { 922 tmppath = buf+1; /* real path */ 923 buf = strchr(path, ':'); /* skip zfs: */ 924 buf++; 925 tmpdev = strdup(buf); 926 buf = strchr(tmpdev, ':'); /* get ending : */ 927 *buf = '\0'; 928 } else { 929 tmppath = (char *)path; 930 if (tmppath[0] != '/') 931 if ((cwd = getcwd(NULL, PATH_MAX)) == NULL) { 932 (void) fclose(fp); 933 return (strdup(path)); 934 } 935 936 currdev = getenv("currdev"); 937 buf = strchr(currdev, ':'); /* skip zfs: */ 938 if (buf == NULL) { 939 (void) fclose(fp); 940 return (strdup(path)); 941 } 942 buf++; 943 tmpdev = strdup(buf); 944 buf = strchr(tmpdev, ':'); /* get ending : */ 945 *buf = '\0'; 946 } 947 948 mpref.mnt_special = tmpdev; 949 ret = getmntany(fp, &mp, &mpref); 950 (void) fclose(fp); 951 free(tmpdev); 952 953 if (cwd == NULL) 954 (void) asprintf(&buf, "%s/%s", ret? "":mp.mnt_mountp, tmppath); 955 else { 956 (void) asprintf(&buf, "%s/%s/%s", ret? "":mp.mnt_mountp, cwd, 957 tmppath); 958 free(cwd); 959 } 960 return (buf); 961 } 962 963 static void 964 ngets(char *buf, int n) 965 { 966 int c; 967 char *lp; 968 969 for (lp = buf; ; ) 970 switch (c = getchar() & 0177) { 971 case '\n': 972 case '\r': 973 *lp = '\0'; 974 putchar('\n'); 975 return; 976 case '\b': 977 case '\177': 978 if (lp > buf) { 979 lp--; 980 putchar('\b'); 981 putchar(' '); 982 putchar('\b'); 983 } 984 break; 985 case 'r'&037: { 986 char *p; 987 988 putchar('\n'); 989 for (p = buf; p < lp; ++p) 990 putchar(*p); 991 break; 992 } 993 case 'u'&037: 994 case 'w'&037: 995 lp = buf; 996 putchar('\n'); 997 break; 998 default: 999 if ((n < 1) || ((lp - buf) < n - 1)) { 1000 *lp++ = c; 1001 putchar(c); 1002 } 1003 } 1004 /*NOTREACHED*/ 1005 } 1006 1007 static int 1008 fgetstr(char *buf, int size, int fd) 1009 { 1010 char c; 1011 int err, len; 1012 1013 size--; /* leave space for terminator */ 1014 len = 0; 1015 while (size != 0) { 1016 err = read(fd, &c, sizeof (c)); 1017 if (err < 0) /* read error */ 1018 return (-1); 1019 1020 if (err == 0) { /* EOF */ 1021 if (len == 0) 1022 return (-1); /* nothing to read */ 1023 break; 1024 } 1025 if ((c == '\r') || (c == '\n')) /* line terminators */ 1026 break; 1027 *buf++ = c; /* keep char */ 1028 size--; 1029 len++; 1030 } 1031 *buf = 0; 1032 return (len); 1033 } 1034 1035 static char * 1036 unargv(int argc, char *argv[]) 1037 { 1038 size_t hlong; 1039 int i; 1040 char *cp; 1041 1042 for (i = 0, hlong = 0; i < argc; i++) 1043 hlong += strlen(argv[i]) + 2; 1044 1045 if (hlong == 0) 1046 return (NULL); 1047 1048 cp = malloc(hlong); 1049 cp[0] = 0; 1050 for (i = 0; i < argc; i++) { 1051 strcat(cp, argv[i]); 1052 if (i < (argc - 1)) 1053 strcat(cp, " "); 1054 } 1055 1056 return (cp); 1057 } 1058 1059 /* 1060 * Help is read from a formatted text file. 1061 * 1062 * Entries in the file are formatted as: 1063 * # Ttopic [Ssubtopic] Ddescription 1064 * help 1065 * text 1066 * here 1067 * # 1068 * 1069 * Note that for code simplicity's sake, the above format must be followed 1070 * exactly. 1071 * 1072 * Subtopic entries must immediately follow the topic (this is used to 1073 * produce the listing of subtopics). 1074 * 1075 * If no argument(s) are supplied by the user, the help for 'help' is displayed. 1076 */ 1077 static int 1078 help_getnext(int fd, char **topic, char **subtopic, char **desc) 1079 { 1080 char line[81], *cp, *ep; 1081 1082 *topic = *subtopic = *desc = NULL; 1083 for (;;) { 1084 if (fgetstr(line, 80, fd) < 0) 1085 return (0); 1086 1087 if (strlen(line) < 3 || line[0] != '#' || line[1] != ' ') 1088 continue; 1089 1090 *topic = *subtopic = *desc = NULL; 1091 cp = line + 2; 1092 while (cp != NULL && *cp != 0) { 1093 ep = strchr(cp, ' '); 1094 if (*cp == 'T' && *topic == NULL) { 1095 if (ep != NULL) 1096 *ep++ = 0; 1097 *topic = strdup(cp + 1); 1098 } else if (*cp == 'S' && *subtopic == NULL) { 1099 if (ep != NULL) 1100 *ep++ = 0; 1101 *subtopic = strdup(cp + 1); 1102 } else if (*cp == 'D') { 1103 *desc = strdup(cp + 1); 1104 ep = NULL; 1105 } 1106 cp = ep; 1107 } 1108 if (*topic == NULL) { 1109 free(*subtopic); 1110 free(*desc); 1111 continue; 1112 } 1113 return (1); 1114 } 1115 } 1116 1117 static int 1118 help_emitsummary(char *topic, char *subtopic, char *desc) 1119 { 1120 int i; 1121 1122 pager_output(" "); 1123 pager_output(topic); 1124 i = strlen(topic); 1125 if (subtopic != NULL) { 1126 pager_output(" "); 1127 pager_output(subtopic); 1128 i += strlen(subtopic) + 1; 1129 } 1130 if (desc != NULL) { 1131 do { 1132 pager_output(" "); 1133 } while (i++ < 30); 1134 pager_output(desc); 1135 } 1136 return (pager_output("\n")); 1137 } 1138 1139 static int 1140 command_help(int argc, char *argv[]) 1141 { 1142 char buf[81]; /* XXX buffer size? */ 1143 int hfd, matched, doindex; 1144 char *topic, *subtopic, *t, *s, *d; 1145 1146 /* page the help text from our load path */ 1147 snprintf(buf, sizeof (buf), "/boot/loader.help"); 1148 if ((hfd = open(buf, O_RDONLY)) < 0) { 1149 printf("Verbose help not available, " 1150 "use '?' to list commands\n"); 1151 return (CMD_OK); 1152 } 1153 1154 /* pick up request from arguments */ 1155 topic = subtopic = NULL; 1156 switch (argc) { 1157 case 3: 1158 subtopic = strdup(argv[2]); 1159 /* FALLTHROUGH */ 1160 case 2: 1161 topic = strdup(argv[1]); 1162 break; 1163 case 1: 1164 topic = strdup("help"); 1165 break; 1166 default: 1167 command_errmsg = "usage is 'help <topic> [<subtopic>]"; 1168 close(hfd); 1169 return (CMD_ERROR); 1170 } 1171 1172 /* magic "index" keyword */ 1173 doindex = strcmp(topic, "index") == 0; 1174 matched = doindex; 1175 1176 /* Scan the helpfile looking for help matching the request */ 1177 pager_open(); 1178 while (help_getnext(hfd, &t, &s, &d)) { 1179 if (doindex) { /* dink around formatting */ 1180 if (help_emitsummary(t, s, d)) 1181 break; 1182 1183 } else if (strcmp(topic, t)) { 1184 /* topic mismatch */ 1185 /* nothing more on this topic, stop scanning */ 1186 if (matched) 1187 break; 1188 } else { 1189 /* topic matched */ 1190 matched = 1; 1191 if ((subtopic == NULL && s == NULL) || 1192 (subtopic != NULL && s != NULL && 1193 strcmp(subtopic, s) == 0)) { 1194 /* exact match, print text */ 1195 while (fgetstr(buf, 80, hfd) >= 0 && 1196 buf[0] != '#') { 1197 if (pager_output(buf)) 1198 break; 1199 if (pager_output("\n")) 1200 break; 1201 } 1202 } else if (subtopic == NULL && s != NULL) { 1203 /* topic match, list subtopics */ 1204 if (help_emitsummary(t, s, d)) 1205 break; 1206 } 1207 } 1208 free(t); 1209 free(s); 1210 free(d); 1211 t = s = d = NULL; 1212 } 1213 free(t); 1214 free(s); 1215 free(d); 1216 pager_close(); 1217 close(hfd); 1218 if (!matched) { 1219 snprintf(command_errbuf, sizeof (command_errbuf), 1220 "no help available for '%s'", topic); 1221 free(topic); 1222 free(subtopic); 1223 return (CMD_ERROR); 1224 } 1225 free(topic); 1226 free(subtopic); 1227 return (CMD_OK); 1228 } 1229 1230 static int 1231 command_commandlist(int argc __unused, char *argv[] __unused) 1232 { 1233 struct bootblk_command *cmdp; 1234 int res; 1235 char name[20]; 1236 1237 res = 0; 1238 pager_open(); 1239 res = pager_output("Available commands:\n"); 1240 cmdp = NULL; 1241 STAILQ_FOREACH(cmdp, &commands, next) { 1242 if (res) 1243 break; 1244 if (cmdp->c_name != NULL && cmdp->c_desc != NULL) { 1245 snprintf(name, sizeof (name), " %-15s ", 1246 cmdp->c_name); 1247 pager_output(name); 1248 pager_output(cmdp->c_desc); 1249 res = pager_output("\n"); 1250 } 1251 } 1252 pager_close(); 1253 return (CMD_OK); 1254 } 1255 1256 /* 1257 * XXX set/show should become set/echo if we have variable 1258 * substitution happening. 1259 */ 1260 static int 1261 command_show(int argc, char *argv[]) 1262 { 1263 char **ev; 1264 char *cp; 1265 1266 if (argc < 2) { 1267 /* 1268 * With no arguments, print everything. 1269 */ 1270 pager_open(); 1271 for (ev = _environ; *ev != NULL; ev++) { 1272 pager_output(*ev); 1273 cp = getenv(*ev); 1274 if (cp != NULL) { 1275 pager_output("="); 1276 pager_output(cp); 1277 } 1278 if (pager_output("\n")) 1279 break; 1280 } 1281 pager_close(); 1282 } else { 1283 if ((cp = getenv(argv[1])) != NULL) { 1284 printf("%s\n", cp); 1285 } else { 1286 snprintf(command_errbuf, sizeof (command_errbuf), 1287 "variable '%s' not found", argv[1]); 1288 return (CMD_ERROR); 1289 } 1290 } 1291 return (CMD_OK); 1292 } 1293 1294 static int 1295 command_set(int argc, char *argv[]) 1296 { 1297 int err; 1298 char *value, *copy; 1299 1300 if (argc != 2) { 1301 command_errmsg = "wrong number of arguments"; 1302 return (CMD_ERROR); 1303 } else { 1304 copy = strdup(argv[1]); 1305 if (copy == NULL) { 1306 command_errmsg = strerror(errno); 1307 return (CMD_ERROR); 1308 } 1309 if ((value = strchr(copy, '=')) != NULL) 1310 *(value++) = 0; 1311 else 1312 value = ""; 1313 if ((err = setenv(copy, value, 1)) != 0) { 1314 free(copy); 1315 command_errmsg = strerror(errno); 1316 return (CMD_ERROR); 1317 } 1318 free(copy); 1319 } 1320 return (CMD_OK); 1321 } 1322 1323 static int 1324 command_setprop(int argc, char *argv[]) 1325 { 1326 int err; 1327 1328 if (argc != 3) { 1329 command_errmsg = "wrong number of arguments"; 1330 return (CMD_ERROR); 1331 } else { 1332 if ((err = setenv(argv[1], argv[2], 1)) != 0) { 1333 command_errmsg = strerror(err); 1334 return (CMD_ERROR); 1335 } 1336 } 1337 return (CMD_OK); 1338 } 1339 1340 static int 1341 command_unset(int argc, char *argv[]) 1342 { 1343 int err; 1344 1345 if (argc != 2) { 1346 command_errmsg = "wrong number of arguments"; 1347 return (CMD_ERROR); 1348 } else { 1349 if ((err = unsetenv(argv[1])) != 0) { 1350 command_errmsg = strerror(err); 1351 return (CMD_ERROR); 1352 } 1353 } 1354 return (CMD_OK); 1355 } 1356 1357 static int 1358 command_echo(int argc, char *argv[]) 1359 { 1360 char *s; 1361 int nl, ch; 1362 1363 nl = 0; 1364 optind = 1; 1365 opterr = 1; 1366 while ((ch = getopt(argc, argv, "n")) != -1) { 1367 switch (ch) { 1368 case 'n': 1369 nl = 1; 1370 break; 1371 case '?': 1372 default: 1373 /* getopt has already reported an error */ 1374 return (CMD_OK); 1375 } 1376 } 1377 argv += (optind); 1378 argc -= (optind); 1379 1380 s = unargv(argc, argv); 1381 if (s != NULL) { 1382 printf("%s", s); 1383 free(s); 1384 } 1385 if (!nl) 1386 printf("\n"); 1387 return (CMD_OK); 1388 } 1389 1390 /* 1391 * A passable emulation of the sh(1) command of the same name. 1392 */ 1393 static int 1394 ischar(void) 1395 { 1396 return (1); 1397 } 1398 1399 static int 1400 command_read(int argc, char *argv[]) 1401 { 1402 char *prompt; 1403 int timeout; 1404 time_t when; 1405 char *cp; 1406 char *name; 1407 char buf[256]; /* XXX size? */ 1408 int c; 1409 1410 timeout = -1; 1411 prompt = NULL; 1412 optind = 1; 1413 opterr = 1; 1414 while ((c = getopt(argc, argv, "p:t:")) != -1) { 1415 switch (c) { 1416 case 'p': 1417 prompt = optarg; 1418 break; 1419 case 't': 1420 timeout = strtol(optarg, &cp, 0); 1421 if (cp == optarg) { 1422 snprintf(command_errbuf, 1423 sizeof (command_errbuf), 1424 "bad timeout '%s'", optarg); 1425 return (CMD_ERROR); 1426 } 1427 break; 1428 default: 1429 return (CMD_OK); 1430 } 1431 } 1432 1433 argv += (optind); 1434 argc -= (optind); 1435 name = (argc > 0) ? argv[0]: NULL; 1436 1437 if (prompt != NULL) 1438 printf("%s", prompt); 1439 if (timeout >= 0) { 1440 when = time(NULL) + timeout; 1441 while (!ischar()) 1442 if (time(NULL) >= when) 1443 return (CMD_OK); /* is timeout an error? */ 1444 } 1445 1446 ngets(buf, sizeof (buf)); 1447 1448 if (name != NULL) 1449 setenv(name, buf, 1); 1450 return (CMD_OK); 1451 } 1452 1453 /* 1454 * File pager 1455 */ 1456 static int 1457 command_more(int argc, char *argv[]) 1458 { 1459 int i; 1460 int res; 1461 char line[80]; 1462 char *name; 1463 1464 res = 0; 1465 pager_open(); 1466 for (i = 1; (i < argc) && (res == 0); i++) { 1467 snprintf(line, sizeof (line), "*** FILE %s BEGIN ***\n", 1468 argv[i]); 1469 if (pager_output(line)) 1470 break; 1471 name = get_dev(argv[i]); 1472 res = page_file(name); 1473 free(name); 1474 if (!res) { 1475 snprintf(line, sizeof (line), "*** FILE %s END ***\n", 1476 argv[i]); 1477 res = pager_output(line); 1478 } 1479 } 1480 pager_close(); 1481 1482 if (res == 0) 1483 return (CMD_OK); 1484 return (CMD_ERROR); 1485 } 1486 1487 static int 1488 page_file(char *filename) 1489 { 1490 int result; 1491 1492 result = pager_file(filename); 1493 1494 if (result == -1) { 1495 snprintf(command_errbuf, sizeof (command_errbuf), 1496 "error showing %s", filename); 1497 } 1498 1499 return (result); 1500 } 1501 1502 static int 1503 command_ls(int argc, char *argv[]) 1504 { 1505 DIR *dir; 1506 int fd; 1507 struct stat sb; 1508 struct dirent *d; 1509 char *buf, *path; 1510 char lbuf[128]; /* one line */ 1511 int result, ch; 1512 int verbose; 1513 1514 result = CMD_OK; 1515 fd = -1; 1516 verbose = 0; 1517 optind = 1; 1518 opterr = 1; 1519 while ((ch = getopt(argc, argv, "l")) != -1) { 1520 switch (ch) { 1521 case 'l': 1522 verbose = 1; 1523 break; 1524 case '?': 1525 default: 1526 /* getopt has already reported an error */ 1527 return (CMD_OK); 1528 } 1529 } 1530 argv += (optind - 1); 1531 argc -= (optind - 1); 1532 1533 if (argc < 2) { 1534 path = ""; 1535 } else { 1536 path = argv[1]; 1537 } 1538 1539 fd = ls_getdir(&path); 1540 if (fd == -1) { 1541 result = CMD_ERROR; 1542 goto out; 1543 } 1544 dir = fdopendir(fd); 1545 pager_open(); 1546 pager_output(path); 1547 pager_output("\n"); 1548 1549 while ((d = readdir(dir)) != NULL) { 1550 if (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) { 1551 /* stat the file, if possible */ 1552 sb.st_size = 0; 1553 sb.st_mode = 0; 1554 buf = malloc(strlen(path) + strlen(d->d_name) + 2); 1555 if (path[0] == '\0') { 1556 snprintf(buf, sizeof (buf), "%s", d->d_name); 1557 } else { 1558 snprintf(buf, sizeof (buf), "%s/%s", path, 1559 d->d_name); 1560 } 1561 /* ignore return, could be symlink, etc. */ 1562 if (stat(buf, &sb)) 1563 sb.st_size = 0; 1564 free(buf); 1565 if (verbose) { 1566 snprintf(lbuf, sizeof (lbuf), " %c %8d %s\n", 1567 typestr[sb.st_mode >> 12], 1568 (int)sb.st_size, d->d_name); 1569 } else { 1570 snprintf(lbuf, sizeof (lbuf), " %c %s\n", 1571 typestr[sb.st_mode >> 12], d->d_name); 1572 } 1573 if (pager_output(lbuf)) 1574 goto out; 1575 } 1576 } 1577 out: 1578 pager_close(); 1579 if (fd != -1) 1580 closedir(dir); 1581 if (path != NULL) 1582 free(path); 1583 return (result); 1584 } 1585 1586 /* 1587 * Given (path) containing a vaguely reasonable path specification, return an fd 1588 * on the directory, and an allocated copy of the path to the directory. 1589 */ 1590 static int 1591 ls_getdir(char **pathp) 1592 { 1593 struct stat sb; 1594 int fd; 1595 char *cp, *path; 1596 1597 fd = -1; 1598 1599 /* one extra byte for a possible trailing slash required */ 1600 path = malloc(strlen(*pathp) + 2); 1601 strcpy(path, *pathp); 1602 1603 /* Make sure the path is respectable to begin with */ 1604 if ((cp = get_dev(path)) == NULL) { 1605 snprintf(command_errbuf, sizeof (command_errbuf), 1606 "bad path '%s'", path); 1607 goto out; 1608 } 1609 1610 /* If there's no path on the device, assume '/' */ 1611 if (*cp == 0) 1612 strcat(path, "/"); 1613 1614 fd = open(cp, O_RDONLY); 1615 if (fd < 0) { 1616 snprintf(command_errbuf, sizeof (command_errbuf), 1617 "open '%s' failed: %s", path, strerror(errno)); 1618 goto out; 1619 } 1620 if (fstat(fd, &sb) < 0) { 1621 snprintf(command_errbuf, sizeof (command_errbuf), 1622 "stat failed: %s", strerror(errno)); 1623 goto out; 1624 } 1625 if (!S_ISDIR(sb.st_mode)) { 1626 snprintf(command_errbuf, sizeof (command_errbuf), 1627 "%s: %s", path, strerror(ENOTDIR)); 1628 goto out; 1629 } 1630 1631 free(cp); 1632 *pathp = path; 1633 return (fd); 1634 1635 out: 1636 free(cp); 1637 free(path); 1638 *pathp = NULL; 1639 if (fd != -1) 1640 close(fd); 1641 return (-1); 1642 } 1643 1644 static int 1645 command_include(int argc, char *argv[]) 1646 { 1647 int i; 1648 int res; 1649 char **argvbuf; 1650 1651 /* 1652 * Since argv is static, we need to save it here. 1653 */ 1654 argvbuf = (char **)calloc(argc, sizeof (char *)); 1655 for (i = 0; i < argc; i++) 1656 argvbuf[i] = strdup(argv[i]); 1657 1658 res = CMD_OK; 1659 for (i = 1; (i < argc) && (res == CMD_OK); i++) 1660 res = include(argvbuf[i]); 1661 1662 for (i = 0; i < argc; i++) 1663 free(argvbuf[i]); 1664 free(argvbuf); 1665 1666 return (res); 1667 } 1668 1669 /* 1670 * Header prepended to each line. The text immediately follows the header. 1671 * We try to make this short in order to save memory -- the loader has 1672 * limited memory available, and some of the forth files are very long. 1673 */ 1674 struct includeline 1675 { 1676 struct includeline *next; 1677 int line; 1678 char text[]; 1679 }; 1680 1681 int 1682 include(const char *filename) 1683 { 1684 struct includeline *script, *se, *sp; 1685 int res = CMD_OK; 1686 int prevsrcid, fd, line; 1687 char *cp, input[256]; /* big enough? */ 1688 char *path; 1689 1690 path = get_dev(filename); 1691 if (((fd = open(path, O_RDONLY)) == -1)) { 1692 snprintf(command_errbuf, sizeof (command_errbuf), 1693 "can't open '%s': %s", filename, 1694 strerror(errno)); 1695 free(path); 1696 return (CMD_ERROR); 1697 } 1698 1699 free(path); 1700 /* 1701 * Read the script into memory. 1702 */ 1703 script = se = NULL; 1704 line = 0; 1705 1706 while (fgetstr(input, sizeof (input), fd) >= 0) { 1707 line++; 1708 cp = input; 1709 /* Allocate script line structure and copy line, flags */ 1710 if (*cp == '\0') 1711 continue; /* ignore empty line, save memory */ 1712 if (cp[0] == '\\' && cp[1] == ' ') 1713 continue; /* ignore comment */ 1714 1715 sp = malloc(sizeof (struct includeline) + strlen(cp) + 1); 1716 /* 1717 * On malloc failure (it happens!), free as much as possible 1718 * and exit 1719 */ 1720 if (sp == NULL) { 1721 while (script != NULL) { 1722 se = script; 1723 script = script->next; 1724 free(se); 1725 } 1726 snprintf(command_errbuf, sizeof (command_errbuf), 1727 "file '%s' line %d: memory allocation " 1728 "failure - aborting", filename, line); 1729 return (CMD_ERROR); 1730 } 1731 strcpy(sp->text, cp); 1732 sp->line = line; 1733 sp->next = NULL; 1734 1735 if (script == NULL) { 1736 script = sp; 1737 } else { 1738 se->next = sp; 1739 } 1740 se = sp; 1741 } 1742 close(fd); 1743 1744 /* 1745 * Execute the script 1746 */ 1747 1748 prevsrcid = bf_vm->sourceId.i; 1749 bf_vm->sourceId.i = fd+1; /* 0 is user input device */ 1750 1751 res = CMD_OK; 1752 1753 for (sp = script; sp != NULL; sp = sp->next) { 1754 res = bf_run(sp->text); 1755 if (res != FICL_VM_STATUS_OUT_OF_TEXT) { 1756 snprintf(command_errbuf, sizeof (command_errbuf), 1757 "Error while including %s, in the line %d:\n%s", 1758 filename, sp->line, sp->text); 1759 res = CMD_ERROR; 1760 break; 1761 } else 1762 res = CMD_OK; 1763 } 1764 1765 bf_vm->sourceId.i = -1; 1766 (void) bf_run(""); 1767 bf_vm->sourceId.i = prevsrcid; 1768 1769 while (script != NULL) { 1770 se = script; 1771 script = script->next; 1772 free(se); 1773 } 1774 1775 return (res); 1776 } 1777 1778 static int 1779 command_boot(int argc, char *argv[]) 1780 { 1781 return (CMD_OK); 1782 } 1783 1784 static int 1785 command_autoboot(int argc, char *argv[]) 1786 { 1787 return (CMD_OK); 1788 } 1789 1790 static void 1791 moduledir_rebuild(void) 1792 { 1793 struct moduledir *mdp, *mtmp; 1794 const char *path, *cp, *ep; 1795 int cplen; 1796 1797 path = getenv("module_path"); 1798 if (path == NULL) 1799 path = default_searchpath; 1800 /* 1801 * Rebuild list of module directories if it changed 1802 */ 1803 STAILQ_FOREACH(mdp, &moduledir_list, d_link) 1804 mdp->d_flags |= MDIR_REMOVED; 1805 1806 for (ep = path; *ep != 0; ep++) { 1807 cp = ep; 1808 for (; *ep != 0 && *ep != ';'; ep++) 1809 ; 1810 /* 1811 * Ignore trailing slashes 1812 */ 1813 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; 1814 cplen--) 1815 ; 1816 STAILQ_FOREACH(mdp, &moduledir_list, d_link) { 1817 if (strlen(mdp->d_path) != cplen || 1818 bcmp(cp, mdp->d_path, cplen) != 0) 1819 continue; 1820 mdp->d_flags &= ~MDIR_REMOVED; 1821 break; 1822 } 1823 if (mdp == NULL) { 1824 mdp = malloc(sizeof (*mdp) + cplen + 1); 1825 if (mdp == NULL) 1826 return; 1827 mdp->d_path = (char *)(mdp + 1); 1828 bcopy(cp, mdp->d_path, cplen); 1829 mdp->d_path[cplen] = 0; 1830 mdp->d_hints = NULL; 1831 mdp->d_flags = 0; 1832 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link); 1833 } 1834 if (*ep == 0) 1835 break; 1836 } 1837 /* 1838 * Delete unused directories if any 1839 */ 1840 mdp = STAILQ_FIRST(&moduledir_list); 1841 while (mdp) { 1842 if ((mdp->d_flags & MDIR_REMOVED) == 0) { 1843 mdp = STAILQ_NEXT(mdp, d_link); 1844 } else { 1845 if (mdp->d_hints) 1846 free(mdp->d_hints); 1847 mtmp = mdp; 1848 mdp = STAILQ_NEXT(mdp, d_link); 1849 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link); 1850 free(mtmp); 1851 } 1852 } 1853 } 1854 1855 static char * 1856 file_lookup(const char *path, const char *name, int namelen) 1857 { 1858 struct stat st; 1859 char *result, *cp, *gz; 1860 int pathlen; 1861 1862 pathlen = strlen(path); 1863 result = malloc(pathlen + namelen + 2); 1864 if (result == NULL) 1865 return (NULL); 1866 bcopy(path, result, pathlen); 1867 if (pathlen > 0 && result[pathlen - 1] != '/') 1868 result[pathlen++] = '/'; 1869 cp = result + pathlen; 1870 bcopy(name, cp, namelen); 1871 cp += namelen; 1872 *cp = '\0'; 1873 if (stat(result, &st) == 0 && S_ISREG(st.st_mode)) 1874 return (result); 1875 /* also check for gz file */ 1876 (void) asprintf(&gz, "%s.gz", result); 1877 if (gz != NULL) { 1878 int res = stat(gz, &st); 1879 free(gz); 1880 if (res == 0) 1881 return (result); 1882 } 1883 free(result); 1884 return (NULL); 1885 } 1886 1887 static char * 1888 file_search(const char *name) 1889 { 1890 struct moduledir *mdp; 1891 struct stat sb; 1892 char *result; 1893 int namelen; 1894 1895 if (name == NULL) 1896 return (NULL); 1897 if (*name == 0) 1898 return (strdup(name)); 1899 1900 if (strchr(name, '/') != NULL) { 1901 char *gz; 1902 if (stat(name, &sb) == 0) 1903 return (strdup(name)); 1904 /* also check for gz file */ 1905 (void) asprintf(&gz, "%s.gz", name); 1906 if (gz != NULL) { 1907 int res = stat(gz, &sb); 1908 free(gz); 1909 if (res == 0) 1910 return (strdup(name)); 1911 } 1912 return (NULL); 1913 } 1914 1915 moduledir_rebuild(); 1916 result = NULL; 1917 namelen = strlen(name); 1918 STAILQ_FOREACH(mdp, &moduledir_list, d_link) { 1919 result = file_lookup(mdp->d_path, name, namelen); 1920 if (result) 1921 break; 1922 } 1923 return (result); 1924 } 1925 1926 static int 1927 command_load(int argc, char *argv[]) 1928 { 1929 int dofile, ch; 1930 char *typestr = NULL; 1931 char *filename; 1932 dofile = 0; 1933 optind = 1; 1934 1935 if (argc == 1) { 1936 command_errmsg = "no filename specified"; 1937 return (CMD_ERROR); 1938 } 1939 1940 while ((ch = getopt(argc, argv, "kt:")) != -1) { 1941 switch (ch) { 1942 case 'k': 1943 break; 1944 case 't': 1945 typestr = optarg; 1946 dofile = 1; 1947 break; 1948 case '?': 1949 default: 1950 return (CMD_OK); 1951 } 1952 } 1953 argv += (optind - 1); 1954 argc -= (optind - 1); 1955 if (dofile) { 1956 if ((typestr == NULL) || (*typestr == 0)) { 1957 command_errmsg = "invalid load type"; 1958 return (CMD_ERROR); 1959 } 1960 #if 0 1961 return (file_loadraw(argv[1], typestr, argc - 2, argv + 2, 1) 1962 ? CMD_OK : CMD_ERROR); 1963 #endif 1964 return (CMD_OK); 1965 } 1966 1967 filename = file_search(argv[1]); 1968 if (filename == NULL) { 1969 snprintf(command_errbuf, sizeof (command_errbuf), 1970 "can't find '%s'", argv[1]); 1971 return (CMD_ERROR); 1972 } 1973 setenv("kernelname", filename, 1); 1974 1975 return (CMD_OK); 1976 } 1977 1978 static int 1979 command_unload(int argc, char *argv[]) 1980 { 1981 unsetenv("kernelname"); 1982 return (CMD_OK); 1983 } 1984 1985 static int 1986 command_reboot(int argc, char *argv[]) 1987 { 1988 exit(0); 1989 return (CMD_OK); 1990 } 1991 1992 static int 1993 command_sifting(int argc, char *argv[]) 1994 { 1995 if (argc != 2) { 1996 command_errmsg = "wrong number of arguments"; 1997 return (CMD_ERROR); 1998 } 1999 ficlPrimitiveSiftingImpl(bf_vm, argv[1]); 2000 return (CMD_OK); 2001 } 2002 2003 /* Only implement get and list. Ignore arguments on, off and set. */ 2004 static int 2005 command_framebuffer(int argc, char *argv[]) 2006 { 2007 if (fb.fd < 0) { 2008 printf("Framebuffer is not available.\n"); 2009 return (CMD_OK); 2010 } 2011 2012 if (argc == 2 && strcmp(argv[1], "get") == 0) { 2013 printf("\nSystem frame buffer: %s\n", fb.ident.name); 2014 printf("%dx%dx%d, stride=%d\n", fb.fb_width, fb.fb_height, 2015 fb.fb_depth, (fb.fb_pitch << 3) / fb.fb_depth); 2016 return (CMD_OK); 2017 } 2018 if (argc == 2 && strcmp(argv[1], "list") == 0) { 2019 printf("0: %dx%dx%d\n", fb.fb_width, fb.fb_height, fb.fb_depth); 2020 return (CMD_OK); 2021 } 2022 if (argc == 3 && strcmp(argv[1], "set") == 0) 2023 return (CMD_OK); 2024 if (argc == 2 && strcmp(argv[1], "on") == 0) 2025 return (CMD_OK); 2026 if (argc == 2 && strcmp(argv[1], "off") == 0) 2027 return (CMD_OK); 2028 2029 snprintf(command_errbuf, sizeof (command_errbuf), 2030 "usage: %s get | list", argv[0]); 2031 return (CMD_ERROR); 2032 } 2033