mirror of
https://github.com/profanity-im/profanity.git
synced 2024-12-04 14:46:46 -05:00
Moved functions to parser.c, moved parser to tools
This commit is contained in:
parent
907beb55f6
commit
36265dde2f
@ -14,8 +14,8 @@ profanity_SOURCES = \
|
|||||||
src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \
|
src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \
|
||||||
src/ui/console.c src/ui/notifier.c src/ui/notifier.h \
|
src/ui/console.c src/ui/notifier.c src/ui/notifier.h \
|
||||||
src/command/command.h src/command/command.c src/command/history.c \
|
src/command/command.h src/command/command.c src/command/history.c \
|
||||||
src/command/history.h src/command/parser.c \
|
src/command/history.h src/tools/parser.c \
|
||||||
src/command/parser.h \
|
src/tools/parser.h \
|
||||||
src/tools/autocomplete.c src/tools/autocomplete.h \
|
src/tools/autocomplete.c src/tools/autocomplete.h \
|
||||||
src/tools/history.c src/tools/history.h \
|
src/tools/history.c src/tools/history.h \
|
||||||
src/tools/tinyurl.c src/tools/tinyurl.h \
|
src/tools/tinyurl.c src/tools/tinyurl.h \
|
||||||
@ -40,8 +40,8 @@ tests_testsuite_SOURCES = \
|
|||||||
src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \
|
src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \
|
||||||
src/ui/console.c src/ui/notifier.c src/ui/notifier.h \
|
src/ui/console.c src/ui/notifier.c src/ui/notifier.h \
|
||||||
src/command/command.h src/command/command.c src/command/history.c \
|
src/command/command.h src/command/command.c src/command/history.c \
|
||||||
src/command/history.h src/command/parser.c \
|
src/command/history.h src/tools/parser.c \
|
||||||
src/command/parser.h \
|
src/tools/parser.h \
|
||||||
src/tools/autocomplete.c src/tools/autocomplete.h \
|
src/tools/autocomplete.c src/tools/autocomplete.h \
|
||||||
src/tools/history.c src/tools/history.h \
|
src/tools/history.c src/tools/history.h \
|
||||||
src/tools/tinyurl.c src/tools/tinyurl.h \
|
src/tools/tinyurl.c src/tools/tinyurl.h \
|
||||||
|
@ -30,7 +30,6 @@
|
|||||||
#include "chat_session.h"
|
#include "chat_session.h"
|
||||||
#include "command/command.h"
|
#include "command/command.h"
|
||||||
#include "command/history.h"
|
#include "command/history.h"
|
||||||
#include "command/parser.h"
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "config/accounts.h"
|
#include "config/accounts.h"
|
||||||
#include "config/preferences.h"
|
#include "config/preferences.h"
|
||||||
@ -41,6 +40,7 @@
|
|||||||
#include "muc.h"
|
#include "muc.h"
|
||||||
#include "profanity.h"
|
#include "profanity.h"
|
||||||
#include "tools/autocomplete.h"
|
#include "tools/autocomplete.h"
|
||||||
|
#include "tools/parser.h"
|
||||||
#include "tools/tinyurl.h"
|
#include "tools/tinyurl.h"
|
||||||
#include "ui/ui.h"
|
#include "ui/ui.h"
|
||||||
#include "xmpp/xmpp.h"
|
#include "xmpp/xmpp.h"
|
||||||
|
@ -24,7 +24,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "autocomplete.h"
|
#include "tools/autocomplete.h"
|
||||||
|
#include "tools/parser.h"
|
||||||
|
|
||||||
struct autocomplete_t {
|
struct autocomplete_t {
|
||||||
GSList *items;
|
GSList *items;
|
||||||
@ -260,80 +261,6 @@ autocomplete_param_with_ac(char *input, int *size, char *command,
|
|||||||
return auto_msg;
|
return auto_msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
|
||||||
_count_tokens(char *string)
|
|
||||||
{
|
|
||||||
int num_tokens = 0;
|
|
||||||
|
|
||||||
// if no quotes, use glib
|
|
||||||
if (g_strrstr(string, "\"") == NULL) {
|
|
||||||
gchar **tokens = g_strsplit(string, " ", 0);
|
|
||||||
num_tokens = g_strv_length(tokens);
|
|
||||||
g_strfreev(tokens);
|
|
||||||
|
|
||||||
// else count tokens including quoted
|
|
||||||
} else {
|
|
||||||
int length = strlen(string);
|
|
||||||
int i = 0;
|
|
||||||
gboolean in_quotes = FALSE;
|
|
||||||
|
|
||||||
// include first token
|
|
||||||
num_tokens++;
|
|
||||||
|
|
||||||
for (i = 0; i < length; i++) {
|
|
||||||
if (string[i] == ' ') {
|
|
||||||
if (!in_quotes) {
|
|
||||||
num_tokens++;
|
|
||||||
}
|
|
||||||
} else if (string[i] == '"') {
|
|
||||||
if (in_quotes) {
|
|
||||||
in_quotes = FALSE;
|
|
||||||
} else {
|
|
||||||
in_quotes = TRUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return num_tokens;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
|
||||||
_get_start(char *string, int tokens)
|
|
||||||
{
|
|
||||||
char *result_str = NULL;
|
|
||||||
int num_tokens = 0;
|
|
||||||
int length = strlen(string);
|
|
||||||
int i = 0;
|
|
||||||
gboolean in_quotes = FALSE;
|
|
||||||
GString *result = g_string_new("");
|
|
||||||
|
|
||||||
// include first token
|
|
||||||
num_tokens++;
|
|
||||||
|
|
||||||
for (i = 0; i < length; i++) {
|
|
||||||
if (num_tokens < tokens) {
|
|
||||||
g_string_append_c(result, string[i]);
|
|
||||||
}
|
|
||||||
if (string[i] == ' ') {
|
|
||||||
if (!in_quotes) {
|
|
||||||
num_tokens++;
|
|
||||||
}
|
|
||||||
} else if (string[i] == '"') {
|
|
||||||
if (in_quotes) {
|
|
||||||
in_quotes = FALSE;
|
|
||||||
} else {
|
|
||||||
in_quotes = TRUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result_str = result->str;
|
|
||||||
g_string_free(result, FALSE);
|
|
||||||
|
|
||||||
return result_str;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *
|
char *
|
||||||
autocomplete_param_no_with_func(char *input, int *size, char *command,
|
autocomplete_param_no_with_func(char *input, int *size, char *command,
|
||||||
int arg_number, autocomplete_func func)
|
int arg_number, autocomplete_func func)
|
||||||
@ -353,11 +280,11 @@ autocomplete_param_no_with_func(char *input, int *size, char *command,
|
|||||||
g_strstrip(inp_cpy);
|
g_strstrip(inp_cpy);
|
||||||
|
|
||||||
// count tokens properly
|
// count tokens properly
|
||||||
int num_tokens = _count_tokens(inp_cpy);
|
int num_tokens = count_tokens(inp_cpy);
|
||||||
|
|
||||||
// if correct number of tokens, then candidate for autocompletion of last param
|
// if correct number of tokens, then candidate for autocompletion of last param
|
||||||
if (num_tokens == arg_number) {
|
if (num_tokens == arg_number) {
|
||||||
gchar *start_str = _get_start(inp_cpy, arg_number);
|
gchar *start_str = get_start(inp_cpy, arg_number);
|
||||||
gchar *comp_str = g_strdup(&inp_cpy[strlen(start_str)]);
|
gchar *comp_str = g_strdup(&inp_cpy[strlen(start_str)]);
|
||||||
|
|
||||||
// autocomplete param
|
// autocomplete param
|
||||||
|
@ -271,3 +271,79 @@ parse_args_with_freetext(const char * const inp, int min, int max)
|
|||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
count_tokens(char *string)
|
||||||
|
{
|
||||||
|
int num_tokens = 0;
|
||||||
|
|
||||||
|
// if no quotes, use glib
|
||||||
|
if (g_strrstr(string, "\"") == NULL) {
|
||||||
|
gchar **tokens = g_strsplit(string, " ", 0);
|
||||||
|
num_tokens = g_strv_length(tokens);
|
||||||
|
g_strfreev(tokens);
|
||||||
|
|
||||||
|
// else count tokens including quoted
|
||||||
|
} else {
|
||||||
|
int length = strlen(string);
|
||||||
|
int i = 0;
|
||||||
|
gboolean in_quotes = FALSE;
|
||||||
|
|
||||||
|
// include first token
|
||||||
|
num_tokens++;
|
||||||
|
|
||||||
|
for (i = 0; i < length; i++) {
|
||||||
|
if (string[i] == ' ') {
|
||||||
|
if (!in_quotes) {
|
||||||
|
num_tokens++;
|
||||||
|
}
|
||||||
|
} else if (string[i] == '"') {
|
||||||
|
if (in_quotes) {
|
||||||
|
in_quotes = FALSE;
|
||||||
|
} else {
|
||||||
|
in_quotes = TRUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return num_tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *
|
||||||
|
get_start(char *string, int tokens)
|
||||||
|
{
|
||||||
|
char *result_str = NULL;
|
||||||
|
int num_tokens = 0;
|
||||||
|
int length = strlen(string);
|
||||||
|
int i = 0;
|
||||||
|
gboolean in_quotes = FALSE;
|
||||||
|
GString *result = g_string_new("");
|
||||||
|
|
||||||
|
// include first token
|
||||||
|
num_tokens++;
|
||||||
|
|
||||||
|
for (i = 0; i < length; i++) {
|
||||||
|
if (num_tokens < tokens) {
|
||||||
|
g_string_append_c(result, string[i]);
|
||||||
|
}
|
||||||
|
if (string[i] == ' ') {
|
||||||
|
if (!in_quotes) {
|
||||||
|
num_tokens++;
|
||||||
|
}
|
||||||
|
} else if (string[i] == '"') {
|
||||||
|
if (in_quotes) {
|
||||||
|
in_quotes = FALSE;
|
||||||
|
} else {
|
||||||
|
in_quotes = TRUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result_str = result->str;
|
||||||
|
g_string_free(result, FALSE);
|
||||||
|
|
||||||
|
return result_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -27,5 +27,7 @@
|
|||||||
|
|
||||||
gchar** parse_args(const char * const inp, int min, int max);
|
gchar** parse_args(const char * const inp, int min, int max);
|
||||||
gchar** parse_args_with_freetext(const char * const inp, int min, int max);
|
gchar** parse_args_with_freetext(const char * const inp, int min, int max);
|
||||||
|
int count_tokens(char *string);
|
||||||
|
char* get_start(char *string, int tokens);
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -1,7 +1,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <head-unit.h>
|
#include <head-unit.h>
|
||||||
#include "command/parser.h"
|
#include "tools/parser.h"
|
||||||
|
|
||||||
void
|
void
|
||||||
parse_null_returns_null(void)
|
parse_null_returns_null(void)
|
||||||
|
Loading…
Reference in New Issue
Block a user