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