2 /* Copyright (C) 1989-1992, 2000, 2001, 2002, 2004, 2006
3 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.com)
6 This file is part of groff.
8 groff is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 groff is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with groff; see the file COPYING. If not, write to the Free Software
20 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
29 extern "C" const char *Version_string
;
31 const char PRE_LABEL_MARKER
= '\013';
32 const char POST_LABEL_MARKER
= '\014';
33 const char LABEL_MARKER
= '\015'; // label_type is added on
35 #define FORCE_LEFT_BRACKET 04
36 #define FORCE_RIGHT_BRACKET 010
38 static FILE *outfp
= stdout
;
40 string capitalize_fields
;
41 string reverse_fields
;
42 string abbreviate_fields
;
43 string period_before_last_name
= ". ";
44 string period_before_initial
= ".";
45 string period_before_hyphen
= "";
46 string period_before_other
= ". ";
48 int annotation_field
= -1;
49 string annotation_macro
;
50 string discard_fields
= "XYZ";
51 string pre_label
= "\\*([.";
52 string post_label
= "\\*(.]";
53 string sep_label
= ", ";
54 int have_bibliography
= 0;
56 int move_punctuation
= 0;
57 int abbreviate_label_ranges
= 0;
58 string label_range_indicator
;
59 int label_in_text
= 1;
60 int label_in_reference
= 1;
61 int date_as_label
= 0;
62 int sort_adjacent_labels
= 0;
63 // Join exactly two authors with this.
64 string join_authors_exactly_two
= " and ";
65 // When there are more than two authors join the last two with this.
66 string join_authors_last_two
= ", and ";
67 // Otherwise join authors with this.
68 string join_authors_default
= ", ";
69 string separate_label_second_parts
= ", ";
70 // Use this string to represent that there are other authors.
71 string et_al
= " et al";
72 // Use et al only if it can replace at least this many authors.
73 int et_al_min_elide
= 2;
74 // Use et al only if the total number of authors is at least this.
75 int et_al_min_total
= 3;
78 int compatible_flag
= 0;
80 int short_label_flag
= 0;
82 static int recognize_R1_R2
= 1;
84 search_list database_list
;
85 int search_default
= 1;
86 static int default_database_loaded
= 0;
88 static reference
**citation
= 0;
89 static int ncitations
= 0;
90 static int citation_max
= 0;
92 static reference
**reference_hash_table
= 0;
93 static int hash_table_size
;
94 static int nreferences
= 0;
96 static int need_syncing
= 0;
98 string pending_lf_lines
;
100 static void output_pending_line();
101 static unsigned immediately_handle_reference(const string
&);
102 static void immediately_output_references();
103 static unsigned store_reference(const string
&);
104 static void divert_to_temporary_file();
105 static reference
*make_reference(const string
&, unsigned *);
106 static void usage(FILE *stream
);
107 static void do_file(const char *);
108 static void split_punct(string
&line
, string
&punct
);
109 static void output_citation_group(reference
**v
, int n
, label_type
, FILE *fp
);
110 static void possibly_load_default_database();
112 int main(int argc
, char **argv
)
114 program_name
= argv
[0];
115 static char stderr_buf
[BUFSIZ
];
116 setbuf(stderr
, stderr_buf
);
118 int finished_options
= 0;
123 !finished_options
&& argc
> 0 && argv
[0][0] == '-'
124 && argv
[0][1] != '\0';
126 const char *opt
= argv
[0] + 1;
127 while (opt
!= 0 && *opt
!= '\0') {
135 label_in_reference
= 0;
139 annotation_field
= 'X';
140 annotation_macro
= "AP";
142 else if (csalnum(opt
[0]) && opt
[1] == '.' && opt
[2] != '\0') {
143 annotation_field
= opt
[0];
144 annotation_macro
= opt
+ 2;
149 move_punctuation
= 1;
157 // Not a very useful spec.
158 set_label_spec("(A.n|Q)', '(D.y|D)");
172 if (*++opt
== '\0') {
178 error("option `f' requires an argument");
188 for (ptr
= num
; *ptr
; ptr
++)
189 if (!csdigit(*ptr
)) {
190 error("bad character `%1' in argument to -f option", *ptr
);
198 set_label_spec(spec
.contents());
205 label_in_reference
= 0;
213 capitalize_fields
= ++opt
;
223 error("bad field name `%1'", *opt
++);
237 for (ptr
= ++opt
; *ptr
; ptr
++)
238 if (!csdigit(*ptr
)) {
239 error("argument to `a' option not a number");
243 reverse_fields
= 'A';
244 reverse_fields
+= opt
;
250 linear_ignore_fields
= ++opt
;
255 char buf
[INT_DIGITS
*2 + 11]; // A.n+2D.y-3%a
257 if (*++opt
!= '\0' && *opt
!= ',') {
259 long n
= strtol(opt
, &ptr
, 10);
260 if (n
== 0 && ptr
== opt
) {
261 error("bad integer `%1' in `l' option", opt
);
268 sprintf(strchr(buf
, '\0'), "+%ld", n
);
275 long n
= strtol(opt
, &ptr
, 10);
276 if (n
== 0 && ptr
== opt
) {
277 error("bad integer `%1' in `l' option", opt
);
283 sprintf(strchr(buf
, '\0'), "-%ld", n
);
286 error("argument to `l' option not of form `m,n'");
289 if (!set_label_spec(buf
))
300 const char *filename
= 0;
301 if (*++opt
== '\0') {
307 error("option `p' requires an argument");
316 database_list
.add_file(filename
);
331 long n
= strtol(opt
, &ptr
, 10);
332 if (n
== 0 && ptr
== opt
) {
333 error("bad integer `%1' in `t' option", opt
);
339 linear_truncate_len
= int(n
);
344 if (opt
[1] == '\0') {
345 finished_options
= 1;
349 if (strcmp(opt
,"-version")==0) {
351 printf("GNU refer (groff) version %s\n", Version_string
);
355 if (strcmp(opt
,"-help")==0) {
362 error("unrecognized option `%1'", *opt
);
370 set_label_spec("%1");
378 for (int i
= 0; i
< argc
; i
++) {
387 if (fflush(stdout
) < 0)
388 fatal("output error");
392 static void usage(FILE *stream
)
395 "usage: %s [-benvCPRS] [-aN] [-cXYZ] [-fN] [-iXYZ] [-kX] [-lM,N] [-p file]\n"
396 " [-sXYZ] [-tN] [-BL.M] [files ...]\n",
400 static void possibly_load_default_database()
402 if (search_default
&& !default_database_loaded
) {
403 char *filename
= getenv("REFER");
405 database_list
.add_file(filename
);
407 database_list
.add_file(DEFAULT_INDEX
, 1);
408 default_database_loaded
= 1;
412 static int is_list(const string
&str
)
414 const char *start
= str
.contents();
415 const char *end
= start
+ str
.length();
416 while (end
> start
&& csspace(end
[-1]))
418 while (start
< end
&& csspace(*start
))
420 return end
- start
== 6 && memcmp(start
, "$LIST$", 6) == 0;
423 static void do_file(const char *filename
)
426 if (strcmp(filename
, "-") == 0) {
431 fp
= fopen(filename
, "r");
433 error("can't open `%1': %2", filename
, strerror(errno
));
437 current_filename
= filename
;
438 fprintf(outfp
, ".lf 1 %s\n", filename
);
446 if (line
.length() > 0)
450 if (invalid_input_char(c
))
451 error("invalid input character code %1", c
);
458 int len
= line
.length();
462 if (len
>= 2 && line
[0] == '.' && line
[1] == '[') {
463 int start_lineno
= current_lineno
;
464 int start_of_line
= 1;
467 string
pre(line
.contents() + 2, line
.length() - 3);
471 error_with_file_and_line(current_filename
, start_lineno
,
472 "missing `.]' line");
477 if (start_of_line
&& c
== '.') {
480 while ((d
= getc(fp
)) != '\n' && d
!= EOF
) {
481 if (invalid_input_char(d
))
482 error("invalid input character code %1", d
);
491 if (invalid_input_char(c
))
492 error("invalid input character code %1", c
);
495 start_of_line
= (c
== '\n');
498 output_pending_line();
502 error("found `$LIST$' but not accumulating references");
505 unsigned flags
= (accumulate
506 ? store_reference(str
)
507 : immediately_handle_reference(str
));
509 if (accumulate
&& outfp
== stdout
)
510 divert_to_temporary_file();
511 if (pending_line
.length() == 0) {
512 warning("can't attach citation to previous line");
515 pending_line
.set_length(pending_line
.length() - 1);
517 if (move_punctuation
)
518 split_punct(pending_line
, punct
);
519 int have_text
= pre
.length() > 0 || post
.length() > 0;
520 label_type lt
= label_type(flags
& ~(FORCE_LEFT_BRACKET
521 |FORCE_RIGHT_BRACKET
));
522 if ((flags
& FORCE_LEFT_BRACKET
) || !have_text
)
523 pending_line
+= PRE_LABEL_MARKER
;
525 char lm
= LABEL_MARKER
+ (int)lt
;
527 pending_line
+= post
;
528 if ((flags
& FORCE_RIGHT_BRACKET
) || !have_text
)
529 pending_line
+= POST_LABEL_MARKER
;
530 pending_line
+= punct
;
531 pending_line
+= '\n';
537 && line
[0] == '.' && line
[1] == 'l' && line
[2] == 'f'
538 && (compatible_flag
|| line
[3] == '\n' || line
[3] == ' ')) {
539 pending_lf_lines
+= line
;
541 if (interpret_lf_args(line
.contents() + 3))
544 else if (recognize_R1_R2
546 && line
[0] == '.' && line
[1] == 'R' && line
[2] == '1'
547 && (compatible_flag
|| line
[3] == '\n' || line
[3] == ' ')) {
549 int start_of_line
= 1;
550 int start_lineno
= current_lineno
;
553 if (c
!= EOF
&& start_of_line
)
555 if (start_of_line
&& c
== '.') {
561 if (compatible_flag
|| c
== ' ' || c
== '\n' || c
== EOF
) {
562 while (c
!= EOF
&& c
!= '\n')
581 error_with_file_and_line(current_filename
, start_lineno
,
582 "missing `.R2' line");
585 if (invalid_input_char(c
))
586 error("invalid input character code %1", int(c
));
589 start_of_line
= c
== '\n';
592 output_pending_line();
597 process_commands(line
, current_filename
, start_lineno
+ 1);
601 output_pending_line();
606 output_pending_line();
611 class label_processing_state
{
616 PENDING_LABEL_POST_PRE
,
619 label_type type
; // type of pending labels
620 int count
; // number of pending labels
621 reference
**rptr
; // pointer to next reference
622 int rcount
; // number of references left
624 int handle_pending(int c
);
626 label_processing_state(reference
**, int, FILE *);
627 ~label_processing_state();
631 static void output_pending_line()
633 if (label_in_text
&& !accumulate
&& ncitations
> 0) {
634 label_processing_state
state(citation
, ncitations
, outfp
);
635 int len
= pending_line
.length();
636 for (int i
= 0; i
< len
; i
++)
637 state
.process((unsigned char)(pending_line
[i
]));
640 put_string(pending_line
, outfp
);
641 pending_line
.clear();
642 if (pending_lf_lines
.length() > 0) {
643 put_string(pending_lf_lines
, outfp
);
644 pending_lf_lines
.clear();
647 immediately_output_references();
649 fprintf(outfp
, ".lf %d %s\n", current_lineno
, current_filename
);
654 static void split_punct(string
&line
, string
&punct
)
656 const char *start
= line
.contents();
657 const char *end
= start
+ line
.length();
658 const char *ptr
= start
;
659 const char *last_token_start
= 0;
663 last_token_start
= ptr
;
664 if (*ptr
== PRE_LABEL_MARKER
|| *ptr
== POST_LABEL_MARKER
665 || (*ptr
>= LABEL_MARKER
&& *ptr
< LABEL_MARKER
+ N_LABEL_TYPES
))
667 else if (!get_token(&ptr
, end
))
670 if (last_token_start
) {
671 const token_info
*ti
= lookup_token(last_token_start
, end
);
672 if (ti
->is_punct()) {
673 punct
.append(last_token_start
, end
- last_token_start
);
674 line
.set_length(last_token_start
- start
);
679 static void divert_to_temporary_file()
684 static void store_citation(reference
*ref
)
686 if (ncitations
>= citation_max
) {
688 citation
= new reference
*[citation_max
= 100];
690 reference
**old_citation
= citation
;
692 citation
= new reference
*[citation_max
];
693 memcpy(citation
, old_citation
, ncitations
*sizeof(reference
*));
694 a_delete old_citation
;
697 citation
[ncitations
++] = ref
;
700 static unsigned store_reference(const string
&str
)
702 if (reference_hash_table
== 0) {
703 reference_hash_table
= new reference
*[17];
704 hash_table_size
= 17;
705 for (int i
= 0; i
< hash_table_size
; i
++)
706 reference_hash_table
[i
] = 0;
709 reference
*ref
= make_reference(str
, &flags
);
710 ref
->compute_hash_code();
711 unsigned h
= ref
->hash();
713 for (ptr
= reference_hash_table
+ (h
% hash_table_size
);
715 ((ptr
== reference_hash_table
)
716 ? (ptr
= reference_hash_table
+ hash_table_size
- 1)
718 if (same_reference(**ptr
, *ref
))
721 if (ref
->is_merged())
722 warning("fields ignored because reference already used");
728 ref
->set_number(nreferences
);
730 ref
->pre_compute_label();
731 ref
->compute_sort_key();
732 if (nreferences
*2 >= hash_table_size
) {
734 reference
**old_table
= reference_hash_table
;
735 int old_size
= hash_table_size
;
736 hash_table_size
= next_size(hash_table_size
);
737 reference_hash_table
= new reference
*[hash_table_size
];
739 for (i
= 0; i
< hash_table_size
; i
++)
740 reference_hash_table
[i
] = 0;
741 for (i
= 0; i
< old_size
; i
++)
744 for (p
= (reference_hash_table
745 + (old_table
[i
]->hash() % hash_table_size
));
747 ((p
== reference_hash_table
)
748 ? (p
= reference_hash_table
+ hash_table_size
- 1)
761 unsigned immediately_handle_reference(const string
&str
)
764 reference
*ref
= make_reference(str
, &flags
);
765 ref
->set_number(nreferences
);
766 if (label_in_text
|| label_in_reference
) {
767 ref
->pre_compute_label();
768 ref
->immediate_compute_label();
775 static void immediately_output_references()
777 for (int i
= 0; i
< ncitations
; i
++) {
778 reference
*ref
= citation
[i
];
779 if (label_in_reference
) {
780 fputs(".ds [F ", outfp
);
781 const string
&label
= ref
->get_label(NORMAL_LABEL
);
782 if (label
.length() > 0
783 && (label
[0] == ' ' || label
[0] == '\\' || label
[0] == '"'))
785 put_string(label
, outfp
);
794 static void output_citation_group(reference
**v
, int n
, label_type type
,
797 if (sort_adjacent_labels
) {
798 // Do an insertion sort. Usually n will be very small.
799 for (int i
= 1; i
< n
; i
++) {
800 int num
= v
[i
]->get_number();
801 reference
*temp
= v
[i
];
803 for (j
= i
- 1; j
>= 0 && v
[j
]->get_number() > num
; j
--)
808 // This messes up if !accumulate.
809 if (accumulate
&& n
> 1) {
812 for (int i
= 1; i
< n
; i
++)
813 if (v
[i
]->get_label(type
) != v
[i
- 1]->get_label(type
))
818 for (int i
= 0; i
< n
; i
++) {
819 int nmerged
= v
[i
]->merge_labels(v
+ i
+ 1, n
- i
- 1, type
, merged_label
);
821 put_string(merged_label
, fp
);
825 put_string(v
[i
]->get_label(type
), fp
);
827 put_string(sep_label
, fp
);
832 label_processing_state::label_processing_state(reference
**p
, int n
, FILE *f
)
833 : state(NORMAL
), count(0), rptr(p
), rcount(n
), fp(f
)
837 label_processing_state::~label_processing_state()
839 int handled
= handle_pending(EOF
);
844 int label_processing_state::handle_pending(int c
)
850 if (c
== POST_LABEL_MARKER
) {
851 state
= PENDING_LABEL_POST
;
855 output_citation_group(rptr
, count
, type
, fp
);
861 case PENDING_LABEL_POST
:
862 if (c
== PRE_LABEL_MARKER
) {
863 state
= PENDING_LABEL_POST_PRE
;
867 output_citation_group(rptr
, count
, type
, fp
);
870 put_string(post_label
, fp
);
874 case PENDING_LABEL_POST_PRE
:
875 if (c
>= LABEL_MARKER
876 && c
< LABEL_MARKER
+ N_LABEL_TYPES
877 && c
- LABEL_MARKER
== type
) {
879 state
= PENDING_LABEL
;
883 output_citation_group(rptr
, count
, type
, fp
);
886 put_string(sep_label
, fp
);
891 if (c
== PRE_LABEL_MARKER
) {
892 put_string(sep_label
, fp
);
897 put_string(post_label
, fp
);
905 void label_processing_state::process(int c
)
907 if (handle_pending(c
))
909 assert(state
== NORMAL
);
911 case PRE_LABEL_MARKER
:
912 put_string(pre_label
, fp
);
915 case POST_LABEL_MARKER
:
916 state
= PENDING_POST
;
919 case LABEL_MARKER
+ 1:
921 state
= PENDING_LABEL
;
922 type
= label_type(c
- LABEL_MARKER
);
933 int rcompare(const void *p1
, const void *p2
)
935 return compare_reference(**(reference
**)p1
, **(reference
**)p2
);
940 void output_references()
943 if (!hash_table_size
) {
944 if (have_bibliography
)
945 error("nothing to reference (probably `bibliography' before `sort')");
950 if (nreferences
> 0) {
953 for (i
= 0; i
< hash_table_size
; i
++)
954 if (reference_hash_table
[i
] != 0)
955 reference_hash_table
[j
++] = reference_hash_table
[i
];
956 assert(j
== nreferences
);
957 for (; j
< hash_table_size
; j
++)
958 reference_hash_table
[j
] = 0;
959 qsort(reference_hash_table
, nreferences
, sizeof(reference
*), rcompare
);
960 for (i
= 0; i
< nreferences
; i
++)
961 reference_hash_table
[i
]->set_number(i
);
962 compute_labels(reference_hash_table
, nreferences
);
964 if (outfp
!= stdout
) {
967 label_processing_state
state(citation
, ncitations
, stdout
);
969 while ((c
= getc(outfp
)) != EOF
)
976 if (nreferences
> 0) {
977 fputs(".]<\n", outfp
);
978 for (int i
= 0; i
< nreferences
; i
++) {
979 if (sort_fields
.length() > 0)
980 reference_hash_table
[i
]->print_sort_key_comment(outfp
);
981 if (label_in_reference
) {
982 fputs(".ds [F ", outfp
);
983 const string
&label
= reference_hash_table
[i
]->get_label(NORMAL_LABEL
);
984 if (label
.length() > 0
985 && (label
[0] == ' ' || label
[0] == '\\' || label
[0] == '"'))
987 put_string(label
, outfp
);
990 reference_hash_table
[i
]->output(outfp
);
991 delete reference_hash_table
[i
];
992 reference_hash_table
[i
] = 0;
994 fputs(".]>\n", outfp
);
1000 static reference
*find_reference(const char *query
, int query_len
)
1002 // This is so that error messages look better.
1003 while (query_len
> 0 && csspace(query
[query_len
- 1]))
1006 for (int i
= 0; i
< query_len
; i
++)
1007 str
+= query
[i
] == '\n' ? ' ' : query
[i
];
1009 possibly_load_default_database();
1010 search_list_iterator
iter(&database_list
, str
.contents());
1014 if (!iter
.next(&start
, &len
, &rid
)) {
1015 error("no matches for `%1'", str
.contents());
1018 const char *end
= start
+ len
;
1019 while (start
< end
) {
1022 while (start
< end
&& *start
++ != '\n')
1026 error("found a reference for `%1' but it didn't contain any fields",
1030 reference
*result
= new reference(start
, end
- start
, &rid
);
1031 if (iter
.next(&start
, &len
, &rid
))
1032 warning("multiple matches for `%1'", str
.contents());
1036 static reference
*make_reference(const string
&str
, unsigned *flagsp
)
1038 const char *start
= str
.contents();
1039 const char *end
= start
+ str
.length();
1040 const char *ptr
= start
;
1044 while (ptr
< end
&& *ptr
++ != '\n')
1048 for (; start
< ptr
; start
++) {
1050 *flagsp
= (SHORT_LABEL
| (*flagsp
& (FORCE_RIGHT_BRACKET
1051 | FORCE_LEFT_BRACKET
)));
1052 else if (*start
== '[')
1053 *flagsp
|= FORCE_LEFT_BRACKET
;
1054 else if (*start
== ']')
1055 *flagsp
|= FORCE_RIGHT_BRACKET
;
1056 else if (!csspace(*start
))
1060 error("empty reference");
1061 return new reference
;
1063 reference
*database_ref
= 0;
1065 database_ref
= find_reference(start
, ptr
- start
);
1066 reference
*inline_ref
= 0;
1068 inline_ref
= new reference(ptr
, end
- ptr
);
1071 database_ref
->merge(*inline_ref
);
1073 return database_ref
;
1078 else if (database_ref
)
1079 return database_ref
;
1081 return new reference
;
1084 static void do_ref(const string
&str
)
1087 (void)store_reference(str
);
1089 (void)immediately_handle_reference(str
);
1090 immediately_output_references();
1094 static void trim_blanks(string
&str
)
1096 const char *start
= str
.contents();
1097 const char *end
= start
+ str
.length();
1098 while (end
> start
&& end
[-1] != '\n' && csspace(end
[-1]))
1100 str
.set_length(end
- start
);
1103 void do_bib(const char *filename
)
1106 if (strcmp(filename
, "-") == 0)
1110 fp
= fopen(filename
, "r");
1112 error("can't open `%1': %2", filename
, strerror(errno
));
1115 current_filename
= filename
;
1118 START
, MIDDLE
, BODY
, BODY_START
, BODY_BLANK
, BODY_DOT
1125 if (invalid_input_char(c
)) {
1126 error("invalid input character code %1", c
);
1154 else if (csspace(c
)) {
1169 else if (csspace(c
))
1184 state
= c
== '\n' ? BODY_START
: BODY
;
1213 // from the Dragon Book
1215 unsigned hash_string(const char *s
, int len
)
1217 const char *end
= s
+ len
;
1222 if ((g
= h
& 0xf0000000) != 0) {
1230 int next_size(int n
)
1232 static const int table_sizes
[] = {
1233 101, 503, 1009, 2003, 3001, 4001, 5003, 10007, 20011, 40009,
1234 80021, 160001, 500009, 1000003, 2000003, 4000037, 8000009,
1235 16000057, 32000011, 64000031, 128000003, 0
1239 for (p
= table_sizes
; *p
<= n
&& *p
!= 0; p
++)