compositor: fix the over operator
[helenos.git] / uspace / app / netecho / netecho.c
blob018d3f9bea6e83f68f6140b8e75b4fcbb33401e0
1 /*
2 * Copyright (c) 2016 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup netecho
30 * @{
32 /** @file Network UDP echo diagnostic utility.
35 #include <stdbool.h>
36 #include <errno.h>
37 #include <io/console.h>
38 #include <stdio.h>
39 #include <str.h>
41 #include "comm.h"
42 #include "netecho.h"
44 #define NAME "netecho"
46 static console_ctrl_t *con;
47 static bool done;
49 void netecho_received(void *data, size_t size)
51 char *p;
52 size_t i;
54 printf("Received message '");
55 p = data;
57 for (i = 0; i < size; i++)
58 putchar(p[i]);
59 printf("'.\n");
62 static void key_handle_ctrl(kbd_event_t *ev)
64 switch (ev->key) {
65 case KC_Q:
66 done = true;
67 break;
68 default:
69 break;
73 static void send_char(wchar_t c)
75 char cbuf[STR_BOUNDS(1)];
76 size_t offs;
77 errno_t rc;
79 offs = 0;
80 chr_encode(c, cbuf, &offs, STR_BOUNDS(1));
82 rc = comm_send(cbuf, offs);
83 if (rc != EOK) {
84 printf("[Failed sending data]\n");
88 static void key_handle_unmod(kbd_event_t *ev)
90 switch (ev->key) {
91 case KC_ENTER:
92 send_char('\n');
93 break;
94 default:
95 if (ev->c >= 32 || ev->c == '\t' || ev->c == '\b') {
96 send_char(ev->c);
101 static void key_handle(kbd_event_t *ev)
103 if ((ev->mods & KM_ALT) == 0 &&
104 (ev->mods & KM_SHIFT) == 0 &&
105 (ev->mods & KM_CTRL) != 0) {
106 key_handle_ctrl(ev);
107 } else if ((ev->mods & (KM_CTRL | KM_ALT)) == 0) {
108 key_handle_unmod(ev);
112 static void print_syntax(void)
114 printf("syntax:\n");
115 printf("\t%s -l <port>\n", NAME);
116 printf("\t%s -d <host>:<port> [<message> [<message...>]]\n", NAME);
119 /* Interactive mode */
120 static void netecho_interact(void)
122 cons_event_t ev;
124 printf("Communication started. Press Ctrl-Q to quit.\n");
126 con = console_init(stdin, stdout);
128 done = false;
129 while (!done) {
130 console_get_event(con, &ev);
131 if (ev.type == CEV_KEY && ev.ev.key.type == KEY_PRESS)
132 key_handle(&ev.ev.key);
136 static void netecho_send_messages(char **msgs)
138 errno_t rc;
140 while (*msgs != NULL) {
141 rc = comm_send(*msgs, str_size(*msgs));
142 if (rc != EOK) {
143 printf("[Failed sending data]\n");
146 ++msgs;
150 int main(int argc, char *argv[])
152 char *hostport;
153 char *port;
154 char **msgs;
155 errno_t rc;
157 if (argc < 2) {
158 print_syntax();
159 return 1;
162 if (str_cmp(argv[1], "-l") == 0) {
163 if (argc != 3) {
164 print_syntax();
165 return 1;
168 port = argv[2];
169 msgs = NULL;
171 rc = comm_open_listen(port);
172 if (rc != EOK) {
173 printf("Error setting up communication.\n");
174 return 1;
176 } else if (str_cmp(argv[1], "-d") == 0) {
177 if (argc < 3) {
178 print_syntax();
179 return 1;
182 hostport = argv[2];
183 port = NULL;
184 msgs = argv + 3;
186 rc = comm_open_talkto(hostport);
187 if (rc != EOK) {
188 printf("Error setting up communication.\n");
189 return 1;
191 } else {
192 print_syntax();
193 return 1;
196 if (msgs != NULL && *msgs != NULL) {
197 /* Just send messages and quit */
198 netecho_send_messages(msgs);
199 } else {
200 /* Interactive mode */
201 netecho_interact();
204 comm_close();
205 return 0;
208 /** @}