[186] | 1 | /* |
---|
[1927] | 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 | /* |
---|
[12439] | 9 | * $Id: edit.c,v 1.16 1999-02-02 20:39:46 kcr Exp $ |
---|
[186] | 10 | * |
---|
| 11 | * Utility routines. |
---|
| 12 | * |
---|
[8816] | 13 | * |
---|
[186] | 14 | */ |
---|
| 15 | |
---|
| 16 | #ifndef lint |
---|
[1633] | 17 | static char rcsid_discuss_utils_c[] = |
---|
[12439] | 18 | "$Id: edit.c,v 1.16 1999-02-02 20:39:46 kcr Exp $"; |
---|
[1801] | 19 | #endif /* lint */ |
---|
[186] | 20 | |
---|
| 21 | #include <stdio.h> |
---|
| 22 | #include <sys/file.h> |
---|
[1633] | 23 | #include <string.h> |
---|
[186] | 24 | #include <signal.h> |
---|
[8816] | 25 | #include <ss/ss.h> |
---|
[1633] | 26 | #include <discuss/discuss.h> |
---|
[186] | 27 | #include "globals.h" |
---|
[194] | 28 | #include <sys/types.h> |
---|
[12439] | 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 |
---|
[194] | 35 | #include <sys/stat.h> |
---|
| 36 | #include <sys/errno.h> |
---|
[186] | 37 | |
---|
[206] | 38 | bool use_editor = TRUE; |
---|
| 39 | char *editor_path = NULL; |
---|
| 40 | |
---|
[1041] | 41 | extern char *getenv(); |
---|
| 42 | |
---|
[186] | 43 | /* |
---|
[194] | 44 | * int edit(fn, editor) |
---|
[186] | 45 | * |
---|
| 46 | * fn: pathname of file to edit |
---|
| 47 | * |
---|
[194] | 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 | * |
---|
[186] | 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 |
---|
[194] | 61 | edit(fn, edit_path) |
---|
[186] | 62 | char *fn; |
---|
[194] | 63 | char *edit_path; |
---|
[186] | 64 | { |
---|
[1633] | 65 | char *editor_path_e, *editor_path_2 = NULL; |
---|
[1041] | 66 | char *editor_path_v; |
---|
[186] | 67 | int pid; |
---|
| 68 | int (*handler)(); |
---|
[6529] | 69 | int wbuf; |
---|
[7166] | 70 | |
---|
[194] | 71 | struct stat buf; |
---|
[1041] | 72 | char buffer[BUFSIZ]; |
---|
| 73 | FILE *the_file = NULL; |
---|
[12439] | 74 | #if HAVE_SIGACTION |
---|
| 75 | struct sigaction act, oact; |
---|
| 76 | sigemptyset(&act.sa_mask); |
---|
| 77 | act.sa_flags = 0; |
---|
[7166] | 78 | #endif |
---|
[186] | 79 | |
---|
[1041] | 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 | |
---|
[1633] | 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; |
---|
[1041] | 89 | else { |
---|
| 90 | the_file = fopen(fn, "w"); |
---|
| 91 | if (!the_file) { |
---|
[192] | 92 | perror(fn); |
---|
[1041] | 93 | printf("Error opening file: %d\n",errno); |
---|
[192] | 94 | return(errno); |
---|
| 95 | } |
---|
[1041] | 96 | |
---|
| 97 | ftruncate(fileno(the_file), 0); |
---|
[1678] | 98 | fchmod(fileno(the_file), 0700); |
---|
[194] | 99 | printf("Enter transaction; end with ^D or '.' on a line by itself.\n"); |
---|
[1041] | 100 | for (;;) { |
---|
[1089] | 101 | if ((gets(buffer) == NULL) || interrupt || !strcmp(buffer, ".")) break; |
---|
[1041] | 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 | } |
---|
[192] | 115 | } |
---|
[1041] | 116 | } |
---|
[194] | 117 | |
---|
[1041] | 118 | if (editor_path_2) { |
---|
| 119 | if (the_file) { |
---|
| 120 | clearerr(stdin); |
---|
| 121 | fclose(the_file); |
---|
| 122 | } |
---|
[194] | 123 | switch ((pid = fork())) { |
---|
| 124 | case -1: |
---|
| 125 | perror("couldn't fork"); |
---|
[1041] | 126 | printf("Couldn't fork, error %d\n",errno); |
---|
[194] | 127 | return(errno); |
---|
| 128 | case 0: |
---|
[1041] | 129 | (void) execlp(editor_path_2, editor_path_2, fn, 0); |
---|
| 130 | (void) perror(editor_path_2); |
---|
[194] | 131 | exit(1); |
---|
| 132 | default: |
---|
| 133 | break; |
---|
| 134 | } |
---|
[12439] | 135 | #if HAVE_SIGACTION |
---|
[7166] | 136 | act.sa_handler= (void (*)()) SIG_IGN; |
---|
| 137 | (void) sigaction(SIGINT, &act, &oact); |
---|
| 138 | #else |
---|
[194] | 139 | handler = signal(SIGINT, SIG_IGN); |
---|
[7166] | 140 | #endif |
---|
[194] | 141 | while (wait(&wbuf) != pid) |
---|
| 142 | ; |
---|
[12439] | 143 | #if HAVE_SIGACTION |
---|
[7166] | 144 | (void) sigaction(SIGINT, &oact, NULL); |
---|
| 145 | #else |
---|
[194] | 146 | (void) signal(SIGINT, handler); |
---|
[7166] | 147 | #endif |
---|
[194] | 148 | if (WIFSIGNALED(wbuf)) |
---|
| 149 | return(ET_CHILD_DIED); |
---|
[12439] | 150 | if (WEXITSTATUS(wbuf)) |
---|
[194] | 151 | return(ET_CHILD_ERR); |
---|
[1041] | 152 | } else { |
---|
| 153 | clearerr(stdin); |
---|
| 154 | fclose(the_file); |
---|
[192] | 155 | } |
---|
[1041] | 156 | |
---|
[194] | 157 | if (stat (fn, &buf) != 0 || buf.st_size == 0) { |
---|
| 158 | unlink(fn); |
---|
| 159 | } |
---|
| 160 | return(0); |
---|
| 161 | } |
---|
[186] | 162 | |
---|
[194] | 163 | touch(fn) |
---|
| 164 | char *fn; |
---|
| 165 | { |
---|
| 166 | int fd; |
---|
| 167 | if ((fd=creat(fn, 0600)) < 0) |
---|
[186] | 168 | return(errno); |
---|
[194] | 169 | else close(fd); |
---|
[186] | 170 | return(0); |
---|
| 171 | } |
---|