File:  [CSRG BSD Unix] / 43BSDReno / contrib / isode-beta / pepsy / etabs.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 16:12:56 2018 UTC (8 years, 1 month ago) by root
Branches: MAIN, BSD
CVS tags: HEAD, BSD43reno
BSD 4.3reno

/* etabs.c */

#ifndef	lint
static char *rcsid = "$Header: /var/lib/cvsd/repos/CSRG/43BSDReno/contrib/isode-beta/pepsy/etabs.c,v 1.1.1.1 2018/04/24 16:12:56 root Exp $";
#endif

/* 
 * $Header: /var/lib/cvsd/repos/CSRG/43BSDReno/contrib/isode-beta/pepsy/etabs.c,v 1.1.1.1 2018/04/24 16:12:56 root Exp $
 *
 *
 * $Log: etabs.c,v $
 * Revision 1.1.1.1  2018/04/24 16:12:56  root
 * BSD 4.3reno
 *
 * Revision 7.2  90/07/27  08:49:15  mrose
 * update
 * 
 * Revision 7.1  90/07/09  14:52:33  mrose
 * sync
 * 
 * Revision 7.0  90/07/01  19:54:18  mrose
 * *** empty log message ***
 * 
 */

/*
 *				  NOTICE
 *
 *    Acquisition, use, and distribution of this module and related
 *    materials are subject to the restrictions of a license agreement.
 *    Consult the Preface in the User's Manual for the full terms of
 *    this agreement.
 *
 */


#include <stdio.h>
#include <ctype.h>
#include "pepsydefs.h"
#include "sym.h"
#include "pass2.h"
#include "mine.h"

s_table *head;

extern s_table *lookup_list(), *proc_def();

extern FILE *fptab;
extern char *c_tag(), *c_class();
extern char *ec_tag(), *ec_class();
extern char *strip_last();
extern char *get_val(), *get_comp(), *get_string();
extern s_table *get_offset();
extern char *my_strcat(), *strp2name();
extern char *my_new_str();
extern char *mymodule;
extern char *modsym();
extern char *concat();
extern char *genlabel();
extern char *notidtoid();
extern YV calc_yv();
extern SY syfind();
static s_table *en_ptr;
extern s_table *ptr;

static int cons_type = 0;
/* int     explicit; */

s_table *save_ptr;

#define WORDSIZE	20
#define MAXNAME		256	/* maximum size of a identifier */

#ifdef	DEBUG
char   *str_yp_code[] = {
    "Undefined", "Boolean", "Integer", "Named number list", "Bitstring",
    "Named Bitstring list", "Octet String", "Null", "Sequence",
    "Sequence of", "Sequence list", "Set", "Set of", "Set list",
    "Choice", "Any", "Object Identifier",
    "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "Identifier",

};

#endif

/*
 * table encode a type. generate tables for the encoding of a type
 */
tenc_typ(yp, id, type)
YP      yp;
char   *id;
char   *type;
{

    char   *t, *f;
    char   *p1;
    char   *s1, *s2, *s3;
    char   *s;
    s_table *ptr1, *ptr2;
    YP      y;


    if (yp->yp_code < 0 || yp->yp_code > YP_REAL)
	ferrd(1, "tdec_typ: unimplemented type %d\n", yp->yp_code);

    if (yp == NULL) {
	ferr(0, "tenc_typ:NULL argument\n");
	return;
    }
    if (yp->yp_flags & YP_DEFAULT)
	gdflt(yp, G_ENC);

    if (yp->yp_flags & YP_TAG && !(yp->yp_flags & YP_IMPLICIT)) {
	(void) fprintf(fptab, "\t{ ETAG, 0, ");
	(void) fprintf(fptab, "%s, %s },\n", ec_tag(yp), ec_class(yp));
    }
    /* Preserve the type of the containing object */
    if (type)
	t = type;
    else
	t = my_strcat("struct ", modsym(mymodule, id, "type"));
    f = yp->yp_varexp;
    switch (yp->yp_code) {

    case YP_UNDF:
	ferr(1, "tenc_typ:Undefined type\n");

    case YP_BOOL:
	p1 = "BOOLEAN";
	if (yp->yp_varexp) {
	} else
	    t = NULL;
	break;

    case YP_INTLIST:

    case YP_INT:

    case YP_ENUMLIST:
	if (yp->yp_varexp) {
	    p1 = "INTEGER";
	    break;
	}
	t = NULL;
	p1 = NULL;
	(void) fprintf(fptab, "\t{ SINTEGER, 0, %s, %s },\n",
		c_tag(yp), c_class(yp));
	break;

    case YP_REAL:
	if (yp->yp_varexp) {
	    p1 = "REALTYPE";
	    break;
	}
	t = NULL;
	p1 = NULL;
	(void) fprintf(fptab, "\t{ SREALTYPE, 0, %s, %s },\n",
		c_tag(yp), c_class(yp));
	break;

    case YP_BIT:
    case YP_BITLIST:
	if (yp->yp_varexp) {
	    p1 = "BITSTRING";
	    break;
	}
	t = NULL;
	p1 = NULL;
	(void) fprintf(fptab, "\t{ SBITSTRING, 0, %s, %s },\n",
		c_tag(yp), c_class(yp));
	break;

    case YP_OCT:
	if (yp->yp_varexp) {
	    p1 = "OCTETSTRING";
	    break;
	}
	t = NULL;
	p1 = NULL;
	(void) fprintf(fptab, "\t{ SOCTETSTRING, 0, %s, %s },\n",
		c_tag(yp), c_class(yp));
	break;

    case YP_OID:
	if (yp->yp_varexp) {
	    p1 = "OBJID";
	    break;
	}
	t = NULL;
	p1 = NULL;
	(void) fprintf(fptab, "\t{ SOBJID, 0, %s, %s },\n",
		c_tag(yp), c_class(yp));
	break;


    case YP_SEQ:
    case YP_SET:
    case YP_ANY:
	if (yp->yp_varexp) {
	    p1 = "ANY";
	    break;
	}
	t = NULL;
	p1 = NULL;
	(void) fprintf(fptab, "\t{ SANY, 0, %s, %s },\n",
		c_tag(yp), c_class(yp));
	break;

    case YP_NULL:
	p1 = "T_NULL";
	t = NULL;
	break;

    case YP_IDEFINED:
	p1 = NULL;
	{
	    /* Predefined Universal Type */
	    struct univ_typ *p, *univtyp();

	    if ((p = univtyp(yp->yp_identifier))) {
		if (p->univ_flags & UNF_EXTMOD) {
		    yp->yp_module = p->univ_mod;
		    goto do_obj;
		}
		if (f == NULL) {/* No offset type */
		    if (yp->yp_flags & YP_TAG
			&& yp->yp_flags & YP_IMPLICIT)
			(void) fprintf(fptab, "\t{ S%s, 0, %d, %s },\n",
				p->univ_tab,
				yp->yp_tag->yt_value->yv_number,
				c_flags(yp, yp->yp_tag->yt_class));
		    else
			(void) fprintf(fptab, "\t{ S%s, 0, %d, %s },\n",
				p->univ_tab, p->univ_id,
				c_flags(yp, p->univ_class));
		    break;
		}
		if (yp->yp_flags & YP_TAG && yp->yp_flags & YP_IMPLICIT)
		    (void) fprintf(fptab, "\t{ %s, OFFSET(%s, %s), %d, %s },\n",
			    p->univ_tab, t, f,
			    yp->yp_tag->yt_value->yv_number,
			    c_flags(yp, yp->yp_tag->yt_class));
		else
		    (void) fprintf(fptab, "\t{ %s, OFFSET(%s, %s), %d, %s },\n",
			    p->univ_tab, t, f, p->univ_id,
			    c_flags(yp, p->univ_class));
		break;
	    }
	}
do_obj:
	if (yp->yp_flags & YP_TAG && yp->yp_flags & YP_IMPLICIT)
	    (void) fprintf(fptab, "\t{ IMP_OBJ, 0, %s, %s },\n", c_tag(yp), c_class(yp));
	if (yp->yp_parm) {
	    ferr(1, "tenc_typ:YP_IDEFINED:yp_parm found\n");
	}
	if (yp->yp_module == NULL
	    || strcmp(yp->yp_module, mymodule) == 0) {
	    if (f == NULL) {	/* No offset type */
		(void) fprintf(fptab, "\t{ SOBJECT, 0, _Z%s, %s },\n",
		      proc_name(yp->yp_identifier, 0), c_class(yp));
	    } else
		(void) fprintf(fptab,
			"\t{ OBJECT, OFFSET(%s, %s), _Z%s, %s },\n",
		t, f, proc_name(yp->yp_identifier, 0), c_class(yp));
	} else {
	    if (f == NULL) {	/* No offset type */
		(void) fprintf(fptab, "\t{ SEXTOBJ, 0, _Z%s, %s },\n",
			strp2name(yp->yp_identifier, yp->yp_module),
			c_class(yp));
	    } else
		(void) fprintf(fptab,
			"\t{ EXTOBJ, OFFSET(%s, %s), _Z%s, %s },\n",
		  t, f, strp2name(yp->yp_identifier, yp->yp_module),
			c_class(yp));

	    (void) fprintf(fptab, "\t{ EXTMOD, %d, 0, 0 },\n",
		    gen_modref(yp->yp_module));
	}
	break;

    case YP_SEQLIST:
	p1 = NULL;
	/* support for -h flag */
#if 0
	if (hflag && (y = yp->yp_type) && !y->yp_next) {
	    tenc_typ(y, id, yp->yp_structname);
	    break;
	}
#endif
	cons_type++;
	save_ptr = en_ptr;
	if (yp->yp_varexp == NULL && type != NULL)
	    ferr(1, "tenc_typ:YP_SEQLIST:NULL varexp pointer\n");
	if (type != NULL)
	    (void) fprintf(fptab, "\t{ SEQ_START, OFFSET(%s, %s), %s, %s },\n",
		    t, f, c_tag(yp), c_class(yp));
	else
	    (void) fprintf(fptab, "\t{ SEQ_START, 0, %s, %s },\n",
		    c_tag(yp), c_class(yp));
	if (y = yp->yp_type) {
	    if (type) {
		if (yp->yp_declexp == NULL)
		    ferr(1, "tenc_typ:YP_SEQLIST:no declexp\n");
		yp->yp_structname = my_strcat("struct ", yp->yp_declexp);
	    } else
		yp->yp_structname = t;
	    if (optfield(y)) {
		(void) fprintf(fptab,
			"\t{ OPTL, OFFSET(%s, optionals), 0, 0 },\n",
			yp->yp_structname);
	    }
	    tenc_loop(y, id, yp->yp_structname);
	}
	(void) fprintf(fptab, "\t{ PE_END, 0, 0, 0 },\n");
	en_ptr = save_ptr;
	cons_type--;
	break;

    case YP_SETLIST:
	p1 = NULL;
	/* support for -h flag */
#if 0
	if (hflag && (y = yp->yp_type) && !y->yp_next) {
	    tenc_typ(y, id, yp->yp_structname);
	    break;
	}
#endif
	cons_type++;
	if (yp->yp_varexp == NULL && type != NULL)
	    ferr(1, "tenc_typ:YP_SETLIST:NULL varexp pointer\n");
	if (type != NULL)
	    (void) fprintf(fptab, "\t{ SET_START, OFFSET(%s, %s), %s, %s },\n",
		    t, f, c_tag(yp), c_class(yp));
	else
	    (void) fprintf(fptab, "\t{ SET_START, 0, %s, %s },\n",
		    c_tag(yp), c_class(yp));
	if (y = yp->yp_type) {
	    if (type) {
		if (yp->yp_declexp == NULL)
		    ferr(1, "tenc_typ:YP_SETLIST:no declexp\n");
		yp->yp_structname = my_strcat("struct ", yp->yp_declexp);
	    } else
		yp->yp_structname = t;
	    if (optfield(y)) {
		(void) fprintf(fptab,
			"\t{ OPTL, OFFSET(%s, optionals), 0, 0 },\n",
			yp->yp_structname);
	    }
	    tenc_loop(y, id, yp->yp_structname);
	}
	(void) fprintf(fptab, "\t{ PE_END, 0, 0, 0 },\n");
	en_ptr = save_ptr;
	cons_type--;
	break;

    case YP_SEQTYPE:
	p1 = NULL;
	cons_type++;
	save_ptr = en_ptr;
	if (type != NULL)
	    (void) fprintf(fptab, "\t{ SEQOF_START, OFFSET(%s, %s), %s, %s },\n",
		    t, f, c_tag(yp), c_class(yp));
	else
	    (void) fprintf(fptab, "\t{ SEQOF_START, 0, %s, %s },\n",
		    c_tag(yp), c_class(yp));
	if (y = yp->yp_type) {
	    if (type) {
		if (yp->yp_declexp == NULL)
		    ferr(1, "tenc_typ:YP_SEQTYPE:no declexp\n");
		yp->yp_structname = my_strcat("struct ", yp->yp_declexp);
	    } else
		yp->yp_structname = t;
	    tenc_loop(y, id, yp->yp_structname);
	}
	if (yp->yp_structname != NULL)
	    (void) fprintf(fptab, "\t{ PE_END, OFFSET(%s, next), 0, 0 },\n",
		    yp->yp_structname);
	else
	    (void) fprintf(fptab, "\t{ PE_END, 0, 0, 0 },\n");
	en_ptr = save_ptr;
	cons_type--;
	break;

    case YP_SETTYPE:
	p1 = NULL;
	cons_type++;
	save_ptr = en_ptr;
	if (type != NULL)
	    (void) fprintf(fptab, "\t{ SETOF_START, OFFSET(%s, %s), %s, %s },\n",
		    t, f, c_tag(yp), c_class(yp));
	else
	    (void) fprintf(fptab, "\t{ SETOF_START, 0, %s, %s },\n",
		    c_tag(yp), c_class(yp));

	if (y = yp->yp_type) {
	    if (type) {
		if (yp->yp_declexp == NULL)
		    ferr(1, "tenc_typ:YP_SETTYPE:no declexp\n");
		yp->yp_structname = my_strcat("struct ", yp->yp_declexp);
	    } else
		yp->yp_structname = t;
	    tenc_loop(y, id, yp->yp_structname);
	}
	if (yp->yp_structname != NULL)
	    (void) fprintf(fptab, "\t{ PE_END, OFFSET(%s, next), 0, 0 },\n",
		    yp->yp_structname);
	else
	    (void) fprintf(fptab, "\t{ PE_END, 0, 0, 0 },\n");
	en_ptr = save_ptr;
	cons_type--;
	break;

    case YP_CHOICE:
	p1 = NULL;
	/* support for -h flag */
	if (hflag && (y = yp->yp_type) && !y->yp_next) {
	    tenc_typ(y, id, yp->yp_structname);
	    break;
	}
	cons_type++;
	save_ptr = en_ptr;
	if (type != NULL)
	    (void) fprintf(fptab, "\t{ CHOICE_START, OFFSET(%s, %s), 0, %s },\n",
		    t, f, c_class(yp));
	else
	    (void) fprintf(fptab, "\t{ CHOICE_START, 0, 0, 0 },\n");
	if (y = yp->yp_type) {
	    if (type) {
		if (yp->yp_declexp == NULL)
		    ferr(1, "tenc_typ:YP_CHOICE:no declexp\n");
		yp->yp_structname = my_strcat("struct ", yp->yp_declexp);
	    } else
		yp->yp_structname = t;
	    (void) fprintf(fptab, "\t{ SCTRL, OFFSET(%s, offset), 0, 0 },\n",
		    yp->yp_structname);
	    tenc_loop(y, id, yp->yp_structname);
	}
	(void) fprintf(fptab, "\t{ PE_END, 0, 0, 0 },\n");
	en_ptr = save_ptr;
	cons_type--;
	break;

    default:
	ferrd(1, "tenc_typ: yp_code = %d  not implemented\n", yp->yp_code);
    }

    if (p1 != NULL)
	if (t != NULL)
	    (void) fprintf(fptab, "\t{ %s, OFFSET(%s, %s), %s, %s },\n",
		    p1, t, f, c_tag(yp), c_class(yp));
	else
	    (void) fprintf(fptab, "\t{ %s, 0, %s, %s },\n",
		    p1, c_tag(yp), c_class(yp));
}

static int fflags[] = {
    0, 1, 2, 2, 3, 3, 4, 5, 16, 16, 16, 17, 17, 17,
0, -1, 6, 0, 10, 9};

/*
 * calculate the tag string of the given type and return it
 */
char   *
c_tag(yp)
YP      yp;
{
    static char buf[WORDSIZE];
    int     i;

    if (yp->yp_flags & YP_TAG && yp->yp_flags & YP_IMPLICIT) {
	i = yp->yp_tag->yt_value->yv_number;
    } else {
	if (yp->yp_code < 0 || yp->yp_code > YP_REAL
	    || yp->yp_code == YP_CHOICE)
	    i = 0;
	else
	    i = fflags[yp->yp_code];
	/* Choice now legal argument - to allow prte_* routines to work */
	if (i == 0 && yp->yp_code != YP_CHOICE)
	    ferrd (1, "c_tag:Unknown Tag %d", yp->yp_code);
    }

    (void) sprintf(buf, "%d", i);

    return (buf);
}

/*
 * calculate the tag string of the explicit tag and return it
 */
char   *
ec_tag(yp)
YP      yp;
{
    static char buf[WORDSIZE];
    int     i;

    if (!(yp->yp_flags & YP_TAG) || yp->yp_flags & YP_IMPLICIT)
	ferr(1, "ec_tag:internal error:called with out explicit tag\n");

    i = yp->yp_tag->yt_value->yv_number;

    (void) sprintf(buf, "%d", i);

    return (buf);
}

/*
 * produce a string that represents the class/flags field for a given
 * yp entry taking the class to be that given in cl
 */
char   *
c_flags(yp, cl)
YP      yp;
int     cl;
{
    char   *p1;
    static char buf[STRSIZE];

    switch (yp->yp_code) {
    case YP_IDEFINED:
    case YP_CHOICE:
	if (yp->yp_flags & YP_TAG)
	    break;
	if (yp->yp_flags & YP_OPTIONAL) {
	    p1 = "FL_OPTIONAL";
	} else if (yp->yp_flags & YP_DEFAULT) {
	    p1 = "FL_DEFAULT";
	} else
	    p1 = "0";
	return (p1);

    default:
	break;
    }
    switch (cl) {
    case PE_CLASS_UNIV:
	p1 = "FL_UNIVERSAL";
	break;

    case PE_CLASS_APPL:
	p1 = "FL_APPLICATION";
	break;

    case PE_CLASS_PRIV:
	p1 = "FL_PRIVATE";
	break;

    case PE_CLASS_CONT:
	p1 = "FL_CONTEXT";
	break;

    default:
	ferrd(1, "c_flags: illegal class found %d\n", cl);

    }
    if (yp->yp_flags & YP_OPTIONAL) {
	strncpy(buf, p1, STRSIZE);
	p1 = strncat(buf, "|FL_OPTIONAL", STRSIZE);
    } else if (yp->yp_flags & YP_DEFAULT) {
	strncpy(buf, p1, STRSIZE);
	p1 = strncat(buf, "|FL_DEFAULT", STRSIZE);
    }
    return (p1);
}
/*
 * calculate a string specifying the class for the given type and
 * return it
 */
char   *
c_class(yp)
YP      yp;
{
    int     i;

    if (yp->yp_flags & YP_TAG && yp->yp_flags & YP_IMPLICIT) {
	i = yp->yp_tag->yt_class;
    } else {
	i = PE_CLASS_UNIV;
    }
    return (c_flags(yp, i));

}
/*
 * calculate a string specifying the class for the explicit tag and
 * return it
 */
char   *
ec_class(yp)
YP      yp;
{
    int     i;
    char   *p1;
    static char buf[STRSIZE];

    if (!(yp->yp_flags & YP_TAG) || yp->yp_flags & YP_IMPLICIT)
	ferr(1, "ec_class:internal error:called with out explicit tag\n");
    switch (yp->yp_code) {
    case YP_IDEFINED:
    case YP_CHOICE:
	if (yp->yp_flags & YP_TAG)
	    break;
	if (yp->yp_flags & YP_OPTIONAL) {
	    p1 = "FL_OPTIONAL";
	} else if (yp->yp_flags & YP_DEFAULT) {
	    p1 = "FL_DEFAULT";
	} else
	    p1 = "0";
	return (p1);

    default:
	break;
    }

    i = yp->yp_tag->yt_class;

    switch (i) {
    case PE_CLASS_UNIV:
	p1 = "FL_UNIVERSAL";
	break;

    case PE_CLASS_APPL:
	p1 = "FL_APPLICATION";
	break;

    case PE_CLASS_PRIV:
	p1 = "FL_PRIVATE";
	break;

    case PE_CLASS_CONT:
	p1 = "FL_CONTEXT";
	break;

    default:
	ferrd(1, "c_class: illegal class found %d\n", i);

    }
    if (yp->yp_flags & YP_OPTIONAL) {
	strncpy(buf, p1, STRSIZE);
	p1 = strncat(buf, "|FL_OPTIONAL", STRSIZE);
    } else if (yp->yp_flags & YP_DEFAULT) {
	strncpy(buf, p1, STRSIZE);
	p1 = strncat(buf, "|FL_DEFAULT", STRSIZE);
    }
    return (p1);
}

/*
 * generate tables for encoding a contructed type
 */
tenc_loop(yp, id, type)
YP      yp;
char   *id;
char   *type;
{
    for (; yp != NULL; yp = yp->yp_next) {
	tenc_typ(yp, id, type);
    }
}

/*
 * Print the string and exit if argument greater than zero
 */
ferr(i, s)
int     i;
char   *s;
{
    (void) fprintf(stderr, "%s", s);
    if (i > 0)
	exit(i);
}

/*
 * Print the integer and exit if argument greater than zero
 */
ferrd(i, s, d)
int     i;
char   *s;
int     d;
{
    (void) fprintf(stderr, s, d);
    if (i > 0)
	exit(i);
}

/*
 * Print the string and exit if argument greater than zero
 */
ferrs(i, s, d)
int     i;
char   *s;
char   *d;
{
    (void) fprintf(stderr, s, d);
    if (i > 0)
	exit(i);
}

/*
 * return a copy of the string s minus its last character
 */
char   *
strip_last(s)
char   *s;
{
    char   *t, *r;

    if (s) {
	t = new_string(s);
	for (r = t; *r != '\0'; r++);
	;
	*--r = '\0';
	return t;
    } else
	return NULL;
}

/*
 * add the declaration specified by the strings type and id to the
 * start of the declaration list
 */
add_list(type, id)
char   *type, *id;
{

    s_table *prev;

    if ((prev = (s_table *) malloc(sizeof(s_table))) == NULL)
	ferr(1, "add_list: Out of memory\n");
    prev->type = type;
    prev->name = id;
    prev->parent = NULL;
    prev->defined = 0;
    prev->next = head;
    head = prev;
}

#if 0
/*
 * return the element in the declaration list whose name field is id
 */
s_table *
lookup_list(id)
char   *id;
{
    s_table *prev;

    for (prev = head; prev != NULL; prev = prev->next)
	if (!strcmp(prev->name, id))
	    return prev;
    return (NULL);
    /* ferrs(1, "lookup_list: the id %s is not present\n",id); */
}
#endif

/*
 * print the declaration list
 */
print_list()
{
    s_table *prev;

    for (prev = head; prev != NULL; prev = prev->next) {
	(void) printf("type is %s\n", prev->type);
	(void) printf("name is %s\n", prev->name);
	(void) printf("\n");
    }
}

/*
 * parse the declaration in the string s returning the type in v1 and
 * the name in v2
 */
parse_decl(s, v1, v2)
char  **s, **v1, **v2;
{
    char   *t;

    for (t = *s; *t != '\0' && !(isalnum(*t) || *t == '_'); t++);

    *s = t;
    if (*t != '\0') {
	for (; *t != '*'; t++);
	*t = '\0';
	*v1 = my_strcat(*s, "*");
	Printf(3, ("the type is %s\n", *v1));
	if (*++t == '*')
	    t++;
	for (*s = t; isalnum(*t) || *t == '_'; t++);
	if (*t != '\0') {
	    *t = '\0';
	    t++;
	}
	*v2 = new_string(*s);	/* don't really need new_string */
	Printf(2, ("the name is %s\n", *v2));
	*s = t;
    }
}

/*
 * return the next identifier in the string s
 */
char   *
get_val(s)
char  **s;
{
    char   *t, *r;

    for (t = *s; *t != '\0' && !(isalnum(*t) || *t == '_' || *t == '.'); t++);

    if (*t != '\0') {
	for (*s = t; isalnum(*t) || *t == '_' || *t == '.'; t++);
	*t = '\0';
	r = *s;
	Printf(3, ("arg is |%s|\n", r));
	*s = ++t;
	return r;
    } else
	return NULL;
}

#if 0
/*
 * add the definition contained in s to the start of the declaration
 * list
 */
s_table *
proc_def(s)
char   *s;
{
    char   *s1, *s2, *s3;
    s_table *ptr1, *ptr2;

    s1 = get_val(&s);
    ptr1 = lookup_list(s1);
    if (!ptr1->defined) {
	s2 = get_val(&s);
	ptr2 = lookup_list(s2);
	ptr1->parent = ptr2;
	s3 = get_val(&s);
	ptr1->field = s3;
	ptr1->defined++;
    }
    return ptr1;
}
#endif

/*
 * return the next component (sequence of characters up to the next
 * ';' or '\0') of the string s
 */
char   *
get_comp(s)
char  **s;
{
    char   *t, *r;

    for (t = *s; *t != '\0' && !(isalnum(*t) || *t == '_' || *t == ';'); t++);

    if (*t != '\0') {
	for (*s = t; *t != '\0' && *t != ';'; t++);
	*t = '\0';
	r = *s;
	Printf(3, ("component is |%s|\n", r));
	*s = ++t;
	return r;
    } else
	return NULL;
}

#if 0
/*
 * work out what the offset for the type yp should be and return a
 * pointer to the element in the declaration list which can be used
 * to calculate the offset
 */
s_table *
get_offset(yp, level)
YP      yp;
int     level;
{
    char   *s, *s1;
    s_table *ptr1;

    switch (yp->yp_code) {

    case YP_UNDF:
	ferr(1, "get_offset: Undefined type\n");
	break;

    case YP_BOOL:
    case YP_INT:
    case YP_INTLIST:
    case YP_NULL:
	if (yp->yp_varexp) {
	    s = new_string(yp->yp_intexp);
	    s1 = get_val(&s);
	    ptr1 = lookup_list(s1);
	    return ptr1;
	} else {
	    ferr(0, "get_offset: No intexp\n");
	    return NULL;
	}
	break;

    case YP_BIT:
    case YP_BITLIST:
	if (yp->yp_direction & YP_DECODER)
	    if (yp->yp_action2) {
		s = new_string(yp->yp_action2);
		s1 = get_val(&s);
		if (strcmp(s1, "if"))
		    ferrs(1, "get_offset: What to do, action2 = %s\n", yp->yp_action2);
		else {
		    s1 = get_val(&s);
		    ptr1 = lookup_list(s1);
		    return ptr1;
		}
	    } else
		return NULL;
	else if (yp->yp_varexp) {
	    s = new_string(yp->yp_varexp);
	    s1 = get_val(&s);
	    s1 = get_val(&s);
	    ptr1 = lookup_list(s1);
	    return ptr1;
	} else {
	    ferr(0, "get_offset: No strexp (BIT)\n");
	    return NULL;
	}
	break;
    case YP_OCT:
    case YP_OID:
    case YP_SEQ:
    case YP_SET:
    case YP_ANY:
	if (yp->yp_varexp) {
	    s = new_string(yp->yp_varexp);
	    s1 = get_val(&s);
	    ptr1 = lookup_list(s1);
	    return ptr1;
	} else {
	    ferr(0, "get_offset: No strexp\n");
	    return NULL;
	}
	break;

    case YP_IDEFINED:
	if (yp->yp_parm) {
	    s = new_string(yp->yp_parm);
	    s1 = get_val(&s);
	    ptr1 = lookup_list(s1);
	    return ptr1;
	} else {
	    ferr(0, "get_offset: No parm\n");
	    return NULL;
	}
	break;

    case YP_SEQLIST:
    case YP_SETLIST:
    case YP_CHOICE:
	if (yp->yp_action1) {
	    s = get_string(yp->yp_action1, yp->yp_direction);
	    while ((s1 = get_comp(&s)) != NULL)
		(void) proc_def(s1);
	}
	if (yp->yp_type == NULL) {
	    if (level > 0)
		ferr(0, "get_offset: There exists an embeded empty SEQUENCE/SET/CHOICE\n");
	    return (yp->yp_direction & YP_ENCODER ? en_ptr : ptr);
	}
	if ((ptr1 = get_offset(yp->yp_type, level + 1)) != NULL)
	    if (ptr1->parent != NULL && level > 0)
		return ptr1->parent;
	    else
		return ptr1;
	else {
	    ferr(0, "get_offset: returning NULL (SEQLIST/SETLIST/CHOICE)\n");
	    return NULL;
	}
	break;

    case YP_SEQTYPE:
    case YP_SETTYPE:
	if (yp->yp_action3) {
	    s = get_string(yp->yp_action3, yp->yp_direction);
	    while ((s1 = get_comp(&s)) != NULL)
		(void) proc_def(s1);
	}
	if ((ptr1 = get_offset(yp->yp_type, level + 1)) != NULL)
	    if (ptr1->parent != NULL && level > 0)
		return ptr1->parent;
	    else
		return ptr1;
	else {
	    ferr(0, "get_offset: returning NULL (SEQTYPE/SETTYPE)\n");
	    return NULL;
	}
	break;

    default:
	ferrd(1, "get_offset: yp_code = %d  not implemented\n", yp->yp_code);
	return NULL;
	break;
    }
}
#endif

/*
 * return a copy of that part of the string s which may contain
 * definitions for the variables generated by posy
 */
char   *
get_string(s, direction)
char   *s;
int     direction;
{
    char   *t, *t1;

    if (direction & YP_ENCODER)
	return new_string(s);
    if (direction & YP_DECODER) {
	t = new_string(s);
	for (t1 = t; !(isalnum(*t1) || *t1 == '_'); t1++);
	if (*t1 == 'i' && *++t1 == 'f' && *++t1 == ' ') {	/* MALLOC code */
	    for (; *t1 != '}'; t1++)	/* skip MALLOC code */
		;
	    t1++;
	    Printf(4, ("returning the string %s\n", t1));
	    return t1;
	} else
	    return t;
    }
}

#if 0
c_type(yp)
YP      yp;
{
    switch (yp->yp_code) {

    case YP_IDEFINED:
	return T_OBJ;
	break;


    default:
	return T_PRIM;
	break;
    }
}

#endif
/*
 * Determine wether this list contains any items that will generate
 * an optional field. If so return non zero
 */
optfield(yp)
YP      yp;
{
    for (; yp; yp = yp->yp_next) {
	if (yp->yp_flags & YP_OPTIONAL)
	    switch (yp->yp_code) {
	    case YP_BOOL:
	    case YP_INT:
	    case YP_INTLIST:
	    case YP_ENUMLIST:
	    case YP_NULL:
		return (1);
	    }
    }
    return (0);
}

gen_dflts(yp, type)
YP      yp;
char   *type;
{
    YP      y;

    if (yp == NULL)
	return;
    switch (yp->yp_code) {
    case YP_IDEFINED:
#if 0
	if (yp->yp_module == NULL
	    || strcmp(yp->yp_module, mymodule) == 0)
	    break;
	if (lookup_list(yp->yp_module))
	    break;		/* already done */
	/* Got an external reference */
	(void) fprintf(fptab, "#include \"%s%s\"\n", yp->yp_module, HFILE2);
	add_list("module", yp->yp_module);
#endif
	break;

    case YP_CHOICE:
    case YP_SEQTYPE:
    case YP_SETTYPE:
    case YP_SEQLIST:
    case YP_SETLIST:
	for (y = yp->yp_type; y != NULL; y = y->yp_next) {
	    gen_dflts(y, type);
	}
	break;

    default:
	break;
    }
    /* Output definitions for default entries */
    if (yp->yp_flags & YP_DEFAULT)
	defdflt(yp, type);
}
/*
 * Compute the concatenation into a temporary buffer of two strings
 * after having run notid on them first
 */
char   *
strp2name(s1, s2)
char   *s1, *s2;
{
    char   *p;
    static char buf[STRSIZE * 2 + 5];

    if (strlen(s1) > STRSIZE || strlen(s2) > STRSIZE)
	ferr(1, "strp2name:string to big\n");
    strcpy(buf, p = notidtoid(s1));
    free(p);
    strcat(buf, p = notidtoid(s2));
    free(p);

    return (buf);
}

/*
 * Output the definitions for default entries and initialise the yp's
 * to have pointers which reference these definitions for use by
 * gdflt routine.
 */
defdflt(yp, name)
YP      yp;
char   *name;
{
    YV      yv;
    YV      yv1;
    SY      sy;
    YP      yp1;
    int     size, i;
    char   *str;
    char   *label;
    struct univ_typ	*p;
    int		code;

    if ((yp->yp_flags & YP_DEFAULT) == 0)
	ferrd(1, "defdflt:called with out a default code = %d\n", yp->yp_code);
    yv = yp->yp_default;

    yp1 = yp;

    /* Find the bottom definition */
    code = yp1->yp_code;
    while (code == YP_IDEFINED) {
	if ((sy = syfind(yp1->yp_identifier)) == NULL) {
	    if ((p = univtyp(yp1->yp_identifier)) == NULL
	       || p->univ_type <= YP_UNDF)
		ferrs(1,
		    "defdflt:IDEFINED:cannot find definition of symbol %s\n",
		    yp1->yp_identifier);
	    code = p->univ_type;
	    break;
		
	}
	yp1 = sy->sy_type;
	code = yp1->yp_code;
    }

    switch (code) {
    case YP_BOOL:
    case YP_INT:
    case YP_INTLIST:
    case YP_ENUMLIST:
	switch (yv->yv_code) {
	case YV_NUMBER:
	case YV_BOOL:
	    /* None needed */
	    break;

	case YV_IDEFINED:
	    if ((yv1 = calc_yv(yp1, yv->yv_identifier)) == NULL) {
		ferrs(1, "defdflt:BOOL/INT:cannot find definition of %s\n",
		      yv->yv_identifier);
	    }
	    /* None Needed */
	    break;

	default:
	    ferrd(1, "defdflt:INT/BOOL:unimplemented value code = %d\n",
		  yv->yv_code);
	}
	break;

    case YP_REAL:
	switch (yv->yv_code) {
	case YV_REAL:
	     yv1 = yv;
	     goto dumpdef3;

	case YV_IDEFINED:
	    if ((yv1 = calc_yv(yp1, yv->yv_identifier)) == NULL) {
		ferrs(1, "defdflt:REAL:cannot find definition of %s\n",
		      yv->yv_identifier);
	    }
	    goto dumpdef3;
	    break;

	default:
	    ferrd(1, "defdflt:REAL:unimplemented value code = %d\n",
		  yv->yv_code);
	}
	break;

    case YP_BIT:
    case YP_BITLIST:
	switch (yv->yv_code) {
	    /*
	     * This is an illegal value for a bit string ! - BUT ACSE
	     * uses it !
	     */
	    /* gdflt also patched to support it */
	case YV_IDEFINED:
	    ferrs(0,
		  "warning:bitstring default specified illegally with identifier %s\n",
		  yv->yv_identifier);
	    if ((yv1 = calc_yv(yp1, yv->yv_identifier)) == NULL) {
		ferrs(1, "defdflt:BIT:cannot find definition of %s\n",
		      yv->yv_identifier);
	    }
	    /* doesn't work fix posy-yacc.y */
	    size = numtobstr(yv1, &str);
	    goto dumpdef1;

	case YV_NUMBER:
	    /* doesn't work fix posy-yacc.y */
	    size = numtobstr(yv, &str);
	    goto dumpdef1;

	case YV_VALIST:
	    if ((size = valisttobs(yp1, yv, &str)) < 0) {
		ferrs(1, "defdflt:bad default value for bistring %s\n",
		      yp->yp_flags & YP_ID ? yp->yp_identifier : "");
	    }
	    goto dumpdef1;

	default:
	    /* Could be a syntax error */
	    ferrd(1, "defdflt:BIT:illegal value code = %d\n", yv->yv_code);
	}
	break;

    case YP_IDEFINED:
	ferrs(1, "defdflt:IDEFINED:internal error on symbol %s\n",
	      yp1->yp_identifier);
	break;

    case YP_OCT:
	switch (yv->yv_code) {
	case YV_NUMBER:
	    /* doesn't work fix posy-yacc.y */
	    size = numtobstr(yv, &str);
	    goto dumpdef2;

	case YV_STRING:
	    str = yv->yv_string;
	    size = strlen(str);
	    goto dumpdef2;

	default:
	    /* Could be a syntax error */
	    ferrd(1, "defdflt:OCT:illegal value code = %d\n", yv->yv_code);
	}
	break;

    case YP_NULL:
    case YP_SEQ:
    case YP_SEQTYPE:
    case YP_SEQLIST:
    case YP_SET:
    case YP_SETTYPE:
    case YP_SETLIST:
    case YP_CHOICE:
    case YP_ANY:
    case YP_OID:
	/* None yet */
	break;

    default:
	ferrd(1, "defdflt:unknown type %d\n", code);
    }

    return;

dumpdef1:	/* Bitstrings */
    label = genlabel(name, yp);
    yp->yp_action0 = label;
    yp->yp_act0_lineno = size;
    i = (size + NBPC - 1) / NBPC;
    (void) fprintf(fptab, "\nstatic char %s[] = ", label);
    if (printable(str, i))
	prstr(fptab, str, i);
    else
	prhstr(fptab, str, i);
    (void) fprintf(fptab, ";\n");
    return;

dumpdef2:	/* Octet strings (and aliases) */
    label = genlabel(name, yp);
    yp->yp_action0 = label;
    yp->yp_act0_lineno = size;
    (void) fprintf(fptab, "\nstatic char %s[] = ", label);
    if (printable(str, size))
	prstr(fptab, str, size);
    else
	prhstr(fptab, str, size);
    (void) fprintf(fptab, ";\n");
    return;

dumpdef3:	/* Reals */
    label = genlabel(name, yp);
    yp->yp_action0 = label;
    (void) fprintf(fptab, "\nstatic double %s = %f;\n", label, yv1->yv_real);
    return;

}
/*
 * generate the default entry for encoding/decoding fields. This
 * should contain the default value which the encoder will know means
 * default encoding
 */
gdflt(yp, which)
YP      yp;
int     which;			/* Which type of entries to generate
				 * G_ENC encode G_DEC decode */
{
    YV      yv;
    YV      yv1;
    SY      sy;
    YP      yp1;
    int     size;
    char   *str;

    char   *ndflt;
    struct univ_typ     *p;
    int         code;


    if (which == G_ENC)
	ndflt = "DFLT_F";
    else
	ndflt = "DFLT_B";

    if ((yp->yp_flags & YP_DEFAULT) == 0)
	ferrd(1, "gdflt:called with out a default code = %d\n", yp->yp_code);
    yv = yp->yp_default;

    yp1 = yp;

    /* Find the bottom definition */
    code = yp1->yp_code;
    while (code == YP_IDEFINED) {
	if ((sy = syfind(yp1->yp_identifier)) == NULL) {
             if ((p = univtyp(yp1->yp_identifier)) == NULL
               || p->univ_type <= YP_UNDF)
		    ferrs(1,
			"gdflt:IDEFINED:cannot find definition of symbol %s\n",
			yp1->yp_identifier);
	     code = p->univ_type;
	     break;

	}
	yp1 = sy->sy_type;
	code = yp1->yp_code;
    }

    switch (code) {
    case YP_BOOL:
    case YP_INT:
    case YP_INTLIST:
    case YP_ENUMLIST:
	switch (yv->yv_code) {
	case YV_NUMBER:
	case YV_BOOL:
	    (void) fprintf(fptab, "\t{ %s,	%d,	0,	0 },\n", ndflt,
		    yp->yp_default->yv_number);
	    break;

	case YV_IDEFINED:
	    if ((yv1 = calc_yv(yp1, yv->yv_identifier)) == NULL) {
		ferrs(1, "gdflt:BOOL/INT:cannot find definition of %s\n",
		      yv->yv_identifier);
	    }
	    (void) fprintf(fptab, "\t{ %s,	%d,	0,	0 },\n", ndflt,
		    yv1->yv_number);
	    break;

	default:
	    ferrd(1, "gdflt:INT/BOOL:unimplemented value code = %d\n",
		  yv->yv_code);
	}
	break;

    case YP_BIT:
    case YP_BITLIST:
	switch (yv->yv_code) {
#ifdef ILLEGAL_DEFAULTS
	case YV_IDEFINED:	/* supporting illegal default
				 * specification */
#endif
	case YV_NUMBER:
	case YV_VALIST:
	    (void) fprintf(fptab, "\t{ %s,	%d,	%d,	0 },\n", ndflt,
		    yp->yp_act0_lineno, addptr(yp->yp_action0));
	    break;

	default:
	    /* Could be a syntax error */
	    ferrd(1, "gdflt:BIT:illegal value code = %d\n", yv->yv_code);
	}
	break;

    case YP_IDEFINED:
	ferrs(1, "gdflt:IDEFINED:internal error on symbol %s\n",
	      yp1->yp_identifier);
	break;

    case YP_REAL:
	switch (yv->yv_code) {
#ifdef ILLEGAL_DEFAULTS
	case YV_IDEFINED:	/* Illegal according to ASN.1 but we can do it
				 * so why not support it
				 */
#endif
	case YV_REAL:
	    (void) fprintf(fptab, "\t{ %s,	0,	%d,	0 },\n", ndflt,
		    addptr(concat("&", yp->yp_action0)));
	    break;

	default:
	    /* Could be a syntax error */
	    ferrd(1, "gdflt:REAL:illegal value code = %d\n", yv->yv_code);
	}
	break;

    case YP_OCT:
	switch (yv->yv_code) {
	case YV_NUMBER:
	case YV_STRING:
	    (void) fprintf(fptab, "\t{ %s,	%d,	%d,	0 },\n", ndflt,
		    yp->yp_act0_lineno, addptr(yp->yp_action0));
	    break;

	default:
	    /* Could be a syntax error */
	    ferrd(1, "gdflt:OCT:illegal value code = %d\n", yv->yv_code);
	}
	break;

    case YP_NULL:
    case YP_SEQ:
    case YP_SEQTYPE:
    case YP_SEQLIST:
    case YP_SET:
    case YP_SETTYPE:
    case YP_SETLIST:
    case YP_CHOICE:
    case YP_ANY:
    case YP_OID:
	(void) fprintf(fptab, "\t{ %s,	0,	0,	0 },\n", ndflt);
	break;

    default:
	ferrd(1, "gdflt:unknown type %d\n", yp->yp_code);
    }

}
/*
 * Calculate the value associated with the given identifier id by
 * looking at the value definitions associated with type definition
 * yp. Returns the value definition if found or NULL if not.
 */
YV
calc_yv(yp, id)
YP      yp;
char   *id;
{
    YV      yv;

    for (yv = yp->yp_value; yv != NULL; yv = yv->yv_next) {
	if (yv->yv_flags & YV_NAMED && strcmp(yv->yv_named, id) == 0)
	    return (yv);
    }

    return (NULL);
}

/*
 * ******* This does not work. posy needs to be fixed for case of
 * '01'b ***** Turn a Literal number value in yv into a bistring
 * initialisation. Return the length of the bit string or less than
 * zero on error. Set the (char *) pointer, whose address is in
 * ppstr, to point to a string containing the a reference to a
 * character array which contains the bits.
 */

numtobstr(yv, ppstr)
YV      yv;
char  **ppstr;
{

    int     ibits, lastb, i;
    char   *buf;

    buf = malloc(NBPI / NBPC + 1);
    bzero(buf, NBPI / NBPC + 1);
    lastb = -1;
    ibits = yv->yv_number;
    for (i = 0; i < NBPI; i++) {
	if ((1 << i) & ibits) {
	    buf[i / NBPC] |= 1 << (NBPC - 1 - (i % NBPC));
	    lastb = i;
	}
    }

    *ppstr = buf;
    return (lastb + 1);
}
#define ROUNDUP		10
/*
 * Take a list of Values (YV_VALIST) which should contain a list of
 * bits and convert them into a bitstring initialisation. As in
 * numtobstr return the size of the bit string or a negative number
 * if there is an error. Put a reference to a character array which
 * contains the definition of the bits in the character pointer whose
 * address is in ppstr. yp is the definition of the type which
 * contains the names of all the defined bits.
 */
valisttobs(yp, yv, ppstr)
YP      yp;
YV      yv;
char  **ppstr;
{

    YV      yv1, yv2;
    int     lastb, val, nsize, size;
    char   *buf;

    lastb = -1;
    size = ROUNDUP;
    if ((buf = malloc(size)) == NULL) {
	ferrd(1, "valisttobs:malloc:failed on %d\n", size);
    }
    bzero(buf, size);
    for (yv1 = yv->yv_idlist; yv1 != NULL; yv1 = yv1->yv_next) {
	if ((yv2 = calc_yv(yp, yv1->yv_identifier)) == NULL) {
	    return (-1);
	}
	val = yv2->yv_number;
	/* Bug here probably */
	if (size < val / NBPC) {
	    nsize = val / NBPC + ROUNDUP;
	    if ((buf = realloc(buf, nsize)) == NULL) {
		ferrd(1, "valisttobs:realloc:failed on %d\n", nsize);
	    }
	    bzero(buf + size, nsize - size);
	    size = nsize;
	}
	buf[val / NBPC] |= 1 << (NBPC - 1 - (val % NBPC));
	if (val > lastb)
	    lastb = val;
    }
    *ppstr = buf;
    return (lastb + 1);
}
/*
 * Print the string out in a format acceptable as a quoted string in
 * a C program including the quotes. Using \ escapes for unprintable
 * characters
 */
prstr(fp, str, len)
FILE   *fp;
char   *str;
int     len;
{
    (void) fputc('"', fp);
    while (len-- > 0) {
	if (isprint(*str & 0xff)) {
	    (void) fputc(*str & 0xff, fptab);
	    str++;
	    continue;
	}
	(void) fprintf(fptab, "\\%0o", *str);
    }
    (void) fputc('"', fp);
#define MAXPLINE	16
}
/*
 * output a initialisation for a character array as unsigned hex
 * numbers
 */
prhstr(fptab, str, len)
FILE   *fptab;
char   *str;
int     len;
{
    int     npline;		/* number on this line */

    (void) fprintf(fptab, "{\n");
    npline = 0;
    while (len > 0) {
	if (npline >= MAXPLINE) {
	    (void) fputc('\n', fptab);
	    npline = 0;
	}
	npline++;
	(void) fprintf(fptab, " 0x%02x,", *str++ & 0xff);
	len--;
    }
    (void) fprintf(fptab, "}");
}
/*
 * determine if the string is printable i.e. only sensible to be read
 * as a character string. 1 (true) if it is 0, if it isn't
 */
printable(str, i)
char   *str;
int     i;
{
    while (i-- > 0) {
	if (!isprint(*str & 0xff))
	    return (0);		/* look for the first non printable
				 * character */
    }
    return (1);
}
/*
 * generate a unique identifier  using the name given and the name if
 * present in yp. Return a pointer to it in a space malloc'ed out
 */
char   *
genlabel(name, yp)
char   *name;
YP      yp;
{
    char    buf[MAXNAME];
    static int cnt;
    char   *p1, *p2;

    p1 = notidtoid(name);
    if (yp->yp_flags & YP_ID) {
	p2 = notidtoid(yp->yp_id);
	(void) sprintf(buf, "L%s_%s_%d", p1, p2, cnt++);
	free(p2);
    } else
	(void) sprintf(buf, "L%s_X_%d", p1, cnt++);
    free(p1);

    return (my_new_str(buf));
}
/*
 * generate a ptr table reference for the given module table entry
 */
gen_modref(mod)
char	*mod;
{
    char	buf[BUFSIZ];
    char	*p1;
    int		ind;

    p1 = notidtoid(mod);
    (void) sprintf(buf, "&%s%s%s", PREFIX, p1, MODTYP_SUFFIX);
    ind = addptr(buf);
    free(p1);

    return (ind);
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.