-
Notifications
You must be signed in to change notification settings - Fork 2
/
sedparse.py
1629 lines (1307 loc) · 49.6 KB
/
sedparse.py
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
# sedparse
# GNU sed's parser translated from C to Python
# https://github.com/aureliojargas/sedparse
#
# HOW IT WORKS
#
# The original sed script can be specified via strings (option -e) or via files
# (option -f). The main functions to handle them are compile_string() and
# compile_file(), respectively. Both are entrypoints to prepare the input for
# the real parsing which occurs in compile_program().
#
# The parser reads the sed script character by character (see inchar()). The
# opposite function is savchar(), which goes back one character, when necessary.
# There are also some specialized input reading functions, for example
# in_integer() to read a sequence of integers and in_nonblank() to skip a
# sequence of white space chars.
#
# The normal flow is reading the chars forward with inchar() to detect the
# addresses, commands and their arguments. When the end of the current structure
# is only detected after reading the next char, which is not part of that
# structure (for example, getting any non number while reading integers in
# in_integer()), then savchar() is called to give that char back so the main
# parser will handle it.
#
# The data for every successfully parsed command is saved as an instance of the
# struct_sed_cmd class, which is appended in-place to the cur_program list,
# specified as an argument when calling compile_string() or compile_file().
#
# Any detected syntax error aborts the parsing immediately and a ParseError
# exception is raised (see bad_prog()).
#
# The code uses some global variables to save state:
#
# prog
#
# A class to hold information about the input script. If it is a file,
# `prog.file` will hold the open file descriptor (which during the parsing
# is read using fd.seek() and fd.tell()). If it the input came from a string
# (-e option), `prog.base`, `prog.cur` and `prog.end` will be used to save
# state about where the cursor is currently pointing in the string. See more
# details in the `prog_info` class comments.
#
# cur_input
#
# A class to keep information used in error messages. We have the script
# file name and line number saved in `cur_input.name` and `cur_input.line`,
# and if it's not a file, `cur_input.string_expr_count` holds the index of
# the command line "-e" expression.
#
# blocks
#
# An integer describing the current depth of the script. Any new block "{"
# adds a new depth level and its respective "}" remove a level. A script
# like "$ { /foo/ { p;};}" will have blocks=2 when reading the "p" command,
# but will become blocks=0 when reading the last "}".
#
# pending_text
# old_text_buf
#
# Those two variables are temporary data holders so the parser can
# "remember" data from the previous parsed script. This allows for scripts
# like "sed -e 'i\' -e foo", where a single "i" command is specified in two
# chunks (by using the -e option twice).
# STILL NOT TRANSLATED
# - Check if command only accepts one address
# if (cur_cmd->a2) bad_prog (_(ONE_ADDR))
# - Check POSIX compatibility (all GNU sed extensions are supported here)
# if (posixicity == POSIXLY_BASIC)
# Since sedparse is a literal translation, maintaining the same code, variables
# and method names, I have to disable the following checks.
# pylint: disable=global-statement
# pylint: disable=invalid-name
# pylint: disable=no-else-break
# pylint: disable=no-else-return
# pylint: disable=too-few-public-methods
# pylint: disable=too-many-branches
# pylint: disable=too-many-lines
# pylint: disable=too-many-statements
from __future__ import print_function # pylint: disable=unused-variable
import argparse
import json
import os
import sys
import tempfile
__version__ = "0.1.2"
# Adapt some C entities to Python
NULL = None
EOF = "<EOF>"
# Default options when dumping JSON
# The separators argument is required in python2 https://bugs.python.org/issue16333
JSON_OPTS = {"indent": 4, "sort_keys": True, "separators": (",", ": ")}
# Base class to handle translated C structs
class struct:
def to_dict(self, remove_empty=True):
"""
Return the struct data as a dict.
Using plain self.__dict__ works only for a single level. This function
is recursive and converts all the nested structs.
When using remove_empty, keys that have empty or default values are removed.
Note that the structs use no dictionaries to store their data, so we
don't need to check for it in this code.
"""
d = {}
for key, val in self.__dict__.items():
if isinstance(val, struct):
val = val.to_dict(remove_empty)
if remove_empty:
if val is None or val in ("", (), [], {}):
continue
if key == "int_arg" and val == -1:
continue
if key == "addr_bang" and not val:
continue
if key == "addr_step" and val == 0:
continue
if (
key == "addr_number"
and val == 0
and self.__dict__["addr_type"] in (ADDR_IS_REGEX, ADDR_IS_LAST)
):
continue
d[key] = val
return d
def to_json(self, remove_empty=True):
return json.dumps(self, default=lambda x: x.to_dict(remove_empty), **JSON_OPTS)
class ParseError(Exception):
# pylint: disable=too-many-arguments
def __init__(self, message="", file="", line=0, column=0, expression=0, exitcode=1):
# https://stackoverflow.com/a/38857736
super(ParseError, self).__init__(message)
self.message = message
self.file = file
self.line = line
self.column = column
self.expression = expression
self.exitcode = exitcode
######################################## translated from sed.c
program_name = "sedparse"
######################################## translated from basicdefs.h
def ISBLANK(c):
return c in (" ", "\t")
def ISDIGIT(ch):
return ch in "0123456789"
def ISSPACE(c):
# https://github.com/gcc-mirror/gcc/blob/master/include/safe-ctype.h
return c in (" ", "\t", "\n", "\v", "\f", "\r")
######################################## translated from sed.h
# The translation of the following was not necessary:
# - struct vector
# - enum replacement_types
# - enum text_types
# - enum posixicity_types
# - enum addr_state
# sedparse: The original code handles file open/read/write/close operations,
# but here we only care about the filename.
class struct_output(struct):
def __init__(self):
self.name = ""
# sedparse: not used
# missing_newline = False
# fp = None
# link = None
def __repr__(self):
return "%s(name=%r)" % (self.__class__.__name__, self.name)
def __str__(self):
return self.name
class struct_text_buf(struct):
def __init__(self):
self.text = []
# text_length = 0 # sedparse: not used
def __repr__(self):
return "%s(text=%r)" % (self.__class__.__name__, self.text)
def __str__(self):
return "".join(self.text)[:-1] # remove trailing \n
class struct_regex(struct):
def __init__(self):
self.pattern = ""
# In the original this was an integer, a bitwise OR for address flags.
# In sedparse it is a string with all the found flags, in their
# original order, and even repetition is preserved. It is also used
# to save both regex address flags and "s" command flags.
self.flags = "" # sedparse: was 0 in the original
# This is used to save the slash char used as delimiter in: regex
# addresses (/foo/p) and in "s" and "y" commands (s///).
self.slash = "" # sedparse extension
# sedparse: not used
# sz = 0
# dfa = None # struct_dfa()
# begline = False
# endline = False
# re = ""
def __repr__(self):
return "%s(slash=%r, pattern=%r, flags=%r)" % (
self.__class__.__name__,
self.slash,
self.pattern,
self.flags,
)
def __str__(self):
return self.escape() + self.slash + self.pattern + self.slash + self.flags
def escape(self): # sedparse extension
return "\\" if self.slash != "/" else ""
# enum addr_types
# fmt: off
ADDR_IS_NULL = 1 # null address
ADDR_IS_REGEX = 2 # a.addr_regex is valid
ADDR_IS_NUM = 3 # a.addr_number is valid
ADDR_IS_NUM_MOD = 4 # a.addr_number is valid, addr_step is modulo
ADDR_IS_STEP = 5 # address is +N (only valid for addr2)
ADDR_IS_STEP_MOD = 6 # address is ~N (only valid for addr2)
ADDR_IS_LAST = 7 # address is $
# fmt: on
class struct_addr(struct):
def __init__(self):
self.addr_type = ADDR_IS_NULL # enum addr_types
self.addr_number = 0
self.addr_step = 0
self.addr_regex = struct_regex()
def __repr__(self):
return "%s(addr_type=%r, addr_number=%r, addr_step=%r, addr_regex=%r)" % (
self.__class__.__name__,
self.addr_type,
self.addr_number,
self.addr_step,
self.addr_regex,
)
def __str__(self):
ret = ""
if self.addr_type == ADDR_IS_REGEX:
ret = str(self.addr_regex)
elif self.addr_type == ADDR_IS_NUM:
ret = str(self.addr_number)
elif self.addr_type == ADDR_IS_NUM_MOD:
ret = "%s~%s" % (self.addr_number, self.addr_step)
elif self.addr_type == ADDR_IS_STEP:
ret = "+%s" % self.addr_step
elif self.addr_type == ADDR_IS_STEP_MOD:
ret = "~%s" % self.addr_step
elif self.addr_type == ADDR_IS_LAST:
ret = "$"
else: # sedparse: this condition should not happen
ret = "<unknown address type '%s'>" % self.addr_type
return ret
class struct_replacement(struct):
def __init__(self):
self.text = "" # sedparse extension
# sedparse: not used
# prefix = ""
# prefix_length = 0
# subst_id = 0
# repl_type = REPL_ASIS # enum replacement_types
# next_ = None # struct_replacement
def __repr__(self):
return "%s(text=%r)" % (self.__class__.__name__, self.text)
def __str__(self):
return self.text
class struct_subst(struct):
def __init__(self):
self.regx = struct_regex()
self.replacement = struct_replacement()
self.outf = struct_output() # "w" option given
# sedparse: not used
# Note that instead of using those attributes to save the found flags,
# sedparse saves them to self.regx.flags as a single string, preserving
# the original order and possible repetition.
# numb = 0 # if >0, only substitute for match number "numb"
# global_ = False # "g" option given
# print_ = False # "p" option given (before/after eval)
# eval_ = False # "e" option given
# max_id = 0 # maximum backreference on the RHS
# replacement_buffer = "" # ifdef lint
def __repr__(self):
return "%s(regx=%r, replacement=%r, outf=%r)" % (
self.__class__.__name__,
self.regx,
self.replacement,
self.outf,
)
def __str__(self):
return (
self.regx.slash
+ str(self.regx.pattern)
+ self.regx.slash
+ str(self.replacement.text)
+ self.regx.slash
+ self.regx.flags
+ (" " + self.outf.name if "w" in self.regx.flags else "")
)
# sedparse: In the original this was a 'union' inside 'struct sed_cmd'
class struct_sed_cmd_x(struct):
"auxiliary data for various commands"
def __init__(self):
# This structure is used for a, i, and c commands.
self.cmd_txt = struct_text_buf()
# This is used for the l, q and Q commands.
self.int_arg = -1
# This is used for the r command. (sedparse: and R w W)
self.fname = ""
# This is used for the hairy s command. (sedparse: and y)
self.cmd_subst = struct_subst()
# This is used for the ":" command.
self.label_name = ""
# This is used for the command comment.
self.comment = "" # sedparse extension
# sedparse: not used
# # This is used for the {}, b, and t commands.
# jump_index = 0
# # This is used for the w command.
# outf = struct_output()
# # This is used for the R command.
# # (despite the struct name, it is used for both in and out files).
# inf = struct_output()
# # This is used for the y command.
# translate = ""
# translatemb = ""
def __repr__(self):
return (
"%s(int_arg=%r, label_name=%r, fname=%r, comment=%r,"
" cmd_txt=%r, cmd_subst=%r)"
% (
self.__class__.__name__,
self.int_arg,
self.label_name,
self.fname,
self.comment,
self.cmd_txt,
self.cmd_subst,
)
)
class struct_sed_cmd(struct):
def __init__(self):
# Command addresses
self.a1 = struct_addr()
self.a2 = struct_addr()
# Non-zero if command is to be applied to non-matches.
self.addr_bang = False # sedparse: using bool
# The actual command character.
self.cmd = ""
# auxiliary data for various commands
self.x = struct_sed_cmd_x()
# The original line number where this command was found
self.line = 0 # sedparse extension
# sedparse: not used
# range_state = RANGE_INACTIVE # See enum addr_state
def __repr__(self):
return "%s(line=%r, cmd=%r, addr_bang=%r, a1=%r, a2=%r, x=%r)" % (
self.__class__.__name__,
self.line,
self.cmd,
self.addr_bang,
self.a1,
self.a2,
self.x,
)
def __str__(self):
ret = []
if self.a1:
ret.append(str(self.a1))
if self.a2:
ret.append(",%s" % self.a2)
if ret:
ret.append(" ")
if self.addr_bang:
ret.append("!")
ret.append(self.cmd)
if self.cmd == "\n":
pass
elif self.cmd == "#":
ret.append(self.x.comment)
elif self.cmd == ":":
ret.append(self.x.label_name)
elif self.cmd in ("s", "y"):
ret.append(str(self.x.cmd_subst))
elif self.x.label_name:
ret.append(" " + self.x.label_name)
elif self.x.fname:
ret.append(" " + self.x.fname)
elif self.x.int_arg > -1:
ret.append(" %s" % self.x.int_arg)
elif self.x.cmd_txt.text: # a i c
ret.append("\\\n%s" % self.x.cmd_txt)
return "".join(ret)
# This is defined in sed.h, but calls is_mb_char() from mbcs.c, where the real
# implementation is.
# XXX sedparse: maybe the translation is wrong or incomplete. It seems to work,
# but I'm not sure if there are some unhandled edge cases here.
def IS_MB_CHAR(ch):
"""Return True if ch is EOF or a valid single-byte character"""
# sedparse: the first condition is necessary because our EOF constant is a
# string.
return ch != EOF and ord(ch) > 127
######################################## translated from regexp.c
# sedparse: no need to compile the regex, just save the collected strings
def compile_regex(pattern, flags):
r = struct_regex()
r.pattern = "".join(pattern)
r.flags = "".join(flags)
return r
######################################## translated from utils.h
# enum exit_codes
EXIT_SUCCESS = 0
EXIT_BAD_USAGE = 1 # bad program syntax, invalid command-line options
# EXIT_BAD_INPUT = 2 # failed to open some of the input files (sedparse: not used)
EXIT_PANIC = 4 # PANIC during program execution
######################################## translated from utils.c
# Print an error message and exit
def panic(msg):
raise ParseError(exitcode=EXIT_PANIC, message="%s: %s" % (program_name, msg))
# In Python we have lists, so all the buffer-related C code is not necessary.
# Only the very minimal set of buffer functions were translated, to avoid having
# to change the logic in the caller code.
def init_buffer():
return []
def add1_buffer(buffer, ch):
if ch != EOF:
buffer.append(ch) # in-place
# the return is never used
def free_buffer(b):
del b
######################################## translated from compile.c
OPEN_BRACKET = "["
CLOSE_BRACKET = "]"
CLOSE_BRACE = "}"
class prog_info:
# When we're reading a script command from a string, `prog.base' points to
# the first character in the string, 'prog.cur' points to the current
# character in the string, and 'prog.end' points to the end of the string.
# This allows us to compile script strings that contain nulls.
base = None # int
cur = None # int
end = None # int
# This is the current script file. If it is NULL, we are reading from a
# string stored at `prog.cur' instead. If both `prog.file' and `prog.cur'
# are NULL, we're in trouble!
file = None # file descriptor
# Using None because some code checks for "is not None" to detect unset state
text = None # sedparse extension
class error_info:
"""Information used to give out useful and informative error messages."""
# This is the name of the current script file.
name = ""
# This is the number of the current script line that we're compiling.
line = 0
# This is the index of the "-e" expressions on the command line.
string_expr_count = 0
# Where we are in the processing of the input.
class prog(prog_info):
pass
class cur_input(error_info):
pass
# Allow for scripts like "sed -e 'i\' -e foo"
pending_text = NULL
old_text_buf = NULL
# sedparse: this is a sed_label struct in the original code. Here it is just an
# integer, because we only care about the indenting levels.
blocks = 0
# Various error messages we may want to print
# sedparse: not used messages are commented
BAD_BANG = "multiple `!'s"
BAD_COMMA = "unexpected `,'"
BAD_STEP = "invalid usage of +N or ~N as first address"
EXCESS_OPEN_BRACE = "unmatched `{'"
EXCESS_CLOSE_BRACE = "unexpected `}'"
EXCESS_JUNK = "extra characters after command"
EXPECTED_SLASH = "expected \\ after `a', `c' or `i'"
NO_CLOSE_BRACE_ADDR = "`}' doesn't want any addresses"
# NO_COLON_ADDR = ": doesn't want any addresses"
# NO_SHARP_ADDR = "comments don't accept any addresses"
NO_COMMAND = "missing command"
# ONE_ADDR = "command only uses one address"
UNTERM_ADDR_RE = "unterminated address regex"
UNTERM_S_CMD = "unterminated `s' command"
UNTERM_Y_CMD = "unterminated `y' command"
UNKNOWN_S_OPT = "unknown option to `s'"
EXCESS_P_OPT = "multiple `p' options to `s' command"
EXCESS_G_OPT = "multiple `g' options to `s' command"
EXCESS_N_OPT = "multiple number options to `s' command"
ZERO_N_OPT = "number option to `s' command may not be zero"
# Y_CMD_LEN = "strings for `y' command are different lengths"
BAD_DELIM = "delimiter character is not a single-byte character"
# ANCIENT_VERSION = "expected newer version of sed"
INVALID_LINE_0 = "invalid usage of line address 0"
UNKNOWN_CMD = "unknown command: `%c'"
# INCOMPLETE_CMD = "incomplete command"
COLON_LACKS_LABEL = '":" lacks a label'
# RECURSIVE_ESCAPE_C = "recursive escaping after \\c not allowed"
# DISALLOWED_CMD = "e/r/w commands disabled in sandbox mode"
MISSING_FILENAME = "missing filename in r/R/w/W commands"
# Complain about an unknown command and exit.
def bad_command(ch):
bad_prog(UNKNOWN_CMD % ch)
# Complain about a programming error and exit.
def bad_prog(why):
if cur_input.name:
msg = "%s: file %s line %d: %s" % (
program_name,
cur_input.name,
cur_input.line,
why,
)
else:
msg = "%s: -e expression #%d, char %d: %s" % (
program_name,
cur_input.string_expr_count,
(prog.cur or 0) - (prog.base or 0),
why,
)
# sedparse extension
# The following exception can be caught and this module can be used again.
# Reset to make sure data from one execution won't affect the next.
reset_globals()
# sedparse extension
# In this point, the original code shows the error message and exits. Doing
# it here would break the usage of sedparse as a Python module. So we raise
# an exception instead, and the calling code decides what to do.
raise ParseError(
message=msg,
file=cur_input.name,
expression=cur_input.string_expr_count,
line=cur_input.line,
column=(prog.cur - prog.base) if prog.cur else 0,
exitcode=EXIT_BAD_USAGE,
)
# Read the next character from the program. Return EOF if there isn't anything
# to read. Keep cur_input.line up to date, so error messages can be meaningful.
def inchar():
ch = EOF
if prog.cur is not None:
if prog.cur < prog.end:
# Original: ch = *prog.cur++;
ch = prog.text[prog.cur]
prog.cur += 1
elif prog.file:
# Original: if (!feof (prog.file)) ch = getc (prog.file);
# https://stackoverflow.com/a/15599780
ch = prog.file.read(1)
if not ch:
ch = EOF
if ch == "\n":
cur_input.line += 1
debug(ch, stats=True)
return ch
# unget `ch' so the next call to inchar will return it.
def savchar(ch):
debug("savchar(%s)" % ch, stats=True)
if ch == EOF:
return
if ch == "\n" and cur_input.line > 0:
cur_input.line -= 1
if prog.cur:
# Original: if (prog.cur <= prog.base || *--prog.cur != ch)
prog.cur -= 1
if prog.cur <= prog.base or prog.text[prog.cur] != ch:
panic(
# sedparse: original code only shows `ch` in this error message
"Called savchar with unexpected pushback (curr=%s %s!=%s)"
% (prog.cur, prog.text[prog.cur], ch)
)
else:
# Original: ungetc(ch, prog.file)
try:
# Go back one *character* in prog.file file descriptor pointer.
# Since one Unicode character can be composed of multiple bytes,
# we need that encoding to know how many bytes we should rewind.
# Note that tell() does not work when reading from STDIN, see
# the temporary file workaround in compile_file().
prog.file.seek(prog.file.tell() - len(ch.encode("utf-8")))
except ValueError: # negative seek position -1
pass
# Read the next non-blank character from the program.
def in_nonblank():
while True:
ch = inchar()
if not ISBLANK(ch):
break
return ch
# sedparse extension
# Ignore multiple trailing blanks and ; until EOC/EOL/EOF. Skipping those chars
# avoids \n incorrectly being considered a new command and producing a new
# undesired blank line in the output.
def ignore_trailing_fluff():
while True:
ch = in_nonblank()
if ch == ";": # skip it
pass
elif ch in (EOF, "\n"): # EOF, EOL
return
else: # start of a new command
savchar(ch)
return
# Consume script input until a valid end of command marker is found: comment,
# closing brace, newline, semicolon or EOF. If any other character is found, die
# with 'extra characters after command' error.
def read_end_of_cmd():
ch = in_nonblank()
if ch in (CLOSE_BRACE, "#"):
savchar(ch)
elif ch not in (EOF, "\n", ";"):
bad_prog(EXCESS_JUNK)
# sedparse extension: Ignore trailing blanks and ; until EOC/EOL/EOF
elif ch == ";":
ignore_trailing_fluff()
# Read an integer value from the program.
# sedparse: original code uses math, we use list append.
def in_integer(ch):
num = []
while ISDIGIT(ch):
num.append(ch)
ch = inchar()
savchar(ch)
return int("".join(num))
def add_then_next(buffer, ch):
add1_buffer(buffer, ch)
return inchar()
# convert_number() - Translation not needed
# sedparse extension
# This is a copy of read_filename, but preserving blanks.
def read_comment():
b = init_buffer()
ch = inchar()
while ch not in (EOF, "\n"):
ch = add_then_next(b, ch)
return b
# Read in a filename for a `r', `w', or `s///w' command.
def read_filename():
b = init_buffer()
ch = in_nonblank()
while ch not in (EOF, "\n"):
ch = add_then_next(b, ch)
# add1_buffer(b, "\0"); # not necessary in Python
return b
# get_openfile() - Translation not needed
def next_cmd_entry(vector):
# sedparse: in the original there's some of vector handling code. Here the
# equivalent is the next line and `vector.append()` at the end.
cmd = struct_sed_cmd()
cmd.a1 = NULL
cmd.a2 = NULL
# cmd.range_state = RANGE_INACTIVE # sedparse: not used
cmd.addr_bang = False
cmd.cmd = "\0" # something invalid, to catch bugs early
vector.append(cmd)
return cmd
def snarf_char_class(b): # sedparse: cur_stat argument not necessary
state = 0
delim = None # delim IF_LINT( = 0)
ch = inchar()
if ch == "^":
ch = add_then_next(b, ch)
if ch == CLOSE_BRACKET:
ch = add_then_next(b, ch)
# States are:
# 0 outside a collation element, character class or collation class
# 1 after the bracket
# 2 after the opening ./:/=
# 3 after the closing ./:/=
# Original: for (;; ch = add_then_next(b, ch)) {
first_loop_run = True
while True:
if not first_loop_run:
ch = add_then_next(b, ch)
first_loop_run = False
mb_char = IS_MB_CHAR(ch)
if ch in (EOF, "\n"):
return ch
if ch in (".", ":", "="):
if mb_char:
continue
if state == 1:
delim = ch
state = 2
elif state == 2 and ch == delim:
state = 3
# else:
# break # break from C-switch not necessary in Python
continue
if ch == OPEN_BRACKET:
if mb_char:
continue
if state == 0:
state = 1
continue
if ch == CLOSE_BRACKET:
if mb_char:
continue
if state in (0, 1):
return ch
if state == 3:
state = 0
# Getting a character different from .=: whilst in state 1
# goes back to state 0, getting a character different from ]
# whilst in state 3 goes back to state 2.
#
# Original: state &= ~1
# sedparse: Instead of following the original (tricky &= right after a
# switch inside a loop), I've opted to implement literally what the
# comment says.
if ch not in (".", ":", "=") and state == 1:
state = 0
elif ch != CLOSE_BRACKET and state == 3:
state = 2
def match_slash(slash, regex): # char, bool
# We allow only 1 byte characters for a slash.
if IS_MB_CHAR(slash):
bad_prog(BAD_DELIM)
b = init_buffer()
# Original: while ((ch = inchar ()) != EOF && ch != '\n')
while True:
ch = inchar()
if ch in (EOF, "\n"):
break
if not IS_MB_CHAR(ch):
if ch == slash:
return b
elif ch == "\\":
ch = inchar()
if ch == EOF:
break
# sedparse: GNU sed interprets \n here, we don't
# elif ch == "n" and regex:
# ch = "\n"
# sedparse: GNU sed remove the leading \ from \\n, \/, \&.
# We don't since we keep the original user text.
# elif (ch != "\n" and (ch != slash or (not regex and ch == "&"))):
else:
add1_buffer(b, "\\")
elif ch == OPEN_BRACKET and regex:
add1_buffer(b, ch)
ch = snarf_char_class(b)
if ch != CLOSE_BRACKET:
break
add1_buffer(b, ch)
if ch == "\n":
savchar(ch) # for proper line number in error report
free_buffer(b)
return NULL
# sedparse: this function works differently from the original.
# In GNU sed, there's no return, since it just sets all the flags as properties
# of "cmd_s". Here it collects and returns the flags as a list.
def mark_subst_opts(cmd_s):
flags = []
numb = False
while True:
ch = in_nonblank()
debug("s flag candidate: %r" % ch)
# sedparse: just append the flags to the list
if ch in ("i", "I", "m", "M", "e"): # GNU extensions
flags.append(ch)
elif ch == "p":
if ch in flags:
bad_prog(EXCESS_P_OPT)
flags.append(ch)
elif ch == "g":
if ch in flags:
bad_prog(EXCESS_G_OPT)
flags.append(ch)
elif ch == "w":
flags.append(ch)
# sedparse: This flag will always be at the end of the list, since
# after w cannot exist any other flag because the filename consumes
# everything until the end of the line.
b = read_filename()
if not b:
bad_prog(MISSING_FILENAME)
cmd_s.outf.name = "".join(b)
debug("s flag filename: %r" % cmd_s.outf.name)
return flags
elif ch in "0123456789":
if numb:
bad_prog(EXCESS_N_OPT)
n = in_integer(ch)
if int(n) == 0:
bad_prog(ZERO_N_OPT)
flags.append(str(n))
numb = True
elif ch in (CLOSE_BRACE, "#"):
savchar(ch)
return flags
elif ch in (EOF, "\n"):
return flags
# sedparse extension: Ignore trailing blanks and ; until EOC/EOL/EOF
elif ch == ";":
ignore_trailing_fluff()
return flags
elif ch == "\r":
if inchar() == "\n":
return flags
bad_prog(UNKNOWN_S_OPT)
else:
bad_prog(UNKNOWN_S_OPT)
# read in a label for a `:', `b', or `t' command
def read_label():
b = init_buffer()
ch = in_nonblank()
while ch not in (EOF, "\n", ";", CLOSE_BRACE, "#") and not ISBLANK(ch):
ch = add_then_next(b, ch)
savchar(ch)
# sedparse extension: Ignore trailing blanks and ; until EOC/EOL/EOF
ignore_trailing_fluff()
# add1_buffer(b, "\0") # not necessary in Python
ret = "".join(b)
free_buffer(b)
return ret