[augeas-devel] [PATCH] Add -t|--transform option to augtool
Raphaël Pinson
raphael.pinson at camptocamp.com
Thu Aug 2 11:34:00 UTC 2012
This patch adds a -t|--transform option to augtool.
This new option allows to pass transforms to augtool, mapping files to lenses (thus adding incl nodes). For example, if you only want to parse /etc/fstab and /etc/passwd:
augtool --noautoload --transform Fstab=/etc/fstab --transform Passwd=/etc/passwd
When a module name (e.g. Fstab or Passwd) is passed, augtool assumes the lns lens. You can however pass the lens name:
augtool --noautoload --transform Shellvars.lns=/etc/default/ntp # Use standard lens
augtool --noautoload --transform Shellvars.lns_norec=/etc/default/ntp # Use non-recursive lens
---
src/augtool.c | 122 ++++++++++++++++++++++++++++++++++++++++++---------------
1 file changed, 90 insertions(+), 32 deletions(-)
diff --git a/src/augtool.c b/src/augtool.c
index ce4a8a3..15f86aa 100644
--- a/src/augtool.c
+++ b/src/augtool.c
@@ -44,6 +44,8 @@ static const char *const progname = "augtool";
static unsigned int flags = AUG_NONE;
const char *root = NULL;
char *loadpath = NULL;
+char *transforms = NULL;
+size_t transformslen = 0;
const char *inputfile = NULL;
int echo_commands = 0; /* Gets also changed in main_loop */
bool print_version = false;
@@ -266,22 +268,25 @@ static void usage(void) {
fprintf(stderr, "Run '%s help' to get a list of possible commands.\n",
progname);
fprintf(stderr, "\nOptions:\n\n");
- fprintf(stderr, " -c, --typecheck typecheck lenses\n");
- fprintf(stderr, " -b, --backup preserve originals of modified files with\n"
- " extension '.augsave'\n");
- fprintf(stderr, " -n, --new save changes in files with extension '.augnew',\n"
- " leave original unchanged\n");
- fprintf(stderr, " -r, --root ROOT use ROOT as the root of the filesystem\n");
- fprintf(stderr, " -I, --include DIR search DIR for modules; can be given mutiple times\n");
- fprintf(stderr, " -e, --echo echo commands when reading from a file\n");
- fprintf(stderr, " -f, --file FILE read commands from FILE\n");
- fprintf(stderr, " -s, --autosave automatically save at the end of instructions\n");
- fprintf(stderr, " -i, --interactive run an interactive shell after evaluating the commands in STDIN and FILE\n");
- fprintf(stderr, " -S, --nostdinc do not search the builtin default directories for modules\n");
- fprintf(stderr, " -L, --noload do not load any files into the tree on startup\n");
- fprintf(stderr, " -A, --noautoload do not autoload modules from the search path\n");
- fprintf(stderr, " --span load span positions for nodes related to a file\n");
- fprintf(stderr, " --version print version information and exit.\n");
+ fprintf(stderr, " -c, --typecheck typecheck lenses\n");
+ fprintf(stderr, " -b, --backup preserve originals of modified files with\n"
+ " extension '.augsave'\n");
+ fprintf(stderr, " -n, --new save changes in files with extension '.augnew',\n"
+ " leave original unchanged\n");
+ fprintf(stderr, " -r, --root ROOT use ROOT as the root of the filesystem\n");
+ fprintf(stderr, " -I, --include DIR search DIR for modules; can be given mutiple times\n");
+ fprintf(stderr, " -t, --transform LENS=FILE add a transform for FILE using LENS\n");
+ fprintf(stderr, " -e, --echo echo commands when reading from a file\n");
+ fprintf(stderr, " -f, --file FILE read commands from FILE\n");
+ fprintf(stderr, " -s, --autosave automatically save at the end of instructions\n");
+ fprintf(stderr, " -i, --interactive run an interactive shell after evaluating\n"
+ " the commands in STDIN and FILE\n");
+ fprintf(stderr, " -S, --nostdinc do not search the builtin default directories\n"
+ " for modules\n");
+ fprintf(stderr, " -L, --noload do not load any files into the tree on startup\n");
+ fprintf(stderr, " -A, --noautoload do not autoload modules from the search path\n");
+ fprintf(stderr, " --span load span positions for nodes related to a file\n");
+ fprintf(stderr, " --version print version information and exit.\n");
exit(EXIT_FAILURE);
}
@@ -294,26 +299,27 @@ static void parse_opts(int argc, char **argv) {
VAL_SPAN = VAL_VERSION + 1
};
struct option options[] = {
- { "help", 0, 0, 'h' },
- { "typecheck", 0, 0, 'c' },
- { "backup", 0, 0, 'b' },
- { "new", 0, 0, 'n' },
- { "root", 1, 0, 'r' },
- { "include", 1, 0, 'I' },
- { "echo", 0, 0, 'e' },
- { "file", 1, 0, 'f' },
- { "autosave", 0, 0, 's' },
- { "interactive", 0, 0, 'i' },
- { "nostdinc", 0, 0, 'S' },
- { "noload", 0, 0, 'L' },
- { "noautoload", 0, 0, 'A' },
- { "span", 0, 0, VAL_SPAN },
- { "version", 0, 0, VAL_VERSION },
+ { "help", 0, 0, 'h' },
+ { "typecheck", 0, 0, 'c' },
+ { "backup", 0, 0, 'b' },
+ { "new", 0, 0, 'n' },
+ { "root", 1, 0, 'r' },
+ { "include", 1, 0, 'I' },
+ { "transform", 1, 0, 't' },
+ { "echo", 0, 0, 'e' },
+ { "file", 1, 0, 'f' },
+ { "autosave", 0, 0, 's' },
+ { "interactive", 0, 0, 'i' },
+ { "nostdinc", 0, 0, 'S' },
+ { "noload", 0, 0, 'L' },
+ { "noautoload", 0, 0, 'A' },
+ { "span", 0, 0, VAL_SPAN },
+ { "version", 0, 0, VAL_VERSION },
{ 0, 0, 0, 0}
};
int idx;
- while ((opt = getopt_long(argc, argv, "hnbcr:I:ef:siSLA", options, &idx)) != -1) {
+ while ((opt = getopt_long(argc, argv, "hnbcr:I:t:ef:siSLA", options, &idx)) != -1) {
switch(opt) {
case 'c':
flags |= AUG_TYPE_CHECK;
@@ -333,6 +339,9 @@ static void parse_opts(int argc, char **argv) {
case 'I':
argz_add(&loadpath, &loadpathlen, optarg);
break;
+ case 't':
+ argz_add(&transforms, &transformslen, optarg);
+ break;
case 'e':
echo_commands = 1;
break;
@@ -537,8 +546,54 @@ static int run_args(int argc, char **argv) {
return (code == 0 || code == -2) ? 0 : -1;
}
+static void add_transform(const char *t) {
+ char delims[] = "=";
+ char *lens, *file;
+ char *transform = strdup(t);
+ int r;
+
+ lens = strtok(transform, delims);
+ file = strtok(NULL, delims);
+
+ char *lenspath = malloc(14+strlen(lens));
+ sprintf(lenspath, "/augeas/load/%s", lens);
+
+ char *inclpath = malloc(strlen(lenspath)+16);
+ sprintf(inclpath, "%s/incl[last()+1]", lenspath);
+
+ char *lenslenspath = malloc(strlen(lenspath)+6);
+ sprintf(lenslenspath, "%s/lens", lenspath);
+
+ char *lensname;
+ if (strchr(lens, '.')) {
+ lensname = malloc(strlen(lens)+1);
+ strcpy(lensname, lens);
+ } else {
+ lensname = malloc(strlen(lens)+5);
+ sprintf(lensname, "%s.lns", lens);
+ }
+
+
+ if (aug_match(aug, lenslenspath, NULL) == 0)
+ r = aug_set(aug, lenslenspath, lensname);
+
+ r = aug_set(aug, inclpath, file);
+ if (r < 0)
+ fprintf(stderr, "error: %s\n", aug_error_message(aug));
+
+ r = aug_load(aug);
+ if (r < 0)
+ fprintf(stderr, "error: %s\n", aug_error_message(aug));
+
+ free(lenspath);
+ free(inclpath);
+ free(lenslenspath);
+ free(lensname);
+}
+
int main(int argc, char **argv) {
int r;
+ char *t = NULL;
setlocale(LC_ALL, "");
@@ -551,6 +606,9 @@ int main(int argc, char **argv) {
print_aug_error();
exit(EXIT_FAILURE);
}
+ while ((t = argz_next(transforms, transformslen, t))) {
+ add_transform(t);
+ }
if (print_version) {
print_version_info();
return EXIT_SUCCESS;
--
1.7.9.5
More information about the augeas-devel
mailing list