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

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