[ipxe-devel] [PATCH] add --timeout to login command (v2)

Martin Vogt mvogt1 at gmail.com
Fri Sep 7 08:21:20 UTC 2018


Hello,

this is an improved version of the patch. It adds a short documentation and
the getkey handling is improved. The cursor now blinks during the first
timeout in the username edit and not in the password editline.

The patch adds the following behaviour to login_ui:

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

Is it possible to add this to current git master?

regards,

Martin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ipxe.org/pipermail/ipxe-devel/attachments/20180907/5ea1801f/attachment.htm>
-------------- next part --------------
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..395969d 100644
--- a/src/hci/tui/login_ui.c
+++ b/src/hci/tui/login_ui.c
@@ -48,7 +48,13 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 #define EDITBOX_COL		( ( COLS / 2U ) - 10U )
 #define EDITBOX_WIDTH		20U
 
-int login_ui ( void ) {
+/**
+ * login: username / password dialog
+ *
+ * @v timeout		if no interaction happens in timeout(ms)
+ *                      the dialog cancel itsself
+ */
+int login_ui ( unsigned long timeout ) {
 	char username[64];
 	char password[64];
 	struct edit_box username_box;
@@ -87,8 +93,11 @@ int login_ui ( void ) {
 	while ( rc == -EINPROGRESS ) {
 
 		draw_editbox ( current_box );
+		key = getkey ( timeout );
+
+		/* clear timeout, in case it was set */
+		timeout=0;
 
-		key = getkey ( 0 );
 		switch ( key ) {
 		case KEY_DOWN:
 			current_box = &password_box;
@@ -107,6 +116,8 @@ int login_ui ( void ) {
 				rc = 0;
 			}
 			break;
+		/* getkey return -1 in case of timeout */	
+		case -1: 
 		case CTRL_C:
 		case ESC:
 			rc = -ECANCELED;
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 */


More information about the ipxe-devel mailing list