Commit | Line | Data |
---|---|---|
ad787160 C |
1 | /*- |
2 | * Copyright (c) 1992, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
35 | static char copyright[] = | |
36 | "@(#) Copyright (c) 1992, 1993\n\ | |
37 | The Regents of the University of California. All rights reserved.\n"; | |
38 | #endif /* not lint */ | |
39 | ||
40 | #ifndef lint | |
41 | static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/9/93"; | |
42 | #endif /* not lint */ | |
43 | ||
44 | #include <sys/param.h> | |
45 | #include <sys/stat.h> | |
46 | ||
47 | #include <ctype.h> | |
48 | #include <err.h> | |
49 | #include <errno.h> | |
50 | #include <stdlib.h> | |
51 | #include <string.h> | |
52 | #include <termios.h> | |
53 | #include <unistd.h> | |
54 | ||
55 | #if __STDC__ | |
56 | #include <stdarg.h> | |
57 | #else | |
58 | #include <varargs.h> | |
59 | #endif | |
60 | ||
61 | #include "vi.h" | |
62 | #include "excmd.h" | |
63 | #include "recover.h" | |
64 | #include "pathnames.h" | |
65 | #include "tag.h" | |
66 | ||
67 | static void msgflush __P((GS *)); | |
68 | static void obsolete __P((char *[])); | |
69 | static void reset __P((GS *)); | |
70 | static void usage __P((void)); | |
71 | ||
72 | GS *__global_list; /* GLOBAL: List of screens. */ | |
73 | ||
74 | int | |
75 | main(argc, argv) | |
76 | int argc; | |
77 | char *argv[]; | |
78 | { | |
79 | extern int optind; | |
80 | extern char *optarg; | |
81 | static int reenter; /* STATIC: Re-entrancy check. */ | |
82 | EXCMDARG cmd; | |
83 | GS *gp; | |
84 | SCR *sp; | |
85 | int ch, flagchk, eval; | |
86 | char *excmdarg, *errf, *myname, *p, *rfname, *tag; | |
87 | ||
88 | /* Stop if indirecting through a NULL pointer. */ | |
89 | if (reenter++) | |
90 | abort(); | |
91 | ||
92 | eval = 0; | |
93 | ||
94 | /* Figure out the program name. */ | |
95 | if ((myname = strrchr(*argv, '/')) == NULL) | |
96 | myname = *argv; | |
97 | else | |
98 | ++myname; | |
99 | ||
100 | /* Build and initialize the GS structure. */ | |
101 | if ((gp = malloc(sizeof(GS))) == NULL) | |
102 | err(1, NULL); | |
103 | __global_list = gp; | |
104 | memset(gp, 0, sizeof(GS)); | |
105 | HDR_INIT(gp->scrhdr, next, prev); | |
106 | HDR_INIT(gp->exfhdr, next, prev); | |
107 | if (tcgetattr(STDIN_FILENO, &gp->original_termios)) | |
108 | err(1, "tcgetattr"); | |
109 | ||
110 | /* Build and initialize the first/current screen. */ | |
111 | if ((sp = malloc(sizeof(SCR))) == NULL) | |
112 | err(1, NULL); | |
113 | if (scr_init(NULL, sp)) | |
114 | err(1, NULL); | |
115 | sp->gp = gp; /* All screens point to the GS structure. */ | |
116 | HDR_INIT(sp->seqhdr, next, prev); | |
117 | /* No need to block SIGALRM yet. */ | |
118 | HDR_APPEND(sp, &gp->scrhdr, next, prev, SCR); | |
119 | ||
120 | if (set_window_size(sp, 0)) /* Set the window size. */ | |
121 | goto err1; | |
122 | ||
123 | if (opts_init(sp)) /* Options initialization. */ | |
124 | goto err1; | |
125 | ||
126 | /* | |
127 | * Keymaps, special keys. | |
128 | * Must follow options and sequence map initializations. | |
129 | */ | |
130 | if (term_init(sp)) | |
131 | goto err1; | |
132 | ||
133 | #ifndef NO_DIGRAPH | |
134 | if (digraph_init(sp)) /* Digraph initialization. */ | |
135 | goto err1; | |
136 | #endif | |
137 | /* Set screen mode based on the program name. */ | |
138 | if (!strcmp(myname, "ex") || !strcmp(myname, "nex")) | |
139 | F_SET(sp, S_MODE_EX); | |
140 | else if (!strcmp(myname, "view")) { | |
141 | O_SET(sp, O_READONLY); | |
142 | F_SET(sp, S_MODE_VI); | |
143 | } else | |
144 | F_SET(sp, S_MODE_VI); | |
145 | ||
146 | /* Convert old-style arguments into new-style ones. */ | |
147 | obsolete(argv); | |
148 | ||
149 | /* Parse the arguments. */ | |
150 | flagchk = 0; | |
151 | F_SET(gp, G_SNAPSHOT); | |
152 | excmdarg = errf = rfname = tag = NULL; | |
153 | while ((ch = getopt(argc, argv, "c:elmRr:sT:t:v")) != EOF) | |
154 | switch(ch) { | |
155 | case 'c': /* Run the command. */ | |
156 | excmdarg = optarg; | |
157 | break; | |
158 | case 'e': /* Ex mode. */ | |
159 | F_SET(sp, S_MODE_EX); | |
160 | break; | |
161 | case 'l': | |
162 | exit(rcv_list()); | |
163 | #ifndef NO_ERRLIST | |
164 | case 'm': /* Error list. */ | |
165 | ++flagchk; | |
166 | errf = optarg; | |
167 | break; | |
168 | #endif | |
169 | case 'R': /* Readonly. */ | |
170 | O_SET(sp,O_READONLY); | |
171 | break; | |
172 | case 'r': /* Recover. */ | |
173 | ++flagchk; | |
174 | rfname = optarg; | |
175 | break; | |
176 | case 's': /* No snapshot. */ | |
177 | F_CLR(gp, G_SNAPSHOT); | |
178 | break; | |
179 | #ifdef DEBUG | |
180 | case 'T': /* Trace. */ | |
181 | if ((gp->tracefp = fopen(optarg, "w")) == NULL) | |
182 | err(1, "%s", optarg); | |
183 | (void)fprintf(gp->tracefp, | |
184 | "\n===\ntrace: open %s\n", optarg); | |
185 | break; | |
186 | #endif | |
187 | case 't': /* Tag. */ | |
188 | ++flagchk; | |
189 | tag = optarg; | |
190 | break; | |
191 | case 'v': /* Vi mode. */ | |
192 | F_SET(sp, S_MODE_VI); | |
193 | break; | |
194 | case '?': | |
195 | default: | |
196 | usage(); | |
197 | } | |
198 | argc -= optind; | |
199 | argv += optind; | |
200 | ||
201 | /* Only permit one file specification. */ | |
202 | if (flagchk > 1) | |
203 | err(1, "only one of -r and -t is permitted."); | |
204 | ||
205 | /* | |
206 | * Source the system, environment, ~user and local .exrc values. | |
207 | * If the environment exists, vi historically doesn't check ~user. | |
208 | */ | |
209 | (void)ex_cfile(sp, NULL, _PATH_SYSEXRC, 0); | |
210 | ||
211 | /* Source the EXINIT environment variable. */ | |
212 | if ((p = getenv("EXINIT")) != NULL) | |
213 | if ((p = strdup(p)) == NULL) { | |
214 | msgq(sp, M_ERR, "Error: %s", strerror(errno)); | |
215 | goto err1; | |
216 | } else { | |
217 | (void)ex_cstring(sp, NULL, p, strlen(p)); | |
218 | free(p); | |
219 | } | |
220 | else if ((p = getenv("HOME")) != NULL && *p) { | |
221 | char path[MAXPATHLEN]; | |
222 | ||
223 | (void)snprintf(path, sizeof(path), "%s/.exrc", p); | |
224 | (void)ex_cfile(sp, NULL, path, 0); | |
225 | } | |
226 | if (O_ISSET(sp, O_EXRC)) | |
227 | (void)ex_cfile(sp, NULL, _PATH_EXRC, 0); | |
228 | ||
229 | /* Any remaining arguments are file names. */ | |
230 | if (argc) | |
231 | file_set(sp, argc, argv); | |
232 | ||
233 | /* Use an error list file, recovery file or tag file if specified. */ | |
234 | #ifndef NO_ERRLIST | |
235 | if (errf != NULL) { | |
236 | SETCMDARG(cmd, C_ERRLIST, 0, OOBLNO, 0, 0, errf); | |
237 | if (ex_errlist(sp, NULL, &cmd)) | |
238 | goto err1; | |
239 | } else | |
240 | #endif | |
241 | if (tag != NULL) { | |
242 | if (ex_tagfirst(sp, tag)) | |
243 | goto err1; | |
244 | } else if (rfname != NULL) { | |
245 | if ((sp->ep = rcv_read(sp, rfname)) == NULL) | |
246 | goto err1; | |
247 | } else if ((sp->ep = file_start(sp, file_first(sp, 1), NULL)) == NULL) | |
248 | goto err1; | |
249 | ||
250 | /* Do commands from the command line. */ | |
251 | if (excmdarg != NULL) | |
252 | (void)ex_cstring(sp, sp->ep, excmdarg, strlen(excmdarg)); | |
253 | ||
254 | /* | |
255 | * The commands we executed probably positioned the file. This | |
256 | * isn't a wonderful test, but it's probably fairly safe. | |
257 | */ | |
258 | if ((sp->lno != 1 || sp->cno != 0) && F_ISSET(sp->ep, F_NOSETPOS)) | |
259 | F_CLR(sp->ep, F_NOSETPOS); | |
260 | ||
261 | /* Call a screen. */ | |
262 | while (sp != NULL) | |
263 | switch(F_ISSET(sp, S_MODE_EX | S_MODE_VI)) { | |
264 | case S_MODE_EX: | |
265 | if (sex(sp, sp->ep, &sp)) | |
266 | goto err2; | |
267 | break; | |
268 | case S_MODE_VI: | |
269 | if (svi(sp, sp->ep, &sp)) | |
270 | goto err2; | |
271 | break; | |
272 | default: | |
273 | abort(); | |
274 | } | |
275 | ||
276 | /* | |
277 | * Two error paths. The first means that something failed before | |
278 | * we called a screen routine. Swap the message pointers between | |
279 | * the SCR and the GS, so messages get displayed. The second is | |
280 | * something failed in a screen. NOTE: sp may be GONE when the | |
281 | * screen returns, so only the gp can be trusted. | |
282 | */ | |
283 | if (0) { | |
284 | err1: gp->msgp = sp->msgp; | |
285 | err2: eval = 1; | |
286 | } | |
287 | ||
288 | /* Turn off the recovery timer. */ | |
289 | rcv_end(); | |
290 | ||
291 | /* Reset anything that needs resetting. */ | |
292 | reset(gp); | |
293 | ||
294 | /* Flush any left-over error messages. */ | |
295 | msgflush(gp); | |
296 | ||
297 | if (tcsetattr(STDIN_FILENO, TCSADRAIN, &gp->original_termios)) | |
298 | err(1, "tcsetattr"); | |
299 | exit(eval); | |
300 | } | |
301 | ||
302 | /* | |
303 | * reset -- | |
304 | * Reset any changed global state. | |
305 | */ | |
306 | static void | |
307 | reset(gp) | |
308 | GS *gp; | |
309 | { | |
310 | char *tty; | |
311 | ||
312 | if (gp->flags & G_SETMODE) /* O_MESG */ | |
313 | if ((tty = ttyname(STDERR_FILENO)) == NULL) | |
314 | warn("ttyname"); | |
315 | else if (chmod(tty, gp->origmode) < 0) | |
316 | warn("%s", tty); | |
317 | } | |
318 | ||
319 | /* | |
320 | * msgflush -- | |
321 | * Flush any remaining messages. | |
322 | */ | |
323 | static void | |
324 | msgflush(gp) | |
325 | GS *gp; | |
326 | { | |
327 | MSG *mp; | |
328 | ||
329 | /* Ring the bell. */ | |
330 | if (F_ISSET(gp, S_BELLSCHED)) | |
331 | (void)fprintf(stderr, "\07"); /* \a */ | |
332 | ||
333 | /* Display the messages. */ | |
334 | for (mp = gp->msgp; | |
335 | mp != NULL && !(F_ISSET(mp, M_EMPTY)); mp = mp->next) | |
336 | (void)fprintf(stderr, "%.*s\n", (int)mp->len, mp->mbuf); | |
337 | } | |
338 | ||
339 | static void | |
340 | obsolete(argv) | |
341 | char *argv[]; | |
342 | { | |
343 | size_t len; | |
344 | char *p, *myname; | |
345 | ||
346 | /* | |
347 | * Translate old style arguments into something getopt will like. | |
348 | * Make sure it's not text space memory, because ex changes the | |
349 | * strings. | |
350 | * Change "+/command" into "-ccommand". | |
351 | * Change "+" and "+$" into "-c$". | |
352 | * Change "+[0-9]*" into "-c[0-9]". | |
353 | * Change "-r" into "-l" | |
354 | */ | |
355 | for (myname = argv[0]; *++argv;) | |
356 | if (argv[0][0] == '+') { | |
357 | if (argv[0][1] == '\0' || argv[0][1] == '$') { | |
358 | if ((argv[0] = malloc(4)) == NULL) | |
359 | err(1, NULL); | |
360 | memmove(argv[0], "-c$", 4); | |
361 | } else if (argv[0][1] == '/') { | |
362 | p = argv[0]; | |
363 | len = strlen(argv[0]); | |
364 | if ((argv[0] = malloc(len + 3)) == NULL) | |
365 | err(1, NULL); | |
366 | argv[0][0] = '-'; | |
367 | argv[0][1] = 'c'; | |
368 | (void)strcpy(argv[0] + 2, p + 1); | |
369 | } else if (isdigit(argv[0][1])) { | |
370 | p = argv[0]; | |
371 | len = strlen(argv[0]); | |
372 | if ((argv[0] = malloc(len + 2)) == NULL) | |
373 | err(1, NULL); | |
374 | argv[0][0] = '-'; | |
375 | argv[0][1] = 'c'; | |
376 | (void)strcpy(argv[0] + 2, p + 1); | |
377 | } | |
378 | } | |
379 | else if (argv[0][0] == '-' && | |
380 | argv[0][1] == 'r' && argv[1] == NULL) | |
381 | argv[0][1] = 'l'; | |
382 | } | |
383 | ||
384 | static void | |
385 | usage() | |
386 | { | |
387 | (void)fprintf(stderr, | |
388 | "usage: vi [-eRrsv] [-c command] [-m file] [-t tag]\n"); | |
389 | exit(1); | |
390 | } |