xref: /haiku/src/apps/terminal/TermParse.h (revision 4466b89c65970de4c7236ac87faa2bee4589f413)
1 /*
2  * Copyright (c) 2003-4 Kian Duffy <myob@users.sourceforge.net>
3  * Parts Copyright (C) 1998,99 Kazuho Okui and Takashi Murai.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files or portions
7  * thereof (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so, subject
11  * to the following conditions:
12  *
13  *  * Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  *
16  *  * Redistributions in binary form must reproduce the above copyright notice
17  *    in the  binary, as well as this list of conditions and the following
18  *    disclaimer in the documentation and/or other materials provided with
19  *    the distribution.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27  * THE SOFTWARE.
28  *
29  */
30 #ifndef TERMPARSE_H
31 #define TERMPARSE_H
32 
33 
34 #include "TermConst.h"
35 
36 #include <Handler.h>
37 #include <OS.h>
38 
39 
40 #define READ_BUF_SIZE 2048
41 	// pty read buffer size
42 #define MIN_PTY_BUFFER_SPACE	16
43 	// minimal space left before the reader tries to read more
44 #define ESC_PARSER_BUFFER_SIZE	64
45 	// size of the parser buffer
46 
47 
48 class TerminalBuffer;
49 
50 class TermParse : public BHandler {
51 public:
52 	TermParse(int fd);
53 	~TermParse();
54 
55 	status_t StartThreads(TerminalBuffer *view);
56 	status_t StopThreads();
57 
58 private:
59 	inline uchar _NextParseChar();
60 
61 	// Initialize TermParse and PtyReader thread.
62 	status_t _InitTermParse();
63 	status_t _InitPtyReader();
64 
65 	void _StopTermParse();
66 	void _StopPtyReader();
67 
68 	int32 EscParse();
69 	int32 PtyReader();
70 
71 	void DumpState(int *groundtable, int *parsestate, uchar c);
72 
73 	static int32 _ptyreader_thread(void *);
74 	static int32 _escparse_thread(void *);
75 
76 	status_t _ReadParserBuffer();
77 
78 	void _DeviceStatusReport(int n);
79 	void _DecReqTermParms(int value);
80 	void _DecPrivateModeSet(int value);
81 	void _DecPrivateModeReset(int value);
82 	void _DecSaveCursor();
83 	void _DecRestoreCursor();
84 
85 	int fFd;
86 
87 	uint32 fAttr;
88 	uint32 fSavedAttr;
89 
90 	thread_id fParseThread;
91 	thread_id fReaderThread;
92 	sem_id fReaderSem;
93 	sem_id fReaderLocker;
94 
95 	uint fBufferPosition;
96 	uchar fReadBuffer[READ_BUF_SIZE];
97 	vint32 fReadBufferSize;
98 
99 	uchar fParserBuffer[ESC_PARSER_BUFFER_SIZE];
100 	int32 fParserBufferSize;
101 	int32 fParserBufferOffset;
102 
103 	TerminalBuffer *fBuffer;
104 
105 	bool fQuitting;
106 };
107 
108 #endif // TERMPARSE_H
109