Sourcecode and documentation for libtasn1-2 version 0.2.10-3sarge1
show bar | Show file versions
Search packages:
| Sourcecode archive home

asn1Parser.c

00001 /*
00002  *      Copyright (C) 2002 Fabio Fiorina
00003  *
00004  * This file is part of LIBASN1.
00005  *
00006  * LIBASN1 is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * LIBASN1 is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00019  */
00020 
00021 
00022 /*****************************************************/
00023 /* File: asn1Parser.c                                */
00024 /* Description: program to parse a file with ASN1    */
00025 /*              definitions.                         */   
00026 /*****************************************************/
00027 
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <libtasn1.h>
00031 #include <stdlib.h>
00032 #include <config.h>
00033 
00034 #ifdef HAVE_UNISTD_H
00035 #include <unistd.h>
00036 #endif
00037 
00038 #ifdef HAVE_GETOPT_H
00039   #include <getopt.h>
00040 #endif
00041 
00042 char version_man[] = "asn1Parser (GNU libasn1) " VERSION;
00043 
00044 char help_man[] = "asn1Parser reads files with ASN1 definitions and\n"
00045                   "generates a C array to use with libtasn1 functions.\n"
00046                   "\n"
00047                   "Usage: asn1Parser [options] file\n"
00048                   "\n"
00049 #ifdef HAVE_GETOPT_LONG
00050                   "Operation modes:\n"
00051                   "  -h, --help    shows this message and exit\n"
00052                   "  -v, --version shows version information and exit.\n"
00053                   "  -c, --check   checks the syntax only.\n"
00054                   "\n"
00055                   "Output:\n"
00056                   "  -o <file>, --output <file>  output file\n"
00057                   "  -n <name>, --name <name>    array name\n";
00058 #else
00059                   "Operation modes:\n"
00060                   "  -h    shows this message and exit\n"
00061                   "  -v    shows version information and exit.\n"
00062                   "  -c    checks the syntax only.\n"
00063                   "\n"
00064                   "Output:\n"
00065                   "  -o <file>  output file\n"
00066                   "  -n <name>  array name\n";
00067 #endif
00068 
00069 /********************************************************/
00070 /* Function : main                                      */
00071 /* Description:                                         */
00072 /********************************************************/
00073 int
00074 main(int argc,char *argv[])
00075 {
00076 
00077 #ifdef HAVE_GETOPT_LONG
00078   static struct option long_options[] =
00079   {
00080     {"help",    no_argument,       0, 'h'},
00081     {"version", no_argument,       0, 'v'},
00082     {"check",   no_argument,       0, 'c'},
00083     {"output",  required_argument, 0, 'o'},
00084     {"name",    required_argument, 0, 'n'},
00085     {0, 0, 0, 0}
00086   };
00087   int option_index = 0;
00088 #endif
00089 
00090  int option_result;
00091  char *outputFileName=NULL;
00092  char *inputFileName=NULL;
00093  char *vectorName=NULL;
00094  int checkSyntaxOnly=0;
00095  ASN1_TYPE pointer=ASN1_TYPE_EMPTY;
00096  char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
00097  int parse_result=ASN1_SUCCESS;
00098 
00099  opterr=0; /* disable error messages from getopt */
00100 
00101  printf("\n");
00102 
00103  while(1){
00104 
00105 #ifdef HAVE_GETOPT_LONG
00106    option_result=getopt_long(argc,argv,"hvco:n:",long_options,&option_index);
00107 #else
00108    option_result=getopt(argc,argv,"hvco:n:");
00109 #endif
00110 
00111    if(option_result == -1) break;
00112 
00113    switch(option_result){
00114    case 0:
00115 #ifdef HAVE_GETOPT_LONG
00116      printf("option %s",long_options[option_index].name);
00117      if(optarg) printf(" with arg %s",optarg);
00118      printf("\n");
00119 #endif
00120      break;
00121    case 'h':  /* HELP */
00122      printf("%s\n",help_man);
00123 
00124      if(outputFileName) free(outputFileName);
00125      if(vectorName) free(vectorName);
00126      exit(0);
00127      break;
00128    case 'v':  /* VERSION */
00129      printf("%s\n",version_man);
00130 
00131      if(outputFileName) free(outputFileName);
00132      if(vectorName) free(vectorName);
00133      exit(0);
00134      break;
00135    case 'c':  /* CHECK SYNTAX */
00136      checkSyntaxOnly = 1;
00137      break;
00138    case 'o':  /* OUTPUT */
00139      outputFileName=(char *)malloc(strlen(optarg)+1);
00140      strcpy(outputFileName,optarg);
00141      break;
00142    case 'n':  /* VECTOR NAME */
00143      vectorName=(char *)malloc(strlen(optarg)+1);
00144      strcpy(vectorName,optarg);
00145      break;
00146    case '?':  /* UNKNOW OPTION */
00147      fprintf(stderr,"asn1Parser: option '%s' not recognized or without argument.\n\n",argv[optind-1]);
00148      printf("%s\n",help_man);
00149 
00150      if(outputFileName) free(outputFileName);
00151      if(vectorName) free(vectorName);
00152      exit(1);
00153      break;
00154    default:
00155      fprintf(stderr,"asn1Parser: ?? getopt returned character code Ox%x ??\n",option_result);
00156    }
00157 
00158  }
00159 
00160  if(optind == argc){
00161    fprintf(stderr,"asn1Parser: input file name missing.\n\n");
00162    printf("%s\n",help_man);
00163 
00164    if(outputFileName) free(outputFileName);
00165    if(vectorName) free(vectorName);
00166    exit(1);
00167  }
00168  else{
00169    inputFileName=(char *)malloc(strlen(argv[optind])+1);
00170    strcpy(inputFileName,argv[optind]);
00171  }
00172 
00173  if(checkSyntaxOnly == 1){
00174    parse_result=asn1_parser2tree(inputFileName,&pointer,errorDescription);
00175    asn1_delete_structure(&pointer);
00176  }
00177  else /* C VECTOR CREATION */
00178    parse_result=asn1_parser2array(inputFileName,
00179                 outputFileName,vectorName,errorDescription);
00180 
00181  switch(parse_result){
00182  case ASN1_SUCCESS:
00183    printf("Done.\n");
00184    break;
00185  case ASN1_FILE_NOT_FOUND:
00186    printf("asn1Parser: FILE %s NOT FOUND\n",inputFileName);
00187    break;
00188  case ASN1_SYNTAX_ERROR: 
00189  case ASN1_IDENTIFIER_NOT_FOUND: 
00190  case ASN1_NAME_TOO_LONG:
00191    printf("asn1Parser: %s\n",errorDescription);
00192    break;
00193  default:
00194    printf("libasn1 ERROR: %s\n",libtasn1_strerror(parse_result));
00195  }
00196 
00197 
00198  free(inputFileName);
00199  if(outputFileName) free(outputFileName);
00200  if(vectorName) free(vectorName);
00201 
00202  if(parse_result != ASN1_SUCCESS) exit(1);
00203  exit(0);
00204 }
00205 
00206 
00207 
00208 
00209 
00210 
00211 

Generated by  Doxygen 1.5.1