summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@gmail.com>2014-10-22 19:03:40 +0000
committerRichard Maw <richard.maw@codethink.co.uk>2014-10-24 14:26:04 +0000
commita54e12e1ab19c3e952d9c76b3aed59738a4af98c (patch)
tree4d2434d96fd909336ec381f5f7e19f730f007811
parent694a481360edeb0146c02362385af59f0371eb97 (diff)
downloadmorph-a54e12e1ab19c3e952d9c76b3aed59738a4af98c.tar.gz
test-shell: Allow to be used in system-integrations
It now works when passed a file, rather than a command to run, and ignores comments and set commands, so the generated preamble is accepted.
-rw-r--r--scripts/test-shell.c88
1 files changed, 49 insertions, 39 deletions
diff --git a/scripts/test-shell.c b/scripts/test-shell.c
index d1a10e5b..7e600388 100644
--- a/scripts/test-shell.c
+++ b/scripts/test-shell.c
@@ -118,53 +118,63 @@ int copy_file_objects(FILE *source, FILE *target) {
return ferror(source) ? -1 : 0;
}
-int main(int argc, char *argv[]) {
+int run_commands(FILE *cmdstream){
int ret = 1;
- if (argc != 3 || strcmp(argv[1], "-c") != 0) {
- fprintf(stderr, "Usage: %s -c COMMAND\n", argv[0]);
- return 1;
- }
- size_t cmdlen = strlen(argv[2]);
- FILE *cmdstream = fmemopen(argv[2], cmdlen, "r");
- {
- ssize_t read;
- size_t len = 0;
- char *line = NULL;
+ ssize_t read;
+ size_t len = 0;
+ char *line = NULL;
- ret = 0;
- while ((read = getline(&line, &len, cmdstream)) != -1) {
- if (line[read - 1] == '\n') line[read - 1] = '\0';
- if (strcmp(line, "copy files") == 0) {
- /* Recursively copy contents of current dir to DESTDIR */
- if (nftw(".", copy_entry, 20, FTW_PHYS)) {
- ret = 1;
- break;
- }
- } else if (strcmp(line, "false") == 0 ||
- strstr(line, "false ") == line) {
+ ret = 0;
+ while ((read = getline(&line, &len, cmdstream)) != -1) {
+ if (line[read - 1] == '\n') line[read - 1] = '\0';
+ if (strcmp(line, "copy files") == 0) {
+ /* Recursively copy contents of current dir to DESTDIR */
+ if (nftw(".", copy_entry, 20, FTW_PHYS)) {
ret = 1;
break;
- } else if (strstr(line, "echo ") == line) {
- if (puts(line + sizeof("echo ") - 1) == EOF){
- perror("echo");
- ret = 1;
- break;
- }
- } else if (strstr(line, "create file ") == line) {
- char const *filename = line + sizeof("create file ") -1;
- FILE *outfile = fopen(filename, "w");
- if (copy_file_objects(cmdstream, outfile) < 0) {
- ret = 1;
- fclose(outfile);
- break;
- }
+ }
+ } else if (strcmp(line, "false") == 0 ||
+ strstr(line, "false ") == line) {
+ ret = 1;
+ break;
+ } else if (strstr(line, "echo ") == line) {
+ if (puts(line + sizeof("echo ") - 1) == EOF){
+ perror("echo");
+ ret = 1;
+ break;
+ }
+ } else if (strstr(line, "create file ") == line) {
+ char const *filename = line + sizeof("create file ") -1;
+ FILE *outfile = fopen(filename, "w");
+ if (copy_file_objects(cmdstream, outfile) < 0) {
+ ret = 1;
fclose(outfile);
- } else {
- ret = 127;
break;
}
+ fclose(outfile);
+ } else if (line[0] == '#' || strstr(line, "set ") == line) {
+ /* Comments and set commands are ignored */
+ continue;
+ } else {
+ fprintf(stderr, "Unrecognized command: %s\n", line);
+ ret = 127;
+ break;
}
- free(line);
}
+ free(line);
return ret;
}
+
+int main(int argc, char *argv[]) {
+ if (argc == 3 && strcmp(argv[1], "-c") == 0) {
+ size_t cmdlen = strlen(argv[2]);
+ FILE *cmdstream = fmemopen(argv[2], cmdlen, "r");
+ return run_commands(cmdstream);
+ } else if (argc == 2) {
+ FILE *cmdstream = fopen(argv[1], "r");
+ return run_commands(cmdstream);
+ } else {
+ fprintf(stderr, "Usage: %s -c COMMAND|%s SCRIPT\n", argv[0], argv[0]);
+ return 1;
+ }
+}