1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)domacro.c	1.6 (Berkeley) 2/28/89";
20 #endif /* not lint */
21 
22 #include "ftp_var.h"
23 #include "prototypes.h"
24 
25 #include <signal.h>
26 #include <stdio.h>
27 //#include <errno.h>
28 #include <ctype.h>
29 //#include <sys/ttychars.h>
30 
31 void domacro(argc, argv)
32 	int argc;
33 	const char *argv[];
34 {
35 	int i, j;
36 	const char *cp1;
37     char *cp2;
38 	int count = 2, loopflg = 0;
39 	char line2[200];
40 	struct cmd *getcmd(), *c;
41 
42 	if (argc < 2) {
43 		(void) strcat(line, " ");
44 		printf("(macro name) ");
45 		(void) fflush(stdout);
46 		(void) gets(&line[strlen(line)]);
47 		makeargv();
48 		argc = margc;
49 		argv = margv;
50 	}
51 	if (argc < 2) {
52 		printf("Usage: %s macro_name.\n", argv[0]);
53 		(void) fflush(stdout);
54 		code = -1;
55 		return;
56 	}
57 	for (i = 0; i < macnum; ++i) {
58 		if (!strncmp(argv[1], macros[i].mac_name, 9)) {
59 			break;
60 		}
61 	}
62 	if (i == macnum) {
63 		printf("'%s' macro not found.\n", argv[1]);
64 		(void) fflush(stdout);
65 		code = -1;
66 		return;
67 	}
68 	(void) strcpy(line2, line);
69 TOP:
70 	cp1 = macros[i].mac_start;
71 	while (cp1 != macros[i].mac_end) {
72 		while (isspace(*cp1)) {
73 			cp1++;
74 		}
75 		cp2 = line;
76 		while (*cp1 != '\0') {
77 		      switch(*cp1) {
78 		   	    case '\\':
79 				 *cp2++ = *++cp1;
80 				 break;
81 			    case '$':
82 				 if (isdigit(*(cp1+1))) {
83 				    j = 0;
84 				    while (isdigit(*++cp1)) {
85 					  j = 10*j +  *cp1 - '0';
86 				    }
87 				    cp1--;
88 				    if (argc - 2 >= j) {
89 					(void) strcpy(cp2, argv[j+1]);
90 					cp2 += strlen(argv[j+1]);
91 				    }
92 				    break;
93 				 }
94 				 if (*(cp1+1) == 'i') {
95 					loopflg = 1;
96 					cp1++;
97 					if (count < argc) {
98 					   (void) strcpy(cp2, argv[count]);
99 					   cp2 += strlen(argv[count]);
100 					}
101 					break;
102 				}
103 				/* intentional drop through */
104 			    default:
105 				*cp2++ = *cp1;
106 				break;
107 		      }
108 		      if (*cp1 != '\0') {
109 			 cp1++;
110 		      }
111 		}
112 		*cp2 = '\0';
113 		makeargv();
114 		c = getcmd(margv[0]);
115 		if (c == (struct cmd *)-1) {
116 			printf("?Ambiguous command\n");
117 			code = -1;
118 		}
119 		else if (c == 0) {
120 			printf("?Invalid command\n");
121 			code = -1;
122 		}
123 		else if (c->c_conn && !connected) {
124 			printf("Not connected.\n");
125 			code = -1;
126 		}
127 		else {
128 			if (verbose) {
129 				printf("%s\n",line);
130 			}
131 			(*c->c_handler)(margc, margv);
132 			if (bell && c->c_bell) {
133 				(void) putchar('\007');
134 			}
135 			(void) strcpy(line, line2);
136 			makeargv();
137 			argc = margc;
138 			argv = margv;
139 		}
140 		if (cp1 != macros[i].mac_end) {
141 			cp1++;
142 		}
143 		(void) fflush(stdout);
144 	}
145 	if (loopflg && ++count < argc) {
146 		goto TOP;
147 	}
148 }
149