From 11122bed42e23f677434031ad71c16b6398c1ed9 Mon Sep 17 00:00:00 2001 From: evanlin96069 <72735402+evanlin96069@users.noreply.github.com> Date: Thu, 23 Nov 2023 16:09:52 +0800 Subject: [PATCH] Improve command line parsing --- docs/example.ninorc | 12 +++---- src/config.c | 81 +++++++++++++++++++++++++++++---------------- 2 files changed, 57 insertions(+), 36 deletions(-) diff --git a/docs/example.ninorc b/docs/example.ninorc index bfb7065..3ea59da 100644 --- a/docs/example.ninorc +++ b/docs/example.ninorc @@ -1,15 +1,13 @@ # cvars # helpinfo 0 -tabsize 4 -whitespace 1 autoindent 1 -backspace 1 bracket 1 -trailing 1 -ignorecase 2 -syntax 1 -mouse 1 # color # color status.fg f3f3f3 # color status.bg 007acc + +# toggle autoindent +# alias id "id_off" +# alias id_on "autoindent 1; alias id id_off; echo autoindent on;" +# alias id_off "autoindent 0; alias id id_on; echo autoindent off;" diff --git a/src/config.c b/src/config.c index c06ddb7..7792c04 100644 --- a/src/config.c +++ b/src/config.c @@ -231,11 +231,11 @@ CON_COMMAND(crash, "Cause the editor to crash. (Debug!!)") { switch (crash_type) { case 0: - // SIGABRT + // SIGSEGV *(char*)0 = 0; break; case 1: - // SIGSEGV + // SIGABRT abort(); default: editorSetStatusMsg("Unknown crash type."); @@ -326,7 +326,7 @@ static CmdAlias* cmd_alias = NULL; static CmdAlias* findAlias(const char* name) { CmdAlias* a = cmd_alias; while (a) { - if (strcmp(name, a->name) == 0) { + if (strCaseCmp(name, a->name) == 0) { break; } a = a->next; @@ -380,7 +380,18 @@ CON_COMMAND(alias, "Alias a command.") { snprintf(a->value, size, "%s", cmd); } -static void executeCommand(EditorConCmdArgs args) { +static void parseLine(const char* cmd, int depth); + +static void executeCommand(EditorConCmdArgs args, int depth) { + if (args.argc < 1) + return; + + CmdAlias* a = findAlias(args.argv[0]); + if (a) { + parseLine(a->value, depth + 1); + return; + } + EditorConCmd* cmd = editorFindCmd(args.argv[0]); if (!cmd) { editorSetStatusMsg("Unknown command \"%s\".", args.argv[0]); @@ -400,34 +411,46 @@ static void parseLine(const char* cmd, int depth) { return; } - size_t size = strlen(cmd) + 1; - char* line = malloc_s(size); - snprintf(line, size, "%s", cmd); - - // remove comment - char* hash = strchr(line, '#'); - if (hash) - *hash = '\0'; - - char* token = strtok(line, " "); + // Command line parsing EditorConCmdArgs args = {0}; - for (int i = 0; token && i < 4; i++, args.argc++) { - snprintf(args.argv[i], COMMAND_MAX_LENGTH, "%s", token); - token = strtok(NULL, " "); - } - - free(line); - - if (args.argc < 1) - return; - - CmdAlias* a = findAlias(args.argv[0]); - if (a) { - parseLine(a->value, depth + 1); - return; + while (*cmd != '\0' && *cmd != '#') { + switch (*cmd) { + case '\t': + case ' ': + cmd++; + break; + + case '"': + cmd++; + for (int i = 0; *cmd != '\0' && *cmd != '"'; i++) { + args.argv[args.argc][i] = *cmd; + cmd++; + } + + if (*cmd == '"') { + cmd++; + } + args.argc++; + break; + + case ';': + executeCommand(args, depth); + memset(&args, 0, sizeof(EditorConCmdArgs)); + cmd++; + break; + + default: + for (int i = 0; + *cmd != '\0' && *cmd != '#' && *cmd != ';' && *cmd != ' '; + i++) { + args.argv[args.argc][i] = *cmd; + cmd++; + } + args.argc++; + } } - executeCommand(args); + executeCommand(args, depth); } bool editorLoadConfig(const char* path) {