1 /*
2  * tomods.c
3  *
4  * Copyright (c) Chris Putnam 2004-2020
5  * Copyright (c) Georgi N. Boshnakov 2020
6  *
7  * Program and source code released under the GPL version 2
8  *
9  */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "slist.h"
14 #include "bibl.h"
15 #include "bibutils.h"
16 #include "tomods.h"
17 #include "args.h"
18 #include "bibprog.h"
19 
20 static void
args_tomods_help(char * progname,char * help1,char * help2)21 args_tomods_help( char *progname, char *help1, char *help2 )
22 {
23 	args_tellversion( progname );
24 	REprintf("%s", help1 );
25 
26 	REprintf("usage: %s %s > xml_file\n\n", progname, help2 );
27         REprintf("  %s can be replaced with file list or "
28 			"omitted to use as a filter\n\n", help2 );
29 
30 	REprintf("  -h, --help                display this help\n");
31 	REprintf("  -v, --version             display version\n");
32 	REprintf("  -a, --add-refcount        add \"_#\", where # is reference count to reference\n");
33 	REprintf("  -s, --single-refperfile   one reference per output file\n");
34 	REprintf("  -i, --input-encoding      input character encoding\n");
35 	REprintf("  -o, --output-encoding     output character encoding\n");
36 	REprintf("  -u, --unicode-characters  DEFAULT: write unicode (not xml entities)\n");
37 	REprintf("  -un,--unicode-no-bom      as -u, but don't write byte order mark\n");
38 	REprintf("  -x, --xml-entities        write xml entities and not direclty unicode\n");
39 	REprintf("  -nl,--no-latex            do not convert latex-style character combinations\n");
40 	REprintf("  -d, --drop-key            don't put key in MODS ID field\n");
41 	REprintf("  -c, --corporation-file    specify file of corporation names\n");
42 	REprintf("  -as, --asis               specify file of names that shouldn't be mangled\n");
43 	REprintf("  -nt, --nosplit-title      don't split titles into TITLE/SUBTITLE pairs\n");
44 	REprintf("  --verbose                 report all warnings\n");
45 	REprintf("  --debug                   very verbose output\n\n");
46 
47 	REprintf("http://sourceforge.net/p/bibutils/home/Bibutils for more details\n\n");
48 }
49 
50 static void
args_namelist(int argc,char * argv[],int i,char * progname,char * shortarg,char * longarg)51 args_namelist( int argc, char *argv[], int i, char *progname, char *shortarg,
52 		char *longarg )
53 {
54 	if ( i+1 >= argc ) {
55 		REprintf( "%s: error %s (%s) takes the argument of "
56 			"the file\n", progname, shortarg, longarg );
57 		error("\n"); // error( EXIT_FAILURE );
58 	}
59 }
60 
61 void
tomods_processargs(int * argc,char * argv[],param * p,char * help1,char * help2)62 tomods_processargs( int *argc, char *argv[], param *p,
63 	char *help1, char *help2 )
64 {
65 	int i, j, subtract, status;
66 
67 	process_charsets( argc, argv, p );
68 
69         i = 0;
70 	while ( i<*argc ) {
71 		subtract = 0;
72 		if ( args_match( argv[i], "-h", "--help" ) ) {
73 			args_tomods_help( p->progname, help1, help2 );
74 			// error("\n"); // error( EXIT_SUCCESS );
75 			subtract = 1;
76 		} else if ( args_match( argv[i], "-v", "--version" ) ) {
77 			args_tellversion( p->progname );
78 			// error("\n"); // error( EXIT_SUCCESS );
79 			subtract = 1;
80 		} else if ( args_match( argv[i], "-a", "--add-refcount" ) ) {
81 			p->addcount = 1;
82 			subtract = 1;
83 		} else if ( args_match(argv[i], NULL, "--verbose" ) ) {
84 			/* --debug + --verbose = --debug */
85 			if ( p->verbose<1 ) p->verbose = 1;
86 			p->format_opts |= BIBL_FORMAT_VERBOSE;
87 			subtract = 1;
88 		} else if ( args_match(argv[i], NULL, "--debug" ) ) {
89 			p->verbose = 3;
90 			p->format_opts |= BIBL_FORMAT_VERBOSE;
91 			subtract = 1;
92 		} else if ( args_match( argv[i], "-d", "--drop-key" ) ) {
93 			p->format_opts |= BIBL_FORMAT_MODSOUT_DROPKEY;
94 			subtract = 1;
95 		} else if ( args_match( argv[i], "-s", "--single-refperfile" )){
96 			p->singlerefperfile = 1;
97 			subtract = 1;
98 		} else if ( args_match( argv[i], "-u", "--unicode-characters")){
99 			p->utf8out = 1;
100 			p->utf8bom = 1;
101 			p->charsetout = BIBL_CHARSET_UNICODE;
102 			p->charsetout_src = BIBL_SRC_USER;
103 			subtract = 1;
104 		} else if ( args_match( argv[i], "-un", "--unicode-no-bom")){
105 			p->utf8out = 1;
106 			p->utf8bom = 0;
107 			p->charsetout = BIBL_CHARSET_UNICODE;
108 			p->charsetout_src = BIBL_SRC_USER;
109 			subtract = 1;
110 		} else if ( args_match( argv[i], "-nl", "--no-latex" ) ) {
111 			p->latexin = 0;
112 			subtract = 1;
113 		} else if ( args_match( argv[i], "-nt", "--nosplit-title" ) ){
114 			p->nosplittitle = 1;
115 			subtract = 1;
116 		} else if ( args_match( argv[i], "-x", "--xml-entities" ) ) {
117 			p->utf8out = 0;
118 			p->utf8bom = 0;
119 			p->xmlout = 1;
120 			subtract = 1;
121 		} else if ( args_match( argv[i], "-c", "--corporation-file")){
122 			args_namelist( *argc, argv, i, p->progname,
123 				"-c", "--corporation-file" );
124 			status = bibl_readcorps( p, argv[i+1] );
125 			if ( status == BIBL_ERR_MEMERR ) {
126 				REprintf( "%s: Memory error when reading --corporation-file '%s'\n",
127 					p->progname, argv[i+1] );
128 				error("\n"); // error( EXIT_FAILURE );
129 			} else if ( status == BIBL_ERR_CANTOPEN ) {
130 				REprintf( "%s: Cannot read --corporation-file '%s'\n",
131 					p->progname, argv[i+1] );
132 			}
133 			subtract = 2;
134 		} else if ( args_match( argv[i], "-as", "--asis")) {
135 			args_namelist( *argc, argv, i, p->progname,
136 				"-as", "--asis" );
137 			status = bibl_readasis( p, argv[i+1] );
138 			if ( status == BIBL_ERR_MEMERR ) {
139 				REprintf( "%s: Memory error when reading --asis file '%s'\n",
140 					p->progname, argv[i+1] );
141 				error("\n"); // error( EXIT_FAILURE );
142 			} else if ( status == BIBL_ERR_CANTOPEN ) {
143 				REprintf( "%s: Cannot read --asis file '%s'\n",
144 					p->progname, argv[i+1] );
145 			}
146 			subtract = 2;
147 		}
148 		if ( subtract ) {
149 			for ( j=i+subtract; j<*argc; j++ ) {
150 				argv[j-subtract] = argv[j];
151 			}
152 			*argc -= subtract;
153 		} else {
154 			if ( argv[i][0]=='-' ) REprintf( "(todmod.c) Warning: Did not recognize potential command-line argument %s\n", argv[i] );
155 			i++;
156 		}
157 	}
158 }
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170