Commit | Line | Data |
---|---|---|
8ebd1ebf GM |
1 | /* |
2 | * Copyright (c) 1985 Regents of the University of California. | |
11c5f0a3 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
b36fc510 KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
8ebd1ebf GM |
16 | */ |
17 | ||
18 | #ifndef lint | |
b36fc510 | 19 | static char sccsid[] = "@(#)domacro.c 1.5 (Berkeley) %G%"; |
11c5f0a3 | 20 | #endif /* not lint */ |
8ebd1ebf GM |
21 | |
22 | #include "ftp_var.h" | |
23 | ||
24 | #include <signal.h> | |
25 | #include <stdio.h> | |
26 | #include <errno.h> | |
27 | #include <ctype.h> | |
28 | #include <sys/ttychars.h> | |
29 | ||
8ebd1ebf GM |
30 | domacro(argc, argv) |
31 | int argc; | |
32 | char *argv[]; | |
33 | { | |
34 | register int i, j; | |
35 | register char *cp1, *cp2; | |
04480325 GM |
36 | int count = 2, loopflg = 0; |
37 | char line2[200]; | |
8ebd1ebf GM |
38 | extern char **glob(), *globerr; |
39 | struct cmd *getcmd(), *c; | |
40 | extern struct cmd cmdtab[]; | |
41 | ||
42 | if (argc < 2) { | |
04480325 | 43 | (void) strcat(line, " "); |
8ebd1ebf | 44 | printf("(macro name) "); |
04480325 | 45 | (void) gets(&line[strlen(line)]); |
8ebd1ebf GM |
46 | makeargv(); |
47 | argc = margc; | |
48 | argv = margv; | |
49 | } | |
50 | if (argc < 2) { | |
51 | printf("Usage: %s macro_name.\n", argv[0]); | |
52 | code = -1; | |
53 | return; | |
54 | } | |
55 | for (i = 0; i < macnum; ++i) { | |
56 | if (!strncmp(argv[1], macros[i].mac_name, 9)) { | |
57 | break; | |
58 | } | |
59 | } | |
60 | if (i == macnum) { | |
61 | printf("'%s' macro not found.\n", argv[1]); | |
62 | code = -1; | |
63 | return; | |
64 | } | |
04480325 | 65 | (void) strcpy(line2, line); |
8ebd1ebf GM |
66 | TOP: |
67 | cp1 = macros[i].mac_start; | |
68 | while (cp1 != macros[i].mac_end) { | |
69 | while (isspace(*cp1)) { | |
70 | cp1++; | |
71 | } | |
72 | cp2 = line; | |
73 | while (*cp1 != '\0') { | |
74 | switch(*cp1) { | |
75 | case '\\': | |
76 | *cp2++ = *++cp1; | |
77 | break; | |
78 | case '$': | |
79 | if (isdigit(*(cp1+1))) { | |
80 | j = 0; | |
81 | while (isdigit(*++cp1)) { | |
82 | j = 10*j + *cp1 - '0'; | |
83 | } | |
84 | cp1--; | |
85 | if (argc - 2 >= j) { | |
04480325 | 86 | (void) strcpy(cp2, argv[j+1]); |
8ebd1ebf GM |
87 | cp2 += strlen(argv[j+1]); |
88 | } | |
89 | break; | |
90 | } | |
91 | if (*(cp1+1) == 'i') { | |
92 | loopflg = 1; | |
93 | cp1++; | |
94 | if (count < argc) { | |
04480325 | 95 | (void) strcpy(cp2, argv[count]); |
8ebd1ebf GM |
96 | cp2 += strlen(argv[count]); |
97 | } | |
98 | break; | |
99 | } | |
100 | /* intentional drop through */ | |
101 | default: | |
102 | *cp2++ = *cp1; | |
103 | break; | |
104 | } | |
105 | if (*cp1 != '\0') { | |
106 | cp1++; | |
107 | } | |
108 | } | |
109 | *cp2 = '\0'; | |
110 | makeargv(); | |
111 | c = getcmd(margv[0]); | |
112 | if (c == (struct cmd *)-1) { | |
113 | printf("?Ambiguous command\n"); | |
114 | code = -1; | |
115 | } | |
116 | else if (c == 0) { | |
117 | printf("?Invalid command\n"); | |
118 | code = -1; | |
119 | } | |
120 | else if (c->c_conn && !connected) { | |
121 | printf("Not connected.\n"); | |
122 | code = -1; | |
123 | } | |
124 | else { | |
125 | if (verbose) { | |
126 | printf("%s\n",line); | |
127 | } | |
128 | (*c->c_handler)(margc, margv); | |
129 | if (bell && c->c_bell) { | |
a1ad60e1 | 130 | (void) putchar(CTRL('g')); |
8ebd1ebf | 131 | } |
04480325 | 132 | (void) strcpy(line, line2); |
8ebd1ebf GM |
133 | makeargv(); |
134 | argc = margc; | |
135 | argv = margv; | |
136 | } | |
137 | if (cp1 != macros[i].mac_end) { | |
138 | cp1++; | |
139 | } | |
140 | } | |
141 | if (loopflg && ++count < argc) { | |
142 | goto TOP; | |
143 | } | |
144 | } |