File: option.c

package info (click to toggle)
mit-scheme 10.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 84,732 kB
  • sloc: lisp: 476,370; xml: 133,758; ansic: 70,366; sh: 8,696; makefile: 2,239; asm: 2,109
file content (1022 lines) | stat: -rw-r--r-- 26,910 bytes parent folder | download | duplicates (2)
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
/* -*-C-*-

Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
    2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
    2017, 2018, 2019 Massachusetts Institute of Technology

This file is part of MIT/GNU Scheme.

MIT/GNU Scheme is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

MIT/GNU Scheme is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with MIT/GNU Scheme; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
USA.

*/

/* Command-line option processing */

#include <ctype.h>
#include "scheme.h"
#include "fasl.h"
#include "osenv.h"
#include "osfs.h"
#include "uxtop.h"
#include <sys/stat.h>

#define xfree(p) OS_free ((void *) (p))

#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif

#ifdef __WIN32__
#  include <io.h>
#  include "nt.h"
#  include "ntio.h"
#endif

#if defined(__WIN32__)
#  define DOS_LIKE_FILENAMES
#endif

#ifndef SUB_DIRECTORY_DELIMITER
#  ifdef DOS_LIKE_FILENAMES
#    define SUB_DIRECTORY_DELIMITER '\\'
#  else
#    define SUB_DIRECTORY_DELIMITER '/'
#  endif
#endif

#ifndef PATH_DELIMITER
#  ifdef DOS_LIKE_FILENAMES
#    define PATH_DELIMITER ';'
#  else
#    define PATH_DELIMITER ':'
#  endif
#endif

#ifndef QUOTE_CHAR
#  ifdef DOS_LIKE_FILENAMES
#    define QUOTE_CHAR (-1)
#  else
#    define QUOTE_CHAR '\\'
#  endif
#endif

#ifdef DOS_LIKE_FILENAMES
#  define FILE_ABSOLUTE(filename)			\
     ((((filename) [0]) == SUB_DIRECTORY_DELIMITER)	\
      || (((filename) [1]) == ':'))
#else
#  define FILE_ABSOLUTE(filename) (((filename) [0]) == SUB_DIRECTORY_DELIMITER)
#endif

#define FILE_READABLE(filename) (OS_file_access ((filename), 4))

static bool option_summary;

static const char * option_raw_library;
static const char * option_raw_band;
static const char * option_raw_heap;
static const char * option_raw_constant;
static const char * option_raw_stack;
static const char * option_raw_prepend;

/* Command-line arguments */
int option_saved_argc;
const char ** option_saved_argv;
int option_unused_argc;
const char ** option_unused_argv;

/* Boolean options */
bool option_emacs_subprocess;
bool option_force_interactive;
bool option_disable_core_dump;
bool option_batch_mode;
bool option_show_version;
bool option_show_help;
#ifdef __APPLE__
  bool option_macosx_application;
#endif

/* String options */
const char ** option_library_path = 0;
const char * option_band_file = 0;
const char * option_fasl_file = 0;

/* Numeric options */
unsigned long option_heap_size;
unsigned long option_constant_size;
unsigned long option_stack_size;

#ifndef LIBRARY_PATH_VARIABLE
#  define LIBRARY_PATH_VARIABLE "MITSCHEME_LIBRARY_PATH"
#endif

#ifndef DEFAULT_LIBRARY_PATH
#  ifdef DOS_LIKE_FILENAMES
#    define DEFAULT_LIBRARY_PATH "c:\\local\\mit-scheme"
#  else
#    define DEFAULT_LIBRARY_PATH "/usr/local/lib/mit-scheme"
#  endif
#endif

#ifndef BAND_VARIABLE
#  define BAND_VARIABLE "MITSCHEME_BAND"
#endif

#ifndef DEFAULT_STD_BAND
#  define DEFAULT_STD_BAND "all.com"
#endif

void
print_help (void)
{
  outf_fatal ("Usage: mit-scheme --OPTION ARG ... --OPTION ARG ...\n\
\n\
This machine accepts the following command-line options.  The options\n\
may appear in any order, but they must all appear before any options\n\
for the band.\n\
\n\
--library PATH\n\
  Sets the library search path to PATH.  This is a colon-separated\n\
  list of directories that is searched to find various library files,\n\
  such as bands.  If this option is not given, the value of the\n\
  environment variable MITSCHEME_LIBRARY_PATH is used; if that isn't\n\
  defined, \"%s\" is used.\n\
\n\
--band FILENAME\n\
  Specifies the initial band to be loaded.  Searches for FILENAME in\n\
  the working directory and the library directories, returning the\n\
  full pathname of the first readable file of that name.  If this\n\
  option isn't given, the filename is the value of the environment\n\
  variable MITSCHEME_BAND, or if that isn't defined, \"%s\"; in\n\
  these cases the library directories are searched, but not the\n\
  working directory.\n\
\n\
--fasl FILENAME\n\
  Specifies that a cold load should be performed, using FILENAME as\n\
  the initial file to be loaded.  If this option isn't given, a normal\n\
  load is performed instead.  This option may not be used together\n\
  with the \"--band\" option.\n\
\n\
--heap BLOCKS\n\
  Specifies the size of the heap in 1024-word blocks.  Overrides any\n\
  default.\n\
\n\
--constant BLOCKS\n\
  Specifies the size of constant space in 1024-word blocks.  Overrides\n\
  any default.\n\
\n\
--stack BLOCKS\n\
  Specifies the size of the stack in 1024-word blocks.  Overrides any\n\
  default.\n\
\n\
--option-summary\n\
  Causes Scheme to write option values to standard error.\n\
\n\
--help\n\
  Causes Scheme to report the available command line options.\n\
\n\
--version\n\
  Causes Scheme to report versions and copyrights, then exit.\n\
\n\
--batch-mode, --quiet, --silent\n\
  Suppresses the startup report of versions and copyrights, and the\n\
  valediction.\n\
\n\
--emacs\n\
  Specifies that Scheme is running as a subprocess of GNU Emacs.\n\
  This option is automatically supplied by GNU Emacs, and should not\n\
  be given under other circumstances.\n\
\n\
--interactive\n\
  If this option isn't specified, and Scheme's standard I/O is not a\n\
  terminal, Scheme will detach itself from its controlling terminal.\n\
  This will prevent it from getting signals sent to the process group\n\
  of that terminal.  If this option is specified, Scheme will not\n\
  detach itself from the controlling terminal.\n\
\n\
--nocore\n\
  Specifies that Scheme should not generate a core dump under any\n\
  circumstances.\n\
"
#ifdef __APPLE__
"\n\
--macosx-application\n\
  Specifies that Scheme is running as a Mac OS X application.\n\
  This option is automatically supplied when the application is\n\
  launched from an icon, and should not be given under other\n\
  circumstances.\n\
"
#endif /* __APPLE__ */
"\n\
--prepend-library DIRNAME\n\
  Adds DIRNAME to the front of the library search path.  This option\n\
  takes one value and can be specified once.\n\
\n\
Please report bugs to %s.\n\
\n\
Additional options may be supported by the band (and described below).\n\
\n", DEFAULT_LIBRARY_PATH, DEFAULT_STD_BAND, PACKAGE_BUGREPORT);
}

#ifndef DEFAULT_HEAP_SIZE
#  if SIZEOF_UNSIGNED_LONG == 8
#    define DEFAULT_HEAP_SIZE 16384
#  else
#    define DEFAULT_HEAP_SIZE 3072
#  endif
#endif

#ifndef HEAP_SIZE_VARIABLE
#  define HEAP_SIZE_VARIABLE "MITSCHEME_HEAP_SIZE"
#endif

#ifndef DEFAULT_CONSTANT_SIZE
#  define DEFAULT_CONSTANT_SIZE 1024
#endif

#ifndef MINIMUM_FASL_CONSTANT
#  define MINIMUM_FASL_CONSTANT (DEFAULT_CONSTANT_SIZE * 2)
#endif

#ifndef DEFAULT_STACK_SIZE
#  define DEFAULT_STACK_SIZE 1024
#endif

#ifndef STACK_SIZE_VARIABLE
#  define STACK_SIZE_VARIABLE "MITSCHEME_STACK_SIZE"
#endif

static int
string_compare_ci (const char * string1, const char * string2)
{
  const char * scan1 = string1;
  unsigned int length1 = (strlen (string1));
  const char * scan2 = string2;
  unsigned int length2 = (strlen (string2));
  unsigned int length = ((length1 < length2) ? length1 : length2);
  const char * end1 = (scan1 + length);
  const char * end2 = (scan2 + length);
  while ((scan1 < end1) && (scan2 < end2))
    {
      int c1 = (*scan1++);
      int c2 = (*scan2++);
      if (islower (c1))
	{
	  if (! (islower (c2)))
	    c1 = (toupper (c1));
	}
      else
	{
	  if (islower (c2))
	    c2 = (toupper (c2));
	}
      if (c1 != c2)
	return ((c1 < c2) ? (-1) : 1);
    }
  return
    ((length1 == length2)
     ? 0
     : ((length1 < length2) ? (-1) : 1));
}

static char *
string_copy (const char * s)
{
  char * result = (OS_malloc ((strlen (s)) + 1));
  strcpy (result, s);
  return (result);
}

static char *
string_copy_limited (const char * s, const char * e)
{
  unsigned int n_chars = (e - s);
  char * result = (OS_malloc (n_chars + 1));
  strncpy (result, s, n_chars);
  result[n_chars] = '\0';
  return (result);
}

static bool
must_quote_char_p (int c)
{
  return ((c == QUOTE_CHAR) || (c == PATH_DELIMITER));
}

static unsigned int
strlen_after_quoting (const char * s)
{
  const char * scan = s;
  unsigned int n_chars = 0;
  while (true)
    {
      int c = (*scan++);
      if (c == '\0')
	return n_chars;
      if (must_quote_char_p (c))
	n_chars += 1;
      n_chars += 1;
    }
}

static char *
quote_string (const char * s)
{
  const char * scan_in = s;
  char * result = (OS_malloc ((strlen_after_quoting (s)) + 1));
  char * scan_out = result;
  while (true)
    {
      int c = (*scan_in++);
      if (c == '\0')
	break;
      if (must_quote_char_p (c))
	(*scan_out++) = QUOTE_CHAR;
      (*scan_out++) = c;
    }
  (*scan_out) = '\0';
  return result;
}

static unsigned int
strlen_after_unquoting (const char * s)
{
  const char * scan = s;
  unsigned int n_chars = 0;
  while (true)
    {
      int c = (*scan++);
      if (c == QUOTE_CHAR)
	c = (*scan++);
      if (c == '\0')
	return n_chars;
      n_chars += 1;
    }
}

static char *
unquote_string (const char * s)
{
  const char * scan_in = s;
  char * result = (OS_malloc ((strlen_after_unquoting (s)) + 1));
  char * scan_out = result;
  while (true)
    {
      int c = (*scan_in++);
      if (c == QUOTE_CHAR)
	c = (*scan_in++);
      (*scan_out++) = c;
      if (c == '\0')
	return result;
    }
}

struct option_descriptor
{
  const char * option;
  bool argument_p;
  void * value_cell;
};

static void
option_argument (const char * option, bool argument_p, void * value_cell)
{
  struct option_descriptor descriptor;
  (descriptor . option) = option;
  (descriptor . argument_p) = argument_p;
  (descriptor . value_cell) = value_cell;
  obstack_grow ((&scratch_obstack), (&descriptor), (sizeof (descriptor)));
}

static void
parse_options (int argc, const char ** argv)
{
  const char ** scan_argv = (argv + 1);
  const char ** end_argv = (scan_argv + (argc - 1));
  unsigned int n_descriptors
    = ((obstack_object_size (&scratch_obstack))
       / (sizeof (struct option_descriptor)));
  struct option_descriptor * descriptors = (obstack_finish (&scratch_obstack));
  struct option_descriptor * end_desc = (descriptors + n_descriptors);
  struct option_descriptor * scan_desc;
  for (scan_desc = descriptors; (scan_desc < end_desc); scan_desc += 1)
    if (scan_desc->argument_p)
      {
	const char ** value_cell = (scan_desc->value_cell);
	if (value_cell != 0)
	  (*value_cell) = 0;
      }
    else
      {
	bool * value_cell = (scan_desc->value_cell);
	if (value_cell != 0)
	  (*value_cell) = false;
      }
  while (scan_argv < end_argv)
    {
      const char * option = (*scan_argv++);
      if ((strncmp ("--", option, 2)) == 0)
	option += 2;
      else if ((strncmp ("-", option, 1)) == 0)
	option += 1;
      else
	{
	  scan_argv -= 1;
	  break;
	}
      for (scan_desc = descriptors; (scan_desc < end_desc); scan_desc += 1)
	if ((string_compare_ci (option, (scan_desc->option))) == 0)
	  {
	    if (scan_desc->argument_p)
	      {
		const char ** value_cell = (scan_desc->value_cell);
		if (scan_argv < end_argv)
		  {
		    if (value_cell != 0)
		      (*value_cell) = (*scan_argv++);
		  }
		else
		  {
		    outf_fatal ("%s: option --%s requires an argument.\n",
				scheme_program_name, option);
		    termination_init_error ();
		  }
	      }
	    else
	      {
		bool * value_cell = (scan_desc->value_cell);
		if (value_cell != 0)
		  (*value_cell) = true;
	      }
	    break;
	  }
      if (scan_desc == end_desc)
	{
	  scan_argv -= 1;
	  break;
	}
    }
  obstack_free ((&scratch_obstack), descriptors);
  /* Pass --version and --help through to the band, sort of. */
  if (strncmp ("--version", scan_argv[-1], 9) == 0)
    scan_argv--;
  if (strncmp ("--help", scan_argv[-1], 6) == 0)
    scan_argv--;

  option_saved_argc = argc;
  option_saved_argv = argv;
  option_unused_argc = (end_argv - scan_argv);
  option_unused_argv = scan_argv;
}

static void
parse_standard_options (int argc, const char ** argv)
{
  option_argument ("band", true, (&option_raw_band));
  option_argument ("batch-mode", false, (&option_batch_mode));
  option_argument ("constant", true, (&option_raw_constant));
  option_argument ("emacs", false, (&option_emacs_subprocess));
  option_argument ("fasl", true, (&option_fasl_file));
  option_argument ("heap", true, (&option_raw_heap));
  option_argument ("help", false, (&option_show_help));
  option_argument ("interactive", false, (&option_force_interactive));
  option_argument ("library", true, (&option_raw_library));
#ifdef __APPLE__
  option_argument ("macosx-application", false, (&option_macosx_application));
#endif
  option_argument ("nocore", false, (&option_disable_core_dump));
  option_argument ("option-summary", false, (&option_summary));
  option_argument ("prepend-library", true, (&option_raw_prepend));
  option_argument ("quiet", false, (&option_batch_mode));
  option_argument ("silent", false, (&option_batch_mode));
  option_argument ("stack", true, (&option_raw_stack));
  option_argument ("version", false, (&option_show_version));

  /* These are deprecated: */
  option_argument ("compiler", false, 0);
  option_argument ("edwin", false, 0);
  option_argument ("large", false, 0);
  option_argument ("utab", true, 0);
  option_argument ("utabmd", true, 0);

  parse_options (argc, argv);
}

static const char *
standard_string_option (const char * option,
			const char * variable,
			const char * defval)
{
  if (option != 0)
    return (option);
  {
    const char * t = (getenv (variable));
    return ((t != 0) ? t : defval);
  }
}

static unsigned long
standard_numeric_option (const char * option,
			 const char * optval,
			 const char * variable,
			 unsigned long defval)
{
  if (optval != 0)
    {
      char * end;
      unsigned long n = (strtoul (optval, (&end), 0));;
      if ((end == optval) || ((*end) != '\0'))
	{
	  outf_fatal ("%s: illegal argument for option --%s: %s\n",
		      scheme_program_name, option, optval);
	  termination_init_error ();
	}
      return (n);
    }
  if (variable != 0)
    {
      const char * t = (getenv (variable));
      if (t != 0)
	{
	  char * end;
	  unsigned long n = (strtoul (t, (&end), 0));;
	  if ((end == t) || ((*end) != '\0'))
	    {
	      outf_fatal
		("%s: illegal value for environment variable %s: %s\n",
		 scheme_program_name, variable, t);
	      termination_init_error ();
	    }
	  return (n);
	}
    }
  return (defval);
}

static const char *
get_wd (void)
{
  const char * wd = (OS_working_dir_pathname ());
  unsigned int len = (strlen (wd));
  if ((wd [len - 1]) == SUB_DIRECTORY_DELIMITER)
    len -= 1;
  {
    char * result = (OS_malloc (len + 1));
    char * scan_result = result;
    const char * scan_wd = wd;
    const char * end_wd = (scan_wd + len);
    while (scan_wd < end_wd)
      (*scan_result++) = (*scan_wd++);
    (*scan_result) = '\0';
    return (result);
  }
}

static const char *
find_path_delimiter (const char * s)
{
  const char * scan = s;
  while (true)
    {
      int c = (*scan++);
      if (c == PATH_DELIMITER)
	return (scan - 1);
      if (c == QUOTE_CHAR)
	c = (*scan++);
      if (c == '\0')
	return 0;
    }
}

static const char **
parse_path_string (const char * path)
{
  const char * start = path;
  /* It is important that this get_wd be called here to make sure that
     the unix getcwd is called now, before it allocates heap space.
     This is because getcwd forks off a new process and we want to do
     that before the scheme process gets too big.  */
  const char * wd = (get_wd ());
  unsigned int lwd = (strlen (wd));
  while (true)
    {
      const char * delim = (find_path_delimiter (start));
      char * unquoted;
      char * end;
      char * element;
      if (delim != 0)
	{
	  const char * copy = (string_copy_limited (start, delim));
	  unquoted = (unquote_string (copy));
	  xfree (copy);
	}
      else
	unquoted = (unquote_string (start));

      end = (unquoted + (strlen (unquoted)));
      if ((end > unquoted) && ((* (end - 1)) == SUB_DIRECTORY_DELIMITER))
	(*--end) = '\0';
      if (FILE_ABSOLUTE (unquoted))
	element = unquoted;
      else
	{
	  if ((*unquoted) == '\0')
	    element = (string_copy (wd));
	  else
	    {
	      element = (OS_malloc (lwd + (strlen (unquoted)) + 2));
	      strcpy (element, wd);
	      end = (element + lwd);
	      (*end++) = SUB_DIRECTORY_DELIMITER;
	      strcpy (end, unquoted);
	    }
	  xfree (unquoted);
	}
      obstack_ptr_grow ((&scratch_obstack), element);
      if (delim == 0)
	break;
      start = (delim + 1);
    }
  obstack_ptr_grow ((&scratch_obstack), 0);
  if (wd != 0)
    xfree (wd);
  {
    unsigned int n_bytes = (obstack_object_size (&scratch_obstack));
    const char ** elements = (obstack_finish (&scratch_obstack));
    const char ** scan = elements;
    const char ** end = (scan + (n_bytes / (sizeof (char *))));
    const char ** result = (OS_malloc (n_bytes));
    const char ** scan_result = result;
    while (scan < end)
      (*scan_result++) = (*scan++);
    obstack_free ((&scratch_obstack), elements);
    return (result);
  }
}

static void
free_parsed_path (const char ** path)
{
  const char ** scan = path;
  while (1)
    {
      const char * element = (*scan++);
      if (element == 0)
	break;
      xfree (element);
    }
  xfree (path);
}

static char *
add_to_library_path (const char * new_dir, const char * library_path)
{
  const char * quoted_dir = (quote_string (new_dir));
  unsigned int quoted_dir_len = (strlen (quoted_dir));
  char * result = (OS_malloc (quoted_dir_len + (strlen (library_path)) + 2));
  char * end = (result + quoted_dir_len);
  strcpy (result, quoted_dir);
  (*end++) = PATH_DELIMITER;
  strcpy (end, library_path);
  xfree (quoted_dir);
  return (result);
}

const char *
search_for_library_file (const char * filename)
{
  unsigned int flen = (strlen (filename));
  const char ** scan_path = option_library_path;
  while (1)
    {
      const char * directory = (*scan_path++);
      unsigned int dlen;
      const char * fullname;
      if (directory == 0)
	return (0);
      dlen = (strlen (directory));
      if (dlen > 0)
	{
	  obstack_grow ((&scratch_obstack), directory, dlen);
	  obstack_1grow ((&scratch_obstack), SUB_DIRECTORY_DELIMITER);
	}
      obstack_grow ((&scratch_obstack), filename, flen);
      obstack_1grow ((&scratch_obstack), '\0');
      fullname = (obstack_finish (&scratch_obstack));
      if (FILE_READABLE (fullname))
	{
	  const char * result = (string_copy (fullname));
	  obstack_free ((&scratch_obstack), ((char *) fullname));
	  return (result);
	}
      obstack_free ((&scratch_obstack), ((char *) fullname));
    }
}

const char *
search_path_for_file (const char * option,
		      const char * filename,
		      bool default_p,
		      bool fail_p)
{
  const char * result = (search_for_library_file (filename));
  if (result != 0)
    return (result);
  if (!fail_p)
    return (filename);
  else
    {
      const char ** scan_path = option_library_path;
      outf_fatal ("%s: can't find a readable %s",
		  scheme_program_name,
		  (default_p ? "default" : "file"));
      if (option != 0)
	outf_fatal (" for option --%s", option);
      outf_fatal (".\n");
      outf_fatal ("\tsearched for file %s in these directories:\n", filename);
      if (!default_p)
	outf_fatal ("\t.\n");
      while (1)
	{
	  const char * element = (*scan_path++);
	  if (element == 0)
	    break;
	  outf_fatal ("\t%s\n", element);
	}
      termination_init_error ();
      /*NOTREACHED*/
      return (0);
    }
}

static const char *
standard_filename_option (const char * option,
			  const char * optval,
			  const char * variable,
			  const char * defval,
			  bool fail_p)
{
  if (optval != 0)
    {
      if (FILE_READABLE (optval))
	return (string_copy (optval));
      if (FILE_ABSOLUTE (optval))
	{
	  if (fail_p)
	    {
	      outf_fatal ("%s: can't read file %s for option --%s.\n",
			  scheme_program_name, optval, option);
	      termination_init_error ();
	    }
	  return (string_copy (optval));
	}
      return (search_path_for_file (option, optval, false, fail_p));
    }
  {
    const char * filename = (getenv (variable));
    if (filename == 0)
      filename = defval;
    if (FILE_ABSOLUTE (filename))
      {
	if ((! (FILE_READABLE (filename))) && fail_p)
	  {
	    outf_fatal ("%s: can't read default file %s for option --%s.\n",
			scheme_program_name, filename, option);
	    termination_init_error ();
	  }
	return (string_copy (filename));
      }
    else
      return (search_path_for_file (option, filename, true, fail_p));
  }
}

static void
conflicting_options (const char * option1, const char * option2)
{
  outf_fatal ("%s: can't specify both options --%s and --%s.\n",
	      scheme_program_name, option1, option2);
  termination_init_error ();
}

#define SCHEME_WORDS_TO_BLOCKS(n) (((n) + 1023) / 1024)

static int
read_band_sizes (const char * filename,
		 unsigned long * constant_size,
		 unsigned long * heap_size)
{
  fasl_file_handle_t handle;
  fasl_header_t h;
  bool ok;

  if (!open_fasl_input_file (filename, (&handle)))
    return (0);
  ok = (read_fasl_header ((&h), handle));
  if (! ((close_fasl_input_file (handle)) && ok))
    return (0);
  if ((check_fasl_version (&h)) != FASL_FILE_FINE)
    return (0);
  if ((check_fasl_cc_version ((&h),
			      COMPILER_INTERFACE_VERSION,
			      COMPILER_PROCESSOR_TYPE))
      != FASL_FILE_FINE)
    return (0);
  (*constant_size) = (SCHEME_WORDS_TO_BLOCKS (FASLHDR_CONSTANT_SIZE (&h)));
  (*heap_size) = (SCHEME_WORDS_TO_BLOCKS (FASLHDR_HEAP_SIZE (&h)));
  return (1);
}

static void
describe_boolean_option (const char * name, int value)
{
  outf_fatal ("  %s: %s\n", name, (value ? "yes" : "no"));
}

static void
describe_string_option (const char * name, const char * value)
{
  outf_fatal ("  %s: %s\n", name, value);
}

static void
describe_size_option (const char * name, unsigned int value)
{
  outf_fatal ("  %s size: %d\n", name, value);
}

static void
describe_path_option (const char * name, const char ** value)
{
  outf_fatal ("  %s: ", name);
  {
    const char ** scan = value;
    outf_fatal ("%s", (*scan++));
    while (1)
      {
	const char * element = (*scan++);
	if (element == 0) break;
	outf_fatal (":%s", element);
      }
  }
  outf_fatal ("\n");
}

static void
describe_options (void)
{
  outf_fatal ("Summary of configuration options:\n");
  describe_size_option ("heap", option_heap_size);
  describe_size_option ("constant-space", option_constant_size);
  describe_size_option ("stack", option_stack_size);
  describe_path_option ("library path", option_library_path);
  if (option_fasl_file != 0)
    describe_string_option ("FASL file", option_fasl_file);
  else
    describe_string_option ("band", option_band_file);
  describe_boolean_option ("emacs subprocess", option_emacs_subprocess);
  describe_boolean_option ("force interactive", option_force_interactive);
  describe_boolean_option ("disable core dump", option_disable_core_dump);
  describe_boolean_option ("suppress noise", option_batch_mode);
  if (option_unused_argc == 0)
    outf_fatal ("  no unused arguments\n");
  else
    {
      const char ** scan = option_unused_argv;
      const char ** end = (scan + option_unused_argc);
      outf_fatal ("  unused arguments:");
      while (scan < end)
	outf_fatal (" %s", (*scan++));
      outf_fatal ("\n");
    }
}

void
read_command_line_options (int argc, const char ** argv)
{
  bool band_sizes_valid = false;
  unsigned long band_constant_size = 0;
  unsigned long band_heap_size = 0;
  const char * library_path;
  const char * default_library_path = DEFAULT_LIBRARY_PATH;

  parse_standard_options (argc, argv);

  if (option_library_path != 0)
    free_parsed_path (option_library_path);
#ifdef __APPLE__
  const char * main_bundle_path = (macosx_main_bundle_dir ());
  if (main_bundle_path != 0)
    {
      default_library_path =
	(add_to_library_path (main_bundle_path, default_library_path));
      xfree (main_bundle_path);
    }
#endif
  library_path = standard_string_option (option_raw_library,
					 LIBRARY_PATH_VARIABLE,
					 default_library_path);
  if (option_raw_prepend != NULL)
    {
      const char * new_path;
      new_path = add_to_library_path (option_raw_prepend, library_path);
      library_path = new_path;
    }
  option_library_path = parse_path_string (library_path);

  if (option_band_file != 0)
    {
      xfree (option_band_file);
      option_band_file = 0;
    }
  if (option_fasl_file != 0)
    {
      if (option_raw_band != 0)
	conflicting_options ("fasl", "band");
#ifndef CC_IS_C
      if (!FILE_READABLE (option_fasl_file))
	{
	  /* Kludge; FILE_READABLE doesn't work right for this case.  */
	  outf_fatal ("%s: can't read option file: --fasl %s\n",
		   scheme_program_name, option_fasl_file);
	  termination_init_error ();
	}
#endif
    }
  else
    {
      const char * default_band = DEFAULT_STD_BAND;
      const char * bands [] =
	{
	  DEFAULT_STD_BAND,
	  "runtime.com",
	  "mechanics.com",
	  "edwin-mechanics.com",
	  0
	};
      unsigned int i = 0;
      for ( ; ((bands[i]) != 0); i += 1)
	if (search_for_library_file (bands[i]))
	  {
	    default_band = (bands[i]);
	    break;
	  }
      option_band_file
	= (standard_filename_option ("band",
				     option_raw_band,
				     BAND_VARIABLE,
				     default_band,
				     true));
    }
  if (option_band_file != 0)
    band_sizes_valid
      = (read_band_sizes (option_band_file,
			  (&band_constant_size),
			  (&band_heap_size)));

  option_heap_size
    = (standard_numeric_option ("heap",
				option_raw_heap,
				HEAP_SIZE_VARIABLE,
				DEFAULT_HEAP_SIZE));
  if (band_sizes_valid)
    option_heap_size += band_heap_size;
  else if ((option_fasl_file != 0)
	   && (option_heap_size < MINIMUM_FASL_CONSTANT))
    option_heap_size = MINIMUM_FASL_CONSTANT;
  option_constant_size
    = (standard_numeric_option ("constant",
				option_raw_constant,
				0,
				(band_sizes_valid
				 ? band_constant_size
				 : DEFAULT_CONSTANT_SIZE)));
  option_stack_size
    = (standard_numeric_option ("stack",
				option_raw_stack,
				STACK_SIZE_VARIABLE,
				DEFAULT_STACK_SIZE));
  if (option_show_version)
    {
      outf_console ("%s\n", PACKAGE_STRING);
      outf_flush_console ();
    }
  if (option_show_help)
    print_help ();
  if (option_summary)
    describe_options ();
}