xref: /openbsd-src/usr.bin/ftp/domacro.c (revision 544013c6c35af2bf66b85b3ea775ea993ffb9d07)
1 /*	$OpenBSD: domacro.c,v 1.22 2019/11/18 04:37:35 deraadt 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 #ifndef SMALL
34 
35 #include <ctype.h>
36 #include <signal.h>
37 #include <stdio.h>
38 #include <string.h>
39 
40 #include "ftp_var.h"
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((unsigned char)*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((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') {
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 		} else if (c == 0) {
117 			fputs("?Invalid command.\n", ttyout);
118 			code = -1;
119 		} else if (c->c_conn && !connected) {
120 			fputs("Not connected.\n", ttyout);
121 			code = -1;
122 		} else {
123 			if (verbose) {
124 				fputs(line, ttyout);
125 				fputc('\n', ttyout);
126 			}
127 			(*c->c_handler)(margc, margv);
128 			if (bell && c->c_bell) {
129 				(void)putc('\007', ttyout);
130 			}
131 			(void)strlcpy(line, line2, sizeof(line));
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 }
144 
145 #endif /* !SMALL */
146 
147