Ok. You could try this out, if you like. Forever means forever, though... - Shao

From 31189196d38c0b27dcbd42023d96f068652201ae Mon Sep 17 00:00:00 2001
From: Shao Miller <[email protected]>
Date: Sun, 28 Mar 2010 16:09:40 +0000
Subject: [PATCH] [time_cmd] Add 'tryfor' command

Usage:
 tryfor <seconds> <command>

Keep trying <command> up to a maximum of
<second> seconds. 0 == forever.

Uncompresed code size cost: 226 bytes

Suggested-by: Jonathan Andrews <[email protected]>
Suggested-by: Piotr Jaroszynski <[email protected]>
---
src/hci/commands/time_cmd.c |   30 ++++++++++++++++++++++++++++++
1 files changed, 30 insertions(+), 0 deletions(-)
mode change 100644 => 100755 src/hci/commands/time_cmd.c

diff --git a/src/hci/commands/time_cmd.c b/src/hci/commands/time_cmd.c
old mode 100644
new mode 100755
index 947410e..e736b87
--- a/src/hci/commands/time_cmd.c
+++ b/src/hci/commands/time_cmd.c
@@ -1,5 +1,6 @@
/*
 * Copyright (C) 2009 Daniel Verkamp <[email protected]>.
+ * Portions copyright (C) 2010 Shao Miller <[email protected]>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
@@ -17,6 +18,9 @@
 *
 * March-19-2009 @ 02:44: Added sleep command.
 * Shao Miller <[email protected]>.
+ *
+ * March-28-2010 @ 11:33: Added tryfor command.
+ * Shao Miller <[email protected]>.
 */

#include <stdio.h>
@@ -82,3 +86,29 @@ struct command sleep_command __command = {
    .name = "sleep",
    .exec = sleep_exec,
};
+
+static int tryfor_exec ( int argc, char **argv ) {
+    unsigned long start, timeout;
+    int rc;
+
+    if ( argc < 3 ) {
+        printf ( "Usage:\n"
+             "  %s <seconds> <command>\n"
+             "\n"
+             "Keep trying <command> up to a maximum of <second> "
+             "seconds. 0 == forever\n",
+             argv[0] );
+        return 1;
+    }
+    start = currticks();
+    timeout = strtoul ( argv[1], NULL, 0 ) * ticks_per_sec();
+    while ( ( ( rc = execv ( argv[2], &argv[2] ) ) != 0 ) &&
+        ( ( timeout == 0 ) ||
+          ( ( currticks() - start ) <= timeout ) ) ) {};
+    return rc;
+}
+
+struct command tryfor_command __command = {
+    .name = "tryfor",
+    .exec = tryfor_exec,
+};
--
1.5.3.4

>From 31189196d38c0b27dcbd42023d96f068652201ae Mon Sep 17 00:00:00 2001
From: Shao Miller <[email protected]>
Date: Sun, 28 Mar 2010 16:09:40 +0000
Subject: [PATCH] [time_cmd] Add 'tryfor' command

Usage:
  tryfor <seconds> <command>

Keep trying <command> up to a maximum of
<second> seconds. 0 == forever.

Uncompresed code size cost: 226 bytes

Suggested-by: Jonathan Andrews <[email protected]>
Suggested-by: Piotr Jaroszynski <[email protected]>
---
 src/hci/commands/time_cmd.c |   30 ++++++++++++++++++++++++++++++
 1 files changed, 30 insertions(+), 0 deletions(-)
 mode change 100644 => 100755 src/hci/commands/time_cmd.c

diff --git a/src/hci/commands/time_cmd.c b/src/hci/commands/time_cmd.c
old mode 100644
new mode 100755
index 947410e..e736b87
--- a/src/hci/commands/time_cmd.c
+++ b/src/hci/commands/time_cmd.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2009 Daniel Verkamp <[email protected]>.
+ * Portions copyright (C) 2010 Shao Miller <[email protected]>.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -17,6 +18,9 @@
  *
  * March-19-2009 @ 02:44: Added sleep command.
  * Shao Miller <[email protected]>.
+ *
+ * March-28-2010 @ 11:33: Added tryfor command.
+ * Shao Miller <[email protected]>.
  */
 
 #include <stdio.h>
@@ -82,3 +86,29 @@ struct command sleep_command __command = {
        .name = "sleep",
        .exec = sleep_exec,
 };
+
+static int tryfor_exec ( int argc, char **argv ) {
+       unsigned long start, timeout;
+       int rc;
+
+       if ( argc < 3 ) {
+               printf ( "Usage:\n"
+                        "  %s <seconds> <command>\n"
+                        "\n"
+                        "Keep trying <command> up to a maximum of <second> "
+                        "seconds. 0 == forever\n",
+                        argv[0] );
+               return 1;
+       }
+       start = currticks();
+       timeout = strtoul ( argv[1], NULL, 0 ) * ticks_per_sec();
+       while ( ( ( rc = execv ( argv[2], &argv[2] ) ) != 0 ) &&
+               ( ( timeout == 0 ) ||
+                 ( ( currticks() - start ) <= timeout ) ) ) {};
+       return rc;
+}
+
+struct command tryfor_command __command = {
+       .name = "tryfor",
+       .exec = tryfor_exec,
+};
-- 
1.5.3.4

_______________________________________________
gPXE mailing list
[email protected]
http://etherboot.org/mailman/listinfo/gpxe

Reply via email to