domacro.c revision 55682
1107120Sjulian/*
2107120Sjulian * Copyright (c) 1985, 1993, 1994
3139823Simp *	The Regents of the University of California.  All rights reserved.
4139823Simp *
5139823Simp * Redistribution and use in source and binary forms, with or without
6107120Sjulian * modification, are permitted provided that the following conditions
7107120Sjulian * are met:
8107120Sjulian * 1. Redistributions of source code must retain the above copyright
9107120Sjulian *    notice, this list of conditions and the following disclaimer.
10107120Sjulian * 2. Redistributions in binary form must reproduce the above copyright
11107120Sjulian *    notice, this list of conditions and the following disclaimer in the
12107120Sjulian *    documentation and/or other materials provided with the distribution.
13107120Sjulian * 3. All advertising materials mentioning features or use of this software
14107120Sjulian *    must display the following acknowledgement:
15107120Sjulian *	This product includes software developed by the University of
16107120Sjulian *	California, Berkeley and its contributors.
17107120Sjulian * 4. Neither the name of the University nor the names of its contributors
18107120Sjulian *    may be used to endorse or promote products derived from this software
19107120Sjulian *    without specific prior written permission.
20107120Sjulian *
21107120Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22107120Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23107120Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24107120Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25107120Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26107120Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27107120Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28107120Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29107120Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30114878Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31107120Sjulian * SUCH DAMAGE.
32107120Sjulian */
33107120Sjulian
34107120Sjulian#include "ftp_locl.h"
35107120SjulianRCSID("$Id: domacro.c,v 1.7 1999/09/16 20:37:29 assar Exp $");
36107120Sjulian
37107120Sjulianvoid
38107120Sjuliandomacro(int argc, char **argv)
39107120Sjulian{
40107120Sjulian	int i, j, count = 2, loopflg = 0;
41107120Sjulian	char *cp1, *cp2, line2[200];
42107120Sjulian	struct cmd *c;
43107120Sjulian
44107120Sjulian	if (argc < 2 && !another(&argc, &argv, "macro name")) {
45107120Sjulian		printf("Usage: %s macro_name.\n", argv[0]);
46107120Sjulian		code = -1;
47107120Sjulian		return;
48122634Semax	}
49107120Sjulian	for (i = 0; i < macnum; ++i) {
50107120Sjulian		if (!strncmp(argv[1], macros[i].mac_name, 9)) {
51107120Sjulian			break;
52107120Sjulian		}
53107120Sjulian	}
54107120Sjulian	if (i == macnum) {
55107120Sjulian		printf("'%s' macro not found.\n", argv[1]);
56107120Sjulian		code = -1;
57107120Sjulian		return;
58107120Sjulian	}
59107120Sjulian	strlcpy(line2, line, sizeof(line2));
60107120SjulianTOP:
61107120Sjulian	cp1 = macros[i].mac_start;
62107120Sjulian	while (cp1 != macros[i].mac_end) {
63107120Sjulian		while (isspace(*cp1)) {
64107120Sjulian			cp1++;
65107120Sjulian		}
66107120Sjulian		cp2 = line;
67107120Sjulian		while (*cp1 != '\0') {
68107120Sjulian		      switch(*cp1) {
69107120Sjulian		   	    case '\\':
70107120Sjulian				 *cp2++ = *++cp1;
71107120Sjulian				 break;
72107120Sjulian			    case '$':
73107120Sjulian				 if (isdigit(*(cp1+1))) {
74107120Sjulian				    j = 0;
75107120Sjulian				    while (isdigit(*++cp1)) {
76107120Sjulian					  j = 10*j +  *cp1 - '0';
77107120Sjulian				    }
78107120Sjulian				    cp1--;
79107120Sjulian				    if (argc - 2 >= j) {
80107120Sjulian					strcpy(cp2, argv[j+1]);
81107120Sjulian					cp2 += strlen(argv[j+1]);
82107120Sjulian				    }
83107120Sjulian				    break;
84107120Sjulian				 }
85107120Sjulian				 if (*(cp1+1) == 'i') {
86107120Sjulian					loopflg = 1;
87107120Sjulian					cp1++;
88107120Sjulian					if (count < argc) {
89107120Sjulian					   strcpy(cp2, argv[count]);
90107120Sjulian					   cp2 += strlen(argv[count]);
91107120Sjulian					}
92107120Sjulian					break;
93107120Sjulian				}
94107120Sjulian				/* intentional drop through */
95107120Sjulian			    default:
96107120Sjulian				*cp2++ = *cp1;
97107120Sjulian				break;
98107120Sjulian		      }
99107120Sjulian		      if (*cp1 != '\0') {
100107120Sjulian			 cp1++;
101107120Sjulian		      }
102107120Sjulian		}
103107120Sjulian		*cp2 = '\0';
104107120Sjulian		makeargv();
105107120Sjulian		c = getcmd(margv[0]);
106107120Sjulian		if (c == (struct cmd *)-1) {
107107120Sjulian			printf("?Ambiguous command\n");
108107120Sjulian			code = -1;
109107120Sjulian		}
110107120Sjulian		else if (c == 0) {
111107120Sjulian			printf("?Invalid command\n");
112107120Sjulian			code = -1;
113107120Sjulian		}
114107120Sjulian		else if (c->c_conn && !connected) {
115107120Sjulian			printf("Not connected.\n");
116107120Sjulian			code = -1;
117107120Sjulian		}
118107120Sjulian		else {
119107120Sjulian			if (verbose) {
120107120Sjulian				printf("%s\n",line);
121107120Sjulian			}
122107120Sjulian			(*c->c_handler)(margc, margv);
123107120Sjulian			if (bell && c->c_bell) {
124107120Sjulian				putchar('\007');
125107120Sjulian			}
126107120Sjulian			strcpy(line, line2);
127107120Sjulian			makeargv();
128107120Sjulian			argc = margc;
129107120Sjulian			argv = margv;
130107120Sjulian		}
131107120Sjulian		if (cp1 != macros[i].mac_end) {
132107120Sjulian			cp1++;
133107120Sjulian		}
134107120Sjulian	}
135107120Sjulian	if (loopflg && ++count < argc) {
136107120Sjulian		goto TOP;
137107120Sjulian	}
138107120Sjulian}
139107120Sjulian