Debian Codegraph @

texmf-dist/source/latex/splitindex/splitindex.c

raw
1 /*
2 * splitindex.c
3 * Copyright (c) Markus Kohm, 2002
4 *
5 * $Id: splitindex.c 4 2016-02-18 10:13:32Z mjk $
6 *
7 * This file is part of the SplitIndex bundle.
8 *
9 * This work may be distributed and/or modified under the conditions of
10 * the LaTeX Project Public License, version 1.3c of the license.
11 * The latest version of this license is in
12 * http://www.latex-project.org/lppl.txt
13 * and version 1.3c or later is part of all distributions of LaTeX
14 * version 2005/12/01 or later and of this work.
15 *
16 * This work has the LPPL maintenance status "author-maintained".
17 *
18 * The Current Maintainer and author of this work is Markus Kohm.
19 *
20 * The list of all files belongig to the SplitIndex bundle is given in
21 * in the file `manifest.txt'. Files generated by means of unpacking the
22 * distribution (using, for example, the docstrip program) or by means
23 * of compiling them from a source file, for example, from splitindex.c
24 * or splitindex.java may be distributed at the distributor's discretion.
25 * However if they are distributed then a copy of the SplitIndex bundle
26 * must be distributed together with them.
27 *
28 * The list of derived (unpacked or compiled) files belongig to the
29 * distribution and covered by LPPL is defined by the unpacking scripts
30 * (with extension .ins) and the installation script (with name
31 * install.sh) which are part of the distribution.
32 *
33 * Two often ignorred clauses from LPPL 1.3c you should not ignore:
34 * ----------------------------------------------------------------
35 * 2. You may distribute a complete, unmodified copy of the Work as you
36 * received it. Distribution of only part of the Work is considered
37 * modification of the Work, and no right to distribute such a Derived
38 * Work may be assumed under the terms of this clause.
39 * 3. You may distribute a Compiled Work that has been generated from a
40 * complete, unmodified copy of the Work as distributed under Clause 2
41 * above, as long as that Compiled Work is distributed in such a way that
42 * the recipients may install the Compiled Work on their system exactly
43 * as it would have been installed if they generated a Compiled Work
44 * directly from the Work.
45 */
46
47 #include <sys/types.h>
48 #include <sys/wait.h>
49
50 #include <ctype.h>
51 #include <errno.h>
52 #include <regex.h>
53 #include <stdarg.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 enum {
60 OPT_HELP = 'h', OPT_MAKEINDEX = 'm', OPT_IDENTIFY = 'i',
61 OPT_RESULTIS = 'r', OPT_VERBOSE = 'v', OPT_SUFFIXIS = 's',
62 OPT_VERSION = 'V'
63 };
64
65 #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined (NO_LONGOPT)
66
67 #define HAS_LONGOPT 0
68 #define HAPAR ""
69
70 #else /* defined(__OpenBSD__) || defined(__FreeBSD__) || defined(NO_LONGOPT) */
71
72 #define HAS_LONGOPT 1
73 #define HAPAR "\n\t\t"
74
75 #include <getopt.h>
76
77 static const struct option long_options[] = {
78 { "help", 0, NULL, OPT_HELP },
79 { "makeindex", 1, NULL, OPT_MAKEINDEX },
80 { "identify", 1, NULL, OPT_IDENTIFY },
81 { "resultis", 1, NULL, OPT_RESULTIS },
82 { "suffixis", 1, NULL, OPT_SUFFIXIS },
83 { "verbose", 0, NULL, OPT_VERBOSE },
84 { "version", 0, NULL, OPT_VERSION },
85 { NULL, 0, NULL, 0 },
86 };
87
88 #endif
89
90 static const char short_options[] = {
91 OPT_HELP,
92 OPT_MAKEINDEX, ':',
93 OPT_IDENTIFY, ':',
94 OPT_RESULTIS, ':',
95 OPT_SUFFIXIS, ':',
96 OPT_VERBOSE,
97 OPT_VERSION,
98 };
99
100 int Verbose = 0;
101
102 char *MakeIndex = "makeindex";
103 char *Identify = "^(\\\\indexentry)\\[([^]]*)\\](.*)$";
104 char *ResultIs = "$1$3";
105 char *SuffixIs = "-$2";
106 char *IDX = NULL;
107 char *Jobname = NULL;
108 char * const * MakeIndexArgs = NULL;
109 char *prgname = "splitindex";
110 int MakeIndexArgc = 0;
111
112 static void show_version( void ) {
113 printf( "splitindex 0.2a\n"
114 "Copyright (c) 2002 Markus Kohm <[email protected]>\n" );
115 }
116
117 static void show_usage( FILE *out ) {
118 fputs( "Usage: splitindex [OPTION]... RAWINDEXFILE [MAKEINDEXOPTION]...\n",
119 out );
120 }
121
122 static void usage_error( const char *format, ... ) {
123 if ( format != NULL ) {
124 va_list ap;
125 va_start( ap, format );
126 vfprintf( stderr, format, ap );
127 va_end( ap );
128 }
129 fputs( "Try `splitindex "
130 #if HAS_LONGOPT
131 "--help"
132 #else
133 "-h"
134 #endif
135 "' for more information.\n", stderr );
136 exit( 1 );
137 }
138
139 static void show_help( void ) {
140 show_version();
141 printf( "\n" );
142 show_usage( stdout );
143 printf(
144 "Split a single raw index file into multiple raw index files.\n"
145 "Example: splitindex.pl foo.idx.\n"
146 "\n"
147 "Options:\n"
148 " -h"
149 #if HAS_LONGOPT
150 ", --help "
151 #else
152 " "
153 #endif
154 "\tshow this help and terminate\n"
155 " -m"
156 #if HAS_LONGOPT
157 ", --makeindex"
158 #endif
159 " PROGNAME " HAPAR
160 "\tcall PROGNAME instead of default `%s'.\n",
161 MakeIndex
162 );
163 printf(
164 " -i"
165 #if HAS_LONGOPT
166 ", --identify"
167 #endif
168 " EXPRESSION " HAPAR
169 "\tuse regular EXPRESSION to match entries\n"
170 "\t\t\t(see also option --resultis and --suffixis).\n"
171 "\t\t\tDefault is `%s'.\n",
172 Identify
173 );
174 printf(
175 " -r"
176 #if HAS_LONGOPT
177 ", --resultis"
178 #endif
179 " PATTERN " HAPAR
180 "\tcreate line to be written from PATTERN after matching\n"
181 "\t\t\tlines (see also option --identify).\n"
182 "\t\t\tDefault is `%s'.\n",
183 ResultIs
184 );
185 printf(
186 " -s"
187 #if HAS_LONGOPT
188 ", --suffixis"
189 #endif
190 " PATTERN " HAPAR
191 "\tcreate suffix to be used from PATTERN after matching\n"
192 "\t\t\tlines (see also option --identify).\n"
193 "\t\t\tDefault is `%s'.\n",
194 SuffixIs
195 );
196 printf(
197 " -v"
198 #if HAS_LONGOPT
199 ", --verbose "
200 #else
201 " "
202 #endif
203 "\tbe more verbose\n"
204 "\t\t\t(can be used multiple to increase verbosity)\n"
205 " -V"
206 #if HAS_LONGOPT
207 ", --version "
208 #else
209 " "
210 #endif
211 "\tshow version and terminate\n"
212 );
213 exit(0);
214 }
215
216 static void ScanArguments( int argc, char * const argv[] ) {
217 int retVal;
218 char *hs;
219 if ( argc > 0 )
220 prgname = argv[0];
221 #if HAS_LONGOPT
222 while ( ( retVal = getopt_long( argc, argv, short_options,
223 long_options, NULL ) ) != - 1 ) {
224 #else
225 while ( ( retVal = getopt( argc, argv, short_options ) ) != - 1 ) {
226 #endif
227 switch( retVal ) {
228 case OPT_HELP:
229 show_help();
230 break; /* should never be reached */
231 case OPT_VERSION:
232 show_version();
233 exit( 0 );
234 break; /* should never be reached */
235 case OPT_MAKEINDEX:
236 MakeIndex = optarg;
237 break;
238 case OPT_IDENTIFY:
239 Identify = optarg;
240 break;
241 case OPT_RESULTIS:
242 ResultIs = optarg;
243 break;
244 case OPT_SUFFIXIS:
245 SuffixIs = optarg;
246 break;
247 case OPT_VERBOSE:
248 Verbose++;
249 break;
250 case ':':
251 case '?':
252 usage_error( NULL );
253 break; /* should never be reached */
254 }
255 }
256 if ( optind < argc ) {
257 IDX = argv[optind++];
258 MakeIndexArgc = argc - optind;
259 MakeIndexArgs = argv + optind;
260 } else {
261 usage_error( "missing raw index file\n" );
262 return; /* should never be reached */
263 }
264
265 if ( ( Jobname = strdup( IDX ) ) == NULL ) {
266 perror( prgname );
267 exit( errno );
268 }
269
270 if ( ( ( hs = strrchr( Jobname, '.' ) ) != NULL )
271 && !strcmp( hs, ".idx" ) )
272 *hs = 0;
273 }
274
275 static char *regsub( const char *string, const char *replace,
276 int nmatch, regmatch_t pmatch[] ) {
277 int pass;
278 int size, n;
279 const char *hs;
280 char c;
281 char *retVal = NULL;
282 char *ts;
283
284 /* Two times parsing to get size and replace */
285 for ( pass = size = 0; pass < 2; pass++ ) {
286 ts = retVal;
287 for ( hs = replace; *hs; hs++ ) {
288 switch( *hs ) {
289 case '$':
290 if ( isdigit( hs[1] ) ) {
291 n = *++hs - '0';
292 if ( ( pmatch[n].rm_so >= 0 )
293 && ( pmatch[n].rm_so < pmatch[n].rm_eo ) ) {
294 /* maybe group n is not empty */
295 if ( pass ) {
296 strncpy( ts,
297 string + pmatch[n].rm_so,
298 pmatch[n].rm_eo - pmatch[n].rm_so );
299 ts += pmatch[n].rm_eo - pmatch[n].rm_so;
300 } else {
301 size += pmatch[n].rm_eo - pmatch[n].rm_so;
302 }
303 }
304 } else {
305 if ( pass ) {
306 *ts++ = '$';
307 } else {
308 size++;
309 }
310 }
311 break;
312 case '\\':
313 switch( c = *++hs ) {
314 case 0: c = '\\'; hs--; break;
315 case 'b': c = '\b'; break;
316 case 'f': c = '\f'; break;
317 case 'n': c = '\n'; break;
318 case 'r': c = '\r'; break;
319 case 't': c = '\t'; break;
320 }
321 if ( pass ) {
322 *ts++ = c;
323 } else {
324 size++;
325 }
326 break;
327 default:
328 if ( pass ) {
329 *ts++ = *hs;
330 } else {
331 size++;
332 }
333 break;
334 }
335 }
336 if ( pass ) {
337 *ts = '\0';
338 } else {
339 if ( ( retVal = malloc( size + 1 ) ) == NULL ) {
340 perror( prgname );
341 exit( 1 );
342 }
343 }
344 }
345
346 return retVal;
347 }
348
349 static struct list {
350 struct list *next;
351 char *name;
352 FILE *file;
353 } *IDXfiles = NULL;
354
355 static FILE *findsuffix( const char *name ) {
356 struct list *run;
357 for ( run = IDXfiles;
358 run != NULL;
359 run = run->next ) {
360 const char *suffix = strchr( run->name, '\0' );
361 suffix -= 4; /* .idx */
362 suffix -= strlen( name );
363 if ( ( suffix >= run->name )
364 && !strncmp( suffix, name, strlen( name ) ) ) {
365 return run->file;
366 }
367 }
368 return NULL;
369 }
370
371 static struct list *addentry( char *name, FILE *file ) {
372 struct list *newentry = malloc( sizeof( struct list ) );
373 if ( !newentry ) {
374 perror( prgname );
375 exit( 1 );
376 }
377 newentry->name = name;
378 newentry->file = file;
379 if ( IDXfiles == NULL ) {
380 newentry->next = NULL;
381 } else {
382 newentry->next = IDXfiles;
383 }
384 IDXfiles = newentry;
385 return IDXfiles;
386 }
387
388 static FILE *newentry( const char *suffix ) {
389 char *name;
390 FILE *file;
391
392 if ( ( name = malloc( strlen( Jobname ) + strlen( suffix ) + 5 ) ) == NULL ) {
393 perror( prgname );
394 exit( 1 );
395 }
396
397 strcpy( name, Jobname );
398 strcat( name, suffix );
399 strcat( name, ".idx" );
400
401 if ( ( file = fopen( name, "w" ) ) == NULL ) {
402 perror( name );
403 exit( errno );
404 }
405
406 if ( Verbose > 1 )
407 printf( "New index file %s\n", name );
408 addentry( name, file );
409 return file;
410 }
411
412 static void CloseAllIDX( void ) {
413 struct list *run;
414 for ( run = IDXfiles; run != NULL; run = run->next )
415 if ( fclose( run->file ) ) {
416 perror( run->name );
417 exit( 1 );
418 }
419 }
420
421 static void CallMakeIndex( void ) {
422 char **argv;
423 int idx;
424 struct list *run, *next;
425
426 if ( ( argv = calloc( MakeIndexArgc + 3, sizeof( char * ) ) ) == NULL ) {
427 perror( prgname );
428 exit( 0 );
429 }
430
431 for ( idx = 0; idx < MakeIndexArgc; idx++ )
432 argv[idx+1] = MakeIndexArgs[idx];
433
434 idx++;
435 argv[0] = MakeIndex;
436
437 for ( run = IDXfiles; run != NULL; run = next ) {
438 pid_t fret;
439 int status;
440 argv[idx] = run->name;
441
442 if ( ( fret = fork() ) == 0 ) {
443 /* This is the child */
444 if ( Verbose > 1 ) {
445 int i;
446 for ( i = 0; argv[i] != NULL; i++ )
447 printf( "\"%s\" ", argv[i] );
448 printf( "\n" );
449 }
450 if ( execvp( MakeIndex, argv ) ) {
451 perror( MakeIndex );
452 exit( 1 );
453 }
454 } else if ( fret == -1 ) {
455 perror( prgname );
456 exit( 1 );
457 } else {
458 /* Parent */
459 waitpid( fret, &status, 0 );
460 if ( ! WIFEXITED( status ) ) {
461 fprintf( stderr, "%s terminated abnormally!\n", MakeIndex );
462 exit( 1 );
463 }
464 }
465
466 free( (void *)run->name );
467 next = run->next;
468 free( run );
469 }
470
471
472 }
473
474 static void ProcessIDXFile( void ) {
475 FILE *fIDX;
476 char *line;
477 regex_t preg;
478 int retVal;
479 int buffersize = 2050;
480 regmatch_t pmatch[10];
481
482 if ( ( line = malloc( buffersize ) ) == NULL ) {
483 perror( prgname );
484 exit( errno );
485 }
486
487 if ( ( retVal = regcomp( &preg, Identify, REG_EXTENDED ) ) != 0 ) {
488 regerror( retVal, &preg, line, buffersize );
489 fprintf( stderr, "Error at identify: %s\n", line );
490 free( line );
491 exit( 1 );
492 }
493
494 if ( ( fIDX = fopen( IDX, "r" ) ) == NULL ) {
495 if ( !strcmp ( IDX, Jobname ) ) {
496 if ( ( IDX = malloc( strlen( Jobname + 5 ) ) ) == NULL ) {
497 perror( prgname );
498 regfree( &preg );
499 free( line );
500 exit( errno );
501 }
502 strcpy( IDX, Jobname );
503 strcat( IDX, ".idx" );
504 if ( ( fIDX = fopen( IDX, "r" ) ) == NULL ) {
505 fprintf( stderr,
506 "Can read neither file %s nor file %s\n",
507 Jobname, IDX );
508 regfree( &preg );
509 free( line );
510 exit( 1 );
511 }
512 } else {
513 fprintf( stderr,
514 "Can't red file %s\n", IDX );
515 regfree( &preg );
516 free( line );
517 exit( 1 );
518 }
519 }
520
521 while ( fgets( line, buffersize, fIDX ) != NULL ) {
522 char *hs;
523 char *result, *suffix;
524 FILE *f;
525 while ( ( ( hs = strchr( line, '\n' ) ) == NULL )
526 && ! feof( fIDX ) ) {
527 if ( ( line = realloc( line, 2 * buffersize ) ) == NULL ) {
528 perror( prgname );
529 exit( errno );
530 }
531 if ( fgets( line + buffersize - 1, buffersize + 1, fIDX ) == NULL )
532 break;
533 buffersize *= 2;
534 }
535 if ( ferror( fIDX ) )
536 break;
537 if ( ( hs = strchr( line, '\n' ) ) != NULL ) // remove newline
538 *hs = 0;
539 if ( ( retVal = regexec( &preg, line, 10, pmatch, 0 ) ) == 0 ) {
540 result = regsub( line, ResultIs, 10, pmatch );
541 suffix = regsub( line, SuffixIs, 10, pmatch );
542 } else {
543 int i;
544 result = strdup( line );
545 for ( i = 0; i < 10; i++ ) {
546 pmatch[i].rm_so = 0;
547 pmatch[i].rm_eo = 3;
548 }
549 suffix = regsub( "idx", SuffixIs, 10, pmatch );
550 }
551 if ( !result || !suffix ) {
552 perror( prgname );
553 exit ( 1 );
554 }
555
556 if ( ( f = findsuffix( suffix ) ) == NULL )
557 f = newentry( suffix );
558 fprintf( f, "%s\n", result );
559
560 free( suffix );
561 free( result );
562 }
563
564 regfree( &preg );
565 free( line );
566 if ( ferror( fIDX ) ) {
567 perror( IDX );
568 fclose( fIDX );
569 exit( errno );
570 }
571 fclose( fIDX );
572
573 CloseAllIDX();
574
575 CallMakeIndex();
576 }
577
578 int main( int argc, char * const argv[] ) {
579 int retVal = 0;
580
581 ScanArguments( argc, argv );
582
583 if ( Verbose > 0 ) {
584 show_version();
585 if ( Verbose > 9 ) {
586 int i;
587 printf( "Identify: \"%s\"\n", Identify );
588 printf( "ResultIs: \"%s\"\n", ResultIs );
589 printf( "SuffixIs: \"%s\"\n", SuffixIs );
590 printf( "IDX: \"%s\"\n", IDX );
591 printf( "Jobname: \"%s\"\n", Jobname );
592 printf( "MakeIndex: \"%s\"", MakeIndex );
593 for ( i = 0; i < MakeIndexArgc; i++ )
594 printf( " \"%s\"", MakeIndexArgs[i] );
595 printf( "\n" );
596 }
597 }
598
599 ProcessIDXFile();
600
601 return retVal;
602 }