~ubuntu-branches/ubuntu/hardy/klibc/hardy-updates

« back to all changes in this revision

Viewing changes to ash/histedit.c

  • Committer: Bazaar Package Importer
  • Author(s): Jeff Bailey
  • Date: 2006-01-04 20:24:52 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060104202452-ec4v3n829rymukuv
Tags: 1.1.15-0ubuntu1
* New upstream version.

* Patch to fix compilation on parisc64 kernels.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*      $NetBSD: histedit.c,v 1.34 2003/10/27 06:19:29 lukem Exp $      */
2
 
 
3
 
/*-
4
 
 * Copyright (c) 1993
5
 
 *      The Regents of the University of California.  All rights reserved.
6
 
 *
7
 
 * This code is derived from software contributed to Berkeley by
8
 
 * Kenneth Almquist.
9
 
 *
10
 
 * Redistribution and use in source and binary forms, with or without
11
 
 * modification, are permitted provided that the following conditions
12
 
 * are met:
13
 
 * 1. Redistributions of source code must retain the above copyright
14
 
 *    notice, this list of conditions and the following disclaimer.
15
 
 * 2. Redistributions in binary form must reproduce the above copyright
16
 
 *    notice, this list of conditions and the following disclaimer in the
17
 
 *    documentation and/or other materials provided with the distribution.
18
 
 * 3. Neither the name of the University nor the names of its contributors
19
 
 *    may be used to endorse or promote products derived from this software
20
 
 *    without specific prior written permission.
21
 
 *
22
 
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23
 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
 
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26
 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
 
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
 
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
 
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
 
 * SUCH DAMAGE.
33
 
 */
34
 
 
35
 
#ifndef __KLIBC__
36
 
#include <sys/cdefs.h>
37
 
#endif
38
 
#ifndef __RCSID
39
 
#define __RCSID(arg)
40
 
#endif
41
 
#ifndef lint
42
 
#if 0
43
 
static char sccsid[] = "@(#)histedit.c  8.2 (Berkeley) 5/4/95";
44
 
#else
45
 
__RCSID("$NetBSD: histedit.c,v 1.34 2003/10/27 06:19:29 lukem Exp $");
46
 
#endif
47
 
#endif /* not lint */
48
 
 
49
 
#include <sys/param.h>
50
 
#ifndef __KLIBC__
51
 
#include <paths.h>
52
 
#endif
53
 
#include <stdio.h>
54
 
#include <stdlib.h>
55
 
#include <unistd.h>
56
 
/*
57
 
 * Editline and history functions (and glue).
58
 
 */
59
 
#include "shell.h"
60
 
#include "parser.h"
61
 
#include "var.h"
62
 
#include "options.h"
63
 
#include "main.h"
64
 
#include "output.h"
65
 
#include "mystring.h"
66
 
#include "error.h"
67
 
#ifdef KLIBC_SH_HISTORY
68
 
#include "myhistedit.h"
69
 
#include "eval.h"
70
 
#include "memalloc.h"
71
 
 
72
 
#ifdef __linux__
73
 
#include "compat_linux.h"
74
 
#endif
75
 
 
76
 
#define MAXHISTLOOPS    4       /* max recursions through fc */
77
 
#define DEFEDITOR       "ed"    /* default editor *should* be $EDITOR */
78
 
 
79
 
History *hist;  /* history cookie */
80
 
EditLine *el;   /* editline cookie */
81
 
int displayhist;
82
 
static FILE *el_in, *el_out;
83
 
 
84
 
STATIC const char *fc_replace(const char *, char *, char *);
85
 
 
86
 
#ifdef DEBUG
87
 
extern FILE *tracefile;
88
 
#endif
89
 
 
90
 
/*
91
 
 * Set history and editing status.  Called whenever the status may
92
 
 * have changed (figures out what to do).
93
 
 */
94
 
void
95
 
histedit(void)
96
 
{
97
 
        FILE *el_err;
98
 
 
99
 
#define editing (Eflag || Vflag)
100
 
 
101
 
        if (iflag) {
102
 
                if (!hist) {
103
 
                        /*
104
 
                         * turn history on
105
 
                         */
106
 
                        INTOFF;
107
 
                        hist = history_init();
108
 
                        INTON;
109
 
 
110
 
                        if (hist != NULL)
111
 
                                sethistsize(histsizeval());
112
 
                        else
113
 
                                out2str("sh: can't initialize history\n");
114
 
                }
115
 
                if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
116
 
                        /*
117
 
                         * turn editing on
118
 
                         */
119
 
                        char *term, *shname;
120
 
 
121
 
                        INTOFF;
122
 
                        if (el_in == NULL)
123
 
                                el_in = fdopen(0, "r");
124
 
                        if (el_out == NULL)
125
 
                                el_out = fdopen(2, "w");
126
 
                        if (el_in == NULL || el_out == NULL)
127
 
                                goto bad;
128
 
                        el_err = el_out;
129
 
#if DEBUG
130
 
                        if (tracefile)
131
 
                                el_err = tracefile;
132
 
#endif
133
 
                        term = lookupvar("TERM");
134
 
                        if (term)
135
 
                                setenv("TERM", term, 1);
136
 
                        else
137
 
                                unsetenv("TERM");
138
 
                        shname = arg0;
139
 
                        if (shname[0] == '-')
140
 
                                shname++;
141
 
                        el = el_init(shname, el_in, el_out, el_err);
142
 
                        if (el != NULL) {
143
 
                                if (hist)
144
 
                                        el_set(el, EL_HIST, history, hist);
145
 
                                el_set(el, EL_PROMPT, getprompt);
146
 
                                el_set(el, EL_SIGNAL, 1);
147
 
                        } else {
148
 
bad:
149
 
                                out2str("sh: can't initialize editing\n");
150
 
                        }
151
 
                        INTON;
152
 
                } else if (!editing && el) {
153
 
                        INTOFF;
154
 
                        el_end(el);
155
 
                        el = NULL;
156
 
                        INTON;
157
 
                }
158
 
                if (el) {
159
 
                        if (Vflag)
160
 
                                el_set(el, EL_EDITOR, "vi");
161
 
                        else if (Eflag)
162
 
                                el_set(el, EL_EDITOR, "emacs");
163
 
                        el_source(el, NULL);
164
 
                }
165
 
        } else {
166
 
                INTOFF;
167
 
                if (el) {       /* no editing if not interactive */
168
 
                        el_end(el);
169
 
                        el = NULL;
170
 
                }
171
 
                if (hist) {
172
 
                        history_end(hist);
173
 
                        hist = NULL;
174
 
                }
175
 
                INTON;
176
 
        }
177
 
}
178
 
 
179
 
 
180
 
void
181
 
sethistsize(const char *hs)
182
 
{
183
 
        int histsize;
184
 
        HistEvent he;
185
 
 
186
 
        if (hist != NULL) {
187
 
                if (hs == NULL || *hs == '\0' ||
188
 
                   (histsize = atoi(hs)) < 0)
189
 
                        histsize = 100;
190
 
                history(hist, &he, H_SETSIZE, histsize);
191
 
        }
192
 
}
193
 
 
194
 
void
195
 
setterm(const char *term)
196
 
{
197
 
        if (el != NULL && term != NULL)
198
 
                if (el_set(el, EL_TERMINAL, term) != 0) {
199
 
                        outfmt(out2, "sh: Can't set terminal type %s\n", term);
200
 
                        outfmt(out2, "sh: Using dumb terminal settings.\n");
201
 
                }
202
 
}
203
 
 
204
 
int
205
 
inputrc(argc, argv)
206
 
        int argc;
207
 
        char **argv;
208
 
{
209
 
        if (argc != 2) {
210
 
                out2str("usage: inputrc file\n");
211
 
                return 1;
212
 
        }
213
 
        if (el != NULL) {
214
 
                if (el_source(el, argv[1])) {
215
 
                        out2str("inputrc: failed\n");
216
 
                        return 1;
217
 
                } else
218
 
                        return 0;
219
 
        } else {
220
 
                out2str("sh: inputrc ignored, not editing\n");
221
 
                return 1;
222
 
        }
223
 
}
224
 
 
225
 
/*
226
 
 *  This command is provided since POSIX decided to standardize
227
 
 *  the Korn shell fc command.  Oh well...
228
 
 */
229
 
int
230
 
histcmd(int argc, char **argv)
231
 
{
232
 
        int ch;
233
 
        const char *editor = NULL;
234
 
        HistEvent he;
235
 
        int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
236
 
        int i, retval;
237
 
        const char *firststr, *laststr;
238
 
        int first, last, direction;
239
 
        char *pat = NULL, *repl;        /* ksh "fc old=new" crap */
240
 
        static int active = 0;
241
 
        struct jmploc jmploc;
242
 
        struct jmploc *volatile savehandler;
243
 
        char editfile[MAXPATHLEN + 1];
244
 
        FILE *efp;
245
 
#ifdef __GNUC__
246
 
        /* Avoid longjmp clobbering */
247
 
        (void) &editor;
248
 
        (void) &lflg;
249
 
        (void) &nflg;
250
 
        (void) &rflg;
251
 
        (void) &sflg;
252
 
        (void) &firststr;
253
 
        (void) &laststr;
254
 
        (void) &pat;
255
 
        (void) &repl;
256
 
        (void) &efp;
257
 
        (void) &argc;
258
 
        (void) &argv;
259
 
#endif
260
 
 
261
 
        if (hist == NULL)
262
 
                error("history not active");
263
 
 
264
 
        if (argc == 1)
265
 
                error("missing history argument");
266
 
 
267
 
#ifndef __linux__
268
 
        optreset = 1; /* initialize getopt */
269
 
#endif
270
 
        optind = 1; /* initialize getopt */
271
 
        while (not_fcnumber(argv[optind]) &&
272
 
              (ch = getopt(argc, argv, ":e:lnrs")) != -1)
273
 
                switch ((char)ch) {
274
 
                case 'e':
275
 
                        editor = optionarg;
276
 
                        break;
277
 
                case 'l':
278
 
                        lflg = 1;
279
 
                        break;
280
 
                case 'n':
281
 
                        nflg = 1;
282
 
                        break;
283
 
                case 'r':
284
 
                        rflg = 1;
285
 
                        break;
286
 
                case 's':
287
 
                        sflg = 1;
288
 
                        break;
289
 
                case ':':
290
 
                        error("option -%c expects argument", optopt);
291
 
                        /* NOTREACHED */
292
 
                case '?':
293
 
                default:
294
 
                        error("unknown option: -%c", optopt);
295
 
                        /* NOTREACHED */
296
 
                }
297
 
        argc -= optind, argv += optind;
298
 
 
299
 
        /*
300
 
         * If executing...
301
 
         */
302
 
        if (lflg == 0 || editor || sflg) {
303
 
                lflg = 0;       /* ignore */
304
 
                editfile[0] = '\0';
305
 
                /*
306
 
                 * Catch interrupts to reset active counter and
307
 
                 * cleanup temp files.
308
 
                 */
309
 
                if (setjmp(jmploc.loc)) {
310
 
                        active = 0;
311
 
                        if (*editfile)
312
 
                                unlink(editfile);
313
 
                        handler = savehandler;
314
 
                        longjmp(handler->loc, 1);
315
 
                }
316
 
                savehandler = handler;
317
 
                handler = &jmploc;
318
 
                if (++active > MAXHISTLOOPS) {
319
 
                        active = 0;
320
 
                        displayhist = 0;
321
 
                        error("called recursively too many times");
322
 
                }
323
 
                /*
324
 
                 * Set editor.
325
 
                 */
326
 
                if (sflg == 0) {
327
 
                        if (editor == NULL &&
328
 
                            (editor = bltinlookup("FCEDIT", 1)) == NULL &&
329
 
                            (editor = bltinlookup("EDITOR", 1)) == NULL)
330
 
                                editor = DEFEDITOR;
331
 
                        if (editor[0] == '-' && editor[1] == '\0') {
332
 
                                sflg = 1;       /* no edit */
333
 
                                editor = NULL;
334
 
                        }
335
 
                }
336
 
        }
337
 
 
338
 
        /*
339
 
         * If executing, parse [old=new] now
340
 
         */
341
 
        if (lflg == 0 && argc > 0 &&
342
 
             ((repl = strchr(argv[0], '=')) != NULL)) {
343
 
                pat = argv[0];
344
 
                *repl++ = '\0';
345
 
                argc--, argv++;
346
 
        }
347
 
        /*
348
 
         * determine [first] and [last]
349
 
         */
350
 
        switch (argc) {
351
 
        case 0:
352
 
                firststr = lflg ? "-16" : "-1";
353
 
                laststr = "-1";
354
 
                break;
355
 
        case 1:
356
 
                firststr = argv[0];
357
 
                laststr = lflg ? "-1" : argv[0];
358
 
                break;
359
 
        case 2:
360
 
                firststr = argv[0];
361
 
                laststr = argv[1];
362
 
                break;
363
 
        default:
364
 
                error("too many args");
365
 
                /* NOTREACHED */
366
 
        }
367
 
        /*
368
 
         * Turn into event numbers.
369
 
         */
370
 
        first = str_to_event(firststr, 0);
371
 
        last = str_to_event(laststr, 1);
372
 
 
373
 
        if (rflg) {
374
 
                i = last;
375
 
                last = first;
376
 
                first = i;
377
 
        }
378
 
        /*
379
 
         * XXX - this should not depend on the event numbers
380
 
         * always increasing.  Add sequence numbers or offset
381
 
         * to the history element in next (diskbased) release.
382
 
         */
383
 
        direction = first < last ? H_PREV : H_NEXT;
384
 
 
385
 
        /*
386
 
         * If editing, grab a temp file.
387
 
         */
388
 
        if (editor) {
389
 
                int fd;
390
 
                INTOFF;         /* easier */
391
 
                snprintf(editfile, sizeof(editfile), "%s_shXXXXXX", _PATH_TMP);
392
 
                if ((fd = mkstemp(editfile)) < 0)
393
 
                        error("can't create temporary file %s", editfile);
394
 
                if ((efp = fdopen(fd, "w")) == NULL) {
395
 
                        close(fd);
396
 
                        error("can't allocate stdio buffer for temp");
397
 
                }
398
 
        }
399
 
 
400
 
        /*
401
 
         * Loop through selected history events.  If listing or executing,
402
 
         * do it now.  Otherwise, put into temp file and call the editor
403
 
         * after.
404
 
         *
405
 
         * The history interface needs rethinking, as the following
406
 
         * convolutions will demonstrate.
407
 
         */
408
 
        history(hist, &he, H_FIRST);
409
 
        retval = history(hist, &he, H_NEXT_EVENT, first);
410
 
        for (;retval != -1; retval = history(hist, &he, direction)) {
411
 
                if (lflg) {
412
 
                        if (!nflg)
413
 
                                out1fmt("%5d ", he.num);
414
 
                        out1str(he.str);
415
 
                } else {
416
 
                        const char *s = pat ?
417
 
                           fc_replace(he.str, pat, repl) : he.str;
418
 
 
419
 
                        if (sflg) {
420
 
                                if (displayhist) {
421
 
                                        out2str(s);
422
 
                                }
423
 
 
424
 
                                evalstring(strcpy(stalloc(strlen(s) + 1), s), 0);
425
 
                                if (displayhist && hist) {
426
 
                                        /*
427
 
                                         *  XXX what about recursive and
428
 
                                         *  relative histnums.
429
 
                                         */
430
 
                                        history(hist, &he, H_ENTER, s);
431
 
                                }
432
 
                        } else
433
 
                                fputs(s, efp);
434
 
                }
435
 
                /*
436
 
                 * At end?  (if we were to lose last, we'd sure be
437
 
                 * messed up).
438
 
                 */
439
 
                if (he.num == last)
440
 
                        break;
441
 
        }
442
 
        if (editor) {
443
 
                char *editcmd;
444
 
 
445
 
                fclose(efp);
446
 
                editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
447
 
                sprintf(editcmd, "%s %s", editor, editfile);
448
 
                evalstring(editcmd, 0); /* XXX - should use no JC command */
449
 
                INTON;
450
 
                readcmdfile(editfile);  /* XXX - should read back - quick tst */
451
 
                unlink(editfile);
452
 
        }
453
 
 
454
 
        if (lflg == 0 && active > 0)
455
 
                --active;
456
 
        if (displayhist)
457
 
                displayhist = 0;
458
 
        return 0;
459
 
}
460
 
 
461
 
STATIC const char *
462
 
fc_replace(const char *s, char *p, char *r)
463
 
{
464
 
        char *dest;
465
 
        int plen = strlen(p);
466
 
 
467
 
        STARTSTACKSTR(dest);
468
 
        while (*s) {
469
 
                if (*s == *p && strncmp(s, p, plen) == 0) {
470
 
                        while (*r)
471
 
                                STPUTC(*r++, dest);
472
 
                        s += plen;
473
 
                        *p = '\0';      /* so no more matches */
474
 
                } else
475
 
                        STPUTC(*s++, dest);
476
 
        }
477
 
        STACKSTRNUL(dest);
478
 
        dest = grabstackstr(dest);
479
 
 
480
 
        return (dest);
481
 
}
482
 
 
483
 
int
484
 
not_fcnumber(char *s)
485
 
{
486
 
        if (s == NULL)
487
 
                return 0;
488
 
        if (*s == '-')
489
 
                s++;
490
 
        return (!is_number(s));
491
 
}
492
 
 
493
 
int
494
 
str_to_event(const char *str, int last)
495
 
{
496
 
        HistEvent he;
497
 
        const char *s = str;
498
 
        int relative = 0;
499
 
        int i, retval;
500
 
 
501
 
        retval = history(hist, &he, H_FIRST);
502
 
        switch (*s) {
503
 
        case '-':
504
 
                relative = 1;
505
 
                /*FALLTHROUGH*/
506
 
        case '+':
507
 
                s++;
508
 
        }
509
 
        if (is_number(s)) {
510
 
                i = atoi(s);
511
 
                if (relative) {
512
 
                        while (retval != -1 && i--) {
513
 
                                retval = history(hist, &he, H_NEXT);
514
 
                        }
515
 
                        if (retval == -1)
516
 
                                retval = history(hist, &he, H_LAST);
517
 
                } else {
518
 
                        retval = history(hist, &he, H_NEXT_EVENT, i);
519
 
                        if (retval == -1) {
520
 
                                /*
521
 
                                 * the notion of first and last is
522
 
                                 * backwards to that of the history package
523
 
                                 */
524
 
                                retval = history(hist, &he,
525
 
                                                last ? H_FIRST : H_LAST);
526
 
                        }
527
 
                }
528
 
                if (retval == -1)
529
 
                        error("history number %s not found (internal error)",
530
 
                               str);
531
 
        } else {
532
 
                /*
533
 
                 * pattern
534
 
                 */
535
 
                retval = history(hist, &he, H_PREV_STR, str);
536
 
                if (retval == -1)
537
 
                        error("history pattern not found: %s", str);
538
 
        }
539
 
        return (he.num);
540
 
}
541
 
#endif /* KLIBC_SH_HISTORY */