#!/bin/bash

## This patch adds support for -W source parameter to awk
cat >awk.patch <<EOF
diff -ruN a/editors/awk.c b/editors/awk.c
--- a/editors/awk.c	2009-10-08 02:59:09.000000000 +0200
+++ b/editors/awk.c	2010-08-26 10:48:48.000000000 +0200
@@ -2795,6 +2795,7 @@
 	char *opt_F, *opt_W;
 	llist_t *list_v = NULL;
 	llist_t *list_f = NULL;
+	llist_t *list_W = NULL;
 	int i, j;
 	var *v;
 	var tv;
@@ -2853,8 +2854,8 @@
 			*s1 = '=';
 		}
 	}
-	opt_complementary = "v::f::"; /* -v and -f can occur multiple times */
-	opt = getopt32(argv, "F:v:f:W:", &opt_F, &list_v, &list_f, &opt_W);
+	opt_complementary = "v::f::W::"; /* -v and -f can occur multiple times */
+	opt = getopt32(argv, "F:v:f:W:", &opt_F, &list_v, &list_f, &list_W);
 	argv += optind;
 	argc -= optind;
 	if (opt & 0x1)
@@ -2888,7 +2889,16 @@
 		parse_program(*argv++);
 	}
 	if (opt & 0x8) // -W
-		bb_error_msg("warning: unrecognized option '-W %s' ignored", opt_W);
+		while ( list_W ) {
+			char *s;
+			s = llist_pop(&list_W);
+			if ( strcmp(s, "source") ) {
+				bb_error_msg("warning: unrecognized option '-W %s' ignored\n", s);
+				continue;
+			}
+			g_progname = "cmd. line";
+			parse_program(*argv++);
+		}
 
 	/* fill in ARGV array */
 	setvar_i(intvar[ARGC], argc);
EOF

patch -p1 < awk.patch
rm -f awk.patch

## This patch adds the vendorspec option to dhcpd. The value for the
## parameter is given as a list of hex bytes:
## option vendorspec = 40:56:20:3:56:12
cat >dhcp.patch <<EOF
diff -ruN a/networking/udhcp/files.c b/networking/udhcp/files.c
--- a/networking/udhcp/files.c	2009-09-26 15:14:57.000000000 +0200
+++ b/networking/udhcp/files.c	2010-12-16 10:23:38.464000933 +0100
@@ -75,6 +75,23 @@
 	return 0;
 }
 
+static int FAST_FUNC read_binary(char *line, void *arg)
+{
+	char **dest = arg;
+	char *saveptr;
+	char *val, *l = xstrdup(line);
+	int i = 0;
+
+	// caller already uses strtok, so we must not.
+	val = strtok_r(l, ":", &saveptr);
+	while ( val ) {
+		line[i++] = (char)strtol(val, NULL, 16);
+		val = strtok_r(NULL, ":", &saveptr);
+	}
+	free(l);
+	*dest = line;
+	return i;
+}
 
 /* find option 'code' in opt_list */
 struct option_set* FAST_FUNC find_option(struct option_set *opt_list, uint8_t code)
@@ -242,6 +259,10 @@
 			retval = (endptr[0] == '\0');
 			break;
 		}
+		case OPTION_BINARY:
+			length = read_binary(val, &opt);
+			retval = (length > 0);
+			break;
 		default:
 			break;
 		}
diff -ruN a/networking/udhcp/options.c b/networking/udhcp/options.c
--- a/networking/udhcp/options.c	2009-10-08 02:59:09.000000000 +0200
+++ b/networking/udhcp/options.c	2010-12-16 09:55:15.260000933 +0100
@@ -34,6 +34,7 @@
 	{ OPTION_STRING                           , 0x28 }, /* nisdomain          */
 	{ OPTION_IP | OPTION_LIST                 , 0x29 }, /* nissrv             */
 	{ OPTION_IP | OPTION_LIST     | OPTION_REQ, 0x2a }, /* DHCP_NTP_SERVER    */
+	{ OPTION_BINARY                           , 0x2b }, /* vendor specific    */
 	{ OPTION_IP | OPTION_LIST                 , 0x2c }, /* DHCP_WINS_SERVER   */
 	{ OPTION_IP                               , 0x32 }, /* DHCP_REQUESTED_IP  */
 	{ OPTION_U32                              , 0x33 }, /* DHCP_LEASE_TIME    */
@@ -84,6 +85,7 @@
 	"nisdomain" "\0"   /*                     */
 	"nissrv" "\0"      /*                     */
 	"ntpsrv" "\0"      /* DHCP_NTP_SERVER     */
+	"vendorspec" "\0"  /* Vendor Specific     */
 	"wins" "\0"        /* DHCP_WINS_SERVER    */
 	"requestip" "\0"   /* DHCP_REQUESTED_IP   */
 	"lease" "\0"       /* DHCP_LEASE_TIME     */
@@ -120,6 +122,7 @@
 	[OPTION_S32] =     4,
 	/* Just like OPTION_STRING, we use minimum length here */
 	[OPTION_STATIC_ROUTES] = 5,
+	[OPTION_BINARY] =  1,
 };
 
 
diff -ruN a/networking/udhcp/options.h b/networking/udhcp/options.h
--- a/networking/udhcp/options.h	2009-09-26 15:14:57.000000000 +0200
+++ b/networking/udhcp/options.h	2010-12-16 09:54:07.240000934 +0100
@@ -21,6 +21,7 @@
 	OPTION_U32,
 	OPTION_S32,
 	OPTION_STATIC_ROUTES,
+	OPTION_BINARY,
 };
 
 /* Client requests this option by default */
EOF

patch -p1 < dhcp.patch
rm -f dhcp.patch

