Annotation of src/usr.bin/ftp/domacro.c, Revision 1.16
1.16 ! martynas 1: /* $OpenBSD: domacro.c,v 1.15 2009/04/27 21:37:13 deraadt 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) {
68: while (isspace(*cp1)) {
69: cp1++;
70: }
71: cp2 = line;
72: while (*cp1 != '\0') {
73: switch(*cp1) {
74: case '\\':
75: *cp2++ = *++cp1;
76: break;
77: case '$':
78: if (isdigit(*(cp1+1))) {
79: j = 0;
80: while (isdigit(*++cp1)) {
81: j = 10*j + *cp1 - '0';
82: }
83: cp1--;
84: if (argc - 2 >= j) {
1.8 itojun 85: (void)strlcpy(cp2, argv[j+1],
86: sizeof(line) - (cp2 - line));
1.1 deraadt 87: cp2 += strlen(argv[j+1]);
88: }
89: break;
90: }
91: if (*(cp1+1) == 'i') {
92: loopflg = 1;
93: cp1++;
94: if (count < argc) {
1.8 itojun 95: (void)strlcpy(cp2, argv[count],
96: sizeof(line) - (cp2 - line));
1.1 deraadt 97: cp2 += strlen(argv[count]);
98: }
99: break;
100: }
1.13 ray 101: /* FALLTHROUGH */
1.1 deraadt 102: default:
103: *cp2++ = *cp1;
104: break;
105: }
106: if (*cp1 != '\0') {
107: cp1++;
108: }
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;
116: }
117: else if (c == 0) {
1.6 deraadt 118: fputs("?Invalid command.\n", ttyout);
1.1 deraadt 119: code = -1;
120: }
121: else if (c->c_conn && !connected) {
1.6 deraadt 122: fputs("Not connected.\n", ttyout);
1.1 deraadt 123: code = -1;
124: }
125: else {
1.6 deraadt 126: if (verbose) {
127: fputs(line, ttyout);
1.7 millert 128: fputc('\n', ttyout);
1.6 deraadt 129: }
1.1 deraadt 130: (*c->c_handler)(margc, margv);
131: if (bell && c->c_bell) {
1.6 deraadt 132: (void)putc('\007', ttyout);
1.1 deraadt 133: }
1.8 itojun 134: (void)strlcpy(line, line2, sizeof(line));
1.1 deraadt 135: makeargv();
136: argc = margc;
137: argv = margv;
138: }
139: if (cp1 != macros[i].mac_end) {
140: cp1++;
141: }
142: }
143: if (loopflg && ++count < argc) {
144: goto TOP;
145: }
146: }
1.16 ! martynas 147:
! 148: #endif /* !SMALL */