This file is indexed.

/usr/share/doc/mlton/cmcat/cmcat.sml is in mlton-doc 20100608-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
(*
 * Authors: Stephen Weeks (sweeks@sweeks.com)
 *          Matthew Fluet (fluet@cs.cornell.edu) 
 *
 * This requires that you have SML/NJ installed.
 * It works with SML/NJ 110.44 and may require changes to work with other
 * versions, since it depends on the CM structure.
 *
 * cmcat takes a ".cm" file and prints on stdout a list of files in the order
 * deduced by CM.
 *
 * To use from the REPL, do the following:
 * CMcat.cmcat {comments = true,
 *              defines = [],
 *              sources = "sources.cm",
 *              out = TextIO.stdOut}
 *
 * Before using from the shell, you must do the following, where <smlnj> is
 * the root directory of the SML/NJ installation.  You may need to be root in
 * order to do these.
 * 1. From the SML/NJ REPL:
 *      CM.make "sources.cm";
 *      CMcat.export ();
 * 2. ln -s <smlnj>/bin/.run-sml <smlnj>/bin/cmcat
 * 3. mv cmcat.x86-linux <smlnj>/bin/.heap
 *
 * Once it is installed, the usage is as follows:
 *   cmcat [-comments] [-Dsym ...] sources.cm
 *
 * -comments can be used to add comments to the output, including import dependencies.
 * -Dsym can be used to define CM preprocessor symbols.
 *)

structure CMcat :
sig
   val cmcat : {comments: bool,
                defines: String.t list,
                out: Out.t,
                sources: String.t} -> unit
   val export : unit -> unit
end =
struct
   structure PG = PortableGraph
   structure Graph = DirectedGraph
   structure Node = Graph.Node
   structure Edge = Graph.Edge

   fun message s = Out.output (Out.error, s ^ "\n")

   structure CM =
      struct
         open CM

         structure Graph =
            struct
               val graph = fn src =>
                  (Graph.graph src)
                  handle _ => NONE
            end
      end

   structure SrcDescr :> 
      sig
         type t
         val make : String.t * String.t -> t
         val src : t -> String.t
         val descr : t -> String.t
         val equals : t * t -> bool
         val hash : t -> Word.t
         val toString : t -> String.t
      end =
      struct
         datatype t = T of String.t * String.t

         fun make (src,descr) = T (src,descr)
         fun src (T (s,_)) = s
         fun descr (T (_,d)) = d

         fun equals (T (src1,descr1), T (src2,descr2)) =
            String.equals(src1, src2)
         fun hash (T (src, descr)) =
            String.hash src
         fun toString (T (src, descr)) =
            concat [descr, ":", src]
      end

   structure Closure =
      struct
         fun topoSortImportGraph source =
            let
               datatype t = T of {graph: {graph: PG.graph,
                                          imports: SrcDescr.t List.t,
                                          nativesrc: String.t -> String.t} option,
                                  hash: Word.t,
                                  node: t Node.t,
                                  srcdescr: SrcDescr.t}

               val g : t Graph.t = Graph.new ()
               val m : t HashSet.t =
                  HashSet.new {hash = fn T {hash, ...} => hash}
               val {get : t Node.t -> t,
                    set, rem} =
                  Property.getSetOnce
                  (Node.plist, Property.initRaise ("topoSortImportGraph:get", Node.layout))

               val todo = ref [(SrcDescr.make (source,source),fn _ => ())];

               fun closure () =
                  if List.length (!todo) = 0
                     then ()
                     else Exn.withEscape
                          (fn esc =>
                           let
                              val (srcdescr,finish) = List.pop todo
                              val hash = SrcDescr.hash srcdescr

                              val T {node, ...} =
                                 HashSet.lookupOrInsert
                                 (m, hash, fn T {srcdescr = srcdescr', ...} => 
                                  SrcDescr.equals(srcdescr, srcdescr'),
                                  fn () => 
                                  case CM.Graph.graph (SrcDescr.src srcdescr) of
                                     NONE => let
                                                val node = Graph.newNode g
                                                val result =
                                                   T {graph = NONE,
                                                      hash = hash,
                                                      node = node,
                                                      srcdescr = srcdescr}
                                                val _ = set(node, result)
                                             in
                                                result
                                             end
                                   | SOME {graph, imports, nativesrc, ...} => 
                                        let
                                           val node = Graph.newNode g
                                           val imports =
                                              List.map
                                              (imports, fn lib =>
                                               let
                                                  val descr = CM.Library.descr lib
                                                  val descr = List.last (String.split(descr, #":"))
                                                  val src = CM.Library.osstring lib
                                                  val finish = fn import_node =>
                                                     (ignore o Graph.addEdge)
                                                     (g, {from = import_node, to = node})
                                               in
                                                  List.push(todo, (SrcDescr.make (src, descr), finish)) ;
                                                  SrcDescr.make (src, descr)
                                               end)
                                           val result = 
                                              T {graph = SOME {graph = graph,
                                                               imports = imports,
                                                               nativesrc = nativesrc},
                                                 hash = hash,
                                                 node = node,
                                                 srcdescr = srcdescr}
                                        in
                                           set (node, result) ;
                                           result
                                        end)
                              val _ = finish node
                           in
                              closure ()
                           end)
               val _ = closure ()

               val libs =
                  case Graph.topologicalSort g of
                     NONE => raise Fail "topologicalSort of import graph failed"
                   | SOME nodes => 
                        let
                           val libs = 
                              List.map
                              (nodes, fn n =>
                               let
                                  val T {graph, srcdescr, ...} = get n
                               in
                                  {graph = graph,
                                   srcdescr = srcdescr}
                               end)
                        in
                           libs
                        end
            in
               libs
            end

         fun filter (libs : {graph: {graph: PG.graph, 
                                     imports: SrcDescr.t List.t,
                                     nativesrc: String.t -> String.t} option, 
                             srcdescr: SrcDescr.t} List.t) =
            let
               datatype t = T of {hash: Word.t,
                                  lhs: SrcDescr.t * PG.varname,
                                  syms: (SrcDescr.t * PG.namespace * String.t * t Node.t) list}
               val symsNodesDefs : t HashSet.t =
                  HashSet.new {hash = fn T {hash, ...} => hash}

               datatype s = S of {hash: Word.t,
                                  known: Bool.t,
                                  srcdescr: SrcDescr.t,
                                  syms: (SrcDescr.t * PG.namespace * String.t * t Node.t) list ref}
               val exports : s HashSet.t =
                  HashSet.new {hash = fn S {hash, ...} => hash}

               val g : t Graph.t = Graph.new ()
               val {get : t Node.t -> (unit -> unit),
                    set, rem} =
                  Property.getSetOnce
                  (Node.plist, Property.initRaise ("filter:get", Node.layout))

               datatype w = W of {hash: Word.t,
                                  lhs: SrcDescr.t * PG.varname}
               val keep : w HashSet.t =
                  HashSet.new {hash = fn W {hash, ...} => hash}
               val addKeep =
                  fn (srcdescr,vn) =>
                  let
                     val hash = Word.xorb(SrcDescr.hash srcdescr, String.hash vn)
                     val result = W {hash = hash,
                                     lhs = (srcdescr, vn)}
                  in
                     fn () =>
                     (HashSet.insertIfNew
                      (keep, hash, fn W {lhs = (srcdescr',vn'), ...} =>
                       SrcDescr.equals(srcdescr, srcdescr') andalso
                       vn = vn', fn () => result,
                       fn _ => raise Fail "keep") ;
                      ())
                  end

               datatype x = X of {hash: Word.t,
                                  srcdescr: SrcDescr.t,
                                  syms: (PG.namespace * String.t) list ref}
               val imports : x HashSet.t =
                  HashSet.new {hash = fn X {hash, ...} => hash}
               val addImport =
                  fn (srcdescr,ns,s) =>
                  let
                     val hash = SrcDescr.hash srcdescr
                  in
                     fn () =>
                     let
                        val X {syms, ...} =
                           HashSet.lookupOrInsert
                           (imports, hash, fn X {srcdescr = srcdescr', ...} =>
                            SrcDescr.equals(srcdescr, srcdescr'), fn () =>
                            X {hash = hash,
                               srcdescr = srcdescr,
                               syms = ref []})
                     in
                        List.push(syms,(ns,s))
                     end
                  end

               val _ =
                  List.foreach
                  (libs, 
                   fn {graph = NONE, srcdescr, ...} =>
                   let
                      val hash = SrcDescr.hash srcdescr
                      val _ =
                         HashSet.insertIfNew
                         (exports, hash, 
                          fn S {srcdescr = srcdescr', ...} => 
                          SrcDescr.equals(srcdescr, srcdescr'),
                          fn () => S {hash = hash,
                                      known = false,
                                      srcdescr = srcdescr,
                                      syms = ref []},
                          fn _ => raise Fail (concat ["srcdescr: ", 
                                                      SrcDescr.toString srcdescr, 
                                                      " repeated"]))
                   in
                      ()
                   end
                    | {graph = SOME {graph = PG.GRAPH {defs, export, imports}, 
                                     imports = imports', ...},
                       srcdescr, ...} =>
                   let
                      val srcdescr_hash = SrcDescr.hash srcdescr

                      local
                         val imports =
                            List.map2(imports, imports', fn (vn,import) =>
                                      (vn,
                                       let
                                          val hash = SrcDescr.hash import
                                          val S {known, syms as envSyms, ...} =
                                             case HashSet.peek
                                                  (exports, hash, fn S {srcdescr, ...} =>
                                                   SrcDescr.equals(import, srcdescr)) of
                                                NONE => raise Fail (concat ["srcdescr: ", 
                                                                            SrcDescr.toString srcdescr, 
                                                                            " unknown"])
                                              | SOME s => s
                                       in
                                          if known
                                             then 
                                                fn symsSyms =>
                                                List.keepAll
                                                (!envSyms, fn (srcdescr,ns,v,node) =>
                                                 List.contains(symsSyms,(ns,v),(op =)))
                                             else 
                                                fn symsSyms =>
                                                List.map
                                                (symsSyms, fn (ns,s) =>
                                                 case List.peek(!syms, fn (_,ns',s',_) => (ns,s) = (ns',s')) of
                                                    SOME z => z
                                                  | NONE => let
                                                               val node = Graph.newNode g
                                                               val _ = set(node,addImport (import,ns,s))
                                                               val z = (import,ns,s,node)
                                                            in
                                                               List.push(syms,z) ;
                                                               z
                                                            end)
                                       end))
                      in
                         val importFn =
                            String.memoizeList(fn _ => raise Fail "importFn",
                                               imports)
                      end

                      datatype u = U of {hash: Word.t,
                                         lhs: PG.varname,
                                         sym: PG.namespace * String.t}
                      val symDefs : u HashSet.t =
                         HashSet.new {hash = fn U {hash, ...} => hash}
                      datatype v = V of {hash: Word.t,
                                         lhs: PG.varname,
                                         syms: (PG.namespace * String.t) list}
                      val symsDefs : v HashSet.t =
                         HashSet.new {hash = fn V {hash, ...} => hash}

                      val _ =
                         List.foreach
                         (defs, fn PG.DEF {lhs, rhs} =>
                          case rhs of
                             PG.SYM (ns,s) =>
                                let
                                   val hash = String.hash lhs
                                   val result = U {hash = hash,
                                                   lhs = lhs,
                                                   sym = (ns,s)}
                                in
                                   HashSet.insertIfNew
                                   (symDefs, hash, fn U {lhs = lhs', ...} =>
                                    lhs = lhs', fn () => result,
                                    fn _ => raise Fail (concat ["lhs: ", lhs, " violates VARNAME_ONCE"])) ;
                                   ()
                                end
                           | PG.SYMS vns => 
                                let
                                   val hash = String.hash lhs
                                   val syms =
                                      List.foldr
                                      (vns, [], fn (vn,symsAcc) =>
                                       let val hash = String.hash vn
                                       in
                                          case HashSet.peek
                                               (symDefs, hash, fn U {lhs, ...} =>
                                                vn = lhs) of
                                             NONE => raise Fail (concat ["lhs: ", lhs, " violates SYM_TYPE"])
                                           | SOME (U {sym, ...}) => sym::symsAcc
                                       end)
                                   val result =
                                      V {hash = hash,
                                         lhs = lhs,
                                         syms = syms}
                                in
                                   HashSet.insertIfNew
                                   (symsDefs, hash, fn V {lhs = lhs', ...} =>
                                    lhs = lhs', fn () => result,
                                    fn _ => raise Fail (concat ["lhs: ", lhs, " violates VARNAME_ONCE"])) ;
                                   ()
                                end
                           | PG.IMPORT {lib, syms} => 
                                let
                                   val hash = Word.xorb(srcdescr_hash, String.hash lhs)

                                   val symsSyms =
                                      let val hash = String.hash syms
                                      in
                                         case HashSet.peek
                                              (symsDefs, hash, fn V {lhs, ...} =>
                                               syms = lhs) of
                                            NONE => raise Fail (concat ["lhs: ", lhs, " violates SYMS_TYPE"])
                                          | SOME (V {syms, ...}) => syms
                                      end
                                   val syms = importFn lib symsSyms
                                   val result = 
                                      T {hash = hash,
                                         lhs = (srcdescr, lhs),
                                         syms = syms}
                                in
                                   HashSet.insertIfNew
                                   (symsNodesDefs, hash, fn T {lhs = (srcdescr',lhs'), ...} =>
                                    SrcDescr.equals(srcdescr, srcdescr') andalso
                                    lhs = lhs', fn () => result,
                                    fn _ => raise Fail (concat ["lhs: ", lhs, " violates VARNAME_ONCE"])) ;
                                   ()
                                end
                           | PG.COMPILE {src, env, syms} => 
                                let
                                   val hash = Word.xorb(srcdescr_hash, String.hash lhs)
                                   val envSyms =
                                      let val hash = Word.xorb(srcdescr_hash, String.hash env)
                                      in
                                         case HashSet.peek
                                              (symsNodesDefs, hash, fn T {lhs = (srcdescr',env'), ...} =>
                                               SrcDescr.equals(srcdescr, srcdescr') andalso
                                               env = env') of
                                            NONE => raise Fail (concat ["lhs: ", lhs, " violates ENV_TYPE"])
                                          | SOME (T {syms, ...}) => syms
                                      end       
                                   val symsSyms =
                                      let val hash = String.hash syms
                                      in
                                         case HashSet.peek
                                              (symsDefs, hash, fn V {lhs, ...} =>
                                               syms = lhs) of
                                            NONE => raise Fail (concat ["lhs: ", lhs, " violates SYMS_TYPE"])
                                          | SOME (V {syms, ...}) => syms
                                      end
                                   val node = Graph.newNode g
                                   val _ = set(node, addKeep (srcdescr, lhs))
                                   val _ = 
                                      List.foreach
                                      (envSyms, fn (_,_,_,node') =>
                                       ignore(Graph.addEdge(g, {from = node, to = node'})))
                                   val syms =
                                      List.map
                                      (symsSyms, fn (ns,v) =>
                                       (srcdescr,ns,v,node))
                                   val result = 
                                      T {hash = hash,
                                         lhs = (srcdescr, lhs),
                                         syms = syms}
                                in
                                   HashSet.insertIfNew
                                   (symsNodesDefs, hash, fn T {lhs = (srcdescr',lhs'), ...} =>
                                    SrcDescr.equals(srcdescr, srcdescr') andalso
                                    lhs = lhs', fn () => result,
                                    fn _ => raise Fail (concat ["lhs: ", lhs, " violates VARNAME_ONCE"])) ;
                                   ()
                                end
                           | PG.FILTER {env, syms} => 
                                let
                                   val hash = Word.xorb(srcdescr_hash, String.hash lhs)
                                   val envSyms =
                                      let val hash = Word.xorb(srcdescr_hash, String.hash env)
                                      in
                                         case HashSet.peek
                                              (symsNodesDefs, hash, fn T {lhs = (srcdescr',env'), ...} =>
                                               SrcDescr.equals(srcdescr, srcdescr') andalso
                                               env = env') of
                                            NONE => raise Fail (concat ["lhs: ", lhs, " violates ENV_TYPE"])
                                          | SOME (T {syms, ...}) => syms
                                      end
                                   val symsSyms =
                                      let val hash = String.hash syms
                                      in
                                         case HashSet.peek
                                              (symsDefs, hash, fn V {lhs, ...} =>
                                               syms = lhs) of
                                            NONE => raise Fail (concat ["lhs: ", lhs, " violates SYMS_TYPE"])
                                          | SOME (V {syms, ...}) => syms
                                      end
                                   val syms =
                                      List.keepAll
                                      (envSyms, fn (srcdescr,ns,v,node) =>
                                       List.contains(symsSyms,(ns,v),(op =)))
                                   val result = 
                                      T {hash = hash,
                                         lhs = (srcdescr, lhs),
                                         syms = syms}
                                in
                                   HashSet.insertIfNew
                                   (symsNodesDefs, hash, fn T {lhs = (srcdescr',lhs'), ...} =>
                                    SrcDescr.equals(srcdescr, srcdescr') andalso
                                    lhs = lhs', fn () => result,
                                    fn _ => raise Fail (concat ["lhs: ", lhs, " violates VARNAME_ONCE"])) ;
                                   ()
                                end
                           | PG.MERGE vns => 
                                let
                                   val hash = Word.xorb(srcdescr_hash, String.hash lhs)
                                   val syms =
                                      List.foldr
                                      (vns, [], fn (vn,symsAcc) =>
                                       let val hash = Word.xorb(srcdescr_hash, String.hash vn)
                                       in
                                          case HashSet.peek
                                               (symsNodesDefs, hash, fn T {lhs = (srcdescr',vn'), ...} =>
                                                SrcDescr.equals(srcdescr, srcdescr') andalso
                                                vn = vn') of
                                             NONE => raise Fail (concat ["lhs: ", lhs, " violates ENV_TYPE"])
                                           | SOME (T {syms, ...}) => symsAcc @ syms
                                       end)
                                   val result =
                                      T {hash = hash,
                                         lhs = (srcdescr, lhs),
                                         syms = syms}
                                in      
                                   HashSet.insertIfNew
                                   (symsNodesDefs, hash, fn T {lhs = (srcdescr',lhs'), ...} =>
                                    SrcDescr.equals(srcdescr, srcdescr') andalso
                                    lhs = lhs', fn () => result,
                                    fn _ => raise Fail (concat ["lhs: ", lhs, " violates VARNAME_ONCE"])) ;
                                   ()
                                end)

                      val exportSyms =
                         let val hash = Word.xorb(srcdescr_hash, String.hash export)
                         in
                            case HashSet.peek
                                 (symsNodesDefs, hash, fn T {lhs = (srcdescr',export'), ...} =>
                                  SrcDescr.equals(srcdescr, srcdescr') andalso
                                  export = export') of
                               NONE => raise Fail (concat ["lhs: ", export, " violates ENV_TYPE"])
                             | SOME (T {syms, ...}) => syms
                         end
                      val result = S {hash = srcdescr_hash,
                                      known = true,
                                      srcdescr = srcdescr,
                                      syms = ref exportSyms}
                      val _ =
                         HashSet.insertIfNew
                         (exports, srcdescr_hash, fn S {srcdescr = srcdescr', ...} =>
                          SrcDescr.equals(srcdescr, srcdescr'), fn () => result,
                          fn _ => raise Fail (concat ["srcdescr: ", 
                                                      SrcDescr.toString srcdescr, 
                                                      " repeated"]))

                   in
                      ()
                   end)

               val {srcdescr, ...} = List.last libs
               val nodes =
                  case HashSet.peek
                       (exports, SrcDescr.hash srcdescr, fn S {srcdescr = srcdescr', ...} =>
                        SrcDescr.equals(srcdescr, srcdescr')) of
                     NONE => raise Fail "nodes"
                   | SOME (S {syms , ...}) => 
                        List.map(!syms,fn (_,_,_,n) => n)
               val _ =
                  Graph.dfsNodes
                  (g, nodes,
                   Graph.DfsParam.startNode
                   (fn node => (get node) ()))

               val keep = fn (srcdescr, vn) =>
                  Option.isSome
                  (HashSet.peek
                   (keep, Word.xorb(SrcDescr.hash srcdescr, String.hash vn), 
                    fn W {lhs = (srcdescr',vn'), ...} => 
                    SrcDescr.equals(srcdescr, srcdescr') andalso
                    vn = vn'))

               val imports = fn import =>
                  case HashSet.peek
                       (imports, SrcDescr.hash import, fn X {srcdescr, ...} =>
                        SrcDescr.equals(import, srcdescr)) of
                     NONE => []
                   | SOME (X {syms, ...}) => !syms
            in
               (keep, imports)
            end
      end

   fun cmcat {comments, defines, out, sources} =
      let
         (* Define preprocessor symbols *)
         val _ = List.foreach(defines, fn sym => 
                              (#set (CM.symval sym)) (SOME 1))
         val _ = (#set CM.Control.verbose) false
         val _ = (#set CM.Control.warn_obsolete) false
         val _ = Control.printWarnings := false
         val dir = OS.FileSys.getDir ()
         val libs = Closure.topoSortImportGraph sources
         val (keep,imports) = Closure.filter libs
      in
         List.foreach
         (libs,
          fn {graph = NONE, srcdescr, ...} =>
          if comments
             then (Out.output (out, "(* " ^ (SrcDescr.descr srcdescr) ^ "\n");
                               List.foreach
                               (imports srcdescr, fn (ns,s) =>
                               Out.output (out, " * " ^ (case ns of 
                                                            PG.SGN => "signature " 
                                                          | PG.STR => "structure " 
                                                          | PG.FCT => "functor ") ^ 
                                                s ^ "\n"));
                               Out.output (out, " *)\n"))
             else ()
           | {graph = SOME {graph, nativesrc, ...}, srcdescr, ...} =>
          (if comments
              then Out.output (out, 
                               "(* " ^ 
                               (OS.Path.mkRelative {path = SrcDescr.src srcdescr, relativeTo = dir}) ^ 
                               " *)\n")
              else ();
           let val PG.GRAPH {defs, ...} = graph
           in 
              List.foreach
              (defs, fn def =>
               case def of
                  PG.DEF {lhs, rhs = PG.COMPILE {src = (src, native), ...}, ...} =>
                     if keep(srcdescr,lhs)
                        then Out.output(out, (if native then src else nativesrc src) ^ "\n")
                        else ()
                | _ => ())
           end))
      end

   fun die msg =
      (message "Usage: cmcat [-comments] [-Dsym ...] sources.cm"
       ; message ("Error: " ^ msg)
       ; OS.Process.exit OS.Process.failure)

   fun export () =
      SMLofNJ.exportFn
      ("cmcat", fn (_, args) =>
       let
          val comments = ref false
          val defines = ref ["MLton"]
          fun loop args = 
             case args of
                [file] =>
                   cmcat {comments = !comments,
                          defines = !defines,
                          out = Out.standard,
                          sources = file}
              | flag :: args =>
                   if String.equals (flag, "-comments")
                      then
                         (comments := true;
                          loop args)
                   else if String.hasPrefix (flag, {prefix = "-D"})
                      then
                         (defines := String.extract (flag, 2, NONE) :: !defines
                          ; loop args)
                   else die (String.concat ["invalid flag ", flag])
              | _ => die "wrong number of arguments"
       in
          loop args (* handle _ => die "cmcat failed" *)
          ; 0
       end)

end