summaryrefslogtreecommitdiff
path: root/psi/imainarg.c
blob: 8787718e59ee0213d63b6c5f76ce16a276525d60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* Command line parsing and dispatching */

#include "ctype_.h"
#include "memory_.h"
#include "string_.h"
#include <stdlib.h>     /* for qsort */

#include "ghost.h"
#include "gp.h"
#include "gsargs.h"
#include "gscdefs.h"
#include "gsmalloc.h"           /* for gs_malloc_limit */
#include "gsmdebug.h"
#include "gspaint.h"		/* for gs_erasepage */
#include "gxdevice.h"
#include "gxdevmem.h"
#include "gsdevice.h"
#include "gxdevsop.h"		/* for gxdso_* enums */
#include "gxclpage.h"
#include "gdevprn.h"
#include "stream.h"
#include "ierrors.h"
#include "estack.h"
#include "ialloc.h"
#include "strimpl.h"            /* for sfilter.h */
#include "sfilter.h"            /* for iscan.h */
#include "ostack.h"             /* must precede iscan.h */
#include "iscan.h"
#include "iconf.h"
#include "imain.h"
#include "imainarg.h"
#include "iapi.h"
#include "iminst.h"
#include "iname.h"
#include "store.h"
#include "files.h"              /* requires stream.h */
#include "interp.h"
#include "iutil.h"
#include "ivmspace.h"

/* Import operator procedures */
extern int zflush(i_ctx_t *);
extern int zflushpage(i_ctx_t *);

#ifndef GS_LIB
#  define GS_LIB "GS_LIB"
#endif

#ifndef GS_OPTIONS
#  define GS_OPTIONS "GS_OPTIONS"
#endif

/* This is now the default number of entries we initially
 * allocate for the search path array objects. The arrays
 * will now enlarge as required -
 * see imain.c: file_path_add()/extend_path_list_container()
 */

#ifndef GS_MAX_LIB_DIRS
#  define GS_MAX_LIB_DIRS 25
#endif

#define MAX_BUFFERED_SIZE 1024

/* Note: sscanf incorrectly defines its first argument as char * */
/* rather than const char *.  This accounts for the ugly casts below. */

/* Redefine puts to use outprintf, */
/* so it will work even without stdio. */
#undef puts
#define puts(mem, str) outprintf(mem, "%s\n", str)

/* Forward references */
#define runInit 1
#define runFlush 2
#define runBuffer 4
static int swproc(gs_main_instance *, const char *, arg_list *);
static int argproc(gs_main_instance *, const char *);
static int run_buffered(gs_main_instance *, const char *);
static int esc_strlen(const char *);
static void esc_strcat(char *, const char *);
static int runarg(gs_main_instance *, const char *, const char *, const char *, int, int, int *, ref *);
static int run_string(gs_main_instance *, const char *, int, int, int *, ref *);
static int run_finish(gs_main_instance *, int, int, ref *);
static int try_stdout_redirect(gs_main_instance * minst,
                                const char *command, const char *filename);

/* Forward references for help printout */
static void print_help(gs_main_instance *);
static void print_revision(const gs_main_instance *);
static void print_version(const gs_main_instance *);
static void print_usage(const gs_main_instance *);
static void print_devices(const gs_main_instance *);
static void print_emulators(const gs_main_instance *);
static void print_paths(gs_main_instance *);
static void print_help_trailer(const gs_main_instance *);

/* ------ Main program ------ */

/* Process the command line with a given instance. */
static stream *
gs_main_arg_sopen(const char *fname, void *vminst)
{
    gs_main_set_lib_paths((gs_main_instance *) vminst);
    return lib_sopen(&((gs_main_instance *)vminst)->lib_path,
                     ((gs_main_instance *)vminst)->heap, fname);
}
static void
set_debug_flags(const char *arg, char *flags)
{
    byte value = (*arg == '-' ? (++arg, 0) : 0xff);

    while (*arg)
        flags[*arg++ & 127] = value;
}

int
gs_main_init_with_args01(gs_main_instance * minst, int argc, char *argv[])
{
    const char *arg;
    arg_list args;
    int code;
    int have_dumped_args = 0;

    /* Now we actually process them */
    code = arg_init(&args, (const char **)argv, argc,
                    gs_main_arg_sopen, (void *)minst,
                    minst->get_codepoint,
                    minst->heap);
    if (code < 0)
        return code;
    code = gs_main_init0(minst, 0, 0, 0, GS_MAX_LIB_DIRS);
    if (code < 0)
        return code;
/* This first check is not needed on VMS since GS_LIB evaluates to the same
   value as that returned by gs_lib_default_path.  Also, since GS_LIB is
   defined as a searchlist logical and getenv only returns the first entry
   in the searchlist, it really doesn't make sense to search that particular
   directory twice.
*/
#ifndef __VMS
    {
        int len = 0;
        int code = gp_getenv(GS_LIB, (char *)0, &len);

        if (code < 0) {         /* key present, value doesn't fit */
            char *path = (char *)gs_alloc_bytes(minst->heap, len, "GS_LIB");

            gp_getenv(GS_LIB, path, &len);      /* can't fail */
            minst->lib_path.env = path;
        }
    }
#endif /* __VMS */
    minst->lib_path.final = gs_lib_default_path;
    code = gs_main_set_lib_paths(minst);
    if (code < 0)
        return code;
    /* Prescan the command line for --help and --version. */
    {
        int i;
        bool helping = false;

        for (i = 1; i < argc; ++i)
            if (!arg_strcmp(&args, argv[i], "--")) {
                /* A PostScript program will be interpreting all the */
                /* remaining switches, so stop scanning. */
                helping = false;
                break;
            } else if (!arg_strcmp(&args, argv[i], "--help")) {
                print_help(minst);
                helping = true;
            } else if (!arg_strcmp(&args, argv[i], "--debug")) {
                gs_debug_flags_list(minst->heap);
                helping = true;
            } else if (!arg_strcmp(&args, argv[i], "--version")) {
                print_version(minst);
                puts(minst->heap, "");  /* \n */
                helping = true;
            }
        if (helping)
            return gs_error_Info;
    }
    /* Execute files named in the command line, */
    /* processing options along the way. */
    /* Wait until the first file name (or the end */
    /* of the line) to finish initialization. */
    minst->run_start = true;

    {
        int len = 0;
        int code = gp_getenv(GS_OPTIONS, (char *)0, &len);

        if (code < 0) {         /* key present, value doesn't fit */
            char *opts =
            (char *)gs_alloc_bytes(minst->heap, len, "GS_OPTIONS");

            gp_getenv(GS_OPTIONS, opts, &len);  /* can't fail */
            if (arg_push_decoded_memory_string(&args, opts, false, true, minst->heap))
                return gs_error_Fatal;
        }
    }
    while ((code = arg_next(&args, (const char **)&arg, minst->heap)) > 0) {
        code = gs_lib_ctx_stash_sanitized_arg(minst->heap->gs_lib_ctx, arg);
        if (code < 0)
            return code;
        switch (*arg) {
            case '-':
                code = swproc(minst, arg, &args);
                if (code < 0)
                    return code;
                if (code > 0)
                    outprintf(minst->heap, "Unknown switch %s - ignoring\n", arg);
                if (gs_debug[':'] && !have_dumped_args) {
                    int i;

                    dmprintf1(minst->heap, "%% Args passed to instance "PRI_INTPTR": ",
                              (intptr_t)minst);
                    for (i=1; i<argc; i++)
                        dmprintf1(minst->heap, "%s ", argv[i]);
                    dmprintf(minst->heap, "\n");
                    have_dumped_args = 1;
                }
                break;
            default:
                /* default is to treat this as a file name to be run */
                code = argproc(minst, arg);
                if (code < 0)
                    return code;
                if (minst->saved_pages_test_mode) {
                    gx_device *pdev;
                    int ret;
                    gxdso_device_child_request child_dev_data;

                    /* get the real target (printer) device */
                    pdev = gs_currentdevice(minst->i_ctx_p->pgs);
                    do {
                        child_dev_data.target = pdev;
                        ret = dev_proc(pdev, dev_spec_op)(pdev, gxdso_device_child, &child_dev_data,
                                                          sizeof(child_dev_data));
                        if (ret > 0)
                            pdev = child_dev_data.target;
                    } while ((ret > 0) && (child_dev_data.n != 0));
                    if ((code = gx_saved_pages_param_process((gx_device_printer *)pdev,
                               (byte *)"print normal flush", 18)) < 0)
                        return code;
                    if (code > 0)
                        if ((code = gs_erasepage(minst->i_ctx_p->pgs)) < 0)
                            return code;
                }
        }
    }

    return code;
}

int
gs_main_init_with_args2(gs_main_instance * minst)
{
    int code;

    code = gs_main_init2(minst);
    if (code < 0)
        return code;

    if (!minst->run_start)
        return gs_error_Quit;
    return code;
}

int
gs_main_init_with_args(gs_main_instance * minst, int argc, char *argv[])
{
    int code = gs_main_init_with_args01(minst, argc, argv);

    if (code < 0)
        return code;
    return gs_main_init_with_args2(minst);
}

/*
 * Specify a decoding function
 */
void gs_main_inst_arg_decode(gs_main_instance * minst,
                             gs_arg_get_codepoint *get_codepoint)
{
    minst->get_codepoint = get_codepoint;
}

gs_arg_get_codepoint *gs_main_inst_get_arg_decode(gs_main_instance * minst)
{
    return minst->get_codepoint;
}

/*
 * Run the 'start' procedure (after processing the command line).
 */
int
gs_main_run_start(gs_main_instance * minst)
{
    return run_string(minst, "systemdict /start get exec", runFlush, minst->user_errors, NULL, NULL);
}

static int
do_arg_match(const char **arg, const char *match, size_t match_len)
{
    const char *s = *arg;
    if (strncmp(s, match, match_len) != 0)
        return 0;
    s += match_len;
    if (*s == '=')
        *arg = ++s;
    else if (*s != 0)
        return 0;
    else
        *arg = NULL;
    return 1;
}

#define arg_match(A, B) do_arg_match(A, B, sizeof(B)-1)

/* Process switches.  Return 0 if processed, 1 for unknown switch, */
/* <0 if error. */
static int
swproc(gs_main_instance * minst, const char *arg, arg_list * pal)
{
    char sw = arg[1];
    ref vtrue;
    int code = 0;

    make_true(&vtrue);
    arg += 2;                   /* skip - and letter */
    switch (sw) {
        default:
            return 1;
        case 0:         /* read stdin as a file char-by-char */
            /* This is a ******HACK****** for Ghostview. */
            minst->heap->gs_lib_ctx->core->stdin_is_interactive = true;
            goto run_stdin;
        case '_':       /* read stdin with normal buffering */
            minst->heap->gs_lib_ctx->core->stdin_is_interactive = false;
run_stdin:
            minst->run_start = false;   /* don't run 'start' */
            /* Set NOPAUSE so showpage won't try to read from stdin. */
            code = swproc(minst, "-dNOPAUSE", pal);
            if (code)
                return code;
            code = gs_main_init2(minst);        /* Finish initialization */
            if (code < 0)
                return code;

            code = run_string(minst, ".runstdin", runFlush, minst->user_errors, NULL, NULL);
            if (code < 0)
                return code;
            /* If in saved_pages_test_mode, print and flush previous job before the next file */
            if (minst->saved_pages_test_mode) {
                gx_device *pdev;
                int ret;
                gxdso_device_child_request child_dev_data;

                /* get the real target (printer) device */
                pdev = gs_currentdevice(minst->i_ctx_p->pgs);
                do {
                    child_dev_data.target = pdev;
                    ret = dev_proc(pdev, dev_spec_op)(pdev, gxdso_device_child, &child_dev_data,
                                                      sizeof(child_dev_data));
                    if (ret > 0)
                        pdev = child_dev_data.target;
                } while ((ret > 0) && (child_dev_data.n != 0));
                if ((code = gx_saved_pages_param_process((gx_device_printer *)pdev,
                           (byte *)"print normal flush", 18)) < 0)
                    return code;
                if (code > 0)
                    if ((code = gs_erasepage(minst->i_ctx_p->pgs)) < 0)
                        return code;
            }
            break;
        case '-':               /* run with command line args */
            if (strncmp(arg, "debug=", 6) == 0) {
                code = gs_debug_flags_parse(minst->heap, arg+6);
                if (code < 0)
                    return code;
                break;
            } else if (strncmp(arg, "saved-pages=", 12) == 0) {
                gx_device *pdev;

                /* If init2 not yet done, just save the argument for processing then */
                if (minst->init_done < 2) {
                    if (minst->saved_pages_initial_arg == NULL) {
                        /* Tuck the parameters away for later when init2 is done (usually "begin") */
                        minst->saved_pages_initial_arg = (char *)gs_alloc_bytes(minst->heap,
                                                                                1+strlen((char *)arg+12),
                                                                               "GS_OPTIONS");
                        if (minst->saved_pages_initial_arg != NULL) {
                            strcpy(minst->saved_pages_initial_arg,(char *)arg+12);
                        } else {
                            outprintf(minst->heap,
                                      "   saved_pages_initial_arg larger than expected\n");
                            arg_finit(pal);
                            return gs_error_Fatal;
                        }
                    } else {
                        outprintf(minst->heap,
                                  "   Only one --saved-pages=... command allowed before processing input\n");
                        arg_finit(pal);
                        return gs_error_Fatal;
                    }
                } else {
                    int ret;
                    gxdso_device_child_request child_dev_data;

                    /* get the current device */
                    pdev = gs_currentdevice(minst->i_ctx_p->pgs);
                    if (dev_proc(pdev, dev_spec_op)(pdev, gxdso_supports_saved_pages, NULL, 0) <= 0) {
                        outprintf(minst->heap,
                                  "   --saved-pages not supported by the '%s' device.\n",
                                  pdev->dname);
                        arg_finit(pal);
                        return gs_error_Fatal;
                    }
                    /* get the real target (printer) device */
                    do {
                        child_dev_data.target = pdev;
                        ret = dev_proc(pdev, dev_spec_op)(pdev, gxdso_device_child, &child_dev_data,
                                                          sizeof(child_dev_data));
                        if (ret > 0)
                            pdev = child_dev_data.target;
                    } while ((ret > 0) && (child_dev_data.n != 0));
                    if ((code = gx_saved_pages_param_process((gx_device_printer *)pdev,
                                                              (byte *)arg+12, strlen(arg+12))) < 0)
                        return code;
                    if (code > 0)
                        if ((code = gs_erasepage(minst->i_ctx_p->pgs)) < 0)
                            return code;
                }
                break;
            /* The following code is only to allow regression testing of saved-pages */
            } else if (strncmp(arg, "saved-pages-test", 16) == 0) {
                minst->saved_pages_test_mode = true;
                break;
            /* Now handle the explicitly added paths to the file control lists */
            } else if (arg_match(&arg, "permit-file-read")) {
                code = gs_add_explicit_control_path(minst->heap, arg, gs_permit_file_reading);
                if (code < 0) return code;
                break;
            } else if (arg_match(&arg, "permit-file-write")) {
                code = gs_add_explicit_control_path(minst->heap, arg, gs_permit_file_writing);
                if (code < 0) return code;
                break;
            } else if (arg_match(&arg, "permit-file-control")) {
                code = gs_add_explicit_control_path(minst->heap, arg, gs_permit_file_control);
                if (code < 0) return code;
                break;
            } else if (arg_match(&arg, "permit-file-all")) {
                code = gs_add_explicit_control_path(minst->heap, arg, gs_permit_file_reading);
                if (code < 0) return code;
                code = gs_add_explicit_control_path(minst->heap, arg, gs_permit_file_writing);
                if (code < 0) return code;
                code = gs_add_explicit_control_path(minst->heap, arg, gs_permit_file_control);
                if (code < 0) return code;
                break;
            }
            if (*arg != 0) {
                /* Unmatched switch. */
                outprintf(minst->heap,
                          "   Unknown switch '--%s'.\n",
                          arg);
                arg_finit(pal);
                return gs_error_Fatal;
            }
            /* FALLTHROUGH */
        case '+':
            pal->expand_ats = false;
            /* FALLTHROUGH */
        case '@':               /* ditto with @-expansion */
            {
                char *psarg;

                code = arg_next(pal, (const char **)&psarg, minst->heap);
                /* Don't stash the @ file name */

                if (code < 0)
                    return gs_error_Fatal;
                if (psarg == NULL) {
                    outprintf(minst->heap, "Usage: gs ... -%c file.ps arg1 ... argn\n", sw);
                    arg_finit(pal);
                    return gs_error_Fatal;
                }
                psarg = arg_copy(psarg, minst->heap);
                if (psarg == NULL)
                    code = gs_error_Fatal;
                else
                    code = gs_main_init2(minst);
                if (code >= 0)
                    code = run_string(minst, "userdict/ARGUMENTS[", 0, minst->user_errors, NULL, NULL);
                if (code >= 0)
                    while ((code = arg_next(pal, (const char **)&arg, minst->heap)) > 0) {
                        code = gs_lib_ctx_stash_sanitized_arg(minst->heap->gs_lib_ctx, arg);
                        if (code < 0)
                            break;
                        code = runarg(minst, "", arg, "", runInit, minst->user_errors, NULL, NULL);
                        if (code < 0)
                            break;
                    }
                if (code >= 0)
                    code = run_string(minst, "]put", 0, minst->user_errors, NULL, NULL);
                if (code >= 0)
                    code = argproc(minst, psarg);
                arg_free((char *)psarg, minst->heap);
                if (code >= 0)
                    code = gs_error_Quit;

                return code;
            }
        case 'B':               /* set run_string buffer size */
            if (*arg == '-')
                minst->run_buffer_size = 0;
            else {
                uint bsize;

                if (sscanf((const char *)arg, "%u", &bsize) != 1 ||
                    bsize <= 0 || bsize > MAX_BUFFERED_SIZE
                    ) {
                    outprintf(minst->heap,
                              "-B must be followed by - or size between 1 and %u\n",
                              MAX_BUFFERED_SIZE);
                    return gs_error_Fatal;
                }
                minst->run_buffer_size = bsize;
            }
            break;
        case 'c':               /* code follows */
            {
                bool ats = pal->expand_ats;

                code = gs_main_init2(minst);
                if (code < 0)
                    return code;
                pal->expand_ats = false;
                while ((code = arg_next(pal, (const char **)&arg, minst->heap)) > 0) {
                    if (arg[0] == '@' ||
                        (arg[0] == '-' && !isdigit((unsigned char)arg[1]))
                        )
                        break;
                    code = gs_lib_ctx_stash_sanitized_arg(minst->heap->gs_lib_ctx, "?");
                    if (code < 0)
                        return code;
                    code = runarg(minst, "", arg, ".runstring", 0, minst->user_errors, NULL, NULL);
                    if (code < 0)
                        return code;
                }
                if (code < 0)
                    return gs_error_Fatal;
                if (arg != NULL) {
                    char *p = arg_copy(arg, minst->heap);
                    if (p == NULL)
                        return gs_error_Fatal;
                    arg_push_string(pal, p, true);
                }
                pal->expand_ats = ats;
                break;
            }
        case 'f':               /* run file of arbitrary name */
            if (*arg != 0) {
                code = argproc(minst, arg);
                if (code < 0)
                    return code;
                /* If in saved_pages_test_mode, print and flush previous job before the next file */
                if (minst->saved_pages_test_mode) {
                    gx_device *pdev;
                    int ret;
                    gxdso_device_child_request child_dev_data;

                    /* get the real target (printer) device */
                    pdev = gs_currentdevice(minst->i_ctx_p->pgs);
                    do {
                        child_dev_data.target = pdev;
                        ret = dev_proc(pdev, dev_spec_op)(pdev, gxdso_device_child, &child_dev_data,
                                                          sizeof(child_dev_data));
                        if (ret > 0)
                            pdev = child_dev_data.target;
                    } while ((ret > 0) && (child_dev_data.n != 0));
                    if ((code = gx_saved_pages_param_process((gx_device_printer *)pdev,
                               (byte *)"print normal flush", 18)) < 0)
                        return code;
                    if (code > 0)
                        if ((code = gs_erasepage(minst->i_ctx_p->pgs)) < 0)
                            return code;
                }
            }
            break;
        case 'F':               /* run file with buffer_size = 1 */
            if (!*arg) {
                puts(minst->heap, "-F requires a file name");
                return gs_error_Fatal;
            } {
                uint bsize = minst->run_buffer_size;

                minst->run_buffer_size = 1;
                code = argproc(minst, arg);
                minst->run_buffer_size = bsize;
                if (code < 0)
                    return code;
                /* If in saved_pages_test_mode, print and flush previous job before the next file */
                if (minst->saved_pages_test_mode) {
                    gx_device *pdev;
                    int ret;
                    gxdso_device_child_request child_dev_data;

                    /* get the real target (printer) device */
                    pdev = gs_currentdevice(minst->i_ctx_p->pgs);
                    do {
                        child_dev_data.target = pdev;
                        ret = dev_proc(pdev, dev_spec_op)(pdev, gxdso_device_child, &child_dev_data,
                                                          sizeof(child_dev_data));
                        if (ret > 0)
                            pdev = child_dev_data.target;
                    } while ((ret > 0) && (child_dev_data.n != 0));
                    if ((code = gx_saved_pages_param_process((gx_device_printer *)pdev,
                               (byte *)"print normal flush", 18)) < 0)
                        return code;
                    if (code > 0)
                        if ((code = gs_erasepage(minst->i_ctx_p->pgs)) < 0)
                            return code;
                }
            }
            break;
        case 'g':               /* define device geometry */
            {
                long dimensions[2];

                if ((code = gs_main_init1(minst)) < 0)
                    return code;
                if (sscanf((const char *)arg, "%ldx%ld", &dimensions[0], &dimensions[1]) != 2) {
                    puts(minst->heap, "-g must be followed by <width>x<height>");
                    return gs_error_Fatal;
                }
                gs_main_force_dimensions(minst, dimensions);
                break;
            }
        case 'h':               /* print help */
        case '?':               /* ditto */
            print_help(minst);
            return gs_error_Info;      /* show usage info on exit */
        case 'I':               /* specify search path */
            {
                const char *path;

                if (arg[0] == 0) {
                    code = arg_next(pal, (const char **)&path, minst->heap);
                    if (code < 0)
                        return code;
                    code = gs_lib_ctx_stash_sanitized_arg(minst->heap->gs_lib_ctx, "?");
                    if (code < 0)
                        return code;
                } else
                    path = arg;
                if (path == NULL)
                    return gs_error_Fatal;
                gs_main_add_lib_path(minst, path);
            }
            break;
        case 'K':               /* set malloc limit */
            {
                long msize = 0;
                gs_malloc_memory_t *rawheap = gs_malloc_wrapped_contents(minst->heap);

                (void)sscanf((const char *)arg, "%ld", &msize);
                if (msize <= 0 || msize > max_long >> 10) {
                    outprintf(minst->heap, "-K<numK> must have 1 <= numK <= %ld\n",
                              max_long >> 10);
                    return gs_error_Fatal;
                }
                rawheap->limit = msize << 10;
            }
            break;
        case 'M':               /* set memory allocation increment */
            {
                unsigned msize = 0;

                (void)sscanf((const char *)arg, "%u", &msize);
                if (msize <= 0 || msize >= (max_uint >> 10)) {
                    outprintf(minst->heap, "-M must be between 1 and %d\n", (int)((max_uint >> 10) - 1));
                    return gs_error_Fatal;
                }
                minst->memory_clump_size = msize << 10;
            }
            break;
        case 'N':               /* set size of name table */
            {
                unsigned nsize = 0;

                (void)sscanf((const char *)arg, "%d", &nsize);
                if (nsize < 2 || nsize > (max_uint >> 10)) {
                    outprintf(minst->heap, "-N must be between 2 and %d\n", (int)(max_uint >> 10));
                    return gs_error_Fatal;
                }
                minst->name_table_size = (ulong) nsize << 10;
            }
            break;
        case 'o':               /* set output file name and batch mode */
            {
                i_ctx_t *i_ctx_p;
                uint space;
                const char *adef;
                byte *str;
                ref value;
                int len;

                if ((code = gs_main_init1(minst)) < 0)
                    return code;

                i_ctx_p = minst->i_ctx_p;
                space = icurrent_space;
                if (arg[0] == 0) {
                    code = arg_next(pal, (const char **)&adef, minst->heap);
                    if (code < 0)
                        return code;
                    if (code == 0)
                        return gs_error_undefinedfilename;
                    code = gs_lib_ctx_stash_sanitized_arg(minst->heap->gs_lib_ctx, "?");
                    if (code < 0)
                        return code;
                } else
                    adef = arg;
                if ((code = gs_main_init1(minst)) < 0)
                    return code;

                if (strlen(adef) > 0) {
                    code = gs_add_outputfile_control_path(minst->heap, adef);
                    if (code < 0) return code;
                }

                ialloc_set_space(idmemory, avm_system);
                len = strlen(adef);
                str = ialloc_string(len, "-o");
                if (str == NULL)
                    return gs_error_VMerror;
                memcpy(str, adef, len);
                make_const_string(&value, a_readonly | avm_system, len, str);
                ialloc_set_space(idmemory, space);
                i_initial_enter_name(minst->i_ctx_p, "OutputFile", &value);
                i_initial_enter_name(minst->i_ctx_p, "NOPAUSE", &vtrue);
                i_initial_enter_name(minst->i_ctx_p, "BATCH", &vtrue);
            }
            break;
        case 'P':               /* choose whether search '.' first */
            if (!strcmp(arg, ""))
                minst->search_here_first = true;
            else if (!strcmp(arg, "-"))
                minst->search_here_first = false;
            else {
                puts(minst->heap, "Only -P or -P- is allowed.");
                return gs_error_Fatal;
            }
            break;
        case 'q':               /* quiet startup */
            if ((code = gs_main_init1(minst)) < 0)
                return code;
            i_initial_enter_name(minst->i_ctx_p, "QUIET", &vtrue);
            break;
        case 'r':               /* define device resolution */
            {
                float res[2];

                if ((code = gs_main_init1(minst)) < 0)
                    return code;
                switch (sscanf((const char *)arg, "%fx%f", &res[0], &res[1])) {
                    default:
                        puts(minst->heap, "-r must be followed by <res> or <xres>x<yres>");
                        return gs_error_Fatal;
                    case 1:     /* -r<res> */
                        res[1] = res[0];
                        /* fall through */
                    case 2:     /* -r<xres>x<yres> */
                        gs_main_force_resolutions(minst, res);
                }
                break;
            }
        case 'D':               /* define name */
        case 'd':
        case 'S':               /* define name as string */
        case 's':
            {
                char *adef = arg_copy(arg, minst->heap);
                char *eqp;
                bool isd = (sw == 'D' || sw == 'd');
                ref value;

                if (adef == NULL)
                    return gs_error_Fatal;
                eqp = strchr(adef, '=');

                if (eqp == NULL)
                    eqp = strchr(adef, '#');
                /* Initialize the object memory, scanner, and */
                /* name table now if needed. */
                if ((code = gs_main_init1(minst)) < 0) {
                    arg_free((char *)adef, minst->heap);
                    return code;
                }
                if (eqp == adef) {
                    puts(minst->heap, "Usage: -dNAME, -dNAME=TOKEN, -sNAME=STRING");
                    arg_free((char *)adef, minst->heap);
                    return gs_error_Fatal;
                }
                if (eqp == NULL) {
                    if (isd)
                        make_true(&value);
                    else
                        make_empty_string(&value, a_readonly);
                } else {
                    int code;
                    i_ctx_t *i_ctx_p = minst->i_ctx_p;
                    uint space = icurrent_space;

                    *eqp++ = 0;

                    if (strlen(adef) == 10 && strncmp(adef, "OutputFile", 10) == 0 && strlen(eqp) > 0) {
                        code = gs_add_outputfile_control_path(minst->heap, eqp);
                        if (code < 0) {
                            arg_free((char *)adef, minst->heap);
                            return code;
                        }
                    }

                    ialloc_set_space(idmemory, avm_system);
                    if (isd) {
                        int i;
                        int64_t num;

                        /* Check for numbers so we can provide for suffix scalers */
                        /* Note the check for '#' is for PS "radix" numbers such as 16#ff */
                        /* and check for '.' and 'e' or 'E' which are 'real' numbers */
                        if ((strchr(eqp, '#') == NULL) && (strchr(eqp, '.') == NULL) &&
                            (strchr(eqp, 'e') == NULL) && (strchr(eqp, 'E') == NULL) &&
                            ((i = sscanf((const char *)eqp, "%"PRIi64, &num)) == 1)) {
                            char suffix = eqp[strlen(eqp) - 1];

                            switch (suffix) {
                                case 'k':
                                case 'K':
                                    num *= 1024;
                                    break;
                                case 'm':
                                case 'M':
                                    num *= 1024 * 1024;
                                    break;
                                case 'g':
                                case 'G':
                                    /* caveat emptor: more than 2g will overflow */
                                    /* and really should produce a 'real', so don't do this */
                                    num *= 1024 * 1024 * 1024;
                                    break;
                                default:
                                    break;   /* not a valid suffix or last char was digit */
                            }
                            make_int(&value, (ps_int)num);
                        } else {
                            /* use the PS scanner to capture other valid token types */
                            stream astream;
                            scanner_state state;

                            s_init(&astream, NULL);
                            sread_string(&astream,
                                         (const byte *)eqp, strlen(eqp));
                            gs_scanner_init_stream(&state, &astream);
                            code = gs_scan_token(minst->i_ctx_p, &value, &state);
                            if (code) {
                                outprintf(minst->heap, "Invalid value for option -d%s, -dNAME= must be followed by a valid token\n", arg);
                                arg_free((char *)adef, minst->heap);
                                return gs_error_Fatal;
                            }
                            if (r_has_type_attrs(&value, t_name,
                                                 a_executable)) {
                                ref nsref;

                                name_string_ref(minst->heap, &value, &nsref);
#undef string_is
#define string_is(nsref, str, len)\
       (r_size(&(nsref)) == (len) &&\
       !strncmp((const char *)(nsref).value.const_bytes, str, (len)))
                                if (string_is(nsref, "null", 4))
                                    make_null(&value);
                                else if (string_is(nsref, "true", 4))
                                    make_true(&value);
                                else if (string_is(nsref, "false", 5))
                                    make_false(&value);
                                else {
                                    outprintf(minst->heap, "Invalid value for option -d%s, use -sNAME= to define string constants\n", arg);
                                    arg_free((char *)adef, minst->heap);
                                    return gs_error_Fatal;
                                }
                            }
                        }
                    } else {
                        int len = strlen(eqp);
                        byte *body = ialloc_string(len, "-s");

                        if (body == NULL) {
                            lprintf("Out of memory!\n");
                            arg_free((char *)adef, minst->heap);
                            return gs_error_Fatal;
                        }
                        memcpy(body, eqp, len);
                        make_const_string(&value, a_readonly | avm_system, len, body);
                        if ((code = try_stdout_redirect(minst, adef, eqp)) < 0) {
                            arg_free((char *)adef, minst->heap);
                            return code;
                        }
                    }
                    ialloc_set_space(idmemory, space);
                }
                /* Enter the name in systemdict. */
                i_initial_enter_name_copy(minst->i_ctx_p, adef, &value);
                arg_free((char *)adef, minst->heap);
                break;
            }
        case 'p':
            {
                char *adef = arg_copy(arg, minst->heap);
                char *eqp;

                if (adef == NULL)
                    return gs_error_Fatal;
                eqp = strchr(adef, '=');

                if (eqp == NULL)
                    eqp = strchr(adef, '#');
                if (eqp == NULL) {
                    outprintf(minst->heap, "Usage: -pNAME=STRING\n");
                    arg_free((char *)adef, minst->heap);
                    return gs_error_Fatal;
                }
                *eqp++ = 0;
                /* Slightly uncomfortable calling back up to a higher
                 * level, but we'll live with it. */
                code = gsapi_set_param(gs_lib_ctx_get_interp_instance(minst->heap),
                                       adef, eqp, gs_spt_parsed);
                if (code < 0) {
                    arg_free((char *)adef, minst->heap);
                    return code;
                }
                break;
            }
        case 'u':               /* undefine name */
            if (!*arg) {
                puts(minst->heap, "-u requires a name to undefine.");
                return gs_error_Fatal;
            }
            if ((code = gs_main_init1(minst)) < 0)
                return code;
            i_initial_remove_name(minst->i_ctx_p, arg);
            break;
        case 'v':               /* print revision */
            print_revision(minst);
            return gs_error_Info;
/*#ifdef DEBUG */
            /*
             * Here we provide a place for inserting debugging code that can be
             * run in place of the normal interpreter code.
             */
        case 'X':
            code = gs_main_init2(minst);
            if (code < 0)
                return code;
            {
                int xec;        /* exit_code */
                ref xeo;        /* error_object */

#define start_x()\
  gs_main_run_string_begin(minst, 1, &xec, &xeo)
#define run_x(str)\
  gs_main_run_string_continue(minst, str, strlen(str), 1, &xec, &xeo)
#define stop_x()\
  gs_main_run_string_end(minst, 1, &xec, &xeo)
                start_x();
                run_x("\216\003abc");
                run_x("== flush\n");
                stop_x();
            }
            return gs_error_Quit;
/*#endif */
        case 'Z':
            set_debug_flags(arg, gs_debug);
            break;
    }
    return 0;
}

/* Define versions of strlen and strcat that encode strings in hex. */
/* This is so we can enter escaped characters regardless of whether */
/* the Level 1 convention of ignoring \s in strings-within-strings */
/* is being observed (sigh). */
static int
esc_strlen(const char *str)
{
    return strlen(str) * 2 + 2;
}
static void
esc_strcat(char *dest, const char *src)
{
    char *d = dest + strlen(dest);
    const char *p;
    static const char *const hex = "0123456789abcdef";

    *d++ = '<';
    for (p = src; *p; p++) {
        byte c = (byte) * p;

        *d++ = hex[c >> 4];
        *d++ = hex[c & 0xf];
    }
    *d++ = '>';
    *d = 0;
}

/* Process file names */
static int
argproc(gs_main_instance * minst, const char *arg)
{
    int code1, code = gs_main_init1(minst);            /* need i_ctx_p to proceed */

    if (code < 0)
        return code;

    code = gs_add_control_path(minst->heap, gs_permit_file_reading, arg);
    if (code < 0) return code;

    if (minst->run_buffer_size) {
        /* Run file with run_string. */
        code = run_buffered(minst, arg);
    } else {
        /* Run file directly in the normal way. */
        code = runarg(minst, "", arg, ".runfile", runInit | runFlush, minst->user_errors, NULL, NULL);
    }

    code1 = gs_remove_control_path(minst->heap, gs_permit_file_reading, arg);
    if (code >= 0 && code1 < 0) code = code1;

    return code;
}
static int
run_buffered(gs_main_instance * minst, const char *arg)
{
    gp_file *in = gp_fopen(minst->heap, arg, gp_fmode_rb);
    int exit_code;
    ref error_object;
    int code;

    if (in == 0) {
        outprintf(minst->heap, "Unable to open %s for reading", arg);
        return_error(gs_error_invalidfileaccess);
    }
    code = gs_main_init2(minst);
    if (code < 0) {
        gp_fclose(in);
        return code;
    }
    code = gs_main_run_string_begin(minst, minst->user_errors,
                                    &exit_code, &error_object);
    if (!code) {
        char buf[MAX_BUFFERED_SIZE];
        int count;

        code = gs_error_NeedInput;
        while ((count = gp_fread(buf, 1, minst->run_buffer_size, in)) > 0) {
            code = gs_main_run_string_continue(minst, buf, count,
                                               minst->user_errors,
                                               &exit_code, &error_object);
            if (code != gs_error_NeedInput)
                break;
        }
        if (code == gs_error_NeedInput) {
            code = gs_main_run_string_end(minst, minst->user_errors,
                                          &exit_code, &error_object);
        }
    }
    gp_fclose(in);
    zflush(minst->i_ctx_p);
    zflushpage(minst->i_ctx_p);
    return run_finish(minst, code, exit_code, &error_object);
}
static int
runarg(gs_main_instance *minst,
       const char       *pre,
       const char       *arg,
       const char       *post,
       int               options,
       int               user_errors,
       int              *pexit_code,
       ref              *perror_object)
{
    int len = strlen(pre) + esc_strlen(arg) + strlen(post) + 1;
    int code;
    char *line;

    if (options & runInit) {
        code = gs_main_init2(minst);    /* Finish initialization */

        if (code < 0)
            return code;
    }
    line = (char *)gs_alloc_bytes(minst->heap, len, "runarg");
    if (line == 0) {
        lprintf("Out of memory!\n");
        return_error(gs_error_VMerror);
    }
    strcpy(line, pre);
    esc_strcat(line, arg);
    strcat(line, post);
    minst->i_ctx_p->starting_arg_file = true;
    code = run_string(minst, line, options, user_errors, pexit_code, perror_object);
    minst->i_ctx_p->starting_arg_file = false;
    gs_free_object(minst->heap, line, "runarg");
    return code;
}
int
gs_main_run_file2(gs_main_instance *minst,
                  const char       *filename,
                  int               user_errors,
                  int              *pexit_code,
                  ref              *perror_object)
{
    int code, code1;

    code = gs_add_control_path(minst->heap, gs_permit_file_reading, filename);
    if (code < 0) return code;

    code = runarg(minst, "", filename, ".runfile", runFlush, user_errors, pexit_code, perror_object);

    code1 = gs_remove_control_path(minst->heap, gs_permit_file_reading, filename);
    if (code >= 0 && code1 < 0) code = code1;

    return code;
}
static int
run_string(gs_main_instance *minst,
           const char       *str,
           int               options,
           int               user_errors,
           int              *pexit_code,
           ref              *perror_object)
{
    int exit_code;
    ref error_object;
    int code;

    if (pexit_code == NULL)
        pexit_code = &exit_code;
    if (perror_object == NULL)
        perror_object = &error_object;

    code = gs_main_run_string(minst, str, user_errors,
                              pexit_code, perror_object);

    if ((options & runFlush) || code != 0) {
        zflush(minst->i_ctx_p);         /* flush stdout */
        zflushpage(minst->i_ctx_p);     /* force display update */
    }
    return run_finish(minst, code, *pexit_code, perror_object);
}
static int
run_finish(gs_main_instance *minst, int code, int exit_code,
           ref * perror_object)
{
    switch (code) {
        case gs_error_Quit:
        case 0:
            break;
        case gs_error_Fatal:
            if (exit_code == gs_error_InterpreterExit)
                code = exit_code;
            else
                emprintf1(minst->heap,
                          "Unrecoverable error, exit code %d\n",
                          exit_code);
            break;
        default:
            gs_main_dump_stack(minst, code, perror_object);
    }
    return code;
}

/* Redirect stdout to a file:
 *  -sstdout=filename
 *  -sstdout=-
 *  -sstdout=%stdout
 *  -sstdout=%stderr
 * -sOutputFile=- is not affected.
 * File is closed at program exit (if not stdout/err)
 * or when -sstdout is used again.
 */
static int
try_stdout_redirect(gs_main_instance * minst,
    const char *command, const char *filename)
{
    gs_lib_ctx_core_t *core = minst->heap->gs_lib_ctx->core;
    if (strcmp(command, "stdout") == 0) {
        core->stdout_to_stderr = 0;
        core->stdout_is_redirected = 0;
        /* If stdout already being redirected and it is not stdout
         * or stderr, close it
         */
        if (core->fstdout2
            && (gp_get_file(core->fstdout2) != core->fstdout)
            && (gp_get_file(core->fstdout2) != core->fstderr)) {
            gp_fclose(core->fstdout2);
            core->fstdout2 = NULL;
        }
        /* If stdout is being redirected, set minst->fstdout2 */
        if ( (filename != 0) && strlen(filename) &&
            strcmp(filename, "-") && strcmp(filename, "%stdout") ) {
            if (strcmp(filename, "%stderr") == 0) {
                core->stdout_to_stderr = 1;
            } else {
                core->fstdout2 = gp_fopen(minst->heap, filename, "w");
                if (core->fstdout2 == NULL)
                    return_error(gs_error_invalidfileaccess);
            }
            core->stdout_is_redirected = 1;
        }
        return 0;
    }
    return 1;
}

/* ---------------- Print information ---------------- */

/*
 * Help strings.  We have to break them up into parts, because
 * the Watcom compiler has a limit of 510 characters for a single token.
 * For PC displays, we want to limit the strings to 24 lines.
 */
static const char help_usage1[] = "\
Usage: gs [switches] [file1.ps file2.ps ...]\n\
Most frequently used switches: (you can use # in place of =)\n\
 -dNOPAUSE           no pause after page   | -q       `quiet', fewer messages\n\
 -g<width>x<height>  page size in pixels   | -r<res>  pixels/inch resolution\n";
static const char help_usage2[] = "\
 -sDEVICE=<devname>  select device         | -dBATCH  exit after last file\n\
 -sOutputFile=<file> select output file: - for stdout, |command for pipe,\n\
                                         embed %d or %ld for page #\n";
#ifdef DEBUG
static const char help_debug[] = "\
 --debug=<option>[,<option>]*  select debugging options\n\
 --debug                       list debugging options\n";
#endif
static const char help_trailer[] = "\
For more information, see %s.\n\
Please report bugs to bugs.ghostscript.com.\n";
static const char help_devices[] = "Available devices:";
static const char help_default_device[] = "Default output device:";
static const char help_emulators[] = "Input formats:";
static const char help_paths[] = "Search path:";
#ifdef HAVE_FONTCONFIG
static const char help_fontconfig[] = "Ghostscript is also using fontconfig to search for font files\n";
#else
static const char help_fontconfig[] = "";
#endif

extern_gx_io_device_table();

/* Print the standard help message. */
static void
print_help(gs_main_instance * minst)
{
    int i, have_rom_device = 0;

    print_revision(minst);
    print_usage(minst);
    print_emulators(minst);
    print_devices(minst);
    print_paths(minst);
    /* Check if we have the %rom device */
    for (i = 0; i < gx_io_device_table_count; i++) {
        const gx_io_device *iodev = gx_io_device_table[i];
        const char *dname = iodev->dname;

        if (dname && strlen(dname) == 5 && !memcmp("%rom%", dname, 5)) {
            struct stat pstat;
            /* gs_error_unregistered means no usable romfs is available */
            int code = iodev->procs.file_status((gx_io_device *)iodev, dname, &pstat);
            if (code != gs_error_unregistered){
                have_rom_device = 1;
            }
            break;
        }
    }
    if (have_rom_device) {
        outprintf(minst->heap, "Initialization files are compiled into the executable.\n");
    }
    print_help_trailer(minst);
}

/* Print the revision, revision date, and copyright. */
static void
print_revision(const gs_main_instance *minst)
{
    printf_program_ident(minst->heap, gs_product, gs_revision);
    outprintf(minst->heap, " (%d-%02d-%02d)\n%s\n",
            (int)(gs_revisiondate / 10000),
            (int)(gs_revisiondate / 100 % 100),
            (int)(gs_revisiondate % 100),
            gs_copyright);
}

/* Print the version number. */
static void
print_version(const gs_main_instance *minst)
{
    printf_program_ident(minst->heap, NULL, gs_revision);
}

/* Print usage information. */
static void
print_usage(const gs_main_instance *minst)
{
    outprintf(minst->heap, "%s", help_usage1);
    outprintf(minst->heap, "%s", help_usage2);
#ifdef DEBUG
    outprintf(minst->heap, "%s", help_debug);
#endif
}

/* compare function for qsort */
static int
cmpstr(const void *v1, const void *v2)
{
    return strcmp( *(char * const *)v1, *(char * const *)v2 );
}

/* Print the list of available devices. */
static void
print_devices(const gs_main_instance *minst)
{
    outprintf(minst->heap, "%s", help_default_device);
    outprintf(minst->heap, " %s\n", gs_devicename(gs_getdefaultdevice()));
    outprintf(minst->heap, "%s", help_devices);
    {
        int i;
        int pos = 100;
        const gx_device *pdev;
        const char **names;
        size_t ndev = 0;

        for (i = 0; gs_getdevice(i) != 0; i++)
            ;
        ndev = (size_t)i;
        names = (const char **)gs_alloc_bytes(minst->heap, ndev * sizeof(const char*), "print_devices");
        if (names == (const char **)NULL) { /* old-style unsorted device list */
            for (i = 0; (pdev = gs_getdevice(i)) != 0; i++) {
                const char *dname = gs_devicename(pdev);
                int len = strlen(dname);

                if (pos + 1 + len > 76)
                    outprintf(minst->heap, "\n  "), pos = 2;
                outprintf(minst->heap, " %s", dname);
                pos += 1 + len;
            }
        }
        else {                          /* new-style sorted device list */
            for (i = 0; (pdev = gs_getdevice(i)) != 0; i++)
                names[i] = gs_devicename(pdev);
            qsort((void*)names, ndev, sizeof(const char*), cmpstr);
            for (i = 0; i < ndev; i++) {
                int len = strlen(names[i]);

                if (pos + 1 + len > 76)
                    outprintf(minst->heap, "\n  "), pos = 2;
                outprintf(minst->heap, " %s", names[i]);
                pos += 1 + len;
            }
            gs_free(minst->heap, (char *)names, ndev * sizeof(const char*), 1, "print_devices");
        }
    }
    outprintf(minst->heap, "\n");
}

/* Print the list of language emulators. */
static void
print_emulators(const gs_main_instance *minst)
{
    outprintf(minst->heap, "%s", help_emulators);
    {
        const byte *s;

        for (s = gs_emulators; s[0] != 0; s += strlen((const char *)s) + 1)
            outprintf(minst->heap, " %s", s);
    }
    outprintf(minst->heap, "\n");
}

/* Print the search paths. */
static void
print_paths(gs_main_instance * minst)
{
    outprintf(minst->heap, "%s", help_paths);
    gs_main_set_lib_paths(minst);
    {
        uint count = r_size(&minst->lib_path.list);
        uint i;
        int pos = 100;
        char fsepr[3];

        fsepr[0] = ' ', fsepr[1] = gp_file_name_list_separator,
            fsepr[2] = 0;
        for (i = 0; i < count; ++i) {
            const ref *prdir =
            minst->lib_path.list.value.refs + i;
            uint len = r_size(prdir);
            const char *sepr = (i == count - 1 ? "" : fsepr);

            if (1 + pos + strlen(sepr) + len > 76)
                outprintf(minst->heap, "\n  "), pos = 2;
            outprintf(minst->heap, " ");
            /*
             * This is really ugly, but it's necessary because some
             * platforms rely on all console output being funneled through
             * outprintf.  We wish we could just do:
             fwrite(prdir->value.bytes, 1, len, minst->fstdout);
             */
            {
                const char *p = (const char *)prdir->value.bytes;
                uint j;

                for (j = len; j; j--)
                    outprintf(minst->heap, "%c", *p++);
            }
            outprintf(minst->heap, "%s", sepr);
            pos += 1 + len + strlen(sepr);
        }
    }
    outprintf(minst->heap, "\n");
    outprintf(minst->heap, "%s", help_fontconfig);
}

/* Print the help trailer. */
static void
print_help_trailer(const gs_main_instance *minst)
{
    char buffer[gp_file_name_sizeof];
    const char *use_htm = "Use.htm", *p = buffer;
    uint blen = sizeof(buffer);

    if (gp_file_name_combine(gs_doc_directory, strlen(gs_doc_directory),
            use_htm, strlen(use_htm), false, buffer, &blen) != gp_combine_success)
        p = use_htm;
    outprintf(minst->heap, help_trailer, p);
}