xref: /original-bsd/contrib/ed/bang.c (revision 4670e840)
1 /*-
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rodney Ruddock of the University of Guelph.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)bang.c	5.3 (Berkeley) 02/28/93";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 
17 #include <limits.h>
18 #include <regex.h>
19 #include <setjmp.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #ifdef DBI
25 #include <db.h>
26 #endif
27 
28 #include "ed.h"
29 #include "extern.h"
30 
31 /*
32  * Execute a command in sh (and always sh). For those wondering the
33  * proper name for '!' _is_ bang.
34  */
35 
36 void
37 bang(inputt, errnum)
38 	FILE *inputt;
39 	int *errnum;
40 {
41 	static int l_cnt_last_pos;		/* "!!", l_shellcmd offset */
42 	static char l_shellcmd[FILENAME_LEN];	/* "!!" */
43 	int l_cnt = 0, l_esc = 0;
44 
45 	for (;;) {
46 		ss = getchar();
47 		if ((ss == '\\') && (l_esc == 0)) {
48 			ss = getchar();
49 			l_esc = 1;
50 		} else
51 			l_esc = 0;
52 		if ((ss == '\n') || (ss == EOF)) {
53 			if (l_cnt == 0) {
54 				strcpy(help_msg, "no shell command given");
55 				*errnum = -1;
56 				ungetc('\n', inputt);
57 				return;
58 			}
59 			l_shellcmd[l_cnt] = '\0';
60 			break;
61 		} else
62 			if ((ss == '!') && (l_esc == 0))
63 				l_cnt = l_cnt_last_pos;
64 			else
65 				if ((ss == '%') && (l_esc == 0)) {
66 					l_shellcmd[l_cnt] = '\0';
67 					if (filename_current) {
68 						strcat(l_shellcmd,
69 						    filename_current);
70 						l_cnt =
71 						    l_cnt +
72 						    strlen(filename_current);
73 					}
74 				} else
75 					l_shellcmd[l_cnt++] = ss;
76 		if (l_cnt >= FILENAME_LEN) {
77 			strcpy(help_msg, "shell command too long");
78 			*errnum = -1;
79 			ungetc('\n', inputt);
80 			return;
81 		}
82 	}
83 
84 	system(l_shellcmd);
85 	if (explain_flag != 0)	/* for the -s option */
86 		printf("!\n");
87 	l_cnt_last_pos = l_cnt;
88 	*errnum = 0;
89 }
90