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