1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
/*
* args.c
*
* Copyright (c) 2004-2021
*
* Source code released under the GPL version 2
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "charsets.h"
#include "bibutils.h"
#include "args.h"
void
args_tellversion( const char *progname )
{
const char bibutils_version[] = CURR_VERSION;
const char bibutils_date[] = CURR_DATE;
fprintf( stderr, "%s, ", progname );
fprintf( stderr, "bibutils suite version %s date %s\n",
bibutils_version, bibutils_date );
}
int
args_match( const char *check, const char *shortarg, const char *longarg )
{
if ( shortarg && !strcmp( check, shortarg ) ) return 1;
if ( longarg && !strcmp( check, longarg ) ) return 1;
return 0;
}
char *
args_next( int argc, char *argv[], int n, const char *progname, const char *shortarg, const char *longarg )
{
if ( n>=argc ) {
fprintf( stderr, "%s: option ", progname );
if ( shortarg ) fprintf( stderr, "%s", shortarg );
if ( shortarg && longarg ) fprintf( stderr, "/" );
if ( longarg ) fprintf( stderr, "%s", longarg );
fprintf( stderr, " takes an argument. Exiting.\n" );
exit( EXIT_FAILURE );
}
return argv[n+1];
}
static int
args_charset( char *charset_name, int *charset, unsigned char *utf8 )
{
if ( !strcasecmp( charset_name, "unicode" ) ||
!strcasecmp( charset_name, "utf8" ) ) {
*charset = BIBL_CHARSET_UNICODE;
*utf8 = 1;
} else if ( !strcasecmp( charset_name, "gb18030" ) ) {
*charset = BIBL_CHARSET_GB18030;
*utf8 = 0;
} else {
*charset = charset_find( charset_name );
*utf8 = 0;
}
if ( *charset == BIBL_CHARSET_UNKNOWN ) return 0;
else return 1;
}
static void
args_encoding( int argc, char *argv[], int i, int *charset,
unsigned char *utf8, char *progname, int inout )
{
char *shortver[] = { "-i", "-o" };
char *longver[] = { "--input-encoding", "--output-encoding" };
if ( i+1 >= argc ) {
fprintf( stderr, "%s: error %s (%s) takes "
"the argument of the character set type\n",
progname, shortver[inout], longver[inout] );
fprintf( stderr, "UNICODE UTF-8: unicode OR utf8\n" );
fprintf( stderr, "CHINESE: gb18030\n" );
fprintf( stderr, "OTHERS:\n" );
charset_list_all( stderr );
fprintf( stderr, "SPECIFY AS: -i CHARSETNAME or -o CHARSETNAME\n" );
exit( EXIT_FAILURE );
} else {
if ( !args_charset( argv[i+1], charset, utf8 ) ) {
fprintf( stderr, "%s: character encoding lookup "
"failed.\n", progname );
charset_list_all( stderr );
}
}
}
/* Must process charset info first so switches are order independent */
void
process_charsets( int *argc, char *argv[], param *p )
{
int i, j, subtract;
i = 1;
while ( i<*argc ) {
subtract = 0;
if ( args_match( argv[i], "-i", "--input-encoding" ) ) {
args_encoding( *argc, argv, i, &(p->charsetin),
&(p->utf8in), p->progname, 0 );
if ( p->charsetin!=BIBL_CHARSET_UNICODE )
p->utf8in = 0;
p->charsetin_src = BIBL_SRC_USER;
subtract = 2;
} else if ( args_match( argv[i], "-o", "--output-encoding" ) ) {
args_encoding( *argc, argv, i, &(p->charsetout),
&(p->utf8out), p->progname, 1 );
if ( p->charsetout==BIBL_CHARSET_UNICODE ) {
p->utf8out = 1;
p->utf8bom = 1;
} else if ( p->charsetout==BIBL_CHARSET_GB18030 ) {
p->latexout = 0;
} else {
p->utf8out = 0;
p->utf8bom = 0;
}
p->charsetout_src = BIBL_SRC_USER;
subtract = 2;
}
if ( subtract ) {
for ( j=i+subtract; j<*argc; ++j )
argv[j-subtract] = argv[j];
*argc -= subtract;
} else i++;
}
}
|