1 /* $OpenBSD: domacro.c,v 1.13 2006/05/16 23:43:16 ray Exp $ */
2 /* $NetBSD: domacro.c,v 1.10 1997/07/20 09:45:45 lukem Exp $ */
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.
16 * 3. Neither the name of the University nor the names of its contributors
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
33 #include <ctype.h>
34 #include <signal.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "ftp_var.h"
39
40 __RCSID("$MirOS: src/usr.bin/ftp/domacro.c,v 1.4 2006/10/03 19:22:17 tg Exp $");
41
42 void
domacro(int argc,char * argv[])43 domacro(int argc, char *argv[])
44 {
45 int i, j, count = 2, loopflg = 0;
46 char *cp1, *cp2, line2[FTPBUFLEN];
47 struct cmd *c;
48
49 if (argc < 2 && !another(&argc, &argv, "macro name")) {
50 fprintf(ttyout, "usage: %s macro_name\n", argv[0]);
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) {
60 fprintf(ttyout, "'%s' macro not found.\n", argv[1]);
61 code = -1;
62 return;
63 }
64 (void)strlcpy(line2, line, sizeof(line2));
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) {
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') {
92 loopflg = 1;
93 cp1++;
94 if (count < argc) {
95 (void)strlcpy(cp2, argv[count],
96 sizeof(line) - (cp2 - line));
97 cp2 += strlen(argv[count]);
98 }
99 break;
100 }
101 /* FALLTHROUGH */
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) {
114 fputs("?Ambiguous command.\n", ttyout);
115 code = -1;
116 }
117 else if (c == 0) {
118 fputs("?Invalid command.\n", ttyout);
119 code = -1;
120 }
121 else if (c->c_conn && !connected) {
122 fputs("Not connected.\n", ttyout);
123 code = -1;
124 }
125 else {
126 if (verbose) {
127 fputs(line, ttyout);
128 fputc('\n', ttyout);
129 }
130 (*c->c_handler)(margc, margv);
131 if (bell && c->c_bell) {
132 (void)putc('\007', ttyout);
133 }
134 (void)strlcpy(line, line2, sizeof(line));
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 }
147