2 * Copyright (C) 2006, 2007 Free Software Foundation
3 * Copyright (C) 2002 Fabio Fiorina
5 * This file is part of LIBTASN1.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /*****************************************************/
24 /* File: asn1Parser.c */
25 /* Description: program to parse a file with ASN1 */
27 /*****************************************************/
40 #include <version-etc.h>
42 static const char help_man
[] =
43 "Usage: asn1Parser [OPTION] FILE\n"
44 "Read FILE with ASN.1 definitions and generate\n"
45 "a C array that is used with libtasn1 functions.\n"
47 "Mandatory arguments to long options are mandatory for short options too.\n"
48 " -c, --check checks the syntax only\n"
49 " -o, --output FILE output file\n"
50 " -n, --name NAME array name\n"
51 " -h, --help display this help and exit\n"
52 " -v, --version output version information and exit.\n"
54 "Report bugs to <" PACKAGE_BUGREPORT
">.";
56 /********************************************************/
59 /********************************************************/
61 main(int argc
,char *argv
[])
63 static struct option long_options
[] =
65 {"help", no_argument
, 0, 'h'},
66 {"version", no_argument
, 0, 'v'},
67 {"check", no_argument
, 0, 'c'},
68 {"output", required_argument
, 0, 'o'},
69 {"name", required_argument
, 0, 'n'},
74 char *outputFileName
=NULL
;
75 char *inputFileName
=NULL
;
76 char *vectorName
=NULL
;
77 int checkSyntaxOnly
=0;
78 ASN1_TYPE pointer
=ASN1_TYPE_EMPTY
;
79 char errorDescription
[MAX_ERROR_DESCRIPTION_SIZE
];
80 int parse_result
=ASN1_SUCCESS
;
82 set_program_name (argv
[0]);
84 opterr
=0; /* disable error messages from getopt */
88 option_result
=getopt_long(argc
,argv
,"hvco:n:",long_options
,&option_index
);
90 if(option_result
== -1) break;
92 switch(option_result
){
94 printf("option %s",long_options
[option_index
].name
);
95 if(optarg
) printf(" with arg %s",optarg
);
99 printf("%s\n",help_man
);
101 if(outputFileName
) free(outputFileName
);
102 if(vectorName
) free(vectorName
);
105 case 'v': /* VERSION */
106 version_etc (stdout
, program_name
, PACKAGE
, VERSION
,
107 "Fabio Fiorina", NULL
);
108 if(outputFileName
) free(outputFileName
);
109 if(vectorName
) free(vectorName
);
112 case 'c': /* CHECK SYNTAX */
115 case 'o': /* OUTPUT */
116 outputFileName
=(char *)malloc(strlen(optarg
)+1);
117 strcpy(outputFileName
,optarg
);
119 case 'n': /* VECTOR NAME */
120 vectorName
=(char *)malloc(strlen(optarg
)+1);
121 strcpy(vectorName
,optarg
);
123 case '?': /* UNKNOW OPTION */
124 fprintf(stderr
,"asn1Parser: option '%s' not recognized or without argument.\n\n",argv
[optind
-1]);
125 printf("%s\n",help_man
);
127 if(outputFileName
) free(outputFileName
);
128 if(vectorName
) free(vectorName
);
132 fprintf(stderr
,"asn1Parser: ?? getopt returned character code Ox%x ??\n",option_result
);
138 fprintf(stderr
,"asn1Parser: input file name missing.\n\n");
139 printf("%s\n",help_man
);
141 if(outputFileName
) free(outputFileName
);
142 if(vectorName
) free(vectorName
);
146 inputFileName
=(char *)malloc(strlen(argv
[optind
])+1);
147 strcpy(inputFileName
,argv
[optind
]);
150 if(checkSyntaxOnly
== 1){
151 parse_result
=asn1_parser2tree(inputFileName
,&pointer
,errorDescription
);
152 asn1_delete_structure(&pointer
);
154 else /* C VECTOR CREATION */
155 parse_result
=asn1_parser2array(inputFileName
,
156 outputFileName
,vectorName
,errorDescription
);
158 switch(parse_result
){
162 case ASN1_FILE_NOT_FOUND
:
163 printf("asn1Parser: FILE %s NOT FOUND\n",inputFileName
);
165 case ASN1_SYNTAX_ERROR
:
166 case ASN1_IDENTIFIER_NOT_FOUND
:
167 case ASN1_NAME_TOO_LONG
:
168 printf("asn1Parser: %s\n",errorDescription
);
171 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(parse_result
));
176 if(outputFileName
) free(outputFileName
);
177 if(vectorName
) free(vectorName
);
179 if(parse_result
!= ASN1_SUCCESS
) exit(1);