xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.bin/ftp/domacro.c (revision 2c5ec7a875dcd76853e6618614e990f1e8cdd56d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
24  *	Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	All Rights Reserved  	*/
29 
30 /*
31  *	University Copyright- Copyright (c) 1982, 1986, 1988
32  *	The Regents of the University of California
33  *	All Rights Reserved
34  *
35  *	University Acknowledgment- Portions of this document are derived from
36  *	software developed by the University of California, Berkeley, and its
37  *	contributors.
38  */
39 
40 #include <libintl.h>
41 #include <stdlib.h>
42 
43 #include "ftp_var.h"
44 
45 #include <signal.h>
46 #include <stdio.h>
47 #include <errno.h>
48 #include <ctype.h>
49 
50 void
51 domacro(int argc, char *argv[])
52 {
53 	register int i, j;
54 	register char *cp1, *cp2;
55 	int count = 2, loopflg = 0;
56 	char line2[200];
57 	struct cmd *c;
58 	int	len;
59 
60 	if (argc < 2) {
61 		stop_timer();
62 		(void) strcat(line, " ");
63 		printf("(macro name) ");
64 		(void) gets(&line[strlen(line)]);
65 		reset_timer();
66 		makeargv();
67 		argc = margc;
68 		argv = margv;
69 	}
70 	if (argc < 2) {
71 		printf("Usage: %s macro_name.\n", argv[0]);
72 		code = -1;
73 		return;
74 	}
75 	for (i = 0; i < macnum; ++i) {
76 		if (strncmp(argv[1], macros[i].mac_name, 9) == 0) {
77 			break;
78 		}
79 	}
80 	if (i == macnum) {
81 		printf("'%s' macro not found.\n", argv[1]);
82 		code = -1;
83 		return;
84 	}
85 	(void) strcpy(line2, line);
86 TOP:
87 	cp1 = macros[i].mac_start;
88 	while (cp1 != macros[i].mac_end) {
89 		while (isspace(*cp1)) {
90 			cp1++;
91 		}
92 		cp2 = line;
93 		while (*cp1 != '\0') {
94 			switch (*cp1) {
95 			case '\\':
96 				cp1++;
97 				if ((len = mblen(cp1, MB_CUR_MAX)) <= 0)
98 					len = 1;
99 				memcpy(cp2, cp1, len);
100 				cp2 += len;
101 				cp1 += len;
102 				break;
103 
104 			case '$':
105 				if (isdigit(*(cp1+1))) {
106 					j = 0;
107 					while (isdigit(*++cp1))
108 						j = 10 * j +  *cp1 - '0';
109 					if (argc - 2 >= j) {
110 						(void) strcpy(cp2, argv[j+1]);
111 						cp2 += strlen(argv[j+1]);
112 					}
113 					break;
114 				}
115 				if (*(cp1+1) == 'i') {
116 					loopflg = 1;
117 					cp1 += 2;
118 					if (count < argc) {
119 						(void) strcpy(cp2, argv[count]);
120 						cp2 += strlen(argv[count]);
121 					}
122 					break;
123 				}
124 				/* FALLTHROUGH */
125 			default:
126 				if ((len = mblen(cp1, MB_CUR_MAX)) <= 0)
127 					len = 1;
128 				memcpy(cp2, cp1, len);
129 				cp2 += len;
130 				cp1 += len;
131 				break;
132 			}
133 		}
134 		*cp2 = '\0';
135 		makeargv();
136 		if (margv[0] == NULL) {
137 			code = -1;
138 			return;
139 		} else {
140 			c = getcmd(margv[0]);
141 		}
142 		if (c == (struct cmd *)-1) {
143 			printf("?Ambiguous command\n");
144 			code = -1;
145 		} else if (c == 0) {
146 			printf("?Invalid command\n");
147 			code = -1;
148 		} else if (c->c_conn && !connected) {
149 			printf("Not connected.\n");
150 			code = -1;
151 		} else {
152 			if (verbose) {
153 				printf("%s\n", line);
154 			}
155 			(*c->c_handler)(margc, margv);
156 #define	CTRL(c) ((c)&037)
157 			if (bell && c->c_bell) {
158 				(void) putchar(CTRL('g'));
159 			}
160 			(void) strcpy(line, line2);
161 			makeargv();
162 			argc = margc;
163 			argv = margv;
164 		}
165 		if (cp1 != macros[i].mac_end) {
166 			cp1++;
167 		}
168 	}
169 	if (loopflg && ++count < argc) {
170 		goto TOP;
171 	}
172 }
173