1 /* asn1Parser.c -- program to parse a file with ASN1 definitions
2 * Copyright (C) 2002, 2006, 2007, 2008, 2009, 2010 Free Software
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/>.
33 #include <version-etc.h>
35 /* This feature is available in gcc versions 2.5 and later. */
36 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
37 # define ATTR_NO_RETRUN
39 # define ATTR_NO_RETRUN __attribute__ ((__noreturn__))
42 ATTR_NO_RETRUN static void
45 if (status != EXIT_SUCCESS)
46 fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
50 Usage: %s [OPTION] FILE\n", program_name);
52 Read FILE with ASN.1 definitions and generate\n\
53 a C array that is used with libtasn1 functions.\n\
56 Mandatory arguments to long options are mandatory for short options too.\n\
57 -c, --check checks the syntax only\n\
58 -o, --output=FILE output file\n\
59 -n, --name=NAME array name\n\
60 -h, --help display this help and exit\n\
61 -v, --version output version information and exit\n");
62 emit_bug_reporting_address ();
68 main (int argc, char *argv[])
70 static const struct option long_options[] = {
71 {"help", no_argument, 0, 'h'},
72 {"version", no_argument, 0, 'v'},
73 {"check", no_argument, 0, 'c'},
74 {"output", required_argument, 0, 'o'},
75 {"name", required_argument, 0, 'n'},
80 char *outputFileName = NULL;
81 char *inputFileName = NULL;
82 char *vectorName = NULL;
83 int checkSyntaxOnly = 0;
84 ASN1_TYPE pointer = ASN1_TYPE_EMPTY;
85 char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
86 int parse_result = ASN1_SUCCESS;
88 set_program_name (argv[0]);
90 opterr = 0; /* disable error messages from getopt */
96 getopt_long (argc, argv, "hvco:n:", long_options, &option_index);
98 if (option_result == -1)
101 switch (option_result)
104 printf ("option %s", long_options[option_index].name);
106 printf (" with arg %s", optarg);
110 free (outputFileName);
112 usage (EXIT_SUCCESS);
114 case 'v': /* VERSION */
115 version_etc (stdout, program_name, PACKAGE, VERSION,
116 "Fabio Fiorina", NULL);
117 free (outputFileName);
121 case 'c': /* CHECK SYNTAX */
124 case 'o': /* OUTPUT */
125 outputFileName = (char *) malloc (strlen (optarg) + 1);
126 strcpy (outputFileName, optarg);
128 case 'n': /* VECTOR NAME */
129 vectorName = (char *) malloc (strlen (optarg) + 1);
130 strcpy (vectorName, optarg);
132 case '?': /* UNKNOW OPTION */
134 "asn1Parser: option '%s' not recognized or without argument.\n\n",
136 free (outputFileName);
138 usage (EXIT_FAILURE);
142 "asn1Parser: ?? getopt returned character code Ox%x ??\n",
150 free (outputFileName);
152 usage (EXIT_SUCCESS);
156 inputFileName = (char *) malloc (strlen (argv[optind]) + 1);
157 strcpy (inputFileName, argv[optind]);
160 if (checkSyntaxOnly == 1)
163 asn1_parser2tree (inputFileName, &pointer, errorDescription);
164 asn1_delete_structure (&pointer);
166 else /* C VECTOR CREATION */
167 parse_result = asn1_parser2array (inputFileName,
168 outputFileName, vectorName,
171 switch (parse_result)
176 case ASN1_FILE_NOT_FOUND:
177 printf ("asn1Parser: FILE %s NOT FOUND\n", inputFileName);
179 case ASN1_SYNTAX_ERROR:
180 case ASN1_IDENTIFIER_NOT_FOUND:
181 case ASN1_NAME_TOO_LONG:
182 printf ("asn1Parser: %s\n", errorDescription);
185 printf ("libtasn1 ERROR: %s\n", asn1_strerror (parse_result));
189 free (inputFileName);
190 free (outputFileName);
193 if (parse_result != ASN1_SUCCESS)