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); shell_write(SHELL_NEWLINE_STR SHELL_WELCOME_STR SHELL_NEWLINE_STR);
while (true) { while (true) {
int i, n, ret; int i, n, ret, lastopt;
char line[SHELL_MAX_LINE_LENGTH]; char line[SHELL_MAX_LINE_LENGTH];
char *args[SHELL_MAX_ARGUMENTS + 1]; char *args[SHELL_MAX_ARGUMENTS + 1];
char *ap, *tokp; char *ap, *tokp;
@ -452,11 +452,10 @@ int main(int argc, char *argv[], char *envp[]) {
} }
/* Adding the command name as-is.*/ /* Adding the command name as-is.*/
sglob_init(&sglob, 0); sglob_init(&sglob);
ret = sglob_add(&sglob, args[0]); ret = sglob_add(&sglob, args[0]);
if (ret == SGLOB_NOSPACE) { if (ret == SGLOB_NOSPACE) {
shell_error("msh: out of memory" SHELL_NEWLINE_STR); goto outofmem;
break;
} }
/* Arguments processing except those starting with a "-" which are /* 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]); ret = sglob_add(&sglob, args[i]);
} }
if (ret == SGLOB_NOSPACE) { if (ret == SGLOB_NOSPACE) {
shell_error("msh: out of memory" SHELL_NEWLINE_STR); goto outofmem;
break;
} }
} }
/* If no errors then executing the command.*/ /* Building the full arguments array.*/
if (ret != SGLOB_NOSPACE) { ret = sglob_build(&sglob, 0);
/* Building the full arguments array.*/ if (ret == SGLOB_NOSPACE) {
ret = sglob_build(&sglob); goto outofmem;
if (ret != 0) { }
} /* Sorting the glob in sub-groups delimited by option arguments, we don't
if (shell_execute(sglob.sgl_pathc, sglob.sgl_pathv)) { want arguments to move around options. The base file name is also
shell_error("msh: "); considered an option and not moved.*/
shell_error(args[0]); lastopt = 0;
shell_error(": command not found" SHELL_NEWLINE_STR); 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.*/ /* Freeing memory allocated during processing.*/
sglob_free(&sglob); 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. */ /* 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_buf = NULL;
psglob->sgl_next = NULL; psglob->sgl_next = NULL;
psglob->sgl_last = NULL; psglob->sgl_last = NULL;
@ -124,7 +123,7 @@ int sglob_add(sglob_t *psglob, const char *s) {
char *p; char *p;
int ret; int ret;
p = lsalloc(psglob, strlen(s)); p = lsalloc(psglob, strlen(s) + 1);
if (p != NULL) { if (p != NULL) {
strcpy(p, s); strcpy(p, s);
psglob->sgl_pathc++; psglob->sgl_pathc++;
@ -235,17 +234,16 @@ skiperr:
return ret; return ret;
} }
int sglob_build(sglob_t *psglob) { int sglob_build(sglob_t *psglob, size_t offs) {
lstring_t *lsp; lstring_t *lsp;
int i; int i;
psglob->sgl_pathv = malloc((psglob->sgl_pathc + psglob->sgl_offs + 1) * psglob->sgl_pathv = malloc((psglob->sgl_pathc + offs + 1) * sizeof (char *));
sizeof (char *));
if (psglob->sgl_pathv == NULL) { if (psglob->sgl_pathv == NULL) {
return SGLOB_NOSPACE; return SGLOB_NOSPACE;
} }
i = psglob->sgl_offs; i = offs;
lsp = psglob->sgl_next; lsp = psglob->sgl_next;
while (lsp != NULL) { while (lsp != NULL) {
psglob->sgl_pathv[i++] = lsp->string; psglob->sgl_pathv[i++] = lsp->string;
@ -256,6 +254,15 @@ int sglob_build(sglob_t *psglob) {
return 0; 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) { void sglob_free(sglob_t *psglob) {
lstring_t *lsp; lstring_t *lsp;

View File

@ -53,7 +53,6 @@ typedef struct lstring {
} lstring_t; } lstring_t;
typedef struct { typedef struct {
int sgl_offs;
lstring_t *sgl_next; lstring_t *sgl_next;
lstring_t *sgl_last; lstring_t *sgl_last;
char *sgl_buf; char *sgl_buf;
@ -72,10 +71,11 @@ typedef struct {
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #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_add(sglob_t *psglob, const char *s);
int sglob_match(sglob_t *psglob, const char *pattern); 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); void sglob_free(sglob_t *psglob);
#ifdef __cplusplus #ifdef __cplusplus
} }