File: ion-completefile.c

package info (click to toggle)
notion 4.0.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,676 kB
  • sloc: ansic: 47,508; sh: 2,096; makefile: 603; perl: 270
file content (614 lines) | stat: -rw-r--r-- 15,608 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
 * ion/share/ion-completefile/ion-completefile.c
 */

/****************************************************************************/
/*                                                                          */
/* Copyright 1992 Simmule Turner and Rich Salz.  All rights reserved.       */
/*                                                                          */
/* This software is not subject to any license of the American Telephone    */
/* and Telegraph Company or of the Regents of the University of California. */
/*                                                                          */
/* Permission is granted to anyone to use this software for any purpose on  */
/* any computer system, and to alter it and redistribute it freely, subject */
/* to the following restrictions:                                           */
/* 1. The authors are not responsible for the consequences of use of this   */
/*    software, no matter how awful, even if they arise from flaws in it.   */
/* 2. The origin of this software must not be misrepresented, either by     */
/*    explicit claim or by omission.  Since few users ever read sources,    */
/*    credits must appear in the documentation.                             */
/* 3. Altered versions must be plainly marked as such, and must not be      */
/*    misrepresented as being the original software.  Since few users       */
/*    ever read sources, credits must appear in the documentation.          */
/* 4. This notice may not be removed or altered.                            */
/*                                                                          */
/****************************************************************************/
/*                                                                          */
/*  This is a line-editing library, it can be linked into almost any        */
/*  program to provide command-line editing and recall.                     */
/*                                                                          */
/*  Posted to comp.sources.misc Sun, 2 Aug 1992 03:05:27 GMT                */
/*      by rsalz@osf.org (Rich $alz)                                        */
/*                                                                          */
/****************************************************************************/
/*                                                                          */
/*  The version contained here has some modifications by awb@cstr.ed.ac.uk  */
/*  (Alan W Black) in order to integrate it with the Edinburgh Speech Tools */
/*  library and Scheme-in-one-defun in particular.  All modifications to    */
/*  to this work are continued with the same copyright above.  That is      */
/*  This version editline does not have the the "no commercial use"         */
/*  restriction that some of the rest of the EST library may have           */
/*  awb Dec 30 1998                                                         */
/*                                                                          */
/****************************************************************************/
/*  $Revision: 1.2 $
 **
 **  History and file completion functions for editline library.
 */


/*
 * Adapted for use with Ion and tilde-expansion added by
 * Tuomo Valkonen <tuomov@cc.tut.fi>, 2000-08-23.
 */

#include <sys/param.h>
#include <sys/types.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <pwd.h>
/* needed for NGROUPS_MAX on Solaris 10 */
#include <limits.h>

#include <libtu/util.h>
#include <libtu/types.h>
#include <libtu/misc.h>
#include <libtu/output.h>

#define STRDUP(X) scopy(X)
#define DISPOSE(X) free(X)
#define NEW(T, X)  ALLOC_N(T, X)
#define STATIC static
#define SIZE_T int
#define MEM_INC 64
#define COPYFROMTO(new, p, len) \
    (void)memcpy((char *)(new), (char *)(p), (int)(len))

#ifndef NGROUPS
/* Hopefully one of these is defined... */
#define NGROUPS NGROUPS_MAX
#endif

typedef struct dirent DIRENTRY;

static int el_is_directory(char *path)
{
    struct stat sb;

    if ((stat(path, &sb) >= 0) && S_ISDIR(sb.st_mode))
        return 1;
    else
        return 0;
}

static int el_is_executable(char *path)
{
    unsigned int t_uid = geteuid();
    gid_t t_gids[NGROUPS];
    int groupcount;
    struct stat sb;
    int i;

    groupcount = getgroups(NGROUPS, t_gids);

    if((stat(path, &sb) >= 0) && S_ISREG(sb.st_mode)) {
        /* Normal file, see if we can execute it. */

        if (sb.st_mode & S_IXOTH) { /* All can execute */
            return (1);
        }
        if (sb.st_uid == t_uid && (sb.st_mode & S_IXUSR)) {
            return (1);
        }
        if (sb.st_mode & S_IXGRP) {
            for (i = 0; i < groupcount; i++) {
                if (sb.st_gid == t_gids[i]) {
                    return (1);
                }
            }
        }
    }
    return (0);
}

/*
 **  Fill in *avp with an array of names that match file, up to its length.
 **  Ignore . and .. .
 */
static int FindMatches(char *dir,char *file,char ***avp)
{
    char	**av;
    char	**new;
    char	*p;
    DIR		*dp;
    DIRENTRY	*ep;
    SIZE_T	ac;
    SIZE_T	len;

    if(*dir=='\0')
        dir=".";

    if ((dp = opendir(dir)) == NULL)
        return 0;

    av = NULL;
    ac = 0;
    len = strlen(file);
    while ((ep = readdir(dp)) != NULL) {
        p = ep->d_name;
        if (p[0] == '.' && (p[1] == '\0' || (p[1] == '.' && p[2] == '\0')))
            continue;
        if (len && strncmp(p, file, len) != 0)
            continue;

        if ((ac % MEM_INC) == 0) {
            if ((new = NEW(char*, ac + MEM_INC)) == NULL)
                break;
            if (ac) {
                COPYFROMTO(new, av, ac * sizeof (char **));
                DISPOSE(av);
            }
            *avp = av = new;
        }

        if ((av[ac] = STRDUP(p)) == NULL) {
            if (ac == 0)
                DISPOSE(av);
            break;
        }
        ac++;
    }

    /* Clean up and return. */
    (void)closedir(dp);
    /*    if (ac)
     qsort(av, ac, sizeof (char **), compare);*/
    return ac;
}


/*
 ** Slight modification on FindMatches, to search through current PATH
 **
 */
static int FindFullPathMatches(char *file,char ***avp)
{
    char	**av;
    char	**new;
    char	*p;
    char    *path = getenv("PATH");
    char    t_tmp[1024];
    char    *t_path;
    char    *t_t_path;
    DIR		*dp;
    DIRENTRY*ep;
    SIZE_T	ac;
    SIZE_T	len;

    t_path = strdup(path);
    t_t_path = t_path;

    av = NULL;
    ac = 0;

    t_t_path = strtok(t_path, ":\n\0");
    while (t_t_path) {
        if ((dp = opendir(t_t_path)) == NULL) {
            t_t_path = strtok(NULL, ":\n\0");
            continue;
        }

        len = strlen(file);
        while ((ep = readdir(dp)) != NULL) {
            p = ep->d_name;
            if (p[0] == '.' && (p[1] == '\0' || (p[0] == '.' &&
                                                 p[1] == '.' &&
                                                 p[2] == '\0'))) {
                continue;
            }
            if (len && strncmp(p, file, len) != 0) {
                continue;
            }

            snprintf(t_tmp, 1024, "%s/%s", t_t_path, p);
            if(!el_is_executable(t_tmp)) {
                continue;
            }

            if ((ac % MEM_INC) == 0) {
                if ((new = NEW(char*, ac + MEM_INC)) == NULL) {
                    break;
                }
                if (ac) {
                    COPYFROMTO(new, av, ac * sizeof (char **));
                    DISPOSE(av);
                }
                *avp = av = new;
            }
            if ((av[ac] = STRDUP(p)) == NULL) {
                if (ac == 0)
                    DISPOSE(av);
                break;
            }
            ac++;
        }
        (void)closedir(dp);
        t_t_path = strtok(NULL, ":\n\0");


    } /* t_path-while */

    /* Clean up and return. */

    /*if (ac)
     qsort(av, ac, sizeof (char **), compare); */
    free(t_path);

    return ac;
}


/*
 **  Split a pathname into allocated directory and trailing filename parts.
 */
STATIC int SplitPath(const char *path,char **dirpart,char **filepart)
{
    static char	DOT[] = "./";
    char	*dpart;
    char	*fpart;

    if ((fpart = strrchr(path, '/')) == NULL) {
        /* No slashes in path */
        if ((dpart = STRDUP(DOT)) == NULL)
            return -1;
        if ((fpart = STRDUP(path)) == NULL) {
            DISPOSE(dpart);
            return -1;
        }
    }else{
        if ((dpart = STRDUP(path)) == NULL)
            return -1;
        /* Include the slash -- Tuomo */
        dpart[fpart - path + 1] = '\0';
        if ((fpart = STRDUP(++fpart)) == NULL) {
            DISPOSE(dpart);
            return -1;
        }
        /* Root no longer a special case due above -- Tuomo
         if (dpart[0] == '\0')
         {
         dpart[0] = '/';
         dpart[1] = '\0';
         }
         */
    }
    *dirpart = dpart;
    *filepart = fpart;
    return 0;
}

/*
 **  Split a pathname into allocated directory and trailing filename parts.
 */
STATIC int SplitRelativePath(const char *path,char **dirpart,char **filepart)
{
    static char EOL[] = "\0";
    char *dpart;
    char *fpart;

    if ((fpart = strrchr(path, '/')) == NULL) {
        /* No slashes in path */
        if ((dpart = STRDUP(EOL)) == NULL)
            return -1;
        if ((fpart = STRDUP(path)) == NULL) {
            DISPOSE(dpart);
            return -1;
        }
    }
    else {
        if ((dpart = STRDUP(path)) == NULL)
            return -1;
        /* Include the slash -- Tuomo */
        dpart[fpart - path + 1] = '\0';
        if ((fpart = STRDUP(++fpart)) == NULL) {
            DISPOSE(dpart);
            return -1;
        }
        /* Root no longer a special case due above -- Tuomo
         if (dpart[0] == '\0')
         {
         dpart[0] = '/';
         dpart[1] = '\0';
         }
         */
    }
    *dirpart = dpart;
    *filepart = fpart;
    return 0;
}

static int complete_homedir(const char *username, char ***cp_ret, char **UNUSED(beg))
{
    struct passwd *pw;
    char *name;
    char **cp;
    int n=0, l=strlen(username);

    *cp_ret=NULL;

    for(pw=getpwent(); pw!=NULL; pw=getpwent()){
        name=pw->pw_name;

        if(l && strncmp(name, username, l))
            continue;

        name=scat3("~", name, "/");

        if(name==NULL){
            warn_err();
            continue;
        }

        cp=REALLOC_N(*cp_ret, char*, n, n+1);

        if(cp==NULL){
            warn_err();
            free(name);
            if(*cp_ret!=NULL)
                free(*cp_ret);
        }else{
            cp[n]=name;
            n++;
            *cp_ret=cp;
        }
    }

    endpwent();

    if(n==1){
        name=**cp_ret;
        name[strlen(name)-1]='\0';
        pw=getpwnam(name+1);
        if(pw!=NULL && pw->pw_dir!=NULL){
            name=scat(pw->pw_dir, "/");
            if(name!=NULL){
                free(**cp_ret);
                **cp_ret=name;
            }
        }
    }

    return n;
}

/*
 * ret: 0 not a home directory,
 * 		1 home directory (repath set)
 *		2 someone's home directory
 */
static int tilde_complete(char *path, char **retpath)
{
    char *home;
    char *p;
    struct passwd *pw;

    if(*path!='~')
        return 0;

    if(*(path+1)!='/' && *(path+1)!='\0'){
        p=strchr(path, '/');

        if(p==NULL)
            return 2;

        *p='\0';
        pw=getpwnam(path+1);
        *p='/';

        if(pw==NULL)
            return 0;

        home=pw->pw_dir;
    }else{
        p=path+1;
        home=getenv("HOME");
    }

    if(home!=NULL){
        if(*p=='\0')
            *retpath=scat3(home, p, "/");
        else
            *retpath=scat(home, p);
    }

    return (*retpath!=NULL);
}


/*
 **  Return all possible completions.
 */
int do_complete_file(char *pathname, char ***avp, char **beg)
{
    char	*dir;
    char	*file, *path=NULL, *tt;
    int		ac=0, i;

    switch(tilde_complete(pathname, &path)){
    case 0:
        i=SplitPath(pathname, &dir, &file);
        break;
    case 2:
        return complete_homedir(pathname+1, avp, beg);
    default:
        i=SplitPath(path, &dir, &file);
    }

    if(i<0)
        return 0;

    ac=FindMatches(dir, file, avp);

    DISPOSE(file);

    if(ac==0 && path!=NULL){
        *avp=ALLOC(char*);
        if(*avp==NULL)
            return 0;
        **avp=path;
        return 1;
    }else if(path!=NULL){
        free(path);
    }

    /* Identify directories with trailing / */
    for(i=0; i<ac; i++){
        path = NEW(char,strlen(dir)+strlen((*avp)[i])+3);
        sprintf(path,"%s/%s",dir,(*avp)[i]);
        if(el_is_directory(path)){
            tt = NEW(char,strlen((*avp)[i])+2);
            sprintf(tt,"%s/",(*avp)[i]);
            DISPOSE((*avp)[i]);
            (*avp)[i] = tt;
        }
        DISPOSE(path);
    }

    *beg=dir;

    return ac;
}


/*
 **  Return all possible completions.
 */
int do_complete_file_with_path(char *pathname, char ***avp, char **beg)
{
    char	*dir;
    char	*file, *path=NULL, *tt;
    int		ac=0, i, dcomp=FALSE;

    switch(tilde_complete(pathname, &path)){
    case 0:
        i=SplitRelativePath(pathname, &dir, &file);
        break;
    case 2:
        return complete_homedir(pathname+1, avp, beg);
    default:
        i=SplitPath(path, &dir, &file);
    }

    if(i<0)
        return 0;

    if(*dir=='\0')
        ac=FindFullPathMatches(file, avp); /* No slashes in path so far. */

    if(ac==0){
        if(*dir=='\0'){
            dir=scopy("./");
            if(dir==NULL){
                warn_err();
                return 0;
            }
        }
        ac=FindMatches(dir, file, avp);
        dcomp=TRUE;
    }

    DISPOSE(file);

    if(ac==0 && path!=NULL){
        *avp=ALLOC(char*);
        if(*avp==NULL)
            return 0;
        **avp=path;
        return 1;
    }else if(path!=NULL){
        free(path);
    }

    /* Identify directories with trailing / */
    if(dcomp){
        for (i = 0; i < ac; i++) {
            path = NEW(char,strlen(dir)+strlen((*avp)[i])+3);
            sprintf(path,"%s/%s",dir,(*avp)[i]);
            if (el_is_directory(path)) {
                tt = NEW(char,strlen((*avp)[i])+2);
                sprintf(tt,"%s/",(*avp)[i]);
                DISPOSE((*avp)[i]);
                (*avp)[i] = tt;
            }
            DISPOSE(path);
        }
    }

    *beg=dir;

    return ac;
}



int main(int argc, char *argv[])
{
    char **avp=NULL;
    char *beg=NULL;
    bool wp=FALSE;
    int i, j, n;

    libtu_init(argv[0]);

    for(i=1; i<argc; i++){
        if(strcmp(argv[i], "-h")==0){
            printf("Usage: ion-completefile [-help] [-wp] [to_complete...]\n");
            return EXIT_SUCCESS;
        }
    }

    for(i=1; i<argc; i++){
        if(strcmp(argv[i], "-wp")==0){
            wp=TRUE;
        }else{
            if(wp){
                n=do_complete_file_with_path(argv[i], &avp, &beg);
            }else{
                n=do_complete_file(argv[i], &avp, &beg);
            }

            if(beg){
                printf("%s\n", beg);
                free(beg);
                beg=NULL;
            }else{
                printf("\n");
            }


            if(avp){
                for(j=0; j<n; j++){
                    printf("%s\n", avp[j]);
                    free(avp[j]);
                }
                free(avp);
                avp=NULL;
            }
        }
    }

    return EXIT_SUCCESS;
}