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