2 /* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.com)
5 This file is part of groff.
7 groff is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 groff is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with groff; see the file COPYING. If not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
28 const char PRE_LABEL_MARKER
= '\013';
29 const char POST_LABEL_MARKER
= '\014';
30 const char LABEL_MARKER
= '\015'; // label_type is added on
32 #define FORCE_LEFT_BRACKET 04
33 #define FORCE_RIGHT_BRACKET 010
35 static FILE *outfp
= stdout
;
37 string capitalize_fields
;
38 string reverse_fields
;
39 string abbreviate_fields
;
40 string period_before_last_name
= ". ";
41 string period_before_initial
= ".";
42 string period_before_hyphen
= "";
43 string period_before_other
= ". ";
45 int annotation_field
= -1;
46 string annotation_macro
;
47 string discard_fields
= "XYZ";
48 string pre_label
= "\\*([.";
49 string post_label
= "\\*(.]";
50 string sep_label
= ", ";
52 int move_punctuation
= 0;
53 int abbreviate_label_ranges
= 0;
54 string label_range_indicator
;
55 int label_in_text
= 1;
56 int label_in_reference
= 1;
57 int date_as_label
= 0;
58 int sort_adjacent_labels
= 0;
59 // Join exactly two authors with this.
60 string join_authors_exactly_two
= " and ";
61 // When there are more than two authors join the last two with this.
62 string join_authors_last_two
= ", and ";
63 // Otherwise join authors with this.
64 string join_authors_default
= ", ";
65 string separate_label_second_parts
= ", ";
66 // Use this string to represent that there are other authors.
67 string et_al
= " et al";
68 // Use et al only if it can replace at least this many authors.
69 int et_al_min_elide
= 2;
70 // Use et al only if the total number of authors is at least this.
71 int et_al_min_total
= 3;
74 int compatible_flag
= 0;
76 int short_label_flag
= 0;
78 static int recognize_R1_R2
= 1;
80 search_list database_list
;
81 int search_default
= 1;
82 static int default_database_loaded
= 0;
84 static reference
**citation
= 0;
85 static int ncitations
= 0;
86 static int citation_max
= 0;
88 static reference
**reference_hash_table
= 0;
89 static int hash_table_size
;
90 static int nreferences
= 0;
92 static int need_syncing
= 0;
94 string pending_lf_lines
;
96 static void output_pending_line();
97 static unsigned immediately_handle_reference(const string
&);
98 static void immediately_output_references();
99 static unsigned store_reference(const string
&);
100 static void divert_to_temporary_file();
101 static reference
*make_reference(const string
&, unsigned *);
103 static void do_file(const char *);
104 static void split_punct(string
&line
, string
&punct
);
105 static void output_citation_group(reference
**v
, int n
, label_type
, FILE *fp
);
106 static void possibly_load_default_database();
108 int main(int argc
, char **argv
)
110 program_name
= argv
[0];
111 static char stderr_buf
[BUFSIZ
];
112 setbuf(stderr
, stderr_buf
);
114 int finished_options
= 0;
119 !finished_options
&& argc
> 0 && argv
[0][0] == '-'
120 && argv
[0][1] != '\0';
122 const char *opt
= argv
[0] + 1;
123 while (opt
!= 0 && *opt
!= '\0') {
131 label_in_reference
= 0;
135 annotation_field
= 'X';
136 annotation_macro
= "AP";
138 else if (csalnum(opt
[0]) && opt
[1] == '.' && opt
[2] != '\0') {
139 annotation_field
= opt
[0];
140 annotation_macro
= opt
+ 2;
145 move_punctuation
= 1;
153 // Not a very useful spec.
154 set_label_spec("(A.n|Q)', '(D.y|D)");
168 if (*++opt
== '\0') {
174 error("option `f' requires an argument");
182 for (const char *ptr
= num
; *ptr
; ptr
++)
183 if (!csdigit(*ptr
)) {
184 error("bad character `%1' in argument to -f option", *ptr
);
192 set_label_spec(spec
.contents());
199 label_in_reference
= 0;
207 capitalize_fields
= ++opt
;
217 error("bad field name `%1'", *opt
++);
230 for (const char *ptr
= ++opt
; *ptr
; ptr
++)
231 if (!csdigit(*ptr
)) {
232 error("argument to `a' option not a number");
236 reverse_fields
= 'A';
237 reverse_fields
+= opt
;
243 linear_ignore_fields
= ++opt
;
248 char buf
[INT_DIGITS
*2 + 11]; // A.n+2D.y-3%a
250 if (*++opt
!= '\0' && *opt
!= ',') {
252 long n
= strtol(opt
, &ptr
, 10);
253 if (n
== 0 && ptr
== opt
) {
254 error("bad integer `%1' in `l' option", opt
);
261 sprintf(strchr(buf
, '\0'), "+%d", n
);
268 long n
= strtol(opt
, &ptr
, 10);
269 if (n
== 0 && ptr
== opt
) {
270 error("bad integer `%1' in `l' option", opt
);
276 sprintf(strchr(buf
, '\0'), "-%d", n
);
279 error("argument to `l' option not of form `m,n'");
282 if (!set_label_spec(buf
))
293 const char *filename
= 0;
294 if (*++opt
== '\0') {
300 error("option `p' requires an argument");
308 database_list
.add_file(filename
);
323 long n
= strtol(opt
, &ptr
, 10);
324 if (n
== 0 && ptr
== opt
) {
325 error("bad integer `%1' in `t' option", opt
);
331 linear_truncate_len
= int(n
);
337 extern const char *version_string
;
338 fprintf(stderr
, "GNU refer version %s\n", version_string
);
344 if (opt
[1] == '\0') {
345 finished_options
= 1;
351 error("unrecognized option `%1'", *opt
);
358 set_label_spec("%1");
366 for (int i
= 0; i
< argc
; i
++) {
375 if (fflush(stdout
) < 0)
376 fatal("output error");
383 "usage: %s [-benvCPRS] [-aN] [-cXYZ] [-fN] [-iXYZ] [-kX] [-lM,N] [-p file]\n"
384 " [-sXYZ] [-tN] [-BL.M] [files ...]\n",
389 static void possibly_load_default_database()
391 if (search_default
&& !default_database_loaded
) {
392 char *filename
= getenv("REFER");
394 database_list
.add_file(filename
);
396 database_list
.add_file(DEFAULT_INDEX
, 1);
397 default_database_loaded
= 1;
401 static int is_list(const string
&str
)
403 const char *start
= str
.contents();
404 const char *end
= start
+ str
.length();
405 while (end
> start
&& csspace(end
[-1]))
407 while (start
< end
&& csspace(*start
))
409 return end
- start
== 6 && memcmp(start
, "$LIST$", 6) == 0;
412 static void do_file(const char *filename
)
415 if (strcmp(filename
, "-") == 0) {
420 fp
= fopen(filename
, "r");
422 error("can't open `%1': %2", filename
, strerror(errno
));
425 current_filename
= filename
;
427 fprintf(outfp
, ".lf 1 %s\n", filename
);
435 if (line
.length() > 0)
439 if (illegal_input_char(c
))
440 error("illegal input character code %1", c
);
447 int len
= line
.length();
451 if (len
>= 2 && line
[0] == '.' && line
[1] == '[') {
452 int start_lineno
= current_lineno
;
453 int start_of_line
= 1;
456 string
pre(line
.contents() + 2, line
.length() - 3);
460 error_with_file_and_line(current_filename
, start_lineno
,
461 "missing `.]' line");
466 if (start_of_line
&& c
== '.') {
469 while ((d
= getc(fp
)) != '\n' && d
!= EOF
) {
470 if (illegal_input_char(d
))
471 error("illegal input character code %1", d
);
480 if (illegal_input_char(c
))
481 error("illegal input character code %1", c
);
484 start_of_line
= (c
== '\n');
487 output_pending_line();
491 error("found `$LIST$' but not accumulating references");
494 unsigned flags
= (accumulate
495 ? store_reference(str
)
496 : immediately_handle_reference(str
));
498 if (accumulate
&& outfp
== stdout
)
499 divert_to_temporary_file();
500 if (pending_line
.length() == 0) {
501 warning("can't attach citation to previous line");
504 pending_line
.set_length(pending_line
.length() - 1);
506 if (move_punctuation
)
507 split_punct(pending_line
, punct
);
508 int have_text
= pre
.length() > 0 || post
.length() > 0;
509 label_type lt
= label_type(flags
& ~(FORCE_LEFT_BRACKET
510 |FORCE_RIGHT_BRACKET
));
511 if ((flags
& FORCE_LEFT_BRACKET
) || !have_text
)
512 pending_line
+= PRE_LABEL_MARKER
;
514 pending_line
+= LABEL_MARKER
+ lt
;
515 pending_line
+= post
;
516 if ((flags
& FORCE_RIGHT_BRACKET
) || !have_text
)
517 pending_line
+= POST_LABEL_MARKER
;
518 pending_line
+= punct
;
519 pending_line
+= '\n';
525 && line
[0] == '.' && line
[1] == 'l' && line
[2] == 'f'
526 && (compatible_flag
|| line
[3] == '\n' || line
[3] == ' ')) {
527 pending_lf_lines
+= line
;
529 if (interpret_lf_args(line
.contents() + 3))
532 else if (recognize_R1_R2
534 && line
[0] == '.' && line
[1] == 'R' && line
[2] == '1'
535 && (compatible_flag
|| line
[3] == '\n' || line
[3] == ' ')) {
537 int start_of_line
= 1;
538 int start_lineno
= current_lineno
;
541 if (c
!= EOF
&& start_of_line
)
543 if (start_of_line
&& c
== '.') {
549 if (compatible_flag
|| c
== ' ' || c
== '\n' || c
== EOF
) {
550 while (c
!= EOF
&& c
!= '\n')
569 error_with_file_and_line(current_filename
, start_lineno
,
570 "missing `.R2' line");
573 if (illegal_input_char(c
))
574 error("illegal input character code %1", int(c
));
577 start_of_line
= c
== '\n';
580 output_pending_line();
585 process_commands(line
, current_filename
, start_lineno
+ 1);
589 output_pending_line();
594 output_pending_line();
599 class label_processing_state
{
604 PENDING_LABEL_POST_PRE
,
607 label_type type
; // type of pending labels
608 int count
; // number of pending labels
609 reference
**rptr
; // pointer to next reference
610 int rcount
; // number of references left
612 int handle_pending(int c
);
614 label_processing_state(reference
**, int, FILE *);
615 ~label_processing_state();
619 static void output_pending_line()
621 if (label_in_text
&& !accumulate
&& ncitations
> 0) {
622 label_processing_state
state(citation
, ncitations
, outfp
);
623 int len
= pending_line
.length();
624 for (int i
= 0; i
< len
; i
++)
625 state
.process((unsigned char)(pending_line
[i
]));
628 put_string(pending_line
, outfp
);
629 pending_line
.clear();
630 if (pending_lf_lines
.length() > 0) {
631 put_string(pending_lf_lines
, outfp
);
632 pending_lf_lines
.clear();
635 immediately_output_references();
637 fprintf(outfp
, ".lf %d %s\n", current_lineno
, current_filename
);
642 static void split_punct(string
&line
, string
&punct
)
644 const char *start
= line
.contents();
645 const char *end
= start
+ line
.length();
646 const char *ptr
= start
;
647 const char *last_token_start
= 0;
651 last_token_start
= ptr
;
652 if (*ptr
== PRE_LABEL_MARKER
|| *ptr
== POST_LABEL_MARKER
653 || (*ptr
>= LABEL_MARKER
&& *ptr
< LABEL_MARKER
+ N_LABEL_TYPES
))
655 else if (!get_token(&ptr
, end
))
658 if (last_token_start
) {
659 const token_info
*ti
= lookup_token(last_token_start
, end
);
660 if (ti
->is_punct()) {
661 punct
.append(last_token_start
, end
- last_token_start
);
662 line
.set_length(last_token_start
- start
);
667 static void divert_to_temporary_file()
672 static void store_citation(reference
*ref
)
674 if (ncitations
>= citation_max
) {
676 citation
= new reference
*[citation_max
= 100];
678 reference
**old_citation
= citation
;
680 citation
= new reference
*[citation_max
];
681 memcpy(citation
, old_citation
, ncitations
*sizeof(reference
*));
682 a_delete old_citation
;
685 citation
[ncitations
++] = ref
;
688 static unsigned store_reference(const string
&str
)
690 if (reference_hash_table
== 0) {
691 reference_hash_table
= new reference
*[17];
692 hash_table_size
= 17;
693 for (int i
= 0; i
< hash_table_size
; i
++)
694 reference_hash_table
[i
] = 0;
697 reference
*ref
= make_reference(str
, &flags
);
698 ref
->compute_hash_code();
699 unsigned h
= ref
->hash();
700 for (reference
**ptr
= reference_hash_table
+ (h
% hash_table_size
);
702 ((ptr
== reference_hash_table
)
703 ? (ptr
= reference_hash_table
+ hash_table_size
- 1)
705 if (same_reference(**ptr
, *ref
))
708 if (ref
->is_merged())
709 warning("fields ignored because reference already used");
715 ref
->set_number(nreferences
);
717 ref
->pre_compute_label();
718 ref
->compute_sort_key();
719 if (nreferences
*2 >= hash_table_size
) {
721 reference
**old_table
= reference_hash_table
;
722 int old_size
= hash_table_size
;
723 hash_table_size
= next_size(hash_table_size
);
724 reference_hash_table
= new reference
*[hash_table_size
];
726 for (i
= 0; i
< hash_table_size
; i
++)
727 reference_hash_table
[i
] = 0;
728 for (i
= 0; i
< old_size
; i
++)
730 for (reference
**p
= (reference_hash_table
731 + (old_table
[i
]->hash() % hash_table_size
));
733 ((p
== reference_hash_table
)
734 ? (p
= reference_hash_table
+ hash_table_size
- 1)
747 unsigned immediately_handle_reference(const string
&str
)
750 reference
*ref
= make_reference(str
, &flags
);
751 ref
->set_number(nreferences
);
752 if (label_in_text
|| label_in_reference
) {
753 ref
->pre_compute_label();
754 ref
->immediate_compute_label();
761 static void immediately_output_references()
763 for (int i
= 0; i
< ncitations
; i
++) {
764 reference
*ref
= citation
[i
];
765 if (label_in_reference
) {
766 fputs(".ds [F ", outfp
);
767 const string
&label
= ref
->get_label(NORMAL_LABEL
);
768 if (label
.length() > 0
769 && (label
[0] == ' ' || label
[0] == '\\' || label
[0] == '"'))
771 put_string(label
, outfp
);
780 static void output_citation_group(reference
**v
, int n
, label_type type
,
783 if (sort_adjacent_labels
) {
784 // Do an insertion sort. Usually n will be very small.
785 for (int i
= 1; i
< n
; i
++) {
786 int num
= v
[i
]->get_number();
787 reference
*temp
= v
[i
];
788 for (int j
= i
- 1; j
>= 0 && v
[j
]->get_number() > num
; j
--)
793 // This messes up if !accumulate.
794 if (accumulate
&& n
> 1) {
797 for (int i
= 1; i
< n
; i
++)
798 if (v
[i
]->get_label(type
) != v
[i
- 1]->get_label(type
))
803 for (int i
= 0; i
< n
; i
++) {
804 int nmerged
= v
[i
]->merge_labels(v
+ i
+ 1, n
- i
- 1, type
, merged_label
);
806 put_string(merged_label
, fp
);
810 put_string(v
[i
]->get_label(type
), fp
);
812 put_string(sep_label
, fp
);
817 label_processing_state::label_processing_state(reference
**p
, int n
, FILE *f
)
818 : state(NORMAL
), count(0), rptr(p
), rcount(n
), fp(f
)
822 label_processing_state::~label_processing_state()
824 int handled
= handle_pending(EOF
);
829 int label_processing_state::handle_pending(int c
)
835 if (c
== POST_LABEL_MARKER
) {
836 state
= PENDING_LABEL_POST
;
840 output_citation_group(rptr
, count
, type
, fp
);
846 case PENDING_LABEL_POST
:
847 if (c
== PRE_LABEL_MARKER
) {
848 state
= PENDING_LABEL_POST_PRE
;
852 output_citation_group(rptr
, count
, type
, fp
);
855 put_string(post_label
, fp
);
859 case PENDING_LABEL_POST_PRE
:
860 if (c
>= LABEL_MARKER
861 && c
< LABEL_MARKER
+ N_LABEL_TYPES
862 && c
- LABEL_MARKER
== type
) {
864 state
= PENDING_LABEL
;
868 output_citation_group(rptr
, count
, type
, fp
);
871 put_string(sep_label
, fp
);
876 if (c
== PRE_LABEL_MARKER
) {
877 put_string(sep_label
, fp
);
882 put_string(post_label
, fp
);
890 void label_processing_state::process(int c
)
892 if (handle_pending(c
))
894 assert(state
== NORMAL
);
896 case PRE_LABEL_MARKER
:
897 put_string(pre_label
, fp
);
900 case POST_LABEL_MARKER
:
901 state
= PENDING_POST
;
904 case LABEL_MARKER
+ 1:
906 state
= PENDING_LABEL
;
907 type
= label_type(c
- LABEL_MARKER
);
918 static int rcompare(const void *p1
, const void *p2
)
920 return compare_reference(**(reference
**)p1
, **(reference
**)p2
);
925 void output_references()
928 if (nreferences
> 0) {
931 for (i
= 0; i
< hash_table_size
; i
++)
932 if (reference_hash_table
[i
] != 0)
933 reference_hash_table
[j
++] = reference_hash_table
[i
];
934 assert(j
== nreferences
);
935 for (; j
< hash_table_size
; j
++)
936 reference_hash_table
[j
] = 0;
937 qsort(reference_hash_table
, nreferences
, sizeof(reference
*), rcompare
);
938 for (i
= 0; i
< nreferences
; i
++)
939 reference_hash_table
[i
]->set_number(i
);
940 compute_labels(reference_hash_table
, nreferences
);
942 if (outfp
!= stdout
) {
945 label_processing_state
state(citation
, ncitations
, stdout
);
947 while ((c
= getc(outfp
)) != EOF
)
954 if (nreferences
> 0) {
955 fputs(".]<\n", outfp
);
956 for (int i
= 0; i
< nreferences
; i
++) {
957 if (sort_fields
.length() > 0)
958 reference_hash_table
[i
]->print_sort_key_comment(outfp
);
959 if (label_in_reference
) {
960 fputs(".ds [F ", outfp
);
961 const string
&label
= reference_hash_table
[i
]->get_label(NORMAL_LABEL
);
962 if (label
.length() > 0
963 && (label
[0] == ' ' || label
[0] == '\\' || label
[0] == '"'))
965 put_string(label
, outfp
);
968 reference_hash_table
[i
]->output(outfp
);
969 delete reference_hash_table
[i
];
970 reference_hash_table
[i
] = 0;
972 fputs(".]>\n", outfp
);
978 static reference
*find_reference(const char *query
, int query_len
)
980 // This is so that error messages look better.
981 while (query_len
> 0 && csspace(query
[query_len
- 1]))
984 for (int i
= 0; i
< query_len
; i
++)
985 str
+= query
[i
] == '\n' ? ' ' : query
[i
];
987 possibly_load_default_database();
988 search_list_iterator
iter(&database_list
, str
.contents());
992 if (!iter
.next(&start
, &len
, &rid
)) {
993 error("no matches for `%1'", str
.contents());
996 const char *end
= start
+ len
;
997 while (start
< end
) {
1000 while (start
< end
&& *start
++ != '\n')
1004 error("found a reference for `%1' but it didn't contain any fields",
1008 reference
*result
= new reference(start
, end
- start
, &rid
);
1009 if (iter
.next(&start
, &len
, &rid
))
1010 warning("multiple matches for `%1'", str
.contents());
1014 static reference
*make_reference(const string
&str
, unsigned *flagsp
)
1016 const char *start
= str
.contents();
1017 const char *end
= start
+ str
.length();
1018 const char *ptr
= start
;
1022 while (ptr
< end
&& *ptr
++ != '\n')
1026 for (; start
< ptr
; start
++) {
1028 *flagsp
= (SHORT_LABEL
| (*flagsp
& (FORCE_RIGHT_BRACKET
1029 | FORCE_LEFT_BRACKET
)));
1030 else if (*start
== '[')
1031 *flagsp
|= FORCE_LEFT_BRACKET
;
1032 else if (*start
== ']')
1033 *flagsp
|= FORCE_RIGHT_BRACKET
;
1034 else if (!csspace(*start
))
1038 error("empty reference");
1039 return new reference
;
1041 reference
*database_ref
= 0;
1043 database_ref
= find_reference(start
, ptr
- start
);
1044 reference
*inline_ref
= 0;
1046 inline_ref
= new reference(ptr
, end
- ptr
);
1049 database_ref
->merge(*inline_ref
);
1051 return database_ref
;
1056 else if (database_ref
)
1057 return database_ref
;
1059 return new reference
;
1062 static void do_ref(const string
&str
)
1065 (void)store_reference(str
);
1067 (void)immediately_handle_reference(str
);
1068 immediately_output_references();
1072 static void trim_blanks(string
&str
)
1074 const char *start
= str
.contents();
1075 const char *end
= start
+ str
.length();
1076 while (end
> start
&& end
[-1] != '\n' && csspace(end
[-1]))
1078 str
.set_length(end
- start
);
1081 void do_bib(const char *filename
)
1084 if (strcmp(filename
, "-") == 0)
1088 fp
= fopen(filename
, "r");
1090 error("can't open `%1': %2", filename
, strerror(errno
));
1093 current_filename
= filename
;
1096 START
, MIDDLE
, BODY
, BODY_START
, BODY_BLANK
, BODY_DOT
1103 if (illegal_input_char(c
)) {
1104 error("illegal input character code %1", c
);
1132 else if (csspace(c
)) {
1147 else if (csspace(c
))
1162 state
= c
== '\n' ? BODY_START
: BODY
;
1191 // from the Dragon Book
1193 unsigned hash_string(const char *s
, int len
)
1195 const char *end
= s
+ len
;
1200 if ((g
= h
& 0xf0000000) != 0) {
1208 int next_size(int n
)
1210 static const int table_sizes
[] = {
1211 101, 503, 1009, 2003, 3001, 4001, 5003, 10007, 20011, 40009,
1212 80021, 160001, 500009, 1000003, 2000003, 4000037, 8000009,
1213 16000057, 32000011, 64000031, 128000003, 0
1216 for (const int *p
= table_sizes
; *p
<= n
&& *p
!= 0; p
++)