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