xref: /original-bsd/games/pig/pig.c (revision 4918a0cd)
1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1992, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)pig.c	8.1 (Berkeley) 05/31/93";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 void pigout __P((char *, int));
26 void usage __P((void));
27 
28 int
29 main(argc, argv)
30 	int argc;
31 	char *argv[];
32 {
33 	register int len;
34 	int ch;
35 	char buf[1024];
36 
37 	while ((ch = getopt(argc, argv, "")) != EOF)
38 		switch(ch) {
39 		case '?':
40 		default:
41 			usage();
42 		}
43 	argc -= optind;
44 	argv += optind;
45 
46 	for (len = 0; (ch = getchar()) != EOF;) {
47 		if (isalpha(ch)) {
48 			if (len >= sizeof(buf)) {
49 				(void)fprintf(stderr, "pig: ate too much!\n");
50 				exit(1);
51 			}
52 			buf[len++] = ch;
53 			continue;
54 		}
55 		if (len != 0) {
56 			pigout(buf, len);
57 			len = 0;
58 		}
59 		(void)putchar(ch);
60 	}
61 	exit(0);
62 }
63 
64 void
65 pigout(buf, len)
66 	char *buf;
67 	int len;
68 {
69 	register int ch, start;
70 	int olen;
71 
72 	/*
73 	 * If the word starts with a vowel, append "way".  Don't treat 'y'
74 	 * as a vowel if it appears first.
75 	 */
76 	if (index("aeiouAEIOU", buf[0]) != NULL) {
77 		(void)printf("%.*sway", len, buf);
78 		return;
79 	}
80 
81 	/*
82 	 * Copy leading consonants to the end of the word.  The unit "qu"
83 	 * isn't treated as a vowel.
84 	 */
85 	for (start = 0, olen = len;
86 	    !index("aeiouyAEIOUY", buf[start]) && start < olen;) {
87 		ch = buf[len++] = buf[start++];
88 		if ((ch == 'q' || ch == 'Q') && start < olen &&
89 		    (buf[start] == 'u' || buf[start] == 'U'))
90 			buf[len++] = buf[start++];
91 	}
92 	(void)printf("%.*say", olen, buf + start);
93 }
94 
95 void
96 usage()
97 {
98 	(void)fprintf(stderr, "usage: pig\n");
99 	exit(1);
100 }
101