xref: /csrg-svn/usr.bin/ftp/domacro.c (revision 33737)
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 this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)domacro.c	1.4 (Berkeley) 03/14/88";
15 #endif /* not lint */
16 
17 #include "ftp_var.h"
18 
19 #include <signal.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <sys/ttychars.h>
24 
25 domacro(argc, argv)
26 	int argc;
27 	char *argv[];
28 {
29 	register int i, j;
30 	register char *cp1, *cp2;
31 	int count = 2, loopflg = 0;
32 	char line2[200];
33 	extern char **glob(), *globerr;
34 	struct cmd *getcmd(), *c;
35 	extern struct cmd cmdtab[];
36 
37 	if (argc < 2) {
38 		(void) strcat(line, " ");
39 		printf("(macro name) ");
40 		(void) gets(&line[strlen(line)]);
41 		makeargv();
42 		argc = margc;
43 		argv = margv;
44 	}
45 	if (argc < 2) {
46 		printf("Usage: %s macro_name.\n", argv[0]);
47 		code = -1;
48 		return;
49 	}
50 	for (i = 0; i < macnum; ++i) {
51 		if (!strncmp(argv[1], macros[i].mac_name, 9)) {
52 			break;
53 		}
54 	}
55 	if (i == macnum) {
56 		printf("'%s' macro not found.\n", argv[1]);
57 		code = -1;
58 		return;
59 	}
60 	(void) strcpy(line2, line);
61 TOP:
62 	cp1 = macros[i].mac_start;
63 	while (cp1 != macros[i].mac_end) {
64 		while (isspace(*cp1)) {
65 			cp1++;
66 		}
67 		cp2 = line;
68 		while (*cp1 != '\0') {
69 		      switch(*cp1) {
70 		   	    case '\\':
71 				 *cp2++ = *++cp1;
72 				 break;
73 			    case '$':
74 				 if (isdigit(*(cp1+1))) {
75 				    j = 0;
76 				    while (isdigit(*++cp1)) {
77 					  j = 10*j +  *cp1 - '0';
78 				    }
79 				    cp1--;
80 				    if (argc - 2 >= j) {
81 					(void) strcpy(cp2, argv[j+1]);
82 					cp2 += strlen(argv[j+1]);
83 				    }
84 				    break;
85 				 }
86 				 if (*(cp1+1) == 'i') {
87 					loopflg = 1;
88 					cp1++;
89 					if (count < argc) {
90 					   (void) strcpy(cp2, argv[count]);
91 					   cp2 += strlen(argv[count]);
92 					}
93 					break;
94 				}
95 				/* intentional drop through */
96 			    default:
97 				*cp2++ = *cp1;
98 				break;
99 		      }
100 		      if (*cp1 != '\0') {
101 			 cp1++;
102 		      }
103 		}
104 		*cp2 = '\0';
105 		makeargv();
106 		c = getcmd(margv[0]);
107 		if (c == (struct cmd *)-1) {
108 			printf("?Ambiguous command\n");
109 			code = -1;
110 		}
111 		else if (c == 0) {
112 			printf("?Invalid command\n");
113 			code = -1;
114 		}
115 		else if (c->c_conn && !connected) {
116 			printf("Not connected.\n");
117 			code = -1;
118 		}
119 		else {
120 			if (verbose) {
121 				printf("%s\n",line);
122 			}
123 			(*c->c_handler)(margc, margv);
124 			if (bell && c->c_bell) {
125 				(void) putchar(CTRL('g'));
126 			}
127 			(void) strcpy(line, line2);
128 			makeargv();
129 			argc = margc;
130 			argv = margv;
131 		}
132 		if (cp1 != macros[i].mac_end) {
133 			cp1++;
134 		}
135 	}
136 	if (loopflg && ++count < argc) {
137 		goto TOP;
138 	}
139 }
140