summaryrefslogtreecommitdiff
path: root/daemons
diff options
context:
space:
mode:
authorBrant Thomsen <brant.thomsen@harman.com>2017-05-03 16:49:26 -0600
committerBrant Thomsen <brant.thomsen@harman.com>2017-05-03 16:49:26 -0600
commitd02a2d7d1684c624f75eeea900a9f02a0871d2b4 (patch)
tree672516f244b1c467379808a5c9862751ae1aea25 /daemons
parent1c8cf60c43a4d2c73c0120e3e6df29e3461c7cae (diff)
downloadOpen-AVB-d02a2d7d1684c624f75eeea900a9f02a0871d2b4.tar.gz
Added support for text interaction by clients
Clients that want to interact with the MAAP daemon using text (such as telnet clients) are now supported. Also fixed some memory leaks with invalid command-line flags.
Diffstat (limited to 'daemons')
-rw-r--r--daemons/maap/common/maap.c114
-rw-r--r--daemons/maap/common/maap.h7
-rw-r--r--daemons/maap/common/maap_iface.h7
-rw-r--r--daemons/maap/common/maap_parse.c66
-rw-r--r--daemons/maap/common/maap_parse.h14
-rw-r--r--daemons/maap/linux/src/maap_daemon.c158
6 files changed, 220 insertions, 146 deletions
diff --git a/daemons/maap/common/maap.c b/daemons/maap/common/maap.c
index 87f78ef0..03897753 100644
--- a/daemons/maap/common/maap.c
+++ b/daemons/maap/common/maap.c
@@ -303,77 +303,7 @@ int get_notify(Maap_Client *mc, const void **sender, Maap_Notify *mn) {
return 0;
}
-static void log_print_notify_result(Maap_Output_Type outputType, int logLevel, const char *szOutput)
-{
- if ((outputType & MAAP_OUTPUT_LOGGING)) {
- switch (logLevel) {
- case MAAP_LOG_LEVEL_ERROR:
- MAAP_LOG_ERROR(szOutput);
- break;
- case MAAP_LOG_LEVEL_WARNING:
- MAAP_LOG_WARNING(szOutput);
- break;
- case MAAP_LOG_LEVEL_INFO:
- MAAP_LOG_INFO(szOutput);
- break;
- case MAAP_LOG_LEVEL_STATUS:
- MAAP_LOG_STATUS(szOutput);
- break;
- case MAAP_LOG_LEVEL_DEBUG:
- MAAP_LOG_DEBUG(szOutput);
- break;
- case MAAP_LOG_LEVEL_VERBOSE:
- MAAP_LOG_VERBOSE(szOutput);
- break;
- }
- }
-
- if ((outputType & MAAP_OUTPUT_USER_FRIENDLY)) {
- int i, nLastSpace;
- int nInitial = -1;
-
- /* Break the string up into one-line chunks.
- * Note that tabs and newlines are not handled correctly. */
- while (*szOutput) {
- if (nInitial < 0) {
- if (logLevel == MAAP_LOG_LEVEL_ERROR) {
- printf("Error: ");
- nInitial = (int) strlen("Error: ");
- } else if (logLevel == MAAP_LOG_LEVEL_WARNING) {
- printf("Warning: ");
- nInitial = (int) strlen("Warning: ");
- } else {
- nInitial = 0;
- }
- } else {
- /* We already accounted for the initial text. */
- nInitial = 0;
- }
-
- nLastSpace = -1;
- for (i = 0; (i < MAAP_LOG_STDOUT_CONSOLE_WIDTH - nInitial || nLastSpace <= 0) && szOutput[i]; ++i) {
- if (isspace(szOutput[i])) { nLastSpace = i; }
- }
- if (szOutput[i] == '\0') {
- /* Print the remainder of the string. */
- puts(szOutput);
- break;
- }
-
- /* Print the string up to the last space. */
- for (i = 0; i < nLastSpace; ++i) {
- putc(*szOutput, stdout);
- szOutput++;
- }
- putc('\n', stdout);
-
- /* Go to the start of the next word in the string. */
- while (isspace(*szOutput)) { szOutput++; }
- }
- }
-}
-
-void print_notify(Maap_Notify *mn, Maap_Output_Type outputType)
+void print_notify(Maap_Notify *mn, print_notify_callback_t notify_callback, void *callback_data)
{
char szOutput[300];
@@ -385,33 +315,33 @@ void print_notify(Maap_Notify *mn, Maap_Output_Type outputType)
/* No error. Don't display anything. */
break;
case MAAP_NOTIFY_ERROR_REQUIRES_INITIALIZATION:
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_ERROR,
+ notify_callback(callback_data, MAAP_LOG_LEVEL_ERROR,
"MAAP is not initialized, so the command cannot be performed.");
break;
case MAAP_NOTIFY_ERROR_ALREADY_INITIALIZED:
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_ERROR,
+ notify_callback(callback_data, MAAP_LOG_LEVEL_ERROR,
"MAAP is already initialized, so the values cannot be changed.");
break;
case MAAP_NOTIFY_ERROR_RESERVE_NOT_AVAILABLE:
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_ERROR,
+ notify_callback(callback_data, MAAP_LOG_LEVEL_ERROR,
"The MAAP reservation is not available, or yield cannot allocate a replacement block. "
"Try again with a smaller address block size.");
break;
case MAAP_NOTIFY_ERROR_RELEASE_INVALID_ID:
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_ERROR,
+ notify_callback(callback_data, MAAP_LOG_LEVEL_ERROR,
"The MAAP reservation ID is not valid, so cannot be released or report its status.");
break;
case MAAP_NOTIFY_ERROR_OUT_OF_MEMORY:
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_ERROR,
+ notify_callback(callback_data, MAAP_LOG_LEVEL_ERROR,
"The MAAP application is out of memory.");
break;
case MAAP_NOTIFY_ERROR_INTERNAL:
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_ERROR,
+ notify_callback(callback_data, MAAP_LOG_LEVEL_ERROR,
"The MAAP application experienced an internal error.");
break;
default:
sprintf(szOutput, "The MAAP application returned an unknown error %d.", mn->result);
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_ERROR, szOutput);
+ notify_callback(callback_data, MAAP_LOG_LEVEL_ERROR, szOutput);
break;
}
@@ -423,13 +353,13 @@ void print_notify(Maap_Notify *mn, Maap_Output_Type outputType)
(unsigned long long) mn->start,
(unsigned long long) mn->start + mn->count - 1,
(unsigned int) mn->count);
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_INFO, szOutput);
+ notify_callback(callback_data, MAAP_LOG_LEVEL_INFO, szOutput);
} else {
sprintf(szOutput, "MAAP previously initialized to 0x%012llx-0x%012llx (Size: %d)",
(unsigned long long) mn->start,
(unsigned long long) mn->start + mn->count - 1,
(unsigned int) mn->count);
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_ERROR, szOutput);
+ notify_callback(callback_data, MAAP_LOG_LEVEL_ERROR, szOutput);
}
break;
case MAAP_NOTIFY_ACQUIRING:
@@ -439,10 +369,10 @@ void print_notify(Maap_Notify *mn, Maap_Output_Type outputType)
(unsigned long long) mn->start,
(unsigned long long) mn->start + mn->count - 1,
mn->count);
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_INFO, szOutput);
+ notify_callback(callback_data, MAAP_LOG_LEVEL_INFO, szOutput);
} else {
sprintf(szOutput, "Unknown address range %d acquisition error", mn->id);
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_ERROR, szOutput);
+ notify_callback(callback_data, MAAP_LOG_LEVEL_ERROR, szOutput);
}
break;
case MAAP_NOTIFY_ACQUIRED:
@@ -452,15 +382,15 @@ void print_notify(Maap_Notify *mn, Maap_Output_Type outputType)
(unsigned long long) mn->start,
(unsigned long long) mn->start + mn->count - 1,
mn->count);
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_INFO, szOutput);
+ notify_callback(callback_data, MAAP_LOG_LEVEL_INFO, szOutput);
} else if (mn->id != -1) {
sprintf(szOutput, "Address range %d of size %d not acquired",
mn->id, mn->count);
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_ERROR, szOutput);
+ notify_callback(callback_data, MAAP_LOG_LEVEL_ERROR, szOutput);
} else {
sprintf(szOutput, "Address range of size %d not acquired",
mn->count);
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_ERROR, szOutput);
+ notify_callback(callback_data, MAAP_LOG_LEVEL_ERROR, szOutput);
}
break;
case MAAP_NOTIFY_RELEASED:
@@ -470,11 +400,11 @@ void print_notify(Maap_Notify *mn, Maap_Output_Type outputType)
(unsigned long long) mn->start,
(unsigned long long) mn->start + mn->count - 1,
mn->count);
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_INFO, szOutput);
+ notify_callback(callback_data, MAAP_LOG_LEVEL_INFO, szOutput);
} else {
sprintf(szOutput, "Address range %d not released",
mn->id);
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_ERROR, szOutput);
+ notify_callback(callback_data, MAAP_LOG_LEVEL_ERROR, szOutput);
}
break;
case MAAP_NOTIFY_STATUS:
@@ -484,11 +414,11 @@ void print_notify(Maap_Notify *mn, Maap_Output_Type outputType)
(unsigned long long) mn->start,
(unsigned long long) mn->start + mn->count - 1,
mn->count);
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_INFO, szOutput);
+ notify_callback(callback_data, MAAP_LOG_LEVEL_INFO, szOutput);
} else {
sprintf(szOutput, "ID %d is not valid",
mn->id);
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_ERROR, szOutput);
+ notify_callback(callback_data, MAAP_LOG_LEVEL_ERROR, szOutput);
}
break;
case MAAP_NOTIFY_YIELDED:
@@ -497,15 +427,15 @@ void print_notify(Maap_Notify *mn, Maap_Output_Type outputType)
(unsigned long long) mn->start,
(unsigned long long) mn->start + mn->count - 1,
mn->count);
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_WARNING, szOutput);
+ notify_callback(callback_data, MAAP_LOG_LEVEL_WARNING, szOutput);
if (mn->result != MAAP_NOTIFY_ERROR_NONE) {
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_ERROR,
+ notify_callback(callback_data, MAAP_LOG_LEVEL_ERROR,
"A new address range will not be allocated");
}
break;
default:
sprintf(szOutput, "Notification type %d not recognized", mn->kind);
- log_print_notify_result(outputType, MAAP_LOG_LEVEL_ERROR, szOutput);
+ notify_callback(callback_data, MAAP_LOG_LEVEL_ERROR, szOutput);
break;
}
}
diff --git a/daemons/maap/common/maap.h b/daemons/maap/common/maap.h
index 3abce59d..7c585304 100644
--- a/daemons/maap/common/maap.h
+++ b/daemons/maap/common/maap.h
@@ -228,11 +228,12 @@ void add_notify(Maap_Client *mc, const void *sender, const Maap_Notify *mn);
int get_notify(Maap_Client *mc, const void **sender, Maap_Notify *mn);
/**
- * Output the text equivalent of the notification information to stdout.
+ * Output the text equivalent of the notification information to the callback function.
*
* @param mn Pointer to the notification information structure.
- * @param outputType One or more of the #Maap_Output_Type flag values.
+ * @param notify_callback Function of type #print_notify_callback_t that will handle printable results.
+ * @param callback_data Data to return with the callback.
*/
-void print_notify(Maap_Notify *mn, Maap_Output_Type outputType);
+void print_notify(Maap_Notify *mn, print_notify_callback_t notify_callback, void *callback_data);
#endif
diff --git a/daemons/maap/common/maap_iface.h b/daemons/maap/common/maap_iface.h
index 0ebe24aa..d388520a 100644
--- a/daemons/maap/common/maap_iface.h
+++ b/daemons/maap/common/maap_iface.h
@@ -101,10 +101,7 @@ typedef struct {
} Maap_Notify;
-/** MAAP Output Type Desired Flags */
-typedef enum {
- MAAP_OUTPUT_LOGGING = 0x01, /**< Send the results to the logging engine */
- MAAP_OUTPUT_USER_FRIENDLY = 0x02, /**< Send the results to stdout in user-friendly format */
-} Maap_Output_Type;
+/** Callback function used by #print_notify and #print_cmd_usage */
+typedef void (*print_notify_callback_t)(void *callback_data, int logLevel, const char *notifyText);
#endif
diff --git a/daemons/maap/common/maap_parse.c b/daemons/maap/common/maap_parse.c
index 0468f0a8..01140cbb 100644
--- a/daemons/maap/common/maap_parse.c
+++ b/daemons/maap/common/maap_parse.c
@@ -96,30 +96,13 @@ int parse_text_cmd(char *buf, Maap_Cmd *cmd) {
} else if (strncmp(argv[0], "exit", 4) == 0 && argc == 1) {
cmd->kind = MAAP_CMD_EXIT;
set_cmd = 1;
- } else {
- printf("Invalid command type\n");
}
}
- if (!set_cmd)
- {
- printf("input usage:\n");
- printf(" init [<range_base> <range_size>] - Initialize the MAAP daemon to recognize\n"
- " the specified range of addresses. If not specified, it uses\n"
- " range_base=0x%llx, range_size=0x%04x.\n", MAAP_DYNAMIC_POOL_BASE, MAAP_DYNAMIC_POOL_SIZE);
- printf(" reserve [<addr_base>] <addr_size> - Reserve a range of addresses of size\n"
- " <addr_size> in the initialized range. If <addr_base> is specified,\n"
- " that address base will be attempted first.\n");
- printf(" release <id> - Release the range of addresses with identifier ID\n");
- printf(" status <id> - Get the range of addresses associated with identifier ID\n");
- printf(" exit - Shutdown the MAAP daemon\n\n");
- return 0;
- }
-
- return 1;
+ return set_cmd;
}
-int parse_write(Maap_Client *mc, const void *sender, char *buf, Maap_Output_Type outputType) {
+int parse_write(Maap_Client *mc, const void *sender, char *buf, int *input_is_text) {
Maap_Cmd *bufcmd, cmd;
int rv = 0;
int retVal = 0;
@@ -132,12 +115,18 @@ int parse_write(Maap_Client *mc, const void *sender, char *buf, Maap_Output_Type
case MAAP_CMD_RELEASE:
case MAAP_CMD_STATUS:
case MAAP_CMD_EXIT:
+ if (input_is_text) { *input_is_text = 0; }
memcpy(&cmd, bufcmd, sizeof (Maap_Cmd));
rv = 1;
break;
default:
+ if (input_is_text) { *input_is_text = 1; }
memset(&cmd, 0, sizeof (Maap_Cmd));
rv = parse_text_cmd(buf, &cmd);
+ if (!rv) {
+ /* Unrecognized command. */
+ retVal = -1;
+ }
break;
}
@@ -180,16 +169,41 @@ int parse_write(Maap_Client *mc, const void *sender, char *buf, Maap_Output_Type
retVal = 1; /* Indicate that we should exit. */
break;
default:
- if ((outputType & MAAP_OUTPUT_LOGGING)) {
- MAAP_LOG_ERROR("Unrecognized input to parse_write");
- }
- if ((outputType & MAAP_OUTPUT_USER_FRIENDLY)) {
- printf("Unrecognized input to parse_write\n");
- }
- rv = 0;
+ MAAP_LOG_ERROR("Unrecognized input to parse_write");
+ retVal = -1;
break;
}
}
return retVal;
}
+
+void parse_usage(print_notify_callback_t print_callback, void *callback_data)
+{
+ char szOutput[100];
+ print_callback(callback_data, MAAP_LOG_LEVEL_INFO,
+ "Invalid command type");
+ print_callback(callback_data, MAAP_LOG_LEVEL_INFO,
+ "Input usage:");
+ print_callback(callback_data, MAAP_LOG_LEVEL_INFO,
+ " init [<range_base> <range_size>] - Initialize the MAAP daemon to recognize");
+ print_callback(callback_data, MAAP_LOG_LEVEL_INFO,
+ " the specified range of addresses. If not specified, it uses");
+ sprintf(szOutput,
+ " range_base=0x%llx, range_size=0x%04x.", MAAP_DYNAMIC_POOL_BASE, MAAP_DYNAMIC_POOL_SIZE);
+ print_callback(callback_data, MAAP_LOG_LEVEL_INFO, szOutput);
+ print_callback(callback_data, MAAP_LOG_LEVEL_INFO,
+ " reserve [<addr_base>] <addr_size> - Reserve a range of addresses of size");
+ print_callback(callback_data, MAAP_LOG_LEVEL_INFO,
+ " <addr_size> in the initialized range. If <addr_base> is specified,");
+ print_callback(callback_data, MAAP_LOG_LEVEL_INFO,
+ " that address base will be attempted first.");
+ print_callback(callback_data, MAAP_LOG_LEVEL_INFO,
+ " release <id> - Release the range of addresses with identifier ID");
+ print_callback(callback_data, MAAP_LOG_LEVEL_INFO,
+ " status <id> - Get the range of addresses associated with identifier ID");
+ print_callback(callback_data, MAAP_LOG_LEVEL_INFO,
+ " exit - Shutdown the MAAP daemon");
+ print_callback(callback_data, MAAP_LOG_LEVEL_INFO,
+ " "); // Blank line
+}
diff --git a/daemons/maap/common/maap_parse.h b/daemons/maap/common/maap_parse.h
index d92bc44f..c079e696 100644
--- a/daemons/maap/common/maap_parse.h
+++ b/daemons/maap/common/maap_parse.h
@@ -53,10 +53,18 @@ int parse_text_cmd(char *buf, Maap_Cmd *cmd);
* @param mc Pointer to the Maap_Client structure to use
* @param sender Sender information pointer used to track the entity requesting the command
* @param buf Binary or text data to parse
- * @param outputType One or more of the #Maap_Output_Type flag values.
+ * @param input_is_text Optional pointer for variable set to 1 if the input is text, 0 if the input is binary.
*
- * @return 1 if the exit command was received, 0 otherwise.
+ * @return 1 if the exit command was received, -1 for an unrecognized command, or 0 otherwise.
*/
-int parse_write(Maap_Client *mc, const void *sender, char *buf, Maap_Output_Type outputType);
+int parse_write(Maap_Client *mc, const void *sender, char *buf, int *input_is_text);
+
+/**
+ * Prints the usage expected for parsing.
+ *
+ * @param print_callback Function of type #print_notify_callback_t that will handle printable results.
+ * @param callback_data Data to return with the callback.
+ */
+void parse_usage(print_notify_callback_t print_callback, void *callback_data);
#endif /* MAAP_PARSE_H_ */
diff --git a/daemons/maap/linux/src/maap_daemon.c b/daemons/maap/linux/src/maap_daemon.c
index 2ebd6f82..ebfd409d 100644
--- a/daemons/maap/linux/src/maap_daemon.c
+++ b/daemons/maap/linux/src/maap_daemon.c
@@ -36,6 +36,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#include <ctype.h>
#include <unistd.h>
#include <signal.h>
#include <netdb.h>
@@ -71,11 +72,15 @@ static int act_as_client(const char *listenport);
static int act_as_server(const char *listenport, char *iface, int daemonize);
static int do_daemonize(void);
+static void log_print_notify_result(void *callback_data, int logLevel, const char *notifyText);
+static void display_print_notify_result(void *callback_data, int logLevel, const char *notifyText);
+static void send_print_notify_result(void *callback_data, int logLevel, const char *notifyText);
+
static const char *version_str =
"maap_daemon v" VERSION_STR "\n"
"Copyright (c) 2014-2015, VAYAVYA LABS PVT LTD\n"
- "Copyright (c) 2016, Harman International Industries, Inc.\n";
+ "Copyright (c) 2016-2017, Harman International Industries, Inc.\n";
static void usage(void)
{
@@ -132,11 +137,8 @@ int main(int argc, char *argv[])
if (daemonize)
{
fprintf(stderr, "Only one log file per server is supported\n");
+ free(logfile);
usage();
- if (logfile)
- {
- free(logfile);
- }
}
daemonize = 1;
logfile = strdup(optarg);
@@ -146,8 +148,8 @@ int main(int argc, char *argv[])
if (iface)
{
fprintf(stderr, "Only one interface per server is supported\n");
- usage();
free(iface);
+ usage();
}
iface = strdup(optarg);
break;
@@ -156,8 +158,8 @@ int main(int argc, char *argv[])
if (listenport)
{
fprintf(stderr, "Only one port per server is supported\n");
- usage();
free(listenport);
+ usage();
}
listenport = strdup(optarg);
break;
@@ -242,6 +244,7 @@ static int act_as_server(const char *listenport, char *iface, int daemonize)
int listener;
int clientfd[MAX_CLIENT_CONNECTIONS];
+ int client_wants_text[MAX_CLIENT_CONNECTIONS];
int i, nextclientindex;
fd_set master, read_fds;
@@ -300,7 +303,10 @@ static int act_as_server(const char *listenport, char *iface, int daemonize)
fdmax = listener;
}
- for (i = 0; i < MAX_CLIENT_CONNECTIONS; ++i) { clientfd[i] = -1; }
+ for (i = 0; i < MAX_CLIENT_CONNECTIONS; ++i) {
+ clientfd[i] = -1;
+ client_wants_text[i] = 0;
+ }
nextclientindex = 0;
@@ -346,20 +352,26 @@ static int act_as_server(const char *listenport, char *iface, int daemonize)
{
if ((int) notifysocket == -1) {
/* Just display the information for the user. */
- print_notify(&recvnotify, MAAP_OUTPUT_USER_FRIENDLY);
+ print_notify(&recvnotify, display_print_notify_result, NULL);
} else {
/* Log the result. */
- print_notify(&recvnotify, MAAP_OUTPUT_LOGGING);
+ print_notify(&recvnotify, log_print_notify_result, NULL);
/* Send the notification information to the client. */
for (i = 0; i < MAX_CLIENT_CONNECTIONS; ++i)
{
if (clientfd[i] == (int) notifysocket)
{
- if (send((int) notifysocket, &recvnotify, sizeof(recvnotify), 0) < 0)
- {
- /* Something went wrong. Assume the socket will be closed below. */
- MAAP_LOGF_ERROR("Error %d writing to client socket %d (%s)", errno, (int) notifysocket, strerror(errno));
+ if (client_wants_text[i]) {
+ // Send the friendly text notification to the socket.
+ print_notify(&recvnotify, send_print_notify_result, (void *) &(clientfd[i]));
+ } else {
+ // Send the raw notification to the socket.
+ if (send((int) notifysocket, &recvnotify, sizeof(recvnotify), 0) < 0)
+ {
+ /* Something went wrong. Assume the socket will be closed below. */
+ MAAP_LOGF_ERROR("Error %d writing to client socket %d (%s)", errno, (int) notifysocket, strerror(errno));
+ }
}
break;
}
@@ -486,11 +498,17 @@ static int act_as_server(const char *listenport, char *iface, int daemonize)
/* Process the command data (may be binary or text). */
memset(&recvcmd, 0, sizeof(recvcmd));
- if (parse_write(&mc, (const void *)(uintptr_t) -1, recvbuffer, MAAP_OUTPUT_USER_FRIENDLY))
+ int result = parse_write(&mc, (const void *)(uintptr_t) -1, recvbuffer, NULL);
+ if (result > 0)
{
/* Received a command to exit. */
exit_received = 1;
}
+ else if (result < 0)
+ {
+ /* Invalid command. Tell the user what valid commands are. */
+ parse_usage(display_print_notify_result, NULL);
+ }
}
}
@@ -522,11 +540,17 @@ static int act_as_server(const char *listenport, char *iface, int daemonize)
/* Process the command data (may be binary or text). */
memset(&recvcmd, 0, sizeof(recvcmd));
- if (parse_write(&mc, (const void *)(uintptr_t) clientfd[i], recvbuffer, MAAP_OUTPUT_LOGGING))
+ int result = parse_write(&mc, (const void *)(uintptr_t) clientfd[i], recvbuffer, &(client_wants_text[i]));
+ if (result > 0)
{
/* Received a command to exit. */
exit_received = 1;
}
+ else if (result < 0)
+ {
+ /* Invalid command. Tell the user what valid commands are. */
+ parse_usage(send_print_notify_result, (void *) &(clientfd[i]));
+ }
}
}
}
@@ -767,7 +791,7 @@ static int act_as_client(const char *listenport)
/* Process the response data (will be binary). */
if (recvbytes == sizeof(Maap_Notify))
{
- print_notify((Maap_Notify *) recvbuffer, MAAP_OUTPUT_USER_FRIENDLY);
+ print_notify((Maap_Notify *) recvbuffer, display_print_notify_result, NULL);
}
else
{
@@ -816,6 +840,9 @@ static int act_as_client(const char *listenport)
default:
memset(&recvcmd, 0, sizeof(Maap_Cmd));
rv = parse_text_cmd(recvbuffer, &recvcmd);
+ if (!rv) {
+ parse_usage(display_print_notify_result, NULL);
+ }
break;
}
@@ -876,3 +903,100 @@ static int do_daemonize(void)
return 0;
}
+
+static void log_print_notify_result(void *callback_data, int logLevel, const char *notifyText)
+{
+ switch (logLevel) {
+ case MAAP_LOG_LEVEL_ERROR:
+ MAAP_LOG_ERROR(notifyText);
+ break;
+ case MAAP_LOG_LEVEL_WARNING:
+ MAAP_LOG_WARNING(notifyText);
+ break;
+ case MAAP_LOG_LEVEL_INFO:
+ MAAP_LOG_INFO(notifyText);
+ break;
+ case MAAP_LOG_LEVEL_STATUS:
+ MAAP_LOG_STATUS(notifyText);
+ break;
+ case MAAP_LOG_LEVEL_DEBUG:
+ MAAP_LOG_DEBUG(notifyText);
+ break;
+ case MAAP_LOG_LEVEL_VERBOSE:
+ MAAP_LOG_VERBOSE(notifyText);
+ break;
+ }
+}
+
+static void format_print_notify_result(int logLevel, const char *notifyText, char *szOutputText)
+
+{
+ int i, nLastSpace;
+ int nInitial = -1;
+ char *pszOut = szOutputText;
+
+ /* Break the string up into one-line chunks.
+ * Note that tabs and newlines are not handled correctly. */
+ while (*notifyText) {
+ if (nInitial < 0) {
+ if (logLevel == MAAP_LOG_LEVEL_ERROR) {
+ strcpy(pszOut, "Error: ");
+ nInitial = (int) strlen(pszOut);
+ pszOut += nInitial;
+ } else if (logLevel == MAAP_LOG_LEVEL_WARNING) {
+ strcpy(pszOut, "Warning: ");
+ nInitial = (int) strlen(pszOut);
+ pszOut += nInitial;
+ } else {
+ nInitial = 0;
+ }
+ } else {
+ /* We already accounted for the initial text. */
+ nInitial = 0;
+ }
+
+ nLastSpace = -1;
+ for (i = 0; (i < MAAP_LOG_STDOUT_CONSOLE_WIDTH - nInitial || nLastSpace <= 0) && notifyText[i]; ++i) {
+ if (isspace(notifyText[i])) { nLastSpace = i; }
+ }
+ if (notifyText[i] == '\0') {
+ /* Print the remainder of the string. */
+ strcpy(pszOut, notifyText);
+ pszOut += strlen(pszOut);
+ *pszOut++ = '\n';
+ break;
+ }
+
+ /* Print the string up to the last space. */
+ for (i = 0; i < nLastSpace; ++i) {
+ *pszOut++ = *notifyText++;
+ }
+ *pszOut++ = '\n';
+
+ /* Go to the start of the next word in the string. */
+ while (isspace(*notifyText)) { notifyText++; }
+ }
+
+ *pszOut = '\0';
+}
+
+static void display_print_notify_result(void *callback_data, int logLevel, const char *notifyText)
+{
+ char szOutputText[ 300 ];
+
+ format_print_notify_result(logLevel, notifyText, szOutputText);
+ fputs(szOutputText, stdout);
+ fflush(stdout);
+}
+
+static void send_print_notify_result(void *callback_data, int logLevel, const char *notifyText)
+{
+ char szOutputText[ 300 ];
+
+ format_print_notify_result(logLevel, notifyText, szOutputText);
+ if (send(*(int *)callback_data, szOutputText, strlen(szOutputText), 0) < 0)
+ {
+ /* Something went wrong. Assume the socket will be closed below. */
+ MAAP_LOGF_ERROR("Error %d writing to client socket %d (%s)", errno, *(int *)callback_data, strerror(errno));
+ }
+}