xref: /original-bsd/games/battlestar/getcom.c (revision e188a54c)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)getcom.c	5.2 (Berkeley) 06/19/88";
20 #endif /* not lint */
21 
22 #include <stdio.h>
23 #include <ctype.h>
24 
25 char *
26 getcom(buf, size, prompt, error)
27 	char *buf;
28 	int size;
29 	char *prompt, *error;
30 {
31 	for (;;) {
32 		fputs(prompt, stdout);
33 		if (fgets(buf, size, stdin) == 0) {
34 			clearerr(stdin);
35 			continue;
36 		}
37 		while (isspace(*buf))
38 			buf++;
39 		if (*buf)
40 			break;
41 		if (error)
42 			puts(error);
43 	}
44 	return (buf);
45 }
46 
47 
48 /*
49  * shifts to UPPERCASE if flag > 0, lowercase if flag < 0,
50  * and leaves it unchanged if flag = 0
51  */
52 char *
53 getword(buf1, buf2, flag)
54 	register char *buf1, *buf2;
55 	register flag;
56 {
57 	while (isspace(*buf1))
58 		buf1++;
59 	if (*buf1 != ',') {
60 		if (!*buf1) {
61 			*buf2 = 0;
62 			return (0);
63 		}
64 		while (*buf1 && !isspace(*buf1) && *buf1 != ',')
65 			if (flag < 0)
66 				if (isupper(*buf1))
67 					*buf2++ = tolower(*buf1++);
68 				else
69 					*buf2++ = *buf1++;
70 			else if (flag > 0)
71 				if (islower(*buf1))
72 					*buf2++ = toupper(*buf1++);
73 				else
74 					*buf2++ = *buf1++;
75 			else
76 				*buf2++ = *buf1++;
77 	} else
78 		*buf2++ = *buf1++;
79 	*buf2 = 0;
80 	while (isspace(*buf1))
81 		buf1++;
82 	return (*buf1 ? buf1 : 0);
83 }
84