forked from ge-ne/bibtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey.c
2559 lines (2462 loc) · 112 KB
/
key.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*** key.c ********************************************************************
**
** This file is part of BibTool.
** It is distributed under the GNU General Public License.
** See the file COPYING for details.
**
** (c) 1996-2017 Gerd Neugebauer
**
** Net: [email protected]
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2, or (at your option)
** any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
******************************************************************************/
#include <bibtool/general.h>
#include <bibtool/symbols.h>
#include <bibtool/type.h>
#include <bibtool/entry.h>
#include <bibtool/error.h>
#include <bibtool/key.h>
#include <bibtool/rsc.h>
#include <bibtool/names.h>
#include <bibtool/tex_read.h>
#include <bibtool/wordlist.h>
#include <bibtool/expand.h>
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#include "config.h"
/*****************************************************************************/
/* Internal Programs */
/*===========================================================================*/
#ifdef __STDC__
#define _ARG(A) A
#else
#define _ARG(A) ()
#endif
static KeyNode new_key_node _ARG((int type,Symbol sym));/* key.c */
static bool fmt_digits _ARG((StringBuffer *sb,String s,int mp,int pp,int n,int sel,bool trunc));/* key.c*/
static bool fmt_c_words _ARG((String line,int min,int max,bool not,bool ignore));/* key.c*/
static bool fmt_c_names _ARG((String line,int min,int max,bool not));/* key.c*/
static bool fmt_c_string _ARG((String s,int min,int max,bool not));/* key.c */
static bool add_fmt_tree _ARG((char *s,KeyNode *treep));/* key.c */
static bool eval__fmt _ARG((StringBuffer *sb,KeyNode kn,Record rec));/* key.c*/
static bool eval_fmt _ARG((StringBuffer *sb,KeyNode kn,Record rec,DB db));/* key.c*/
static char * itostr _ARG((int i,char *digits)); /* key.c */
static int deTeX _ARG((String line,void (*save_fct)_ARG((String)),int commap));/*key.c*/
static int fmt__parse _ARG((char **sp,KeyNode *knp));/* key.c */
static int fmt_parse _ARG((char **sp,KeyNode *knp));/* key.c */
static void Push_Word _ARG((String s)); /* key.c */
static void eval__special _ARG((StringBuffer *sb,KeyNode kn,Record rec));/* key.c*/
static void fmt_names _ARG((StringBuffer *sb,String line,int maxname,int post,String trans));/* key.c*/
static void fmt_string _ARG((StringBuffer *sb,String s,int n,String trans,String sep));/* key.c*/
static void fmt_title _ARG((StringBuffer *sb,String line,int len,int in,String trans,bool ignore,Symbol sep));/* key.c*/
static void init_key _ARG(()); /* key.c */
static void key_init _ARG((void)); /* key.c */
static void push_s _ARG((StringBuffer *sb,String s,int max,String trans));/* key.c*/
static void push_word _ARG((String s)); /* key.c */
#ifdef DEBUG
static void show_fmt _ARG((KeyNode kn,int in)); /* key.c */
#endif
/*****************************************************************************/
/* External Programs */
/*===========================================================================*/
static DB tmp_key_db;
/*---------------------------------------------------------------------------*/
#define KEYSTYLE_EMPTY 0x0010
#define KEYSTYLE_SHORT 0x0020
#define KEYSTYLE_LONG 0x0030
#define KEYSTYLE_EXTENDED 0x0040
static char *percent_chars = "NntTdDsSwWp"; /* */
#define DetexNone 0
#define DetexLower 1
#define DetexUpper 2
static StringBuffer *key_sb = (StringBuffer*)NULL;/* */
static StringBuffer *tmp_sb = (StringBuffer*)NULL;/* */
/*---------------------------------------------------------------------------*/
static KeyNode key_tree = (KeyNode)0; /* */
static KeyNode sort_key_tree = (KeyNode)0; /* */
static NameNode format[NUMBER_OF_FORMATS]; /* */
#define SkipSpaces(CP) while (is_space(*CP)) ++(CP)
#define SkipAllowed(CP) while (is_allowed(*CP)) ++(CP)
#define ParseNumber(CP,N) if (is_digit(*CP)) { N = 0; \
while(is_digit(*CP) ){ N = N*10 + (*CP)-'0'; ++CP; }}
#define Expect(CP,C,RET) SkipSpaces(CP); \
if ( *CP == C ) { ++(CP); } \
else { ErrPrintF("*** BibTool: Missing %c",C);\
return RET; }
#define ExpectVoid(CP,C) SkipSpaces(CP); \
if ( *CP == C ) { ++(CP); } \
else { ErrPrintF("*** BibTool: Missing %c",C);\
return; }
#define MakeNode(KNP,TYPE,SP,CP) c = *CP; *CP = '\0'; \
*KNP = new_key_node(TYPE,symbol((String)*SP));\
*CP = c;
#define ParseOrReturn(CP,NODEP,MSG) \
if ((ret=fmt_parse(CP,NODEP)) != 0 ) \
{ DebugPrintF1(MSG); return ret; }
/*****************************************************************************/
/*** Private word stack ***/
/*****************************************************************************/
static String *words = (String*)NULL;
static size_t words_len = 0;
static size_t words_used = 0;
#define WordLenInc 16
#define PushWord(S) if (words_len>words_used) words[words_used++]=S; \
else Push_Word(S)
#define ResetWords words_used = 0
/*-----------------------------------------------------------------------------
** Function*: push_word()
** Purpose: Push a word to the stack. Wrapper function.
** Arguments:
** word word to push
** Returns: nothing
**___________________________________________________ */
static void push_word(word) /* */
register String word; /* */
{ PushWord(word); /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function*: Push_Word()
** Purpose: Push a word to the stack when no space is left in the
** allocated array.
** Arguments:
** s word to push
** Returns: nothing
**___________________________________________________ */
static void Push_Word(s) /* */
register String s; /* */
{ register String *wp; /* */
/* */
words_len += WordLenInc; /* */
if (words_len == WordLenInc) /* */
{ wp = (String*)malloc(words_len*sizeof(String)); }/* */
else /* */
{ wp = (String*)realloc(words,words_len*sizeof(String)); }/* */
if (wp == (String*)0) /* */
{ words_len -= WordLenInc; /* */
ERROR((String)"Push_Word() failed: Word not pushed.");/* */
return; /* */
} /* */
/* */
words = wp; /* */
PushWord(s); /* */
} /*------------------------*/
/*****************************************************************************/
/*** Key Separator Section ***/
/*****************************************************************************/
#define NoSeps 8
static Symbol *key_seps = NULL;
#define DefaultKey key_seps[0]
#define InterNameSep key_seps[1]
#define NamePreSep key_seps[2]
#define NameNameSep key_seps[3]
#define NameTitleSep key_seps[4]
#define TitleTitleSep key_seps[5]
#define KeyNumberSep key_seps[6]
#define EtAl key_seps[7]
/*-----------------------------------------------------------------------------
** Function: set_separator()
** Purpose: Modify the |key_seps| array. This array contains the
** different separators used during key formatting. The
** elements of the array have the following meaning:
** \begin{description}
** \item[0] The default key which is used when the
** formatting instruction fails completely.
** \item[1] The separator which is inserted between
** different names of a multi-authored publication.
** \item[2] The separator inserted between the first name
** and the last name when a name is formatted.
** \item[3] The separator inserted between the last names
** when more then one last name is present
** \item[4] The separator between the name and the title
** of a publication.
** \item[5] The separator inserted between words of the
** title.
** \item[6] The separator inserted before the number
** which might be added to disambiguate reference keys.
** \item[7] The string which is added when a list of
** names is truncated. (|.ea|)
** \end{description}
** Arguments:
** n Array index to modify.
** s New value for the given separator. The new value is
** stored as a symbol. Thus the memory of |s| need not to
** be preserved after this function is completed.
** The characters which are not allowed are silently suppressed.
** Returns: nothing
**___________________________________________________ */
void set_separator(n, s) /* */
register int n; /* */
String s; /* */
{ /* */
if (n < 0 || n >= NoSeps) /* */
{ ERROR("Invalid separator reference."); /* */
return; /* */
} /* */
while (*s && !is_allowed(*s)) { s++; } /* */
if (*s && !is_allowed(*s)) /* */
{ ERROR2("Invalid separator specification: ", s);/* */
return; /* */
} /* */
/* */
if (key_seps == NULL) init_key(); /* */
/* */
if (*s) /* */
{ key_seps[n] = sym_extract(&s, false); /* */
} else { /* */
key_seps[n] = sym_empty; /* */
} /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: get_separator()
** Type: String
** Purpose: Getter for the key separator. The
** elements under the index have the following meaning:
** \begin{description}
** \item[0] The default key which is used when the
** formatting instruction fails completely.
** \item[1] The separator which is inserted between
** different names of a multi-authored publication.
** \item[2] The separator inserted between the first name
** and the last name when a name is formatted.
** \item[3] The separator inserted between the last names
** when more then one last name is present
** \item[4] The separator between the name and the title
** of a publication.
** \item[5] The separator inserted between words of the
** title.
** \item[6] The separator inserted before the number
** which might be added to disambiguate reference keys.
** \item[7] The string which is added when a list of
** names is truncated. (|.ea|)
** \end{description}
** Arguments:
** n the index
** Returns: the separator for the given index or NULL
**___________________________________________________ */
Symbol get_separator(n) /* */
int n; /* */
{ /* */
if (n < 0 || n >= NoSeps) /* */
{ ERROR("Invalid separator reference."); /* */
return NULL; /* */
} /* */
return key_seps[n]; /* */
} /*------------------------*/
/*****************************************************************************/
/*** Key Base Section ***/
/*****************************************************************************/
static char * key__base[] =
{ "0123456789",
"_abcdefghijklmnopqrstuvwxyz",
"_ABCDEFGHIJKLMNOPQRSTUVWXYZ"
};
#define KEY_BASE_DIGIT 0
#define KEY_BASE_LOWER 1
#define KEY_BASE_UPPER 2
static int key_base = KEY_BASE_DIGIT;
/*-----------------------------------------------------------------------------
** Function: set_base()
** Purpose: Define the key base. This value determines the format
** of the disambiguation string added to a key if
** required. The following values are considered:
** \begin{itemize}
** \item If the value is |upper| or starts with an upper
** case letter then the disambiguation is done with
** upper-case letters.
** \item If the value is |lower| or starts with a lower
** case letter then the disambiguation is done with
** lower-case letters.
** \item If the value is |digit| or starts with an digit
** then the disambiguation is done with arabic numbers.
** \end{itemize}
** The comparison of the keywords is done case
** insensitive. The special values take precedence before
** the first character rules.
**
** If an invalid value is given to this function then an
** error is raised and the program is terminated.
** Arguments:
** value String representation of the new value.
** Returns: nothing
**___________________________________________________ */
void set_base(value) /* */
String value; /* */
{ /* */
if (case_eq(value,(String)"upper")) key_base = KEY_BASE_UPPER;/* */
else if (case_eq(value,(String)"lower")) key_base = KEY_BASE_LOWER;/* */
else if (case_eq(value,(String)"digit")) key_base = KEY_BASE_DIGIT;/* */
else if (is_upper(*value)) key_base = KEY_BASE_UPPER;/* */
else if (is_lower(*value)) key_base = KEY_BASE_LOWER;/* */
else if (is_digit(*value)) key_base = KEY_BASE_DIGIT;/* */
else { ERROR("Unknown base ignored."); } /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: get_base()
** Type: String
** Purpose: Getter for the printable representation of the key_base.
**
** Arguments: none
** Returns:
**___________________________________________________ */
String get_base() /* */
{ /* */
switch (key_base) /* */
{ case KEY_BASE_UPPER: return (String)"upper"; /* */
case KEY_BASE_LOWER: return (String)"lower"; /* */
case KEY_BASE_DIGIT: return (String)"digit"; /* */
default: return s_empty; /* */
} /* */
} /*------------------------*/
#define ITOA_LEN 64
/*-----------------------------------------------------------------------------
** Function*: itostr()
** Purpose: Translate number using the ``digits'' given.
** A static string is returned containing the result.
** This is a routine generalizing |itoa()|.
** Examples:
** itostr(X,"0123456789ABCDEF") returns the hexadecimal representation
** itostr(X,"0123456789") returns the decimal representation
** itostr(X,"01234567") returns the octal representation
** itostr(X,"01") returns the binary representation
**
** Arguments:
** i Integer to translate
** digits String of digits to use
** Returns:
**___________________________________________________ */
static char * itostr(i,digits) /* */
register int i; /* */
register char *digits; /* */
{ static char buffer[ITOA_LEN]; /* buffer to store result */
register char *bp; /* buffer pointer */
register int sign, /* */
base; /* */
/* */
base = strlen(digits); /* how many digits? */
bp = buffer+ITOA_LEN-1; /* set pointer to the end.*/
*(bp--) = '\0'; /* mark the end. */
if (i < 0) { sign = -1; i = -i; } /* */
else { sign = 0; if ( i == 0 ) *(bp--) = '0'; } /* */
while (i > 0) /* */
{ *(bp--) = digits[i%base]; /* */
i = i/base; /* */
} /* */
if ( sign ) *bp = '-'; else ++bp; /* */
return(bp); /* */
} /*------------------------*/
/*****************************************************************************/
/*** Key Init Section ***/
/*****************************************************************************/
/*-----------------------------------------------------------------------------
** Function*: init_key()
** Purpose: Global initializations for the key module.
** This function has to be called before some of the
** functions provided in this module are guaranteed to
** work properly.
** Arguments: none
** Returns: nothing
**___________________________________________________ */
static void init_key() /* */
{ int i; /* */
String s; /* */
/* */
if (key_seps != NULL) return; /* */
/* */
key_seps = (Symbol*)malloc(8*sizeof(Symbol)); /* */
key_seps[0] = symbol((String)"**key*"); /* */
key_seps[1] = symbol((String)"-"); /* */
key_seps[2] = symbol((String)"."); /* */
key_seps[3] = symbol((String)"."); /* */
key_seps[4] = symbol((String)":"); /* */
key_seps[5] = symbol((String)"-"); /* */
key_seps[6] = symbol((String)"*"); /* */
key_seps[7] = symbol((String)".ea"); /* */
/* */
for (i = 0; i < NUMBER_OF_FORMATS; i++) /* */
{ format[i] = NameNULL; } /* */
/* */
if ( format[0] == NameNULL ) /* */
{ s = newString("%*l"); /* */
format[0] = name_format(s); /* */
free(s); /* */
} /* */
NameMid(format[0]) = InterNameSep; /* */
/* */
if ( format[1] == NameNULL ) /* */
{ s = newString("%*l%*1f"); /* */
format[1] = name_format(s); /* */
free(s); /* */
} /* */
NameMid(format[1]) = InterNameSep; /* */
if (NextName(format[1])) /* */
{ NamePre(NextName(format[1])) = NamePreSep; /* */
} /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function*: key_init()
** Purpose: Perform initializations for key generation.
** The string buffer is opened.
** Ignored words are initialized if necessary.
** Arguments: none
** Returns: nothing
**___________________________________________________ */
static void key_init() /* */
{ /* */
#ifdef INITIALIZE_IGNORED_WORDS
static char *word_list[] = /* default ignored words. */
{ INITIALIZE_IGNORED_WORDS, NULL }; /* Mark the end with NULL */
register char**wp; /* */
#endif
/* */
if ( key_sb == (StringBuffer*)0 ) /* Is it the first time? */
{ /* */
if ( (key_sb=sbopen()) == (StringBuffer*)0 ) /* open string buffer */
{ OUT_OF_MEMORY("key generation."); } /* */
#ifdef INITIALIZE_IGNORED_WORDS
for ( wp = word_list; *wp != NULL; ++wp ) /* add ignored words. */
{ add_ignored_word(symbol((String)*wp)); } /* */
#endif
} /* */
} /*------------------------*/
/*****************************************************************************/
/*** Key DeTeX Section ***/
/*****************************************************************************/
#define DeTeX_BUFF_LEN 256
#define DETEX_FLAG_NONE 0
#define DETEX_FLAG_COMMA 1
#define DETEX_FLAG_ALLOWED 2
/*-----------------------------------------------------------------------------
** Function*: deTeX()
** Purpose: Expand TeX sequences or eliminate them.
**
** Arguments:
** line string to work on
** save_fct Function pointer which is called to store resulting
** characters.
** flags
** Returns:
**___________________________________________________ */
static int deTeX(line,save_fct,flags) /* */
String line; /* */
int flags; /* */
void (*save_fct)_ARG((String)); /* */
{ static String buffer; /* */
static size_t len = 0; /* */
Uchar c; /* */
String s, bp; /* */
Uchar last = (Uchar)' '; /* */
int wp = 0; /* */
int brace; /* */
/* */
#define SaveWord wp++; (*save_fct)(bp)
#define SaveChar(C) *(bp++) = last = C
#define StoreChar(C) *(bp++) = C
/* */
brace = 8 * strlen((char*)line); /* */
if ( brace > (int)len ) /* */
{ if ( len == 0 ) /* */
{ len = ( brace < DeTeX_BUFF_LEN /* */
? DeTeX_BUFF_LEN : brace ); /* */
buffer = (String)malloc(sizeof(Uchar)*len); /* */
} /* */
else /* */
{ len = brace; /* */
buffer = (String)realloc((char*)buffer, /* */
sizeof(Uchar)*len); /* */
} /* */
if ( buffer == NULL ) /* */
{ OUT_OF_MEMORY("deTeX()"); } /* */
} /* */
/* */
TeX_open_string(line); /* */
bp = buffer; /* */
brace = 0; /* */
wp = 0; /* */
SaveWord; /* */
/* */
while ( TeX_read(&c, &s) ) /* */
{ /* */
if ( is_alpha(c) /* Letters and */
|| is_digit(c) /* digits and */
|| is_extended(c) /* extended chars and */
|| ( c == (Uchar)'-' && last != c ) ) /* non-repeated hyphens */
{ SaveChar(c); } /* */
else if ( c == (Uchar)'{' ) { ++brace; } /* Count braces. */
else if ( c == (Uchar)'}' ) /* */
{ if ( --brace < 0 ) brace = 0; /* */
} /* */
else if ( ( c == ',' /* Commas */
&& (flags&DETEX_FLAG_COMMA) ) /* */
|| /* or */
( (flags&DETEX_FLAG_ALLOWED) /* allowed characters */
&& is_allowed(c) ) /* */
) /* */
{ if ( bp != buffer && *(bp-1) != '\0' ) /* are treated as */
{ StoreChar('\0'); /* single words upon */
SaveWord; /* request, or ignored. */
} /* */
StoreChar(c); /* */
StoreChar('\0'); /* */
SaveWord; /* */
last = ' '; /* */
} /* */
else if ( !(flags&DETEX_FLAG_ALLOWED) /* */
&& is_wordsep(c) /* */
&& !is_wordsep(last) ) /* Don't repeat spaces. */
{ if ( brace == 0 ) /* Remember the beginning */
{ StoreChar('\0'); SaveWord; last = ' '; } /* of the next word. */
else /* */
{ String t; /* */
for (t = SymbolValue(InterNameSep); *t; ++t)/* */
{ SaveChar(*t); } /* */
} /* */
} /* */
} /* */
/* */
if (buffer) { /* */
if ( bp != buffer && *(bp-1) != '\0' ) /* */
{ StoreChar('\0'); /* */
SaveWord; /* */
} /* */
/* */
StoreChar('\0'); /* Mark the end. */
/* */
if ( bp-buffer >= (int)len ) /* */
{ ERROR_EXIT("deTeX buffer overflow."); } /* */
} /* */
/* */
TeX_close(); /* */
return wp-1; /* */
} /*------------------------*/
/*****************************************************************************/
/*** Title Formatting Section ***/
/*****************************************************************************/
#define PushS(SB,S) push_s(SB,S,0,trans)
#define PushStr(SB,S,M) push_s(SB,S,M,trans)
#define PushC(SB,C) (void)sbputchar(trans[(unsigned int)(C)],SB);
/*-----------------------------------------------------------------------------
** Function*: push_s()
** Purpose: Write a translated string to the key string buffer.
** Arguments:
** s String to translate
** max Maximum number of characters
** trans Translation table
** Returns: nothing
**___________________________________________________ */
static void push_s(sb,s,max,trans) /* */
StringBuffer *sb; /* */
String s; /* */
int max; /* */
String trans; /* */
{ /* */
if ( max <= 0 ) /* */
{ while ( *s ) /* */
{ (void)sbputchar(trans[*(s++)],sb); } /* */
} /* */
else /* */
{ while ( *s && max-->0 ) /* */
{ (void)sbputchar(trans[*(s++)],sb); } /* */
} /* */
} /*------------------------*/
static WordList ignored_words[32] =
{ WordNULL, WordNULL, WordNULL, WordNULL,
WordNULL, WordNULL, WordNULL, WordNULL,
WordNULL, WordNULL, WordNULL, WordNULL,
WordNULL, WordNULL, WordNULL, WordNULL,
WordNULL, WordNULL, WordNULL, WordNULL,
WordNULL, WordNULL, WordNULL, WordNULL,
WordNULL, WordNULL, WordNULL, WordNULL,
WordNULL, WordNULL, WordNULL, WordNULL
};
/*-----------------------------------------------------------------------------
** Function: add_ignored_word()
** Purpose: Add a new word to the list of ignored words for title
** key generation.
** The argument has to be saved by the caller! This means
** that it is assumed that the argument is a symbol.
** Arguments:
** word Word to add.
** Returns: nothing
**___________________________________________________ */
void add_ignored_word(word) /* */
Symbol word; /* */
{ /* */
key_init(); /* */
add_word(word, /* */
&ignored_words[(*SymbolValue(word))&31]);/* */
DebugPrint2("Adding ignored word ", /* */
SymbolValue(word)); /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: clear_ignored_words()
** Purpose: Delete the list of ignored words. Afterwards no words are
** recognized as ignored words.
** Arguments: none
** Returns: nothing
**___________________________________________________ */
void clear_ignored_words() /* */
{ int i; /* */
key_init(); /* */
/* */
for (i = 0; i < 32; i++) /* */
{ free_words(&ignored_words[i], NULL); } /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: foreach_ignored_word()
** Purpose: Iterator a given function |fct| is applied to each
** ignored word in turn. If the function returns 0 then
** the loop is terminated. The different words are
** visited in a fixed order which does not necessarily
** coincide with the natural order of words. Thus don't
** assume this.
** Arguments:
** fct Function to apply.
** Returns: The return status of the last |fct| call.
**___________________________________________________ */
bool foreach_ignored_word(fct) /* */
bool (*fct)_ARG((Symbol)); /* */
{ int i; /* */
/* */
key_init(); /* */
/* */
for (i = 0; i < 32; i++) /* */
{ if (!foreach_word(ignored_words[i], fct)) /* */
{ return false; } /* */
} /* */
return true; /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function*: fmt_title()
** Purpose: Format a string according to rules for titles.
** \TeX{} sequences are expanded and some words may be ignored.
** The result is pushed to the local string buffer.
** Arguments:
** line String to format.
** len Number of words to use
** in Number of characters per word to use
** trans Translation table
** ignore Boolean. Certain words are ignored if it is |true|
** sep String separating the words.
** Returns: nothing
**___________________________________________________ */
static void fmt_title(sb, line, len, in, trans, ignore, sep)/* */
StringBuffer *sb; /* */
String line; /* */
int len; /* */
int in; /* */
String trans; /* Translation table */
bool ignore; /* */
Symbol sep; /* */
{ bool first = true; /* */
int nw, i, j; /* */
String s; /* */
/* */
/* if ( len == 0 ) return; */
/* */
if ( tmp_sb == (StringBuffer*)NULL /* */
&& (tmp_sb=sbopen()) == (StringBuffer*)NULL )/* */
{ OUT_OF_MEMORY("fmt_title()"); } /* */
/* */
ResetWords; /* */
nw = deTeX(*line == (Uchar)'{' ? line + 1 : line,/* */
push_word, /* */
DETEX_FLAG_NONE); /* */
/* */
for (i = 0; i < nw; ++i) /* */
{ /* */
sbrewind(tmp_sb); /* Reset the string buffer*/
for (s = words[i]; *s; ++s) /* Translate the current */
{ (void)sbputchar(trans[*s],tmp_sb); } /* word into the sbuffer */
s = (String)sbflush(tmp_sb); /* Get the translated word*/
/* */
if ( ! ignore || /* */
! find_word((String)s, /* */
ignored_words[(*s)&31] ) ) /* */
{ if (first) { first = false; } /* */
else { PushS(sb, SymbolValue(sep)); } /* */
if ( in <= 0 ) /* */
{ PushS(sb, words[i]); } /* Push the current word */
else /* */
{ for (s = words[i], j = in; /* */
*s && j-->0; /* */
++s) /* Push the initial part */
{ PushC(sb, *s); } /* of the current word. */
} /* */
if ( len == 1 ) return; /* */
if ( len > 0 ) len--; /* */
} /* */
} /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function*: fmt_c_words()
** Purpose: Count a list of words.
** Arguments:
** line string to analyze
** min minimum
** max maximum or 0
** not negation flag
** ignore flag to indicate if certain words should be ignored.
** Returns:
**___________________________________________________ */
static bool fmt_c_words(line, min, max, not, ignore)/* */
String line; /* */
int min; /* */
int max; /* */
bool not; /* */
bool ignore; /* */
{ int n, i, nw; /* */
/* */
ResetWords; /* */
nw = deTeX(*line == (Uchar)'{' ? line + 1 : line,/* */
push_word, /* */
DETEX_FLAG_NONE); /* */
n = 0; /* */
/* */
for (i = 0; i < nw; ++i) /* */
{ /* */
if (!ignore || /* */
!find_word(words[i], /* */
ignored_words[(*words[i])&31]) )/* */
{ n++; } /* */
} /* */
/* */
if (n < min || (max > 0 && n > max)) /* */
{ return (not ? false : true); } /* */
/* */
return (not ? true : false); /* */
} /*------------------------*/
/*****************************************************************************/
/*** Name Formatting Section ***/
/*****************************************************************************/
/*-----------------------------------------------------------------------------
** Function: def_format_type()
** Purpose:
**
**
** Arguments:
** s
** Returns: nothing
**___________________________________________________ */
void def_format_type(s) /* */
String s; /* */
{ int n; /* */
String cp; /* */
Uchar c; /* */
/* */
SkipSpaces(s); /* */
n = 0; /* */
while (is_digit(*s)) { n = n*10 + (*s++) - '0'; }/* */
if ( n >= NUMBER_OF_FORMATS ) /* */
{ WARNING("Format type number is out of range.");/* */
return; /* */
} /* */
if ( rsc_verbose && n <= 1 ) /* */
{ VerbosePrint3("Name format specifier ", /* */
(n == 0 ? "0" : "1"), /* */
" has been changed.\n"); /* */
} /* */
/* */
if (key_seps == NULL) { init_key(); } /* */
/* */
SkipSpaces(s); /* */
if ( *s == '=' ) s++; /* */
ExpectVoid(s,'"'); /* */
cp = s; /* */
while ( *cp && *cp != '"' ) cp++; /* */
c = *cp; /* */
*cp = (Uchar)'\0'; /* */
format[n] = name_format(s); /* */
*cp = c; /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function*: fmt_names()
** Purpose: Format a list of names separated by 'and'.
** 'and others' is handled properly.
** Arguments:
** line String to format.
** maxname number of names to consider
** maxlen number of characters per name to consider
** post the number of relevant char
** trans translation table
** Returns: nothing
**___________________________________________________ */
static void fmt_names(sb,line,maxname,post,trans) /* */
StringBuffer *sb; /* */
String line; /* Name list string */
int maxname; /* number of names b4 etal*/
int post; /* number of relevant char*/
String trans; /* Translation table */
{ int wp, /* */
i; /* */
static bool undef_warning = false; /* */
/* */
if (maxname == 0) return; /* */
/* */
if ( post < 0 /* */
|| post >= NUMBER_OF_FORMATS /* */
|| format[post] == NameNULL ) /* */
{ if (undef_warning) /* */
{ ErrPrintF("*** BibTool: Format %d is not defined. Ignored.\n",/* */
post); /* */
undef_warning = true; /* */
} /* */
return; /* */
} /* */
/* */
ResetWords; /* */
wp = deTeX(*line == (Uchar)'{' ? line + 1 : line,/* */
push_word, /* */
DETEX_FLAG_COMMA); /* */
words[wp] = StringNULL; /* */
/* */
for (i = 0; i < wp; i++) /* */
{ /* */
DebugPrintF3("+++ %3d '%s'\n", i, words[i]); /* */
/* */
if (strcmp((char*)words[i],"and") == 0) /* */
{ words[i] = SymbolValue(sym_et); } /* */
else if (strcmp((char*)words[i], ",") == 0) /* */
{ words[i] = SymbolValue(sym_comma); } /* */
} /* */
/* */
PushS(sb, /* */
pp_list_of_names(words, /* */
format[post], /* */
trans, /* */
maxname, /* */
SymbolValue(sym_comma), /* */
SymbolValue(sym_et), /* */
(char*)SymbolValue(NameNameSep),/* */
(char*)SymbolValue(EtAl)));/* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function*: fmt_c_names()
** Purpose: Count a list of names
** Arguments:
** line string to analyze
** min minimum
** max maximum or 0
** not negation flag
** Returns:
**___________________________________________________ */
static bool fmt_c_names(line,min,max,not) /* */
String line; /* Name list string */
int min; /* number of relevant char*/
int max; /* number of names b4 etal*/
bool not; /* negation flag */
{ int wp, /* */
i, /* */
n; /* */
/* */
ResetWords; /* */
wp = deTeX(*line == (Uchar)'{' ? line + 1 : line,/* */
push_word, /* */
DETEX_FLAG_COMMA); /* */
words[wp] = NULL; /* */
/* */
for (i = 0, n = 1; i < wp; i++) /* */
{ if (strcmp((char*)words[i],"and") == 0) /* */
{ n++; } /* */
} /* */
/* */
if (n < min || (max > 0 && n > max)) /* */
{ return (not ? false : true); } /* */
/* */
return (not ? true : false); /* */
} /*------------------------*/
/*****************************************************************************/
/*** Number Formatting Section ***/
/*****************************************************************************/
/*-----------------------------------------------------------------------------
** Function*: fmt_digits()
** Purpose: Search a sequence of digits and push at most n of them
** counting from right to the string buffer.
** Example: fmt_digits("jun 1958",2) pushes "58" to the string buffer.
** Arguments:
** sb String buffer to store the result in
** s String to format
** mp Boolean indicating whether padding with 0s should be performed
** (true) or not.
** pp Boolean indicating whether no number should be 0 or fail
** n Length
** sel The number to select.
** trunc Boolean indicating whether or not to truncate the number.
** Returns:
**___________________________________________________ */
static bool fmt_digits(sb,s,mp,pp,n,sel,trunc) /* */
StringBuffer *sb; /* */
register String s; /* */
int mp; /* */
int pp; /* */
register int n; /* */
int sel; /* */
bool trunc; /* */
{ register String cp; /* */
/* */
if (n < 0) { n = ( mp ? 1 : 0x7fff ); } /* */
/* */
cp = s; /* */
while (sel-->0) /* */
{ s = cp; /* */
while (*s && !is_digit(*s)) { ++s; } /* search first digit */
if ( *s == '\0' ) /* */
{ cp = s; sel = 0; } /* */
else /* */
{ for (cp = s; *cp && is_digit(*cp); ++cp) {} /* skip over digits */
} /* */
} /* */
/* */
if (trunc && cp-s > n) s = cp-n; /* */
else if (mp) /* */
{ while (cp - s < n--) /* */
{ (void)sbputchar('0', sb); } /* */
} /* */
if (!mp && *s == '\0') /* */
{ if (pp) (void)sbputchar('0', sb); /* */
else return true; /* */
} /* */
else /* */
{ /* */
while ( s != cp ) /* */
{ (void)sbputchar((char)(*s), sb); /* */
++s; /* */
} /* */