6
This is skeleton code for the \.{idxmerge} utility. For detailed usage
7
instructions, please see the \.{texinfo} documentation.
9
Apologies for the lack of comments. Time is short, and this particular
10
subproject isn't really finished.
17
@ The following routine works with |qsort| for sorting file names.
20
fcmp(const void *arg1, const void *arg2)
22
return strcmp(*(char **)arg1, *(char **)arg2);
28
@d CALLOC(n, type) (type *)calloc(n, sizeof(type))
31
main(int num_args, char *args[])
33
char **file_name, *root_name = NULL, *out_name = NULL;
35
char *name_list = NULL;
39
int i, k, nfiles, nlines;
40
FILE *fp_in, *fp_out, *fp_names;
44
puts("Usage: idxmerge [-o output_file_name] [-s] file1 [file2] ...");
50
/* Allocate the file-name list. There can't be more file names than the
51
total number of command-line arguments. */
52
file_name = CALLOC(num_args, char *);
54
for(i=1; i<num_args; i++)
59
@<Process command-line option@>@;
61
file_name[nfiles++] = pa;
66
puts("! No input files; nothing done.");
71
name_list = "idx-names.tex";
76
fp_out = stdout; // Unix convention.
78
fp_out = fopen(out_name, "w");
81
qsort(file_name, nfiles, sizeof(char *), fcmp);
83
printf("Writing merged output to `%s', file names to `%s'...",
87
fp_names = fopen(name_list, "w");
91
for(k=0; k<nfiles; k++)
92
@<Process one file@>@;
95
"IDXMERGE: %i files, %i index entries.\n", nfiles, nlines);
108
/* Specify name of output file-name list. */
109
name_list = CALLOC(strlen(pa)+1, char);
110
strcpy(name_list, pa);
114
/* Set name of output file (no \.{.idx}). */
115
root_name = CALLOC((root_len=strlen(pa))+1, char);
116
strcpy(root_name, pa);
117
out_name = CALLOC(root_len+5, char);
118
strcpy(out_name, root_name);
119
strcpy(out_name+root_len, ".idx");
123
name_list = CALLOC(root_len + 12, char);
124
strcpy(name_list, root_name);
125
strcpy(name_list + root_len, "-names.tex");
130
/* Give nondefault value of \.{makeindex.pg}. */
131
pg = CALLOC(strlen(pa)+2, char);
137
/* Turn off sorting of file names. */
138
sort_names = !sort_names;
142
printf("! Invalid command-line option `-%s' ignored.", pa-1);
152
fp_in = fopen(file_name[k], "r");
156
printf("! Can't open input file #%i, `%s'.\n", k, file_name[k]);
160
while(fgets(buf, BUF_LEN, fp_in))
167
p += pg_len + 1; // Address of the right brace in \.{|pg\{\}\dots}.
170
fprintf(fp_out, "%s%i%c%s", buf, k+1, c, p+1);
176
fprintf(fp_names, "\\idxname{%i}{%s}\n", k+1, file_name[k]);