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