~ubuntu-branches/ubuntu/precise/mysql-5.5/precise-201203300109

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-02-14 23:59:22 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20120214235922-cux5uek1e5l0hje9
Tags: 5.5.20-0ubuntu1
* New upstream release.
* d/mysql-server-5.5.mysql.upstart: Fix stop on to make sure mysql is
  fully stopped before shutdown commences. (LP: #688541) Also simplify
  start on as it is redundant.
* d/control: Depend on upstart version which has apparmor profile load
  script to prevent failure on upgrade from lucid to precise.
  (LP: #907465)
* d/apparmor-profile: need to allow /run since that is the true path
  of /var/run files. (LP: #917542)
* d/control: mysql-server-5.5 has files in it that used to be owned
  by libmysqlclient-dev, so it must break/replace it. (LP: #912487)
* d/rules, d/control: 5.5.20 Fixes segfault on tests with gcc 4.6,
  change compiler back to system default.
* d/rules: Turn off embedded libedit/readline.(Closes: #659566)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*      $NetBSD: read.c,v 1.43 2009/02/05 19:15:44 christos Exp $       */
 
1
/*      $NetBSD: read.c,v 1.67 2011/08/16 16:25:15 christos Exp $       */
2
2
 
3
3
/*-
4
4
 * Copyright (c) 1992, 1993
48
48
#include <fcntl.h>
49
49
#include <unistd.h>
50
50
#include <stdlib.h>
 
51
#include <limits.h>
51
52
#include "el.h"
52
53
 
53
 
#define OKCMD   -1
 
54
#define OKCMD   -1      /* must be -1! */
54
55
 
55
56
private int     read__fixio(int, int);
56
57
private int     read_preread(EditLine *);
57
 
private int     read_char(EditLine *, char *);
58
 
private int     read_getcmd(EditLine *, el_action_t *, char *);
 
58
private int     read_char(EditLine *, Char *);
 
59
private int     read_getcmd(EditLine *, el_action_t *, Char *);
59
60
private void    read_pop(c_macro_t *);
60
61
 
61
62
/* read_init():
89
90
protected el_rfunc_t
90
91
el_read_getfn(EditLine *el)
91
92
{
92
 
       return (el->el_read.read_char == read_char) ?
 
93
       return el->el_read.read_char == read_char ?
93
94
            EL_BUILTIN_GETCFN : el->el_read.read_char;
94
95
}
95
96
 
131
132
#ifdef EWOULDBLOCK
132
133
        case EWOULDBLOCK:
133
134
#ifndef TRY_AGAIN
134
 
#define TRY_AGAIN
 
135
#define TRY_AGAIN
135
136
#endif
136
137
#endif /* EWOULDBLOCK */
137
138
 
139
140
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
140
141
        case EAGAIN:
141
142
#ifndef TRY_AGAIN
142
 
#define TRY_AGAIN
 
143
#define TRY_AGAIN
143
144
#endif
144
145
#endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
145
146
#endif /* POSIX && EAGAIN */
148
149
#ifdef TRY_AGAIN
149
150
#if defined(F_SETFL) && defined(O_NDELAY)
150
151
                if ((e = fcntl(fd, F_GETFL, 0)) == -1)
151
 
                        return (-1);
 
152
                        return -1;
152
153
 
153
154
                if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
154
 
                        return (-1);
 
155
                        return -1;
155
156
                else
156
157
                        e = 1;
157
158
#endif /* F_SETFL && O_NDELAY */
160
161
                {
161
162
                        int zero = 0;
162
163
 
163
 
                        if (ioctl(fd, FIONBIO, (ioctl_t) & zero) == -1)
164
 
                                return (-1);
 
164
                        if (ioctl(fd, FIONBIO, &zero) == -1)
 
165
                                return -1;
165
166
                        else
166
167
                                e = 1;
167
168
                }
168
169
#endif /* FIONBIO */
169
170
 
170
171
#endif /* TRY_AGAIN */
171
 
                return (e ? 0 : -1);
 
172
                return e ? 0 : -1;
172
173
 
173
174
        case EINTR:
174
 
                return (0);
 
175
                return 0;
175
176
 
176
177
        default:
177
 
                return (-1);
 
178
                return -1;
178
179
        }
179
180
}
180
181
 
188
189
        int chrs = 0;
189
190
 
190
191
        if (el->el_tty.t_mode == ED_IO)
191
 
                return (0);
 
192
                return 0;
192
193
 
 
194
#ifndef WIDECHAR
 
195
/* FIONREAD attempts to buffer up multiple bytes, and to make that work
 
196
 * properly with partial wide/UTF-8 characters would need some careful work. */
193
197
#ifdef FIONREAD
194
 
        (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
 
198
        (void) ioctl(el->el_infd, FIONREAD, &chrs);
195
199
        if (chrs > 0) {
196
200
                char buf[EL_BUFSIZ];
197
201
 
203
207
                }
204
208
        }
205
209
#endif /* FIONREAD */
206
 
 
207
 
        return (chrs > 0);
 
210
#endif
 
211
        return chrs > 0;
208
212
}
209
213
 
210
214
 
212
216
 *      Push a macro
213
217
 */
214
218
public void
215
 
el_push(EditLine *el, const char *str)
 
219
FUN(el,push)(EditLine *el, const Char *str)
216
220
{
217
221
        c_macro_t *ma = &el->el_chared.c_macro;
218
222
 
219
223
        if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
220
224
                ma->level++;
221
 
                if ((ma->macro[ma->level] = el_strdup(str)) != NULL)
 
225
                if ((ma->macro[ma->level] = Strdup(str)) != NULL)
222
226
                        return;
223
227
                ma->level--;
224
228
        }
225
 
        term_beep(el);
226
 
        term__flush(el);
 
229
        terminal_beep(el);
 
230
        terminal__flush(el);
227
231
}
228
232
 
229
233
 
230
234
/* read_getcmd():
231
235
 *      Return next command from the input stream.
 
236
 *      Character values > 255 are not looked up in the map, but inserted.
232
237
 */
233
238
private int
234
 
read_getcmd(EditLine *el, el_action_t *cmdnum, char *ch)
 
239
read_getcmd(EditLine *el, el_action_t *cmdnum, Char *ch)
235
240
{
236
241
        el_action_t cmd;
237
242
        int num;
238
243
 
 
244
        el->el_errno = 0;
239
245
        do {
240
 
                if ((num = el_getc(el, ch)) != 1)       /* if EOF or error */
241
 
                        return (num);
 
246
                if ((num = FUN(el,getc)(el, ch)) != 1) {/* if EOF or error */
 
247
                        el->el_errno = num == 0 ? 0 : errno;
 
248
                        return num;
 
249
                }
242
250
 
243
251
#ifdef  KANJI
244
252
                if ((*ch & 0200)) {
252
260
                        el->el_state.metanext = 0;
253
261
                        *ch |= 0200;
254
262
                }
255
 
                cmd = el->el_map.current[(unsigned char) *ch];
 
263
#ifdef WIDECHAR
 
264
                if (*ch >= N_KEYS)
 
265
                        cmd = ED_INSERT;
 
266
                else
 
267
#endif
 
268
                        cmd = el->el_map.current[(unsigned char) *ch];
256
269
                if (cmd == ED_SEQUENCE_LEAD_IN) {
257
 
                        key_value_t val;
258
 
                        switch (key_get(el, ch, &val)) {
 
270
                        keymacro_value_t val;
 
271
                        switch (keymacro_get(el, ch, &val)) {
259
272
                        case XK_CMD:
260
273
                                cmd = val.cmd;
261
274
                                break;
262
275
                        case XK_STR:
263
 
                                el_push(el, val.str);
 
276
                                FUN(el,push)(el, val.str);
264
277
                                break;
265
278
#ifdef notyet
266
279
                        case XK_EXE:
277
290
                        el->el_map.current = el->el_map.key;
278
291
        } while (cmd == ED_SEQUENCE_LEAD_IN);
279
292
        *cmdnum = cmd;
280
 
        return (OKCMD);
 
293
        return OKCMD;
281
294
}
282
295
 
283
 
 
284
296
/* read_char():
285
297
 *      Read a character from the tty.
286
298
 */
287
299
private int
288
 
read_char(EditLine *el, char *cp)
 
300
read_char(EditLine *el, Char *cp)
289
301
{
290
 
        int num_read;
 
302
        ssize_t num_read;
291
303
        int tried = 0;
292
 
 
293
 
        while ((num_read = read(el->el_infd, cp, 1)) == -1)
 
304
        char cbuf[MB_LEN_MAX];
 
305
        size_t cbp = 0;
 
306
        int bytes = 0;
 
307
 
 
308
#ifdef WIDECHAR
 
309
static mbstate_t state, temp_state;
 
310
memset(&state, 0, sizeof(mbstate_t));
 
311
#endif
 
312
 
 
313
 again:
 
314
        el->el_signal->sig_no = 0;
 
315
        while ((num_read = read(el->el_infd, cbuf + cbp, (size_t)1)) == -1) {
 
316
                switch (el->el_signal->sig_no) {
 
317
                case SIGCONT:
 
318
                        FUN(el,set)(el, EL_REFRESH);
 
319
                        /*FALLTHROUGH*/
 
320
                case SIGWINCH:
 
321
                        sig_set(el);
 
322
                        goto again;
 
323
                default:
 
324
                        break;
 
325
                }
294
326
                if (!tried && read__fixio(el->el_infd, errno) == 0)
295
327
                        tried = 1;
296
328
                else {
297
329
                        *cp = '\0';
298
 
                        return (-1);
 
330
                        return -1;
299
331
                }
300
 
 
301
 
        return (num_read);
 
332
        }
 
333
 
 
334
#ifdef WIDECHAR
 
335
        ++cbp;
 
336
        if (cbp > (size_t) MB_CUR_MAX) { /* "shouldn't happen" */
 
337
          *cp = '\0';
 
338
          return (-1);
 
339
        }
 
340
 
 
341
        temp_state= state;
 
342
 
 
343
        if ((bytes = mbrtowc(cp, cbuf, cbp, &state)) == -2)
 
344
        {
 
345
          /* Incomplete sequence, restore the state and scan more bytes. */
 
346
          state= temp_state;
 
347
          goto again;
 
348
        }
 
349
        else if (bytes == -1)
 
350
        {
 
351
          /* Invalid sequence, reset the state and continue. */
 
352
          cbp= 0;
 
353
          memset(&state, 0, sizeof(mbstate_t));
 
354
          goto again;
 
355
        }
 
356
        /* We successfully read one single or multi-byte character */
 
357
#else
 
358
        *cp = (unsigned char)cbuf[0];
 
359
#endif
 
360
 
 
361
#if 0
 
362
        if ((el->el_flags & IGNORE_EXTCHARS) && bytes > 1) {
 
363
                cbp = 0; /* skip this character */
 
364
                goto again;
 
365
        }
 
366
#endif
 
367
 
 
368
        return (int)num_read;
302
369
}
303
370
 
304
371
/* read_pop():
310
377
        int i;
311
378
 
312
379
        el_free(ma->macro[0]);
313
 
        for (i = ma->level--; i > 0; i--)
314
 
                ma->macro[i - 1] = ma->macro[i];
 
380
        for (i = 0; i < ma->level; i++)
 
381
                ma->macro[i] = ma->macro[i + 1];
 
382
        ma->level--;
315
383
        ma->offset = 0;
316
384
}
317
385
 
319
387
 *      Read a character
320
388
 */
321
389
public int
322
 
el_getc(EditLine *el, char *cp)
 
390
FUN(el,getc)(EditLine *el, Char *cp)
323
391
{
324
392
        int num_read;
325
393
        c_macro_t *ma = &el->el_chared.c_macro;
326
394
 
327
 
        term__flush(el);
 
395
        terminal__flush(el);
328
396
        for (;;) {
329
397
                if (ma->level < 0) {
330
398
                        if (!read_preread(el))
339
407
                        continue;
340
408
                }
341
409
 
342
 
                *cp = ma->macro[0][ma->offset++] & 0377;
 
410
                *cp = ma->macro[0][ma->offset++];
343
411
 
344
412
                if (ma->macro[0][ma->offset] == '\0') {
345
413
                        /* Needed for QuoteMode On */
346
414
                        read_pop(ma);
347
415
                }
348
416
 
349
 
                return (1);
 
417
                return 1;
350
418
        }
351
419
 
352
420
#ifdef DEBUG_READ
353
421
        (void) fprintf(el->el_errfile, "Turning raw mode on\n");
354
422
#endif /* DEBUG_READ */
355
423
        if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
356
 
                return (0);
 
424
                return 0;
357
425
 
358
426
#ifdef DEBUG_READ
359
427
        (void) fprintf(el->el_errfile, "Reading a character\n");
360
428
#endif /* DEBUG_READ */
361
429
        num_read = (*el->el_read.read_char)(el, cp);
 
430
#ifdef WIDECHAR
 
431
        if (el->el_flags & NARROW_READ)
 
432
                *cp = *(char *)(void *)cp;
 
433
#endif
362
434
#ifdef DEBUG_READ
363
435
        (void) fprintf(el->el_errfile, "Got it %c\n", *cp);
364
436
#endif /* DEBUG_READ */
365
 
        return (num_read);
 
437
        return num_read;
366
438
}
367
439
 
368
440
protected void
383
455
        re_refresh(el);         /* print the prompt */
384
456
 
385
457
        if (el->el_flags & UNBUFFERED)
386
 
                term__flush(el);
 
458
                terminal__flush(el);
387
459
}
388
460
 
389
461
protected void
395
467
                sig_clr(el);
396
468
}
397
469
 
398
 
public const char *
399
 
el_gets(EditLine *el, int *nread)
 
470
public const Char *
 
471
FUN(el,gets)(EditLine *el, int *nread)
400
472
{
401
473
        int retval;
402
474
        el_action_t cmdnum = 0;
403
475
        int num;                /* how many chars we have read at NL */
404
 
        char ch;
 
476
        Char ch, *cp;
405
477
        int crlf = 0;
 
478
        int nrb;
406
479
#ifdef FIONREAD
407
480
        c_macro_t *ma = &el->el_chared.c_macro;
408
481
#endif /* FIONREAD */
409
482
 
 
483
        if (nread == NULL)
 
484
                nread = &nrb;
 
485
        *nread = 0;
 
486
 
410
487
        if (el->el_flags & NO_TTY) {
411
 
                char *cp = el->el_line.buffer;
412
488
                size_t idx;
413
489
 
414
 
                while ((*el->el_read.read_char)(el, cp) == 1) {
 
490
                cp = el->el_line.buffer;
 
491
                while ((num = (*el->el_read.read_char)(el, cp)) == 1) {
415
492
                        /* make sure there is space for next character */
416
493
                        if (cp + 1 >= el->el_line.limit) {
417
 
                                idx = (cp - el->el_line.buffer);
418
 
                                if (!ch_enlargebufs(el, 2))
 
494
                                idx = (size_t)(cp - el->el_line.buffer);
 
495
                                if (!ch_enlargebufs(el, (size_t)2))
419
496
                                        break;
420
497
                                cp = &el->el_line.buffer[idx];
421
498
                        }
425
502
                        if (cp[-1] == '\r' || cp[-1] == '\n')
426
503
                                break;
427
504
                }
 
505
                if (num == -1) {
 
506
                        if (errno == EINTR)
 
507
                                cp = el->el_line.buffer;
 
508
                        el->el_errno = errno;
 
509
                }
428
510
 
429
 
                el->el_line.cursor = el->el_line.lastchar = cp;
430
 
                *cp = '\0';
431
 
                if (nread)
432
 
                        *nread = el->el_line.cursor - el->el_line.buffer;
433
 
                return (el->el_line.buffer);
 
511
                goto noedit;
434
512
        }
435
513
 
436
514
 
438
516
        if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
439
517
                long chrs = 0;
440
518
 
441
 
                (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
 
519
                (void) ioctl(el->el_infd, FIONREAD, &chrs);
442
520
                if (chrs == 0) {
443
521
                        if (tty_rawmode(el) < 0) {
444
 
                                if (nread)
445
 
                                        *nread = 0;
446
 
                                return (NULL);
 
522
                                errno = 0;
 
523
                                *nread = 0;
 
524
                                return NULL;
447
525
                        }
448
526
                }
449
527
        }
453
531
                read_prepare(el);
454
532
 
455
533
        if (el->el_flags & EDIT_DISABLED) {
456
 
                char *cp;
457
534
                size_t idx;
 
535
 
458
536
                if ((el->el_flags & UNBUFFERED) == 0)
459
537
                        cp = el->el_line.buffer;
460
538
                else
461
539
                        cp = el->el_line.lastchar;
462
540
 
463
 
                term__flush(el);
 
541
                terminal__flush(el);
464
542
 
465
 
                while ((*el->el_read.read_char)(el, cp) == 1) {
 
543
                while ((num = (*el->el_read.read_char)(el, cp)) == 1) {
466
544
                        /* make sure there is space next character */
467
545
                        if (cp + 1 >= el->el_line.limit) {
468
 
                                idx = (cp - el->el_line.buffer);
469
 
                                if (!ch_enlargebufs(el, 2))
 
546
                                idx = (size_t)(cp - el->el_line.buffer);
 
547
                                if (!ch_enlargebufs(el, (size_t)2))
470
548
                                        break;
471
549
                                cp = &el->el_line.buffer[idx];
472
550
                        }
473
 
                        if (*cp == 4)   /* ought to be stty eof */
474
 
                                break;
475
551
                        cp++;
476
552
                        crlf = cp[-1] == '\r' || cp[-1] == '\n';
477
553
                        if (el->el_flags & UNBUFFERED)
480
556
                                break;
481
557
                }
482
558
 
483
 
                el->el_line.cursor = el->el_line.lastchar = cp;
484
 
                *cp = '\0';
485
 
                if (nread)
486
 
                        *nread = el->el_line.cursor - el->el_line.buffer;
487
 
                return (el->el_line.buffer);
 
559
                if (num == -1) {
 
560
                        if (errno == EINTR)
 
561
                                cp = el->el_line.buffer;
 
562
                        el->el_errno = errno;
 
563
                }
 
564
 
 
565
                goto noedit;
488
566
        }
489
567
 
490
568
        for (num = OKCMD; num == OKCMD;) {      /* while still editing this
500
578
#endif /* DEBUG_READ */
501
579
                        break;
502
580
                }
 
581
                if (el->el_errno == EINTR) {
 
582
                        el->el_line.buffer[0] = '\0';
 
583
                        el->el_line.lastchar =
 
584
                            el->el_line.cursor = el->el_line.buffer;
 
585
                        break;
 
586
                }
503
587
                if ((unsigned int)cmdnum >= (unsigned int)el->el_map.nfunc) {   /* BUG CHECK command */
504
588
#ifdef DEBUG_EDIT
505
589
                        (void) fprintf(el->el_errfile,
530
614
                    el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
531
615
                        if (cmdnum == VI_DELETE_PREV_CHAR &&
532
616
                            el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
533
 
                            && el_isprint((unsigned char)el->el_chared.c_redo.pos[-1]))
 
617
                            && Isprint(el->el_chared.c_redo.pos[-1]))
534
618
                                el->el_chared.c_redo.pos--;
535
619
                        else
536
620
                                *el->el_chared.c_redo.pos++ = ch;
561
645
 
562
646
                case CC_REFRESH_BEEP:
563
647
                        re_refresh(el);
564
 
                        term_beep(el);
 
648
                        terminal_beep(el);
565
649
                        break;
566
650
 
567
651
                case CC_NORM:   /* normal char */
582
666
                        break;
583
667
 
584
668
                case CC_NEWLINE:        /* normal end of line */
585
 
                        num = el->el_line.lastchar - el->el_line.buffer;
 
669
                        num = (int)(el->el_line.lastchar - el->el_line.buffer);
586
670
                        break;
587
671
 
588
672
                case CC_FATAL:  /* fatal error, reset to known state */
593
677
                        /* put (real) cursor in a known place */
594
678
                        re_clear_display(el);   /* reset the display stuff */
595
679
                        ch_reset(el, 1);        /* reset the input pointers */
596
 
                        re_refresh(el); /* print the prompt again */
 
680
                        re_refresh(el); /* print the prompt again */
597
681
                        break;
598
682
 
599
683
                case CC_ERROR:
602
686
                        (void) fprintf(el->el_errfile,
603
687
                            "*** editor ERROR ***\r\n\n");
604
688
#endif /* DEBUG_READ */
605
 
                        term_beep(el);
606
 
                        term__flush(el);
 
689
                        terminal_beep(el);
 
690
                        terminal__flush(el);
607
691
                        break;
608
692
                }
609
693
                el->el_state.argument = 1;
613
697
                        break;
614
698
        }
615
699
 
616
 
        term__flush(el);                /* flush any buffered output */
 
700
        terminal__flush(el);            /* flush any buffered output */
617
701
        /* make sure the tty is set up correctly */
618
702
        if ((el->el_flags & UNBUFFERED) == 0) {
619
703
                read_finish(el);
620
 
                if (nread)
621
 
                        *nread = num;
 
704
                *nread = num != -1 ? num : 0;
622
705
        } else {
623
 
                if (nread)
624
 
                        *nread = el->el_line.lastchar - el->el_line.buffer;
 
706
                *nread = (int)(el->el_line.lastchar - el->el_line.buffer);
625
707
        }
626
 
        return (num ? el->el_line.buffer : NULL);
 
708
        goto done;
 
709
noedit:
 
710
        el->el_line.cursor = el->el_line.lastchar = cp;
 
711
        *cp = '\0';
 
712
        *nread = (int)(el->el_line.cursor - el->el_line.buffer);
 
713
done:
 
714
        if (*nread == 0) {
 
715
                if (num == -1) {
 
716
                        *nread = -1;
 
717
                        errno = el->el_errno;
 
718
                }
 
719
                return NULL;
 
720
        } else
 
721
                return el->el_line.buffer;
627
722
}