Skip to content

Commit

Permalink
Improve command line parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlin96069 committed Nov 23, 2023
1 parent fa01a4e commit 11122be
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 36 deletions.
12 changes: 5 additions & 7 deletions docs/example.ninorc
Original file line number Diff line number Diff line change
@@ -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;"
81 changes: 52 additions & 29 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand All @@ -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) {
Expand Down

0 comments on commit 11122be

Please sign in to comment.