xref: /titanic_50/usr/src/cmd/refer/refer4.c (revision 275c9da86e89f8abf71135cf63d9fc23671b2e60)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980 Regents of the University of California.
11  * All rights reserved. The Berkeley software License Agreement
12  * specifies the terms and conditions for redistribution.
13  */
14 
15 #pragma ident	"%Z%%M%	%I%	%E% SMI"
16 
17 #include "refer..c"
18 #include <locale.h>
19 
20 #define	punctuat(c)	(c == '.' || c == '?' || c == '!' || \
21 			    c == ',' || c == ';' || c == ':')
22 
23 static int gate = 0;
24 static char buff[BUFSIZ];
25 
26 extern void err();
27 
28 char *trimnl(char *);
29 
30 void
31 output(char *s)
32 {
33 	if (gate)
34 		fputs(buff, ftemp);
35 	else
36 		gate = 1;
37 	strcpy(buff, s);
38 	if (strlen(buff) > BUFSIZ)
39 		err(gettext("one buff too big (%d)!"), BUFSIZ);
40 }
41 
42 void
43 append(char *s)
44 {
45 	char *p;
46 	int lch;
47 
48 	trimnl(buff);
49 	for (p = buff; *p; p++)
50 		;
51 	lch = *--p;
52 	if (postpunct && punctuat(lch))
53 		*p = NULL;
54 	else /* pre-punctuation */
55 		switch (lch) {
56 		case '.':
57 		case '?':
58 		case '!':
59 		case ',':
60 		case ';':
61 		case ':':
62 			*p++ = lch;
63 			*p = NULL;
64 		}
65 	strcat(buff, s);
66 	if (postpunct)
67 		switch (lch) {
68 		case '.':
69 		case '?':
70 		case '!':
71 		case ',':
72 		case ';':
73 		case ':':
74 			for (p = buff; *p; p++)
75 				;
76 			if (*--p == '\n')
77 				*p = NULL;
78 			*p++ = lch;
79 			*p++ = '\n';
80 			*p = NULL;
81 		}
82 	if (strlen(buff) > BUFSIZ)
83 		err(gettext("output buff too long (%d)"), BUFSIZ);
84 }
85 
86 void
87 flout(void)
88 {
89 	if (gate)
90 		fputs(buff, ftemp);
91 	gate = 0;
92 }
93 
94 char *
95 trimnl(char *ln)
96 {
97 	char *p = ln;
98 
99 	while (*p)
100 		p++;
101 	p--;
102 	if (*p == '\n')
103 		*p = 0;
104 	return (ln);
105 }
106