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 sprintf(create_buf, "builtin: %s", cmdp->c_name); 803 rv = ficlVmEvaluate(bf_vm, create_buf); 804 if (rv != FICL_VM_STATUS_OUT_OF_TEXT) { 805 printf("error interpreting forth: %d\n", rv); 806 exit(1); 807 } 808 rv = ficlVmEvaluate(bf_vm, "builtins definitions"); 809 if (rv != FICL_VM_STATUS_OUT_OF_TEXT) { 810 printf("error interpreting forth: %d\n", rv); 811 exit(1); 812 } 813 } 814 rv = ficlVmEvaluate(bf_vm, "only forth definitions"); 815 if (rv != FICL_VM_STATUS_OUT_OF_TEXT) { 816 printf("error interpreting forth: %d\n", rv); 817 exit(1); 818 } 819 820 /* 821 * Export some version numbers so that code can detect the 822 * loader/host version 823 */ 824 env = ficlSystemGetEnvironment(bf_sys); 825 ficlDictionarySetConstant(env, "loader_version", 826 (bootprog_rev[0] - '0') * 10 + (bootprog_rev[2] - '0')); 827 828 /* try to load and run init file if present */ 829 if (rc == NULL) 830 rc = "/boot/forth/boot.4th"; 831 if (*rc != '\0') { 832 fd = open(rc, O_RDONLY); 833 if (fd != -1) { 834 (void) ficlExecFD(bf_vm, fd); 835 close(fd); 836 } 837 } 838 839 return (bf_vm); 840 } 841 842 void 843 bf_fini(void) 844 { 845 ficlSystemDestroy(bf_sys); 846 } 847 848 /* 849 * Feed a line of user input to the Forth interpreter 850 */ 851 int 852 bf_run(char *line) 853 { 854 int result; 855 ficlString s; 856 857 FICL_STRING_SET_FROM_CSTRING(s, line); 858 result = ficlVmExecuteString(bf_vm, s); 859 860 switch (result) { 861 case FICL_VM_STATUS_OUT_OF_TEXT: 862 case FICL_VM_STATUS_ABORTQ: 863 case FICL_VM_STATUS_QUIT: 864 case FICL_VM_STATUS_ERROR_EXIT: 865 break; 866 case FICL_VM_STATUS_USER_EXIT: 867 break; 868 case FICL_VM_STATUS_ABORT: 869 printf("Aborted!\n"); 870 break; 871 case BF_PARSE: 872 printf("Parse error!\n"); 873 break; 874 default: 875 if (command_errmsg != NULL) { 876 printf("%s\n", command_errmsg); 877 command_errmsg = NULL; 878 } 879 } 880 881 setenv("interpret", bf_vm->state ? "" : "ok", 1); 882 883 return (result); 884 } 885 886 char * 887 get_dev(const char *path) 888 { 889 FILE *fp; 890 struct mnttab mpref = {0}; 891 struct mnttab mp = {0}; 892 char *currdev; 893 int ret; 894 char *buf; 895 char *tmppath; 896 char *tmpdev; 897 char *cwd = NULL; 898 899 fp = fopen(MNTTAB, "r"); 900 901 /* do the best we can to return something... */ 902 if (fp == NULL) 903 return (strdup(path)); 904 905 /* 906 * the path can have device provided, check for it 907 * and extract it. 908 */ 909 buf = strrchr(path, ':'); 910 if (buf != NULL) { 911 tmppath = buf+1; /* real path */ 912 buf = strchr(path, ':'); /* skip zfs: */ 913 buf++; 914 tmpdev = strdup(buf); 915 buf = strchr(tmpdev, ':'); /* get ending : */ 916 *buf = '\0'; 917 } else { 918 tmppath = (char *)path; 919 if (tmppath[0] != '/') 920 if ((cwd = getcwd(NULL, PATH_MAX)) == NULL) { 921 (void) fclose(fp); 922 return (strdup(path)); 923 } 924 925 currdev = getenv("currdev"); 926 buf = strchr(currdev, ':'); /* skip zfs: */ 927 if (buf == NULL) { 928 (void) fclose(fp); 929 return (strdup(path)); 930 } 931 buf++; 932 tmpdev = strdup(buf); 933 buf = strchr(tmpdev, ':'); /* get ending : */ 934 *buf = '\0'; 935 } 936 937 mpref.mnt_special = tmpdev; 938 ret = getmntany(fp, &mp, &mpref); 939 (void) fclose(fp); 940 free(tmpdev); 941 942 if (cwd == NULL) 943 (void) asprintf(&buf, "%s/%s", ret? "":mp.mnt_mountp, tmppath); 944 else { 945 (void) asprintf(&buf, "%s/%s/%s", ret? "":mp.mnt_mountp, cwd, 946 tmppath); 947 free(cwd); 948 } 949 return (buf); 950 } 951 952 static void 953 ngets(char *buf, int n) 954 { 955 int c; 956 char *lp; 957 958 for (lp = buf; ; ) 959 switch (c = getchar() & 0177) { 960 case '\n': 961 case '\r': 962 *lp = '\0'; 963 putchar('\n'); 964 return; 965 case '\b': 966 case '\177': 967 if (lp > buf) { 968 lp--; 969 putchar('\b'); 970 putchar(' '); 971 putchar('\b'); 972 } 973 break; 974 case 'r'&037: { 975 char *p; 976 977 putchar('\n'); 978 for (p = buf; p < lp; ++p) 979 putchar(*p); 980 break; 981 } 982 case 'u'&037: 983 case 'w'&037: 984 lp = buf; 985 putchar('\n'); 986 break; 987 default: 988 if ((n < 1) || ((lp - buf) < n - 1)) { 989 *lp++ = c; 990 putchar(c); 991 } 992 } 993 /*NOTREACHED*/ 994 } 995 996 static int 997 fgetstr(char *buf, int size, int fd) 998 { 999 char c; 1000 int err, len; 1001 1002 size--; /* leave space for terminator */ 1003 len = 0; 1004 while (size != 0) { 1005 err = read(fd, &c, sizeof (c)); 1006 if (err < 0) /* read error */ 1007 return (-1); 1008 1009 if (err == 0) { /* EOF */ 1010 if (len == 0) 1011 return (-1); /* nothing to read */ 1012 break; 1013 } 1014 if ((c == '\r') || (c == '\n')) /* line terminators */ 1015 break; 1016 *buf++ = c; /* keep char */ 1017 size--; 1018 len++; 1019 } 1020 *buf = 0; 1021 return (len); 1022 } 1023 1024 static char * 1025 unargv(int argc, char *argv[]) 1026 { 1027 size_t hlong; 1028 int i; 1029 char *cp; 1030 1031 for (i = 0, hlong = 0; i < argc; i++) 1032 hlong += strlen(argv[i]) + 2; 1033 1034 if (hlong == 0) 1035 return (NULL); 1036 1037 cp = malloc(hlong); 1038 cp[0] = 0; 1039 for (i = 0; i < argc; i++) { 1040 strcat(cp, argv[i]); 1041 if (i < (argc - 1)) 1042 strcat(cp, " "); 1043 } 1044 1045 return (cp); 1046 } 1047 1048 /* 1049 * Help is read from a formatted text file. 1050 * 1051 * Entries in the file are formatted as: 1052 * # Ttopic [Ssubtopic] Ddescription 1053 * help 1054 * text 1055 * here 1056 * # 1057 * 1058 * Note that for code simplicity's sake, the above format must be followed 1059 * exactly. 1060 * 1061 * Subtopic entries must immediately follow the topic (this is used to 1062 * produce the listing of subtopics). 1063 * 1064 * If no argument(s) are supplied by the user, the help for 'help' is displayed. 1065 */ 1066 static int 1067 help_getnext(int fd, char **topic, char **subtopic, char **desc) 1068 { 1069 char line[81], *cp, *ep; 1070 1071 for (;;) { 1072 if (fgetstr(line, 80, fd) < 0) 1073 return (0); 1074 1075 if ((strlen(line) < 3) || (line[0] != '#') || (line[1] != ' ')) 1076 continue; 1077 1078 *topic = *subtopic = *desc = NULL; 1079 cp = line + 2; 1080 while ((cp != NULL) && (*cp != 0)) { 1081 ep = strchr(cp, ' '); 1082 if ((*cp == 'T') && (*topic == NULL)) { 1083 if (ep != NULL) 1084 *ep++ = 0; 1085 *topic = strdup(cp + 1); 1086 } else if ((*cp == 'S') && (*subtopic == NULL)) { 1087 if (ep != NULL) 1088 *ep++ = 0; 1089 *subtopic = strdup(cp + 1); 1090 } else if (*cp == 'D') { 1091 *desc = strdup(cp + 1); 1092 ep = NULL; 1093 } 1094 cp = ep; 1095 } 1096 if (*topic == NULL) { 1097 if (*subtopic != NULL) 1098 free(*subtopic); 1099 if (*desc != NULL) 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 sprintf(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 } 1202 pager_close(); 1203 close(hfd); 1204 if (!matched) { 1205 snprintf(command_errbuf, sizeof (command_errbuf), 1206 "no help available for '%s'", topic); 1207 free(topic); 1208 if (subtopic) 1209 free(subtopic); 1210 return (CMD_ERROR); 1211 } 1212 free(topic); 1213 if (subtopic) 1214 free(subtopic); 1215 return (CMD_OK); 1216 } 1217 1218 static int 1219 command_commandlist(int argc, char *argv[]) 1220 { 1221 struct bootblk_command *cmdp; 1222 int res; 1223 char name[20]; 1224 1225 res = 0; 1226 pager_open(); 1227 res = pager_output("Available commands:\n"); 1228 cmdp = NULL; 1229 STAILQ_FOREACH(cmdp, &commands, next) { 1230 if (res) 1231 break; 1232 if ((cmdp->c_name != NULL) && (cmdp->c_desc != NULL)) { 1233 sprintf(name, " %-15s ", cmdp->c_name); 1234 pager_output(name); 1235 pager_output(cmdp->c_desc); 1236 res = pager_output("\n"); 1237 } 1238 } 1239 pager_close(); 1240 return (CMD_OK); 1241 } 1242 1243 /* 1244 * XXX set/show should become set/echo if we have variable 1245 * substitution happening. 1246 */ 1247 static int 1248 command_show(int argc, char *argv[]) 1249 { 1250 char **ev; 1251 char *cp; 1252 1253 if (argc < 2) { 1254 /* 1255 * With no arguments, print everything. 1256 */ 1257 pager_open(); 1258 for (ev = _environ; *ev != NULL; ev++) { 1259 pager_output(*ev); 1260 cp = getenv(*ev); 1261 if (cp != NULL) { 1262 pager_output("="); 1263 pager_output(cp); 1264 } 1265 if (pager_output("\n")) 1266 break; 1267 } 1268 pager_close(); 1269 } else { 1270 if ((cp = getenv(argv[1])) != NULL) { 1271 printf("%s\n", cp); 1272 } else { 1273 snprintf(command_errbuf, sizeof (command_errbuf), 1274 "variable '%s' not found", argv[1]); 1275 return (CMD_ERROR); 1276 } 1277 } 1278 return (CMD_OK); 1279 } 1280 1281 static int 1282 command_set(int argc, char *argv[]) 1283 { 1284 int err; 1285 char *value, *copy; 1286 1287 if (argc != 2) { 1288 command_errmsg = "wrong number of arguments"; 1289 return (CMD_ERROR); 1290 } else { 1291 copy = strdup(argv[1]); 1292 if (copy == NULL) { 1293 command_errmsg = strerror(errno); 1294 return (CMD_ERROR); 1295 } 1296 if ((value = strchr(copy, '=')) != NULL) 1297 *(value++) = 0; 1298 else 1299 value = ""; 1300 if ((err = setenv(copy, value, 1)) != 0) { 1301 free(copy); 1302 command_errmsg = strerror(errno); 1303 return (CMD_ERROR); 1304 } 1305 free(copy); 1306 } 1307 return (CMD_OK); 1308 } 1309 1310 static int 1311 command_setprop(int argc, char *argv[]) 1312 { 1313 int err; 1314 1315 if (argc != 3) { 1316 command_errmsg = "wrong number of arguments"; 1317 return (CMD_ERROR); 1318 } else { 1319 if ((err = setenv(argv[1], argv[2], 1)) != 0) { 1320 command_errmsg = strerror(err); 1321 return (CMD_ERROR); 1322 } 1323 } 1324 return (CMD_OK); 1325 } 1326 1327 static int 1328 command_unset(int argc, char *argv[]) 1329 { 1330 int err; 1331 1332 if (argc != 2) { 1333 command_errmsg = "wrong number of arguments"; 1334 return (CMD_ERROR); 1335 } else { 1336 if ((err = unsetenv(argv[1])) != 0) { 1337 command_errmsg = strerror(err); 1338 return (CMD_ERROR); 1339 } 1340 } 1341 return (CMD_OK); 1342 } 1343 1344 static int 1345 command_echo(int argc, char *argv[]) 1346 { 1347 char *s; 1348 int nl, ch; 1349 1350 nl = 0; 1351 optind = 1; 1352 opterr = 1; 1353 while ((ch = getopt(argc, argv, "n")) != -1) { 1354 switch (ch) { 1355 case 'n': 1356 nl = 1; 1357 break; 1358 case '?': 1359 default: 1360 /* getopt has already reported an error */ 1361 return (CMD_OK); 1362 } 1363 } 1364 argv += (optind); 1365 argc -= (optind); 1366 1367 s = unargv(argc, argv); 1368 if (s != NULL) { 1369 printf("%s", s); 1370 free(s); 1371 } 1372 if (!nl) 1373 printf("\n"); 1374 return (CMD_OK); 1375 } 1376 1377 /* 1378 * A passable emulation of the sh(1) command of the same name. 1379 */ 1380 static int 1381 ischar(void) 1382 { 1383 return (1); 1384 } 1385 1386 static int 1387 command_read(int argc, char *argv[]) 1388 { 1389 char *prompt; 1390 int timeout; 1391 time_t when; 1392 char *cp; 1393 char *name; 1394 char buf[256]; /* XXX size? */ 1395 int c; 1396 1397 timeout = -1; 1398 prompt = NULL; 1399 optind = 1; 1400 opterr = 1; 1401 while ((c = getopt(argc, argv, "p:t:")) != -1) { 1402 switch (c) { 1403 case 'p': 1404 prompt = optarg; 1405 break; 1406 case 't': 1407 timeout = strtol(optarg, &cp, 0); 1408 if (cp == optarg) { 1409 snprintf(command_errbuf, 1410 sizeof (command_errbuf), 1411 "bad timeout '%s'", optarg); 1412 return (CMD_ERROR); 1413 } 1414 break; 1415 default: 1416 return (CMD_OK); 1417 } 1418 } 1419 1420 argv += (optind); 1421 argc -= (optind); 1422 name = (argc > 0) ? argv[0]: NULL; 1423 1424 if (prompt != NULL) 1425 printf("%s", prompt); 1426 if (timeout >= 0) { 1427 when = time(NULL) + timeout; 1428 while (!ischar()) 1429 if (time(NULL) >= when) 1430 return (CMD_OK); /* is timeout an error? */ 1431 } 1432 1433 ngets(buf, sizeof (buf)); 1434 1435 if (name != NULL) 1436 setenv(name, buf, 1); 1437 return (CMD_OK); 1438 } 1439 1440 /* 1441 * File pager 1442 */ 1443 static int 1444 command_more(int argc, char *argv[]) 1445 { 1446 int i; 1447 int res; 1448 char line[80]; 1449 char *name; 1450 1451 res = 0; 1452 pager_open(); 1453 for (i = 1; (i < argc) && (res == 0); i++) { 1454 sprintf(line, "*** FILE %s BEGIN ***\n", argv[i]); 1455 if (pager_output(line)) 1456 break; 1457 name = get_dev(argv[i]); 1458 res = page_file(name); 1459 free(name); 1460 if (!res) { 1461 sprintf(line, "*** FILE %s END ***\n", argv[i]); 1462 res = pager_output(line); 1463 } 1464 } 1465 pager_close(); 1466 1467 if (res == 0) 1468 return (CMD_OK); 1469 return (CMD_ERROR); 1470 } 1471 1472 static int 1473 page_file(char *filename) 1474 { 1475 int result; 1476 1477 result = pager_file(filename); 1478 1479 if (result == -1) { 1480 snprintf(command_errbuf, sizeof (command_errbuf), 1481 "error showing %s", filename); 1482 } 1483 1484 return (result); 1485 } 1486 1487 static int 1488 command_ls(int argc, char *argv[]) 1489 { 1490 DIR *dir; 1491 int fd; 1492 struct stat sb; 1493 struct dirent *d; 1494 char *buf, *path; 1495 char lbuf[128]; /* one line */ 1496 int result, ch; 1497 int verbose; 1498 1499 result = CMD_OK; 1500 fd = -1; 1501 verbose = 0; 1502 optind = 1; 1503 opterr = 1; 1504 while ((ch = getopt(argc, argv, "l")) != -1) { 1505 switch (ch) { 1506 case 'l': 1507 verbose = 1; 1508 break; 1509 case '?': 1510 default: 1511 /* getopt has already reported an error */ 1512 return (CMD_OK); 1513 } 1514 } 1515 argv += (optind - 1); 1516 argc -= (optind - 1); 1517 1518 if (argc < 2) { 1519 path = ""; 1520 } else { 1521 path = argv[1]; 1522 } 1523 1524 fd = ls_getdir(&path); 1525 if (fd == -1) { 1526 result = CMD_ERROR; 1527 goto out; 1528 } 1529 dir = fdopendir(fd); 1530 pager_open(); 1531 pager_output(path); 1532 pager_output("\n"); 1533 1534 while ((d = readdir(dir)) != NULL) { 1535 if (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) { 1536 /* stat the file, if possible */ 1537 sb.st_size = 0; 1538 sb.st_mode = 0; 1539 buf = malloc(strlen(path) + strlen(d->d_name) + 2); 1540 if (path[0] == '\0') 1541 sprintf(buf, "%s", d->d_name); 1542 else 1543 sprintf(buf, "%s/%s", path, d->d_name); 1544 /* ignore return, could be symlink, etc. */ 1545 if (stat(buf, &sb)) 1546 sb.st_size = 0; 1547 free(buf); 1548 if (verbose) { 1549 sprintf(lbuf, " %c %8d %s\n", 1550 typestr[sb.st_mode >> 12], 1551 (int)sb.st_size, d->d_name); 1552 } else { 1553 sprintf(lbuf, " %c %s\n", 1554 typestr[sb.st_mode >> 12], d->d_name); 1555 } 1556 if (pager_output(lbuf)) 1557 goto out; 1558 } 1559 } 1560 out: 1561 pager_close(); 1562 if (fd != -1) 1563 closedir(dir); 1564 if (path != NULL) 1565 free(path); 1566 return (result); 1567 } 1568 1569 /* 1570 * Given (path) containing a vaguely reasonable path specification, return an fd 1571 * on the directory, and an allocated copy of the path to the directory. 1572 */ 1573 static int 1574 ls_getdir(char **pathp) 1575 { 1576 struct stat sb; 1577 int fd; 1578 char *cp, *path; 1579 1580 fd = -1; 1581 1582 /* one extra byte for a possible trailing slash required */ 1583 path = malloc(strlen(*pathp) + 2); 1584 strcpy(path, *pathp); 1585 1586 /* Make sure the path is respectable to begin with */ 1587 if ((cp = get_dev(path)) == NULL) { 1588 snprintf(command_errbuf, sizeof (command_errbuf), 1589 "bad path '%s'", path); 1590 goto out; 1591 } 1592 1593 /* If there's no path on the device, assume '/' */ 1594 if (*cp == 0) 1595 strcat(path, "/"); 1596 1597 fd = open(cp, O_RDONLY); 1598 if (fd < 0) { 1599 snprintf(command_errbuf, sizeof (command_errbuf), 1600 "open '%s' failed: %s", path, strerror(errno)); 1601 goto out; 1602 } 1603 if (fstat(fd, &sb) < 0) { 1604 snprintf(command_errbuf, sizeof (command_errbuf), 1605 "stat failed: %s", strerror(errno)); 1606 goto out; 1607 } 1608 if (!S_ISDIR(sb.st_mode)) { 1609 snprintf(command_errbuf, sizeof (command_errbuf), 1610 "%s: %s", path, strerror(ENOTDIR)); 1611 goto out; 1612 } 1613 1614 free(cp); 1615 *pathp = path; 1616 return (fd); 1617 1618 out: 1619 free(cp); 1620 free(path); 1621 *pathp = NULL; 1622 if (fd != -1) 1623 close(fd); 1624 return (-1); 1625 } 1626 1627 static int 1628 command_include(int argc, char *argv[]) 1629 { 1630 int i; 1631 int res; 1632 char **argvbuf; 1633 1634 /* 1635 * Since argv is static, we need to save it here. 1636 */ 1637 argvbuf = (char **)calloc(argc, sizeof (char *)); 1638 for (i = 0; i < argc; i++) 1639 argvbuf[i] = strdup(argv[i]); 1640 1641 res = CMD_OK; 1642 for (i = 1; (i < argc) && (res == CMD_OK); i++) 1643 res = include(argvbuf[i]); 1644 1645 for (i = 0; i < argc; i++) 1646 free(argvbuf[i]); 1647 free(argvbuf); 1648 1649 return (res); 1650 } 1651 1652 /* 1653 * Header prepended to each line. The text immediately follows the header. 1654 * We try to make this short in order to save memory -- the loader has 1655 * limited memory available, and some of the forth files are very long. 1656 */ 1657 struct includeline 1658 { 1659 struct includeline *next; 1660 int line; 1661 char text[]; 1662 }; 1663 1664 int 1665 include(const char *filename) 1666 { 1667 struct includeline *script, *se, *sp; 1668 int res = CMD_OK; 1669 int prevsrcid, fd, line; 1670 char *cp, input[256]; /* big enough? */ 1671 char *path; 1672 1673 path = get_dev(filename); 1674 if (((fd = open(path, O_RDONLY)) == -1)) { 1675 snprintf(command_errbuf, sizeof (command_errbuf), 1676 "can't open '%s': %s", filename, 1677 strerror(errno)); 1678 free(path); 1679 return (CMD_ERROR); 1680 } 1681 1682 free(path); 1683 /* 1684 * Read the script into memory. 1685 */ 1686 script = se = NULL; 1687 line = 0; 1688 1689 while (fgetstr(input, sizeof (input), fd) >= 0) { 1690 line++; 1691 cp = input; 1692 /* Allocate script line structure and copy line, flags */ 1693 if (*cp == '\0') 1694 continue; /* ignore empty line, save memory */ 1695 if (cp[0] == '\\' && cp[1] == ' ') 1696 continue; /* ignore comment */ 1697 1698 sp = malloc(sizeof (struct includeline) + strlen(cp) + 1); 1699 /* 1700 * On malloc failure (it happens!), free as much as possible 1701 * and exit 1702 */ 1703 if (sp == NULL) { 1704 while (script != NULL) { 1705 se = script; 1706 script = script->next; 1707 free(se); 1708 } 1709 snprintf(command_errbuf, sizeof (command_errbuf), 1710 "file '%s' line %d: memory allocation " 1711 "failure - aborting", filename, line); 1712 return (CMD_ERROR); 1713 } 1714 strcpy(sp->text, cp); 1715 sp->line = line; 1716 sp->next = NULL; 1717 1718 if (script == NULL) { 1719 script = sp; 1720 } else { 1721 se->next = sp; 1722 } 1723 se = sp; 1724 } 1725 close(fd); 1726 1727 /* 1728 * Execute the script 1729 */ 1730 1731 prevsrcid = bf_vm->sourceId.i; 1732 bf_vm->sourceId.i = fd+1; /* 0 is user input device */ 1733 1734 res = CMD_OK; 1735 1736 for (sp = script; sp != NULL; sp = sp->next) { 1737 res = bf_run(sp->text); 1738 if (res != FICL_VM_STATUS_OUT_OF_TEXT) { 1739 snprintf(command_errbuf, sizeof (command_errbuf), 1740 "Error while including %s, in the line %d:\n%s", 1741 filename, sp->line, sp->text); 1742 res = CMD_ERROR; 1743 break; 1744 } else 1745 res = CMD_OK; 1746 } 1747 1748 bf_vm->sourceId.i = -1; 1749 (void) bf_run(""); 1750 bf_vm->sourceId.i = prevsrcid; 1751 1752 while (script != NULL) { 1753 se = script; 1754 script = script->next; 1755 free(se); 1756 } 1757 1758 return (res); 1759 } 1760 1761 static int 1762 command_boot(int argc, char *argv[]) 1763 { 1764 return (CMD_OK); 1765 } 1766 1767 static int 1768 command_autoboot(int argc, char *argv[]) 1769 { 1770 return (CMD_OK); 1771 } 1772 1773 static void 1774 moduledir_rebuild(void) 1775 { 1776 struct moduledir *mdp, *mtmp; 1777 const char *path, *cp, *ep; 1778 int cplen; 1779 1780 path = getenv("module_path"); 1781 if (path == NULL) 1782 path = default_searchpath; 1783 /* 1784 * Rebuild list of module directories if it changed 1785 */ 1786 STAILQ_FOREACH(mdp, &moduledir_list, d_link) 1787 mdp->d_flags |= MDIR_REMOVED; 1788 1789 for (ep = path; *ep != 0; ep++) { 1790 cp = ep; 1791 for (; *ep != 0 && *ep != ';'; ep++) 1792 ; 1793 /* 1794 * Ignore trailing slashes 1795 */ 1796 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; 1797 cplen--) 1798 ; 1799 STAILQ_FOREACH(mdp, &moduledir_list, d_link) { 1800 if (strlen(mdp->d_path) != cplen || 1801 bcmp(cp, mdp->d_path, cplen) != 0) 1802 continue; 1803 mdp->d_flags &= ~MDIR_REMOVED; 1804 break; 1805 } 1806 if (mdp == NULL) { 1807 mdp = malloc(sizeof (*mdp) + cplen + 1); 1808 if (mdp == NULL) 1809 return; 1810 mdp->d_path = (char *)(mdp + 1); 1811 bcopy(cp, mdp->d_path, cplen); 1812 mdp->d_path[cplen] = 0; 1813 mdp->d_hints = NULL; 1814 mdp->d_flags = 0; 1815 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link); 1816 } 1817 if (*ep == 0) 1818 break; 1819 } 1820 /* 1821 * Delete unused directories if any 1822 */ 1823 mdp = STAILQ_FIRST(&moduledir_list); 1824 while (mdp) { 1825 if ((mdp->d_flags & MDIR_REMOVED) == 0) { 1826 mdp = STAILQ_NEXT(mdp, d_link); 1827 } else { 1828 if (mdp->d_hints) 1829 free(mdp->d_hints); 1830 mtmp = mdp; 1831 mdp = STAILQ_NEXT(mdp, d_link); 1832 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link); 1833 free(mtmp); 1834 } 1835 } 1836 } 1837 1838 static char * 1839 file_lookup(const char *path, const char *name, int namelen) 1840 { 1841 struct stat st; 1842 char *result, *cp, *gz; 1843 int pathlen; 1844 1845 pathlen = strlen(path); 1846 result = malloc(pathlen + namelen + 2); 1847 if (result == NULL) 1848 return (NULL); 1849 bcopy(path, result, pathlen); 1850 if (pathlen > 0 && result[pathlen - 1] != '/') 1851 result[pathlen++] = '/'; 1852 cp = result + pathlen; 1853 bcopy(name, cp, namelen); 1854 cp += namelen; 1855 *cp = '\0'; 1856 if (stat(result, &st) == 0 && S_ISREG(st.st_mode)) 1857 return (result); 1858 /* also check for gz file */ 1859 (void) asprintf(&gz, "%s.gz", result); 1860 if (gz != NULL) { 1861 int res = stat(gz, &st); 1862 free(gz); 1863 if (res == 0) 1864 return (result); 1865 } 1866 free(result); 1867 return (NULL); 1868 } 1869 1870 static char * 1871 file_search(const char *name) 1872 { 1873 struct moduledir *mdp; 1874 struct stat sb; 1875 char *result; 1876 int namelen; 1877 1878 if (name == NULL) 1879 return (NULL); 1880 if (*name == 0) 1881 return (strdup(name)); 1882 1883 if (strchr(name, '/') != NULL) { 1884 char *gz; 1885 if (stat(name, &sb) == 0) 1886 return (strdup(name)); 1887 /* also check for gz file */ 1888 (void) asprintf(&gz, "%s.gz", name); 1889 if (gz != NULL) { 1890 int res = stat(gz, &sb); 1891 free(gz); 1892 if (res == 0) 1893 return (strdup(name)); 1894 } 1895 return (NULL); 1896 } 1897 1898 moduledir_rebuild(); 1899 result = NULL; 1900 namelen = strlen(name); 1901 STAILQ_FOREACH(mdp, &moduledir_list, d_link) { 1902 result = file_lookup(mdp->d_path, name, namelen); 1903 if (result) 1904 break; 1905 } 1906 return (result); 1907 } 1908 1909 static int 1910 command_load(int argc, char *argv[]) 1911 { 1912 int dofile, ch; 1913 char *typestr = NULL; 1914 char *filename; 1915 dofile = 0; 1916 optind = 1; 1917 1918 if (argc == 1) { 1919 command_errmsg = "no filename specified"; 1920 return (CMD_ERROR); 1921 } 1922 1923 while ((ch = getopt(argc, argv, "kt:")) != -1) { 1924 switch (ch) { 1925 case 'k': 1926 break; 1927 case 't': 1928 typestr = optarg; 1929 dofile = 1; 1930 break; 1931 case '?': 1932 default: 1933 return (CMD_OK); 1934 } 1935 } 1936 argv += (optind - 1); 1937 argc -= (optind - 1); 1938 if (dofile) { 1939 if ((typestr == NULL) || (*typestr == 0)) { 1940 command_errmsg = "invalid load type"; 1941 return (CMD_ERROR); 1942 } 1943 #if 0 1944 return (file_loadraw(argv[1], typestr, argc - 2, argv + 2, 1) 1945 ? CMD_OK : CMD_ERROR); 1946 #endif 1947 return (CMD_OK); 1948 } 1949 1950 filename = file_search(argv[1]); 1951 if (filename == NULL) { 1952 snprintf(command_errbuf, sizeof (command_errbuf), 1953 "can't find '%s'", argv[1]); 1954 return (CMD_ERROR); 1955 } 1956 setenv("kernelname", filename, 1); 1957 1958 return (CMD_OK); 1959 } 1960 1961 static int 1962 command_unload(int argc, char *argv[]) 1963 { 1964 unsetenv("kernelname"); 1965 return (CMD_OK); 1966 } 1967 1968 static int 1969 command_reboot(int argc, char *argv[]) 1970 { 1971 exit(0); 1972 return (CMD_OK); 1973 } 1974