* Added new class ShellParameters that bundles all parameters passed to the

shell.
* Also added a parameter for the current working directory and. If supplied,
  it is applied in Shell::_Spawn().
* Pass the current working directory of the active tab when opening a new
  tab. Implements part of #6712.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39457 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold 2010-11-16 20:40:26 +00:00
parent 6a009eb657
commit 37322c4b52
9 changed files with 140 additions and 32 deletions

View File

@ -20,6 +20,7 @@ Application Terminal :
PrefHandler.cpp
PrefWindow.cpp
Shell.cpp
ShellParameters.cpp
SmartTabView.cpp
TermApp.cpp
TerminalBuffer.cpp

View File

@ -40,6 +40,7 @@
#include <extended_system_info_defs.h>
#include "ActiveProcessInfo.h"
#include "ShellParameters.h"
#include "TermConst.h"
#include "TermParse.h"
#include "TerminalBuffer.h"
@ -139,12 +140,12 @@ Shell::~Shell()
status_t
Shell::Open(int row, int col, const char *encoding, int argc, const char **argv)
Shell::Open(int row, int col, const ShellParameters& parameters)
{
if (fFd >= 0)
return B_ERROR;
status_t status = _Spawn(row, col, encoding, argc, argv);
status_t status = _Spawn(row, col, parameters);
if (status < B_OK)
return status;
@ -382,8 +383,10 @@ initialize_termios(struct termios &tio)
#define B_TRANSLATE_CONTEXT "Terminal Shell"
status_t
Shell::_Spawn(int row, int col, const char *encoding, int argc, const char **argv)
Shell::_Spawn(int row, int col, const ShellParameters& parameters)
{
const char** argv = (const char**)parameters.Arguments();
int argc = parameters.ArgumentCount();
const char* defaultArgs[3] = {kDefaultShell, "-l", NULL};
struct passwd passwdStruct;
struct passwd *passwdResult;
@ -533,7 +536,11 @@ Shell::_Spawn(int row, int col, const char *encoding, int argc, const char **arg
*/
setenv("TERM", "xterm", true);
setenv("TTY", ttyName, true);
setenv("TTYPE", encoding, true);
setenv("TTYPE", parameters.Encoding(), true);
// set the current working directory, if one is given
if (parameters.CurrentDirectory().Length() > 0)
chdir(parameters.CurrentDirectory().String());
execve(argv[0], (char * const *)argv, environ);

View File

@ -18,6 +18,7 @@
class ActiveProcessInfo;
class ShellParameters;
// TODO: Maybe merge TermParse and Shell classes ?
class TerminalBuffer;
class TermParse;
@ -28,8 +29,8 @@ public:
Shell();
virtual ~Shell();
status_t Open(int row, int col, const char* encoding,
int argc, const char** argv);
status_t Open(int row, int col,
const ShellParameters& parameters);
void Close();
const char* TTYName() const;
@ -53,8 +54,8 @@ public:
virtual void DetachBuffer();
private:
status_t _Spawn(int row, int col, const char* encoding,
int argc, const char** argv);
status_t _Spawn(int row, int col,
const ShellParameters& parameters);
private:
int fFd;

View File

@ -0,0 +1,40 @@
/*
* Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#include "ShellParameters.h"
ShellParameters::ShellParameters(int argc, const char* const* argv,
const BString& currentDirectory)
:
fArguments(argv),
fArgumentCount(argc),
fCurrentDirectory(currentDirectory),
fEncoding("UTF8")
{
}
void
ShellParameters::SetArguments(int argc, const char* const* argv)
{
fArguments = argv;
fArgumentCount = argc;
}
void
ShellParameters::SetCurrentDirectory(const BString& currentDirectory)
{
fCurrentDirectory = currentDirectory;
}
void
ShellParameters::SetEncoding(const BString& encoding)
{
fEncoding = encoding;
}

View File

@ -0,0 +1,43 @@
/*
* Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#ifndef SHELL_PARAMETERS_H
#define SHELL_PARAMETERS_H
#include <String.h>
class ShellParameters {
public:
ShellParameters(int argc,
const char* const* argv,
const BString& currentDirectory
= BString());
void SetArguments(int argc, const char* const* argv);
const char* const* Arguments() const
{ return fArguments; }
int ArgumentCount() const
{ return fArgumentCount; }
void SetCurrentDirectory(
const BString& currentDirectory);
const BString& CurrentDirectory() const
{ return fCurrentDirectory; }
void SetEncoding(const BString& encoding);
const BString& Encoding() const
{ return fEncoding; }
private:
const char* const* fArguments;
int fArgumentCount;
BString fCurrentDirectory;
BString fEncoding;
};
#endif // SHELL_PARAMETERS_H

View File

@ -53,6 +53,7 @@
#include "Encoding.h"
#include "InlineInput.h"
#include "Shell.h"
#include "ShellParameters.h"
#include "TermConst.h"
#include "TerminalBuffer.h"
#include "TerminalCharClassifier.h"
@ -422,7 +423,8 @@ private:
// #pragma mark -
TermView::TermView(BRect frame, int32 argc, const char** argv, int32 historySize)
TermView::TermView(BRect frame, const ShellParameters& shellParameters,
int32 historySize)
: BView(frame, "termview", B_FOLLOW_ALL,
B_WILL_DRAW | B_FRAME_EVENTS | B_FULL_UPDATE_ON_RESIZE),
fColumns(COLUMNS_DEFAULT),
@ -435,15 +437,15 @@ TermView::TermView(BRect frame, int32 argc, const char** argv, int32 historySize
fReportButtonMouseEvent(false),
fReportAnyMouseEvent(false)
{
status_t status = _InitObject(argc, argv);
status_t status = _InitObject(shellParameters);
if (status != B_OK)
throw status;
SetTermSize(frame);
}
TermView::TermView(int rows, int columns, int32 argc, const char** argv,
int32 historySize)
TermView::TermView(int rows, int columns,
const ShellParameters& shellParameters, int32 historySize)
: BView(BRect(0, 0, 0, 0), "termview", B_FOLLOW_ALL,
B_WILL_DRAW | B_FRAME_EVENTS | B_FULL_UPDATE_ON_RESIZE),
fColumns(columns),
@ -456,7 +458,7 @@ TermView::TermView(int rows, int columns, int32 argc, const char** argv,
fReportButtonMouseEvent(false),
fReportAnyMouseEvent(false)
{
status_t status = _InitObject(argc, argv);
status_t status = _InitObject(shellParameters);
if (status != B_OK)
throw status;
@ -507,7 +509,7 @@ TermView::TermView(BMessage* archive)
}
// TODO: Retrieve colors, history size, etc. from archive
status_t status = _InitObject(argc, argv);
status_t status = _InitObject(ShellParameters(argc, argv));
if (status != B_OK)
throw status;
@ -524,7 +526,7 @@ TermView::TermView(BMessage* archive)
already be initialized; they are not touched by this method.
*/
status_t
TermView::_InitObject(int32 argc, const char** argv)
TermView::_InitObject(const ShellParameters& shellParameters)
{
SetFlags(Flags() | B_WILL_DRAW | B_FRAME_EVENTS
| B_FULL_UPDATE_ON_RESIZE/* | B_INPUT_METHOD_AWARE*/);
@ -596,8 +598,11 @@ TermView::_InitObject(int32 argc, const char** argv)
SetTermFont(be_fixed_font);
error = fShell->Open(fRows, fColumns,
EncodingAsShortString(fEncoding), argc, argv);
// set the shell parameters' encoding
ShellParameters modifiedShellParameters(shellParameters);
modifiedShellParameters.SetEncoding(EncodingAsShortString(fEncoding));
error = fShell->Open(fRows, fColumns, modifiedShellParameters);
if (error < B_OK)
return error;

View File

@ -29,16 +29,19 @@ class BStringView;
class BasicTerminalBuffer;
class InlineInput;
class ResizeWindow;
class ShellParameters;
class TermBuffer;
class TerminalBuffer;
class Shell;
class TermView : public BView {
public:
TermView(BRect frame, int32 argc, const char** argv,
TermView(BRect frame,
const ShellParameters& shellParameters,
int32 historySize);
TermView(int rows, int columns,
const ShellParameters& shellParameters,
int32 historySize);
TermView(int rows, int columns, int32 argc,
const char** argv, int32 historySize);
TermView(BMessage* archive);
~TermView();
@ -133,7 +136,7 @@ private:
inline void _InvalidateTextRect(int32 x1, int32 y1, int32 x2,
int32 y2);
status_t _InitObject(int32 argc, const char** argv);
status_t _InitObject(const ShellParameters& shellParameters);
status_t _AttachShell(Shell* shell);
void _DetachShell();

View File

@ -31,6 +31,7 @@
#include <ScrollView.h>
#include <String.h>
#include "ActiveProcessInfo.h"
#include "Arguments.h"
#include "AppearPrefView.h"
#include "Encoding.h"
@ -38,6 +39,7 @@
#include "Globals.h"
#include "PrefWindow.h"
#include "PrefHandler.h"
#include "ShellParameters.h"
#include "SmartTabView.h"
#include "TermConst.h"
#include "TermScrollView.h"
@ -61,7 +63,8 @@ const static uint32 kSetActiveTab = 'STab';
class CustomTermView : public TermView {
public:
CustomTermView(int32 rows, int32 columns, int32 argc, const char **argv, int32 historySize = 1000);
CustomTermView(int32 rows, int32 columns,
const ShellParameters& shellParameters, int32 historySize = 1000);
virtual void NotifyQuit(int32 reason);
virtual void SetTitle(const char *title);
};
@ -690,7 +693,12 @@ TermWindow::MessageReceived(BMessage *message)
if (fTabView->CountTabs() < kMaxTabs) {
if (fFullScreen)
_ActiveTermView()->ScrollBar()->Show();
_AddTab(NULL);
ActiveProcessInfo info;
if (_ActiveTermView()->GetActiveProcessInfo(info))
_AddTab(NULL, info.CurrentDirectory());
else
_AddTab(NULL);
}
break;
@ -828,22 +836,19 @@ TermWindow::_DoPrint()
void
TermWindow::_AddTab(Arguments* args)
TermWindow::_AddTab(Arguments* args, const BString& currentDirectory)
{
int argc = 0;
const char* const* argv = NULL;
if (args != NULL)
args->GetShellArguments(argc, argv);
ShellParameters shellParameters(argc, argv, currentDirectory);
try {
// Note: I don't pass the Arguments class directly to the termview,
// only to avoid adding it as a dependency: in other words, to keep
// the TermView class as agnostic as possible about the surrounding
// world.
CustomTermView* view = new CustomTermView(
PrefHandler::Default()->getInt32(PREF_ROWS),
PrefHandler::Default()->getInt32(PREF_COLS),
argc, (const char**)argv,
shellParameters,
PrefHandler::Default()->getInt32(PREF_HISTORY_SIZE));
TermViewContainerView* containerView = new TermViewContainerView(view);
@ -1104,9 +1109,10 @@ TermWindow::_NewSessionID()
// CustomTermView
CustomTermView::CustomTermView(int32 rows, int32 columns, int32 argc, const char **argv, int32 historySize)
CustomTermView::CustomTermView(int32 rows, int32 columns,
const ShellParameters& shellParameters, int32 historySize)
:
TermView(rows, columns, argc, argv, historySize)
TermView(rows, columns, shellParameters, historySize)
{
}

View File

@ -79,7 +79,9 @@ private:
void _GetPreferredFont(BFont &font);
status_t _DoPageSetup();
void _DoPrint();
void _AddTab(Arguments* args);
void _AddTab(Arguments* args,
const BString& currentDirectory
= BString());
void _RemoveTab(int32 index);
bool _CanClose(int32 index);
TermViewContainerView* _ActiveTermViewContainerView() const;