BSD 4_4 release
[unix-history] / usr / src / contrib / ed / bang.c
CommitLineData
ccd1d075 1/*-
ba5e8546
KB
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
ccd1d075
KB
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rodney Ruddock of the University of Guelph.
7 *
ad787160
C
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
ccd1d075
KB
35 */
36
37#ifndef lint
ad787160 38static char sccsid[] = "@(#)bang.c 8.1 (Berkeley) 5/31/93";
ccd1d075
KB
39#endif /* not lint */
40
ecbf4ad0
KB
41#include <sys/types.h>
42
e692f66f 43#include <limits.h>
ecbf4ad0
KB
44#include <regex.h>
45#include <setjmp.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49
e692f66f
KB
50#ifdef DBI
51#include <db.h>
52#endif
53
ccd1d075 54#include "ed.h"
ecbf4ad0 55#include "extern.h"
ccd1d075
KB
56
57/*
58 * Execute a command in sh (and always sh). For those wondering the
59 * proper name for '!' _is_ bang.
60 */
61
62void
63bang(inputt, errnum)
ecbf4ad0
KB
64 FILE *inputt;
65 int *errnum;
ccd1d075 66{
71fc9f52 67 static int l_cnt_last_pos=0; /* "!!", l_shellcmd offset */
ecbf4ad0 68 static char l_shellcmd[FILENAME_LEN]; /* "!!" */
71fc9f52
KB
69 static char l_shellcmd2[FILENAME_LEN]; /* "!!" */
70 int l_cnt = 0, l_esc=0, l_flag=0;
71
72 memcpy(l_shellcmd, l_shellcmd2, FILENAME_LEN);
ccd1d075 73
ecbf4ad0 74 for (;;) {
71fc9f52
KB
75 ss = getc(inputt);
76 l_esc = 0;
77 if (ss == '\\') {
78 ss = getc(inputt);
ecbf4ad0 79 l_esc = 1;
71fc9f52
KB
80 }
81 if (((!l_esc) && (ss == '\n')) || (ss == EOF)) {
ecbf4ad0
KB
82 l_shellcmd[l_cnt] = '\0';
83 break;
84 } else
71fc9f52
KB
85 if ((ss == '!') && (l_cnt == 0) && (l_esc == 0)) {
86 if (l_cnt_last_pos == 0) {
87 strcpy(help_msg,
88 "no remembered command");
89 *errnum = -1;
90 return;
91 }
ecbf4ad0 92 l_cnt = l_cnt_last_pos;
71fc9f52 93 l_flag = 1;
afdef93a 94 }
ecbf4ad0
KB
95 else
96 if ((ss == '%') && (l_esc == 0)) {
e692f66f 97 if (filename_current) {
71fc9f52 98 l_shellcmd[l_cnt] = '\0';
e692f66f
KB
99 strcat(l_shellcmd,
100 filename_current);
101 l_cnt =
102 l_cnt +
71fc9f52
KB
103 strlen(filename_current);
104 }
105 else {
106 strcpy(help_msg,
107 "no current filename");
108 *errnum = -1;
109 return;
e692f66f 110 }
ecbf4ad0
KB
111 } else
112 l_shellcmd[l_cnt++] = ss;
113 if (l_cnt >= FILENAME_LEN) {
114 strcpy(help_msg, "shell command too long");
115 *errnum = -1;
ecbf4ad0
KB
116 return;
117 }
118 }
ccd1d075 119
71fc9f52
KB
120 if (l_flag)
121 printf("%s\n", l_shellcmd);
ecbf4ad0 122 system(l_shellcmd);
672cc1c0 123 if (explain_flag > 0) /* for the -s option */
ecbf4ad0
KB
124 printf("!\n");
125 l_cnt_last_pos = l_cnt;
71fc9f52 126 memcpy(l_shellcmd2, l_shellcmd, FILENAME_LEN);
ecbf4ad0
KB
127 *errnum = 0;
128}