Planning for expansion sorting, fixed a small allocation bug in sglob, removed an unnecessary field in sglob_t.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15456 27425a3e-05d8-49a3-a47f-9c15f0e5edd8
This commit is contained in:
Giovanni Di Sirio 2022-02-09 14:19:39 +00:00
parent 3cf08662ea
commit dfa77ef0d7
3 changed files with 45 additions and 27 deletions

View File

@ -416,7 +416,7 @@ int main(int argc, char *argv[], char *envp[]) {
shell_write(SHELL_NEWLINE_STR SHELL_WELCOME_STR SHELL_NEWLINE_STR);
while (true) {
int i, n, ret;
int i, n, ret, lastopt;
char line[SHELL_MAX_LINE_LENGTH];
char *args[SHELL_MAX_ARGUMENTS + 1];
char *ap, *tokp;
@ -452,11 +452,10 @@ int main(int argc, char *argv[], char *envp[]) {
}
/* Adding the command name as-is.*/
sglob_init(&sglob, 0);
sglob_init(&sglob);
ret = sglob_add(&sglob, args[0]);
if (ret == SGLOB_NOSPACE) {
shell_error("msh: out of memory" SHELL_NEWLINE_STR);
break;
goto outofmem;
}
/* Arguments processing except those starting with a "-" which are
@ -470,26 +469,38 @@ int main(int argc, char *argv[], char *envp[]) {
ret = sglob_add(&sglob, args[i]);
}
if (ret == SGLOB_NOSPACE) {
shell_error("msh: out of memory" SHELL_NEWLINE_STR);
break;
goto outofmem;
}
}
/* If no errors then executing the command.*/
if (ret != SGLOB_NOSPACE) {
/* Building the full arguments array.*/
ret = sglob_build(&sglob);
if (ret != 0) {
/* Building the full arguments array.*/
ret = sglob_build(&sglob, 0);
if (ret == SGLOB_NOSPACE) {
goto outofmem;
}
}
if (shell_execute(sglob.sgl_pathc, sglob.sgl_pathv)) {
shell_error("msh: ");
shell_error(args[0]);
shell_error(": command not found" SHELL_NEWLINE_STR);
}
/* Sorting the glob in sub-groups delimited by option arguments, we don't
want arguments to move around options. The base file name is also
considered an option and not moved.*/
lastopt = 0;
for (i = 1; i < n; i++) {
/* TODO */
(void)lastopt;
}
/* Executing the glob.*/
if (shell_execute(sglob.sgl_pathc, sglob.sgl_pathv)) {
shell_error("msh: ");
shell_error(args[0]);
shell_error(": command not found" SHELL_NEWLINE_STR);
}
/* Freeing memory allocated during processing.*/
sglob_free(&sglob);
continue;
outofmem:
shell_error("msh: out of memory" SHELL_NEWLINE_STR);
sglob_free(&sglob);
}
}

View File

@ -110,9 +110,8 @@ bool match(const char *pattern, const char *text) {
/* Module exported functions. */
/*===========================================================================*/
void sglob_init(sglob_t *psglob, size_t offs) {
void sglob_init(sglob_t *psglob) {
psglob->sgl_offs = offs;
psglob->sgl_buf = NULL;
psglob->sgl_next = NULL;
psglob->sgl_last = NULL;
@ -124,7 +123,7 @@ int sglob_add(sglob_t *psglob, const char *s) {
char *p;
int ret;
p = lsalloc(psglob, strlen(s));
p = lsalloc(psglob, strlen(s) + 1);
if (p != NULL) {
strcpy(p, s);
psglob->sgl_pathc++;
@ -235,17 +234,16 @@ skiperr:
return ret;
}
int sglob_build(sglob_t *psglob) {
int sglob_build(sglob_t *psglob, size_t offs) {
lstring_t *lsp;
int i;
psglob->sgl_pathv = malloc((psglob->sgl_pathc + psglob->sgl_offs + 1) *
sizeof (char *));
psglob->sgl_pathv = malloc((psglob->sgl_pathc + offs + 1) * sizeof (char *));
if (psglob->sgl_pathv == NULL) {
return SGLOB_NOSPACE;
}
i = psglob->sgl_offs;
i = offs;
lsp = psglob->sgl_next;
while (lsp != NULL) {
psglob->sgl_pathv[i++] = lsp->string;
@ -256,6 +254,15 @@ int sglob_build(sglob_t *psglob) {
return 0;
}
void sglob_sort(sglob_t *psglob, int start, int n) {
(void)psglob;
(void)start;
(void)n;
/* TODO */
}
void sglob_free(sglob_t *psglob) {
lstring_t *lsp;

View File

@ -53,7 +53,6 @@ typedef struct lstring {
} lstring_t;
typedef struct {
int sgl_offs;
lstring_t *sgl_next;
lstring_t *sgl_last;
char *sgl_buf;
@ -72,10 +71,11 @@ typedef struct {
#ifdef __cplusplus
extern "C" {
#endif
void sglob_init(sglob_t *psglob, size_t offs);
void sglob_init(sglob_t *psglob);
int sglob_add(sglob_t *psglob, const char *s);
int sglob_match(sglob_t *psglob, const char *pattern);
int sglob_build(sglob_t *psglob);
int sglob_build(sglob_t *psglob, size_t offs);
void sglob_sort(sglob_t *psglob, int start, int n);
void sglob_free(sglob_t *psglob);
#ifdef __cplusplus
}