xref: /freebsd/contrib/tnftp/src/domacro.c (revision aa24f48b361effe51163877d84f1b70d32b77e04)
1 /*	$NetBSD: domacro.c,v 1.8 2009/05/20 12:53:47 lukem Exp $	*/
2 /*	from	NetBSD: domacro.c,v 1.22 2009/04/12 10:18:52 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 "tnftp.h"
34 
35 #if 0	/* tnftp */
36 
37 #include <sys/cdefs.h>
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)domacro.c	8.3 (Berkeley) 4/2/94";
41 #else
42 __RCSID(" NetBSD: domacro.c,v 1.22 2009/04/12 10:18:52 lukem Exp  ");
43 #endif
44 #endif /* not lint */
45 
46 #include <ctype.h>
47 #include <stdio.h>
48 #include <string.h>
49 
50 #endif	/* tnftp */
51 
52 #include "ftp_var.h"
53 
54 void
55 domacro(int argc, char *argv[])
56 {
57 	int i, j, count = 2, loopflg = 0;
58 	char *cp1, *cp2, line2[FTPBUFLEN];
59 	struct cmd *c;
60 	char cmdbuf[MAX_C_NAME];
61 
62 	if ((argc == 0 && argv != NULL) ||
63 	    (argc < 2 && !another(&argc, &argv, "macro name"))) {
64 		UPRINTF("usage: %s macro_name [args]\n", argv[0]);
65 		code = -1;
66 		return;
67 	}
68 	for (i = 0; i < macnum; ++i) {
69 		if (!strncmp(argv[1], macros[i].mac_name, 9))
70 			break;
71 	}
72 	if (i == macnum) {
73 		fprintf(ttyout, "'%s' macro not found.\n", argv[1]);
74 		code = -1;
75 		return;
76 	}
77 	(void)strlcpy(line2, line, sizeof(line2));
78  TOP:
79 	cp1 = macros[i].mac_start;
80 	while (cp1 != macros[i].mac_end) {
81 		while (isspace((unsigned char)*cp1))
82 			cp1++;
83 		cp2 = line;
84 		while (*cp1 != '\0') {
85 			switch(*cp1) {
86 			case '\\':
87 				*cp2++ = *++cp1;
88 				break;
89 			case '$':
90 				if (isdigit((unsigned char)*(cp1+1))) {
91 					j = 0;
92 					while (isdigit((unsigned char)*++cp1))
93 						j = 10*j +  *cp1 - '0';
94 					cp1--;
95 					if (argc - 2 >= j) {
96 						(void)strlcpy(cp2, argv[j+1],
97 						    sizeof(line) - (cp2 - line));
98 						cp2 += strlen(argv[j+1]);
99 					}
100 					break;
101 				}
102 				if (*(cp1+1) == 'i') {
103 					loopflg = 1;
104 					cp1++;
105 					if (count < argc) {
106 						(void)strlcpy(cp2, argv[count],
107 						    sizeof(line) - (cp2 - line));
108 						cp2 += strlen(argv[count]);
109 					}
110 					break;
111 				}
112 				/* intentional drop through */
113 			default:
114 				*cp2++ = *cp1;
115 				break;
116 			}
117 			if (*cp1 != '\0')
118 				cp1++;
119 		}
120 		*cp2 = '\0';
121 		makeargv();
122 		c = getcmd(margv[0]);
123 		if (c == (struct cmd *)-1) {
124 			fputs("?Ambiguous command.\n", ttyout);
125 			code = -1;
126 		} else if (c == 0) {
127 			fputs("?Invalid command.\n", ttyout);
128 			code = -1;
129 		} else if (c->c_conn && !connected) {
130 			fputs("Not connected.\n", ttyout);
131 			code = -1;
132 		} else {
133 			if (verbose) {
134 				fputs(line, ttyout);
135 				putc('\n', ttyout);
136 			}
137 			(void)strlcpy(cmdbuf, c->c_name, sizeof(cmdbuf));
138 			margv[0] = cmdbuf;
139 			(*c->c_handler)(margc, margv);
140 			if (bell && c->c_bell)
141 				(void)putc('\007', ttyout);
142 			(void)strlcpy(line, line2, sizeof(line));
143 			makeargv();
144 			argc = margc;
145 			argv = margv;
146 		}
147 		if (cp1 != macros[i].mac_end)
148 			cp1++;
149 	}
150 	if (loopflg && ++count < argc)
151 		goto TOP;
152 }
153