Changeset 6be4142 in mainline for uspace/app/bithenge/transform.c


Ignore:
Timestamp:
2012-08-12T04:53:47Z (12 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
1b6b76d
Parents:
0153c87
Message:

Bithenge: print transform errors; fixes and fat.bh improvements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bithenge/transform.c

    r0153c87 r6be4142  
    3737#include <assert.h>
    3838#include <errno.h>
     39#include <stdarg.h>
    3940#include <stdlib.h>
    4041#include "blob.h"
     42#include "print.h"
    4143#include "transform.h"
    4244
     
    187189                bithenge_scope_inc_ref(outer);
    188190        self->outer = outer;
     191        self->error = NULL;
    189192        self->barrier = false;
    190193        self->num_params = 0;
     
    209212        bithenge_scope_dec_ref(self->outer);
    210213        free(self->params);
     214        free(self->error);
    211215        free(self);
    212216}
     
    218222{
    219223        return self->outer;
     224}
     225
     226/** Get the error message stored in the scope, which may be NULL. The error
     227 * message only exists as long as the scope does.
     228 * @param scope The scope to get the error message from.
     229 * @return The error message, or NULL. */
     230const char *bithenge_scope_get_error(bithenge_scope_t *scope)
     231{
     232        return scope->error;
     233}
     234
     235/** Set the error message for the scope. The error message is stored in the
     236 * outermost scope, but if any scope already has an error message this error
     237 * message is ignored.
     238 * @param scope The scope.
     239 * @param format The format string.
     240 * @return EINVAL normally, or another error code from errno.h. */
     241int bithenge_scope_error(bithenge_scope_t *scope, const char *format, ...)
     242{
     243        if (scope->error)
     244                return EINVAL;
     245        while (scope->outer) {
     246                scope = scope->outer;
     247                if (scope->error)
     248                        return EINVAL;
     249        }
     250        size_t space_left = 256;
     251        scope->error = malloc(space_left);
     252        if (!scope->error)
     253                return ENOMEM;
     254        char *out = scope->error;
     255        va_list ap;
     256        va_start(ap, format);
     257
     258        while (*format) {
     259                if (format[0] == '%' && format[1] == 't') {
     260                        format += 2;
     261                        int rc = bithenge_print_node_to_string(&out,
     262                            &space_left, BITHENGE_PRINT_PYTHON,
     263                            va_arg(ap, bithenge_node_t *));
     264                        if (rc != EOK) {
     265                                va_end(ap);
     266                                return rc;
     267                        }
     268                } else {
     269                        const char *end = str_chr(format, '%');
     270                        if (!end)
     271                                end = format + str_length(format);
     272                        size_t size = min((size_t)(end - format),
     273                            space_left - 1);
     274                        memcpy(out, format, size);
     275                        format = end;
     276                        out += size;
     277                        space_left -= size;
     278                }
     279        }
     280        *out = '\0';
     281
     282        va_end(ap);
     283        return EINVAL;
    220284}
    221285
Note: See TracChangeset for help on using the changeset viewer.