| 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 <inttypes.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <sys/types.h> |
| 32 | #ifdef OS_WINDOWS |
| 33 | #include <io.h> |
| 34 | #else |
| 35 | #include <unistd.h> |
| 36 | #endif |
| 37 | #include <errno.h> |
| 38 | #include <assert.h> |
| 39 | |
| 40 | #include "generic/common.h" |
| 41 | #include "generic/strbuf.h" |
| 42 | #include "_sexp-types.h" |
| 43 | #include "_sexp-output.h" |
| 44 | #include "_sexp-value.h" |
| 45 | #include "_sexp-datatype.h" |
| 46 | #include "_sexp-rawptr.h" |
| 47 | #include "debug_priv.h" |
| 48 | |
| 49 | #define SEXP_SBPRINTF_BUFSZ 1024 |
| 50 | |
| 51 | int SEXP_sbprintf_t (SEXP_t *s_exp, strbuf_t *sb) |
| 52 | { |
| 53 | SEXP_val_t v_dsc; |
| 54 | int buflen; |
| 55 | |
| 56 | if (SEXP_rawptr_mask(s_exp->s_type, SEXP_DATATYPEPTR_MASK) != NULL) { |
| 57 | const char *name; |
| 58 | char buffer[64+1]; |
| 59 | |
| 60 | name = SEXP_datatype_name(s_exp->s_type); |
| 61 | buflen = snprintf (buffer, sizeof buffer, |
| 62 | "#d%zu[%s]", strlen (name), name); |
| 63 | |
| 64 | _A((size_t)buflen < sizeof buffer); |
| 65 | |
| 66 | if (strbuf_add (sb, buffer, buflen) != 0) |
| 67 | return (-1); |
| 68 | } |
| 69 | |
| 70 | SEXP_val_dsc (&v_dsc, s_exp->s_valp); |
| 71 | |
| 72 | switch (v_dsc.type) { |
| 73 | case SEXP_VALTYPE_NUMBER: |
| 74 | { |
| 75 | SEXP_numtype_t t; |
| 76 | |
| 77 | t = SEXP_NTYPEP(v_dsc.hdr->size, v_dsc.mem); |
| 78 | |
| 79 | if (t <= SEXP_NUM_UINT16) { |
| 80 | if (t <= SEXP_NUM_UINT8) { |
| 81 | char buffer[6+1]; |
| 82 | |
| 83 | switch (t) { |
| 84 | case SEXP_NUM_BOOL: |
| 85 | buflen = snprintf (buffer, sizeof buffer, |
| 86 | "#%c", SEXP_NCASTP(b ,v_dsc.mem)->n ? 'T':'F'); |
| 87 | break; |
| 88 | case SEXP_NUM_INT8: |
| 89 | buflen = snprintf (buffer, sizeof buffer, |
| 90 | "#d%hhd", SEXP_NCASTP(i8,v_dsc.mem)->n); |
| 91 | break; |
| 92 | case SEXP_NUM_UINT8: |
| 93 | buflen = snprintf (buffer, sizeof buffer, |
| 94 | "#d%hhu", SEXP_NCASTP(u8,v_dsc.mem)->n); |
| 95 | break; |
| 96 | default: |
| 97 | abort (); |
| 98 | } |
| 99 | |
| 100 | _A(buflen >= 0); |
| 101 | _A((size_t)buflen < sizeof buffer); |
| 102 | |
| 103 | if (strbuf_add (sb, (const char *)buffer, (size_t)buflen) != 0) |
| 104 | return (-1); |
| 105 | |
| 106 | } else { |
| 107 | char buffer[8+1]; |
| 108 | |
| 109 | switch (t) { |
| 110 | case SEXP_NUM_INT16: |
| 111 | buflen = snprintf (buffer, sizeof buffer, |
| 112 | "#d%hd", SEXP_NCASTP(i16,v_dsc.mem)->n); |
| 113 | break; |
| 114 | case SEXP_NUM_UINT16: |
| 115 | buflen = snprintf (buffer, sizeof buffer, |
| 116 | "#d%hu", SEXP_NCASTP(u16,v_dsc.mem)->n); |
| 117 | break; |
| 118 | default: |
| 119 | abort (); |
| 120 | } |
| 121 | |
| 122 | _A(buflen >= 0); |
| 123 | _A((size_t)buflen < sizeof buffer); |
| 124 | |
| 125 | if (strbuf_add (sb, (const char *)buffer, (size_t)buflen) != 0) |
| 126 | return (-1); |
| 127 | |
| 128 | } |
| 129 | } else { |
| 130 | if (t <= SEXP_NUM_INT64) { |
| 131 | char buffer[22+1]; |
| 132 | |
| 133 | switch (t) { |
| 134 | case SEXP_NUM_INT32: |
| 135 | buflen = snprintf (buffer, sizeof buffer, |
| 136 | "#d%" PRId32, SEXP_NCASTP(i32,v_dsc.mem)->n); |
| 137 | break; |
| 138 | case SEXP_NUM_UINT32: |
| 139 | buflen = snprintf (buffer, sizeof buffer, |
| 140 | "#d%" PRIu32, SEXP_NCASTP(u32,v_dsc.mem)->n); |
| 141 | break; |
| 142 | case SEXP_NUM_INT64: |
| 143 | buflen = snprintf (buffer, sizeof buffer, |
| 144 | "#d%" PRId64, SEXP_NCASTP(i64,v_dsc.mem)->n); |
| 145 | break; |
| 146 | default: |
| 147 | abort (); |
| 148 | } |
| 149 | |
| 150 | _A(buflen >= 0); |
| 151 | _A((size_t)buflen < sizeof buffer); |
| 152 | |
| 153 | if (strbuf_add (sb, (const char *)buffer, (size_t)buflen) != 0) |
| 154 | return (-1); |
| 155 | |
| 156 | } else { |
| 157 | char buffer[66+1]; |
| 158 | |
| 159 | switch (t) { |
| 160 | case SEXP_NUM_UINT64: |
| 161 | buflen = snprintf (buffer, sizeof buffer, |
| 162 | "#d%" PRIu64, SEXP_NCASTP(u64,v_dsc.mem)->n); |
| 163 | break; |
| 164 | case SEXP_NUM_DOUBLE: |
| 165 | buflen = snprintf (buffer, sizeof buffer, |
| 166 | "#d%g", SEXP_NCASTP(f ,v_dsc.mem)->n); |
| 167 | break; |
| 168 | default: |
| 169 | abort (); |
| 170 | } |
| 171 | |
| 172 | _A(buflen >= 0); |
| 173 | _A((size_t)buflen < sizeof buffer); |
| 174 | |
| 175 | if (strbuf_add (sb, (const char *)buffer, (size_t)buflen) != 0) |
| 176 | return (-1); |
| 177 | |
| 178 | } |
| 179 | } |
| 180 | break; |
| 181 | } |
| 182 | case SEXP_VALTYPE_STRING: |
| 183 | { |
| 184 | char buffer[20+1]; |
| 185 | |
| 186 | buflen = snprintf (buffer, sizeof buffer, "#d%zu:", v_dsc.hdr->size / sizeof (char)); |
| 187 | |
| 188 | _A(buflen >= 0); |
| 189 | _A((size_t)buflen < sizeof buffer); |
| 190 | |
| 191 | if (strbuf_add (sb, (const char *)buffer, (size_t)buflen) != 0) |
| 192 | return (-1); |
| 193 | if (strbuf_add (sb, (const char *)v_dsc.mem, v_dsc.hdr->size / sizeof (char)) != 0) |
| 194 | return (-1); |
| 195 | |
| 196 | break; |
| 197 | } |
| 198 | case SEXP_VALTYPE_LIST: |
| 199 | |
| 200 | if (strbuf_add (sb, "(", 1) != 0) |
| 201 | return (-1); |
| 202 | if (SEXP_rawval_lblk_cb ((uintptr_t)SEXP_LCASTP(v_dsc.mem)->b_addr, (int (*)(SEXP_t *, void *)) SEXP_sbprintf_t, (void *)sb, |
| 203 | SEXP_LCASTP(v_dsc.mem)->offset + 1) != 0) |
| 204 | return (-1); |
| 205 | if (strbuf_trunc (sb, 1) != 0) |
| 206 | return (-1); |
| 207 | if (strbuf_add (sb, ")", 1) != 0) |
| 208 | return (-1); |
| 209 | |
| 210 | break; |
| 211 | default: |
| 212 | abort (); |
| 213 | } |
| 214 | |
| 215 | return (0); |
| 216 | } |
| 217 | |
| 218 | typedef struct { |
| 219 | size_t sz; |
| 220 | FILE *fp; |
| 221 | } __fprintfa_t; |
| 222 | |
| 223 | #define AS(t,p) ((t)(p)) |
| 224 | |
| 225 | static int __SEXP_fprintfa_lmemb (const SEXP_t *s_exp, void *arg) |
| 226 | { |
| 227 | AS(__fprintfa_t *, arg)->sz += SEXP_fprintfa (AS(__fprintfa_t *, arg)->fp, s_exp); |
| 228 | fputc (' ', AS(__fprintfa_t *, arg)->fp); |
| 229 | return (0); |
| 230 | } |
| 231 | |
| 232 | size_t SEXP_fprintfa (FILE *fp, const SEXP_t *s_exp) |
| 233 | { |
| 234 | SEXP_val_t v_dsc; |
| 235 | |
| 236 | if (SEXP_rawptr_mask(s_exp->s_type, SEXP_DATATYPEPTR_MASK) != NULL) |
| 237 | fprintf (fp, "[%s]", SEXP_datatype_name(s_exp->s_type)); |
| 238 | |
| 239 | SEXP_val_dsc (&v_dsc, s_exp->s_valp); |
| 240 | |
| 241 | switch (v_dsc.type) { |
| 242 | case SEXP_VALTYPE_NUMBER: |
| 243 | { |
| 244 | SEXP_numtype_t t; |
| 245 | |
| 246 | t = SEXP_NTYPEP(v_dsc.hdr->size, v_dsc.mem); |
| 247 | |
| 248 | if (t <= SEXP_NUM_UINT16) { |
| 249 | if (t <= SEXP_NUM_UINT8) { |
| 250 | switch (t) { |
| 251 | case SEXP_NUM_BOOL: return fprintf (fp, "#%c", SEXP_NCASTP(b ,v_dsc.mem)->n ? 'T':'F'); |
| 252 | case SEXP_NUM_INT8: return fprintf (fp, "%hhd", SEXP_NCASTP(i8,v_dsc.mem)->n); |
| 253 | case SEXP_NUM_UINT8: return fprintf (fp, "%hhu", SEXP_NCASTP(u8,v_dsc.mem)->n); |
| 254 | } |
| 255 | } else { |
| 256 | switch (t) { |
| 257 | case SEXP_NUM_INT16: return fprintf (fp, "%hd", SEXP_NCASTP(i16,v_dsc.mem)->n); |
| 258 | case SEXP_NUM_UINT16: return fprintf (fp, "%hu", SEXP_NCASTP(u16,v_dsc.mem)->n); |
| 259 | } |
| 260 | } |
| 261 | } else { |
| 262 | if (t <= SEXP_NUM_INT64) { |
| 263 | switch (t) { |
| 264 | case SEXP_NUM_INT32: return fprintf (fp, "%" PRId32, SEXP_NCASTP(i32,v_dsc.mem)->n); |
| 265 | case SEXP_NUM_UINT32: return fprintf (fp, "%" PRIu32, SEXP_NCASTP(u32,v_dsc.mem)->n); |
| 266 | case SEXP_NUM_INT64: return fprintf (fp, "%" PRId64, SEXP_NCASTP(i64,v_dsc.mem)->n); |
| 267 | } |
| 268 | } else { |
| 269 | switch (t) { |
| 270 | case SEXP_NUM_UINT64: return fprintf (fp, "%" PRIu64, SEXP_NCASTP(u64,v_dsc.mem)->n); |
| 271 | case SEXP_NUM_DOUBLE: return fprintf (fp, "%g", SEXP_NCASTP(f ,v_dsc.mem)->n); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | abort (); |
| 277 | } break; |
| 278 | case SEXP_VALTYPE_STRING: |
| 279 | return fprintf (fp, "\"%.*s\"", (int)(v_dsc.hdr->size / sizeof (char)), (char *)v_dsc.mem); |
| 280 | case SEXP_VALTYPE_LIST: |
| 281 | { |
| 282 | __fprintfa_t fpa; |
| 283 | |
| 284 | fpa.sz = 2; |
| 285 | fpa.fp = fp; |
| 286 | fputc ('(', fp); |
| 287 | |
| 288 | SEXP_rawval_lblk_cb ((uintptr_t)SEXP_LCASTP(v_dsc.mem)->b_addr, |
| 289 | (int(*)(SEXP_t *, void *))__SEXP_fprintfa_lmemb, &fpa, |
| 290 | SEXP_LCASTP(v_dsc.mem)->offset + 1); |
| 291 | |
| 292 | fputc (')', fp); |
| 293 | |
| 294 | return (fpa.sz); |
| 295 | } |
| 296 | default: |
| 297 | abort (); |
| 298 | } |
| 299 | |
| 300 | return (0); |
| 301 | } |