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 
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <getopt.h>
31 #include <readline/readline.h>
32 #include <ipxe/command.h>
33 #include <ipxe/parseopt.h>
34 #include <ipxe/shell.h>
35 #include <config/branding.h>
36 
37 /** @file
38  *
39  * Minimal command shell
40  *
41  */
42 
43 /** The shell prompt string */
44 static const char shell_prompt[] = PRODUCT_SHORT_NAME "> ";
45 
46 /**
47  * "help" command
48  *
49  * @v argc Argument count
50  * @v argv Argument list
51  * @ret rc Return status code
52  */
53 static int help_exec ( int argc __unused, char **argv __unused ) {
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 }
73 
74 /** "help" command */
75 struct command help_command __command = {
76  .name = "help",
77  .exec = help_exec,
78 };
79 
80 /**
81  * Start command shell
82  *
83  */
84 int shell ( void ) {
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 }
106 
107 /** "shell" options */
108 struct shell_options {};
109 
110 /** "shell" option list */
111 static struct option_descriptor shell_opts[] = {};
112 
113 /** "shell" command descriptor */
115  COMMAND_DESC ( struct shell_options, shell_opts, 0, 0, NULL );
116 
117 /**
118  * "shell" command
119  *
120  * @v argc Argument count
121  * @v argv Argument list
122  * @ret rc Return status code
123  */
124 static int shell_exec ( int argc, char **argv ) {
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 }
138 
139 /** "shell" command */
140 struct command shell_command __command = {
141  .name = "shell",
142  .exec = shell_exec,
143 };
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define PRODUCT_SHORT_NAME
Definition: branding.h:28
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
Minimal command shell.
A command-line command.
Definition: command.h:9
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
Branding configuration.
A command descriptor.
Definition: parseopt.h:77
Minmal readline.
Parse command-line options.
static int shell_exec(int argc, char **argv)
"shell" command
Definition: shell.c:124
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
A readline history buffer.
Definition: readline.h:31
static struct option_descriptor shell_opts[]
"shell" option list
Definition: shell.c:111
static struct command_descriptor shell_cmd
"shell" command descriptor
Definition: shell.c:114
Command line option parsing.
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
struct command help_command __command
"help" command
Definition: shell.c:75
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:108
static int command
Definition: epic100.c:68
static union @437 opts
"cert<xxx>" option list
static const char shell_prompt[]
The shell prompt string.
Definition: shell.c:44
const char * name
Name of the command.
Definition: command.h:11
int shell_stopped(int stop)
Test and consume shell stop state.
Definition: exec.c:227
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
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:53
#define COMMANDS
Definition: command.h:22
Stop processing commands.
Definition: shell.h:29
A command-line option descriptor.
Definition: parseopt.h:23
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
uint8_t system[ETH_ALEN]
System identifier.
Definition: eth_slow.h:24
void * memset(void *dest, int character, size_t len) __nonnull