Add.
[libtasn1.git] / src / asn1Parser.c
blobb968f22cafcc041ea18341806e2da12e0457ba9f
1 /*
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 */
26 /* definitions. */
27 /*****************************************************/
29 #include <config.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <getopt.h>
37 #include <libtasn1.h>
39 #include <progname.h>
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"
46 "\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"
53 "\n"
54 "Report bugs to <" PACKAGE_BUGREPORT ">.";
56 /********************************************************/
57 /* Function : main */
58 /* Description: */
59 /********************************************************/
60 int
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'},
70 {0, 0, 0, 0}
72 int option_index = 0;
73 int option_result;
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 */
86 while(1){
88 option_result=getopt_long(argc,argv,"hvco:n:",long_options,&option_index);
90 if(option_result == -1) break;
92 switch(option_result){
93 case 0:
94 printf("option %s",long_options[option_index].name);
95 if(optarg) printf(" with arg %s",optarg);
96 printf("\n");
97 break;
98 case 'h': /* HELP */
99 printf("%s\n",help_man);
101 if(outputFileName) free(outputFileName);
102 if(vectorName) free(vectorName);
103 exit(0);
104 break;
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);
110 exit(0);
111 break;
112 case 'c': /* CHECK SYNTAX */
113 checkSyntaxOnly = 1;
114 break;
115 case 'o': /* OUTPUT */
116 outputFileName=(char *)malloc(strlen(optarg)+1);
117 strcpy(outputFileName,optarg);
118 break;
119 case 'n': /* VECTOR NAME */
120 vectorName=(char *)malloc(strlen(optarg)+1);
121 strcpy(vectorName,optarg);
122 break;
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);
129 exit(1);
130 break;
131 default:
132 fprintf(stderr,"asn1Parser: ?? getopt returned character code Ox%x ??\n",option_result);
137 if(optind == argc){
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);
143 exit(1);
145 else{
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){
159 case ASN1_SUCCESS:
160 printf("Done.\n");
161 break;
162 case ASN1_FILE_NOT_FOUND:
163 printf("asn1Parser: FILE %s NOT FOUND\n",inputFileName);
164 break;
165 case ASN1_SYNTAX_ERROR:
166 case ASN1_IDENTIFIER_NOT_FOUND:
167 case ASN1_NAME_TOO_LONG:
168 printf("asn1Parser: %s\n",errorDescription);
169 break;
170 default:
171 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(parse_result));
175 free(inputFileName);
176 if(outputFileName) free(outputFileName);
177 if(vectorName) free(vectorName);
179 if(parse_result != ASN1_SUCCESS) exit(1);
180 exit(0);