xref: /csrg-svn/usr.bin/ftp/domacro.c (revision 36935)
126052Sminshall /*
226052Sminshall  * Copyright (c) 1985 Regents of the University of California.
333737Sbostic  * All rights reserved.
433737Sbostic  *
533737Sbostic  * Redistribution and use in source and binary forms are permitted
634901Sbostic  * provided that the above copyright notice and this paragraph are
734901Sbostic  * duplicated in all such forms and that any documentation,
834901Sbostic  * advertising materials, and other materials related to such
934901Sbostic  * distribution and use acknowledge that the software was developed
1034901Sbostic  * by the University of California, Berkeley.  The name of the
1134901Sbostic  * University may not be used to endorse or promote products derived
1234901Sbostic  * from this software without specific prior written permission.
1334901Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434901Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*36935Skarels  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1626052Sminshall  */
1726052Sminshall 
1826052Sminshall #ifndef lint
19*36935Skarels static char sccsid[] = "@(#)domacro.c	1.6 (Berkeley) 02/28/89";
2033737Sbostic #endif /* not lint */
2126052Sminshall 
2226052Sminshall #include "ftp_var.h"
2326052Sminshall 
2426052Sminshall #include <signal.h>
2526052Sminshall #include <stdio.h>
2626052Sminshall #include <errno.h>
2726052Sminshall #include <ctype.h>
2826052Sminshall #include <sys/ttychars.h>
2926052Sminshall 
3026052Sminshall domacro(argc, argv)
3126052Sminshall 	int argc;
3226052Sminshall 	char *argv[];
3326052Sminshall {
3426052Sminshall 	register int i, j;
3526052Sminshall 	register char *cp1, *cp2;
3626498Sminshall 	int count = 2, loopflg = 0;
3726498Sminshall 	char line2[200];
3826052Sminshall 	extern char **glob(), *globerr;
3926052Sminshall 	struct cmd *getcmd(), *c;
4026052Sminshall 	extern struct cmd cmdtab[];
4126052Sminshall 
4226052Sminshall 	if (argc < 2) {
4326498Sminshall 		(void) strcat(line, " ");
4426052Sminshall 		printf("(macro name) ");
4526498Sminshall 		(void) gets(&line[strlen(line)]);
4626052Sminshall 		makeargv();
4726052Sminshall 		argc = margc;
4826052Sminshall 		argv = margv;
4926052Sminshall 	}
5026052Sminshall 	if (argc < 2) {
5126052Sminshall 		printf("Usage: %s macro_name.\n", argv[0]);
5226052Sminshall 		code = -1;
5326052Sminshall 		return;
5426052Sminshall 	}
5526052Sminshall 	for (i = 0; i < macnum; ++i) {
5626052Sminshall 		if (!strncmp(argv[1], macros[i].mac_name, 9)) {
5726052Sminshall 			break;
5826052Sminshall 		}
5926052Sminshall 	}
6026052Sminshall 	if (i == macnum) {
6126052Sminshall 		printf("'%s' macro not found.\n", argv[1]);
6226052Sminshall 		code = -1;
6326052Sminshall 		return;
6426052Sminshall 	}
6526498Sminshall 	(void) strcpy(line2, line);
6626052Sminshall TOP:
6726052Sminshall 	cp1 = macros[i].mac_start;
6826052Sminshall 	while (cp1 != macros[i].mac_end) {
6926052Sminshall 		while (isspace(*cp1)) {
7026052Sminshall 			cp1++;
7126052Sminshall 		}
7226052Sminshall 		cp2 = line;
7326052Sminshall 		while (*cp1 != '\0') {
7426052Sminshall 		      switch(*cp1) {
7526052Sminshall 		   	    case '\\':
7626052Sminshall 				 *cp2++ = *++cp1;
7726052Sminshall 				 break;
7826052Sminshall 			    case '$':
7926052Sminshall 				 if (isdigit(*(cp1+1))) {
8026052Sminshall 				    j = 0;
8126052Sminshall 				    while (isdigit(*++cp1)) {
8226052Sminshall 					  j = 10*j +  *cp1 - '0';
8326052Sminshall 				    }
8426052Sminshall 				    cp1--;
8526052Sminshall 				    if (argc - 2 >= j) {
8626498Sminshall 					(void) strcpy(cp2, argv[j+1]);
8726052Sminshall 					cp2 += strlen(argv[j+1]);
8826052Sminshall 				    }
8926052Sminshall 				    break;
9026052Sminshall 				 }
9126052Sminshall 				 if (*(cp1+1) == 'i') {
9226052Sminshall 					loopflg = 1;
9326052Sminshall 					cp1++;
9426052Sminshall 					if (count < argc) {
9526498Sminshall 					   (void) strcpy(cp2, argv[count]);
9626052Sminshall 					   cp2 += strlen(argv[count]);
9726052Sminshall 					}
9826052Sminshall 					break;
9926052Sminshall 				}
10026052Sminshall 				/* intentional drop through */
10126052Sminshall 			    default:
10226052Sminshall 				*cp2++ = *cp1;
10326052Sminshall 				break;
10426052Sminshall 		      }
10526052Sminshall 		      if (*cp1 != '\0') {
10626052Sminshall 			 cp1++;
10726052Sminshall 		      }
10826052Sminshall 		}
10926052Sminshall 		*cp2 = '\0';
11026052Sminshall 		makeargv();
11126052Sminshall 		c = getcmd(margv[0]);
11226052Sminshall 		if (c == (struct cmd *)-1) {
11326052Sminshall 			printf("?Ambiguous command\n");
11426052Sminshall 			code = -1;
11526052Sminshall 		}
11626052Sminshall 		else if (c == 0) {
11726052Sminshall 			printf("?Invalid command\n");
11826052Sminshall 			code = -1;
11926052Sminshall 		}
12026052Sminshall 		else if (c->c_conn && !connected) {
12126052Sminshall 			printf("Not connected.\n");
12226052Sminshall 			code = -1;
12326052Sminshall 		}
12426052Sminshall 		else {
12526052Sminshall 			if (verbose) {
12626052Sminshall 				printf("%s\n",line);
12726052Sminshall 			}
12826052Sminshall 			(*c->c_handler)(margc, margv);
12926052Sminshall 			if (bell && c->c_bell) {
130*36935Skarels 				(void) putchar('\007');
13126052Sminshall 			}
13226498Sminshall 			(void) strcpy(line, line2);
13326052Sminshall 			makeargv();
13426052Sminshall 			argc = margc;
13526052Sminshall 			argv = margv;
13626052Sminshall 		}
13726052Sminshall 		if (cp1 != macros[i].mac_end) {
13826052Sminshall 			cp1++;
13926052Sminshall 		}
14026052Sminshall 	}
14126052Sminshall 	if (loopflg && ++count < argc) {
14226052Sminshall 		goto TOP;
14326052Sminshall 	}
14426052Sminshall }
145