Пример #1
0
void test_encode (void)
{
    int rlen;
    char *r;
    char p[] = "abcdefghijklmnop";
    char q[] = "";
    bool eof;

    char *json = zio_json_encode (p, strlen (p), false);
    if (!json)
        BAIL_OUT ("zio_json_encode failed");
    ok (json != NULL,
        "zio_json_encode works");
    diag ("%s", json);
    r = NULL;
    eof = true;
    rlen = zio_json_decode (json, (void **)&r, &eof);
    ok (rlen == strlen (p) && r != NULL && strcmp (r, p) == 0 && eof == false,
        "zio_json_decode worked");
    free (r);

    eof = true;
    rlen = zio_json_decode (json, NULL, &eof);
    ok (rlen == 0 && eof == false,
        "zio_json_decode worked with NULL data return arg");

    free (json);

    json = zio_json_encode (q, strlen (q), true);
    if (!json)
        BAIL_OUT ("zio_json_encode failed");
    ok (json != NULL,
        "zio_json_encode works on zero length data");
    diag ("%s", json);
    r = NULL;
    eof = false;
    rlen = zio_json_decode (json, (void **)&r, &eof);
    ok (rlen == strlen (q) && r != NULL && strcmp (r, q) == 0 && eof == true,
        "zio_json_decode worked");
    free (r);

    free (json);

    json = zio_json_encode (NULL, 0, true);
    if (!json)
        BAIL_OUT ("zio_json_encode failed");
    ok (json != NULL,
        "zio_json_encode works on NULL data");
    diag ("%s", json);
    rlen = zio_json_decode (json, (void **)&r, NULL);
    ok (rlen == 0 && r != NULL && strcmp (r, q) == 0,
        "zio_json_decode returned empty string");
    free (r);
    free (json);
}
Пример #2
0
int kz_get (kz_t *kz, char **datap)
{
    char *json_str = NULL;
    char *data;
    int len = -1;

    if (!datap || (kz->flags & KZ_FLAGS_RAW)) {
        errno = EINVAL;
        goto done;
    }
    if (kz->eof)
        return 0;
    if ((kz->flags & KZ_FLAGS_NONBLOCK))
        json_str = getnext (kz);
    else
        json_str = getnext_blocking (kz);
    if (!json_str)
        goto done;
    if ((len = zio_json_decode (json_str, (void **) &data, &kz->eof)) < 0) {
        errno = EPROTO;
        goto done;
    }
    *datap = data;
done:
    if (json_str)
        free (json_str);
    return len;
}
Пример #3
0
/* Note: return value of this function is ignored by libsubprocess.
 * Also: zio_json_decode() returns -1 on error, 0 on eof, strlen(s) on
 * success; caller must free 's'.
 */
static int subprocess_io_cb (struct subprocess *p, const char *json_str)
{
    runlevel_t *r;
    json_object *o = NULL;
    const char *name;
    int len;
    bool eof;
    char *s = NULL, *argz = NULL, *line = NULL;
    size_t argz_len;

    r = subprocess_get_context (p, "runlevel_t");
    assert (r != NULL);

    if (!r->io_cb)
        goto done;
    if (!(o = Jfromstr (json_str)) || !Jget_str (o, "name", &name))
        goto done;
    len = zio_json_decode (json_str, (void **)&s, &eof);
    if (len <= 0 || !s || !*s || s[len] != '\0')
        goto done;
    if (argz_create_sep (s, '\n', &argz, &argz_len) != 0)
        goto done;
    while ((line = argz_next (argz, argz_len, line)) && *line)
        r->io_cb (r, name, line, r->io_cb_arg);
done:
    if (s)
        free (s);
    if (argz)
        free (argz);
    Jput (o);
    return 0;
}
Пример #4
0
/*
 *  Write json string to this zio object, buffering unwritten data.
 */
int zio_write_json (zio_t *zio, const char *json_str)
{
    char *s = NULL;
    int len, rc = 0;
    bool eof;

    if ((zio == NULL) || (zio->magic != ZIO_MAGIC) || !zio_writer (zio)) {
        errno = EINVAL;
        return (-1);
    }
    len = zio_json_decode (json_str, (void **)&s, &eof);
    if (len < 0) {
        errno = EINVAL;
        return (-1);
    }
    if (eof)
        zio_set_eof (zio);
    if (len > 0)
        rc = zio_write_internal (zio, s, len);
    else if (zio_write_pending (zio))
        zio_writer_schedule (zio);

    free (s);
    return rc;
}
Пример #5
0
static int testio_cb (struct subprocess *p, const char *json_str)
{
    char **bufp = subprocess_get_context (p, "io");
    bool eof;
    if (*bufp == NULL)
        zio_json_decode (json_str, (void **) bufp, &eof);
    return 0;
}
Пример #6
0
bool zio_json_eof (const char *json_str)
{
    bool eof;

    if (zio_json_decode (json_str, NULL, &eof) < 0)
        return false;
    return eof;
}
Пример #7
0
/*
 *  Default handler for stdout/err: send output directly into
 *   stderr of caller...
 */
static int send_output_to_stream (const char *name, const char *json_str)
{
    FILE *fp = stdout;
    char *s = NULL;
    bool eof;

    int len = zio_json_decode (json_str, (void **) &s, &eof);

    if (strcmp (name, "stderr") == 0)
        fp = stderr;

    if (len > 0)
        fputs (s, fp);
    if (eof)
        fclose (fp);

    free (s);
    return (len);
}