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
  
/* 
 * AutoDoc mk II extraction script. 
 * 
 * Henrik Grubbström 2001-01-08 
 */ 
 
#pike __REAL_VERSION__ 
 
constant description = "Extracts autodoc from Pike or C code."; 
 
string imgsrc; 
string imgdir; 
 
Tools.AutoDoc.Flags flags = Tools.AutoDoc.FLAG_NORMAL; 
 
int verbosity = Tools.AutoDoc.FLAG_NORMAL; 
 
int source_timestamp; 
 
int num_updated_files; 
 
// See the BMML rewriter further below about these values. 
// Not that there is both a start and an end sentinel. 
constant bmml_invalidation_times = ({ 
  0, 855275859, 855644314, 855648534, 855725630, 857038037, 0x7fffffff, 
}); 
 
int bmml_invalidate_before; 
int bmml_invalidate_after; 
 
int main(int n, array(string) args) 
{ 
  string srcdir, builddir = "./"; 
  array(string) root = ({"predef::"}); 
 
  int return_count; 
 
  foreach(Getopt.find_all_options(args, ({ 
    ({ "srcdir",     Getopt.HAS_ARG,      "--srcdir" }), 
    ({ "imgsrc",     Getopt.HAS_ARG,      "--imgsrc" }), 
    ({ "builddir",   Getopt.HAS_ARG,      "--builddir" }), 
    ({ "imgdir",     Getopt.NO_ARG,       "--imgdir" }), 
    ({ "root",       Getopt.HAS_ARG,      "--root" }), 
    ({ "compat",     Getopt.NO_ARG,       "--compat" }), 
    ({ "count",      Getopt.NO_ARG,       "--count" }), 
    ({ "timestamp",  Getopt.HAS_ARG,      "--source-timestamp" }), 
    ({ "no-dynamic", Getopt.NO_ARG,       "--no-dynamic" }), 
    ({ "keep-going", Getopt.NO_ARG,       "--keep-going" }), 
    ({ "verbose",    Getopt.NO_ARG,       "-v,--verbose"/"," }), 
    ({ "quiet",      Getopt.NO_ARG,       "-q,--quiet"/"," }), 
    ({ "help",       Getopt.NO_ARG,       "-h,--help"/"," }) })), array opt) 
    switch(opt[0]) 
    { 
    case "srcdir": 
      srcdir = combine_path(getcwd(), opt[1]); 
      if(srcdir[-1]!='/') srcdir += "/"; 
      break; 
    case "imgsrc": 
      imgsrc = combine_path(getcwd(), opt[1]); 
      break; 
    case "builddir": 
      builddir = combine_path(getcwd(), opt[1]); 
      if(builddir[-1]!='/') builddir += "/"; 
      break; 
    case "imgdir": 
      imgdir = combine_path(getcwd(), opt[1]); 
      if(imgdir[-1]!='/') imgdir += "/"; 
      break; 
    case "compat": 
      flags |= Tools.AutoDoc.FLAG_COMPAT; 
      break; 
    case "count": 
      return_count = 1; 
      break; 
    case "no-dynamic": 
      flags |= Tools.AutoDoc.FLAG_NO_DYNAMIC; 
      break; 
    case "keep-going": 
      flags |= Tools.AutoDoc.FLAG_KEEP_GOING; 
      break; 
    case "root": 
      root = opt[1]/"."; 
      break; 
    case "quiet": 
      flags = (flags & ~Tools.AutoDoc.FLAG_VERB_MASK)|Tools.AutoDoc.FLAG_QUIET; 
      break; 
    case "verbose": 
      verbosity = flags & Tools.AutoDoc.FLAG_VERB_MASK; 
      if (verbosity < Tools.AutoDoc.FLAG_DEBUG) verbosity++; 
      flags = (flags & ~Tools.AutoDoc.FLAG_VERB_MASK)|verbosity; 
      break; 
    case "timestamp": 
      // This is currently only used by the BMML handler. 
      // It's used to convert /precompiled/foo module-references 
      // to their corresponding module name as of this timestamp. 
      source_timestamp = (int)opt[1]; 
 
      for (int i = sizeof(bmml_invalidation_times); i--;) { 
        if (bmml_invalidation_times[i] < source_timestamp) { 
          bmml_invalidate_before = bmml_invalidation_times[i]; 
          bmml_invalidate_after = bmml_invalidation_times[i+1]; 
          break; 
        } 
      } 
      break; 
    case "help": 
      werror("Usage:\n" 
             "\tpike -x extract_autodoc [-q|--quiet] [-v|--verbose]\n" 
             "\t     [--compat] [--no-dynamic] [--keep-going]\n" 
             "\t     --srcdir=<srcdir>\n" 
             "\t     [--imgsrcdir=<imgsrcdir>] [--builddir=<builddir>]\n" 
             "\t     [--imgdir=<imgdir>] [--root=<module>]\n" 
             "\t     [file1 [... filen]]\n"); 
      return 0; 
    } 
 
  verbosity = flags & Tools.AutoDoc.FLAG_VERB_MASK; 
 
  args = args[1..] - ({0}); 
 
  if(srcdir) 
    recurse(srcdir, builddir, 0, root); 
  else if(sizeof(args)) { 
    foreach(args, string fn) 
    { 
      Stdio.Stat stat = file_stat(fn); 
      Stdio.Stat dstat = file_stat(builddir+fn+".xml"); 
 
      // Build the xml file if it doesn't exist, if it is older than the 
      // source file, or if the root has changed since the previous build. 
      if(!dstat || dstat->mtime <= stat->mtime) { 
        string res = extract(fn, imgdir, builddir, root); 
 
        if(!res) { 
          if (flags & Tools.AutoDoc.FLAG_KEEP_GOING) continue; 
          exit(1); 
        } 
 
        num_updated_files++; 
 
        if (sizeof(res) && (res != "\n")) { 
          // Validate the extracted XML. 
          mixed err = catch { 
              Parser.XML.Tree.simple_parse_input(res); 
            }; 
          if (err) { 
            werror("Extractor generated broken XML for file %s:\n" 
                   "%s", 
                   builddir + fn + ".xml", describe_error(err)); 
            rm(builddir+fn+".xml"); 
            Stdio.write_file(builddir+fn+".brokenxml", res); 
            Stdio.write_file(builddir+fn+".xml.stamp", 
                             (string)source_timestamp); 
            werror("Result saved as %s.\n", builddir + fn + ".brokenxml"); 
            if (flags & Tools.AutoDoc.FLAG_KEEP_GOING) continue; 
            exit(1); 
          } 
        } 
 
        Stdio.write_file(builddir+fn+".xml", res); 
        Stdio.write_file(builddir+fn+".xml.stamp", (string)source_timestamp); 
      } 
    } 
  } 
  else { 
    werror("No source directory or input files given.\n"); 
    return 1; 
  } 
 
  if (return_count) return num_updated_files; 
  return 0; 
} 
 
void recurse(string srcdir, string builddir, int root_ts, array(string) root) 
{ 
  if (verbosity > 1) 
    werror("Extracting from %s\n", srcdir); 
 
  Stdio.Stat st; 
  if(file_stat(srcdir+"/.noautodoc")) 
    return; 
 
  if(st = file_stat(srcdir+"/.autodoc")) { 
    // Note .autodoc files are space-separated to allow for namespaces like 
    //      "7.0::". 
    root = (Stdio.read_file(srcdir+"/.autodoc")/"\n")[0]/" " - ({""}); 
    if (!sizeof(root) || !has_suffix(root[0], "::")) { 
      if (sizeof(root) && has_value(root[0], "::")) { 
        // Broken .autodoc file 
        werror("Invalid syntax in %s.\n" 
               ":: Must be last in the token.\n", 
               srcdir + "/.autodoc"); 
        if (!flags & Tools.AutoDoc.FLAG_COMPAT) { 
          error("Invalid syntax in .autodoc file.\n"); 
        } 
        array(string) a = root[0]/"::"; 
        root = ({ a[0] + "::" }) + a[1..] + root[1..]; 
      } else { 
        // The default namespace is predef:: 
        root = ({ "predef::" }) + root; 
      } 
    } 
    root_ts = st->mtime; 
  } else if (st = file_stat(srcdir+"/.bmmlrc")) { 
    if (Stdio.read_file(srcdir+"/.bmmlrc") == "prefix internal\n") { 
      root = ({ "c::" }); 
    } 
    root_ts = st->mtime; 
  } 
 
  foreach(get_dir(builddir), string fn) { 
    if ((fn != ".cache.xml") && has_suffix(fn, ".xml")) { 
      if (!Stdio.is_file(srcdir + fn[..<4])) { 
        if (verbosity > 0) 
          werror("The file %O is no more.\n", srcdir + fn[..<4]); 
 
        num_updated_files++; 
        rm(builddir + fn); 
        rm(builddir + fn[..<4] + ".brokenxml"); 
        rm(builddir + fn + ".stamp"); 
        rm(builddir + ".cache.xml.stamp"); 
      } else if (source_timestamp && (source_timestamp < 950000000) && 
                 (sizeof(fn/".") == 2)) { 
        // BMML. 
        int old_ts = (int)Stdio.read_bytes(builddir + fn + ".stamp"); 
        if ((old_ts < bmml_invalidate_before) || 
            (old_ts >= bmml_invalidate_after)) { 
          // BMML file that may have changed at this time. 
          if (verbosity > 1) 
            werror("Forcing reextraction of %s.\n", srcdir + fn[..<4]); 
          rm(builddir + fn + ".stamp"); 
        } 
      } 
    } else if (Stdio.is_dir(builddir + fn) && !Stdio.is_dir(srcdir + fn)) { 
      // Recurse and clean away old obsolete files. 
      recurse(srcdir + fn + "/", builddir + fn + "/", root_ts, root); 
      rm(builddir + fn + "/.cache.xml.stamp"); 
      rm(builddir + fn + "/.cache.xml"); 
      // Try deleting the directory. 
      rm(builddir + fn); 
      rm(builddir + ".cache.xml.stamp"); 
    } 
  } 
 
  if(!file_stat(srcdir)) { 
    if (!Stdio.is_dir(builddir)) 
      werror("Could not find directory %O.\n", srcdir); 
    return; 
  } 
 
  // do not recurse into the build dir directory to avoid infinite loop 
  // by building the autodoc of the autodoc and so on 
  if(search(builddir, srcdir) == -1) { 
    foreach(filter(get_dir(srcdir), has_suffix, ".cmod"), string fn) { 
      Stdio.Stat stat = file_stat(srcdir + fn); 
      if (!stat || !stat->isreg) continue; 
      int mtime = stat->mtime; 
 
      // Check for #cmod_include. 
      multiset(string) checked = (<>); 
      string data = Stdio.read_bytes(srcdir + fn); 
      foreach(filter(data/"\n", has_prefix, "#"), string line) { 
        if (sscanf(line, "#%*[ \t]cmod_include%*[ \t]\"%s\"", string inc) > 2) { 
          if (!checked[inc]) { 
            checked[inc] = 1; 
            stat = file_stat(combine_path(srcdir, inc)); 
            if (stat && stat->isreg && stat->mtime > mtime) { 
              mtime = stat->mtime; 
            } 
          } 
        } 
      } 
 
      string target = fn[..<5] + ".c"; 
      stat = file_stat(srcdir + target); 
      if (!stat || stat->mtime <= mtime) { 
        // Regenerate the target. 
        mixed err; 
        if (err = catch { 
            Tools.Standalone.precompile()-> 
              main(6, ({ "precompile.pike", "--api=max", "-w", 
                         "-o", srcdir+target, srcdir+fn })); 
          }) { 
          // Something failed. 
          werror("Precompilation of %s to %s failed:\n" 
                 "%s", 
                 srcdir+fn, srcdir+target, describe_error(err)); 
          rm(srcdir+target); 
          rm(builddir+target+".xml"); 
          rm(builddir+target+".xml.stamp"); 
        } 
      } 
    } 
    foreach(get_dir(srcdir), string fn) { 
      if(fn=="CVS") continue; 
      if(fn[0]=='.') continue; 
      if(fn[-1]=='~') continue; 
      if(fn[0]=='#' && fn[-1]=='#') continue; 
 
      Stdio.Stat stat = file_stat(srcdir+fn); 
 
      if (!stat) continue; 
 
      if(stat->isdir) { 
        if(!file_stat(builddir+fn)) mkdir(builddir+fn); 
        string mod_name = fn; 
        sscanf(mod_name, "%s.pmod", mod_name); 
        recurse(srcdir+fn+"/", builddir+fn+"/", root_ts, root + ({mod_name})); 
        continue; 
      } 
 
      if(stat->size<1) continue; 
 
      if(!has_suffix(fn, ".pike") && !has_suffix(fn, ".pike.in") && 
         !has_suffix(fn, ".pmod") && !has_suffix(fn, ".pmod.in") && 
         //       !has_suffix(fn, ".cmod") && !has_suffix(fn, ".cmod.in") && 
         !has_suffix(fn, ".c") && !has_suffix(fn, ".cc") && 
         !has_suffix(fn, ".m") && !has_suffix(fn, ".bmml") && 
         has_value(fn, ".")) continue; 
 
      Stdio.Stat dstat = file_stat(builddir+fn+".xml") && 
        file_stat(builddir+fn+".xml.stamp"); 
 
      // Build the xml file if it doesn't exist, if it is older than the 
      // source file, or if the root has changed since the previous build. 
      if(!dstat || dstat->mtime <= stat->mtime || dstat->mtime <= root_ts) { 
        string res = extract(srcdir+fn, imgdir, builddir, root); 
        if(!res) { 
          if (!(flags & Tools.AutoDoc.FLAG_KEEP_GOING)) 
            exit(1); 
          res = ""; 
        } 
 
        if (sizeof(res) && (res != "\n")) { 
          // Validate the extracted XML. 
          mixed err = catch { 
              Parser.XML.Tree.SimpleRootNode root = 
                Parser.XML.Tree.simple_parse_input(res); 
              Parser.XML.Tree.Node n = root->get_children()[0]; 
              if ((n->get_node_type() == Parser.XML.Tree.XML_HEADER) && 
                  (lower_case(n->get_attributes()->encoding||"") == "utf-8")) { 
                // Validate UTF-8 encoding. 
                string dec = utf8_to_string(res); 
              } 
            }; 
          if (err) { 
            werror("Extractor generated broken XML for file %s:\n" 
                   "%s", 
                   builddir + fn + ".xml", describe_error(err)); 
            Stdio.write_file(builddir+fn+".brokenxml", res); 
            if (Stdio.exist(builddir+fn+".xml")) { 
              num_updated_files++; 
              rm(builddir+fn+".xml"); 
            } 
            Stdio.write_file(builddir+fn+".xml.stamp", 
                             (string)source_timestamp); 
            werror("Result saved as %s.\n", builddir + fn + ".brokenxml"); 
            if (flags & Tools.AutoDoc.FLAG_KEEP_GOING) continue; 
            exit(1); 
          } 
        } 
 
        string orig = Stdio.read_bytes(builddir + fn + ".xml"); 
        if (res != orig) { 
          num_updated_files++; 
      Stdio.mkdirhier( builddir ); 
          Stdio.write_file(builddir+fn+".xml", res); 
          rm(builddir + fn + ".brokenxml"); 
        } 
        Stdio.write_file(builddir+fn+".xml.stamp", (string)source_timestamp); 
      } 
    } 
  } 
} 
 
string extract(string filename, string imgdest, 
               string builddir, array(string) root) 
{ 
  if (verbosity > 1) 
    werror("Extracting file %O...\n", filename); 
  string file = Stdio.read_file(filename); 
 
  if (!file) { 
    werror("WARNING: Failed to read file %O!\n", filename); 
    return "\n"; 
  } 
 
  if (has_suffix(filename, ".bmml") || !has_value(basename(filename), ".")) { 
    if (has_suffix(filename, "/control_structures") || 
        has_suffix(filename, "/reserved")) { 
      // These two have a unique "markup" that is not yet supported 
      // by the BMML parser. 
      return ""; 
    } 
    if(flags & Tools.AutoDoc.FLAG_COMPAT && 
       (source_timestamp >= 855275859) && 
       has_value(file, "/precompiled/")) { 
      // After new module system was implemented. 
      // Convert the old module names according to the timestamp 
      if (source_timestamp < 855644314) { 
        // Before the modules were capitalized. 
        file = replace(file, 
                       ({ "/precompiled/FILE", 
                          "/precompiled/file", 
                          "/precompiled/port", 
                          "/precompiled/stack", 
                          "/precompiled/string_buffer", 
                       }), 
                       ({ "stdio.FILE", 
                          "stdio.File", 
                          "stdio.Port", 
                          "stack", 
                          "string_functions.String_buffer", 
                       })); 
      } else { 
        file = replace(file, 
                       ({ "/precompiled/FILE", 
                          "/precompiled/file", 
                          "/precompiled/port", 
                          "/precompiled/stack", 
                          "/precompiled/string_buffer", 
                       }), 
                       ({ "Stdio.FILE", 
                          "Stdio.File", 
                          "Stdio.Port", 
                          "Stack", 
                          "String.String_buffer", 
                       })); 
      } 
      if (source_timestamp < 855648534) { 
        // Before the modules were renamed and capitalized. 
        file = replace(file, 
                       ({ "/precompiled/gdbm", 
                          "/precompiled/mpz", 
                          "/precompiled/regexp", 
                          "/precompiled/sql", 
                          "/precompiled/sql_result", 
                          "/precompiled/sql/*", 
                          "/precompiled/sql/mysql", 
                          "/precompiled/sql/mysql_result", 
                       }), 
                       ({ "gdbmmod.gdbm", 
                          "gmpmod.mpz", 
                          "regexp.regexp", 
                          "sql.sql", 
                          "sql.sql_result", 
                          "sql.*", 
                          "sql.mysql", 
                          "sql.mysql_result", 
                       })); 
      } else { 
        file = replace(file, 
                       ({ "/precompiled/gdbm", 
                          "/precompiled/mpz", 
                          "/precompiled/regexp", 
                          "/precompiled/sql", 
                          "/precompiled/sql_result", 
                          "/precompiled/sql/*", 
                          "/precompiled/sql/mysql", 
                          "/precompiled/sql/mysql_result", 
                       }), 
                       ({ "Gdbm.gdbm", 
                          "Gmp.mpz", 
                          "Regexp", 
                          "sql.sql", 
                          "sql.sql_result", 
                          "sql.*", 
                          "sql.mysql", 
                          "sql.mysql_result", 
                       })); 
      } 
      if (source_timestamp < 855725630) { 
        // Before the builtin was capitalized. 
        file = replace(file, 
                       ({ "/precompiled/condition", 
                          "/precompiled/mutex", 
                          "builtin/", 
                       }), 
                       ({ "builtin.condition", 
                          "builtin.mutex", 
                          "builtin.", 
                       })); 
      } else if (source_timestamp < 857038037) { 
        // Before the Thread module. 
        file = replace(file, 
                       ({ "/precompiled/condition", 
                          "/precompiled/mutex", 
                          "builtin/", 
                       }), 
                       ({ "Builtin.condition", 
                          "Builtin.mutex", 
                          "Builtin.", 
                       })); 
      } else { 
        file = replace(file, 
                       ({ "/precompiled/condition", 
                          "/precompiled/mutex", 
                          "builtin/", 
                       }), 
                       ({ "Thread.Condition", 
                          "Thread.Mutex", 
                          "Builtin.", 
                       })); 
      } 
      if (source_timestamp < 855644314) { 
        // Before the modules were capitalized. 
        file = replace(file, 
                       ({ "/precompiled/fifo", 
                          "/precompiled/queue", 
                       }), 
                       ({ "fifo.Fifo", 
                          "fifo.Queue", 
                       })); 
      } else if (source_timestamp < 857038037) { 
        // Before the Thread module. 
        file = replace(file, 
                       ({ "/precompiled/fifo", 
                          "/precompiled/queue", 
                       }), 
                       ({ "Fifo.Fifo", 
                          "Fifo.Queue", 
                       })); 
      } else { 
        file = replace(file, 
                       ({ "/precompiled/fifo", 
                          "/precompiled/queue", 
                       }), 
                       ({ "Thread.Fifo", 
                          "Thread.Queue", 
                       })); 
      } 
    } 
    file = replace(file, "Myslq", "Mysql"); 
    Tools.AutoDoc.BMMLParser bmml_parser = Tools.AutoDoc.BMMLParser(); 
    return bmml_parser->convert_page(filename, basename(filename), file, 
                                     flags, root); 
  } 
 
  int i; 
  if (has_value(file, "**""!") || 
      ((((i = search(file, "//""! ""module ")) != -1) || 
        ((i = search(file, "//""! ""submodule ")) != -1)) && 
       (sizeof(array_sscanf(file[i..],"%s\n%*s")[0]/" ") == 3))) { 
    // Mirar-style markup. 
    if(imgsrc && imgdest) { 
      mixed err = catch { 
        Tools.AutoDoc.MirarDocParser mirar_parser = 
          Tools.AutoDoc.MirarDocParser(imgsrc, flags); 
 
        if (flags & Tools.AutoDoc.FLAG_COMPAT) { 
          // Special cases for some files that have 
          // known breakage due to missing headers. 
          foreach((["/files/termios.c": 
                    "**""! module Stdio\n" 
                    "**\n" 
                    "**""! class File\n" 
                    "**\n", 
                    "/Image/blit.c": 
                    "**""! module Image\n" 
                    "**\n" 
                    "**""! class image\n" 
                    "**\n", 
                    "Calendar.pmod/Gregorian.pmod": 
                    "//! module Calendar\n" 
                    "//\n", 
                    "Calendar.pmod/Stardate.pmod": 
                    "//! module Calendar\n" 
                    "//\n", 
                    "Calendar_I.pmod/Gregorian.pmod": 
                    "//! module Calendar_I\n" 
                    "//\n", 
                    "Calendar_I.pmod/Stardate.pmod": 
                    "//! module Calendar_I\n" 
                    "//\n", 
                  ]); string suffix; string lines) { 
            if (has_suffix(filename, suffix) && 
                !has_value(file, (lines/"\n")[0])) { 
              foreach(lines/"\n"; int lineno; string line) { 
                mirar_parser->process_line(line, "MAGIC:" + filename, lineno+1); 
              } 
              break; 
            } 
          } 
          // Repair some known typos and other breakage. 
          foreach((["/Image/blit.c": 
                    ({ ({ 
                      "object add_layers(array(int|object)) layer0,", 
                      "object add_layers(int x1,int y1,int x2,int y2," 
                      "array(int|object)) layer0,", 
                    }), ({ 
                      "object add_layers(array(int|object) layer0,", 
                      "object add_layers(int x1,int y1,int x2,int y2," 
                      "array(int|object) layer0,", 
                    }) }), 
                    "/Image/colors.c": 
                    ({ ({ 
                      "**""! constant modifiers=({", 
                    }), ({ 
                      "**""! array(string) modifiers=({", 
                    }) }), 
                    "/Parser/html.c": 
                    ({ ({ 
                      "simplifying work for custom classes that\n" 
                      "**""!\tinherits <ref>Parser.HTML</ref>.", 
                    }), ({ 
                      "simplifying work for custom classes that\n" 
                      "**""!\tinherit <ref>Parser.HTML</ref>.", 
                    }) }), 
                    "/Calendar.pmod/Islamic.pmod": 
                    ({ ({ 
                      "//""! submodule Gregorian\n", 
                    }), ({ 
                      "//""! submodule Islamic\n", 
                    }) }), 
                    "/Calendar_I.pmod/Gregorian.pmod": 
                    ({ ({ 
                      "//""! module Calendar\n", 
                    }), ({ 
                      "//""! module Calendar_I\n", 
                    }) }), 
                    "/Calendar_I.pmod/Stardate.pmod": 
                    ({ ({ 
                      "//""! module Calendar\n", 
                    }), ({ 
                      "//""! module Calendar_I\n", 
                    }) }), 
                    "/Calendar_I.pmod/module.pmod": 
                    ({ ({ 
                      "//""! module Calendar\n", 
                    }), ({ 
                      "//""! module Calendar_I\n", 
                    }) }), 
                  ]); string suffix; array(array(string)) repl) { 
            if (has_suffix(filename, suffix)) { 
              file = replace(file, @repl); 
              break; 
            } 
          } 
          if (has_suffix(filename, "/Image/colors.c") && 
              has_value(file, "sort(grey")) { 
            // Special fix to force stable output 
            // for the list of colors. (Used to be mapping-sort...) 
            if (!has_value(file, "sort(grey->name(), grey);")) { 
              file = replace(file, "sort(grey", 
                             "sort(grey->name(), grey);" 
                             "sort(colored->name(), colored);" 
                             "sort(grey"); 
            } 
          } 
        } 
 
        int lineno = 1; 
        foreach(file/"\n", string line) { 
          mirar_parser->process_line(line, filename, lineno++); 
        } 
        return mirar_parser->make_doc_files(builddir, imgdest, root[0]); 
      }; 
      if (!(flags & Tools.AutoDoc.FLAG_KEEP_GOING)) throw(err); 
      werror("MirarDoc extractor failed: %s\n", describe_backtrace(err)); 
      return "\n"; 
    } 
    else 
      error("Found Mirar style markup. Need imgsrc and imgdir.\n"); 
  } 
 
  string name_sans_suffix, suffix; 
  if (has_suffix(filename, ".in")) { 
    name_sans_suffix = filename[..sizeof(filename)-4]; 
  } 
  else 
    name_sans_suffix = filename; 
  if(!has_value(name_sans_suffix, ".")) 
    error("No suffix in file %O.\n", name_sans_suffix); 
  suffix = ((name_sans_suffix/"/")[-1]/".")[-1]; 
  if( !(< "c", "cc", "cpp", "m", /* "cmod", */ "pike", "pmod", >)[suffix] ) 
    error("Unknown filetype %O.\n", suffix); 
  name_sans_suffix = 
    name_sans_suffix[..sizeof(name_sans_suffix)-(sizeof(suffix)+2)]; 
 
  string result; 
  mixed err = catch { 
    if( suffix == "c" || suffix == "cc" || suffix == "cpp" 
        /* || suffix == "cmod" */) 
      result = Tools.AutoDoc.ProcessXML.extractXML(filename,0,0,0,root,flags); 
    else { 
      string type = ([ "pike":"class", "pmod":"module", ])[suffix]; 
      string name = (name_sans_suffix/"/")[-1]; 
#if 0 
      werror("root: %{%O, %}\n" 
             "type: %O\n" 
             "name: %O\n", root, type, name); 
#endif /* 0 */ 
      // Support filenames with embedded dots. 
      // cf GTK2:refdoc/GTK2.pmod/G.Object.pike 
      root += name/"."; 
      name = root[-1]; 
      root = root[..sizeof(root)-2]; 
      if(name == "module" && type != "class") { 
        if(sizeof(root)<2) 
          error("Unknown module parent name.\n"); 
        name = root[-1]; 
        root = root[..sizeof(root)-2]; 
      } else if ((name == "__default") && (sizeof(root) == 1)) { 
        // Pike backward compatibility module. 
        name = root[0]; 
        if (has_suffix(name, "::")) name = name[..sizeof(name)-3]; 
        root = ({}); 
        type = "namespace"; 
      } 
 
      result = Tools.AutoDoc.ProcessXML.extractXML(filename, 1, type, 
                                                   name, root, flags); 
    } 
  }; 
 
  if (err) { 
    if (!verbosity) 
      ; 
    werror("\nERROR: %s\n", describe_error(err)); 
 
    return 0; 
  } 
 
  if(!result) result=""; 
 
  if(sizeof(result) && imgdest) 
    result = Tools.AutoDoc.ProcessXML.moveImages(result, 
                                                 combine_path(filename, ".."), 
                                                 imgdest, !verbosity); 
  return result+"\n"; 
}