Beispiel #1
0
int mrp_attribute_copy_definitions(mrp_attr_def_t *from, mrp_attr_def_t *to)
{
    mrp_attr_def_t *s, *d;

    MRP_ASSERT(to,"invalid argument");

    if (from) {
        for (s = from, d = to;   s->name;   s++, d++) {

            if (!(d->name = mrp_strdup(s->name)))
                goto no_memory;

            d->access = s->access;

            if ((d->type = s->type) != mqi_string)
                d->value = s->value;
            else {
                if (!(d->value.string = mrp_strdup(s->value.string))) {
                    mrp_free((void *)d->name);
                    memset(d, 0, sizeof(*d));
                    goto no_memory;
                }
            }
        }
    }

    return 0;

 no_memory:
    mrp_log_error("Memory alloc failure. Can't copy attribute definition");
    return -1;
}
Beispiel #2
0
int clients_register_player(context_t *ctx,
                            const char *name,
                            const char *service,
                            const char *object)
{
    clients_t *clients;
    player_t *player;

    if (!ctx || !name || !(clients = ctx->clients))
        return -1;

    if (!(player = mrp_allocz(sizeof(player_t))))
        return -1;

    player->ctx = ctx;
    player->name = mrp_strdup(name);
    player->service = service ? mrp_strdup(service) : NULL;
    player->object = object ? mrp_strdup(object) : NULL;

    mrp_htbl_insert(clients->player.name, (void*)player->name, (void*)player);

    mrp_log_info("Mpris2 player '%s' (service '%s' object '%s') registered",
                 player->name, player->service ? player->service : "none",
                 player->object ? player->object : "none");

    if (!clients->deflt) {
        clients->current = clients->deflt = player;
        mrp_log_info("'%s' become the default player", player->name);
    }

    return 0;
}
Beispiel #3
0
mrp_res_attribute_t *mrp_attribute_array_dup(uint32_t dim,
                                 mrp_res_attribute_t *arr)
{
    size_t size;
    uint32_t i;
    mrp_res_attribute_t *sattr, *dattr;
    mrp_res_attribute_t *dup;
    int err;

    size = (sizeof(mrp_res_attribute_t) * (dim + 1));

    if (!(dup = mrp_allocz(size))) {
        err = ENOMEM;
        goto failed;
    }

    for (i = 0; i < dim; i++) {
        sattr = arr + i;
        dattr = dup + i;

        if (!(dattr->name = mrp_strdup(sattr->name))) {
            err = ENOMEM;
            goto failed;
        }

        switch ((dattr->type = sattr->type)) {
        case mrp_string:
            if (!(dattr->string = mrp_strdup(sattr->string))) {
                err = ENOMEM;
                goto failed;
            }
            break;
        case mrp_int32:
            dattr->integer = sattr->integer;
            break;
        case mrp_uint32:
            dattr->type = mrp_uint32;
            dattr->unsignd = sattr->unsignd;
            break;
        case mrp_double:
            dattr->type = mrp_double;
            dattr->floating = sattr->floating;
            break;
        default:
            err = EINVAL;
            goto failed;
        }
    }

    return dup;

 failed:
    mrp_attribute_array_free(dup, dim);
    errno = err;
    return NULL;
}
Beispiel #4
0
mrp_res_string_array_t * mrp_res_list_attribute_names(mrp_res_context_t *cx,
        const mrp_res_resource_t *res)
{
    int i;
    mrp_res_string_array_t *ret;

    if (!cx || !res)
        return NULL;

    ret = mrp_allocz(sizeof(mrp_res_string_array_t));

    if (!ret)
        return NULL;

    ret->num_strings = res->priv->num_attributes;
    ret->strings = mrp_allocz_array(const char *, res->priv->num_attributes);

    if (!ret->strings) {
        mrp_free(ret);
        return NULL;
    }

    for (i = 0; i < res->priv->num_attributes; i++) {
        ret->strings[i] = mrp_strdup(res->priv->attrs[i].name);
        if (!ret->strings[i]) {
            ret->num_strings = i;
            mrp_res_free_string_array(ret);
            return NULL;
        }
    }

    return ret;
}
Beispiel #5
0
static playlist_t *playlist_dup(size_t nlist, playlist_t *lists)
{
    playlist_t *dup = mrp_allocz(sizeof(playlist_t) * (nlist + 1));
    playlist_t *src, *dst;
    size_t i;

    for (i = 0;  i < nlist;  i++) {
        src = lists + i;
        dst = dup + i;

        dst->id = mrp_strdup(src->id);
        dst->name = mrp_strdup(src->name);
    }

    return dup;
}
Beispiel #6
0
static void add_command(client_t *c, int ntoken, char **tokens)
{
    char   command[1024];
    size_t osize, nsize;

    if (c->registered) {
        print(c, "You need to unregister first to modify commands.");
        return;
    }

    if (concat_tokens(command, sizeof(command), ntoken, tokens) == NULL) {
        print(c, "Command too long.");
        return;
    }

    osize = sizeof(*c->commands) *  c->ncommand;
    nsize = sizeof(*c->commands) * (c->ncommand + 1);

    if (!mrp_reallocz(c->commands, osize, nsize)) {
        print(c, "Failed to add new command.");
        return;
    }

    c->commands[c->ncommand] = mrp_strdup(command);

    if (c->commands[c->ncommand] != NULL) {
        c->ncommand++;
        print(c, "Command '%s' added to command set.", command);
    }
    else
        print(c, "Failed to register new command.");
}
Beispiel #7
0
int mrp_attribute_set_values(mrp_attr_t      *values,
                             uint32_t          nattr,
                             mrp_attr_def_t   *defs,
                             mrp_attr_value_t *attrs)
{
    mrp_attr_def_t *adef;
    mrp_attr_value_t *vsrc;
    mrp_attr_value_t *vdst;
    uint32_t i;


    MRP_ASSERT(!nattr || (nattr > 0 && defs && attrs),
               "invalid arguments");

    for (i = 0;  i < nattr;  i++) {
        adef = defs  + i;
        vdst = attrs + i;

        if (!(adef->access & MRP_RESOURCE_WRITE) ||
            !(vsrc = get_attr_value_from_list(values, adef->name, adef->type)))
            vsrc = &adef->value; /* default value */

        if (adef->type !=  mqi_string)
            *vdst = *vsrc;
        else if (vdst->string != vsrc->string) {
            /* if the string is not the same, change it */
            mrp_free((void *)vdst->string);
            if (!(vdst->string = mrp_strdup(vsrc->string)))
                return -1;
        }
    }

    return 0;
}
Beispiel #8
0
static int basic_tests(int n)
{
    void **ptrs;
    char   buf[1024], *p;
    int    i;

    mrp_mm_config(MRP_MM_DEBUG);

    ptrs = mrp_allocz(n * sizeof(*ptrs));

    if (ptrs == NULL)
        fatal("Failed to allocate pointer table.");

    for (i = 0; i < n; i++) {
        snprintf(buf, sizeof(buf), "#%d: message number %d (0x%x)", i, i, i);

        p = ptrs[i] = mrp_strdup(buf);

        if (p != NULL) {
            if (!strcmp(buf, p)) {
                printf("'%s' was duplicated as '%s'\n", buf, p);
            }
            else {
                printf("'%s' was incorrectly duplicated as '%s'\n", buf, p);
                return FALSE;
            }
        }
        else {
            printf("failed to duplicate '%s'\n", buf);
            return FALSE;
        }
    }

    mrp_mm_check(stdout);

    for (i = 0; i < n; i += 2) {
        mrp_free(ptrs[i]);
        ptrs[i] = NULL;
    }

    mrp_mm_check(stdout);

    for (i = 0; i < n; i++) {
        mrp_free(ptrs[i]);
        ptrs[i] = NULL;
    }

    mrp_mm_check(stdout);

    mrp_free(ptrs);

    mrp_mm_check(stdout);

    return TRUE;
}
Beispiel #9
0
static int add_rule(const char *func, const char *file, int line, int off)
{
    mrp_htbl_t  *ht;
    char        *rule, *r, buf[PATH_MAX * 2];

    if (rules_on == NULL)
        if (!init_rules())
            return FALSE;

    r = rule = NULL;

    if (!off)
        ht = rules_on;
    else
        ht = rules_off;

    if (func != NULL && file == NULL && line == 0) {
        r    = mrp_htbl_lookup(ht, (void *)func);
        rule = (char *)func;
    }
    else if (func != NULL && file != NULL && line == 0) {
        snprintf(buf, sizeof(buf), "%s@%s", func, file);
        r    = mrp_htbl_lookup(ht, (void *)buf);
        rule = buf;
    }
    else if (func == NULL && file != NULL && line == 0) {
        snprintf(buf, sizeof(buf), "@%s", file);
        r    = mrp_htbl_lookup(ht, (void *)buf);
        rule = buf;
    }
    else if (func == NULL && file != NULL && line > 0) {
        snprintf(buf, sizeof(buf), "%s:%d", file, line);
        r    = mrp_htbl_lookup(ht, (void *)buf);
        rule = buf;
    }

    if (r != NULL)
        return FALSE;

    rule = mrp_strdup(rule);
    if (rule == NULL)
        return FALSE;

    if (mrp_htbl_insert(ht, rule, rule)) {
        mrp_debug_stamp++;

        return TRUE;
    }
    else {
        mrp_free(rule);

        return FALSE;
    }
}
Beispiel #10
0
static int obj_setup(void *ptr)
{
    static int  idx = 0;
    obj_t      *obj = ptr;

    snprintf(obj->name, sizeof(obj->name), NAME_FORMAT, idx);
    obj->i = idx;
    obj->d = 2.0 * idx;
    obj->s = mrp_strdup(obj->name);
    obj->p = ptr;

    return TRUE;
}
Beispiel #11
0
static int priv_res_to_mrp_res(uint32_t id, resource_def_t *src, mrp_res_resource_t *dst)
{
    dst->name  = mrp_strdup(src->name);
    dst->state = MRP_RES_RESOURCE_LOST;
    dst->priv->mandatory = false;
    dst->priv->shared = false;

    dst->priv->server_id = id;

    dst->priv->num_attributes = src->num_attrs;
    dst->priv->attrs = src->attrs;
    return 0;
}
Beispiel #12
0
static int cdpa_open(rnc_dev_t *dev, const char *device)
{
    cdpa_t *cdpa;
    char   *msg;

    mrp_debug("opening device '%s' with cdparanoia", device);

    msg  = NULL;
    cdpa = dev->data = mrp_allocz(sizeof(*cdpa));

    if (cdpa == NULL)
        goto fail;

    cdpa->cdio = cdio_open(device, DRIVER_LINUX);

    if (cdpa->cdio == NULL) {
        msg = "failed open device";
        goto fail;
    }

    cdpa->cdda = cdio_cddap_identify_cdio(cdpa->cdio, CDDA_MESSAGE_LOGIT, &msg);

    if (cdpa->cdda == NULL)
        goto fail;

    cdio_cddap_free_messages(msg);
    msg = NULL;

    if (cdio_cddap_open(cdpa->cdda) < 0) {
        msg = "failed to CDDAP-open device";
        goto fail;
    }

    cdpa->cdpa = cdio_paranoia_init(cdpa->cdda);

    if (cdpa->cdpa == NULL) {
        msg = "failed to initialize cd-paranoia";
        goto fail;
    }

    cdpa->ctrack = -1;

    return 0;

 fail:
    if (cdpa != NULL && msg != NULL)
        cdpa->errmsg = mrp_strdup(msg);

    return -1;
}
Beispiel #13
0
void utterance_start(context_t *ctx)
{
    decoder_set_t *decset;
    decoder_t *dec;
    char utid[256];

    if (ctx && (decset = ctx->decset) && (dec = decset->curdec)) {
        if (!dec->utter) {
            snprintf(utid, sizeof(utid), "%07u-%s", dec->utid++, dec->name);
            ps_start_utt(dec->ps, mrp_strdup(utid));
            dec->utter = true;
        }
    }
}
Beispiel #14
0
static rnc_buf_t *buf_alloc(const char *name, buf_api_t *api, size_t size)
{
    rnc_buf_t *b;

    if ((b = mrp_allocz(size)) == NULL || (b->name = mrp_strdup(name)) == NULL)
        goto nomem;

    b->api = api;

    return b;

 nomem:
    mrp_free(b);
    return NULL;
}
Beispiel #15
0
static rnc_meta_t *tracklist_lookup(rnc_metadb_t *db, int track)
{
    tracklist_t *t = (tracklist_t *)db->data;
    rnc_meta_t  *m;

    if (t == NULL)
        goto invalid;

    track--;

    if (track < 0 || track >= t->ntrack)
        goto noentry;

    m = mrp_allocz(sizeof(*m));

    if (m == NULL)
        goto nomem;

    m->track  = track;
    m->title  = mrp_strdup(t->tracks[track]);
    m->album  = mrp_strdup(t->album);
    m->artist = mrp_strdup(t->artist);
    m->genre  = mrp_strdup(t->genre);

    m->date.tm_year = t->year;

    return m;

 invalid:
    errno = EINVAL;
    return NULL;
 noentry:
    errno = ENOENT;
 nomem:
    return NULL;
}
Beispiel #16
0
static int set_address(transport_lua_t *t, const char *address,
                       char *err, size_t elen, int overwrite)
{
    MRP_LUA_ERRUSE(err, elen);

    if (t->address != NULL) {
        if (t->address == address) {
            if (t->alen > 0 && t->atype != NULL)
                return 1;
        }
        else {
            if (!overwrite)
                return mrp_lua_error(-1, t->L,
                                     "address already set ('%s')", t->address);
        }
    }

    if (t->address != address) {
        mrp_free(t->address);
        t->address = NULL;
    }

    t->atype = NULL;
    t->alen  = 0;

    if (address == NULL)
        return 1;

    if ((t->address = mrp_strdup(address)) == NULL)
        return mrp_lua_error(-1, t->L,
                             "failed to store address '%s'", address);

    t->alen = mrp_transport_resolve(NULL, t->address, &t->addr,
                                    sizeof(t->addr), &t->atype);

    if (t->alen <= 0) {
        if (address != t->address)
            mrp_free(t->address);

        t->atype = NULL;
        t->alen  = 0;

        return mrp_lua_error(-1, t->L, "failed to resolve '%s'", address);
    }

    return 0;
}
Beispiel #17
0
int dbusif_create(context_t *ctx, mrp_mainloop_t *ml)
{
    dbusif_t *dbusif = NULL;

    if (!(dbusif = mrp_allocz(sizeof(dbusif_t))))
        return -1;

    dbusif->bustype = mrp_strdup("session");
    dbusif->dbus = mrp_dbus_get(ml, dbusif->bustype, NULL);

    if (!dbusif->dbus) {
        mrp_log_error("mpris2 plugin: failed to obtain DBus");
        mrp_free(dbusif);
        return -1;
    }

    ctx->dbusif = dbusif;

    return 0;
}
Beispiel #18
0
void clients_player_appeared(context_t *ctx,
                             const char *name,
                             const char *address)
{
    clients_t *clients;
    player_t *player;

    if (ctx && (clients = ctx->clients) && clients->player.name) {
        if ((player = mrp_htbl_lookup(clients->player.name, (void *)name))) {
            mrp_free((void *)player->address);
            player->address = mrp_strdup(address);

            mrp_htbl_insert(clients->player.addr, (void *)player->address,
                            player);

            mrp_log_info("mrpis2 client '%s' appeared (address %s)",
                         name, address);

            dbusif_query_player_properties(player);
        }
    }
}
Beispiel #19
0
int mrp_res_set_attribute_string(mrp_res_context_t *cx,
        mrp_res_attribute_t *attr, const char *value)
{
    char *str;

    if (!cx || !attr)
        return -1;

    /* check the attribute type */

    if (attr->type != mrp_string)
        return -1;

    str = mrp_strdup(value);

    if (!str)
        return -1;

    mrp_free((void *) attr->string);
    attr->string = str;

    return 0;
}
Beispiel #20
0
static void set_client_defaults(client_t *c, const char *argv0)
{
    int i;

    c->app_class    = "player";
    c->app_name     = strrchr(argv0, '/');

    if (c->app_name != NULL)
        c->app_name++;
    else
        c->app_name = argv0;

    c->commands = mrp_allocz(sizeof(default_commands));
    c->ncommand = MRP_ARRAY_SIZE(default_commands);

    for (i = 0; i < c->ncommand; i++) {
        c->commands[i] = mrp_strdup(default_commands[i]);
        if (c->commands[i] == NULL) {
            print(c, "Failed to initialize default command set.");
            exit(1);
        }
    }
}
Beispiel #21
0
static uint32_t synth_load(const char *path, int cache, void *api_data)
{
    synth_t *synth = (synth_t *)api_data;
    sound_t *snd;

    if (mrp_reallocz(synth->sounds, synth->nsound, synth->nsound + 1) != NULL) {
        snd = synth->sounds + synth->nsound;

        snd->path  = mrp_strdup(path);
        snd->cache = cache ? 1 : 0;
        snd->id    = SYNTH_TYPE_SOUND | synth->nsound;

        if (snd->path != NULL) {
            synth->nsound++;

            return snd->id;
        }

        mrp_reallocz(synth->sounds, synth->nsound + 1, synth->nsound);
    }

    return SRS_VOICE_INVALID;
}
Beispiel #22
0
static tracklist_t *tracklist_parse(const char *path)
{
#define CHECK_STRTAG(_tag, _member)             \
    if (!strcasecmp(tag, _tag)) {               \
        mrp_free(t->_member);                   \
        t->_member = mrp_strdup(value);         \
        continue;                               \
    }

#define CHECK_INTTAG(_tag, _member)                             \
    if (!strcasecmp(tag, _tag)) {                               \
        t->_member = (int)strtoul(value, &e, 10);               \
        if (e && *e)                                            \
            goto invalid;                                       \
        continue;                                               \
    }

    tracklist_t *t;
    char         buf[16 * 1024], *tag, *sep, *value, *p, *e;
    int          fd, n;

    fd = open(path, O_RDONLY);

    if (fd < 0)
        goto failed;

    n = read(fd, buf, sizeof(buf) - 1);

    close(fd);

    if (n < 0)
        goto failed;

    if (n >= (int)sizeof(buf) - 1)
        goto nospace;

    buf[n] = '\0';

    t = mrp_allocz(sizeof(*t));

    if (t == NULL)
        goto nomem;

    p = buf;
    while (p && *p) {
        while (*p == ' ' || *p == '\t')
            p++;

        if (*p == '\n') {
            p++;
            continue;
        }

        tag = p;

        while (*p && *p != ':' && *p != '.' && *p != '\n')
            p++;

        if (*p == '\n')
            goto invalid;

        sep = p;
        p++;

        while (*p == ' ' || *p == '\t')
            p++;

        value = p;

        while (*p && *p != '\n')
            p++;

        if (p != '\0')
            *p++ = '\0';

        e = p - 1;
        while (e > value && (*e == ' ' || *e == '\t'))
            *e-- = '\0';

        if (*sep == ':') {
            *sep = '\0';

            CHECK_STRTAG("Album" , album);
            CHECK_STRTAG("Artist", artist);
            CHECK_STRTAG("Genre" , genre);
            CHECK_INTTAG("Year"  , year);

            mrp_log_warning("Ignoring unknown tag '%s'...", tag);
            continue;
        }

        if (*sep == '.') {
            *sep = '\0';

            n = strtoul(tag, &e, 10);

            if (e && *e)
                goto invalid;

            if (n < 1 || n > 99)         /* max. CDDA tracks */
                goto invalid;

            if (n > t->ntrack) {
                if (!mrp_reallocz(t->tracks, t->ntrack, n))
                    goto nomem;
                t->ntrack = n;
            }

            mrp_free(t->tracks[n - 1]);
            t->tracks[n - 1] = mrp_strdup(value);
        }
    }

    return t;

 nospace:
    errno = ENOBUFS;
 failed:
 nomem:
    return NULL;

 invalid:
    errno = EINVAL;
    tracklist_free(t);
    return NULL;
}
Beispiel #23
0
static int resource_set_add_resource(lua_State *L)
{
    int narg;
    resource_set_lua_t *rset;
    resource_lua_t *resource;
    const char *resource_name;
    bool shared = FALSE;
    bool mandatory = TRUE;
    mrp_attr_t attribute_list[MAX_ATTRS], *attrs;

    mrp_debug("> add_resource");

    narg = lua_gettop(L);

    if (narg != 2)
        return luaL_error(L, "expecting one argument");

    rset = resource_set_lua_check(L, 1);

    if (!rset)
        goto error;

    /* the argument should be a table with at least "resource_name" index */

    if (!lua_istable(L, -1))
        return luaL_error(L, "argument error -- not a table");

    lua_pushstring(L, "resource_name");
    lua_gettable(L, -2);

    if (!lua_isstring(L, -1))
        return luaL_error(L, "'resource_name' is a mandatory field");

    resource_name = lua_tostring(L, -1);
    lua_pop(L, 1);

    lua_pushstring(L, "mandatory");
    lua_gettable(L, -2);

    if (lua_isboolean(L, -1)) {
        mandatory = lua_toboolean(L, -1);
    }
    lua_pop(L, 1);

    lua_pushstring(L, "shared");
    lua_gettable(L, -2);

    if (lua_isboolean(L, -1)) {
        shared = lua_toboolean(L, -1);
    }
    lua_pop(L, 1);

    /* create resource object and add it to the resource table in the resource
     * set object */

    resource = (resource_lua_t *) mrp_lua_create_object(L, RESOURCE_LUA_CLASS,
            NULL, 0);

    if (!resource)
        goto error;

    /* mrp_lua_object_ref_value(resource, L, 0); */

    resource->mandatory = mandatory;
    resource->shared = shared;
    resource->acquired = FALSE;
    resource->available = FALSE;
    resource->resource_name = mrp_strdup(resource_name);

    if (!resource->resource_name)
        goto error;

    resource->parent = rset;
    resource->L = L;

    resource->real_attributes = (attribute_lua_t *) mrp_lua_create_object(L,
            ATTRIBUTE_LUA_CLASS, NULL, 0);

    if (!resource->real_attributes)
        goto error;

    /* mrp_lua_object_ref_value(resource->real_attributes, L, 0); */

    resource->real_attributes->L = L;
    resource->real_attributes->parent = resource;
    resource->real_attributes->resource_set = rset;
    resource->real_attributes->initialized = TRUE;

    attrs = mrp_resource_set_read_all_attributes(rset->resource_set,
            resource->resource_name, MAX_ATTRS-1, attribute_list);

    if (mrp_resource_set_add_resource(rset->resource_set,
            resource->resource_name, shared, attrs, mandatory) < 0)
        goto error;

    /* add to resource map */

    mrp_debug("inserted resource %s to %p", resource->resource_name, rset);
    mrp_htbl_insert(rset->resources, resource->resource_name, resource);

    return 0;

error:
    /* TODO: clean up the already allocated objects */

    return luaL_error(L, "internal resource library error");
}
static int add_decoder(int ncfg,
                       srs_cfg_t *cfgs,
                       const char *name,
                       size_t *pndec,
                       options_decoder_t **pdecs)
{
    int i;
    srs_cfg_t *cfg;
    const char *key;
    const char *value;
    size_t pfxlen;
    char pfx[1024];
    options_decoder_t *decs, *dec;
    size_t ndec, size;
    const char *hmm = NULL;
    const char *lm = NULL;
    const char *dict = NULL;
    const char *fsg = NULL;

    pfxlen = snprintf(pfx, sizeof(pfx), SPHINX_PREFIX "%s.", name);

    for (i = 0;  i < ncfg;  i++) {
        cfg = cfgs + i;
        key = cfg->key + pfxlen;
        value = cfg->value;

        if (!strncmp(cfg->key, pfx, pfxlen)) {

            switch (key[0]) {

            case 'd':
                if (!strcmp(key, "dict"))
                    dict = value;
                break;

            case 'f':
                if (!strcmp(key, "fsg"))
                    fsg = value;
                break;

            case 'h':
                if (!strcmp(key, "hmm"))
                    hmm = value;
                break;

            case 'l':
                if (!strcmp(key, "lm"))
                    lm = value;
                break;
            }
        }
    }

    ndec = *pndec;
    size = sizeof(options_decoder_t) * (ndec + 1);

    if (lm && dict && (decs = mrp_realloc(*pdecs, size))) {
        dec = decs + ndec++;

        dec->name = mrp_strdup(name);
        dec->hmm  = hmm ? mrp_strdup(hmm) : NULL;
        dec->lm   = mrp_strdup(lm);
        dec->dict = mrp_strdup(dict);
        dec->fsg  = fsg ? mrp_strdup(fsg) : NULL;

        *pndec = ndec;
        *pdecs = decs;

        return 0;
    }

    return -1;
}
int options_create(context_t *ctx, int ncfg, srs_cfg_t *cfgs)
{
    options_t *opts;
    srs_cfg_t *cfg;
    const char *key;
    const char *value;
    char *e;
    bool verbose;
    int i;
    int sts;
    size_t pfxlen;
    size_t ndec;
    options_decoder_t *decs;
    char buf[65536];

    if (!ctx) {
        errno = EINVAL;
        return -1;
    }

    pfxlen = strlen(SPHINX_PREFIX);

    if (!(opts = mrp_allocz(sizeof(options_t))) ||
        !(decs = mrp_allocz(sizeof(options_decoder_t))))
        return -1;

    ndec = 1;
    decs->name = mrp_strdup("default");
    decs->hmm = mrp_strdup(DEFAULT_HMM);
    decs->lm = mrp_strdup(DEFAULT_LM);
    decs->dict = mrp_strdup(DEFAULT_DICT);
    decs->fsg = NULL;

    opts->srcnam = NULL;
    opts->audio = NULL;
    opts->logfn = mrp_strdup("/dev/null");
    opts->topn = 12;
    opts->rate = 16000;
    opts->silen = 1.0;

    verbose = false;
    sts = 0;

    for (i = 0;  i < ncfg;  i++) {
        cfg = cfgs + i;
        key = cfg->key + pfxlen;
        value = cfg->value;

        if (!strncmp(cfg->key, SPHINX_PREFIX, pfxlen)) {

            switch (key[0]) {

            case 'd':
                if (!strcmp(key, "dict")) {
                    mrp_free((void *)decs->dict);
                    decs->dict = mrp_strdup(value);
                }
                else if (!strncmp(key, "decoder", 7)) {
                    add_decoder(ncfg, cfgs, value, &ndec, &decs);
                }
                break;

            case 'f':
                if (!strcmp(key, "fsg")) {
                    mrp_free((void *)decs->fsg);
                    decs->fsg = mrp_strdup(value);
                }
                break;

            case 'h':
                if (!strcmp(key, "hmm")) {
                    mrp_free((void *)decs->hmm);
                    decs->hmm = mrp_strdup(value);
                }
                break;

            case 'l':
                if (!strcmp(key, "lm")) {
                    mrp_free((void *)decs->lm);
                    decs->lm = mrp_strdup(value);
                }
                break;

            case 'p':
                if (!strcmp(key, "pulsesrc")) {
                    mrp_free((void *)opts->srcnam);
                    opts->srcnam = mrp_strdup(value);
                }
                break;

            case 'r':
                if (!strcmp(key, "record")) {
                    mrp_free((void *)opts->audio);
                    opts->audio = mrp_strdup(value);
                }
                break;

            case 's':
                if (!strcmp(key, "samplerate")) {
                    opts->rate = strtoul(value, &e, 10);
                    if (e[0] || e == value ||
                        opts->rate < 8000 || opts->rate > 4800)
                    {
                        mrp_log_error("invalid value %s for samplerate",value);
                        sts = -1;
                    }
                }
                break;

            case 't':
                if (!strcmp(key, "topn")) {
                    opts->topn = strtoul(value, &e, 10);
                    if (e[0] || e == value ||
                        opts->topn < 1 || opts->topn > 100)
                    {
                        mrp_log_error("invalid value %s for topn", value);
                        sts = -1;
                    }
                }
                break;

            default:
                // cfg->used = FALSE;
                break;

            } /* switch key */
        }
    } /* for cfg */

    opts->ndec = ndec;
    opts->decs = decs;

    if (sts == 0) {
        print_decoders(opts->ndec, opts->decs, sizeof(buf), buf);

        mrp_log_info("topn: %u\n"
                     "   pulseaudio source name: %s\n"
                     "   sample rate: %.1lf KHz\n"
                     "   audio recording file: %s\n"
                     "%s",
                     opts->topn,
                     opts->srcnam ? opts->srcnam : "<default-source>",
                     (double)opts->rate / 1000.0,
                     opts->audio,
                     buf);
    }

    ctx->opts = opts;
    ctx->verbose = verbose;

    return sts;
}
Beispiel #26
0
static int resource_set_lua_create(lua_State *L)
{
    char e[128] = "";
    resource_set_lua_t *rset;
    int narg;
    mrp_htbl_config_t conf;

    mrp_debug("create");

    narg = lua_gettop(L);

    rset = (resource_set_lua_t *) mrp_lua_create_object(L,
            RESOURCE_SET_LUA_CLASS, NULL, 0);

    if (!rset)
        return luaL_error(L, "could not create Lua object");

    rset->L = L;

    /* user can affect these values */
    rset->zone = mrp_strdup("default");
    rset->application_class = NULL;
    rset->autorelease = FALSE;
    rset->dont_wait = FALSE;
    rset->priority = 0;
    rset->committed = FALSE;
    rset->initialized = FALSE;

    switch (narg) {
    case 2:
        /* argument table */
        if (mrp_lua_init_members(rset, L, -2, e, sizeof(e)) != 1)
            return luaL_error(L, "failed to initialize resource members (%s)",
                    e);
        break;
    default:
        return luaL_error(L, "expecting a constructor argument, "
                          "got %d", narg);
    }

    if (rset->application_class == NULL)
        return luaL_error(L, "application_class is a mandatory parameter");

    if (rset->priority < 0)
        rset->priority = 0;

    /* initial state, these cannot be set by user */
    rset->available = FALSE;
    rset->acquired = FALSE;

    /* initialize resource map */
    conf.nbucket = 0;
    conf.nentry = 10;
    conf.comp = mrp_string_comp;
    conf.hash = mrp_string_hash;
    conf.free = htbl_free_resource;

    rset->resources = mrp_htbl_create(&conf);
    if (!rset->resources)
        goto error;

    /* do the actual resource work */

    if (!client) {
        /* create the resource client */

        client = mrp_resource_client_create("lua", NULL);

        if (!client)
            goto error;
    }

    rset->resource_set = mrp_resource_set_create(client, rset->autorelease,
            rset->dont_wait, rset->priority, event_cb, rset);

    if (rset->resource_set)
        n_sets++;
    else
        goto error;

    rset->initialized = TRUE;

    mrp_lua_push_object(L, rset);

    return 1;

error:
    return luaL_error(L, "internal resource library error");
}
Beispiel #27
0
bool mrp_funcbridge_call_from_c(lua_State *L,
                                mrp_funcbridge_t *fb,
                                const char *signature,
                                mrp_funcbridge_value_t *args,
                                char *ret_type,
                                mrp_funcbridge_value_t *ret_value)
{
    char t;
    int i;
    int sp;
    mrp_funcbridge_value_t *a;
    int sts;
    bool success;

    if (!fb)
        success = false;
    else {
        switch (fb->type) {

        case MRP_C_FUNCTION:
            if (!strcmp(signature, fb->c.signature))
                success = fb->c.func(L, fb->c.data, signature, args, ret_type,
                                     ret_value);
            else {
                *ret_type = MRP_FUNCBRIDGE_STRING;
                ret_value->string = mrp_strdup("mismatching signature "
                                               "@ C invocation");
                success = false;
            }
            break;

        case MRP_LUA_FUNCTION:
            sp = lua_gettop(L);
            mrp_funcbridge_push(L, fb);
            lua_rawgeti(L, -1, 1);
            luaL_checktype(L, -1, LUA_TFUNCTION);
            for (i = 0;   (t = signature[i]);   i++) {
                a = args + i;
                switch (t) {
                case MRP_FUNCBRIDGE_STRING:
                    lua_pushstring(L, a->string);
                    break;
                case MRP_FUNCBRIDGE_INTEGER:
                    lua_pushinteger(L, a->integer);
                    break;
                case MRP_FUNCBRIDGE_FLOATING:
                    lua_pushnumber(L, a->floating);
                    break;
                case MRP_FUNCBRIDGE_BOOLEAN:
                    lua_pushboolean(L, a->boolean);
                    break;
                case MRP_FUNCBRIDGE_OBJECT:
                    mrp_lua_push_object(L, a->pointer);
                    break;
                default:
                    success = false;
                    goto done;
                }
            }

            sts = lua_pcall(L, i, 1, 0);

            MRP_ASSERT(!sts || (sts && lua_type(L, -1) == LUA_TSTRING),
                       "lua pcall did not return error string when failed");

            switch (lua_type(L, -1)) {
            case LUA_TSTRING:
                *ret_type = MRP_FUNCBRIDGE_STRING;
                ret_value->string = mrp_strdup(lua_tolstring(L, -1, NULL));
                break;
            case LUA_TNUMBER:
                *ret_type = MRP_FUNCBRIDGE_FLOATING;
                ret_value->floating = lua_tonumber(L, -1);
                break;
            case LUA_TBOOLEAN:
                *ret_type = MRP_FUNCBRIDGE_BOOLEAN;
                ret_value->boolean = lua_toboolean(L, -1);
                break;
            default:
                *ret_type = MRP_FUNCBRIDGE_NO_DATA;
                memset(ret_value, 0, sizeof(*ret_value));
                break;
            }
            success = !sts;
        done:
            lua_settop(L, sp);
            break;

        default:
            success = false;
            break;
        }
    }

    return success;
}