Open SCAP Library
sexp-manip.h
Go to the documentation of this file.
1 
13 /*
14  * Copyright 2009 Red Hat Inc., Durham, North Carolina.
15  * All Rights Reserved.
16  *
17  * This library is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU Lesser General Public
19  * License as published by the Free Software Foundation; either
20  * version 2.1 of the License, or (at your option) any later version.
21  *
22  * This library is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25  * Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  *
31  * Authors:
32  * Daniel Kopecek <dkopecek@redhat.com>
33  */
34 
35 #pragma once
36 #ifndef SEXP_MANIP_H
37 #define SEXP_MANIP_H
38 
39 #include <stdarg.h>
40 #include <stddef.h>
41 #include <stdint.h>
42 #include <stdbool.h>
43 #include <sexp-types.h>
44 #include <helpers.h>
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 /*
51  * number
52  */
53 
59 SEXP_t *SEXP_number_new (SEXP_numtype_t t, const void *n) __attribute__ ((nonnull (2)));
60 
65 SEXP_t *SEXP_number_newb (bool n);
66 
71 SEXP_t *SEXP_number_newi_8 (int8_t n);
72 
77 SEXP_t *SEXP_number_newu_8 (uint8_t n);
78 
83 uint8_t SEXP_number_getu_8 (const SEXP_t *s_exp);
84 
89 SEXP_t *SEXP_number_newi_16 (int16_t n);
90 
95 SEXP_t *SEXP_number_newu_16 (uint16_t n);
96 
101 #define SEXP_number_newi SEXP_number_newi_32
102 
107 SEXP_t *SEXP_number_newi_32 (int32_t n);
108 
113 #define SEXP_number_geti SEXP_number_geti_32
114 
119 int32_t SEXP_number_geti_32 (const SEXP_t *s_exp);
120 
125 bool SEXP_number_getb (const SEXP_t *s_exp);
126 
131 #define SEXP_number_newu SEXP_number_newu_32
132 
137 SEXP_t *SEXP_number_newu_32 (uint32_t n);
138 
143 #define SEXP_number_getu SEXP_number_getu_32
144 
149 uint32_t SEXP_number_getu_32 (const SEXP_t *s_exp);
150 
155 SEXP_t *SEXP_number_newi_64 (int64_t n);
156 
161 int64_t SEXP_number_geti_64 (const SEXP_t *s_exp);
162 
167 SEXP_t *SEXP_number_newu_64 (uint64_t n);
168 
173 uint64_t SEXP_number_getu_64 (const SEXP_t *s_exp);
174 
179 SEXP_t *SEXP_number_newf (double n);
180 
185 double SEXP_number_getf (const SEXP_t *s_exp);
186 
193 int SEXP_number_get (const SEXP_t *s_exp, void *dst, SEXP_numtype_t type);
194 
199 uint16_t SEXP_number_getu_16 (const SEXP_t *s_exp);
200 
205 void SEXP_number_free (SEXP_t *s_exp);
206 
211 bool SEXP_numberp (const SEXP_t *s_exp);
212 
217 SEXP_numtype_t SEXP_number_type (const SEXP_t *sexp);
218 
219 /*
220  * string
221  */
222 
228 SEXP_t *SEXP_string_new (const void *string, size_t strlen) __attribute__ ((nonnull (1)));
229 
235 SEXP_t *SEXP_string_newf (const char *format, ...) __attribute__ ((format (printf, 1, 2), nonnull (1)));
236 
241 void SEXP_string_free (SEXP_t *s_exp);
242 
247 bool SEXP_stringp (const SEXP_t *s_exp);
248 
253 size_t SEXP_string_length (const SEXP_t *s_exp);
254 
260 int SEXP_strcmp (const SEXP_t *s_exp, const char *str) __attribute__ ((nonnull (2)));
261 
268 int SEXP_strncmp (const SEXP_t *s_exp, const char *str, size_t n) __attribute__ ((nonnull (2)));
269 
275 int SEXP_string_nth (const SEXP_t *s_exp, size_t n);
276 
280 char *SEXP_string_cstr (const SEXP_t *s_exp);
281 
289 size_t SEXP_string_cstr_r (const SEXP_t *s_exp, char *buf, size_t len) __attribute__ ((nonnull (2)));
290 
294 char *SEXP_string_cstrp (const SEXP_t *s_exp);
295 
302 char *SEXP_string_subcstr (const SEXP_t *s_exp, size_t beg, size_t len);
303 
309 int SEXP_string_cmp (const SEXP_t *str_a, const SEXP_t *str_b);
310 
315 bool SEXP_string_getb (const SEXP_t *s_exp);
316 
317 /*
318  * list
319  */
320 
327 SEXP_t *SEXP_list_new (SEXP_t *memb, ...);
328 
333 void SEXP_list_free (SEXP_t *s_exp);
334 
339 bool SEXP_listp (const SEXP_t *s_exp);
340 
345 size_t SEXP_list_length (const SEXP_t *s_exp);
346 
352 SEXP_t *SEXP_list_first (const SEXP_t *list);
353 
359 SEXP_t *SEXP_list_rest (const SEXP_t *list);
360 
366 SEXP_t *SEXP_list_last (const SEXP_t *list);
367 
374 SEXP_t *SEXP_list_nth (const SEXP_t *list, uint32_t n);
375 
382 SEXP_t *SEXP_list_add (SEXP_t *list, const SEXP_t *s_exp);
383 
390 SEXP_t *SEXP_list_join (const SEXP_t *list_a, const SEXP_t *list_b);
391 
398 SEXP_t *SEXP_list_push (SEXP_t *list, const SEXP_t *s_exp);
399 
405 SEXP_t *SEXP_list_pop (SEXP_t *list);
406 
410 SEXP_t *SEXP_list_sort(SEXP_t *list, int(*compare)(const SEXP_t *, const SEXP_t *));
411 
420 SEXP_t *SEXP_list_replace (SEXP_t *list, uint32_t n, const SEXP_t *s_exp);
421 
427 SEXP_t *SEXP_listref_first (SEXP_t *list);
428 
434 SEXP_t *SEXP_listref_rest (SEXP_t *list);
435 
441 SEXP_t *SEXP_listref_last (SEXP_t *list);
442 
449 SEXP_t *SEXP_listref_nth (SEXP_t *list, uint32_t n);
450 
451 typedef struct SEXP_it SEXP_it_t;
452 
453 #define SEXP_IT_RECURSIVE 0x01
454 #define SEXP_IT_HARDREF 0x02
455 
456 SEXP_it_t *SEXP_listit_new (const SEXP_t *list, int flags);
457 SEXP_t *SEXP_listit_next(SEXP_it_t *it);
458 SEXP_t *SEXP_listit_prev (SEXP_it_t *it);
459 SEXP_t *SEXP_listit_length (SEXP_it_t *it);
460 SEXP_t *SEXP_listit_seek (SEXP_it_t *it, uint32_t n);
461 void SEXP_listit_free (SEXP_it_t *it);
462 
463 typedef struct SEXP_list_it SEXP_list_it;
464 
465 SEXP_list_it *SEXP_list_it_new(const SEXP_t *list);
466 SEXP_t *SEXP_list_it_next(SEXP_list_it *it);
467 void SEXP_list_it_free(SEXP_list_it *it);
468 
469 #if __STDC_VERSION__ >= 199901L
470 # include <common/util.h>
471 
472 /* TODO: use alloca & softref_r here */
478 #define SEXP_list_foreach(var, list) \
479  for (uint32_t OSCAP_CONCAT(i,__LINE__) = 1; ((var) = SEXP_list_nth (list, OSCAP_CONCAT(i,__LINE__))) != NULL; ++OSCAP_CONCAT(i,__LINE__), SEXP_free (var), (var) = NULL)
480 
488 #define SEXP_sublist_foreach(var, list, beg, end) \
489  for (uint32_t OSCAP_CONCAT(i,__LINE__) = (beg); OSCAP_CONCAT(i,__LINE__) <= ((size_t)(end)) && ((var) = SEXP_list_nth (list, OSCAP_CONCAT(i,__LINE__))) != NULL; ++OSCAP_CONCAT(i,__LINE__), SEXP_free (var), (var) = NULL)
490 
491 #define SEXP_LIST_END (UINT32_MAX - 1)
492 
493 #endif /* __STDC_VERSION__ >= 199901L */
494 
495 /*
496  * generic
497  */
498 SEXP_t *SEXP_new (void);
499 
500 bool SEXP_emptyp(SEXP_t *sexp);
501 
506 SEXP_t *SEXP_ref (const SEXP_t *s_exp);
507 
508 SEXP_t *SEXP_unref (SEXP_t *s_exp_o);
509 
514 SEXP_t *SEXP_softref (SEXP_t *s_exp);
515 
520 bool SEXP_softrefp(const SEXP_t *s_exp);
521 
526 uint32_t SEXP_refs (const SEXP_t *ref);
527 
528 bool SEXP_eq (const SEXP_t *a, const SEXP_t *b);
529 
533 int SEXP_refcmp(const SEXP_t *a, const SEXP_t *b);
534 
535 bool SEXP_deepcmp(const SEXP_t *a, const SEXP_t *b);
536 
537 #ifdef __COVERITY__
538 /*
539  * An alternative implementation for Coverity, which reports
540  * a lot of false positives because it is not be able to
541  * handle functions with variable number of arguments
542  */
543 # define SEXP_vfree_coverity(...) \
544  do { \
545  SEXP_t *__svf##__LINE__[] = { __VA_ARGS__ }; \
546  size_t __svfc##__LINE__ = sizeof (__svf##__LINE__)/sizeof(SEXP_t *); \
547  for (; __svfc##__LINE__ > 0; --__svfc##__LINE__) \
548  if (__svf##__LINE__[__svfc##__LINE__ - 1]) \
549  SEXP_free(__svf##__LINE__[__svfc##__LINE__ - 1]); \
550  } while(0)
551 #endif /* __COVERITY__ */
552 
553 #if defined(NDEBUG)
554 
558 void SEXP_free (SEXP_t *s_exp);
559 
566 void __SEXP_vfree (int n, SEXP_t *s_exp, ...);
567 #ifdef __COVERITY__
568 # define SEXP_vfree(...) SEXP_vfree_coverity(__VA_ARGS__)
569 #else
570 # define SEXP_vfree(...) __SEXP_vfree(PP_NARG(__VA_ARGS__), __VA_ARGS__)
571 #endif
572 #else
573 # define SEXP_free(ptr) __SEXP_free (ptr, __FILE__, __LINE__, __PRETTY_FUNCTION__)
574 void __SEXP_free (SEXP_t *s_exp, const char *file, uint32_t line, const char *func);
575 #ifdef __COVERITY__
576 # define SEXP_vfree(...) SEXP_vfree_coverity(__VA_ARGS__)
577 #else
578 # define SEXP_vfree(...) __SEXP_vfree (__FILE__, __LINE__, __PRETTY_FUNCTION__, PP_NARG(__VA_ARGS__), __VA_ARGS__)
579 #endif
580 void __SEXP_vfree (const char *file, uint32_t line, const char *func, int n, SEXP_t *s_exp, ...);
581 #endif
582 
587 const char *SEXP_datatype (const SEXP_t *s_exp);
588 
593 int SEXP_datatype_set (SEXP_t *s_exp, const char *name) __attribute__ ((nonnull (2)));
594 
601 int SEXP_datatype_set_nth (SEXP_t *list, uint32_t n, const char *name) __attribute__ ((nonnull (3)));
602 
607 SEXP_type_t SEXP_typeof (const SEXP_t *s_exp);
608 
613 const char *SEXP_strtype (const SEXP_t *s_exp);
614 
615 SEXP_t *SEXP_build (const char *s_str, ...);
616 
617 size_t SEXP_sizeof (const SEXP_t *s_exp);
618 
619 #if !defined(NDEBUG)
620 # define SEXP_VALIDATE(s) __SEXP_VALIDATE(s, __FILE__, __LINE__, __PRETTY_FUNCTION__)
621 # include <stdlib.h>
622 
623 void __SEXP_VALIDATE(const SEXP_t *s_exp, const char *file, uint32_t line, const char *func);
624 
625 #else
626 # define SEXP_VALIDATE(s)
627 #endif
628 
629 #ifdef __cplusplus
630 }
631 #endif
632 
633 #endif /* SEXP_MANIP_H */
634 
635 
636