iPXE
Data Structures | Functions | Variables
shell.c File Reference

Minimal command shell. More...

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <readline/readline.h>
#include <ipxe/command.h>
#include <ipxe/parseopt.h>
#include <ipxe/shell.h>
#include <config/branding.h>

Go to the source code of this file.

Data Structures

struct  shell_options
 "shell" options More...

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static int help_exec (int argc __unused, char **argv __unused)
 "help" command
int shell (void)
 Start command shell.
static int shell_exec (int argc, char **argv)
 "shell" command

Variables

static const char shell_prompt [] = "> "
 The shell prompt string.
struct command help_command __command
 "help" command
static struct option_descriptor shell_opts [] = {}
 "shell" option list
static struct command_descriptor shell_cmd
 "shell" command descriptor

Detailed Description

Minimal command shell.

Definition in file shell.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static int help_exec ( int argc  __unused,
char **argv  __unused 
) [static]

"help" command

Parameters:
argcArgument count
argvArgument list
Return values:
rcReturn status code

Definition at line 53 of file shell.c.

References command, COMMANDS, for_each_table_entry, command::name, and printf().

                                                                 {
        struct command *command;
        unsigned int hpos = 0;

        printf ( "\nAvailable commands:\n\n" );
        for_each_table_entry ( command, COMMANDS ) {
                hpos += printf ( "  %s", command->name );
                if ( hpos > ( 16 * 4 ) ) {
                        printf ( "\n" );
                        hpos = 0;
                } else {
                        while ( hpos % 16 ) {
                                printf ( " " );
                                hpos++;
                        }
                }
        }
        printf ( "\n\nType \"<command> --help\" for further information\n\n" );
        return 0;
}
int shell ( void  )

Start command shell.

Definition at line 84 of file shell.c.

References free, history_free(), memset(), NULL, rc, readline_history(), shell_prompt, SHELL_STOP_COMMAND_SEQUENCE, shell_stopped(), and system.

Referenced by ipxe(), and shell_exec().

                   {
        struct readline_history history;
        char *line;
        int rc = 0;

        /* Initialise shell history */
        memset ( &history, 0, sizeof ( history ) );

        /* Read and execute commands */
        do {
                readline_history ( shell_prompt, NULL, &history, &line );
                if ( line ) {
                        rc = system ( line );
                        free ( line );
                }
        } while ( ! shell_stopped ( SHELL_STOP_COMMAND_SEQUENCE ) );

        /* Discard shell history */
        history_free ( &history );

        return rc;
}
static int shell_exec ( int  argc,
char **  argv 
) [static]

"shell" command

Parameters:
argcArgument count
argvArgument list
Return values:
rcReturn status code

Definition at line 124 of file shell.c.

References parse_options(), rc, and shell().

                                                {
        struct shell_options opts;
        int rc;

        /* Parse options */
        if ( ( rc = parse_options ( argc, argv, &shell_cmd, &opts ) ) != 0 )
                return rc;

        /* Start shell */
        if ( ( rc = shell() ) != 0 )
                return rc;

        return 0;
}

Variable Documentation

const char shell_prompt[] = "> " [static]

The shell prompt string.

Definition at line 44 of file shell.c.

Referenced by shell().

struct command shell_command __command
Initial value:
 {
        .name = "help",
        .exec = help_exec,
}

"help" command

"shell" command

Definition at line 75 of file shell.c.

struct option_descriptor shell_opts[] = {} [static]

"shell" option list

Definition at line 111 of file shell.c.

struct command_descriptor shell_cmd [static]
Initial value:

"shell" command descriptor

Definition at line 114 of file shell.c.