2 * Copyright (C) 2006 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
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * 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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 /*****************************************************/
25 /* File: asn1Parser.c */
26 /* Description: program to parse a file with ASN1 */
28 /*****************************************************/
41 #include <version-etc.h>
43 static const char help_man
[] =
44 "Usage: asn1Parser [OPTION] FILE\n"
45 "Read FILE with ASN.1 definitions and generate\n"
46 "a C array that is used with libtasn1 functions.\n"
48 "Mandatory arguments to long options are mandatory for short options too.\n"
49 " -c, --check checks the syntax only\n"
50 " -o, --output FILE output file\n"
51 " -n, --name NAME array name\n"
52 " -h, --help display this help and exit\n"
53 " -v, --version output version information and exit.\n"
55 "Report bugs to <" PACKAGE_BUGREPORT
">.";
57 /********************************************************/
60 /********************************************************/
62 main(int argc
,char *argv
[])
64 static struct option long_options
[] =
66 {"help", no_argument
, 0, 'h'},
67 {"version", no_argument
, 0, 'v'},
68 {"check", no_argument
, 0, 'c'},
69 {"output", required_argument
, 0, 'o'},
70 {"name", required_argument
, 0, 'n'},
75 char *outputFileName
=NULL
;
76 char *inputFileName
=NULL
;
77 char *vectorName
=NULL
;
78 int checkSyntaxOnly
=0;
79 ASN1_TYPE pointer
=ASN1_TYPE_EMPTY
;
80 char errorDescription
[MAX_ERROR_DESCRIPTION_SIZE
];
81 int parse_result
=ASN1_SUCCESS
;
83 set_program_name (argv
[0]);
85 opterr
=0; /* disable error messages from getopt */
89 option_result
=getopt_long(argc
,argv
,"hvco:n:",long_options
,&option_index
);
91 if(option_result
== -1) break;
93 switch(option_result
){
95 printf("option %s",long_options
[option_index
].name
);
96 if(optarg
) printf(" with arg %s",optarg
);
100 printf("%s\n",help_man
);
102 if(outputFileName
) free(outputFileName
);
103 if(vectorName
) free(vectorName
);
106 case 'v': /* VERSION */
107 version_etc (stdout
, program_name
, PACKAGE
, VERSION
,
108 "Fabio Fiorina", NULL
);
109 if(outputFileName
) free(outputFileName
);
110 if(vectorName
) free(vectorName
);
113 case 'c': /* CHECK SYNTAX */
116 case 'o': /* OUTPUT */
117 outputFileName
=(char *)malloc(strlen(optarg
)+1);
118 strcpy(outputFileName
,optarg
);
120 case 'n': /* VECTOR NAME */
121 vectorName
=(char *)malloc(strlen(optarg
)+1);
122 strcpy(vectorName
,optarg
);
124 case '?': /* UNKNOW OPTION */
125 fprintf(stderr
,"asn1Parser: option '%s' not recognized or without argument.\n\n",argv
[optind
-1]);
126 printf("%s\n",help_man
);
128 if(outputFileName
) free(outputFileName
);
129 if(vectorName
) free(vectorName
);
133 fprintf(stderr
,"asn1Parser: ?? getopt returned character code Ox%x ??\n",option_result
);
139 fprintf(stderr
,"asn1Parser: input file name missing.\n\n");
140 printf("%s\n",help_man
);
142 if(outputFileName
) free(outputFileName
);
143 if(vectorName
) free(vectorName
);
147 inputFileName
=(char *)malloc(strlen(argv
[optind
])+1);
148 strcpy(inputFileName
,argv
[optind
]);
151 if(checkSyntaxOnly
== 1){
152 parse_result
=asn1_parser2tree(inputFileName
,&pointer
,errorDescription
);
153 asn1_delete_structure(&pointer
);
155 else /* C VECTOR CREATION */
156 parse_result
=asn1_parser2array(inputFileName
,
157 outputFileName
,vectorName
,errorDescription
);
159 switch(parse_result
){
163 case ASN1_FILE_NOT_FOUND
:
164 printf("asn1Parser: FILE %s NOT FOUND\n",inputFileName
);
166 case ASN1_SYNTAX_ERROR
:
167 case ASN1_IDENTIFIER_NOT_FOUND
:
168 case ASN1_NAME_TOO_LONG
:
169 printf("asn1Parser: %s\n",errorDescription
);
172 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(parse_result
));
177 if(outputFileName
) free(outputFileName
);
178 if(vectorName
) free(vectorName
);
180 if(parse_result
!= ASN1_SUCCESS
) exit(1);