xref: /original-bsd/games/battlestar/getcom.c (revision 89a39cb6)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)getcom.c	5.3 (Berkeley) 06/01/90";
10 #endif /* not lint */
11 
12 #include <stdio.h>
13 #include <ctype.h>
14 
15 char *
16 getcom(buf, size, prompt, error)
17 	char *buf;
18 	int size;
19 	char *prompt, *error;
20 {
21 	for (;;) {
22 		fputs(prompt, stdout);
23 		if (fgets(buf, size, stdin) == 0) {
24 			clearerr(stdin);
25 			continue;
26 		}
27 		while (isspace(*buf))
28 			buf++;
29 		if (*buf)
30 			break;
31 		if (error)
32 			puts(error);
33 	}
34 	return (buf);
35 }
36 
37 
38 /*
39  * shifts to UPPERCASE if flag > 0, lowercase if flag < 0,
40  * and leaves it unchanged if flag = 0
41  */
42 char *
43 getword(buf1, buf2, flag)
44 	register char *buf1, *buf2;
45 	register flag;
46 {
47 	while (isspace(*buf1))
48 		buf1++;
49 	if (*buf1 != ',') {
50 		if (!*buf1) {
51 			*buf2 = 0;
52 			return (0);
53 		}
54 		while (*buf1 && !isspace(*buf1) && *buf1 != ',')
55 			if (flag < 0)
56 				if (isupper(*buf1))
57 					*buf2++ = tolower(*buf1++);
58 				else
59 					*buf2++ = *buf1++;
60 			else if (flag > 0)
61 				if (islower(*buf1))
62 					*buf2++ = toupper(*buf1++);
63 				else
64 					*buf2++ = *buf1++;
65 			else
66 				*buf2++ = *buf1++;
67 	} else
68 		*buf2++ = *buf1++;
69 	*buf2 = 0;
70 	while (isspace(*buf1))
71 		buf1++;
72 	return (*buf1 ? buf1 : 0);
73 }
74