2 * Copyright (C) 2002 Fabio Fiorina
4 * This file is part of LIBTASN1.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 /*****************************************************/
24 /* File: asn1Parser.c */
25 /* Description: program to parse a file with ASN1 */
27 /*****************************************************/
43 char version_man
[] = "asn1Parser (GNU libasn1) " VERSION
;
45 char help_man
[] = "asn1Parser reads files with ASN1 definitions and\n"
46 "generates a C array to use with libtasn1 functions.\n"
48 "Usage: asn1Parser [options] file\n"
50 #ifdef HAVE_GETOPT_LONG
52 " -h, --help shows this message and exit\n"
53 " -v, --version shows version information and exit.\n"
54 " -c, --check checks the syntax only.\n"
57 " -o <file>, --output <file> output file\n"
58 " -n <name>, --name <name> array name\n";
61 " -h shows this message and exit\n"
62 " -v shows version information and exit.\n"
63 " -c checks the syntax only.\n"
66 " -o <file> output file\n"
67 " -n <name> array name\n";
70 /********************************************************/
73 /********************************************************/
75 main(int argc
,char *argv
[])
78 #ifdef HAVE_GETOPT_LONG
79 static struct option long_options
[] =
81 {"help", no_argument
, 0, 'h'},
82 {"version", no_argument
, 0, 'v'},
83 {"check", no_argument
, 0, 'c'},
84 {"output", required_argument
, 0, 'o'},
85 {"name", required_argument
, 0, 'n'},
92 char *outputFileName
=NULL
;
93 char *inputFileName
=NULL
;
94 char *vectorName
=NULL
;
95 int checkSyntaxOnly
=0;
96 ASN1_TYPE pointer
=ASN1_TYPE_EMPTY
;
97 char errorDescription
[MAX_ERROR_DESCRIPTION_SIZE
];
98 int parse_result
=ASN1_SUCCESS
;
100 opterr
=0; /* disable error messages from getopt */
106 #ifdef HAVE_GETOPT_LONG
107 option_result
=getopt_long(argc
,argv
,"hvco:n:",long_options
,&option_index
);
109 option_result
=getopt(argc
,argv
,"hvco:n:");
112 if(option_result
== -1) break;
114 switch(option_result
){
116 #ifdef HAVE_GETOPT_LONG
117 printf("option %s",long_options
[option_index
].name
);
118 if(optarg
) printf(" with arg %s",optarg
);
123 printf("%s\n",help_man
);
125 if(outputFileName
) free(outputFileName
);
126 if(vectorName
) free(vectorName
);
129 case 'v': /* VERSION */
130 printf("%s\n",version_man
);
132 if(outputFileName
) free(outputFileName
);
133 if(vectorName
) free(vectorName
);
136 case 'c': /* CHECK SYNTAX */
139 case 'o': /* OUTPUT */
140 outputFileName
=(char *)malloc(strlen(optarg
)+1);
141 strcpy(outputFileName
,optarg
);
143 case 'n': /* VECTOR NAME */
144 vectorName
=(char *)malloc(strlen(optarg
)+1);
145 strcpy(vectorName
,optarg
);
147 case '?': /* UNKNOW OPTION */
148 fprintf(stderr
,"asn1Parser: option '%s' not recognized or without argument.\n\n",argv
[optind
-1]);
149 printf("%s\n",help_man
);
151 if(outputFileName
) free(outputFileName
);
152 if(vectorName
) free(vectorName
);
156 fprintf(stderr
,"asn1Parser: ?? getopt returned character code Ox%x ??\n",option_result
);
162 fprintf(stderr
,"asn1Parser: input file name missing.\n\n");
163 printf("%s\n",help_man
);
165 if(outputFileName
) free(outputFileName
);
166 if(vectorName
) free(vectorName
);
170 inputFileName
=(char *)malloc(strlen(argv
[optind
])+1);
171 strcpy(inputFileName
,argv
[optind
]);
174 if(checkSyntaxOnly
== 1){
175 parse_result
=asn1_parser2tree(inputFileName
,&pointer
,errorDescription
);
176 asn1_delete_structure(&pointer
);
178 else /* C VECTOR CREATION */
179 parse_result
=asn1_parser2array(inputFileName
,
180 outputFileName
,vectorName
,errorDescription
);
182 switch(parse_result
){
186 case ASN1_FILE_NOT_FOUND
:
187 printf("asn1Parser: FILE %s NOT FOUND\n",inputFileName
);
189 case ASN1_SYNTAX_ERROR
:
190 case ASN1_IDENTIFIER_NOT_FOUND
:
191 case ASN1_NAME_TOO_LONG
:
192 printf("asn1Parser: %s\n",errorDescription
);
195 printf("libasn1 ERROR: %s\n",libtasn1_strerror(parse_result
));
200 if(outputFileName
) free(outputFileName
);
201 if(vectorName
) free(vectorName
);
203 if(parse_result
!= ASN1_SUCCESS
) exit(1);