1*402f338eSThomas Cort /* $NetBSD: pig.c,v 1.15 2012/06/19 05:46:09 dholland Exp $ */
2*402f338eSThomas Cort
3*402f338eSThomas Cort /*-
4*402f338eSThomas Cort * Copyright (c) 1992, 1993
5*402f338eSThomas Cort * The Regents of the University of California. All rights reserved.
6*402f338eSThomas Cort *
7*402f338eSThomas Cort * Redistribution and use in source and binary forms, with or without
8*402f338eSThomas Cort * modification, are permitted provided that the following conditions
9*402f338eSThomas Cort * are met:
10*402f338eSThomas Cort * 1. Redistributions of source code must retain the above copyright
11*402f338eSThomas Cort * notice, this list of conditions and the following disclaimer.
12*402f338eSThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
13*402f338eSThomas Cort * notice, this list of conditions and the following disclaimer in the
14*402f338eSThomas Cort * documentation and/or other materials provided with the distribution.
15*402f338eSThomas Cort * 3. Neither the name of the University nor the names of its contributors
16*402f338eSThomas Cort * may be used to endorse or promote products derived from this software
17*402f338eSThomas Cort * without specific prior written permission.
18*402f338eSThomas Cort *
19*402f338eSThomas Cort * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*402f338eSThomas Cort * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*402f338eSThomas Cort * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*402f338eSThomas Cort * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*402f338eSThomas Cort * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*402f338eSThomas Cort * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*402f338eSThomas Cort * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*402f338eSThomas Cort * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*402f338eSThomas Cort * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*402f338eSThomas Cort * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*402f338eSThomas Cort * SUCH DAMAGE.
30*402f338eSThomas Cort */
31*402f338eSThomas Cort
32*402f338eSThomas Cort #include <sys/cdefs.h>
33*402f338eSThomas Cort #ifndef lint
34*402f338eSThomas Cort __COPYRIGHT("@(#) Copyright (c) 1992, 1993\
35*402f338eSThomas Cort The Regents of the University of California. All rights reserved.");
36*402f338eSThomas Cort #endif /* not lint */
37*402f338eSThomas Cort
38*402f338eSThomas Cort #ifndef lint
39*402f338eSThomas Cort #if 0
40*402f338eSThomas Cort static char sccsid[] = "@(#)pig.c 8.2 (Berkeley) 5/4/95";
41*402f338eSThomas Cort #else
42*402f338eSThomas Cort __RCSID("$NetBSD: pig.c,v 1.15 2012/06/19 05:46:09 dholland Exp $");
43*402f338eSThomas Cort #endif
44*402f338eSThomas Cort #endif /* not lint */
45*402f338eSThomas Cort
46*402f338eSThomas Cort #include <sys/types.h>
47*402f338eSThomas Cort
48*402f338eSThomas Cort #include <ctype.h>
49*402f338eSThomas Cort #include <err.h>
50*402f338eSThomas Cort #include <stdio.h>
51*402f338eSThomas Cort #include <stdlib.h>
52*402f338eSThomas Cort #include <string.h>
53*402f338eSThomas Cort #include <unistd.h>
54*402f338eSThomas Cort
55*402f338eSThomas Cort int main(int, char *[]);
56*402f338eSThomas Cort static void pigout(char *, int);
57*402f338eSThomas Cort static void usage(void) __dead;
58*402f338eSThomas Cort
59*402f338eSThomas Cort int
main(int argc,char * argv[])60*402f338eSThomas Cort main(int argc, char *argv[])
61*402f338eSThomas Cort {
62*402f338eSThomas Cort int len;
63*402f338eSThomas Cort int ch;
64*402f338eSThomas Cort char buf[1024];
65*402f338eSThomas Cort
66*402f338eSThomas Cort while ((ch = getopt(argc, argv, "")) != -1)
67*402f338eSThomas Cort switch(ch) {
68*402f338eSThomas Cort case '?':
69*402f338eSThomas Cort default:
70*402f338eSThomas Cort usage();
71*402f338eSThomas Cort }
72*402f338eSThomas Cort argc -= optind;
73*402f338eSThomas Cort argv += optind;
74*402f338eSThomas Cort
75*402f338eSThomas Cort for (len = 0; (ch = getchar()) != EOF;) {
76*402f338eSThomas Cort if (isalpha(ch)) {
77*402f338eSThomas Cort if ((size_t)len >= sizeof(buf))
78*402f338eSThomas Cort errx(1, "ate too much!");
79*402f338eSThomas Cort buf[len++] = ch;
80*402f338eSThomas Cort continue;
81*402f338eSThomas Cort }
82*402f338eSThomas Cort if (len != 0) {
83*402f338eSThomas Cort pigout(buf, len);
84*402f338eSThomas Cort len = 0;
85*402f338eSThomas Cort }
86*402f338eSThomas Cort (void)putchar(ch);
87*402f338eSThomas Cort }
88*402f338eSThomas Cort exit(0);
89*402f338eSThomas Cort }
90*402f338eSThomas Cort
91*402f338eSThomas Cort static void
pigout(char * buf,int len)92*402f338eSThomas Cort pigout(char *buf, int len)
93*402f338eSThomas Cort {
94*402f338eSThomas Cort int ch, start, i;
95*402f338eSThomas Cort int olen, allupper, firstupper;
96*402f338eSThomas Cort
97*402f338eSThomas Cort /* See if the word is all upper case */
98*402f338eSThomas Cort allupper = firstupper = isupper((unsigned char)buf[0]);
99*402f338eSThomas Cort for (i = 1; i < len && allupper; i++)
100*402f338eSThomas Cort allupper = allupper && isupper((unsigned char)buf[i]);
101*402f338eSThomas Cort
102*402f338eSThomas Cort /*
103*402f338eSThomas Cort * If the word starts with a vowel, append "way". Don't treat 'y'
104*402f338eSThomas Cort * as a vowel if it appears first.
105*402f338eSThomas Cort */
106*402f338eSThomas Cort if (strchr("aeiouAEIOU", buf[0]) != NULL) {
107*402f338eSThomas Cort (void)printf("%.*s%s", len, buf,
108*402f338eSThomas Cort allupper ? "WAY" : "way");
109*402f338eSThomas Cort return;
110*402f338eSThomas Cort }
111*402f338eSThomas Cort
112*402f338eSThomas Cort /*
113*402f338eSThomas Cort * Copy leading consonants to the end of the word. The unit "qu"
114*402f338eSThomas Cort * isn't treated as a vowel.
115*402f338eSThomas Cort */
116*402f338eSThomas Cort if (!allupper)
117*402f338eSThomas Cort buf[0] = tolower((unsigned char)buf[0]);
118*402f338eSThomas Cort for (start = 0, olen = len;
119*402f338eSThomas Cort !strchr("aeiouyAEIOUY", buf[start]) && start < olen;) {
120*402f338eSThomas Cort ch = buf[len++] = buf[start++];
121*402f338eSThomas Cort if ((ch == 'q' || ch == 'Q') && start < olen &&
122*402f338eSThomas Cort (buf[start] == 'u' || buf[start] == 'U'))
123*402f338eSThomas Cort buf[len++] = buf[start++];
124*402f338eSThomas Cort }
125*402f338eSThomas Cort if (firstupper)
126*402f338eSThomas Cort buf[start] = toupper((unsigned char)buf[start]);
127*402f338eSThomas Cort (void)printf("%.*s%s", olen, buf + start, allupper ? "AY" : "ay");
128*402f338eSThomas Cort }
129*402f338eSThomas Cort
130*402f338eSThomas Cort static void
usage(void)131*402f338eSThomas Cort usage(void)
132*402f338eSThomas Cort {
133*402f338eSThomas Cort (void)fprintf(stderr, "usage: pig\n");
134*402f338eSThomas Cort exit(1);
135*402f338eSThomas Cort }
136