changeset 135:e4257294102b

fc-xram: execvp of secondary program implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 03 Nov 2013 00:14:40 +0000
parents e0d56e9be8a2
children 3b5c3f3646fb
files loadtools/clmain.c
diffstat 1 files changed, 40 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/loadtools/clmain.c	Sat Nov 02 23:15:42 2013 +0000
+++ b/loadtools/clmain.c	Sun Nov 03 00:14:40 2013 +0000
@@ -26,6 +26,9 @@
 struct baudrate *xram_load_baudrate;
 struct baudrate *xram_run_baudrate = baud_rate_table;	/* 1st entry default */
 
+char **passon_argv;
+int passon_argc;
+
 main(argc, argv)
 	char **argv;
 {
@@ -34,7 +37,7 @@
 	int c;
 	struct baudrate *br;
 
-	while ((c = getopt(argc, argv, "a:b:B:h:H:i:n")) != EOF)
+	while ((c = getopt(argc, argv, "+a:b:B:h:H:i:nr:")) != EOF)
 		switch (c) {
 		case 'a':
 			iramimage.filename = optarg;
@@ -60,10 +63,16 @@
 		case 'n':
 			gta_modem_poweron = 0;
 			continue;
+		case 'r':
+			br = find_baudrate_by_name(optarg);
+			if (!br)
+				exit(1);	/* error msg already printed */
+			xram_run_baudrate = br;
+			continue;
 		case '?':
 		default:
 usage:			fprintf(stderr,
-		"usage: fc-xram [options] ttyport xramimage.srec [runbaud]\n");
+		"usage: fc-xram [options] ttyport xramimage.srec [2ndprog]\n");
 			exit(1);
 		}
 	if (argc - optind < 2)
@@ -73,12 +82,8 @@
 	if (!iramimage.filename)
 		iramimage.filename = default_loadagent_image;
 	if (argc - optind >= 3) {
-		if (argc - optind > 3)
-			goto usage;
-		br = find_baudrate_by_name(argv[optind+2]);
-		if (!br)
-			exit(1);	/* error msg already printed */
-		xram_run_baudrate = br;
+		passon_argv = argv + optind + 2;
+		passon_argc = argc - optind - 2;
 	}
 
 	open_target_serial();
@@ -99,6 +104,33 @@
 	}
 	printf("Sending XRAM image to loadagent\n");
 	perform_chain_load();
+	if (passon_argv)
+		exec_2nd_prog();
 	tty_passthru();
 	exit(0);
 }
+
+exec_2nd_prog()
+{
+	char **execp_argv;
+	char **sp, **dp;
+	extern int target_fd;
+	char desc_arg[16];
+
+	sprintf(desc_arg, "-d%d", target_fd);
+	execp_argv = (char **) malloc(sizeof(char *) * (passon_argc + 2));
+	if (!execp_argv) {
+		perror("malloc argv for execvp");
+		exit(1);
+	}
+	sp = passon_argv;
+	dp = execp_argv;
+	*dp++ = *sp++;
+	*dp++ = desc_arg;
+	while (*sp)
+		*dp++ = *sp++;
+	*dp = NULL;
+	execvp(execp_argv[0], execp_argv);
+	fprintf(stderr, "Unable to execvp %s\n", passon_argv[0]);
+	exit(1);
+}