#include <stdio.h>
#include <ctype.h>
#include "xref.h"
struct codeword_entry *root;
struct counters {
int levels[5];
int table_number;
int figure_number;
} counters = {{0, 0, 0, 0, 0}, 0, 0};
int outline_file_read = FALSE;
int appendix_seen = FALSE;
int start_chapter = 1;
int start_appendix = 1;
int plain_preprocessor = FALSE;
main(argc, argv)
int argc;
char *argv[];
{
int file_number;
int ap_ix;
command_name = argv[0];
argc--;
argv++;
root = NULL;
if (argc < 1) {
fprintf(stderr, "Usage: %s [-sbegin] [-o outline] filename ...\n", command_name);
exit(1);
}
while (argc > 0 && argv[0][0] == '-') {
if (argv[0][1] == 's') {
if (isdigit(argv[0][2]))
start_chapter = atoi(&argv[0][2]);
else if (isalpha(argv[0][2])) {
start_appendix = 0;
for (ap_ix = 2; isalpha(argv[0][ap_ix]); ap_ix++) {
if (isupper(argv[0][ap_ix]))
start_appendix = 26*start_appendix
+ argv[0][ap_ix] - 'A' + 1;
else
start_appendix = 26*start_appendix
+ toupper(argv[0][ap_ix]) - 'A' + 1;
}
}
} else if (argv[0][1] == 'o') {
current_filename = argv[1];
process_current_file(current_filename, GATHER_REFERENCES);
outline_file_read = TRUE;
argc--;
argv++;
} else if (argv[0][1] == 'p') {
plain_preprocessor = TRUE;
} else {
fprintf(stderr, "%s: unknown option -%c\n",
command_name, argv[0][1]);
exit(1);
}
argc--;
argv++;
}
if (argc < 1) {
fprintf(stderr, "Usage: %s [-sbegin] [-o outline] filename ...\n", command_name);
exit(1);
}
if (!outline_file_read) {
for (file_number = 0; file_number < argc; file_number++) {
current_filename = argv[file_number];
process_current_file(current_filename, GATHER_REFERENCES);
}
}
for (file_number = 0; file_number < argc; file_number++) {
current_filename = argv[file_number];
process_current_file(current_filename, SUBSTITUTE_REFERENCES);
}
#ifdef DEBUG
dump_codewords(root);
#endif
}
process_current_file(filename, phase)
char *filename;
int phase;
{
FILE *current_file;
char line[MAXLINE];
char new_filename[BUFSIZ];
if ((current_file = fopen(filename, "r")) == NULL) {
fprintf(stderr, "%s: cannot access %s\n", command_name, filename);
exit(1);
}
line_number = 0;
while (fgets(line, sizeof(line), current_file) != NULL) {
line_number++;
if (source_another_file(line, new_filename)) {
process_current_file(new_filename, phase);
} else {
if (phase == GATHER_REFERENCES)
build_tree(line);
else
fill_in_references(line);
}
}
fclose(current_file);
}
source_another_file(line, filename)
char *line;
char *filename;
{
int char_p;
char *file_pos;
char_p = 0;
if (plain_preprocessor == TRUE)
return(FALSE);
if (line[char_p] == '\0' || line[char_p] == '\n')
return(FALSE);
if (line[char_p++] != '.')
return(FALSE);
while (line[char_p] == ' ' || line[char_p] == '\t')
char_p++;
if (line[char_p] == '\0' || line[char_p] == '\n')
return(FALSE);
if (line[char_p] != 's')
return(FALSE);
char_p++;
if (line[char_p] == '\0' || line[char_p] == '\n')
return(FALSE);
if (line[char_p] != 'o')
return(FALSE);
char_p++;
while (line[char_p] == ' ' || line[char_p] == '\t')
char_p++;
if (line[char_p] == '\0' || line[char_p] == '\n')
return(FALSE);
file_pos = filename;
while (line[char_p] != '\n' && line[char_p] != ' ' && line[char_p] != '\t')
*file_pos++ = line[char_p++];
*file_pos = '\0';
if (file_pos == filename)
return(FALSE);
else
return(TRUE);
}
build_tree(line)
char *line;
{
int char_p;
int entry_type;
struct codeword_entry *node;
char_p = 0;
if ((entry_type = get_codeword_type(line, &char_p)) == 0)
return;
if ((node = build_codeword_entry(line, &char_p, entry_type, &counters)) == NULL)
return;
node->entry_type = entry_type;
if (root == NULL)
root = node;
else
node = insert_codeword_entry(root, node);
}
fill_in_references(line)
char *line;
{
int char_p;
char out_line[MAXLINE];
char_p = 0;
if (try_reference_line(line, &char_p) == FALSE) {
strcpy(out_line, line);
fputs(out_line, stdout);
return;
}
substitute_references(line, &char_p);
}
struct codeword_entry *
build_codeword_entry(line, char_p, entry_type, counters)
char *line;
int *char_p;
int entry_type;
struct counters *counters;
{
struct codeword_entry *node;
char current_token[MAXLINE];
char *field;
int clear_level, i;
node = new(struct codeword_entry);
node->entry_type = entry_type;
if (entry_type == HEADING) {
get_token(line, char_p, current_token, SKIP_SPACES);
if (strcmp(current_token, "C") == 0 || strcmp(current_token, "1") == 0) {
node->appendix = FALSE;
if (strcmp(current_token, "1") == 0)
document_type = MINOR_SECTIONED;
else {
document_type = MAJOR_SECTIONED;
counters->table_number = 0;
counters->figure_number = 0;
}
if (start_chapter == 0)
counters->levels[0]++;
else {
counters->levels[0] = start_chapter;
start_chapter = 0;
}
clear_level = 1;
}
else if (strcmp(current_token, "A") == 0 || strcmp(current_token, "PA") == 0) {
node->appendix = TRUE;
if (strcmp(current_token, "PA") == 0)
document_type = MINOR_SECTIONED;
else {
document_type = MAJOR_SECTIONED;
counters->table_number = 0;
counters->figure_number = 0;
}
if (appendix_seen)
counters->levels[0]++;
else {
appendix_seen = TRUE;
if (start_appendix == 0)
counters->levels[0] = 1;
else {
counters->levels[0] = start_appendix;
start_appendix = 0;
}
}
clear_level = 1;
}
else if (strcmp(current_token, "2") == 0) {
counters->levels[1]++;
clear_level = 2;
}
else if (strcmp(current_token, "3") == 0) {
counters->levels[2]++;
clear_level = 3;
}
else if (strcmp(current_token, "4") == 0) {
counters->levels[3]++;
clear_level = 4;
}
else if (strcmp(current_token, "5") == 0) {
counters->levels[4]++;
clear_level = 5;
}
for (i = clear_level; i < 5; i++)
counters->levels[i] = 0;
}
if (entry_type == TABLE) {
counters->table_number++;
}
if (entry_type == FIGURE) {
counters->figure_number++;
}
if (entry_type == CROSSREF) {
free(node);
return(NULL);
}
get_token(line, char_p, current_token, SKIP_SPACES);
node->title = strdup(current_token);
get_token(line, char_p, current_token, SKIP_SPACES);
if (strcmp(current_token, "") == 0) {
free(node);
return(NULL);
} else {
node->codeword = strdup(current_token);
node->appendix = appendix_seen;
node->h1_counter = counters->levels[0];
node->h2_counter = counters->levels[1];
node->h3_counter = counters->levels[2];
node->h4_counter = counters->levels[3];
node->h5_counter = counters->levels[4];
node->table_number = counters->table_number;
node->figure_number = counters->figure_number;
return(node);
}
}