~ubuntu-branches/ubuntu/maverick/mysql-5.1/maverick-proposed

« back to all changes in this revision

Viewing changes to cmd-line-utils/libedit/readline.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-02-22 14:16:05 UTC
  • mto: This revision was merged to the branch mainline in revision 20.
  • Revision ID: package-import@ubuntu.com-20120222141605-nxlu9yzc6attylc2
Tags: upstream-5.1.61
ImportĀ upstreamĀ versionĀ 5.1.61

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*      $NetBSD: readline.c,v 1.78 2009/02/05 19:15:26 christos Exp $   */
 
1
/*      $NetBSD: readline.c,v 1.99 2011/08/16 16:25:15 christos Exp $   */
2
2
 
3
3
/*-
4
4
 * Copyright (c) 1997 The NetBSD Foundation, Inc.
69
69
/* readline compatibility stuff - look at readline sources/documentation */
70
70
/* to see what these variables mean */
71
71
const char *rl_library_version = "EditLine wrapper";
 
72
int rl_readline_version = RL_READLINE_VERSION;
72
73
static char empty[] = { '\0' };
73
74
static char expand_chars[] = { ' ', '\t', '\n', '=', '(', '\0' };
74
75
static char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@', '$',
117
118
VFunction *rl_completion_display_matches_hook = NULL;
118
119
VFunction *rl_prep_term_function = (VFunction *)rl_prep_terminal;
119
120
VFunction *rl_deprep_term_function = (VFunction *)rl_deprep_terminal;
 
121
KEYMAP_ENTRY_ARRAY emacs_meta_keymap;
120
122
 
121
123
/*
122
124
 * The current prompt string.
174
176
_get_prompt(EditLine *el __attribute__((__unused__)))
175
177
{
176
178
        rl_already_prompted = 1;
177
 
        return (rl_prompt);
 
179
        return rl_prompt;
178
180
}
179
181
 
180
182
 
188
190
        static HIST_ENTRY rl_he;
189
191
 
190
192
        if (history(h, &ev, op) != 0)
191
 
                return (HIST_ENTRY *) NULL;
 
193
                return NULL;
192
194
 
193
195
        rl_he.line = ev.str;
194
196
        rl_he.data = NULL;
195
197
 
196
 
        return (&rl_he);
 
198
        return &rl_he;
197
199
}
198
200
 
199
201
 
202
204
 */
203
205
static int
204
206
/*ARGSUSED*/
205
 
_getc_function(EditLine *el, char *c)
 
207
_getc_function(EditLine *el __attribute__((__unused__)), char *c)
206
208
{
207
209
        int i;
208
210
 
209
211
        i = (*rl_getc_function)(NULL);
210
212
        if (i == -1)
211
213
                return 0;
212
 
        *c = i;
 
214
        *c = (char)i;
213
215
        return 1;
214
216
}
215
217
 
 
218
static void
 
219
_resize_fun(EditLine *el, void *a)
 
220
{
 
221
        const LineInfo *li;
 
222
        char **ap = a;
 
223
 
 
224
        li = el_line(el);
 
225
        /* a cheesy way to get rid of const cast. */
 
226
        *ap = memchr(li->buffer, *li->buffer, (size_t)1);
 
227
}
 
228
 
 
229
static const char *
 
230
_default_history_file(void)
 
231
{
 
232
        struct passwd *p;
 
233
        static char path[PATH_MAX];
 
234
 
 
235
        if (*path)
 
236
                return path;
 
237
        if ((p = getpwuid(getuid())) == NULL)
 
238
                return NULL;
 
239
        (void)snprintf(path, sizeof(path), "%s/.history", p->pw_dir);
 
240
        return path;
 
241
}
216
242
 
217
243
/*
218
244
 * READLINE compatibility stuff
224
250
int
225
251
rl_set_prompt(const char *prompt)
226
252
{
 
253
        char *p;
 
254
 
227
255
        if (!prompt)
228
256
                prompt = "";
229
257
        if (rl_prompt != NULL && strcmp(rl_prompt, prompt) == 0) 
230
258
                return 0;
231
259
        if (rl_prompt)
232
 
                free(rl_prompt);
 
260
                el_free(rl_prompt);
233
261
        rl_prompt = strdup(prompt);
234
 
        return rl_prompt == NULL ? -1 : 0;
 
262
        if (rl_prompt == NULL)
 
263
                return -1;
 
264
 
 
265
        while ((p = strchr(rl_prompt, RL_PROMPT_END_IGNORE)) != NULL)
 
266
                *p = RL_PROMPT_START_IGNORE;
 
267
 
 
268
        return 0;
235
269
}
236
270
 
237
271
/*
241
275
rl_initialize(void)
242
276
{
243
277
        HistEvent ev;
244
 
        const LineInfo *li;
245
278
        int editmode = 1;
246
279
        struct termios t;
247
280
 
268
301
 
269
302
        h = history_init();
270
303
        if (!e || !h)
271
 
                return (-1);
 
304
                return -1;
272
305
 
273
306
        history(h, &ev, H_SETSIZE, INT_MAX);    /* unlimited */
274
307
        history_length = 0;
275
308
        max_input_history = INT_MAX;
276
309
        el_set(e, EL_HIST, history, h);
277
310
 
 
311
        /* Setup resize function */
 
312
        el_set(e, EL_RESIZE, _resize_fun, &rl_line_buffer);
 
313
 
278
314
        /* setup getc function if valid */
279
315
        if (rl_getc_function)
280
316
                el_set(e, EL_GETCFN, _getc_function);
285
321
                el_end(e);
286
322
                return -1;
287
323
        }
288
 
        el_set(e, EL_PROMPT, _get_prompt);
 
324
        el_set(e, EL_PROMPT, _get_prompt, RL_PROMPT_START_IGNORE);
289
325
        el_set(e, EL_SIGNAL, rl_catch_signals);
290
326
 
291
327
        /* set default mode to "emacs"-style and read setting afterwards */
312
348
            "ReadLine compatible suspend function",
313
349
            _el_rl_tstp);
314
350
        el_set(e, EL_BIND, "^Z", "rl_tstp", NULL);
315
 
                
 
351
 
 
352
        /*
 
353
         * Set some readline compatible key-bindings.
 
354
         */
 
355
        el_set(e, EL_BIND, "^R", "em-inc-search-prev", NULL);
 
356
 
 
357
        /*
 
358
         * Allow the use of Home/End keys.
 
359
         */
 
360
        el_set(e, EL_BIND, "\\e[1~", "ed-move-to-beg", NULL);
 
361
        el_set(e, EL_BIND, "\\e[4~", "ed-move-to-end", NULL);
 
362
        el_set(e, EL_BIND, "\\e[7~", "ed-move-to-beg", NULL);
 
363
        el_set(e, EL_BIND, "\\e[8~", "ed-move-to-end", NULL);
 
364
        el_set(e, EL_BIND, "\\e[H", "ed-move-to-beg", NULL);
 
365
        el_set(e, EL_BIND, "\\e[F", "ed-move-to-end", NULL);
 
366
 
 
367
        /*
 
368
         * Allow the use of the Delete/Insert keys.
 
369
         */
 
370
        el_set(e, EL_BIND, "\\e[3~", "ed-delete-next-char", NULL);
 
371
        el_set(e, EL_BIND, "\\e[2~", "ed-quoted-insert", NULL);
 
372
 
 
373
        /*
 
374
         * Ctrl-left-arrow and Ctrl-right-arrow for word moving.
 
375
         */
 
376
        el_set(e, EL_BIND, "\\e[1;5C", "em-next-word", NULL);
 
377
        el_set(e, EL_BIND, "\\e[1;5D", "ed-prev-word", NULL);
 
378
        el_set(e, EL_BIND, "\\e[5C", "em-next-word", NULL);
 
379
        el_set(e, EL_BIND, "\\e[5D", "ed-prev-word", NULL);
 
380
        el_set(e, EL_BIND, "\\e\\e[C", "em-next-word", NULL);
 
381
        el_set(e, EL_BIND, "\\e\\e[D", "ed-prev-word", NULL);
 
382
 
316
383
        /* read settings from configuration file */
317
384
        el_source(e, NULL);
318
385
 
320
387
         * Unfortunately, some applications really do use rl_point
321
388
         * and rl_line_buffer directly.
322
389
         */
323
 
        li = el_line(e);
324
 
        /* a cheesy way to get rid of const cast. */
325
 
        rl_line_buffer = memchr(li->buffer, *li->buffer, 1);
 
390
        _resize_fun(e, &rl_line_buffer);
326
391
        _rl_update_pos();
327
392
 
328
393
        if (rl_startup_hook)
329
394
                (*rl_startup_hook)(NULL, 0);
330
395
 
331
 
        return (0);
 
396
        return 0;
332
397
}
333
398
 
334
399
 
437
502
                } else
438
503
                        s++;
439
504
        }
440
 
        r = result = malloc(len + 1);
 
505
        r = result = el_malloc((len + 1) * sizeof(*r));
441
506
        if (result == NULL)
442
507
                return NULL;
443
508
        s = str;
448
513
                        s += what_len;
449
514
                        if (!globally) {
450
515
                                (void)strcpy(r, s);
451
 
                                return(result);
 
516
                                return result;
452
517
                        }
453
518
                } else
454
519
                        *r++ = *s++;
455
520
        }
456
521
        *r = '\0';
457
 
        return(result);
 
522
        return result;
458
523
}
459
524
 
460
525
static  char    *last_search_pat;       /* last !?pat[?] search pattern */
471
536
 
472
537
        idx = *cindex;
473
538
        if (cmd[idx++] != history_expansion_char)
474
 
                return(NULL);
 
539
                return NULL;
475
540
 
476
541
        /* find out which event to take */
477
542
        if (cmd[idx] == history_expansion_char || cmd[idx] == '\0') {
478
543
                if (history(h, &ev, H_FIRST) != 0)
479
 
                        return(NULL);
 
544
                        return NULL;
480
545
                *cindex = cmd[idx]? (idx + 1):idx;
481
 
                return(ev.str);
 
546
                return ev.str;
482
547
        }
483
548
        sign = 0;
484
549
        if (cmd[idx] == '-') {
498
563
                        num = history_length - num + 1;
499
564
 
500
565
                if (!(rl_he = history_get(num)))
501
 
                        return(NULL);
 
566
                        return NULL;
502
567
 
503
568
                *cindex = idx;
504
 
                return(rl_he->line);
 
569
                return rl_he->line;
505
570
        }
506
571
        sub = 0;
507
572
        if (cmd[idx] == '?') {
519
584
                        break;
520
585
                idx++;
521
586
        }
522
 
        len = idx - begin;
 
587
        len = (size_t)idx - (size_t)begin;
523
588
        if (sub && cmd[idx] == '?')
524
589
                idx++;
525
590
        if (sub && len == 0 && last_search_pat && *last_search_pat)
526
591
                pat = last_search_pat;
527
592
        else if (len == 0)
528
 
                return(NULL);
 
593
                return NULL;
529
594
        else {
530
 
                if ((pat = malloc(len + 1)) == NULL)
 
595
                if ((pat = el_malloc((len + 1) * sizeof(*pat))) == NULL)
531
596
                        return NULL;
532
597
                (void)strncpy(pat, cmd + begin, len);
533
598
                pat[len] = '\0';
535
600
 
536
601
        if (history(h, &ev, H_CURR) != 0) {
537
602
                if (pat != last_search_pat)
538
 
                        free(pat);
539
 
                return (NULL);
 
603
                        el_free(pat);
 
604
                return NULL;
540
605
        }
541
606
        num = ev.num;
542
607
 
543
608
        if (sub) {
544
609
                if (pat != last_search_pat) {
545
610
                        if (last_search_pat)
546
 
                                free(last_search_pat);
 
611
                                el_free(last_search_pat);
547
612
                        last_search_pat = pat;
548
613
                }
549
614
                ret = history_search(pat, -1);
555
620
                history(h, &ev, H_FIRST);
556
621
                (void)fprintf(rl_outstream, "%s: Event not found\n", pat);
557
622
                if (pat != last_search_pat)
558
 
                        free(pat);
559
 
                return(NULL);
 
623
                        el_free(pat);
 
624
                return NULL;
560
625
        }
561
626
 
562
627
        if (sub && len) {
563
628
                if (last_search_match && last_search_match != pat)
564
 
                        free(last_search_match);
 
629
                        el_free(last_search_match);
565
630
                last_search_match = pat;
566
631
        }
567
632
 
568
633
        if (pat != last_search_pat)
569
 
                free(pat);
 
634
                el_free(pat);
570
635
 
571
636
        if (history(h, &ev, H_CURR) != 0)
572
 
                return(NULL);
 
637
                return NULL;
573
638
        *cindex = idx;
574
639
        rptr = ev.str;
575
640
 
621
686
        } else {
622
687
                if (command[offs + 1] == '#') {
623
688
                        /* use command so far */
624
 
                        if ((aptr = malloc(offs + 1)) == NULL)
 
689
                        if ((aptr = el_malloc((offs + 1) * sizeof(*aptr)))
 
690
                            == NULL)
625
691
                                return -1;
626
692
                        (void)strncpy(aptr, command, offs);
627
693
                        aptr[offs] = '\0';
632
698
                        qchar = (offs > 0 && command[offs - 1] == '"')? '"':0;
633
699
                        ptr = get_history_event(command + offs, &idx, qchar);
634
700
                }
635
 
                has_mods = command[offs + idx] == ':';
 
701
                has_mods = command[offs + (size_t)idx] == ':';
636
702
        }
637
703
 
638
704
        if (ptr == NULL && aptr == NULL)
639
 
                return(-1);
 
705
                return -1;
640
706
 
641
707
        if (!has_mods) {
642
 
                *result = strdup(aptr? aptr : ptr);
 
708
                *result = strdup(aptr ? aptr : ptr);
643
709
                if (aptr)
644
 
                        free(aptr);
645
 
                return(1);
 
710
                        el_free(aptr);
 
711
                if (*result == NULL)
 
712
                        return -1;
 
713
                return 1;
646
714
        }
647
715
 
648
716
        cmd = command + offs + idx + 1;
687
755
                        (void)fprintf(rl_outstream, "%s: Bad word specifier",
688
756
                            command + offs + idx);
689
757
                        if (aptr)
690
 
                                free(aptr);
691
 
                        return(-1);
 
758
                                el_free(aptr);
 
759
                        return -1;
692
760
                }
693
761
        } else
694
762
                tmp = strdup(aptr? aptr:ptr);
695
763
 
696
764
        if (aptr)
697
 
                free(aptr);
 
765
                el_free(aptr);
698
766
 
699
767
        if (*cmd == '\0' || ((size_t)(cmd - (command + offs)) >= cmdlen)) {
700
768
                *result = tmp;
701
 
                return(1);
 
769
                return 1;
702
770
        }
703
771
 
704
772
        for (; *cmd; cmd++) {
710
778
                } else if (*cmd == 't') {       /* remove leading path */
711
779
                        if ((aptr = strrchr(tmp, '/')) != NULL) {
712
780
                                aptr = strdup(aptr + 1);
713
 
                                free(tmp);
 
781
                                el_free(tmp);
714
782
                                tmp = aptr;
715
783
                        }
716
784
                } else if (*cmd == 'r') {       /* remove trailing suffix */
719
787
                } else if (*cmd == 'e') {       /* remove all but suffix */
720
788
                        if ((aptr = strrchr(tmp, '.')) != NULL) {
721
789
                                aptr = strdup(aptr);
722
 
                                free(tmp);
 
790
                                el_free(tmp);
723
791
                                tmp = aptr;
724
792
                        }
725
793
                } else if (*cmd == 'p')         /* print only */
736
804
                        else if (*cmd == 's') {
737
805
                                delim = *(++cmd), cmd++;
738
806
                                size = 16;
739
 
                                what = realloc(from, size);
 
807
                                what = el_realloc(from, size * sizeof(*what));
740
808
                                if (what == NULL) {
741
 
                                        free(from);
742
 
                                        free(tmp);
 
809
                                        el_free(from);
 
810
                                        el_free(tmp);
743
811
                                        return 0;
744
812
                                }
745
813
                                len = 0;
748
816
                                                cmd++;
749
817
                                        if (len >= size) {
750
818
                                                char *nwhat;
751
 
                                                nwhat = realloc(what,
752
 
                                                                (size <<= 1));
 
819
                                                nwhat = el_realloc(what,
 
820
                                                    (size <<= 1) *
 
821
                                                    sizeof(*nwhat));
753
822
                                                if (nwhat == NULL) {
754
 
                                                        free(what);
755
 
                                                        free(tmp);
 
823
                                                        el_free(what);
 
824
                                                        el_free(tmp);
756
825
                                                        return 0;
757
826
                                                }
758
827
                                                what = nwhat;
762
831
                                what[len] = '\0';
763
832
                                from = what;
764
833
                                if (*what == '\0') {
765
 
                                        free(what);
 
834
                                        el_free(what);
766
835
                                        if (search) {
767
836
                                                from = strdup(search);
768
837
                                                if (from == NULL) {
769
 
                                                        free(tmp);
 
838
                                                        el_free(tmp);
770
839
                                                        return 0;
771
840
                                                }
772
841
                                        } else {
773
842
                                                from = NULL;
774
 
                                                free(tmp);
775
 
                                                return (-1);
 
843
                                                el_free(tmp);
 
844
                                                return -1;
776
845
                                        }
777
846
                                }
778
847
                                cmd++;  /* shift after delim */
780
849
                                        continue;
781
850
 
782
851
                                size = 16;
783
 
                                with = realloc(to, size);
 
852
                                with = el_realloc(to, size * sizeof(*with));
784
853
                                if (with == NULL) {
785
 
                                        free(to);
786
 
                                        free(tmp);
 
854
                                        el_free(to);
 
855
                                        el_free(tmp);
787
856
                                        return -1;
788
857
                                }
789
858
                                len = 0;
792
861
                                        if (len + from_len + 1 >= size) {
793
862
                                                char *nwith;
794
863
                                                size += from_len + 1;
795
 
                                                nwith = realloc(with, size);
 
864
                                                nwith = el_realloc(with,
 
865
                                                    size * sizeof(*nwith));
796
866
                                                if (nwith == NULL) {
797
 
                                                        free(with);
798
 
                                                        free(tmp);
 
867
                                                        el_free(with);
 
868
                                                        el_free(tmp);
799
869
                                                        return -1;
800
870
                                                }
801
871
                                                with = nwith;
818
888
 
819
889
                        aptr = _rl_compat_sub(tmp, from, to, g_on);
820
890
                        if (aptr) {
821
 
                                free(tmp);
 
891
                                el_free(tmp);
822
892
                                tmp = aptr;
823
893
                        }
824
894
                        g_on = 0;
825
895
                }
826
896
        }
827
897
        *result = tmp;
828
 
        return (p_on? 2:1);
 
898
        return p_on? 2:1;
829
899
}
830
900
 
831
901
 
844
914
 
845
915
        if (history_expansion_char == 0) {
846
916
                *output = strdup(str);
847
 
                return(0);
 
917
                return 0;
848
918
        }
849
919
 
850
920
        *output = NULL;
851
921
        if (str[0] == history_subst_char) {
852
922
                /* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */
853
 
                *output = malloc(strlen(str) + 4 + 1);
 
923
                *output = el_malloc((strlen(str) + 4 + 1) * sizeof(*output));
854
924
                if (*output == NULL)
855
925
                        return 0;
856
926
                (*output)[0] = (*output)[1] = history_expansion_char;
867
937
#define ADD_STRING(what, len, fr)                                       \
868
938
        {                                                               \
869
939
                if (idx + len + 1 > size) {                             \
870
 
                        char *nresult = realloc(result, (size += len + 1));\
 
940
                        char *nresult = el_realloc(result,              \
 
941
                            (size += len + 1) * sizeof(*nresult));      \
871
942
                        if (nresult == NULL) {                          \
872
 
                                free(*output);                          \
 
943
                                el_free(*output);                       \
873
944
                                if (/*CONSTCOND*/fr)                    \
874
 
                                        free(tmp);                      \
 
945
                                        el_free(tmp);                   \
875
946
                                return 0;                               \
876
947
                        }                                               \
877
948
                        result = nresult;                               \
938
1009
                        ADD_STRING(tmp, len, 1);
939
1010
                }
940
1011
                if (tmp) {
941
 
                        free(tmp);
 
1012
                        el_free(tmp);
942
1013
                        tmp = NULL;
943
1014
                }
944
1015
                i = j;
955
1026
                ret = -1;
956
1027
#endif
957
1028
        }
958
 
        free(*output);
 
1029
        el_free(*output);
959
1030
        *output = result;
960
1031
 
961
 
        return (ret);
 
1032
        return ret;
962
1033
}
963
1034
 
964
1035
/*
968
1039
history_arg_extract(int start, int end, const char *str)
969
1040
{
970
1041
        size_t  i, len, max;
971
 
        char    **arr, *result;
 
1042
        char    **arr, *result = NULL;
972
1043
 
973
1044
        arr = history_tokenize(str);
974
1045
        if (!arr)
975
 
                return(NULL);
976
 
        if (arr && *arr == NULL) {
977
 
                free(arr);
978
 
                return(NULL);
979
 
        }
 
1046
                return NULL;
 
1047
        if (arr && *arr == NULL)
 
1048
                goto out;
980
1049
 
981
1050
        for (max = 0; arr[max]; max++)
982
1051
                continue;
983
1052
        max--;
984
1053
 
985
1054
        if (start == '$')
986
 
                start = max;
 
1055
                start = (int)max;
987
1056
        if (end == '$')
988
 
                end = max;
 
1057
                end = (int)max;
989
1058
        if (end < 0)
990
 
                end = max + end + 1;
 
1059
                end = (int)max + end + 1;
991
1060
        if (start < 0)
992
1061
                start = end;
993
1062
 
994
 
        if (start < 0 || end < 0 || (size_t)start > max || (size_t)end > max || start > end)
995
 
                return(NULL);
 
1063
        if (start < 0 || end < 0 || (size_t)start > max ||
 
1064
            (size_t)end > max || start > end)
 
1065
                goto out;
996
1066
 
997
 
        for (i = start, len = 0; i <= (size_t)end; i++)
 
1067
        for (i = (size_t)start, len = 0; i <= (size_t)end; i++)
998
1068
                len += strlen(arr[i]) + 1;
999
1069
        len++;
1000
 
        result = malloc(len);
 
1070
        result = el_malloc(len * sizeof(*result));
1001
1071
        if (result == NULL)
1002
 
                return NULL;
 
1072
                goto out;
1003
1073
 
1004
 
        for (i = start, len = 0; i <= (size_t)end; i++) {
 
1074
        for (i = (size_t)start, len = 0; i <= (size_t)end; i++) {
1005
1075
                (void)strcpy(result + len, arr[i]);
1006
1076
                len += strlen(arr[i]);
1007
1077
                if (i < (size_t)end)
1009
1079
        }
1010
1080
        result[len] = '\0';
1011
1081
 
 
1082
out:
1012
1083
        for (i = 0; arr[i]; i++)
1013
 
                free(arr[i]);
1014
 
        free(arr);
 
1084
                el_free(arr[i]);
 
1085
        el_free(arr);
1015
1086
 
1016
 
        return(result);
 
1087
        return result;
1017
1088
}
1018
1089
 
1019
1090
/*
1050
1121
                if (idx + 2 >= size) {
1051
1122
                        char **nresult;
1052
1123
                        size <<= 1;
1053
 
                        nresult = realloc(result, size * sizeof(char *));
 
1124
                        nresult = el_realloc(result, (size_t)size * sizeof(*nresult));
1054
1125
                        if (nresult == NULL) {
1055
 
                                free(result);
 
1126
                                el_free(result);
1056
1127
                                return NULL;
1057
1128
                        }
1058
1129
                        result = nresult;
1059
1130
                }
1060
 
                len = i - start;
1061
 
                temp = malloc(len + 1);
 
1131
                len = (size_t)i - (size_t)start;
 
1132
                temp = el_malloc((size_t)(len + 1) * sizeof(*temp));
1062
1133
                if (temp == NULL) {
1063
1134
                        for (i = 0; i < idx; i++)
1064
 
                                free(result[i]);
1065
 
                        free(result);
 
1135
                                el_free(result[i]);
 
1136
                        el_free(result);
1066
1137
                        return NULL;
1067
1138
                }
1068
1139
                (void)strncpy(temp, &str[start], len);
1072
1143
                if (str[i])
1073
1144
                        i++;
1074
1145
        }
1075
 
        return (result);
 
1146
        return result;
1076
1147
}
1077
1148
 
1078
1149
 
1104
1175
        history(h, &ev, H_SETSIZE, INT_MAX);
1105
1176
        omax = max_input_history;
1106
1177
        max_input_history = INT_MAX;
1107
 
        return (omax);          /* some value _must_ be returned */
 
1178
        return omax;            /* some value _must_ be returned */
1108
1179
}
1109
1180
 
1110
1181
 
1113
1184
{
1114
1185
 
1115
1186
        /* cannot return true answer */
1116
 
        return (max_input_history != INT_MAX);
 
1187
        return max_input_history != INT_MAX;
 
1188
}
 
1189
 
 
1190
static const char _history_tmp_template[] = "/tmp/.historyXXXXXX";
 
1191
 
 
1192
int
 
1193
history_truncate_file (const char *filename, int nlines)
 
1194
{
 
1195
        int ret = 0;
 
1196
        FILE *fp, *tp;
 
1197
        char template[sizeof(_history_tmp_template)];
 
1198
        char buf[4096];
 
1199
        int fd;
 
1200
        char *cp;
 
1201
        off_t off;
 
1202
        int count = 0;
 
1203
        ssize_t left = 0;
 
1204
 
 
1205
        if (filename == NULL && (filename = _default_history_file()) == NULL)
 
1206
                return errno;
 
1207
        if ((fp = fopen(filename, "r+")) == NULL)
 
1208
                return errno;
 
1209
        strcpy(template, _history_tmp_template);
 
1210
        if ((fd = mkstemp(template)) == -1) {
 
1211
                ret = errno;
 
1212
                goto out1;
 
1213
        }
 
1214
 
 
1215
        if ((tp = fdopen(fd, "r+")) == NULL) {
 
1216
                close(fd);
 
1217
                ret = errno;
 
1218
                goto out2;
 
1219
        }
 
1220
 
 
1221
        for(;;) {
 
1222
                if (fread(buf, sizeof(buf), (size_t)1, fp) != 1) {
 
1223
                        if (ferror(fp)) {
 
1224
                                ret = errno;
 
1225
                                break;
 
1226
                        }
 
1227
                        if (fseeko(fp, (off_t)sizeof(buf) * count, SEEK_SET) ==
 
1228
                            (off_t)-1) {
 
1229
                                ret = errno;
 
1230
                                break;
 
1231
                        }
 
1232
                        left = (ssize_t)fread(buf, (size_t)1, sizeof(buf), fp);
 
1233
                        if (ferror(fp)) {
 
1234
                                ret = errno;
 
1235
                                break;
 
1236
                        }
 
1237
                        if (left == 0) {
 
1238
                                count--;
 
1239
                                left = sizeof(buf);
 
1240
                        } else if (fwrite(buf, (size_t)left, (size_t)1, tp)
 
1241
                            != 1) {
 
1242
                                ret = errno;
 
1243
                                break;
 
1244
                        }
 
1245
                        fflush(tp);
 
1246
                        break;
 
1247
                }
 
1248
                if (fwrite(buf, sizeof(buf), (size_t)1, tp) != 1) {
 
1249
                        ret = errno;
 
1250
                        break;
 
1251
                }
 
1252
                count++;
 
1253
        }
 
1254
        if (ret)
 
1255
                goto out3;
 
1256
        cp = buf + left - 1;
 
1257
        if(*cp != '\n')
 
1258
                cp++;
 
1259
        for(;;) {
 
1260
                while (--cp >= buf) {
 
1261
                        if (*cp == '\n') {
 
1262
                                if (--nlines == 0) {
 
1263
                                        if (++cp >= buf + sizeof(buf)) {
 
1264
                                                count++;
 
1265
                                                cp = buf;
 
1266
                                        }
 
1267
                                        break;
 
1268
                                }
 
1269
                        }
 
1270
                }
 
1271
                if (nlines <= 0 || count == 0)
 
1272
                        break;
 
1273
                count--;
 
1274
                if (fseeko(tp, (off_t)sizeof(buf) * count, SEEK_SET) < 0) {
 
1275
                        ret = errno;
 
1276
                        break;
 
1277
                }
 
1278
                if (fread(buf, sizeof(buf), (size_t)1, tp) != 1) {
 
1279
                        if (ferror(tp)) {
 
1280
                                ret = errno;
 
1281
                                break;
 
1282
                        }
 
1283
                        ret = EAGAIN;
 
1284
                        break;
 
1285
                }
 
1286
                cp = buf + sizeof(buf);
 
1287
        }
 
1288
 
 
1289
        if (ret || nlines > 0)
 
1290
                goto out3;
 
1291
 
 
1292
        if (fseeko(fp, (off_t)0, SEEK_SET) == (off_t)-1) {
 
1293
                ret = errno;
 
1294
                goto out3;
 
1295
        }
 
1296
 
 
1297
        if (fseeko(tp, (off_t)sizeof(buf) * count + (cp - buf), SEEK_SET) ==
 
1298
            (off_t)-1) {
 
1299
                ret = errno;
 
1300
                goto out3;
 
1301
        }
 
1302
 
 
1303
        for(;;) {
 
1304
                if ((left = (ssize_t)fread(buf, (size_t)1, sizeof(buf), tp)) == 0) {
 
1305
                        if (ferror(fp))
 
1306
                                ret = errno;
 
1307
                        break;
 
1308
                }
 
1309
                if (fwrite(buf, (size_t)left, (size_t)1, fp) != 1) {
 
1310
                        ret = errno;
 
1311
                        break;
 
1312
                }
 
1313
        }
 
1314
        fflush(fp);
 
1315
        if((off = ftello(fp)) > 0) {
 
1316
                if (ftruncate(fileno(fp), off) == -1)
 
1317
                  ret = errno;
 
1318
        }
 
1319
out3:
 
1320
        fclose(tp);
 
1321
out2:
 
1322
        unlink(template);
 
1323
out1:
 
1324
        fclose(fp);
 
1325
 
 
1326
        return ret;
1117
1327
}
1118
1328
 
1119
1329
 
1127
1337
 
1128
1338
        if (h == NULL || e == NULL)
1129
1339
                rl_initialize();
1130
 
        return (history(h, &ev, H_LOAD, filename) == -1);
 
1340
        if (filename == NULL && (filename = _default_history_file()) == NULL)
 
1341
                return errno;
 
1342
        return history(h, &ev, H_LOAD, filename) == -1 ?
 
1343
            (errno ? errno : EINVAL) : 0;
1131
1344
}
1132
1345
 
1133
1346
 
1141
1354
 
1142
1355
        if (h == NULL || e == NULL)
1143
1356
                rl_initialize();
1144
 
        return (history(h, &ev, H_SAVE, filename) == -1);
 
1357
        if (filename == NULL && (filename = _default_history_file()) == NULL)
 
1358
                return errno;
 
1359
        return history(h, &ev, H_SAVE, filename) == -1 ?
 
1360
            (errno ? errno : EINVAL) : 0;
1145
1361
}
1146
1362
 
1147
1363
 
1162
1378
 
1163
1379
        /* save current position */
1164
1380
        if (history(h, &ev, H_CURR) != 0)
1165
 
                return (NULL);
 
1381
                return NULL;
1166
1382
        curr_num = ev.num;
1167
1383
 
1168
 
        /* start from most recent */
1169
 
        if (history(h, &ev, H_FIRST) != 0)
1170
 
                return (NULL);  /* error */
 
1384
        /* start from the oldest */
 
1385
        if (history(h, &ev, H_LAST) != 0)
 
1386
                return NULL;    /* error */
1171
1387
 
1172
 
        /* look backwards for event matching specified offset */
1173
 
        if (history(h, &ev, H_NEXT_EVENT, num + 1))
1174
 
                return (NULL);
 
1388
        /* look forwards for event matching specified offset */
 
1389
        if (history(h, &ev, H_NEXT_EVDATA, num, &she.data))
 
1390
                return NULL;
1175
1391
 
1176
1392
        she.line = ev.str;
1177
 
        she.data = NULL;
1178
1393
 
1179
1394
        /* restore pointer to where it was */
1180
1395
        (void)history(h, &ev, H_SET, curr_num);
1181
1396
 
1182
 
        return (&she);
 
1397
        return &she;
1183
1398
}
1184
1399
 
1185
1400
 
1198
1413
        if (history(h, &ev, H_GETSIZE) == 0)
1199
1414
                history_length = ev.num;
1200
1415
 
1201
 
        return (!(history_length > 0)); /* return 0 if all is okay */
 
1416
        return !(history_length > 0); /* return 0 if all is okay */
1202
1417
}
1203
1418
 
1204
1419
 
1208
1423
HIST_ENTRY *
1209
1424
remove_history(int num)
1210
1425
{
1211
 
        HIST_ENTRY *she;
1212
 
        HistEvent ev;
1213
 
 
1214
 
        if (h == NULL || e == NULL)
1215
 
                rl_initialize();
1216
 
 
1217
 
        if (history(h, &ev, H_DEL, num) != 0)
1218
 
                return NULL;
1219
 
 
1220
 
        if ((she = malloc(sizeof(*she))) == NULL)
1221
 
                return NULL;
1222
 
 
1223
 
        she->line = ev.str;
1224
 
        she->data = NULL;
1225
 
 
1226
 
        return she;
1227
 
}
1228
 
 
 
1426
        HIST_ENTRY *he;
 
1427
        HistEvent ev;
 
1428
 
 
1429
        if (h == NULL || e == NULL)
 
1430
                rl_initialize();
 
1431
 
 
1432
        if ((he = el_malloc(sizeof(*he))) == NULL)
 
1433
                return NULL;
 
1434
 
 
1435
        if (history(h, &ev, H_DELDATA, num, &he->data) != 0) {
 
1436
                el_free(he);
 
1437
                return NULL;
 
1438
        }
 
1439
 
 
1440
        he->line = ev.str;
 
1441
        if (history(h, &ev, H_GETSIZE) == 0)
 
1442
                history_length = ev.num;
 
1443
 
 
1444
        return he;
 
1445
}
 
1446
 
 
1447
 
 
1448
/*
 
1449
 * replace the line and data of the num-th entry
 
1450
 */
 
1451
HIST_ENTRY *
 
1452
replace_history_entry(int num, const char *line, histdata_t data)
 
1453
{
 
1454
        HIST_ENTRY *he;
 
1455
        HistEvent ev;
 
1456
        int curr_num;
 
1457
 
 
1458
        if (h == NULL || e == NULL)
 
1459
                rl_initialize();
 
1460
 
 
1461
        /* save current position */
 
1462
        if (history(h, &ev, H_CURR) != 0)
 
1463
                return NULL;
 
1464
        curr_num = ev.num;
 
1465
 
 
1466
        /* start from the oldest */
 
1467
        if (history(h, &ev, H_LAST) != 0)
 
1468
                return NULL;    /* error */
 
1469
 
 
1470
        if ((he = el_malloc(sizeof(*he))) == NULL)
 
1471
                return NULL;
 
1472
 
 
1473
        /* look forwards for event matching specified offset */
 
1474
        if (history(h, &ev, H_NEXT_EVDATA, num, &he->data))
 
1475
                goto out;
 
1476
 
 
1477
        he->line = strdup(ev.str);
 
1478
        if (he->line == NULL)
 
1479
                goto out;
 
1480
 
 
1481
        if (history(h, &ev, H_REPLACE, line, data))
 
1482
                goto out;
 
1483
 
 
1484
        /* restore pointer to where it was */
 
1485
        if (history(h, &ev, H_SET, curr_num))
 
1486
                goto out;
 
1487
 
 
1488
        return he;
 
1489
out:
 
1490
        el_free(he);
 
1491
        return NULL;
 
1492
}
1229
1493
 
1230
1494
/*
1231
1495
 * clear the history list - delete all entries
1235
1499
{
1236
1500
        HistEvent ev;
1237
1501
 
1238
 
        history(h, &ev, H_CLEAR);
 
1502
        (void)history(h, &ev, H_CLEAR);
 
1503
        history_length = 0;
1239
1504
}
1240
1505
 
1241
1506
 
1249
1514
        int curr_num, off;
1250
1515
 
1251
1516
        if (history(h, &ev, H_CURR) != 0)
1252
 
                return (0);
 
1517
                return 0;
1253
1518
        curr_num = ev.num;
1254
1519
 
1255
 
        history(h, &ev, H_FIRST);
 
1520
        (void)history(h, &ev, H_FIRST);
1256
1521
        off = 1;
1257
1522
        while (ev.num != curr_num && history(h, &ev, H_NEXT) == 0)
1258
1523
                off++;
1259
1524
 
1260
 
        return (off);
 
1525
        return off;
1261
1526
}
1262
1527
 
1263
1528
 
1268
1533
current_history(void)
1269
1534
{
1270
1535
 
1271
 
        return (_move_history(H_CURR));
 
1536
        return _move_history(H_CURR);
1272
1537
}
1273
1538
 
1274
1539
 
1279
1544
history_total_bytes(void)
1280
1545
{
1281
1546
        HistEvent ev;
1282
 
        int curr_num, size;
 
1547
        int curr_num;
 
1548
        size_t size;
1283
1549
 
1284
1550
        if (history(h, &ev, H_CURR) != 0)
1285
 
                return (-1);
 
1551
                return -1;
1286
1552
        curr_num = ev.num;
1287
1553
 
1288
 
        history(h, &ev, H_FIRST);
 
1554
        (void)history(h, &ev, H_FIRST);
1289
1555
        size = 0;
1290
1556
        do
1291
 
                size += strlen(ev.str);
 
1557
                size += strlen(ev.str) * sizeof(*ev.str);
1292
1558
        while (history(h, &ev, H_NEXT) == 0);
1293
1559
 
1294
1560
        /* get to the same position as before */
1295
1561
        history(h, &ev, H_PREV_EVENT, curr_num);
1296
1562
 
1297
 
        return (size);
 
1563
        return (int)size;
1298
1564
}
1299
1565
 
1300
1566
 
1307
1573
        HistEvent ev;
1308
1574
        int curr_num;
1309
1575
 
1310
 
        if (pos > history_length || pos < 0)
1311
 
                return (-1);
 
1576
        if (pos >= history_length || pos < 0)
 
1577
                return -1;
1312
1578
 
1313
 
        history(h, &ev, H_CURR);
 
1579
        (void)history(h, &ev, H_CURR);
1314
1580
        curr_num = ev.num;
1315
1581
 
1316
 
        if (history(h, &ev, H_SET, pos)) {
1317
 
                history(h, &ev, H_SET, curr_num);
1318
 
                return(-1);
 
1582
        /*
 
1583
         * use H_DELDATA to set to nth history (without delete) by passing
 
1584
         * (void **)-1
 
1585
         */
 
1586
        if (history(h, &ev, H_DELDATA, pos, (void **)-1)) {
 
1587
                (void)history(h, &ev, H_SET, curr_num);
 
1588
                return -1;
1319
1589
        }
1320
 
        return (0);
 
1590
        return 0;
1321
1591
}
1322
1592
 
1323
1593
 
1328
1598
previous_history(void)
1329
1599
{
1330
1600
 
1331
 
        return (_move_history(H_PREV));
 
1601
        return _move_history(H_PREV);
1332
1602
}
1333
1603
 
1334
1604
 
1339
1609
next_history(void)
1340
1610
{
1341
1611
 
1342
 
        return (_move_history(H_NEXT));
 
1612
        return _move_history(H_NEXT);
1343
1613
}
1344
1614
 
1345
1615
 
1354
1624
        int curr_num;
1355
1625
 
1356
1626
        if (history(h, &ev, H_CURR) != 0)
1357
 
                return (-1);
 
1627
                return -1;
1358
1628
        curr_num = ev.num;
1359
1629
 
1360
1630
        for (;;) {
1361
1631
                if ((strp = strstr(ev.str, str)) != NULL)
1362
 
                        return (int) (strp - ev.str);
 
1632
                        return (int)(strp - ev.str);
1363
1633
                if (history(h, &ev, direction < 0 ? H_NEXT:H_PREV) != 0)
1364
1634
                        break;
1365
1635
        }
1366
 
        history(h, &ev, H_SET, curr_num);
1367
 
        return (-1);
 
1636
        (void)history(h, &ev, H_SET, curr_num);
 
1637
        return -1;
1368
1638
}
1369
1639
 
1370
1640
 
1376
1646
{
1377
1647
        HistEvent ev;
1378
1648
 
1379
 
        return (history(h, &ev, direction < 0? H_PREV_STR:H_NEXT_STR, str));
 
1649
        return (history(h, &ev, direction < 0 ?
 
1650
            H_PREV_STR : H_NEXT_STR, str));
1380
1651
}
1381
1652
 
1382
1653
 
1396
1667
        pos = (pos > 0) ? 1 : -1;
1397
1668
 
1398
1669
        if (history(h, &ev, H_CURR) != 0)
1399
 
                return (-1);
 
1670
                return -1;
1400
1671
        curr_num = ev.num;
1401
1672
 
1402
1673
        if (history_set_pos(off) != 0 || history(h, &ev, H_CURR) != 0)
1403
 
                return (-1);
1404
 
 
 
1674
                return -1;
1405
1675
 
1406
1676
        for (;;) {
1407
1677
                if (strstr(ev.str, str))
1408
 
                        return (off);
 
1678
                        return off;
1409
1679
                if (history(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0)
1410
1680
                        break;
1411
1681
        }
1412
1682
 
1413
1683
        /* set "current" pointer back to previous state */
1414
 
        history(h, &ev, (pos < 0) ? H_NEXT_EVENT : H_PREV_EVENT, curr_num);
 
1684
        (void)history(h, &ev,
 
1685
            pos < 0 ? H_NEXT_EVENT : H_PREV_EVENT, curr_num);
1415
1686
 
1416
 
        return (-1);
 
1687
        return -1;
1417
1688
}
1418
1689
 
1419
1690
 
1436
1707
 * a completion generator for usernames; returns _first_ username
1437
1708
 * which starts with supplied text
1438
1709
 * text contains a partial username preceded by random character
1439
 
 * (usually '~'); state is ignored
 
1710
 * (usually '~'); state resets search from start (??? should we do that anyway)
1440
1711
 * it's callers responsibility to free returned value
1441
1712
 */
1442
1713
char *
1443
1714
username_completion_function(const char *text, int state)
1444
1715
{
1445
 
        struct passwd *pwd;
 
1716
#if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
 
1717
        struct passwd pwres;
 
1718
        char pwbuf[1024];
 
1719
#endif
 
1720
        struct passwd *pass = NULL;
1446
1721
 
1447
1722
        if (text[0] == '\0')
1448
 
                return (NULL);
 
1723
                return NULL;
1449
1724
 
1450
1725
        if (*text == '~')
1451
1726
                text++;
1453
1728
        if (state == 0)
1454
1729
                setpwent();
1455
1730
 
1456
 
        /* XXXMYSQL: just use non-_r functions for now */
1457
 
        while ((pwd = getpwent()) && text[0] == pwd->pw_name[0]
1458
 
            && strcmp(text, pwd->pw_name) == 0);
 
1731
        while (
 
1732
#if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
 
1733
            getpwent_r(&pwres, pwbuf, sizeof(pwbuf), &pass) == 0 && pass != NULL
 
1734
#else
 
1735
            (pass = getpwent()) != NULL
 
1736
#endif
 
1737
            && text[0] == pass->pw_name[0]
 
1738
            && strcmp(text, pass->pw_name) == 0)
 
1739
                continue;
1459
1740
 
1460
 
        if (pwd == NULL) {
 
1741
        if (pass == NULL) {
1461
1742
                endpwent();
1462
 
                return (NULL);
 
1743
                return NULL;
1463
1744
        }
1464
 
        return (strdup(pwd->pw_name));
 
1745
        return strdup(pass->pw_name);
1465
1746
}
1466
1747
 
1467
1748
 
1485
1766
rl_display_match_list(char **matches, int len, int max)
1486
1767
{
1487
1768
 
1488
 
        fn_display_match_list(e, matches, len, max);
 
1769
        fn_display_match_list(e, matches, (size_t)len, (size_t)max);
1489
1770
}
1490
1771
 
1491
1772
static const char *
1494
1775
    __attribute__((__unused__)))
1495
1776
{
1496
1777
        static char buf[2];
1497
 
        buf[0] = rl_completion_append_character;
 
1778
        buf[0] = (char)rl_completion_append_character;
1498
1779
        buf[1] = '\0';
1499
1780
        return buf;
1500
1781
}
1507
1788
int
1508
1789
rl_complete(int ignore __attribute__((__unused__)), int invoking_key)
1509
1790
{
 
1791
#ifdef WIDECHAR
 
1792
        static ct_buffer_t wbreak_conv, sprefix_conv;
 
1793
#endif
 
1794
 
1510
1795
        if (h == NULL || e == NULL)
1511
1796
                rl_initialize();
1512
1797
 
1515
1800
                arr[0] = (char)invoking_key;
1516
1801
                arr[1] = '\0';
1517
1802
                el_insertstr(e, arr);
1518
 
                return (CC_REFRESH);
 
1803
                return CC_REFRESH;
1519
1804
        }
1520
1805
 
1521
1806
        /* Just look at how many global variables modify this operation! */
1522
1807
        return fn_complete(e,
1523
1808
            (CPFunction *)rl_completion_entry_function,
1524
1809
            rl_attempted_completion_function,
1525
 
            rl_basic_word_break_characters, rl_special_prefixes,
1526
 
            _rl_completion_append_character_function, rl_completion_query_items,
 
1810
            ct_decode_string(rl_basic_word_break_characters, &wbreak_conv),
 
1811
            ct_decode_string(rl_special_prefixes, &sprefix_conv),
 
1812
            _rl_completion_append_character_function,
 
1813
            (size_t)rl_completion_query_items,
1527
1814
            &rl_completion_type, &rl_attempted_completion_over,
1528
1815
            &rl_point, &rl_end);
 
1816
 
 
1817
 
1529
1818
}
1530
1819
 
1531
1820
 
1544
1833
 * bind key c to readline-type function func
1545
1834
 */
1546
1835
int
1547
 
rl_bind_key(int c, int func(int, int))
 
1836
rl_bind_key(int c, rl_command_func_t *func)
1548
1837
{
1549
1838
        int retval = -1;
1550
1839
 
1556
1845
                e->el_map.key[c] = ED_INSERT;
1557
1846
                retval = 0;
1558
1847
        }
1559
 
        return (retval);
 
1848
        return retval;
1560
1849
}
1561
1850
 
1562
1851
 
1572
1861
        if (e == NULL || h == NULL)
1573
1862
                rl_initialize();
1574
1863
 
1575
 
        return (el_getc(e, fooarr));
 
1864
        return el_getc(e, fooarr);
1576
1865
}
1577
1866
 
1578
1867
 
1602
1891
                rl_initialize();
1603
1892
 
1604
1893
        /* XXX - int -> char conversion can lose on multichars */
1605
 
        arr[0] = c;
 
1894
        arr[0] = (char)c;
1606
1895
        arr[1] = '\0';
1607
1896
 
1608
1897
        for (; count > 0; count--)
1609
1898
                el_push(e, arr);
1610
1899
 
1611
 
        return (0);
 
1900
        return 0;
 
1901
}
 
1902
 
 
1903
int
 
1904
rl_insert_text(const char *text)
 
1905
{
 
1906
        if (!text || *text == 0)
 
1907
                return 0;
 
1908
 
 
1909
        if (h == NULL || e == NULL)
 
1910
                rl_initialize();
 
1911
 
 
1912
        if (el_insertstr(e, text) < 0)
 
1913
                return 0;
 
1914
        return (int)strlen(text);
1612
1915
}
1613
1916
 
1614
1917
/*ARGSUSED*/
1615
1918
int
1616
 
rl_newline(int count, int c)
 
1919
rl_newline(int count __attribute__((__unused__)),
 
1920
    int c __attribute__((__unused__)))
1617
1921
{
1618
1922
        /*
1619
1923
         * Readline-4.0 appears to ignore the args.
1623
1927
 
1624
1928
/*ARGSUSED*/
1625
1929
static unsigned char
1626
 
rl_bind_wrapper(EditLine *el, unsigned char c)
 
1930
rl_bind_wrapper(EditLine *el __attribute__((__unused__)), unsigned char c)
1627
1931
{
1628
1932
        if (map[c] == NULL)
1629
1933
            return CC_ERROR;
1674
1978
                } else
1675
1979
                        wbuf = NULL;
1676
1980
                (*(void (*)(const char *))rl_linefunc)(wbuf);
1677
 
                el_set(e, EL_UNBUFFERED, 1);
 
1981
                //el_set(e, EL_UNBUFFERED, 1);
1678
1982
        }
1679
1983
}
1680
1984
 
1700
2004
rl_redisplay(void)
1701
2005
{
1702
2006
        char a[2];
1703
 
        a[0] = e->el_tty.t_c[TS_IO][C_REPRINT];
 
2007
        a[0] = (char)e->el_tty.t_c[TS_IO][C_REPRINT];
1704
2008
        a[1] = '\0';
1705
2009
        el_push(e, a);
1706
2010
}
1709
2013
rl_get_previous_history(int count, int key)
1710
2014
{
1711
2015
        char a[2];
1712
 
        a[0] = key;
 
2016
        a[0] = (char)key;
1713
2017
        a[1] = '\0';
1714
2018
        while (count--)
1715
2019
                el_push(e, a);
1718
2022
 
1719
2023
void
1720
2024
/*ARGSUSED*/
1721
 
rl_prep_terminal(int meta_flag)
 
2025
rl_prep_terminal(int meta_flag __attribute__((__unused__)))
1722
2026
{
1723
2027
        el_set(e, EL_PREP_TERM, 1);
1724
2028
}
1732
2036
int
1733
2037
rl_read_init_file(const char *s)
1734
2038
{
1735
 
        return(el_source(e, s));
 
2039
        return el_source(e, s);
1736
2040
}
1737
2041
 
1738
2042
int
1746
2050
        tok_str(tok, line, &argc, &argv);
1747
2051
        argc = el_parse(e, argc, argv);
1748
2052
        tok_end(tok);
1749
 
        return (argc ? 1 : 0);
 
2053
        return argc ? 1 : 0;
1750
2054
}
1751
2055
 
1752
2056
int
1756
2060
         * The proper return value is undocument, but this is what the
1757
2061
         * readline source seems to do.
1758
2062
         */
1759
 
        return ((el_set(e, EL_BIND, "", var, value) == -1) ? 1 : 0);
 
2063
        return el_set(e, EL_BIND, "", var, value) == -1 ? 1 : 0;
1760
2064
}
1761
2065
 
1762
2066
void
1764
2068
{
1765
2069
        char buf[2];
1766
2070
 
1767
 
        buf[0] = c;
 
2071
        buf[0] = (char)c;
1768
2072
        buf[1] = '\0';
1769
2073
        el_insertstr(e, buf);
1770
2074
}
1772
2076
static int
1773
2077
_rl_event_read_char(EditLine *el, char *cp)
1774
2078
{
1775
 
        int     n, num_read = 0;
 
2079
        int     n;
 
2080
        ssize_t num_read = 0;
1776
2081
 
1777
2082
        *cp = '\0';
1778
2083
        while (rl_event_hook) {
1781
2086
 
1782
2087
#if defined(FIONREAD)
1783
2088
                if (ioctl(el->el_infd, FIONREAD, &n) < 0)
1784
 
                        return(-1);
 
2089
                        return -1;
1785
2090
                if (n)
1786
 
                        num_read = read(el->el_infd, cp, 1);
 
2091
                        num_read = read(el->el_infd, cp, (size_t)1);
1787
2092
                else
1788
2093
                        num_read = 0;
1789
2094
#elif defined(F_SETFL) && defined(O_NDELAY)
1790
2095
                if ((n = fcntl(el->el_infd, F_GETFL, 0)) < 0)
1791
 
                        return(-1);
 
2096
                        return -1;
1792
2097
                if (fcntl(el->el_infd, F_SETFL, n|O_NDELAY) < 0)
1793
 
                        return(-1);
 
2098
                        return -1;
1794
2099
                num_read = read(el->el_infd, cp, 1);
1795
2100
                if (fcntl(el->el_infd, F_SETFL, n))
1796
 
                        return(-1);
 
2101
                        return -1;
1797
2102
#else
1798
2103
                /* not non-blocking, but what you gonna do? */
1799
2104
                num_read = read(el->el_infd, cp, 1);
1800
 
                return(-1);
 
2105
                return -1;
1801
2106
#endif
1802
2107
 
1803
2108
                if (num_read < 0 && errno == EAGAIN)
1808
2113
        }
1809
2114
        if (!rl_event_hook)
1810
2115
                el_set(el, EL_GETCFN, EL_BUILTIN_GETCFN);
1811
 
        return(num_read);
 
2116
        return (int)num_read;
1812
2117
}
1813
2118
 
1814
2119
static void
1816
2121
{
1817
2122
        const LineInfo *li = el_line(e);
1818
2123
 
1819
 
        rl_point = li->cursor - li->buffer;
1820
 
        rl_end = li->lastchar - li->buffer;
 
2124
        rl_point = (int)(li->cursor - li->buffer);
 
2125
        rl_end = (int)(li->lastchar - li->buffer);
1821
2126
}
1822
2127
 
1823
2128
void
1847
2152
 
1848
2153
        len = 1;
1849
2154
        max = 10;
1850
 
        if ((list = malloc(max * sizeof(*list))) == NULL)
 
2155
        if ((list = el_malloc(max * sizeof(*list))) == NULL)
1851
2156
                return NULL;
1852
2157
 
1853
2158
        while ((match = (*fun)(str, (int)(len - 1))) != NULL) {
 
2159
                list[len++] = match;
1854
2160
                if (len == max) {
1855
2161
                        char **nl;
1856
2162
                        max += 10;
1857
 
                        if ((nl = realloc(list, max * sizeof(*nl))) == NULL)
 
2163
                        if ((nl = el_realloc(list, max * sizeof(*nl))) == NULL)
1858
2164
                                goto out;
1859
2165
                        list = nl;
1860
2166
                }
1861
 
                list[len++] = match;
1862
2167
        }
1863
2168
        if (len == 1)
1864
2169
                goto out;
1882
2187
                if ((list[0] = strdup(str)) == NULL)
1883
2188
                        goto out;
1884
2189
        } else {
1885
 
                if ((list[0] = malloc(min + 1)) == NULL)
 
2190
                if ((list[0] = el_malloc((min + 1) * sizeof(*list[0]))) == NULL)
1886
2191
                        goto out;
1887
2192
                (void)memcpy(list[0], list[1], min);
1888
2193
                list[0][min] = '\0';
1890
2195
        return list;
1891
2196
                
1892
2197
out:
1893
 
        free(list);
 
2198
        el_free(list);
1894
2199
        return NULL;
1895
2200
}
1896
2201
 
1920
2225
        return strcoll(*s1, *s2);
1921
2226
}
1922
2227
 
 
2228
HISTORY_STATE *
 
2229
history_get_history_state(void)
 
2230
{
 
2231
        HISTORY_STATE *hs;
 
2232
 
 
2233
        if ((hs = el_malloc(sizeof(*hs))) == NULL)
 
2234
                return NULL;
 
2235
        hs->length = history_length;
 
2236
        return hs;
 
2237
}
 
2238
 
1923
2239
int
1924
2240
/*ARGSUSED*/
1925
 
rl_kill_text(int from, int to)
 
2241
rl_kill_text(int from __attribute__((__unused__)),
 
2242
    int to __attribute__((__unused__)))
1926
2243
{
1927
2244
        return 0;
1928
2245
}
1941
2258
 
1942
2259
void
1943
2260
/*ARGSUSED*/
1944
 
rl_set_keymap(Keymap k)
1945
 
{
1946
 
}
1947
 
 
1948
 
int
1949
 
/*ARGSUSED*/
1950
 
rl_generic_bind(int type, const char * keyseq, const char * data, Keymap k)
1951
 
{
1952
 
        return 0;
1953
 
}
1954
 
 
1955
 
int
1956
 
/*ARGSUSED*/
1957
 
rl_bind_key_in_map(int key, Function *fun, Keymap k)
 
2261
rl_set_keymap(Keymap k __attribute__((__unused__)))
 
2262
{
 
2263
}
 
2264
 
 
2265
int
 
2266
/*ARGSUSED*/
 
2267
rl_generic_bind(int type __attribute__((__unused__)),
 
2268
    const char * keyseq __attribute__((__unused__)),
 
2269
    const char * data __attribute__((__unused__)),
 
2270
    Keymap k __attribute__((__unused__)))
 
2271
{
 
2272
        return 0;
 
2273
}
 
2274
 
 
2275
int
 
2276
/*ARGSUSED*/
 
2277
rl_bind_key_in_map(int key __attribute__((__unused__)),
 
2278
    rl_command_func_t *fun __attribute__((__unused__)),
 
2279
    Keymap k __attribute__((__unused__)))
 
2280
{
 
2281
        return 0;
 
2282
}
 
2283
 
 
2284
/* unsupported, but needed by python */
 
2285
void
 
2286
rl_cleanup_after_signal(void)
 
2287
{
 
2288
}
 
2289
 
 
2290
int
 
2291
rl_on_new_line(void)
1958
2292
{
1959
2293
        return 0;
1960
2294
}