📄 asn_gen.c
字号:
/*****************************************************************************
File: asn_gen.c
Contents: Main function of the ASN_GEN program plus various functions
called by construct(), print_hdr(), pre_proc(), and tabulate().
System: ASN development.
Created: 11-Jan-1995
Author: Charles W. Gardiner <gardiner@bbn.com>
Remarks:
COPYRIGHT 1995 BBN Systems and Technologies, A Division of Bolt Beranek and
Newman Inc.
150 CambridgePark Drive
Cambridge, Ma. 02140
617-873-4000
*****************************************************************************/
const char asn_gen_version[]="Version 2.0a";
const char asn_gen_rcsid[]="$Header: /nfs/sub-rosa/u2/IOS_Project/ASN/Dev/rcs/cmd/asn_gen/asn_gen.c,v 1.22 1995/10/05 15:17:18 jlowry Exp $";
char asn_gen_id[] = "@(#)asn_gen.c 282P";
#include "includes.h"
#include "asn_obj.h"
#include "asn_gen.h"
static void print_if_include(FILE *, char *);
int array, classcount, flags, min, max, option,
state = GLOBAL,
explicit = 3;
long tag, type, subtype, tablepos, real_start, curr_pos;
FILE *outstr;
char fname[80],
token[BSIZE], itemname[BSIZE], classname[BSIZE], prevname[BSIZE], path[16],
subclass[BSIZE], defaultname[32], numstring[BSIZE], defined_by[BSIZE],
definer[BSIZE], tablename[32], inclass[BSIZE], *source,
algid_w[] = "AlgorithmIdentifier",
any_w[] = "ANY",
application_w[] = "APPLICATION",
array_w[] = "array",
begin_w[] = "BEGIN",
choice_w[] = "CHOICE",
components_w[] = "COMPONENTS",
default_w[] = "DEFAULT",
defined_w[] = "DEFINED",
definitions_w[] = "DEFINITIONS",
empty_w[] = "EMPTY",
enumerated_w[] = "ENUMERATED",
equals[] = "::=",
explicit_w[] = "EXPLICIT",
from_w[] = "FROM",
function_w[] = "FUNCTION",
if_include[] = "#ifndef _%s\n#include \"%s\"\n#endif\n",
implicit_w[] = "IMPLICIT",
imports_w[] = "IMPORTS",
in_w[] = "IN",
none_w[] = "NONE",
notasn1_w[] = "NOTASN1",
null_w[] = "NULL",
of_w[] = "OF",
optional_w[] = "OPTIONAL",
private_w[] = "PRIVATE",
size_w[] = "SIZE",
table_w[] = "TABLE",
tags_w[] = "TAGS",
*reserved_words[] = { "", any_w, application_w, choice_w, components_w,
default_w, defined_w, definitions_w, empty_w, explicit_w, implicit_w,
imports_w, of_w, optional_w, private_w, size_w, table_w, tags_w, 0},
*msgs[] =
{
"Asn_gen finished %s OK\n",
"Invalid parameter: %s\n", /* 1 */
"Can't open %s\n", /* 2 */
"Nesting in %s not allowed\n", /* 3 */
"invalid state %d\n", /* 4 */
"no child of %s in table\n", /* 5 */
"invalid word: %s\n", /* 6 */
"memory error\n", /* 7 */
"tag for %s already defined\n", /* 8 */
"overflow in area %s\n", /* 9 */
"ambiguity in DEFINED BY %s\n", /* 10 */
"can't find definer/defined path for %s\n", /* 11 */
"syntax error at %s\n", /* 12 */
"nesting detected\n", /* 13 */
"unexpected EOF at %s\n", /* 14 */
"extra tag definition 0x%lX\n", /* 15 */
"undefined upper bound %s\n", /* 16 */
"duplicate definition of %s\n", /* 17 */
"ID %s is not defined\n", /* 18 */
"no table defined for %s\n", /* 19 */
"missing %s\n", /* 20 */
"%s is not on the export list\n", /* 21 */
"stuck in loop at %s, Check syntax.\n", /* 22 */
"ambiguous tagging of %s\n", /* 23 */
"internal error in %s\n", /* 24 */
"multiple definers for %s\n", /* 25 */
},
*sfcsids[7] = {asn_constr_id, asn_gen_id, asn_hdr_id, asn_pproc_id,
asn_read_id, asn_tabulate_id, 0},
*i_names, *i_paths, *xport,
*mktemp(char *);
int i_namesize, i_pathsize, /* current sizes of i_names & i_paths */
bflag, dflag, vflag;
void clear_globals(), print_tables();
struct tag_table tag_table[] =
{
{ ASN_ANY, "ANY", "AsnAny", "ASN_ANY" },
{ ASN_BOOLEAN, "BOOLEAN", "AsnBoolean", "ASN_BOOLEAN"},
{ ASN_INTEGER, "INTEGER", "AsnInteger", "ASN_INTEGER"},
{ ASN_BITSTRING, "BIT", "AsnBitString", "ASN_BITSTRING"},
{ ASN_OCTETSTRING, "OCTET", "AsnOctetString",
"ASN_OCTETSTRING"},
{ ASN_NULL, "NULL", "AsnNull", "ASN_NULL"},
{ ASN_OBJ_ID, "OBJECT", "AsnObjectIdentifier",
"ASN_OBJ_ID"},
{ ASN_ENUMERATED, "ENUMERATED", "AsnEnumerated","ASN_ENUMERATED" },
{ ASN_NUMERIC_STRING, "NumericString", "AsnNumericString",
"ASN_NUMERIC_STRING"},
{ ASN_PRINTABLE_STRING, "PrintableString", "AsnPrintableString",
"ASN_PRINTABLE_STRING"},
{ ASN_T61_STRING, "TeletexString", "AsnTeletexString",
"ASN_T61_STRING"},
{ ASN_T61_STRING, "T61String", "AsnTeletexString",
"ASN_T61_STRING"},
{ ASN_VIDEOTEX_STRING, "VideotexString", "AsnVideotexString",
"ASN_VIDEOTEX_STRING" },
{ ASN_IA5_STRING, "IA5String", "AsnIA5String",
"ASN_IA5_STRING"},
{ ASN_UTCTIME, "UTCTime", "AsnUTCTime", "ASN_UTCTIME"},
{ ASN_GENTIME, "GeneralizedTime", "AsnGeneralizedTime", "ASN_GENTIME"},
{ ASN_SEQUENCE, "SEQUENCE", "AsnSequence", "ASN_SEQUENCE"},
{ ASN_SET, "SET", "AsnSet", "ASN_SET"},
{ ASN_CHOICE, choice_w, "AsnChoice", "ASN_CHOICE"},
{ ASN_NONE, none_w, "AsnNone", "ASN_NONE" },
{ ASN_FUNCTION, function_w, "", "ASN_FUNCTION" },
{ ASN_NOTASN1, notasn1_w, "AsnNotAsn1 ", "ASN_NOTASN1" },
{ ASN_NOTYPE, "", 0, 0},
};
struct name_area name_area =
{ "name_area", sizeof(struct name_table), 50, 4000 },
id_area = {"id_area", sizeof(struct id_table), 20, 4000 },
ub_area =
{ "ub_area", sizeof(struct ub_table), 10, 4000 } ;
struct alt_subclass *alt_subclassp;
void main(argc, argv)
int argc;
char *argv[];
{
FILE *tmpstr;
char *b, *c, **p, locbuf[80], sourcebuf[80], pprocname[80], *sfx;
int did, fd, do_flag, tflag, uflag, lflag;
time_t last, now, start, time(time_t *);
struct name_table *ntbp, *entbp;
struct parent *parentp;
struct ub_table *ubp, *eubp;
struct id_table *idp, *eidp;
if (!getenv("ASN_GEN_LICENSE"))
{
fprintf(stderr,
"\nUse of this ASN.1 compiler requires the user's acceptance of the software\n\
license that is bundled with the source distribution. Any use of this\n\
compiler for other than U. S. Government purposes is subject to the\n\
imposition of license fees. Contact asn-support@bbn.com.\n\n");
}
for(p = &argv[1], lflag = tflag = vflag = uflag = do_flag = 0, source =
(char *)0; *p; p++)
{
if (*(c = *p) == '-')
{
if (*(++c) == 'b') bflag = 1;
else if (*c == 'C') do_flag = 2;
else if (*c == 'd') dflag = 1;
else if (*c == 'h') do_flag = 1;
else if (*c == 'I' || *c == 'i' || *c == 'x')
{
b = c;
if (!*(++c)) c = *(++p);
if (*b == 'i') add_include_name(c);
else if (*b == 'x') xport = c;
else
{
did = strlen (c) + 1;
if ((!i_paths && !(i_paths = calloc(did, 1))) ||
!(i_paths = realloc(i_paths, (i_pathsize + did))))
fatal(7, (char *)0);
strcpy(&i_paths[i_pathsize], c);
i_pathsize += did;
}
}
else if (*c == 'l')
{
if (*fname) fatal(1, c);
lflag = 1;
}
else if (*c == 't') tflag = 1;
else if (*c == 'u') uflag = 1;
else if (*c == 'v') vflag = 1;
else if (*c == 'V') vflag = bflag = dflag = 1;
else if (*c == 'w')
{
puts(asn_gen_version);
for (did = 0; sfcsids[did]; puts(&sfcsids[did++][4]));
fatal(0, (char *)0);
}
else fatal(1, *p);
}
else if (!source) source = *p;
else if (lflag) fatal(1, *p);
else cat(fname, *p);
}
if (!source) fatal(2, "source file");
if (!lflag)
{
if (*fname) for (c = fname; *c; c++);
else
{
for (b = c = cat(fname, source); --c >= fname && *c != '.';);
if (c <= fname) c = b;
}
cat((sfx = c), ".C");
}
if (!do_flag) do_flag = 3;
if ((fd = open(source, O_RDONLY)) < 0) fatal(2, source);
dup2(fd, 0);
close(fd);
fprintf(stderr, "Starting %s\n", source);
start = time(&last);
tmpstr = mk_tempfile(pprocname, "pre proc");
state = PRE_GLOBAL;
pre_proc(0, tmpstr, 0);
fclose(tmpstr);
fd = open(pprocname, O_RDONLY);
dup2(fd, 0);
close(fd);
if (uflag) printf("%s\n", pprocname);
else unlink(pprocname);
time(&now);
if (tflag) printf("Pre_proc took %d secs.\n", (int)(now - last));
last = now;
tabulate(0);
clear_globals();
time(&now);
if (tflag) printf("Tabulate took %d secs.\n", (int)(now - last));
last = now;
for(did = 0, ntbp = (struct name_table *)name_area.area,
entbp = &ntbp[name_area.next]; ntbp < entbp; ntbp++)
{
if (ntbp->generation || ntbp->pos < real_start) continue;
*token = '_';
cat(&token[1], ntbp->name);
if (find_name(token) >= entbp)
{
if (!did++) printf("Defined but not used:\n");
printf(" %s\n", ntbp->name);
}
}
for(ubp = (struct ub_table *)ub_area.area, eubp = &ubp[ub_area.next];
ubp < eubp; ubp++)
{
if (!(find_name(ubp->name))->name) printf(" %s\n", ubp->name);
}
printf("\n");
for(did = 0, ntbp = (struct name_table *)name_area.area; ntbp <
&((struct name_table *)name_area.area)[name_area.next]; ntbp++)
{
if (ntbp->type != 0xFFFFFFFF || *ntbp->name > 'Z' ||
(ntbp->flags & (ASN_DEFINED_FLAG | ASN_DEFINER_FLAG | ASN_OF_FLAG |
ASN_POINTER_FLAG)) || is_ub(ntbp->name)) continue;
/* if parents are all imports, skip it */
for(parentp = &ntbp->parent; parentp; parentp = parentp->next)
{
if (((struct name_table *)name_area.area)[parentp->index].pos >=
real_start) break;
}
if (!parentp) continue;
if (!did++) printf("Undefined items were\007:\n");
printf(" %s\n", ntbp->name);
}
if (did) printf("\n");
print_tables();
if ((do_flag & 2)) /* .C file */
{
time(&last);
if (!*fname) outstr = stdout;
else
{
printf("File %s\n", fname);
if (!(outstr = fopen(fname, "w"))) fatal (2, fname);
cat(sfx, ".h");
}
print_if_include(outstr, fname);
fprintf(outstr, "\n");
do_hdr(0, 0);
clear_globals();
curr_pos = lseek(0, 0L, 0);
construct(0);
if (*fname) fclose(outstr);
clear_globals();
time(&now);
if (tflag) printf("Making .C took %d secs.\n", (int)(now - last));
last = now;
}
if ((do_flag & 1)) /* .h file */
{
if (!*fname) outstr = stdout;
else
{
if ((do_flag & 2)) fclose (outstr);
cat(sfx, ".h");
printf("File %s\n", fname);
if (!(outstr = fopen(fname, "w"))) fatal (2, fname);
for(b = strcpy(locbuf, fname); *b; b++)
{
if (*b == '.') *b = '_';
} /* #endif comes later */
fprintf(outstr, "#ifndef _%s\n#define _%s\n\n", locbuf, locbuf);
print_if_include(outstr, "asn_obj.h");
for (b = cat(sourcebuf, source); b > sourcebuf && *(--b) != '.'; );
cat(b, ".h");
for (c = i_names; c < &i_names[i_namesize]; )
{
if (strcmp(c, sourcebuf)) print_if_include(outstr, c);
while (*c++);
}
}
for(idp = (struct id_table *)id_area.area, eidp = &idp[id_area.next];
idp < eidp; idp++)
fprintf(outstr, "#define %s \"%s\"\n", idp->name, idp->val);
for(ubp = (struct ub_table *)ub_area.area, eubp = &ubp[ub_area.next];
ubp < eubp; ubp++)
fprintf(outstr, "#define %s %d\n", ubp->name, ubp->val);
if (id_area.area) fprintf(outstr, "\n");
fprintf(outstr, "#ifdef __cplusplus\n");
curr_pos = lseek(0, 0L, 0);
do_hdr(0, (ASN_EXPORT_FLAG | ASN_SUB_EXPORT_FLAG));
fprintf(outstr, "#endif /* __cplusplus */\n#endif /* %s */\n", locbuf);
fclose(outstr);
time(&now);
if (tflag) printf("Making .h took %d secs.\n", (int)(now - last));
}
if (tflag) printf("Total time %d secs.\n", (int)(now - start));
close(0);
fatal(0, source);
}
int add_child(char *name, int parent, int offset, long type, int option)
{
/**
Function: Adds 'name' to object name table with 'parent', path defined by
'offset', tag of 'type' and flags of 'option'
Procedure:
1. Add name to table
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -