mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-03 11:12:27 +00:00
Add minor improvements and cleanup
This commit is contained in:
parent
9e3e985ae5
commit
feed0d2b0e
163 changed files with 2286 additions and 2245 deletions
118
libc/str/escapedos.c
Normal file
118
libc/str/escapedos.c
Normal file
|
@ -0,0 +1,118 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/str/str.h"
|
||||
|
||||
static textwindows bool shouldescapedos(const char16_t c) {
|
||||
if (c == u'"') return true;
|
||||
if (c == u'&') return true;
|
||||
if (c == u'%') return true;
|
||||
if (c == u'^') return true;
|
||||
if (c == u'<') return true;
|
||||
if (c == u'>') return true;
|
||||
if (c == u'|') return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
static textwindows bool shouldquotedos(const char16_t c) {
|
||||
if (c == u' ') return true;
|
||||
if (c == u'\t') return true;
|
||||
if (c == u'\n') return true;
|
||||
if (c == u'\v') return true;
|
||||
if (c == u'"') return true;
|
||||
if (c == u'*') return true;
|
||||
return shouldescapedos(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes command so DOS can run it.
|
||||
* @see Iain Patterson's NSSM for original code in public domain
|
||||
*/
|
||||
textwindows bool escapedos(char16_t *buffer, unsigned buflen,
|
||||
const char16_t *unquoted, unsigned len) {
|
||||
unsigned i, j, n;
|
||||
if (len > buflen - 1) return false;
|
||||
bool escape = false;
|
||||
bool quotes = false;
|
||||
for (i = 0; i < len; i++) {
|
||||
if (shouldescapedos(unquoted[i])) {
|
||||
escape = quotes = true;
|
||||
break;
|
||||
}
|
||||
if (shouldquotedos(unquoted[i])) quotes = true;
|
||||
}
|
||||
if (!quotes) {
|
||||
memmove(buffer, unquoted, (len + 1) * sizeof(char16_t));
|
||||
return true;
|
||||
}
|
||||
/* "" */
|
||||
unsigned quoted_len = 2;
|
||||
if (escape) quoted_len += 2;
|
||||
for (i = 0;; i++) {
|
||||
n = 0;
|
||||
while (i != len && unquoted[i] == u'\\') {
|
||||
i++;
|
||||
n++;
|
||||
}
|
||||
if (i == len) {
|
||||
quoted_len += n * 2;
|
||||
break;
|
||||
} else if (unquoted[i] == u'"')
|
||||
quoted_len += n * 2 + 2;
|
||||
else
|
||||
quoted_len += n + 1;
|
||||
if (shouldescapedos(unquoted[i])) quoted_len += n;
|
||||
}
|
||||
if (quoted_len > buflen - 1) return false;
|
||||
char16_t *s = buffer;
|
||||
if (escape) *s++ = u'^';
|
||||
*s++ = u'"';
|
||||
for (i = 0;; i++) {
|
||||
n = 0;
|
||||
while (i != len && unquoted[i] == u'\\') {
|
||||
i++;
|
||||
n++;
|
||||
}
|
||||
if (i == len) {
|
||||
for (j = 0; j < n * 2; j++) {
|
||||
if (escape) *s++ = u'^';
|
||||
*s++ = u'\\';
|
||||
}
|
||||
break;
|
||||
} else if (unquoted[i] == u'"') {
|
||||
for (j = 0; j < n * 2 + 1; j++) {
|
||||
if (escape) *s++ = u'^';
|
||||
*s++ = u'\\';
|
||||
}
|
||||
if (escape && shouldescapedos(unquoted[i])) *s++ = u'^';
|
||||
*s++ = unquoted[i];
|
||||
} else {
|
||||
for (j = 0; j < n; j++) {
|
||||
if (escape) *s++ = u'^';
|
||||
*s++ = u'\\';
|
||||
}
|
||||
if (escape && shouldescapedos(unquoted[i])) *s++ = u'^';
|
||||
*s++ = unquoted[i];
|
||||
}
|
||||
}
|
||||
if (escape) *s++ = u'^';
|
||||
*s++ = u'"';
|
||||
*s++ = u'\0';
|
||||
return true;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue