Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetTerminalWidth() into a more proper module #323

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions platform/unix/syspovconsole.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//******************************************************************************
///
/// @file platform/unix/syspovconsole.cpp
///
/// Unix-specific implementation of the @ref pov_base::GetTerminalWidth() function
///
/// @copyright
/// @parblock
///
/// Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
/// Copyright 1991-2016 Persistence of Vision Raytracer Pty. Ltd.
///
/// POV-Ray is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License as
/// published by the Free Software Foundation, either version 3 of the
/// License, or (at your option) any later version.
///
/// POV-Ray is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU Affero General Public License for more details.
///
/// You should have received a copy of the GNU Affero General Public License
/// along with this program. If not, see <http://www.gnu.org/licenses/>.
///
/// ----------------------------------------------------------------------------
///
/// POV-Ray is based on the popular DKB raytracer version 2.12.
/// DKBTrace was originally written by David K. Buck.
/// DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
///
/// @endparblock
///
//******************************************************************************

#include "syspovconsole.h"

#if defined(HAVE_SYS_IOCTL_H)
#include <sys/ioctl.h>
#endif
#if !defined(GWINSZ_IN_SYS_IOCTL)
#include <termios.h>
#endif

// this must be the last file included
#include "base/povdebug.h"

namespace pov_base
{

//******************************************************************************

unsigned int GetTerminalWidth()
{
#if defined(TIOCGWINSZ) && defined(HAVE_IOCTL)
struct winsize w;

// the ioctl call returns non-zero in case of errors (terminal not ready or
// function non supported)
// further, some systems use to return zero even though the call wasn't
// successful, and signal the error condition by filling the column number
// with zero
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0 && w.ws_col != 0)
return (unsigned int)w.ws_col;
#endif
return 80;
}

}
57 changes: 57 additions & 0 deletions platform/unix/syspovconsole.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//******************************************************************************
///
/// @file platform/unix/syspovconsole.h
///
/// Unix-specific declaration of the @ref pov_base::GetTerminalWidth() function
///
/// @copyright
/// @parblock
///
/// Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
/// Copyright 1991-2017 Persistence of Vision Raytracer Pty. Ltd.
///
/// POV-Ray is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License as
/// published by the Free Software Foundation, either version 3 of the
/// License, or (at your option) any later version.
///
/// POV-Ray is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU Affero General Public License for more details.
///
/// You should have received a copy of the GNU Affero General Public License
/// along with this program. If not, see <http://www.gnu.org/licenses/>.
///
/// ----------------------------------------------------------------------------
///
/// POV-Ray is based on the popular DKB raytracer version 2.12.
/// DKBTrace was originally written by David K. Buck.
/// DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
///
/// @endparblock
///
//******************************************************************************

#ifndef POVRAY_UNIX_SYSPOVCONSOLE_H
#define POVRAY_UNIX_SYSPOVCONSOLE_H

#include "base/configbase.h"

namespace pov_base
{

/// Detect the terminal width.
///
/// This Unix-specific function implements the specific (virtual) terminal
/// calls needed to know the width of the current console. This is required by
/// POV-Ray since some diagnostic routines use to wrap text messages at
/// terminal border in a clean way and not let the terminal cut words at any
/// position.
///

unsigned int GetTerminalWidth(void);

}

#endif // POVRAY_UNIX_SYSPOVCONSOLE_H
3 changes: 3 additions & 0 deletions unix/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,9 @@ if test x"$C99_COMPATIBLE_RADIOSITY" != x"" || test x"$ac_cv_sizeof_int" != x"4"
fi
fi

AC_HEADER_TIOCGWINSZ
AC_CHECK_HEADERS([sys/ioctl.h])
AC_CHECK_FUNCS([ioctl])

###############################################################################

Expand Down
7 changes: 7 additions & 0 deletions vfe/vfesession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@

#include "vfe.h"
#include "backend/povray.h"
#ifndef _MSC_VER
#include "syspovconsole.h"
#endif

static POVMSContext POVMS_Output_Context = NULL;

Expand Down Expand Up @@ -74,7 +77,11 @@ vfeSession::vfeSession(int id)
m_MaxGenericMessages = -1;
m_MaxConsoleMessages = -1;
m_OptimizeForConsoleOutput = true;
#ifdef _MSC_VER
m_ConsoleWidth = 80;
#else
m_ConsoleWidth = pov_base::GetTerminalWidth();
#endif
m_RequestFlag = rqNoRequest;
m_RequestResult = 0;
m_StartTime = 0;
Expand Down