Initialize Tizen 2.3
[external/libtasn1.git] / src / asn1Parser.c
1 /* asn1Parser.c -- program to parse a file with ASN1 definitions
2  * Copyright (C) 2002, 2006, 2007, 2008, 2009, 2010 Free Software
3  * Foundation, Inc.
4  *
5  * This file is part of LIBTASN1.
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 #include <config.h>
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <getopt.h>
29
30 #include <libtasn1.h>
31
32 #include <progname.h>
33 #include <version-etc.h>
34
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
38 #else
39 # define ATTR_NO_RETRUN __attribute__ ((__noreturn__))
40 #endif
41
42 ATTR_NO_RETRUN static void
43 usage (int status)
44 {
45   if (status != EXIT_SUCCESS)
46     fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
47   else
48     {
49       printf ("\
50 Usage: %s [OPTION] FILE\n", program_name);
51       printf ("\
52 Read FILE with ASN.1 definitions and generate\n\
53 a C array that is used with libtasn1 functions.\n\
54 \n");
55       printf ("\
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 ();
63     }
64   exit (status);
65 }
66
67 int
68 main (int argc, char *argv[])
69 {
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'},
76     {0, 0, 0, 0}
77   };
78   int option_index = 0;
79   int option_result;
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;
87
88   set_program_name (argv[0]);
89
90   opterr = 0;                   /* disable error messages from getopt */
91
92   while (1)
93     {
94
95       option_result =
96         getopt_long (argc, argv, "hvco:n:", long_options, &option_index);
97
98       if (option_result == -1)
99         break;
100
101       switch (option_result)
102         {
103         case 0:
104           printf ("option %s", long_options[option_index].name);
105           if (optarg)
106             printf (" with arg %s", optarg);
107           printf ("\n");
108           break;
109         case 'h':               /* HELP */
110           free (outputFileName);
111           free (vectorName);
112           usage (EXIT_SUCCESS);
113           break;
114         case 'v':               /* VERSION */
115           version_etc (stdout, program_name, PACKAGE, VERSION,
116                        "Fabio Fiorina", NULL);
117           free (outputFileName);
118           free (vectorName);
119           exit (0);
120           break;
121         case 'c':               /* CHECK SYNTAX */
122           checkSyntaxOnly = 1;
123           break;
124         case 'o':               /* OUTPUT */
125           outputFileName = (char *) malloc (strlen (optarg) + 1);
126           strcpy (outputFileName, optarg);
127           break;
128         case 'n':               /* VECTOR NAME */
129           vectorName = (char *) malloc (strlen (optarg) + 1);
130           strcpy (vectorName, optarg);
131           break;
132         case '?':               /* UNKNOW OPTION */
133           fprintf (stderr,
134                    "asn1Parser: option '%s' not recognized or without argument.\n\n",
135                    argv[optind - 1]);
136           free (outputFileName);
137           free (vectorName);
138           usage (EXIT_FAILURE);
139           break;
140         default:
141           fprintf (stderr,
142                    "asn1Parser: ?? getopt returned character code Ox%x ??\n",
143                    option_result);
144         }
145
146     }
147
148   if (optind == argc)
149     {
150       free (outputFileName);
151       free (vectorName);
152       usage (EXIT_SUCCESS);
153     }
154   else
155     {
156       inputFileName = (char *) malloc (strlen (argv[optind]) + 1);
157       strcpy (inputFileName, argv[optind]);
158     }
159
160   if (checkSyntaxOnly == 1)
161     {
162       parse_result =
163         asn1_parser2tree (inputFileName, &pointer, errorDescription);
164       asn1_delete_structure (&pointer);
165     }
166   else                          /* C VECTOR CREATION */
167     parse_result = asn1_parser2array (inputFileName,
168                                       outputFileName, vectorName,
169                                       errorDescription);
170
171   switch (parse_result)
172     {
173     case ASN1_SUCCESS:
174       printf ("Done.\n");
175       break;
176     case ASN1_FILE_NOT_FOUND:
177       printf ("asn1Parser: FILE %s NOT FOUND\n", inputFileName);
178       break;
179     case ASN1_SYNTAX_ERROR:
180     case ASN1_IDENTIFIER_NOT_FOUND:
181     case ASN1_NAME_TOO_LONG:
182       printf ("asn1Parser: %s\n", errorDescription);
183       break;
184     default:
185       printf ("libtasn1 ERROR: %s\n", asn1_strerror (parse_result));
186     }
187
188
189   free (inputFileName);
190   free (outputFileName);
191   free (vectorName);
192
193   if (parse_result != ASN1_SUCCESS)
194     exit (1);
195   exit (0);
196 }