[SLOF] [PATCH v3 4/6] libnet: Simplify the Forth-to-C wrapper of ping()

Thomas Huth thuth at redhat.com
Wed Oct 12 21:44:06 AEDT 2016


Now that we do not link libnet against net-snk anymore, we can
change the prototype of ping() and thus simplify the "net-ping"
Forth-to-C wrapper. There is no need to convert the parameters
to a temporary argv[] array anymore.

Signed-off-by: Thomas Huth <thuth at redhat.com>
---
 lib/libnet/libnet.code       | 15 ++-------------
 lib/libnet/netapps.h         |  2 +-
 lib/libnet/ping.c            | 20 ++++++++++++--------
 slof/fs/packages/obp-tftp.fs |  5 +----
 4 files changed, 16 insertions(+), 26 deletions(-)

diff --git a/lib/libnet/libnet.code b/lib/libnet/libnet.code
index a1c5a90..ab67fac 100644
--- a/lib/libnet/libnet.code
+++ b/lib/libnet/libnet.code
@@ -19,18 +19,7 @@ PRIM(NET_X2d_LOAD)
 MIRP
 
 PRIM(NET_X2d_PING)
-	int slen = TOS.n; POP;
+	int alen = TOS.n; POP;
 	char *arg = TOS.a;
-	char *argvs[8];
-	int i, p;
-	argvs[0] = arg;
-	i = 1;
-	for (p = 0; p < slen; p++) {
-		if (arg[p] == ' ') {
-			arg[p] = 0;
-			argvs[i] = &arg[p + 1];
-			i++;
-		}
-	}
-	TOS.n = ping(i, argvs);
+	TOS.n = ping(arg, alen);
 MIRP
diff --git a/lib/libnet/netapps.h b/lib/libnet/netapps.h
index 693de69..00b6318 100644
--- a/lib/libnet/netapps.h
+++ b/lib/libnet/netapps.h
@@ -20,7 +20,7 @@ struct filename_ip;
 
 extern int netboot(int argc, char *argv[]);
 extern int netsave(int argc, char *argv[]);
-extern int ping(int argc, char *argv[]);
+extern int ping(char *args_fs, int alen);
 extern int dhcp(char *ret_buffer, struct filename_ip *fn_ip,
 		unsigned int retries, int flags);
 
diff --git a/lib/libnet/ping.c b/lib/libnet/ping.c
index 3da519f..edad5eb 100644
--- a/lib/libnet/ping.c
+++ b/lib/libnet/ping.c
@@ -106,8 +106,7 @@ parse_args(const char *args, struct ping_args *ping_args)
 	return 0;
 }
 
-int
-ping(int argc, char *argv[])
+int ping(char *args_fs, int alen)
 {
 	short arp_failed = 0;
 	filename_ip_t fn_ip;
@@ -115,15 +114,20 @@ ping(int argc, char *argv[])
 	struct ping_args ping_args;
 	uint8_t own_mac[6];
 	uint32_t netmask;
+	char args[256];
 
 	memset(&ping_args, 0, sizeof(struct ping_args));
 
-	if (argc == 2) {
-		if (parse_args(argv[1], &ping_args)) {
-			usage();
-			return -1;
-		}
-	} else {
+	if (alen <= 0 && alen >= sizeof(args) - 1) {
+		usage();
+		return -1;
+	}
+
+	/* Convert forth string into NUL-terminated C-string */
+	memcpy(args, args_fs, alen);
+	args[alen] = 0;
+
+	if (parse_args(args, &ping_args)) {
 		usage();
 		return -1;
 	}
diff --git a/slof/fs/packages/obp-tftp.fs b/slof/fs/packages/obp-tftp.fs
index 84ac439..30070a6 100644
--- a/slof/fs/packages/obp-tftp.fs
+++ b/slof/fs/packages/obp-tftp.fs
@@ -70,8 +70,5 @@ INSTANCE VARIABLE ciregs-buffer
 ;
 
 : ping  ( -- )
-    s" ping " my-args $cat
-    \ Zero-terminate string:
-    s"  " $cat 2dup + 1 - 0 swap c!
-    net-ping
+    my-args net-ping
 ;
-- 
1.8.3.1



More information about the SLOF mailing list