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