Home | History | Annotate | Line # | Download | only in ftp
domacro.c revision 1.4.2.2
      1 /*
      2  * Copyright (c) 1985, 1993, 1994
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 /*static char sccsid[] = "from: @(#)domacro.c	8.3 (Berkeley) 4/2/94";*/
     36 static char *rcsid = "$Id: domacro.c,v 1.4.2.2 1994/08/29 03:09:10 mycroft Exp $";
     37 #endif /* not lint */
     38 
     39 #include <ctype.h>
     40 #include <signal.h>
     41 #include <stdio.h>
     42 #include <strings.h>
     43 
     44 #include "ftp_var.h"
     45 
     46 void
     47 domacro(argc, argv)
     48 	int argc;
     49 	char *argv[];
     50 {
     51 	int i, j, count = 2, loopflg = 0;
     52 	char *cp1, *cp2, line2[200];
     53 	struct cmd *c;
     54 
     55 	if (argc < 2 && !another(&argc, &argv, "macro name")) {
     56 		printf("Usage: %s macro_name.\n", argv[0]);
     57 		code = -1;
     58 		return;
     59 	}
     60 	for (i = 0; i < macnum; ++i) {
     61 		if (!strncmp(argv[1], macros[i].mac_name, 9)) {
     62 			break;
     63 		}
     64 	}
     65 	if (i == macnum) {
     66 		printf("'%s' macro not found.\n", argv[1]);
     67 		code = -1;
     68 		return;
     69 	}
     70 	(void) strcpy(line2, line);
     71 TOP:
     72 	cp1 = macros[i].mac_start;
     73 	while (cp1 != macros[i].mac_end) {
     74 		while (isspace(*cp1)) {
     75 			cp1++;
     76 		}
     77 		cp2 = line;
     78 		while (*cp1 != '\0') {
     79 		      switch(*cp1) {
     80 		   	    case '\\':
     81 				 *cp2++ = *++cp1;
     82 				 break;
     83 			    case '$':
     84 				 if (isdigit(*(cp1+1))) {
     85 				    j = 0;
     86 				    while (isdigit(*++cp1)) {
     87 					  j = 10*j +  *cp1 - '0';
     88 				    }
     89 				    cp1--;
     90 				    if (argc - 2 >= j) {
     91 					(void) strcpy(cp2, argv[j+1]);
     92 					cp2 += strlen(argv[j+1]);
     93 				    }
     94 				    break;
     95 				 }
     96 				 if (*(cp1+1) == 'i') {
     97 					loopflg = 1;
     98 					cp1++;
     99 					if (count < argc) {
    100 					   (void) strcpy(cp2, argv[count]);
    101 					   cp2 += strlen(argv[count]);
    102 					}
    103 					break;
    104 				}
    105 				/* intentional drop through */
    106 			    default:
    107 				*cp2++ = *cp1;
    108 				break;
    109 		      }
    110 		      if (*cp1 != '\0') {
    111 			 cp1++;
    112 		      }
    113 		}
    114 		*cp2 = '\0';
    115 		makeargv();
    116 		c = getcmd(margv[0]);
    117 		if (c == (struct cmd *)-1) {
    118 			printf("?Ambiguous command\n");
    119 			code = -1;
    120 		}
    121 		else if (c == 0) {
    122 			printf("?Invalid command\n");
    123 			code = -1;
    124 		}
    125 		else if (c->c_conn && !connected) {
    126 			printf("Not connected.\n");
    127 			code = -1;
    128 		}
    129 		else {
    130 			if (verbose) {
    131 				printf("%s\n",line);
    132 			}
    133 			(*c->c_handler)(margc, margv);
    134 			if (bell && c->c_bell) {
    135 				(void) putchar('\007');
    136 			}
    137 			(void) strcpy(line, line2);
    138 			makeargv();
    139 			argc = margc;
    140 			argv = margv;
    141 		}
    142 		if (cp1 != macros[i].mac_end) {
    143 			cp1++;
    144 		}
    145 	}
    146 	if (loopflg && ++count < argc) {
    147 		goto TOP;
    148 	}
    149 }
    150