static void add_transforms(char *ts, size_t tslen) { char *command; int r; char *t = NULL; bool added_transform = false; while ((t = argz_next(ts, tslen, t))) { r = xasprintf(&command, "transform %s", t); if (r < 0) fprintf(stderr, "error: Failed to add transform %s: could not allocate memory\n", t); r = aug_srun(aug, stdout, command); if (r < 0) fprintf(stderr, "error: Failed to add transform %s: %s\n", t, aug_error_message(aug)); free(command); added_transform = true; } if (added_transform) { r = aug_load(aug); if (r < 0) fprintf(stderr, "error: Failed to load with new transforms: %s\n", aug_error_message(aug)); } }
static int run_command(const char *line) { int result; result = aug_srun(aug, stdout, line); if (isatty(fileno(stdin))) add_history(line); return result; }
static int run_one_test(struct test *test) { int r; struct augeas *aug = NULL; struct memstream ms; int result = 0; MEMZERO(&ms, 1); aug = aug_init("/dev/null", lensdir, AUG_NO_STDINC|AUG_NO_MODL_AUTOLOAD); fail(aug == NULL, "aug_init"); fail(aug_error(aug) != AUG_NOERROR, "aug_init: errcode was %d", aug_error(aug)); printf("%-30s ... ", test->name); r = load_module(aug, test); if (r < 0) goto error; r = init_memstream(&ms); fail(r < 0, "init_memstream"); r = aug_srun(aug, ms.stream, test->cmd); fail(r != test->result, "return value: expected %d, actual %d", test->result, r); fail(aug_error(aug) != test->errcode, "errcode: expected %s, actual %s", errtokens[test->errcode], errtokens[aug_error(aug)]); r = close_memstream(&ms); fail(r < 0, "close_memstream"); fail(ms.buf == NULL, "close_memstream left buf NULL"); if (test->out != NULL) { fail(STRNEQ(ms.buf, test->out), "output: expected '%s', actual '%s'", test->out, ms.buf); } else if (test->out_present) { fail(strlen(ms.buf) == 0, "output: expected some output"); } else { fail(strlen(ms.buf) > 0, "output: expected nothing, actual '%s'", ms.buf); } printf("PASS\n"); done: free(ms.buf); aug_close(aug); return result; error: result = -1; goto done; }
/* * call-seq: * srun(COMMANDS) -> [int, String] * * Run one or more newline-separated commands, returning their output. * * Returns: * an array where the first element is the number of executed commands on * success, -1 on failure, and -2 if a 'quit' command was encountered. * The second element is a string of the output from all commands. */ VALUE augeas_srun(VALUE s, VALUE text) { augeas *aug = aug_handle(s); const char *ctext = StringValueCStr(text); struct memstream ms; __aug_init_memstream(&ms); int r = aug_srun(aug, ms.stream, ctext); __aug_close_memstream(&ms); VALUE result = rb_ary_new(); rb_ary_push(result, INT2NUM(r)); rb_ary_push(result, rb_str_new2(ms.buf)); free(ms.buf); return result; }