diff --git a/src/apps/terminal/Coding.h b/src/apps/terminal/Coding.h index 529e19d32f..b09acb9832 100644 --- a/src/apps/terminal/Coding.h +++ b/src/apps/terminal/Coding.h @@ -83,7 +83,7 @@ const uint32 coding_translation_table[] = { }; -status_t get_nth_encoding(int i, int *op); +status_t get_nth_encoding(int i, int *id); int longname2id(const char *longname); const char * longname2shortname(const char *longname); @@ -93,5 +93,4 @@ const char id2shortcut(int op); void SetEncoding(int encoding); int GetEncoding(); - #endif /* _CODING_H_ */ diff --git a/src/apps/terminal/Shell.cpp b/src/apps/terminal/Shell.cpp index a8c9303364..919269660b 100644 --- a/src/apps/terminal/Shell.cpp +++ b/src/apps/terminal/Shell.cpp @@ -1,4 +1,5 @@ /* + * Copyright 2007 Haiku, inc. * Copyright (c) 2003-4 Kian Duffy * Copyright (c) 2004 Daniel Furrer * Parts Copyright (C) 1998,99 Kazuho Okui and Takashi Murai. @@ -54,10 +55,6 @@ #define SHELL_COMMAND "/bin/sh -login" -const static char *kSpawnAlertMessage = "alert --stop " "'Cannot execute \"%s\":\n" - "\t%s\n'" - "'Use Default Shell' 'Abort'"; - /* * Set environment variable. */ @@ -92,11 +89,6 @@ setenv(const char *var, const char *value, bool overwrite) #endif -/* - * spawn_shell(): spawn child process, create pty master/slave device and - * execute SHELL program. - */ - /* handshake interface */ typedef struct { @@ -112,10 +104,111 @@ typedef struct #define PTY_WS 2 /* pty need WINSIZE (row and col ) */ - static pid_t sShPid; +Shell::Shell() + :fFd(-1) +{ +} + + +Shell::~Shell() +{ + Close(); +} + + +status_t +Shell::Open(int row, int col, const char *command, const char *coding) +{ + if (fFd >= 0) + return B_ERROR; + + return _Spawn(row, col, command, coding); +} + + +void +Shell::Close() +{ + if (fFd >= 0) { + close(fFd); + kill(-sShPid, SIGHUP); + int status; + wait(&status); + fFd = -1; + } +} + + +const char * +Shell::TTYName() const +{ + return ttyname(fFd); +} + + +ssize_t +Shell::Read(void *buffer, size_t numBytes) +{ + if (fFd < 0) + return B_NO_INIT; + + return read(fFd, buffer, numBytes); +} + + +ssize_t +Shell::Write(const void *buffer, size_t numBytes) +{ + if (fFd < 0) + return B_NO_INIT; + + return write(fFd, buffer, numBytes); +} + + +void +Shell::UpdateWindowSize(int rows, int columns) +{ + struct winsize winSize; + winSize.ws_row = rows; + winSize.ws_col = columns; + ioctl(fFd, TIOCSWINSZ, &winSize); + Signal(SIGWINCH); +} + + +void +Shell::Signal(int signal) +{ + kill(-sShPid, signal); +} + + +status_t +Shell::GetAttr(struct termios &attr) +{ + return tcgetattr(fFd, &attr); +} + + +status_t +Shell::SetAttr(struct termios &attr) +{ + return tcsetattr(fFd, TCSANOW, &attr); +} + + +int +Shell::FD() const +{ + return fFd; +} + + +// private static status_t send_handshake_message(thread_id target, const handshake_t& handshake) { @@ -131,14 +224,14 @@ receive_handshake_message(handshake_t& handshake) } -static int -spawn_shell(int row, int col, const char *command, const char *coding) +status_t +Shell::_Spawn(int row, int col, const char *command, const char *coding) { signal(SIGTTOU, SIG_IGN); /* * Get a pseudo-tty. We do this by cycling through files in the - * directory. The oparationg system will not allow us to open a master + * directory. The operationg system will not allow us to open a master * which is already in use, so we simply go until the open succeeds. */ char ttyName[B_PATH_NAME_LENGTH]; @@ -170,27 +263,27 @@ spawn_shell(int row, int col, const char *command, const char *coding) if (master < 0) { printf("didn't find any available pseudo ttys."); - return -1; + return B_ERROR; } - /* - * Get the modes of the current terminal. We will duplicates these - * on the pseudo terminal. - */ + /* + * Get the modes of the current terminal. We will duplicates these + * on the pseudo terminal. + */ thread_id terminalThread = find_thread(NULL); /* Fork a child process. */ if ((sShPid = fork()) < 0) { close(master); - return -1; + return B_ERROR; } handshake_t handshake; if (sShPid == 0) { - // Now in child process. + // Now in child process. /* * Make our controlling tty the pseudo tty. This hapens because @@ -408,8 +501,12 @@ spawn_shell(int row, int col, const char *command, const char *coding) * Exec failed. */ sleep(1); + const char *spawnAlertMessage = "alert --stop " + "'Cannot execute \"%s\":\n" + "\t%s\n'" + "'Use Default Shell' 'Abort'"; char errorMessage[256]; - snprintf(errorMessage, sizeof(errorMessage), kSpawnAlertMessage, commandLine, strerror(errno)); + snprintf(errorMessage, sizeof(errorMessage), spawnAlertMessage, commandLine, strerror(errno)); if (system(errorMessage) == 0) execl("/bin/sh", "/bin/sh", "-login", NULL); @@ -449,119 +546,11 @@ spawn_shell(int row, int col, const char *command, const char *coding) } } - return (done > 0) ? master : -1; -} + if (done <= 0) + return B_ERROR; - -static void -close_shell(int fd) -{ - if (fd < 0) - return; - - close(fd); - - int status; - kill(-sShPid, SIGHUP); - wait(&status); -} - - -Shell::Shell() - :fFd(-1) -{ -} - - -Shell::~Shell() -{ - Close(); -} - - -status_t -Shell::Open(int row, int col, const char *command, const char *coding) -{ - fFd = spawn_shell(row, col, command, coding); - if (fFd < 0) - return fFd; + fFd = master; return B_OK; } - -void -Shell::Close() -{ - if (fFd >= 0) { - close_shell(fFd); - fFd = -1; - } -} - - -const char * -Shell::TTYName() const -{ - return ttyname(fFd); -} - - -ssize_t -Shell::Read(void *buffer, size_t numBytes) -{ - if (fFd < 0) - return B_NO_INIT; - - return read(fFd, buffer, numBytes); -} - - -ssize_t -Shell::Write(const void *buffer, size_t numBytes) -{ - if (fFd < 0) - return B_NO_INIT; - - return write(fFd, buffer, numBytes); -} - - -void -Shell::UpdateWindowSize(int rows, int columns) -{ - struct winsize winSize; - winSize.ws_row = rows; - winSize.ws_col = columns; - ioctl(fFd, TIOCSWINSZ, &winSize); - Signal(SIGWINCH); -} - - -void -Shell::Signal(int signal) -{ - kill(-sShPid, signal); -} - - -status_t -Shell::GetAttr(struct termios &attr) -{ - return tcgetattr(fFd, &attr); -} - - -status_t -Shell::SetAttr(struct termios &attr) -{ - return tcsetattr(fFd, TCSANOW, &attr); -} - - -int -Shell::FD() const -{ - return fFd; -} - diff --git a/src/apps/terminal/Shell.h b/src/apps/terminal/Shell.h index c7f42bc14a..1c11ea59f9 100644 --- a/src/apps/terminal/Shell.h +++ b/src/apps/terminal/Shell.h @@ -103,7 +103,9 @@ public: int FD() const; private: - int fFd; + int fFd; + + status_t _Spawn(int row, int col, const char *command, const char *coding); }; #endif // _SHELL_H diff --git a/src/apps/terminal/TermWindow.cpp b/src/apps/terminal/TermWindow.cpp index 6a4e86bc41..a6d5645e06 100644 --- a/src/apps/terminal/TermWindow.cpp +++ b/src/apps/terminal/TermWindow.cpp @@ -1,4 +1,5 @@ /* + * Copyright 2007 Haiku, Inc. * Copyright (c) 2003-2004 Kian Duffy * Copyright (C) 1998,99 Kazuho Okui and Takashi Murai. * Copyright (c) 2004 Daniel Furrer @@ -10,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -20,7 +22,6 @@ #include #include -#include #include #include #include @@ -33,7 +34,6 @@ #include "PrefWindow.h" #include "PrefView.h" #include "PrefHandler.h" -#include "TermApp.h" #include "TermBaseView.h" #include "TermBuffer.h" #include "TermParse.h" @@ -171,7 +171,7 @@ TermWindow::InitWindow() * TermView is character Terminal view on BaseView. It has paste * on BaseView shift as VIEW_OFFSET. */ - fBaseView = new TermBaseView(textframe, fTermView); + //fBaseView = new TermBaseView(textframe, fTermView); // Initialize TermView. (font, size and color) @@ -186,7 +186,7 @@ TermWindow::InitWindow() MIN_COLS * height, MAX_COLS * height); fTermView->SetTermColor(); - fBaseView->SetViewColor(PrefHandler::Default()->getRGB(PREF_TEXT_BACK_COLOR)); + //fBaseView->SetViewColor(PrefHandler::Default()->getRGB(PREF_TEXT_BACK_COLOR)); // Add offset to baseview. rect.InsetBy(-VIEW_OFFSET, -VIEW_OFFSET); @@ -196,9 +196,9 @@ TermWindow::InitWindow() ResizeTo(rect.Width()+ B_V_SCROLL_BAR_WIDTH, rect.Height() + fMenubar->Bounds().Height()); - fBaseView->ResizeTo(rect.Width(), rect.Height()); - fBaseView->AddChild(fTermView); - fTermView->MoveBy(VIEW_OFFSET, VIEW_OFFSET); + //fBaseView->ResizeTo(rect.Width(), rect.Height()); + //fBaseView->AddChild(fTermView); + //fTermView->MoveBy(VIEW_OFFSET, VIEW_OFFSET); // Make Scroll Bar. @@ -212,7 +212,8 @@ TermWindow::InitWindow() fTermView->SetScrollBar(scrollBar); AddChild(scrollBar); - AddChild(fBaseView); + //AddChild(fBaseView); + AddChild(fTermView); // Set fEditmenu's target to fTermView. (Oh!...) fEditmenu->SetTargetForItems(fTermView); @@ -223,14 +224,6 @@ TermWindow::InitWindow() if (fTermParse->StartThreads() < B_OK) return; - // Set Coding. - - // Init find parameters - fMatchCase = false; - fMatchWord = false; - fFindSelection = false; - fForwardSearch = false; - // Initialize MessageRunner. fWindowUpdate = new BMessageRunner(BMessenger(this), new BMessage (MSGRUN_WINDOW), 500000); @@ -238,7 +231,7 @@ TermWindow::InitWindow() void -TermWindow::MenusBeginning(void) +TermWindow::MenusBeginning() { // Syncronize Encode Menu Pop-up menu and Preference. (fEncodingmenu->FindItem(id2longname(GetEncoding())))->SetMarked(true); @@ -247,7 +240,7 @@ TermWindow::MenusBeginning(void) void -TermWindow::SetupMenu(void) +TermWindow::SetupMenu() { PrefHandler menuText; @@ -561,8 +554,8 @@ TermWindow::MessageReceived(BMessage *message) BScreen screen(this); fTermView->ScrollBar()->Hide(); fMenubar->Hide(); - fBaseView->MoveTo(0,0); - fBaseView->ResizeBy(B_V_SCROLL_BAR_WIDTH, mbHeight); + fTermView->MoveTo(0,0); + fTermView->ResizeBy(B_V_SCROLL_BAR_WIDTH, mbHeight); fSavedLook = Look(); // done before ResizeTo to work around a Dano bug (not erasing the decor) SetLook(B_NO_BORDER_WINDOW_LOOK); @@ -574,8 +567,8 @@ TermWindow::MessageReceived(BMessage *message) fTermView->ScrollBar()->Show(); ResizeTo(fSavedFrame.Width(), fSavedFrame.Height()); MoveTo(fSavedFrame.left, fSavedFrame.top); - fBaseView->ResizeBy(-B_V_SCROLL_BAR_WIDTH, -mbHeight); - fBaseView->MoveTo(0,mbHeight); + fTermView->ResizeBy(-B_V_SCROLL_BAR_WIDTH, -mbHeight); + fTermView->MoveTo(0,mbHeight); SetLook(fSavedLook); fSavedFrame = BRect(0,0,-1,-1); } @@ -587,9 +580,9 @@ TermWindow::MessageReceived(BMessage *message) break; } case MSG_COLOR_CHANGED: { - fBaseView->SetViewColor (PrefHandler::Default()->getRGB (PREF_TEXT_BACK_COLOR)); - fTermView->SetTermColor (); - fBaseView->Invalidate(); + //fBaseView->SetViewColor (PrefHandler::Default()->getRGB (PREF_TEXT_BACK_COLOR)); + fTermView->SetTermColor(); + //fBaseView->Invalidate(); fTermView->Invalidate(); break; } diff --git a/src/apps/terminal/TermWindow.h b/src/apps/terminal/TermWindow.h index 108a4dee1f..05f2b0aedc 100644 --- a/src/apps/terminal/TermWindow.h +++ b/src/apps/terminal/TermWindow.h @@ -28,72 +28,74 @@ * THE SOFTWARE. * */ -#ifndef TERMWIN_H -#define TERMWIN_H +#ifndef __TERMWINDOW_H +#define __TERMWINDOW_H - -#include -#include -#include #include +#include -class BFont; -class TermView; -class TermParse; +class BMenu; +class BMenuBar; +class BMessageRunner; class CodeConv; -class PrefWindow; class FindWindow; +class PrefWindow; class Shell; +class TermParse; +class TermView; class TermWindow : public BWindow { - public: - TermWindow(BRect frame, const char* title, const char *command); - virtual ~TermWindow(); +public: + TermWindow(BRect frame, const char* title, const char *command); + virtual ~TermWindow(); - void TermWinActivate(); + void TermWinActivate(); - protected: - virtual void MessageReceived(BMessage *message); - virtual void WindowActivated(bool); - virtual void MenusBeginning(void); - virtual bool QuitRequested(); +protected: + virtual void MessageReceived(BMessage *message); + virtual void WindowActivated(bool); + virtual void MenusBeginning(); + virtual bool QuitRequested(); - status_t GetSupportedSuites(BMessage *msg); - BHandler* ResolveSpecifier(BMessage *msg, int32 index, - BMessage *specifier, int32 form, - const char *property); + status_t GetSupportedSuites(BMessage *msg); + BHandler* ResolveSpecifier(BMessage *msg, int32 index, + BMessage *specifier, int32 form, + const char *property); - private: - void InitWindow(); - void SetupMenu(); - status_t DoPageSetup(); - void DoPrint(); +private: + void InitWindow(); + void SetupMenu(); + status_t DoPageSetup(); + void DoPrint(); - /* - * data member - */ - Shell *fShell; - TermParse *fTermParse; - BMenuBar *fMenubar; - BMenu *fFilemenu, *fEditmenu, *fEncodingmenu, *fHelpmenu, *fFontMenu, *fWindowSizeMenu, *fNewFontMenu; - TermView *fTermView; - BView *fBaseView; - CodeConv *fCodeConv; - BMessage *fPrintSettings; - PrefWindow *fPrefWindow; - FindWindow *fFindPanel; - BMessageRunner *fWindowUpdate; - - BRect fSavedFrame; - window_look fSavedLook; - //Saved search parameters - BString fFindString; - BMenuItem *fFindForwardMenuItem; - BMenuItem *fFindBackwardMenuItem; - bool fFindSelection; - bool fForwardSearch; - bool fMatchCase; - bool fMatchWord; + Shell *fShell; + TermParse *fTermParse; + BMenuBar *fMenubar; + BMenu *fFilemenu, + *fEditmenu, + *fEncodingmenu, + *fHelpmenu, + *fFontMenu, + *fWindowSizeMenu, + *fNewFontMenu; + TermView *fTermView; + BView *fBaseView; + CodeConv *fCodeConv; + BMessage *fPrintSettings; + PrefWindow *fPrefWindow; + FindWindow *fFindPanel; + BMessageRunner *fWindowUpdate; + BRect fSavedFrame; + window_look fSavedLook; + + //Saved search parameters + BString fFindString; + BMenuItem *fFindForwardMenuItem; + BMenuItem *fFindBackwardMenuItem; + bool fFindSelection; + bool fForwardSearch; + bool fMatchCase; + bool fMatchWord; }; -#endif // TERMWIN_H +#endif // __TERMWINDOW_H