[BACK]Return to edit.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mail

Annotation of src/usr.bin/mail/edit.c, Revision 1.2

1.2     ! deraadt     1: /*     $OpenBSD: edit.c,v 1.5 1996/06/08 19:48:20 christos Exp $       */
        !             2: /*     $NetBSD: edit.c,v 1.5 1996/06/08 19:48:20 christos Exp $        */
        !             3:
1.1       deraadt     4: /*
                      5:  * Copyright (c) 1980, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      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.
                     35:  */
                     36:
                     37: #ifndef lint
1.2     ! deraadt    38: #if 0
        !            39: static char sccsid[] = "@(#)edit.c     8.1 (Berkeley) 6/6/93";
        !            40: #else
        !            41: static char rcsid[] = "$OpenBSD: edit.c,v 1.5 1996/06/08 19:48:20 christos Exp $";
        !            42: #endif
1.1       deraadt    43: #endif /* not lint */
                     44:
                     45: #include "rcv.h"
                     46: #include <fcntl.h>
                     47: #include "extern.h"
                     48:
                     49: /*
                     50:  * Mail -- a mail program
                     51:  *
                     52:  * Perform message editing functions.
                     53:  */
                     54:
                     55: /*
                     56:  * Edit a message list.
                     57:  */
                     58: int
1.2     ! deraadt    59: editor(v)
        !            60:        void *v;
1.1       deraadt    61: {
1.2     ! deraadt    62:        int *msgvec = v;
1.1       deraadt    63:
                     64:        return edit1(msgvec, 'e');
                     65: }
                     66:
                     67: /*
                     68:  * Invoke the visual editor on a message list.
                     69:  */
                     70: int
1.2     ! deraadt    71: visual(v)
        !            72:        void *v;
1.1       deraadt    73: {
1.2     ! deraadt    74:        int *msgvec = v;
1.1       deraadt    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:        register int c;
                     90:        int i;
                     91:        FILE *fp;
                     92:        register 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:        register FILE *fp;
                    152:        off_t size;
                    153:        int type, readonly;
                    154: {
                    155:        register FILE *nf = NULL;
                    156:        register int t;
                    157:        time_t modtime;
                    158:        char *edit;
                    159:        struct stat statb;
                    160:        extern char *tempEdit;
                    161:
                    162:        if ((t = creat(tempEdit, readonly ? 0400 : 0600)) < 0) {
                    163:                perror(tempEdit);
                    164:                goto out;
                    165:        }
                    166:        if ((nf = Fdopen(t, "w")) == NULL) {
                    167:                perror(tempEdit);
                    168:                (void) unlink(tempEdit);
                    169:                goto out;
                    170:        }
                    171:        if (size >= 0)
                    172:                while (--size >= 0 && (t = getc(fp)) != EOF)
                    173:                        (void) putc(t, nf);
                    174:        else
                    175:                while ((t = getc(fp)) != EOF)
                    176:                        (void) putc(t, nf);
                    177:        (void) fflush(nf);
                    178:        if (fstat(fileno(nf), &statb) < 0)
                    179:                modtime = 0;
                    180:        else
                    181:                modtime = statb.st_mtime;
                    182:        if (ferror(nf)) {
                    183:                (void) Fclose(nf);
                    184:                perror(tempEdit);
                    185:                (void) unlink(tempEdit);
                    186:                nf = NULL;
                    187:                goto out;
                    188:        }
                    189:        if (Fclose(nf) < 0) {
                    190:                perror(tempEdit);
                    191:                (void) unlink(tempEdit);
                    192:                nf = NULL;
                    193:                goto out;
                    194:        }
                    195:        nf = NULL;
                    196:        if ((edit = value(type == 'e' ? "EDITOR" : "VISUAL")) == NOSTR)
                    197:                edit = type == 'e' ? _PATH_EX : _PATH_VI;
                    198:        if (run_command(edit, 0, -1, -1, tempEdit, NOSTR, NOSTR) < 0) {
                    199:                (void) unlink(tempEdit);
                    200:                goto out;
                    201:        }
                    202:        /*
                    203:         * If in read only mode or file unchanged, just remove the editor
                    204:         * temporary and return.
                    205:         */
                    206:        if (readonly) {
                    207:                (void) unlink(tempEdit);
                    208:                goto out;
                    209:        }
                    210:        if (stat(tempEdit, &statb) < 0) {
                    211:                perror(tempEdit);
                    212:                goto out;
                    213:        }
                    214:        if (modtime == statb.st_mtime) {
                    215:                (void) unlink(tempEdit);
                    216:                goto out;
                    217:        }
                    218:        /*
                    219:         * Now switch to new file.
                    220:         */
                    221:        if ((nf = Fopen(tempEdit, "a+")) == NULL) {
                    222:                perror(tempEdit);
                    223:                (void) unlink(tempEdit);
                    224:                goto out;
                    225:        }
                    226:        (void) unlink(tempEdit);
                    227: out:
                    228:        return nf;
                    229: }