summaryrefslogtreecommitdiff
path: root/doc/doc-scripts/g2t
blob: c840ac64f13da4643f95dde92614874f686fe84a (plain)
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
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
#! /usr/bin/perl -w

# A Perl script to turn the SGCAL source of the Exim documentation into
# Texinfo input, more or less...

# Supply the source file names as arguments.
# The output goes to the standard output.


##################################################
#         Ensure unique node name                #
##################################################

# Node names must be unique. Occasionally in the Exim spec there are duplicate
# section names, and it's become too much of a hassle to keep them distinct
# manually. So it is now automated.

########### Never really got this working. Abandoned ###############

sub unique {
my($node) = $_[0];
if (defined $node_names{$node})
  {
  $node_names{$node} += 1; 
  $node = "$node ($node_names{$node})"; 
  
print STDERR "+++ $node\n";
 
  }
else
  {
  $node_names{$node} = 0;
  }
$node;
}  



##################################################
#         De-comma a node name                   #
##################################################

# Commas, colons, and apostrophes are not permitted in Texinfo
# node names. I find this incredible, but it is clearly documented.
# The Exim manual has been re-organized not to have colons or
# apostrophes in any chapter or section titles, but I can't manage
# without commas. This function turns "," into " and", which is
# the best that can be done; we can use some clever Perlery to
# just take out commas before "and".

# Sigh. The Sendmail option -p<rval>:<sval> now means that there's a colon
# in the node name for that option. Turn the colon into <colon>. This is also
# done for menus.

# Another thing that causes problems in node names in some versions of
# Texinfo is the use of @sc{xxx} for small caps. Just turn these all into
# real caps. This is also done for menus.

sub decomma {
$_[0] =~ s/,(?!\sand)/ and/g;
$_[0] =~ s/,//g;
$_[0] =~ s/\@sc\{([^}]*)\}/\U$1/g;
$_[0] =~ s/:/<colon>/g;
$_[0];
}



##################################################
#           De-quote a string                    #
##################################################

# @x is turned into x, except when x=@, or when asis is set,
# in which case single @ must turn into @@. A single substitute
# doesn't work in the non-asis case, because of the problems of
# handling things like @@@$, so we do it the hard way.

sub dequote {
if ($asis) { $_[0] =~ s/@/@@/g; } else
  {
  $_[0] =~ s/@@/&at&/g;
  $_[0] =~ s/@([^@])/$1/g;
  $_[0] =~ s/&at&/@@/g;
  }
$_[0];
}


##################################################
#           Get next line                        #
##################################################

# Called from handle_directive, to get the next source line
# into $_.

sub get_next_line {
if ($processing_subsection)
  { return $_ = shift @SUBBUFFER; }
else
  { return $_ = <>; }
}



##################################################
#           Handle text lines                    #
##################################################

# This function is handed whole paragraphs, and we assume that
# SGCAL font changing markup is always complete within a paragraph.
# We have to replace escaped versions of significant characters with
# some magic before performing general transformations, and then
# put them back afterwards. The character & is not common in the text,
# and && is unknown, so we use that.

sub handle_text {
$_ = $_[0];

if ($asis)
  {
  $_ = dequote($_);
  s/(\{|\})/\@$1/g;
  return $_;
  }

while (/~~/)
  {
  $left = $`;
  ($name) = $' =~ /^(\w+)/;
  $right = $';

  $value = $references{$name};
  $value = "" if !defined($value);

  if ($value =~ /\*\*\*\*/)
    {
    $value = ($` eq $current_chapter)? "\"$'\"" :
      "\"$'\" in chapter \"$`\"";
    $value = "" if $value eq "\"\"";
    }
  elsif ($value !~ /^[0-9]+\.[0-9]+$/)   # quote unless version number
    {                                                
    $value = "\"$value\"";                          
    }

  $_ = "${left}${value}${right}";
  }

s/\@\@/&&a/g;         # @@
s/\@\\/&&b/g;         # @\
s/\@</&&l/g;          # @<
s/\@>/&&g/g;          # @>
s/\@\{/&&c/g;         # @{
s/\@\}/&&d/g;         # @}
s/\@#/&&s/g;          # @#

# Now remove all other @'s

$_ = dequote($_);

# Convert SGCAL markup

s/#/ /g;                            # turn #   into a space
s/\$~//g;                           # turn $~  into nothing
s/__/_/g;                           # turn __  into _
s/\$sm\{//g;                        # turn $sm{     into nothing
s/\$sc\{([^\}]*?)\}/$1/g;           # turn $sc{xxx} into xxx
s/\$st\{([^\}]*?)\}/$1/g;           # turn $st{xxx} into xxx
s/\$si\{([^\}]*?)\}/$1/g;           # turn $si{xxx} into xxx
s/\$tt\{([^\}]*?)\}/$1/g;           # turn $tt{xxx} into xxx

s/\$it\{([^\}]*?)\}/$1/g;           # turn $it{xxx} into xxx

s/\$bf\{([^\}]*?)\}/$1/g;           # turn $bf{xxx} into xxx
s/\$rm\{([^\}]*?)\}/$1/g;           # turn $rm{xxx} into xxx
s/\$cb\{([^\}]*?)\}/$1/g;           # turn $cb{xxx} into xxx

# This is a fudge for some specific usages of $<; can't just do a global
# is it occurs in things like $<variable name> as well.

s/\[\$<\]/[]/g;                     # turn [$<]     into []
s/&&b\$<\./&&b./g;                  # turn \$<.     into \.  (\ == &&b by now)
s/(\d)\$<-/$1-/g;                   # turn 0$<-     into 0-

# There is one case where the terminating } of an escape sequence is
# in another paragraph - this follows $sm{ - it can be fixed by
# removing any stray } in a paragraph that contains no { chars.

s/\}//g if !/\{/;

# Any remaining {} must be escaped to prevent Texinfo from complaining

s/(\{|\})/\@$1/g;

# Now to conversions that put {} into the file.
# Change <<..>> from @var to just <...> as the caps that Texinfo
# uses look far too shouty.

s/\\\\([^\\]*?)\\\\/\@sc\{\L$1\}/g; # turn \\xxx\\  into @sc{xxx}
s/\\\(([^)]*?)\)\\/\@file\{$1\}/g;  # turn \(xxx)\  into @file{xxx}
s/\\\"([^\"]*?)\"\\/\@file\{$1\}/g; # turn \"xxx"\  into @file{xxx}

s/\\\?([^?]*?)\?\\/$1/g;            # turn \?URL?\    into URL
s/<<([^>]*?)>>/<$1>/g;              # turn <<xxx>>    into <xxx>
s/\\\$([^\$]*?)\$\\/\$$1/g;         # turn \$xxx$\    into $xxx
s/\\\-([^-]*?)\-\\/\-$1/g;          # turn \-xxx-\    into -xxx
s/\\\*\*([^*]*?)\*\*\\/$1/g;        # turn \**xxx**\  into xxx
s/\[\(([\w\/]*)\)\]//g;             # remove inline HTML

s/\\\*([^*]*?)\*\\/\@dfn\{$1\}/g;     # turn \*xxx*\    into @dfn{xxx}
s/\\%([^*]*?)%\\/\@dfn\{$1\}/g;       # turn \%xxx%\    into @dfn{xxx}
s/:::([^:]*?)::/\@dfn\{:$1:\}/g;      # turn :::xxx::   into @dfn{:xxx:}
s/::([^:]*?)::/\@dfn\{$1:\}/g;        # turn ::xxx::    into @dfn{xxx:}
s/\\([^\\]*?)\\/\@dfn\{$1\}/g;        # turn \xxx\      into @dfn{xxx}
s/\$\*\$/\*/g;                        # turn $*$        into *

# Put back escaped SGCAL specials

s/&&a/\@\@/g;
s/&&b/\\/g;
s/&&l/</g;
s/&&g/>/g;
s/&&c/\@{/g;
s/&&rc/{/g;
s/&&rd/}/g;
s/&&d/\@}/g;
s/&&s/#/g;

# Remove any null flags ($$)

s/\$\$//g;

# If the paragraph starts with $c\b, change this into @center. Assume
# we don't ever get two of these in a row.

s/^\$c\b/\@center /;

# If the paragraph starts with $e\b, stuff some tabs in there, as
# Texinfo can't do this on its own (as far as I can see). They must
# tabs; Texinfo treats them as different to spaces. Sigh.

s/^\$e\b/\t\t\t\t\t\t\t/;

# Handle $t. The Exim spec only ever has one tab per line. Er, not
# quite true, but a good enough assumption. $t is always followed
# by a non-word character.

# The .tabs directive has stashed the value in the $tab variable.
# Don't count Texinfo font chars.

while (/(^|.+?\n)(.+?)\$t(\W.*\n)/)
  {
  $before = $` . $1;
  $after = $';
  $left = $2;
  $right = $3;

  $left =~ s/\s$//;
  $right =~ s/^\s+//;

  $plainleft = $left;
  $plainleft =~ s/\@[a-z]+\{([^}]+?)\}/$1/g;
  $plainleft =~ s/\@//g;

  $_ = $before . $left . (" " x ($tab - length($plainleft))) . $right . $after;

  # Fudge for the one case where there are two tabs

  if ($tab2 != 0)
    {
    $temp = $tab;
    $tab = $tab2;
    $tab2 = $temp;
    }
  }

# Return the new line (paragraph)

$_;
}



##################################################
#           Handle directive lines               #
##################################################

# Use get_next_line() instead of <> because this is called to process
# stacked up subsection lines

sub handle_directive {

my($new_lastwasitem) = 0;

# Chapter directives just require . => @; however, dequoting the
# line thereafter will remove the first @, so just force it back
# afterwards. If the chapter is is one describing a driver, set
# the driver name.

if (/\.chapter/)
  {
  tr/./@/;
  push(@ONESECTION, "@" . &dequote("$_\n"));
  $driver_name = (/The\s+(\S+)\s+(director|router|transport|authenticator)/)? $1 :
    (/Generic options common to both directors and routers/)?
      "director or router" :
    (/[Gg]eneric\s+options for (\S+)s/)? $1 : "";
  $driver_name = &dequote($driver_name);
  }

# Section directives just require . => @; however, dequoting the
# line thereafter will remove the first @, so just force it back
# afterwards. Remove any colons in section titles as they cause
# Texinfo trouble. Also remove any \\ (small caps) markup, which
# appears in a couple of cases.

elsif (/\.section/)
  {
  tr/./@/;
  s/://;
  s"\\\\""g;
  push(@ONESECTION, "@" . &dequote("$_\n"));

  # Horrible magic fudge to cope with the fact that exim_lock has
  # -v and -q options, just like the main program.

  $driver_name = "exim_lock" if /Mailbox maintenance/;
  
  # Similar magic for exiqgrep, which also duplicates options
  
  $driver_name = "exiqgrep" if /Selective queue listing/;  
  }

# .newline must put @* on the end of the previous line, if any, except
# inside a display, where it causes trouble.

elsif (/\.newline/)
  {
  if (@ONESECTION > 0 && ! $indisplay)
    {
    $_ = pop(@ONESECTION);
    s/(\n*)$/\@*$1/;
    push(@ONESECTION, $_);
    }
  }

# .blank turns into @sp, adding 1 if no argument

elsif (/\.blank/)
  {
  s/\.blank\s+(\d+)/\@sp $1/;
  s/\.blank/\@sp 1/;
  push(@ONESECTION, $_);
  }

# .rule turns into a line of hyphens

elsif (/\.rule/)
  {
  push(@ONESECTION, ("-" x ($in_itemize? 68 : 73)) . "\@*\n");
  }

# There's one explicit .tabset setting for two tab stops

elsif (/\.tabset\s*/)
  {
  $rest = $';
  ($first,$second) = $rest =~ /(\d+)em\s+(\d+)em/;
  $tab = ($first * 7)/6;
  $tab2 = $tab + ($second * 7)/6;
  }

# .tabs remembers the first (and only) tab setting

elsif (/\.tabs\s*/)
  {
  $tab = ($' * 7)/6;
  $tab2 = 0;
  }

# .tempindent is used only to align some of the expansion stuff nicely;
# just ignore it. It is used in conjunction with .push/.pop.

elsif (/\.(tempindent|push|pop)\s*/)
  {
  }

# There are some instances of .if ~~sys.fancy in the source. Some of these
# are two-part things, in which case we just keep the non-fancy. For diagrams,
# however, they are in three parts:
#
# .if ~~sys.fancy
# <aspic drawing stuff>
# .elif ~~nothtml
# <ascii art for txt and Texinfo>
# .else
# <HTML instructions for including a gif>
# .fi

elsif (/\.if \~\~sys\.fancy/)
  {
  while (&get_next_line())
    { last if /\.else\b/ || /\.elif\s+\~\~nothtml/ || /\.fi\b/; }

  if (/\.elif/)
    {
    $skip_else = 1;
    }
  }

# There are occasional requirements to do things differently for
# Texinfo/HTML and the PS/txt versions, and there are also some
# HTML-specific things.

elsif (/\.if\s+~~sgcal/ || /\.if\s+~~html/)
  {
  while (&get_next_line()) { last if /\.else\b/ || /\.fi\b/; }
  }

# We may also have Texinfo-specific bits

elsif (/^\.if\s+~~texinfo/)
  {
  $skip_else = 1;
  }

# Ignore any other .if directives

elsif (/\.if/) {}

# Skip else part if flag set

elsif (/\.else/ && $skip_else)
  {
  while (&get_next_line()) { last if /\.fi\b/; }
  $skip_else = 0;
  }

# Ignore other .fi and .else as any .if directives are handled specially

elsif (/\.fi/ || /\.else/) {}

# Ignore .indent

elsif (/\.indent/) {}

# Plain .index goes to @cindex - the "concept" index. Also, there
# are some calls to vindex and findex in the SGCAL source - treated
# as synonymous with .index - which are split into the equivalent
# indexes here.

elsif (/\.(.?)index/)
  {
  $rest = $';
  $letter = ($1 eq "")? "c" : $1;
  tr/./@/;                           # .index -> @index
  
  $rest =~ s/\\\(//g;                # Remove markup
  $rest =~ s/\)\\//g; 
  $rest =~ s/\\%//g;
  $rest =~ s/%\\//g;
  $rest =~ s/\\\*//g;
  $rest =~ s/\*\\//g;    
  $rest =~ s/\\"//g;
  $rest =~ s/"\\//g;
  $rest =~ s/:://g;
  $rest =~ s/\\-/-/g;
  $rest =~ s/-\\//g;
  $rest =~ s/~~//g;     
 
  $rest =~ tr/\\//d;                 # Remove \
   
  $rest =~ s/\@\$/\$/g;              # @$  -> $
  $rest =~ s/\@_/_/g;                # @_  -> _
  $rest =~ s/\@\+/+/g;               # @+  -> +
  $rest =~ s/\$\*\$/\*/g;            # $*$ -> *
  $rest =~ s/\$([^\$]+)\$/\$$1/g;    # $x$ -> $x
   
  $rest =~ s/^\s+//;                 # Remove leading spaces
  $rest =~ s/\s+$//;                 # Remove trailing spaces
  $rest =~ s/\|\|/:/;                # || -> : 
  push(@ONESECTION, "\@${letter}index $rest\n");

  # Duplicate entries for things that were listed as "x see y"

  if (defined $indirections{$rest})
    {
    push(@ONESECTION, "\@${letter}index $indirections{$rest}\n");
    }
  }

# Various flavours of numberpars map to itemize and enumerate.
# Haven't found a way of having a blank space 'bullet' yet, so
# currently using minus.

elsif (/\.numberpars/)
  {
  $rest = $';
  $type = "enumerate";
  $flag = "";

  if    ($rest =~ /\$\./)  { $flag = " \@bullet"; $type = "itemize" }
  elsif ($rest =~ /\" \"/) { $flag = " \@minus";  $type = "itemize"; }
  elsif ($rest =~ /roman/) { $flag = " a"; $type = "enumerate"; }

  push(@ONESECTION, "\n\@$type$flag\n\n\@item\n");
  push(@ENDLIST, $type);
  $in_itemize++;
  }

elsif (/\.nextp/)
  {
  push(@ONESECTION, "\n\@item\n");
  }

elsif (/\.endp/)
  {
  $endname = pop(@ENDLIST);
  push(@ONESECTION, "\@end $endname\n\n");
  $in_itemize--;
  }

# The normal .display (typewriter font) => @example, while the rm
# form goes to @display (no change of font). For Texinfo we need a
# blank line after @display.

elsif (/\.display/)
  {
  $type = /rm/? "display" : "example";
  $asis = 1 if /asis/;
  $indisplay = 1;
  push(@ONESECTION, "\@$type\n\n");
  push(@ENDLIST, $type);
  }

elsif (/\.endd/)
  {
  $asis = 0;
  $indisplay = 0;
  $endname = pop(@ENDLIST);
  push(@ONESECTION, "\@end $endname\n\n");
  }

elsif (/\.conf/)
  {
  ($option, $type, $default) =
    /\.conf\s+(\S+)\s+("(?:[^"]|"")+"|\S+)\s+("(?:[^"]|"")+"|.*)/;

  $option = &dequote($option);

  # If $type ends with $**$ (turned into a dagger for PS version),
  # replace with ", expanded". Remove any surrounding quotes.

  $type =~ s/^"([^"]+)"/$1/;
  $type =~ s/\$\*\*\$/, expanded/;

  # Default may be quoted, and it may also have quotes that are required,
  # if it is a string.

  $default =~ s/^"(.*)"$/$1/;
  $default =~ s/""/"/g;
  $default = &handle_text($default);

  push(@ONESECTION, "\nType: $type\@*\nDefault: $default\n\n");
  }

# Handle .startitems, .enditems, and .item

elsif (/\.startitem/ || /\.enditem/) {}

elsif (/\.item/)
  {
  $arg = $';
  $arg =~ s/^\s*"//;
  $arg =~ s/"\s*$//;
  $arg = &dequote($arg);
  $arg = &handle_text("\\**$arg**\\");

  # If there are two .items in a row, we don't want to put in the
  # separator line.

#  push(@ONESECTION, "\n\@example\n");
  push(@ONESECTION, "\n");
  if (! $lastwasitem)
    {
    push(@ONESECTION, "_" x 75, "\n\n");
    }
#  push(@ONESECTION, "$arg\n\@end example\n\n");
  push(@ONESECTION, "$arg\n\n");
  $new_lastwasitem = 1;
  }

elsif (/\.option/)
  {
  chomp($arg = $');
  $arg =~ s/^\s*//;
  $arg = &dequote("-$arg");
  $arg = &handle_text($arg);
  }

# Texinfo has no facility for emphasis bars.

elsif (/\.em/) {}
elsif (/\.nem/) {}

# Just ignore any .(r)set directives pro tem (or maybe always!)

elsif (/\.r?set/) {}

# Ignore .space, .linelength, and .justify

elsif (/\.space/ || /\.justify/ || /\.linelength/) {}

# Found an SGCAL directive that isn't dealt with. Oh dear.
# Turn the embarrassing characters into question marks and
# output it in an emphasized way.

else
  {
  tr/@{}/???/;
  push(@ONESECTION, "\n\>>>>>>> $_\n") if ! /^\.( |$)/;
  }

$lastwasitem = $new_lastwasitem;
}



##################################################
#             Flush a section                    #
##################################################

# $section_name is the name of the next section.
# $current_section is the name of the one we have buffered up.
# If it is unset, we are at the first section of a chapter.
# $previous_node is the section we last flushed if it was a node.

sub flush_section {

# If there is no text in the section, omit it entirely. However, it
# will have had a pointer set up at the start of the previous section.
# Remember what to replace this with when the chapter gets flushed.

my($skip) = 1;
foreach $s (@ONESECTION)
  {
  if ($s !~ /^(\@cindex|\@section|\s*$)/) { $skip = 0; last }
  }

if ($skip)
  {
  pop @section_list;
  $rewrite{$current_section} = $section_name;
  @ONESECTION = ();
  return;
  }

# There is data in the section: write it out to the chapter file

if ($current_section)
  {
  printf ONECHAPTER ("\@node %s, %s, %s, %s\n",
    &decomma($current_section), &decomma($section_name),
    &decomma($previous_node), &decomma($current_up));
  $previous_node = $current_section;
  while(scalar(@ONESECTION))
    { print ONECHAPTER shift(@ONESECTION); }
  }
else
  {
  while(scalar(@ONESECTION))
    { push(@TOPSECTION, shift(@ONESECTION)); }
  }
}



##################################################
#          Handle a "subsection"                 #
##################################################

# A "subsection" is a set of options that must have their own
# local menu. Do two passes; the first just collects the names
# for the menu. This is called for .conf and .option items.

sub handle_subsection{
my($type) = $_[0];
my($save_up) = $current_up;

$current_up = $current_section? $current_section : $current_chapter;

@sublist = ();
@SUBBUFFER = ();

while (<>)
  {
  last if /^\.end$type/;
  push(@SUBBUFFER, $_);

  # .conf takes the first non-space string as the name, but as there are
  # duplicate confs in various parts of the spec, use the driver name to
  # de-duplicate; .option takes the entire rest of arg as the name, but
  # removes any sequence of ... because this disturbs TexInfo. Also, it
  # turns @- into -.

  if (/^\.$type\s+(\S+)(.*)/)
    {
    if ($type eq "conf")
      {
      $name = &handle_text($1);
      $name .= " ($driver_name)" if ($driver_name ne "");
      }
    else
      {
      chomp($name = &handle_text("-$1$2"));
      $name =~ s/\s*\.\.\.//g;

      $name .= " ($driver_name)" if ($driver_name ne "");

      # There seems to be a major problem in texinfo with the string "--".
      # In the text it gets turned into a single hyphen. This happens if it
      # is used as a menu item, but *not* as a node name. Exim has a command
      # line option "--". With no special action, this appears in the menu
      # as "-", but then the info software complains there is no node called
      # "-". If we triple it in the menu it gets displayed OK, but building
      # software complains about non-existent cross references etc.

      # I have gone for the horrid kludge of turning it into "-<hyphen>"
      # in the menus and nodes.

      # Exim 4 has added --help, which has the same problem.

      $name = "-<hyphen>" if ($name eq "--");
      $name = "-<hyphen>help" if ($name eq "--help");
      }
    push(@sublist, $name);
    }
  }

push (@ONESECTION, "\n\@sp 2\n\@menu\n");
for ($i = 0; $i < scalar(@sublist); $i++)
  {
  $mitem = $sublist[$i];
  $mitem =~ s/\@sc\{([^}]*)\}/\U$1/g;       # Get rid of small caps
  $mitem =~ s/:/<colon>/g;                  # Get rid of colons
  push (@ONESECTION, "* ${mitem}::\n");
  }
push (@ONESECTION, "\@end menu\n\n");

$prevsub = $current_up;
$processing_subsection = 1;
while ($_ = shift(@SUBBUFFER))
  {
  if (/^\.$type\s+(\S+)/)
    {
    $name = shift @sublist;
    $next = (scalar(@sublist))? $sublist[0] : "";
    push @ONESECTION, sprintf("\@node %s, %s, %s, %s\n",
      &decomma($name), &decomma($next), &decomma($prevsub),
      &decomma($current_up));

    if ($name eq "-<hyphen>")    # Fudge for Texinfo
      {
      push(@ONESECTION,
	   "\@findex $name\n",
	   "\@unnumberedsubsec --- option\n");
      push(@ONESECTION,
           "This option consists of two consecutive hyphens. It appears in\n",
           "the menu as \"-<hyphen>\" because otherwise Texinfo gets\n",
           "confused with its cross-referencing.\n");
      }
    elsif ($name eq "-<hyphen>help")    # Fudge for Texinfo
      {
      push(@ONESECTION,
	   "\@findex $name\n",
	   "\@unnumberedsubsec ---help option\n");
      push(@ONESECTION,
           "This option consists of \"help\" preceded by two consecutive\n" .
           "hyphens. It appears in the menu as \"-<hyphen>help\" because\n" .
           "otherwise Texinfo gets confused with its cross-referencing.\n");
      }
    else
      {
      push(@ONESECTION,
	   "\@findex $name\n",
	   "\@unnumberedsubsec $name option\n");
      }

    $prevsub = $name;
    }

  # Then handle as text or directive

  if (substr($_, 0, 1) eq ".")
    { handle_directive(); }
  else
    {
    while($nextline = shift(@SUBBUFFER))
      {
      last if $nextline =~ /^(\.|\s*$)/;
      $_ .= $nextline;
      }
    push(@ONESECTION, handle_text($_));
    $_ = $nextline;
    last if !defined($_);
    redo;
    }
  }

$processing_subsection = 0;
$section_pending = 1;
$current_up = $save_up;
}




##################################################
#            Handle a single chapter             #
##################################################

sub handle_chapter{
chop;
($current_chapter) = /^\.chapter (.*)/;
$current_chapter = &dequote($current_chapter);

$current_chapter = $current_chapter;

my($tmp) = $current_chapter;
$tmp =~ s/\[\[\[\]\]\]/./;
print STDERR "processing chapter: $tmp\n";

# Remember the chapter name for the top-level menu

push(@chapter_list, $current_chapter);

# Open a temporary file to hold the chapter's text while collecting
# all its sections for a chapter-level menu.

$ONECHAPTER = "/tmp/ONECHAPTER.$$";
open(ONECHAPTER, ">$ONECHAPTER") || die "Can't open $ONECHAPTER for writing";

# Initialize for handling sections

@section_list = ();
%rewrite = ();
@ONESECTION = ();
@TOPSECTION = ();
undef $current_section;
undef $next_node;

$processing_subsection = 0;

$previous_node = $current_up = $current_chapter;
$section_pending = 0;

# Handle the .chapter directive as the first text of a section without
# a section title.

handle_directive();

# Loop, handling each section. Assume they are sufficiently short that
# we can buffer the text in store, in an array called ONESECTION, instead
# of thrashing yet another file.

while (<>)
  {
  last if /^\.chapter /;

  # Handle a new section, preserving $_ (handle_text flattens it).
  # It seems we cannot get a fullstop into a Texinfo node name; use a magic
  # character string that gets turned back into a dot by the post-processing.

  if (/^\.section\s+/)
    {
    $save = $_;
    $section_name = $';
    $section_name =~ s/(\s|\n)+$//;
    $section_name =~ s/://;
    $section_name = &handle_text($section_name);
    flush_section();
    push(@section_list, $section_name);
    $current_section = $section_name;
    $next_node = $section_name if !$next_node;
    $section_pending = 0;
    $_ = $save;
    }

  # The .startconf macro introduces a set of .conf's which must have
  # their own local set of menus. Suspend processing the section while
  # we sort out the menu and copy their data. This is all done in a
  # subroutine that is shared with options.

  elsif (/^\.startconf\s+(.*)/)
    {
    $confuse = $1; 
    $confuse = &dequote($confuse); 
    handle_subsection("conf");
    next;
    }

  elsif (/^\.startoption/)
    {
    handle_subsection("option");
    next;
    }

  # Deal with the actual data lines; if there's a section pending
  # start a new section on hitting some text. We hope this happens
  # only once per chapter...

  if (substr($_, 0, 1) eq ".")
    {
    handle_directive();
    }
  else
    {
    while($nextline = <>)
      {
      last if $nextline =~ /^(\.|\s*$)/;
      $_ .= $nextline;
      }
    if ($section_pending && !/^\s*$/)
      {
      $section_name = (defined $current_section)?
        "$current_section (continued)" :
        "$current_chapter (continued)" ;
      flush_section();
      push(@section_list, $section_name);
      $current_section = $section_name;
      $next_node = $section_name if !$next_node;
      $section_pending = 0;
      }

    push(@ONESECTION, handle_text($_));
    $_ = $nextline;
    last if !defined($_);
    redo;
    }
  }

# Flush any pending text, making its next field null.
# and fudging section_name for the final section of the previous.

$section_name = "";
flush_section();

# Set up section name as the start of the next chapter

$section_name = "Concept Index" if (!$doing_filter);

if (defined $_ && /^\.chapter (.*)/)
  {
  $section_name = $1;
  $section_name = &dequote($section_name);
  }
$next_node = $section_name;

# Write out the chapter to the CHAPTERS file, sticking the chapter
# menu after the text that came before the first section heading. This
# will always at least contain the chapter title.

printf CHAPTERS ("\@node %s, %s, %s, Top\n",
  &decomma($current_chapter), &decomma($next_node),
  &decomma($previous_chapter));

# The pre-section stuff; if we hit an @end menu line, it is the menu of
# a "subsection" before the first section. In that case, we need to put
# the chapter's menu one the end of it, and then resume with the rest of
# the TOPSECTION data afterwards. We also need to thread together this
# "subsection"s nodes because they are all at the same level under the
# chapter.

$in_menu = 0;
while(scalar(@TOPSECTION))
  {
  $s = shift(@TOPSECTION);
  if ($s =~ /^\@end menu/)
    {
    $in_menu = 1;
    last;
    }
  print CHAPTERS $s;
  }

# Menu for sections

undef $next_actual_section;
undef $point_back;

if (scalar(@section_list))
  {
  print CHAPTERS "\n\@sp 2\n\@menu\n" if ! $in_menu;
  $next_actual_section = $section_list[0];
  for ($i = 0; $i < scalar(@section_list); $i++)
    {
    $section_name = $section_list[$i];
    $section_name =~ s/\@sc\{([^}]*)\}/\U$1/g;
    print CHAPTERS "* ${section_name}::\n";
    }
  $in_menu = 1;
  }
print CHAPTERS "\@end menu\n\n" if $in_menu;

# Remainder of topsection; we must arrange that the final @node in
# it (which will have a blank "next" field) actually points on to
# the next section, if any. If this happens, then the next section
# must point back to the final @node.

while(scalar(@TOPSECTION))
  {
  $s = shift(@TOPSECTION);
  if ($next_actual_section && $s =~
         /^\@node\s+([^,]+),\s*,\s*([^,]*),\s*(.*)/)
    {
    my($t1, $t2, $t3) = ($1, $2, $3);    # So can be decomma'd
    printf CHAPTERS ("\@node %s, %s, %s, %s\n", &decomma($t1),
      &decomma($next_actual_section), &decomma($t2), &decomma($t3));
    $point_back = $1;
    }
  else { print CHAPTERS $s; }
  }

close(ONECHAPTER);
open(ONECHAPTER, "$ONECHAPTER") || die "Can't open $ONECHAPTER for reading";

# While copying the chapter data, check for node references to empty
# sections that got omitted and correct them, and correct the prev pointer
# in the first node if necessary.

while ($buff = <ONECHAPTER>)
  {
  foreach $key (keys %rewrite)
    {
    $buff =~ s/$key/$rewrite{$key}/;
    }
  if ($point_back && $buff =~ /^\@node\s+([^,]+),\s*([^,]*),\s*([^,]*),\s*(.*)/)
    {
    my($t1, $t2, $t4) = ($1, $2, $4);   # so can be decomma'd
    printf CHAPTERS ("\@node %s, %s, %s, %s\n", &decomma($t1),
      &decomma($t2), &decomma($point_back), &decomma($t4));
    undef $point_back;
    }
  else { print CHAPTERS $buff; }
  }

$previous_chapter = $current_chapter;

close(ONECHAPTER);
unlink($ONECHAPTER);
}



##################################################
#                Main Program                    #
##################################################

# This is a two-pass algorithm. The first pass goes through and gets the
# variable names for cross references. The second pass does the real work,
# but we can't just read through doing the translation in one pass. We need
# to know the list of chapters in order to build a top-level menu, and for
# each chapter we need to know the sections in order to build a section
# menu. Consequently, make use of temporary files to buffer things.

# This script is used for the filter document and the overview as well;
# flags tell it if it is doing one of them.

$doing_filter = 0;
$skip_else = 0;
$in_itemize = 0;
$lastwasitem = 0;
$confuse = "";

$chapter_number = 0;
$section_number = 0;

if ($#ARGV >= 0 && $ARGV[0] eq "-filter")
  {
  $doing_filter = 1;
  shift @ARGV;
  }

# First pass: Just fish out variable settings. Save the arguments so that
# they can be reinstated for a second pass.

print STDERR "Scanning for references\n";
@save_argv = @ARGV;

# Pick up any .set directives right at the very start

while (<>)
  {
  last if ! /^\.set\s+(\S+)\s+(.+)$/;
  $name = $1;
  $value = $2;
  $value =~ s/^\"?(.*?)\"?\s*$/$1/;
  $references{$name} = $value;
  }

# Now skip everything before the first .chapter except for
# .index lines that set up indirections. Save these so that
# the relevant index entries can be duplicated.

while (<>)
  {
  if (/^\.chapter\s+(.+)$/)
    {
    $chapter_number++;
    $section_number = 0;
    $current_chapter = $1;
    $current_chapter = $current_chapter;
    $current_section = "";
    last;
    }

  if (/^\.index\s+([^\$]+)\s+\$it\{see\s+([^}]+)\}\s*$/)
    {
    $indirections{"$2"} = $1;
    }
  }

# Do the business

while (<>)
  {
  if (/^\.chapter\s+(.+)$/)
    {
    $current_chapter = $1;
    $current_chapter = &dequote($current_chapter);
    $current_section = "";
    }
  elsif (/^\.section\s+(.+)$/)
    {
    $current_section = $1;
    $current_section = &dequote($current_section);
    $current_section =~ s/://;
    }
  elsif (/^\.r?set\s+(\S+)\s+(.+)$/ && $1 ne "runningfoot")
    {
    $name = $1;
    $value = $2;

    # Only set the first time. This handles a few special cases in part2
    # which is included in the filter text as well.

    if (!exists($references{$name}))
      {
      $value =~ s/^\"?(.*?)\"?\s*$/$1/;
      $value =~ s/~~chapter\./~~chapter****/;
      $value =~ s/~~chapter/$current_chapter/;
      $value =~ s/~~section/$current_section/;
      $references{$name} = $value;
      }
    }
  }

$final_chapter = defined($current_chapter)? $current_chapter : "";

# Reinstate ARGV with the list of files and proceed to the main pass

@ARGV = @save_argv;

# $asis is set true when processing .display asis blocks, to stop
# characters getting interpreted.

$asis = 0;

# $indisplay is set true while processing .display blocks, to stop
# .newlines being handled therein (adding @* wrecks alignment)

$indisplay = 0;

# $tab is set to the value of the tab stop - only one stop is ever used
# in the Exim source.

$tab = 0;

# Current driver name, for disambiguating nodes

$driver_name = "";

# $section_pending is set if a new section is to be started on hitting
# any data lines.

$section_pending = 0;

# Open a file to buffer up the entire set of chapters

$CHAPTERS = "/tmp/CHAPTERS.$$";
open(CHAPTERS, ">$CHAPTERS") || die "Can't open $CHAPTERS for writing";

# Skip everything before the first .chapter

while (<>) { last if /^\.chapter /; }

# Loop, handling each chapter

$current_up = "";
$previous_chapter = "Top";
$previous_node = "Top";

$chapter_number = 0;
$section_number = 0;

while (defined ($_) && /^\.chapter /)
  {
  handle_chapter();
  }

# Output the stuff at the start of the file

print "\\input texinfo\n";

print "\@set{wmYear} 2003\n";
print "\@set{wmAuthor} Philip Hazel\n";
print "\@set{wmAuthor_email} <ph10\@\@cus.cam.ac.uk>\n";
print "\@set{COPYRIGHT1} Copyright \@copyright{} \@value{wmYear} University of Cambridge\n";

print "\@c %**start of header\n";

if (!$doing_filter)
  {
  print "\@setfilename spec.info\n";
  print "\@settitle Exim Specification\n";
  }
else
  {
  print "\@setfilename filter.info\n";
  print "\@settitle Exim Filter Specification\n";
  }

print "\@paragraphindent 0\n";
print "\@c %**end of header\n\n";


print "\@titlepage\n";
print "\@title The Exim Mail Transfer Agent\n";
print "\@author \@value{wmAuthor}\n";

print "\@page\n";
print "\@vskip 0pt plus 1filll\n";

print "Permission is granted to make and distribute verbatim copies of this manual provided the\n";
print "copyright notice and this permission notice are preserved on all copies.\n";

print "\@sp2\n";
print "\@value{COPYRIGHT1}\@*\n";

print "\@end titlepage\n\n";

# Output the top-level node and its introductory blurb

print "\@node       Top,       $chapter_list[0], (dir), (dir)\n";
print "\@top\n";

if (!$doing_filter)
{
print <<End;
The Exim Mail Transfer Agent\@*
****************************

The specification of the Exim Mail Transfer Agent is converted mechanically
into Texinfo format from its original marked-up source. Some typographic
representations are changed, chapters and sections cannot be numbered, and
Texinfo lacks the ability to mark updated parts of the specification with
change bars.

Because the chapters and sections are unnumbered, cross references are set to
their names. This makes the English a bit odd, with phrases like \`see chapter
\"Retry configuration\"\' but it seemed very cumbersome to change this to \`see
the chapter entitled \"Retry configuration\"\' each time.

Each chapter, section, and configuration option has been placed in a separate
Texinfo node. Texinfo doesn\'t allow commas, colons, or apostrophes in node
names, which is a rather nasty restriction. I have arranged not to use colons
or apostrophes in section titles, but cannot bring myself to omit them from
titles such as \"The foo, bar and baz commands\". For the corresponding node
names I have just used multiple occurrences of \"and\", though it looks very
ugly.

If a chapter or section continues after a list of configuration options that is
not in a new section, a new node is started, using the chapter\'s or section\'s
name plus \`(continued)\'. The \`Up\' operation from a section or configuration
option returns to the start of the current chapter; the \`Up\' operation at a
chapter start returns to the top of the document; the \`Up\' in a list of
configuration options within a section returns to the top of that section.

A number of drivers have options with the same name, so they have been
disambiguated by adding the name of the driver to its option names in order to
create node names. Thus, for example, the specification of the \`command\'
options of the \`lmtp\' and \`pipe\' transports are in nodes called \`command
(lmtp)\' and \`command (pipe)\', respectively.

End
}

else
{
print <<End;
Filtering with the Exim Mail Transfer Agent\@*
*******************************************

The specifications of the Exim Mail Transfer Agent\'s filtering facility is
converted mechanically into Texinfo format from its original marked-up source.
Some typographic representations are changed, chapters and sections cannot be
numbered, and Texinfo lacks the ability to mark updated parts of the
specification with change bars.

Because the chapters and sections are unnumbered, cross references are set to
their names. This makes the English a bit odd, with phrases like \`see section
\"Multiple personal mailboxes\"\' but it seemed very cumbersome to change this to
\`see the section entitled \"Multiple personal mailboxes\"\' each time.

End
}

# Output the top-level menu

print "\@menu\n";

while (scalar(@chapter_list))
  {
  $name = &decomma(shift(@chapter_list));
  print "* ${name}::\n";
  }
print "* Concept Index::\n" if (!$doing_filter);
print "\@end menu\n\n";

# Copy the chapters, then delete the temporary file

close(CHAPTERS);
open(CHAPTERS, "$CHAPTERS") || die "Can't open $CHAPTERS for reading";
print $buff while($buff = <CHAPTERS>);
close(CHAPTERS);
unlink($CHAPTERS);

# Output the finishing off stuff

if (!$doing_filter)
  {
  print "\@node Concept Index, , $final_chapter, Top\n";
  print "\@chapter Concept Index\n\@printindex cp\n";
  print "\@chapter Function Index\n\@printindex fn\n";
  }
print "\@contents\n";
print "\@bye\n";

# End