Imported Upstream version 1.12.0
[platform/upstream/augeas.git] / src / augparse.c
1 /*
2  * augparse.c: utility for parsing config files and seeing what's happening
3  *
4  * Copyright (C) 2007-2016 David Lutterkort
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 <dlutter@redhat.com>
21  */
22
23 #include <config.h>
24 #include <argz.h>
25 #include <getopt.h>
26
27 #include "list.h"
28 #include "syntax.h"
29 #include "augeas.h"
30 #include <locale.h>
31
32 const char *progname;
33 bool print_version = false;
34
35 __attribute__((noreturn))
36 static void usage(void) {
37     fprintf(stderr, "Usage: %s [OPTIONS] MODULE\n", progname);
38     fprintf(stderr, "Evaluate MODULE. Generally, MODULE should contain unit tests.\n");
39     fprintf(stderr, "\nOptions:\n\n");
40     fprintf(stderr, "  -I, --include DIR  search DIR for modules; can be given multiple times\n");
41     fprintf(stderr, "  -t, --trace        trace module loading\n");
42     fprintf(stderr, "  --nostdinc         do not search the builtin default directories for modules\n");
43     fprintf(stderr, "  --notypecheck      do not typecheck lenses\n");
44     fprintf(stderr, "  --version          print version information and exit\n");
45
46     exit(EXIT_FAILURE);
47 }
48
49 static void print_version_info(struct augeas *aug) {
50     const char *version;
51     int r;
52
53     r = aug_get(aug, "/augeas/version", &version);
54     if (r != 1)
55         goto error;
56
57     fprintf(stderr, "augparse %s <http://augeas.net/>\n", version);
58     fprintf(stderr, "Copyright (C) 2007-2016 David Lutterkort\n");
59     fprintf(stderr, "License LGPLv2+: GNU LGPL version 2.1 or later\n");
60     fprintf(stderr, "                 <http://www.gnu.org/licenses/lgpl-2.1.html>\n");
61     fprintf(stderr, "This is free software: you are free to change and redistribute it.\n");
62     fprintf(stderr, "There is NO WARRANTY, to the extent permitted by law.\n\n");
63     fprintf(stderr, "Written by David Lutterkort\n");
64     return;
65  error:
66     fprintf(stderr, "Something went terribly wrong internally - please file a bug\n");
67 }
68
69 int main(int argc, char **argv) {
70     int opt;
71     struct augeas *aug;
72     char *loadpath = NULL;
73     size_t loadpathlen = 0;
74     enum {
75         VAL_NO_STDINC = CHAR_MAX + 1,
76         VAL_NO_TYPECHECK = VAL_NO_STDINC + 1,
77         VAL_VERSION = VAL_NO_TYPECHECK + 1
78     };
79     struct option options[] = {
80         { "help",      0, 0, 'h' },
81         { "include",   1, 0, 'I' },
82         { "trace",     0, 0, 't' },
83         { "nostdinc",  0, 0, VAL_NO_STDINC },
84         { "notypecheck",  0, 0, VAL_NO_TYPECHECK },
85         { "version",  0, 0, VAL_VERSION },
86         { 0, 0, 0, 0}
87     };
88     int idx;
89     unsigned int flags = AUG_TYPE_CHECK|AUG_NO_MODL_AUTOLOAD;
90     progname = argv[0];
91
92     setlocale(LC_ALL, "");
93     while ((opt = getopt_long(argc, argv, "hI:t", options, &idx)) != -1) {
94         switch(opt) {
95         case 'I':
96             argz_add(&loadpath, &loadpathlen, optarg);
97             break;
98         case 't':
99             flags |= AUG_TRACE_MODULE_LOADING;
100             break;
101         case 'h':
102             usage();
103             break;
104         case VAL_NO_STDINC:
105             flags |= AUG_NO_STDINC;
106             break;
107         case VAL_NO_TYPECHECK:
108             flags &= ~(AUG_TYPE_CHECK);
109             break;
110         case VAL_VERSION:
111             print_version = true;
112             break;
113         default:
114             usage();
115             break;
116         }
117     }
118
119     if (!print_version && optind >= argc) {
120         fprintf(stderr, "Expected .aug file\n");
121         usage();
122     }
123
124     argz_stringify(loadpath, loadpathlen, PATH_SEP_CHAR);
125     aug = aug_init(NULL, loadpath, flags);
126     if (aug == NULL) {
127         fprintf(stderr, "Memory exhausted\n");
128         return 2;
129     }
130
131     if (print_version) {
132         print_version_info(aug);
133         aug_close(aug);
134         return EXIT_SUCCESS;
135     }
136
137     if (__aug_load_module_file(aug, argv[optind]) == -1) {
138         fprintf(stderr, "%s\n", aug_error_message(aug));
139         const char *s = aug_error_details(aug);
140         if (s != NULL) {
141             fprintf(stderr, "%s\n", s);
142         }
143         aug_close(aug);
144         exit(EXIT_FAILURE);
145     }
146
147     aug_close(aug);
148     free(loadpath);
149 }
150
151 /*
152  * Local variables:
153  *  indent-tabs-mode: nil
154  *  c-indent-level: 4
155  *  c-basic-offset: 4
156  *  tab-width: 4
157  * End:
158  */