-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathconfig.py
More file actions
1299 lines (1188 loc) · 49.9 KB
/
config.py
File metadata and controls
1299 lines (1188 loc) · 49.9 KB
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
"""
PC-BASIC - config.py
Configuration file and command-line options parser
(c) 2013--2023 Rob Hagemans
This file is released under the GNU GPL version 3 or later.
"""
import os
import io
import sys
import logging
import zipfile
import locale
import shutil
import codecs
from collections import deque
from .compat import iteritems, text_type, iterchar
from .compat import configparser
from .compat import WIN32, get_short_pathname, argv, getcwdu
from .compat import USER_CONFIG_HOME, USER_DATA_HOME, PY2
from .compat import split_quoted, split_pair
from .compat import console, IS_CONSOLE_APP, stdio
from .compat import TemporaryDirectory
from .data import CODEPAGES, FONTS, PROGRAMS, ICON
from .basic import VERSION, NAME
from . import data
# base directory name
MAJOR_VERSION = u'.'.join(VERSION.split(u'.')[:2])
BASENAME = u'pcbasic-{0}'.format(MAJOR_VERSION)
# user configuration and state directories
USER_CONFIG_DIR = os.path.join(USER_CONFIG_HOME, BASENAME)
STATE_PATH = os.path.join(USER_DATA_HOME, BASENAME)
# default config file name
CONFIG_NAME = u'PCBASIC.INI'
# user and local config files
USER_CONFIG_PATH = os.path.join(USER_CONFIG_DIR, CONFIG_NAME)
# save-state file name
STATE_NAME = 'pcbasic.session'
# @: target drive for bundled programs
PROGRAM_PATH = os.path.join(STATE_PATH, u'bundled_programs')
# maximum memory size
MAX_MEMORY_SIZE = 65534
# format for log files
LOGGING_FORMAT = u'[%(asctime)s.%(msecs)04d] %(levelname)s: %(message)s'
LOGGING_FORMATTER = logging.Formatter(fmt=LOGGING_FORMAT, datefmt=u'%H:%M:%S')
# drive letters except @, bytes constant
UPPERCASE = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# bool strings
TRUES = (u'YES', u'TRUE', u'ON', u'1')
FALSES = (u'NO', u'FALSE', u'OFF', u'0')
##############################################################################
# version checks
# minimum required python versions
MIN_PYTHON2_VERSION = (2, 7, 12)
MIN_PYTHON3_VERSION = (3, 5, 0)
def _validate_version():
"""Initial validations."""
# sys.version_info tuple's first three elements are guaranteed to be ints
python_version = sys.version_info[:3]
if (
(python_version[0] == 2 and python_version < MIN_PYTHON2_VERSION) or
(python_version[0] == 3 and python_version < MIN_PYTHON3_VERSION)
):
msg = (
'PC-BASIC requires Python version %d.%d.%d, ' % MIN_PYTHON2_VERSION +
'version %d.%d.%d, or higher. ' % MIN_PYTHON3_VERSION +
'You have %d.%d.%d.' % python_version
)
logging.fatal(msg)
raise ImportError(msg)
# raise ImportError if incorrect Python version
_validate_version()
##############################################################################
# Default preset definitions
# For example, --preset=tandy will load all options in the [tandy] section.
# Preset options override default options.
# Presets can also be added by adding a section in brackets in the user configuration file
# e.g., a section headed [myconfig] will be loaded with --preset=myconfig
PRESETS = {
u'strict': {
u'hide-listing': u'65530',
u'text-encoding': u'',
u'soft-linefeed': u'False',
u'hide-protected': u'True',
u'allow-code-poke': u'True',
u'prevent-close': u'True',
u'ctrl-c-break': u'False',
},
u'gwbasic': {
u'syntax': u'gwbasic',
},
u'basica': {
u'reserved-memory': u'789',
},
u'pcjr': {
u'syntax': u'pcjr',
u'term': os.path.join(PROGRAM_PATH, 'PCTERM.BAS'),
u'video': u'pcjr',
u'font': u'vga',
u'codepage': u'437',
u'reserved-memory': u'4035',
u'text-width': u'40',
u'video-memory': u'16384',
},
u'tandy': {
u'syntax': u'tandy',
u'video': u'tandy',
u'font': u'tandy2',
u'codepage': u'437',
u'aspect': u'3072,2000',
u'max-reclen': u'255',
u'reserved-memory': u'3240',
u'video-memory': u'16384',
},
u'cga': {
u'video': u'cga',
u'font': u'cga',
u'codepage': u'437',
u'text-width': u'40',
},
u'ega': {
u'video': u'ega',
u'font': u'vga',
},
u'mda': {
u'video': u'mda',
u'font': u'cga,mda',
u'codepage': u'437',
u'monitor': u'mono',
},
u'hercules': {
u'video': u'hercules',
u'font': u'cga,mda',
u'codepage': u'437',
u'monitor': u'mono',
},
u'olivetti': {
u'video': u'olivetti',
u'font': u'cga,olivetti',
u'codepage': u'437',
},
u'vga': {
u'video': u'vga',
u'font': u'vga',
u'codepage': u'437',
},
}
# by default, load what's in section [pcbasic] and override with anything
DEFAULT_SECTION = [u'pcbasic']
##############################################################################
# short-form arguments
SHORT_ARGS = {
u'd': (u'double', u'True'),
u'h': (u'help', u'True'),
u'q': (u'quit', u'True'),
u'v': (u'version', u'True'),
u'w': (u'wait', u'True'),
u'b': (u'interface', u'cli'),
u't': (u'interface', u'text'),
u'n': (u'interface', u'none'),
u'f': (u'max-files', None),
u's': (u'max-reclen', None),
u'l': (u'load', None),
u'r': (u'run', None),
u'e': (u'exec', None),
u'k': (u'keys', None),
u'i': (u'input', None),
u'o': (u'output', None),
}
# -c means the same as -q -n -e
SHORTHAND = {
u'c': u'qne',
}
##############################################################################
# GWBASIC-style options
# GWBASIC [prog] [<inp] [[>]>outp] [/f:n] [/i] [/s:n] [/c:n] [/m:[n][,m]] [/d]
# /d Allow double-precision ATN, COS, EXP, LOG, SIN, SQR, and TAN.
# /f:n Set maximum number of open files to n. Default is 3.
# Each additional file reduces free memory by 322 bytes.
# /s:n Set the maximum record length for RANDOM files.
# Default is 128, maximum is 32768.
# /c:n Set the COM receive buffer to n bytes.
# If n==0, disable the COM ports.
# /i Statically allocate file control blocks and data buffer.
# NOTE: this appears to be always the case in GW-BASIC, as here.
# /m:n,m Set the highest memory location to n (default 65534) and maximum
# BASIC memory to m*16 bytes (default is all available).
GW_OPTIONS = {
u'<': 'input',
u'>': 'output',
u'>>': 'output:append',
u'/d': 'double',
u'/i': '',
u'/f': 'max-files',
u'/s': 'max-reclen',
u'/c': 'serial-buffer-size',
u'/m': 'max-memory',
}
##############################################################################
# long-form arguments
def _check_text_encoding(arg):
"""Check if text-encoding argument is acceptable."""
try:
codecs.lookup(arg)
except LookupError:
return False
return True
def _check_max_memory(arglist):
"""Check if max-memory argument is acceptable."""
mem_sizes = [arglist[0], arglist[1]*16 if arglist[1] else None]
if min((mem_size for mem_size in mem_sizes if mem_size), default=MAX_MEMORY_SIZE) > MAX_MEMORY_SIZE:
logging.warning(u'max-memory value > %s', MAX_MEMORY_SIZE)
return False
return True
# number of positional arguments
NUM_POSITIONAL = 2
ARGUMENTS = {
u'input': {u'type': u'string', u'default': u'', },
u'output': {u'type': u'string', u'default': u'', },
u'interface': {
u'type': u'string', u'default': u'',
u'choices': (
u'', u'none', u'cli', u'text', u'graphical',
u'ansi', u'curses', u'pygame', u'sdl2'
),
},
u'sound': {
u'type': u'string', u'default': u'true',
u'choices': (u'true', u'false', u'none', u'beep', u'portaudio', u'sdl2', u'interface'),
},
u'load': {u'type': u'string', u'default': u'', },
u'run': {u'type': u'string', u'default': u'', },
u'convert': {u'type': u'string', u'default': u'', },
u'help': {u'type': u'bool', u'default': False, },
u'keys': {u'type': u'string', u'default': u'', },
u'exec': {u'type': u'string', u'default': u'', },
u'quit': {u'type': u'bool', u'default': False,},
u'double': {u'type': u'bool', u'default': False,},
u'max-files': {u'type': u'int', u'default': 3,},
u'max-reclen': {u'type': u'int', u'default': 128,},
u'serial-buffer-size': {u'type': u'int', u'default': 256,},
u'peek': {u'type': u'string', u'list': u'*', u'default': [],},
u'lpt1': {u'type': u'string', u'default': u'PRINTER:',},
u'lpt2': {u'type': u'string', u'default': u'',},
u'lpt3': {u'type': u'string', u'default': u'',},
u'cas1': {u'type': u'string', u'default': u'',},
u'com1': {u'type': u'string', u'default': u'',},
u'com2': {u'type': u'string', u'default': u'',},
u'codepage': {u'type': u'string', u'choices': CODEPAGES, u'default': u'437',},
u'font': {
u'type': u'string', u'list': u'*', u'choices': FONTS,
u'default': [u'default'],
},
u'dimensions': {u'type': u'int', u'list': 2, u'default': [],},
u'fullscreen': {u'type': u'bool', u'default': False,},
u'prevent-close': {u'type': u'bool', u'default': False,},
u'debug': {u'type': u'bool', u'default': False,},
u'hide-listing': {u'type': u'int', u'default': 65535,},
u'hide-protected': {u'type': u'bool', u'default': False,},
u'mount': {u'type': u'string', u'list': u'*', u'default': [],},
u'resume': {u'type': u'bool', u'default': False,},
u'syntax': {
u'type': u'string', u'choices': (u'advanced', u'gwbasic', u'pcjr', u'tandy'),
u'default': u'advanced',
},
u'term': {u'type': u'string', u'default': u'',},
u'video': {
u'type': u'string', u'default': 'vga',
u'choices': (
u'vga', u'ega', u'cga', u'mda',
u'pcjr', u'tandy', u'hercules', u'olivetti'
),
},
u'text-encoding': {u'type': u'string', u'default': u'', u'check': _check_text_encoding},
u'soft-linefeed': {u'type': u'bool', u'default': False,},
u'border': {u'type': u'int', u'default': 5,},
u'mouse-clipboard': {u'type': u'bool', u'default': True,},
u'state': {u'type': u'string', u'default': u'',},
u'monitor': {
u'type': u'string',
u'choices': (u'rgb', u'composite', u'green', u'amber', u'grey', u'mono'),
u'default': u'rgb',
},
u'aspect': {u'type': u'int', u'list': 2, u'default': [4, 3],},
u'scaling': {
u'type': u'string', u'choices':(u'smooth', u'native', u'crisp'),
u'default': u'smooth',
},
u'version': {u'type': u'bool', u'default': False,},
u'config': {u'type': u'string', u'default': u'',},
u'logfile': {u'type': u'string', u'default': u'',},
# negative list length means 'optionally up to'
u'max-memory': {u'type': u'int', u'list': -2, u'default': [MAX_MEMORY_SIZE, 4096], u'listcheck': _check_max_memory},
u'allow-code-poke': {u'type': u'bool', u'default': False,},
u'reserved-memory': {u'type': u'int', u'default': 3429,},
u'caption': {u'type': u'string', u'default': NAME,},
u'text-width': {u'type': u'int', u'choices':(u'40', u'80'), u'default': 80,},
u'video-memory': {u'type': u'int', u'default': 262144,},
u'shell': {u'type': u'string', u'default': u'',},
u'ctrl-c-break': {u'type': u'bool', u'default': True,},
u'wait': {u'type': u'bool', u'default': False,},
u'current-device': {u'type': u'string', u'default': ''},
u'extension': {u'type': u'string', u'list': u'*', u'default': []},
u'options': {u'type': u'string', u'default': ''},
# depecated argument, use text-encoding instead
u'utf8': {u'type': u'bool', u'default': False,},
}
##########################################################################
# logging
class Lumberjack(object):
"""Logging manager."""
def __init__(self):
"""Set up the global logger temporarily until we know the log stream."""
# include messages from warnings madule in the logs
logging.captureWarnings(True)
# we use the awkward logging interface as we can only use basicConfig once
# get the root logger
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
# send to a buffer until we know where to log to
self._logstream = io.StringIO()
handler = logging.StreamHandler(self._logstream)
handler.setFormatter(LOGGING_FORMATTER)
root_logger.addHandler(handler)
def reset(self):
"""Reset root logger."""
# o dear o dear what a horrible API
root_logger = logging.getLogger()
# remove all old handlers: temporary ones we set as well as any default ones
for handler in root_logger.handlers:
root_logger.removeHandler(handler)
return root_logger
def prepare(self, logfile, debug):
"""Set up the global logger."""
# get log stream and level from options
loglevel = logging.DEBUG if debug else logging.INFO
root_logger = self.reset()
root_logger.setLevel(loglevel)
if logfile:
logstream = io.open(logfile, 'w', encoding='utf_8', errors='replace')
else:
logstream = stdio.stderr
# write out cached logs
logstream.write(self._logstream.getvalue())
handler = logging.StreamHandler(logstream)
handler.setFormatter(LOGGING_FORMATTER)
root_logger.addHandler(handler)
##########################################################################
# write default config file
CONFIG_HEADER = (
u"# PC-BASIC configuration file.\n"
u"# Edit this file to change your default settings or add presets.\n"
u"# Changes to this file will not affect any other users of your computer.\n"
u"# All lines starting with # are comments and have no effect.\n"
u"# Thus, to use one of the example options below, "
u"you need to remove the # at the start of the line.\n"
u"\n"
u"[pcbasic]\n"
u"# Use the [pcbasic] section to specify options you want to be enabled by default.\n"
u"# See the documentation or run pcbasic -h for a list of available options.\n"
u"# for example (for version '%s'):\n" % VERSION
)
CONFIG_FOOTER = (
u"\n\n# To add presets, create a section header between brackets and put the \n"
u"# options you need below it, like this:\n"
u"# [your_preset]\n"
u"# border=0\n"
u"# \n"
u"# You will then be able to load these options with --preset=your_preset.\n"
u"# If you choose the same name as a system preset, PC-BASIC will use your\n"
u"# options for that preset and not the system ones. This is not recommended.\n"
)
def _build_default_config_file(file_name):
"""Write a default config file."""
argnames = sorted(ARGUMENTS.keys())
try:
with io.open(file_name, 'w', encoding='utf_8_sig', errors='replace') as f:
f.write(CONFIG_HEADER)
for a in argnames:
try:
f.write(
u'## choices: %s\n' %
u', '.join(u'%s' % (_s,) for _s in ARGUMENTS[a][u'choices'])
)
except(KeyError, TypeError):
pass
try:
# check if it's a list
ARGUMENTS[a][u'list']
formatted = u','.join(u'%s' % (_s,) for _s in ARGUMENTS[a][u'default'])
except(KeyError, TypeError):
formatted = u'%s' % (ARGUMENTS[a][u'default'],)
f.write(u'#%s=%s\n' % (a, formatted))
f.write(CONFIG_FOOTER)
except (OSError, IOError):
# can't create file, ignore. we'll get a message later.
pass
##############################################################################
# settings container
def _store_bundled_programs(PROGRAM_PATH):
"""Retrieve contents of BASIC programs."""
for name in PROGRAMS:
with io.open(os.path.join(PROGRAM_PATH, name), 'wb') as f:
f.write(data.read_program_file(name))
class Settings(object):
"""Read and retrieve command-line settings and options."""
def __init__(self, temp_dir, arguments):
"""Initialise settings."""
# arguments should be unicode
if not arguments:
self._uargv = argv[1:]
else:
self._uargv = list(arguments)
lumberjack = Lumberjack()
try:
# create state path if needed
if not os.path.exists(STATE_PATH):
os.makedirs(STATE_PATH)
# create user config file if needed
if not os.path.exists(USER_CONFIG_PATH):
try:
os.makedirs(USER_CONFIG_DIR)
except OSError:
pass
_build_default_config_file(USER_CONFIG_PATH)
# create @: drive if not present
if not os.path.exists(PROGRAM_PATH):
os.makedirs(PROGRAM_PATH)
# unpack bundled programs
_store_bundled_programs(PROGRAM_PATH)
# store options in options dictionary
self._options = ArgumentParser().retrieve_options(self._uargv, temp_dir)
except:
# avoid losing exception messages occuring while logging was disabled
lumberjack.reset()
raise
# prepare global logger for use by main program
lumberjack.prepare(self.get('logfile'), self.get('debug'))
self._session_params = None
def get(self, name, get_default=True):
"""Get value of option; choose whether to get default or None (unspecified) or '' (empty)."""
try:
value = self._options[name]
if get_default and (value is None or value == u'' or value == []):
raise KeyError
except KeyError:
if get_default:
try:
value = ARGUMENTS[name][u'default']
except KeyError:
if name in range(NUM_POSITIONAL):
return u''
else:
value = None
return value
##########################################################################
# session parameters
@property
def session_params(self):
"""Return a dictionary of parameters for the Session object."""
if self._session_params:
return self._session_params
# don't parse any options on --resume
# except redirects
if self.get('resume'):
params = self._add_implicit_redirects()
return params
# preset PEEK values
peek_values = {}
try:
for a in self.get('peek'):
seg, addr, val = a.split(u':')
peek_values[int(seg)*0x10 + int(addr)] = int(val)
except (TypeError, ValueError):
pass
# devices and mounts
device_params = {
key.upper(): self.get(key)
for key in ('lpt1', 'lpt2', 'lpt3', 'com1', 'com2', 'cas1')
}
current_device, mount_dict = self._get_drives()
device_params.update(mount_dict)
# memory setting
max_list = self.get('max-memory')
max_list[1] = max_list[1]*16 if max_list[1] else max_list[0]
max_list[0] = max_list[0] or max_list[1]
# codepage parameters
codepage_params = self.get('codepage').split(u':')
codepage_dict = data.read_codepage(codepage_params[0])
nobox = len(codepage_params) > 1 and codepage_params[1] == u'nobox'
# video parameters
video_params = self.get('video').split(u':')
# redirects
params = self._get_redirects()
params.update({
'syntax': self.get('syntax'),
'video': video_params[0],
'codepage': codepage_dict,
'box_protect': not nobox,
'monitor': self.get('monitor'),
# screen settings
'text_width': self.get('text-width'),
'video_memory': self.get('video-memory'),
'font': data.read_fonts(codepage_dict, self.get('font')),
# find program for PCjr TERM command
'term': self.get('term'),
'shell': self.get('shell'),
'double': self.get('double'),
# device settings
'devices': device_params,
'current_device': current_device,
'serial_buffer_size': self.get('serial-buffer-size'),
# text file parameters
'textfile_encoding': self.get('text-encoding'),
'soft_linefeed': self.get('soft-linefeed'),
# keyboard settings
'ctrl_c_is_break': self.get('ctrl-c-break'),
# program parameters
'hide_listing': self.get('hide-listing'),
'hide_protected': self.get('hide-protected'),
'allow_code_poke': self.get('allow-code-poke'),
'rebuild_offsets': not self.convert,
# max available memory to BASIC (set by /m)
'max_memory': min(max_list) or 65534,
# maximum record length (-s)
'max_reclen': max(1, min(32767, self.get('max-reclen'))),
# number of file records
'max_files': self.get('max-files'),
# first field buffer address (workspace size; 3429 for gw-basic)
'reserved_memory': self.get('reserved-memory'),
'peek_values': peek_values,
'extension': self.get('extension'),
# ignore key buffer in console-based interfaces, to allow pasting text in console
'check_keybuffer_full': self.get('interface') not in ('cli', 'text', 'ansi', 'curses'),
})
# deprecated arguments
if self.get('utf8', get_default=False) is not None:
if self.get('text-encoding', get_default=False) is not None:
logging.warning(
'Deprecated option `utf8` ignored: `text-encoding` takes precedence.'
)
else:
logging.warning('Option `utf8` is deprecated; use `text-encoding=utf-8` instead.')
params['textfile_encoding'] = u'utf-8' if self.get('utf8') else u''
self._session_params = params
return params
def _get_redirects(self):
"""Determine which i/o streams to attach based on config choices."""
input_streams, output_streams = [], []
# input redirects
infile_params = self.get('input').split(u':')
if infile_params[0].upper() in (u'STDIO', u'STDIN'):
if u'RAW' in (_x.upper() for _x in infile_params):
input_streams.append(stdio.stdin.buffer)
else:
input_streams.append(stdio.stdin)
else:
if len(infile_params) > 1 and infile_params[0].upper() == u'FILE':
infile = infile_params[1]
else:
infile = infile_params[0]
if infile:
try:
input_streams.append(io.open(infile, 'rb'))
except EnvironmentError as e:
logging.warning(u'Could not open input file `%s`: %s', infile, e.strerror)
# output redirects
outfile_params = self.get('output').split(u':')
if outfile_params[0].upper() in (u'STDIO', u'STDOUT'):
if u'RAW' in (_x.upper() for _x in outfile_params):
output_streams.append(stdio.stdout.buffer)
else:
output_streams.append(stdio.stdout)
else:
if len(outfile_params) > 1 and outfile_params[0].upper() == u'FILE':
outfile_params = outfile_params[1:]
outfile = outfile_params[0]
append = len(outfile_params) > 1 and outfile_params[1].lower() == u'append'
if outfile:
try:
output_streams.append(io.open(outfile, 'ab' if append else 'wb'))
except EnvironmentError as e:
logging.warning(u'Could not open output file `%s`: %s', outfile, e.strerror)
return self._add_implicit_redirects(input_streams, output_streams)
def _add_implicit_redirects(self, input_streams=(), output_streams=()):
"""Determine which i/o streams to attach implicitly."""
input_streams = list(input_streams)
output_streams = list(output_streams)
# add stdio if redirected or no interface
if stdio.stdin not in input_streams and stdio.stdin.buffer not in input_streams:
if IS_CONSOLE_APP and not stdio.stdin.isatty():
# redirected on console; use bytes stream
input_streams.append(stdio.stdin.buffer)
elif IS_CONSOLE_APP and not self.interface:
# no interface & on console; use unicode stream
input_streams.append(stdio.stdin)
# redirect output as well if input is redirected, but not the other way around
# this is because (1) GW-BASIC does this from the DOS prompt
# (2) otherwise we don't see anything - we quit after input closes
# isatty is also false if we run as a GUI exe, so check that here
if stdio.stdout not in output_streams and stdio.stdout.buffer not in output_streams:
if IS_CONSOLE_APP and (not stdio.stdout.isatty() or not stdio.stdin.isatty()):
output_streams.append(stdio.stdout.buffer)
elif IS_CONSOLE_APP and not self.interface:
output_streams.append(stdio.stdout)
return {
'output_streams': output_streams,
'input_streams': input_streams,
}
def _get_drives(self):
"""Assign disk locations to disk devices."""
# always get current device
current_device = self.get('current-device').upper()
# build mount dictionary
mount_list = self.get('mount', False)
if mount_list is None:
mount_dict = self._get_default_drives()
if not current_device:
current_device = self._get_default_current_device()
else:
mount_dict = self._get_drives_from_list(mount_list)
# directory for bundled BASIC programs accessible through @:
mount_dict[b'@'] = PROGRAM_PATH
# if Z: not specified, override it to avoid mounting through Session default
if b'Z' not in mount_dict:
mount_dict[b'Z'] = None
return current_device, mount_dict
def _get_drives_from_list(self, mount_list):
"""Assign drive letters based on mount specification."""
mount_dict = {}
for spec in mount_list:
# the last one that's specified will stick
try:
letter, path = spec.split(u':', 1)
try:
letter = letter.encode('ascii').upper()
except UnicodeError:
logging.error(u'Could not mount `%s`: invalid drive letter', spec)
# take abspath first to ensure unicode, realpath gives bytes for u'.'
path = os.path.realpath(os.path.abspath(path))
if not os.path.isdir(path):
logging.error(u'Could not mount `%s`: not a directory', spec)
else:
mount_dict[letter] = path
except (TypeError, ValueError) as e:
logging.error(u'Could not mount `%s`: %s', spec, e)
return mount_dict
def _get_default_drives(self):
"""Assign default drive letters."""
mount_dict = {}
if WIN32:
# get all drives in use by windows
# if started from CMD.EXE, get the 'current working dir' for each drive
# if not in CMD.EXE, there's only one cwd
save_current = getcwdu()
for letter in iterchar(UPPERCASE):
try:
os.chdir(letter + b':')
cwd = get_short_pathname(getcwdu()) or getcwdu()
except EnvironmentError:
# doesn't exist or can't access, do not mount this drive
pass
else:
path, cwd = os.path.splitdrive(cwd)
if path:
# cwd must not start with \\
if cwd[:1] == u'\\':
path += u'\\'
cwd = cwd[1:]
if cwd:
mount_dict[letter] = u':'.join((path, cwd))
else:
mount_dict[letter] = path
else:
logging.warning('Not mounting `%s`: no drive letter.', cwd)
os.chdir(save_current)
else:
# non-Windows systems simply have 'Z:' set to their their cwd by default
mount_dict[b'Z'] = getcwdu()
return mount_dict
def _get_default_current_device(self):
"""Get the current drive letter or Z:"""
if WIN32:
letter, _ = os.path.splitdrive(os.path.abspath(getcwdu()))
try:
current_device = letter.encode('ascii')
except UnicodeError:
pass
else:
current_device = b'Z'
return current_device
##########################################################################
# interface parameters
@property
def interface(self):
"""Run with interface."""
return self.get('interface') != 'none'
@property
def iface_params(self):
"""Dict of interface parameters."""
interface = self.get('interface')
# categorical interfaces
categories = {
'text': ('ansi', 'curses'),
'graphical': ('sdl2', 'pygame'),
}
if not interface:
# default: try graphical first, then text, then cli
iface_list = categories['graphical'] + categories['text'] + ('cli',)
else:
try:
iface_list = categories[interface]
except KeyError:
iface_list = (interface,)
iface_params = {
'try_interfaces': iface_list,
'audio_override': self.get('sound') not in ('true', 'interface') and self.get('sound'),
}
iface_params.update(self._get_video_parameters())
iface_params.update(self._get_audio_parameters())
return iface_params
def _get_video_parameters(self):
"""Return a dictionary of parameters for the video plugin."""
return {
'dimensions': self.get('dimensions'),
'aspect_ratio': self.get('aspect'),
'border_width': self.get('border'),
'scaling': self.get('scaling'),
'fullscreen': self.get('fullscreen'),
'prevent_close': self.get('prevent-close'),
'caption': self.get('caption'),
'mouse_clipboard': self.get('mouse-clipboard'),
'icon': ICON,
'wait': self.get('wait'),
}
def _get_audio_parameters(self):
"""Return a dictionary of parameters for the audio plugin."""
return {}
##########################################################################
# launch parameters
@property
def launch_params(self):
"""Dict of launch parameters."""
# build list of commands to execute on session startup
commands = []
greeting = False
if not self.get('resume'):
run = (
# positional argument and --load or -l not specified (empty is specified)
(self.get(0) and self.get('load', get_default=False) is None)
# or run specified explicitly
or self.get('run')
)
# treat colons as CRs
commands = split_quoted(self.get('exec'), split_by=u':', quote=u'"', strip_quotes=False)
# note that executing commands (or RUN) will suppress greeting
# following GW, don't write greeting for redirected input or command-line filter run
greeting = not run and not commands and not self.session_params['input_streams']
if run:
commands.append('RUN')
if self.get('quit'):
commands.append('SYSTEM')
launch_params = {
'prog': self.get('run') or self.get('load') or self.get(0),
'resume': self.get('resume'),
'greeting': greeting,
'state_file': self._get_state_file(),
'commands': commands,
# inserted keystrokes
# we first need to encode the unicode to bytes before we can decode it
# this preserves unicode as \x (if latin-1) and \u escapes
'keys': self.get('keys').encode('ascii', 'backslashreplace').decode('unicode-escape'),
'debug': self.get('debug'),
}
launch_params.update(self.session_params)
return launch_params
def _get_state_file(self):
"""Name of state file"""
state_name = self.get('state') or STATE_NAME
if not os.path.exists(state_name):
state_name = os.path.join(STATE_PATH, state_name)
return state_name
##########################################################################
# other calls
@property
def guard_params(self):
"""Dict of exception guard parameters."""
return {
'uargv': self._uargv,
'log_dir': STATE_PATH,
}
@property
def conv_params(self):
"""Get parameters for file conversion."""
# conversion output
# argument is mode
mode = self.get('convert', get_default=False)
# keep uppercase first letter
mode = mode[0].upper() if mode else 'A'
name_in = (self.get(0) or self.get('run') or self.get('load'))
name_out = self.get(1)
return mode, name_in, name_out
@property
def version(self):
"""Version operating mode."""
return self.get('version')
@property
def help(self):
"""Help operating mode."""
return self.get('help')
@property
def convert(self):
"""Converter operating mode."""
return self.get('convert', get_default=False) is not None
@property
def debug(self):
"""Debugging mode."""
return self.get('debug')
##############################################################################
# argument parsing
class ArgumentParser(object):
"""Parse PC-BASIC config file and command-line arguments."""
def retrieve_options(self, uargv, temp_dir):
"""Retrieve command line and option file options."""
# convert command line arguments to string dictionary form
remaining = self._get_arguments_dict(uargv)
# unpack any packages
package = self._parse_package_arg_and_unpack(remaining, temp_dir)
# get preset groups from specified config file
preset_dict = self._parse_config_arg_and_process_config_file(remaining)
# parse default presets nested in config presets
preset_dict = {
_key: self._merge_arguments(
self._parse_presets(_dict, PRESETS),
_dict
)
for _key, _dict in iteritems(preset_dict)
}
# set defaults based on presets
args = self._parse_presets(remaining, preset_dict)
# local config file settings override preset settings
self._merge_arguments(args, preset_dict[u'pcbasic'])
# find unrecognised arguments
unrecognised = ((_k, _v) for _k, _v in iteritems(args) if _k not in ARGUMENTS)
for key, value in unrecognised:
logging.warning(
'Ignored unrecognised option `%s=%s` in configuration file', key, value
)
args = {_k: _v for _k, _v in iteritems(args) if _k in ARGUMENTS}
# parse rest of command line args
cmd_line_args = self._parse_args(remaining)
# command-line args override config file settings
self._merge_arguments(args, cmd_line_args)
# parse GW-BASIC style options
self._parse_gw_options(args)
# clean up arguments
self._convert_types(args)
if package:
# do not resume from a package
args['resume'] = False
return args
def _append_short_args(self, args, key, value):
"""Append short arguments and value to dict."""
# apply shorthands
for short_arg, replacement in iteritems(SHORTHAND):
key = key.replace(short_arg, replacement)
long_arg_value = None
for i, short_arg in enumerate(key[1:]):
try:
long_arg, long_arg_value = SHORT_ARGS[short_arg]
except KeyError:
logging.warning(u'Ignored unrecognised option `-%s`', short_arg)
else:
if i == len(key)-2:
# assign provided value to last argument specified
if long_arg_value and value:
logging.debug(
u'Value `%s` provided to option `-%s` interpreted as positional',
value, short_arg
)
self._append_arg(args, long_arg, long_arg_value or value or u'')
else:
self._append_arg(args, long_arg, long_arg_value or u'')
# if value provided not used, push back as positional
if long_arg_value and value:
return value
return None
def _append_arg(self, args, key, value):
"""Update a single list-type argument by appending a value."""
if not value:
# if we call _append_arg it means the key may be empty but is at least specified
value = u''
if key in args and args[key]:
if value:
args[key] += u',' + value
else:
args[key] = value
def _get_arguments_dict(self, argv):
"""Convert command-line arguments to dictionary."""
args = {}
arg_deque = deque(argv)
# positional arguments
pos = 0
# use -- to end option parsing, everything is a positional argument afterwards
options_ended = False
while arg_deque:
arg = arg_deque.popleft()
if not arg.startswith(u'-') or options_ended:
# not an option flag, interpret as positional
# strip enclosing quotes, but only if paired
for quote in u'"\'':
if arg.startswith(quote) and arg.endswith(quote):
arg = arg.strip(quote)