add 1994 copyright
[unix-history] / usr / src / usr.bin / ftp / domacro.c
CommitLineData
8ebd1ebf 1/*
522da3e4 2 * Copyright (c) 1985, 1993, 1994
acd4f2e1 3 * The Regents of the University of California. All rights reserved.
11c5f0a3 4 *
1343342a 5 * %sccs.include.redist.c%
8ebd1ebf
GM
6 */
7
8#ifndef lint
522da3e4 9static char sccsid[] = "@(#)domacro.c 8.3 (Berkeley) %G%";
11c5f0a3 10#endif /* not lint */
8ebd1ebf 11
26128054 12#include <ctype.h>
8ebd1ebf
GM
13#include <signal.h>
14#include <stdio.h>
26128054
JSP
15#include <strings.h>
16
17#include "ftp_var.h"
8ebd1ebf 18
26128054 19void
8ebd1ebf
GM
20domacro(argc, argv)
21 int argc;
22 char *argv[];
23{
26128054
JSP
24 int i, j, count = 2, loopflg = 0;
25 char *cp1, *cp2, line2[200];
26 struct cmd *c;
8ebd1ebf 27
67ff27c7 28 if (argc < 2 && !another(&argc, &argv, "macro name")) {
8ebd1ebf
GM
29 printf("Usage: %s macro_name.\n", argv[0]);
30 code = -1;
31 return;
32 }
33 for (i = 0; i < macnum; ++i) {
34 if (!strncmp(argv[1], macros[i].mac_name, 9)) {
35 break;
36 }
37 }
38 if (i == macnum) {
39 printf("'%s' macro not found.\n", argv[1]);
40 code = -1;
41 return;
42 }
04480325 43 (void) strcpy(line2, line);
8ebd1ebf
GM
44TOP:
45 cp1 = macros[i].mac_start;
46 while (cp1 != macros[i].mac_end) {
47 while (isspace(*cp1)) {
48 cp1++;
49 }
50 cp2 = line;
51 while (*cp1 != '\0') {
52 switch(*cp1) {
53 case '\\':
54 *cp2++ = *++cp1;
55 break;
56 case '$':
57 if (isdigit(*(cp1+1))) {
58 j = 0;
59 while (isdigit(*++cp1)) {
60 j = 10*j + *cp1 - '0';
61 }
62 cp1--;
63 if (argc - 2 >= j) {
04480325 64 (void) strcpy(cp2, argv[j+1]);
8ebd1ebf
GM
65 cp2 += strlen(argv[j+1]);
66 }
67 break;
68 }
69 if (*(cp1+1) == 'i') {
70 loopflg = 1;
71 cp1++;
72 if (count < argc) {
04480325 73 (void) strcpy(cp2, argv[count]);
8ebd1ebf
GM
74 cp2 += strlen(argv[count]);
75 }
76 break;
77 }
78 /* intentional drop through */
79 default:
80 *cp2++ = *cp1;
81 break;
82 }
83 if (*cp1 != '\0') {
84 cp1++;
85 }
86 }
87 *cp2 = '\0';
88 makeargv();
89 c = getcmd(margv[0]);
90 if (c == (struct cmd *)-1) {
91 printf("?Ambiguous command\n");
92 code = -1;
93 }
94 else if (c == 0) {
95 printf("?Invalid command\n");
96 code = -1;
97 }
98 else if (c->c_conn && !connected) {
99 printf("Not connected.\n");
100 code = -1;
101 }
102 else {
103 if (verbose) {
104 printf("%s\n",line);
105 }
106 (*c->c_handler)(margc, margv);
107 if (bell && c->c_bell) {
ff00793c 108 (void) putchar('\007');
8ebd1ebf 109 }
04480325 110 (void) strcpy(line, line2);
8ebd1ebf
GM
111 makeargv();
112 argc = margc;
113 argv = margv;
114 }
115 if (cp1 != macros[i].mac_end) {
116 cp1++;
117 }
118 }
119 if (loopflg && ++count < argc) {
120 goto TOP;
121 }
122}