xref: /illumos-gate/usr/src/cmd/refer/refer4.c (revision 7bc3049f)
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 #include "refer..c"
16 #include <locale.h>
17 
18 #define	punctuat(c)	(c == '.' || c == '?' || c == '!' || \
19 			    c == ',' || c == ';' || c == ':')
20 
21 static int gate = 0;
22 static char buff[BUFSIZ];
23 
24 extern void err();
25 
26 char *trimnl(char *);
27 
28 void
output(char * s)29 output(char *s)
30 {
31 	if (gate)
32 		fputs(buff, ftemp);
33 	else
34 		gate = 1;
35 	strcpy(buff, s);
36 	if (strlen(buff) > BUFSIZ)
37 		err(gettext("one buff too big (%d)!"), BUFSIZ);
38 }
39 
40 void
append(char * s)41 append(char *s)
42 {
43 	char *p;
44 	int lch;
45 
46 	trimnl(buff);
47 	for (p = buff; *p; p++)
48 		;
49 	lch = *--p;
50 	if (postpunct && punctuat(lch))
51 		*p = '\0';
52 	else /* pre-punctuation */
53 		switch (lch) {
54 		case '.':
55 		case '?':
56 		case '!':
57 		case ',':
58 		case ';':
59 		case ':':
60 			*p++ = lch;
61 			*p = '\0';
62 		}
63 	strcat(buff, s);
64 	if (postpunct)
65 		switch (lch) {
66 		case '.':
67 		case '?':
68 		case '!':
69 		case ',':
70 		case ';':
71 		case ':':
72 			for (p = buff; *p; p++)
73 				;
74 			if (*--p == '\n')
75 				*p = '\0';
76 			*p++ = lch;
77 			*p++ = '\n';
78 			*p = '\0';
79 		}
80 	if (strlen(buff) > BUFSIZ)
81 		err(gettext("output buff too long (%d)"), BUFSIZ);
82 }
83 
84 void
flout(void)85 flout(void)
86 {
87 	if (gate)
88 		fputs(buff, ftemp);
89 	gate = 0;
90 }
91 
92 char *
trimnl(char * ln)93 trimnl(char *ln)
94 {
95 	char *p = ln;
96 
97 	while (*p)
98 		p++;
99 	p--;
100 	if (*p == '\n')
101 		*p = 0;
102 	return (ln);
103 }
104