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