Annotation of qemu/roms/ipxe/src/hci/shell.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (C) 2006 Michael Brown <[email protected]>.
        !             3:  *
        !             4:  * This program is free software; you can redistribute it and/or
        !             5:  * modify it under the terms of the GNU General Public License as
        !             6:  * published by the Free Software Foundation; either version 2 of the
        !             7:  * License, or any later version.
        !             8:  *
        !             9:  * This program is distributed in the hope that it will be useful, but
        !            10:  * WITHOUT ANY WARRANTY; without even the implied warranty of
        !            11:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
        !            12:  * General Public License for more details.
        !            13:  *
        !            14:  * You should have received a copy of the GNU General Public License
        !            15:  * along with this program; if not, write to the Free Software
        !            16:  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
        !            17:  */
        !            18: 
        !            19: FILE_LICENCE ( GPL2_OR_LATER );
        !            20: 
        !            21: #include <stdint.h>
        !            22: #include <stdlib.h>
        !            23: #include <stdio.h>
        !            24: #include <string.h>
        !            25: #include <getopt.h>
        !            26: #include <readline/readline.h>
        !            27: #include <ipxe/command.h>
        !            28: #include <ipxe/parseopt.h>
        !            29: #include <ipxe/shell.h>
        !            30: 
        !            31: /** @file
        !            32:  *
        !            33:  * Minimal command shell
        !            34:  *
        !            35:  */
        !            36: 
        !            37: /** The shell prompt string */
        !            38: static const char shell_prompt[] = "iPXE> ";
        !            39: 
        !            40: /**
        !            41:  * "help" command
        !            42:  *
        !            43:  * @v argc             Argument count
        !            44:  * @v argv             Argument list
        !            45:  * @ret rc             Return status code
        !            46:  */
        !            47: static int help_exec ( int argc __unused, char **argv __unused ) {
        !            48:        struct command *command;
        !            49:        unsigned int hpos = 0;
        !            50: 
        !            51:        printf ( "\nAvailable commands:\n\n" );
        !            52:        for_each_table_entry ( command, COMMANDS ) {
        !            53:                hpos += printf ( "  %s", command->name );
        !            54:                if ( hpos > ( 16 * 4 ) ) {
        !            55:                        printf ( "\n" );
        !            56:                        hpos = 0;
        !            57:                } else {
        !            58:                        while ( hpos % 16 ) {
        !            59:                                printf ( " " );
        !            60:                                hpos++;
        !            61:                        }
        !            62:                }
        !            63:        }
        !            64:        printf ( "\n\nType \"<command> --help\" for further information\n\n" );
        !            65:        return 0;
        !            66: }
        !            67: 
        !            68: /** "help" command */
        !            69: struct command help_command __command = {
        !            70:        .name = "help",
        !            71:        .exec = help_exec,
        !            72: };
        !            73: 
        !            74: /**
        !            75:  * Start command shell
        !            76:  *
        !            77:  */
        !            78: int shell ( void ) {
        !            79:        struct readline_history history;
        !            80:        char *line;
        !            81:        int rc = 0;
        !            82: 
        !            83:        /* Initialise shell history */
        !            84:        memset ( &history, 0, sizeof ( history ) );
        !            85: 
        !            86:        /* Read and execute commands */
        !            87:        do {
        !            88:                line = readline_history ( shell_prompt, &history );
        !            89:                if ( line ) {
        !            90:                        rc = system ( line );
        !            91:                        free ( line );
        !            92:                }
        !            93:        } while ( ! shell_stopped ( SHELL_STOP_COMMAND_SEQUENCE ) );
        !            94: 
        !            95:        /* Discard shell history */
        !            96:        history_free ( &history );
        !            97: 
        !            98:        return rc;
        !            99: }
        !           100: 
        !           101: /** "shell" options */
        !           102: struct shell_options {};
        !           103: 
        !           104: /** "shell" option list */
        !           105: static struct option_descriptor shell_opts[] = {};
        !           106: 
        !           107: /** "shell" command descriptor */
        !           108: static struct command_descriptor shell_cmd =
        !           109:        COMMAND_DESC ( struct shell_options, shell_opts, 0, 0, "" );
        !           110: 
        !           111: /**
        !           112:  * "shell" command
        !           113:  *
        !           114:  * @v argc             Argument count
        !           115:  * @v argv             Argument list
        !           116:  * @ret rc             Return status code
        !           117:  */
        !           118: static int shell_exec ( int argc, char **argv ) {
        !           119:        struct shell_options opts;
        !           120:        int rc;
        !           121: 
        !           122:        /* Parse options */
        !           123:        if ( ( rc = parse_options ( argc, argv, &shell_cmd, &opts ) ) != 0 )
        !           124:                return rc;
        !           125: 
        !           126:        /* Start shell */
        !           127:        if ( ( rc = shell() ) != 0 )
        !           128:                return rc;
        !           129: 
        !           130:        return 0;
        !           131: }
        !           132: 
        !           133: /** "shell" command */
        !           134: struct command shell_command __command = {
        !           135:        .name = "shell",
        !           136:        .exec = shell_exec,
        !           137: };

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.