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