mirror of https://github.com/ipxe/ipxe.git
[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>pull/17/head
parent
b15dbc9cc6
commit
5e1fa5cd40
|
@ -30,6 +30,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
#include <ipxe/menu.h>
|
#include <ipxe/menu.h>
|
||||||
#include <ipxe/settings.h>
|
#include <ipxe/settings.h>
|
||||||
#include <ipxe/params.h>
|
#include <ipxe/params.h>
|
||||||
|
#include <ipxe/timer.h>
|
||||||
#include <ipxe/parseopt.h>
|
#include <ipxe/parseopt.h>
|
||||||
|
|
||||||
/** @file
|
/** @file
|
||||||
|
@ -95,6 +96,27 @@ int parse_integer ( char *text, unsigned int *value ) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse timeout value (in ms)
|
||||||
|
*
|
||||||
|
* @v text Text
|
||||||
|
* @ret value Integer value
|
||||||
|
* @ret rc Return status code
|
||||||
|
*/
|
||||||
|
int parse_timeout ( char *text, unsigned long *value ) {
|
||||||
|
unsigned int value_ms;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
/* Parse raw integer value */
|
||||||
|
if ( ( rc = parse_integer ( text, &value_ms ) ) != 0 )
|
||||||
|
return rc;
|
||||||
|
|
||||||
|
/* Convert to a number of timer ticks */
|
||||||
|
*value = ( ( value_ms * TICKS_PER_SEC ) / 1000 );
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse network device name
|
* Parse network device name
|
||||||
*
|
*
|
||||||
|
|
|
@ -194,7 +194,7 @@ struct choose_options {
|
||||||
/** Menu name */
|
/** Menu name */
|
||||||
char *menu;
|
char *menu;
|
||||||
/** Timeout */
|
/** Timeout */
|
||||||
unsigned int timeout;
|
unsigned long timeout;
|
||||||
/** Default selection */
|
/** Default selection */
|
||||||
char *select;
|
char *select;
|
||||||
/** Keep menu */
|
/** Keep menu */
|
||||||
|
@ -208,7 +208,7 @@ static struct option_descriptor choose_opts[] = {
|
||||||
OPTION_DESC ( "default", 'd', required_argument,
|
OPTION_DESC ( "default", 'd', required_argument,
|
||||||
struct choose_options, select, parse_string ),
|
struct choose_options, select, parse_string ),
|
||||||
OPTION_DESC ( "timeout", 't', required_argument,
|
OPTION_DESC ( "timeout", 't', required_argument,
|
||||||
struct choose_options, timeout, parse_integer ),
|
struct choose_options, timeout, parse_timeout ),
|
||||||
OPTION_DESC ( "keep", 'k', no_argument,
|
OPTION_DESC ( "keep", 'k', no_argument,
|
||||||
struct choose_options, keep, parse_flag ),
|
struct choose_options, keep, parse_flag ),
|
||||||
};
|
};
|
||||||
|
|
|
@ -27,6 +27,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <ipxe/command.h>
|
#include <ipxe/command.h>
|
||||||
#include <ipxe/parseopt.h>
|
#include <ipxe/parseopt.h>
|
||||||
|
#include <ipxe/timer.h>
|
||||||
#include <usr/pingmgmt.h>
|
#include <usr/pingmgmt.h>
|
||||||
|
|
||||||
/** @file
|
/** @file
|
||||||
|
@ -39,14 +40,14 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
#define PING_DEFAULT_SIZE 64
|
#define PING_DEFAULT_SIZE 64
|
||||||
|
|
||||||
/** Default timeout */
|
/** Default timeout */
|
||||||
#define PING_DEFAULT_TIMEOUT 1000
|
#define PING_DEFAULT_TIMEOUT TICKS_PER_SEC
|
||||||
|
|
||||||
/** "ping" options */
|
/** "ping" options */
|
||||||
struct ping_options {
|
struct ping_options {
|
||||||
/** Payload length */
|
/** Payload length */
|
||||||
unsigned int size;
|
unsigned int size;
|
||||||
/** Timeout (in ms) */
|
/** Timeout (in ms) */
|
||||||
unsigned int timeout;
|
unsigned long timeout;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** "ping" option list */
|
/** "ping" option list */
|
||||||
|
@ -54,7 +55,7 @@ static struct option_descriptor ping_opts[] = {
|
||||||
OPTION_DESC ( "size", 's', required_argument,
|
OPTION_DESC ( "size", 's', required_argument,
|
||||||
struct ping_options, size, parse_integer ),
|
struct ping_options, size, parse_integer ),
|
||||||
OPTION_DESC ( "timeout", 't', required_argument,
|
OPTION_DESC ( "timeout", 't', required_argument,
|
||||||
struct ping_options, timeout, parse_integer ),
|
struct ping_options, timeout, parse_timeout ),
|
||||||
};
|
};
|
||||||
|
|
||||||
/** "ping" command descriptor */
|
/** "ping" command descriptor */
|
||||||
|
|
|
@ -24,7 +24,6 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <ipxe/command.h>
|
#include <ipxe/command.h>
|
||||||
#include <ipxe/parseopt.h>
|
#include <ipxe/parseopt.h>
|
||||||
#include <ipxe/timer.h>
|
|
||||||
#include <ipxe/pending.h>
|
#include <ipxe/pending.h>
|
||||||
|
|
||||||
/** @file
|
/** @file
|
||||||
|
@ -36,13 +35,13 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
/** "sync" options */
|
/** "sync" options */
|
||||||
struct sync_options {
|
struct sync_options {
|
||||||
/** Timeout */
|
/** Timeout */
|
||||||
unsigned int timeout;
|
unsigned long timeout;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** "sync" option list */
|
/** "sync" option list */
|
||||||
static struct option_descriptor sync_opts[] = {
|
static struct option_descriptor sync_opts[] = {
|
||||||
OPTION_DESC ( "timeout", 't', required_argument,
|
OPTION_DESC ( "timeout", 't', required_argument,
|
||||||
struct sync_options, timeout, parse_integer ),
|
struct sync_options, timeout, parse_timeout ),
|
||||||
};
|
};
|
||||||
|
|
||||||
/** "sync" command descriptor */
|
/** "sync" command descriptor */
|
||||||
|
@ -59,7 +58,6 @@ static struct command_descriptor sync_cmd =
|
||||||
*/
|
*/
|
||||||
static int sync_exec ( int argc, char **argv ) {
|
static int sync_exec ( int argc, char **argv ) {
|
||||||
struct sync_options opts;
|
struct sync_options opts;
|
||||||
unsigned long timeout;
|
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
/* Parse options */
|
/* Parse options */
|
||||||
|
@ -67,8 +65,7 @@ static int sync_exec ( int argc, char **argv ) {
|
||||||
return rc;
|
return rc;
|
||||||
|
|
||||||
/* Wait for pending operations to complete */
|
/* Wait for pending operations to complete */
|
||||||
timeout = ( ( opts.timeout * TICKS_PER_SEC ) / 1000 );
|
if ( ( rc = pending_wait ( opts.timeout ) ) != 0 ) {
|
||||||
if ( ( rc = pending_wait ( timeout ) ) != 0 ) {
|
|
||||||
printf ( "Operations did not complete: %s\n", strerror ( rc ) );
|
printf ( "Operations did not complete: %s\n", strerror ( rc ) );
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
|
@ -303,11 +303,11 @@ static int menu_loop ( struct menu_ui *ui, struct menu_item **selected ) {
|
||||||
* Show menu
|
* Show menu
|
||||||
*
|
*
|
||||||
* @v menu 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 selected Selected item
|
||||||
* @ret rc Return status code
|
* @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 ) {
|
const char *select, struct menu_item **selected ) {
|
||||||
struct menu_item *item;
|
struct menu_item *item;
|
||||||
struct menu_ui ui;
|
struct menu_ui ui;
|
||||||
|
@ -318,7 +318,7 @@ int show_menu ( struct menu *menu, unsigned int timeout_ms,
|
||||||
/* Initialise UI */
|
/* Initialise UI */
|
||||||
memset ( &ui, 0, sizeof ( ui ) );
|
memset ( &ui, 0, sizeof ( ui ) );
|
||||||
ui.menu = menu;
|
ui.menu = menu;
|
||||||
ui.timeout = ( ( timeout_ms * TICKS_PER_SEC ) / 1000 );
|
ui.timeout = timeout;
|
||||||
list_for_each_entry ( item, &menu->items, list ) {
|
list_for_each_entry ( item, &menu->items, list ) {
|
||||||
if ( item->label ) {
|
if ( item->label ) {
|
||||||
if ( ! labelled_count )
|
if ( ! labelled_count )
|
||||||
|
|
|
@ -361,7 +361,7 @@ struct prompt_options {
|
||||||
/** Key to wait for */
|
/** Key to wait for */
|
||||||
unsigned int key;
|
unsigned int key;
|
||||||
/** Timeout */
|
/** Timeout */
|
||||||
unsigned int timeout;
|
unsigned long timeout;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** "prompt" option list */
|
/** "prompt" option list */
|
||||||
|
@ -369,7 +369,7 @@ static struct option_descriptor prompt_opts[] = {
|
||||||
OPTION_DESC ( "key", 'k', required_argument,
|
OPTION_DESC ( "key", 'k', required_argument,
|
||||||
struct prompt_options, key, parse_key ),
|
struct prompt_options, key, parse_key ),
|
||||||
OPTION_DESC ( "timeout", 't', required_argument,
|
OPTION_DESC ( "timeout", 't', required_argument,
|
||||||
struct prompt_options, timeout, parse_integer ),
|
struct prompt_options, timeout, parse_timeout ),
|
||||||
};
|
};
|
||||||
|
|
||||||
/** "prompt" command descriptor */
|
/** "prompt" command descriptor */
|
||||||
|
|
|
@ -43,7 +43,7 @@ extern struct menu_item * add_menu_item ( struct menu *menu, const char *label,
|
||||||
int is_default );
|
int is_default );
|
||||||
extern void destroy_menu ( struct menu *menu );
|
extern void destroy_menu ( struct menu *menu );
|
||||||
extern struct menu * find_menu ( const char *name );
|
extern struct menu * find_menu ( const char *name );
|
||||||
extern int show_menu ( struct menu *menu, unsigned int timeout_ms,
|
extern int show_menu ( struct menu *menu, unsigned long timeout,
|
||||||
const char *select, struct menu_item **selected );
|
const char *select, struct menu_item **selected );
|
||||||
|
|
||||||
#endif /* _IPXE_MENU_H */
|
#endif /* _IPXE_MENU_H */
|
||||||
|
|
|
@ -126,6 +126,7 @@ struct named_setting {
|
||||||
|
|
||||||
extern int parse_string ( char *text, char **value );
|
extern int parse_string ( char *text, char **value );
|
||||||
extern int parse_integer ( char *text, unsigned int *value );
|
extern int parse_integer ( char *text, unsigned int *value );
|
||||||
|
extern int parse_timeout ( char *text, unsigned long *value );
|
||||||
extern int parse_netdev ( char *text, struct net_device **netdev );
|
extern int parse_netdev ( char *text, struct net_device **netdev );
|
||||||
extern int parse_menu ( char *text, struct menu **menu );
|
extern int parse_menu ( char *text, struct menu **menu );
|
||||||
extern int parse_flag ( char *text __unused, int *flag );
|
extern int parse_flag ( char *text __unused, int *flag );
|
||||||
|
|
|
@ -11,6 +11,6 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
extern int ping ( const char *hostname, unsigned long timeout_ms, size_t len );
|
extern int ping ( const char *hostname, unsigned long timeout, size_t len );
|
||||||
|
|
||||||
#endif /* _USR_PINGMGMT_H */
|
#endif /* _USR_PINGMGMT_H */
|
||||||
|
|
|
@ -9,6 +9,6 @@
|
||||||
|
|
||||||
FILE_LICENCE ( GPL2_OR_LATER );
|
FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
|
|
||||||
extern int prompt ( const char *text, unsigned int wait_ms, int key );
|
extern int prompt ( const char *text, unsigned long timeout, int key );
|
||||||
|
|
||||||
#endif /* _USR_PROMPT_H */
|
#endif /* _USR_PROMPT_H */
|
||||||
|
|
|
@ -56,16 +56,15 @@ static void ping_callback ( struct sockaddr *peer, unsigned int sequence,
|
||||||
* Ping a host
|
* Ping a host
|
||||||
*
|
*
|
||||||
* @v hostname Hostname
|
* @v hostname Hostname
|
||||||
* @v timeout_ms Timeout between pings, in ms
|
* @v timeout Timeout between pings, in ticks
|
||||||
* @v len Payload length
|
* @v len Payload length
|
||||||
* @ret rc Return status code
|
* @ret rc Return status code
|
||||||
*/
|
*/
|
||||||
int ping ( const char *hostname, unsigned long timeout_ms, size_t len ) {
|
int ping ( const char *hostname, unsigned long timeout, size_t len ) {
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
/* Create pinger */
|
/* Create pinger */
|
||||||
if ( ( rc = create_pinger ( &monojob, hostname,
|
if ( ( rc = create_pinger ( &monojob, hostname, timeout,
|
||||||
( ( timeout_ms * TICKS_PER_SEC ) / 1000 ),
|
|
||||||
len, ping_callback ) ) != 0 ) {
|
len, ping_callback ) ) != 0 ) {
|
||||||
printf ( "Could not start ping: %s\n", strerror ( rc ) );
|
printf ( "Could not start ping: %s\n", strerror ( rc ) );
|
||||||
return rc;
|
return rc;
|
||||||
|
|
|
@ -28,28 +28,27 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <ipxe/console.h>
|
#include <ipxe/console.h>
|
||||||
#include <ipxe/timer.h>
|
|
||||||
#include <usr/prompt.h>
|
#include <usr/prompt.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prompt for keypress
|
* Prompt for keypress
|
||||||
*
|
*
|
||||||
* @v text Prompt string
|
* @v text Prompt string
|
||||||
* @v wait_ms Time to wait, in milliseconds (0=indefinite)
|
* @v timeout Timeout period, in ticks (0=indefinite)
|
||||||
* @v key Key to wait for (0=any key)
|
* @v key Key to wait for (0=any key)
|
||||||
* @ret rc Return status code
|
* @ret rc Return status code
|
||||||
*
|
*
|
||||||
* Returns success if the specified key was pressed within the
|
* Returns success if the specified key was pressed within the
|
||||||
* specified timeout period.
|
* specified timeout period.
|
||||||
*/
|
*/
|
||||||
int prompt ( const char *text, unsigned int wait_ms, int key ) {
|
int prompt ( const char *text, unsigned long timeout, int key ) {
|
||||||
int key_pressed;
|
int key_pressed;
|
||||||
|
|
||||||
/* Display prompt */
|
/* Display prompt */
|
||||||
printf ( "%s", text );
|
printf ( "%s", text );
|
||||||
|
|
||||||
/* Wait for key */
|
/* Wait for key */
|
||||||
key_pressed = getkey ( ( wait_ms * TICKS_PER_SEC ) / 1000 );
|
key_pressed = getkey ( timeout );
|
||||||
|
|
||||||
/* Clear the prompt line */
|
/* Clear the prompt line */
|
||||||
while ( *(text++) )
|
while ( *(text++) )
|
||||||
|
|
Loading…
Reference in New Issue