2017-10-24 23:19:19 -05:00
|
|
|
#ifndef UTIL_LINUX_CAREFULPUTC_H
|
|
|
|
#define UTIL_LINUX_CAREFULPUTC_H
|
2006-12-06 17:26:54 -06:00
|
|
|
|
2014-04-28 11:08:42 -05:00
|
|
|
/*
|
|
|
|
* A putc() for use in write and wall (that sometimes are sgid tty).
|
|
|
|
* It avoids control characters in our locale, and also ASCII control
|
|
|
|
* characters. Note that the locale of the recipient is unknown.
|
|
|
|
*/
|
2006-12-06 17:25:44 -06:00
|
|
|
#include <stdio.h>
|
2014-03-19 07:25:45 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2006-12-06 17:25:44 -06:00
|
|
|
|
2019-08-08 14:12:55 -05:00
|
|
|
#include "cctype.h"
|
|
|
|
|
2014-08-10 12:23:14 -05:00
|
|
|
static inline int fputc_careful(int c, FILE *fp, const char fail)
|
|
|
|
{
|
2006-12-06 17:25:44 -06:00
|
|
|
int ret;
|
|
|
|
|
2014-08-10 12:23:14 -05:00
|
|
|
if (isprint(c) || c == '\a' || c == '\t' || c == '\r' || c == '\n')
|
2006-12-06 17:25:44 -06:00
|
|
|
ret = putc(c, fp);
|
2019-08-08 14:12:55 -05:00
|
|
|
else if (!c_isascii(c))
|
2014-08-10 12:23:14 -05:00
|
|
|
ret = fprintf(fp, "\\%3o", (unsigned char)c);
|
2006-12-06 17:25:44 -06:00
|
|
|
else {
|
2013-08-17 13:15:11 -05:00
|
|
|
ret = putc(fail, fp);
|
2006-12-06 17:25:44 -06:00
|
|
|
if (ret != EOF)
|
2014-08-10 12:23:14 -05:00
|
|
|
ret = putc(c ^ 0x40, fp);
|
2006-12-06 17:25:44 -06:00
|
|
|
}
|
|
|
|
return (ret < 0) ? EOF : 0;
|
|
|
|
}
|
2006-12-06 17:26:54 -06:00
|
|
|
|
2016-08-02 04:58:50 -05:00
|
|
|
/*
|
|
|
|
* Requirements enumerated via testing (V8, Firefox, IE11):
|
|
|
|
*
|
|
|
|
* var charsToEscape = [];
|
|
|
|
* for (var i = 0; i < 65535; i += 1) {
|
|
|
|
* try {
|
|
|
|
* JSON.parse('{"sample": "' + String.fromCodePoint(i) + '"}');
|
|
|
|
* } catch (e) {
|
|
|
|
* charsToEscape.push(i);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
static inline void fputs_quoted_case_json(const char *data, FILE *out, int dir)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
fputc('"', out);
|
|
|
|
for (p = data; p && *p; p++) {
|
|
|
|
|
|
|
|
const unsigned char c = (unsigned char) *p;
|
|
|
|
|
|
|
|
/* From http://www.json.org
|
|
|
|
*
|
|
|
|
* The double-quote and backslashes would break out a string or
|
|
|
|
* init an escape sequence if not escaped.
|
|
|
|
*
|
|
|
|
* Note that single-quotes and forward slashes, while they're
|
|
|
|
* in the JSON spec, don't break double-quoted strings.
|
|
|
|
*/
|
|
|
|
if (c == '"' || c == '\\') {
|
|
|
|
fputc('\\', out);
|
|
|
|
fputc(c, out);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All non-control characters OK; do the case swap as required. */
|
|
|
|
if (c >= 0x20) {
|
|
|
|
fputc(dir == 1 ? toupper(c) :
|
|
|
|
dir == -1 ? tolower(c) : *p, out);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In addition, all chars under ' ' break Node's/V8/Chrome's, and
|
|
|
|
* Firefox's JSON.parse function
|
|
|
|
*/
|
|
|
|
switch (c) {
|
|
|
|
/* Handle short-hand cases to reduce output size. C
|
|
|
|
* has most of the same stuff here, so if there's an
|
|
|
|
* "Escape for C" function somewhere in the STL, we
|
|
|
|
* should probably be using it.
|
|
|
|
*/
|
|
|
|
case '\b':
|
|
|
|
fputs("\\b", out);
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
fputs("\\t", out);
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
fputs("\\n", out);
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
fputs("\\f", out);
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
fputs("\\r", out);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Other assorted control characters */
|
|
|
|
fprintf(out, "\\u00%02x", c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fputc('"', out);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-05 04:58:42 -05:00
|
|
|
static inline void fputs_quoted_case(const char *data, FILE *out, int dir)
|
2014-03-19 07:25:45 -05:00
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
fputc('"', out);
|
|
|
|
for (p = data; p && *p; p++) {
|
|
|
|
if ((unsigned char) *p == 0x22 || /* " */
|
|
|
|
(unsigned char) *p == 0x5c || /* \ */
|
2014-11-27 06:36:09 -06:00
|
|
|
(unsigned char) *p == 0x60 || /* ` */
|
|
|
|
(unsigned char) *p == 0x24 || /* $ */
|
2014-03-19 07:25:45 -05:00
|
|
|
!isprint((unsigned char) *p) ||
|
|
|
|
iscntrl((unsigned char) *p)) {
|
|
|
|
|
|
|
|
fprintf(out, "\\x%02x", (unsigned char) *p);
|
|
|
|
} else
|
2015-06-05 04:58:42 -05:00
|
|
|
fputc(dir == 1 ? toupper(*p) :
|
|
|
|
dir == -1 ? tolower(*p) :
|
|
|
|
*p, out);
|
2014-03-19 07:25:45 -05:00
|
|
|
}
|
|
|
|
fputc('"', out);
|
|
|
|
}
|
|
|
|
|
2015-06-05 04:58:42 -05:00
|
|
|
#define fputs_quoted(_d, _o) fputs_quoted_case(_d, _o, 0)
|
|
|
|
#define fputs_quoted_upper(_d, _o) fputs_quoted_case(_d, _o, 1)
|
|
|
|
#define fputs_quoted_lower(_d, _o) fputs_quoted_case(_d, _o, -1)
|
|
|
|
|
2016-08-02 04:58:50 -05:00
|
|
|
#define fputs_quoted_json(_d, _o) fputs_quoted_case_json(_d, _o, 0)
|
|
|
|
#define fputs_quoted_json_upper(_d, _o) fputs_quoted_case_json(_d, _o, 1)
|
|
|
|
#define fputs_quoted_json_lower(_d, _o) fputs_quoted_case_json(_d, _o, -1)
|
|
|
|
|
2014-03-19 07:25:45 -05:00
|
|
|
static inline void fputs_nonblank(const char *data, FILE *out)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
for (p = data; p && *p; p++) {
|
|
|
|
if (isblank((unsigned char) *p) ||
|
|
|
|
(unsigned char) *p == 0x5c || /* \ */
|
|
|
|
!isprint((unsigned char) *p) ||
|
|
|
|
iscntrl((unsigned char) *p)) {
|
|
|
|
|
|
|
|
fprintf(out, "\\x%02x", (unsigned char) *p);
|
|
|
|
|
|
|
|
} else
|
|
|
|
fputc(*p, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:14:10 -06:00
|
|
|
static inline void fputs_shell_ident(const char *data, FILE *out)
|
|
|
|
{
|
|
|
|
const char *p = data;
|
|
|
|
|
|
|
|
/* convert "1FOO" to "_1FOO" */
|
|
|
|
if (p && !isalpha(*p))
|
|
|
|
fputc('_', out);
|
|
|
|
|
|
|
|
/* replace all "bad" chars with "_" */
|
|
|
|
for (p = data; p && *p; p++) {
|
|
|
|
if (!isalnum(*p))
|
|
|
|
fputc('_', out);
|
|
|
|
else
|
|
|
|
fputc(*p, out);
|
|
|
|
}
|
|
|
|
}
|
2014-03-19 07:25:45 -05:00
|
|
|
|
2017-10-24 23:19:19 -05:00
|
|
|
#endif /* _CAREFULPUTC_H */
|