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

Annotation of src/usr.bin/ftp/domacro.c, Revision 1.22

1.22    ! deraadt     1: /*     $OpenBSD: domacro.c,v 1.21 2019/05/16 12:44:17 florian Exp $    */
1.7       millert     2: /*     $NetBSD: domacro.c,v 1.10 1997/07/20 09:45:45 lukem Exp $       */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1985, 1993, 1994
                      6:  *     The Regents of the University of California.  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.
1.9       millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
1.16      martynas   33: #ifndef SMALL
                     34:
1.1       deraadt    35: #include <ctype.h>
                     36: #include <signal.h>
                     37: #include <stdio.h>
1.3       millert    38: #include <string.h>
1.1       deraadt    39:
                     40: #include "ftp_var.h"
                     41:
                     42: void
1.11      deraadt    43: domacro(int argc, char *argv[])
1.1       deraadt    44: {
                     45:        int i, j, count = 2, loopflg = 0;
1.8       itojun     46:        char *cp1, *cp2, line2[FTPBUFLEN];
1.1       deraadt    47:        struct cmd *c;
                     48:
                     49:        if (argc < 2 && !another(&argc, &argv, "macro name")) {
1.14      sobrado    50:                fprintf(ttyout, "usage: %s macro-name\n", argv[0]);
1.1       deraadt    51:                code = -1;
                     52:                return;
                     53:        }
                     54:        for (i = 0; i < macnum; ++i) {
                     55:                if (!strncmp(argv[1], macros[i].mac_name, 9)) {
                     56:                        break;
                     57:                }
                     58:        }
                     59:        if (i == macnum) {
1.6       deraadt    60:                fprintf(ttyout, "'%s' macro not found.\n", argv[1]);
1.1       deraadt    61:                code = -1;
                     62:                return;
                     63:        }
1.8       itojun     64:        (void)strlcpy(line2, line, sizeof(line2));
1.1       deraadt    65: TOP:
                     66:        cp1 = macros[i].mac_start;
                     67:        while (cp1 != macros[i].mac_end) {
1.18      mmcc       68:                while (isspace((unsigned char)*cp1)) {
1.1       deraadt    69:                        cp1++;
                     70:                }
                     71:                cp2 = line;
                     72:                while (*cp1 != '\0') {
1.22    ! deraadt    73:                        switch(*cp1) {
        !            74:                        case '\\':
        !            75:                                *cp2++ = *++cp1;
        !            76:                                break;
        !            77:                        case '$':
        !            78:                                if (isdigit((unsigned char)*(cp1 + 1))) {
        !            79:                                        j = 0;
        !            80:                                        while (isdigit((unsigned char)*++cp1)) {
        !            81:                                                j = 10*j +  *cp1 - '0';
        !            82:                                        }
        !            83:                                        cp1--;
        !            84:                                        if (argc - 2 >= j) {
        !            85:                                                (void)strlcpy(cp2, argv[j+1],
        !            86:                                                    sizeof(line) - (cp2 - line));
        !            87:                                                cp2 += strlen(argv[j+1]);
        !            88:                                        }
        !            89:                                        break;
        !            90:                                }
        !            91:                                if (*(cp1+1) == 'i') {
1.1       deraadt    92:                                        loopflg = 1;
                     93:                                        cp1++;
                     94:                                        if (count < argc) {
1.22    ! deraadt    95:                                                (void)strlcpy(cp2, argv[count],
        !            96:                                                    sizeof(line) - (cp2 - line));
        !            97:                                                cp2 += strlen(argv[count]);
1.1       deraadt    98:                                        }
                     99:                                        break;
                    100:                                }
1.13      ray       101:                                /* FALLTHROUGH */
1.22    ! deraadt   102:                        default:
1.1       deraadt   103:                                *cp2++ = *cp1;
                    104:                                break;
1.22    ! deraadt   105:                        }
        !           106:                        if (*cp1 != '\0') {
        !           107:                                cp1++;
        !           108:                        }
1.1       deraadt   109:                }
                    110:                *cp2 = '\0';
                    111:                makeargv();
                    112:                c = getcmd(margv[0]);
                    113:                if (c == (struct cmd *)-1) {
1.6       deraadt   114:                        fputs("?Ambiguous command.\n", ttyout);
1.1       deraadt   115:                        code = -1;
1.22    ! deraadt   116:                } else if (c == 0) {
1.6       deraadt   117:                        fputs("?Invalid command.\n", ttyout);
1.1       deraadt   118:                        code = -1;
1.22    ! deraadt   119:                } else if (c->c_conn && !connected) {
1.6       deraadt   120:                        fputs("Not connected.\n", ttyout);
1.1       deraadt   121:                        code = -1;
1.22    ! deraadt   122:                } else {
1.6       deraadt   123:                        if (verbose) {
                    124:                                fputs(line, ttyout);
1.7       millert   125:                                fputc('\n', ttyout);
1.6       deraadt   126:                        }
1.1       deraadt   127:                        (*c->c_handler)(margc, margv);
                    128:                        if (bell && c->c_bell) {
1.6       deraadt   129:                                (void)putc('\007', ttyout);
1.1       deraadt   130:                        }
1.8       itojun    131:                        (void)strlcpy(line, line2, sizeof(line));
1.1       deraadt   132:                        makeargv();
                    133:                        argc = margc;
                    134:                        argv = margv;
                    135:                }
                    136:                if (cp1 != macros[i].mac_end) {
                    137:                        cp1++;
                    138:                }
                    139:        }
                    140:        if (loopflg && ++count < argc) {
                    141:                goto TOP;
                    142:        }
                    143: }
1.16      martynas  144:
                    145: #endif /* !SMALL */
1.17      martynas  146: