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 COMMAND ( help, help_exec );
76 
77 /**
78  * Start command shell
79  *
80  */
81 int shell ( void ) {
82  struct readline_history history;
83  char *line;
84  int rc = 0;
85 
86  /* Initialise shell history */
87  memset ( &history, 0, sizeof ( history ) );
88 
89  /* Read and execute commands */
90  do {
91  readline_history ( shell_prompt, NULL, &history, 0, &line );
92  if ( line ) {
93  rc = system ( line );
94  free ( line );
95  }
97 
98  /* Discard shell history */
99  history_free ( &history );
100 
101  return rc;
102 }
103 
104 /** "shell" options */
105 struct shell_options {};
106 
107 /** "shell" option list */
108 static struct option_descriptor shell_opts[] = {};
109 
110 /** "shell" command descriptor */
112  COMMAND_DESC ( struct shell_options, shell_opts, 0, 0, NULL );
113 
114 /**
115  * "shell" command
116  *
117  * @v argc Argument count
118  * @v argv Argument list
119  * @ret rc Return status code
120  */
121 static int shell_exec ( int argc, char **argv ) {
122  struct shell_options opts;
123  int rc;
124 
125  /* Parse options */
126  if ( ( rc = parse_options ( argc, argv, &shell_cmd, &opts ) ) != 0 )
127  return rc;
128 
129  /* Start shell */
130  if ( ( rc = shell() ) != 0 )
131  return rc;
132 
133  return 0;
134 }
135 
136 /** "shell" command */
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:81
Branding configuration.
A command descriptor.
Definition: parseopt.h:77
Minmal readline.
Parse command-line options.
#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:121
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:108
static struct command_descriptor shell_cmd
"shell" command descriptor
Definition: shell.c:111
Command line option parsing.
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
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
"shell" options
Definition: shell.c:105
static int command
Definition: epic100.c:68
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
void history_free(struct readline_history *history)
Free history buffer.
Definition: readline.c:231
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
static union @447 opts
"cert<xxx>" option list
#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.
COMMAND(help, help_exec)
"help" command
uint8_t system[ETH_ALEN]
System identifier.
Definition: eth_slow.h:24
void * memset(void *dest, int character, size_t len) __nonnull