sclang: fix readline prompt display
[supercollider.git] / include / lang / SC_TerminalClient.h
blobc61c771db3466a6fea2d42965dad0f12131e529c
1 /* -*- c++ -*-
2 Commandline interpreter interface.
3 Copyright (c) 2003 2004 stefan kersten.
5 ====================================================================
7 SuperCollider real time audio synthesis system
8 Copyright (c) 2002 James McCartney. All rights reserved.
9 http://www.audiosynth.com
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #ifndef SC_TERMINALCLIENT_H_INCLUDED
27 #define SC_TERMINALCLIENT_H_INCLUDED
29 #include "SC_LanguageClient.h"
30 #include "SC_StringBuffer.h"
32 // =====================================================================
33 // SC_TerminalClient - command line sclang client.
34 // =====================================================================
36 class SC_DLLEXPORT SC_TerminalClient : public SC_LanguageClient
38 public:
39 enum
41 kInterpretCmdLine = 0x1b,
42 kInterpretPrintCmdLine = 0x0c
45 struct Options : public SC_LanguageClient::Options
47 Options()
48 : mLibraryConfigFile(0),
49 mDaemon(false),
50 mCallRun(false),
51 mCallStop(false),
52 mArgc(0), mArgv(0)
53 { }
55 char* mLibraryConfigFile;
56 bool mDaemon;
57 bool mCallRun;
58 bool mCallStop;
59 int mArgc;
60 char** mArgv;
63 SC_TerminalClient(const char* name);
64 virtual ~SC_TerminalClient();
66 const Options& options() const { return mOptions; }
67 bool shouldBeRunning() const { return mShouldBeRunning; }
69 int run(int argc, char** argv);
70 void quit(int code);
72 virtual void postText(const char* str, size_t len);
73 virtual void postFlush(const char* str, size_t len);
74 virtual void postError(const char* str, size_t len);
75 virtual void flush();
77 protected:
78 bool parseOptions(int& argc, char**& argv, Options& opt);
79 void printUsage();
81 void interpretCmdLine(PyrSymbol* method, SC_StringBuffer& cmdLine);
82 void interpretCmdLine(PyrSymbol* method, const char* cmdLine);
83 void interpretLocked(PyrSymbol* method, const char *buf, size_t size);
85 void lockInput() { pthread_mutex_lock(&mInputMutex); }
86 void unlockInput() { pthread_mutex_unlock(&mInputMutex); }
88 // --------------------------------------------------------------
90 // NOTE: Subclasses should call when signalled:
91 // Must be called with input locked:
92 void interpretInput();
93 // Must be called with interpreter locked:
94 // SC_LanguageClinet::tickLocked()
96 // --------------------------------------------------------------
98 // NOTE: Subclasses should respond to following signals:
100 // Called from input thread with input locked.
101 // Calls for interpretInput() on main thread:
102 virtual void onInput();
104 // The following are called from language,
105 // locked, and may be called from any thread:
107 // Calls for tickLocked() and adjusting timing according to new schedule
108 virtual void onScheduleChanged();
109 // Language requested the application to quit
110 virtual void onQuit( int exitCode );
111 // See super class
112 virtual void onLibraryStartup();
114 // --------------------------------------------------------------
116 // NOTE: Subclasses should override:
117 virtual void commandLoop();
118 virtual void daemonLoop();
120 // --------------------------------------------------------------
122 static int prArgv(struct VMGlobals* g, int);
123 static int prExit(struct VMGlobals* g, int);
124 static int prScheduleChanged( struct VMGlobals *, int);
126 private:
127 // NOTE: called from input thread:
128 #ifdef HAVE_READLINE
129 static void readlineCb( char *cmdLine );
130 static void *readlineFunc( void * );
131 #endif
132 static void *pipeFunc( void * );
133 void pushCmdLine( SC_StringBuffer &buf, const char *newData, size_t size );
135 void initCmdLine();
136 void startCmdLine();
137 void endCmdLine();
138 void cleanupCmdLine();
140 // helpers
141 void lockSignal() { pthread_mutex_lock(&mSignalMutex); }
142 void unlockSignal() { pthread_mutex_unlock(&mSignalMutex); }
144 bool mShouldBeRunning;
145 int mReturnCode;
146 Options mOptions;
148 bool mUseReadline;
149 SC_StringBuffer mCmdLine;
150 #ifndef _WIN32
151 int mCmdPipe[2];
152 #else
153 HANDLE mQuitInputEvent;
154 #endif
156 pthread_t mInputThread;
157 pthread_mutex_t mInputMutex;
158 pthread_mutex_t mSignalMutex;
159 pthread_cond_t mCond;
160 pthread_cond_t mInputReadCond;
162 bool mInput;
163 bool mSched;
166 #endif // SC_TERMINALCLIENT_H_INCLUDED