Fix release.
[libtasn1.git] / src / asn1Parser.c
blobce8d609d5cc4d38ae6539e8fc8e4cd06ee66993e
1 /*
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
20 * 02110-1301, USA
24 /*****************************************************/
25 /* File: asn1Parser.c */
26 /* Description: program to parse a file with ASN1 */
27 /* definitions. */
28 /*****************************************************/
30 #include <config.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <getopt.h>
38 #include <libtasn1.h>
40 #include <progname.h>
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"
47 "\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"
54 "\n"
55 "Report bugs to <" PACKAGE_BUGREPORT ">.";
57 /********************************************************/
58 /* Function : main */
59 /* Description: */
60 /********************************************************/
61 int
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'},
71 {0, 0, 0, 0}
73 int option_index = 0;
74 int option_result;
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 */
87 while(1){
89 option_result=getopt_long(argc,argv,"hvco:n:",long_options,&option_index);
91 if(option_result == -1) break;
93 switch(option_result){
94 case 0:
95 printf("option %s",long_options[option_index].name);
96 if(optarg) printf(" with arg %s",optarg);
97 printf("\n");
98 break;
99 case 'h': /* HELP */
100 printf("%s\n",help_man);
102 if(outputFileName) free(outputFileName);
103 if(vectorName) free(vectorName);
104 exit(0);
105 break;
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);
111 exit(0);
112 break;
113 case 'c': /* CHECK SYNTAX */
114 checkSyntaxOnly = 1;
115 break;
116 case 'o': /* OUTPUT */
117 outputFileName=(char *)malloc(strlen(optarg)+1);
118 strcpy(outputFileName,optarg);
119 break;
120 case 'n': /* VECTOR NAME */
121 vectorName=(char *)malloc(strlen(optarg)+1);
122 strcpy(vectorName,optarg);
123 break;
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);
130 exit(1);
131 break;
132 default:
133 fprintf(stderr,"asn1Parser: ?? getopt returned character code Ox%x ??\n",option_result);
138 if(optind == argc){
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);
144 exit(1);
146 else{
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){
160 case ASN1_SUCCESS:
161 printf("Done.\n");
162 break;
163 case ASN1_FILE_NOT_FOUND:
164 printf("asn1Parser: FILE %s NOT FOUND\n",inputFileName);
165 break;
166 case ASN1_SYNTAX_ERROR:
167 case ASN1_IDENTIFIER_NOT_FOUND:
168 case ASN1_NAME_TOO_LONG:
169 printf("asn1Parser: %s\n",errorDescription);
170 break;
171 default:
172 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(parse_result));
176 free(inputFileName);
177 if(outputFileName) free(outputFileName);
178 if(vectorName) free(vectorName);
180 if(parse_result != ASN1_SUCCESS) exit(1);
181 exit(0);