1 | /* |
---|
2 | * |
---|
3 | * Copyright (C) 1989 by the Massachusetts Institute of Technology |
---|
4 | * Developed by the MIT Student Information Processing Board (SIPB). |
---|
5 | * For copying information, see the file mit-copyright.h in this release. |
---|
6 | * |
---|
7 | */ |
---|
8 | /* |
---|
9 | * $Id: edit.c,v 1.16 1999-02-02 20:39:46 kcr Exp $ |
---|
10 | * |
---|
11 | * Utility routines. |
---|
12 | * |
---|
13 | * |
---|
14 | */ |
---|
15 | |
---|
16 | #ifndef lint |
---|
17 | static char rcsid_discuss_utils_c[] = |
---|
18 | "$Id: edit.c,v 1.16 1999-02-02 20:39:46 kcr Exp $"; |
---|
19 | #endif /* lint */ |
---|
20 | |
---|
21 | #include <stdio.h> |
---|
22 | #include <sys/file.h> |
---|
23 | #include <string.h> |
---|
24 | #include <signal.h> |
---|
25 | #include <ss/ss.h> |
---|
26 | #include <discuss/discuss.h> |
---|
27 | #include "globals.h" |
---|
28 | #include <sys/types.h> |
---|
29 | #if HAVE_SYS_WAIT_H |
---|
30 | #include <sys/wait.h> |
---|
31 | #endif |
---|
32 | #ifndef WEXITSTATUS |
---|
33 | #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) |
---|
34 | #endif |
---|
35 | #include <sys/stat.h> |
---|
36 | #include <sys/errno.h> |
---|
37 | |
---|
38 | bool use_editor = TRUE; |
---|
39 | char *editor_path = NULL; |
---|
40 | |
---|
41 | extern char *getenv(); |
---|
42 | |
---|
43 | /* |
---|
44 | * int edit(fn, editor) |
---|
45 | * |
---|
46 | * fn: pathname of file to edit |
---|
47 | * |
---|
48 | * editor: |
---|
49 | * name of editor command to run; if NULL, use the default |
---|
50 | * (specified on the command line or $EDITOR); if "", use a simple |
---|
51 | * type-in prompter. |
---|
52 | * |
---|
53 | * return value: error_code if error occurs, or child exits with nonzero |
---|
54 | * status, 0 otherwise |
---|
55 | * |
---|
56 | * call up an editor (from environment variable EDITOR or default |
---|
57 | * value DEFAULT_EDITOR) on the specified file. |
---|
58 | */ |
---|
59 | |
---|
60 | int |
---|
61 | edit(fn, edit_path) |
---|
62 | char *fn; |
---|
63 | char *edit_path; |
---|
64 | { |
---|
65 | char *editor_path_e, *editor_path_2 = NULL; |
---|
66 | char *editor_path_v; |
---|
67 | int pid; |
---|
68 | int (*handler)(); |
---|
69 | int wbuf; |
---|
70 | |
---|
71 | struct stat buf; |
---|
72 | char buffer[BUFSIZ]; |
---|
73 | FILE *the_file = NULL; |
---|
74 | #if HAVE_SIGACTION |
---|
75 | struct sigaction act, oact; |
---|
76 | sigemptyset(&act.sa_mask); |
---|
77 | act.sa_flags = 0; |
---|
78 | #endif |
---|
79 | |
---|
80 | editor_path_e = getenv("EDITOR"); |
---|
81 | if (!editor_path_e) editor_path_e = "/bin/ed"; |
---|
82 | editor_path_v = getenv("VISUAL"); |
---|
83 | if (!editor_path_v) editor_path_v = "/usr/ucb/vi"; |
---|
84 | |
---|
85 | if (use_editor && editor_path && !edit_path) |
---|
86 | editor_path_2 = editor_path; |
---|
87 | else if (edit_path && (*edit_path != '\0')) |
---|
88 | editor_path_2 = edit_path; |
---|
89 | else { |
---|
90 | the_file = fopen(fn, "w"); |
---|
91 | if (!the_file) { |
---|
92 | perror(fn); |
---|
93 | printf("Error opening file: %d\n",errno); |
---|
94 | return(errno); |
---|
95 | } |
---|
96 | |
---|
97 | ftruncate(fileno(the_file), 0); |
---|
98 | fchmod(fileno(the_file), 0700); |
---|
99 | printf("Enter transaction; end with ^D or '.' on a line by itself.\n"); |
---|
100 | for (;;) { |
---|
101 | if ((gets(buffer) == NULL) || interrupt || !strcmp(buffer, ".")) break; |
---|
102 | else if (!strcmp(buffer,"\\f")) { |
---|
103 | editor_path_2 = editor_path_e; |
---|
104 | break; |
---|
105 | } else if (!strcmp(buffer,"~e")) { |
---|
106 | editor_path_2 = editor_path_e; |
---|
107 | break; |
---|
108 | } else if (!strcmp(buffer,"~v")) { |
---|
109 | editor_path_2 = editor_path_v; |
---|
110 | break; |
---|
111 | } else { |
---|
112 | fputs(buffer,the_file); |
---|
113 | fputc('\n',the_file); |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | if (editor_path_2) { |
---|
119 | if (the_file) { |
---|
120 | clearerr(stdin); |
---|
121 | fclose(the_file); |
---|
122 | } |
---|
123 | switch ((pid = fork())) { |
---|
124 | case -1: |
---|
125 | perror("couldn't fork"); |
---|
126 | printf("Couldn't fork, error %d\n",errno); |
---|
127 | return(errno); |
---|
128 | case 0: |
---|
129 | (void) execlp(editor_path_2, editor_path_2, fn, 0); |
---|
130 | (void) perror(editor_path_2); |
---|
131 | exit(1); |
---|
132 | default: |
---|
133 | break; |
---|
134 | } |
---|
135 | #if HAVE_SIGACTION |
---|
136 | act.sa_handler= (void (*)()) SIG_IGN; |
---|
137 | (void) sigaction(SIGINT, &act, &oact); |
---|
138 | #else |
---|
139 | handler = signal(SIGINT, SIG_IGN); |
---|
140 | #endif |
---|
141 | while (wait(&wbuf) != pid) |
---|
142 | ; |
---|
143 | #if HAVE_SIGACTION |
---|
144 | (void) sigaction(SIGINT, &oact, NULL); |
---|
145 | #else |
---|
146 | (void) signal(SIGINT, handler); |
---|
147 | #endif |
---|
148 | if (WIFSIGNALED(wbuf)) |
---|
149 | return(ET_CHILD_DIED); |
---|
150 | if (WEXITSTATUS(wbuf)) |
---|
151 | return(ET_CHILD_ERR); |
---|
152 | } else { |
---|
153 | clearerr(stdin); |
---|
154 | fclose(the_file); |
---|
155 | } |
---|
156 | |
---|
157 | if (stat (fn, &buf) != 0 || buf.st_size == 0) { |
---|
158 | unlink(fn); |
---|
159 | } |
---|
160 | return(0); |
---|
161 | } |
---|
162 | |
---|
163 | touch(fn) |
---|
164 | char *fn; |
---|
165 | { |
---|
166 | int fd; |
---|
167 | if ((fd=creat(fn, 0600)) < 0) |
---|
168 | return(errno); |
---|
169 | else close(fd); |
---|
170 | return(0); |
---|
171 | } |
---|