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 More...
 
int shell (void)
 Start command shell. More...
 
static int shell_exec (int argc, char **argv)
 "shell" command More...
 

Variables

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

Detailed Description

Minimal command shell.

Definition in file shell.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ help_exec()

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.

53  {
54  struct command *command;
55  unsigned int hpos = 0;
56 
57  printf ( "\nAvailable commands:\n\n" );
59  hpos += printf ( " %s", command->name );
60  if ( hpos > ( 16 * 4 ) ) {
61  printf ( "\n" );
62  hpos = 0;
63  } else {
64  while ( hpos % 16 ) {
65  printf ( " " );
66  hpos++;
67  }
68  }
69  }
70  printf ( "\n\nType \"<command> --help\" for further information\n\n" );
71  return 0;
72 }
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
A command-line command.
Definition: command.h:9
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
static int command
Definition: epic100.c:68
const char * name
Name of the command.
Definition: command.h:11
#define COMMANDS
Definition: command.h:22

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

◆ shell()

int shell ( void  )

Start command shell.

Definition at line 84 of file shell.c.

84  {
85  struct readline_history history;
86  char *line;
87  int rc = 0;
88 
89  /* Initialise shell history */
90  memset ( &history, 0, sizeof ( history ) );
91 
92  /* Read and execute commands */
93  do {
94  readline_history ( shell_prompt, NULL, &history, 0, &line );
95  if ( line ) {
96  rc = system ( line );
97  free ( line );
98  }
100 
101  /* Discard shell history */
102  history_free ( &history );
103 
104  return rc;
105 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
A readline history buffer.
Definition: readline.h:31
int readline_history(const char *prompt, const char *prefill, struct readline_history *history, unsigned long timeout, char **line)
Read line from console (with history)
Definition: readline.c:257
static const char shell_prompt[]
The shell prompt string.
Definition: shell.c:44
int shell_stopped(int stop)
Test and consume shell stop state.
Definition: exec.c:227
void history_free(struct readline_history *history)
Free history buffer.
Definition: readline.c:231
Stop processing commands.
Definition: shell.h:29
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
uint8_t system[ETH_ALEN]
System identifier.
Definition: eth_slow.h:24
void * memset(void *dest, int character, size_t len) __nonnull

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

Referenced by interrupt(), ipxe(), and shell_exec().

◆ shell_exec()

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.

124  {
125  struct shell_options opts;
126  int rc;
127 
128  /* Parse options */
129  if ( ( rc = parse_options ( argc, argv, &shell_cmd, &opts ) ) != 0 )
130  return rc;
131 
132  /* Start shell */
133  if ( ( rc = shell() ) != 0 )
134  return rc;
135 
136  return 0;
137 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
int shell(void)
Start command shell.
Definition: shell.c:84
static struct command_descriptor shell_cmd
"shell" command descriptor
Definition: shell.c:114
"shell" options
Definition: shell.c:108
static union @437 opts
"cert<xxx>" option list

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

Variable Documentation

◆ shell_prompt

const char shell_prompt[] = PRODUCT_SHORT_NAME "> "
static

The shell prompt string.

Definition at line 44 of file shell.c.

Referenced by shell().

◆ __command

struct command shell_command __command
Initial value:
= {
.name = "help",
.exec = help_exec,
}
static int help_exec(int argc __unused, char **argv __unused)
"help" command
Definition: shell.c:53

"help" command

"shell" command

Definition at line 75 of file shell.c.

◆ shell_opts

struct option_descriptor shell_opts[] = {}
static

"shell" option list

Definition at line 111 of file shell.c.

◆ shell_cmd

struct command_descriptor shell_cmd
static
Initial value:
=
static struct option_descriptor shell_opts[]
"shell" option list
Definition: shell.c:111
"shell" options
Definition: shell.c:108
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

"shell" command descriptor

Definition at line 114 of file shell.c.

Referenced by shell_exec().