Author: bonefish Date: 2010-11-16 21:40:26 +0100 (Tue, 16 Nov 2010) New Revision: 39457 Changeset: http://dev.haiku-os.org/changeset/39457 Ticket: http://dev.haiku-os.org/ticket/6712 Added: haiku/trunk/src/apps/terminal/ShellParameters.cpp haiku/trunk/src/apps/terminal/ShellParameters.h Modified: haiku/trunk/src/apps/terminal/Jamfile haiku/trunk/src/apps/terminal/Shell.cpp haiku/trunk/src/apps/terminal/Shell.h haiku/trunk/src/apps/terminal/TermView.cpp haiku/trunk/src/apps/terminal/TermView.h haiku/trunk/src/apps/terminal/TermWindow.cpp haiku/trunk/src/apps/terminal/TermWindow.h Log: * 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. Modified: haiku/trunk/src/apps/terminal/Jamfile =================================================================== --- haiku/trunk/src/apps/terminal/Jamfile 2010-11-16 20:08:17 UTC (rev 39456) +++ haiku/trunk/src/apps/terminal/Jamfile 2010-11-16 20:40:26 UTC (rev 39457) @@ -20,6 +20,7 @@ PrefHandler.cpp PrefWindow.cpp Shell.cpp + ShellParameters.cpp SmartTabView.cpp TermApp.cpp TerminalBuffer.cpp Modified: haiku/trunk/src/apps/terminal/Shell.cpp =================================================================== --- haiku/trunk/src/apps/terminal/Shell.cpp 2010-11-16 20:08:17 UTC (rev 39456) +++ haiku/trunk/src/apps/terminal/Shell.cpp 2010-11-16 20:40:26 UTC (rev 39457) @@ -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 @@ 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 @@ #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,8 +536,12 @@ */ 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); // Exec failed. Modified: haiku/trunk/src/apps/terminal/Shell.h =================================================================== --- haiku/trunk/src/apps/terminal/Shell.h 2010-11-16 20:08:17 UTC (rev 39456) +++ haiku/trunk/src/apps/terminal/Shell.h 2010-11-16 20:40:26 UTC (rev 39457) @@ -18,6 +18,7 @@ class ActiveProcessInfo; +class ShellParameters; // TODO: Maybe merge TermParse and Shell classes ? class TerminalBuffer; class TermParse; @@ -28,8 +29,8 @@ 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 @@ 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; Added: haiku/trunk/src/apps/terminal/ShellParameters.cpp =================================================================== --- haiku/trunk/src/apps/terminal/ShellParameters.cpp (rev 0) +++ haiku/trunk/src/apps/terminal/ShellParameters.cpp 2010-11-16 20:40:26 UTC (rev 39457) @@ -0,0 +1,40 @@ +/* + * Copyright 2010, Ingo Weinhold, ingo_weinhold@xxxxxxx + * 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; +} Added: haiku/trunk/src/apps/terminal/ShellParameters.h =================================================================== --- haiku/trunk/src/apps/terminal/ShellParameters.h (rev 0) +++ haiku/trunk/src/apps/terminal/ShellParameters.h 2010-11-16 20:40:26 UTC (rev 39457) @@ -0,0 +1,43 @@ +/* + * Copyright 2010, Ingo Weinhold, ingo_weinhold@xxxxxxx + * 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 Modified: haiku/trunk/src/apps/terminal/TermView.cpp =================================================================== --- haiku/trunk/src/apps/terminal/TermView.cpp 2010-11-16 20:08:17 UTC (rev 39456) +++ haiku/trunk/src/apps/terminal/TermView.cpp 2010-11-16 20:40:26 UTC (rev 39457) @@ -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 @@ // #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 @@ 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 @@ 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 @@ } // 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 @@ 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,9 +598,12 @@ 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; Modified: haiku/trunk/src/apps/terminal/TermView.h =================================================================== --- haiku/trunk/src/apps/terminal/TermView.h 2010-11-16 20:08:17 UTC (rev 39456) +++ haiku/trunk/src/apps/terminal/TermView.h 2010-11-16 20:40:26 UTC (rev 39457) @@ -29,16 +29,19 @@ 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, int32 argc, - const char** argv, int32 historySize); + TermView(int rows, int columns, + const ShellParameters& shellParameters, + int32 historySize); TermView(BMessage* archive); ~TermView(); @@ -133,7 +136,7 @@ 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(); Modified: haiku/trunk/src/apps/terminal/TermWindow.cpp =================================================================== --- haiku/trunk/src/apps/terminal/TermWindow.cpp 2010-11-16 20:08:17 UTC (rev 39456) +++ haiku/trunk/src/apps/terminal/TermWindow.cpp 2010-11-16 20:40:26 UTC (rev 39457) @@ -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 @@ 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 @@ 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 @@ 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 @@ // 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) { } Modified: haiku/trunk/src/apps/terminal/TermWindow.h =================================================================== --- haiku/trunk/src/apps/terminal/TermWindow.h 2010-11-16 20:08:17 UTC (rev 39456) +++ haiku/trunk/src/apps/terminal/TermWindow.h 2010-11-16 20:40:26 UTC (rev 39457) @@ -79,7 +79,9 @@ 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;