Browse Source

[script] Add "prompt" command

The "prompt" command exposes the prompt() function, allowing a script
to prompt the user for a keypress and take action depending on the
result.  For example

  #!ipxe

  prompt -k 0x197e -t 2000 Press F12 to boot from network... || exit
  autoboot

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 13 years ago
parent
commit
e87fc5e743
1 changed files with 67 additions and 0 deletions
  1. 67
    0
      src/image/script.c

+ 67
- 0
src/image/script.c View File

35
 #include <ipxe/parseopt.h>
35
 #include <ipxe/parseopt.h>
36
 #include <ipxe/image.h>
36
 #include <ipxe/image.h>
37
 #include <ipxe/shell.h>
37
 #include <ipxe/shell.h>
38
+#include <usr/prompt.h>
38
 
39
 
39
 /** Currently running script
40
 /** Currently running script
40
  *
41
  *
288
 	.name = "goto",
289
 	.name = "goto",
289
 	.exec = goto_exec,
290
 	.exec = goto_exec,
290
 };
291
 };
292
+
293
+/** "prompt" options */
294
+struct prompt_options {
295
+	/** Key to wait for */
296
+	unsigned int key;
297
+	/** Timeout */
298
+	unsigned int timeout;
299
+};
300
+
301
+/** "prompt" option list */
302
+static struct option_descriptor prompt_opts[] = {
303
+	OPTION_DESC ( "key", 'k', required_argument,
304
+		      struct prompt_options, key, parse_integer ),
305
+	OPTION_DESC ( "timeout", 't', required_argument,
306
+		      struct prompt_options, timeout, parse_integer ),
307
+};
308
+
309
+/** "prompt" command descriptor */
310
+static struct command_descriptor prompt_cmd =
311
+	COMMAND_DESC ( struct prompt_options, prompt_opts, 0, MAX_ARGUMENTS,
312
+		       "[--key <key>] [--timeout <timeout>] [<text>]" );
313
+
314
+/**
315
+ * "prompt" command
316
+ *
317
+ * @v argc		Argument count
318
+ * @v argv		Argument list
319
+ * @ret rc		Return status code
320
+ */
321
+static int prompt_exec ( int argc, char **argv ) {
322
+	struct prompt_options opts;
323
+	char *text;
324
+	int rc;
325
+
326
+	/* Parse options */
327
+	if ( ( rc = parse_options ( argc, argv, &prompt_cmd, &opts ) ) != 0 )
328
+		goto err_parse;
329
+
330
+	/* Parse prompt text */
331
+	text = concat_args ( &argv[optind] );
332
+	if ( ! text ) {
333
+		rc = -ENOMEM;
334
+		goto err_concat;
335
+	}
336
+
337
+	/* Display prompt and wait for key */
338
+	if ( ( rc = prompt ( text, opts.timeout, opts.key ) ) != 0 )
339
+		goto err_prompt;
340
+
341
+	/* Free prompt text */
342
+	free ( text );
343
+
344
+	return 0;
345
+
346
+ err_prompt:
347
+	free ( text );
348
+ err_concat:
349
+ err_parse:
350
+	return rc;
351
+}
352
+
353
+/** "prompt" command */
354
+struct command prompt_command __command = {
355
+	.name = "prompt",
356
+	.exec = prompt_exec,
357
+};

Loading…
Cancel
Save