iPXE
console_cmd.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 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 /** @file
27  *
28  * Console management commands
29  *
30  */
31 
32 #include <string.h>
33 #include <getopt.h>
34 #include <ipxe/command.h>
35 #include <ipxe/parseopt.h>
36 #include <ipxe/console.h>
37 #include <ipxe/image.h>
38 #include <ipxe/pixbuf.h>
39 #include <ipxe/ansiesc.h>
40 #include <ipxe/ansicol.h>
41 #include <usr/imgmgmt.h>
42 
43 /** "console" options */
45  /** Console configuration */
47  /** Picture URI */
48  char *picture;
49  /** Keep picture after configuration */
50  int keep;
51 };
52 
53 /** "console" option list */
54 static struct option_descriptor console_opts[] = {
55  OPTION_DESC ( "x", 'x', required_argument,
56  struct console_options, config.width, parse_integer ),
57  OPTION_DESC ( "y", 'y', required_argument,
58  struct console_options, config.height, parse_integer ),
59  OPTION_DESC ( "left", 'l', required_argument,
60  struct console_options, config.left, parse_integer ),
61  OPTION_DESC ( "right", 'r', required_argument,
62  struct console_options, config.right, parse_integer ),
63  OPTION_DESC ( "top", 't', required_argument,
64  struct console_options, config.top, parse_integer ),
65  OPTION_DESC ( "bottom", 'b', required_argument,
66  struct console_options, config.bottom, parse_integer ),
67  OPTION_DESC ( "depth", 'd', required_argument,
68  struct console_options, config.depth, parse_integer ),
69  OPTION_DESC ( "picture", 'p', required_argument,
70  struct console_options, picture, parse_string ),
71  OPTION_DESC ( "keep", 'k', no_argument,
72  struct console_options, keep, parse_flag ),
73 };
74 
75 /** "console" command descriptor */
77  COMMAND_DESC ( struct console_options, console_opts, 0, 0, NULL );
78 
79 /**
80  * "console" command
81  *
82  * @v argc Argument count
83  * @v argv Argument list
84  * @ret rc Return status code
85  */
86 static int console_exec ( int argc, char **argv ) {
87  struct console_options opts;
88  struct image *image = NULL;
89  int rc;
90 
91  /* Parse options */
92  if ( ( rc = parse_options ( argc, argv, &console_cmd, &opts ) ) != 0 )
93  goto err_parse;
94 
95  /* Handle background picture, if applicable */
96  if ( opts.picture ) {
97 
98  /* Acquire image */
99  if ( ( rc = imgacquire ( opts.picture, 0, &image ) ) != 0 )
100  goto err_acquire;
101 
102  /* Convert to pixel buffer */
103  if ( ( rc = image_pixbuf ( image, &opts.config.pixbuf ) ) != 0){
104  printf ( "Could not use picture: %s\n",
105  strerror ( rc ) );
106  goto err_pixbuf;
107  }
108 
109  /* Apply image's width and height if none specified */
110  if ( ! opts.config.width )
111  opts.config.width = opts.config.pixbuf->width;
112  if ( ! opts.config.height )
113  opts.config.height = opts.config.pixbuf->height;
114  }
115 
116  /* Configure console */
117  if ( ( rc = console_configure ( &opts.config ) ) != 0 ) {
118  printf ( "Could not configure console: %s\n", strerror ( rc ) );
119  goto err_configure;
120  }
121 
122  /* Reapply default colour pair and clear screen */
124  printf ( CSI "2J" CSI "H" );
125 
126  err_configure:
127  pixbuf_put ( opts.config.pixbuf );
128  err_pixbuf:
129  /* Discard image unless --keep was specified */
130  if ( image && ( ! opts.keep ) )
132  err_acquire:
133  err_parse:
134  return rc;
135 }
136 
137 /** "colour" options */
139  /** Basic colour */
140  unsigned int basic;
141  /** 24-bit RGB value */
142  unsigned int rgb;
143 };
144 
145 /** "colour" option list */
146 static struct option_descriptor colour_opts[] = {
147  OPTION_DESC ( "basic", 'b', required_argument,
148  struct colour_options, basic, parse_integer ),
149  OPTION_DESC ( "rgb", 'r', required_argument,
150  struct colour_options, rgb, parse_integer ),
151 };
152 
153 /** "colour" command descriptor */
155  COMMAND_DESC ( struct colour_options, colour_opts, 1, 1, "<colour>" );
156 
157 /**
158  * "colour" command
159  *
160  * @v argc Argument count
161  * @v argv Argument list
162  * @ret rc Return status code
163  */
164 static int colour_exec ( int argc, char **argv ) {
165  struct colour_options opts;
166  unsigned int colour;
167  int rc;
168 
169  /* Initialise options */
170  memset ( &opts, 0, sizeof ( opts ) );
171  opts.basic = COLOUR_DEFAULT;
172  opts.rgb = ANSICOL_NO_RGB;
173 
174  /* Parse options */
175  if ( ( rc = reparse_options ( argc, argv, &colour_cmd, &opts ) ) != 0 )
176  return rc;
177 
178  /* Parse colour index */
179  if ( ( rc = parse_integer ( argv[optind], &colour ) ) != 0 )
180  return rc;
181 
182  /* Define colour */
183  if ( ( rc = ansicol_define ( colour, opts.basic, opts.rgb ) ) != 0 ) {
184  printf ( "Could not define colour: %s\n", strerror ( rc ) );
185  return rc;
186  }
187 
188  /* Reapply default colour pair, in case definition has changed */
190 
191  return 0;
192 }
193 
194 /** "cpair" options */
196  /** Foreground colour */
197  unsigned int foreground;
198  /** Background colour */
199  unsigned int background;
200 };
201 
202 /** "cpair" option list */
203 static struct option_descriptor cpair_opts[] = {
204  OPTION_DESC ( "foreground", 'f', required_argument,
205  struct cpair_options, foreground, parse_integer ),
206  OPTION_DESC ( "background", 'b', required_argument,
207  struct cpair_options, background, parse_integer ),
208 };
209 
210 /** "cpair" command descriptor */
212  COMMAND_DESC ( struct cpair_options, cpair_opts, 1, 1, "<cpair>" );
213 
214 /**
215  * "cpair" command
216  *
217  * @v argc Argument count
218  * @v argv Argument list
219  * @ret rc Return status code
220  */
221 static int cpair_exec ( int argc, char **argv ) {
222  struct cpair_options opts;
223  unsigned int cpair;
224  int rc;
225 
226  /* Initialise options */
227  memset ( &opts, 0, sizeof ( opts ) );
228  opts.foreground = COLOUR_DEFAULT;
229  opts.background = COLOUR_DEFAULT;
230 
231  /* Parse options */
232  if ( ( rc = reparse_options ( argc, argv, &cpair_cmd, &opts ) ) != 0 )
233  return rc;
234 
235  /* Parse colour pair index */
236  if ( ( rc = parse_integer ( argv[optind], &cpair ) ) != 0 )
237  return rc;
238 
239  /* Define colour pair */
240  if ( ( rc = ansicol_define_pair ( cpair, opts.foreground,
241  opts.background ) ) != 0 ) {
242  printf ( "Could not define colour pair: %s\n",
243  strerror ( rc ) );
244  return rc;
245  }
246 
247  /* Reapply default colour pair, in case definition has changed */
249 
250  return 0;
251 }
252 
253 /** Console management commands */
254 struct command console_commands[] __command = {
255  {
256  .name = "console",
257  .exec = console_exec,
258  },
259  {
260  .name = "colour",
261  .exec = colour_exec,
262  },
263  {
264  .name = "cpair",
265  .exec = cpair_exec,
266  },
267 };
int image_pixbuf(struct image *image, struct pixel_buffer **pixbuf)
Create pixel buffer from image.
Definition: pixbuf.c:97
int parse_integer(char *text, unsigned int *value)
Parse integer value.
Definition: parseopt.c:91
unsigned int rgb
24-bit RGB value
Definition: console_cmd.c:142
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
int optind
Current option index.
Definition: getopt.c:51
unsigned int basic
Basic colour.
Definition: console_cmd.c:140
struct command console_commands [] __command
Console management commands.
Definition: console_cmd.c:254
unsigned int background
Background colour.
Definition: console_cmd.c:199
A command-line command.
Definition: command.h:9
char * picture
Picture URI.
Definition: console_cmd.c:48
int keep
Keep picture after configuration.
Definition: console_cmd.c:50
static int console_exec(int argc, char **argv)
"console" command
Definition: console_cmd.c:86
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
An executable image.
Definition: image.h:24
Pixel buffer.
A command descriptor.
Definition: parseopt.h:77
static int cpair_exec(int argc, char **argv)
"cpair" command
Definition: console_cmd.c:221
A console configuration.
Definition: console.h:24
static struct option_descriptor console_opts[]
"console" option list
Definition: console_cmd.c:54
void ansicol_set_pair(unsigned int cpair)
Set ANSI foreground and background colour.
Definition: ansicol.c:89
static struct command_descriptor colour_cmd
"colour" command descriptor
Definition: console_cmd.c:154
#define CSI
Control Sequence Introducer.
Definition: ansiesc.h:95
"console" options
Definition: console_cmd.c:44
Parse command-line options.
#define colour
Colour for debug messages.
Definition: acpi.c:39
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:73
Executable images.
struct console_configuration config
Console configuration.
Definition: console_cmd.c:46
int ansicol_define(unsigned int colour, unsigned int basic, uint32_t rgb)
Define ANSI colour.
Definition: ansicoldef.c:125
int ansicol_define_pair(unsigned int cpair, unsigned int foreground, unsigned int background)
Define ANSI colour pair.
Definition: ansicol.c:110
static struct command_descriptor console_cmd
"console" command descriptor
Definition: console_cmd.c:76
ANSI escape sequences.
static struct option_descriptor cpair_opts[]
"cpair" option list
Definition: console_cmd.c:203
int reparse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Reparse command-line options.
Definition: parseopt.c:401
User interaction.
#define COLOUR_DEFAULT
Default colour (usually white foreground, black background)
Definition: ansicol.h:16
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:226
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Command line option parsing.
Option does not take an argument.
Definition: getopt.h:16
static struct option_descriptor colour_opts[]
"colour" option list
Definition: console_cmd.c:146
static union @437 opts
"cert<xxx>" option list
int console_configure(struct console_configuration *config)
Configure console.
Definition: console.c:146
#define ANSICOL_NO_RGB
RGB value for "not defined".
Definition: ansicol.h:29
const char * name
Name of the command.
Definition: command.h:11
unsigned int foreground
Foreground colour.
Definition: console_cmd.c:197
#define CPAIR_DEFAULT
Default colour pair.
Definition: ansicol.h:37
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:303
Image management.
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:67
Option requires an argument.
Definition: getopt.h:18
A command-line option descriptor.
Definition: parseopt.h:23
static int colour_exec(int argc, char **argv)
"colour" command
Definition: console_cmd.c:164
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
static struct command_descriptor cpair_cmd
"cpair" command descriptor
Definition: console_cmd.c:211
"colour" options
Definition: console_cmd.c:138
"cpair" options
Definition: console_cmd.c:195
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
ANSI colours.
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:141
void * memset(void *dest, int character, size_t len) __nonnull