summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/splitindex/splitindex.java
blob: c6c6d4bbf1335ec00a99d8106d72e8ad9e97d14b (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 * splitindex.java
 * Copyright (c) Markus Kohm, 2002-2009
 *
 * $Id: splitindex.java 4 2016-02-18 10:13:32Z mjk $
 *
 * This file is part of the SplitIndex bundle.
 *
 * This work may be distributed and/or modified under the conditions of
 * the LaTeX Project Public License, version 1.3c of the license.
 * The latest version of this license is in
 *   http://www.latex-project.org/lppl.txt
 * and version 1.3c or later is part of all distributions of LaTeX
 * version 2005/12/01 or later and of this work.
 *
 * This work has the LPPL maintenance status "author-maintained".
 *
 * The Current Maintainer and author of this work is Markus Kohm.
 *
 * The list of all files belongig to the SplitIndex bundle is given in
 * in the file `manifest.txt'. Files generated by means of unpacking the
 * distribution (using, for example, the docstrip program) or by means
 * of compiling them from a source file, for example, from splitindex.c
 * or splitindex.java may be distributed at the distributor's discretion.
 * However if they are distributed then a copy of the SplitIndex bundle
 * must be distributed together with them.
 *
 * The list of derived (unpacked or compiled) files belongig to the 
 * distribution and covered by LPPL is defined by the unpacking scripts 
 * (with extension .ins) and the installation script (with name 
 * install.sh) which are part of the distribution.
 *
 * Two often ignorred clauses from LPPL 1.3c you should not ignore:
 * ----------------------------------------------------------------
 * 2. You may distribute a complete, unmodified copy of the Work as you
 *    received it.  Distribution of only part of the Work is considered
 *    modification of the Work, and no right to distribute such a Derived
 *    Work may be assumed under the terms of this clause.
 * 3. You may distribute a Compiled Work that has been generated from a
 *    complete, unmodified copy of the Work as distributed under Clause 2
 *    above, as long as that Compiled Work is distributed in such a way that
 *    the recipients may install the Compiled Work on their system exactly
 *    as it would have been installed if they generated a Compiled Work
 *    directly from the Work.
 */

/*
  I know, I should never write an application in a single static class 
  like I do here. But I wanted the program at each language to be one
  file.
*/

import java.util.*;
import java.util.regex.*;
import java.io.*;

public final class splitindex {
    private static ArrayList MakeIndexArgs = new ArrayList();
//     private static String Identify = "^(.*)\\\\UseIndex *\\{([^\\}]*)\\}(.*)$";
    private static String Identify = "^(\\\\indexentry)\\[([^\\]]*)\\](.*)$";
    private static String ResultIs = "$1$3";
    private static String SuffixIs = "-$2";
    private static int Verbose = 0;
    private static String IDX = null;
    private static String Jobname = null;
    private static HashMap IDXwriters = new HashMap();

    private static void ShowHelp() {
	String nl = System.getProperty( "line.separator" );
	ShowVersion();
	System.out.println();
	ShowUsage();
	System.out.println(
	    "Split a single raw index file into multiple raw index files." +nl+
	    "Example: java splitindex foo.idx." +nl+
	    nl+
	    "Options:" +nl+
	    "  -h, --help    " +
	    "\tshow this help and terminate" +nl+
	    "  -m, --makeindex PROGNAME" +nl+
	    "\t\t\tcall PROGNAME instead of default `makeindex'." +nl+
	    "  -i, --identify EXPRESSION" +nl+
	    "\t\t\tuse regular EXPRESSION to match entries" +nl+
	    "\t\t\t(see also option --resultis and --suffixis)." +nl+
	    "\t\t\tDefault is \'" + Identify + "\'." +nl+
	    "  -r, --resultis PATTERN" +nl+
	    "\t\t\tcreate line to be written from PATTERN after matching" +nl+
	    "\t\t\tlines (see also option --identify)." +nl+
	    "\t\t\tDefault is \'" + ResultIs + "\'." +nl+
	    "  -s, --suffixis PATTERN" +nl+
	    "\t\t\tcreate suffix to be used from PATTERN after matching" +nl+
	    "\t\t\tlines (see also option --identify)." +nl+
	    "\t\t\tDefault is \'" + SuffixIs +"\'." +nl+
	    "  -v, --verbose " +
	    "\tbe more verbose" +nl+
	    "\t\t\t(can be used multiple to increase verbosity)" +nl+
	    "      --version " +
	    "\tshow version and terminate"
	    );
	System.exit( 0 );
    }

    private static void ShowVersion() {
	System.out.println( "splitindex.pl 0.1" );
	System.out.println( "Copyright (c) 2002 Markus Kohm <kohm@gmx.de>" );
    }

    private static void ShowUsage( PrintStream out ) {
	out.println( "Usage: java splitindex [OPTION]... RAWINDEXFILE [MAKEINDEXOPTION]..." );
    }

    private static void ShowUsage() {
	ShowUsage( System.out );
    }

    private static void UsageError( String msg ) {
	System.err.println( msg );
	System.err.println( "Try `java splitindex --help' for more information." );
	System.exit( 1 );
    }

    private static void ScanArguments(String[] args) {
	String MakeIndex = "makeindex";
	for (int i = 0; i < args.length; i++) {
	    if ( args[i].charAt(0) == '-' ) {
		// Option
		if ( args[i].charAt(1) == '-' ) {
		    // Long Option
		    if ( args[i].substring(2).equals( "help" ) ) {
			ShowHelp();
			System.exit(0);
		    } else if ( args[i].substring(2).equals( "version" ) ) {
			ShowVersion();
			System.exit(0);
		    } else if ( args[i].substring(2).equals( "verbose" ) ) {
			Verbose++;
		    } else if ( args[i].substring(2).equals( "makeindex" ) ) {
			if ( ++i >= args.length ) {
			    UsageError( "Option makeindex requires an argument" );
			}
			MakeIndex = args[i];
		    } else if ( ( args[i].length() >= 11 ) && 
				( args[i].substring(2,11).equals( "makeindex=" ) ) ) {
			MakeIndex = args[i].substring(12);
		    } else if ( args[i].substring(2).equals( "identify" ) ) {
			if ( ++i >= args.length ) {
			    UsageError( "Option identify requires an argument" );
			}
		        Identify = args[i];
		    } else if ( ( args[i].length() >= 10 ) && 
				( args[i].substring(2,10).equals( "identify=" ) ) ) {
			Identify = args[i].substring(11);
		    } else if ( args[i].substring(2).equals( "resultis" ) ) {
			if ( ++i >= args.length ) {
			    UsageError( "Option resultis requires an argument" );
			}
			ResultIs = args[i];
		    } else if ( ( args[i].length() >= 10 ) && 
				( args[i].substring(2,10).equals( "resultis=" ) ) ) {
			ResultIs = args[i].substring(11);
		    } else if ( args[i].substring(2).equals( "suffixis" ) ) {
			if ( ++i >= args.length ) {
			    UsageError( "Option suffixis requires an argument" );
			}
			SuffixIs = args[i];
		    } else if ( ( args[i].length() >= 10 ) && 
				( args[i].substring(2,10).equals( "suffixis=" ) ) ) {
			SuffixIs = args[i].substring(11);
		    } else if ( args[i].equals( "--" ) ) {
			while ( ++i < args.length ) {
			    MakeIndexArgs.add( args[i] );
			}
		    } else {
			UsageError( "Unknown option " + args[i].substring(2) );
		    }
		} else {
		    // Short Option
		    for ( int n = 1; n < args[i].length(); ) {
			switch( args[i].charAt(n++) ) {
			    case 'h':
				ShowHelp();
				System.exit(0);
			    case 'm':
				if ( n >= args[i].length() ) {
				    if ( ++i >= args.length ) {
					UsageError( "Option makeindex requires an argument" );
				    } else {
					MakeIndex = args[i];
					n = args[i].length();
				    }
				} else {
				    MakeIndex = args[i].substring( n );
				    n = args[i].length();
				}
				break;
			    case 'i':
				if ( n >= args[i].length() ) {
				    if ( ++i >= args.length ) {
					UsageError( "Option identify requires an argument" );
				    } else {
					Identify = args[i];
					n = args[i].length();
				    }
				} else {
				    Identify = args[i].substring( n );
				    n = args[i].length();
				}
				break;
			    case 'r':
				if ( n >= args[i].length() ) {
				    if ( ++i >= args.length ) {
					UsageError( "Option resultis requires an argument" );
				    } else {
				        ResultIs = args[i];
					n = args[i].length();
				    }
				} else {
				    ResultIs = args[i].substring( n );
				    n = args[i].length();
				}
				break;
			    case 's':
				if ( n >= args[i].length() ) {
				    if ( ++i >= args.length ) {
					UsageError( "Option suffixis requires an argument" );
				    } else {
					SuffixIs = args[i];
					n = args[i].length();
				    }
				} else {
				    SuffixIs = args[i].substring( n );
				    n = args[i].length();
				}
				break;
			    case 'v':
				Verbose++;
				break;
			}
		    }
		}
	    } else {
		MakeIndexArgs.add( args[i] );
	    }
	}

	// Args --> IDX + Stringarray
	if ( MakeIndexArgs.isEmpty() ) {
	    UsageError( "missing raw index file" );
	} else {
	    IDX = (String)MakeIndexArgs.get(0);
	    MakeIndexArgs.remove(0);
	    MakeIndexArgs.add( 0, MakeIndex );
	}

	if ( IDX.endsWith( ".idx" ) ) {
	    Jobname = IDX.substring( 0, IDX.length() - 4 );
	} else {
	    Jobname = IDX;
	}
    }

    public static void main(String[] args) {
	ScanArguments(args);

	if ( Verbose > 0 ) {
	    ShowVersion();
	    System.out.println();
	    if ( Verbose > 9 ) {
		System.out.println( "Identify:  \"" + Identify + "\"" );
		System.out.println( "ResultIs:  \"" + ResultIs + "\"" );
		System.out.println( "SuffixIs:  \"" + SuffixIs + "\"" );
		System.out.println( "IDX:       \"" + IDX + "\"" );
		System.out.println( "Jobname:   \"" + Jobname + "\"" );
		System.out.print( "MakeIndex:" );
		for ( int i = 0; i < MakeIndexArgs.size(); i++ )
		    System.out.print( " \"" + (String)MakeIndexArgs.get(i) + "\"" );
		System.out.println();
	    }
	}

	ProcessIDXFile();
    }

    private static void ProcessIDXFile() {
	File fIDX = new File( IDX );
	LineNumberReader rIDX = null;
	boolean error = false;
	if ( !fIDX.canRead() ) {
	    if ( IDX.equals( Jobname ) ) {
		IDX = IDX.concat( ".idx" );
		fIDX = new File( IDX );
		if ( !fIDX.canRead() ) {
		    System.err.println( "Can read neither file " + Jobname +
					" nor file " + IDX );
		    System.exit( 1 );
		}
	    } else {
		System.err.println( "Can't read file " + Jobname );
		System.exit( 1 );
	    }
	}
	
	try {
	    FileReader reader = new FileReader( fIDX );
	    rIDX = new LineNumberReader( reader );
	} catch ( FileNotFoundException ex ) {
	    System.err.println( ex.getMessage() );
	    System.exit( 1 );
	}
    
	try {
	    Pattern search = Pattern.compile( Identify );
	    while ( rIDX.ready() ) {
		String line = rIDX.readLine();
		Matcher match = search.matcher( line );
		String suffix, result;
		
		try {
 		    if ( match.find() ) {
 			suffix = match.replaceFirst( SuffixIs );
 			result = match.replaceFirst( ResultIs );
 		    } else {
 			result = line;
 			suffix = SuffixIs.replaceFirst( "\\$\\d", "idx" );
 		    }
		    WriteToIndex( Jobname + suffix + ".idx", result );
		} catch ( Exception ex ) {
		    System.err.println( ex.getMessage() );
		    error = true;
		    break;
		}
	    }

	    if ( ! CloseAllIndex() )
		error = true;
	    else if ( ! CallAllMakeIndex() )
		error = true;
	} catch ( IOException ex ) {
	    System.err.println( ex.getMessage() );
	}

	try {
	    rIDX.close();
	} catch ( IOException ex ) {
	    System.err.println( ex.getMessage() );
	    System.exit( 1 );
	}

	if ( error )
	    System.exit( 1 );

    }

    static void WriteToIndex( String Name, String line ) 
	throws FileNotFoundException, SecurityException {
	PrintWriter fOut;
	if ( ( fOut = (PrintWriter)IDXwriters.get( Name ) ) == null ) {
	    if ( Verbose > 1 ) {
		System.out.println( "New index file " + Name );
	    }
	    fOut = new PrintWriter( new FileOutputStream( Name ) );
	    IDXwriters.put( Name, fOut );
	}
	fOut.println( line );
    }

    static boolean CloseAllIndex() {
	Iterator all = IDXwriters.entrySet().iterator();
	boolean retVal = true;
	while ( all.hasNext() ) {
	    Map.Entry entry = (Map.Entry)all.next();
	    PrintWriter writer = (PrintWriter)entry.getValue();
	    if ( Verbose > 1 ) {
		System.out.println( "Close " + entry.getKey() );
	    }
	    writer.close();
	    if ( writer.checkError() ) {
		System.err.println( "Error writing " + entry.getKey() );
		retVal = false;
	    }
	}
	return retVal;
    }

    static boolean CallAllMakeIndex() {
	Iterator all = IDXwriters.entrySet().iterator();
	boolean retVal = true;
	ArrayList processes = new ArrayList();
	while ( all.hasNext() ) {
	    Map.Entry entry = (Map.Entry)all.next();
	    String name = (String)entry.getKey();
	    try {
		MakeIndexArgs.add(name);
		String Args[] = new String[MakeIndexArgs.size()];
		Args = (String[])MakeIndexArgs.toArray( Args );
		MakeIndexArgs.remove(MakeIndexArgs.size()-1);
		if ( Verbose > 1 ) {
		    System.out.print( "MakeIndex:" );
		    for ( int i = 0; i < Args.length; i++ )
			System.out.print( " \"" + Args[i] + "\"" );
		    System.out.println();
		}
		processes.add(Runtime.getRuntime().exec( Args ));
	    } catch ( Exception ex ) {
		System.err.println( ex.getMessage() );
		retVal = false;
	    }
	}

	for ( int i = 0; i < processes.size(); i++ ) {
	    Process p = (Process)processes.get( i );
	    InputStream out = p.getInputStream();
	    byte[] buffer = new byte[1024];
	    for ( boolean oncemore = true; oncemore; ) {
		oncemore = false;
		try {
		    while ( out.available() > 0 ) {
			out.read( buffer );
			System.out.print( new String(buffer) );
		    }
		    for ( out = p.getErrorStream();
			  out.available() > 0; ) {
			out.read( buffer );
			System.err.print( new String( buffer ) );
		    }
		} catch ( Exception ex ) {
		    System.err.println( ex.getMessage() );
		    retVal = false;
		}
		try {
		    if ( p.exitValue() != 0 )
			retVal = false;
		} catch ( IllegalThreadStateException ex ) {
		    oncemore = true;
		}
	    }
	}
	
	return retVal;
    }
}