broken code, but added attempt at writing grammar
This commit is contained in:
parent
fb95e5d026
commit
20e3757f44
18 changed files with 1145 additions and 142 deletions
156
src/spho/scope.c
Normal file
156
src/spho/scope.c
Normal file
|
@ -0,0 +1,156 @@
|
|||
#include <sys/queue.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "spho/ctx.h"
|
||||
#include "spho/scope.h"
|
||||
#include "spho/util.h"
|
||||
|
||||
static void spho_nom_term(struct spho_nom *);
|
||||
|
||||
int
|
||||
spho_scope_init(struct spho_scope *sc, struct spho_ctx *ctx,
|
||||
struct spho_scope *p)
|
||||
{
|
||||
SLIST_INIT(&sc->subs);
|
||||
SLIST_INIT(&sc->noms);
|
||||
|
||||
sc->parent = p;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
spho_scope_term(struct spho_scope *sc)
|
||||
{
|
||||
SPHO_UTIL_SLIST_DESTROY(&sc->subs, spho_scope, next, spho_scope_term);
|
||||
|
||||
SPHO_UTIL_SLIST_DESTROY(&sc->noms, spho_nom, next, spho_nom_term);
|
||||
|
||||
sc->parent = NULL;
|
||||
}
|
||||
|
||||
struct spho_scope *
|
||||
spho_scope_global(struct spho_ctx *ctx)
|
||||
{
|
||||
SPHO_PRECOND(ctx != NULL);
|
||||
|
||||
return (&ctx->glob);
|
||||
}
|
||||
|
||||
|
||||
struct spho_scope *
|
||||
spho_scope_create(struct spho_scope *sc)
|
||||
{
|
||||
struct spho_scope *sub;
|
||||
|
||||
if ((sub = malloc(sizeof(struct spho_scope))) == NULL) {
|
||||
SPHO_ERR(sc->ctx, SPHO_ERR_SYS);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (spho_scope_init(sub, sc->ctx, sc) == -1) {
|
||||
free(sub);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
SLIST_INSERT_HEAD(&sc->subs, sub, next);
|
||||
|
||||
return (sub);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
spho_nom_term(struct spho_nom *nom)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
spho_scope_destroy(struct spho_scope *sc)
|
||||
{
|
||||
struct spho_scope *sub;
|
||||
struct spho_scope *p;
|
||||
|
||||
p = sc->parent;
|
||||
SLIST_REMOVE(&p->subs, sc, spho_scope, next);
|
||||
spho_scope_term(sc);
|
||||
free(sc);
|
||||
}
|
||||
|
||||
|
||||
struct spho_nom *
|
||||
spho_scope_nom_add(struct spho_scope *sc, const char *nomstr, size_t sz)
|
||||
{
|
||||
#ifdef SPHO_USE_STRLCPY
|
||||
size_t res;
|
||||
#else
|
||||
ssize_t res;
|
||||
#endif
|
||||
struct spho_nom *nom;
|
||||
|
||||
|
||||
if (spho_scope_nom_get(sc, nomstr, sz, &nom) == -1)
|
||||
return (NULL);
|
||||
|
||||
if (nom != NULL) {
|
||||
SPHO_SC_ERR(sc, SPHO_ERR_NAMEINUSE);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((nom = malloc(sizeof(struct spho_nom))) == NULL) {
|
||||
SPHO_SC_ERR(sc, SPHO_ERR_SYS);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
#ifdef SPHO_USE_STRLCPY
|
||||
res = strlcpy(nom->s, nomstr, sizeof(nom->s));
|
||||
if (res >= sizeof(nom->s)) {
|
||||
SPHO_SC_ERR(sc, SPHO_ERR_TOOBIG);
|
||||
goto err;
|
||||
}
|
||||
#else
|
||||
res = snprintf(nom->s, sizeof(nom->s), "%s", nomstr);
|
||||
if (res < 0) {
|
||||
SPHO_SC_ERR(sc, SPHO_ERR_SYS);
|
||||
goto err;
|
||||
}
|
||||
if (res >= (ssize_t)sizeof(nom->s)) {
|
||||
SPHO_SC_ERR(sc, SPHO_ERR_TOOBIG);
|
||||
goto err;
|
||||
}
|
||||
#endif
|
||||
|
||||
SLIST_INSERT_HEAD(&sc->noms, nom, next);
|
||||
|
||||
return (nom);
|
||||
err:
|
||||
free(nom);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
spho_scope_nom_get(struct spho_scope *sc, const char *nomstr, size_t sz,
|
||||
struct spho_nom **out)
|
||||
{
|
||||
struct spho_nom *nom;
|
||||
|
||||
nom = NULL;
|
||||
|
||||
if (sz > sizeof(nom->s)) {
|
||||
SPHO_ERR(sc->ctx, SPHO_ERR_TOOBIG);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
out = NULL;
|
||||
SLIST_FOREACH(nom, &sc->noms, next) {
|
||||
if (strncmp(nom->s, nomstr, sz) == 0) {
|
||||
*out = nom;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue