[BACK]Return to complete.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ftp

Annotation of src/usr.bin/ftp/complete.c, Revision 1.11

1.11    ! lebel       1: /*     $OpenBSD: complete.c,v 1.10 2001/03/09 17:01:02 millert Exp $   */
1.9       millert     2: /*     $NetBSD: complete.c,v 1.10 1997/08/18 10:20:18 lukem Exp $      */
1.1       millert     3:
                      4: /*-
                      5:  * Copyright (c) 1997 The NetBSD Foundation, Inc.
                      6:  * All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to The NetBSD Foundation
                      9:  * by Luke Mewburn.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *        This product includes software developed by the NetBSD
                     22:  *        Foundation, Inc. and its contributors.
                     23:  * 4. Neither the name of The NetBSD Foundation nor the names of its
                     24:  *    contributors may be used to endorse or promote products derived
                     25:  *    from this software without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     28:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     29:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.9       millert    30:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     31:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1.1       millert    32:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     33:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     34:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     35:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     36:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     37:  * POSSIBILITY OF SUCH DAMAGE.
                     38:  */
                     39:
1.5       millert    40: #ifndef SMALL
1.1       millert    41: #ifndef lint
1.11    ! lebel      42: static char rcsid[] = "$OpenBSD: complete.c,v 1.10 2001/03/09 17:01:02 millert Exp $";
1.1       millert    43: #endif /* not lint */
                     44:
                     45: /*
                     46:  * FTP user program - command and file completion routines
                     47:  */
                     48:
                     49: #include <ctype.h>
                     50: #include <err.h>
                     51: #include <dirent.h>
                     52: #include <stdio.h>
                     53: #include <stdlib.h>
                     54: #include <string.h>
                     55:
                     56: #include "ftp_var.h"
                     57:
1.8       millert    58: static int          comparstr          __P((const void *, const void *));
                     59: static unsigned char complete_ambiguous        __P((char *, int, StringList *));
                     60: static unsigned char complete_command  __P((char *, int));
                     61: static unsigned char complete_local    __P((char *, int));
                     62: static unsigned char complete_remote   __P((char *, int));
                     63:
1.1       millert    64: static int
                     65: comparstr(a, b)
                     66:        const void *a, *b;
                     67: {
1.4       millert    68:        return (strcmp(*(char **)a, *(char **)b));
1.1       millert    69: }
                     70:
                     71: /*
                     72:  * Determine if complete is ambiguous. If unique, insert.
                     73:  * If no choices, error. If unambiguous prefix, insert that.
                     74:  * Otherwise, list choices. words is assumed to be filtered
                     75:  * to only contain possible choices.
                     76:  * Args:
                     77:  *     word    word which started the match
                     78:  *     list    list by default
                     79:  *     words   stringlist containing possible matches
                     80:  */
                     81: static unsigned char
                     82: complete_ambiguous(word, list, words)
                     83:        char *word;
                     84:        int list;
                     85:        StringList *words;
                     86: {
1.3       millert    87:        char insertstr[MAXPATHLEN];
1.1       millert    88:        char *lastmatch;
1.8       millert    89:        int i, j;
                     90:        size_t matchlen, wordlen;
1.1       millert    91:
                     92:        wordlen = strlen(word);
                     93:        if (words->sl_cur == 0)
1.4       millert    94:                return (CC_ERROR);      /* no choices available */
1.1       millert    95:
                     96:        if (words->sl_cur == 1) {       /* only once choice available */
1.5       millert    97:                (void)strcpy(insertstr, words->sl_str[0]);
1.1       millert    98:                if (el_insertstr(el, insertstr + wordlen) == -1)
1.4       millert    99:                        return (CC_ERROR);
1.1       millert   100:                else
1.4       millert   101:                        return (CC_REFRESH);
1.1       millert   102:        }
                    103:
                    104:        if (!list) {
                    105:                matchlen = 0;
                    106:                lastmatch = words->sl_str[0];
                    107:                matchlen = strlen(lastmatch);
                    108:                for (i = 1 ; i < words->sl_cur ; i++) {
                    109:                        for (j = wordlen ; j < strlen(words->sl_str[i]); j++)
                    110:                                if (lastmatch[j] != words->sl_str[i][j])
                    111:                                        break;
                    112:                        if (j < matchlen)
                    113:                                matchlen = j;
                    114:                }
                    115:                if (matchlen > wordlen) {
1.11    ! lebel     116:                        (void)strlcpy(insertstr, lastmatch, matchlen+1);
1.1       millert   117:                        if (el_insertstr(el, insertstr + wordlen) == -1)
1.4       millert   118:                                return (CC_ERROR);
1.1       millert   119:                        else
                    120:                                        /*
                    121:                                         * XXX: really want CC_REFRESH_BEEP
                    122:                                         */
1.4       millert   123:                                return (CC_REFRESH);
1.1       millert   124:                }
                    125:        }
                    126:
1.7       deraadt   127:        putc('\n', ttyout);
1.1       millert   128:        qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
                    129:        list_vertical(words);
1.4       millert   130:        return (CC_REDISPLAY);
1.1       millert   131: }
                    132:
                    133: /*
                    134:  * Complete a command
                    135:  */
                    136: static unsigned char
                    137: complete_command(word, list)
                    138:        char *word;
                    139:        int list;
                    140: {
                    141:        struct cmd *c;
                    142:        StringList *words;
1.8       millert   143:        size_t wordlen;
1.1       millert   144:        unsigned char rv;
                    145:
                    146:        words = sl_init();
                    147:        wordlen = strlen(word);
                    148:
                    149:        for (c = cmdtab; c->c_name != NULL; c++) {
                    150:                if (wordlen > strlen(c->c_name))
                    151:                        continue;
                    152:                if (strncmp(word, c->c_name, wordlen) == 0)
                    153:                        sl_add(words, c->c_name);
                    154:        }
                    155:
                    156:        rv = complete_ambiguous(word, list, words);
                    157:        sl_free(words, 0);
1.4       millert   158:        return (rv);
1.1       millert   159: }
                    160:
                    161: /*
                    162:  * Complete a local file
                    163:  */
                    164: static unsigned char
                    165: complete_local(word, list)
                    166:        char *word;
                    167:        int list;
                    168: {
                    169:        StringList *words;
1.3       millert   170:        char dir[MAXPATHLEN];
1.1       millert   171:        char *file;
                    172:        DIR *dd;
                    173:        struct dirent *dp;
                    174:        unsigned char rv;
                    175:
                    176:        if ((file = strrchr(word, '/')) == NULL) {
1.5       millert   177:                dir[0] = '.';
                    178:                dir[1] = '\0';
1.1       millert   179:                file = word;
                    180:        } else {
1.5       millert   181:                if (file == word) {
                    182:                        dir[0] = '/';
                    183:                        dir[1] = '\0';
                    184:                } else {
1.11    ! lebel     185:                        (void)strlcpy(dir, word, (size_t)(file - word) + 1);
1.1       millert   186:                }
1.5       millert   187:                file++;
1.1       millert   188:        }
                    189:
                    190:        if ((dd = opendir(dir)) == NULL)
1.4       millert   191:                return (CC_ERROR);
1.1       millert   192:
                    193:        words = sl_init();
                    194:
                    195:        for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
                    196:                if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
                    197:                        continue;
                    198:                if (strlen(file) > dp->d_namlen)
                    199:                        continue;
                    200:                if (strncmp(file, dp->d_name, strlen(file)) == 0) {
                    201:                        char *tcp;
                    202:
                    203:                        tcp = strdup(dp->d_name);
                    204:                        if (tcp == NULL)
                    205:                                errx(1, "Can't allocate memory for local dir");
                    206:                        sl_add(words, tcp);
                    207:                }
                    208:        }
                    209:        closedir(dd);
                    210:
                    211:        rv = complete_ambiguous(file, list, words);
                    212:        sl_free(words, 1);
1.4       millert   213:        return (rv);
1.1       millert   214: }
                    215:
                    216: /*
                    217:  * Complete a remote file
                    218:  */
                    219: static unsigned char
                    220: complete_remote(word, list)
                    221:        char *word;
                    222:        int list;
                    223: {
                    224:        static StringList *dirlist;
1.3       millert   225:        static char      lastdir[MAXPATHLEN];
1.1       millert   226:        StringList      *words;
1.3       millert   227:        char             dir[MAXPATHLEN];
1.1       millert   228:        char            *file, *cp;
1.4       millert   229:        int              i;
1.1       millert   230:        unsigned char    rv;
                    231:
                    232:        char *dummyargv[] = { "complete", dir, NULL };
                    233:
                    234:        if ((file = strrchr(word, '/')) == NULL) {
1.5       millert   235:                dir[0] = '.';
                    236:                dir[1] = '\0';
1.1       millert   237:                file = word;
                    238:        } else {
1.4       millert   239:                cp = file;
                    240:                while (*cp == '/' && cp > word)
                    241:                        cp--;
1.11    ! lebel     242:                (void)strlcpy(dir, word, (size_t)(cp - word + 2));
1.1       millert   243:                file++;
                    244:        }
                    245:
                    246:        if (dirchange || strcmp(dir, lastdir) != 0) {   /* dir not cached */
1.4       millert   247:                char *emesg;
                    248:
1.1       millert   249:                if (dirlist != NULL)
                    250:                        sl_free(dirlist, 1);
                    251:                dirlist = sl_init();
                    252:
                    253:                mflag = 1;
1.4       millert   254:                emesg = NULL;
1.10      millert   255:                if (debug)
                    256:                        (void)putc('\n', ttyout);
1.4       millert   257:                while ((cp = remglob(dummyargv, 0, &emesg)) != NULL) {
1.1       millert   258:                        char *tcp;
                    259:
                    260:                        if (!mflag)
                    261:                                continue;
                    262:                        if (*cp == '\0') {
                    263:                                mflag = 0;
                    264:                                continue;
                    265:                        }
1.8       millert   266:                        tcp = strrchr(cp, '/');
                    267:                        if (tcp)
                    268:                                tcp++;
                    269:                        else
                    270:                                tcp = cp;
                    271:                        tcp = strdup(tcp);
1.1       millert   272:                        if (tcp == NULL)
                    273:                                errx(1, "Can't allocate memory for remote dir");
                    274:                        sl_add(dirlist, tcp);
                    275:                }
1.4       millert   276:                if (emesg != NULL) {
1.7       deraadt   277:                        fprintf(ttyout, "\n%s\n", emesg);
1.4       millert   278:                        return (CC_REDISPLAY);
                    279:                }
                    280:                (void)strcpy(lastdir, dir);
1.1       millert   281:                dirchange = 0;
                    282:        }
                    283:
                    284:        words = sl_init();
                    285:        for (i = 0; i < dirlist->sl_cur; i++) {
                    286:                cp = dirlist->sl_str[i];
1.4       millert   287:                if (strlen(file) > strlen(cp))
1.1       millert   288:                        continue;
1.4       millert   289:                if (strncmp(file, cp, strlen(file)) == 0)
                    290:                        sl_add(words, cp);
1.1       millert   291:        }
                    292:        rv = complete_ambiguous(file, list, words);
                    293:        sl_free(words, 0);
1.4       millert   294:        return (rv);
1.1       millert   295: }
                    296:
                    297: /*
                    298:  * Generic complete routine
                    299:  */
                    300: unsigned char
                    301: complete(el, ch)
                    302:        EditLine *el;
                    303:        int ch;
                    304: {
                    305:        static char word[FTPBUFLEN];
                    306:        static int lastc_argc, lastc_argo;
                    307:
                    308:        struct cmd *c;
                    309:        const LineInfo *lf;
1.8       millert   310:        int celems, dolist;
                    311:        size_t len;
1.1       millert   312:
                    313:        lf = el_line(el);
                    314:        len = lf->lastchar - lf->buffer;
                    315:        if (len >= sizeof(line))
1.4       millert   316:                return (CC_ERROR);
1.11    ! lebel     317:        (void)strlcpy(line, lf->buffer, len+1);
1.1       millert   318:        cursor_pos = line + (lf->cursor - lf->buffer);
                    319:        lastc_argc = cursor_argc;       /* remember last cursor pos */
                    320:        lastc_argo = cursor_argo;
                    321:        makeargv();                     /* build argc/argv of current line */
                    322:
                    323:        if (cursor_argo >= sizeof(word))
1.4       millert   324:                return (CC_ERROR);
1.1       millert   325:
                    326:        dolist = 0;
                    327:                        /* if cursor and word is same, list alternatives */
                    328:        if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
                    329:            && strncmp(word, margv[cursor_argc], cursor_argo) == 0)
                    330:                dolist = 1;
                    331:        else
1.11    ! lebel     332:            (void)strlcpy(word, margv[cursor_argc], cursor_argo+1);
1.1       millert   333:
                    334:        if (cursor_argc == 0)
1.4       millert   335:                return (complete_command(word, dolist));
1.1       millert   336:
                    337:        c = getcmd(margv[0]);
                    338:        if (c == (struct cmd *)-1 || c == 0)
1.4       millert   339:                return (CC_ERROR);
1.1       millert   340:        celems = strlen(c->c_complete);
                    341:
                    342:                /* check for 'continuation' completes (which are uppercase) */
                    343:        if ((cursor_argc > celems) && (celems > 0)
                    344:            && isupper(c->c_complete[celems-1]))
                    345:                cursor_argc = celems;
                    346:
                    347:        if (cursor_argc > celems)
1.4       millert   348:                return (CC_ERROR);
1.1       millert   349:
                    350:        switch (c->c_complete[cursor_argc - 1]) {
                    351:                case 'l':                       /* local complete */
                    352:                case 'L':
1.4       millert   353:                        return (complete_local(word, dolist));
1.1       millert   354:                case 'r':                       /* remote complete */
                    355:                case 'R':
1.6       millert   356:                        if (connected != -1) {
1.7       deraadt   357:                                fputs("\nMust be logged in to complete.\n", ttyout);
1.4       millert   358:                                return (CC_REDISPLAY);
1.1       millert   359:                        }
1.4       millert   360:                        return (complete_remote(word, dolist));
1.1       millert   361:                case 'c':                       /* command complete */
                    362:                case 'C':
1.4       millert   363:                        return (complete_command(word, dolist));
1.1       millert   364:                case 'n':                       /* no complete */
                    365:                default:
1.4       millert   366:                        return (CC_ERROR);
1.1       millert   367:        }
                    368:
1.4       millert   369:        return (CC_ERROR);
1.1       millert   370: }
1.8       millert   371:
                    372: #endif /* !SMALL */