Add GTK-DOC.
[libtasn1.git] / src / asn1Parser.c
blob009ccff5ff608968351b49ba1223c92309264314
1 /*
2 * Copyright (C) 2002 Fabio Fiorina
4 * This file is part of LIBASN1.
6 * LIBASN1 is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * LIBASN1 is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 /*****************************************************/
23 /* File: asn1Parser.c */
24 /* Description: program to parse a file with ASN1 */
25 /* definitions. */
26 /*****************************************************/
28 #include <stdio.h>
29 #include <string.h>
30 #include <libtasn1.h>
31 #include <stdlib.h>
32 #include <config.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
38 #ifdef HAVE_GETOPT_H
39 #include <getopt.h>
40 #endif
42 char version_man[] = "asn1Parser (GNU libasn1) " VERSION;
44 char help_man[] = "asn1Parser reads files with ASN1 definitions and\n"
45 "generates a C array to use with libtasn1 functions.\n"
46 "\n"
47 "Usage: asn1Parser [options] file\n"
48 "\n"
49 #ifdef HAVE_GETOPT_LONG
50 "Operation modes:\n"
51 " -h, --help shows this message and exit\n"
52 " -v, --version shows version information and exit.\n"
53 " -c, --check checks the syntax only.\n"
54 "\n"
55 "Output:\n"
56 " -o <file>, --output <file> output file\n"
57 " -n <name>, --name <name> array name\n";
58 #else
59 "Operation modes:\n"
60 " -h shows this message and exit\n"
61 " -v shows version information and exit.\n"
62 " -c checks the syntax only.\n"
63 "\n"
64 "Output:\n"
65 " -o <file> output file\n"
66 " -n <name> array name\n";
67 #endif
69 /********************************************************/
70 /* Function : main */
71 /* Description: */
72 /********************************************************/
73 int
74 main(int argc,char *argv[])
77 #ifdef HAVE_GETOPT_LONG
78 static struct option long_options[] =
80 {"help", no_argument, 0, 'h'},
81 {"version", no_argument, 0, 'v'},
82 {"check", no_argument, 0, 'c'},
83 {"output", required_argument, 0, 'o'},
84 {"name", required_argument, 0, 'n'},
85 {0, 0, 0, 0}
87 int option_index = 0;
88 #endif
90 int option_result;
91 char *outputFileName=NULL;
92 char *inputFileName=NULL;
93 char *vectorName=NULL;
94 int checkSyntaxOnly=0;
95 ASN1_TYPE pointer=ASN1_TYPE_EMPTY;
96 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
97 int parse_result=ASN1_SUCCESS;
99 opterr=0; /* disable error messages from getopt */
101 printf("\n");
103 while(1){
105 #ifdef HAVE_GETOPT_LONG
106 option_result=getopt_long(argc,argv,"hvco:n:",long_options,&option_index);
107 #else
108 option_result=getopt(argc,argv,"hvco:n:");
109 #endif
111 if(option_result == -1) break;
113 switch(option_result){
114 case 0:
115 #ifdef HAVE_GETOPT_LONG
116 printf("option %s",long_options[option_index].name);
117 if(optarg) printf(" with arg %s",optarg);
118 printf("\n");
119 #endif
120 break;
121 case 'h': /* HELP */
122 printf("%s\n",help_man);
124 if(outputFileName) free(outputFileName);
125 if(vectorName) free(vectorName);
126 exit(0);
127 break;
128 case 'v': /* VERSION */
129 printf("%s\n",version_man);
131 if(outputFileName) free(outputFileName);
132 if(vectorName) free(vectorName);
133 exit(0);
134 break;
135 case 'c': /* CHECK SYNTAX */
136 checkSyntaxOnly = 1;
137 break;
138 case 'o': /* OUTPUT */
139 outputFileName=(char *)malloc(strlen(optarg)+1);
140 strcpy(outputFileName,optarg);
141 break;
142 case 'n': /* VECTOR NAME */
143 vectorName=(char *)malloc(strlen(optarg)+1);
144 strcpy(vectorName,optarg);
145 break;
146 case '?': /* UNKNOW OPTION */
147 fprintf(stderr,"asn1Parser: option '%s' not recognized or without argument.\n\n",argv[optind-1]);
148 printf("%s\n",help_man);
150 if(outputFileName) free(outputFileName);
151 if(vectorName) free(vectorName);
152 exit(1);
153 break;
154 default:
155 fprintf(stderr,"asn1Parser: ?? getopt returned character code Ox%x ??\n",option_result);
160 if(optind == argc){
161 fprintf(stderr,"asn1Parser: input file name missing.\n\n");
162 printf("%s\n",help_man);
164 if(outputFileName) free(outputFileName);
165 if(vectorName) free(vectorName);
166 exit(1);
168 else{
169 inputFileName=(char *)malloc(strlen(argv[optind])+1);
170 strcpy(inputFileName,argv[optind]);
173 if(checkSyntaxOnly == 1){
174 parse_result=asn1_parser2tree(inputFileName,&pointer,errorDescription);
175 asn1_delete_structure(&pointer);
177 else /* C VECTOR CREATION */
178 parse_result=asn1_parser2array(inputFileName,
179 outputFileName,vectorName,errorDescription);
181 switch(parse_result){
182 case ASN1_SUCCESS:
183 printf("Done.\n");
184 break;
185 case ASN1_FILE_NOT_FOUND:
186 printf("asn1Parser: FILE %s NOT FOUND\n",inputFileName);
187 break;
188 case ASN1_SYNTAX_ERROR:
189 case ASN1_IDENTIFIER_NOT_FOUND:
190 case ASN1_NAME_TOO_LONG:
191 printf("asn1Parser: %s\n",errorDescription);
192 break;
193 default:
194 printf("libasn1 ERROR: %s\n",libtasn1_strerror(parse_result));
198 free(inputFileName);
199 if(outputFileName) free(outputFileName);
200 if(vectorName) free(vectorName);
202 if(parse_result != ASN1_SUCCESS) exit(1);
203 exit(0);