xref: /aosp_15_r20/external/libconfig/lib/scanctx.h (revision 2e9d491483b805f09ea864149eadd5680efcc72a)
1 /* ----------------------------------------------------------------------------
2    libconfig - A library for processing structured configuration files
3    Copyright (C) 2005-2020  Mark A Lindner
4 
5    This file is part of libconfig.
6 
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Lesser General Public License
9    as published by the Free Software Foundation; either version 2.1 of
10    the License, or (at your option) any later version.
11 
12    This library is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16 
17    You should have received a copy of the GNU Library General Public
18    License along with this library; if not, see
19    <http://www.gnu.org/licenses/>.
20    ----------------------------------------------------------------------------
21 */
22 
23 #ifndef __libconfig_scanctx_h
24 #define __libconfig_scanctx_h
25 
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/types.h>
29 
30 #include "libconfig.h"
31 #include "strbuf.h"
32 #include "strvec.h"
33 
34 #define MAX_INCLUDE_DEPTH 10
35 
36 struct include_stack_frame
37 {
38   /*
39    * Member strings are not owned; they are pointers into scan_context's
40    * filenames vector.
41    */
42   const char **files;
43   const char **current_file;
44   FILE *current_stream;
45   void *parent_buffer;
46 };
47 
48 struct scan_context
49 {
50   config_t *config;
51   const char *top_filename;
52   struct include_stack_frame include_stack[MAX_INCLUDE_DEPTH];
53   int stack_depth;
54   strbuf_t string;
55   strvec_t filenames;
56 };
57 
58 extern void libconfig_scanctx_init(struct scan_context *ctx,
59                                    const char *top_filename);
60 extern const char **libconfig_scanctx_cleanup(struct scan_context *ctx);
61 
62 /*
63  * Pushes a new frame onto the include stack, and returns an open stream to the
64  * first file in the include list, if any.
65  *
66  * ctx - The scan context
67  * prev_buffer - The current input buffer, to be restored when this frame is
68  * popped
69  * path - The string argument to the @include directive, to be expanded into a
70  * list of zero or more filenames using the function ctx->config->include_fn
71  * error - A pointer at which to store a static error message, if any.
72  *
73  * On success, the new frame will be pushed and the stream to the first file
74  * will be returned.
75  *
76  * On failure, the frame will not be pushed and NULL will be returned. If
77  * *error is NULL, it means there are no files in the list. Otherwise, it
78  * points to an error and parsing should be aborted.
79  */
80 extern FILE *libconfig_scanctx_push_include(struct scan_context *ctx,
81                                             void *prev_buffer,
82                                             const char *path,
83                                             const char **error);
84 
85 /*
86  * Returns the next include file in the current include stack frame.
87  *
88  * Returns NULL on failure or if there are no more files left in the current
89  * frame. If there was an error, sets *error.
90  */
91 extern FILE *libconfig_scanctx_next_include_file(struct scan_context *ctx,
92                                                  const char **error);
93 
94 /*
95  * Pops a frame off the include stack.
96  */
97 extern void *libconfig_scanctx_pop_include(struct scan_context *ctx);
98 
99 #define libconfig_scanctx_append_string(C, S) \
100   libconfig_strbuf_append_string(&((C)->string), (S))
101 
102 #define libconfig_scanctx_append_char(C, X) \
103   libconfig_strbuf_append_char(&((C)->string), (X))
104 
105 extern char *libconfig_scanctx_take_string(struct scan_context *ctx);
106 
107 extern const char *libconfig_scanctx_current_filename(struct scan_context *ctx);
108 
109 #endif /* __libconfig_scanctx_h */
110