Esempio n. 1
0
static channel*
chanSearch (Dt_t* chans, segment* seg)
{
  channel* cp;
  chanItem* chani = dtmatch (chans, &seg->comm_coord);
  assert (chani);
  cp = dtmatch (chani->chans, &seg->p);
  assert (cp);
  return cp;
}
Esempio n. 2
0
static Agedge_t*
mapEdge (Dt_t* emap, Agedge_t* e)
{
     edgepair_t* ep = dtmatch (emap, &e);
     if (ep) return ep->val;
     else return NULL;
}
Esempio n. 3
0
Pss_dev_t
pssttydev(register Pss_t* pss, const char* name)
{
	register const char*	s;
	register Tty_t*		tty;
	struct stat		st;

	s = name;
	if (*s == '?' || *s == '-')
		return PSS_NODEV;
	if (pss->meth->ttydevf)
		return (*pss->meth->ttydevf)(pss, s);
	if (tty = (Tty_t*)dtmatch(pss->ttybyname, s))
		return tty->dev;
	if (stat(s, &st))
	{
		sfsprintf(pss->buf, sizeof(pss->buf), "/dev/%s", name);
		s = (const char*)pss->buf;
		if (stat(s, &st))
		{
			sfsprintf(pss->buf, sizeof(pss->buf), "/dev/tty%s", name);
			if (stat(s, &st))
			{
				if (pss->disc->errorf)
					(*pss->disc->errorf)(pss, pss->disc, ERROR_SYSTEM|2, "%s: unknown tty", name);
			}
		}
	}
	pssttyadd(pss, name, st.st_rdev);
	return st.st_rdev;
}
Esempio n. 4
0
vertex_t *findvertex (xy_t xy) {
    vertex_t *vp;

    if (!(vp = dtmatch (vertexdict, &xy)))
        return NULL;
    return vp;
}
Esempio n. 5
0
edge_t *findedge (int tlid) {
    edge_t *ep;

    if (!(ep = dtmatch (edgedict, &tlid)))
        return NULL;
    return ep;
}
Esempio n. 6
0
string_t *findstring (char *str) {
    string_t *sp;

    if (!(sp = dtmatch (stringdict, str)))
        return NULL;
    return sp;
}
Esempio n. 7
0
Agsym_t *agfindattr(void *obj, char *name)
{
    Agsym_t *rv;
    Agdict_t *dict = agdictof(obj);

    rv = (Agsym_t *) dtmatch(dict->dict, name);
    return rv;
}
Esempio n. 8
0
/* mapEdge:
 * Check if we already have cluster edge corresponding to t->h,
 * and return it.
 */
static item *mapEdge(Dt_t * map, edge_t * e)
{
    void *key[2];

    key[0] = e->tail;
    key[1] = e->head;
    return (item *) dtmatch(map, &key);
}
Esempio n. 9
0
/* mapEdge:
 * Check if we already have cluster edge corresponding to t->h,
 * and return it.
 */
static item *mapEdge(Dt_t * map, edge_t * e)
{
    void *key[2];

    key[0] = agtail(e);
    key[1] = aghead(e);
    return (item *) dtmatch(map, &key);
}
Esempio n. 10
0
int
pssttyadd(register Pss_t* pss, const char* name, Pss_dev_t dev)
{
	register Tty_t*	tty;

	if (!dtmatch(pss->ttybyname, name))
	{
		if (!(tty = vmnewof(pss->vm, 0, Tty_t, 1, strlen(name))))
		{
			if (pss->disc->errorf)
				(*pss->disc->errorf)(pss, pss->disc, ERROR_SYSTEM|2, "out of space");
			return -1;
		}
		strcpy(tty->name, name);
		tty->dev = dev;
		dtinsert(pss->ttybyname, tty);
		if (!dtmatch(pss->ttybydev, &dev))
			dtinsert(pss->ttybydev, tty);
	}
	return 0;
}
Esempio n. 11
0
static void
addChan (Dt_t* chdict, channel* cp, double j)
{
    chanItem* subd = dtmatch (chdict, &j);

    if (!subd) {
	subd = NEW (chanItem);
	subd->v = j;
	subd->chans = dtopen (&chanDisc, Dtoset);
	dtinsert (chdict, subd);
    }
    dtinsert (subd->chans, cp);
}
Esempio n. 12
0
char*
pssttyname(register Pss_t* pss, Pssent_t* pe)
{
	register Tty_t*	tty;
	Pss_dev_t	dev;
	char*		s;

	if (pss->meth->ttynamef && (s = (*pss->meth->ttynamef)(pss, pe)))
		return s;
	if (pe->ttyname)
		return pe->ttyname;
	dev = pe->tty;
	if (dev == PSS_NODEV)
		return "?";
	if (tty = (Tty_t*)dtmatch(pss->ttybydev, &dev))
		return tty->name;
	if (!pss->ttyscan)
		ttyscan(pss);
	if (tty = (Tty_t*)dtmatch(pss->ttybydev, &dev))
		return tty->name;
	sfsprintf(pss->buf, sizeof(pss->buf), "%03d,%03d", major(dev), minor(dev));
	return pss->buf;
}
Esempio n. 13
0
static void insert (Dt_t* map, char* name, int v)
{
    intm* ip = (intm*)dtmatch(map, name);    

    if (ip) {
	if (ip->v != v)
	    agerr(AGWARN, "Duplicate cluster name \"%s\"\n", name);
	return;
    }
    ip = NEW(intm);
    ip->id = strdup(name);
    ip->v = v;
    dtinsert (map, ip);
}
Esempio n. 14
0
static usershape_t *user_init(const char *str)
{
    char *contents;
    char line[BUFSIZ];
    FILE *fp;
    struct stat statbuf;
    int saw_bb, must_inline, rc;
    int lx, ly, ux, uy;
    usershape_t *us;

    if (!EPSF_contents)
	EPSF_contents = dtopen(&ImageDictDisc, Dtoset);

    us = dtmatch(EPSF_contents, str);
    if (us)
	return us;

    if (!(fp = fopen(str, "r"))) {
	agerr(AGWARN, "couldn't open epsf file %s\n", str);
	return NULL;
    }
    /* try to find size */
    saw_bb = must_inline = FALSE;
    while (fgets(line, sizeof(line), fp)) {
	if (sscanf
	    (line, "%%%%BoundingBox: %d %d %d %d", &lx, &ly, &ux, &uy) == 4) {
	    saw_bb = TRUE;
	}
	if ((line[0] != '%') && strstr(line,"read")) must_inline = TRUE;
	if (saw_bb && must_inline) break;
    }

    if (saw_bb) {
	us = GNEW(usershape_t);
	us->x = lx;
	us->y = ly;
	us->w = ux - lx;
	us->y = uy - ly;
	us->name = str;
	us->macro_id = N_EPSF_files++;
	fstat(fileno(fp), &statbuf);
	contents = us->data = N_GNEW(statbuf.st_size + 1, char);
	fseek(fp, 0, SEEK_SET);
	rc = fread(contents, statbuf.st_size, 1, fp);
	contents[statbuf.st_size] = '\0';
	dtinsert(EPSF_contents, us);
	us->must_inline = must_inline;
    } else {
Esempio n. 15
0
Exnode_t *exexpr(Expr_t * ex, const char *name, Exid_t * sym, int type)
{
    if (ex) {
        if (!sym)
            sym = name ? (Exid_t *) dtmatch(ex->symbols, name) : &ex->main;
        if (sym && sym->lex == PROCEDURE && sym->value) {
            if (type != DELETE_T)
                return excast(ex, sym->value->data.procedure.body, type,
                              NiL, 0);
            exfreenode(ex, sym->value);
            sym->lex = NAME;
            sym->value = 0;
        }
    }
    return 0;
}
Esempio n. 16
0
static snode*
findSVert (sgraph* g, Dt_t* cdt, pointf p, snodeitem* ditems, boolean isVert)
{
    snodeitem* n = dtmatch (cdt, &p);

    if (!n) {
        snode* np = createSNode (g);
        assert(ditems);
        n = ditems + np->index;
	n->p = p;
	n->np = np;
	np->isVert = isVert;
	dtinsert (cdt, n);
    }

    return n->np;
}
Esempio n. 17
0
char*
fmtfs(struct stat* st)
{
	register Id_t*		ip;
	register void*		mp;
	register Mnt_t*		mnt;
	register char*		s;
	struct stat		rt;
	char*			buf;

	static Dt_t*		dict;
	static Dtdisc_t		disc;

	if (!dict)
	{
		disc.key = offsetof(Id_t, id);
		disc.size = sizeof(dev_t);
		dict = dtopen(&disc, Dthash);
	}
	else if (ip = (Id_t*)dtmatch(dict, &st->st_dev))
		return ip->name;
	s = FS_default;
	if (mp = mntopen(NiL, "r"))
	{
		while ((mnt = mntread(mp)) && (stat(mnt->dir, &rt) || rt.st_dev != st->st_dev));
		if (mnt && mnt->type)
			s = mnt->type;
	}
	if (!dict || !(ip = newof(0, Id_t, 1, strlen(s))))
	{
		if (!mp)
			return s;
		buf = fmtbuf(strlen(s) + 1);
		strcpy(buf, s);
		mntclose(mp);
		return buf;
	}
	strcpy(ip->name, s);
	if (mp)
		mntclose(mp);
	dtinsert(dict, ip);
	return ip->name;
}
Esempio n. 18
0
static Message_t*
match(const char* cat, const char* msg)
{
	register char*	s;
	register char*	t;
	Catalog_t*	cp;
	Message_t*	mp;
	size_t		n;

	char		buf[1024];

	s = (char*)cat;
	for (;;)
	{
		if (t = strchr(s, ':'))
		{
			if (s == (char*)cat)
			{
				if ((n = strlen(s)) >= sizeof(buf))
					n = sizeof(buf) - 1;
				s = (char*)memcpy(buf, s, n);
				s[n] = 0;
				t = strchr(s, ':');
			}
			*t = 0;
		}
		if (*s && ((cp = (Catalog_t*)dtmatch(state.catalogs, s)) || (cp = init(s))) && cp->messages && (mp = (Message_t*)dtmatch(cp->messages, msg)))
		{
			mp->cat = cp;
			return mp;
		}
		if (!t)
			break;
		s = t + 1;
	}
	return 0;
}
Esempio n. 19
0
static Extype_t
getdyn(Expr_t* ex, register Exnode_t* expr, void* env, Exassoc_t** assoc)
{
	Exassoc_t*	b;
	Extype_t	v;

	if (expr->data.variable.index)
	{
		char	buf[9];

		v = eval(ex, expr->data.variable.index, env);
		if (expr->data.variable.index->type != STRING)
		{
			sfsprintf(buf, sizeof(buf), "%I*x", sizeof(v.integer), v.integer);
			v.string = buf;
		}
		if (!(b = (Exassoc_t*)dtmatch((Dt_t*)expr->data.variable.symbol->local.pointer, v.string)))
		{
			if (!(b = newof(0, Exassoc_t, 1, strlen(v.string))))
				exnospace();
			strcpy(b->name, v.string);
			dtinsert((Dt_t*)expr->data.variable.symbol->local.pointer, b);
		}
		*assoc = b;
		if (b)
		{
			if (expr->data.variable.symbol->type == STRING && !b->value.string)
				b->value = exzero(expr->data.variable.symbol->type);
			return b->value;
		}
		v = exzero(expr->data.variable.symbol->type);
		return v;
	}
	*assoc = 0;
	return expr->data.variable.symbol->value->data.constant.value;
}
Esempio n. 20
0
int 
inIntSet (Dt_t* is, int v)
{
    return (dtmatch (is, &v) != 0);
}
Esempio n. 21
0
int
regcollate(register const char* s, char** e, char* buf, int size)
{
	register int		c;
	register char*		u;
	register char*		b;
	register char*		x;
	register Ucs_map_t*	a;
	Ucs_map_t*		z;
	const char*		t;
	const char*		v;
	int			n;
	int			r;
	int			ul;
	int			term;
	wchar_t			w[2];
	Ucs_attr_t		attr[3];

	if (size < 2)
		r = -1;
	else if ((term = *s++) != '.' && term != '=')
	{
		s--;
		r = -1;
	}
	else if (*s == term && *(s + 1) == ']')
		r = -1;
	else
	{
		t = s;
		mbchar(s);
		if ((n = (s - t)) == 1)
		{
			if (*s == term && *(s + 1) == ']')
			{
				s += 2;
				r = -1;
			}
			else
			{
				if (!local.attrs && initialize())
					return -1;
				attr[0] = attr[1] = attr[2] = 0;
				ul = 0;
				b = buf;
				x = buf + size - 2;
				r = 1;
				s = t;
				do
				{
					v = s;
					u = b;
					for (;;)
					{
						if (!(c = *s++))
							return -1;
						if (c == term)
						{
							if (!(c = *s++))
								return -1;
							if (c != term)
							{
								if (c != ']')
									return -1;
								r = -1;
								break;
							}
						}
						if (c == ' ' || c == '-' && u > b && *s != ' ' && *s != '-')
							break;
						if (isupper(c))
							c = tolower(c);
						if (u > x)
							break;
						*u++ = c;
					}
					*u = 0;
					if (a = (Ucs_map_t*)dtmatch(local.attrs, b))
						setattr(attr, a->code);
					else
					{
						if (u < x)
							*u++ = ' ';
						if (b == buf)
						{
							if (isupper(*v))
								ul = UCS_UC;
							else if (islower(*v))
								ul = UCS_LC;
						}
						b = u;
					}
				} while (r > 0);
				if (b > buf && *(b - 1) == ' ')
					b--;
				*b = 0;
				attr[0] &= ~((Ucs_attr_t)1);
				if (ul)
				{
					if (tstattr(attr, UCS_UC) || tstattr(attr, UCS_LC))
						ul = 0;
					else
						setattr(attr, ul);
				}
				if (z = (Ucs_map_t*)dtmatch(local.names, buf))
					for(;;)
					{
						for (a = z; a; a = a->next)
							if ((attr[0] & a->attr[0]) == attr[0] && (attr[1] & a->attr[1]) == attr[1] && (attr[2] & a->attr[2]) == attr[2])
							{
#if 0
								if (a->code <= 0xff)
								{
#if CC_NATIVE != CC_ASCII
									buf[0] = local.a2n[a->code];
#else
									buf[0] = a->code;
#endif
									buf[r = 1] = 0;
									ul = 0;
									break;
								}
#endif
								w[0] = a->code;
								w[1] = 0;
								if ((r = wcstombs(buf, w, size)) > 0)
									ul = 0;
								break;
							}
						if (!ul)
							break;
						clrattr(attr, ul);
						ul = 0;
					}
			}
			if (r < 0)
			{
				if ((n = s - t - 2) > (size - 1))
					return -1;
				memcpy(buf, t, n);
				buf[n] = 0;
				if (n == 1)
					r = n;
				else
				{
					for (t = buf; isalnum(*t); t++);
					if (!*t)
						r = n;
				}
			}
		}
		else if (*s++ != term || *s++ != ']')
		{
			s--;
			r = -1;
		}
		else if (n > (size - 1))
			r = -1;
		else
		{
			memcpy(buf, t, n);
			buf[r = n] = 0;
		}
	}
	if (e)
		*e = (char*)s;
	return r;
}
Esempio n. 22
0
/* mkMazeGraph:
 */
static sgraph*
mkMazeGraph (maze* mp, boxf bb)
{
    int nsides, i, ncnt, maxdeg;
    int bound = 4*mp->ncells;
    sgraph* g = createSGraph (bound + 2);
    Dt_t* vdict = dtopen(&vdictDisc,Dtoset);
    Dt_t* hdict = dtopen(&hdictDisc,Dtoset);
    snodeitem* ditems = N_NEW(bound, snodeitem);
    snode** sides;

    /* For each cell, create if necessary and attach a node in search
     * corresponding to each internal face. The node also gets
     * a pointer to the cell.
     */
    sides = N_NEW(4*mp->ncells, snode*);
    ncnt = 0;
    for (i = 0; i < mp->ncells; i++) {
	cell* cp = mp->cells+i;
        snode* np;
	pointf pt;

	cp->nsides = 4; 
	cp->sides = sides + 4*i;
	if (cp->bb.UR.x < bb.UR.x) {
	    pt.x = cp->bb.UR.x;
	    pt.y = cp->bb.LL.y;
	    np = findSVert (g, vdict, pt, ditems, TRUE);
	    np->cells[0] = cp;
	    cp->sides[M_RIGHT] = np;
	}
	if (cp->bb.UR.y < bb.UR.y) {
	    pt.x = cp->bb.LL.x;
	    pt.y = cp->bb.UR.y;
	    np = findSVert (g, hdict, pt, ditems, FALSE);
	    np->cells[0] = cp;
	    cp->sides[M_TOP] = np;
	}
	if (cp->bb.LL.x > bb.LL.x) {
	    np = findSVert (g, vdict, cp->bb.LL, ditems, TRUE);
	    np->cells[1] = cp;
	    cp->sides[M_LEFT] = np;
	}
	if (cp->bb.LL.y > bb.LL.y) {
	    np = findSVert (g, hdict, cp->bb.LL, ditems, FALSE);
	    np->cells[1] = cp;
	    cp->sides[M_BOTTOM] = np;
	}
    }

    /* For each gcell, corresponding to a node in the input graph,
     * connect it to its corresponding search nodes.
     */
    maxdeg = 0;
    sides = N_NEW(g->nnodes, snode*);
    nsides = 0;
    for (i = 0; i < mp->ngcells; i++) {
	cell* cp = mp->gcells+i;
        pointf pt; 
	snodeitem* np;

	cp->sides = sides+nsides;
        pt = cp->bb.LL;
	np = dtmatch (hdict, &pt);
	for (; np && np->p.x < cp->bb.UR.x; np = dtnext (hdict, np)) {
	    cp->sides[cp->nsides++] = np->np;
	    np->np->cells[1] = cp;
	}
	np = dtmatch (vdict, &pt);
	for (; np && np->p.y < cp->bb.UR.y; np = dtnext (vdict, np)) {
	    cp->sides[cp->nsides++] = np->np;
	    np->np->cells[1] = cp;
	}
	pt.y = cp->bb.UR.y;
	np = dtmatch (hdict, &pt);
	for (; np && np->p.x < cp->bb.UR.x; np = dtnext (hdict, np)) {
	    cp->sides[cp->nsides++] = np->np;
	    np->np->cells[0] = cp;
	}
	pt.x = cp->bb.UR.x;
	pt.y = cp->bb.LL.y;
	np = dtmatch (vdict, &pt);
	for (; np && np->p.y < cp->bb.UR.y; np = dtnext (vdict, np)) {
	    cp->sides[cp->nsides++] = np->np;
	    np->np->cells[0] = cp;
	}
	nsides += cp->nsides;
        if (cp->nsides > maxdeg) maxdeg = cp->nsides;
    }
    /* sides = RALLOC (nsides, sides, snode*); */

    /* Mark cells that are small because of a small node, not because of the close
     * alignment of two rectangles.
     */
    for (i = 0; i < mp->ngcells; i++) {
	cell* cp = mp->gcells+i;
	markSmall (cp, g);
    }

    /* Set index of two dummy nodes used for real nodes */
    g->nodes[g->nnodes].index = g->nnodes;
    g->nodes[g->nnodes+1].index = g->nnodes+1;
    
    /* create edges
     * For each ordinary cell, there can be at most 6 edges.
     * At most 2 gcells will be used at a time, and each of these
     * can have at most degree maxdeg.
     */
    initSEdges (g, maxdeg);
    for (i = 0; i < mp->ncells; i++) {
	cell* cp = mp->cells+i;
	createSEdges (cp, g);
    }

    /* tidy up memory */
    /* g->nodes = RALLOC (g->nnodes+2, g->nodes, snode); */
    /* g->edges = RALLOC (g->nedges+2*maxdeg, g->edges, sedge); */
    dtclose (vdict);
    dtclose (hdict);
    free (ditems);

chkSgraph (g);
    /* save core graph state */
    gsave(g);
    return g;
}
Esempio n. 23
0
static int recparse (
    XMLnode_t *xp, EMrec_t *crecs, int crecn, EMrec_t *drecs, int drecn,
    int dir, EMbb_t *bbp
) {
    EMrec_t *recs, *recp;
    int reci, recj, recn, ri;
    RIop_t *opp, top;
    int opi, opk, opl;
    EMbb_t cbb;
    EMimage_t *ip, *imem;
    XMLnode_t *cp, *pp, *tp, *fnp, *fsp, *clp;
    char *fs, *fn, *cl;
    int ci;
    int dw, dh;

    if (xp->type == XML_TYPE_TEXT) {
        SUwarning (0, "recparse", "called with text node");
        return -1;
    }

    opk = EMopm;
    bbp->w = bbp->h = 0;
    for (ci = 0; ci < xp->nodem; ci++) {
        cp = xp->nodes[ci];
        if (cp->type == XML_TYPE_TEXT)
            continue;
        if (strcmp (cp->tag, "v") == 0 || strcmp (cp->tag, "h") == 0) {
            opl = EMopm;
            if (recparse (
                cp, crecs, crecn, drecs, drecn,
                (cp->tag[0] == 'v') ? EM_DIR_V : EM_DIR_H, &cbb
            ) == -1) {
                SUwarning (0, "recparse", "cannot parse v list");
                return -1;
            }
            if (dir == EM_DIR_H) {
                for (opi = opl; opi < EMopm; opi++) {
                    if (EMops[opi].type == RI_OP_I) {
                        EMops[opi].u.img.rx += bbp->w;
                        EMops[opi].u.img.ry += (bbp->h - cbb.h) / 2;
                    } else if (EMops[opi].type == RI_OP_T) {
                        EMops[opi].u.text.p.x += bbp->w;
                        EMops[opi].u.text.p.y += (bbp->h - cbb.h) / 2;
                    }
                }
                bbp->w += cbb.w;
                if (bbp->h < cbb.h) {
                    dh = cbb.h - bbp->h;
                    bbp->h = cbb.h;
                    for (opi = opk; opi < EMopm; opi++) {
                        if (EMops[opi].type == RI_OP_I)
                            EMops[opi].u.img.ry += dh / 2;
                        else if (EMops[opi].type == RI_OP_T)
                            EMops[opi].u.text.p.y += dh / 2;
                    }
                }
            } else {
                for (opi = opl; opi < EMopm; opi++) {
                    if (EMops[opi].type == RI_OP_I) {
                        EMops[opi].u.img.ry += bbp->h;
                        EMops[opi].u.img.rx += (bbp->w - cbb.w) / 2;
                    } else if (EMops[opi].type == RI_OP_T) {
                        EMops[opi].u.text.p.y += bbp->h;
                        EMops[opi].u.text.p.x += (bbp->w - cbb.w) / 2;
                    }
                }
                bbp->h += cbb.h;
                if (bbp->w < cbb.w) {
                    dw = cbb.w - bbp->w;
                    bbp->w = cbb.w;
                    for (opi = opk; opi < EMopm; opi++) {
                        if (EMops[opi].type == RI_OP_I)
                            EMops[opi].u.img.rx += dw / 2;
                        else if (EMops[opi].type == RI_OP_T)
                            EMops[opi].u.text.p.x += dw / 2;
                    }
                }
            }
        } else if (strcmp (cp->tag, "rimg") == 0) {
            if (!(tp = XMLfind (cp, "path", XML_TYPE_TEXT, -1, TRUE))) {
                SUwarning (0, "recparse", "cannot find rimg/path");
                continue;
            }
            if (!(ip = dtmatch (imagedict, tp->text))) {
                if (!(imem = vmalloc (Vmheap, sizeof (EMimage_t)))) {
                    SUwarning (0, "recparse", "cannot allocate imem");
                    return -1;
                }
                memset (imem, 0, sizeof (EMimage_t));
                if (!(imem->file = vmstrdup (Vmheap, tp->text))) {
                    SUwarning (0, "recparse", "cannot copy image file");
                    return -1;
                }
                if (!(ip = dtinsert (imagedict, imem))) {
                    SUwarning (0, "recparse", "cannot insert image");
                    vmfree (Vmheap, imem->file);
                    vmfree (Vmheap, imem);
                    return -1;
                }
                if (RIloadimage (ip->file, &ip->op) == -1) {
                    SUwarning (0, "recparse", "cannot load image");
                    continue;
                }
            }

            if (ip->op.type == RI_OP_NOOP) {
                if ((pp = XMLfind (cp, "alt", XML_TYPE_TAG, -1, TRUE))) {
                    if (recparse (
                        pp, crecs, crecn, drecs, drecn, dir, &cbb
                    ) == -1) {
                        SUwarning (0, "recparse", "cannot parse alt tag");
                        return -1;
                    }
                    if (dir == EM_DIR_H) {
                        bbp->w += cbb.w;
                        if (bbp->h < cbb.h) {
                            dh = cbb.h - bbp->h;
                            bbp->h = cbb.h;
                        }
                    } else {
                        bbp->h += cbb.h;
                        if (bbp->w < cbb.w) {
                            dw = cbb.w - bbp->w;
                            bbp->w = cbb.w;
                        }
                    }
                }
            } else {
                if (EMopm >= EMopn) {
                    if (!(EMops = vmresize (
                        Vmheap, EMops, (EMopn + 100) * sizeof (RIop_t),
                        VM_RSCOPY
                    ))) {
                        SUwarning (0, "recparse", "cannot grow EMops array");
                        return -1;
                    }
                    EMopn += 100;
                }
                opp = &EMops[EMopm++];
                *opp = ip->op;
                if (dir == EM_DIR_H) {
                    opp->u.img.rx = bbp->w + EM_MARGIN_W / 2;
                    opp->u.img.ry = (bbp->h - opp->u.img.ih) / 2;
                    bbp->w += opp->u.img.iw + EM_MARGIN_W;
                    if (bbp->h < opp->u.img.ih + EM_MARGIN_H) {
                        dh = opp->u.img.ih + EM_MARGIN_H - bbp->h;
                        bbp->h = opp->u.img.ih + EM_MARGIN_H;
                        for (opi = opk; opi < EMopm; opi++) {
                            if (EMops[opi].type == RI_OP_I)
                                EMops[opi].u.img.ry += dh / 2;
                            else if (EMops[opi].type == RI_OP_T)
                                EMops[opi].u.text.p.y += dh / 2;
                        }
                    }
                } else {
                    opp->u.img.rx = (bbp->w - opp->u.img.iw) / 2;
                    opp->u.img.ry = bbp->h + EM_MARGIN_H / 2;
                    if (bbp->w < opp->u.img.iw + EM_MARGIN_W) {
                        dw = opp->u.img.iw + EM_MARGIN_W - bbp->w;
                        bbp->w = opp->u.img.iw + EM_MARGIN_W;
                        for (opi = opk; opi < EMopm; opi++) {
                            if (EMops[opi].type == RI_OP_I)
                                EMops[opi].u.img.rx += dw / 2;
                            else if (EMops[opi].type == RI_OP_T)
                                EMops[opi].u.text.p.x += dw / 2;
                        }
                    }
                    bbp->h += opp->u.img.ih + EM_MARGIN_H;
                }
            }
        } else if (strcmp (cp->tag, "qimg") == 0) {
            if (!(tp = XMLfind (cp, "path", XML_TYPE_TEXT, -1, TRUE))) {
                SUwarning (0, "recparse", "cannot find qimg/path");
                continue;
            }
            for (ri = 0; ri < 2; ri++) {
                if (ri == 0)
                    recs = crecs, recn = crecn;
                else
                    recs = drecs, recn = drecn;
                recj = -1;
                for (reci = 0; reci < recn; reci++) {
                    recp = &recs[reci];
                    if (strcmp (recp->id, tp->text) != 0)
                        continue;

                    if (recp->type == EM_TYPE_I) {
                        recj = reci;
                        if (!(ip = dtmatch (imagedict, recp->u.i.file))) {
                            if (!(imem = vmalloc (
                                Vmheap, sizeof (EMimage_t)
                            ))) {
                                SUwarning (
                                    0, "recparse", "cannot allocate imem"
                                );
                                return -1;
                            }
                            memset (imem, 0, sizeof (EMimage_t));
                            if (!(imem->file = vmstrdup (
                                Vmheap, recp->u.i.file
                            ))) {
                                SUwarning (
                                    0, "recparse", "cannot copy image file"
                                );
                                return -1;
                            }
                            if (!(ip = dtinsert (imagedict, imem))) {
                                SUwarning (
                                    0, "recparse", "cannot insert image"
                                );
                                vmfree (Vmheap, imem->file);
                                vmfree (Vmheap, imem);
                                return -1;
                            }
                            if (RIloadimage (ip->file, &ip->op) == -1) {
                                SUwarning (0, "recparse", "cannot load image");
                                continue;
                            }
                        }
                        if (EMopm >= EMopn) {
                            if (!(EMops = vmresize (
                                Vmheap, EMops, (EMopn + 100) * sizeof (RIop_t),
                                VM_RSCOPY
                            ))) {
                                SUwarning (
                                    0, "recparse", "cannot grow EMops array"
                                );
                                return -1;
                            }
                            EMopn += 100;
                        }
                        opp = &EMops[EMopm++];
                        *opp = ip->op;
                        opp->u.img.ix = recp->u.i.x1;
                        opp->u.img.iy = recp->u.i.y1;
                        opp->u.img.iw = recp->u.i.x2 - recp->u.i.x1 + 1;
                        opp->u.img.ih = recp->u.i.y2 - recp->u.i.y1 + 1;
                        if (dir == EM_DIR_H) {
                            opp->u.img.rx = bbp->w + EM_MARGIN_W / 2;
                            opp->u.img.ry = (bbp->h - opp->u.img.ih) / 2;
                            bbp->w += opp->u.img.iw + EM_MARGIN_W;
                            if (bbp->h < opp->u.img.ih + EM_MARGIN_H) {
                                dh = opp->u.img.ih + EM_MARGIN_H - bbp->h;
                                bbp->h = opp->u.img.ih + EM_MARGIN_H;
                                for (opi = opk; opi < EMopm; opi++) {
                                    if (EMops[opi].type == RI_OP_I)
                                        EMops[opi].u.img.ry += dh / 2;
                                    else if (EMops[opi].type == RI_OP_T)
                                        EMops[opi].u.text.p.y += dh / 2;
                                }
                            }
                        } else {
                            opp->u.img.rx = (bbp->w - opp->u.img.iw) / 2;
                            opp->u.img.ry = bbp->h + EM_MARGIN_H / 2;
                            if (bbp->w < opp->u.img.iw + EM_MARGIN_W) {
                                dw = opp->u.img.iw + EM_MARGIN_W - bbp->w;
                                bbp->w = opp->u.img.iw + EM_MARGIN_W;
                                for (opi = opk; opi < EMopm; opi++) {
                                    if (EMops[opi].type == RI_OP_I)
                                        EMops[opi].u.img.rx += dw / 2;
                                    else if (EMops[opi].type == RI_OP_T)
                                        EMops[opi].u.text.p.x += dw / 2;
                                }
                            }
                            bbp->h += opp->u.img.ih + EM_MARGIN_H;
                        }
                    } else if (recp->type == EM_TYPE_S) {
                        recj = reci;
                        if (EMopm + 3 >= EMopn) {
                            if (!(EMops = vmresize (
                                Vmheap, EMops, (EMopn + 100) * sizeof (RIop_t),
                                VM_RSCOPY
                            ))) {
                                SUwarning (
                                    0, "recparse", "cannot grow EMops array"
                                );
                                return -1;
                            }
                            EMopn += 100;
                        }
                        if (recp->u.s.lncl && recp->u.s.lncl[0]) {
                            opp = &EMops[EMopm++];
                            opp->type = RI_OP_COPYc;
                            opp->u.color.n = 1;
                            opp->u.color.t = recp->u.s.lncl;
                        }
                        if (recp->u.s.fncl && recp->u.s.fncl[0]) {
                            opp = &EMops[EMopm++];
                            opp->type = RI_OP_COPYFC;
                            opp->u.color.n = 1;
                            opp->u.color.t = recp->u.s.fncl;
                        }
                        if (recp->u.s.flcl && recp->u.s.flcl[0]) {
                            opp = &EMops[EMopm++];
                            opp->type = RI_OP_COPYC;
                            opp->u.color.n = 1;
                            opp->u.color.t = recp->u.s.flcl;
                        }
                    }
                }
                if (recj != -1)
                    break;
            }
        } else if (strcmp (cp->tag, "label") == 0) {
            if (!(fnp = XMLfind (cp, "fontname", XML_TYPE_TEXT, -1, TRUE)))
                fn = "abc";
            else
                fn = fnp->text;
            if (!(fsp = XMLfind (cp, "fontsize", XML_TYPE_TEXT, -1, TRUE)))
                fs = "10";
            else
                fs = fsp->text;
            if (!(clp = XMLfind (cp, "color", XML_TYPE_TEXT, -1, TRUE)))
                cl = NULL;
            else
                cl = clp->text;
            if (!(tp = XMLfind (cp, "text", XML_TYPE_TEXT, -1, TRUE)))
                continue;
            if (RIgettextsize (tp->text, fn, fs, &top) == -1) {
                SUwarning (0, "recparse", "cannot get text size");
                return -1;
            }
            top.u.font.w += 8, top.u.font.h += 4;
            if (EMopm + 3 >= EMopn) {
                if (!(EMops = vmresize (
                    Vmheap, EMops, (EMopn + 100) * sizeof (RIop_t), VM_RSCOPY
                ))) {
                    SUwarning (0, "recparse", "cannot grow EMops array");
                    return -1;
                }
                EMopn += 100;
            }
            opp = &EMops[EMopm++];
            *opp = top;
            opp->type = RI_OP_F;
            if (cl) {
                opp = &EMops[EMopm++];
                memset (opp, 0, sizeof (RIop_t));
                opp->type = RI_OP_FC;
                if (!(opp->u.color.t = vmstrdup (Vmheap, cl))) {
                    SUwarning (0, "recparse", "cannot copy color");
                    return -1;
                }
            }
            opp = &EMops[EMopm++];
            memset (opp, 0, sizeof (RIop_t));
            opp->type = RI_OP_T;
            opp->u.text.jx = 0;
            opp->u.text.jy = -1;
            opp->u.text.w = top.u.font.w - 8;
            opp->u.text.h = top.u.font.h - 4;
            if (!(opp->u.text.t = vmstrdup (Vmheap, tp->text))) {
                SUwarning (0, "recparse", "cannot copy text");
                return -1;
            }
            if (dir == EM_DIR_H) {
                opp->u.text.p.x = bbp->w + top.u.font.w / 2 + 4;
                opp->u.text.p.y = (bbp->h - top.u.font.h) / 2 + 2;
                bbp->w += top.u.font.w + EM_MARGIN_W;
                if (bbp->h < top.u.font.h + EM_MARGIN_H) {
                    dh = top.u.font.h + EM_MARGIN_H - bbp->h;
                    bbp->h = top.u.font.h + EM_MARGIN_H;
                    for (opi = opk; opi < EMopm; opi++) {
                        if (EMops[opi].type == RI_OP_I)
                            EMops[opi].u.img.ry += dh / 2;
                        else if (EMops[opi].type == RI_OP_T)
                            EMops[opi].u.text.p.y += dh / 2;
                    }
                }
            } else {
                opp->u.text.p.x = bbp->w / 2 + 0;
                opp->u.text.p.y = bbp->h + EM_MARGIN_H / 2 + 2;
                if (bbp->w < top.u.font.w + EM_MARGIN_W) {
                    dw = top.u.font.w + EM_MARGIN_W - bbp->w;
                    bbp->w = top.u.font.w + EM_MARGIN_W;
                    for (opi = opk; opi < EMopm; opi++) {
                        if (EMops[opi].type == RI_OP_I)
                            EMops[opi].u.img.rx += dw / 2;
                        else if (EMops[opi].type == RI_OP_T)
                            EMops[opi].u.text.p.x += dw / 2;
                    }
                }
                bbp->h += top.u.font.h + EM_MARGIN_H;
            }
        }
    }

    return 0;
}
Esempio n. 24
0
static int vMap(Dt_t * map, int i)
{
    Ipair *ip;
    ip = (Ipair *) dtmatch(map, &i);
    return ip->j;
}
Esempio n. 25
0
int SWMIopnexec (state_t *sp) {
  char q[1024], opnstr[1024], *name, *origname, *s;
  opn_t *opnp, *opnbasep, *opntimep, *opnfreqp;
  obj_t *objp;
  HRESULT hr;
  BSTR propb, ctb, wqlb, qb;
  SAFEARRAY *props;
  VARIANT var;
  LONG pu, pl, pi;
  ULONG ul;
  double v;
  kvt_t *kvtp;
  int kvti;

  ctb = s2b ("countertype");
  wqlb = s2b ("WQL");
  for (
    objp = (obj_t *) dtfirst (objdict); objp;
    objp = (obj_t *) dtnext (objdict, objp)
  ) {
    sfsprintf (q, 1024, "select * from %s", objp->obj);
    if (verbose)
      sfprintf (sfstderr, "query: %s\n", q);
    if (!(qb = s2b (q))) {
      sfprintf (sfstderr, "cannot convert obj name %s to bstr\n", objp->obj);
      return -1;
    }

    if ((hr = sp->pSvc->ExecQuery (
      wqlb, qb, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
      0, &sp->pEnum
    )) != S_OK) {
      sfprintf (
        sfstderr, "cannot execute query %s, error %s\n", q, E2ccp (hr)
      );
      return -1;
    }

    if (SWMIproxysecurity (sp, sp->pEnum) < 0) {
      sfprintf (sfstderr, "cannot enable security for query %s\n", q);
      return -1;
    }

    while (
      sp->pEnum->Next (30 * 1000, 1, &sp->pClsObj, &ul
    ) == S_OK && ul == 1) {
      if ((hr = sp->pClsObj->GetNames (
        0, WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY, 0, &props
      )) != S_OK) {
        sfprintf (
          sfstderr, "cannot get props for obj %s, error %s\n",
          objp->obj, E2ccp (hr)
        );
        return -1;
      }
      if(
        (hr = SafeArrayGetLBound (props, 1, &pl)) != S_OK ||
        (hr = SafeArrayGetUBound (props, 1, &pu)) != S_OK
      ) {
        sfprintf (
          sfstderr, "cannot get props bounds for %s, error %s\n",
          objp->obj, E2ccp (hr)
        );
        return -1;
      }
      if (pu - pl + 1 > kvtn) {
        kvtn = pu - pl + 1;
        if (!(kvts = (kvt_t *) vmresize (
          Vmheap, kvts, kvtn * sizeof (kvt_t), VM_RSCOPY
        ))) {
          sfprintf (sfstderr, "cannot grow kvt array\n");
          return -1;
        }
      }
      name = NULL;
      for (pi = pl, kvtl = 0; pi <= pu; pi++, kvtl++) {
        kvtp = &kvts[kvtl];
        if ((hr = SafeArrayGetElement (props, &pi, &propb)) != S_OK) {
          sfprintf (
            sfstderr, "cannot get prop name for %d/%s, error %s\n",
            pi, objp->obj, E2ccp (hr)
          );
          continue;
        }
        kvtp->kp = (char *) b2s (propb);
        if ((hr = sp->pClsObj->Get (propb, 0, &var, 0, 0)) != S_OK) {
          sfprintf (
            sfstderr, "cannot get prop value for %d/%s, error %s\n",
            pi, objp->obj, E2ccp (hr)
          );
          continue;
        }
        if (
            !(kvtp->vp = (char *) V2ccp (&var)) ||
            !(kvtp->vp = vmstrdup (Vmheap, kvtp->vp))
        ) {
          kvtl--;
          continue;
        }
        switch (var.vt) {
        case VT_UI1: kvtp->v = var.bVal; break;
        case VT_I2: kvtp->v = var.iVal;  break;
        case VT_I4: kvtp->v = var.lVal;  break;
        default: kvtp->v = strtoull (kvtp->vp, &s, 10); break;
        }
        if (strcmp (kvtp->kp, "Name") == 0) {
          if (var.vt == VT_NULL)
            name = "ALL";
          else
            name = kvtp->vp;
          origname = name;
        }

        if ((hr = sp->pClsObj->GetPropertyQualifierSet (
          propb, &sp->pQualSet
        )) != S_OK) {
          sfprintf (
            sfstderr, "cannot get quals for %d/%s, error %s\n",
            pi, objp->obj, E2ccp (hr)
          );
          continue;
        }
        kvtp->type = V_TYPE_STRING;
        if ((hr = sp->pQualSet->Get (
          ctb, 0, &var, 0
        )) == S_OK && var.vt == VT_I4) {
          switch (var.lVal) {
          case PERF_COUNTER_RAWCOUNT:
          case PERF_COUNTER_LARGE_RAWCOUNT:
            kvtp->type = V_TYPE_SIMPLE; break;
          case PERF_RAW_FRACTION:
            kvtp->type = V_TYPE_WITHBASE; break;
          case PERF_RAW_BASE:
          case PERF_PRECISION_TIMESTAMP:
            kvtp->type = V_TYPE_ISBASE; break;
          case PERF_COUNTER_COUNTER:
          case PERF_COUNTER_BULK_COUNT:
            kvtp->type = V_TYPE_WITHTIMENFREQ; break;
          case PERF_100NSEC_TIMER:
            kvtp->type = V_TYPE_WITH100NSEC; break;
          case PERF_100NSEC_TIMER_INV:
            kvtp->type = V_TYPE_WITH100NSECINV; break;
          default:
            kvtp->type = V_TYPE_SIMPLE; break;
          }
        } else {
          if (strcmp (kvtp->kp, "Timestamp_PerfTime") == 0)
            kvtp->type = V_TYPE_ISTIME;
          else if (strcmp (kvtp->kp, "Frequency_PerfTime") == 0)
            kvtp->type = V_TYPE_ISFREQ;
          else if (strcmp (kvtp->kp, "Timestamp_Sys100NS") == 0)
            kvtp->type = V_TYPE_IS100NSEC;
          else if (kvtp->vp[0] == 0 || isdigit (kvtp->vp[0]))
            kvtp->type = V_TYPE_SIMPLE;
        }
      }
      ::SafeArrayDestroy (props);
      sp->pClsObj->Release ();

      for (kvti = 0; kvti < kvtl; kvti++) {
        kvtp = &kvts[kvti];
        sfsprintf (opnstr, 1024, "%s.%s.%s", objp->obj, kvtp->kp, name);
        if (!(opnp = (opn_t *) dtmatch (opndict, opnstr))) {
          sfsprintf (opnstr, 1024, "%s.%s.ALL", objp->obj, kvtp->kp);
          if ((opnp = (opn_t *) dtmatch (opndict, opnstr)))
            name = "ALL";
          else
            sfsprintf (opnstr, 1024, "%s.%s.%s", objp->obj, kvtp->kp, name);
        }

        if (!opnp) {
          if (kvtp->type < V_TYPE_SUPPORT)
            continue;
          if (!(opnp = opninsert (
            OPN_KIND_S, "", "", "", objp->obj, kvtp->kp, name, ""
          ))) {
            sfprintf (sfstderr, "cannot insert opn\n");
            return -1;
          }
          opnp->objp = objp;
        }
        if (opnp->ep && strstr (origname, opnp->ep))
          continue;
        if (opnp->type == 0)
          opnp->type = kvtp->type;
        opnp->vp = kvtp->vp;
        opnp->cv += kvtp->v;
        opnp->havec = TRUE;
      }
      for (kvti = 0; kvti < kvtl; kvti++) {
        kvtp = &kvts[kvti];
        sfsprintf (opnstr, 1024, "%s.%s.SUM", objp->obj, kvtp->kp);
        if (!(opnp = (opn_t *) dtmatch (opndict, opnstr)))
          continue;

        if (opnp->ep && strstr (origname, opnp->ep))
          continue;
        if (opnp->type == 0)
          opnp->type = kvtp->type;
        opnp->vp = kvtp->vp;
        opnp->cv += kvtp->v;
        opnp->havec = TRUE;
      }
    }
  }
  for (
    opnp = (opn_t *) dtfirst (opndict); opnp;
    opnp = (opn_t *) dtnext (opndict, opnp)
  ) {
    if (opnp->kind != OPN_KIND_P || !opnp->havec)
      continue;
    if (opnp->type >= V_TYPE_SUPPORT)
      continue;
    switch (opnp->type) {
    case V_TYPE_STRING:
#if 0
      sfprintf (
        sfstdout, "rt=STAT type=string name=%s str=%s%s%s%s\n",
        opnp->mname, opnp->vp,
        (opnp->lp[0]) ? " label='" : "", (opnp->lp[0]) ? opnp->lp : "",
        (opnp->lp[0]) ? "'" : ""
      );
#endif
      break;
    case V_TYPE_SIMPLE:
      sfprintf (
        sfstdout, "rt=STAT type=%s name=%s unit=%s num=%lld%s%s%s\n",
        opnp->mtype, opnp->mname, opnp->munit, opnp->cv,
        (opnp->lp[0]) ? " label='" : "", (opnp->lp[0]) ? opnp->lp : "",
        (opnp->lp[0]) ? "'" : ""
      );
      break;
    case V_TYPE_WITHBASE:
      sfsprintf (opnstr, 1024, "%s.%s_Base.%s", opnp->op, opnp->pp, opnp->np);
      if (!(opnbasep = (opn_t *) dtmatch (opndict, opnstr))) {
        sfprintf (sfstderr, "cannot find base property for %s\n", opnp->opn);
        continue;
      }
      if (!opnbasep->havec)
        continue;
      if ((v = (opnbasep->cv == 0) ? 0.00 : 100.0 * (
        opnp->cv / (double) opnbasep->cv
      )) < 0.0)
        continue;
      sfprintf (
        sfstdout, "rt=STAT type=%s name=%s unit=%s num=%lf%s%s%s\n",
        opnp->mtype, opnp->mname, opnp->munit, v,
        (opnp->lp[0]) ? " label='" : "", (opnp->lp[0]) ? opnp->lp : "",
        (opnp->lp[0]) ? "'" : ""
      );
      break;
    case V_TYPE_WITHTIMENFREQ:
      if (!opnp->havep)
        continue;
      sfsprintf (opnstr, 1024, "%s.Timestamp_PerfTime.%s", opnp->op, opnp->np);
      if (!(opntimep = (opn_t *) dtmatch (opndict, opnstr))) {
        sfprintf (sfstderr, "cannot find time property for %s\n", opnp->opn);
        continue;
      }
      if (!opntimep->havec || !opntimep->havep)
        continue;
      sfsprintf (opnstr, 1024, "%s.Frequency_PerfTime.%s", opnp->op, opnp->np);
      if (!(opnfreqp = (opn_t *) dtmatch (opndict, opnstr))) {
        sfprintf (sfstderr, "cannot find freq property for %s\n", opnp->opn);
        continue;
      }
      if (!opnfreqp->havec)
        continue;
      if (
        opnp->cv < opnp->pv || opntimep->cv <= opntimep->pv ||
        opnfreqp->cv == 0
      )
        continue;
      if ((v = (opnp->cv - opnp->pv) / (
        (opntimep->cv - opntimep->pv) / (double) opnfreqp->cv
      )) < 0.0) {
        if (v > -1)
          v = 0.0;
        else
          continue;
      }
      sfprintf (
        sfstdout, "rt=STAT type=%s name=%s unit=%s num=%lf%s%s%s\n",
        opnp->mtype, opnp->mname, opnp->munit, v,
        (opnp->lp[0]) ? " label='" : "", (opnp->lp[0]) ? opnp->lp : "",
        (opnp->lp[0]) ? "'" : ""
      );
      break;
    case V_TYPE_WITH100NSEC:
    case V_TYPE_WITH100NSECINV:
      if (!opnp->havep)
        continue;
      sfsprintf (opnstr, 1024, "%s.Timestamp_Sys100NS.%s", opnp->op, opnp->np);
      if (!(opntimep = (opn_t *) dtmatch (opndict, opnstr))) {
        sfprintf (sfstderr, "cannot find time property for %s\n", opnp->opn);
        continue;
      }
      if (!opntimep->havec || !opntimep->havep)
        continue;
      if (opnp->cv < opnp->pv || opntimep->cv <= opntimep->pv)
        continue;
      if (opnp->type == V_TYPE_WITH100NSEC) {
        if ((v = 100.0 * (
          (opnp->cv - opnp->pv) /
          ((double) opntimep->cv - opntimep->pv)
        )) < 0.0) {
          if (v > -1)
            v = 0.0;
          else
            continue;
        }
        sfprintf (
          sfstdout, "rt=STAT type=%s name=%s unit=%s num=%lf%s%s%s\n",
          opnp->mtype, opnp->mname, opnp->munit, v,
          (opnp->lp[0]) ? " label='" : "", (opnp->lp[0]) ? opnp->lp : "",
          (opnp->lp[0]) ? "'" : ""
        );
      } else {
        if ((v = 100.0 * (
          1.0 - (opnp->cv - opnp->pv) /
          ((double) opntimep->cv - opntimep->pv)
        )) < 0.0) {
          if (v > -1)
            v = 0.0;
          else
            continue;
        }
        sfprintf (
          sfstdout, "rt=STAT type=%s name=%s unit=%s num=%lf%s%s%s\n",
          opnp->mtype, opnp->mname, opnp->munit, v,
          (opnp->lp[0]) ? " label='" : "", (opnp->lp[0]) ? opnp->lp : "",
          (opnp->lp[0]) ? "'" : ""
        );
      }
      break;
    }
  }
  return 0;
}
Esempio n. 26
0
static int lookup (Dt_t* map, char* name)
{
    intm* ip = (intm*)dtmatch(map, name);    
    if (ip) return ip->v;
    else return -1;
}
Esempio n. 27
0
int SWMIopnload (char *tfile, char *sfile) {
  Sfio_t *tfp, *sfp;
  obj_t *objp;
  opn_t *opnp;
  char *line, *mname, *mtype, *munit, *op, *pp, *np, *vp, *tp, *ep, *lp, *s;

  if (!(objdict = dtopen (&objdisc, Dtset))) {
    sfprintf (sfstderr, "cannot create obj dict\n");
    return -1;
  }
  if (!(opndict = dtopen (&opndisc, Dtset))) {
    sfprintf (sfstderr, "cannot create opn dict\n");
    return -1;
  }

  if (!(tfp = sfopen (NULL, tfile, "r"))) {
    sfprintf (sfstderr, "cannot open file %s\n", tfile);
    return -1;
  }
  while ((line = sfgetr (tfp, '\n', 1))) {
    mname = line;
    if (!(mtype = strchr (mname, '|'))) {
      sfprintf (sfstderr, "badly formed opn line %s", line);
      continue;
    }
    if (!(munit = strchr (mtype + 1, '|'))) {
      sfprintf (sfstderr, "badly formed opn line %s", line);
      continue;
    }
    if (!(op = strchr (munit + 1, '|'))) {
      sfprintf (sfstderr, "badly formed opn line %s", line);
      continue;
    }
    if (!(tp = strchr (op + 1, '|'))) {
      sfprintf (sfstderr, "badly formed opn line %s", line);
      continue;
    }
    if (!(ep = strchr (tp + 1, '|'))) {
      sfprintf (sfstderr, "badly formed opn line %s", line);
      continue;
    }
    if (!(lp = strchr (ep + 1, '|'))) {
      sfprintf (sfstderr, "badly formed opn line %s", line);
      continue;
    }
    if (!(pp = strchr (op + 1, '.')) || !(np = strchr (pp + 1, '.'))) {
      sfprintf (sfstderr, "badly formed opn line %s", line);
      continue;
    }
    *mtype++ = 0, *munit++ = 0, *op++ = 0, *tp++ = 0, *ep++ = 0, *lp++ = 0;
    if (dtmatch (opndict, op))
      continue;
    *pp++ = 0, *np++ = 0;
    if (!(opnp = opninsert (OPN_KIND_P, mname, mtype, munit, op, pp, np, lp))) {
      sfprintf (sfstderr, "cannot insert opn\n");
      return -1;
    }
    if (*tp) {
        if (strcmp (tp, "simple") == 0)
            opnp->type = V_TYPE_SIMPLE;
        else if (strcmp (tp, "withbase") == 0)
            opnp->type = V_TYPE_WITHBASE;
    }
    if (*ep) {
      if (!(opnp->ep = vmstrdup (Vmheap, ep))) {
        sfprintf (sfstderr, "cannot allocate ep\n");
        return NULL;
      }
    }

    if ((objp = (obj_t *) dtmatch (objdict, op))) {
      opnp->objp = objp;
      continue;
    }
    if (!(objp = objinsert (op))) {
      sfprintf (sfstderr, "cannot insert obj\n");
      return -1;
    }

    opnp->objp = objp;
  }
  sfclose (tfp);

  if (!(sfp = sfopen (NULL, sfile, "r"))) {
    sfprintf (sfstderr, "cannot open file %s\n", sfile);
    return -1;
  }
  while ((line = sfgetr (sfp, '\n', 1))) {
    if (!(vp = strrchr (line, '|'))) {
      sfprintf (sfstderr, "badly formed opn state %s", line);
      return -1;
    }
    *vp++ = 0;
    op = line;
    if (!(pp = strchr (op, '.')) || !(np = strchr (pp + 1, '.'))) {
      sfprintf (sfstderr, "badly formed opn %s", line);
      continue;
    }
    if (!(opnp = (opn_t *) dtmatch (opndict, op))) {
      *pp++ = 0, *np++ = 0;
      if (!(opnp = opninsert (OPN_KIND_S, "", "", "", op, pp, np, ""))) {
        sfprintf (sfstderr, "cannot insert opn\n");
        return -1;
      }
      if ((objp = (obj_t *) dtmatch (objdict, op)))
        opnp->objp = objp;
    }

    if (strcmp (vp, "NULL") == 0) {
      opnp->havep = FALSE;
    } else {
      opnp->pv = strtoull (vp, &s, 10);
      if (s && *s) {
        sfprintf (sfstderr, "cannot calculate previous value\n");
        opnp->havep = FALSE;
      } else
        opnp->havep = TRUE;
    }
  }
  sfclose (sfp);

  if (!(kvts = (kvt_t *) vmalloc (Vmheap, 100 * sizeof (kvt_t)))) {
    sfprintf (sfstderr, "cannot allocate kvt array\n");
    return -1;
  }
  kvtn = 100;

  return 0;
}
Esempio n. 28
0
int
main(int argc, register char** argv)
{
	register int	n;
	register char*	s;
	char*		args;
	char*		codes;
	char**		av;
	char**		ap;
	int		i;
	int		count;
	int		len;
	int		traverse;
	int		size;
	Dir_t*		firstdir;
	Dir_t*		lastdir;
	Exnode_t*	x;
	Exnode_t*	y;
	Ftw_t		ftw;
	Finddisc_t	disc;

	setlocale(LC_ALL, "");
	error_info.id = "tw";
	av = argv + 1;
	args = 0;
	codes = 0;
	count = 0;
	size = 0;
	traverse = 1;
	firstdir = lastdir = newof(0, Dir_t, 1, 0);
	firstdir->name = ".";
	state.action = LIST;
	state.cmdflags = CMD_EXIT|CMD_IGNORE|CMD_IMPLICIT|CMD_NEWLINE;
	state.errexit = EXIT_QUIT;
	state.ftwflags = ftwflags()|FTW_DELAY;
	state.select = ALL;
	state.separator = '\n';
	memset(&disc, 0, sizeof(disc));
	for (;;)
	{
		switch (optget(argv, usage))
		{
		case 'a':
			args = opt_info.arg;
			state.cmdflags |= CMD_POST;
			continue;
		case 'c':
			if ((count = opt_info.num) < 0)
				error(3, "argument count must be >= 0");
			continue;
		case 'd':
			lastdir = lastdir->next = newof(0, Dir_t, 1, 0);
			lastdir->name = opt_info.arg;
			continue;
		case 'e':
			compile(opt_info.arg, 0);
			continue;
		case 'f':
			state.pattern = opt_info.arg;
			continue;
		case 'i':
			state.ignore = 1;
			continue;
		case 'l':
			state.localfs = 1;
			continue;
		case 'm':
			state.intermediate = 1;
			continue;
		case 'n':
			traverse = 0;
			continue;
		case 'p':
			state.ftwflags |= FTW_TWICE;
			continue;
		case 'q':
			state.cmdflags |= CMD_QUERY;
			continue;
		case 'r':
			state.ftwflags |= FTW_RECURSIVE;
			continue;
		case 's':
			if ((size = opt_info.num) < 0)
				error(3, "command size must be >= 0");
			continue;
		case 't':
			state.cmdflags |= CMD_TRACE;
			continue;
		case 'x':
			state.errexit = opt_info.arg ? opt_info.num : EXIT_QUIT;
			continue;
		case 'z':
			if (s = sfgetr(sfstdin, '\n', 1))
			{
				if (!(s = strdup(s)))
					error(ERROR_SYSTEM|3, "out of space");
				n = state.snapshot.format.delim = *s++;
				state.snapshot.format.path = s;
				if (!(s = strchr(s, n)))
				{
				osnap:
					error(3, "invalid snapshot on standard input");
				}
				*s++ = 0;
				if (!streq(state.snapshot.format.path, SNAPSHOT_ID))
					goto osnap;
				state.snapshot.format.path = s;
				if (!(s = strchr(s, n)))
					goto osnap;
				*s++ = 0;
				state.snapshot.format.easy = s;
				if (!(s = strchr(s, n)))
					goto osnap;
				*s++ = 0;
				if (*(state.snapshot.format.hard = s))
				{
					if (!(s = strchr(s, n)))
						goto osnap;
					*s = 0;
				}
				else
					state.snapshot.format.hard = 0;
				state.snapshot.sp = sfstdin;
				state.snapshot.prev = sfgetr(sfstdin, '\n', 0);
			}
			else
			{
				state.snapshot.format.path = SNAPSHOT_PATH;
				state.snapshot.format.easy = SNAPSHOT_EASY;
				state.snapshot.format.hard = SNAPSHOT_HARD;
				state.snapshot.format.delim = SNAPSHOT_DELIM[0];
			}
			if (!(state.snapshot.tmp = sfstropen()))
				error(ERROR_SYSTEM|3, "out of space");
			compile("sort:name;", 0);
			continue;
		case 'C':
			state.ftwflags |= FTW_NOSEEDOTDIR;
			continue;
		case 'D':
			error_info.trace = -opt_info.num;
			continue;
		case 'E':
			compile(opt_info.arg, 1);
			continue;
		case 'F':
			codes = opt_info.arg;
			continue;
		case 'G':
			disc.flags |= FIND_GENERATE;
			if (streq(opt_info.arg, "old"))
				disc.flags |= FIND_OLD;
			else if (streq(opt_info.arg, "gnu") || streq(opt_info.arg, "locate"))
				disc.flags |= FIND_GNU;
			else if (streq(opt_info.arg, "type"))
				disc.flags |= FIND_TYPE;
			else if (streq(opt_info.arg, "?"))
			{
				error(2, "formats are { default|dir type old gnu|locate }");
				return 0;
			}
			else if (!streq(opt_info.arg, "-") && !streq(opt_info.arg, "default") && !streq(opt_info.arg, "dir"))
				error(3, "%s: invalid find codes format -- { default|dir type old gnu|locate } expected", opt_info.arg);
			continue;
		case 'H':
			state.ftwflags |= FTW_META|FTW_PHYSICAL;
			continue;
		case 'I':
			state.icase = 1;
			continue;
		case 'L':
			state.ftwflags &= ~(FTW_META|FTW_PHYSICAL|FTW_SEEDOTDIR);
			continue;
		case 'P':
			state.ftwflags &= ~FTW_META;
			state.ftwflags |= FTW_PHYSICAL;
			continue;
		case 'S':
			state.separator = *opt_info.arg;
			continue;
		case 'X':
			state.ftwflags |= FTW_MOUNT;
			continue;
		case '?':
			error(ERROR_USAGE|4, "%s", opt_info.arg);
			continue;
		case ':':
			error(2, "%s", opt_info.arg);
			continue;
		}
		break;
	}
	argv += opt_info.index;
	argc -= opt_info.index;
	if (error_info.errors)
		error(ERROR_USAGE|4, "%s", optusage(NiL));

	/*
	 * do it
	 */

	if (state.snapshot.tmp)
		sfprintf(sfstdout, "%c%s%c%s%c%s%c%s%c\n",
			state.snapshot.format.delim, SNAPSHOT_ID,
			state.snapshot.format.delim, state.snapshot.format.path,
			state.snapshot.format.delim, state.snapshot.format.easy,
			state.snapshot.format.delim, state.snapshot.format.hard ? state.snapshot.format.hard : "",
			state.snapshot.format.delim);
	if (x = exexpr(state.program, "begin", NiL, 0))
		eval(x, NiL);
	if ((x = exexpr(state.program, "select", NiL, INTEGER)) || (x = exexpr(state.program, NiL, NiL, INTEGER)))
		state.select = x;
	if (!(state.ftwflags & FTW_PHYSICAL))
		state.ftwflags &= ~FTW_DELAY;
	memset(&ftw, 0, sizeof(ftw));
	ftw.path = ftw.name = "";
	if (traverse)
	{
		if (x = exexpr(state.program, "action", NiL, 0))
			state.action = x;
		if (x = exexpr(state.program, "sort", NiL, 0))
		{
			state.sortkey = x;
			y = 0;
			for (;;)
			{
				switch (x->op)
				{
				case ',':
					y = x->data.operand.right;
					/*FALLTHROUGH*/
				case '!':
				case '~':
				case S2B:
				case X2I:
					x = x->data.operand.left;
					continue;
				case ID:
					if (!(x = y))
						break;
					y = 0;
					continue;
				default:
					error(3, "invalid sort identifier (op 0x%02x)", x->op);
					break;
				}
				break;
			}
			state.sort = order;
		}
		if (*argv && (*argv)[0] == '-' && (*argv)[1] == 0)
		{
			state.ftwflags |= FTW_LIST;
			argv++;
			argc--;
		}
		if (*argv || args || count || !(state.cmdflags & CMD_IMPLICIT))
		{
			Cmddisc_t	disc;

			CMDDISC(&disc, state.cmdflags, errorf);
			state.cmd = cmdopen(argv, count, size, args, &disc);
			state.ftwflags |= FTW_DOT;
		}
		else
			state.cmdflags &= ~CMD_IMPLICIT;
		if (codes && (disc.flags & FIND_GENERATE))
		{
			char*	p;
			Dir_t*	dp;
			char	pwd[PATH_MAX];
			char	tmp[PATH_MAX];

			disc.version = FIND_VERSION;
			if (state.cmdflags & CMD_TRACE)
				disc.flags |= FIND_TYPE;
			if (state.cmdflags & CMD_QUERY)
				disc.flags |= FIND_OLD;
			disc.errorf = errorf;
			if (!(state.find = findopen(codes, NiL, NiL, &disc)))
				exit(2);
			if (disc.flags & FIND_TYPE)
			{
				state.act = ACT_CODETYPE;
				compile("_tw_init:mime;", 0);
				state.magicdisc.flags |= MAGIC_MIME;
			}
			else
				state.act = ACT_CODE;
			state.icase = 1;
			state.pattern = 0;
			state.sort = order;
			if (!state.program)
				compile("1", 0);
			if (!(state.sortkey = newof(0, Exnode_t, 1, 0)) || !(state.sortkey->data.variable.symbol = (Exid_t*)dtmatch(state.program->symbols, "name")))
				error(ERROR_SYSTEM|3, "out of space");
			state.sortkey->op = ID;
			s = p = 0;
			for (dp = (firstdir == lastdir) ? firstdir : firstdir->next; dp; dp = dp->next)
			{
				if (*(s = dp->name) == '/')
					sfsprintf(tmp, sizeof(tmp), "%s", s);
				else if (!p && !(p = getcwd(pwd, sizeof(pwd))))
					error(ERROR_SYSTEM|3, "cannot determine pwd path");
				else
					sfsprintf(tmp, sizeof(tmp), "%s/%s", p, s);
				pathcanon(tmp, sizeof(tmp), PATH_PHYSICAL);
				if (!(dp->name = strdup(tmp)))
					error(ERROR_SYSTEM|3, "out of space [PATH_PHYSICAL]");
			}
		}
		else if (state.snapshot.tmp)
			state.act = ACT_SNAPSHOT;
		else if (state.cmdflags & CMD_IMPLICIT)
			state.act = ACT_CMDARG;
		else if (state.action == LIST)
			state.act = ACT_LIST;
		else if (state.action)
			state.act = ACT_EVAL;
		if (state.intermediate)
		{
			state.actII = state.act;
			state.act = ACT_INTERMEDIATE;
		}
		if (state.pattern)
		{
			disc.version = FIND_VERSION;
			if (state.icase)
				disc.flags |= FIND_ICASE;
			disc.errorf = errorf;
			disc.dirs = ap = av;
			if (firstdir != lastdir)
				firstdir = firstdir->next;
			do {*ap++ = firstdir->name;} while (firstdir = firstdir->next);
			*ap = 0;
			if (!(state.find = findopen(codes, state.pattern, NiL, &disc)))
				exit(1);
			state.ftwflags |= FTW_TOP;
			n = state.select == ALL ? state.act : ACT_EVAL;
			while (s = findread(state.find))
			{
				switch (n)
				{
				case ACT_CMDARG:
					if ((i = cmdarg(state.cmd, s, strlen(s))) >= state.errexit)
						exit(i);
					break;
				case ACT_LIST:
					sfputr(sfstdout, s, '\n');
					break;
				default:
					ftwalk(s, tw, state.ftwflags, NiL);
					break;
				}
			}
		}
		else if (state.ftwflags & FTW_LIST)
		{
			sfopen(sfstdin, NiL, "rt");
			n = state.select == ALL && state.act == ACT_CMDARG;
			for (;;)
			{
				if (s = sfgetr(sfstdin, state.separator, 1))
					len = sfvalue(sfstdin) - 1;
				else if (state.separator != '\n')
				{
					state.separator = '\n';
					continue;
				}
				else if (s = sfgetr(sfstdin, state.separator, -1))
					len = sfvalue(sfstdin);
				else
					break;
				if (!n)
					ftwalk(s, tw, state.ftwflags, NiL);
				else if ((i = cmdarg(state.cmd, s, len)) >= state.errexit)
					exit(i);
			}
			if (sferror(sfstdin))
				error(ERROR_SYSTEM|2, "input read error");
		}
		else if (firstdir == lastdir)
			ftwalk(firstdir->name, tw, state.ftwflags, state.sort);
		else
		{
			ap = av;
			while (firstdir = firstdir->next)
				*ap++ = firstdir->name;
			*ap = 0;
			ftwalk((char*)av, tw, state.ftwflags|FTW_MULTIPLE, state.sort);
		}
		if (state.cmd && (i = cmdflush(state.cmd)) >= state.errexit)
			exit(i);
		if (state.find && (findclose(state.find) || state.finderror))
			exit(2);
	}
	else if (state.select)
		error_info.errors = eval(state.select, &ftw) == 0;
	if (x = exexpr(state.program, "end", NiL, 0))
		eval(x, &ftw);
	if (sfsync(sfstdout))
		error(ERROR_SYSTEM|2, "write error");
	exit(error_info.errors != 0);
}
Esempio n. 29
0
int
extoken_fn(register Expr_t* ex)
{
	register int	c;
	register char*	s;
	register int	q;
	char*		e;

	if (ex->eof || ex->errors)
		return 0;
 again:
	for (;;) switch (c = lex(ex))
	{
	case 0:
		goto eof;
	case '/':
		switch (q = lex(ex))
		{
		case '*':
			for (;;) switch (lex(ex))
			{
			case '\n':
				BUMP (error_info.line);
				continue;
			case '*':
				switch (lex(ex))
				{
				case 0:
					goto eof;
				case '\n':
					BUMP (error_info.line);
					break;
				case '*':
					exunlex(ex, '*');
					break;
				case '/':
					goto again;
				}
				break;
			}
			break;
		case '/':
			while ((c = lex(ex)) != '\n')
				if (!c)
					goto eof;
			break;
		default:
			goto opeq;
		}
		/*FALLTHROUGH*/
	case '\n':
		BUMP (error_info.line);
		/*FALLTHROUGH*/
	case ' ':
	case '\t':
		break;
	case '(':
	case '{':
	case '[':
		ex->input->nesting++;
		return exlval.op = c;
	case ')':
	case '}':
	case ']':
		ex->input->nesting--;
		return exlval.op = c;
	case '+':
	case '-':
		if ((q = lex(ex)) == c)
			return exlval.op = c == '+' ? INC : DEC;
		goto opeq;
	case '*':
	case '%':
	case '^':
		q = lex(ex);
	opeq:
		exlval.op = c;
		if (q == '=')
			c = '=';
		else if (q == '%' && c == '%')
		{
			if (ex->input->fp)
				ex->more = (const char*)ex->input->fp;
			else ex->more = ex->input->sp;
			goto eof;
		}
		else exunlex(ex, q);
		return c;
	case '&':
	case '|':
		if ((q = lex(ex)) == '=')
		{
			exlval.op = c;
			return '=';
		}
		if (q == c)
			c = c == '&' ? AND : OR;
		else exunlex(ex, q);
		return exlval.op = c;
	case '<':
	case '>':
		if ((q = lex(ex)) == c)
		{
			exlval.op = c = c == '<' ? LS : RS;
			if ((q = lex(ex)) == '=')
				c = '=';
			else exunlex(ex, q);
			return c;
		}
		goto relational;
	case '=':
	case '!':
		q = lex(ex);
	relational:
		if (q == '=') switch (c)
		{
		case '<':
			c = LE;
			break;
		case '>':
			c = GE;
			break;
		case '=':
			c = EQ;
			break;
		case '!':
			c = NE;
			break;
		}
		else exunlex(ex, q);
		return exlval.op = c;
	case '#':
		if (!ex->linewrap && !(ex->disc->flags & EX_PURE))
		{
			s = ex->linep - 1;
			while (s > ex->line && isspace(*(s - 1)))
				s--;
			if (s == ex->line)
			{
				switch (extoken_fn(ex))
				{
				case DYNAMIC:
				case ID:
				case NAME:
					s = exlval.id->name;
					break;
				default:
					s = "";
					break;
				}
				if (streq(s, "include"))
				{
					if (extoken_fn(ex) != STRING)
						exerror("#%s: string argument expected", s);
					else if (!expush(ex, exlval.string, 1, NiL, NiL))
					{
						setcontext(ex);
						goto again;
					}
				}
				else exerror("unknown directive");
			}
		}
		return exlval.op = c;
	case '\'':
	case '"':
		q = c;
		sfstrset(ex->tmp, 0);
		ex->input->nesting++;
		while ((c = lex(ex)) != q)
		{
			if (c == '\\')
			{
				sfputc(ex->tmp, c);
				c = lex(ex);
			}
			if (!c)
			{
				exerror("unterminated %c string", q);
				goto eof;
			}
			if (c == '\n')
			{
				BUMP (error_info.line);
			}
			sfputc(ex->tmp, c);
		}
		ex->input->nesting--;
		s = sfstruse(ex->tmp);
		if (q == '"' || (ex->disc->flags & EX_CHARSTRING))
		{
			if (!(exlval.string = vmstrdup(ex->vm, s)))
				goto eof;
			stresc(exlval.string);
			return STRING;
		}
		exlval.integer = chrtoi(s);
		return INTEGER;
	case '.':
		if (isdigit(c = lex(ex)))
		{
			sfstrset(ex->tmp, 0);
			sfputc(ex->tmp, '0');
			sfputc(ex->tmp, '.');
			goto floating;
		}
		exunlex(ex, c);
		return exlval.op = '.';
	case '0': case '1': case '2': case '3': case '4':
	case '5': case '6': case '7': case '8': case '9':
		sfstrset(ex->tmp, 0);
		sfputc(ex->tmp, c);
		q = INTEGER;
		if ((c = lex(ex)) == 'x' || c == 'X')
		{
			sfputc(ex->tmp, c);
			for (;;)
			{
				switch (c = lex(ex))
				{
				case '0': case '1': case '2': case '3': case '4':
				case '5': case '6': case '7': case '8': case '9':
				case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': 
				case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': 
					sfputc(ex->tmp, c);
					continue;
				}
				break;
			}
		}
		else
		{
			while (isdigit(c))
			{
				sfputc(ex->tmp, c);
				c = lex(ex);
			}
			if (c == '#')
			{
				sfputc(ex->tmp, c);
				/* s = sfstruse(ex->tmp); */
				/* b = strtol(s, NiL, 10); */
				do
				{
					sfputc(ex->tmp, c);
				} while (isalnum(c = lex(ex)));
			}
			else
			{
				if (c == '.')
				{
				floating:
					q = FLOATING;
					sfputc(ex->tmp, c);
					while (isdigit(c = lex(ex)))
						sfputc(ex->tmp, c);
				}
				if (c == 'e' || c == 'E')
				{
					q = FLOATING;
					sfputc(ex->tmp, c);
					if ((c = lex(ex)) == '-' || c == '+')
					{
						sfputc(ex->tmp, c);
						c = lex(ex);
					}
					while (isdigit(c))
					{
						sfputc(ex->tmp, c);
						c = lex(ex);
					}
				}
			}
		}
		s = sfstruse(ex->tmp);
		if (q == FLOATING)
			exlval.floating = strtod(s, &e);
		else
		{
			if (c == 'u' || c == 'U')
			{
				q = UNSIGNED;
				c = lex(ex);
				exlval.integer = strToL(s, &e);
			}
			else
				exlval.integer = strToL(s, &e);
			if (*e)
			{
				*--e = 1;
				exlval.integer *= strton(e, &e, NiL, 0);
			}
		}
		exunlex(ex, c);
		if (*e || isalpha(c) || c == '_' || c == '$')
		{
			exerror("%s: invalid numeric constant", s);
			goto eof;
		}
		return q;
	default:
		if (isalpha(c) || c == '_' || c == '$')
		{
			sfstrset(ex->tmp, 0);
			sfputc(ex->tmp, c);
			while (isalnum(c = lex(ex)) || c == '_' || c == '$')
				sfputc(ex->tmp, c);
			exunlex(ex, c);
			s = sfstruse(ex->tmp);
			if (!(exlval.id = (Exid_t*)dtmatch(ex->symbols, s)))
			{
				if (!(exlval.id = newof(0, Exid_t, 1, strlen(s) - EX_NAMELEN + 1)))
				{
					exerror("out of space");
					goto eof;
				}
				strcpy(exlval.id->name, s);
				exlval.id->lex = NAME;
				dtinsert((ex->formals || !ex->symbols->view) ? ex->symbols : ex->symbols->view, exlval.id);
			}

			/*
			 * lexical analyzer state controlled by the grammar
			 */

			switch (exlval.id->lex)
			{
			case DECLARE:
				if (exlval.id->index == CHAR)
				{
					/*
					 * `char*' === `string'
					 * the * must immediately follow char
					 */

					if (c == '*')
					{
						lex(ex);
						exlval.id = id_string;
					}
				}
				break;
			case NAME:
				/*
				 * action labels are disambiguated from ?:
				 * through the expr.nolabel grammar hook
				 * the : must immediately follow labels
				 */

				if (c == ':' && !expr.nolabel)
					return LABEL;
				break;
			case PRAGMA:
				/*
				 * user specific statement stripped and
				 * passed as string
				 */

				{
					int	b;
					int	n;
					int	pc = 0;
					int	po;
					int	t;

					/*UNDENT...*/
	sfstrset(ex->tmp, 0);
	b = 1;
	n = 0;
	po = 0;
	t = 0;
	for (c = t = lex(ex);; c = lex(ex))
	{
		switch (c)
		{
		case 0:
			goto eof;
		case '/':
			switch (q = lex(ex))
			{
			case '*':
				for (;;)
				{
					switch (lex(ex))
					{
					case '\n':
						BUMP (error_info.line);
						continue;
					case '*':
						switch (lex(ex))
						{
						case 0:
							goto eof;
						case '\n':
							BUMP (error_info.line);
							continue;
						case '*':
							exunlex(ex, '*');
							continue;
						case '/':
							break;
						default:
							continue;
						}
						break;
					}
					if (!b++)
						goto eof;
					sfputc(ex->tmp, ' ');
					break;
				}
				break;
			case '/':
				while ((c = lex(ex)) != '\n')
					if (!c)
						goto eof;
				BUMP (error_info.line);
				b = 1;
				sfputc(ex->tmp, '\n');
				break;
			default:
				b = 0;
				sfputc(ex->tmp, c);
				sfputc(ex->tmp, q);
				break;
			}
			continue;
		case '\n':
			BUMP (error_info.line);
			b = 1;
			sfputc(ex->tmp, '\n');
			continue;
		case ' ':
		case '\t':
			if (!b++)
				goto eof;
			sfputc(ex->tmp, ' ');
			continue;
		case '(':
		case '{':
		case '[':
			b = 0;
			if (!po)
			{
				switch (po = c)
				{
				case '(':
					pc = ')';
					break;
				case '{':
					pc = '}';
					break;
				case '[':
					pc = ']';
					break;
				}
				n++;
			}
			else if (c == po)
				n++;
			sfputc(ex->tmp, c);
			continue;
		case ')':
		case '}':
		case ']':
			b = 0;
			if (!po)
			{
				exunlex(ex, c);
				break;
			}
			sfputc(ex->tmp, c);
			if (c == pc && --n <= 0)
			{
				if (t == po)
					break;
				po = 0;
			}
			continue;
		case ';':
			b = 0;
			if (!n)
				break;
			sfputc(ex->tmp, c);
			continue;
		case '\'':
		case '"':
			b = 0;
			sfputc(ex->tmp, c);
			ex->input->nesting++;
			q = c;
			while ((c = lex(ex)) != q)
			{
				if (c == '\\')
				{
					sfputc(ex->tmp, c);
					c = lex(ex);
				}
				if (!c)
				{
					exerror("unterminated %c string", q);
					goto eof;
				}
				if (c == '\n')
				{
					BUMP (error_info.line);
				}
				sfputc(ex->tmp, c);
			}
			ex->input->nesting--;
			continue;
		default:
			b = 0;
			sfputc(ex->tmp, c);
			continue;
		}
		break;
	}
	(*ex->disc->reff)(ex, NiL, exlval.id, NiL, sfstruse(ex->tmp), 0, ex->disc);

					/*..INDENT*/
				}
				goto again;
			}
			return exlval.id->lex;
		}
		return exlval.op = c;
	}
 eof:
	ex->eof = 1;
	return exlval.op = ';';
}
Esempio n. 30
0
static Dssmeth_t*
textmeth(const char* name, const char* options, const char* schema, Dssdisc_t* disc, Dssmeth_t* ometh)
{
	register Text_t*	text;
	register Dssmeth_t*	meth;
	register Cxvariable_t*	var;
	register char*		s;
	register char*		t;
	register char*		f;
	register int		c;
	char*			d;
	int			p;
	int			index;

	if (options)
	{
		if (dssoptlib(ometh->cx->buf, &dss_lib_text, usage, disc))
			goto drop;
		s = sfstruse(ometh->cx->buf);
		for (;;)
		{
			switch (optstr(options, s))
			{
			case '?':
				if (disc->errorf)
					(*disc->errorf)(NiL, disc, ERROR_USAGE|4, "%s", opt_info.arg);
				goto drop;
			case ':':
				if (disc->errorf)
					(*disc->errorf)(NiL, disc, 2, "%s", opt_info.arg);
				goto drop;
			}
			break;
		}
	}
	if (!schema || !*schema)
		return ometh;
	if (!(meth = newof(0, Dssmeth_t, 1, sizeof(Text_t) + strlen(name) + 2 * strlen(schema) + 2)))
	{
		free(meth);
		if (disc->errorf)
			(*disc->errorf)(NiL, disc, 2, "out of space");
		return 0;
	}
	*meth = *ometh;
	meth->data = text = (Text_t*)(meth + 1);
	text->format = strcopy(text->name, name) + 1;
	index = 0;
	s = (char*)schema;
	f = text->format;
	for (;;)
	{
		switch (c = *s++)
		{
		case 0:
			break;
		case '%':
			*f++ = '%';
			var = 0;
			switch (c = *s++)
			{
			case 0:
				goto invalid;
			case 'h': case 'l': case 'L':
			case '+': case '-': case '.': case '_':
			case '0': case '1': case '2': case '3': case '4':
			case '5': case '6': case '7': case '8': case '9':
				continue;
			case '%':
				*f++ = '%';
				continue;
			case '(':
				t = f;
				d = 0;
				p = 1;
				for (;;)
				{
					switch (c = *s++)
					{
					case 0:
						goto invalid;
					case '(':
						p++;
						*t++ = c;
						continue;
					case ')':
						if (!--p)
							break;
						*t++ = c;
						continue;
					case ':':
						if (d)
							*t++ = c;
						else
						{
							*t++ = 0;
							d = t;
						}
						continue;
					default:
						*t++ = c;
						continue;
					}
					break;
				}
				*t = 0;
				if (dtmatch(meth->cx->variables, f))
				{
					if (disc->errorf)
						(*disc->errorf)(NiL, disc, 2, "%s: duplicate field", f);
					goto drop;
				}
				if (!(var = newof(0, Cxvariable_t, 1, t - f + 1)))
				{
					if (disc->errorf)
						(*disc->errorf)(NiL, disc, 2, "out of space");
					goto drop;
				}
				var->index = index;
				t = strcopy((char*)(var->name = (char*)(var + 1)), f);
				if (d)
					var->description = strcpy(t + 1, d);
				break;
			}
			for (;;)
			{
				switch (c = *s++)
				{
				case 0:
					goto invalid;
				case 'h': case 'l': case 'L':
				case '+': case '-': case '.': case '_':
				case '0': case '1': case '2': case '3': case '4':
				case '5': case '6': case '7': case '8': case '9':
					continue;
				}
				break;
			}
			if (var)
			{
				switch (c)
				{
				case 'd':
				case 'f':
				case 'g':
				case 'n':
				case 'o':
				case 'u':
				case 'x':
					var->type = (Cxtype_t*)"number";
					break;
				case 'i':
					var->type = (Cxtype_t*)"ipaddr_t";
					break;
				case 's':
					var->type = (Cxtype_t*)"string";
					break;
				case 't':
					var->type = (Cxtype_t*)"time_t";
					break;
				default:
					if (disc->errorf)
						(*disc->errorf)(NiL, disc, 2, "%c: invalid field format >>>%s", c, s - 1);
					goto drop;
				}
				if (cxaddvariable(meth->cx, var, disc))
					goto drop;
			}
			index++;
			*f++ = c;
			continue;
		case ' ':
		case '\t':
		case '\n':
			if (f == text->format || *(f - 1) != ' ')
				*f++ = ' ';
			continue;
		default:
			*f++ = c;
			continue;
		}
		break;
	}
	if (!(text->vars = index))
		goto invalid;
	*f = 0;
	dtinsert(meth->formats, &text_format);
	for (c = 0; c < elementsof(local_callouts); c++)
		if (cxaddcallout(meth->cx, &local_callouts[c], disc))
			return 0;
	return meth;
 invalid:
	if (disc->errorf)
		(*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "%s: invalid schema", schema);
 drop:
	free(meth);
	return 0;
}