xref: /dflybsd-src/lib/libc/stdio/xprintf_quote.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /*-
286d7f5d3SJohn Marino  * Copyright (c) 2005 Poul-Henning Kamp
386d7f5d3SJohn Marino  * All rights reserved.
486d7f5d3SJohn Marino  *
586d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
686d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
786d7f5d3SJohn Marino  * are met:
886d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
986d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
1086d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
1186d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
1286d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
1386d7f5d3SJohn Marino  *
1486d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1586d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1686d7f5d3SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1786d7f5d3SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1886d7f5d3SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1986d7f5d3SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2086d7f5d3SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2186d7f5d3SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2286d7f5d3SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2386d7f5d3SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2486d7f5d3SJohn Marino  * SUCH DAMAGE.
2586d7f5d3SJohn Marino  *
2686d7f5d3SJohn Marino  * $FreeBSD: src/lib/libc/stdio/xprintf_quote.c,v 1.2 2006/03/02 08:53:45 phk Exp $
2786d7f5d3SJohn Marino  */
2886d7f5d3SJohn Marino 
2986d7f5d3SJohn Marino #include "namespace.h"
3086d7f5d3SJohn Marino #include <stdio.h>
3186d7f5d3SJohn Marino #include <string.h>
3286d7f5d3SJohn Marino #include <stdlib.h>
3386d7f5d3SJohn Marino #include <ctype.h>
3486d7f5d3SJohn Marino #include <wchar.h>
3586d7f5d3SJohn Marino #include <vis.h>
3686d7f5d3SJohn Marino #include <assert.h>
3786d7f5d3SJohn Marino #include <sys/time.h>
3886d7f5d3SJohn Marino #include "un-namespace.h"
3986d7f5d3SJohn Marino #include "printf.h"
4086d7f5d3SJohn Marino 
4186d7f5d3SJohn Marino int
__printf_arginfo_quote(const struct printf_info * pi __unused,size_t n,int * argt)4286d7f5d3SJohn Marino __printf_arginfo_quote(const struct printf_info *pi __unused, size_t n,
4386d7f5d3SJohn Marino 		       int *argt)
4486d7f5d3SJohn Marino {
4586d7f5d3SJohn Marino 
4686d7f5d3SJohn Marino 	assert(n >= 1);
4786d7f5d3SJohn Marino 	argt[0] = PA_POINTER;
4886d7f5d3SJohn Marino 	return (1);
4986d7f5d3SJohn Marino }
5086d7f5d3SJohn Marino 
5186d7f5d3SJohn Marino int
__printf_render_quote(struct __printf_io * io,const struct printf_info * pi __unused,const void * const * arg)5286d7f5d3SJohn Marino __printf_render_quote(struct __printf_io *io,
5386d7f5d3SJohn Marino 		      const struct printf_info *pi __unused,
5486d7f5d3SJohn Marino 		      const void *const *arg)
5586d7f5d3SJohn Marino {
5686d7f5d3SJohn Marino 	const char *str, *p, *t, *o;
5786d7f5d3SJohn Marino 	char r[5];
5886d7f5d3SJohn Marino 	int i, ret;
5986d7f5d3SJohn Marino 
6086d7f5d3SJohn Marino 	str = *((const char *const *)arg[0]);
6186d7f5d3SJohn Marino 	if (str == NULL)
6286d7f5d3SJohn Marino 		return (__printf_out(io, pi, "\"(null)\"", 8));
6386d7f5d3SJohn Marino 	if (*str == '\0')
6486d7f5d3SJohn Marino 		return (__printf_out(io, pi, "\"\"", 2));
6586d7f5d3SJohn Marino 
6686d7f5d3SJohn Marino 	for (i = 0, p = str; *p; p++)
6786d7f5d3SJohn Marino 		if (isspace(*p) || *p == '\\' || *p == '"')
6886d7f5d3SJohn Marino 			i++;
6986d7f5d3SJohn Marino 	if (!i)
7086d7f5d3SJohn Marino 		return (__printf_out(io, pi, str, strlen(str)));
7186d7f5d3SJohn Marino 
7286d7f5d3SJohn Marino 	ret = __printf_out(io, pi, "\"", 1);
7386d7f5d3SJohn Marino 	for (t = p = str; *p; p++) {
7486d7f5d3SJohn Marino 		o = NULL;
7586d7f5d3SJohn Marino 		if (*p == '\\')
7686d7f5d3SJohn Marino 			o = "\\\\";
7786d7f5d3SJohn Marino 		else if (*p == '\n')
7886d7f5d3SJohn Marino 			o = "\\n";
7986d7f5d3SJohn Marino 		else if (*p == '\r')
8086d7f5d3SJohn Marino 			o = "\\r";
8186d7f5d3SJohn Marino 		else if (*p == '\t')
8286d7f5d3SJohn Marino 			o = "\\t";
8386d7f5d3SJohn Marino 		else if (*p == ' ')
8486d7f5d3SJohn Marino 			o = " ";
8586d7f5d3SJohn Marino 		else if (*p == '"')
8686d7f5d3SJohn Marino 			o = "\\\"";
8786d7f5d3SJohn Marino 		else if (isspace(*p)) {
8886d7f5d3SJohn Marino 			sprintf(r, "\\%03o", *p);
8986d7f5d3SJohn Marino 			o = r;
9086d7f5d3SJohn Marino 		} else {
9186d7f5d3SJohn Marino 			continue;
9286d7f5d3SJohn Marino 		}
9386d7f5d3SJohn Marino 		if (p != t)
9486d7f5d3SJohn Marino 			ret += __printf_out(io, pi, t, p - t);
9586d7f5d3SJohn Marino 		ret += __printf_out(io, pi, o, strlen(o));
9686d7f5d3SJohn Marino 		t = p + 1;
9786d7f5d3SJohn Marino 	}
9886d7f5d3SJohn Marino 	if (p != t)
9986d7f5d3SJohn Marino 		ret += __printf_out(io, pi, t, p - t);
10086d7f5d3SJohn Marino 	ret += __printf_out(io, pi, "\"", 1);
10186d7f5d3SJohn Marino 	__printf_flush(io);
10286d7f5d3SJohn Marino 	return(ret);
10386d7f5d3SJohn Marino }
104