aboutsummaryrefslogtreecommitdiff
path: root/src/conversation/gnunet-conversation.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/conversation/gnunet-conversation.c')
-rw-r--r--src/conversation/gnunet-conversation.c132
1 files changed, 110 insertions, 22 deletions
diff --git a/src/conversation/gnunet-conversation.c b/src/conversation/gnunet-conversation.c
index a042629cc..3cb6cc05f 100644
--- a/src/conversation/gnunet-conversation.c
+++ b/src/conversation/gnunet-conversation.c
@@ -29,13 +29,25 @@
29#include "gnunet_gnsrecord_lib.h" 29#include "gnunet_gnsrecord_lib.h"
30#include "gnunet_conversation_service.h" 30#include "gnunet_conversation_service.h"
31#include "gnunet_namestore_service.h" 31#include "gnunet_namestore_service.h"
32 32#ifdef WINDOWS
33#include "../util/gnunet-helper-w32-console.h"
34#endif
33 35
34/** 36/**
35 * Maximum length allowed for the command line input. 37 * Maximum length allowed for the command line input.
36 */ 38 */
37#define MAX_MESSAGE_LENGTH 1024 39#define MAX_MESSAGE_LENGTH 1024
38 40
41#define XSTRINGIFY(x) STRINGIFY(x)
42
43#define STRINGIFY(x) (#x)
44
45#ifdef WINDOWS
46/**
47 * Helper that reads the console for us.
48 */
49struct GNUNET_HELPER_Handle *stdin_hlp;
50#endif
39 51
40/** 52/**
41 * Possible states of the phone. 53 * Possible states of the phone.
@@ -997,6 +1009,13 @@ static void
997do_stop_task (void *cls, 1009do_stop_task (void *cls,
998 const struct GNUNET_SCHEDULER_TaskContext *tc) 1010 const struct GNUNET_SCHEDULER_TaskContext *tc)
999{ 1011{
1012#ifdef WINDOWS
1013 if (NULL != stdin_hlp)
1014 {
1015 GNUNET_HELPER_stop (stdin_hlp, GNUNET_NO);
1016 stdin_hlp = NULL;
1017 }
1018#endif
1000 if (NULL != call) 1019 if (NULL != call)
1001 { 1020 {
1002 GNUNET_CONVERSATION_call_stop (call); 1021 GNUNET_CONVERSATION_call_stop (call);
@@ -1027,6 +1046,60 @@ do_stop_task (void *cls,
1027 phone_state = PS_ERROR; 1046 phone_state = PS_ERROR;
1028} 1047}
1029 1048
1049static void
1050handle_command_string (char *message, size_t str_len)
1051{
1052 size_t i;
1053 const char *ptr;
1054
1055 if (0 == str_len)
1056 return;
1057 if (message[str_len - 1] == '\n')
1058 message[str_len - 1] = '\0';
1059 if (message[str_len - 2] == '\r')
1060 message[str_len - 2] = '\0';
1061 if (0 == strlen (message))
1062 return;
1063 i = 0;
1064 while ((NULL != commands[i].command) &&
1065 (0 != strncasecmp (commands[i].command, message,
1066 strlen (commands[i].command))))
1067 i++;
1068 ptr = &message[strlen (commands[i].command)];
1069 while (isspace ((int) *ptr))
1070 ptr++;
1071 if ('\0' == *ptr)
1072 ptr = NULL;
1073 commands[i].Action (ptr);
1074}
1075
1076
1077#ifdef WINDOWS
1078int
1079console_reader_chars (void *cls, void *client,
1080 const struct GNUNET_MessageHeader *message)
1081{
1082 char *chars;
1083 size_t str_size;
1084 switch (ntohs (message->type))
1085 {
1086 case GNUNET_MESSAGE_TYPE_W32_CONSOLE_HELPER_CHARS:
1087 chars = (char *) &message[1];
1088 str_size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
1089 if (chars[str_size - 1] != '\0')
1090 return GNUNET_SYSERR;
1091 /* FIXME: is it ok that we pass part of a const struct to
1092 * this function that may mangle the contents?
1093 */
1094 handle_command_string (chars, str_size - 1);
1095 break;
1096 default:
1097 GNUNET_break (0);
1098 break;
1099 }
1100 return GNUNET_OK;
1101}
1102#endif
1030 1103
1031/** 1104/**
1032 * Task to handle commands from the terminal. 1105 * Task to handle commands from the terminal.
@@ -1039,8 +1112,6 @@ handle_command (void *cls,
1039 const struct GNUNET_SCHEDULER_TaskContext *tc) 1112 const struct GNUNET_SCHEDULER_TaskContext *tc)
1040{ 1113{
1041 char message[MAX_MESSAGE_LENGTH + 1]; 1114 char message[MAX_MESSAGE_LENGTH + 1];
1042 const char *ptr;
1043 size_t i;
1044 1115
1045 handle_cmd_task = 1116 handle_cmd_task =
1046 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 1117 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
@@ -1050,23 +1121,7 @@ handle_command (void *cls,
1050 memset (message, 0, MAX_MESSAGE_LENGTH + 1); 1121 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
1051 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin)) 1122 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
1052 return; 1123 return;
1053 if (0 == strlen (message)) 1124 handle_command_string (message, strlen (message));
1054 return;
1055 if (message[strlen (message) - 1] == '\n')
1056 message[strlen (message) - 1] = '\0';
1057 if (0 == strlen (message))
1058 return;
1059 i = 0;
1060 while ((NULL != commands[i].command) &&
1061 (0 != strncasecmp (commands[i].command, message,
1062 strlen (commands[i].command))))
1063 i++;
1064 ptr = &message[strlen (commands[i].command)];
1065 while (isspace ((int) *ptr))
1066 ptr++;
1067 if ('\0' == *ptr)
1068 ptr = NULL;
1069 commands[i].Action (ptr);
1070} 1125}
1071 1126
1072 1127
@@ -1144,6 +1199,30 @@ run (void *cls,
1144 id = GNUNET_IDENTITY_connect (cfg, 1199 id = GNUNET_IDENTITY_connect (cfg,
1145 &identity_cb, 1200 &identity_cb,
1146 NULL); 1201 NULL);
1202#ifdef WINDOWS
1203 if (stdin_fh == NULL)
1204 {
1205 static char cpid[64];
1206 static char *args[] = {"gnunet-helper-w32-console.exe", "chars",
1207 XSTRINGIFY (MAX_MESSAGE_LENGTH), cpid, NULL};
1208 snprintf (cpid, 64, "%d", GetCurrentProcessId ());
1209 stdin_hlp = GNUNET_HELPER_start (
1210 GNUNET_NO,
1211 "gnunet-helper-w32-console",
1212 args,
1213 console_reader_chars,
1214 NULL,
1215 NULL);
1216 if (NULL == stdin_hlp)
1217 {
1218 FPRINTF (stderr,
1219 "%s",
1220 _("Failed to start gnunet-helper-w32-console\n"));
1221 return;
1222 }
1223 }
1224 else
1225#endif
1147 handle_cmd_task = 1226 handle_cmd_task =
1148 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI, 1227 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
1149 &handle_command, NULL); 1228 &handle_command, NULL);
@@ -1171,13 +1250,22 @@ main (int argc, char *const *argv)
1171 1, &GNUNET_GETOPT_set_uint, &line}, 1250 1, &GNUNET_GETOPT_set_uint, &line},
1172 GNUNET_GETOPT_OPTION_END 1251 GNUNET_GETOPT_OPTION_END
1173 }; 1252 };
1174 int flags;
1175 int ret; 1253 int ret;
1176 1254#ifndef WINDOWS
1255 int flags;
1177 flags = fcntl (0, F_GETFL, 0); 1256 flags = fcntl (0, F_GETFL, 0);
1178 flags |= O_NONBLOCK; 1257 flags |= O_NONBLOCK;
1179 fcntl (0, F_SETFL, flags); 1258 fcntl (0, F_SETFL, flags);
1180 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0); 1259 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1260#else
1261 if (FILE_TYPE_CHAR == GetFileType ((HANDLE) _get_osfhandle (0)))
1262 {
1263 stdin_fh = NULL;
1264 }
1265 else
1266 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1267#endif
1268
1181 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 1269 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1182 return 2; 1270 return 2;
1183 ret = GNUNET_PROGRAM_run (argc, argv, 1271 ret = GNUNET_PROGRAM_run (argc, argv,