Skip to content

Commit

Permalink
kerl: added new function for citation handling only, without argc/arg…
Browse files Browse the repository at this point in the history
…v stuff
  • Loading branch information
kallewoof committed Aug 20, 2018
1 parent f55a0f3 commit 29097ed
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
36 changes: 36 additions & 0 deletions kerl/kerl.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,42 @@ void kerl_free_argcv(size_t argc, char **argv)
free(argv);
}

int kerl_process_citation(const char* argstring, size_t* bytesOut, char** argsOut) {
register int i, j = 0;
char *line = NULL, ch, *buf, quot = 0;
size_t bufcap = strlen(argstring) + 1;
buf = malloc(bufcap);
while (1) {
if (bufcap >= j - 2) { bufcap *= 2; buf = realloc(buf, bufcap); }
for (i = 0; argstring[i]; i++) {
ch = argstring[i];
if (quot) {
if (ch == quot) quot = 0;
} else if (ch == '\'' || ch == '"') {
quot = ch;
}
buf[j++] = ch;
}
if (line) free(line);
#ifdef HAVE_LIBREADLINE
if (quot) {
buf[j++] = '\n';
line = readline(quot == '"' ? "dquote> " : quot == '\'' ? "quote> " : "> ");
if (!line) { printf("\n"); free(buf); *bytesOut = 0; *argsOut = NULL; return -1; }
argstring = line; // preserve whitespace as we are quoting
} else break;
#else
break;
#endif // HAVE_LIBREADLINE
}

buf[j] = 0;
*bytesOut = j;
*argsOut = buf;

return 0;
}

/* **************************************************************** */
/* */
/* Interface to Readline Completion */
Expand Down
5 changes: 3 additions & 2 deletions kerl/kerl.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ void kerl_set_history_file(const char *path);
void kerl_set_repeat_on_empty(int flag);
void kerl_set_comment_char(char commentchar);
void kerl_register_fallback(kerl_bindable func);
int kerl_make_argcv(const char *argstring, size_t *argcOut, char ***argvOut);
int kerl_make_argcv_escape(const char *argstring, size_t *argcOut, char ***argvOut, char escape);
int kerl_make_argcv(const char* argstring, size_t *argcOut, char*** argvOut);
int kerl_make_argcv_escape(const char* argstring, size_t *argcOut, char*** argvOut, char escape);
void kerl_free_argcv(size_t argc, char **argv);
int kerl_process_citation(const char* argstring, size_t* bytesOut, char** argsOut);

/* sensitivity is used to skip certain inputs as sensitive, such as
password data or similar. enabling sensitivity incurs a slight
Expand Down

0 comments on commit 29097ed

Please sign in to comment.