xref: /original-bsd/contrib/ed/q.c (revision 91f04672)
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[] = "@(#)q.c	5.2 (Berkeley) 01/23/93";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 
17 #include <db.h>
18 #include <regex.h>
19 #include <setjmp.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 
25 #include "ed.h"
26 #include "extern.h"
27 
28 /*
29  * End this editting session and exit with saving the buffer. If no
30  * write has occurred since the last buffer modification a warning
31  * is given once ('Q' over-rides the warning).
32  */
33 void
34 q(inputt, errnum)
35 	FILE *inputt;
36 	int *errnum;
37 {
38 	register int l_ss = ss;
39 	int l_which;			/* Which is it? 'q' or 'Q'. */
40 
41 	l_which = ss;
42 	for (;;) {
43 		l_ss = getc(inputt);
44 		if ((l_ss != ' ') && (l_ss != '\n') && (l_ss != EOF)) {
45 			*errnum = -1;
46 			strcpy(help_msg, "illegal command option");
47 			return;
48 		}
49 		if ((l_ss == '\n') || (l_ss == EOF))
50 			break;
51 	}
52 
53 	ungetc(l_ss, inputt);
54 	/* Note: 'Q' will bypass this if stmt., which warns of no save. */
55 	if ((change_flag == 1L) && (l_which == 'q')) {
56 		change_flag = 0L;
57 		strcpy(help_msg, "buffer changes not saved");
58 		*errnum = -1;
59 		ss = l_ss;
60 		return;
61 	}
62 	/* Do cleanup; should it be even bothered?? */
63 	start = top;
64 	End = bottom;
65 	start_default = End_default = 0;
66 
67 	/* We don't care about the returned errnum val anymore. */
68 	d(inputt, errnum);
69 	u_clr_stk();
70 	free(text);
71 	free(filename_current);
72 	(dbhtmp->close) (dbhtmp);	/* Overhead as the cache is flushed. */
73 	unlink(template);
74 	exit(0);
75 }
76