Commit | Line | Data |
---|---|---|
8ebd1ebf GM |
1 | /* |
2 | * Copyright (c) 1985 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
7 | #ifndef lint | |
8 | static char sccsid[] = "@(#)domacro.c 1.1 (Berkeley) %G%"; | |
9 | #endif not lint | |
10 | ||
11 | #include "ftp_var.h" | |
12 | ||
13 | #include <signal.h> | |
14 | #include <stdio.h> | |
15 | #include <errno.h> | |
16 | #include <ctype.h> | |
17 | #include <sys/ttychars.h> | |
18 | ||
19 | jmp_buf macabort; | |
20 | ||
21 | domacro(argc, argv) | |
22 | int argc; | |
23 | char *argv[]; | |
24 | { | |
25 | register int i, j; | |
26 | register char *cp1, *cp2; | |
27 | int count = 2, loopflg = 0, (*oldintr)(); | |
28 | char **gargs, **cpp, line2[200]; | |
29 | extern char **glob(), *globerr; | |
30 | struct cmd *getcmd(), *c; | |
31 | extern struct cmd cmdtab[]; | |
32 | ||
33 | if (argc < 2) { | |
34 | strcat(line, " "); | |
35 | printf("(macro name) "); | |
36 | gets(&line[strlen(line)]); | |
37 | makeargv(); | |
38 | argc = margc; | |
39 | argv = margv; | |
40 | } | |
41 | if (argc < 2) { | |
42 | printf("Usage: %s macro_name.\n", argv[0]); | |
43 | code = -1; | |
44 | return; | |
45 | } | |
46 | for (i = 0; i < macnum; ++i) { | |
47 | if (!strncmp(argv[1], macros[i].mac_name, 9)) { | |
48 | break; | |
49 | } | |
50 | } | |
51 | if (i == macnum) { | |
52 | printf("'%s' macro not found.\n", argv[1]); | |
53 | code = -1; | |
54 | return; | |
55 | } | |
56 | strcpy(line2, line); | |
57 | TOP: | |
58 | cp1 = macros[i].mac_start; | |
59 | while (cp1 != macros[i].mac_end) { | |
60 | while (isspace(*cp1)) { | |
61 | cp1++; | |
62 | } | |
63 | cp2 = line; | |
64 | while (*cp1 != '\0') { | |
65 | switch(*cp1) { | |
66 | case '\\': | |
67 | *cp2++ = *++cp1; | |
68 | break; | |
69 | case '$': | |
70 | if (isdigit(*(cp1+1))) { | |
71 | j = 0; | |
72 | while (isdigit(*++cp1)) { | |
73 | j = 10*j + *cp1 - '0'; | |
74 | } | |
75 | cp1--; | |
76 | if (argc - 2 >= j) { | |
77 | strcpy(cp2, argv[j+1]); | |
78 | cp2 += strlen(argv[j+1]); | |
79 | } | |
80 | break; | |
81 | } | |
82 | if (*(cp1+1) == 'i') { | |
83 | loopflg = 1; | |
84 | cp1++; | |
85 | if (count < argc) { | |
86 | strcpy(cp2, argv[count]); | |
87 | cp2 += strlen(argv[count]); | |
88 | } | |
89 | break; | |
90 | } | |
91 | /* intentional drop through */ | |
92 | default: | |
93 | *cp2++ = *cp1; | |
94 | break; | |
95 | } | |
96 | if (*cp1 != '\0') { | |
97 | cp1++; | |
98 | } | |
99 | } | |
100 | *cp2 = '\0'; | |
101 | makeargv(); | |
102 | c = getcmd(margv[0]); | |
103 | if (c == (struct cmd *)-1) { | |
104 | printf("?Ambiguous command\n"); | |
105 | code = -1; | |
106 | } | |
107 | else if (c == 0) { | |
108 | printf("?Invalid command\n"); | |
109 | code = -1; | |
110 | } | |
111 | else if (c->c_conn && !connected) { | |
112 | printf("Not connected.\n"); | |
113 | code = -1; | |
114 | } | |
115 | else { | |
116 | if (verbose) { | |
117 | printf("%s\n",line); | |
118 | } | |
119 | (*c->c_handler)(margc, margv); | |
120 | if (bell && c->c_bell) { | |
121 | putchar(CTRL(g)); | |
122 | } | |
123 | strcpy(line, line2); | |
124 | makeargv(); | |
125 | argc = margc; | |
126 | argv = margv; | |
127 | } | |
128 | if (cp1 != macros[i].mac_end) { | |
129 | cp1++; | |
130 | } | |
131 | } | |
132 | if (loopflg && ++count < argc) { | |
133 | goto TOP; | |
134 | } | |
135 | } |