#define MAXBUILTINS 50
int main(void) {
- int listfd, l;
+ int listfd, d;
DIR *dir;
- char *src[MAXBUILTINS + 2 + 1], **p;
+ char *srcs[MAXBUILTINS + 2 + 1], **src, *decl;
struct dirent *entry;
build("./");
dprintf(listfd, "#include <stddef.h>\n\n#include \"builtin.h\"\n"
"#include \"list.h\"\n\n");
- p = src;
+ src = srcs;
errno = 0;
while ((entry = readdir(dir))) {
if (strcmp(entry->d_name, "build.c") == 0
- || !(*p = strrchr(entry->d_name, '.')) || strcmp(*p, ".c") != 0)
+ || !(*src = strrchr(entry->d_name, '.')) || strcmp(*src, ".c") != 0)
continue;
- if (!(*p = strdup(entry->d_name)))
+ if (!(*src = strdup(entry->d_name)))
err(EXIT_FAILURE, "Unable to duplicate directory entry");
- (*p)[strlen(*p) - 2] = '\0';
- if (p - src == 2 + MAXBUILTINS + 1)
+ (*src)[strlen(*src) - 2] = '\0';
+ if (src - srcs == 2 + MAXBUILTINS + 1)
errx(EXIT_FAILURE, "Unable to add %s built-in, maximum reached (%d)",
- *p, MAXBUILTINS);
- if (strcmp(*p, "builtin") != 0 && strcmp(*p, "list") != 0)
- dprintf(listfd, "extern BUILTIN(%s);\n", *p);
- ++p;
+ *src, MAXBUILTINS);
+ if (strcmp(*src, "builtin") != 0 && strcmp(*src, "list") != 0)
+ dprintf(listfd, "BUILTIN(%s);\n", *src);
+ ++src;
}
if (errno) err(EXIT_FAILURE, "Unable to read from current directory");
+ *src = NULL;
- *p = "struct builtin builtins[] = {";
- l = (int)strlen(*p);
- dprintf(listfd, "\n%s", *p);
- *p = NULL;
- for (p = src; *p; ++p)
- if (strcmp(*p, "builtin") != 0 && strcmp(*p, "list") != 0)
- dprintf(listfd, "{\"%s\", %s},\n%*s", *p, *p, l, "");
+ decl = "struct builtin builtins[] = {";
+ d = (int)strlen(decl);
+ dprintf(listfd, "\n%s", decl);
+ for (src = srcs; *src; ++src)
+ if (strcmp(*src, "builtin") != 0 && strcmp(*src, "list") != 0)
+ dprintf(listfd, "{\"%s\", %s},\n%*s", *src, *src, d, "");
dprintf(listfd, "{NULL}};\n");
if (closedir(dir) == -1)
if (close(listfd) == -1) err(EXIT_FAILURE, "Unable to close `list.c'");
cflags = LIST("-I../");
- for (p = src; *p; ++p) compile(*p);
- load('s', "../builtins", src);
+ for (src = srcs; *src; ++src) compile(*src);
+ load('s', "../builtins", srcs);
- for (p = src; *p; ++p) free(*p);
+ for (src = srcs; *src; ++src) free(*src);
return EXIT_SUCCESS;
}
};
int stringinput(struct context *c) {
- char *start;
+ char *end;
size_t l;
- if (!*c->string) {
+ if (!c->string[0]) {
if (c->script && munmap(c->map, c->maplen) == -1)
note("Unable to unmap memory associated with `%s'", c->script);
return 0;
}
- start = c->string;
- while (*c->string && *c->string != '\n') ++c->string;
- l = c->string - start;
- if (*c->string == '\n') ++c->string;
+ end = c->string;
+ while (*end && *end != '\n') ++end;
+ l = end - c->string;
+ while (*end == '\n') ++end; // scriptinput() repeatedly uses stringinput()
if (l > MAXCHARS) {
note("Line too long, exceeds %d character limit", MAXCHARS);
return 0;
}
- strncpy(c->buffer, start, l);
+ strncpy(c->buffer, c->string, l);
c->buffer[l] = ';';
c->buffer[l + 1] = '\0';
+ c->string = end;
return 1;
}
if (c->r->mode) {
switch (*c->r->oldname) {
case '&':
- if ((l = strtol(++c->r->oldname, &stlend, 10)) < 0 || l > INT_MAX || *stlend) {
+ if ((l = strtol(++c->r->oldname, &stlend, 10)) < 0 || l > INT_MAX
+ || *stlend) {
case '\0':
note("Invalid syntax for file redirection");
return quit(c);
globbing = 0;
*c->t = c->b;
- } else if (!c->alias && c->t == c->tokens && (sub = getalias(*c->tokens)) || globbing) {
+ } else if (!c->alias && c->t == c->tokens && (sub = getalias(c->tokens[0]))
+ || globbing) {
if (globbing) {
globflags = GLOB_MARK;
if (prevsublen) globflags |= GLOB_APPEND;
if (term != ' ') {
if (c->t != c->tokens) {
*c->t = NULL;
- strcpy(c->current.name, *c->tokens);
+ strcpy(c->current.name, c->tokens[0]);
} else c->t = NULL;
if (c->r == c->redirects) c->r = NULL;
switch (term) {