The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/boot/common/commands.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/8.2/sys/boot/common/commands.c 198884 2009-11-04 13:06:09Z brueffer $");
   29 
   30 #include <stand.h>
   31 #include <string.h>
   32 
   33 #include "bootstrap.h"
   34 
   35 char            *command_errmsg;
   36 char            command_errbuf[256];    /* XXX should have procedural interface for setting, size limit? */
   37 
   38 static int page_file(char *filename);
   39 
   40 /*
   41  * Help is read from a formatted text file.
   42  *
   43  * Entries in the file are formatted as 
   44 
   45 # Ttopic [Ssubtopic] Ddescription
   46 help
   47 text
   48 here
   49 #
   50 
   51  *
   52  * Note that for code simplicity's sake, the above format must be followed
   53  * exactly.
   54  *
   55  * Subtopic entries must immediately follow the topic (this is used to
   56  * produce the listing of subtopics).
   57  *
   58  * If no argument(s) are supplied by the user, the help for 'help' is displayed.
   59  */
   60 COMMAND_SET(help, "help", "detailed help", command_help);
   61 
   62 static int
   63 help_getnext(int fd, char **topic, char **subtopic, char **desc) 
   64 {
   65     char        line[81], *cp, *ep;
   66     
   67     for (;;) {
   68         if (fgetstr(line, 80, fd) < 0)
   69             return(0);
   70         
   71         if ((strlen(line) < 3) || (line[0] != '#') || (line[1] != ' '))
   72             continue;
   73 
   74         *topic = *subtopic = *desc = NULL;
   75         cp = line + 2;
   76         while((cp != NULL) && (*cp != 0)) {
   77             ep = strchr(cp, ' ');
   78             if ((*cp == 'T') && (*topic == NULL)) {
   79                 if (ep != NULL)
   80                     *ep++ = 0;
   81                 *topic = strdup(cp + 1);
   82             } else if ((*cp == 'S') && (*subtopic == NULL)) {
   83                 if (ep != NULL)
   84                     *ep++ = 0;
   85                 *subtopic = strdup(cp + 1);
   86             } else if (*cp == 'D') {
   87                 *desc = strdup(cp + 1);
   88                 ep = NULL;
   89             }
   90             cp = ep;
   91         }
   92         if (*topic == NULL) {
   93             if (*subtopic != NULL)
   94                 free(*subtopic);
   95             if (*desc != NULL)
   96                 free(*desc);
   97             continue;
   98         }
   99         return(1);
  100     }
  101 }
  102 
  103 static int
  104 help_emitsummary(char *topic, char *subtopic, char *desc)
  105 {
  106     int         i;
  107     
  108     pager_output("    ");
  109     pager_output(topic);
  110     i = strlen(topic);
  111     if (subtopic != NULL) {
  112         pager_output(" ");
  113         pager_output(subtopic);
  114         i += strlen(subtopic) + 1;
  115     }
  116     if (desc != NULL) {
  117         do {
  118             pager_output(" ");
  119         } while (i++ < 30);
  120         pager_output(desc);
  121     }
  122     return (pager_output("\n"));
  123 }
  124 
  125             
  126 static int
  127 command_help(int argc, char *argv[]) 
  128 {
  129     char        buf[81];        /* XXX buffer size? */
  130     int         hfd, matched, doindex;
  131     char        *topic, *subtopic, *t, *s, *d;
  132 
  133     /* page the help text from our load path */
  134     sprintf(buf, "%s/boot/loader.help", getenv("loaddev"));
  135     if ((hfd = open(buf, O_RDONLY)) < 0) {
  136         printf("Verbose help not available, use '?' to list commands\n");
  137         return(CMD_OK);
  138     }
  139 
  140     /* pick up request from arguments */
  141     topic = subtopic = NULL;
  142     switch(argc) {
  143     case 3:
  144         subtopic = strdup(argv[2]);
  145     case 2:
  146         topic = strdup(argv[1]);
  147         break;
  148     case 1:
  149         topic = strdup("help");
  150         break;
  151     default:
  152         command_errmsg = "usage is 'help <topic> [<subtopic>]";
  153         close(hfd);
  154         return(CMD_ERROR);
  155     }
  156 
  157     /* magic "index" keyword */
  158     doindex = !strcmp(topic, "index");
  159     matched = doindex;
  160     
  161     /* Scan the helpfile looking for help matching the request */
  162     pager_open();
  163     while(help_getnext(hfd, &t, &s, &d)) {
  164 
  165         if (doindex) {          /* dink around formatting */
  166             if (help_emitsummary(t, s, d))
  167                 break;
  168 
  169         } else if (strcmp(topic, t)) {
  170             /* topic mismatch */
  171             if(matched)         /* nothing more on this topic, stop scanning */
  172                 break;
  173 
  174         } else {
  175             /* topic matched */
  176             matched = 1;
  177             if (((subtopic == NULL) && (s == NULL)) ||
  178                 ((subtopic != NULL) && (s != NULL) && !strcmp(subtopic, s))) {
  179                 /* exact match, print text */
  180                 while((fgetstr(buf, 80, hfd) >= 0) && (buf[0] != '#')) {
  181                     if (pager_output(buf))
  182                         break;
  183                     if (pager_output("\n"))
  184                         break;
  185                 }
  186             } else if ((subtopic == NULL) && (s != NULL)) {
  187                 /* topic match, list subtopics */
  188                 if (help_emitsummary(t, s, d))
  189                     break;
  190             }
  191         }
  192         free(t);
  193         free(s);
  194         free(d);
  195     }
  196     pager_close();
  197     close(hfd);
  198     if (!matched) {
  199         sprintf(command_errbuf, "no help available for '%s'", topic);
  200         free(topic);
  201         if (subtopic)
  202             free(subtopic);
  203         return(CMD_ERROR);
  204     }
  205     free(topic);
  206     if (subtopic)
  207         free(subtopic);
  208     return(CMD_OK);
  209 }
  210 
  211 
  212 COMMAND_SET(commandlist, "?", "list commands", command_commandlist);
  213 
  214 static int
  215 command_commandlist(int argc, char *argv[])
  216 {
  217     struct bootblk_command      **cmdp;
  218     int         res;
  219     char        name[20];
  220 
  221     res = 0;
  222     pager_open();
  223     res = pager_output("Available commands:\n");
  224     SET_FOREACH(cmdp, Xcommand_set) {
  225         if (res)
  226             break;
  227         if (((*cmdp)->c_name != NULL) && ((*cmdp)->c_desc != NULL)) {
  228             sprintf(name, "  %-15s  ", (*cmdp)->c_name);
  229             pager_output(name);
  230             pager_output((*cmdp)->c_desc);
  231             res = pager_output("\n");
  232         }
  233     }
  234     pager_close();
  235     return(CMD_OK);
  236 }
  237 
  238 /*
  239  * XXX set/show should become set/echo if we have variable
  240  * substitution happening.
  241  */
  242 
  243 COMMAND_SET(show, "show", "show variable(s)", command_show);
  244 
  245 static int
  246 command_show(int argc, char *argv[])
  247 {
  248     struct env_var      *ev;
  249     char                *cp;
  250 
  251     if (argc < 2) {
  252         /* 
  253          * With no arguments, print everything.
  254          */
  255         pager_open();
  256         for (ev = environ; ev != NULL; ev = ev->ev_next) {
  257             pager_output(ev->ev_name);
  258             cp = getenv(ev->ev_name);
  259             if (cp != NULL) {
  260                 pager_output("=");
  261                 pager_output(cp);
  262             }
  263             if (pager_output("\n"))
  264                 break;
  265         }
  266         pager_close();
  267     } else {
  268         if ((cp = getenv(argv[1])) != NULL) {
  269             printf("%s\n", cp);
  270         } else {
  271             sprintf(command_errbuf, "variable '%s' not found", argv[1]);
  272             return(CMD_ERROR);
  273         }
  274     }
  275     return(CMD_OK);
  276 }
  277 
  278 COMMAND_SET(set, "set", "set a variable", command_set);
  279 
  280 static int
  281 command_set(int argc, char *argv[])
  282 {
  283     int         err;
  284     
  285     if (argc != 2) {
  286         command_errmsg = "wrong number of arguments";
  287         return(CMD_ERROR);
  288     } else {
  289         if ((err = putenv(argv[1])) != 0) {
  290             command_errmsg = strerror(err);
  291             return(CMD_ERROR);
  292         }
  293     }
  294     return(CMD_OK);
  295 }
  296 
  297 COMMAND_SET(unset, "unset", "unset a variable", command_unset);
  298 
  299 static int
  300 command_unset(int argc, char *argv[]) 
  301 {
  302     int         err;
  303     
  304     if (argc != 2) {
  305         command_errmsg = "wrong number of arguments";
  306         return(CMD_ERROR);
  307     } else {
  308         if ((err = unsetenv(argv[1])) != 0) {
  309             command_errmsg = strerror(err);
  310             return(CMD_ERROR);
  311         }
  312     }
  313     return(CMD_OK);
  314 }
  315 
  316 COMMAND_SET(echo, "echo", "echo arguments", command_echo);
  317 
  318 static int
  319 command_echo(int argc, char *argv[])
  320 {
  321     char        *s;
  322     int         nl, ch;
  323     
  324     nl = 0;
  325     optind = 1;
  326     optreset = 1;
  327     while ((ch = getopt(argc, argv, "n")) != -1) {
  328         switch(ch) {
  329         case 'n':
  330             nl = 1;
  331             break;
  332         case '?':
  333         default:
  334             /* getopt has already reported an error */
  335             return(CMD_OK);
  336         }
  337     }
  338     argv += (optind);
  339     argc -= (optind);
  340 
  341     s = unargv(argc, argv);
  342     if (s != NULL) {
  343         printf("%s", s);
  344         free(s);
  345     }
  346     if (!nl)
  347         printf("\n");
  348     return(CMD_OK);
  349 }
  350 
  351 /*
  352  * A passable emulation of the sh(1) command of the same name.
  353  */
  354 
  355 COMMAND_SET(read, "read", "read input from the terminal", command_read);
  356 
  357 static int
  358 command_read(int argc, char *argv[])
  359 {
  360     char        *prompt;
  361     int         timeout;
  362     time_t      when;
  363     char        *cp;
  364     char        *name;
  365     char        buf[256];               /* XXX size? */
  366     int         c;
  367     
  368     timeout = -1;
  369     prompt = NULL;
  370     optind = 1;
  371     optreset = 1;
  372     while ((c = getopt(argc, argv, "p:t:")) != -1) {
  373         switch(c) {
  374             
  375         case 'p':
  376             prompt = optarg;
  377             break;
  378         case 't':
  379             timeout = strtol(optarg, &cp, 0);
  380             if (cp == optarg) {
  381                 sprintf(command_errbuf, "bad timeout '%s'", optarg);
  382                 return(CMD_ERROR);
  383             }
  384             break;
  385         default:
  386             return(CMD_OK);
  387         }
  388     }
  389 
  390     argv += (optind);
  391     argc -= (optind);
  392     name = (argc > 0) ? argv[0]: NULL;
  393         
  394     if (prompt != NULL)
  395         printf("%s", prompt);
  396     if (timeout >= 0) {
  397         when = time(NULL) + timeout;
  398         while (!ischar())
  399             if (time(NULL) >= when)
  400                 return(CMD_OK);         /* is timeout an error? */
  401     }
  402 
  403     ngets(buf, sizeof(buf));
  404 
  405     if (name != NULL)
  406         setenv(name, buf, 1);
  407     return(CMD_OK);
  408 }
  409 
  410 /*
  411  * File pager
  412  */
  413 COMMAND_SET(more, "more", "show contents of a file", command_more);
  414 
  415 static int
  416 command_more(int argc, char *argv[])
  417 {
  418     int         i;
  419     int         res;
  420     char        line[80];
  421 
  422     res=0;
  423     pager_open();
  424     for (i = 1; (i < argc) && (res == 0); i++) {
  425         sprintf(line, "*** FILE %s BEGIN ***\n", argv[i]);
  426         if (pager_output(line))
  427                 break;
  428         res = page_file(argv[i]);
  429         if (!res) {
  430             sprintf(line, "*** FILE %s END ***\n", argv[i]);
  431             res = pager_output(line);
  432         }
  433     }
  434     pager_close();
  435 
  436     if (res == 0)
  437         return CMD_OK;
  438     else
  439         return CMD_ERROR;
  440 }
  441 
  442 static int
  443 page_file(char *filename)
  444 {
  445     int result;
  446 
  447     result = pager_file(filename);
  448 
  449     if (result == -1)
  450         sprintf(command_errbuf, "error showing %s", filename);
  451 
  452     return result;
  453 }   
  454 
  455 /*
  456  * List all disk-like devices
  457  */
  458 COMMAND_SET(lsdev, "lsdev", "list all devices", command_lsdev);
  459 
  460 static int
  461 command_lsdev(int argc, char *argv[])
  462 {
  463     int         verbose, ch, i;
  464     char        line[80];
  465     
  466     verbose = 0;
  467     optind = 1;
  468     optreset = 1;
  469     while ((ch = getopt(argc, argv, "v")) != -1) {
  470         switch(ch) {
  471         case 'v':
  472             verbose = 1;
  473             break;
  474         case '?':
  475         default:
  476             /* getopt has already reported an error */
  477             return(CMD_OK);
  478         }
  479     }
  480     argv += (optind);
  481     argc -= (optind);
  482 
  483     pager_open();
  484     for (i = 0; devsw[i] != NULL; i++) {
  485         if (devsw[i]->dv_print != NULL){
  486             sprintf(line, "%s devices:\n", devsw[i]->dv_name);
  487             if (pager_output(line))
  488                     break;
  489             devsw[i]->dv_print(verbose);
  490         } else {
  491             sprintf(line, "%s: (unknown)\n", devsw[i]->dv_name);
  492             if (pager_output(line))
  493                     break;
  494         }
  495     }
  496     pager_close();
  497     return(CMD_OK);
  498 }
  499 

Cache object: 786226582f394e1c7423f1eb50d6678f


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.