-
Notifications
You must be signed in to change notification settings - Fork 810
/
smallchat-client.c
261 lines (227 loc) · 8.98 KB
/
smallchat-client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* smallchat-client.c -- Client program for smallchat-server.
*
* Copyright (c) 2023, Salvatore Sanfilippo <antirez at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the project name of nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/select.h>
#include <unistd.h>
#include <termios.h>
#include <errno.h>
#include "chatlib.h"
/* ============================================================================
* Low level terminal handling.
* ========================================================================== */
void disableRawModeAtExit(void);
/* Raw mode: 1960 magic shit. */
int setRawMode(int fd, int enable) {
/* We have a bit of global state (but local in scope) here.
* This is needed to correctly set/undo raw mode. */
static struct termios orig_termios; // Save original terminal status here.
static int atexit_registered = 0; // Avoid registering atexit() many times.
static int rawmode_is_set = 0; // True if raw mode was enabled.
struct termios raw;
/* If enable is zero, we just have to disable raw mode if it is
* currently set. */
if (enable == 0) {
/* Don't even check the return value as it's too late. */
if (rawmode_is_set && tcsetattr(fd,TCSAFLUSH,&orig_termios) != -1)
rawmode_is_set = 0;
return 0;
}
/* Enable raw mode. */
if (!isatty(fd)) goto fatal;
if (!atexit_registered) {
atexit(disableRawModeAtExit);
atexit_registered = 1;
}
if (tcgetattr(fd,&orig_termios) == -1) goto fatal;
raw = orig_termios; /* modify the original mode */
/* input modes: no break, no CR to NL, no parity check, no strip char,
* no start/stop output control. */
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
/* output modes - do nothing. We want post processing enabled so that
* \n will be automatically translated to \r\n. */
// raw.c_oflag &= ...
/* control modes - set 8 bit chars */
raw.c_cflag |= (CS8);
/* local modes - choing off, canonical off, no extended functions,
* but take signal chars (^Z,^C) enabled. */
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
/* control chars - set return condition: min number of bytes and timer.
* We want read to return every single byte, without timeout. */
raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0; /* 1 byte, no timer */
/* put terminal in raw mode after flushing */
if (tcsetattr(fd,TCSAFLUSH,&raw) < 0) goto fatal;
rawmode_is_set = 1;
return 0;
fatal:
errno = ENOTTY;
return -1;
}
/* At exit we'll try to fix the terminal to the initial conditions. */
void disableRawModeAtExit(void) {
setRawMode(STDIN_FILENO,0);
}
/* ============================================================================
* Mininal line editing.
* ========================================================================== */
void terminalCleanCurrentLine(void) {
write(fileno(stdout),"\e[2K",4);
}
void terminalCursorAtLineStart(void) {
write(fileno(stdout),"\r",1);
}
#define IB_MAX 128
struct InputBuffer {
char buf[IB_MAX]; // Buffer holding the data.
int len; // Current length.
};
/* inputBuffer*() return values: */
#define IB_ERR 0 // Sorry, unable to comply.
#define IB_OK 1 // Ok, got the new char, did the operation, ...
#define IB_GOTLINE 2 // Hey, now there is a well formed line to read.
/* Append the specified character to the buffer. */
int inputBufferAppend(struct InputBuffer *ib, int c) {
if (ib->len >= IB_MAX) return IB_ERR; // No room.
ib->buf[ib->len] = c;
ib->len++;
return IB_OK;
}
void inputBufferHide(struct InputBuffer *ib);
void inputBufferShow(struct InputBuffer *ib);
/* Process every new keystroke arriving from the keyboard. As a side effect
* the input buffer state is modified in order to reflect the current line
* the user is typing, so that reading the input buffer 'buf' for 'len'
* bytes will contain it. */
int inputBufferFeedChar(struct InputBuffer *ib, int c) {
switch(c) {
case '\n':
break; // Ignored. We handle \r instead.
case '\r':
return IB_GOTLINE;
case 127: // Backspace.
if (ib->len > 0) {
ib->len--;
inputBufferHide(ib);
inputBufferShow(ib);
}
break;
default:
if (inputBufferAppend(ib,c) == IB_OK)
write(fileno(stdout),ib->buf+ib->len-1,1);
break;
}
return IB_OK;
}
/* Hide the line the user is typing. */
void inputBufferHide(struct InputBuffer *ib) {
(void)ib; // Not used var, but is conceptually part of the API.
terminalCleanCurrentLine();
terminalCursorAtLineStart();
}
/* Show again the current line. Usually called after InputBufferHide(). */
void inputBufferShow(struct InputBuffer *ib) {
write(fileno(stdout),ib->buf,ib->len);
}
/* Reset the buffer to be empty. */
void inputBufferClear(struct InputBuffer *ib) {
ib->len = 0;
inputBufferHide(ib);
}
/* =============================================================================
* Main program logic, finally :)
* ========================================================================== */
int main(int argc, char **argv) {
if (argc != 3) {
printf("Usage: %s <host> <port>\n", argv[0]);
exit(1);
}
/* Create a TCP connection with the server. */
int s = TCPConnect(argv[1],atoi(argv[2]),0);
if (s == -1) {
perror("Connecting to server");
exit(1);
}
/* Put the terminal in raw mode: this way we will receive every
* single key stroke as soon as the user types it. No buffering
* nor translation of escape sequences of any kind. */
setRawMode(fileno(stdin),1);
/* Wait for the standard input or the server socket to
* have some data. */
fd_set readfds;
int stdin_fd = fileno(stdin);
struct InputBuffer ib;
inputBufferClear(&ib);
while(1) {
FD_ZERO(&readfds);
FD_SET(s, &readfds);
FD_SET(stdin_fd, &readfds);
int maxfd = s > stdin_fd ? s : stdin_fd;
int num_events = select(maxfd+1, &readfds, NULL, NULL, NULL);
if (num_events == -1) {
perror("select() error");
exit(1);
} else if (num_events) {
char buf[128]; /* Generic buffer for both code paths. */
if (FD_ISSET(s, &readfds)) {
/* Data from the server? */
ssize_t count = read(s,buf,sizeof(buf));
if (count <= 0) {
printf("Connection lost\n");
exit(1);
}
inputBufferHide(&ib);
write(fileno(stdout),buf,count);
inputBufferShow(&ib);
} else if (FD_ISSET(stdin_fd, &readfds)) {
/* Data from the user typing on the terminal? */
ssize_t count = read(stdin_fd,buf,sizeof(buf));
for (int j = 0; j < count; j++) {
int res = inputBufferFeedChar(&ib,buf[j]);
switch(res) {
case IB_GOTLINE:
inputBufferAppend(&ib,'\n');
inputBufferHide(&ib);
write(fileno(stdout),"you> ", 5);
write(fileno(stdout),ib.buf,ib.len);
write(s,ib.buf,ib.len);
inputBufferClear(&ib);
break;
case IB_OK:
break;
}
}
}
}
}
close(s);
return 0;
}