update from Rodney Ruddock
[unix-history] / usr / src / contrib / ed / bang.c
CommitLineData
ccd1d075
KB
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
672cc1c0 12static char sccsid[] = "@(#)bang.c 5.4 (Berkeley) %G%";
ccd1d075
KB
13#endif /* not lint */
14
ecbf4ad0
KB
15#include <sys/types.h>
16
e692f66f 17#include <limits.h>
ecbf4ad0
KB
18#include <regex.h>
19#include <setjmp.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
e692f66f
KB
24#ifdef DBI
25#include <db.h>
26#endif
27
ccd1d075 28#include "ed.h"
ecbf4ad0 29#include "extern.h"
ccd1d075
KB
30
31/*
32 * Execute a command in sh (and always sh). For those wondering the
33 * proper name for '!' _is_ bang.
34 */
35
36void
37bang(inputt, errnum)
ecbf4ad0
KB
38 FILE *inputt;
39 int *errnum;
ccd1d075 40{
ecbf4ad0
KB
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;
ccd1d075 44
ecbf4ad0 45 for (;;) {
ecbf4ad0
KB
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';
e692f66f
KB
67 if (filename_current) {
68 strcat(l_shellcmd,
69 filename_current);
70 l_cnt =
71 l_cnt +
72 strlen(filename_current);
73 }
ecbf4ad0
KB
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 }
ccd1d075 83
ecbf4ad0 84 system(l_shellcmd);
672cc1c0 85 if (explain_flag > 0) /* for the -s option */
ecbf4ad0
KB
86 printf("!\n");
87 l_cnt_last_pos = l_cnt;
88 *errnum = 0;
89}