File: chek.c

package info (click to toggle)
bibclean 2.11.4-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 5,212 kB
  • ctags: 1,545
  • sloc: ansic: 10,737; makefile: 817; perl: 66; sh: 40; awk: 27; sed: 6
file content (1270 lines) | stat: -rw-r--r-- 34,977 bytes parent folder | download | duplicates (5)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
#include <config.h>
#include "xctype.h"
#include "xstat.h"
#include "xstring.h"

RCSID("$Id: chek.c,v 1.3 1996/05/03 20:11:39 beebe Exp beebe $")

/* ISBN validation code changed heavily by Henning Makholm
 * <henning@makholm.net> in December 2004 to add support for ISBN-13
 */

#include "yesorno.h"
#include "match.h"			/* must come AFTER yesorno.h */
#include "token.h"
#include "longflds.h"

typedef struct s_parse_data
{
    YESorNO	(*is_name_char) ARGS((int c_, size_t n_));
    const char	*s;		/* pointer to next char in list */
    const char	*token;		/* pointer to token in list */
    int		token_length;	/* number of token characters (== (s - token)) */
} parse_data;

typedef struct s_pattern_table
{
    MATCH_PATTERN *patterns;
    int current_size;
    int maximum_size;
} PATTERN_TABLE;

typedef struct s_pattern_names
{
    const char *name;
    PATTERN_TABLE *table;
} PATTERN_NAMES;

typedef struct s_name_pair
{
    const char *old_name;
    const char *new_name;
} NAME_PAIR;

#if defined(HAVE_PATTERNS)
#define PATTERN_MATCHES(string,pattern) (match_pattern(string,pattern) == YES)
#else /* NOT defined(HAVE_PATTERNS) */
#define PATTERN_MATCHES(string,pattern) match_regexp(string,pattern)
#endif /* defined(HAVE_PATTERNS) */

#define	PT_CHAPTER	0		/* index in pattern_names[] */
#define	PT_MONTH	1		/* index in pattern_names[] */
#define	PT_NUMBER	2		/* index in pattern_names[] */
#define	PT_PAGES	3		/* index in pattern_names[] */
#define	PT_VOLUME	4		/* index in pattern_names[] */
#define	PT_YEAR		5		/* index in pattern_names[] */

#define STD_MAX_TOKEN	((size_t)1000)	/* Standard BibTeX limit */

#define UNKNOWN_CODEN	"??????"
#define MAX_CODEN	(sizeof(UNKNOWN_CODEN)-1)

#define UNKNOWN_ISSN	"????????"
#define MAX_ISSN	(sizeof(UNKNOWN_ISSN)-1)

extern YESorNO	check_values;		/* NO: suppress value checks */
extern char	current_field[];	/* field name */
extern char	current_key[];		/* string value */
extern char	current_value[];	/* string value */
extern NAME_PAIR month_pair[];
extern PATTERN_NAMES pattern_names[];
extern char	shared_string[];
extern FILE	*stdlog;		/* usually stderr */
extern YESorNO	stdlog_on_stdout;	/* NO for separate files */

extern void	error ARGS((const char *msg_));
extern void	ISBN_hyphenate ARGS((char *s_,char *t_,size_t maxs_));
extern void	warning ARGS((const char *msg_));

void		check_chapter ARGS((void));
void		check_CODEN ARGS((void));
void		check_ISBN ARGS((void));
void		check_ISSN ARGS((void));
YESorNO		check_junior ARGS((const char *last_name_));
void		check_key ARGS((void));
void		check_length ARGS((size_t n_));
void		check_month ARGS((void));
void		check_number ARGS((void));
void		check_other ARGS((void));
void		check_pages ARGS((void));
YESorNO		check_patterns ARGS((PATTERN_TABLE *pt_,const char *value_));
void		check_volume ARGS((void));
void		check_year ARGS((void));

static void	bad_CODEN ARGS((char CODEN_[6]));
static void	bad_ISBN ARGS((const char *problem,
                               const char *isbn, size_t isbn_length));
static void	bad_ISSN ARGS((char ISSN_[9]));
static int	CODEN_character_value ARGS((int c_));
static size_t	copy_element ARGS((char *target_, size_t nt_, const char *source_, size_t ns_));
static void	incomplete_CODEN ARGS((char CODEN_[6]));
static YESorNO	is_CODEN_char ARGS((int c_, size_t n_));
static YESorNO	is_ISBN_char ARGS((int c_, size_t n_));
static YESorNO	is_ISSN_char ARGS((int c_, size_t n_));
static void	parse_list ARGS((const char *s,
				 YESorNO (*is_name_char_) ARGS((int c_, size_t n_)),
				 void (*validate_) ARGS((const char *CODEN_, size_t n_))));
static void	rewrite_list ARGS((char *s,size_t buflen,
				  YESorNO (*is_name_char_) ARGS((int c_, size_t n_)),
				  size_t (*rewrite_) ARGS((char *DEST,size_t DS_,const char *SRC,size_t SS_))));
static void	parse_element ARGS((parse_data *pd_));
static void	parse_separator ARGS((parse_data *pd_));
static void	validate_CODEN ARGS((const char *CODEN_, size_t n_));
static size_t   rewrite_ISBN ARGS((char *DEST,size_t DS,const char *SRC,size_t SS));
static void	validate_ISSN ARGS((const char *ISSN_, size_t n_));
static void	unexpected ARGS((void));

#define ISBN_DIGIT_VALUE(c)	((((c) == 'X') || ((c) == 'x')) ? 10 : \
					((c) - '0'))
				/* correct only if digits are valid; */
				/* the code below ensures that */

#define ISSN_DIGIT_VALUE(c)	ISBN_DIGIT_VALUE(c)
				/* ISSN digits are just like ISBN digits */

#if defined(HAVE_STDC)
static void
bad_CODEN(char CODEN[7])
#else /* K&R style */
static void
bad_CODEN(CODEN)
char CODEN[7];
#endif
{
    static char fmt[] =
	"Invalid checksum for CODEN %c%c%c%c%c%c in ``%%f = %%v''";
    char msg[sizeof(fmt)];

#define XCODEN(n)	(int)((CODEN[n] == '\0') ? '?' : CODEN[n])

    (void)sprintf(msg, fmt,
		  XCODEN(1), XCODEN(2), XCODEN(3), XCODEN(4), XCODEN(5), XCODEN(6));
    warning(msg);	/* should be error(), but some journals might have */
			/* invalid CODENs (some books have invalid ISBNs) */
}


static void
bad_ISBN(const char *problem, const char *isbn, size_t isbn_length)
{
    static char fmt[] = "%s for ISBN %*s in ``%%f = %%v''";
    char msg[100] ;
    if (isbn_length > 100-strlen(problem) - sizeof(fmt))
        isbn_length=10 ;
    (void)sprintf(msg, fmt, problem, isbn_length, isbn);
    warning(msg);	/* used to be error(), but some books actually have */
			/* invalid ISBNs */
}


#if defined(HAVE_STDC)
static void
bad_ISSN(char ISSN[9])
#else /* K&R style */
static void
bad_ISSN(ISSN)
char ISSN[9];
#endif
{
    static char fmt[] =
	"Invalid checksum for ISSN %c%c%c%c-%c%c%c%c in ``%%f = %%v''";
    char msg[sizeof(fmt)];

#define XISSN(n)	(int)((ISSN[n] == '\0') ? '?' : ISSN[n])

    (void)sprintf(msg, fmt, XISSN(1), XISSN(2), XISSN(3), XISSN(4),
		  XISSN(5), XISSN(6), XISSN(7), XISSN(8));
    warning(msg);	/* used to be error(), but some journals might have */
			/* invalid ISSNs (some books have invalid ISBNs) */
}


void
check_chapter(VOID)
{
#if defined(HAVE_OLDCODE)
    size_t k;
    size_t n = strlen(current_value) - 1;

    /* match patterns like "23" and "23-1" */
    for (k = 1; k < n; ++k)
    {	/* omit first and last characters -- they are quotation marks */
	if (!(Isdigit(current_value[k]) || (current_value[k] == '-')))
	    break;
    }
    if (k == n)
	return;
#else /* NOT defined(HAVE_OLDCODE) */
    if (check_patterns(pattern_names[PT_CHAPTER].table,current_value) == YES)
	return;
#endif /* defined(HAVE_OLDCODE) */

    unexpected();
}


void
check_CODEN(VOID)
{
    parse_list(current_value, is_CODEN_char, validate_CODEN);
}


void
check_inodes(VOID)
{
    struct stat buflog;
    struct stat bufout;

    stdlog_on_stdout = YES;			/* assume the worst initially */

    (void)fstat(fileno(stdlog),&buflog);
    (void)fstat(fileno(stdout),&bufout);

#if OS_UNIX
    stdlog_on_stdout = ((buflog.st_dev == bufout.st_dev) &&
			(buflog.st_ino == bufout.st_ino)) ? YES : NO;
#endif /* OS_UNIX */

#if OS_PCDOS
    /* No inodes, so use other fields instead */
    stdlog_on_stdout = ((buflog.st_dev == bufout.st_dev) &&
			(buflog.st_mode == bufout.st_mode) &&
			(buflog.st_size == bufout.st_size) &&
			(buflog.st_ctime == bufout.st_ctime)) ? YES : NO;
#endif /* OS_PCDOS */

#if OS_VAXVMS
    /* Inode field is 3 separate values */
    stdlog_on_stdout = ((buflog.st_dev == bufout.st_dev) &&
			(buflog.st_ino[0] == bufout.st_ino[0]) &&
			(buflog.st_ino[1] == bufout.st_ino[1]) &&
			(buflog.st_ino[2] == bufout.st_ino[2])) ? YES : NO;
#endif /* OS_VAXVMS */

}


void
check_ISBN(VOID)
{
    rewrite_list(current_value,MAX_TOKEN_SIZE, is_ISBN_char, rewrite_ISBN);
}


void
check_ISSN(VOID)
{
    parse_list(current_value, is_ISSN_char, validate_ISSN);
}


#if defined(HAVE_STDC)
YESorNO
check_junior(const char *last_name)
#else /* K&R style */
YESorNO
check_junior(last_name)
const char *last_name;
#endif
{				/* return YES: name is Jr.-like, else: NO */
    int b_level;		/* brace level */
    static const char *juniors[] =
    {				/* name parts that parse like "Jr." */
	"Jr",
	"Jr.",
	"Sr",
	"Sr.",
	"SJ",
	"S.J.",
	"S. J.",
	(const char*)NULL,	/* list terminator */
    };
    int k;			/* index into juniors[] */
    int n;			/* index into last_name[] */

    for (n = 0, b_level = 0; last_name[n]; ++n)
    {				/* check for "Smith, Jr" and "Smith Jr" and */
	switch (last_name[n])	/* convert to "{Smith, Jr}" and "{Smith Jr}" */
	{
	case '{':
	    b_level++;
	    break;

	case '}':
	    b_level--;
	    break;

	case ',':
	    if (b_level == 0)
		return (YES);
	    break;

	case '\t':
	case ' ':		/* test for Jr.-like name */
	    if (b_level == 0)
	    {
		for (k = 0; juniors[k] != (const char*)NULL; ++k)
		{
		    if (strnicmp(&last_name[n+1],juniors[k],strlen(juniors[k]))
			== 0)
			return (YES);
		}			/* end for (k...) */
		if (strcspn(&last_name[n+1],"IVX") == 0)
		    return (YES); /* probably small upper-case Roman number */
	    }
	    break;

	default:
	    break;
	}				/* end switch (last_name[n]) */
    }					/* end for (n = 0,...) */
    return (NO);
}


void
check_key(VOID)
{
    int k;				/* index into pattern_names[] */

    for (k = 0; pattern_names[k].name != (const char*)NULL; ++k)
    {
	if (stricmp(pattern_names[k].name,current_key) == 0)
	{				/* then found the required table */
	    if (check_patterns(pattern_names[k].table,current_key) == NO)
		warning("Unexpected citation key ``%k''");
	    return;
	}
    }
}


struct long_field_struct *long_fields = (struct long_field_struct *)0 ;
#if defined(HAVE_STDC)
void
check_length(size_t n)
#else /* K&R style */
void
check_length(n)
size_t n;
#endif
{
    struct long_field_struct *lfp;
    for( lfp = long_fields; lfp; lfp = lfp->next )
      if( STREQUAL(current_field,lfp->fieldname) )
        return ;
    if ((check_values == YES) && (n >= STD_MAX_TOKEN))
	warning("String length exceeds standard BibTeX limit for ``%f'' entry");
}


void
check_month(VOID)
{
    int m;			/* month index */
    size_t n = strlen(current_value);

    if (n == 3)			/* check for match against standard abbrevs */
    {
	for (m = 0; month_pair[m].old_name != (const char*)NULL; ++m)
	{
	    if (stricmp(month_pair[m].new_name,current_value) == 0)
		return;
	}
    }

    /* Hand coding for the remaining patterns is too ugly to contemplate,
       so we only provide the checking when real pattern matching is
       available. */

#if !defined(HAVE_OLDCODE)
    if (check_patterns(pattern_names[PT_MONTH].table,current_value) == YES)
	return;
#endif /* !defined(HAVE_OLDCODE) */

    unexpected();
}


void
check_number(VOID)
{
#if defined(HAVE_OLDCODE)
    size_t k;
    size_t n = strlen(current_value) - 1;

    /* We expect the value string to match the regexp "[0-9a-zA-Z---,/ ()]+
    to handle values like "UMIACS-TR-89-11, CS-TR-2189, SRC-TR-89-13",
    "RJ 3847 (43914)", "{STAN-CS-89-1256}", "UMIACS-TR-89-3.1, CS-TR-2177.1",
    "TR\#89-24", "23", "23-27", and "3+4". */

    for (k = 1; k < n; ++k)
    {	/* omit first and last characters -- they are quotation marks */
	if (!(     Isalnum(current_value[k])
		|| Isspace(current_value[k]) || (current_value[k] == '-')
		|| (current_value[k] == '+') || (current_value[k] == ',')
		|| (current_value[k] == '.') || (current_value[k] == '/')
		|| (current_value[k] == '#') || (current_value[k] == '\\')
		|| (current_value[k] == '(') || (current_value[k] == ')')
		|| (current_value[k] == '{') || (current_value[k] == '}') ))
	    break;
    }
    if (k == n)
	return;
#else /* NOT defined(HAVE_OLDCODE) */
    if (check_patterns(pattern_names[PT_NUMBER].table,current_value) == YES)
	return;
#endif /* defined(HAVE_OLDCODE) */

    unexpected();
}


void
check_other(VOID)
{
    int k;				/* index into pattern_names[] */

    for (k = 0; pattern_names[k].name != (const char*)NULL; ++k)
    {
	if (stricmp(pattern_names[k].name,current_field) == 0)
	{				/* then found the required table */
	    if (check_patterns(pattern_names[k].table,current_value) == NO)
		unexpected();
	    return;
	}
    }
}


void
check_pages(VOID)
{
    /* Need to handle "B721--B729" as well as "721--729"; some
       physics journals use an initial letter in page number. */

#if defined(HAVE_OLDCODE)
    int number = 1;
    size_t k;
    size_t n = strlen(current_value) - 1;

    /* We expect the value string to match the regexps [0-9]+ or
       [0-9]+--[0-9]+ */
    for (k = 1; k < n; ++k)
    {	/* omit first and last characters -- they are quotation marks */
	switch (current_value[k])
	{
	case '0':
	case '1':
	case '2':
	case '3':
	case '4':
	case '5':
	case '6':
	case '7':
	case '8':
	case '9':
	    if (number > 2)
	    {
		warning("More than 2 page numbers in ``%f = %v''");
		return;
	    }
	    break;

	case '-':
	    number++;
	    if (current_value[k+1] != '-')	/* expect -- */
	    {
		warning(
		    "Use en-dash, --, to separate page numbers in ``%f = %v''");
		return;
	    }
	    ++k;
	    if (current_value[k+1] == '-')	/* should not have --- */
	    {
		warning(
		    "Use en-dash, --, to separate page numbers in ``%f = %v''");
		return;
	    }
	    break;

	case ',':
	    number++;
	    break;

	default:
	    unexpected();
	    return;
	}
    }
#else /* NOT defined(HAVE_OLDCODE) */
    if (check_patterns(pattern_names[PT_PAGES].table,current_value) == YES)
	return;
#endif /* defined(HAVE_OLDCODE) */

    unexpected();
}


#if (defined(HAVE_PATTERNS) || defined(HAVE_REGEXP) || defined(HAVE_RECOMP))

#if defined(HAVE_STDC)
YESorNO
check_patterns(PATTERN_TABLE* pt,const char *value)
#else /* K&R style */
YESorNO
check_patterns(pt,value)
PATTERN_TABLE* pt;
const char *value;
#endif
{
    /* Return YES if current_value[] matches a pattern, or there are no
       patterns, and NO if there is a match failure.  Any message
       associated with a successfully-matched pattern is printed before
       returning. */

    int k;

    for (k = 0; k < pt->current_size; ++k)
    {
	if (PATTERN_MATCHES(value,pt->patterns[k].pattern))
	{
	    if (pt->patterns[k].message != (const char*)NULL)
	    {
		if (pt->patterns[k].message[0] == '?') /* special error flag */
		    error(pt->patterns[k].message + 1);
		else		/* just normal warning */
		    warning(pt->patterns[k].message);
	    }
	    return (YES);
	}
    }
    return ((pt->current_size == 0) ? YES : NO);
}
#endif /* (defined(HAVE_PATTERNS) || defined(HAVE_REGEXP) ||
	   defined(HAVE_RECOMP)) */


void
check_volume(VOID)
{
#if defined(HAVE_OLDCODE)
    size_t k;
    size_t n = strlen(current_value) - 1;

    /* Match patterns like "27", "27A", "27/3", "27A 3", "SMC-13", "VIII",
       "B", "{IX}", "1.2", "Special issue A", and  "11 and 12".  However,
       NEVER match pattern like "11(5)", since that is probably an erroneous
       incorporation of issue number into the volume value. */

    for (k = 1; k < n; ++k)
    {	/* omit first and last characters -- they are quotation marks */
	if (!(     Isalnum(current_value[k])
		|| (current_value[k] == '-')
		|| (current_value[k] == '/')
		|| (current_value[k] == '.')
		|| isspace(current_value[k])
		|| (current_value[k] == '{')
		|| (current_value[k] == '}') ))
	{
	    unexpected();
	    return;
	}
    }
#else /* NOT defined(HAVE_OLDCODE) */
    if (check_patterns(pattern_names[PT_VOLUME].table,current_value) == YES)
	return;
#endif /* defined(HAVE_OLDCODE) */

    unexpected();
}


void
check_year(VOID)
{
    char *p;
    char *q;
    long year;

#if defined(HAVE_OLDCODE)
    size_t k;
    size_t n;

    /* We expect the value string to match the regexp [0-9]+ */
    for (k = 1, n = strlen(current_value) - 1; k < n; ++k)
    {	/* omit first and last characters -- they are quotation marks */
	if (!Isdigit(current_value[k]))
	{
	    warning("Non-digit found in field value of ``%f = %v''");
	    return;
	}
    }
#else /* NOT defined(HAVE_PATTERNS) */
    if (check_patterns(pattern_names[PT_YEAR].table,current_value) == YES)
	return;
    unexpected();
#endif /* defined(HAVE_PATTERNS) */

    for (p = current_value; *p ; ) /* now validate all digit strings */
    {
	if (Isdigit(*p))	/* then have digit string */
	{			/* now make sure year is `reasonable' */
	    year = strtol(p,&q,10);
	    if ((year < 1800L) || (year > 2099L))
		warning("Suspicious year in ``%f = %v''");
	    p = q;
	}
	else			/* ignore other characters */
	    p++;
    }
}


#if defined(HAVE_STDC)
static int
CODEN_character_value(int c)
#else /* K&R style */
static int
CODEN_character_value(c)
int c;
#endif
{
    if (('a' <= c) && (c <= 'z'))
	return ((c - 'a' + 1));
    else if (('A' <= c) && (c <= 'Z'))
	return ((c - 'A' + 1));
    else if (('1' <= c) && (c <= '9'))
	return ((c - '1' + 27));
    else if (c == '0')
	return (36);
    else
	return (-1);
}


#if defined(HAVE_STDC)
static size_t
copy_element(char *target, size_t nt, const char *source, size_t ns)
#else /* K&R style */
static size_t
copy_element(char *target, size_t nt, const char *source, size_t ns)
#endif
{    /* Copy source[] into target[], excluding spaces and hyphens, and add a */
     /* trailing NUL.  Return the number of characters left in source[], */
     /* after ignoring trailing spaces and hyphens. */
    size_t ks;
    size_t kt;

    for (ks = 0, kt = 0; (ks < ns) && (kt < (nt - 1)); ++ks)
    {
	if (!((source[ks] == '-') || isspace(source[ks])))
	    target[kt++] = source[ks];
    }
    target[kt] = '\0';

    for ( ; (source[ks] == '-') || isspace(source[ks]); ++ks)
	/* NO-OP */;		 /* skip trailing space and hyphens */

    return (size_t)(ns - ks);
}


#if defined(HAVE_STDC)
static void
incomplete_CODEN(char CODEN[7])
#else /* K&R style */
static void
incomplete_CODEN(CODEN)
char CODEN[7];
#endif
{
    static char fmt[] =
	"Incomplete CODEN %c%c%c%c%c should be %c%c%c%c%c%c in ``%%f = %%v''";
    char msg[sizeof(fmt)];

    (void)sprintf(msg, fmt, CODEN[1], CODEN[2], CODEN[3], CODEN[4], CODEN[5],
		  CODEN[1], CODEN[2], CODEN[3], CODEN[4], CODEN[5], CODEN[6]);
    warning(msg);	/* should be error(), but some journals might have */
			/* invalid CODENs (some books have invalid ISBNs) */
}


#if defined(HAVE_STDC)
static YESorNO
is_CODEN_char(int c, size_t n)
#else /* K&R style */
static YESorNO
is_CODEN_char(c,n)
int c;
size_t n;
#endif
{
    static size_t n_significant = 0;
		/* number of significant chars already seen in current CODEN */

    /* CODENs match [A-Z]-*[A-Z]-*[A-Z]-*[A-Z]-*[A-Z]-*[A-Z0-9], but we
       also allow lower-case letters. */

    if (n == 0)				/* start new CODEN */
	n_significant = 0;

    /* embedded hyphens are accepted, but are not significant */
    if ((n_significant > 0) && (c == '-'))
	return (YES);
    else if ((n_significant < 5) && isalpha(c))
    {
	n_significant++;
	return (YES);
    }
    else if ((n_significant >= 5) && isalnum(c)) /* sixth char can be a digit */
    {
	n_significant++;
	return (YES);
    }

    return (NO);
}


#if defined(HAVE_STDC)
static YESorNO
is_ISBN_char(int c, size_t n)
#else /* K&R style */
static YESorNO
is_ISBN_char(c,n)
int c;
size_t n;
#endif
{
    static size_t n_significant = 0;
		/* number of significant chars already seen in current CODEN */

    /* ISBNs match
	[0-9][- ]*[0-9][- ]*[0-9][- ]*[0-9][- ]*[0-9][- ]*
        [0-9][- ]*[0-9][- ]*[0-9][- ]*[0-9][- ]*[0-9xX]
       or
        ([0-9][- ]*){13}
       but in order to catch wrong-number-of-digit errors we accept any
       number of digits, and start accepting X'es even early in the
       sequence.
    */

    if (n == 0)				/* start new ISBN */
	n_significant = 0;

    /* embedded hyphens and space are accepted, but are not significant */
    if ((n_significant > 0) && ((c == '-') || isspace(c)))
	return (YES);
    else if (isdigit(c))
    {
	n_significant++;
	return (YES);
    }
    else if ((n_significant >= 5) && ((c == 'X') || (c == 'x')))
    {
	n_significant++;
	return (YES);
    }

    return (NO);
}


#if defined(HAVE_STDC)
static YESorNO
is_ISSN_char(int c, size_t n)
#else /* K&R style */
static YESorNO
is_ISSN_char(c,n)
int c;
size_t n;
#endif
{
    static size_t n_significant = 0;
		/* number of significant chars already seen in current CODEN */

    /* ISSNs match
	[0-9][- ]*[0-9][- ]*[0-9][- ]*[0-9][- ]*
        [0-9][- ]*[0-9][- ]*[0-9][- ]*[0-9xX]
    */

    if (n == 0)				/* start new ISSN */
	n_significant = 0;

    /* embedded hyphens and space are accepted, but are not significant */
    if ((n_significant > 0) && ((c == '-') || isspace(c)))
	return (YES);
    else if ((n_significant < 7) && isdigit(c))
    {
	n_significant++;
	return (YES);
    }
    else if ((n_significant >= 7) && (isdigit(c) || (c == 'X') || (c == 'x')))
    {					/* eighth character may be [0-9xX] */
	n_significant++;
	return (YES);
    }

    return (NO);
}


#if defined(HAVE_STDC)
static void
parse_list(const char *s, YESorNO (*is_name_char) ARGS((int c, size_t n)),
	   void (*validate) ARGS((const char *s, size_t n)))
#else /* K&R style */
static void
parse_list(s, is_name_char, validate)
const char *s;
YESorNO (*is_name_char) ARGS((int c, size_t n));
void (*validate) ARGS((const char *s, size_t n));
#endif
{
    parse_data pd;

    /*******************************************************************
       Parse a list of CODEN, ISBN, or ISSN elements, according to the
       grammar:

	       LIST : NAME
		      | NAME SEPARATOR LIST

	       SEPARATOR : [not-a-token-char]+ | (nested balanced parentheses)

	       NAME : SEPARATOR* NAME'

	       NAME' : [token-char]+

       This simple, and permissive, grammar accepts any strings that
       contain sequences of zero or more CODEN, ISBN, or ISSN
       elements, separated by one or more of characters which are not
       themselves legal element characters.  The first element in the
       list may be preceded by any number of non-element characters.
       Comments are supported as arbitrary strings inside balanced
       parentheses, allowing lists like

		"0-387-97621-4 (invalid ISBN checksum), 3-540-97621-3"

		"0020-0190 (1982--1990), 0733-8716 (1991--)"

		"0-8493-0190-4 (set), 0-8493-0191-2 (v. 1),
		 0-8493-0192-0 (v. 2), 0-8493-0193-9 (v. 3)"

       The distinction between NAME' and SEPARATOR characters is made
       by the argument function, (*is_name_char)(), and the validation
       of the elements is done by the argument function (*validate)().

       This generality makes it possible for the same code to be
       reused for at least CODEN, ISBN, and ISSN values, and possibly
       others in future versions of this program.

       Tokens are not copied from the list, so no additional dynamic
       string storage is required.
    *******************************************************************/

    pd.s = s;
    pd.is_name_char = is_name_char;

    for (;;)
    {
	parse_separator(&pd);		/* may produce a zero-length token */
	parse_element(&pd);
	if (pd.token_length == 0)	/* no more tokens in list */
	    return;
	(*validate)(pd.token, pd.token_length);
    }
}

#if defined(HAVE_STDC)
static void
rewrite_list(char *s, size_t buflen,
             YESorNO (*is_name_char) ARGS((int c, size_t n)),
	     size_t (*rewrite) ARGS((char *d, size_t ds,const char *s,size_t ss)))
#else /* K&R style */
static void
rewrite_list(s, buflen, is_name_char, rewrite)
const char *s;
size_t buflen;
YESorNO (*is_name_char) ARGS((int c, size_t n));
size_t (*rewrite) ARGS((char *d, size_t ds,const char *s,size_t ss));
#endif
{
    parse_data pd;
    char workbuf[MAX_TOKEN_SIZE] ;
    char *filler ;
    size_t tmp ;

    /*******************************************************************
       Parse a list of the same shape as for parse_list, but allow
       the individual names to be rewritten while validating them.

       The buffer pointed to by s has size buflen, and is updated
       destructively, but only after all names have been rewritten
       succesfully.

       The rewrite() argument works with non-zero-terminated strings.
       It returns the number of characters copied to the destination,
       or (size_t)(-1) if there is not enough space.
    *******************************************************************/

    pd.s = s;
    pd.is_name_char = is_name_char;
    if( buflen > sizeof(workbuf) )
      buflen = sizeof(workbuf) ;
    filler = workbuf ;

    for (;;)
    {
	parse_separator(&pd);		/* may produce a zero-length token */
        if( pd.token_length > buflen ) {
          buflen = 0 ;
          break ;
        }
        (void)memcpy(filler,pd.token,pd.token_length);
        filler += pd.token_length;
        buflen -= pd.token_length;
        
	parse_element(&pd);
	if (pd.token_length == 0)	/* no more tokens in list */
	    break;
        tmp = (*rewrite)(filler,buflen,pd.token,pd.token_length);
        if( tmp == (size_t)(-1) || tmp > buflen ) {
          buflen = 0 ;
          break ;
        }
        filler += tmp ;
        buflen -= tmp ;
    }
    if( buflen > 0 ) {
      *filler = '\0' ;
      strcpy(s,workbuf);
    } else {
      error("Expansion of field ``%v'' would be too long");
    }
}

#if defined(HAVE_STDC)
static void
parse_element(parse_data *pd)
#else /* K&R style */
static void
parse_element(pd)
parse_data *pd;
#endif
{
    size_t n;

    for (n = 0, pd->token = pd->s; *pd->s && ((*pd->is_name_char)(*pd->s,n) == YES);
	 n++, pd->s++)
	/* NO-OP */;

    pd->token_length = (int)(pd->s - pd->token);
}


#if defined(HAVE_STDC)
static void
parse_separator(parse_data *pd)
#else /* K&R style */
static void
parse_separator(pd)
parse_data *pd;
#endif
{
    size_t n;
    int paren_level;			/* parenthesis level */

    pd->token = pd->s;

    for (n = 0, paren_level = 0;
	 (*pd->s && (((*pd->is_name_char)(*pd->s,n) == NO) || (paren_level > 0)));
	n++, pd->s++)
    {
	if (*pd->s == '(')
	    paren_level++;
	else if (*pd->s == ')')
	{
	    paren_level--;
	    if (paren_level == 0)
	        n = 0;
	}
    }

    pd->token_length = (int)(pd->s - pd->token);
    if (paren_level != 0)
	warning("Non-zero parenthesis level in ``%f = %v''");
}


static void
unexpected(VOID)
{
    warning("Unexpected value in ``%f = %v''");
}


static void
validate_CODEN(const char *the_CODEN, size_t n)
{
    int checksum;
    char CODEN[1 + MAX_CODEN + 1];	/* saved CODEN for error messages */
					/* (use slots 1..6 instead of 0..5) */
    size_t k;				/* index into CODEN[] */
    size_t nleft;

#define CODEN_CHECK_CHARACTER(n)	"9ABCDEFGHIJKLMNOPQRSTUVWXYZ2345678"[n]

    /*******************************************************************
       CODEN values are 6-character strings from the set [A-Z0-9],
       with a check digit stored in the 6th position given by

	   (11*N1 + 7*N2 + 5*N3 + 3*N4 + 1*N5) mod 34 == X

       where the Nk are 1..26 for A..Z, and 27..36 for 1..9..0.
       However, the checksum X (in 0..33) is represented by the
       corresponding character in the different 34-character range
       [9A-Z2-8], which excludes digits 0 and 1 to avoid confusion
       with letters O and I.

       In library catalogs, the 6th CODEN digit is often omitted, so
       when we find it missing in a CODEN value string, we print a
       warning to tell the user what it should be.  However, we
       intentionally do NOT insert it into the bibclean output,
       because the value string may be corrupted, instead of just
       truncated.

       The largest possible sum above is 11*36 + 7*36 + 5*36 + 3*36 +
       1*36 = 36*(11 + 7 + 5 + 3 + 1) = 36*27 = 972, corresponding to
       the CODEN value 00000T, since 972 mod 34 = 20, which maps to
       the letter T.  In reality, the limit is lower than this,
       because the initial CODEN character is always alphabetic; the
       largest usable CODEN would then be Z0000, which has a checksum
       of 11*26 + 7*36 + 5*36 + 3*36 + 1*36 = 36*(11 + 7 + 5 + 3 + 1)
       - 10*11 = 862.  Even 16-bit (short) integers are adequate for
       this computation.

       Old CODEN values may be stored with a hyphen between the 4th
       and 5th characters, e.g. "JACS-A" and "JACS-AT", as well as
       just "JACSA" and "JACSAT".  Unlike ISBN and ISSN values, spaces
       are NOT used inside CODEN values.
    *******************************************************************/

    (void)strcpy(&CODEN[1], UNKNOWN_CODEN);
    nleft = copy_element(&CODEN[1], sizeof(CODEN)-1, the_CODEN, n);

    for (checksum = 0, k = 1; CODEN[k]; ++k)
    {
	if (k < MAX_CODEN)
	{
	    static int multiplier[] = { 0, 11, 7, 5, 3, 1 };

	    checksum += CODEN_character_value((int)CODEN[k]) * multiplier[k];
	}
	else if (k == MAX_CODEN)
	{
	    if (CODEN_CHECK_CHARACTER(checksum % 34) != CODEN[k])
		bad_CODEN(CODEN);
	}
    }				/* end for (loop over CODEN[]) */

    if (strlen(&CODEN[1]) == (MAX_CODEN - 1))
    {		/* check digit omitted, so tell the user what it should be */
	CODEN[MAX_CODEN] = CODEN_CHECK_CHARACTER(checksum % 34);
	incomplete_CODEN(CODEN);
    }
    else if ((strlen(&CODEN[1]) != MAX_CODEN) || (nleft > 0))
	bad_CODEN(CODEN);
}


static size_t
rewrite_ISBN(char *dest, size_t destlen, const char *src, size_t srclen)
{
    const char *src_first, *src_last;
    const char *wrong_because;
    int n, xpos, checksum, k;
    int hyphenate10;
    char c, digits[20];

    /*******************************************************************
       ISBN numbers are 10-character values from the set [0-9Xx], with
       a checksum given by

		(sum(k=1:9) digit(k) * k) mod 11 == digit(10)

       where digits have their normal value, X (or x) as a digit has
       value 10, and spaces and hyphens are ignored.  The sum is
       bounded from above by 10*(1 + 2 + ... + 9) = 450, so even short
       (16-bit) integers are sufficient for the accumulation.

       ISBN digits are grouped into four parts separated by space or
       hyphen: countrygroupnumber-publishernumber-booknumber-checkdigit.

       Starting January 1, 2005 ISBN numbers can also be 13-digit
       numbers that are identical to the EAN retail product barcode
       for the book. The first three digits is the "bookland" code
       978; the last 10 digits follows the same numbering plan as
       old 10-digit ISBNs; however the check digit is computed
       according to the EAN checksum algorithm.

       ISBN-13 numbers are grouped with a hyphen after the bookland
       code and the last 10 digits hyphenated as ISBN-10.

       There is also a reserve bookland code 979. The numbering
       space for numbers in this range have not been allocated yet,
       so we do not try to adjust spacing and hyphenation for them,
       though we do validate the checksu.

       Books published before 2005 are still referred to with an
       ISBN-10, so we need to be able to validate and reformat
       either kind of number.
    *******************************************************************/

    src_first = src_last = src ;
    for (n=0, xpos=0; srclen>0; srclen--)
    {
        c = *src++ ;
        if( isdigit(c) )
            /* nothing yet */;
        else if( c == 'X' || c == 'x' )
        {
            if( xpos == 0 ) xpos = n+1 ;
            c = 'X';
        }
        else
            continue ;

        if( n < 13 )
            digits[n] = c ;
        n++ ;
        src_last = src ;
    }

    wrong_because = (const char*)NULL;

    if (xpos != 0) wrong_because = "Illegal position of X";

    if (n == 10)
    {
        if( destlen < 14 ) return (size_t)(-1);
        if (xpos == 10 ) wrong_because = (const char*)NULL ;
        for (checksum = 0, k = 0; k < 9; ++k)
        {
            dest[k] = digits[k];
            checksum += ISBN_DIGIT_VALUE(digits[k]) * (k+1);
        }
        dest[9] = digits[9];
        if ((checksum % 11) != ISBN_DIGIT_VALUE(digits[9]))
            wrong_because = "Invalid checksum";
        hyphenate10 = 0 ;
    }
    else if (n == 13)
    {
        if( destlen < 18 ) return (size_t)(-1);
        for (checksum = 0, k = 0; k < 13; ++k)
        {
            dest[k+(k>=3)] = digits[k] ;
            checksum += ISBN_DIGIT_VALUE(digits[k]) * (1+2*(k%2));
        }
        dest[3] = '-';
        if ((checksum % 10) != 0)
            wrong_because = "Invalid checksum";
        else if (digits[0] != '9' ||
                 digits[1] != '7' ||
                 (digits[2] != '8' && digits[2] != '9'))
            wrong_because = "Non-bookland prefix";
        /* Only change hyphenation for bookland 978; no prefixes
         * for booland 979 have been assigned yet.
         */
        hyphenate10 = digits[2] == '8' ? 4 : -1 ;
    }
    else if (n < 6)
    {
        /* Do nothing. Things shorter than 6 characters probably
         * aren't meant as ISBNs anyway. */
        hyphenate10 = -1;
    }
    else
    {
        wrong_because = "Invalid length" ;
        hyphenate10 = -1;
    }

    if (hyphenate10 >= 0)
    {
        dest[hyphenate10+10] = '\0' ;
        k = destlen-hyphenate10;
        if (k >= 20) k = 20;
        (void)ISBN_hyphenate(dest+hyphenate10,digits,k);

        k = strlen(dest);
        if (k > hyphenate10+10)
        {
            /* We only get here if the ISBN was actually hyphenated; this
             * may not happen if the ISBN is in an unknown number range.
             */
            if (wrong_because != NULL)
                (void)bad_ISBN(wrong_because,dest,k);
            
            n = src - src_last ;
            if (destlen < k + n)
                return (size_t)(-1);
            (void)memcpy(dest+k,src_last,n);
            return k+n ;
        }
    }

    if (wrong_because != NULL)
        (void)bad_ISBN(wrong_because,src_first,src_last-src_first);
    n = src - src_first ;
    if (destlen < n)
        return (size_t)(-1);
    (void)memcpy(dest,src_first,n);
    return n ;
}


static void
validate_ISSN(const char *the_ISSN, size_t n)
{
    int checksum;
    char ISSN[1 + MAX_ISSN + 1];	/* saved ISSN for error messages */
					/* (use slots 1..8 instead of 0..7) */
    size_t k;				/* index into ISSN[] */
    size_t nleft;

    /*******************************************************************
       ISSN numbers are 8-character values from the set [0-9Xx], with
       a checksum given by

		(sum(k=1:7) digit(k) * (k+2)) mod 11 == digit(8)

       where digits have their normal value, X (or x) as a digit has
       value 10, and spaces and hyphens are ignored.  The sum is
       bounded from above by 10*(3 + 4 + ... + 9) = 420, so even short
       (16-bit) integers are sufficient for the accumulation.

       ISSN digits are grouped into two 4-digit parts separated by
       space or hyphen.
    *******************************************************************/

    (void)strcpy(&ISSN[1],UNKNOWN_ISSN);
    nleft = copy_element(&ISSN[1], sizeof(ISSN)-1, the_ISSN, n);

    for (checksum = 0, k = 1; ISSN[k]; ++k)
    {
	if (k < MAX_ISSN)
	    checksum += ISSN_DIGIT_VALUE(ISSN[k]) * (k + 2);
	else if (k == MAX_ISSN)
	{
	    if ((checksum % 11) != ISSN_DIGIT_VALUE(ISSN[k]))
		bad_ISSN(ISSN);
	}
    } 					/* end for (loop over ISSN[]) */

    if ((strlen(&ISSN[1]) != MAX_ISSN) || (nleft > 0))
	bad_ISSN(ISSN);
}