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