1 /* Permuted index for GNU, with keywords in their context.
2 Copyright (C) 1990, 1991, 1993, 1998-2003 Free Software Foundation, Inc.
3 François Pinard <pinard@iro.umontreal.ca>, 1988.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 François Pinard <pinard@iro.umontreal.ca> */
25 #include <sys/types.h>
29 #include "bumpalloc.h"
34 /* The official name of this program (e.g., no `g' prefix). */
35 #define PROGRAM_NAME "ptx"
37 #define AUTHORS "François Pinard"
39 /* Number of possible characters in a byte. */
40 #define CHAR_SET_SIZE 256
42 #define ISODIGIT(C) ((C) >= '0' && (C) <= '7')
43 #define HEXTOBIN(C) ((C) >= 'a' && (C) <= 'f' ? (C)-'a'+10 \
44 : (C) >= 'A' && (C) <= 'F' ? (C)-'A'+10 : (C)-'0')
45 #define OCTTOBIN(C) ((C) - '0')
47 /* Debugging the memory allocator. */
50 # define MALLOC_FUNC_CHECK 1
54 /* Global definitions. */
56 /* Reallocation step when swallowing non regular files. The value is not
57 the actual reallocation step, but its base two logarithm. */
58 #define SWALLOW_REALLOC_LOG 12
60 /* Imported from "regex.c". */
63 /* The name this program was run with. */
66 /* Program options. */
70 UNKNOWN_FORMAT, /* output format still unknown */
71 DUMB_FORMAT, /* output for a dumb terminal */
72 ROFF_FORMAT, /* output for `troff' or `nroff' */
73 TEX_FORMAT /* output for `TeX' or `LaTeX' */
76 int gnu_extensions = 1; /* trigger all GNU extensions */
77 int auto_reference = 0; /* references are `file_name:line_number:' */
78 int input_reference = 0; /* references at beginning of input lines */
79 int right_reference = 0; /* output references after right context */
80 int line_width = 72; /* output line width in characters */
81 int gap_size = 3; /* number of spaces between output fields */
82 const char *truncation_string = "/";
83 /* string used to mark line truncations */
84 const char *macro_name = "xx"; /* macro name for roff or TeX output */
85 enum Format output_format = UNKNOWN_FORMAT;
88 int ignore_case = 0; /* fold lower to upper case for sorting */
89 const char *context_regex_string = NULL;
90 /* raw regex for end of context */
91 const char *word_regex_string = NULL;
92 /* raw regex for a keyword */
93 const char *break_file = NULL; /* name of the `Break characters' file */
94 const char *only_file = NULL; /* name of the `Only words' file */
95 const char *ignore_file = NULL; /* name of the `Ignore words' file */
97 /* A BLOCK delimit a region in memory of arbitrary size, like the copy of a
98 whole file. A WORD is something smaller, its length should fit in a
99 short integer. A WORD_TABLE may contain several WORDs. */
103 char *start; /* pointer to beginning of region */
104 char *end; /* pointer to end + 1 of region */
110 char *start; /* pointer to beginning of region */
111 short size; /* length of the region */
117 WORD *start; /* array of WORDs */
118 size_t length; /* number of entries */
122 /* Pattern description tables. */
124 /* For each character, provide its folded equivalent. */
125 unsigned char folded_chars[CHAR_SET_SIZE];
127 /* For each character, indicate if it is part of a word. */
128 char syntax_table[CHAR_SET_SIZE];
129 char *re_syntax_table = syntax_table;
131 /* Compiled regex for end of context. */
132 struct re_pattern_buffer *context_regex;
134 /* End of context pattern register indices. */
135 struct re_registers context_regs;
137 /* Compiled regex for a keyword. */
138 struct re_pattern_buffer *word_regex;
140 /* Keyword pattern register indices. */
141 struct re_registers word_regs;
143 /* A word characters fastmap is used only when no word regexp has been
144 provided. A word is then made up of a sequence of one or more characters
145 allowed by the fastmap. Contains !0 if character allowed in word. Not
146 only this is faster in most cases, but it simplifies the implementation
147 of the Break files. */
148 char word_fastmap[CHAR_SET_SIZE];
150 /* Maximum length of any word read. */
151 int maximum_word_length;
153 /* Maximum width of any reference used. */
154 int reference_max_width;
156 /* Ignore and Only word tables. */
158 WORD_TABLE ignore_table; /* table of words to ignore */
159 WORD_TABLE only_table; /* table of words to select */
161 #define ALLOC_NEW_WORD(table) \
162 BUMP_ALLOC ((table)->start, (table)->length, 8, WORD)
164 /* Source text table, and scanning macros. */
166 int number_input_files; /* number of text input files */
167 int total_line_count; /* total number of lines seen so far */
168 const char **input_file_name; /* array of text input file names */
169 int *file_line_count; /* array of `total_line_count' values at end */
171 BLOCK text_buffer; /* file to study */
172 char *text_buffer_maxend; /* allocated end of text_buffer */
174 /* SKIP_NON_WHITE used only for getting or skipping the reference. */
176 #define SKIP_NON_WHITE(cursor, limit) \
177 while (cursor < limit && !ISSPACE(*cursor)) \
180 #define SKIP_WHITE(cursor, limit) \
181 while (cursor < limit && ISSPACE(*cursor)) \
184 #define SKIP_WHITE_BACKWARDS(cursor, start) \
185 while (cursor > start && ISSPACE(cursor[-1])) \
188 #define SKIP_SOMETHING(cursor, limit) \
189 if (word_regex_string) \
192 count = re_match (word_regex, cursor, limit - cursor, 0, NULL); \
193 cursor += count <= 0 ? 1 : count; \
195 else if (word_fastmap[(unsigned char) *cursor]) \
196 while (cursor < limit && word_fastmap[(unsigned char) *cursor]) \
201 /* Occurrences table.
203 The `keyword' pointer provides the central word, which is surrounded
204 by a left context and a right context. The `keyword' and `length'
205 field allow full 8-bit characters keys, even including NULs. At other
206 places in this program, the name `keyafter' refers to the keyword
207 followed by its right context.
209 The left context does not extend, towards the beginning of the file,
210 further than a distance given by the `left' value. This value is
211 relative to the keyword beginning, it is usually negative. This
212 insures that, except for white space, we will never have to backward
213 scan the source text, when it is time to generate the final output
216 The right context, indirectly attainable through the keyword end, does
217 not extend, towards the end of the file, further than a distance given
218 by the `right' value. This value is relative to the keyword
219 beginning, it is usually positive.
221 When automatic references are used, the `reference' value is the
222 overall line number in all input files read so far, in this case, it
223 is of type (int). When input references are used, the `reference'
224 value indicates the distance between the keyword beginning and the
225 start of the reference field, it is of type (DELTA) and usually
228 typedef short DELTA; /* to hold displacement within one context */
232 WORD key; /* description of the keyword */
233 DELTA left; /* distance to left context start */
234 DELTA right; /* distance to right context end */
235 int reference; /* reference descriptor */
239 /* The various OCCURS tables are indexed by the language. But the time
240 being, there is no such multiple language support. */
242 OCCURS *occurs_table[1]; /* all words retained from the read text */
243 size_t number_of_occurs[1]; /* number of used slots in occurs_table */
245 #define ALLOC_NEW_OCCURS(language) \
246 BUMP_ALLOC (occurs_table[language], number_of_occurs[language], 9, OCCURS)
248 /* Communication among output routines. */
250 /* Indicate if special output processing is requested for each character. */
251 char edited_flag[CHAR_SET_SIZE];
253 int half_line_width; /* half of line width, reference excluded */
254 int before_max_width; /* maximum width of before field */
255 int keyafter_max_width; /* maximum width of keyword-and-after field */
256 int truncation_string_length; /* length of string used to flag truncation */
258 /* When context is limited by lines, wraparound may happen on final output:
259 the `head' pointer gives access to some supplementary left context which
260 will be seen at the end of the output line, the `tail' pointer gives
261 access to some supplementary right context which will be seen at the
262 beginning of the output line. */
264 BLOCK tail; /* tail field */
265 int tail_truncation; /* flag truncation after the tail field */
267 BLOCK before; /* before field */
268 int before_truncation; /* flag truncation before the before field */
270 BLOCK keyafter; /* keyword-and-after field */
271 int keyafter_truncation; /* flag truncation after the keyafter field */
273 BLOCK head; /* head field */
274 int head_truncation; /* flag truncation before the head field */
276 BLOCK reference; /* reference field for input reference mode */
278 /* Miscellaneous routines. */
280 /*------------------------------------------------------.
281 | Duplicate string STRING, while evaluating \-escapes. |
282 `------------------------------------------------------*/
284 /* Loosely adapted from GNU sh-utils printf.c code. */
287 copy_unescaped_string (const char *string)
289 char *result; /* allocated result */
290 char *cursor; /* cursor in result */
291 int value; /* value of \nnn escape */
292 int length; /* length of \nnn escape */
294 result = xmalloc (strlen (string) + 1);
303 case 'x': /* \xhhh escape, 3 chars maximum */
305 for (length = 0, string++;
306 length < 3 && ISXDIGIT (*string);
308 value = value * 16 + HEXTOBIN (*string);
318 case '0': /* \0ooo escape, 3 chars maximum */
320 for (length = 0, string++;
321 length < 3 && ISODIGIT (*string);
323 value = value * 8 + OCTTOBIN (*string);
327 case 'a': /* alert */
336 case 'b': /* backspace */
341 case 'c': /* cancel the rest of the output */
346 case 'f': /* form feed */
351 case 'n': /* new line */
356 case 'r': /* carriage return */
361 case 't': /* horizontal tab */
366 case 'v': /* vertical tab */
377 *cursor++ = *string++;
382 *cursor++ = *string++;
388 /*-------------------------------------------------------------------.
389 | Compile the regex represented by STRING, diagnose and abort if any |
390 | error. Returns the compiled regex structure. |
391 `-------------------------------------------------------------------*/
393 static struct re_pattern_buffer *
394 alloc_and_compile_regex (const char *string)
396 struct re_pattern_buffer *pattern; /* newly allocated structure */
397 const char *message; /* error message returned by regex.c */
399 pattern = (struct re_pattern_buffer *)
400 xmalloc (sizeof (struct re_pattern_buffer));
401 memset (pattern, 0, sizeof (struct re_pattern_buffer));
403 pattern->buffer = NULL;
404 pattern->allocated = 0;
405 pattern->translate = ignore_case ? (char *) folded_chars : NULL;
406 pattern->fastmap = (char *) xmalloc ((size_t) CHAR_SET_SIZE);
408 message = re_compile_pattern (string, (int) strlen (string), pattern);
410 error (EXIT_FAILURE, 0, _("%s (for regexp `%s')"), message, string);
412 /* The fastmap should be compiled before `re_match'. The following
413 call is not mandatory, because `re_search' is always called sooner,
414 and it compiles the fastmap if this has not been done yet. */
416 re_compile_fastmap (pattern);
418 /* Do not waste extra allocated space. */
420 if (pattern->allocated > pattern->used)
423 = (unsigned char *) xrealloc (pattern->buffer, (size_t) pattern->used);
424 pattern->allocated = pattern->used;
430 /*------------------------------------------------------------------------.
431 | This will initialize various tables for pattern match and compiles some |
433 `------------------------------------------------------------------------*/
436 initialize_regex (void)
438 int character; /* character value */
440 /* Initialize the regex syntax table. */
442 for (character = 0; character < CHAR_SET_SIZE; character++)
443 syntax_table[character] = ISALPHA (character) ? Sword : 0;
445 /* Initialize the case folding table. */
448 for (character = 0; character < CHAR_SET_SIZE; character++)
449 folded_chars[character] = TOUPPER (character);
451 /* Unless the user already provided a description of the end of line or
452 end of sentence sequence, select an end of line sequence to compile.
453 If the user provided an empty definition, thus disabling end of line
454 or sentence feature, make it NULL to speed up tests. If GNU
455 extensions are enabled, use end of sentence like in GNU emacs. If
456 disabled, use end of lines. */
458 if (context_regex_string)
460 if (!*context_regex_string)
461 context_regex_string = NULL;
463 else if (gnu_extensions && !input_reference)
464 context_regex_string = "[.?!][]\"')}]*\\($\\|\t\\| \\)[ \t\n]*";
466 context_regex_string = "\n";
468 if (context_regex_string)
469 context_regex = alloc_and_compile_regex (context_regex_string);
471 /* If the user has already provided a non-empty regexp to describe
472 words, compile it. Else, unless this has already been done through
473 a user provided Break character file, construct a fastmap of
474 characters that may appear in a word. If GNU extensions enabled,
475 include only letters of the underlying character set. If disabled,
476 include almost everything, even punctuations; stop only on white
479 if (word_regex_string && *word_regex_string)
480 word_regex = alloc_and_compile_regex (word_regex_string);
481 else if (!break_file)
488 for (character = 0; character < CHAR_SET_SIZE; character++)
489 word_fastmap[character] = ISALPHA (character) ? 1 : 0;
494 /* Simulate [^ \t\n]+. */
496 memset (word_fastmap, 1, CHAR_SET_SIZE);
497 word_fastmap[' '] = 0;
498 word_fastmap['\t'] = 0;
499 word_fastmap['\n'] = 0;
504 /*------------------------------------------------------------------------.
505 | This routine will attempt to swallow a whole file name FILE_NAME into a |
506 | contiguous region of memory and return a description of it into BLOCK. |
507 | Standard input is assumed whenever FILE_NAME is NULL, empty or "-". |
509 | Previously, in some cases, white space compression was attempted while |
510 | inputting text. This was defeating some regexps like default end of |
511 | sentence, which checks for two consecutive spaces. If white space |
512 | compression is ever reinstated, it should be in output routines. |
513 `------------------------------------------------------------------------*/
516 swallow_file_in_memory (const char *file_name, BLOCK *block)
518 int file_handle; /* file descriptor number */
519 struct stat stat_block; /* stat block for file */
520 size_t allocated_length; /* allocated length of memory buffer */
521 size_t used_length; /* used length in memory buffer */
522 int read_length; /* number of character gotten on last read */
524 /* As special cases, a file name which is NULL or "-" indicates standard
525 input, which is already opened. In all other cases, open the file from
527 bool using_stdin = !file_name || !*file_name || strcmp (file_name, "-") == 0;
529 file_handle = STDIN_FILENO;
531 if ((file_handle = open (file_name, O_RDONLY)) < 0)
532 error (EXIT_FAILURE, errno, "%s", file_name);
534 /* If the file is a plain, regular file, allocate the memory buffer all at
535 once and swallow the file in one blow. In other cases, read the file
536 repeatedly in smaller chunks until we have it all, reallocating memory
537 once in a while, as we go. */
539 if (fstat (file_handle, &stat_block) < 0)
540 error (EXIT_FAILURE, errno, "%s", file_name);
542 if (S_ISREG (stat_block.st_mode))
544 size_t in_memory_size;
546 block->start = (char *) xmalloc ((size_t) stat_block.st_size);
548 if ((in_memory_size = read (file_handle,
549 block->start, (size_t) stat_block.st_size))
550 != stat_block.st_size)
553 /* On MSDOS, in memory size may be smaller than the file
554 size, because of end of line conversions. But it can
555 never be smaller than half the file size, because the
556 minimum is when all lines are empty and terminated by
558 if (in_memory_size != (size_t)-1
559 && in_memory_size >= stat_block.st_size / 2)
560 block->start = (char *) xrealloc (block->start, in_memory_size);
562 #endif /* not MSDOS */
564 error (EXIT_FAILURE, errno, "%s", file_name);
566 block->end = block->start + in_memory_size;
570 block->start = (char *) xmalloc ((size_t) 1 << SWALLOW_REALLOC_LOG);
572 allocated_length = (1 << SWALLOW_REALLOC_LOG);
574 while (read_length = read (file_handle,
575 block->start + used_length,
576 allocated_length - used_length),
579 used_length += read_length;
580 if (used_length == allocated_length)
582 allocated_length += (1 << SWALLOW_REALLOC_LOG);
584 = (char *) xrealloc (block->start, allocated_length);
589 error (EXIT_FAILURE, errno, "%s", file_name);
591 block->end = block->start + used_length;
594 /* Close the file, but only if it was not the standard input. */
596 if (! using_stdin && close (file_handle) != 0)
597 error (EXIT_FAILURE, errno, "%s", file_name);
600 /* Sort and search routines. */
602 /*--------------------------------------------------------------------------.
603 | Compare two words, FIRST and SECOND, and return 0 if they are identical. |
604 | Return less than 0 if the first word goes before the second; return |
605 | greater than 0 if the first word goes after the second. |
607 | If a word is indeed a prefix of the other, the shorter should go first. |
608 `--------------------------------------------------------------------------*/
611 compare_words (const void *void_first, const void *void_second)
613 #define first ((const WORD *) void_first)
614 #define second ((const WORD *) void_second)
615 int length; /* minimum of two lengths */
616 int counter; /* cursor in words */
617 int value; /* value of comparison */
619 length = first->size < second->size ? first->size : second->size;
623 for (counter = 0; counter < length; counter++)
625 value = (folded_chars [(unsigned char) (first->start[counter])]
626 - folded_chars [(unsigned char) (second->start[counter])]);
633 for (counter = 0; counter < length; counter++)
635 value = ((unsigned char) first->start[counter]
636 - (unsigned char) second->start[counter]);
642 return first->size - second->size;
647 /*-----------------------------------------------------------------------.
648 | Decides which of two OCCURS, FIRST or SECOND, should lexicographically |
649 | go first. In case of a tie, preserve the original order through a |
650 | pointer comparison. |
651 `-----------------------------------------------------------------------*/
654 compare_occurs (const void *void_first, const void *void_second)
656 #define first ((const OCCURS *) void_first)
657 #define second ((const OCCURS *) void_second)
660 value = compare_words (&first->key, &second->key);
661 return value == 0 ? first->key.start - second->key.start : value;
666 /*------------------------------------------------------------.
667 | Return !0 if WORD appears in TABLE. Uses a binary search. |
668 `------------------------------------------------------------*/
671 search_table (WORD *word, WORD_TABLE *table)
673 int lowest; /* current lowest possible index */
674 int highest; /* current highest possible index */
675 int middle; /* current middle index */
676 int value; /* value from last comparison */
679 highest = table->length - 1;
680 while (lowest <= highest)
682 middle = (lowest + highest) / 2;
683 value = compare_words (word, table->start + middle);
685 highest = middle - 1;
694 /*---------------------------------------------------------------------.
695 | Sort the whole occurs table in memory. Presumably, `qsort' does not |
696 | take intermediate copies or table elements, so the sort will be |
697 | stabilized throughout the comparison routine. |
698 `---------------------------------------------------------------------*/
701 sort_found_occurs (void)
704 /* Only one language for the time being. */
706 qsort (occurs_table[0], number_of_occurs[0], sizeof (OCCURS),
710 /* Parameter files reading routines. */
712 /*----------------------------------------------------------------------.
713 | Read a file named FILE_NAME, containing a set of break characters. |
714 | Build a content to the array word_fastmap in which all characters are |
715 | allowed except those found in the file. Characters may be repeated. |
716 `----------------------------------------------------------------------*/
719 digest_break_file (const char *file_name)
721 BLOCK file_contents; /* to receive a copy of the file */
722 char *cursor; /* cursor in file copy */
724 swallow_file_in_memory (file_name, &file_contents);
726 /* Make the fastmap and record the file contents in it. */
728 memset (word_fastmap, 1, CHAR_SET_SIZE);
729 for (cursor = file_contents.start; cursor < file_contents.end; cursor++)
730 word_fastmap[(unsigned char) *cursor] = 0;
735 /* If GNU extensions are enabled, the only way to avoid newline as
736 a break character is to write all the break characters in the
737 file with no newline at all, not even at the end of the file.
738 If disabled, spaces, tabs and newlines are always considered as
739 break characters even if not included in the break file. */
741 word_fastmap[' '] = 0;
742 word_fastmap['\t'] = 0;
743 word_fastmap['\n'] = 0;
746 /* Return the space of the file, which is no more required. */
748 free (file_contents.start);
751 /*-----------------------------------------------------------------------.
752 | Read a file named FILE_NAME, containing one word per line, then |
753 | construct in TABLE a table of WORD descriptors for them. The routine |
754 | swallows the whole file in memory; this is at the expense of space |
755 | needed for newlines, which are useless; however, the reading is fast. |
756 `-----------------------------------------------------------------------*/
759 digest_word_file (const char *file_name, WORD_TABLE *table)
761 BLOCK file_contents; /* to receive a copy of the file */
762 char *cursor; /* cursor in file copy */
763 char *word_start; /* start of the current word */
765 swallow_file_in_memory (file_name, &file_contents);
770 /* Read the whole file. */
772 cursor = file_contents.start;
773 while (cursor < file_contents.end)
776 /* Read one line, and save the word in contains. */
779 while (cursor < file_contents.end && *cursor != '\n')
782 /* Record the word in table if it is not empty. */
784 if (cursor > word_start)
786 ALLOC_NEW_WORD (table);
787 table->start[table->length].start = word_start;
788 table->start[table->length].size = cursor - word_start;
792 /* This test allows for an incomplete line at end of file. */
794 if (cursor < file_contents.end)
798 /* Finally, sort all the words read. */
800 qsort (table->start, table->length, (size_t) sizeof (WORD), compare_words);
803 /* Keyword recognition and selection. */
805 /*----------------------------------------------------------------------.
806 | For each keyword in the source text, constructs an OCCURS structure. |
807 `----------------------------------------------------------------------*/
810 find_occurs_in_text (void)
812 char *cursor; /* for scanning the source text */
813 char *scan; /* for scanning the source text also */
814 char *line_start; /* start of the current input line */
815 char *line_scan; /* newlines scanned until this point */
816 int reference_length; /* length of reference in input mode */
817 WORD possible_key; /* possible key, to ease searches */
818 OCCURS *occurs_cursor; /* current OCCURS under construction */
820 char *context_start; /* start of left context */
821 char *context_end; /* end of right context */
822 char *word_start; /* start of word */
823 char *word_end; /* end of word */
824 char *next_context_start; /* next start of left context */
826 /* reference_length is always used within `if (input_reference)'.
827 However, GNU C diagnoses that it may be used uninitialized. The
828 following assignment is merely to shut it up. */
830 reference_length = 0;
832 /* Tracking where lines start is helpful for reference processing. In
833 auto reference mode, this allows counting lines. In input reference
834 mode, this permits finding the beginning of the references.
836 The first line begins with the file, skip immediately this very first
837 reference in input reference mode, to help further rejection any word
838 found inside it. Also, unconditionally assigning these variable has
839 the happy effect of shutting up lint. */
841 line_start = text_buffer.start;
842 line_scan = line_start;
845 SKIP_NON_WHITE (line_scan, text_buffer.end);
846 reference_length = line_scan - line_start;
847 SKIP_WHITE (line_scan, text_buffer.end);
850 /* Process the whole buffer, one line or one sentence at a time. */
852 for (cursor = text_buffer.start;
853 cursor < text_buffer.end;
854 cursor = next_context_start)
857 /* `context_start' gets initialized before the processing of each
858 line, or once for the whole buffer if no end of line or sentence
859 sequence separator. */
861 context_start = cursor;
863 /* If a end of line or end of sentence sequence is defined and
864 non-empty, `next_context_start' will be recomputed to be the end of
865 each line or sentence, before each one is processed. If no such
866 sequence, then `next_context_start' is set at the end of the whole
867 buffer, which is then considered to be a single line or sentence.
868 This test also accounts for the case of an incomplete line or
869 sentence at the end of the buffer. */
871 if (context_regex_string
872 && (re_search (context_regex, cursor, text_buffer.end - cursor,
873 0, text_buffer.end - cursor, &context_regs)
875 next_context_start = cursor + context_regs.end[0];
878 next_context_start = text_buffer.end;
880 /* Include the separator into the right context, but not any suffix
881 white space in this separator; this insures it will be seen in
882 output and will not take more space than necessary. */
884 context_end = next_context_start;
885 SKIP_WHITE_BACKWARDS (context_end, context_start);
887 /* Read and process a single input line or sentence, one word at a
894 /* If a word regexp has been compiled, use it to skip at the
895 beginning of the next word. If there is no such word, exit
899 if (re_search (word_regex, cursor, context_end - cursor,
900 0, context_end - cursor, &word_regs)
903 word_start = cursor + word_regs.start[0];
904 word_end = cursor + word_regs.end[0];
908 /* Avoid re_search and use the fastmap to skip to the
909 beginning of the next word. If there is no more word in
910 the buffer, exit the loop. */
914 while (scan < context_end
915 && !word_fastmap[(unsigned char) *scan])
918 if (scan == context_end)
923 while (scan < context_end
924 && word_fastmap[(unsigned char) *scan])
930 /* Skip right to the beginning of the found word. */
934 /* Skip any zero length word. Just advance a single position,
935 then go fetch the next word. */
937 if (word_end == word_start)
943 /* This is a genuine, non empty word, so save it as a possible
944 key. Then skip over it. Also, maintain the maximum length of
945 all words read so far. It is mandatory to take the maximum
946 length of all words in the file, without considering if they
947 are actually kept or rejected, because backward jumps at output
948 generation time may fall in *any* word. */
950 possible_key.start = cursor;
951 possible_key.size = word_end - word_start;
952 cursor += possible_key.size;
954 if (possible_key.size > maximum_word_length)
955 maximum_word_length = possible_key.size;
957 /* In input reference mode, update `line_start' from its previous
958 value. Count the lines just in case auto reference mode is
959 also selected. If it happens that the word just matched is
960 indeed part of a reference; just ignore it. */
964 while (line_scan < possible_key.start)
965 if (*line_scan == '\n')
969 line_start = line_scan;
970 SKIP_NON_WHITE (line_scan, text_buffer.end);
971 reference_length = line_scan - line_start;
975 if (line_scan > possible_key.start)
979 /* Ignore the word if an `Ignore words' table exists and if it is
980 part of it. Also ignore the word if an `Only words' table and
981 if it is *not* part of it.
983 It is allowed that both tables be used at once, even if this
984 may look strange for now. Just ignore a word that would appear
985 in both. If regexps are eventually implemented for these
986 tables, the Ignore table could then reject words that would
987 have been previously accepted by the Only table. */
989 if (ignore_file && search_table (&possible_key, &ignore_table))
991 if (only_file && !search_table (&possible_key, &only_table))
994 /* A non-empty word has been found. First of all, insure
995 proper allocation of the next OCCURS, and make a pointer to
996 where it will be constructed. */
998 ALLOC_NEW_OCCURS (0);
999 occurs_cursor = occurs_table[0] + number_of_occurs[0];
1001 /* Define the refence field, if any. */
1006 /* While auto referencing, update `line_start' from its
1007 previous value, counting lines as we go. If input
1008 referencing at the same time, `line_start' has been
1009 advanced earlier, and the following loop is never really
1012 while (line_scan < possible_key.start)
1013 if (*line_scan == '\n')
1017 line_start = line_scan;
1018 SKIP_NON_WHITE (line_scan, text_buffer.end);
1023 occurs_cursor->reference = total_line_count;
1025 else if (input_reference)
1028 /* If only input referencing, `line_start' has been computed
1029 earlier to detect the case the word matched would be part
1030 of the reference. The reference position is simply the
1031 value of `line_start'. */
1033 occurs_cursor->reference
1034 = (DELTA) (line_start - possible_key.start);
1035 if (reference_length > reference_max_width)
1036 reference_max_width = reference_length;
1039 /* Exclude the reference from the context in simple cases. */
1041 if (input_reference && line_start == context_start)
1043 SKIP_NON_WHITE (context_start, context_end);
1044 SKIP_WHITE (context_start, context_end);
1047 /* Completes the OCCURS structure. */
1049 occurs_cursor->key = possible_key;
1050 occurs_cursor->left = context_start - possible_key.start;
1051 occurs_cursor->right = context_end - possible_key.start;
1053 number_of_occurs[0]++;
1058 /* Formatting and actual output - service routines. */
1060 /*-----------------------------------------.
1061 | Prints some NUMBER of spaces on stdout. |
1062 `-----------------------------------------*/
1065 print_spaces (int number)
1069 for (counter = number; counter > 0; counter--)
1073 /*-------------------------------------.
1074 | Prints the field provided by FIELD. |
1075 `-------------------------------------*/
1078 print_field (BLOCK field)
1080 char *cursor; /* Cursor in field to print */
1081 int character; /* Current character */
1082 int base; /* Base character, without diacritic */
1083 int diacritic; /* Diacritic code for the character */
1085 /* Whitespace is not really compressed. Instead, each white space
1086 character (tab, vt, ht etc.) is printed as one single space. */
1088 for (cursor = field.start; cursor < field.end; cursor++)
1090 character = (unsigned char) *cursor;
1091 if (edited_flag[character])
1094 /* First check if this is a diacriticized character.
1096 This works only for TeX. I do not know how diacriticized
1097 letters work with `roff'. Please someone explain it to me! */
1099 diacritic = todiac (character);
1100 if (diacritic != 0 && output_format == TEX_FORMAT)
1102 base = tobase (character);
1106 case 1: /* Latin diphthongs */
1110 fputs ("\\oe{}", stdout);
1114 fputs ("\\OE{}", stdout);
1118 fputs ("\\ae{}", stdout);
1122 fputs ("\\AE{}", stdout);
1130 case 2: /* Acute accent */
1131 printf ("\\'%s%c", (base == 'i' ? "\\" : ""), base);
1134 case 3: /* Grave accent */
1135 printf ("\\`%s%c", (base == 'i' ? "\\" : ""), base);
1138 case 4: /* Circumflex accent */
1139 printf ("\\^%s%c", (base == 'i' ? "\\" : ""), base);
1142 case 5: /* Diaeresis */
1143 printf ("\\\"%s%c", (base == 'i' ? "\\" : ""), base);
1146 case 6: /* Tilde accent */
1147 printf ("\\~%s%c", (base == 'i' ? "\\" : ""), base);
1150 case 7: /* Cedilla */
1151 printf ("\\c{%c}", base);
1154 case 8: /* Small circle beneath */
1158 fputs ("\\aa{}", stdout);
1162 fputs ("\\AA{}", stdout);
1170 case 9: /* Strike through */
1174 fputs ("\\o{}", stdout);
1178 fputs ("\\O{}", stdout);
1189 /* This is not a diacritic character, so handle cases which are
1190 really specific to `roff' or TeX. All white space processing
1191 is done as the default case of this switch. */
1196 /* In roff output format, double any quote. */
1206 /* In TeX output format, precede these with a backslash. */
1208 putchar (character);
1213 /* In TeX output format, precede these with a backslash and
1214 force mathematical mode. */
1215 printf ("$\\%c$", character);
1219 /* In TeX output mode, request production of a backslash. */
1220 fputs ("\\backslash{}", stdout);
1224 /* Any other flagged character produces a single space. */
1233 /* Formatting and actual output - planning routines. */
1235 /*--------------------------------------------------------------------.
1236 | From information collected from command line options and input file |
1237 | readings, compute and fix some output parameter values. |
1238 `--------------------------------------------------------------------*/
1241 fix_output_parameters (void)
1243 int file_index; /* index in text input file arrays */
1244 int line_ordinal; /* line ordinal value for reference */
1245 char ordinal_string[12]; /* edited line ordinal for reference */
1246 int reference_width; /* width for the whole reference */
1247 int character; /* character ordinal */
1248 const char *cursor; /* cursor in some constant strings */
1250 /* In auto reference mode, the maximum width of this field is
1251 precomputed and subtracted from the overall line width. Add one for
1252 the column which separate the file name from the line number. */
1256 reference_max_width = 0;
1257 for (file_index = 0; file_index < number_input_files; file_index++)
1259 line_ordinal = file_line_count[file_index] + 1;
1261 line_ordinal -= file_line_count[file_index - 1];
1262 sprintf (ordinal_string, "%d", line_ordinal);
1263 reference_width = strlen (ordinal_string);
1264 if (input_file_name[file_index])
1265 reference_width += strlen (input_file_name[file_index]);
1266 if (reference_width > reference_max_width)
1267 reference_max_width = reference_width;
1269 reference_max_width++;
1270 reference.start = (char *) xmalloc ((size_t) reference_max_width + 1);
1273 /* If the reference appears to the left of the output line, reserve some
1274 space for it right away, including one gap size. */
1276 if ((auto_reference || input_reference) && !right_reference)
1277 line_width -= reference_max_width + gap_size;
1279 /* The output lines, minimally, will contain from left to right a left
1280 context, a gap, and a keyword followed by the right context with no
1281 special intervening gap. Half of the line width is dedicated to the
1282 left context and the gap, the other half is dedicated to the keyword
1283 and the right context; these values are computed once and for all here.
1284 There also are tail and head wrap around fields, used when the keyword
1285 is near the beginning or the end of the line, or when some long word
1286 cannot fit in, but leave place from wrapped around shorter words. The
1287 maximum width of these fields are recomputed separately for each line,
1288 on a case by case basis. It is worth noting that it cannot happen that
1289 both the tail and head fields are used at once. */
1291 half_line_width = line_width / 2;
1292 before_max_width = half_line_width - gap_size;
1293 keyafter_max_width = half_line_width;
1295 /* If truncation_string is the empty string, make it NULL to speed up
1296 tests. In this case, truncation_string_length will never get used, so
1297 there is no need to set it. */
1299 if (truncation_string && *truncation_string)
1300 truncation_string_length = strlen (truncation_string);
1302 truncation_string = NULL;
1307 /* When flagging truncation at the left of the keyword, the
1308 truncation mark goes at the beginning of the before field,
1309 unless there is a head field, in which case the mark goes at the
1310 left of the head field. When flagging truncation at the right
1311 of the keyword, the mark goes at the end of the keyafter field,
1312 unless there is a tail field, in which case the mark goes at the
1313 end of the tail field. Only eight combination cases could arise
1314 for truncation marks:
1317 . One beginning the before field.
1318 . One beginning the head field.
1319 . One ending the keyafter field.
1320 . One ending the tail field.
1321 . One beginning the before field, another ending the keyafter field.
1322 . One ending the tail field, another beginning the before field.
1323 . One ending the keyafter field, another beginning the head field.
1325 So, there is at most two truncation marks, which could appear both
1326 on the left side of the center of the output line, both on the
1327 right side, or one on either side. */
1329 before_max_width -= 2 * truncation_string_length;
1330 keyafter_max_width -= 2 * truncation_string_length;
1335 /* I never figured out exactly how UNIX' ptx plans the output width
1336 of its various fields. If GNU extensions are disabled, do not
1337 try computing the field widths correctly; instead, use the
1338 following formula, which does not completely imitate UNIX' ptx,
1341 keyafter_max_width -= 2 * truncation_string_length + 1;
1344 /* Compute which characters need special output processing. Initialize
1345 by flagging any white space character. Some systems do not consider
1346 form feed as a space character, but we do. */
1348 for (character = 0; character < CHAR_SET_SIZE; character++)
1349 edited_flag[character] = ISSPACE (character) != 0;
1350 edited_flag['\f'] = 1;
1352 /* Complete the special character flagging according to selected output
1355 switch (output_format)
1357 case UNKNOWN_FORMAT:
1358 /* Should never happen. */
1365 /* `Quote' characters should be doubled. */
1367 edited_flag['"'] = 1;
1372 /* Various characters need special processing. */
1374 for (cursor = "$%&#_{}\\"; *cursor; cursor++)
1375 edited_flag[(unsigned char) *cursor] = 1;
1377 /* Any character with 8th bit set will print to a single space, unless
1378 it is diacriticized. */
1380 for (character = 0200; character < CHAR_SET_SIZE; character++)
1381 edited_flag[character] = todiac (character) != 0;
1386 /*------------------------------------------------------------------.
1387 | Compute the position and length of all the output fields, given a |
1388 | pointer to some OCCURS. |
1389 `------------------------------------------------------------------*/
1392 define_all_fields (OCCURS *occurs)
1394 int tail_max_width; /* allowable width of tail field */
1395 int head_max_width; /* allowable width of head field */
1396 char *cursor; /* running cursor in source text */
1397 char *left_context_start; /* start of left context */
1398 char *right_context_end; /* end of right context */
1399 char *left_field_start; /* conservative start for `head'/`before' */
1400 int file_index; /* index in text input file arrays */
1401 const char *file_name; /* file name for reference */
1402 int line_ordinal; /* line ordinal for reference */
1404 /* Define `keyafter', start of left context and end of right context.
1405 `keyafter' starts at the saved position for keyword and extend to the
1406 right from the end of the keyword, eating separators or full words, but
1407 not beyond maximum allowed width for `keyafter' field or limit for the
1408 right context. Suffix spaces will be removed afterwards. */
1410 keyafter.start = occurs->key.start;
1411 keyafter.end = keyafter.start + occurs->key.size;
1412 left_context_start = keyafter.start + occurs->left;
1413 right_context_end = keyafter.start + occurs->right;
1415 cursor = keyafter.end;
1416 while (cursor < right_context_end
1417 && cursor <= keyafter.start + keyafter_max_width)
1419 keyafter.end = cursor;
1420 SKIP_SOMETHING (cursor, right_context_end);
1422 if (cursor <= keyafter.start + keyafter_max_width)
1423 keyafter.end = cursor;
1425 keyafter_truncation = truncation_string && keyafter.end < right_context_end;
1427 SKIP_WHITE_BACKWARDS (keyafter.end, keyafter.start);
1429 /* When the left context is wide, it might take some time to catch up from
1430 the left context boundary to the beginning of the `head' or `before'
1431 fields. So, in this case, to speed the catchup, we jump back from the
1432 keyword, using some secure distance, possibly falling in the middle of
1433 a word. A secure backward jump would be at least half the maximum
1434 width of a line, plus the size of the longest word met in the whole
1435 input. We conclude this backward jump by a skip forward of at least
1436 one word. In this manner, we should not inadvertently accept only part
1437 of a word. From the reached point, when it will be time to fix the
1438 beginning of `head' or `before' fields, we will skip forward words or
1439 delimiters until we get sufficiently near. */
1441 if (-occurs->left > half_line_width + maximum_word_length)
1444 = keyafter.start - (half_line_width + maximum_word_length);
1445 SKIP_SOMETHING (left_field_start, keyafter.start);
1448 left_field_start = keyafter.start + occurs->left;
1450 /* `before' certainly ends at the keyword, but not including separating
1451 spaces. It starts after than the saved value for the left context, by
1452 advancing it until it falls inside the maximum allowed width for the
1453 before field. There will be no prefix spaces either. `before' only
1454 advances by skipping single separators or whole words. */
1456 before.start = left_field_start;
1457 before.end = keyafter.start;
1458 SKIP_WHITE_BACKWARDS (before.end, before.start);
1460 while (before.start + before_max_width < before.end)
1461 SKIP_SOMETHING (before.start, before.end);
1463 if (truncation_string)
1465 cursor = before.start;
1466 SKIP_WHITE_BACKWARDS (cursor, text_buffer.start);
1467 before_truncation = cursor > left_context_start;
1470 before_truncation = 0;
1472 SKIP_WHITE (before.start, text_buffer.end);
1474 /* The tail could not take more columns than what has been left in the
1475 left context field, and a gap is mandatory. It starts after the
1476 right context, and does not contain prefixed spaces. It ends at
1477 the end of line, the end of buffer or when the tail field is full,
1478 whichever comes first. It cannot contain only part of a word, and
1479 has no suffixed spaces. */
1482 = before_max_width - (before.end - before.start) - gap_size;
1484 if (tail_max_width > 0)
1486 tail.start = keyafter.end;
1487 SKIP_WHITE (tail.start, text_buffer.end);
1489 tail.end = tail.start;
1491 while (cursor < right_context_end
1492 && cursor < tail.start + tail_max_width)
1495 SKIP_SOMETHING (cursor, right_context_end);
1498 if (cursor < tail.start + tail_max_width)
1501 if (tail.end > tail.start)
1503 keyafter_truncation = 0;
1504 tail_truncation = truncation_string && tail.end < right_context_end;
1507 tail_truncation = 0;
1509 SKIP_WHITE_BACKWARDS (tail.end, tail.start);
1514 /* No place left for a tail field. */
1518 tail_truncation = 0;
1521 /* `head' could not take more columns than what has been left in the right
1522 context field, and a gap is mandatory. It ends before the left
1523 context, and does not contain suffixed spaces. Its pointer is advanced
1524 until the head field has shrunk to its allowed width. It cannot
1525 contain only part of a word, and has no suffixed spaces. */
1528 = keyafter_max_width - (keyafter.end - keyafter.start) - gap_size;
1530 if (head_max_width > 0)
1532 head.end = before.start;
1533 SKIP_WHITE_BACKWARDS (head.end, text_buffer.start);
1535 head.start = left_field_start;
1536 while (head.start + head_max_width < head.end)
1537 SKIP_SOMETHING (head.start, head.end);
1539 if (head.end > head.start)
1541 before_truncation = 0;
1542 head_truncation = (truncation_string
1543 && head.start > left_context_start);
1546 head_truncation = 0;
1548 SKIP_WHITE (head.start, head.end);
1553 /* No place left for a head field. */
1557 head_truncation = 0;
1563 /* Construct the reference text in preallocated space from the file
1564 name and the line number. Find out in which file the reference
1565 occurred. Standard input yields an empty file name. Insure line
1566 numbers are one based, even if they are computed zero based. */
1569 while (file_line_count[file_index] < occurs->reference)
1572 file_name = input_file_name[file_index];
1576 line_ordinal = occurs->reference + 1;
1578 line_ordinal -= file_line_count[file_index - 1];
1580 sprintf (reference.start, "%s:%d", file_name, line_ordinal);
1581 reference.end = reference.start + strlen (reference.start);
1583 else if (input_reference)
1586 /* Reference starts at saved position for reference and extends right
1587 until some white space is met. */
1589 reference.start = keyafter.start + (DELTA) occurs->reference;
1590 reference.end = reference.start;
1591 SKIP_NON_WHITE (reference.end, right_context_end);
1595 /* Formatting and actual output - control routines. */
1597 /*----------------------------------------------------------------------.
1598 | Output the current output fields as one line for `troff' or `nroff'. |
1599 `----------------------------------------------------------------------*/
1602 output_one_roff_line (void)
1604 /* Output the `tail' field. */
1606 printf (".%s \"", macro_name);
1608 if (tail_truncation)
1609 fputs (truncation_string, stdout);
1612 /* Output the `before' field. */
1614 fputs (" \"", stdout);
1615 if (before_truncation)
1616 fputs (truncation_string, stdout);
1617 print_field (before);
1620 /* Output the `keyafter' field. */
1622 fputs (" \"", stdout);
1623 print_field (keyafter);
1624 if (keyafter_truncation)
1625 fputs (truncation_string, stdout);
1628 /* Output the `head' field. */
1630 fputs (" \"", stdout);
1631 if (head_truncation)
1632 fputs (truncation_string, stdout);
1636 /* Conditionally output the `reference' field. */
1638 if (auto_reference || input_reference)
1640 fputs (" \"", stdout);
1641 print_field (reference);
1648 /*---------------------------------------------------------.
1649 | Output the current output fields as one line for `TeX'. |
1650 `---------------------------------------------------------*/
1653 output_one_tex_line (void)
1655 BLOCK key; /* key field, isolated */
1656 BLOCK after; /* after field, isolated */
1657 char *cursor; /* running cursor in source text */
1659 printf ("\\%s ", macro_name);
1662 fputs ("}{", stdout);
1663 print_field (before);
1664 fputs ("}{", stdout);
1665 key.start = keyafter.start;
1666 after.end = keyafter.end;
1667 cursor = keyafter.start;
1668 SKIP_SOMETHING (cursor, keyafter.end);
1670 after.start = cursor;
1672 fputs ("}{", stdout);
1673 print_field (after);
1674 fputs ("}{", stdout);
1677 if (auto_reference || input_reference)
1680 print_field (reference);
1686 /*-------------------------------------------------------------------.
1687 | Output the current output fields as one line for a dumb terminal. |
1688 `-------------------------------------------------------------------*/
1691 output_one_dumb_line (void)
1693 if (!right_reference)
1698 /* Output the `reference' field, in such a way that GNU emacs
1699 next-error will handle it. The ending colon is taken from the
1700 gap which follows. */
1702 print_field (reference);
1704 print_spaces (reference_max_width
1706 - (reference.end - reference.start)
1712 /* Output the `reference' field and its following gap. */
1714 print_field (reference);
1715 print_spaces (reference_max_width
1717 - (reference.end - reference.start));
1721 if (tail.start < tail.end)
1723 /* Output the `tail' field. */
1726 if (tail_truncation)
1727 fputs (truncation_string, stdout);
1729 print_spaces (half_line_width - gap_size
1730 - (before.end - before.start)
1731 - (before_truncation ? truncation_string_length : 0)
1732 - (tail.end - tail.start)
1733 - (tail_truncation ? truncation_string_length : 0));
1736 print_spaces (half_line_width - gap_size
1737 - (before.end - before.start)
1738 - (before_truncation ? truncation_string_length : 0));
1740 /* Output the `before' field. */
1742 if (before_truncation)
1743 fputs (truncation_string, stdout);
1744 print_field (before);
1746 print_spaces (gap_size);
1748 /* Output the `keyafter' field. */
1750 print_field (keyafter);
1751 if (keyafter_truncation)
1752 fputs (truncation_string, stdout);
1754 if (head.start < head.end)
1756 /* Output the `head' field. */
1758 print_spaces (half_line_width
1759 - (keyafter.end - keyafter.start)
1760 - (keyafter_truncation ? truncation_string_length : 0)
1761 - (head.end - head.start)
1762 - (head_truncation ? truncation_string_length : 0));
1763 if (head_truncation)
1764 fputs (truncation_string, stdout);
1769 if ((auto_reference || input_reference) && right_reference)
1770 print_spaces (half_line_width
1771 - (keyafter.end - keyafter.start)
1772 - (keyafter_truncation ? truncation_string_length : 0));
1774 if ((auto_reference || input_reference) && right_reference)
1776 /* Output the `reference' field. */
1778 print_spaces (gap_size);
1779 print_field (reference);
1785 /*------------------------------------------------------------------------.
1786 | Scan the whole occurs table and, for each entry, output one line in the |
1787 | appropriate format. |
1788 `------------------------------------------------------------------------*/
1791 generate_all_output (void)
1793 size_t occurs_index; /* index of keyword entry being processed */
1794 OCCURS *occurs_cursor; /* current keyword entry being processed */
1796 /* The following assignments are useful to provide default values in case
1797 line contexts or references are not used, in which case these variables
1798 would never be computed. */
1802 tail_truncation = 0;
1806 head_truncation = 0;
1808 /* Loop over all keyword occurrences. */
1810 occurs_cursor = occurs_table[0];
1812 for (occurs_index = 0; occurs_index < number_of_occurs[0]; occurs_index++)
1814 /* Compute the exact size of every field and whenever truncation flags
1815 are present or not. */
1817 define_all_fields (occurs_cursor);
1819 /* Produce one output line according to selected format. */
1821 switch (output_format)
1823 case UNKNOWN_FORMAT:
1824 /* Should never happen. */
1827 output_one_dumb_line ();
1831 output_one_roff_line ();
1835 output_one_tex_line ();
1839 /* Advance the cursor into the occurs table. */
1845 /* Option decoding and main program. */
1847 /*------------------------------------------------------.
1848 | Print program identification and options, then exit. |
1849 `------------------------------------------------------*/
1854 if (status != EXIT_SUCCESS)
1855 fprintf (stderr, _("Try `%s --help' for more information.\n"),
1860 Usage: %s [OPTION]... [INPUT]... (without -G)\n\
1861 or: %s -G [OPTION]... [INPUT [OUTPUT]]\n"),
1862 program_name, program_name);
1864 Output a permuted index, including context, of the words in the input files.\n\
1868 Mandatory arguments to long options are mandatory for short options too.\n\
1871 -A, --auto-reference output automatically generated references\n\
1872 -C, --copyright display Copyright and copying conditions\n\
1873 -G, --traditional behave more like System V `ptx'\n\
1874 -F, --flag-truncation=STRING use STRING for flagging line truncations\n\
1877 -M, --macro-name=STRING macro name to use instead of `xx'\n\
1878 -O, --format=roff generate output as roff directives\n\
1879 -R, --right-side-refs put references at right, not counted in -w\n\
1880 -S, --sentence-regexp=REGEXP for end of lines or end of sentences\n\
1881 -T, --format=tex generate output as TeX directives\n\
1884 -W, --word-regexp=REGEXP use REGEXP to match each keyword\n\
1885 -b, --break-file=FILE word break characters in this FILE\n\
1886 -f, --ignore-case fold lower case to upper case for sorting\n\
1887 -g, --gap-size=NUMBER gap size in columns between output fields\n\
1888 -i, --ignore-file=FILE read ignore word list from FILE\n\
1889 -o, --only-file=FILE read only word list from this FILE\n\
1892 -r, --references first field of each line is a reference\n\
1893 -t, --typeset-mode - not implemented -\n\
1894 -w, --width=NUMBER output width in columns, reference excluded\n\
1896 fputs (HELP_OPTION_DESCRIPTION, stdout);
1897 fputs (VERSION_OPTION_DESCRIPTION, stdout);
1900 With no FILE or if FILE is -, read Standard Input. `-F /' by default.\n\
1906 /*----------------------------------------------------------------------.
1907 | Main program. Decode ARGC arguments passed through the ARGV array of |
1908 | strings, then launch execution. |
1909 `----------------------------------------------------------------------*/
1911 /* Long options equivalences. */
1912 static const struct option long_options[] =
1914 {"auto-reference", no_argument, NULL, 'A'},
1915 {"break-file", required_argument, NULL, 'b'},
1916 {"copyright", no_argument, NULL, 'C'},
1917 {"flag-truncation", required_argument, NULL, 'F'},
1918 {"ignore-case", no_argument, NULL, 'f'},
1919 {"gap-size", required_argument, NULL, 'g'},
1920 {"ignore-file", required_argument, NULL, 'i'},
1921 {"macro-name", required_argument, NULL, 'M'},
1922 {"only-file", required_argument, NULL, 'o'},
1923 {"references", no_argument, NULL, 'r'},
1924 {"right-side-refs", no_argument, NULL, 'R'},
1925 {"format", required_argument, NULL, 10},
1926 {"sentence-regexp", required_argument, NULL, 'S'},
1927 {"traditional", no_argument, NULL, 'G'},
1928 {"typeset-mode", no_argument, NULL, 't'},
1929 {"width", required_argument, NULL, 'w'},
1930 {"word-regexp", required_argument, NULL, 'W'},
1931 {GETOPT_HELP_OPTION_DECL},
1932 {GETOPT_VERSION_OPTION_DECL},
1936 static char const* const format_args[] =
1941 static enum Format const format_vals[] =
1943 ROFF_FORMAT, TEX_FORMAT
1947 main (int argc, char **argv)
1949 int optchar; /* argument character */
1950 int file_index; /* index in text input file arrays */
1952 /* Decode program options. */
1954 program_name = argv[0];
1955 setlocale (LC_ALL, "");
1956 bindtextdomain (PACKAGE, LOCALEDIR);
1957 textdomain (PACKAGE);
1959 atexit (close_stdout);
1961 #if HAVE_SETCHRCLASS
1965 while (optchar = getopt_long (argc, argv, "ACF:GM:ORS:TW:b:i:fg:o:trw:",
1966 long_options, NULL),
1972 usage (EXIT_FAILURE);
1979 This program is free software; you can redistribute it and/or modify\n\
1980 it under the terms of the GNU General Public License as published by\n\
1981 the Free Software Foundation; either version 2, or (at your option)\n\
1982 any later version.\n\
1986 This program is distributed in the hope that it will be useful,\n\
1987 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
1988 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
1989 GNU General Public License for more details.\n\
1993 You should have received a copy of the GNU General Public License\n\
1994 along with this program; if not, write to the Free Software Foundation,\n\
1995 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n"),
1998 exit (EXIT_SUCCESS);
2005 break_file = optarg;
2013 gap_size = atoi (optarg);
2017 ignore_file = optarg;
2025 input_reference = 1;
2029 /* Yet to understand... */
2033 line_width = atoi (optarg);
2041 truncation_string = copy_unescaped_string (optarg);
2045 macro_name = optarg;
2049 output_format = ROFF_FORMAT;
2053 right_reference = 1;
2057 context_regex_string = copy_unescaped_string (optarg);
2061 output_format = TEX_FORMAT;
2065 word_regex_string = copy_unescaped_string (optarg);
2069 output_format = XARGMATCH ("--format", optarg,
2070 format_args, format_vals);
2071 case_GETOPT_HELP_CHAR;
2073 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
2077 /* Change the default Ignore file if one is defined. */
2079 #ifdef DEFAULT_IGNORE_FILE
2081 ignore_file = DEFAULT_IGNORE_FILE;
2084 /* Process remaining arguments. If GNU extensions are enabled, process
2085 all arguments as input parameters. If disabled, accept at most two
2086 arguments, the second of which is an output parameter. */
2091 /* No more argument simply means: read standard input. */
2093 input_file_name = (const char **) xmalloc (sizeof (const char *));
2094 file_line_count = (int *) xmalloc (sizeof (int));
2095 number_input_files = 1;
2096 input_file_name[0] = NULL;
2098 else if (gnu_extensions)
2100 number_input_files = argc - optind;
2102 = (const char **) xmalloc (number_input_files * sizeof (const char *));
2104 = (int *) xmalloc (number_input_files * sizeof (int));
2106 for (file_index = 0; file_index < number_input_files; file_index++)
2108 input_file_name[file_index] = argv[optind];
2109 if (!*argv[optind] || strcmp (argv[optind], "-") == 0)
2110 input_file_name[0] = NULL;
2112 input_file_name[0] = argv[optind];
2119 /* There is one necessary input file. */
2121 number_input_files = 1;
2122 input_file_name = (const char **) xmalloc (sizeof (const char *));
2123 file_line_count = (int *) xmalloc (sizeof (int));
2124 if (!*argv[optind] || strcmp (argv[optind], "-") == 0)
2125 input_file_name[0] = NULL;
2127 input_file_name[0] = argv[optind];
2130 /* Redirect standard output, only if requested. */
2134 /* FIXME: don't fclose here? */
2136 if (fopen (argv[optind], "w") == NULL)
2137 error (EXIT_FAILURE, errno, "%s", argv[optind]);
2141 /* Diagnose any other argument as an error. */
2144 usage (EXIT_FAILURE);
2147 /* If the output format has not been explicitly selected, choose dumb
2148 terminal format if GNU extensions are enabled, else `roff' format. */
2150 if (output_format == UNKNOWN_FORMAT)
2151 output_format = gnu_extensions ? DUMB_FORMAT : ROFF_FORMAT;
2153 /* Initialize the main tables. */
2155 initialize_regex ();
2157 /* Read `Break character' file, if any. */
2160 digest_break_file (break_file);
2162 /* Read `Ignore words' file and `Only words' files, if any. If any of
2163 these files is empty, reset the name of the file to NULL, to avoid
2164 unnecessary calls to search_table. */
2168 digest_word_file (ignore_file, &ignore_table);
2169 if (ignore_table.length == 0)
2175 digest_word_file (only_file, &only_table);
2176 if (only_table.length == 0)
2180 /* Prepare to study all the input files. */
2182 number_of_occurs[0] = 0;
2183 total_line_count = 0;
2184 maximum_word_length = 0;
2185 reference_max_width = 0;
2187 for (file_index = 0; file_index < number_input_files; file_index++)
2190 /* Read the file in core, than study it. */
2192 swallow_file_in_memory (input_file_name[file_index], &text_buffer);
2193 find_occurs_in_text ();
2195 /* Maintain for each file how many lines has been read so far when its
2196 end is reached. Incrementing the count first is a simple kludge to
2197 handle a possible incomplete line at end of file. */
2200 file_line_count[file_index] = total_line_count;
2203 /* Do the output process phase. */
2205 sort_found_occurs ();
2206 fix_output_parameters ();
2207 generate_all_output ();
2211 exit (EXIT_SUCCESS);