1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 #include "sl_pp_public.h"
31 #include "sl_pp_context.h"
34 struct sl_pp_context
*
35 sl_pp_context_create(const char *input
,
36 const struct sl_pp_purify_options
*options
)
38 struct sl_pp_context
*context
;
40 context
= calloc(1, sizeof(struct sl_pp_context
));
45 if (sl_pp_dict_init(context
)) {
46 sl_pp_context_destroy(context
);
50 context
->getc_buf_capacity
= 64;
51 context
->getc_buf
= malloc(context
->getc_buf_capacity
* sizeof(char));
52 if (!context
->getc_buf
) {
53 sl_pp_context_destroy(context
);
57 if (sl_pp_token_buffer_init(&context
->tokens
, context
)) {
58 sl_pp_context_destroy(context
);
62 context
->macro_tail
= &context
->macro
;
63 context
->if_ptr
= SL_PP_MAX_IF_NESTING
;
64 context
->if_value
= 1;
65 memset(context
->error_msg
, 0, sizeof(context
->error_msg
));
66 context
->error_line
= 1;
70 sl_pp_purify_state_init(&context
->pure
, input
, options
);
72 memset(&context
->process_state
, 0, sizeof(context
->process_state
));
78 sl_pp_context_destroy(struct sl_pp_context
*context
)
81 free(context
->cstr_pool
);
82 sl_pp_macro_free(context
->macro
);
83 free(context
->getc_buf
);
84 sl_pp_token_buffer_destroy(&context
->tokens
);
85 free(context
->process_state
.out
);
91 sl_pp_context_error_message(const struct sl_pp_context
*context
)
93 return context
->error_msg
;
97 sl_pp_context_error_position(const struct sl_pp_context
*context
,
105 *line
= context
->error_line
;
110 sl_pp_context_add_predefined(struct sl_pp_context
*context
,
114 struct sl_pp_predefined pre
;
116 if (context
->num_predefined
== SL_PP_MAX_PREDEFINED
) {
120 pre
.name
= sl_pp_context_add_unique_str(context
, name
);
121 if (pre
.name
== -1) {
125 pre
.value
= sl_pp_context_add_unique_str(context
, value
);
126 if (pre
.value
== -1) {
130 context
->predefined
[context
->num_predefined
++] = pre
;
135 sl_pp_context_add_unique_str(struct sl_pp_context
*context
,
139 unsigned int offset
= 0;
141 size
= strlen(str
) + 1;
143 /* Find out if this is a unique string. */
144 while (offset
< context
->cstr_pool_len
) {
148 str2
= &context
->cstr_pool
[offset
];
149 size2
= strlen(str2
) + 1;
150 if (size
== size2
&& !memcmp(str
, str2
, size
- 1)) {
157 if (context
->cstr_pool_len
+ size
> context
->cstr_pool_max
) {
158 context
->cstr_pool_max
= (context
->cstr_pool_len
+ size
+ 0xffff) & ~0xffff;
159 context
->cstr_pool
= realloc(context
->cstr_pool
, context
->cstr_pool_max
);
162 if (!context
->cstr_pool
) {
163 strcpy(context
->error_msg
, "out of memory");
167 offset
= context
->cstr_pool_len
;
168 memcpy(&context
->cstr_pool
[offset
], str
, size
);
169 context
->cstr_pool_len
+= size
;
175 sl_pp_context_cstr(const struct sl_pp_context
*context
,
181 return &context
->cstr_pool
[offset
];