xref: /csrg-svn/usr.bin/ftp/domacro.c (revision 36935)
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) 02/28/89";
20 #endif /* not lint */
21 
22 #include "ftp_var.h"
23 
24 #include <signal.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <sys/ttychars.h>
29 
30 domacro(argc, argv)
31 	int argc;
32 	char *argv[];
33 {
34 	register int i, j;
35 	register char *cp1, *cp2;
36 	int count = 2, loopflg = 0;
37 	char line2[200];
38 	extern char **glob(), *globerr;
39 	struct cmd *getcmd(), *c;
40 	extern struct cmd cmdtab[];
41 
42 	if (argc < 2) {
43 		(void) strcat(line, " ");
44 		printf("(macro name) ");
45 		(void) gets(&line[strlen(line)]);
46 		makeargv();
47 		argc = margc;
48 		argv = margv;
49 	}
50 	if (argc < 2) {
51 		printf("Usage: %s macro_name.\n", argv[0]);
52 		code = -1;
53 		return;
54 	}
55 	for (i = 0; i < macnum; ++i) {
56 		if (!strncmp(argv[1], macros[i].mac_name, 9)) {
57 			break;
58 		}
59 	}
60 	if (i == macnum) {
61 		printf("'%s' macro not found.\n", argv[1]);
62 		code = -1;
63 		return;
64 	}
65 	(void) strcpy(line2, line);
66 TOP:
67 	cp1 = macros[i].mac_start;
68 	while (cp1 != macros[i].mac_end) {
69 		while (isspace(*cp1)) {
70 			cp1++;
71 		}
72 		cp2 = line;
73 		while (*cp1 != '\0') {
74 		      switch(*cp1) {
75 		   	    case '\\':
76 				 *cp2++ = *++cp1;
77 				 break;
78 			    case '$':
79 				 if (isdigit(*(cp1+1))) {
80 				    j = 0;
81 				    while (isdigit(*++cp1)) {
82 					  j = 10*j +  *cp1 - '0';
83 				    }
84 				    cp1--;
85 				    if (argc - 2 >= j) {
86 					(void) strcpy(cp2, argv[j+1]);
87 					cp2 += strlen(argv[j+1]);
88 				    }
89 				    break;
90 				 }
91 				 if (*(cp1+1) == 'i') {
92 					loopflg = 1;
93 					cp1++;
94 					if (count < argc) {
95 					   (void) strcpy(cp2, argv[count]);
96 					   cp2 += strlen(argv[count]);
97 					}
98 					break;
99 				}
100 				/* intentional drop through */
101 			    default:
102 				*cp2++ = *cp1;
103 				break;
104 		      }
105 		      if (*cp1 != '\0') {
106 			 cp1++;
107 		      }
108 		}
109 		*cp2 = '\0';
110 		makeargv();
111 		c = getcmd(margv[0]);
112 		if (c == (struct cmd *)-1) {
113 			printf("?Ambiguous command\n");
114 			code = -1;
115 		}
116 		else if (c == 0) {
117 			printf("?Invalid command\n");
118 			code = -1;
119 		}
120 		else if (c->c_conn && !connected) {
121 			printf("Not connected.\n");
122 			code = -1;
123 		}
124 		else {
125 			if (verbose) {
126 				printf("%s\n",line);
127 			}
128 			(*c->c_handler)(margc, margv);
129 			if (bell && c->c_bell) {
130 				(void) putchar('\007');
131 			}
132 			(void) strcpy(line, line2);
133 			makeargv();
134 			argc = margc;
135 			argv = margv;
136 		}
137 		if (cp1 != macros[i].mac_end) {
138 			cp1++;
139 		}
140 	}
141 	if (loopflg && ++count < argc) {
142 		goto TOP;
143 	}
144 }
145