mirror of
https://github.com/ipxe/ipxe
synced 2025-12-23 05:21:49 +03:00
[parseopt] Add parse_timeout()
Parsing a timeout value (specified in milliseconds) into an internal timeout value measured in timer ticks is a common operation. Provide a parse_timeout() value to carry out this conversion automatically. Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
@@ -194,7 +194,7 @@ struct choose_options {
|
||||
/** Menu name */
|
||||
char *menu;
|
||||
/** Timeout */
|
||||
unsigned int timeout;
|
||||
unsigned long timeout;
|
||||
/** Default selection */
|
||||
char *select;
|
||||
/** Keep menu */
|
||||
@@ -208,7 +208,7 @@ static struct option_descriptor choose_opts[] = {
|
||||
OPTION_DESC ( "default", 'd', required_argument,
|
||||
struct choose_options, select, parse_string ),
|
||||
OPTION_DESC ( "timeout", 't', required_argument,
|
||||
struct choose_options, timeout, parse_integer ),
|
||||
struct choose_options, timeout, parse_timeout ),
|
||||
OPTION_DESC ( "keep", 'k', no_argument,
|
||||
struct choose_options, keep, parse_flag ),
|
||||
};
|
||||
|
||||
@@ -27,6 +27,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||
#include <getopt.h>
|
||||
#include <ipxe/command.h>
|
||||
#include <ipxe/parseopt.h>
|
||||
#include <ipxe/timer.h>
|
||||
#include <usr/pingmgmt.h>
|
||||
|
||||
/** @file
|
||||
@@ -39,14 +40,14 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||
#define PING_DEFAULT_SIZE 64
|
||||
|
||||
/** Default timeout */
|
||||
#define PING_DEFAULT_TIMEOUT 1000
|
||||
#define PING_DEFAULT_TIMEOUT TICKS_PER_SEC
|
||||
|
||||
/** "ping" options */
|
||||
struct ping_options {
|
||||
/** Payload length */
|
||||
unsigned int size;
|
||||
/** Timeout (in ms) */
|
||||
unsigned int timeout;
|
||||
unsigned long timeout;
|
||||
};
|
||||
|
||||
/** "ping" option list */
|
||||
@@ -54,7 +55,7 @@ static struct option_descriptor ping_opts[] = {
|
||||
OPTION_DESC ( "size", 's', required_argument,
|
||||
struct ping_options, size, parse_integer ),
|
||||
OPTION_DESC ( "timeout", 't', required_argument,
|
||||
struct ping_options, timeout, parse_integer ),
|
||||
struct ping_options, timeout, parse_timeout ),
|
||||
};
|
||||
|
||||
/** "ping" command descriptor */
|
||||
|
||||
@@ -24,7 +24,6 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||
#include <getopt.h>
|
||||
#include <ipxe/command.h>
|
||||
#include <ipxe/parseopt.h>
|
||||
#include <ipxe/timer.h>
|
||||
#include <ipxe/pending.h>
|
||||
|
||||
/** @file
|
||||
@@ -36,13 +35,13 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||
/** "sync" options */
|
||||
struct sync_options {
|
||||
/** Timeout */
|
||||
unsigned int timeout;
|
||||
unsigned long timeout;
|
||||
};
|
||||
|
||||
/** "sync" option list */
|
||||
static struct option_descriptor sync_opts[] = {
|
||||
OPTION_DESC ( "timeout", 't', required_argument,
|
||||
struct sync_options, timeout, parse_integer ),
|
||||
struct sync_options, timeout, parse_timeout ),
|
||||
};
|
||||
|
||||
/** "sync" command descriptor */
|
||||
@@ -59,7 +58,6 @@ static struct command_descriptor sync_cmd =
|
||||
*/
|
||||
static int sync_exec ( int argc, char **argv ) {
|
||||
struct sync_options opts;
|
||||
unsigned long timeout;
|
||||
int rc;
|
||||
|
||||
/* Parse options */
|
||||
@@ -67,8 +65,7 @@ static int sync_exec ( int argc, char **argv ) {
|
||||
return rc;
|
||||
|
||||
/* Wait for pending operations to complete */
|
||||
timeout = ( ( opts.timeout * TICKS_PER_SEC ) / 1000 );
|
||||
if ( ( rc = pending_wait ( timeout ) ) != 0 ) {
|
||||
if ( ( rc = pending_wait ( opts.timeout ) ) != 0 ) {
|
||||
printf ( "Operations did not complete: %s\n", strerror ( rc ) );
|
||||
return rc;
|
||||
}
|
||||
|
||||
@@ -303,11 +303,11 @@ static int menu_loop ( struct menu_ui *ui, struct menu_item **selected ) {
|
||||
* Show menu
|
||||
*
|
||||
* @v menu Menu
|
||||
* @v wait_ms Time to wait, in milliseconds (0=indefinite)
|
||||
* @v timeout Timeout period, in ticks (0=indefinite)
|
||||
* @ret selected Selected item
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
int show_menu ( struct menu *menu, unsigned int timeout_ms,
|
||||
int show_menu ( struct menu *menu, unsigned long timeout,
|
||||
const char *select, struct menu_item **selected ) {
|
||||
struct menu_item *item;
|
||||
struct menu_ui ui;
|
||||
@@ -318,7 +318,7 @@ int show_menu ( struct menu *menu, unsigned int timeout_ms,
|
||||
/* Initialise UI */
|
||||
memset ( &ui, 0, sizeof ( ui ) );
|
||||
ui.menu = menu;
|
||||
ui.timeout = ( ( timeout_ms * TICKS_PER_SEC ) / 1000 );
|
||||
ui.timeout = timeout;
|
||||
list_for_each_entry ( item, &menu->items, list ) {
|
||||
if ( item->label ) {
|
||||
if ( ! labelled_count )
|
||||
|
||||
Reference in New Issue
Block a user