Hello,

the following patch adds a --timeout <ms> option to the login command.

If no keypress happens before the timeout, the login screen is canceled,
as if the user press ESC.

Is it possible to add this to current git master?

regards,

Martin

diff --git a/src/hci/commands/login_cmd.c b/src/hci/commands/login_cmd.c
index c9e1964..ea9b115 100644
--- a/src/hci/commands/login_cmd.c
+++ b/src/hci/commands/login_cmd.c
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <ipxe/command.h>
 #include <ipxe/parseopt.h>
+#include <getopt.h>
 #include <ipxe/login_ui.h>
 
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
@@ -36,14 +37,20 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  */
 
 /** "login" options */
-struct login_options {};
+struct login_options {
+	/** Timeout (in ms). (0=indefinite)  */
+	unsigned long timeout;
+};
 
 /** "login" option list */
-static struct option_descriptor login_opts[] = {};
+static struct option_descriptor login_opts[] = {
+	OPTION_DESC ( "timeout", 't', required_argument,
+		      struct login_options, timeout, parse_timeout ),
+};
 
 /** "login" command descriptor */
 static struct command_descriptor login_cmd =
-	COMMAND_DESC ( struct login_options, login_opts, 0, 0, NULL );
+	COMMAND_DESC ( struct login_options, login_opts, 0, 1, NULL );
 
 /**
  * "login" command
@@ -56,12 +63,16 @@ static int login_exec ( int argc, char **argv ) {
 	struct login_options opts;
 	int rc;
 
+	/* Initialise options */
+	memset ( &opts, 0, sizeof ( opts ) );
+	opts.timeout = 0;
+
 	/* Parse options */
 	if ( ( rc = parse_options ( argc, argv, &login_cmd, &opts ) ) != 0 )
 		return rc;
 
 	/* Show login UI */
-	if ( ( rc = login_ui() ) != 0 ) {
+	if ( ( rc = login_ui(opts.timeout) ) != 0 ) {
 		printf ( "Could not set credentials: %s\n",
 			 strerror ( rc ) );
 		return rc;
diff --git a/src/hci/tui/login_ui.c b/src/hci/tui/login_ui.c
index 3c55325..a9a85b7 100644
--- a/src/hci/tui/login_ui.c
+++ b/src/hci/tui/login_ui.c
@@ -48,13 +48,13 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 #define EDITBOX_COL		( ( COLS / 2U ) - 10U )
 #define EDITBOX_WIDTH		20U
 
-int login_ui ( void ) {
+int login_ui ( unsigned long timeout ) {
 	char username[64];
 	char password[64];
 	struct edit_box username_box;
 	struct edit_box password_box;
 	struct edit_box *current_box = &username_box;
-	int key;
+	int key=-1;
 	int rc = -EINPROGRESS;
 
 	/* Fetch current setting values */
@@ -83,12 +83,19 @@ int login_ui ( void ) {
 	draw_editbox ( &username_box );
 	draw_editbox ( &password_box );
 
+	if (timeout) {
+	  key=getkey(timeout);
+	  /* key is -1 in case of timeout */
+	  if ( key == -1 ) rc = -ECANCELED;
+	}
+
 	/* Main loop */
 	while ( rc == -EINPROGRESS ) {
 
 		draw_editbox ( current_box );
-
-		key = getkey ( 0 );
+		if ( key == -1 ) {
+		  key = getkey ( 0 );
+		}
 		switch ( key ) {
 		case KEY_DOWN:
 			current_box = &password_box;
@@ -115,6 +122,7 @@ int login_ui ( void ) {
 			edit_editbox ( current_box, key );
 			break;
 		}
+		key = -1;
 	}
 
 	/* Terminate UI */
diff --git a/src/include/ipxe/login_ui.h b/src/include/ipxe/login_ui.h
index 313e073..ff8dd80 100644
--- a/src/include/ipxe/login_ui.h
+++ b/src/include/ipxe/login_ui.h
@@ -9,6 +9,6 @@
 
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 
-extern int login_ui ( void );
+extern int login_ui ( unsigned long timeout );
 
 #endif /* _IPXE_LOGIN_UI_H */
_______________________________________________
ipxe-devel mailing list
ipxe-devel@lists.ipxe.org
https://lists.ipxe.org/mailman/listinfo.cgi/ipxe-devel

Reply via email to