-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrek.c
1498 lines (1341 loc) · 34.7 KB
/
srek.c
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
/*
* srek - Structural RegEx Kit
*
* MIT License
* Copyright (c) 2022 Barnabás Zahorán, see LICENSE
*
* For usage details, run with --help, see srek(1) or the README.md
*/
#include <ctype.h>
#include <getopt.h>
#include <regex.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if __linux__
#include <sys/prctl.h>
#endif
#include <sys/wait.h>
#include <unistd.h>
#define VERSION "1.1"
#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(*(arr)))
#define ERR_NO_CMDLINE 1
#define ERR_INVALID_CMDLINE 2
#define ERR_CANNOT_READ_INPUT 3
#define ERR_CANNOT_WRITE_OUTPUT 4
#define ERR_REGCOMP_FAILED 5
#define ERR_MEM_ALLOC_FAILED 6
#define ERR_EXTCMD_RUN_FAILED 7
#define ERR_EMPTY_ARG 8
#define RE_SUBMATCH_CNT 9 /* support up to 9 submatches: \1,...,\9 */
#define ERRBUF_SIZE 256
#define READ_CHUNK_SIZE 65536 /* 64 KB */
static const char *HELP[] = {
"Usage: srek [OPTION...] COMMAND-LINE [FILE...]\n\n"
"When FILE is missing, srek will read from stdin.\n\n",
"OPTIONS\n"
"-B, --basic-regexp Use POSIX Basic regular expressions\n"
"-E, --extended-regexp Use POSIX Extended regular expressions (this is the default)\n"
"-f, --file=<file> Read COMMAND-LINE from <file>\n"
"-h, --help Display this help\n"
"-i, --ignorecase Ignore case when matching regex\n"
"-n, --quiet Do not put an implicit print command at the end\n"
"-N, --reg-newline Match-any-character operators don't match a newline\n"
"-v, --version Display version information\n\n",
"COMMAND-LINE may contain a list of commands separated by optional whitespaces:\n\n"
"x/regexp/ Extract matches from input to a set of selections\n"
"y/regexp/ Like x, but extract the non-matching parts instead\n"
"g/regexp/ Filter selections with <regexp>\n"
"v/regexp/ Like g, but keep the non-matching selections instead\n"
"~ Flip selections (everything selected becomes unselected and vice versa)\n"
"L Extract lines, shorthand for x/[^\\n]*\\n/\n"
"u Undo all selections\n\n",
"p Print all selection to stdout\n"
"d Delete selected text, selection resets\n"
"c/replacement/ Replace each selection with <replacement>, selection resets\n"
"s/regexp/replacement/ Replace matching parts of each selection with <replacement>\n"
"i/prefix/ Prefix selections with <prefix>, shorthand for s/^/text/\n"
"a/suffix/ Suffix selections with <suffix>, shorthand for s/$/text/\n"
"S/prefix/suffix/ Surround selections, shorthand for i/prefix/a/suffix/\n\n",
"r/file/ Replace selections with contents read from <file>\n"
"R/file/ Like r, but append instead\n"
"w/file/sep/ Write selections to <file>, each separated by <sep>\n"
"W/file/sep/ Like w, but append to file instead\n\n",
"!/cmd/ Run <cmd> once for each selection\n"
"</cmd/ Run <cmd> once, and replace selections with its stdout\n"
">/cmd/ Run <cmd> once for each selection by passing the selection to its stdin\n"
"|/cmd/ Run <cmd> on each selection by taking it as stdin and replacing it with stdout\n"
"t/cmd/ Keep only those selections for which <cmd> returned with success (zero)\n"
"T/cmd/ Keep only those selections for which <cmd> returned with error (non-zero)\n"
" Note: <cmd> must be a valid shell command\n\n",
"#comment Comment till next newline\n"
};
typedef int bool;
enum { false, true };
typedef enum {
CMD_PRINT, /* print current selections */
CMD_DELETE, /* delete selections */
CMD_CHANGE, /* change selections to arbitrary string */
CMD_SUB, /* regex replace on selections */
CMD_GUARD, /* regex test on selections to deselect or keep */
CMD_VGUARD, /* negated guard */
CMD_XTRACT, /* extract regex matches from selections to new selections */
CMD_YTRACT, /* like extract, but only select the non-matching */
CMD_INSERT, /* prefix selections with arbitrary string */
CMD_APPEND, /* suffix selections with arbitrary string */
CMD_SURROUND, /* prefix and suffix selections with arbitrary string */
CMD_FLIP, /* flip selections (take complement of the intervals) */
CMD_UNDOX, /* undo all selections */
CMD_READ, /* replace selections with file content */
CMD_READAPP, /* append selections with file content */
CMD_WRITE, /* overwrite file with selections separated by a separator */
CMD_WRITEAPP, /* append file with selections separated by a separator */
CMD_EXTCMD, /* run external command for each selection */
CMD_EXTCMD_I, /* replace selections with output of external command */
CMD_EXTCMD_O, /* run external command with selections as input */
CMD_EXTCMD_IO, /* pass each selection to external command then replace it with the output */
CMD_EXTCMD_T, /* deselect those for which the command returned with error */
CMD_EXTCMD_TN, /* deselect those for which the command returned with success */
CMD_LINES, /* extract lines to selections */
CMD_MAX
} CmdId;
typedef struct Cmd {
CmdId id;
char **args;
regex_t *regex;
struct Cmd *next;
} Cmd;
typedef struct {
char ch;
size_t argcnt;
void (*callback)(const Cmd*);
} CmdDesc;
typedef struct Intval {
size_t from;
size_t len;
struct Intval *next;
} Intval;
typedef struct StrList {
char *str;
struct StrList *next;
} StrList;
static Cmd* addcmd(CmdId id);
static Intval* addintv(size_t from, size_t len, Intval **list, Intval **last);
static void* alloc(size_t size);
static CmdId chtocmdid(char ch);
static void cleanupall(void);
static void cleanupcmds(void);
static void cleanupsels(void);
static void compileregexes(void);
static void emptyargerr(const Cmd *cmd);
static void escapechars(char **str, bool freeorig);
static void flipintvals(const Intval *bound, Intval **intvs, Intval **lastintv);
static void guardinternal(const Cmd *cmd, bool negated);
static void parsecmds(char *cmdline);
static char* readfullfile(FILE * file, size_t *size);
static char* readfullnamedfile(const char *fname, size_t *size);
static void removebackslashes(char **str);
static void run(void);
static void testinternal(const char *cmdline, bool negated);
static void writeinternal(const char *fname, const char *sep, bool append);
static Intval* xtractinternal(regex_t *regex, Intval **prevsel, Intval *sel, bool negated);
static void cmd_print(const Cmd *cmd); /* command callbacks */
static void cmd_delete(const Cmd *cmd);
static void cmd_change(const Cmd *cmd);
static void cmd_sub(const Cmd *cmd);
static void cmd_guard(const Cmd *cmd);
static void cmd_vguard(const Cmd *cmd);
static void cmd_xtract(const Cmd *cmd);
static void cmd_ytract(const Cmd *cmd);
static void cmd_insert(const Cmd *cmd);
static void cmd_append(const Cmd *cmd);
static void cmd_surround(const Cmd *cmd);
static void cmd_flip(const Cmd *cmd);
static void cmd_undox(const Cmd *cmd);
static void cmd_read(const Cmd *cmd);
static void cmd_readapp(const Cmd *cmd);
static void cmd_write(const Cmd *cmd);
static void cmd_writeapp(const Cmd *cmd);
static void cmd_extcmd(const Cmd *cmd);
static void cmd_extcmd_i(const Cmd *cmd);
static void cmd_extcmd_o(const Cmd *cmd);
static void cmd_extcmd_io(const Cmd *cmd);
static void cmd_extcmd_t(const Cmd *cmd);
static void cmd_extcmd_tn(const Cmd *cmd);
static void cmd_lines(const Cmd *cmd);
static const CmdDesc cmddescs[CMD_MAX] = {
/* id (index) ch argcnt callback */
/* CMD_PRINT */ { 'p', 0, cmd_print },
/* CMD_DELETE */ { 'd', 0, cmd_delete },
/* CMD_CHANGE */ { 'c', 1, cmd_change },
/* CMD_SUB */ { 's', 2, cmd_sub },
/* CMD_GUARD */ { 'g', 1, cmd_guard },
/* CMD_VGUARD */ { 'v', 1, cmd_vguard },
/* CMD_XTRACT */ { 'x', 1, cmd_xtract },
/* CMD_YTRACT */ { 'y', 1, cmd_ytract },
/* CMD_INSERT */ { 'i', 1, cmd_insert },
/* CMD_APPEND */ { 'a', 1, cmd_append },
/* CMD_SURROUND */ { 'S', 2, cmd_surround },
/* CMD_FLIP */ { '~', 0, cmd_flip },
/* CMD_UNDOX */ { 'u', 0, cmd_undox },
/* CMD_READ */ { 'r', 1, cmd_read },
/* CMD_READAPP */ { 'R', 1, cmd_readapp },
/* CMD_WRITE */ { 'w', 2, cmd_write },
/* CMD_WRITEAPP */ { 'W', 2, cmd_writeapp },
/* CMD_EXTCMD */ { '!', 1, cmd_extcmd },
/* CMD_EXTCMD_I */ { '<', 1, cmd_extcmd_i },
/* CMD_EXTCMD_O */ { '>', 1, cmd_extcmd_o },
/* CMD_EXTCMD_IO */ { '|', 1, cmd_extcmd_io },
/* CMD_EXTCMD_T */ { 't', 1, cmd_extcmd_t },
/* CMD_EXTCMD_TN */ { 'T', 1, cmd_extcmd_tn },
/* CMD_LINES */ { 'L', 0, cmd_lines },
};
static const struct option long_options[] = {
{ "basic-regexp" , no_argument , NULL, 'B' },
{ "extended-regexp", no_argument , NULL, 'E' },
{ "file" , required_argument, NULL, 'f' },
{ "help" , no_argument , NULL, 'h' },
{ "ignorecase" , no_argument , NULL, 'i' },
{ "quiet" , no_argument , NULL, 'n' },
{ "reg-newline" , no_argument , NULL, 'N' },
{ "version" , no_argument , NULL, 'v' },
{ 0 , 0 , 0 , 0 }
};
static Cmd *cmds = NULL; /* globals */
static Cmd *lastcmd = NULL;
static Intval *sels = NULL;
static Intval *lastsel = NULL;
static char *buffer;
static size_t buflen;
static bool quiet = false; /* settings */
static bool ignorecase = false;
static bool extendedreg = true;
static bool regnewline = false;
Cmd*
addcmd(CmdId id)
{
size_t i;
Cmd *cmd = alloc(sizeof(Cmd));
cmd->id = id;
if (0 < cmddescs[id].argcnt) {
cmd->args = alloc(sizeof(char*) * cmddescs[id].argcnt);
for (i = 0; i < cmddescs[id].argcnt; ++i) {
cmd->args[i] = NULL;
}
}
else {
cmd->args = NULL;
}
cmd->regex = NULL;
cmd->next = NULL;
if (lastcmd) {
lastcmd->next = cmd;
}
else {
cmds = cmd;
lastcmd = cmd;
}
return cmd;
}
Intval*
addintv(size_t from, size_t len, Intval **list, Intval **last)
{
Intval *intv = alloc(sizeof(Intval));
intv->from = from;
intv->len = len;
intv->next = NULL;
if (*list) {
(*last)->next = intv;
}
else {
*list = intv;
}
*last = intv;
return intv;
}
void*
alloc(size_t size)
{
void *buf = malloc(size);
if (buf) {
return buf;
}
perror("malloc()");
exit(ERR_MEM_ALLOC_FAILED);
}
CmdId
chtocmdid(char ch)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(cmddescs); ++i) {
if (cmddescs[i].ch == ch) {
return i;
}
}
return CMD_MAX; /* not found */
}
void
cleanupall(void)
{
cleanupsels();
cleanupcmds();
free(buffer);
}
void
cleanupcmds(void)
{
Cmd *cmd;
Cmd *nextc;
unsigned int i;
for (cmd = cmds; cmd != NULL;) {
nextc = cmd->next;
for (i = 0; i < cmddescs[cmd->id].argcnt; ++i) {
free(cmd->args[i]);
}
free(cmd->args);
if (cmd->regex) {
regfree(cmd->regex);
free(cmd->regex);
}
free(cmd);
cmd = nextc;
}
cmds = NULL;
lastcmd = NULL;
}
void
cleanupsels(void)
{
Intval *sel;
Intval *nexts;
for (sel = sels; sel != NULL;) {
nexts = sel->next;
free(sel);
sel = nexts;
}
sels = NULL;
lastsel = NULL;
}
void
emptyargerr(const Cmd *cmd)
{
switch (cmd->id) {
case CMD_READ: case CMD_READAPP: case CMD_WRITE: case CMD_WRITEAPP:
case CMD_EXTCMD: case CMD_EXTCMD_I: case CMD_EXTCMD_O: case CMD_EXTCMD_IO:
case CMD_EXTCMD_T: case CMD_EXTCMD_TN:
if (!cmd->args[0]) {
fprintf(stderr, "Error: argument cannot be empty for '%c'!\n", cmddescs[cmd->id].ch);
cleanupall(); exit(ERR_EMPTY_ARG);
}
default: break;
}
}
void
escapechars(char **str, bool freeorig)
{
char *newstr = alloc(strlen(*str) + 1);
size_t iold = 0;
size_t inew = 0;
for (; (*str)[iold] != '\0'; ++iold, ++inew) {
if ((*str)[iold] == '\\') {
switch ((*str)[iold + 1]) {
case 'n': newstr[inew] = '\n'; ++iold; continue;
case 't': newstr[inew] = '\t'; ++iold; continue;
}
}
newstr[inew] = (*str)[iold];
}
newstr[inew] = '\0';
if (freeorig) {
free(*str);
}
*str = newstr;
}
void
compileregexes(void)
{
Cmd *cmd;
int cflags = 0;
int errcode;
char errbuf[ERRBUF_SIZE];
if (ignorecase) { cflags |= REG_ICASE; }
if (extendedreg) { cflags |= REG_EXTENDED; }
if (regnewline) { cflags |= REG_NEWLINE; }
for (cmd = cmds; cmd != NULL; cmd = cmd->next) {
switch (cmd->id) {
case CMD_SUB:
case CMD_GUARD:
case CMD_VGUARD:
case CMD_XTRACT:
case CMD_YTRACT:
if (cmd->args[0]) { /* empty args[0] -> NULL regex, which must be handled later */
cmd->regex = alloc(sizeof(regex_t));
if ((errcode = regcomp(cmd->regex, cmd->args[0], cflags)) != 0) {
regerror(errcode, cmd->regex, errbuf, ARRAY_SIZE(errbuf));
fprintf(stderr, "Error: invalid pattern '%s': %s!\n", cmd->args[0], errbuf);
cleanupcmds(); exit(ERR_REGCOMP_FAILED);
}
}
break;
default: break;
}
}
}
void
cmd_print(const Cmd *cmd)
{
Intval *sel;
for (sel = sels; sel != NULL; sel = sel->next) {
fwrite(buffer + sel->from, sizeof(char), sel->len, stdout);
}
}
void
cmd_delete(const Cmd *cmd)
{
static char *empty = "";
Cmd proxycmd = {CMD_CHANGE, &empty, NULL, NULL};
cmd_change(&proxycmd);
}
void
cmd_change(const Cmd *cmd)
{
Intval *sel;
size_t iold = 0;
size_t inew = 0;
size_t newbuflen = buflen;
char *newbuffer;
const char *tostr = cmd->args[0];
size_t tostrlen = tostr ? strlen(tostr) : 0;
for (sel = sels; sel != NULL; sel = sel->next) {
newbuflen += (tostrlen - sel->len);
}
if (newbuflen == 0) { /* empty buffer needs no new memory or copying */
buffer[0] = '\0';
buflen = 0;
cleanupsels();
return;
}
newbuffer = alloc(newbuflen + 1);
for (sel = sels; sel != NULL;) {
if (iold < sel->from) {
memcpy(newbuffer + inew, buffer + iold, sel->from - iold);
inew += sel->from - iold;
iold = sel->from;
}
else {
memcpy(newbuffer + inew, tostr, tostrlen);
inew += tostrlen;
iold += sel->len;
sel = sel->next;
}
}
if (inew < newbuflen) {
memcpy(newbuffer + inew, buffer + iold, buflen - iold);
}
free(buffer);
buffer = newbuffer;
buflen = newbuflen;
cleanupsels(); /* selection should be reset after a change */
addintv(0, buflen, &sels, &lastsel);
}
void
cmd_sub(const Cmd *cmd)
{
Intval *sel;
char tmp;
char *str;
regmatch_t pmatch[1 + RE_SUBMATCH_CNT]; /* 0th is entire match, rest are submatches */
Intval *intv;
Intval *tmpintv;
Intval *matchintvs = NULL;
Intval *lastintv = NULL;
unsigned int matchcnt = 0;
unsigned int matchcnt_sel;
const char *tostr = cmd->args[1];
size_t tostrlen = tostr ? strlen(tostr) : 0;
size_t replacedlen = 0;
size_t replacedlen_sel;
size_t newbuflen;
size_t iold;
size_t inew;
size_t offs;
size_t newseloffs = 0;
char *newbuffer;
if (!cmd->regex && (!cmd->args[0] || (strcmp(cmd->args[0], "^") && strcmp(cmd->args[0], "$")))) {
return; /* empty regex matches nothing -> no match -> no sub to do */
}
for (sel = sels; sel != NULL; sel = sel->next) {
tmp = buffer[sel->from + sel->len];
buffer[sel->from + sel->len] = '\0';
str = buffer + sel->from;
matchcnt_sel = 0;
replacedlen_sel = 0;
/* handle some special cases (e.g. zero size regexes) */
if (!strcmp(cmd->args[0], "^")) {
addintv((size_t)(str - buffer), 0, &matchintvs, &lastintv);
++matchcnt_sel;
}
else if (!strcmp(cmd->args[0], "$")) {
addintv(sel->from + sel->len, 0, &matchintvs, &lastintv);
++matchcnt_sel;
}
else if (!strcmp(cmd->args[0], "^$")) {
if (sel->len == 0) { /* should only match an empty range */
intv = addintv(sel->from, 0, &matchintvs, &lastintv);
++matchcnt_sel;
replacedlen_sel += intv->len;
}
}
else {
while (regexec(cmd->regex, str, ARRAY_SIZE(pmatch), pmatch, 0) == 0) {
intv = addintv(
(size_t)(str - buffer) + (size_t)pmatch[0].rm_so,
(size_t)(pmatch[0].rm_eo - pmatch[0].rm_so),
&matchintvs,
&lastintv
);
++matchcnt_sel;
replacedlen_sel += intv->len;
str += pmatch[0].rm_eo;
if (*str == '\0') {
break;
}
}
}
buffer[sel->from + sel->len] = tmp;
matchcnt += matchcnt_sel;
replacedlen += replacedlen_sel;
sel->from += newseloffs; /* update selection intervals */
offs = matchcnt_sel * tostrlen - replacedlen_sel;
sel->len += offs;
newseloffs += offs;
}
if (matchcnt == 0) {
return; /* no match, nothing to substitute */
}
newbuflen = buflen - replacedlen + matchcnt * tostrlen;
newbuffer = alloc(newbuflen + 1);
iold = 0;
inew = 0;
for (intv = matchintvs; intv != NULL;) {
if (iold < intv->from) {
memcpy(newbuffer + inew, buffer + iold, intv->from - iold);
inew += intv->from - iold;
iold = intv->from;
}
else {
if (tostrlen != 0) {
memcpy(newbuffer + inew, tostr, tostrlen);
inew += tostrlen;
}
iold += intv->len;
intv = intv->next;
}
}
if (inew < newbuflen) {
memcpy(newbuffer + inew, buffer + iold, buflen - iold);
}
free(buffer);
buffer = newbuffer;
buflen = newbuflen;
/* cleanup */
for (intv = matchintvs; intv != NULL;) {
tmpintv = intv;
intv = intv->next;
free(tmpintv);
}
}
void
cmd_guard(const Cmd *cmd)
{
guardinternal(cmd, false);
}
void
cmd_vguard(const Cmd *cmd)
{
guardinternal(cmd, true);
}
void
cmd_xtract(const Cmd *cmd)
{
Intval *sel;
Intval *prevsel = NULL;
/* zero length extractions should result in no extraction */
if (!cmd->args[0] || !strcmp(cmd->args[0], "^") || !strcmp(cmd->args[0], "$") || !strcmp(cmd->args[0], "^$")) {
cleanupsels();
return;
}
for (sel = sels; sel != NULL;) {
prevsel = xtractinternal(cmd->regex, &prevsel, sel, false);
sel = prevsel ? prevsel->next : NULL;
}
lastsel = prevsel;
}
void
cmd_ytract(const Cmd *cmd)
{
Intval *sel;
Intval *prevsel = NULL;
/* complement of zero length extractions should imply full extraction */
if (!cmd->args[0] || !strcmp(cmd->args[0], "^") || !strcmp(cmd->args[0], "$") || !strcmp(cmd->args[0], "^$")) {
return;
}
for (sel = sels; sel != NULL;) {
prevsel = xtractinternal(cmd->regex, &prevsel, sel, true);
sel = prevsel ? prevsel->next : NULL;
}
lastsel = prevsel;
}
void
cmd_insert(const Cmd *cmd)
{
static char *patternstart = "^";
Cmd proxycmd = {CMD_SUB, NULL, NULL, NULL};
char *args[2];
args[0] = patternstart;
args[1] = cmd->args[0]; /* prefix */
proxycmd.args = args;
cmd_sub(&proxycmd);
}
void
cmd_append(const Cmd *cmd)
{
static char *patternend = "$";
Cmd proxycmd = {CMD_SUB, NULL, NULL, NULL};
char *args[2];
args[0] = patternend;
args[1] = cmd->args[0]; /* suffix */
proxycmd.args = args;
cmd_sub(&proxycmd);
}
void
cmd_surround(const Cmd *cmd)
{
Cmd proxyins = {CMD_INSERT, NULL, NULL, NULL};
Cmd proxyapp = {CMD_APPEND, NULL, NULL, NULL};
proxyins.args = &(cmd->args[0]); /* prefix */
proxyapp.args = &(cmd->args[1]); /* suffix */
cmd_insert(&proxyins);
cmd_append(&proxyapp);
}
void
cmd_flip(const Cmd *cmd)
{
Intval bound; bound.from = 0; bound.len = buflen; bound.next = NULL;
flipintvals(&bound, &sels, &lastsel);
}
void
cmd_undox(const Cmd *cmd)
{
cleanupsels();
addintv(0, buflen, &sels, &lastsel);
}
void
cmd_read(const Cmd *cmd)
{
char *fcontent = readfullnamedfile(cmd->args[0], NULL);
Cmd proxycmd = {CMD_CHANGE, NULL, NULL, NULL};
if (fcontent) {
proxycmd.args = &fcontent;
cmd_change(&proxycmd);
free(fcontent);
}
else {
cleanupall(); exit(ERR_CANNOT_READ_INPUT);
}
}
void
cmd_readapp(const Cmd *cmd)
{
char *fcontent = readfullnamedfile(cmd->args[0], NULL);
Cmd proxycmd = {CMD_APPEND, NULL, NULL, NULL};
if (fcontent) {
proxycmd.args = &fcontent;
cmd_append(&proxycmd);
free(fcontent);
}
else {
cleanupall(); exit(ERR_CANNOT_READ_INPUT);
}
}
void
cmd_write(const Cmd *cmd)
{
writeinternal(cmd->args[0], cmd->args[1], false);
}
void
cmd_writeapp(const Cmd *cmd)
{
writeinternal(cmd->args[0], cmd->args[1], true);
}
void
cmd_extcmd(const Cmd *cmd)
{
Intval *sel;
for (sel = sels; sel != NULL; sel = sel->next) {
system(cmd->args[0]);
}
}
void
cmd_extcmd_i(const Cmd *cmd)
{
Cmd proxycmd = {CMD_CHANGE, NULL, NULL, NULL};
FILE *cmdf = popen(cmd->args[0], "r");
char *cmdout = NULL;
if (!cmdf) {
perror("popen()");
cleanupall(); exit(ERR_EXTCMD_RUN_FAILED);
}
if (!(cmdout = readfullfile(cmdf, NULL))) {
cleanupall(); exit(ERR_CANNOT_READ_INPUT);
}
pclose(cmdf);
proxycmd.args = &cmdout;
cmd_change(&proxycmd);
free(cmdout);
}
void
cmd_extcmd_o(const Cmd *cmd)
{
Intval *sel;
FILE *cmdf;
for (sel = sels; sel != NULL; sel = sel->next) {
if (!(cmdf = popen(cmd->args[0], "w"))) {
perror("popen()");
cleanupall(); exit(ERR_EXTCMD_RUN_FAILED);
}
fwrite(buffer + sel->from, sizeof(char), sel->len, cmdf);
pclose(cmdf);
}
}
void
cmd_extcmd_io(const Cmd *cmd)
{
Intval *sel;
int inpipefd[2];
int outpipefd[2];
pid_t pid;
int childret;
ssize_t rwret;
size_t rwcnt;
size_t bsize;
char *buf;
char *bptr;
void *reallocret;
StrList *rstr = NULL;
StrList *rstrtmp;
StrList *replacestrs = NULL;
StrList *lastreplacestr = NULL;
long newbuflen = (signed)buflen;
char *newbuffer;
size_t iold;
size_t inew;
for (sel = sels; sel != NULL; sel = sel->next) {
/* use two pipes for I/O communication */
if (pipe(inpipefd) != 0 || pipe(outpipefd) != 0) {
perror("pipe()");
cleanupall(); exit(ERR_EXTCMD_RUN_FAILED);
}
pid = fork();
if (pid == -1) {
perror("fork()");
cleanupall(); exit(ERR_EXTCMD_RUN_FAILED);
}
if (pid == 0) {
dup2(outpipefd[0], STDIN_FILENO);
dup2(inpipefd[1], STDOUT_FILENO);
dup2(inpipefd[1], STDERR_FILENO);
close(outpipefd[0]);
close(outpipefd[1]);
close(inpipefd[0]);
close(inpipefd[1]);
#if __linux__
prctl(PR_SET_PDEATHSIG, SIGTERM); /* send SIGTERM if the parent dies */
#endif
execl("/bin/sh", "sh", "-c", cmd->args[0], (char*)NULL);
exit(ERR_EXTCMD_RUN_FAILED); /* exec returned meaning an error occured */
}
close(outpipefd[0]); /* close unused pipe ends */
close(inpipefd[1]);
/* write selection to cmd's stdin */
rwcnt = 0;
while (0 < (rwret = write(outpipefd[1], buffer + sel->from + rwcnt, sel->len - rwcnt))) {
rwcnt += (size_t)rwret;
}
if (rwret == -1) {
perror("write()");
cleanupall(); exit(ERR_CANNOT_WRITE_OUTPUT);
}
close(outpipefd[1]);
/* read cmd's stdout and replace selection with it */
bsize = READ_CHUNK_SIZE; /* will get doubled for each new realloc */
buf = alloc(bsize); /* with 64 KB chunk size: 100 MB input needs 11 reallocs, 1 GB needs 14 */
bptr = buf;
rwcnt = 0; rwret = 0;
do {
rwcnt += (size_t)rwret;
bptr += rwret;
if (bsize < rwcnt + READ_CHUNK_SIZE) {
if ((reallocret = realloc(buf, bsize *= 2))) {
buf = reallocret;
bptr = buf + rwcnt;
}
else {
perror("realloc()");
cleanupall(); exit(ERR_MEM_ALLOC_FAILED);
}
}
} while (0 < (rwret = read(inpipefd[0], bptr, READ_CHUNK_SIZE)));
if (rwret == -1) {
perror("read()");
cleanupall(); exit(ERR_CANNOT_WRITE_OUTPUT);
}
close(inpipefd[0]);
*bptr = '\0';
rstr = alloc(sizeof(StrList));
rstr->str = buf;
rstr->next = NULL;
if (lastreplacestr) {
lastreplacestr->next = rstr;
}
else {
replacestrs = rstr;
}
lastreplacestr = rstr;
newbuflen += ((bptr - buf) - (long)sel->len);
waitpid(pid, &childret, 0);
}
newbuffer = alloc((size_t)newbuflen + 1);
iold = 0;
inew = 0;
sel = sels;
rstr = replacestrs;
while (inew < (size_t)newbuflen) {
if (!sel) {
memcpy(newbuffer + inew, buffer + iold, (size_t)newbuflen - inew);
break;
}
if (iold < sel->from) {
memcpy(newbuffer + inew, buffer + iold, sel->from - iold);
inew += sel->from - iold;
iold += sel->from - iold;
}
else {
memcpy(newbuffer + inew, rstr->str, strlen(rstr->str));
inew += strlen(rstr->str);
iold += sel->len;
sel = sel->next;
rstr = rstr->next;
}
}
for (rstr = replacestrs; rstr != NULL;) { /* cleanup */
rstrtmp = rstr;
rstr = rstr->next;
free(rstrtmp->str);
free(rstrtmp);
}
free(buffer);
buffer = newbuffer;
buflen = (size_t)newbuflen;
cleanupsels(); /* selection should be reset after a change */
addintv(0, buflen, &sels, &lastsel);
}
void
cmd_extcmd_t(const Cmd *cmd)
{
testinternal(cmd->args[0], false);
}
void
cmd_extcmd_tn(const Cmd *cmd)
{
testinternal(cmd->args[0], true);
}
void
cmd_lines(const Cmd *cmd)
{
static char *LINES_REGX = "[^\n]*\n";
regex_t reg;
Cmd proxycmd = {CMD_XTRACT, NULL, NULL, NULL};
proxycmd.args = &LINES_REGX;
proxycmd.regex = ®
regcomp(®, LINES_REGX, 0); /* note: we can assume the above regex will always compile */
cmd_xtract(&proxycmd);
regfree(®);
}
void
flipintvals(const Intval *bound, Intval **intvs, Intval **lastintv)
{
size_t start;
size_t end;
Intval *newintvs = NULL;
Intval *newlastintv = NULL;
Intval *previntv;
Intval *intv;
Intval *tmpintv;
if (!(*intvs)) { /* empty interval flipped is the whole interval */
addintv(bound->from, bound->len, &newintvs, &newlastintv);
}
else if ((*intvs)->from == bound->from && (*intvs)->len == bound->len) { /* whole interval flipped is empty */
newintvs = NULL;
newlastintv = NULL;
}
else {
start = bound->from; /* before first intv */
end = (*intvs)->from;
if (bound->from < (*intvs)->from) {
addintv(start, end - start, &newintvs, &newlastintv);
}
for (previntv = (*intvs), intv = (*intvs)->next; intv != NULL;) { /* gaps inbetween intvs */