iPXE
shell.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 FILE_SECBOOT ( PERMITTED );
26 
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <getopt.h>
32 #include <readline/readline.h>
33 #include <ipxe/command.h>
34 #include <ipxe/parseopt.h>
35 #include <ipxe/shell.h>
36 #include <config/branding.h>
37 
38 /** @file
39  *
40  * Minimal command shell
41  *
42  */
43 
44 /** The shell prompt string */
45 static const char shell_prompt[] = PRODUCT_SHORT_NAME "> ";
46 
47 /**
48  * "help" command
49  *
50  * @v argc Argument count
51  * @v argv Argument list
52  * @ret rc Return status code
53  */
54 static int help_exec ( int argc __unused, char **argv __unused ) {
55  struct command *command;
56  unsigned int hpos = 0;
57 
58  printf ( "\nAvailable commands:\n\n" );
60  hpos += printf ( " %s", command->name );
61  if ( hpos > ( 16 * 4 ) ) {
62  printf ( "\n" );
63  hpos = 0;
64  } else {
65  while ( hpos % 16 ) {
66  printf ( " " );
67  hpos++;
68  }
69  }
70  }
71  printf ( "\n\nType \"<command> --help\" for further information\n\n" );
72  return 0;
73 }
74 
75 /** "help" command */
76 COMMAND ( help, help_exec );
77 
78 /**
79  * Start command shell
80  *
81  */
82 int shell ( void ) {
83  struct readline_history history;
84  char *line;
85  int rc = 0;
86 
87  /* Initialise shell history */
88  memset ( &history, 0, sizeof ( history ) );
89 
90  /* Read and execute commands */
91  do {
92  readline_history ( shell_prompt, NULL, &history, 0, &line );
93  if ( line ) {
94  rc = system ( line );
95  free ( line );
96  }
98 
99  /* Discard shell history */
100  history_free ( &history );
101 
102  return rc;
103 }
104 
105 /** "shell" options */
106 struct shell_options {};
107 
108 /** "shell" option list */
109 static struct option_descriptor shell_opts[] = {};
110 
111 /** "shell" command descriptor */
113  COMMAND_DESC ( struct shell_options, shell_opts, 0, 0, NULL );
114 
115 /**
116  * "shell" command
117  *
118  * @v argc Argument count
119  * @v argv Argument list
120  * @ret rc Return status code
121  */
122 static int shell_exec ( int argc, char **argv ) {
123  struct shell_options opts;
124  int rc;
125 
126  /* Parse options */
127  if ( ( rc = parse_options ( argc, argv, &shell_cmd, &opts ) ) != 0 )
128  return rc;
129 
130  /* Start shell */
131  if ( ( rc = shell() ) != 0 )
132  return rc;
133 
134  return 0;
135 }
136 
137 /** "shell" command */
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define PRODUCT_SHORT_NAME
Definition: branding.h:29
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:465
Minimal command shell.
A command-line command.
Definition: command.h:10
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:485
int shell(void)
Start command shell.
Definition: shell.c:82
Branding configuration.
A command descriptor.
Definition: parseopt.h:78
Minmal readline.
Parse command-line options.
FILE_SECBOOT(PERMITTED)
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
static int shell_exec(int argc, char **argv)
"shell" command
Definition: shell.c:122
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
A readline history buffer.
Definition: readline.h:32
static struct option_descriptor shell_opts[]
"shell" option list
Definition: shell.c:109
static struct command_descriptor shell_cmd
"shell" command descriptor
Definition: shell.c:112
Command line option parsing.
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:386
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:258
"shell" options
Definition: shell.c:106
static int command
Definition: epic100.c:68
static const char shell_prompt[]
The shell prompt string.
Definition: shell.c:45
const char * name
Name of the command.
Definition: command.h:12
int shell_stopped(int stop)
Test and consume shell stop state.
Definition: exec.c:228
void history_free(struct readline_history *history)
Free history buffer.
Definition: readline.c:232
static int help_exec(int argc __unused, char **argv __unused)
"help" command
Definition: shell.c:54
#define COMMANDS
Definition: command.h:23
Stop processing commands.
Definition: shell.h:30
A command-line option descriptor.
Definition: parseopt.h:24
static union @447 opts
"cert<xxx>" option list
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:109
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
String functions.
COMMAND(help, help_exec)
"help" command
uint8_t system[ETH_ALEN]
System identifier.
Definition: eth_slow.h:25
void * memset(void *dest, int character, size_t len) __nonnull