1 /* $NetBSD: edit.c,v 1.8 2001/02/05 02:07:53 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)edit.c 8.1 (Berkeley) 6/6/93"; 40 #else 41 __RCSID("$NetBSD: edit.c,v 1.8 2001/02/05 02:07:53 christos Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #include "rcv.h" 46 #include "extern.h" 47 48 /* 49 * Mail -- a mail program 50 * 51 * Perform message editing functions. 52 */ 53 extern char *tempEdit; 54 55 /* 56 * Edit a message list. 57 */ 58 int 59 editor(v) 60 void *v; 61 { 62 int *msgvec = v; 63 64 return edit1(msgvec, 'e'); 65 } 66 67 /* 68 * Invoke the visual editor on a message list. 69 */ 70 int 71 visual(v) 72 void *v; 73 { 74 int *msgvec = v; 75 76 return edit1(msgvec, 'v'); 77 } 78 79 /* 80 * Edit a message by writing the message into a funnily-named file 81 * (which should not exist) and forking an editor on it. 82 * We get the editor from the stuff above. 83 */ 84 int 85 edit1(msgvec, type) 86 int *msgvec; 87 int type; 88 { 89 int c; 90 int i; 91 FILE *fp; 92 struct message *mp; 93 off_t size; 94 95 /* 96 * Deal with each message to be edited . . . 97 */ 98 for (i = 0; msgvec[i] && i < msgCount; i++) { 99 sig_t sigint; 100 101 if (i > 0) { 102 char buf[100]; 103 char *p; 104 105 printf("Edit message %d [ynq]? ", msgvec[i]); 106 if (fgets(buf, sizeof buf, stdin) == 0) 107 break; 108 for (p = buf; *p == ' ' || *p == '\t'; p++) 109 ; 110 if (*p == 'q') 111 break; 112 if (*p == 'n') 113 continue; 114 } 115 dot = mp = &message[msgvec[i] - 1]; 116 touch(mp); 117 sigint = signal(SIGINT, SIG_IGN); 118 fp = run_editor(setinput(mp), mp->m_size, type, readonly); 119 if (fp != NULL) { 120 (void) fseek(otf, 0L, 2); 121 size = ftell(otf); 122 mp->m_block = blockof(size); 123 mp->m_offset = offsetof(size); 124 mp->m_size = fsize(fp); 125 mp->m_lines = 0; 126 mp->m_flag |= MODIFY; 127 rewind(fp); 128 while ((c = getc(fp)) != EOF) { 129 if (c == '\n') 130 mp->m_lines++; 131 if (putc(c, otf) == EOF) 132 break; 133 } 134 if (ferror(otf)) 135 perror("/tmp"); 136 (void) Fclose(fp); 137 } 138 (void) signal(SIGINT, sigint); 139 } 140 return 0; 141 } 142 143 /* 144 * Run an editor on the file at "fpp" of "size" bytes, 145 * and return a new file pointer. 146 * Signals must be handled by the caller. 147 * "Type" is 'e' for _PATH_EX, 'v' for _PATH_VI. 148 */ 149 FILE * 150 run_editor(fp, size, type, readonly) 151 FILE *fp; 152 off_t size; 153 int type, readonly; 154 { 155 FILE *nf = NULL; 156 int t; 157 time_t modtime; 158 char *edit; 159 struct stat statb; 160 161 if ((t = creat(tempEdit, readonly ? 0400 : 0600)) < 0) { 162 perror(tempEdit); 163 goto out; 164 } 165 if ((nf = Fdopen(t, "w")) == NULL) { 166 perror(tempEdit); 167 (void) unlink(tempEdit); 168 goto out; 169 } 170 if (size >= 0) 171 while (--size >= 0 && (t = getc(fp)) != EOF) 172 (void) putc(t, nf); 173 else 174 while ((t = getc(fp)) != EOF) 175 (void) putc(t, nf); 176 (void) fflush(nf); 177 if (ferror(nf)) { 178 (void) Fclose(nf); 179 perror(tempEdit); 180 (void) unlink(tempEdit); 181 nf = NULL; 182 goto out; 183 } 184 if (fstat(fileno(nf), &statb) < 0) 185 modtime = 0; 186 else 187 modtime = statb.st_mtime; 188 if (Fclose(nf) < 0) { 189 perror(tempEdit); 190 (void) unlink(tempEdit); 191 nf = NULL; 192 goto out; 193 } 194 nf = NULL; 195 if ((edit = value(type == 'e' ? "EDITOR" : "VISUAL")) == NOSTR) 196 edit = type == 'e' ? _PATH_EX : _PATH_VI; 197 if (run_command(edit, 0, -1, -1, tempEdit, NOSTR, NOSTR) < 0) { 198 (void) unlink(tempEdit); 199 goto out; 200 } 201 /* 202 * If in read only mode or file unchanged, just remove the editor 203 * temporary and return. 204 */ 205 if (readonly) { 206 (void) unlink(tempEdit); 207 goto out; 208 } 209 if (stat(tempEdit, &statb) < 0) { 210 perror(tempEdit); 211 goto out; 212 } 213 if (modtime == statb.st_mtime) { 214 (void) unlink(tempEdit); 215 goto out; 216 } 217 /* 218 * Now switch to new file. 219 */ 220 if ((nf = Fopen(tempEdit, "a+")) == NULL) { 221 perror(tempEdit); 222 (void) unlink(tempEdit); 223 goto out; 224 } 225 (void) unlink(tempEdit); 226 out: 227 return nf; 228 } 229