xref: /openbsd-src/usr.bin/tmux/cmd-queue.c (revision 928e56dc8f55005879bb6b5f5fece6d8de82f281)
1 /* $OpenBSD: cmd-queue.c,v 1.91 2020/04/23 05:48:42 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2013 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25 
26 #include "tmux.h"
27 
28 /* Command queue flags. */
29 #define CMDQ_FIRED 0x1
30 #define CMDQ_WAITING 0x2
31 
32 /* Command queue item type. */
33 enum cmdq_type {
34 	CMDQ_COMMAND,
35 	CMDQ_CALLBACK,
36 };
37 
38 /* Command queue item. */
39 struct cmdq_item {
40 	char			*name;
41 	struct cmdq_list	*queue;
42 	struct cmdq_item	*next;
43 
44 	struct client		*client;
45 	struct client		*target_client;
46 
47 	enum cmdq_type		 type;
48 	u_int			 group;
49 
50 	u_int			 number;
51 	time_t			 time;
52 
53 	int			 flags;
54 
55 	struct cmdq_state	*state;
56 	struct cmd_find_state	 source;
57 	struct cmd_find_state	 target;
58 
59 	struct cmd_list		*cmdlist;
60 	struct cmd		*cmd;
61 
62 	cmdq_cb			 cb;
63 	void			*data;
64 
65 	TAILQ_ENTRY(cmdq_item)	 entry;
66 };
67 TAILQ_HEAD(cmdq_item_list, cmdq_item);
68 
69 /*
70  * Command queue state. This is the context for commands on the command queue.
71  * It holds information about how the commands were fired (the key and flags),
72  * any additional formats for the commands, and the current default target.
73  * Multiple commands can share the same state and a command may update the
74  * default target.
75  */
76 struct cmdq_state {
77 	int			 references;
78 	int			 flags;
79 
80 	struct format_tree	*formats;
81 
82 	struct key_event	 event;
83 	struct cmd_find_state	 current;
84 };
85 
86 /* Command queue. */
87 struct cmdq_list {
88 	struct cmdq_item	*item;
89 	struct cmdq_item_list	 list;
90 };
91 
92 /* Get command queue name. */
93 static const char *
94 cmdq_name(struct client *c)
95 {
96 	static char	s[256];
97 
98 	if (c == NULL)
99 		return ("<global>");
100 	if (c->name != NULL)
101 		xsnprintf(s, sizeof s, "<%s>", c->name);
102 	else
103 		xsnprintf(s, sizeof s, "<%p>", c);
104 	return (s);
105 }
106 
107 /* Get command queue from client. */
108 static struct cmdq_list *
109 cmdq_get(struct client *c)
110 {
111 	static struct cmdq_list *global_queue;
112 
113 	if (c == NULL) {
114 		if (global_queue == NULL)
115 			global_queue = cmdq_new();
116 		return (global_queue);
117 	}
118 	return (c->queue);
119 }
120 
121 /* Create a queue. */
122 struct cmdq_list *
123 cmdq_new(void)
124 {
125 	struct cmdq_list	*queue;
126 
127 	queue = xcalloc (1, sizeof *queue);
128 	TAILQ_INIT (&queue->list);
129 	return (queue);
130 }
131 
132 /* Free a queue. */
133 void
134 cmdq_free(struct cmdq_list *queue)
135 {
136 	if (!TAILQ_EMPTY(&queue->list))
137 		fatalx("queue not empty");
138 	free(queue);
139 }
140 
141 /* Get item name. */
142 const char *
143 cmdq_get_name(struct cmdq_item *item)
144 {
145 	return (item->name);
146 }
147 
148 /* Get item client. */
149 struct client *
150 cmdq_get_client(struct cmdq_item *item)
151 {
152 	return (item->client);
153 }
154 
155 /* Get item target client. */
156 struct client *
157 cmdq_get_target_client(struct cmdq_item *item)
158 {
159 	return (item->target_client);
160 }
161 
162 /* Get item state. */
163 struct cmdq_state *
164 cmdq_get_state(struct cmdq_item *item)
165 {
166 	return (item->state);
167 }
168 
169 /* Get item target. */
170 struct cmd_find_state *
171 cmdq_get_target(struct cmdq_item *item)
172 {
173 	return (&item->target);
174 }
175 
176 /* Get item source. */
177 struct cmd_find_state *
178 cmdq_get_source(struct cmdq_item *item)
179 {
180 	return (&item->source);
181 }
182 
183 /* Get state event. */
184 struct key_event *
185 cmdq_get_event(struct cmdq_item *item)
186 {
187 	return (&item->state->event);
188 }
189 
190 /* Get state current target. */
191 struct cmd_find_state *
192 cmdq_get_current(struct cmdq_item *item)
193 {
194 	return (&item->state->current);
195 }
196 
197 /* Get state flags. */
198 int
199 cmdq_get_flags(struct cmdq_item *item)
200 {
201 	return (item->state->flags);
202 }
203 
204 /* Create a new state. */
205 struct cmdq_state *
206 cmdq_new_state(struct cmd_find_state *current, struct key_event *event,
207     int flags)
208 {
209 	struct cmdq_state	*state;
210 
211 	state = xcalloc(1, sizeof *state);
212 	state->references = 1;
213 	state->flags = flags;
214 
215 	if (event != NULL)
216 		memcpy(&state->event, event, sizeof state->event);
217 	else
218 		state->event.key = KEYC_NONE;
219 	if (current != NULL && cmd_find_valid_state(current))
220 		cmd_find_copy_state(&state->current, current);
221 	else
222 		cmd_find_clear_state(&state->current, 0);
223 
224 	return (state);
225 }
226 
227 /* Add a reference to a state. */
228 struct cmdq_state *
229 cmdq_link_state(struct cmdq_state *state)
230 {
231 	state->references++;
232 	return (state);
233 }
234 
235 /* Make a copy of a state. */
236 struct cmdq_state *
237 cmdq_copy_state(struct cmdq_state *state)
238 {
239 	return (cmdq_new_state(&state->current, &state->event, state->flags));
240 }
241 
242 /* Free a state. */
243 void
244 cmdq_free_state(struct cmdq_state *state)
245 {
246 	if (--state->references != 0)
247 		return;
248 
249 	if (state->formats != NULL)
250 		format_free(state->formats);
251 	free(state);
252 }
253 
254 /* Add a format to command queue. */
255 void
256 cmdq_add_format(struct cmdq_state *state, const char *key, const char *fmt, ...)
257 {
258 	va_list	 ap;
259 	char	*value;
260 
261 	va_start(ap, fmt);
262 	xvasprintf(&value, fmt, ap);
263 	va_end(ap);
264 
265 	if (state->formats == NULL)
266 		state->formats = format_create(NULL, NULL, FORMAT_NONE, 0);
267 	format_add(state->formats, key, "%s", value);
268 
269 	free(value);
270 }
271 
272 /* Merge formats from item. */
273 void
274 cmdq_merge_formats(struct cmdq_item *item, struct format_tree *ft)
275 {
276 	const struct cmd_entry	*entry;
277 
278 	if (item->cmd != NULL) {
279 		entry = cmd_get_entry (item->cmd);
280 		format_add(ft, "command", "%s", entry->name);
281 	}
282 	if (item->state->formats != NULL)
283 		format_merge(ft, item->state->formats);
284 }
285 
286 /* Append an item. */
287 struct cmdq_item *
288 cmdq_append(struct client *c, struct cmdq_item *item)
289 {
290 	struct cmdq_list	*queue = cmdq_get(c);
291 	struct cmdq_item	*next;
292 
293 	do {
294 		next = item->next;
295 		item->next = NULL;
296 
297 		if (c != NULL)
298 			c->references++;
299 		item->client = c;
300 
301 		item->queue = queue;
302 		TAILQ_INSERT_TAIL(&queue->list, item, entry);
303 		log_debug("%s %s: %s", __func__, cmdq_name(c), item->name);
304 
305 		item = next;
306 	} while (item != NULL);
307 	return (TAILQ_LAST(&queue->list, cmdq_item_list));
308 }
309 
310 /* Insert an item. */
311 struct cmdq_item *
312 cmdq_insert_after(struct cmdq_item *after, struct cmdq_item *item)
313 {
314 	struct client		*c = after->client;
315 	struct cmdq_list	*queue = after->queue;
316 	struct cmdq_item	*next;
317 
318 	do {
319 		next = item->next;
320 		item->next = after->next;
321 		after->next = item;
322 
323 		if (c != NULL)
324 			c->references++;
325 		item->client = c;
326 
327 		item->queue = queue;
328 		TAILQ_INSERT_AFTER(&queue->list, after, item, entry);
329 		log_debug("%s %s: %s after %s", __func__, cmdq_name(c),
330 		    item->name, after->name);
331 
332 		after = item;
333 		item = next;
334 	} while (item != NULL);
335 	return (after);
336 }
337 
338 /* Insert a hook. */
339 void
340 cmdq_insert_hook(struct session *s, struct cmdq_item *item,
341     struct cmd_find_state *current, const char *fmt, ...)
342 {
343 	struct cmdq_state		*state = item->state;
344 	struct options			*oo;
345 	va_list				 ap;
346 	char				*name;
347 	struct cmdq_item		*new_item;
348 	struct cmdq_state		*new_state;
349 	struct options_entry		*o;
350 	struct options_array_item	*a;
351 	struct cmd_list			*cmdlist;
352 
353 	if (item->state->flags & CMDQ_STATE_NOHOOKS)
354 		return;
355 	if (s == NULL)
356 		oo = global_s_options;
357 	else
358 		oo = s->options;
359 
360 	va_start(ap, fmt);
361 	xvasprintf(&name, fmt, ap);
362 	va_end(ap);
363 
364 	o = options_get(oo, name);
365 	if (o == NULL) {
366 		free(name);
367 		return;
368 	}
369 	log_debug("running hook %s (parent %p)", name, item);
370 
371 	/*
372 	 * The hooks get a new state because they should not update the current
373 	 * target or formats for any subsequent commands.
374 	 */
375 	new_state = cmdq_new_state(current, &state->event, CMDQ_STATE_NOHOOKS);
376 	cmdq_add_format(new_state, "hook", "%s", name);
377 
378 	a = options_array_first(o);
379 	while (a != NULL) {
380 		cmdlist = options_array_item_value(a)->cmdlist;
381 		if (cmdlist != NULL) {
382 			new_item = cmdq_get_command(cmdlist, new_state);
383 			if (item != NULL)
384 				item = cmdq_insert_after(item, new_item);
385 			else
386 				item = cmdq_append(NULL, new_item);
387 		}
388 		a = options_array_next(a);
389 	}
390 
391 	cmdq_free_state(new_state);
392 	free(name);
393 }
394 
395 /* Continue processing command queue. */
396 void
397 cmdq_continue(struct cmdq_item *item)
398 {
399 	item->flags &= ~CMDQ_WAITING;
400 }
401 
402 /* Remove an item. */
403 static void
404 cmdq_remove(struct cmdq_item *item)
405 {
406 	if (item->client != NULL)
407 		server_client_unref(item->client);
408 	if (item->cmdlist != NULL)
409 		cmd_list_free(item->cmdlist);
410 	cmdq_free_state(item->state);
411 
412 	TAILQ_REMOVE(&item->queue->list, item, entry);
413 
414 	free(item->name);
415 	free(item);
416 }
417 
418 /* Remove all subsequent items that match this item's group. */
419 static void
420 cmdq_remove_group(struct cmdq_item *item)
421 {
422 	struct cmdq_item	*this, *next;
423 
424 	if (item->group == 0)
425 		return;
426 	this = TAILQ_NEXT(item, entry);
427 	while (this != NULL) {
428 		next = TAILQ_NEXT(this, entry);
429 		if (this->group == item->group)
430 			cmdq_remove(this);
431 		this = next;
432 	}
433 }
434 
435 /* Get a command for the command queue. */
436 struct cmdq_item *
437 cmdq_get_command(struct cmd_list *cmdlist, struct cmdq_state *state)
438 {
439 	struct cmdq_item	*item, *first = NULL, *last = NULL;
440 	struct cmd		*cmd;
441 	const struct cmd_entry	*entry;
442 	int			 created = 0;
443 
444 	if (state == NULL) {
445 		state = cmdq_new_state(NULL, NULL, 0);
446 		created = 1;
447 	}
448 
449 	cmd = cmd_list_first(cmdlist);
450 	while (cmd != NULL) {
451 		entry = cmd_get_entry(cmd);
452 
453 		item = xcalloc(1, sizeof *item);
454 		xasprintf(&item->name, "[%s/%p]", entry->name, item);
455 		item->type = CMDQ_COMMAND;
456 
457 		item->group = cmd_get_group(cmd);
458 		item->state = cmdq_link_state(state);
459 
460 		item->cmdlist = cmdlist;
461 		item->cmd = cmd;
462 
463 		cmdlist->references++;
464 		log_debug("%s: %s group %u", __func__, item->name, item->group);
465 
466 		if (first == NULL)
467 			first = item;
468 		if (last != NULL)
469 			last->next = item;
470 		last = item;
471 
472 		cmd = cmd_list_next(cmd);
473 	}
474 
475 	if (created)
476 		cmdq_free_state(state);
477 	return (first);
478 }
479 
480 /* Fill in flag for a command. */
481 static enum cmd_retval
482 cmdq_find_flag(struct cmdq_item *item, struct cmd_find_state *fs,
483     const struct cmd_entry_flag *flag)
484 {
485 	const char	*value;
486 
487 	if (flag->flag == 0) {
488 		cmd_find_clear_state(fs, 0);
489 		return (CMD_RETURN_NORMAL);
490 	}
491 
492 	value = args_get(cmd_get_args(item->cmd), flag->flag);
493 	if (cmd_find_target(fs, item, value, flag->type, flag->flags) != 0) {
494 		cmd_find_clear_state(fs, 0);
495 		return (CMD_RETURN_ERROR);
496 	}
497 	return (CMD_RETURN_NORMAL);
498 }
499 
500 /* Fire command on command queue. */
501 static enum cmd_retval
502 cmdq_fire_command(struct cmdq_item *item)
503 {
504 	const char		*name = cmdq_name(item->client);
505 	struct cmdq_state	*state = item->state;
506 	struct cmd		*cmd = item->cmd;
507 	struct args		*args = cmd_get_args(cmd);
508 	const struct cmd_entry	*entry = cmd_get_entry(cmd);
509 	struct client		*tc, *saved = item->client;
510 	enum cmd_retval		 retval;
511 	struct cmd_find_state	*fsp, fs;
512 	int			 flags, quiet = 0;
513 	char			*tmp;
514 
515 	if (log_get_level() > 1) {
516 		tmp = cmd_print(cmd);
517 		log_debug("%s %s: (%u) %s", __func__, name, item->group, tmp);
518 		free(tmp);
519 	}
520 
521 	flags = !!(state->flags & CMDQ_STATE_CONTROL);
522 	cmdq_guard(item, "begin", flags);
523 
524 	if (item->client == NULL)
525 		item->client = cmd_find_client(item, NULL, 1);
526 
527 	if (entry->flags & CMD_CLIENT_CANFAIL)
528 		quiet = 1;
529 	if (entry->flags & CMD_CLIENT_CFLAG) {
530 		tc = cmd_find_client(item, args_get(args, 'c'), quiet);
531 		if (tc == NULL && !quiet) {
532 			retval = CMD_RETURN_ERROR;
533 			goto out;
534 		}
535 	} else if (entry->flags & CMD_CLIENT_TFLAG) {
536 		tc = cmd_find_client(item, args_get(args, 't'), quiet);
537 		if (tc == NULL && !quiet) {
538 			retval = CMD_RETURN_ERROR;
539 			goto out;
540 		}
541 	} else
542 		tc = cmd_find_client(item, NULL, 1);
543 	item->target_client = tc;
544 
545 	retval = cmdq_find_flag(item, &item->source, &entry->source);
546 	if (retval == CMD_RETURN_ERROR)
547 		goto out;
548 	retval = cmdq_find_flag(item, &item->target, &entry->target);
549 	if (retval == CMD_RETURN_ERROR)
550 		goto out;
551 
552 
553 	retval = entry->exec(cmd, item);
554 	if (retval == CMD_RETURN_ERROR)
555 		goto out;
556 
557 	if (entry->flags & CMD_AFTERHOOK) {
558 		if (cmd_find_valid_state(&item->target))
559 			fsp = &item->target;
560 		else if (cmd_find_valid_state(&item->state->current))
561 			fsp = &item->state->current;
562 		else if (cmd_find_from_client(&fs, item->client, 0) == 0)
563 			fsp = &fs;
564 		else
565 			goto out;
566 		cmdq_insert_hook(fsp->s, item, fsp, "after-%s", entry->name);
567 	}
568 
569 out:
570 	item->client = saved;
571 	if (retval == CMD_RETURN_ERROR)
572 		cmdq_guard(item, "error", flags);
573 	else
574 		cmdq_guard(item, "end", flags);
575 	return (retval);
576 }
577 
578 /* Get a callback for the command queue. */
579 struct cmdq_item *
580 cmdq_get_callback1(const char *name, cmdq_cb cb, void *data)
581 {
582 	struct cmdq_item	*item;
583 
584 	item = xcalloc(1, sizeof *item);
585 	xasprintf(&item->name, "[%s/%p]", name, item);
586 	item->type = CMDQ_CALLBACK;
587 
588 	item->group = 0;
589 	item->state = cmdq_new_state(NULL, NULL, 0);
590 
591 	item->cb = cb;
592 	item->data = data;
593 
594 	return (item);
595 }
596 
597 /* Generic error callback. */
598 static enum cmd_retval
599 cmdq_error_callback(struct cmdq_item *item, void *data)
600 {
601 	char	*error = data;
602 
603 	cmdq_error(item, "%s", error);
604 	free(error);
605 
606 	return (CMD_RETURN_NORMAL);
607 }
608 
609 /* Get an error callback for the command queue. */
610 struct cmdq_item *
611 cmdq_get_error(const char *error)
612 {
613 	return (cmdq_get_callback(cmdq_error_callback, xstrdup(error)));
614 }
615 
616 /* Fire callback on callback queue. */
617 static enum cmd_retval
618 cmdq_fire_callback(struct cmdq_item *item)
619 {
620 	return (item->cb(item, item->data));
621 }
622 
623 /* Process next item on command queue. */
624 u_int
625 cmdq_next(struct client *c)
626 {
627 	struct cmdq_list	*queue = cmdq_get(c);
628 	const char		*name = cmdq_name(c);
629 	struct cmdq_item	*item;
630 	enum cmd_retval		 retval;
631 	u_int			 items = 0;
632 	static u_int		 number;
633 
634 	if (TAILQ_EMPTY(&queue->list)) {
635 		log_debug("%s %s: empty", __func__, name);
636 		return (0);
637 	}
638 	if (TAILQ_FIRST(&queue->list)->flags & CMDQ_WAITING) {
639 		log_debug("%s %s: waiting", __func__, name);
640 		return (0);
641 	}
642 
643 	log_debug("%s %s: enter", __func__, name);
644 	for (;;) {
645 		item = queue->item = TAILQ_FIRST(&queue->list);
646 		if (item == NULL)
647 			break;
648 		log_debug("%s %s: %s (%d), flags %x", __func__, name,
649 		    item->name, item->type, item->flags);
650 
651 		/*
652 		 * Any item with the waiting flag set waits until an external
653 		 * event clears the flag (for example, a job - look at
654 		 * run-shell).
655 		 */
656 		if (item->flags & CMDQ_WAITING)
657 			goto waiting;
658 
659 		/*
660 		 * Items are only fired once, once the fired flag is set, a
661 		 * waiting flag can only be cleared by an external event.
662 		 */
663 		if (~item->flags & CMDQ_FIRED) {
664 			item->time = time(NULL);
665 			item->number = ++number;
666 
667 			switch (item->type) {
668 			case CMDQ_COMMAND:
669 				retval = cmdq_fire_command(item);
670 
671 				/*
672 				 * If a command returns an error, remove any
673 				 * subsequent commands in the same group.
674 				 */
675 				if (retval == CMD_RETURN_ERROR)
676 					cmdq_remove_group(item);
677 				break;
678 			case CMDQ_CALLBACK:
679 				retval = cmdq_fire_callback(item);
680 				break;
681 			default:
682 				retval = CMD_RETURN_ERROR;
683 				break;
684 			}
685 			item->flags |= CMDQ_FIRED;
686 
687 			if (retval == CMD_RETURN_WAIT) {
688 				item->flags |= CMDQ_WAITING;
689 				goto waiting;
690 			}
691 			items++;
692 		}
693 		cmdq_remove(item);
694 	}
695 	queue->item = NULL;
696 
697 	log_debug("%s %s: exit (empty)", __func__, name);
698 	return (items);
699 
700 waiting:
701 	log_debug("%s %s: exit (wait)", __func__, name);
702 	return (items);
703 }
704 
705 /* Get running item if any. */
706 struct cmdq_item *
707 cmdq_running(struct client *c)
708 {
709 	struct cmdq_list	*queue = cmdq_get(c);
710 
711 	return (queue->item);
712 }
713 
714 /* Print a guard line. */
715 void
716 cmdq_guard(struct cmdq_item *item, const char *guard, int flags)
717 {
718 	struct client	*c = item->client;
719 	long		 t = item->time;
720 	u_int		 number = item->number;
721 
722 	if (c != NULL && (c->flags & CLIENT_CONTROL))
723 		file_print(c, "%%%s %ld %u %d\n", guard, t, number, flags);
724 }
725 
726 /* Show message from command. */
727 void
728 cmdq_print(struct cmdq_item *item, const char *fmt, ...)
729 {
730 	struct client			*c = item->client;
731 	struct window_pane		*wp;
732 	struct window_mode_entry	*wme;
733 	va_list				 ap;
734 	char				*tmp, *msg;
735 
736 	va_start(ap, fmt);
737 	xvasprintf(&msg, fmt, ap);
738 	va_end(ap);
739 
740 	log_debug("%s: %s", __func__, msg);
741 
742 	if (c == NULL)
743 		/* nothing */;
744 	else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
745 		if (~c->flags & CLIENT_UTF8) {
746 			tmp = msg;
747 			msg = utf8_sanitize(tmp);
748 			free(tmp);
749 		}
750 		file_print(c, "%s\n", msg);
751 	} else {
752 		wp = c->session->curw->window->active;
753 		wme = TAILQ_FIRST(&wp->modes);
754 		if (wme == NULL || wme->mode != &window_view_mode) {
755 			window_pane_set_mode(wp, NULL, &window_view_mode, NULL,
756 			    NULL);
757 		}
758 		window_copy_add(wp, "%s", msg);
759 	}
760 
761 	free(msg);
762 }
763 
764 /* Show error from command. */
765 void
766 cmdq_error(struct cmdq_item *item, const char *fmt, ...)
767 {
768 	struct client	*c = item->client;
769 	struct cmd	*cmd = item->cmd;
770 	va_list		 ap;
771 	char		*msg, *tmp;
772 	const char	*file;
773 	u_int		 line;
774 
775 	va_start(ap, fmt);
776 	xvasprintf(&msg, fmt, ap);
777 	va_end(ap);
778 
779 	log_debug("%s: %s", __func__, msg);
780 
781 	if (c == NULL) {
782 		cmd_get_source(cmd, &file, &line);
783 		cfg_add_cause("%s:%u: %s", file, line, msg);
784 	} else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
785 		if (~c->flags & CLIENT_UTF8) {
786 			tmp = msg;
787 			msg = utf8_sanitize(tmp);
788 			free(tmp);
789 		}
790 		if (c->flags & CLIENT_CONTROL)
791 			file_print(c, "%s\n", msg);
792 		else
793 			file_error(c, "%s\n", msg);
794 		c->retval = 1;
795 	} else {
796 		*msg = toupper((u_char) *msg);
797 		status_message_set(c, "%s", msg);
798 	}
799 
800 	free(msg);
801 }
802