From: Petr Tesarik <ptesarik@suse.cz>
Subject: Do not use any formatting on dumb terminals
References: bnc#804602

The 3270 console does not understand ANSI colours or '\r'.

Signed-off-by: Petr Tesarik <ptesarik@suse.cz>

---
 kdumptool/debug.cc    |   14 ++++++++---
 kdumptool/progress.cc |   62 ++++++++++++++++++++++----------------------------
 kdumptool/progress.h  |   61 ++++++++++++++++++++++++++-----------------------
 3 files changed, 71 insertions(+), 66 deletions(-)

--- a/kdumptool/debug.cc
+++ b/kdumptool/debug.cc
@@ -20,6 +20,7 @@
 #include <cstdio>
 #include <cstdarg>
 #include <cstring>
+#include <cstdlib>
 #include <string>
 #include <sstream>
 #include <unistd.h>
@@ -32,8 +33,9 @@ Debug *Debug::m_instance = NULL;
 using std::memset;
 using std::strcat;
 using std::stringstream;
-using std::strcat;
+using std::strcmp;
 using std::strlen;
+using std::getenv;
 using std::string;
 using std::endl;
 using std::cerr;
@@ -219,9 +221,13 @@ void Debug::setStderrUseColor(bool useCo
 // -----------------------------------------------------------------------------
 bool Debug::getStderrUseColor() const
 {
-    if (m_useColorAuto)
-        return isatty(STDERR_FILENO);
-    else
+    if (m_useColorAuto) {
+        const char *term = getenv("TERM");
+        if (term && !strcmp(term, "dumb"))
+            return false;
+        else
+            return isatty(STDERR_FILENO);
+    } else
         return m_useColor;
 }
 
--- a/kdumptool/progress.cc
+++ b/kdumptool/progress.cc
@@ -18,6 +18,7 @@
  */
 #include <iomanip>
 #include <iostream>
+#include <cstdlib>
 
 #include <unistd.h>
 #include <termios.h>
@@ -38,48 +39,36 @@ using std::right;
 using std::endl;
 using std::flush;
 using std::ostream;
+using std::getenv;
 
 //{{{ Terminal -----------------------------------------------------------------
 
 // -----------------------------------------------------------------------------
-Terminal::Size Terminal::getSize(bool *defaultUsed) const
+Terminal::Terminal(void)
     throw ()
 {
-    Terminal::Size sz;
     struct winsize winsize;
 
     int err = ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
-    if (err != 0 || (winsize.ws_col == 0 && winsize.ws_row == 0)) {
-        sz.width = DEFAULT_WIDTH;
-        sz.height = DEFAULT_HEIGHT;
-
-        if (defaultUsed) {
-            *defaultUsed = true;
-        }
-
-        return sz;
-    }
-
-    sz.width = winsize.ws_col;
-    sz.height = winsize.ws_row;
-
-    if (defaultUsed) {
-        *defaultUsed = false;
+    if (!err && winsize.ws_col != 0 && winsize.ws_row != 0) {
+	m_width = winsize.ws_col;
+	m_height = winsize.ws_row;
+    } else {
+        m_width = DEFAULT_WIDTH;
+        m_height = DEFAULT_HEIGHT;
     }
 
-
-    return sz;
+    string term(getenv("TERM") ?: ""); 
+    m_isdumb = (term == "dumb");
 }
 
 // -----------------------------------------------------------------------------
 void Terminal::printLine(ostream &os) const
     throw ()
 {
-    int width = getSize().width - 1;
-
-    for (int i = 0; i < width; i++)
-        os << '-';
-    os << endl;
+    char prevfill = os.fill('-');
+    os << setw(m_width-1) << "" << endl;
+    os.fill(prevfill);
 }
 
 //}}}
@@ -88,19 +77,16 @@ void Terminal::printLine(ostream &os) co
 // -----------------------------------------------------------------------------
 TerminalProgress::TerminalProgress(const string &name)
     throw ()
-    : m_name(name), m_lastUpdate(0)
+    : m_term(), m_name(name), m_lastUpdate(0)
 {
     // truncate the name
     if (m_name.size() > NAME_MAXLENGTH) {
         m_name = m_name.substr(0, NAME_MAXLENGTH);
     }
 
-    Terminal t;
-    m_linelen = t.getSize().width;
-
     // subtract 1 for the space between string and bar, 1 for the leading
     // '|' and 1 for the trailing '|', and one space free, 4 for ...%
-    m_progresslen = m_linelen - NAME_MAXLENGTH - 8;
+    m_progresslen = m_term.width() - NAME_MAXLENGTH - 8;
 }
 
 // -----------------------------------------------------------------------------
@@ -108,7 +94,7 @@ void TerminalProgress::start()
     throw ()
 {
     clearLine();
-    cout << "\r" << setw(NAME_MAXLENGTH) << left << m_name << " Starting.";
+    cout << setw(NAME_MAXLENGTH) << left << m_name << " Starting." << flush;
 }
 
 // -----------------------------------------------------------------------------
@@ -139,7 +125,12 @@ void TerminalProgress::progressed(unsign
 
     number_of_dashes = m_progresslen - number_of_hashes;
 
-    cout << "\r" << setw(NAME_MAXLENGTH) << left << m_name << " ";
+    if (m_term.isdumb())
+        cout << endl;
+    else
+        cout << '\r';
+
+    cout << setw(NAME_MAXLENGTH) << left << m_name << " ";
     cout << "|";
     for (int i = 0; i < number_of_hashes; i++)
         cout << '#';
@@ -160,7 +151,9 @@ void TerminalProgress::stop(bool success
         : " Failed.";
 
     clearLine();
-    cout << "\r" << setw(NAME_MAXLENGTH) << left << m_name << finish_msg;
+    if (m_term.isdumb())
+        cout << endl;
+    cout << setw(NAME_MAXLENGTH) << left << m_name << finish_msg;
     cout << endl;
 }
 
@@ -168,7 +161,8 @@ void TerminalProgress::stop(bool success
 void TerminalProgress::clearLine()
     throw ()
 {
-    cout << "\r" << setw(m_linelen) << " ";
+    if (!m_term.isdumb())
+        cout << "\r" << setw(m_term.width() - 1) << " " << "\r";
 }
 
 //}}}
--- a/kdumptool/progress.h
+++ b/kdumptool/progress.h
@@ -33,44 +33,47 @@ class Terminal {
 
     public:
         /**
-         * Represents a terminal size.
-         */
-        struct Size {
-            /** The width */
-            int width;
-            /** The height */
-            int height;
-        };
+	 * Initialize a new Terminal object for standard output.
+	 */
+	Terminal (void)
+	throw ();
 
     public:
-        /**
-         * Deletes a Terminal object.
-         */
-        virtual ~Terminal()
-        throw () {}
 
-    public:
+	/**
+	 * Get the terminal width.
+	 */
+	int width(void) const
+	throw ()
+	{ return m_width; }
+
+	/**
+	 * Get the terminal height.
+	 */
+	int height(void) const
+	throw ()
+	{ return m_height; }
+
+	bool isdumb(void) const
+	throw ()
+	{ return m_isdumb; }
 
         /**
-         * Retriesves the terminal size.
-         *
-         * @param[out] defaultUsed if that variable is not @c NULL,
-         *             it will be set to @c true if the size of the terminal
-         *             cannot be detected by ioctl() and a default value
-         *             will be assumed and to @c false otherwise.
-         *
-         * @return a size object that represents the terminal size,
-         *         and (0, 0) if the size cannot be retrieved (mostly because
-         *         the standard output is no terminal but redirected to a file)
+         * Prints a horizontal line, as large as the terminal.
          */
-        Size getSize(bool *defaultUsed = NULL) const
+        void printLine(std::ostream &os = std::cout) const
         throw ();
 
+    protected:
         /**
-         * Prints a horizontal line, as large as the terminal.
+         * Represents a terminal size.
          */
-        void printLine(std::ostream &os = std::cout) const
-        throw ();
+	int m_width, m_height;
+
+	/**
+	 * True if this is a dumb terminal.
+	 */
+	bool m_isdumb;
 };
 
 //}}}
@@ -164,6 +167,8 @@ class TerminalProgress : public Progress
         void clearLine()
         throw ();
 
+        Terminal m_term;
+
     private:
         std::string m_name;
         int m_linelen;
