~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/3rdparty/qtcsh/tw.init.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * tw.init.c: Handle lists of things to complete
 
3
 */
 
4
/*-
 
5
 * Copyright (c) 1980, 1991 The Regents of the University of California.
 
6
 * All rights reserved.
 
7
 *
 
8
 * Redistribution and use in source and binary forms, with or without
 
9
 * modification, are permitted provided that the following conditions
 
10
 * are met:
 
11
 * 1. Redistributions of source code must retain the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer.
 
13
 * 2. Redistributions in binary form must reproduce the above copyright
 
14
 *    notice, this list of conditions and the following disclaimer in the
 
15
 *    documentation and/or other materials provided with the distribution.
 
16
 * 3. All advertising materials mentioning features or use of this software
 
17
 *    must display the following acknowledgement:
 
18
 *      This product includes software developed by the University of
 
19
 *      California, Berkeley and its contributors.
 
20
 * 4. Neither the name of the University nor the names of its contributors
 
21
 *    may be used to endorse or promote products derived from this software
 
22
 *    without specific prior written permission.
 
23
 *
 
24
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 
25
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
26
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
27
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 
28
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
29
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
30
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
31
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
33
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
34
 * SUCH DAMAGE.
 
35
 */
 
36
#include "sh.h"
 
37
 
 
38
RCSID("$Id$")
 
39
 
 
40
#include "tw.h"
 
41
#include "ed.h"
 
42
#include "tc.h"
 
43
#include "sh.proc.h"
 
44
 
 
45
#if !defined(NSIG) && defined(SIGMAX)
 
46
# define NSIG (SIGMAX+1)
 
47
#endif /* !NSIG && SIGMAX */
 
48
#if !defined(NSIG) && defined(_NSIG)
 
49
# define NSIG _NSIG
 
50
#endif /* !NSIG && _NSIG */
 
51
 
 
52
#define TW_INCR 128
 
53
 
 
54
typedef struct {
 
55
    Char **list,                        /* List of command names        */
 
56
          *buff;                        /* Space holding command names  */
 
57
    int    nlist,                       /* Number of items              */
 
58
           nbuff,                       /* Current space in name buf    */
 
59
           tlist,                       /* Total space in list          */
 
60
           tbuff;                       /* Total space in name buf      */
 
61
} stringlist_t;
 
62
 
 
63
 
 
64
static struct varent *tw_vptr = NULL;   /* Current shell variable       */
 
65
static Char **tw_env = NULL;            /* Current environment variable */
 
66
static Char  *tw_word;                  /* Current word pointer         */
 
67
static struct KeyFuncs *tw_bind = NULL; /* List of the bindings         */
 
68
#ifndef HAVENOLIMIT
 
69
static struct limits *tw_limit = NULL;  /* List of the resource limits  */
 
70
#endif /* HAVENOLIMIT */
 
71
static int tw_index = 0;                /* signal and job index         */
 
72
static DIR   *tw_dir_fd = NULL;         /* Current directory descriptor */
 
73
static Char   tw_retname[MAXPATHLEN+1]; /* Return buffer                */
 
74
static int    tw_cmd_got = 0;           /* What we need to do           */
 
75
static stringlist_t tw_cmd  = { NULL, NULL, 0, 0, 0, 0 };
 
76
static stringlist_t tw_item = { NULL, NULL, 0, 0, 0, 0 };
 
77
#define TW_FL_CMD       0x01
 
78
#define TW_FL_ALIAS     0x02
 
79
#define TW_FL_BUILTIN   0x04
 
80
#define TW_FL_SORT      0x08
 
81
#define TW_FL_REL       0x10
 
82
 
 
83
static struct {                         /* Current element pointer      */
 
84
    int    cur;                         /* Current element number       */
 
85
    Char **pathv;                       /* Current element in path      */
 
86
    DIR   *dfd;                         /* Current directory descriptor */
 
87
} tw_cmd_state;
 
88
 
 
89
 
 
90
#ifdef BSDSIGS
 
91
static sigmask_t tw_omask;
 
92
# define TW_HOLD()      tw_omask = sigblock(sigmask(SIGINT))
 
93
# define TW_RELS()      (void) sigsetmask(tw_omask)
 
94
#else /* !BSDSIGS */
 
95
# define TW_HOLD()      (void) sighold(SIGINT)
 
96
# define TW_RELS()      (void) sigrelse(SIGINT)
 
97
#endif /* BSDSIGS */
 
98
 
 
99
#define SETDIR(dfd) \
 
100
    { \
 
101
        tw_dir_fd = dfd; \
 
102
        if (tw_dir_fd != NULL) \
 
103
            rewinddir(tw_dir_fd); \
 
104
    }
 
105
 
 
106
#define CLRDIR(dfd) \
 
107
    if (dfd != NULL) { \
 
108
        TW_HOLD(); \
 
109
        (void) closedir(dfd); \
 
110
        dfd = NULL; \
 
111
        TW_RELS(); \
 
112
    }
 
113
 
 
114
static Char     *tw_str_add             __P((stringlist_t *, int));
 
115
static void      tw_str_free            __P((stringlist_t *));
 
116
static Char     *tw_dir_next            __P((DIR *));
 
117
static void      tw_cmd_add             __P((Char *name));
 
118
static void      tw_cmd_cmd             __P((void));
 
119
static void      tw_cmd_builtin         __P((void));
 
120
static void      tw_cmd_alias           __P((void));
 
121
static void      tw_cmd_sort            __P((void));
 
122
static void      tw_vptr_start          __P((struct varent *));
 
123
 
 
124
 
 
125
/* tw_str_add():
 
126
 *      Add an item to the string list
 
127
 */
 
128
static Char *
 
129
tw_str_add(sl, len)
 
130
    stringlist_t *sl;
 
131
    int len;
 
132
{
 
133
    Char *ptr;
 
134
 
 
135
    if (sl->tlist <= sl->nlist) {
 
136
        TW_HOLD();
 
137
        sl->tlist += TW_INCR;
 
138
        sl->list = sl->list ? 
 
139
                    (Char **) xrealloc((ptr_t) sl->list, 
 
140
                                       (size_t) (sl->tlist * sizeof(Char *))) :
 
141
                    (Char **) xmalloc((size_t) (sl->tlist * sizeof(Char *)));
 
142
        TW_RELS();
 
143
    }
 
144
    if (sl->tbuff <= sl->nbuff + len) {
 
145
        int i;
 
146
        ptr = sl->buff;
 
147
 
 
148
        TW_HOLD();
 
149
        sl->tbuff += TW_INCR + len;
 
150
        sl->buff = sl->buff ? 
 
151
                    (Char *) xrealloc((ptr_t) sl->buff, 
 
152
                                      (size_t) (sl->tbuff * sizeof(Char))) :
 
153
                    (Char *) xmalloc((size_t) (sl->tbuff * sizeof(Char)));
 
154
        /* Re-thread the new pointer list, if changed */
 
155
        if (ptr != NULL && ptr != sl->buff) {
 
156
            int offs = (int) (sl->buff - ptr);
 
157
            for (i = 0; i < sl->nlist; i++)
 
158
                sl->list[i] += offs;
 
159
        }
 
160
        TW_RELS();
 
161
    }
 
162
    ptr = sl->list[sl->nlist++] = &sl->buff[sl->nbuff];
 
163
    sl->nbuff += len;
 
164
    return ptr;
 
165
} /* tw_str_add */
 
166
 
 
167
 
 
168
/* tw_str_free():
 
169
 *      Free a stringlist
 
170
 */
 
171
static void
 
172
tw_str_free(sl)
 
173
    stringlist_t *sl;
 
174
{
 
175
    TW_HOLD();
 
176
    if (sl->list) {
 
177
        xfree((ptr_t) sl->list);
 
178
        sl->list = NULL;
 
179
        sl->tlist = sl->nlist = 0;
 
180
    }
 
181
    if (sl->buff) {
 
182
        xfree((ptr_t) sl->buff);
 
183
        sl->buff = NULL;
 
184
        sl->tbuff = sl->nbuff = 0;
 
185
    }
 
186
    TW_RELS();
 
187
} /* end tw_str_free */
 
188
 
 
189
 
 
190
static Char *
 
191
tw_dir_next(dfd)
 
192
    DIR *dfd;
 
193
{
 
194
    register struct dirent *dirp;
 
195
 
 
196
    if (dfd == NULL)
 
197
        return NULL;
 
198
 
 
199
    if ((dirp = readdir(dfd)) != NULL) {
 
200
        (void) Strcpy(tw_retname, str2short(dirp->d_name));
 
201
        return (tw_retname);
 
202
    }
 
203
    return NULL;
 
204
} /* end tw_dir_next */
 
205
 
 
206
 
 
207
/* tw_cmd_add():
 
208
 *      Add the name to the command list
 
209
 */
 
210
static void
 
211
tw_cmd_add(name)
 
212
    Char *name;
 
213
{
 
214
    int len;
 
215
 
 
216
    len = (int) Strlen(name) + 2;
 
217
    (void) Strcpy(tw_str_add(&tw_cmd, len), name);
 
218
} /* end tw_cmd_add */
 
219
 
 
220
 
 
221
/* tw_cmd_free():
 
222
 *      Free the command list
 
223
 */
 
224
void
 
225
tw_cmd_free()
 
226
{
 
227
    CLRDIR(tw_dir_fd)
 
228
    tw_str_free(&tw_cmd);
 
229
    tw_cmd_got = 0;
 
230
} /* end tw_cmd_free */
 
231
 
 
232
/* tw_cmd_cmd():
 
233
 *      Add system commands to the command list
 
234
 */
 
235
static void
 
236
tw_cmd_cmd()
 
237
{
 
238
    register DIR *dirp;
 
239
    register struct dirent *dp;
 
240
    register Char *dir = NULL, *name;
 
241
    register Char **pv;
 
242
    struct varent *v = adrof(STRpath);
 
243
    struct varent *recexec = adrof(STRrecognize_only_executables);
 
244
    int len;
 
245
 
 
246
 
 
247
    if (v == NULL) /* if no path */
 
248
        return;
 
249
 
 
250
    for (pv = v->vec; *pv; pv++) {
 
251
        if (pv[0][0] != '/') {
 
252
            tw_cmd_got |= TW_FL_REL;
 
253
            continue;
 
254
        }
 
255
 
 
256
        if ((dirp = opendir(short2str(*pv))) == NULL)
 
257
            continue;
 
258
 
 
259
        if (recexec)
 
260
            dir = Strspl(*pv, STRslash);
 
261
        while ((dp = readdir(dirp)) != NULL) {
 
262
            /* the call to executable() may make this a bit slow */
 
263
            name = str2short(dp->d_name);
 
264
#ifdef INTERIX
 
265
       if (dp->d_ino == 0 || !executable(dir, name, 0))
 
266
#else
 
267
            if (dp->d_ino == 0 || (recexec && !executable(dir, name, 0)))
 
268
#endif
 
269
                continue;
 
270
            len = (int) Strlen(name) + 2;
 
271
            if (name[0] == '#' ||       /* emacs temp files     */
 
272
                name[0] == '.' ||       /* .files               */
 
273
                name[len - 3] == '~' || /* emacs backups        */
 
274
                name[len - 3] == '%')   /* textedit backups     */
 
275
                continue;               /* Ignore!              */
 
276
            tw_cmd_add(name);
 
277
        }
 
278
        (void) closedir(dirp);
 
279
        if (recexec)
 
280
            xfree((ptr_t) dir);
 
281
    }
 
282
} /* end tw_cmd_cmd */
 
283
 
 
284
 
 
285
/* tw_cmd_builtin():
 
286
 *      Add builtins to the command list
 
287
 */
 
288
static void
 
289
tw_cmd_builtin()
 
290
{
 
291
    register struct biltins *bptr;
 
292
 
 
293
    for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++)
 
294
        if (bptr->bname)
 
295
            tw_cmd_add(str2short(bptr->bname));
 
296
#ifdef WINNT
 
297
    for (bptr = nt_bfunc; bptr < &nt_bfunc[nt_nbfunc]; bptr++)
 
298
        if (bptr->bname)
 
299
            tw_cmd_add(str2short(bptr->bname));
 
300
#endif /* WINNT*/
 
301
} /* end tw_cmd_builtin */
 
302
 
 
303
 
 
304
/* tw_cmd_alias():
 
305
 *      Add aliases to the command list
 
306
 */
 
307
static void
 
308
tw_cmd_alias()
 
309
{
 
310
    register struct varent *p;
 
311
    register struct varent *c;
 
312
 
 
313
    p = &aliases;
 
314
    for (;;) {
 
315
        while (p->v_left)
 
316
            p = p->v_left;
 
317
x:
 
318
        if (p->v_parent == 0) /* is it the header? */
 
319
            return;
 
320
        if (p->v_name)
 
321
            tw_cmd_add(p->v_name);
 
322
        if (p->v_right) {
 
323
            p = p->v_right;
 
324
            continue;
 
325
        }
 
326
        do {
 
327
            c = p;
 
328
            p = p->v_parent;
 
329
        } while (p->v_right == c);
 
330
        goto x;
 
331
    }
 
332
} /* end tw_cmd_alias */
 
333
 
 
334
 
 
335
/* tw_cmd_sort():
 
336
 *      Sort the command list removing duplicate elements
 
337
 */
 
338
static void
 
339
tw_cmd_sort()
 
340
{
 
341
    int fwd, i;
 
342
 
 
343
    TW_HOLD();
 
344
    /* sort the list. */
 
345
    qsort((ptr_t) tw_cmd.list, (size_t) tw_cmd.nlist, sizeof(Char *), 
 
346
          (int (*) __P((const void *, const void *))) fcompare);
 
347
 
 
348
    /* get rid of multiple entries */
 
349
    for (i = 0, fwd = 0; i < tw_cmd.nlist - 1; i++) {
 
350
        if (Strcmp(tw_cmd.list[i], tw_cmd.list[i + 1]) == 0) /* garbage */
 
351
            fwd++;              /* increase the forward ref. count */
 
352
        else if (fwd) 
 
353
            tw_cmd.list[i - fwd] = tw_cmd.list[i];
 
354
    }
 
355
    /* Fix fencepost error -- Theodore Ts'o <tytso@athena.mit.edu> */
 
356
    if (fwd)
 
357
        tw_cmd.list[i - fwd] = tw_cmd.list[i];
 
358
    tw_cmd.nlist -= fwd;
 
359
    TW_RELS();
 
360
} /* end tw_cmd_sort */
 
361
 
 
362
 
 
363
/* tw_cmd_start():
 
364
 *      Get the command list and sort it, if not done yet.
 
365
 *      Reset the current pointer to the beginning of the command list
 
366
 */
 
367
/*ARGSUSED*/
 
368
void
 
369
tw_cmd_start(dfd, pat)
 
370
    DIR *dfd;
 
371
    Char *pat;
 
372
{
 
373
    static Char *defpath[] = { STRNULL, 0 };
 
374
    USE(pat);
 
375
    SETDIR(dfd)
 
376
    if ((tw_cmd_got & TW_FL_CMD) == 0) {
 
377
        tw_cmd_free();
 
378
        tw_cmd_cmd();
 
379
        tw_cmd_got |= TW_FL_CMD;
 
380
    }
 
381
    if ((tw_cmd_got & TW_FL_ALIAS) == 0) {
 
382
        tw_cmd_alias();
 
383
        tw_cmd_got &= ~TW_FL_SORT;
 
384
        tw_cmd_got |= TW_FL_ALIAS;
 
385
    }
 
386
    if ((tw_cmd_got & TW_FL_BUILTIN) == 0) {
 
387
        tw_cmd_builtin();
 
388
        tw_cmd_got &= ~TW_FL_SORT;
 
389
        tw_cmd_got |= TW_FL_BUILTIN;
 
390
    }
 
391
    if ((tw_cmd_got & TW_FL_SORT) == 0) {
 
392
        tw_cmd_sort();
 
393
        tw_cmd_got |= TW_FL_SORT;
 
394
    }
 
395
 
 
396
    tw_cmd_state.cur = 0;
 
397
    CLRDIR(tw_cmd_state.dfd)
 
398
    if (tw_cmd_got & TW_FL_REL) {
 
399
        struct varent *vp = adrof(STRpath);
 
400
        if (vp && vp->vec)
 
401
            tw_cmd_state.pathv = vp->vec;
 
402
        else
 
403
            tw_cmd_state.pathv = defpath;
 
404
    }
 
405
    else 
 
406
        tw_cmd_state.pathv = defpath;
 
407
} /* tw_cmd_start */
 
408
 
 
409
 
 
410
/* tw_cmd_next():
 
411
 *      Return the next element in the command list or
 
412
 *      Look for commands in the relative path components
 
413
 */
 
414
Char *
 
415
tw_cmd_next(dir, flags)
 
416
    Char *dir;
 
417
    int  *flags;
 
418
{
 
419
    Char *ptr = NULL;
 
420
 
 
421
    if (tw_cmd_state.cur < tw_cmd.nlist) {
 
422
        *flags = TW_DIR_OK;
 
423
        return tw_cmd.list[tw_cmd_state.cur++];
 
424
    }
 
425
 
 
426
    /*
 
427
     * We need to process relatives in the path.
 
428
     */
 
429
    while (((tw_cmd_state.dfd == NULL) ||
 
430
            ((ptr = tw_dir_next(tw_cmd_state.dfd)) == NULL)) &&
 
431
           (*tw_cmd_state.pathv != NULL)) {
 
432
 
 
433
        CLRDIR(tw_cmd_state.dfd)
 
434
 
 
435
        while (*tw_cmd_state.pathv && tw_cmd_state.pathv[0][0] == '/')
 
436
            tw_cmd_state.pathv++;
 
437
        if ((ptr = *tw_cmd_state.pathv) != 0) {
 
438
            /*
 
439
             * We complete directories only on '.' should that
 
440
             * be changed?
 
441
             */
 
442
            if (ptr[0] == '\0' || (ptr[0] == '.' && ptr[1] == '\0')) {
 
443
                *dir = '\0';
 
444
                tw_cmd_state.dfd = opendir(".");
 
445
                *flags = TW_DIR_OK | TW_EXEC_CHK;       
 
446
            }
 
447
            else {
 
448
                copyn(dir, *tw_cmd_state.pathv, FILSIZ);
 
449
                catn(dir, STRslash, FILSIZ);
 
450
                tw_cmd_state.dfd = opendir(short2str(*tw_cmd_state.pathv));
 
451
                *flags = TW_EXEC_CHK;
 
452
            }
 
453
            tw_cmd_state.pathv++;
 
454
        }
 
455
    }
 
456
    return ptr;
 
457
} /* end tw_cmd_next */
 
458
 
 
459
 
 
460
/* tw_vptr_start():
 
461
 *      Find the first variable in the variable list
 
462
 */
 
463
static void
 
464
tw_vptr_start(c)
 
465
    struct varent *c;
 
466
{
 
467
    tw_vptr = c;                /* start at beginning of variable list */
 
468
 
 
469
    for (;;) {
 
470
        while (tw_vptr->v_left)
 
471
            tw_vptr = tw_vptr->v_left;
 
472
x:
 
473
        if (tw_vptr->v_parent == 0) {   /* is it the header? */
 
474
            tw_vptr = NULL;
 
475
            return;
 
476
        }
 
477
        if (tw_vptr->v_name)
 
478
            return;             /* found first one */
 
479
        if (tw_vptr->v_right) {
 
480
            tw_vptr = tw_vptr->v_right;
 
481
            continue;
 
482
        }
 
483
        do {
 
484
            c = tw_vptr;
 
485
            tw_vptr = tw_vptr->v_parent;
 
486
        } while (tw_vptr->v_right == c);
 
487
        goto x;
 
488
    }
 
489
} /* end tw_shvar_start */
 
490
 
 
491
 
 
492
/* tw_shvar_next():
 
493
 *      Return the next shell variable
 
494
 */
 
495
/*ARGSUSED*/
 
496
Char *
 
497
tw_shvar_next(dir, flags)
 
498
    Char *dir;
 
499
    int  *flags;
 
500
{
 
501
    register struct varent *p;
 
502
    register struct varent *c;
 
503
    register Char *cp;
 
504
 
 
505
    USE(flags);
 
506
    USE(dir);
 
507
    if ((p = tw_vptr) == NULL)
 
508
        return (NULL);          /* just in case */
 
509
 
 
510
    cp = p->v_name;             /* we know that this name is here now */
 
511
 
 
512
    /* now find the next one */
 
513
    for (;;) {
 
514
        if (p->v_right) {       /* if we can go right */
 
515
            p = p->v_right;
 
516
            while (p->v_left)
 
517
                p = p->v_left;
 
518
        }
 
519
        else {                  /* else go up */
 
520
            do {
 
521
                c = p;
 
522
                p = p->v_parent;
 
523
            } while (p->v_right == c);
 
524
        }
 
525
        if (p->v_parent == 0) { /* is it the header? */
 
526
            tw_vptr = NULL;
 
527
            return (cp);
 
528
        }
 
529
        if (p->v_name) {
 
530
            tw_vptr = p;        /* save state for the next call */
 
531
            return (cp);
 
532
        }
 
533
    }
 
534
} /* end tw_shvar_next */
 
535
 
 
536
 
 
537
/* tw_envvar_next():
 
538
 *      Return the next environment variable
 
539
 */
 
540
/*ARGSUSED*/
 
541
Char *
 
542
tw_envvar_next(dir, flags)
 
543
    Char *dir;
 
544
    int *flags;
 
545
{
 
546
    Char   *ps, *pd;
 
547
 
 
548
    USE(flags);
 
549
    USE(dir);
 
550
    if (tw_env == NULL || *tw_env == NULL)
 
551
        return (NULL);
 
552
    for (ps = *tw_env, pd = tw_retname;
 
553
         *ps && *ps != '=' && pd <= &tw_retname[MAXPATHLEN]; *pd++ = *ps++)
 
554
        continue;
 
555
    *pd = '\0';
 
556
    tw_env++;
 
557
    return (tw_retname);
 
558
} /* end tw_envvar_next */
 
559
 
 
560
 
 
561
/* tw_var_start():
 
562
 *      Begin the list of the shell and environment variables
 
563
 */
 
564
/*ARGSUSED*/
 
565
void
 
566
tw_var_start(dfd, pat)
 
567
    DIR *dfd;
 
568
    Char *pat;
 
569
{
 
570
    USE(pat);
 
571
    SETDIR(dfd)
 
572
    tw_vptr_start(&shvhed);
 
573
    tw_env = STR_environ;
 
574
} /* end tw_var_start */
 
575
 
 
576
 
 
577
/* tw_alias_start():
 
578
 *      Begin the list of the shell aliases
 
579
 */
 
580
/*ARGSUSED*/
 
581
void
 
582
tw_alias_start(dfd, pat)
 
583
    DIR *dfd;
 
584
    Char *pat;
 
585
{
 
586
    USE(pat);
 
587
    SETDIR(dfd)
 
588
    tw_vptr_start(&aliases);
 
589
    tw_env = NULL;
 
590
} /* tw_alias_start */
 
591
 
 
592
 
 
593
/* tw_complete_start():
 
594
 *      Begin the list of completions
 
595
 */
 
596
/*ARGSUSED*/
 
597
void
 
598
tw_complete_start(dfd, pat)
 
599
    DIR *dfd;
 
600
    Char *pat;
 
601
{
 
602
    extern struct varent completions;
 
603
 
 
604
    USE(pat);
 
605
    SETDIR(dfd)
 
606
    tw_vptr_start(&completions);
 
607
    tw_env = NULL;
 
608
} /* end tw_complete_start */
 
609
 
 
610
 
 
611
/* tw_var_next():
 
612
 *      Return the next shell or environment variable
 
613
 */
 
614
Char *
 
615
tw_var_next(dir, flags)
 
616
    Char *dir;
 
617
    int  *flags;
 
618
{
 
619
    Char *ptr = NULL;
 
620
 
 
621
    if (tw_vptr)
 
622
        ptr = tw_shvar_next(dir, flags);
 
623
    if (!ptr && tw_env)
 
624
        ptr = tw_envvar_next(dir, flags);
 
625
    return ptr;
 
626
} /* end tw_var_next */
 
627
 
 
628
 
 
629
/* tw_logname_start():
 
630
 *      Initialize lognames to the beginning of the list
 
631
 */
 
632
/*ARGSUSED*/
 
633
void 
 
634
tw_logname_start(dfd, pat)
 
635
    DIR *dfd;
 
636
    Char *pat;
 
637
{
 
638
    USE(pat);
 
639
    SETDIR(dfd)
 
640
#if !defined(_VMS_POSIX) && !defined(WINNT)
 
641
    (void) setpwent();  /* Open passwd file */
 
642
#endif /* !_VMS_POSIX && !WINNT */
 
643
} /* end tw_logname_start */
 
644
 
 
645
 
 
646
/* tw_logname_next():
 
647
 *      Return the next entry from the passwd file
 
648
 */
 
649
/*ARGSUSED*/
 
650
Char *
 
651
tw_logname_next(dir, flags)
 
652
    Char *dir;
 
653
    int  *flags;
 
654
{
 
655
    static Char retname[MAXPATHLEN];
 
656
    struct passwd *pw;
 
657
    /*
 
658
     * We don't want to get interrupted inside getpwent()
 
659
     * because the yellow pages code is not interruptible,
 
660
     * and if we call endpwent() immediatetely after
 
661
     * (in pintr()) we may be freeing an invalid pointer
 
662
     */
 
663
    USE(flags);
 
664
    USE(dir);
 
665
    TW_HOLD();
 
666
#if !defined(_VMS_POSIX) && !defined(WINNT)
 
667
    /* ISC does not declare getpwent()? */
 
668
    pw = (struct passwd *) getpwent();
 
669
#else /* _VMS_POSIX || WINNT */
 
670
    pw = NULL;
 
671
#endif /* !_VMS_POSIX && !WINNT */
 
672
    TW_RELS();
 
673
 
 
674
    if (pw == NULL) {
 
675
#ifdef YPBUGS
 
676
        fix_yp_bugs();
 
677
#endif
 
678
        return (NULL);
 
679
    }
 
680
    (void) Strcpy(retname, str2short(pw->pw_name));
 
681
    return (retname);
 
682
} /* end tw_logname_next */
 
683
 
 
684
 
 
685
/* tw_logname_end():
 
686
 *      Close the passwd file to finish the logname list
 
687
 */
 
688
void
 
689
tw_logname_end()
 
690
{
 
691
#ifdef YPBUGS
 
692
    fix_yp_bugs();
 
693
#endif
 
694
#if !defined(_VMS_POSIX) && !defined(WINNT)
 
695
   (void) endpwent();
 
696
#endif /* !_VMS_POSIX && !WINNT */
 
697
} /* end tw_logname_end */
 
698
 
 
699
 
 
700
/* tw_grpname_start():
 
701
 *      Initialize grpnames to the beginning of the list
 
702
 */
 
703
/*ARGSUSED*/
 
704
void 
 
705
tw_grpname_start(dfd, pat)
 
706
    DIR *dfd;
 
707
    Char *pat;
 
708
{
 
709
    USE(pat);
 
710
    SETDIR(dfd)
 
711
#if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT)
 
712
    (void) setgrent();  /* Open group file */
 
713
#endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT */
 
714
} /* end tw_grpname_start */
 
715
 
 
716
 
 
717
/* tw_grpname_next():
 
718
 *      Return the next entry from the group file
 
719
 */
 
720
/*ARGSUSED*/
 
721
Char *
 
722
tw_grpname_next(dir, flags)
 
723
    Char *dir;
 
724
    int  *flags;
 
725
{
 
726
    static Char retname[MAXPATHLEN];
 
727
    struct group *gr;
 
728
    /*
 
729
     * We don't want to get interrupted inside getgrent()
 
730
     * because the yellow pages code is not interruptible,
 
731
     * and if we call endgrent() immediatetely after
 
732
     * (in pintr()) we may be freeing an invalid pointer
 
733
     */
 
734
    USE(flags);
 
735
    USE(dir);
 
736
    TW_HOLD();
 
737
#if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT)
 
738
#ifdef INTERIX
 
739
    gr = (struct group *) getgrent_nomembers();
 
740
#else
 
741
    gr = (struct group *) getgrent();
 
742
#endif
 
743
#else /* _VMS_POSIX || _OSD_POSIX || WINNT */
 
744
    gr = NULL;
 
745
#endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT */
 
746
    TW_RELS();
 
747
 
 
748
    if (gr == NULL) {
 
749
#ifdef YPBUGS
 
750
        fix_yp_bugs();
 
751
#endif
 
752
        return (NULL);
 
753
    }
 
754
    (void) Strcpy(retname, str2short(gr->gr_name));
 
755
    return (retname);
 
756
} /* end tw_grpname_next */
 
757
 
 
758
 
 
759
/* tw_grpname_end():
 
760
 *      Close the group file to finish the groupname list
 
761
 */
 
762
void
 
763
tw_grpname_end()
 
764
{
 
765
#ifdef YPBUGS
 
766
    fix_yp_bugs();
 
767
#endif
 
768
#if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT)
 
769
   (void) endgrent();
 
770
#endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT */
 
771
} /* end tw_grpname_end */
 
772
 
 
773
/* tw_file_start():
 
774
 *      Initialize the directory for the file list
 
775
 */
 
776
/*ARGSUSED*/
 
777
void
 
778
tw_file_start(dfd, pat)
 
779
    DIR *dfd;
 
780
    Char *pat;
 
781
{
 
782
    struct varent *vp;
 
783
    USE(pat);
 
784
    SETDIR(dfd)
 
785
    if ((vp = adrof(STRcdpath)) != NULL)
 
786
        tw_env = vp->vec;
 
787
} /* end tw_file_start */
 
788
 
 
789
 
 
790
/* tw_file_next():
 
791
 *      Return the next file in the directory 
 
792
 */
 
793
Char *
 
794
tw_file_next(dir, flags)
 
795
    Char *dir;
 
796
    int  *flags;
 
797
{
 
798
    Char *ptr = tw_dir_next(tw_dir_fd);
 
799
    if (ptr == NULL && (*flags & TW_DIR_OK) != 0) {
 
800
        CLRDIR(tw_dir_fd)
 
801
        while (tw_env && *tw_env)
 
802
            if ((tw_dir_fd = opendir(short2str(*tw_env))) != NULL)
 
803
                break;
 
804
            else
 
805
                tw_env++;
 
806
                
 
807
        if (tw_dir_fd) {
 
808
            copyn(dir, *tw_env++, MAXPATHLEN);
 
809
            catn(dir, STRslash, MAXPATHLEN);
 
810
            ptr = tw_dir_next(tw_dir_fd);
 
811
        }
 
812
    }
 
813
    return ptr;
 
814
} /* end tw_file_next */
 
815
 
 
816
 
 
817
/* tw_dir_end():
 
818
 *      Clear directory related lists
 
819
 */
 
820
void
 
821
tw_dir_end()
 
822
{
 
823
   CLRDIR(tw_dir_fd)
 
824
   CLRDIR(tw_cmd_state.dfd)
 
825
} /* end tw_dir_end */
 
826
 
 
827
 
 
828
/* tw_item_free():
 
829
 *      Free the item list
 
830
 */
 
831
void
 
832
tw_item_free()
 
833
{
 
834
    tw_str_free(&tw_item);
 
835
} /* end tw_item_free */
 
836
 
 
837
 
 
838
/* tw_item_get(): 
 
839
 *      Return the list of items 
 
840
 */
 
841
Char **
 
842
tw_item_get()
 
843
{
 
844
    return tw_item.list;
 
845
} /* end tw_item_get */
 
846
 
 
847
 
 
848
/* tw_item_add():
 
849
 *      Return a new item
 
850
 */
 
851
Char *
 
852
tw_item_add(len)
 
853
    int len;
 
854
{
 
855
     return tw_str_add(&tw_item, len);
 
856
} /* tw_item_add */
 
857
 
 
858
 
 
859
/* tw_item_find():
 
860
 *      Find the string if it exists in the item list 
 
861
 *      end return it.
 
862
 */
 
863
Char *
 
864
tw_item_find(str)
 
865
    Char    *str;
 
866
{
 
867
    int i;
 
868
 
 
869
    if (tw_item.list == NULL || str == NULL)
 
870
        return NULL;
 
871
 
 
872
    for (i = 0; i < tw_item.nlist; i++)
 
873
        if (tw_item.list[i] != NULL && Strcmp(tw_item.list[i], str) == 0)
 
874
            return tw_item.list[i];
 
875
    return NULL;
 
876
} /* end tw_item_find */
 
877
 
 
878
 
 
879
/* tw_vl_start():
 
880
 *      Initialize a variable list
 
881
 */
 
882
void
 
883
tw_vl_start(dfd, pat)
 
884
    DIR *dfd;
 
885
    Char *pat;
 
886
{
 
887
    SETDIR(dfd)
 
888
    if ((tw_vptr = adrof(pat)) != NULL) {
 
889
        tw_env = tw_vptr->vec;
 
890
        tw_vptr = NULL;
 
891
    }
 
892
    else
 
893
        tw_env = NULL;
 
894
} /* end tw_vl_start */
 
895
 
 
896
 
 
897
/*
 
898
 * Initialize a word list
 
899
 */
 
900
void
 
901
tw_wl_start(dfd, pat)
 
902
    DIR *dfd;
 
903
    Char *pat;
 
904
{
 
905
    SETDIR(dfd);
 
906
    tw_word = pat;
 
907
} /* end tw_wl_start */
 
908
 
 
909
 
 
910
/*
 
911
 * Return the next word from the word list
 
912
 */
 
913
/*ARGSUSED*/
 
914
Char *
 
915
tw_wl_next(dir, flags)
 
916
    Char *dir;
 
917
    int *flags;
 
918
{
 
919
    USE(flags);
 
920
    if (tw_word == NULL || tw_word[0] == '\0')
 
921
        return NULL;
 
922
    
 
923
    while (*tw_word && Isspace(*tw_word)) tw_word++;
 
924
 
 
925
    for (dir = tw_word; *tw_word && !Isspace(*tw_word); tw_word++)
 
926
        continue;
 
927
    if (*tw_word)
 
928
        *tw_word++ = '\0';
 
929
    return *dir ? dir : NULL;
 
930
} /* end tw_wl_next */
 
931
 
 
932
 
 
933
/* tw_bind_start():
 
934
 *      Begin the list of the shell bindings
 
935
 */
 
936
/*ARGSUSED*/
 
937
void
 
938
tw_bind_start(dfd, pat)
 
939
    DIR *dfd;
 
940
    Char *pat;
 
941
{
 
942
    USE(pat);
 
943
    SETDIR(dfd)
 
944
    tw_bind = FuncNames;
 
945
} /* end tw_bind_start */
 
946
 
 
947
 
 
948
/* tw_bind_next():
 
949
 *      Begin the list of the shell bindings
 
950
 */
 
951
/*ARGSUSED*/
 
952
Char *
 
953
tw_bind_next(dir, flags)
 
954
    Char *dir;
 
955
    int *flags;
 
956
{
 
957
    char *ptr;
 
958
    USE(flags);
 
959
    if (tw_bind && tw_bind->name) {
 
960
        for (ptr = tw_bind->name, dir = tw_retname;
 
961
             (*dir++ = (Char) *ptr++) != '\0';)
 
962
            continue;
 
963
        tw_bind++;
 
964
        return(tw_retname);
 
965
    }
 
966
    return NULL;
 
967
} /* end tw_bind_next */
 
968
 
 
969
 
 
970
/* tw_limit_start():
 
971
 *      Begin the list of the shell limitings
 
972
 */
 
973
/*ARGSUSED*/
 
974
void
 
975
tw_limit_start(dfd, pat)
 
976
    DIR *dfd;
 
977
    Char *pat;
 
978
{
 
979
    USE(pat);
 
980
    SETDIR(dfd)
 
981
#ifndef HAVENOLIMIT
 
982
    tw_limit = limits;
 
983
#endif /* ! HAVENOLIMIT */
 
984
} /* end tw_limit_start */
 
985
 
 
986
 
 
987
/* tw_limit_next():
 
988
 *      Begin the list of the shell limitings
 
989
 */
 
990
/*ARGSUSED*/
 
991
Char *
 
992
tw_limit_next(dir, flags)
 
993
    Char *dir;
 
994
    int *flags;
 
995
{
 
996
#ifndef HAVENOLIMIT
 
997
    char *ptr;
 
998
    if (tw_limit && tw_limit->limname) {
 
999
        for (ptr = tw_limit->limname, dir = tw_retname; 
 
1000
             (*dir++ = (Char) *ptr++) != '\0';)
 
1001
            continue;
 
1002
        tw_limit++;
 
1003
        return(tw_retname);
 
1004
    }
 
1005
#endif /* ! HAVENOLIMIT */
 
1006
    USE(flags);
 
1007
    return NULL;
 
1008
} /* end tw_limit_next */
 
1009
 
 
1010
 
 
1011
/* tw_sig_start():
 
1012
 *      Begin the list of the shell sigings
 
1013
 */
 
1014
/*ARGSUSED*/
 
1015
void
 
1016
tw_sig_start(dfd, pat)
 
1017
    DIR *dfd;
 
1018
    Char *pat;
 
1019
{
 
1020
    USE(pat);
 
1021
    SETDIR(dfd)
 
1022
    tw_index = 0;
 
1023
} /* end tw_sig_start */
 
1024
 
 
1025
 
 
1026
/* tw_sig_next():
 
1027
 *      Begin the list of the shell sigings
 
1028
 */
 
1029
/*ARGSUSED*/
 
1030
Char *
 
1031
tw_sig_next(dir, flags)
 
1032
    Char *dir;
 
1033
    int *flags;
 
1034
{
 
1035
    char *ptr;
 
1036
    extern int nsig;
 
1037
    USE(flags);
 
1038
    for (;tw_index < nsig; tw_index++) {
 
1039
 
 
1040
        if (mesg[tw_index].iname == NULL)
 
1041
            continue;
 
1042
 
 
1043
        for (ptr = mesg[tw_index].iname, dir = tw_retname; 
 
1044
             (*dir++ = (Char) *ptr++) != '\0';)
 
1045
            continue;
 
1046
        tw_index++;
 
1047
        return(tw_retname);
 
1048
    }
 
1049
    return NULL;
 
1050
} /* end tw_sig_next */
 
1051
 
 
1052
 
 
1053
/* tw_job_start():
 
1054
 *      Begin the list of the shell jobings
 
1055
 */
 
1056
/*ARGSUSED*/
 
1057
void
 
1058
tw_job_start(dfd, pat)
 
1059
    DIR *dfd;
 
1060
    Char *pat;
 
1061
{
 
1062
    USE(pat);
 
1063
    SETDIR(dfd)
 
1064
    tw_index = 1;
 
1065
} /* end tw_job_start */
 
1066
 
 
1067
 
 
1068
/* tw_job_next():
 
1069
 *      Begin the list of the shell jobings
 
1070
 */
 
1071
/*ARGSUSED*/
 
1072
Char *
 
1073
tw_job_next(dir, flags)
 
1074
    Char *dir;
 
1075
    int *flags;
 
1076
{
 
1077
    Char *ptr;
 
1078
    struct process *j;
 
1079
 
 
1080
    USE(flags);
 
1081
    for (;tw_index <= pmaxindex; tw_index++) {
 
1082
        for (j = proclist.p_next; j != NULL; j = j->p_next)
 
1083
            if (j->p_index == tw_index && j->p_procid == j->p_jobid)
 
1084
                break;
 
1085
        if (j == NULL) 
 
1086
            continue;
 
1087
        for (ptr = j->p_command, dir = tw_retname; (*dir++ = *ptr++) != '\0';)
 
1088
            continue;
 
1089
        *dir = '\0';
 
1090
        tw_index++;
 
1091
        return(tw_retname);
 
1092
    }
 
1093
    return NULL;
 
1094
} /* end tw_job_next */