xref: /csrg-svn/games/pig/pig.c (revision 69223)
155918Sbostic /*-
260827Sbostic  * Copyright (c) 1992, 1993
360827Sbostic  *	The Regents of the University of California.  All rights reserved.
455918Sbostic  *
555918Sbostic  * %sccs.include.redist.c%
655918Sbostic  */
755918Sbostic 
855918Sbostic #ifndef lint
960827Sbostic static char copyright[] =
1060827Sbostic "@(#) Copyright (c) 1992, 1993\n\
1160827Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1255918Sbostic #endif /* not lint */
1355918Sbostic 
1455918Sbostic #ifndef lint
15*69223Sbostic static char sccsid[] = "@(#)pig.c	8.2 (Berkeley) 05/04/95";
1655918Sbostic #endif /* not lint */
1755918Sbostic 
1855918Sbostic #include <sys/types.h>
1955918Sbostic 
2055918Sbostic #include <ctype.h>
2155918Sbostic #include <stdio.h>
2255918Sbostic #include <stdlib.h>
2355918Sbostic #include <string.h>
24*69223Sbostic #include <unistd.h>
2555918Sbostic 
2655918Sbostic void pigout __P((char *, int));
2755918Sbostic void usage __P((void));
2855918Sbostic 
2955918Sbostic int
main(argc,argv)3055918Sbostic main(argc, argv)
3155918Sbostic 	int argc;
3255918Sbostic 	char *argv[];
3355918Sbostic {
3455918Sbostic 	register int len;
3555918Sbostic 	int ch;
3655918Sbostic 	char buf[1024];
3755918Sbostic 
3855918Sbostic 	while ((ch = getopt(argc, argv, "")) != EOF)
3955918Sbostic 		switch(ch) {
4055918Sbostic 		case '?':
4155918Sbostic 		default:
4255918Sbostic 			usage();
4355918Sbostic 		}
4455918Sbostic 	argc -= optind;
4555918Sbostic 	argv += optind;
4655918Sbostic 
4755918Sbostic 	for (len = 0; (ch = getchar()) != EOF;) {
4855918Sbostic 		if (isalpha(ch)) {
4955918Sbostic 			if (len >= sizeof(buf)) {
5055918Sbostic 				(void)fprintf(stderr, "pig: ate too much!\n");
5155918Sbostic 				exit(1);
5255918Sbostic 			}
5355918Sbostic 			buf[len++] = ch;
5455918Sbostic 			continue;
5555918Sbostic 		}
5655918Sbostic 		if (len != 0) {
5755918Sbostic 			pigout(buf, len);
5855918Sbostic 			len = 0;
5955918Sbostic 		}
6055918Sbostic 		(void)putchar(ch);
6155918Sbostic 	}
6255918Sbostic 	exit(0);
6355918Sbostic }
6455918Sbostic 
6555918Sbostic void
pigout(buf,len)6655918Sbostic pigout(buf, len)
6755918Sbostic 	char *buf;
6855918Sbostic 	int len;
6955918Sbostic {
7055918Sbostic 	register int ch, start;
7155918Sbostic 	int olen;
7255918Sbostic 
7355918Sbostic 	/*
7455918Sbostic 	 * If the word starts with a vowel, append "way".  Don't treat 'y'
7555918Sbostic 	 * as a vowel if it appears first.
7655918Sbostic 	 */
7755918Sbostic 	if (index("aeiouAEIOU", buf[0]) != NULL) {
7855918Sbostic 		(void)printf("%.*sway", len, buf);
7955918Sbostic 		return;
8055918Sbostic 	}
8155918Sbostic 
8255918Sbostic 	/*
8355918Sbostic 	 * Copy leading consonants to the end of the word.  The unit "qu"
8455918Sbostic 	 * isn't treated as a vowel.
8555918Sbostic 	 */
8655918Sbostic 	for (start = 0, olen = len;
8755918Sbostic 	    !index("aeiouyAEIOUY", buf[start]) && start < olen;) {
8855918Sbostic 		ch = buf[len++] = buf[start++];
8955918Sbostic 		if ((ch == 'q' || ch == 'Q') && start < olen &&
9055918Sbostic 		    (buf[start] == 'u' || buf[start] == 'U'))
9155918Sbostic 			buf[len++] = buf[start++];
9255918Sbostic 	}
9355918Sbostic 	(void)printf("%.*say", olen, buf + start);
9455918Sbostic }
9555918Sbostic 
9655918Sbostic void
usage()9755918Sbostic usage()
9855918Sbostic {
9955918Sbostic 	(void)fprintf(stderr, "usage: pig\n");
10055918Sbostic 	exit(1);
10155918Sbostic }
102