Blame SOURCES/xfsprogs-4.10.0-xfs_db-fix-the-source-command-when-passed-as-a-c-opt.patch

e59f31
From c8dc4235614292020f82a031545d66c000b455f9 Mon Sep 17 00:00:00 2001
e59f31
From: "Darrick J. Wong" <darrick.wong@oracle.com>
e59f31
Date: Wed, 25 Jan 2017 20:02:43 -0600
e59f31
Subject: [PATCH] xfs_db: fix the 'source' command when passed as a -c option
e59f31
e59f31
The 'source' command is supposed to read commands out of a file and
e59f31
execute them.  This works great when done from an interactive command
e59f31
line, but it doesn't work at all when invoked from the command line
e59f31
because we never actually do anything with the opened file.
e59f31
e59f31
So don't load stdin into the input stack when we're only executing
e59f31
command line options, and use that to decide if source_f is executing
e59f31
from the command line so that we can actually run the input loop.  We'll
e59f31
use this for the per-field fuzzing xfstests.
e59f31
e59f31
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
e59f31
Reviewed-by: Eric Sandeen <sandeen@redhat.com>
e59f31
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
e59f31
---
e59f31
 db/init.c  |  2 +-
e59f31
 db/input.c | 43 +++++++++++++++++++++++++++++++++----------
e59f31
 2 files changed, 34 insertions(+), 11 deletions(-)
e59f31
e59f31
Index: xfsprogs-4.5.0/db/init.c
e59f31
===================================================================
e59f31
--- xfsprogs-4.5.0.orig/db/init.c
e59f31
+++ xfsprogs-4.5.0/db/init.c
e59f31
@@ -192,7 +192,6 @@ main(
e59f31
 	char	**v;
e59f31
 	int	start_iocur_sp;
e59f31
 
e59f31
-	pushfile(stdin);
e59f31
 	init(argc, argv);
e59f31
 	start_iocur_sp = iocur_sp;
e59f31
 
e59f31
@@ -207,6 +206,7 @@ main(
e59f31
 		goto close_devices;
e59f31
 	}
e59f31
 
e59f31
+	pushfile(stdin);
e59f31
 	while (!done) {
e59f31
 		if ((input = fetchline()) == NULL)
e59f31
 			break;
e59f31
Index: xfsprogs-4.5.0/db/input.c
e59f31
===================================================================
e59f31
--- xfsprogs-4.5.0.orig/db/input.c
e59f31
+++ xfsprogs-4.5.0/db/input.c
e59f31
@@ -156,7 +156,7 @@ fetchline_internal(void)
e59f31
 
e59f31
 	rval = NULL;
e59f31
 	for (rlen = iscont = 0; ; ) {
e59f31
-		if (inputstacksize == 1) {
e59f31
+		if (curinput == stdin) {
e59f31
 			if (iscont)
e59f31
 				dbprintf("... ");
e59f31
 			else
e59f31
@@ -181,18 +181,24 @@ fetchline_internal(void)
e59f31
 		}
e59f31
 		if (ferror(curinput) || feof(curinput) ||
e59f31
 		    (len = strlen(buf)) == 0) {
e59f31
-			popfile();
e59f31
-			if (curinput == NULL) {
e59f31
+			/*
e59f31
+			 * No more input at this inputstack level; pop
e59f31
+			 * our fd off and return so that a lower
e59f31
+			 * level fetchline can handle us.  If this was
e59f31
+			 * an interactive session, print a newline
e59f31
+			 * because ^D doesn't emit one.
e59f31
+			 */
e59f31
+			if (curinput == stdin)
e59f31
 				dbprintf("\n");
e59f31
-				return NULL;
e59f31
-			}
e59f31
+
e59f31
+			popfile();
e59f31
 			iscont = 0;
e59f31
 			rlen = 0;
e59f31
 			if (rval) {
e59f31
 				xfree(rval);
e59f31
 				rval = NULL;
e59f31
 			}
e59f31
-			continue;
e59f31
+			return NULL;
e59f31
 		}
e59f31
 		if (inputstacksize == 1)
e59f31
 			logprintf("%s", buf);
e59f31
@@ -225,7 +231,9 @@ fetchline(void)
e59f31
 
e59f31
 	if (inputstacksize == 1) {
e59f31
 		line = readline(get_prompt());
e59f31
-		if (line && *line) {
e59f31
+		if (!line)
e59f31
+			dbprintf("\n");
e59f31
+		else if (line && *line) {
e59f31
 			add_history(line);
e59f31
 			logprintf("%s", line);
e59f31
 		}
e59f31
@@ -314,12 +322,27 @@ source_f(
e59f31
 	char	**argv)
e59f31
 {
e59f31
 	FILE	*f;
e59f31
+	int	c, done = 0;
e59f31
+	char	*input;
e59f31
+	char	**v;
e59f31
 
e59f31
 	f = fopen(argv[1], "r");
e59f31
-	if (f == NULL)
e59f31
+	if (f == NULL) {
e59f31
 		dbprintf(_("can't open %s\n"), argv[0]);
e59f31
-	else
e59f31
-		pushfile(f);
e59f31
+		return 0;
e59f31
+	}
e59f31
+
e59f31
+	/* Run the sourced commands now. */
e59f31
+	pushfile(f);
e59f31
+	while (!done) {
e59f31
+		if ((input = fetchline_internal()) == NULL)
e59f31
+			break;
e59f31
+		v = breakline(input, &c);
e59f31
+		if (c)
e59f31
+			done = command(c, v);
e59f31
+		doneline(input, v);
e59f31
+	}
e59f31
+
e59f31
 	return 0;
e59f31
 }
e59f31