155682Smarkm/*
2178825Sdfr * Copyright (c) 1997-2005 Kungliga Tekniska H�gskolan
355682Smarkm * (Royal Institute of Technology, Stockholm, Sweden).
455682Smarkm * All rights reserved.
555682Smarkm *
655682Smarkm * Redistribution and use in source and binary forms, with or without
755682Smarkm * modification, are permitted provided that the following conditions
855682Smarkm * are met:
955682Smarkm *
1055682Smarkm * 1. Redistributions of source code must retain the above copyright
1155682Smarkm *    notice, this list of conditions and the following disclaimer.
1255682Smarkm *
1355682Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1455682Smarkm *    notice, this list of conditions and the following disclaimer in the
1555682Smarkm *    documentation and/or other materials provided with the distribution.
1655682Smarkm *
1755682Smarkm * 3. Neither the name of the Institute nor the names of its contributors
1855682Smarkm *    may be used to endorse or promote products derived from this software
1955682Smarkm *    without specific prior written permission.
2055682Smarkm *
2155682Smarkm * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2255682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2355682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2455682Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2555682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2655682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2755682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2855682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2955682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3055682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3155682Smarkm * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#include "gen_locl.h"
3555682Smarkm#include <getarg.h>
36178825Sdfr#include "lex.h"
3755682Smarkm
38178825SdfrRCSID("$Id: main.c 20858 2007-06-03 18:56:41Z lha $");
3955682Smarkm
4055682Smarkmextern FILE *yyin;
4155682Smarkm
42178825Sdfrstatic getarg_strings preserve;
43178825Sdfrstatic getarg_strings seq;
44178825Sdfr
45178825Sdfrint
46178825Sdfrpreserve_type(const char *p)
47178825Sdfr{
48178825Sdfr    int i;
49178825Sdfr    for (i = 0; i < preserve.num_strings; i++)
50178825Sdfr	if (strcmp(preserve.strings[i], p) == 0)
51178825Sdfr	    return 1;
52178825Sdfr    return 0;
53178825Sdfr}
54178825Sdfr
55178825Sdfrint
56178825Sdfrseq_type(const char *p)
57178825Sdfr{
58178825Sdfr    int i;
59178825Sdfr    for (i = 0; i < seq.num_strings; i++)
60178825Sdfr	if (strcmp(seq.strings[i], p) == 0)
61178825Sdfr	    return 1;
62178825Sdfr    return 0;
63178825Sdfr}
64178825Sdfr
65178825Sdfrint dce_fix;
66178825Sdfrint rfc1510_bitstring;
6755682Smarkmint version_flag;
6855682Smarkmint help_flag;
6955682Smarkmstruct getargs args[] = {
70178825Sdfr    { "encode-rfc1510-bit-string", 0, arg_flag, &rfc1510_bitstring },
71178825Sdfr    { "decode-dce-ber", 0, arg_flag, &dce_fix },
72178825Sdfr    { "preserve-binary", 0, arg_strings, &preserve },
73178825Sdfr    { "sequence", 0, arg_strings, &seq },
7455682Smarkm    { "version", 0, arg_flag, &version_flag },
7555682Smarkm    { "help", 0, arg_flag, &help_flag }
7655682Smarkm};
7755682Smarkmint num_args = sizeof(args) / sizeof(args[0]);
7855682Smarkm
7955682Smarkmstatic void
8055682Smarkmusage(int code)
8155682Smarkm{
8255682Smarkm    arg_printusage(args, num_args, NULL, "[asn1-file [name]]");
8355682Smarkm    exit(code);
8455682Smarkm}
8555682Smarkm
86178825Sdfrint error_flag;
87178825Sdfr
8855682Smarkmint
8955682Smarkmmain(int argc, char **argv)
9055682Smarkm{
9155682Smarkm    int ret;
92178825Sdfr    const char *file;
93178825Sdfr    const char *name = NULL;
94178825Sdfr    int optidx = 0;
9555682Smarkm
9678527Sassar    setprogname(argv[0]);
97178825Sdfr    if(getarg(args, num_args, argc, argv, &optidx))
9855682Smarkm	usage(1);
9955682Smarkm    if(help_flag)
10055682Smarkm	usage(0);
10155682Smarkm    if(version_flag) {
10255682Smarkm	print_version(NULL);
10355682Smarkm	exit(0);
10455682Smarkm    }
105178825Sdfr    if (argc == optidx) {
10655682Smarkm	file = "stdin";
10755682Smarkm	name = "stdin";
10855682Smarkm	yyin = stdin;
10955682Smarkm    } else {
110178825Sdfr	file = argv[optidx];
11155682Smarkm	yyin = fopen (file, "r");
11255682Smarkm	if (yyin == NULL)
11355682Smarkm	    err (1, "open %s", file);
114178825Sdfr	if (argc == optidx + 1) {
115178825Sdfr	    char *p;
116178825Sdfr	    name = estrdup(file);
117178825Sdfr	    p = strrchr(name, '.');
118178825Sdfr	    if (p)
119178825Sdfr		*p = '\0';
120178825Sdfr	} else
121178825Sdfr	    name = argv[optidx + 1];
12255682Smarkm    }
12355682Smarkm
12455682Smarkm    init_generate (file, name);
12555682Smarkm    initsym ();
12655682Smarkm    ret = yyparse ();
127178825Sdfr    if(ret != 0 || error_flag != 0)
128178825Sdfr	exit(1);
12955682Smarkm    close_generate ();
130178825Sdfr    if (argc != optidx)
131178825Sdfr	fclose(yyin);
132178825Sdfr    return 0;
13355682Smarkm}
134