126052Sminshall /* 226052Sminshall * Copyright (c) 1985 Regents of the University of California. 3*33737Sbostic * All rights reserved. 4*33737Sbostic * 5*33737Sbostic * Redistribution and use in source and binary forms are permitted 6*33737Sbostic * provided that this notice is preserved and that due credit is given 7*33737Sbostic * to the University of California at Berkeley. The name of the University 8*33737Sbostic * may not be used to endorse or promote products derived from this 9*33737Sbostic * software without specific prior written permission. This software 10*33737Sbostic * is provided ``as is'' without express or implied warranty. 1126052Sminshall */ 1226052Sminshall 1326052Sminshall #ifndef lint 14*33737Sbostic static char sccsid[] = "@(#)domacro.c 1.4 (Berkeley) 03/14/88"; 15*33737Sbostic #endif /* not lint */ 1626052Sminshall 1726052Sminshall #include "ftp_var.h" 1826052Sminshall 1926052Sminshall #include <signal.h> 2026052Sminshall #include <stdio.h> 2126052Sminshall #include <errno.h> 2226052Sminshall #include <ctype.h> 2326052Sminshall #include <sys/ttychars.h> 2426052Sminshall 2526052Sminshall domacro(argc, argv) 2626052Sminshall int argc; 2726052Sminshall char *argv[]; 2826052Sminshall { 2926052Sminshall register int i, j; 3026052Sminshall register char *cp1, *cp2; 3126498Sminshall int count = 2, loopflg = 0; 3226498Sminshall char line2[200]; 3326052Sminshall extern char **glob(), *globerr; 3426052Sminshall struct cmd *getcmd(), *c; 3526052Sminshall extern struct cmd cmdtab[]; 3626052Sminshall 3726052Sminshall if (argc < 2) { 3826498Sminshall (void) strcat(line, " "); 3926052Sminshall printf("(macro name) "); 4026498Sminshall (void) gets(&line[strlen(line)]); 4126052Sminshall makeargv(); 4226052Sminshall argc = margc; 4326052Sminshall argv = margv; 4426052Sminshall } 4526052Sminshall if (argc < 2) { 4626052Sminshall printf("Usage: %s macro_name.\n", argv[0]); 4726052Sminshall code = -1; 4826052Sminshall return; 4926052Sminshall } 5026052Sminshall for (i = 0; i < macnum; ++i) { 5126052Sminshall if (!strncmp(argv[1], macros[i].mac_name, 9)) { 5226052Sminshall break; 5326052Sminshall } 5426052Sminshall } 5526052Sminshall if (i == macnum) { 5626052Sminshall printf("'%s' macro not found.\n", argv[1]); 5726052Sminshall code = -1; 5826052Sminshall return; 5926052Sminshall } 6026498Sminshall (void) strcpy(line2, line); 6126052Sminshall TOP: 6226052Sminshall cp1 = macros[i].mac_start; 6326052Sminshall while (cp1 != macros[i].mac_end) { 6426052Sminshall while (isspace(*cp1)) { 6526052Sminshall cp1++; 6626052Sminshall } 6726052Sminshall cp2 = line; 6826052Sminshall while (*cp1 != '\0') { 6926052Sminshall switch(*cp1) { 7026052Sminshall case '\\': 7126052Sminshall *cp2++ = *++cp1; 7226052Sminshall break; 7326052Sminshall case '$': 7426052Sminshall if (isdigit(*(cp1+1))) { 7526052Sminshall j = 0; 7626052Sminshall while (isdigit(*++cp1)) { 7726052Sminshall j = 10*j + *cp1 - '0'; 7826052Sminshall } 7926052Sminshall cp1--; 8026052Sminshall if (argc - 2 >= j) { 8126498Sminshall (void) strcpy(cp2, argv[j+1]); 8226052Sminshall cp2 += strlen(argv[j+1]); 8326052Sminshall } 8426052Sminshall break; 8526052Sminshall } 8626052Sminshall if (*(cp1+1) == 'i') { 8726052Sminshall loopflg = 1; 8826052Sminshall cp1++; 8926052Sminshall if (count < argc) { 9026498Sminshall (void) strcpy(cp2, argv[count]); 9126052Sminshall cp2 += strlen(argv[count]); 9226052Sminshall } 9326052Sminshall break; 9426052Sminshall } 9526052Sminshall /* intentional drop through */ 9626052Sminshall default: 9726052Sminshall *cp2++ = *cp1; 9826052Sminshall break; 9926052Sminshall } 10026052Sminshall if (*cp1 != '\0') { 10126052Sminshall cp1++; 10226052Sminshall } 10326052Sminshall } 10426052Sminshall *cp2 = '\0'; 10526052Sminshall makeargv(); 10626052Sminshall c = getcmd(margv[0]); 10726052Sminshall if (c == (struct cmd *)-1) { 10826052Sminshall printf("?Ambiguous command\n"); 10926052Sminshall code = -1; 11026052Sminshall } 11126052Sminshall else if (c == 0) { 11226052Sminshall printf("?Invalid command\n"); 11326052Sminshall code = -1; 11426052Sminshall } 11526052Sminshall else if (c->c_conn && !connected) { 11626052Sminshall printf("Not connected.\n"); 11726052Sminshall code = -1; 11826052Sminshall } 11926052Sminshall else { 12026052Sminshall if (verbose) { 12126052Sminshall printf("%s\n",line); 12226052Sminshall } 12326052Sminshall (*c->c_handler)(margc, margv); 12426052Sminshall if (bell && c->c_bell) { 12533117Sbostic (void) putchar(CTRL('g')); 12626052Sminshall } 12726498Sminshall (void) strcpy(line, line2); 12826052Sminshall makeargv(); 12926052Sminshall argc = margc; 13026052Sminshall argv = margv; 13126052Sminshall } 13226052Sminshall if (cp1 != macros[i].mac_end) { 13326052Sminshall cp1++; 13426052Sminshall } 13526052Sminshall } 13626052Sminshall if (loopflg && ++count < argc) { 13726052Sminshall goto TOP; 13826052Sminshall } 13926052Sminshall } 140