xref: /original-bsd/contrib/ed/q.c (revision 4a884f8b)
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.4 (Berkeley) 03/08/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 	for (;;) {
49 		l_ss = getc(inputt);
50 		if ((l_ss != ' ') && (l_ss != '\n') && (l_ss != EOF)) {
51 			*errnum = -1;
52 			strcpy(help_msg, "illegal command option");
53 			return;
54 		}
55 		if ((l_ss == '\n') || (l_ss == EOF))
56 			break;
57 	}
58 
59 	ungetc(l_ss, inputt);
60 	/* Note: 'Q' will bypass this if stmt., which warns of no save. */
61 	if ((change_flag == 1L) && (l_which == 'q')) {
62 		change_flag = 0L;
63 		strcpy(help_msg, "buffer changes not saved");
64 		*errnum = -1;
65 		ss = l_ss;
66 		return;
67 	}
68 	/* Do cleanup; should it be even bothered?? */
69 	Start = top;
70 	End = bottom;
71 	Start_default = End_default = 0;
72 
73 	/* We don't care about the returned errnum val anymore. */
74 	d(inputt, errnum);
75 	u_clr_stk();
76 	free(text);
77 	free(filename_current);
78 #ifdef STDIO
79 	fclose(fhtmp);
80 #endif
81 #ifdef DBI
82 	(dbhtmp->close) (dbhtmp);	/* Overhead as the cache is flushed. */
83 #endif
84 #ifndef MEMORY
85 	unlink(template);
86 #endif
87 	exit(0);
88 }
89