]> diplodocus.org Git - nmh/blob - docs/historical/mh-jun-1982/subs/m_edit.c
Create new mh-format function %(ordinal)
[nmh] / docs / historical / mh-jun-1982 / subs / m_edit.c
1 #ifdef COMMENT
2 Proprietary Rand Corporation, 1981.
3 Further distribution of this software
4 subject to the terms of the Rand
5 license agreement.
6 #endif
7
8 #include "../mh.h"
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <signal.h>
12 #include <stdio.h>
13 #include <strings.h>
14
15 struct msgs *mp;
16
17 m_edit(ed, file, use, altmsg)
18 char **ed, *file, *altmsg;
19 int use;
20 {
21 /* Exec editor. Normal exit returns 0.
22 * To abort, returns -1. To try again, returns -2
23 */
24
25 static char *edsave;
26 static int reedit;
27 struct stat stbuf;
28 int retstat;
29 register char *cp;
30 int pid, wpid;
31 int (*intr)();
32 int status;
33
34 if(!reedit) { /* set initial editor */
35 if(!*ed && (*ed = m_find("editor")) == NULL)
36 *ed = sysed;
37 } else
38 if(!*ed) { /* no explicit editor */
39 *ed = edsave;
40 cp = rindex(*ed, '/');
41 if(cp == 0)
42 cp = *ed;
43 cp = concat(cp, "-next", 0);
44 if((cp = m_find(cp)) != NULL)
45 *ed = cp;
46 }
47 intr = signal(SIGINT, SIG_IGN);
48 if((pid = fork()) == 0) {
49 if(altmsg) {
50 VOID unlink("@");
51 VOID link(altmsg, "@"); /* An easy handle on cur msg */
52 putenv("editalt", altmsg);
53 }
54 m_update();
55 VOID fflush(stdout);
56 VOID signal(SIGINT, intr);
57 execlp(*ed, r1bindex(*ed, '/'), file, 0);
58 fprintf(stderr, "Can't exec the editor: ");
59 perror(*ed); done(-1);
60 } else if(pid == -1) {
61 fprintf(stderr, "No forks!\n");
62 retstat = -1;
63 goto leave;
64 } else
65 while((wpid = wait(&status)) != -1 && wpid != pid) ;
66 VOID signal(SIGINT, intr);
67 if(status) {
68 if((status&0177400 == 0177400) || /* Can't exec editor */
69 (reedit && (status&0377) == 0)) { /*2nd edit.Aborted by user*/
70 retstat = -2;
71 goto leave;
72 }
73 fprintf(stderr, "[%s aborted--%s ", invo_name, file);
74 if(!use && (status&017740)) { /* edit aborted by user */
75 VOID unlink(file);
76 fprintf(stderr, "deleted]\n");
77 } else /* 'use' or system abort */
78 fprintf(stderr, "preserved]\n");
79 retstat = -2;
80 goto leave;
81 }
82 reedit++;
83 retstat = 0;
84 if(altmsg && !mp->msgflags&READONLY) {
85 VOID stat("@", &stbuf);
86 if(stbuf.st_nlink == 1) /*@'s been edited by Ned*/
87 if(unlink(altmsg) == -1 || link("@", altmsg) == -1){
88 fprintf(stderr, "Can't update %s from @ file!\n",altmsg);
89 retstat = 0;
90 goto leave;
91 }
92 }
93 leave:
94 edsave = getcpy(*ed);
95 *ed = 0;
96 VOID unlink("@"); /* Remove this extra link */
97 return(retstat);
98 }