4 * Copyright (C) 2009 Red Hat Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Author: David Lutterkort <lutter@redhat.com>
24 * Example program for dumping (part of) the Augeas tree.
26 * Run it as 'dump [-n] [pattern] > /tmp/out.txt' to dump all the nodes
27 * matching PATTERN. The PATTERN '/files//descendant::*' dumps all nodes
28 * from files, and '//descendant::*' dumps absolutely everything. If -n is
29 * passed, uses a variable and aug_ns_* functions. Without -n, uses
30 * aug_match and straight aug_get/aug_label/aug_source.
32 * You might have to set AUGEAS_ROOT and AUGEAS_LENS_LIB to point at the
33 * right things. For example, to run dump against the tree that the Augeas
34 * tests use, and to use the lenses in the checkout, run it as
35 * AUGEAS_ROOT=$PWD/tests/root AUGEAS_LENS_LIB=$PWD/lenses \
36 * dump '//descendant::*'
48 * Print out information for all nodes matching PATH using aug_match and
49 * then aug_get etc. on each of the matches.
51 static void dump_match(struct augeas *aug, const char *path) {
56 nmatches = aug_match(aug, path, &matches);
58 fprintf(stderr, "aug_match for '%s' failed\n", path);
59 fprintf(stderr, "error: %s\n", aug_error_message(aug));
63 fprintf(stderr, "iterating matches\n");
64 fprintf(stderr, "%d matches for %s\n", nmatches, path);
66 for (i=0; i < nmatches; i++) {
67 const char *value, *label;
70 aug_get(aug, matches[i], &value);
71 aug_label(aug, matches[i], &label);
72 aug_source(aug, matches[i], &file);
74 printf("%s: %s %s %s\n", matches[i], label, value, file);
82 * Print out information for all nodes matching PATH using aug_ns_*
85 static void dump_var(struct augeas *aug, const char *path) {
89 /* Define the variable 'matches' to hold all the nodes we are
91 aug_defvar(aug, "matches", path);
93 /* Count how many nodes we have */
94 nmatches = aug_match(aug, "$matches", NULL);
96 fprintf(stderr, "aug_match for '%s' failed\n", path);
97 fprintf(stderr, "error: %s\n", aug_error_message(aug));
101 fprintf(stderr, "using var and aug_ns_*\n");
102 fprintf(stderr, "%d matches for %s\n", nmatches, path);
104 for (i=0; i < nmatches; i++) {
105 const char *value, *label;
108 /* Get information about the ith node, equivalent to calling
109 * aug_get etc. for "$matches[i]" but much more efficient internally
111 aug_ns_attr(aug, "matches", i, &value, &label, &file);
113 printf("%d: %s %s %s\n", i, label, value, file);
118 static void print_time_taken(const struct timeval *start,
119 const struct timeval *stop) {
120 time_t elapsed = (stop->tv_sec - start->tv_sec)*1000
121 + (stop->tv_usec - start->tv_usec)/1000;
122 fprintf(stderr, "time: %ld ms\n", elapsed);
125 int main(int argc, char **argv) {
128 const char *pattern = "/files//*";
129 struct timeval stop, start;
131 while ((opt = getopt(argc, argv, "n")) != -1) {
137 fprintf(stderr, "Usage: %s [-n] [pattern]\n", argv[0]);
138 fprintf(stderr, " without '-n', iterate matches\n");
139 fprintf(stderr, " with '-n', use a variable and aug_ns_*\n");
145 struct augeas *aug = aug_init(NULL, NULL, 0);
148 pattern = argv[optind];
150 gettimeofday(&start, NULL);
152 dump_var(aug, pattern);
154 dump_match(aug, pattern);
156 gettimeofday(&stop, NULL);
157 print_time_taken(&start, &stop);