-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbash-5.1.patch
973 lines (973 loc) · 27.1 KB
/
bash-5.1.patch
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
diff --git a/examples/loadables/epc_completion.c b/examples/loadables/epc_completion.c
new file mode 100644
index 0000000..e47828c
--- /dev/null
+++ b/examples/loadables/epc_completion.c
@@ -0,0 +1,954 @@
+#define DISABLE_MALLOC_WRAPPERS
+
+#include <limits.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/ip.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <memory.h>
+#include <arpa/inet.h>
+#include <sched.h>
+#include <unistd.h>
+#include <string.h>
+#include <inttypes.h>
+#include <pthread.h>
+
+#include <config.h>
+#include "loadables.h"
+#include "bashline.h"
+
+#include "jobs.h"
+
+#define MAX_REQUEST 0x1000
+#define MAX_RESPONSE 0x10000
+
+// our functions
+int recv_all(int s, void *buff, size_t len);
+int send_all(int s, void *buff, size_t len);
+int fill_matches_to_buffer(char *to_complete, char **matches, char *buff, size_t buff_len, size_t *out_len);
+
+static pthread_t epc_completion_thread = { 0 };
+static int epc_completion_socket=-1;
+static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
+static char **(*p_bashline_rl_completion)(const char *, int, int);
+
+#include <readline/readline.h>
+#include <readline/rlprivate.h>
+// non exported from readline
+char _rl_find_completion_word (int *fp, int *dp);
+/* int rl_complete_internal (int what_to_do); */
+
+char **try_rl_completions(char *text);
+void _rl_free_match_list (char **matches);
+
+int complete(char *to_complete, char *buff, size_t buff_len, size_t *out_len) {
+ for (int i = 0; i < js.j_jobslots; i++) {
+ if (!INVALID_JOB(i) && RUNNING(i) && IS_FOREGROUND(i)) {
+ out_len = 0;
+ return 0;
+ }
+ }
+ // results should be quoted seperated by spaces
+ char **prog_complete_matches = NULL; //sample_matches;
+
+#ifdef DEBUG
+ printf("completing : %s ...\n", &to_complete);
+#endif
+ prog_complete_matches = try_rl_completions(to_complete);
+
+ int res = fill_matches_to_buffer(to_complete, prog_complete_matches, buff, buff_len, out_len);
+
+ _rl_free_match_list(prog_complete_matches);
+
+ return res;
+}
+
+int fill_matches_to_buffer(char *to_complete, char **matches, char *buff, size_t buff_len, size_t *out_len) {
+ if (!matches) {
+ buff[0] = '\0';
+ *out_len = 0;
+ return 0;
+ }
+ if (matches && matches[1] != NULL) {
+ char *text = matches[0];
+ matches++;
+ char **current = matches;
+ char **next = matches;
+ while (*next != NULL) {
+ if (!strncmp(*next, text, strlen(text))) {
+ if (*current != *next) {
+ *current = *next;
+ }
+ current++;
+ }
+ next++;
+ }
+ *current = *next; // last NULL
+ }
+
+ size_t to_complete_len = strlen(to_complete);
+
+ size_t _out_len = 0;
+ char **p = matches;
+ while (*p != NULL) {
+ char *c = *p;
+ size_t len = 0;
+ for(size_t i = 0; c[i] != 0; ++i) {
+ switch(c[i]) {
+ case '\a':
+ case '\b':
+ case '\f':
+ case '\n':
+ case '\r':
+ case '\t':
+ case '\v':
+ case '\\':
+ case '\'':
+ case '"':
+ case '\?':
+ len+=2;
+ break;
+ default:
+ len++;
+ }
+ }
+ if (to_complete_len > buff_len - _out_len - 3) {
+ continue;
+ }
+ if (len > buff_len - _out_len - 3 - to_complete_len) { // two quotes, and space or \0
+ continue;
+ }
+ buff[_out_len++] = '\"';
+ strcpy(&buff[_out_len], to_complete);
+ _out_len += to_complete_len;
+ for(size_t i = 0; c[i] != 0; ++i) {
+ switch(c[i]) {
+ case '\a': buff[_out_len++] = '\\'; buff[_out_len++] = 'a'; break;
+ case '\b': buff[_out_len++] = '\\'; buff[_out_len++] = 'b'; break;
+ case '\f': buff[_out_len++] = '\\'; buff[_out_len++] = 'f'; break;
+ case '\n': buff[_out_len++] = '\\'; buff[_out_len++] = 'n'; break;
+ case '\r': buff[_out_len++] = '\\'; buff[_out_len++] = 'r'; break;
+ case '\t': buff[_out_len++] = '\\'; buff[_out_len++] = 't'; break;
+ case '\v': buff[_out_len++] = '\\'; buff[_out_len++] = 'v'; break;
+ case '\\': buff[_out_len++] = '\\'; buff[_out_len++] = '\\'; break;
+ case '\'': buff[_out_len++] = '\\'; buff[_out_len++] = '\''; break;
+ case '"': buff[_out_len++] = '\\'; buff[_out_len++] = '"'; break;
+ case '\?': buff[_out_len++] = '\\'; buff[_out_len++] = '?'; break;
+ default: buff[_out_len++] = c[i]; break;
+ }
+ }
+ buff[_out_len++] = '\"';
+ buff[_out_len++] = ' ';
+ p++;
+ }
+ if (_out_len > 0) {
+ buff[--_out_len] = '\0';
+ }
+ *out_len = _out_len;
+
+ return 0;
+}
+
+int handle_parsed_request(int s, uint64_t request_id, char *to_complete) {
+ char response[MAX_RESPONSE] = { 0 };
+#ifdef DEBUG
+ printf("completing %" PRIu64 " : %s ...\n", request_id, to_complete);
+#endif
+ sprintf(response, "(return %" PRIu64 " (", request_id);
+ size_t prefix_len = strlen(response);
+
+ size_t completion_list_len = 0;
+
+ int res = 0;
+ if ((res = complete(to_complete, response + prefix_len,
+ MAX_RESPONSE - prefix_len - strlen("))\n") - 1, &completion_list_len)) < 0) {
+#ifdef DEBUG
+ fprintf(stderr, "error completing\n");
+#endif
+ return res;
+ }
+
+ if (completion_list_len >= MAX_RESPONSE - prefix_len - strlen("))\n")) {
+#ifdef DEBUG
+ fprintf(stderr, "response too long to return\n");
+#endif
+ return -1;
+ }
+ sprintf(response+strlen(response), "))\n");
+
+ // make sure fits %06X
+ if (strlen(response) >= 0x1000000) {
+#ifdef DEBUG
+ fprintf(stderr, "response too long to return\n");
+#endif
+ return -1;
+ }
+
+ char len_buff[7] = { 0 };
+ sprintf(len_buff, "%06X", (int)strlen(response));
+
+ send_all(s, len_buff, 6);
+ send_all(s, response, strlen(response));
+
+ return 0;
+}
+
+int handle_request(int s, char *request, size_t len) {
+ char *init_request = request;
+ /* printf("request `%s`\n", request); */
+ if (init_request[len - 1] != '\n' || init_request[len - 2] != ')' || init_request[len - 3] != '\"') {
+#ifdef DEBUG
+ fprintf(stderr, "invalid request\n");
+#endif
+ return -1;
+ }
+ init_request[len - 3] = '\0';
+
+ if (strncmp(request, "(call ", strlen("(call "))) {
+#ifdef DEBUG
+ fprintf(stderr, "invalid request\n");
+#endif
+ return -1;
+ }
+ request += strlen("(call ");
+ char *method = NULL;
+ long long _request_id = strtoll(request, &method, 10);
+ if (method == request) {
+#ifdef DEBUG
+ fprintf(stderr, "cant find request id\n");
+#endif
+ return -1;
+ }
+
+ if (_request_id == LLONG_MIN || _request_id == LLONG_MAX || _request_id < 0) {
+#ifdef DEBUG
+ perror("invalid request id");
+#endif
+ return -1;
+ }
+
+ uint64_t request_id = (unsigned long long)_request_id;
+
+ if (method - init_request >= MAX_REQUEST - strlen(" complete \"") - strlen("\")\n")) {
+#ifdef DEBUG
+ fprintf(stderr, "invalid request id\n");
+#endif
+ return -1;
+ }
+
+ if (method[0] == '\0') {
+#ifdef DEBUG
+ fprintf(stderr, "cannot find method\n");
+#endif
+ return -1;
+ }
+
+ if (strncmp(method, " complete \"", strlen(" complete \""))) {
+#ifdef DEBUG
+ fprintf(stderr, "invalid request\n");
+#endif
+ return -1;
+ }
+ char *to_complete = method + strlen(" complete \"");
+ if (strlen(to_complete) != len - (to_complete - init_request) - strlen("\")\n")) {
+#ifdef DEBUG
+ fprintf(stderr, "invalid request\n");
+#endif
+ return -1;
+ }
+
+ return handle_parsed_request(s, request_id, to_complete);
+}
+
+
+int completion_client(int s) {
+ int res = 0;
+ while (1) {
+ char len_buff[7] = { 0 };
+ if ((res = recv_all(s, len_buff, 6)) < 0) {
+#ifdef DEBUG
+ perror("error receiving request len");
+#endif
+ return res;
+ }
+ long int len = strtol(len_buff, NULL, 16);
+ if (len < 0 || len > MAX_REQUEST) {
+ // we dont really want to complete long lines
+#ifdef DEBUG
+ fprintf(stderr, "too long line\n");
+#endif
+ return -1;
+ }
+
+ char request[MAX_REQUEST + 1] = { 0 };
+ if ((res = recv_all(s, request, len)) < 0) {
+#ifdef DEBUG
+ perror("error receiveing request");
+#endif
+ return res;
+ }
+
+ if ((res = handle_request(s, request, len)) < 0) {
+#ifdef DEBUG
+ fprintf(stderr, "invalid request\n");
+#endif
+ }
+ fflush(stdout);
+ }
+ return 0;
+}
+
+
+int recv_all(int s, void *buff, size_t len) {
+ size_t received = 0;
+ while (received < len) {
+ int r = recv(s, (void*)((char*)buff + received), len - received, 0);
+ if (r < 0 || r > len - received) {
+ return -1;
+ }
+ received += r;
+ }
+ return 0;
+}
+int send_all(int s, void *buff, size_t len) {
+ size_t sent = 0;
+ while (sent < len) {
+ int r = send(s, (void*)((char*)buff + sent), len - sent, 0);
+ if (r < 0 || r > len - sent) {
+ return -1;
+ }
+ sent += r;
+ }
+ return 0;
+}
+
+
+int epc_completion_main() {
+ int s = -1;
+ pthread_mutex_lock(&lock);
+ s = epc_completion_socket;
+ pthread_mutex_unlock(&lock);
+ if (s == -1) return -1;
+
+ int res = completion_client(s);
+ close(s);
+
+ return res;
+}
+
+void *epc_completion(void *ignored) {
+#ifdef DEBUG
+ printf ("starting thread\n");
+#endif
+ (void)ignored;
+ (void)epc_completion_main();
+ return NULL;
+}
+
+int epc_completion_builtin (WORD_LIST *list)
+{
+ // the command itself does nothing
+ return (EXECUTION_SUCCESS);
+}
+
+int epc_completion_builtin_load (char *ignored)
+{
+ (void)ignored;
+
+ if (p_bashline_rl_completion == NULL) {
+ initialize_readline();
+ p_bashline_rl_completion = rl_attempted_completion_function;
+ }
+
+ char *epc_port_str = getenv("EPC_COMPLETION_SERVER_PORT");
+ if (epc_port_str == NULL) {
+#ifdef DEBUG
+ fprintf(stderr, "missing \"EPC_COMPLETION_SERVER_PORT\"\n");
+#endif
+ return 0;
+ }
+
+ long int epc_port = strtol(epc_port_str, NULL, 10);
+ if (epc_port == LONG_MIN || epc_port == LONG_MAX) {
+#ifdef DEBUG
+ perror("invalid port");
+#endif
+ return 0;
+ }
+ if (epc_port < 1000) {
+#ifdef DEBUG
+ fprintf(stderr, "invalid root port\n");
+#endif
+ return 0;
+ }
+ if (epc_port > ((1 << 16) - 1)) {
+#ifdef DEBUG
+ fprintf(stderr, "not a valid port\n");
+#endif
+ return 0;
+ }
+
+ int s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
+ if (s < 0) {
+#ifdef DEBUG
+ perror("cant create socket");
+#endif
+ return 0;
+ }
+
+ struct sockaddr_in server_addr;
+ memset(&server_addr, 0, sizeof(server_addr));
+ server_addr.sin_family = AF_INET;
+ server_addr.sin_port = htons(epc_port);
+ inet_aton("127.0.0.1", &server_addr.sin_addr);
+
+ if (connect(s, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
+#ifdef DEBUG
+ perror("connect error");
+#endif
+ return 0;
+ }
+
+#ifdef DEBUG
+ printf("connected to epc completion server\n");
+#endif
+ pthread_mutex_lock(&lock);
+ epc_completion_socket=s;
+ pthread_mutex_unlock(&lock);
+
+#ifdef DEBUG
+ printf("starting epc_completion thread\n");
+#endif
+ if (pthread_create(&epc_completion_thread, NULL, &epc_completion, NULL) != 0) {
+#ifdef DEBUG
+ perror("error starting epc_completion thread");
+#endif
+ fflush(stdout);
+ pthread_mutex_lock(&lock);
+ epc_completion_socket=-1;
+ pthread_mutex_unlock(&lock);
+
+ shutdown(s, SHUT_RDWR);
+ (void)sched_yield();
+ close(s);
+
+ return 0;
+ }
+#ifdef DEBUG
+ printf("epc_completion thread started\n");
+#endif
+ fflush(stdout);
+ return 1;
+}
+
+void epc_completion_builtin_unload (char *ignored)
+{
+ (void)ignored;
+
+ pthread_mutex_lock(&lock);
+ int s = epc_completion_socket;
+ pthread_mutex_unlock(&lock);
+
+ if (s >= 0) {
+ shutdown(s, SHUT_RDWR);
+ (void)sched_yield();
+ close(s);
+
+ void *ret = NULL;
+ pthread_join(epc_completion_thread, &ret);
+
+ pthread_mutex_lock(&lock);
+ epc_completion_socket = -1;
+ pthread_mutex_unlock(&lock);
+ }
+#ifdef DEBUG
+ printf("epc_completion builtin unloaded\n");
+#endif
+ fflush(stdout);
+}
+
+char *epc_completion_doc[] = {
+ "epc_completion builtin.",
+ "",
+ "epc_completion bash loadable.\nSee Also: https://github.com/otadmor/emacs-conf/blob/master/emacs.d/lisp/completion-epc.el",
+ (char *)NULL
+};
+
+struct builtin epc_completion_struct = {
+ "epc_completion",
+ epc_completion_builtin,
+ BUILTIN_ENABLED,
+ epc_completion_doc,
+ "epc_completion",
+ 0
+};
+
+
+/* Find the common prefix of the list of matches, and put it into
+ matches[0]. */
+static int
+compute_lcd_of_matches (char **match_list, int matches, const char *text)
+{
+ register int i, c1, c2, si;
+ int low; /* Count of max-matched characters. */
+ int lx;
+ char *dtext; /* dequoted TEXT, if needed */
+#if defined (HANDLE_MULTIBYTE)
+ int v;
+ size_t v1, v2;
+ mbstate_t ps1, ps2;
+ wchar_t wc1, wc2;
+#endif
+
+ /* If only one match, just use that. Otherwise, compare each
+ member of the list with the next, finding out where they
+ stop matching. */
+ if (matches == 1)
+ {
+ match_list[0] = match_list[1];
+ match_list[1] = (char *)NULL;
+ return 1;
+ }
+
+ for (i = 1, low = 100000; i < matches; i++)
+ {
+#if defined (HANDLE_MULTIBYTE)
+ if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+ {
+ memset (&ps1, 0, sizeof (mbstate_t));
+ memset (&ps2, 0, sizeof (mbstate_t));
+ }
+#endif
+ for (si = 0; (c1 = match_list[i][si]) && (c2 = match_list[i + 1][si]); si++)
+ {
+ if (_rl_completion_case_fold)
+ {
+ c1 = _rl_to_lower (c1);
+ c2 = _rl_to_lower (c2);
+ }
+#if defined (HANDLE_MULTIBYTE)
+ if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
+ {
+ v1 = mbrtowc(&wc1, match_list[i]+si, strlen (match_list[i]+si), &ps1);
+ v2 = mbrtowc (&wc2, match_list[i+1]+si, strlen (match_list[i+1]+si), &ps2);
+ if (MB_INVALIDCH (v1) || MB_INVALIDCH (v2))
+ {
+ if (c1 != c2) /* do byte comparison */
+ break;
+ continue;
+ }
+ if (_rl_completion_case_fold)
+ {
+ wc1 = towlower (wc1);
+ wc2 = towlower (wc2);
+ }
+ if (wc1 != wc2)
+ break;
+ else if (v1 > 1)
+ si += v1 - 1;
+ }
+ else
+#endif
+ if (c1 != c2)
+ break;
+ }
+
+ if (low > si)
+ low = si;
+ }
+
+ /* If there were multiple matches, but none matched up to even the
+ first character, and the user typed something, use that as the
+ value of matches[0]. */
+ if (low == 0 && text && *text)
+ {
+ match_list[0] = (char *)malloc (strlen (text) + 1);
+ strcpy (match_list[0], text);
+ }
+ else
+ {
+ match_list[0] = (char *)malloc (low + 1);
+
+ /* XXX - this might need changes in the presence of multibyte chars */
+
+ /* If we are ignoring case, try to preserve the case of the string
+ the user typed in the face of multiple matches differing in case. */
+ if (_rl_completion_case_fold)
+ {
+ /* We're making an assumption here:
+ IF we're completing filenames AND
+ the application has defined a filename dequoting function AND
+ we found a quote character AND
+ the application has requested filename quoting
+ THEN
+ we assume that TEXT was dequoted before checking against
+ the file system and needs to be dequoted here before we
+ check against the list of matches
+ FI */
+ dtext = (char *)NULL;
+ if (rl_filename_completion_desired &&
+ rl_filename_dequoting_function &&
+ rl_completion_found_quote &&
+ rl_filename_quoting_desired)
+ {
+ dtext = (*rl_filename_dequoting_function) ((char *)text, rl_completion_quote_character);
+ text = dtext;
+ }
+
+ /* sort the list to get consistent answers. */
+ if (rl_sort_completion_matches)
+ qsort (match_list+1, matches, sizeof(char *), (QSFUNC *)_rl_qsort_string_compare);
+
+ si = strlen (text);
+ lx = (si <= low) ? si : low; /* check shorter of text and matches */
+ /* Try to preserve the case of what the user typed in the presence of
+ multiple matches: check each match for something that matches
+ what the user typed taking case into account; use it up to common
+ length of matches if one is found. If not, just use first match. */
+ for (i = 1; i <= matches; i++)
+ if (strncmp (match_list[i], text, lx) == 0)
+ {
+ strncpy (match_list[0], match_list[i], low);
+ break;
+ }
+ /* no casematch, use first entry */
+ if (i > matches)
+ strncpy (match_list[0], match_list[1], low);
+
+ FREE (dtext);
+ }
+ else
+ strncpy (match_list[0], match_list[1], low);
+
+ match_list[0][low] = '\0';
+ }
+
+ return matches;
+}
+
+char *
+_savestring (const char *s)
+{
+ char *ret;
+
+ ret = (char *)malloc (strlen (s) + 1);
+ strcpy (ret, s);
+ return ret;
+}
+
+
+/* Filter out duplicates in MATCHES. This frees up the strings in
+ MATCHES. */
+static char **
+remove_duplicate_matches (char **matches)
+{
+ char *lowest_common;
+ int i, j, newlen;
+ char dead_slot;
+ char **temp_array;
+
+ /* Sort the items. */
+ for (i = 0; matches[i]; i++)
+ ;
+
+ /* Sort the array without matches[0], since we need it to
+ stay in place no matter what. */
+ if (i && rl_sort_completion_matches)
+ qsort (matches+1, i-1, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
+
+ /* Remember the lowest common denominator for it may be unique. */
+ lowest_common = _savestring (matches[0]);
+
+ for (i = newlen = 0; matches[i + 1]; i++)
+ {
+ if (strcmp (matches[i], matches[i + 1]) == 0)
+ {
+ FREE (matches[i]);
+ matches[i] = (char *)&dead_slot;
+ }
+ else
+ newlen++;
+ }
+
+ /* We have marked all the dead slots with (char *)&dead_slot.
+ Copy all the non-dead entries into a new array. */
+ temp_array = (char **)malloc ((3 + newlen) * sizeof (char *));
+ for (i = j = 1; matches[i]; i++)
+ {
+ if (matches[i] != (char *)&dead_slot)
+ temp_array[j++] = matches[i];
+ }
+ temp_array[j] = (char *)NULL;
+
+ if (matches[0] != (char *)&dead_slot)
+ FREE (matches[0]);
+
+ /* Place the lowest common denominator back in [0]. */
+ temp_array[0] = lowest_common;
+
+ /* If there is one string left, and it is identical to the
+ lowest common denominator, then the LCD is the string to
+ insert. */
+ if (j == 2 && strcmp (temp_array[0], temp_array[1]) == 0)
+ {
+ FREE (temp_array[1]);
+ temp_array[1] = (char *)NULL;
+ }
+ return (temp_array);
+}
+
+static int
+postprocess_matches (char ***matchesp, int matching_filenames)
+{
+ char *t, **matches, **temp_matches;
+ int nmatch, i;
+
+ matches = *matchesp;
+
+ if (matches == 0)
+ return 0;
+
+ /* It seems to me that in all the cases we handle we would like
+ to ignore duplicate possibilities. Scan for the text to
+ insert being identical to the other completions. */
+ if (rl_ignore_completion_duplicates)
+ {
+ temp_matches = remove_duplicate_matches (matches);
+ FREE (matches);
+ matches = temp_matches;
+ }
+
+ /* If we are matching filenames, then here is our chance to
+ do clever processing by re-examining the list. Call the
+ ignore function with the array as a parameter. It can
+ munge the array, deleting matches as it desires. */
+ if (rl_ignore_some_completions_function && matching_filenames)
+ {
+ for (nmatch = 1; matches[nmatch]; nmatch++)
+ ;
+ (void)(*rl_ignore_some_completions_function) (matches);
+ if (matches == 0 || matches[0] == 0)
+ {
+ FREE (matches);
+ *matchesp = (char **)0;
+ return 0;
+ }
+ else
+ {
+ /* If we removed some matches, recompute the common prefix. */
+ for (i = 1; matches[i]; i++)
+ ;
+ if (i > 1 && i < nmatch)
+ {
+ t = matches[0];
+ /* compute_lcd_of_matches (matches, i - 1, t); */
+ FREE (t);
+ }
+ }
+ }
+
+ *matchesp = matches;
+ return (1);
+}
+
+/* Set default values for readline word completion. These are the variables
+ that application completion functions can change or inspect. */
+static void
+set_completion_defaults (int what_to_do)
+{
+ /* Only the completion entry function can change these. */
+ rl_filename_completion_desired = 0;
+ rl_filename_quoting_desired = 1;
+ rl_completion_type = what_to_do;
+ rl_completion_suppress_append = rl_completion_suppress_quote = 0;
+ rl_completion_append_character = '\n';
+
+ /* The completion entry function may optionally change this. */
+ rl_completion_mark_symlink_dirs = _rl_complete_mark_symlink_dirs;
+ rl_completion_entry_function = NULL;
+
+ /* Reset private state. */
+ /* _rl_complete_display_matches_interrupt = 0; */
+
+ rl_ignore_completion_duplicates = 1;
+}
+
+static char **
+gen_completion_matches (char *text, int start, int end, rl_compentry_func_t *our_func, int found_quote, int quote_char)
+{
+ char **matches;
+
+ rl_completion_found_quote = found_quote;
+ rl_completion_quote_character = quote_char;
+
+ /* If the user wants to TRY to complete, but then wants to give
+ up and use the default completion function, they set the
+ variable rl_attempted_completion_function. */
+ if (rl_attempted_completion_function)
+ {
+ matches = (*rl_attempted_completion_function) (text, start, end);
+ /* if (RL_SIG_RECEIVED()) */
+ /* { */
+ /* _rl_free_match_list (matches); */
+ /* matches = 0; */
+ /* RL_CHECK_SIGNALS (); */
+ /* } */
+
+ if (matches || rl_attempted_completion_over)
+ {
+ rl_attempted_completion_over = 0;
+ return (matches);
+ }
+ }
+
+ /* XXX -- filename dequoting moved into rl_filename_completion_function */
+
+ /* rl_completion_matches will check for signals as well to avoid a long
+ delay while reading a directory. */
+ matches = rl_completion_matches (text, our_func);
+ /* if (RL_SIG_RECEIVED()) */
+ /* { */
+ /* _rl_free_match_list (matches); */
+ /* matches = 0; */
+ /* RL_CHECK_SIGNALS (); */
+ /* } */
+ return matches;
+}
+
+/* Compare a possibly-quoted filename TEXT from the line buffer and a possible
+ MATCH that is the product of filename completion, which acts on the dequoted
+ text. */
+static int
+compare_match (char *text, const char *match)
+{
+ char *temp;
+ int r;
+
+ if (rl_filename_completion_desired && rl_filename_quoting_desired &&
+ rl_completion_found_quote && rl_filename_dequoting_function)
+ {
+ temp = (*rl_filename_dequoting_function) (text, rl_completion_quote_character);
+ r = strcmp (temp, match);
+ free (temp);
+ return r;
+ }
+ return (strcmp (text, match));
+}
+
+// copied from rl_complete_internal
+char **try_rl_completions(char *text) {
+ int start = 0;
+ int end = strlen(text);
+ char *orig_text = text;
+ char **matches;
+ rl_compentry_func_t *our_func;
+ int delimiter, found_quote, i, nontrivial_lcd;
+ char *saved_line_buffer;
+ char quote_char;
+ int tlen, mlen; //, saved_last_completion_failed;
+
+ RL_SETSTATE(RL_STATE_COMPLETING);
+
+ /* saved_last_completion_failed = last_completion_failed; */
+
+ set_completion_defaults ('*');
+
+ /* saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL; */
+ saved_line_buffer = rl_line_buffer;
+ int backup_rl_point = rl_point;
+ rl_point = 0;
+ rl_line_buffer = savestring(text);
+ rl_point = end;
+
+ our_func = rl_completion_entry_function
+ ? rl_completion_entry_function
+ : rl_filename_completion_function;
+ /* We now look backwards for the start of a filename/variable word. */
+ found_quote = delimiter = 0;
+ quote_char = '\0';
+
+ /* end = rl_point; */
+
+ if (end)
+ /* This (possibly) changes rl_point. If it returns a non-zero char,
+ we know we have an open quote. */
+ quote_char = _rl_find_completion_word (&found_quote, &delimiter);
+
+ start = rl_point;
+ rl_point = end;
+
+ /* text = _savestring (&text[start]); text[end] = '\0'; */
+
+ orig_text[start] = '\0';
+ text = rl_copy_text (start, end);
+
+ matches = gen_completion_matches (text, start, end, our_func, found_quote, quote_char);
+ /* nontrivial_lcd is set if the common prefix adds something to the word
+ being completed. */
+ nontrivial_lcd = matches && compare_match (text, matches[0]) != 0;
+ /* if (what_to_do == '!' || what_to_do == '@') */
+ /* tlen = strlen (text); */
+ FREE (text);
+
+ if (matches == 0)
+ {
+ /* rl_ding (); */
+ char *rl_line_buffer_backup = rl_line_buffer;
+ rl_line_buffer = saved_line_buffer;
+ rl_point = backup_rl_point;
+ FREE(rl_line_buffer_backup);
+ rl_line_buffer_backup = NULL;
+ /* FREE (saved_line_buffer); */
+ /* completion_changed_buffer = 0; */
+ /* last_completion_failed = 1; */
+ RL_UNSETSTATE(RL_STATE_COMPLETING);
+ _rl_reset_completion_state ();
+ return (0);
+ }
+
+ /* If we are matching filenames, the attempted completion function will
+ have set rl_filename_completion_desired to a non-zero value. The basic
+ rl_filename_completion_function does this. */
+ i = rl_filename_completion_desired;
+
+ if (postprocess_matches (&matches, i) == 0)
+ {
+ /* rl_ding (); */
+ char *rl_line_buffer_backup = rl_line_buffer;
+ rl_line_buffer = saved_line_buffer;
+ rl_point = backup_rl_point;
+ FREE(rl_line_buffer_backup);
+ rl_line_buffer_backup = NULL;
+ /* FREE (saved_line_buffer); */
+ /* completion_changed_buffer = 0; */
+ /* last_completion_failed = 1; */
+ RL_UNSETSTATE(RL_STATE_COMPLETING);
+ _rl_reset_completion_state ();
+ return (0);
+ }
+
+ /* if (matches && matches[0] && *matches[0]) */
+ /* last_completion_failed = 0; */
+
+ /* insert_all_matches (matches, start, "e_char); */
+
+ /* /\* Check to see if the line has changed through all of this manipulation. *\/ */
+ if (saved_line_buffer)
+ {
+ char *rl_line_buffer_backup = rl_line_buffer;
+ rl_line_buffer = saved_line_buffer;
+ rl_point = backup_rl_point;
+ FREE(rl_line_buffer_backup);
+ rl_line_buffer_backup = NULL;
+ /* completion_changed_buffer = strcmp (rl_line_buffer, saved_line_buffer) != 0; */
+ /* FREE (saved_line_buffer); */
+ }
+
+ RL_UNSETSTATE(RL_STATE_COMPLETING);
+ _rl_reset_completion_state ();
+
+ /* RL_CHECK_SIGNALS (); */
+ return matches;
+
+}
diff --git a/examples/loadables/Makefile.in b/examples/loadables/Makefile.in
index be46121..e1c919f 100644
--- a/examples/loadables/Makefile.in
+++ b/examples/loadables/Makefile.in
@@ -322,3 +322,8 @@ stat.o: stat.c
fdflags.o: fdflags.c
seq.o: seq.c
asort.o: asort.c
+
+epc_completion: epc_completion.o
+ $(SHOBJ_LD) $(SHOBJ_LDFLAGS) $(SHOBJ_XLDFLAGS) -pthread -o $@ epc_completion.o $(SHOBJ_LIBS)
+
+epc_completion.o: epc_completion.c