xref: /titanic_50/usr/src/cmd/cmd-inet/usr.bin/ftp/domacro.c (revision 2eeaed14a5e2ed9bd811643ad5bffc3510ca0310)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 #include <libintl.h>
43 #include <stdlib.h>
44 
45 #include "ftp_var.h"
46 
47 #include <signal.h>
48 #include <stdio.h>
49 #include <errno.h>
50 #include <ctype.h>
51 
52 void
53 domacro(int argc, char *argv[])
54 {
55 	register int i, j;
56 	register char *cp1, *cp2;
57 	int count = 2, loopflg = 0;
58 	char line2[200];
59 	struct cmd *c;
60 	int	len;
61 
62 	if (argc < 2) {
63 		stop_timer();
64 		(void) strcat(line, " ");
65 		printf("(macro name) ");
66 		(void) gets(&line[strlen(line)]);
67 		reset_timer();
68 		makeargv();
69 		argc = margc;
70 		argv = margv;
71 	}
72 	if (argc < 2) {
73 		printf("Usage: %s macro_name.\n", argv[0]);
74 		code = -1;
75 		return;
76 	}
77 	for (i = 0; i < macnum; ++i) {
78 		if (strncmp(argv[1], macros[i].mac_name, 9) == 0) {
79 			break;
80 		}
81 	}
82 	if (i == macnum) {
83 		printf("'%s' macro not found.\n", argv[1]);
84 		code = -1;
85 		return;
86 	}
87 	(void) strcpy(line2, line);
88 TOP:
89 	cp1 = macros[i].mac_start;
90 	while (cp1 != macros[i].mac_end) {
91 		while (isspace(*cp1)) {
92 			cp1++;
93 		}
94 		cp2 = line;
95 		while (*cp1 != '\0') {
96 			switch (*cp1) {
97 			case '\\':
98 				cp1++;
99 				if ((len = mblen(cp1, MB_CUR_MAX)) <= 0)
100 					len = 1;
101 				memcpy(cp2, cp1, len);
102 				cp2 += len;
103 				cp1 += len;
104 				break;
105 
106 			case '$':
107 				if (isdigit(*(cp1+1))) {
108 					j = 0;
109 					while (isdigit(*++cp1))
110 						j = 10 * j +  *cp1 - '0';
111 					if (argc - 2 >= j) {
112 						(void) strcpy(cp2, argv[j+1]);
113 						cp2 += strlen(argv[j+1]);
114 					}
115 					break;
116 				}
117 				if (*(cp1+1) == 'i') {
118 					loopflg = 1;
119 					cp1 += 2;
120 					if (count < argc) {
121 						(void) strcpy(cp2, argv[count]);
122 						cp2 += strlen(argv[count]);
123 					}
124 					break;
125 				}
126 				/* intentional drop through */
127 			default:
128 				if ((len = mblen(cp1, MB_CUR_MAX)) <= 0)
129 					len = 1;
130 				memcpy(cp2, cp1, len);
131 				cp2 += len;
132 				cp1 += len;
133 				break;
134 			}
135 		}
136 		*cp2 = '\0';
137 		makeargv();
138 		if (margv[0] == NULL) {
139 			code = -1;
140 			return;
141 		} else {
142 			c = getcmd(margv[0]);
143 		}
144 		if (c == (struct cmd *)-1) {
145 			printf("?Ambiguous command\n");
146 			code = -1;
147 		} else if (c == 0) {
148 			printf("?Invalid command\n");
149 			code = -1;
150 		} else if (c->c_conn && !connected) {
151 			printf("Not connected.\n");
152 			code = -1;
153 		} else {
154 			if (verbose) {
155 				printf("%s\n", line);
156 			}
157 			(*c->c_handler)(margc, margv);
158 #define	CTRL(c) ((c)&037)
159 			if (bell && c->c_bell) {
160 				(void) putchar(CTRL('g'));
161 			}
162 			(void) strcpy(line, line2);
163 			makeargv();
164 			argc = margc;
165 			argv = margv;
166 		}
167 		if (cp1 != macros[i].mac_end) {
168 			cp1++;
169 		}
170 	}
171 	if (loopflg && ++count < argc) {
172 		goto TOP;
173 	}
174 }
175