1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
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 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 
19 
20 #include "precomp.h"
21 
22 #ifndef lint
23 static char sccsid[] = "@(#)domacro.c	1.6 (Berkeley) 2/28/89";
24 #endif /* not lint */
25 
26 void domacro(int argc, const char *argv[])
27 {
28 	int i, j;
29 	const char *cp1;
30     char *cp2;
31 	int count = 2, loopflg = 0;
32 	char line2[200];
33 	struct cmd *getcmd(), *c;
34 
35 	if (argc < 2) {
36 		(void) strcat(line, " ");
37 		printf("(macro name) ");
38 		(void) fflush(stdout);
39 		(void) gets(&line[strlen(line)]);
40 		makeargv();
41 		argc = margc;
42 		argv = margv;
43 	}
44 	if (argc < 2) {
45 		printf("Usage: %s macro_name.\n", argv[0]);
46 		(void) fflush(stdout);
47 		code = -1;
48 		return;
49 	}
50 	for (i = 0; i < macnum; ++i) {
51 		if (!strncmp(argv[1], macros[i].mac_name, 9)) {
52 			break;
53 		}
54 	}
55 	if (i == macnum) {
56 		printf("'%s' macro not found.\n", argv[1]);
57 		(void) fflush(stdout);
58 		code = -1;
59 		return;
60 	}
61 	(void) strcpy(line2, line);
62 TOP:
63 	cp1 = macros[i].mac_start;
64 	while (cp1 != macros[i].mac_end) {
65 		while (isspace(*cp1)) {
66 			cp1++;
67 		}
68 		cp2 = line;
69 		while (*cp1 != '\0') {
70 		      switch(*cp1) {
71 		   	    case '\\':
72 				 *cp2++ = *++cp1;
73 				 break;
74 			    case '$':
75 				 if (isdigit(*(cp1+1))) {
76 				    j = 0;
77 				    while (isdigit(*++cp1)) {
78 					  j = 10*j +  *cp1 - '0';
79 				    }
80 				    cp1--;
81 				    if (argc - 2 >= j) {
82 					(void) strcpy(cp2, argv[j+1]);
83 					cp2 += strlen(argv[j+1]);
84 				    }
85 				    break;
86 				 }
87 				 if (*(cp1+1) == 'i') {
88 					loopflg = 1;
89 					cp1++;
90 					if (count < argc) {
91 					   (void) strcpy(cp2, argv[count]);
92 					   cp2 += strlen(argv[count]);
93 					}
94 					break;
95 				}
96 				/* intentional drop through */
97 			    default:
98 				*cp2++ = *cp1;
99 				break;
100 		      }
101 		      if (*cp1 != '\0') {
102 			 cp1++;
103 		      }
104 		}
105 		*cp2 = '\0';
106 		makeargv();
107 		c = getcmd(margv[0]);
108 		if (c == (struct cmd *)-1) {
109 			printf("?Ambiguous command\n");
110 			code = -1;
111 		}
112 		else if (c == 0) {
113 			printf("?Invalid command\n");
114 			code = -1;
115 		}
116 		else if (c->c_conn && !connected) {
117 			printf("Not connected.\n");
118 			code = -1;
119 		}
120 		else {
121 			if (verbose) {
122 				printf("%s\n",line);
123 			}
124 			(*c->c_handler)(margc, margv);
125 			if (bell && c->c_bell) {
126 				(void) putchar('\007');
127 			}
128 			(void) strcpy(line, line2);
129 			makeargv();
130 			argc = margc;
131 			argv = margv;
132 		}
133 		if (cp1 != macros[i].mac_end) {
134 			cp1++;
135 		}
136 		(void) fflush(stdout);
137 	}
138 	if (loopflg && ++count < argc) {
139 		goto TOP;
140 	}
141 }
142