source: mainline/uspace/app/nterm/nterm.c

Last change on this file was 87822ce, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Avoid infinite loop when console communication is broken

Need to make sure callers of console_get_event_timeout() can distinguish
between timeout and I/O error. Fix all callers of console_get_event()
and console_get_event_timeout() not to enter infinite loop when console
connection is broken. Also avoid setting of errno variable.

  • Property mode set to 100644
File size: 3.2 KB
Line 
1/*
2 * Copyright (c) 2012 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup nterm
30 * @{
31 */
32/** @file Network serial terminal emulator.
33 */
34
35#include <stdbool.h>
36#include <errno.h>
37#include <io/console.h>
38#include <stdio.h>
39#include <str.h>
40
41#include "conn.h"
42#include "nterm.h"
43
44#define NAME "nterm"
45
46static console_ctrl_t *con;
47static bool done;
48
49static void key_handle_ctrl(kbd_event_t *ev)
50{
51 switch (ev->key) {
52 case KC_Q:
53 done = true;
54 break;
55 default:
56 break;
57 }
58}
59
60static void send_char(char32_t c)
61{
62 char cbuf[STR_BOUNDS(1)];
63 size_t offs;
64 errno_t rc;
65
66 offs = 0;
67 chr_encode(c, cbuf, &offs, STR_BOUNDS(1));
68
69 rc = conn_send(cbuf, offs);
70 if (rc != EOK) {
71 printf("[Failed sending data]\n");
72 }
73}
74
75static void key_handle_unmod(kbd_event_t *ev)
76{
77 switch (ev->key) {
78 case KC_ENTER:
79 send_char('\n');
80 break;
81 default:
82 if (ev->c >= 32 || ev->c == '\t' || ev->c == '\b') {
83 send_char(ev->c);
84 }
85 }
86}
87
88static void key_handle(kbd_event_t *ev)
89{
90 if ((ev->mods & KM_ALT) == 0 &&
91 (ev->mods & KM_SHIFT) == 0 &&
92 (ev->mods & KM_CTRL) != 0) {
93 key_handle_ctrl(ev);
94 } else if ((ev->mods & (KM_CTRL | KM_ALT)) == 0) {
95 key_handle_unmod(ev);
96 }
97}
98
99void nterm_received(void *data, size_t size)
100{
101 fwrite(data, size, 1, stdout);
102 fflush(stdout);
103}
104
105static void print_syntax(void)
106{
107 printf("syntax: nterm <host>:<port>\n");
108}
109
110int main(int argc, char *argv[])
111{
112 cons_event_t ev;
113 errno_t rc;
114
115 if (argc != 2) {
116 print_syntax();
117 return 1;
118 }
119
120 rc = conn_open(argv[1]);
121 if (rc != EOK) {
122 printf("Error connecting.\n");
123 return 1;
124 }
125
126 printf("Connection established.\n");
127
128 con = console_init(stdin, stdout);
129
130 done = false;
131 while (!done) {
132 rc = console_get_event(con, &ev);
133 if (rc != EOK)
134 break;
135
136 if (ev.type == CEV_KEY && ev.ev.key.type == KEY_PRESS)
137 key_handle(&ev.ev.key);
138 }
139
140 return 0;
141}
142
143/** @}
144 */
Note: See TracBrowser for help on using the repository browser.