annotate nnchat.c @ 58:5455d078bedd

Misc. fixes.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 11 Nov 2008 21:29:37 +0200
parents e93532b9dca1
children d57a8acf92bf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
41
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
1 /*
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
2 * NNChat - Custom chat client for NewbieNudes.com chatrooms
39
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
3 * Written by Matti 'ccr' Hämäläinen
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
4 * (C) Copyright 2008 Tecnic Software productions (TNSP)
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
5 */
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 #include <sys/socket.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 #include <sys/types.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include <arpa/inet.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include <sys/time.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include <netdb.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #include <unistd.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 #include <stdlib.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 #include <stdio.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 #include "th_args.h"
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 #include "th_string.h"
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 #include <string.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 #include <errno.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 #include <time.h>
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
20 #include <ncurses.h>
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21
49
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
22
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
23 #define SET_MAX_BACKBUF (1024)
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
24 #define SET_MAX_HISTORY (16)
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
25 #define SET_BUFSIZE (4096)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 #define SET_ALLOC_SIZE (128)
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
27 #define SET_DELAY (15)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
28 #define SET_DELAY_USEC (SET_DELAY * 1000)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29
49
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
30
58
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
31 typedef struct {
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
32 char c;
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
33 char *ent;
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
34 } html_entity_t;
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
35
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
36
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
37 html_entity_t HTMLEntities[] = {
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
38 { '<', "&lt;" },
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
39 { '>', "&gt;" },
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
40 };
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
41
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
42 const int numHTMLEntities = (sizeof(HTMLEntities) / sizeof(HTMLEntities[0]));
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
43
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
44
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 /* Options
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 */
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
47 int optPort = 8005;
39
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
48 int optUserColor = 0x006080;
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
49 char *optServer = "www11.servemedata.com",
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
50 *optUserName = NULL,
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
51 *optUserName2 = NULL,
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
52 *optPassword = NULL,
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
53 *optLogFilename = NULL,
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
54 *setTarget = NULL,
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
55 *optSite = "NN";
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
56 BOOL optDaemon = FALSE;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 FILE *optLogFile = NULL;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
58 WINDOW *mainWin = NULL,
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
59 *statusWin = NULL,
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
60 *editWin = NULL;
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
61 BOOL setPrvMode = FALSE,
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
62 setMorseMode = FALSE;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
64
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 /* Arguments
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 optarg_t optList[] = {
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
68 { 0, '?', "help", "Show this help", OPT_NONE },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
69 { 1, 'v', "verbose", "Be more verbose", OPT_NONE },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
70 { 2, 'p', "port", "Connect to port", OPT_ARGREQ },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
71 { 3, 's', "server", "Server to connect to", OPT_ARGREQ },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
72 { 4, 'C', "color", "Initial color in RGB hex 000000", OPT_ARGREQ },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
73 { 5, 'l', "logfile", "Log filename", OPT_ARGREQ },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
74 { 6, 'D', "daemon", "A pseudo-daemon mode for logging", OPT_NONE },
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
75 { 7, 'S', "site", "Site (default: NN)", OPT_ARGREQ },
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 };
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
78 const int optListN = (sizeof(optList) / sizeof(optList[0]));
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 void argShowHelp()
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 th_args_help(stdout, optList, optListN, th_prog_name,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 "[options] <username> <password>");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
88 int getColor(char *str)
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
89 {
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
90 char *p = str;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
91 int len, val = 0;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
92
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
93 for (len = 0; *p && len < 6; p++, len++) {
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
94 if (*p >= '0' && *p <= '9') {
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
95 val *= 16; val += (*p - '0');
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
96 } else if (*p >= 'A' && *p <= 'F') {
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
97 val *= 16; val += (*p - 'A') + 10;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
98 } else if (*p >= 'a' && *p <= 'f') {
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
99 val *= 16; val += (*p - 'a') + 10;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
100 } else
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
101 return -1;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
102 }
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
103
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
104 return (len == 6) ? val : -1;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
105 }
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
106
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 switch (optN) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 case 0:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 argShowHelp();
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 exit(0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 case 1:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 th_verbosityLevel++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 case 2:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 optPort = atoi(optArg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 case 3:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 optServer = optArg;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 case 4:
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
128 if ((optUserColor = getColor(optArg)) < 0) {
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 THERR("Invalid color argument '%s', should be a RGB hex triplet '000000'.\n",
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 optArg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 THMSG(1, "Using color #%06x\n", optUserColor);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 case 5:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 optLogFilename = optArg;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
140 case 7:
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
141 optSite = optArg;
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
142 break;
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
143
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
144 case 6:
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
145 optDaemon = TRUE;
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
146 THMSG(1, "Running in pseudo-daemon mode.\n");
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
147 break;
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
148
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 default:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 THERR("Unknown option '%s'.\n", currArg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 BOOL argHandleFile(char *currArg)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 if (!optUserName)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 optUserName = currArg;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 else if (!optPassword)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 optPassword = currArg;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 THERR("Username '%s' already specified on commandline!\n", optUserName);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171
49
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
172
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
173 typedef struct {
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
174 char **data;
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
175 size_t n, size;
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
176 } ringbuf_t;
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
177
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
178
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
179 ringbuf_t * newRingBuf(const size_t size)
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
180 {
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
181 ringbuf_t *res = th_calloc(1, sizeof(ringbuf_t));
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
182
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
183 res->data = (char **) th_malloc(size * sizeof(char *));
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
184 res->size = size;
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
185 res->n = 0;
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
186
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
187 return res;
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
188 }
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
189
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
190
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
191 void freeRingBuf(ringbuf_t *buf)
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
192 {
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
193 size_t i;
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
194
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
195 for (i = 0; i < buf->n; i++)
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
196 th_free(buf->data[i]);
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
197
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
198 th_free(buf->data);
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
199 th_free(buf);
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
200 }
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
201
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
202
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
203 void addRingBuf(ringbuf_t *buf, const char *str)
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
204 {
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
205 if (buf->n < buf->size) {
50
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
206 buf->data[buf->n] = strdup(str);
49
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
207 buf->n++;
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
208 } else {
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
209 th_free(buf->data[0]);
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
210 memmove(&(buf->data[0]), &(buf->data[1]), buf->size - 1);
50
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
211 buf->data[buf->size - 1] = strdup(str);
49
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
212 }
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
213 }
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
214
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
215
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
216 typedef struct {
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
217 ssize_t pos, len, size;
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
218 char *data;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
219 } editbuf_t;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
220
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
221
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
222 int writeBuf(editbuf_t *buf, ssize_t pos, int ch)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
223 {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
224 /* Check arguments */
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
225 if (buf->len+1 >= buf->size) return -3;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
226
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
227 if (pos < 0)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
228 return -1;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
229 else if (pos >= buf->len) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
230 buf->data[buf->len++] = ch;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
231 } else {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
232 buf->data[pos] = ch;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
233 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
234 return 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
235 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
236
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
237 int insertBuf(editbuf_t *buf, ssize_t pos, int ch)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
238 {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
239 /* Check arguments */
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
240 if (buf->len+1 >= buf->size) return -3;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
241
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
242 if (pos < 0)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
243 return -1;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
244 else if (pos >= buf->len) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
245 buf->data[buf->len] = ch;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
246 } else {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
247 memmove(&(buf->data[pos+1]), &(buf->data[pos]), buf->len - pos + 1);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
248 buf->data[pos] = ch;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
249 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
250 buf->len++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
251 return 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
252 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
253
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
254 int deleteBuf(editbuf_t *buf, ssize_t pos)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
255 {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
256 /* Check arguments */
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
257 if (pos < 0)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
258 return -1;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
259 else if (pos < buf->len) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
260 memmove(&(buf->data[pos]), &(buf->data[pos+1]), buf->len - pos);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
261 buf->len--;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
262 return 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
263 } else
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
264 return -2;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
265 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
266
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
267 void clearBuf(editbuf_t *buf)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
268 {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
269 buf->len = 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
270 buf->pos = 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
271 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
272
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
273 editbuf_t * newBuf(ssize_t n)
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
274 {
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
275 editbuf_t *res = th_calloc(1, sizeof(editbuf_t));
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
276
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
277 res->data = (char *) th_malloc(n);
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
278 res->size = n;
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
279
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
280 return res;
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
281 }
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
282
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
283 void freeBuf(editbuf_t *buf)
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
284 {
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
285 if (buf) {
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
286 th_free(buf->data);
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
287 th_free(buf);
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
288 }
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
289 }
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
290
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
291 editbuf_t * copyBuf(editbuf_t *src)
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
292 {
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
293 editbuf_t *res;
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
294
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
295 assert(src != NULL);
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
296
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
297 if (src == NULL) return NULL;
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
298
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
299 if ((res = newBuf(src->size)) == NULL)
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
300 return NULL;
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
301
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
302 memcpy(res->data, src->data, src->size);
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
303 res->pos = res->len = src->len;
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
304
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
305 return res;
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
306 }
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
307
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
308 void setBufPos(editbuf_t *buf, ssize_t pos)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
309 {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
310 /* Check arguments */
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
311 if (pos < 0)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
312 buf->pos = 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
313 else if (pos >= buf->len)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
314 buf->pos = buf->len;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
315 else
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
316 buf->pos = pos;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
317 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
318
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
319 void updateStatus(BOOL insertMode)
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
320 {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
321 char tmpStr[128] = "";
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
322 time_t timeStamp;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
323 struct tm *tmpTime;;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
324
32
67ec4073e38c Check for NULL pointers (fixes "daemon" mode)
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
325 if (statusWin == NULL) return;
67ec4073e38c Check for NULL pointers (fixes "daemon" mode)
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
326
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
327 timeStamp = time(NULL);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
328 if ((tmpTime = localtime(&timeStamp)) != NULL) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
329 strftime(tmpStr, sizeof(tmpStr), "%H:%M:%S", tmpTime);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
330 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
331
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
332 wbkgdset(statusWin, 0x0d00);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
333 werase(statusWin);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
334
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
335 wattrset(statusWin, A_BOLD);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
336 mvwaddstr(statusWin, 0, 1, tmpStr);
29
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
337
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
338 waddstr(statusWin, " | ");
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
339 wattrset(statusWin, A_BOLD | COLOR_PAIR(16));
29
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
340 waddstr(statusWin, optUserName);
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
341 wattrset(statusWin, A_BOLD | COLOR_PAIR(13));
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
342
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
343 waddstr(statusWin, " | ");
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
344 wattrset(statusWin, A_BOLD | COLOR_PAIR(11));
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
345 waddstr(statusWin, insertMode ? "INS" : "DEL");
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
346
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
347 wattrset(statusWin, A_BOLD | COLOR_PAIR(13));
29
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
348 waddstr(statusWin, " | Prv: ");
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
349
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
350 wattrset(statusWin, A_BOLD | COLOR_PAIR(11));
29
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
351 waddstr(statusWin, setTarget != NULL ? setTarget : "-");
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
352
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
353 wattrset(statusWin, A_BOLD | COLOR_PAIR(13));
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
354 waddstr(statusWin, " | P/C: ");
29
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
355 wattrset(statusWin, A_BOLD | COLOR_PAIR(11));
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
356 snprintf(tmpStr, sizeof(tmpStr), "%d / #%06x", optPort, optUserColor);
29
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
357 waddstr(statusWin, tmpStr);
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
358
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
359 wrefresh(statusWin);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
360 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
361
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
362 void printEditBuf(char *str, editbuf_t *buf)
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
363 {
32
67ec4073e38c Check for NULL pointers (fixes "daemon" mode)
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
364 if (statusWin == NULL || buf == NULL) return;
67ec4073e38c Check for NULL pointers (fixes "daemon" mode)
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
365
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
366 buf->data[buf->len] = 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
367 werase(editWin);
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
368
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
369 wattrset(editWin, A_BOLD);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
370 mvwaddstr(editWin, 0, 0, str);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
371 waddstr(editWin, "> ");
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
372 wattrset(editWin, A_NORMAL);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
373
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
374 if (buf->pos < buf->len) {
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
375 waddnstr(editWin, buf->data, buf->pos);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
376 wattrset(editWin, A_REVERSE);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
377 waddch(editWin, buf->data[buf->pos]);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
378 wattrset(editWin, A_NORMAL);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
379 waddnstr(editWin, buf->data + buf->pos + 1, buf->len - buf->pos - 1);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
380 } else {
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
381 waddnstr(editWin, buf->data, buf->len);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
382 wattrset(editWin, A_REVERSE);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
383 waddch(editWin, ' ');
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
384 wattrset(editWin, A_NORMAL);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
385 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
386 wrefresh(editWin);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
387 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
388
53
7b98da167a0b Improved error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
389 int openConnection(struct in_addr *addr, const int port)
15
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
390 {
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
391 struct sockaddr_in tmpAddr;
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
392 int sock = -1;
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
393
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
394 tmpAddr.sin_family = AF_INET;
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
395 tmpAddr.sin_port = htons(port);
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
396 tmpAddr.sin_addr = *addr;
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
397
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
398 THMSG(1, "Connecting to %s:%d ...\n",
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
399 inet_ntoa(tmpAddr.sin_addr), port);
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
400
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
401 if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
402 THERR("Could not open socket: %s\n", strerror(errno));
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
403 return -2;
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
404 }
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
405
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
406 THMSG(2, "Using socket %d.\n", sock);
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
407
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
408 if (connect(sock, (struct sockaddr *) &tmpAddr, sizeof(tmpAddr)) == -1) {
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
409 THERR("Could not connect: %s\n", strerror(errno));
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
410 return -5;
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
411 }
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
412
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
413 return sock;
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
414 }
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
415
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
416
53
7b98da167a0b Improved error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
417 void closeConnection(const int sock)
15
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
418 {
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
419 if (sock >= 0) {
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
420 close(sock);
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
421 }
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
422 }
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
423
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
424
53
7b98da167a0b Improved error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
425 BOOL sendToSocket(const int sock, char *buf, const size_t bufLen)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
426 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
427 size_t bufLeft = bufLen;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
428 char *bufPtr = buf;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
429
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
430 while (bufLeft > 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
431 ssize_t bufSent;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
432 bufSent = send(sock, bufPtr, bufLeft, 0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
433 if (bufSent < 0) return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
434 bufLeft -= bufSent;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
435 bufPtr += bufSent;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
436 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
437 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
438 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
439
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
440 int printWin(WINDOW *win, const char *fmt)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
441 {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
442 const char *s = fmt;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
443 int col = 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
444
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
445 while (*s) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
446 if (*s == '½') {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
447 int val = 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
448 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
449 if (*s == '½') {
55
5ec3b7d14715 Add typecasts to fix printing of 8-bit characters.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
450 waddch(win, ((unsigned char) *s) | col);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
451 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
452 } else {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
453 while (*s && isdigit(*s)) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
454 val *= 10;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
455 val += (*s - '0');
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
456 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
457 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
458 if (*s != '½') return -1;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
459 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
460
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
461 if (val < 9) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
462 col = A_DIM | COLOR_PAIR(val);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
463 } else if (val < 30) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
464 col = A_BOLD | COLOR_PAIR(val - 9);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
465 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
466 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
467 } else {
55
5ec3b7d14715 Add typecasts to fix printing of 8-bit characters.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
468 waddch(win, ((unsigned char) *s) | col);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
469 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
470 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
471 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
472 return 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
473 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
474
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
475 int printFile(FILE *outFile, const char *fmt)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
476 {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
477 const char *s = fmt;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
478
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
479 while (*s) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
480 if (*s == '½') {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
481 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
482 if (*s == '½') {
58
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
483 fputc((unsigned char) *s, outFile);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
484 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
485 } else {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
486 while (*s && isdigit(*s)) s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
487 if (*s != '½') return -1;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
488 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
489 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
490 } else {
58
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
491 fputc((unsigned char) *s, outFile);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
492 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
493 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
494 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
495
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
496 return 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
497 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
498
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
499 void printMsg(char *fmt, ...)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
500 {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
501 char tmpStr[128] = "", buf[8192];
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
502 va_list ap;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
503 time_t timeStamp;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 struct tm *tmpTime;;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
506 timeStamp = time(NULL);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
507 if ((tmpTime = localtime(&timeStamp)) != NULL) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
508 strftime(tmpStr, sizeof(tmpStr), "½17½[½11½%H:%M:%S½17½]½0½ ", tmpTime);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
509 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
510
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
511 va_start(ap, fmt);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
512 vsnprintf(buf, sizeof(buf), fmt, ap);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
513 va_end(ap);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
514
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
515 if (optLogFile) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
516 printFile(optLogFile, tmpStr);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
517 printFile(optLogFile, buf);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
518 fflush(optLogFile);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
519 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
520
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
521 if (!optDaemon) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
522 printWin(mainWin, tmpStr);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
523 printWin(mainWin, buf);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
524 wrefresh(mainWin);
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
525 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
526 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
528
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
529 BOOL bufRealloc(char **buf, size_t *size, size_t add)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531 return ((*buf = th_realloc(*buf, *size + add)) != NULL);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
532 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
533
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
534 #define PUSHCHAR(x) bufPushChar(&result, &resSize, &resPos, x)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
535 BOOL bufPushChar(char **buf, size_t *size, size_t *pos, char ch)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
536 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
537 if (*pos >= *size && !bufRealloc(buf, size, SET_ALLOC_SIZE))
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
538 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
539
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
540 (*buf)[*pos] = ch;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
541 (*pos)++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
542 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
543 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
544
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
545 #define PUSHSTR(x) bufPushStr(&result, &resSize, &resPos, x)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
546 BOOL bufPushStr(char **buf, size_t *size, size_t *pos, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
547 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
548 size_t tmpLen;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
549
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
550 if (!str) return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
551 tmpLen = strlen(str);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
552
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
553 if ((*pos + tmpLen) >= *size && !bufRealloc(buf, size, tmpLen + SET_ALLOC_SIZE))
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
554 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
555
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
556 strcpy(*buf + *pos, str);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
557 (*pos) += tmpLen;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
558 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
560
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
562 typedef struct {
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
563 char c;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
564 char *code;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
565 } conv_ent_t;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
566
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
567 conv_ent_t morseTab[] = {
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
568 { ' ', " / " },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
569 { 'A', ".-" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
570 { 'B', "-..." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
571 { 'C', "-.-." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
572 { 'D', "-.." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
573 { 'E', "." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
574 { 'F', "..-." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
575 { 'G', "--." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
576 { 'H', "...." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
577 { 'I', ".." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
578 { 'J', ".---" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
579 { 'K', "-.-" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
580 { 'L', ".-.." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
581 { 'M', "--" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
582 { 'N', "-." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
583 { 'O', "---" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
584 { 'P', ".--." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
585 { 'Q', "--.-" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
586 { 'R', ".-." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
587 { 'S', "..." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
588 { 'T', "-" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
589 { 'U', "..-" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
590 { 'V', "...-" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
591 { 'W', ".--" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
592 { 'X', "-..-" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
593 { 'Y', "-.--" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
594 { 'Z', "--.." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
595 { '0', "-----" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
596 { '1', ".----" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
597 { '2', "..---" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
598 { '3', "...--" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
599 { '4', "....-" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
600 { '5', "....." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
601 { '6', "-...." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
602 { '7', "--..." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
603 { '8', "---.." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
604 { '9', "----." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
605 { '.', ".-.-.-" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
606 { ',', "--..--" },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
607 { '?', "..--.." },
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
608 };
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
609
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
610
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
611 char * conv2tab(conv_ent_t *tab, size_t ntab, char *str)
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
612 {
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
613 char *result, *s = str;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
614 size_t resSize, resPos = 0;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
615
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
616 if (!str) return NULL;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
617
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
618 resSize = (strlen(str) * 4) + SET_ALLOC_SIZE;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
619 if ((result = th_malloc(resSize)) == NULL)
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
620 return NULL;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
621
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
622 while (*s) {
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
623 size_t i;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
624 char c = toupper(*s);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
625 BOOL found = FALSE;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
626 for (i = 0; i < ntab; i++)
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
627 if (tab[i].c == c) {
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
628 PUSHSTR(tab[i].code);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
629 PUSHCHAR(' ');
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
630 found = TRUE;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
631 break;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
632 }
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
633 if (!found) PUSHCHAR(*s);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
634
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
635 s++;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
636 }
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
637 PUSHCHAR(0);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
638
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
639 return result;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
640 }
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
641
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
642
50
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
643 char * tab2str(conv_ent_t *tab, size_t ntab, char *str)
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
644 {
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
645 char *result, *s = str;
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
646 size_t resSize, resPos = 0;
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
647
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
648 if (!str) return NULL;
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
649
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
650 resSize = (strlen(str) * 4) + SET_ALLOC_SIZE;
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
651 if ((result = th_malloc(resSize)) == NULL)
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
652 return NULL;
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
653
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
654 while (*s) {
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
655 size_t i;
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
656 BOOL found = FALSE;
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
657 for (i = 0; i < ntab; i++) {
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
658 size_t len = strlen(tab[i].code);
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
659 if (!strcmp(s, tab[i].code) && s[len] == ' ') {
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
660 s += len;
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
661 PUSHCHAR(tab[i].c);
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
662 found = TRUE;
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
663 break;
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
664 }
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
665 }
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
666 if (!found) PUSHCHAR(*s);
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
667
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
668 s++;
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
669 }
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
670 PUSHCHAR(0);
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
671
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
672 return result;
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
673 }
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
674
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
675
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
676 char *encodeStr1(char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
677 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
678 char *result, *s = str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
679 size_t resSize, resPos = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
680
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
681 if (!str) return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
682
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
683 resSize = strlen(str) + SET_ALLOC_SIZE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
684 if ((result = th_malloc(resSize)) == NULL)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
685 return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
686
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
687 while (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
688 switch (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
689 case 32:
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
690 PUSHCHAR('+');
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
691 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
692
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
693 default:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
694 if (th_isalnum(*s))
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
695 PUSHCHAR(*s);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
696 else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
697 char tmpStr[4];
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
698 sprintf(tmpStr, "%2X", (unsigned char) *s);
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
699 PUSHCHAR('%');
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
700 PUSHSTR(tmpStr);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
701 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
702 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
703 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
704 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
705 }
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
706 PUSHCHAR(0);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
707
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
708 return result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
709 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
710
15
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
711
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
712 int getxdigit(int c, int shift)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
713 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
714 int i;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
715
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
716 if (c >= 'A' && c <= 'F')
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
717 i = c - 'A' + 10;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
718 else if (c >= 'a' && c <= 'f')
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
719 i = c - 'a' + 10;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
720 else if (c >= '0' && c <= '9')
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
721 i = c - '0';
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
722 else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
723 return -1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
724
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
725 return i << shift;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
726 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
727
15
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
728
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
729 char *decodeStr1(char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
730 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
731 char *result, *s = str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
732 size_t resSize, resPos = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
733 int c;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
734
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
735 if (!str) return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
736
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
737 resSize = strlen(str) + SET_ALLOC_SIZE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
738 if ((result = th_malloc(resSize)) == NULL)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
739 return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
740
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
741 while (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
742 switch (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
743 case '+':
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
744 PUSHCHAR(' ');
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
745 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
746 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
747
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
748 case '½':
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
749 /* Escape these .. */
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
750 PUSHCHAR('½');
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
751 PUSHCHAR('½');
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
752 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
753 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
754
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
755 case '\r':
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
756 PUSHCHAR(' ');
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
757 s++;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
758 break;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
759
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
760 case '%':
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
761 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
762 if (*s == '%')
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
763 PUSHCHAR('%');
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
764 else if ((c = getxdigit(*s, 4)) >= 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
765 int i = getxdigit(*(++s), 0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
766 if (i >= 0) {
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
767 PUSHCHAR(c | i);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
768 } else {
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
769 PUSHCHAR('§');
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
770 PUSHCHAR(*s);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
771 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
772 } else {
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
773 PUSHCHAR('§');
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
774 PUSHCHAR(*s);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
775 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
776 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
777 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
778
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
779 default:
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
780 PUSHCHAR(*s);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
781 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
782 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
783 }
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
784 PUSHCHAR(0);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
785
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
786 return result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
787 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
788
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
789
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
790 char *stripTags(char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
791 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
792 char *result, *s = str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
793 size_t resSize, resPos = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
794
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
795 if (!str) return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
796
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
797 resSize = strlen(str) + SET_ALLOC_SIZE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
798 if ((result = th_malloc(resSize)) == NULL)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
799 return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
800
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
801 while (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
802 if (*s == '<') {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
803 while (*s && *s != '>') s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
804 if (*s == '>') s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
805 } else
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
806 PUSHCHAR(*s++);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
807 }
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
808 PUSHCHAR(0);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
809
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
810 return result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
811 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
812
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
813
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
814 char *encodeStr2(char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
815 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
816 char *result, *s = str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
817 size_t resSize, resPos = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
818
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
819 if (!str) return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
820
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
821 resSize = strlen(str) + SET_ALLOC_SIZE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
822 if ((result = th_malloc(resSize)) == NULL)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
823 return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
824
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
825 while (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
826 int i;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
827 BOOL found = FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
828 for (i = 0; i < numHTMLEntities; i++)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
829 if (HTMLEntities[i].c == *s) {
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
830 PUSHSTR(HTMLEntities[i].ent);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
831 found = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
832 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
833 }
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
834 if (!found) PUSHCHAR(*s);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
835
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
836 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
837 }
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
838 PUSHCHAR(0);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
839
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
840 return result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
841 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
842
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
843
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
844 char *decodeStr2(char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
845 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
846 char *result, *s = str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
847 size_t resSize, resPos = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
848
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
849 if (!str) return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
850
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
851 resSize = strlen(str);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
852 if ((result = th_malloc(resSize)) == NULL)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
853 return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
854
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
855 while (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
856 if (*s == '&') {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
857 int i;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
858 BOOL found = FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
859 for (i = 0; i < numHTMLEntities; i++) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
860 html_entity_t *ent = &HTMLEntities[i];
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
861 int len = strlen(ent->ent);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
862 if (!strncmp(s, ent->ent, len)) {
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
863 PUSHCHAR(ent->c);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
864 s += len;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
865 found = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
866 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
867 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
868 }
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
869 if (!found) PUSHCHAR(*s++);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
870 } else
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
871 PUSHCHAR(*s++);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
872 }
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
873 PUSHCHAR(0);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
874
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
875 return result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
876 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
877
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
878
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
879 BOOL sendUserMsg(int sock, char *user, char *fmt, ...)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
880 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
881 char tmpBuf[4096], tmpBuf2[4096+256];
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
882 int n;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
883 va_list ap;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
884
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
885 va_start(ap, fmt);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
886 n = vsnprintf(tmpBuf, sizeof(tmpBuf), fmt, ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
887 va_end(ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
888
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
889 if (n < 0) return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
890
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
891 snprintf(tmpBuf2, sizeof(tmpBuf2),
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
892 "<USER>%s</USER><MESSAGE>%s</MESSAGE>",
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
893 user, tmpBuf);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
894
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
895 return sendToSocket(sock, tmpBuf2, strlen(tmpBuf2) + 1);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
896 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
897
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
898
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
899 int handleUser(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
900 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
901 const char *msg = "</USER><MESSAGE>";
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
902 char *p = str, *q, *s, *t, *h;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
903
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
904 (void) sock;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
905
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
906 s = strstr(str, msg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
907 if (!s) return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
908 *s = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
909 s += strlen(msg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
910
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
911 q = strstr(s, "</MESSAGE>");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
912 if (!q) return 3;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
913 *q = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
914
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
915 s = decodeStr1(s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
916 if (!s) return -1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
917
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
918 p = decodeStr1(p);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
919 if (!p) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
920 th_free(s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
921 return -2;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
922 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
923
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
924
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
925 if (*s == '/') {
40
3e2548c3eb51 Fix PRV handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
926 t = stripTags(s + 1);
3e2548c3eb51 Fix PRV handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
927 if (!strncmp(t, "BPRV", 4)) {
3e2548c3eb51 Fix PRV handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
928 h = decodeStr2(t + 1);
3e2548c3eb51 Fix PRV handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
929 printMsg("½11½%s½0½\n", h);
3e2548c3eb51 Fix PRV handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
930 } else {
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
931 h = decodeStr2(t);
40
3e2548c3eb51 Fix PRV handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
932 printMsg("½9½* %s½0½\n", h);
19
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
933 }
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
934 th_free(h);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
935 th_free(t);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
936 } else {
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
937 t = stripTags(s);
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
938 h = decodeStr2(t);
50
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
939 if (setMorseMode) {
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
940 th_free(t); t = h;
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
941 h = tab2str(morseTab, sizeof(morseTab) / sizeof(morseTab[0]), t);
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
942 }
57
e93532b9dca1 Highlight username differently.
Matti Hamalainen <ccr@tnsp.org>
parents: 56
diff changeset
943 printMsg("½5½<½%d½%s½5½>½0½ %s\n", strcmp(p, optUserName) ? 15 : 14, p, h);
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
944 th_free(h);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
945 th_free(t);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
946 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
947
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
948 th_free(s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
949 th_free(p);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
950 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
951 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
952
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
953
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
954 int handleLogin(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
955 {
19
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
956 char tmpStr[256] = "";
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
957 time_t timeStamp;
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
958 struct tm *tmpTime;;
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
959
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
960 timeStamp = time(NULL);
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
961 if ((tmpTime = localtime(&timeStamp)) != NULL) {
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
962 strftime(tmpStr, sizeof(tmpStr), "%c", tmpTime);
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
963 }
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
964
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
965 if (!strncmp(str, "FAILURE", 7)) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
966 printMsg("½1½Login failure½0½ - ½3½%s½0½\n", tmpStr);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
967 return -2;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
968 } else if (!strncmp(str, "SUCCESS", 7)) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
969 printMsg("½2½Login success½0½ - ½3½%s½0½\n", tmpStr);
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
970 sendUserMsg(sock, optUserName2, "%%2FRequestUserList");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
971 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
972 } else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
973 return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
974 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
975
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
976
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
977 int handleAddUser(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
978 {
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
979 char *p, *s = strstr(str, "</ADD_USER>");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
980
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
981 (void) sock;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
982
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
983 if (!s) return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
984 *s = 0;
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
985
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
986 p = decodeStr1(str);
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
987 if (!p) return -1;
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
988
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
989 printMsg("! ½3½%s½0½ ½2½ADDED.½0½\n", p);
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
990 th_free(p);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
991 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
992 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
993
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
994
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
995 int handleDeleteUser(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
996 {
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
997 char *p, *s = strstr(str, "</DELETE_USER>");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
998
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
999 (void) sock;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1000
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1001 if (!s) return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1002 *s = 0;
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1003
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1004 p = decodeStr1(str);
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1005 if (!p) return -1;
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1006
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1007 printMsg("! ½3½%s½0½ ½1½DELETED.½0½\n", p);
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1008 th_free(p);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1009 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1010 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1011
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1012
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1013 int handleFoo(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1014 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1015 (void) sock; (void) str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1016
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1017 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1018 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1019
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1020
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1021 typedef struct {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1022 char *cmd;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1023 int (*handler)(int, char *);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1024 } protocmd_t;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1025
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1026
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1027 protocmd_t protoCmds[] = {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1028 { "<USER>", handleUser },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1029 { "<LOGIN_", handleLogin },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1030 { "<DELETE_USER>", handleDeleteUser },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1031 { "<ADD_USER>", handleAddUser },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1032 { "<NUMCLIENTS>", handleFoo },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1033 };
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1034
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1035 const int nprotoCmds = (sizeof(protoCmds) / sizeof(protoCmds[0]));
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1036
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1037
51
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1038 int handleProtocol(const int sock, char *buf, size_t bufLen)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1039 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1040 int i;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1041
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1042 for (i = 0; i < nprotoCmds; i++) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1043 size_t cmdLen = strlen(protoCmds[i].cmd);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1044 if (cmdLen < bufLen && !strncmp(buf, protoCmds[i].cmd, cmdLen)) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1045 return protoCmds[i].handler(sock, buf + cmdLen);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1046 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1047 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1048
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1049 return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1050 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1051
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1052
51
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1053 int handleUserInput(const int sock, char *buf, size_t bufLen)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1054 {
25
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
1055 char *tmpStr, *tmpStr2, tmpBuf[4096];
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1056 BOOL result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1057
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1058 /* Trim right */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1059 buf[--bufLen] = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1060 while (bufLen > 0 && (buf[bufLen] == '\n' || buf[bufLen] == '\r' || th_isspace(buf[bufLen])))
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1061 buf[bufLen--] = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1062
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1063 /* Check command */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1064 if (*buf == 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1065 return 1;
25
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
1066 } else if (!strncmp(buf, "/color ", 7)) {
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1067 int tmpInt;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1068 if ((tmpInt = getColor(buf+7)) < 0) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1069 printMsg("Invalid color value '%s'\n", buf+7);
25
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
1070 return 1;
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
1071 }
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1072 optUserColor = tmpInt;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1073 printMsg("Setting color to #%06x\n", optUserColor);
25
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
1074 sendUserMsg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
1075 return 0;
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1076 } else if (!strncmp(buf, "/flood ", 7)) {
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1077 int i;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1078
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1079 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg . .",
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1080 buf+7);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1081
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1082 tmpStr = encodeStr2(tmpBuf);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1083 if (!tmpStr) return -2;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1084 tmpStr2 = encodeStr1(tmpStr);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1085 if (!tmpStr2) {
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1086 th_free(tmpStr);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1087 return -3;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1088 }
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1089
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1090 result = TRUE;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1091 for (i = 0; i < 50 && result; i++) {
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1092 result = sendUserMsg(sock, optUserName2, "%s", tmpStr2);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1093 usleep(250);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1094 }
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1095
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1096 th_free(tmpStr);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1097 th_free(tmpStr2);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1098 return 0;
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1099 } else if (!strncmp(buf, "/to ", 4)) {
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1100 th_free(setTarget);
50
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
1101 setTarget = strdup(buf + 4);
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1102 printMsg("Set prv target to '%s'\n", setTarget);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1103 return 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1104 } else if (setPrvMode) {
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1105 if (setTarget != NULL) {
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1106 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1107 buf = tmpBuf;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1108 } else {
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1109 printMsg("No target set, exiting prv mode.\n");
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1110 setPrvMode = FALSE;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1111 return 1;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1112 }
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1113 } else if (buf[0] != '/') {
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1114 if (setMorseMode) {
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1115 tmpStr = conv2tab(morseTab, sizeof(morseTab) / sizeof(morseTab[0]), buf);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1116 strncpy(tmpBuf, tmpStr, sizeof(tmpBuf));
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1117 tmpBuf[sizeof(tmpBuf)-1] = 0;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1118 th_free(tmpStr);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1119 buf = tmpBuf;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1120 }
24
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
1121 }
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
1122
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1123
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1124
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1125 /* Send double-encoded */
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1126 tmpStr = encodeStr2(buf);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1127 if (!tmpStr) return -2;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1128 tmpStr2 = encodeStr1(tmpStr);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1129 if (!tmpStr2) {
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1130 th_free(tmpStr);
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1131 return -3;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1132 }
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1133
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1134 result = sendUserMsg(sock, optUserName2, "%s", tmpStr2);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1135 th_free(tmpStr);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1136 th_free(tmpStr2);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1137 if (result)
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1138 return 0;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1139 else
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1140 return -1;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1141 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1142
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1143
46
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1144 BOOL initializeWindows(void)
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1145 {
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1146 if (mainWin) delwin(mainWin);
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1147 if (statusWin) delwin(statusWin);
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1148 if (editWin) delwin(editWin);
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1149
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1150 mainWin = newwin(LINES - 4, COLS, 0, 0);
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1151 statusWin = newwin(1, COLS, LINES - 4, 0);
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1152 editWin = newwin(3, COLS, LINES - 3, 0);
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1153
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1154 if (mainWin == NULL || statusWin == NULL || editWin == NULL) {
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1155 THERR("Could not create ncurses windows!\n");
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1156 return FALSE;
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1157 }
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1158 scrollok(mainWin, 1);
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1159
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1160 return TRUE;
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1161 }
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1162
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1163
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1164 int main(int argc, char *argv[])
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1165 {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1166 int tmpSocket, curVis, updateCount = 0;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1167 struct hostent *tmpHost;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1168 BOOL argsOK, isError = FALSE,
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1169 exitProg = FALSE,
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1170 colorSet = FALSE,
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1171 cursesInit = FALSE,
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1172 insertMode = TRUE;
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1173 struct timeval tv;
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1174 fd_set sockfds;
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1175 char *tmpStr;
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
1176 editbuf_t *editBuf = newBuf(SET_BUFSIZE);
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1177 editbuf_t *histBuf[SET_MAX_HISTORY+2];
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1178 int histPos = 0, histMax = 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1179
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1180 memset(histBuf, 0, sizeof(histBuf));
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1181
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1182 /* Initialize */
56
Matti Hamalainen <ccr@tnsp.org>
parents: 55
diff changeset
1183 th_init("NNChat", "Newbie Nudes chat client", "0.6.4",
6
526ba3b578d7 Changed copyright etc. again.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1184 "Written and designed by Anonymous Finnish Guy (C) 2008",
526ba3b578d7 Changed copyright etc. again.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
1185 "This software is freeware, use and distribute as you wish.");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1186 th_verbosityLevel = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1187
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1188 /* Parse arguments */
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
1189 argsOK = th_args_process(argc, argv, optList, optListN,
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1190 argHandleOpt, argHandleFile, FALSE);
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
1191
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1192 /* Check the mode and arguments */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1193 if (optUserName == NULL || optPassword == NULL) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1194 THERR("User/pass not specified, get some --help\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1195 return -1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1196 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1197
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
1198 if (!argsOK)
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
1199 return -2;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
1200
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1201 /* Open logfile */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1202 if (optLogFilename) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1203 THMSG(1, "Opening logfile '%s'\n", optLogFilename);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1204
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1205 if ((optLogFile = fopen(optLogFilename, "a")) == NULL) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1206 THERR("Could not open logfile for appending!\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1207 return -9;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1208 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1209 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1210
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1211 /* Okay ... */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1212 THMSG(1, "Trying to resolve host '%s' ...\n", optServer);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1213 tmpHost = gethostbyname(optServer);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1214 if (tmpHost == NULL) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1215 THERR("Could not resolve hostname: %s.\n",
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1216 hstrerror(h_errno));
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1217 return -3;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1218 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1219 THMSG(2, "True hostname: %s\n", tmpHost->h_name);
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
1220
39
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
1221 #if 0
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1222 /* To emulate the official client, we first make a fake connection ... */
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1223 if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) {
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1224 THERR("Fakeprobe connection setup failed!\n");
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1225 goto err_exit;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1226 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1227
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1228 tmpStr = "<policy-file-request/>";
23
40fecbab1dc1 Check for error condition properly.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
1229 if (sendToSocket(tmpSocket, tmpStr, strlen(tmpStr) + 1) == FALSE) {
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1230 THERR("Failed to send fakeprobe.\n");
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1231 goto err_exit;
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1232 } else {
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1233 ssize_t gotBuf;
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1234 char tmpBuf[4096];
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1235 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0);
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1236 tmpBuf[gotBuf-1] = 0;
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1237 THMSG(2, "Probe got: %s\n", tmpBuf);
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1238 closeConnection(tmpSocket);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1239 }
39
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
1240 #endif
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
1241
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1242 /* Okay, now do the proper connection ... */
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1243 if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) {
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1244 THERR("Main connection setup failed!\n");
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1245 goto err_exit;
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1246 }
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1247
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
1248 THMSG(1, "Connected, logging in as '%s', site '%s'.\n", optUserName, optSite);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1249 optUserName2 = encodeStr1(optUserName);
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
1250 tmpStr = encodeStr1(optSite);
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
1251 sendUserMsg(tmpSocket, optUserName2, "%%2Flogin%%20%%2Dsite%%20%s%%20%%2Dpassword%%20%s", tmpStr, optPassword);
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
1252 th_free(tmpStr);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1253
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
1254 /* Initialize NCurses */
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1255 if (!optDaemon) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1256 initscr();
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1257 raw();
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1258 keypad(stdscr, TRUE);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1259 noecho();
50
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
1260 meta(stdscr, TRUE);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1261 timeout(SET_DELAY);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1262 curVis = curs_set(0);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1263 if (has_colors()) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1264 start_color();
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1265
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1266 init_pair(1, COLOR_RED, COLOR_BLACK);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1267 init_pair(2, COLOR_GREEN, COLOR_BLACK);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1268 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1269 init_pair(4, COLOR_BLUE, COLOR_BLACK);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1270 init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1271 init_pair(6, COLOR_CYAN, COLOR_BLACK);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1272 init_pair(7, COLOR_WHITE, COLOR_BLACK);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1273 init_pair(8, COLOR_BLACK, COLOR_BLACK);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1274
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1275 init_pair(10, COLOR_BLACK, COLOR_RED);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1276 init_pair(11, COLOR_WHITE, COLOR_RED);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1277 init_pair(12, COLOR_GREEN, COLOR_RED);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1278 init_pair(13, COLOR_YELLOW, COLOR_RED);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1279 init_pair(14, COLOR_BLUE, COLOR_RED);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1280 init_pair(15, COLOR_MAGENTA, COLOR_RED);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1281 init_pair(16, COLOR_CYAN, COLOR_RED);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1282
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1283 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1284
46
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1285 cursesInit = TRUE;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1286
46
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1287 if (!initializeWindows())
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1288 goto err_exit;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1289
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1290 clearBuf(editBuf);
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1291 printEditBuf("", editBuf);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1292 updateStatus(insertMode);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1293 }
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1294
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1295
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1296 /* Enter mainloop */
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1297 FD_ZERO(&sockfds);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1298 FD_SET(tmpSocket, &sockfds);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1299
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1300 while (!isError && !exitProg) {
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1301 int result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1302 fd_set tmpfds;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1303
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1304 /* Check for incoming data from the server */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1305 tv.tv_sec = 0;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1306 tv.tv_usec = SET_DELAY_USEC;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1307 tmpfds = sockfds;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1308 if ((result = select(tmpSocket+1, &tmpfds, NULL, NULL, &tv)) == -1) {
53
7b98da167a0b Improved error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
1309 if (errno != EINTR && errno != ERESTART) {
7b98da167a0b Improved error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
1310 printMsg("Error occured in select(sockfds): %d, %s\n", errno, strerror(errno));
7b98da167a0b Improved error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
1311 isError = TRUE;
7b98da167a0b Improved error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
1312 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1313 } else if (FD_ISSET(tmpSocket, &tmpfds)) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1314 ssize_t gotBuf;
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1315 char tmpBuf[8192];
51
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1316 char *bufPtr = tmpBuf;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1317 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1318
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1319 if (gotBuf < 0) {
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
1320 printMsg("Error in recv: %s\n", strerror(errno));
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1321 isError = TRUE;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1322 } else if (gotBuf == 0) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1323 printMsg("Server closed connection.\n");
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1324 isError = TRUE;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1325 } else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1326 /* Handle protocol data */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1327 tmpBuf[gotBuf] = 0;
51
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1328 do {
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1329 size_t bufLen = strlen(bufPtr) + 1;
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1330 result = handleProtocol(tmpSocket, bufPtr, bufLen);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1331
51
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1332 if (result > 0) {
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1333 /* Couldn't handle the message for some reason */
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1334 printMsg("Could not handle: %s\n", tmpBuf);
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1335 } else if (result < 0) {
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1336 /* Fatal error, quit */
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1337 printMsg("Fatal error with message: %s\n", tmpBuf);
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1338 isError = TRUE;
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1339 }
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1340
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1341 gotBuf -= bufLen;
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1342 bufPtr += bufLen;
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
1343 } while (gotBuf > 0 && !isError);
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1344 updateStatus(insertMode);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1345 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1346 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1347
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1348 /* Handle user input */
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
1349 if (!optDaemon) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1350 int c, cnt = 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1351 BOOL update = FALSE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1352
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1353 /* Handle several buffered keypresses at once */
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1354 do {
50
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
1355 c = wgetch(stdscr);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1356 switch (c) {
46
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1357 case KEY_RESIZE:
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1358 if (!initializeWindows()) {
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1359 THERR("Error resizing ncurses windows\n");
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1360 isError = TRUE;
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1361 }
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1362 break;
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
1363
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1364 case KEY_ENTER:
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1365 case '\n':
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1366 case '\r':
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1367 /* Call the user input handler */
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1368 if (editBuf->len > 0) {
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1369
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1370 if (histMax > 0) {
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
1371 freeBuf(histBuf[SET_MAX_HISTORY+1]);
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
1372 histBuf[SET_MAX_HISTORY+1] = NULL;
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1373 memmove(&histBuf[2], &histBuf[1], histMax * sizeof(histBuf[0]));
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1374 }
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
1375
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1376 histPos = 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1377 histBuf[1] = copyBuf(editBuf);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1378 if (histMax < SET_MAX_HISTORY) histMax++;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1379
34
f5d335771b16 Fix a bug with mixed editBuf->pos vs editBuf->len.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
1380 insertBuf(editBuf, editBuf->len, 0);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1381 result = handleUserInput(tmpSocket, editBuf->data, editBuf->len);
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1382
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1383 clearBuf(editBuf);
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1384
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1385 if (result < 0) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1386 printMsg("Fatal error handling user input: %s\n", editBuf->data);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1387 isError = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1388 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1389
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1390 update = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1391 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1392 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1393
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1394 case 0x09: /* Tab = switch between PRV */
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1395 if (setPrvMode)
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1396 setPrvMode = FALSE;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1397 else {
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1398 if (setTarget != NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1399 setPrvMode = TRUE;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1400 }
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1401 update = TRUE;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1402 break;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1403
54
f6666e7c1a48 More comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
1404 case KEY_UP: /* Backwards in input history */
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
1405 if (histPos == 0) {
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
1406 freeBuf(histBuf[0]);
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1407 histBuf[0] = copyBuf(editBuf);
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
1408 }
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1409 if (histPos < histMax) {
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1410 histPos++;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1411 freeBuf(editBuf);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1412 editBuf = copyBuf(histBuf[histPos]);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1413 update = TRUE;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1414 }
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1415 break;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1416
54
f6666e7c1a48 More comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
1417 case KEY_DOWN: /* Forwards in input history */
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1418 if (histPos > 0) {
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1419 histPos--;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1420 freeBuf(editBuf);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1421 editBuf = copyBuf(histBuf[histPos]);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1422 update = TRUE;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1423 }
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1424 break;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1425
54
f6666e7c1a48 More comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
1426 case 0x204: /* ctrl+left = Skip words left */
45
ceec75116aa7 Improved editing commands ctrl+<left/right>
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
1427 while (editBuf->pos > 0 && isspace(editBuf->data[editBuf->pos - 1]))
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1428 editBuf->pos--;
45
ceec75116aa7 Improved editing commands ctrl+<left/right>
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
1429 while (editBuf->pos > 0 && !isspace(editBuf->data[editBuf->pos - 1]))
ceec75116aa7 Improved editing commands ctrl+<left/right>
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
1430 editBuf->pos--;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1431 update = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1432 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1433
54
f6666e7c1a48 More comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
1434 case 0x206: /* ctrl+right = Skip words right */
45
ceec75116aa7 Improved editing commands ctrl+<left/right>
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
1435 while (editBuf->pos < editBuf->len && isspace(editBuf->data[editBuf->pos]))
ceec75116aa7 Improved editing commands ctrl+<left/right>
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
1436 editBuf->pos++;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1437 while (editBuf->pos < editBuf->len && !isspace(editBuf->data[editBuf->pos]))
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1438 editBuf->pos++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1439 if (editBuf->pos > editBuf->len)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1440 editBuf->pos = editBuf->len;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1441 update = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1442 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1443
54
f6666e7c1a48 More comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
1444 case 0x111: /* F9 = Quit */
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
1445 printMsg("Quitting per user request.\n");
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1446 exitProg = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1447 break;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1448
54
f6666e7c1a48 More comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
1449 case 0x109: /* F1 = Toggle insert / overwrite mode */
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1450 insertMode = !insertMode;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1451 update = TRUE;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1452 break;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1453
54
f6666e7c1a48 More comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
1454 case 0x10a: /* F2 = Clear editbuffer */
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1455 clearBuf(editBuf);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1456 update = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1457 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1458
54
f6666e7c1a48 More comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
1459 case 0x10b: /* F3 = Toggle special mode */
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1460 setMorseMode = !setMorseMode;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1461 printMsg("Morse code mode = %d\n", setMorseMode);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1462 update = TRUE;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1463 break;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
1464
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1465 case KEY_HOME: setBufPos(editBuf, 0); update = TRUE; break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1466 case KEY_END: setBufPos(editBuf, editBuf->len); update = TRUE; break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1467 case KEY_LEFT: setBufPos(editBuf, editBuf->pos - 1); update = TRUE; break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1468 case KEY_RIGHT: setBufPos(editBuf, editBuf->pos + 1); update = TRUE; break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1469
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1470 case KEY_BACKSPACE:
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1471 deleteBuf(editBuf, editBuf->pos - 1);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1472 setBufPos(editBuf, editBuf->pos - 1);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1473 update = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1474 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1475
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1476 case 0x14a:
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1477 /* Delete */
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1478 deleteBuf(editBuf, editBuf->pos);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1479 update = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1480 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1481
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1482 case 0x0c:
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1483 /* ctrl+l */
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1484 redrawwin(mainWin);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1485 redrawwin(statusWin);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1486 redrawwin(editWin);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1487 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1488
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1489 case ERR:
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1490 /* Ignore */
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1491 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1492
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1493 default:
36
34864d021d7e Accept 'äöå' in input.
Matti Hamalainen <ccr@tnsp.org>
parents: 35
diff changeset
1494 if (isprint(c) || c == 0xe4 || c == 0xf6 || c == 0xc4 || c == 0xd6) {
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1495 if (insertMode)
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1496 insertBuf(editBuf, editBuf->pos, c);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1497 else
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1498 writeBuf(editBuf, editBuf->pos, c);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1499 setBufPos(editBuf, editBuf->pos + 1);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1500 update = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1501 } else {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1502 printMsg("Unhandled key: %02x\n", c);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1503 }
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1504 break;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1505 }
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1506 } while (c != ERR && !exitProg && ++cnt < 10);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1507
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1508 if (update) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1509 /* Update edit line */
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1510 printEditBuf(setPrvMode ? setTarget : "", editBuf);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1511 updateStatus(insertMode);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1512 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1513 } /* !optDaemon */
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1514
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1515 if (++updateCount > 10) {
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1516 updateStatus(insertMode);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1517 updateCount = 0;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1518 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1519
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1520 if (!colorSet) {
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1521 colorSet = TRUE;
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1522 printMsg("%s v%s - %s\n", th_prog_name, th_prog_version, th_prog_fullname);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1523 printMsg("%s\n", th_prog_author);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1524 printMsg("%s\n", th_prog_license);
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
1525 sendUserMsg(tmpSocket, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1526 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1527 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1528
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1529 /* Shutdown */
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1530 err_exit:
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1531 if (cursesInit) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1532 if (curVis != ERR)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1533 curs_set(curVis);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1534 endwin();
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1535 THMSG(1, "NCurses deinitialized.\n");
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1536 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1537
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1538 if (isError) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1539 THMSG(1, "Error exit.\n");
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1540 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
1541
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1542 th_free(optUserName2);
10
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1543
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1544 closeConnection(tmpSocket);
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1545
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1546 THMSG(1, "Connection terminated.\n");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1547
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1548 if (optLogFile) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1549 THMSG(1, "Closing logfile.\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1550 fclose(optLogFile);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1551 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1552
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1553 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1554 }