File: args.c

package info (click to toggle)
bibutils 4.8-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,512 kB
  • ctags: 1,340
  • sloc: ansic: 72,394; csh: 216; makefile: 117
file content (107 lines) | stat: -rw-r--r-- 2,857 bytes parent folder | download
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
/*
 * args.c
 *
 * Copyright (c) 2004-8
 *
 * Program and source code released under the GPL
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include "newstr_conv.h"
#include "bibutils.h"
#include "args.h"

void
args_tellversion( char *progname )
{
	char bibutils_version[] = CURR_VERSION;
	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( char *check, char *shortarg, char *longarg )
{
	if ( shortarg && !strcmp( check, shortarg ) ) return 1;
	if ( longarg  && !strcmp( check, longarg  ) ) return 1;
	return 0;
}

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" );
		list_charsets( stderr );
		fprintf( stderr, "SPECIFY AS: -o CHARSETNAME\n" );
		exit( EXIT_FAILURE );
	} else {
		if ( !args_charset( argv[i+1], charset, utf8 ) ) {
			fprintf( stderr, "%s: character encoding lookup "
					"failed.\n", progname );
			list_charsets( stderr );
		}
	}
}

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 = get_charset( charset_name );
		*utf8 = 0;
	}
	if ( *charset == BIBL_CHARSET_UNKNOWN ) return 0;
	else return 1;
}

/* Must process charset info first so switches are order independent */
void
process_charsets( int *argc, char *argv[], param *p,
	int use_input, int use_output )
{
	int i, j, subtract;
	i = 1;
	while ( i<*argc ) {
		subtract = 0;
		if ( use_input && args_match( argv[i], "-i", "--input-encoding" ) ) {
			args_encoding( *argc, argv, i, &(p->charsetin), 
					&(p->utf8in), p->progname, 0 );
			p->charsetin_src = BIBL_SRC_USER;
			subtract = 2;
		} else if ( use_output && 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->utf8bom = 1;
			if ( p->charsetout==BIBL_CHARSET_GB18030 )
				p->latexout = 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++;
	}
}