Bump to 1.14.1
[platform/upstream/augeas.git] / examples / dump.c
1 /*
2  * dump.c:
3  *
4  * Copyright (C) 2009 Red Hat Inc.
5  *
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.
10  *
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.
15  *
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
19  *
20  * Author: David Lutterkort <lutter@redhat.com>
21  */
22
23 /*
24  * Example program for dumping (part of) the Augeas tree.
25  *
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.
31  *
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::*'
37  *
38  */
39
40 #include <augeas.h>
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <sys/time.h>
46
47 /*
48  * Print out information for all nodes matching PATH using aug_match and
49  * then aug_get etc. on each of the matches.
50  */
51 static void dump_match(struct augeas *aug, const char *path) {
52     char **matches;
53     int nmatches;
54     int i;
55
56     nmatches = aug_match(aug, path, &matches);
57     if (nmatches < 0) {
58         fprintf(stderr, "aug_match for '%s' failed\n", path);
59         fprintf(stderr, "error: %s\n", aug_error_message(aug));
60         exit(1);
61     }
62
63     fprintf(stderr, "iterating matches\n");
64     fprintf(stderr, "%d matches for %s\n", nmatches, path);
65
66     for (i=0; i < nmatches; i++) {
67         const char *value, *label;
68         char *file;
69
70         aug_get(aug, matches[i], &value);
71         aug_label(aug, matches[i], &label);
72         aug_source(aug, matches[i], &file);
73
74         printf("%s: %s %s %s\n", matches[i], label, value, file);
75         free(file);
76         free(matches[i]);
77     }
78     free(matches);
79 }
80
81 /*
82  * Print out information for all nodes matching PATH using aug_ns_*
83  * functions
84  */
85 static void dump_var(struct augeas *aug, const char *path) {
86     int nmatches;
87     int i;
88
89     /* Define the variable 'matches' to hold all the nodes we are
90        interested in */
91     aug_defvar(aug, "matches", path);
92
93     /* Count how many nodes we have */
94     nmatches = aug_match(aug, "$matches", NULL);
95     if (nmatches < 0) {
96         fprintf(stderr, "aug_match for '%s' failed\n", path);
97         fprintf(stderr, "error: %s\n", aug_error_message(aug));
98         exit(1);
99     }
100
101     fprintf(stderr, "using var and aug_ns_*\n");
102     fprintf(stderr, "%d matches for %s\n", nmatches, path);
103
104     for (i=0; i < nmatches; i++) {
105         const char *value, *label;
106         char *file = NULL;
107
108         /* Get information about the ith node, equivalent to calling
109          * aug_get etc. for "$matches[i]" but much more efficient internally
110          */
111         aug_ns_attr(aug, "matches", i, &value, &label, &file);
112
113         printf("%d: %s %s %s\n", i, label, value, file);
114         free(file);
115     }
116 }
117
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);
123 }
124
125 int main(int argc, char **argv) {
126     int opt;
127     int use_var = 0;
128     const char *pattern = "/files//*";
129     struct timeval stop, start;
130
131     while ((opt = getopt(argc, argv, "n")) != -1) {
132         switch (opt) {
133         case 'n':
134             use_var = 1;
135             break;
136         default:
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");
140             exit(EXIT_FAILURE);
141             break;
142         }
143     }
144
145     struct augeas *aug = aug_init(NULL, NULL, 0);
146
147     if (optind < argc)
148         pattern = argv[optind];
149
150     gettimeofday(&start, NULL);
151     if (use_var) {
152         dump_var(aug, pattern);
153     } else {
154         dump_match(aug, pattern);
155     }
156     gettimeofday(&stop, NULL);
157     print_time_taken(&start, &stop);
158     return 0;
159 }