diff options
author | Michael J. Chudobiak <mjc@avtechpulse.com> | 2012-08-15 10:48:15 -0400 |
---|---|---|
committer | Michael J. Chudobiak <mjc@avtechpulse.com> | 2012-08-15 10:48:15 -0400 |
commit | dc67e4106c45efaf0d6994805bdccab20ad5ca77 (patch) | |
tree | 7433721623ac859cf1d614d6a64c2a4557e70a5e /parser.c | |
parent | a5e307c120a7950f4e469d0c8ca696890adb40a2 (diff) |
better filtering of input to ensure compound messages work
Diffstat (limited to 'parser.c')
-rw-r--r-- | parser.c | 46 |
1 files changed, 42 insertions, 4 deletions
@@ -20,6 +20,7 @@ static int process_float_param (char *parameter, float *value, float min_val, fl static int check_channel_ok (int channel, int enabled_channels, char chankey); static int Parser_id_word(char *id_me, int *channel, int *with_id_code); static int Parser_find_commands(int commands[], int command_depth); +static gchar* filter_input (gchar *raw_in); static int Parser_get_unit(char **parameter, char **units); static int Parser_channel (int *channel,int with_id_code,int routine_num); static int Go_freq_32_33(gchar** response, int channel, char *parameter,char *units,int command_type); @@ -495,6 +496,46 @@ static int Parser_get_unit(char **parameter, char **units) return units_present; } +static gchar* filter_input (gchar *raw_in) +{ + g_strstrip (raw_in); + gchar *step1 = g_strdup (raw_in); + + // replace multiple whitespace with single space + GRegex *strip_space = g_regex_new ("\\s+", 0, 0, NULL); + gchar *step2 = g_regex_replace_literal (strip_space, step1, -1, 0, " ", 0, NULL); + g_regex_unref (strip_space); + g_free (step1); + + // remove spaces between ; and : in compound messages + GRegex *colon_space = g_regex_new (";\\s+:", 0, 0, NULL); + gchar *step3 = g_regex_replace_literal (colon_space, step2, -1, 0, ";:", 0, NULL); + g_regex_unref (colon_space); + g_free (step2); + + // remove spaces before and after semicolons in compound messages + GRegex *semi_space = g_regex_new ("\\s*;\\s*", 0, 0, NULL); + gchar *step4 = g_regex_replace_literal (semi_space, step3, -1, 0, ";", 0, NULL); + g_regex_unref (semi_space); + g_free (step3); + + // remove semicolon at end + GRegex *end_semi = g_regex_new (";$", 0, 0, NULL); + gchar *step5 = g_regex_replace_literal (end_semi, step4, -1, 0, "", 0, NULL); + g_regex_unref (end_semi); + g_free (step4); + + // last step may leave hanging whitespace at end + g_strstrip (step5); + + // add single space to terminate + gchar *filt = g_strdup_printf ("%s ", step5); + g_free (step5); + + return filt; +} + + void Parser_main (char *raw_in, int interactive_terminal, void(*cbfunc)(gpointer, gchar *), gpointer user_data) { int in_pos; /* this identifies the single character of in being processed */ @@ -520,10 +561,7 @@ void Parser_main (char *raw_in, int interactive_terminal, void(*cbfunc)(gpointer int channel; int with_id_code; - g_strstrip (raw_in); - - // add white space to the end of the input string - FIXME? - gchar *in = g_strdup_printf ("%s ", raw_in); + gchar *in = filter_input (raw_in); in_pos = 0; /* start at the beginning of the input string */ semicolon_found = 0; |