| 1 | /* |
| 2 | * Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * "Daniel Kopecek" <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <string.h> |
| 28 | |
| 29 | #include "_sexp-types.h" |
| 30 | #include "_seap-types.h" |
| 31 | #include "_seap-message.h" |
| 32 | #include "debug_priv.h" |
| 33 | |
| 34 | SEAP_msg_t *SEAP_msg_new (void) |
| 35 | { |
| 36 | SEAP_msg_t *new = malloc(sizeof(SEAP_msg_t)); |
| 37 | new->id = 0; |
| 38 | new->attrs = NULL; |
| 39 | new->attrs_cnt = 0; |
| 40 | new->sexp = NULL; |
| 41 | |
| 42 | return (new); |
| 43 | } |
| 44 | |
| 45 | SEAP_msg_t *SEAP_msg_clone (SEAP_msg_t *msg) |
| 46 | { |
| 47 | uint16_t i; |
| 48 | |
| 49 | SEAP_msg_t *new = malloc(sizeof(SEAP_msg_t)); |
| 50 | memcpy (new, msg, sizeof (SEAP_msg_t)); |
| 51 | |
| 52 | new->attrs = malloc(sizeof(SEAP_attr_t) * new->attrs_cnt); |
| 53 | |
| 54 | for (i = 0; i < new->attrs_cnt; ++i) { |
| 55 | new->attrs[i].name = strdup (msg->attrs[i].name); |
| 56 | new->attrs[i].value = SEXP_ref (msg->attrs[i].value); |
| 57 | } |
| 58 | |
| 59 | new->sexp = SEXP_ref (msg->sexp); |
| 60 | |
| 61 | return (new); |
| 62 | } |
| 63 | |
| 64 | void SEAP_msg_free (SEAP_msg_t *msg) |
| 65 | { |
| 66 | if (msg == NULL) |
| 67 | return; |
| 68 | |
| 69 | if (msg->attrs != NULL) { |
| 70 | for (; msg->attrs_cnt > 0; --msg->attrs_cnt) { |
| 71 | dD("name=%s, value=%p", |
| 72 | msg->attrs[msg->attrs_cnt - 1].name, |
| 73 | msg->attrs[msg->attrs_cnt - 1].value); |
| 74 | |
| 75 | free(msg->attrs[msg->attrs_cnt - 1].name); |
| 76 | SEXP_free (msg->attrs[msg->attrs_cnt - 1].value); |
| 77 | } |
| 78 | |
| 79 | free(msg->attrs); |
| 80 | } |
| 81 | |
| 82 | if (msg->sexp != NULL) |
| 83 | SEXP_free (msg->sexp); |
| 84 | |
| 85 | free(msg); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | int SEAP_msg_set (SEAP_msg_t *msg, SEXP_t *sexp) |
| 90 | { |
| 91 | msg->sexp = SEXP_ref (sexp); |
| 92 | return (0); |
| 93 | } |
| 94 | |
| 95 | void SEAP_msg_unset (SEAP_msg_t *msg) |
| 96 | { |
| 97 | SEXP_free (msg->sexp); |
| 98 | msg->sexp = NULL; |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | SEXP_t *SEAP_msg_get (SEAP_msg_t *msg) |
| 103 | { |
| 104 | return SEXP_ref (msg->sexp); |
| 105 | } |
| 106 | |
| 107 | SEAP_msgid_t SEAP_msg_id (SEAP_msg_t *msg) |
| 108 | { |
| 109 | return (msg->id); |
| 110 | } |
| 111 | |
| 112 | int SEAP_msgattr_set (SEAP_msg_t *msg, const char *attr, SEXP_t *value) |
| 113 | { |
| 114 | _A(msg != NULL); |
| 115 | _A(attr != NULL); |
| 116 | |
| 117 | #ifndef NDEBUG |
| 118 | if (value != NULL) |
| 119 | SEXP_VALIDATE(value); |
| 120 | #endif |
| 121 | void *new_attrs = realloc(msg->attrs, sizeof(SEAP_attr_t) * (++msg->attrs_cnt)); |
| 122 | if (new_attrs == NULL) |
| 123 | return -1; |
| 124 | msg->attrs = new_attrs; |
| 125 | msg->attrs[msg->attrs_cnt - 1].name = strdup (attr); |
| 126 | msg->attrs[msg->attrs_cnt - 1].value = (value != NULL ? SEXP_ref (value) : NULL); |
| 127 | |
| 128 | return (0); |
| 129 | } |
| 130 | |
| 131 | bool SEAP_msgattr_exists (SEAP_msg_t *msg, const char *name) |
| 132 | { |
| 133 | uint16_t i; |
| 134 | |
| 135 | _A(msg != NULL); |
| 136 | _A(name != NULL); |
| 137 | |
| 138 | dD("cnt = %u", msg->attrs_cnt); |
| 139 | |
| 140 | /* FIXME: this is stupid */ |
| 141 | for (i = 0; i < msg->attrs_cnt; ++i) { |
| 142 | dD("%s ?= %s", name, msg->attrs[i].name); |
| 143 | if (strcmp (name, msg->attrs[i].name) == 0) |
| 144 | return (true); |
| 145 | } |
| 146 | |
| 147 | return (false); |
| 148 | } |
| 149 |