Fix address in license.
[libtasn1.git] / src / asn1Parser.c
blobc67b104cb68489b988ca0b1ae1931d88baecb22e
1 /*
2 * Copyright (C) 2002 Fabio Fiorina
4 * This file is part of LIBTASN1.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA
23 /*****************************************************/
24 /* File: asn1Parser.c */
25 /* Description: program to parse a file with ASN1 */
26 /* definitions. */
27 /*****************************************************/
29 #include <stdio.h>
30 #include <string.h>
31 #include <libtasn1.h>
32 #include <stdlib.h>
33 #include <config.h>
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
39 #ifdef HAVE_GETOPT_H
40 #include <getopt.h>
41 #endif
43 char version_man[] = "asn1Parser (GNU libasn1) " VERSION;
45 char help_man[] = "asn1Parser reads files with ASN1 definitions and\n"
46 "generates a C array to use with libtasn1 functions.\n"
47 "\n"
48 "Usage: asn1Parser [options] file\n"
49 "\n"
50 #ifdef HAVE_GETOPT_LONG
51 "Operation modes:\n"
52 " -h, --help shows this message and exit\n"
53 " -v, --version shows version information and exit.\n"
54 " -c, --check checks the syntax only.\n"
55 "\n"
56 "Output:\n"
57 " -o <file>, --output <file> output file\n"
58 " -n <name>, --name <name> array name\n";
59 #else
60 "Operation modes:\n"
61 " -h shows this message and exit\n"
62 " -v shows version information and exit.\n"
63 " -c checks the syntax only.\n"
64 "\n"
65 "Output:\n"
66 " -o <file> output file\n"
67 " -n <name> array name\n";
68 #endif
70 /********************************************************/
71 /* Function : main */
72 /* Description: */
73 /********************************************************/
74 int
75 main(int argc,char *argv[])
78 #ifdef HAVE_GETOPT_LONG
79 static struct option long_options[] =
81 {"help", no_argument, 0, 'h'},
82 {"version", no_argument, 0, 'v'},
83 {"check", no_argument, 0, 'c'},
84 {"output", required_argument, 0, 'o'},
85 {"name", required_argument, 0, 'n'},
86 {0, 0, 0, 0}
88 int option_index = 0;
89 #endif
91 int option_result;
92 char *outputFileName=NULL;
93 char *inputFileName=NULL;
94 char *vectorName=NULL;
95 int checkSyntaxOnly=0;
96 ASN1_TYPE pointer=ASN1_TYPE_EMPTY;
97 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
98 int parse_result=ASN1_SUCCESS;
100 opterr=0; /* disable error messages from getopt */
102 printf("\n");
104 while(1){
106 #ifdef HAVE_GETOPT_LONG
107 option_result=getopt_long(argc,argv,"hvco:n:",long_options,&option_index);
108 #else
109 option_result=getopt(argc,argv,"hvco:n:");
110 #endif
112 if(option_result == -1) break;
114 switch(option_result){
115 case 0:
116 #ifdef HAVE_GETOPT_LONG
117 printf("option %s",long_options[option_index].name);
118 if(optarg) printf(" with arg %s",optarg);
119 printf("\n");
120 #endif
121 break;
122 case 'h': /* HELP */
123 printf("%s\n",help_man);
125 if(outputFileName) free(outputFileName);
126 if(vectorName) free(vectorName);
127 exit(0);
128 break;
129 case 'v': /* VERSION */
130 printf("%s\n",version_man);
132 if(outputFileName) free(outputFileName);
133 if(vectorName) free(vectorName);
134 exit(0);
135 break;
136 case 'c': /* CHECK SYNTAX */
137 checkSyntaxOnly = 1;
138 break;
139 case 'o': /* OUTPUT */
140 outputFileName=(char *)malloc(strlen(optarg)+1);
141 strcpy(outputFileName,optarg);
142 break;
143 case 'n': /* VECTOR NAME */
144 vectorName=(char *)malloc(strlen(optarg)+1);
145 strcpy(vectorName,optarg);
146 break;
147 case '?': /* UNKNOW OPTION */
148 fprintf(stderr,"asn1Parser: option '%s' not recognized or without argument.\n\n",argv[optind-1]);
149 printf("%s\n",help_man);
151 if(outputFileName) free(outputFileName);
152 if(vectorName) free(vectorName);
153 exit(1);
154 break;
155 default:
156 fprintf(stderr,"asn1Parser: ?? getopt returned character code Ox%x ??\n",option_result);
161 if(optind == argc){
162 fprintf(stderr,"asn1Parser: input file name missing.\n\n");
163 printf("%s\n",help_man);
165 if(outputFileName) free(outputFileName);
166 if(vectorName) free(vectorName);
167 exit(1);
169 else{
170 inputFileName=(char *)malloc(strlen(argv[optind])+1);
171 strcpy(inputFileName,argv[optind]);
174 if(checkSyntaxOnly == 1){
175 parse_result=asn1_parser2tree(inputFileName,&pointer,errorDescription);
176 asn1_delete_structure(&pointer);
178 else /* C VECTOR CREATION */
179 parse_result=asn1_parser2array(inputFileName,
180 outputFileName,vectorName,errorDescription);
182 switch(parse_result){
183 case ASN1_SUCCESS:
184 printf("Done.\n");
185 break;
186 case ASN1_FILE_NOT_FOUND:
187 printf("asn1Parser: FILE %s NOT FOUND\n",inputFileName);
188 break;
189 case ASN1_SYNTAX_ERROR:
190 case ASN1_IDENTIFIER_NOT_FOUND:
191 case ASN1_NAME_TOO_LONG:
192 printf("asn1Parser: %s\n",errorDescription);
193 break;
194 default:
195 printf("libasn1 ERROR: %s\n",libtasn1_strerror(parse_result));
199 free(inputFileName);
200 if(outputFileName) free(outputFileName);
201 if(vectorName) free(vectorName);
203 if(parse_result != ASN1_SUCCESS) exit(1);
204 exit(0);