From e53ccf61f25fa6f2642a0c492e40eef92bd50431 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Tue, 4 Nov 2014 23:19:39 +0100 Subject: [PATCH] Refactor: replace variables named 'new' with non-reserved word. The word 'new' is a reserved keyword in C++ and C#, replacing it with something else is one step further to making it possible to build editline with a C++ compiler. Signed-off-by: Joachim Nilsson --- src/complete.c | 22 +++++++++--------- src/editline.c | 60 ++++++++++++++++++++++++++++++-------------------- 2 files changed, 47 insertions(+), 35 deletions(-) diff --git a/src/complete.c b/src/complete.c index 96230e9..8ec0551 100644 --- a/src/complete.c +++ b/src/complete.c @@ -37,7 +37,7 @@ static int compare(const void *p1, const void *p2) static int FindMatches(char *dir, char *file, char ***avp) { char **av; - char **new; + char **word; char *p; DIR *dp; DIRENTRY *ep; @@ -69,16 +69,16 @@ static int FindMatches(char *dir, char *file, char ***avp) } if ((ac % MEM_INC) == 0) { - new = malloc(sizeof(char *) * (ac + MEM_INC)); - if (!new) { + word = malloc(sizeof(char *) * (ac + MEM_INC)); + if (!word) { total = 0; break; } if (ac) { - memcpy(new, av, ac * sizeof(char **)); + memcpy(word, av, ac * sizeof(char **)); free(av); } - *avp = av = new; + *avp = av = word; } if ((av[ac] = strdup(p)) == NULL) { @@ -157,7 +157,7 @@ char *el_filename_complete(char *pathname, int *match) char **av; char *dir; char *file; - char *new; + char *path; char *p; size_t ac; size_t end; @@ -185,11 +185,11 @@ char *el_filename_complete(char *pathname, int *match) if (p) { memcpy(p, av[0] + len, j); len = strlen(dir) + strlen(av[0]) + 2; - new = malloc(sizeof(char) * len); - if (new) { - snprintf(new, len, "%s/%s", dir, av[0]); - rl_add_slash(new, p); - free(new); + path = malloc(sizeof(char) * len); + if (path) { + snprintf(path, len, "%s/%s", dir, av[0]); + rl_add_slash(path, p); + free(path); } } } else { diff --git a/src/editline.c b/src/editline.c index a706368..f6bbe77 100644 --- a/src/editline.c +++ b/src/editline.c @@ -467,24 +467,27 @@ static el_status_t insert_string(const char *p) { size_t len; int i; - char *new; + char *line; char *q; len = strlen(p); if (rl_end + len >= Length) { - new = malloc(sizeof(char) * (Length + len + MEM_INC)); - if (!new) + line = malloc(sizeof(char) * (Length + len + MEM_INC)); + if (!line) return CSstay; + if (Length) { - memcpy(new, rl_line_buffer, Length); + memcpy(line, rl_line_buffer, Length); free(rl_line_buffer); } - rl_line_buffer = new; + + rl_line_buffer = line; Length += len + MEM_INC; } for (q = &rl_line_buffer[rl_point], i = rl_end - rl_point; --i >= 0; ) q[len + i] = q[i]; + memcpy(&rl_line_buffer[rl_point], p, len); rl_end += len; rl_line_buffer[rl_end] = '\0'; @@ -1275,9 +1278,9 @@ int write_history(const char *filename) if (fp) { int i = 0; - while (i < H.Size) { + while (i < H.Size) fprintf(fp, "%s\n", H.Lines[i++]); - } + fclose(fp); return 0; @@ -1294,7 +1297,7 @@ int write_history(const char *filename) char *el_find_word(void) { char *p, *q; - char *new; + char *word; size_t len; p = &rl_line_buffer[rl_point]; @@ -1311,11 +1314,11 @@ char *el_find_word(void) } len = rl_point - (p - rl_line_buffer) + 1; - new = malloc(sizeof(char) * len); - if (!new) + word = malloc(sizeof(char) * len); + if (!word) return NULL; - q = new; + q = word; while (p < &rl_line_buffer[rl_point]) { if (*p == '\\') { if (++p == &rl_line_buffer[rl_point]) @@ -1325,7 +1328,7 @@ char *el_find_word(void) } *q = '\0'; - return new; + return word; } static el_status_t c_possible(void) @@ -1353,7 +1356,7 @@ static el_status_t c_possible(void) static el_status_t c_complete(void) { char *p, *q; - char *word, *new; + char *word, *string; size_t len; int unique; el_status_t s = CSdone; @@ -1368,11 +1371,13 @@ static el_status_t c_complete(void) if (p) { len = strlen(p); word = p; - new = q = malloc(sizeof(char) * (2 * len + 1)); - if (!new) { + + string = q = malloc(sizeof(char) * (2 * len + 1)); + if (!string) { free(word); return CSstay; } + while (*p) { if ((*p < ' ' || strchr(SEPS, *p) != NULL) && (!unique || p[1] != 0)) { @@ -1382,17 +1387,20 @@ static el_status_t c_complete(void) } *q = '\0'; free(word); + if (len > 0) { - s = insert_string(new); + s = insert_string(string); #ifdef CONFIG_ANNOYING_NOISE if (!unique) el_ring_bell(); #endif } - free(new); + free(string); + if (len > 0) return s; } + return c_possible(); } @@ -1542,7 +1550,7 @@ static int argify(char *line, char ***avp) { char *c; char **p; - char **new; + char **arg; int ac; int i; @@ -1559,17 +1567,19 @@ static int argify(char *line, char ***avp) for (ac = 0, p[ac++] = c; *c && *c != '\n'; ) { if (isspace(*c)) { *c++ = '\0'; + if (*c && *c != '\n') { if (ac + 1 == i) { - new = malloc(sizeof(char *) * (i + MEM_INC)); - if (!new) { + arg = malloc(sizeof(char *) * (i + MEM_INC)); + if (!arg) { p[ac] = NULL; return ac; } - memcpy(new, p, i * sizeof(char **)); + + memcpy(arg, p, i * sizeof(char **)); i += MEM_INC; free(p); - *avp = p = new; + *avp = p = arg; } p[ac++] = c; } @@ -1577,6 +1587,7 @@ static int argify(char *line, char ***avp) c++; } } + *c = '\0'; p[ac] = NULL; @@ -1593,10 +1604,11 @@ static el_status_t last_argument(void) if (H.Size == 1 || (p = (char *)H.Lines[H.Size - 2]) == NULL) return el_ring_bell(); - if ((p = strdup(p)) == NULL) + p = strdup(p); + if (!p) return CSstay; - ac = argify(p, &av); + ac = argify(p, &av); if (Repeat != NO_ARG) s = Repeat < ac ? insert_string(av[Repeat]) : el_ring_bell(); else