Add.
[libtasn1.git] / src / asn1Parser.c
blob139cdbe5076a1eff14d0d9aa649cc35e503d58e6
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 <stdio.h>
31 #include <string.h>
32 #include <libtasn1.h>
33 #include <stdlib.h>
34 #include <config.h>
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
40 #ifdef HAVE_GETOPT_H
41 #include <getopt.h>
42 #endif
44 char version_man[] = "asn1Parser (GNU libtasn1) " VERSION;
46 char help_man[] = "asn1Parser reads files with ASN1 definitions and\n"
47 "generates a C array to use with libtasn1 functions.\n"
48 "\n"
49 "Usage: asn1Parser [options] file\n"
50 "\n"
51 #ifdef HAVE_GETOPT_LONG
52 "Operation modes:\n"
53 " -h, --help shows this message and exit\n"
54 " -v, --version shows version information and exit.\n"
55 " -c, --check checks the syntax only.\n"
56 "\n"
57 "Output:\n"
58 " -o <file>, --output <file> output file\n"
59 " -n <name>, --name <name> array name\n";
60 #else
61 "Operation modes:\n"
62 " -h shows this message and exit\n"
63 " -v shows version information and exit.\n"
64 " -c checks the syntax only.\n"
65 "\n"
66 "Output:\n"
67 " -o <file> output file\n"
68 " -n <name> array name\n";
69 #endif
71 /********************************************************/
72 /* Function : main */
73 /* Description: */
74 /********************************************************/
75 int
76 main(int argc,char *argv[])
79 #ifdef HAVE_GETOPT_LONG
80 static struct option long_options[] =
82 {"help", no_argument, 0, 'h'},
83 {"version", no_argument, 0, 'v'},
84 {"check", no_argument, 0, 'c'},
85 {"output", required_argument, 0, 'o'},
86 {"name", required_argument, 0, 'n'},
87 {0, 0, 0, 0}
89 int option_index = 0;
90 #endif
92 int option_result;
93 char *outputFileName=NULL;
94 char *inputFileName=NULL;
95 char *vectorName=NULL;
96 int checkSyntaxOnly=0;
97 ASN1_TYPE pointer=ASN1_TYPE_EMPTY;
98 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
99 int parse_result=ASN1_SUCCESS;
101 opterr=0; /* disable error messages from getopt */
103 printf("\n");
105 while(1){
107 #ifdef HAVE_GETOPT_LONG
108 option_result=getopt_long(argc,argv,"hvco:n:",long_options,&option_index);
109 #else
110 option_result=getopt(argc,argv,"hvco:n:");
111 #endif
113 if(option_result == -1) break;
115 switch(option_result){
116 case 0:
117 #ifdef HAVE_GETOPT_LONG
118 printf("option %s",long_options[option_index].name);
119 if(optarg) printf(" with arg %s",optarg);
120 printf("\n");
121 #endif
122 break;
123 case 'h': /* HELP */
124 printf("%s\n",help_man);
126 if(outputFileName) free(outputFileName);
127 if(vectorName) free(vectorName);
128 exit(0);
129 break;
130 case 'v': /* VERSION */
131 printf("%s\n",version_man);
133 if(outputFileName) free(outputFileName);
134 if(vectorName) free(vectorName);
135 exit(0);
136 break;
137 case 'c': /* CHECK SYNTAX */
138 checkSyntaxOnly = 1;
139 break;
140 case 'o': /* OUTPUT */
141 outputFileName=(char *)malloc(strlen(optarg)+1);
142 strcpy(outputFileName,optarg);
143 break;
144 case 'n': /* VECTOR NAME */
145 vectorName=(char *)malloc(strlen(optarg)+1);
146 strcpy(vectorName,optarg);
147 break;
148 case '?': /* UNKNOW OPTION */
149 fprintf(stderr,"asn1Parser: option '%s' not recognized or without argument.\n\n",argv[optind-1]);
150 printf("%s\n",help_man);
152 if(outputFileName) free(outputFileName);
153 if(vectorName) free(vectorName);
154 exit(1);
155 break;
156 default:
157 fprintf(stderr,"asn1Parser: ?? getopt returned character code Ox%x ??\n",option_result);
162 if(optind == argc){
163 fprintf(stderr,"asn1Parser: input file name missing.\n\n");
164 printf("%s\n",help_man);
166 if(outputFileName) free(outputFileName);
167 if(vectorName) free(vectorName);
168 exit(1);
170 else{
171 inputFileName=(char *)malloc(strlen(argv[optind])+1);
172 strcpy(inputFileName,argv[optind]);
175 if(checkSyntaxOnly == 1){
176 parse_result=asn1_parser2tree(inputFileName,&pointer,errorDescription);
177 asn1_delete_structure(&pointer);
179 else /* C VECTOR CREATION */
180 parse_result=asn1_parser2array(inputFileName,
181 outputFileName,vectorName,errorDescription);
183 switch(parse_result){
184 case ASN1_SUCCESS:
185 printf("Done.\n");
186 break;
187 case ASN1_FILE_NOT_FOUND:
188 printf("asn1Parser: FILE %s NOT FOUND\n",inputFileName);
189 break;
190 case ASN1_SYNTAX_ERROR:
191 case ASN1_IDENTIFIER_NOT_FOUND:
192 case ASN1_NAME_TOO_LONG:
193 printf("asn1Parser: %s\n",errorDescription);
194 break;
195 default:
196 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(parse_result));
200 free(inputFileName);
201 if(outputFileName) free(outputFileName);
202 if(vectorName) free(vectorName);
204 if(parse_result != ASN1_SUCCESS) exit(1);
205 exit(0);