Browse Source

[cmdline] Add the "colour" and "cpair" commands

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 11 years ago
parent
commit
e8b5b50486
1 changed files with 125 additions and 0 deletions
  1. 125
    0
      src/hci/commands/console_cmd.c

+ 125
- 0
src/hci/commands/console_cmd.c View File

@@ -32,6 +32,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
32 32
 #include <ipxe/console.h>
33 33
 #include <ipxe/image.h>
34 34
 #include <ipxe/pixbuf.h>
35
+#include <ipxe/ansicol.h>
35 36
 #include <usr/imgmgmt.h>
36 37
 
37 38
 /** "console" options */
@@ -116,10 +117,134 @@ static int console_exec ( int argc, char **argv ) {
116 117
 	return rc;
117 118
 }
118 119
 
120
+/** "colour" options */
121
+struct colour_options {
122
+	/** Basic colour */
123
+	unsigned int basic;
124
+	/** 24-bit RGB value */
125
+	unsigned int rgb;
126
+};
127
+
128
+/** "colour" option list */
129
+static struct option_descriptor colour_opts[] = {
130
+	OPTION_DESC ( "basic", 'b', required_argument,
131
+		      struct colour_options, basic, parse_integer ),
132
+	OPTION_DESC ( "rgb", 'r', required_argument,
133
+		      struct colour_options, rgb, parse_integer ),
134
+};
135
+
136
+/** "colour" command descriptor */
137
+static struct command_descriptor colour_cmd =
138
+	COMMAND_DESC ( struct colour_options, colour_opts, 1, 1, "<colour>" );
139
+
140
+/**
141
+ * "colour" command
142
+ *
143
+ * @v argc		Argument count
144
+ * @v argv		Argument list
145
+ * @ret rc		Return status code
146
+ */
147
+static int colour_exec ( int argc, char **argv ) {
148
+	struct colour_options opts;
149
+	unsigned int colour;
150
+	int rc;
151
+
152
+	/* Initialise options */
153
+	memset ( &opts, 0, sizeof ( opts ) );
154
+	opts.basic = COLOUR_DEFAULT;
155
+	opts.rgb = ANSICOL_NO_RGB;
156
+
157
+	/* Parse options */
158
+	if ( ( rc = reparse_options ( argc, argv, &colour_cmd, &opts ) ) != 0 )
159
+		return rc;
160
+
161
+	/* Parse colour index */
162
+	if ( ( rc = parse_integer ( argv[optind], &colour ) ) != 0 )
163
+		return rc;
164
+
165
+	/* Define colour */
166
+	if ( ( rc = ansicol_define ( colour, opts.basic, opts.rgb ) ) != 0 ) {
167
+		printf ( "Could not define colour: %s\n", strerror ( rc ) );
168
+		return rc;
169
+	}
170
+
171
+	/* Reapply default colour pair, in case definition has changed */
172
+	ansicol_set_pair ( CPAIR_DEFAULT );
173
+
174
+	return 0;
175
+}
176
+
177
+/** "cpair" options */
178
+struct cpair_options {
179
+	/** Foreground colour */
180
+	unsigned int foreground;
181
+	/** Background colour */
182
+	unsigned int background;
183
+};
184
+
185
+/** "cpair" option list */
186
+static struct option_descriptor cpair_opts[] = {
187
+	OPTION_DESC ( "foreground", 'f', required_argument,
188
+		      struct cpair_options, foreground, parse_integer ),
189
+	OPTION_DESC ( "background", 'b', required_argument,
190
+		      struct cpair_options, background, parse_integer ),
191
+};
192
+
193
+/** "cpair" command descriptor */
194
+static struct command_descriptor cpair_cmd =
195
+	COMMAND_DESC ( struct cpair_options, cpair_opts, 1, 1, "<cpair>" );
196
+
197
+/**
198
+ * "cpair" command
199
+ *
200
+ * @v argc		Argument count
201
+ * @v argv		Argument list
202
+ * @ret rc		Return status code
203
+ */
204
+static int cpair_exec ( int argc, char **argv ) {
205
+	struct cpair_options opts;
206
+	unsigned int cpair;
207
+	int rc;
208
+
209
+	/* Initialise options */
210
+	memset ( &opts, 0, sizeof ( opts ) );
211
+	opts.foreground = COLOUR_DEFAULT;
212
+	opts.background = COLOUR_DEFAULT;
213
+
214
+	/* Parse options */
215
+	if ( ( rc = reparse_options ( argc, argv, &cpair_cmd, &opts ) ) != 0 )
216
+		return rc;
217
+
218
+	/* Parse colour pair index */
219
+	if ( ( rc = parse_integer ( argv[optind], &cpair ) ) != 0 )
220
+		return rc;
221
+
222
+	/* Define colour pair */
223
+	if ( ( rc = ansicol_define_pair ( cpair, opts.foreground,
224
+					  opts.background ) ) != 0 ) {
225
+		printf ( "Could not define colour pair: %s\n",
226
+			 strerror ( rc ) );
227
+		return rc;
228
+	}
229
+
230
+	/* Reapply default colour pair, in case definition has changed */
231
+	ansicol_set_pair ( CPAIR_DEFAULT );
232
+
233
+	return 0;
234
+}
235
+
119 236
 /** Console management commands */
120 237
 struct command console_commands[] __command = {
121 238
 	{
122 239
 		.name = "console",
123 240
 		.exec = console_exec,
124 241
 	},
242
+	{
243
+		.name = "colour",
244
+		.exec = colour_exec,
245
+	},
246
+	{
247
+		.name = "cpair",
248
+		.exec = cpair_exec,
249
+	},
125 250
 };

Loading…
Cancel
Save