xref: /illumos-gate/usr/src/cmd/sgs/tools/common/piglatin.c (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  *	Copyright 2007 Sun Microsystems, Inc.
23  *	All rights reserved.
24  *	Use is subject to license terms.
25  *
26  * Very crude pig latin converter.
27  * Piglatin is an encoded form of English that is often used by  children
28  * as a game. A piglatin word is formed from an English word by:
29  *
30  *  .	If the word begins with a consonant, move the consonant to the end of
31  *	the word and append the letters "ay". Example: "door" becomes "oorday".
32  *
33  *  .	If the word begins with a vowel, merely append "way" to the word.
34  *	Example: "ate" becomes "ateway
35  */
36 
37 #include <stdio.h>
38 #include <strings.h>
39 #include <ctype.h>
40 
41 int
42 main()
43 {
44 	char	buffer[32767], * cb, * sb;
45 	int	ic, ignore = 0, word = 0;
46 
47 	sb = cb = &buffer[0];
48 	*sb = '\0';
49 
50 	while ((ic = getc(stdin)) != EOF) {
51 		char c = (char)ic;
52 
53 		/*
54 		 * Ignore all possible formatting statements.
55 		 */
56 		if (c == '%')
57 			ignore = 1;
58 
59 		/*
60 		 * Isolate the word that will be converted.
61 		 */
62 		if (isspace(ic) || (ispunct(ic) && ((ignore == 0) ||
63 		    ((c != '%') && (c != '.') && (c != '#') && (c != '-'))))) {
64 			char s = buffer[0];
65 
66 			/*
67 			 * If we haven't collected any words yet simply
68 			 * printf the last character.
69 			 */
70 			if (word == 0) {
71 				(void) putc(ic, stdout);
72 				continue;
73 			}
74 
75 			/*
76 			 * Leave format strings alone - contain "%".
77 			 * Leave single characters alone, typically
78 			 * these result from "\n", "\t" etc.
79 			 */
80 			if ((ignore == 0) && ((cb - buffer) > 1)) {
81 				if ((s == 'a') || (s == 'A') ||
82 				    (s == 'e') || (s == 'E') ||
83 				    (s == 'i') || (s == 'I') ||
84 				    (s == 'o') || (s == 'O') ||
85 				    (s == 'u') || (s == 'U')) {
86 					/*
87 					 * Append "way" to the word.
88 					 */
89 					(void) strcpy(cb, "way");
90 				} else {
91 					/*
92 					 * Move first letter to the end
93 					 * of the word and add "ay".
94 					 */
95 					sb++;
96 					*cb = s;
97 					(void) strcpy(++cb, "ay");
98 				}
99 			} else
100 				*cb = '\0';
101 
102 			/*
103 			 * Output the collected buffer, the last character
104 			 * read and reinitialize pointers for next round.
105 			 */
106 			(void) printf("%s", sb);
107 			(void) putc(ic, stdout);
108 
109 			sb = cb = &buffer[0];
110 			word = ignore = 0;
111 
112 			continue;
113 		}
114 
115 		/*
116 		 * Store this character into the word buffer.
117 		 */
118 		*cb++ = c;
119 		*cb = '\0';
120 		word = 1;
121 	}
122 
123 	return (0);
124 }
125