xref: /original-bsd/usr.bin/ftp/domacro.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1985, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)domacro.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "ftp_var.h"
13 
14 #include <signal.h>
15 #include <stdio.h>
16 #include <errno.h>
17 #include <ctype.h>
18 #include <sys/ttychars.h>
19 
20 domacro(argc, argv)
21 	int argc;
22 	char *argv[];
23 {
24 	register int i, j;
25 	register char *cp1, *cp2;
26 	int count = 2, loopflg = 0;
27 	char line2[200];
28 	extern char **glob(), *globerr;
29 	struct cmd *getcmd(), *c;
30 	extern struct cmd cmdtab[];
31 
32 	if (argc < 2 && !another(&argc, &argv, "macro name")) {
33 		printf("Usage: %s macro_name.\n", argv[0]);
34 		code = -1;
35 		return;
36 	}
37 	for (i = 0; i < macnum; ++i) {
38 		if (!strncmp(argv[1], macros[i].mac_name, 9)) {
39 			break;
40 		}
41 	}
42 	if (i == macnum) {
43 		printf("'%s' macro not found.\n", argv[1]);
44 		code = -1;
45 		return;
46 	}
47 	(void) strcpy(line2, line);
48 TOP:
49 	cp1 = macros[i].mac_start;
50 	while (cp1 != macros[i].mac_end) {
51 		while (isspace(*cp1)) {
52 			cp1++;
53 		}
54 		cp2 = line;
55 		while (*cp1 != '\0') {
56 		      switch(*cp1) {
57 		   	    case '\\':
58 				 *cp2++ = *++cp1;
59 				 break;
60 			    case '$':
61 				 if (isdigit(*(cp1+1))) {
62 				    j = 0;
63 				    while (isdigit(*++cp1)) {
64 					  j = 10*j +  *cp1 - '0';
65 				    }
66 				    cp1--;
67 				    if (argc - 2 >= j) {
68 					(void) strcpy(cp2, argv[j+1]);
69 					cp2 += strlen(argv[j+1]);
70 				    }
71 				    break;
72 				 }
73 				 if (*(cp1+1) == 'i') {
74 					loopflg = 1;
75 					cp1++;
76 					if (count < argc) {
77 					   (void) strcpy(cp2, argv[count]);
78 					   cp2 += strlen(argv[count]);
79 					}
80 					break;
81 				}
82 				/* intentional drop through */
83 			    default:
84 				*cp2++ = *cp1;
85 				break;
86 		      }
87 		      if (*cp1 != '\0') {
88 			 cp1++;
89 		      }
90 		}
91 		*cp2 = '\0';
92 		makeargv();
93 		c = getcmd(margv[0]);
94 		if (c == (struct cmd *)-1) {
95 			printf("?Ambiguous command\n");
96 			code = -1;
97 		}
98 		else if (c == 0) {
99 			printf("?Invalid command\n");
100 			code = -1;
101 		}
102 		else if (c->c_conn && !connected) {
103 			printf("Not connected.\n");
104 			code = -1;
105 		}
106 		else {
107 			if (verbose) {
108 				printf("%s\n",line);
109 			}
110 			(*c->c_handler)(margc, margv);
111 			if (bell && c->c_bell) {
112 				(void) putchar('\007');
113 			}
114 			(void) strcpy(line, line2);
115 			makeargv();
116 			argc = margc;
117 			argv = margv;
118 		}
119 		if (cp1 != macros[i].mac_end) {
120 			cp1++;
121 		}
122 	}
123 	if (loopflg && ++count < argc) {
124 		goto TOP;
125 	}
126 }
127