| 1 | /*- |
| 2 | * Copyright (c) 1992 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This code is derived from software contributed to Berkeley by |
| 6 | * Rodney Ruddock of the University of Guelph. |
| 7 | * |
| 8 | * %sccs.include.redist.c% |
| 9 | */ |
| 10 | |
| 11 | #ifndef lint |
| 12 | static char sccsid[] = "@(#)e.c 5.1 (Berkeley) %G%"; |
| 13 | #endif /* not lint */ |
| 14 | |
| 15 | #include "ed.h" |
| 16 | |
| 17 | /* |
| 18 | * Places a new file in the buffer to be editted. The current contents |
| 19 | * of the buffer are deleted - no undo can be perfomed. A warning is |
| 20 | * issued once if no write has occurred since the last buffer |
| 21 | * modification (unless 'E' spec'd). |
| 22 | */ |
| 23 | |
| 24 | void |
| 25 | e(inputt, errnum) |
| 26 | |
| 27 | FILE *inputt; |
| 28 | int *errnum; |
| 29 | |
| 30 | { |
| 31 | int l_which; /* which is it? 'e' or 'E' */ |
| 32 | char *l_temp; |
| 33 | |
| 34 | l_which = ss; |
| 35 | |
| 36 | l_temp = filename(inputt, errnum); |
| 37 | if (sigint_flag) |
| 38 | SIGINT_ACTION; |
| 39 | if (*errnum == 1) |
| 40 | { |
| 41 | free(filename_current); |
| 42 | filename_current = l_temp; |
| 43 | } |
| 44 | else if (*errnum == -2) |
| 45 | while (((ss = getc(inputt)) != '\n') || (ss == EOF)) |
| 46 | ; |
| 47 | else if (*errnum < 0) |
| 48 | return; |
| 49 | *errnum = 0; |
| 50 | |
| 51 | /* note: 'E' will bypass this if stmt., which warns of no save */ |
| 52 | if ((change_flag == 1L) && (l_which == 'e')) |
| 53 | { |
| 54 | change_flag = 0L; |
| 55 | strcpy(help_msg, "warning: buffer changes not saved"); |
| 56 | *errnum = -1; |
| 57 | ungetc('\n', inputt); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | start = top; |
| 62 | End = bottom; |
| 63 | start_default = End_default = 0; |
| 64 | if (start == NULL && bottom == NULL) |
| 65 | ; |
| 66 | else |
| 67 | { |
| 68 | ungetc(ss, inputt); |
| 69 | d(inputt, errnum); /* delete the whole buffer */ |
| 70 | } |
| 71 | if (sigint_flag) |
| 72 | SIGINT_ACTION; |
| 73 | u_clr_stk(); /* an 'e' clears all traces of last doc'mt, even in 'g' */ |
| 74 | if (*errnum < 0) |
| 75 | return; |
| 76 | *errnum = 0; |
| 77 | #ifdef STDIO |
| 78 | if (fhtmp > NULL) |
| 79 | { |
| 80 | fclose(fhtmp); |
| 81 | unlink(template); |
| 82 | } |
| 83 | #endif |
| 84 | #ifdef DBI |
| 85 | if (dbhtmp != NULL) |
| 86 | { |
| 87 | (dbhtmp->close)(dbhtmp); |
| 88 | unlink(template); |
| 89 | } |
| 90 | #endif |
| 91 | |
| 92 | name_set = 1; |
| 93 | e2(inputt, errnum); |
| 94 | |
| 95 | *errnum = 1; |
| 96 | } /* end-e */ |
| 97 | |
| 98 | |
| 99 | /* This is pulled out of e.c to make the "simulated 'e'" at startup |
| 100 | * easier to handle. |
| 101 | */ |
| 102 | |
| 103 | void |
| 104 | e2(inputt, errnum) |
| 105 | |
| 106 | FILE *inputt; |
| 107 | int *errnum; |
| 108 | |
| 109 | { |
| 110 | #ifdef DBI |
| 111 | RECNOINFO l_dbaccess; |
| 112 | #endif |
| 113 | |
| 114 | if (template == NULL) |
| 115 | { |
| 116 | template = (char *)calloc(FILENAME_LEN, sizeof(char)); |
| 117 | if (template == NULL) |
| 118 | ed_exit(4); |
| 119 | } |
| 120 | |
| 121 | /* create the buffer using the method favored at compile time */ |
| 122 | bcopy("/tmp/_4.4bsd_ed_XXXXXX\0", template, 22); |
| 123 | mktemp(template); |
| 124 | #ifdef STDIO |
| 125 | fhtmp = fopen(template, "w+"); |
| 126 | file_seek = 0; |
| 127 | #endif |
| 128 | #ifdef DBI |
| 129 | (l_dbaccess.bval) = (u_char)'\0'; |
| 130 | (l_dbaccess.cachesize) = 0; |
| 131 | (l_dbaccess.flags) = R_NOKEY; |
| 132 | (l_dbaccess.lorder) = 0; |
| 133 | (l_dbaccess.reclen) = 0; |
| 134 | dbhtmp = dbopen(template, (O_CREAT | O_RDWR), (S_IRUSR | S_IWUSR), (DBTYPE)DB_RECNO, &l_dbaccess); |
| 135 | #endif |
| 136 | current = top; |
| 137 | start = top; |
| 138 | End = bottom; |
| 139 | |
| 140 | if (sigint_flag) |
| 141 | SIGINT_ACTION; |
| 142 | if (name_set) |
| 143 | { |
| 144 | filename_flag = 1; /* so 'r' knows the filename is already read in */ |
| 145 | r(inputt, errnum); |
| 146 | } |
| 147 | change_flag = 0; |
| 148 | } /* end-e2 */ |