[tcpip] Allow supported address families to be detected at runtime
[ipxe.git] / src / hci / commands / console_cmd.c
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 */
44 struct console_options {
45 /** Console configuration */
46 struct console_configuration config;
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 */
76 static struct command_descriptor console_cmd =
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 */
123 ansicol_set_pair ( CPAIR_DEFAULT );
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 ) )
131 unregister_image ( image );
132 err_acquire:
133 err_parse:
134 return rc;
135 }
136
137 /** "colour" options */
138 struct 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 */
154 static struct command_descriptor colour_cmd =
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 */
189 ansicol_set_pair ( CPAIR_DEFAULT );
190
191 return 0;
192 }
193
194 /** "cpair" options */
195 struct 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 */
211 static struct command_descriptor cpair_cmd =
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 */
248 ansicol_set_pair ( CPAIR_DEFAULT );
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 };