]> git.pond.sub.org Git - empserver/blob - src/lib/subs/pr.c
Cleanup #includes of (mostly a long time) unused header files.
[empserver] / src / lib / subs / pr.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  pr.c: Use to do output to a player
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986, 1989 
32  *     Steve McClure, 1998-2000
33  */
34 /*
35  * The pr routine historically arranged for nonbuffered i/o
36  * because stdio didn't used to automatically flush stdout before
37  * it read something from stdin.  Now pr() prepends an "output id"
38  * in front of each line of text, informing the user interface
39  * what sort of item it is seeing; prompt, noecho prompt,
40  * more input data, etc.
41  */
42
43 #include <string.h>
44 #include <fcntl.h>
45 #include <ctype.h>
46 #include <stdarg.h>
47 #include "proto.h"
48 #include "misc.h"
49 #include "player.h"
50 #include "nat.h"
51 #include "empio.h"
52 #include "file.h"
53 #include "com.h"
54 #include "tel.h"
55 #include "server.h"
56 #include "prototypes.h"
57
58 static void outid(struct player *pl, int n);
59
60 /*VARARGS*/
61 void
62 pr(s_char *format, ...)
63 {
64     s_char buf[4096];
65     va_list ap;
66
67     va_start(ap, format);
68     (void)vsprintf(buf, format, ap);
69     va_end(ap);
70     pr_player(player, C_DATA, buf);
71 }
72
73 void
74 prnf(s_char *buf)
75 {
76     pr_player(player, C_DATA, buf);
77 }
78
79 /*VARARGS*/
80 void
81 pr_id(struct player *p, int id, s_char *format, ...)
82 {
83     s_char buf[4096];
84     va_list ap;
85
86     if (p->curid >= 0) {
87         io_puts(p->iop, "\n");
88         p->curid = -1;
89     }
90     va_start(ap, format);
91     (void)vsprintf(buf, format, ap);
92     va_end(ap);
93     pr_player(p, id, buf);
94 }
95
96 void
97 pr_flash(struct player *pl, s_char *format, ...)
98 {
99     s_char buf[4096];
100     va_list ap;
101
102     if (pl->state != PS_PLAYING)
103         return;
104     va_start(ap, format);
105     (void)vsprintf(buf, format, ap);
106     va_end(ap);
107     pr_player(pl, C_FLASH, buf);
108     io_output(pl->iop, IO_NOWAIT);
109 }
110
111 void
112 pr_inform(struct player *pl, s_char *format, ...)
113 {
114     s_char buf[4096];
115     va_list ap;
116
117     if (pl->state != PS_PLAYING)
118         return;
119     va_start(ap, format);
120     (void)vsprintf(buf, format, ap);
121     va_end(ap);
122     pr_player(pl, C_INFORM, buf);
123     io_output(pl->iop, IO_NOWAIT);
124 }
125
126 void
127 pr_wall(s_char *format, ...)
128 {
129     s_char buf[4096];
130     struct player *p;
131     va_list ap;
132
133     va_start(ap, format);
134     (void)vsprintf(buf, format, ap);
135     va_end(ap);
136     for (p = player_next(0); p; p = player_next(p)) {
137         if (p->state != PS_PLAYING)
138             continue;
139         pr_player(p, C_FLASH, buf);
140         io_output(p->iop, IO_NOWAIT);
141     }
142 }
143
144 void
145 pr_player(struct player *pl, int id, s_char *buf)
146 {
147     register s_char *p;
148     register s_char *bp;
149     register int len;
150
151     bp = buf;
152     while (*bp != '\0') {
153         if (pl->curid != -1 && pl->curid != id) {
154             io_puts(pl->iop, "\n");
155             pl->curid = -1;
156         }
157         if (pl->curid == -1) {
158             outid(pl, id);
159         }
160         p = strchr(bp, '\n');
161         if (p != 0) {
162             len = (p - bp) + 1;
163             if (pl->command && (pl->command->c_flags & C_MOD))
164                 io_write(pl->iop, bp, len, IO_NOWAIT);
165             else
166                 io_write(pl->iop, bp, len, IO_WAIT);
167             bp += len;
168             pl->curid = -1;
169         } else {
170             len = io_puts(pl->iop, bp);
171             bp += len;
172         }
173     }
174 }
175
176 /*
177  * highlighted characters have hex 80 or'ed in
178  * with them to designate their highlightedness
179  */
180 void
181 pr_hilite(s_char *buf)
182 {
183     register s_char *bp;
184     register s_char c;
185     s_char *p;
186
187     p = (s_char *)malloc(strlen(buf) + 1);
188     strcpy(p, buf);
189     for (bp = p; 0 != (c = *bp); bp++)
190         if (isprint(c))
191             *bp |= 0x80;
192     pr(p);
193     free(p);
194 }
195
196 /*
197  * output hex code + space
198  */
199 static void
200 outid(struct player *pl, int n)
201 {
202     s_char c;
203     s_char buf[3];
204
205     if (n > C_LAST) {
206         logerror("outid: %d not valid code\n", n);
207         return;
208     }
209     if (n >= 10)
210         c = 'a' - 10 + n;
211     else
212         c = '0' + n;
213     buf[0] = c;
214     buf[1] = ' ';
215     buf[2] = '\0';
216     io_puts(pl->iop, buf);
217     pl->curid = n;
218 }
219
220 void
221 prredir(s_char *redir)
222 {
223     pr_id(player, *redir == '>' ? C_REDIR : C_PIPE, "%s\n", redir);
224 }
225
226 void
227 prexec(s_char *file)
228 {
229     pr_id(player, C_EXECUTE, "%s\n", file);
230 }
231
232 void
233 prprompt(int min, int btu)
234 {
235     pr_id(player, C_PROMPT, "%d %d\n", min, btu);
236 }
237
238 void
239 showvers(int vers)
240 {
241     pr_id(player, C_INIT, "%d\n", vers);
242 }
243
244 int
245 prmptrd(s_char *prompt, s_char *str, int size)
246 {
247     int r;
248
249     pr_id(player, C_FLUSH, "%s\n", prompt);
250     if ((r = recvclient(str, size)) < 0)
251         return r;
252     time(&player->curup);
253     if (*str == 0)
254         return 1;
255     return strlen(str);
256 }
257
258 void
259 prdate(void)
260 {
261     time_t now;
262
263     (void)time(&now);
264     pr(ctime(&now));
265 }
266
267 /*
268  * print x,y formatting as country
269  */
270 void
271 prxy(s_char *format, coord x, coord y, natid country)
272 {
273     s_char buf[255];
274     struct natstr *np;
275
276     np = getnatp(country);
277     sprintf(buf, format, xrel(np, x), yrel(np, y));
278     pr(buf);
279 }
280
281 /*VARARGS*/
282 void
283 PR(int cn, s_char *format, ...)
284 {
285     /* XXX should really do this on a per-nation basis */
286     static s_char longline[MAXNOC][512];
287     int newline;
288     va_list ap;
289     s_char buf[1024];
290
291     va_start(ap, format);
292     (void)vsprintf(buf, format, ap);
293     va_end(ap);
294     newline = strrchr(buf, '\n') ? 1 : 0;
295     strcat(longline[cn], buf);
296     if (newline) {
297         if (update_pending || (cn && cn != player->cnum))
298             typed_wu(0, cn, longline[cn], TEL_BULLETIN);
299         else
300             pr_player(player, C_DATA, longline[cn]);
301         longline[cn][0] = '\0';
302     }
303 }
304
305 void
306 PRdate(natid cn)
307 {
308     time_t now;
309
310     (void)time(&now);
311     PR(cn, ctime(&now));
312 }
313
314 void
315 pr_beep(void)
316 {
317     struct natstr *np = getnatp(player->cnum);
318
319     if (np->nat_flags & NF_BEEP)
320         pr("\07");
321 }
322
323 void
324 mpr(int cn, s_char *format, ...)
325 {
326     s_char buf[4096];
327     va_list ap;
328
329     va_start(ap, format);
330     (void)vsprintf(buf, format, ap);
331     va_end(ap);
332     if (cn) {
333         if (update_pending || cn != player->cnum)
334             typed_wu(0, cn, buf, TEL_BULLETIN);
335         else
336             pr_player(player, C_DATA, buf);
337     }
338 }