Make improvements

- Expand redbean UNIX module
- Expand redbean documentation
- Ensure Lua copyright is embedded in binary
- Increase the PATH_MAX limit especially on NT
- Use column major sorting for linenoise completions
- Fix some suboptimalities in redbean's new UNIX API
- Figured out right flags for Multics newline in raw mode
This commit is contained in:
Justine Tunney 2022-04-24 09:59:22 -07:00
parent cf3174dc74
commit 2046c0d2ae
305 changed files with 6602 additions and 4221 deletions

View file

@ -18,6 +18,11 @@
*/
#include "libc/fmt/conv.h"
int(abs)(int x) {
/**
* Returns absolute value of 32-bit integer.
* @note `labs(LONG_MIN)` returns `LONG_MIN` unless `-ftrapv`
* @note consider ABS() to avoid narrowing
*/
int abs(int x) {
return 0 < x ? x : -x;
}

View file

@ -368,9 +368,6 @@ hidden int __fmt(void *fn, void *arg, const char *format, va_list va) {
}
break;
case 'n':
// nonstandard %n specifier
// used to print newlines that work in raw terminal modes
if (__nomultics) __FMT_PUT('\r');
__FMT_PUT('\n');
break;
case 'F':

View file

@ -26,10 +26,6 @@ int sscanf(const char *, const char *, ...) scanfesque(2);
int vsscanf(const char *, const char *, va_list);
int vcscanf(int (*)(void *), int (*)(int, void *), void *, const char *,
va_list);
int strerror_r(int, char *, size_t) dontthrow nocallback;
int strerror_wr(int, uint32_t, char *, size_t) dontthrow nocallback;
const char *strerror_short(int) nosideeffect;
const char *strerror_long(int) nosideeffect;
int __fmt(void *, void *, const char *, va_list) hidden;
char *itoa(int, char *, int) compatfn;
char *fcvt(double, int, int *, int *);

View file

@ -19,8 +19,8 @@
#include "libc/macros.internal.h"
.macro .e e s
.long \e - kErrorNamesLong
.long 1f - kErrorNamesLong
.long \e - kErrnoDocs
.long 1f - kErrnoDocs
.rodata.str1.1
1: .asciz "\s"
.previous
@ -29,7 +29,7 @@
.section .rodata
.align 4
.underrun
kErrorNamesLong:
kErrnoDocs:
.e EINVAL,"Invalid argument"
.e ENOSYS,"Function not implemented"
.e EPERM,"Operation not permitted"
@ -115,6 +115,6 @@ kErrorNamesLong:
.e ENOTRECOVERABLE,"State not recoverable"
.e ENONET,"Machine is not on the network"
.e ERESTART,"Interrupted system call should be restarted"
.long 0
.endobj kErrorNamesLong,globl,hidden
.long -123
.endobj kErrnoDocs,globl,hidden
.overrun

View file

@ -19,8 +19,8 @@
#include "libc/macros.internal.h"
.macro .e e
.long \e - kErrorNames
.long 1f - kErrorNames
.long \e - kErrnoNames
.long 1f - kErrnoNames
.rodata.str1.1
1: .string "\e"
.previous
@ -29,7 +29,7 @@
.section .rodata
.align 4
.underrun
kErrorNames:
kErrnoNames:
.e EINVAL
.e ENOSYS
.e EPERM
@ -116,6 +116,6 @@ kErrorNames:
.e ENONET
.e ERESTART
.e ENODATA
.long 0
.endobj kErrorNames,globl,hidden
.long -123
.endobj kErrnoNames,globl,hidden
.overrun

View file

@ -1,16 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_FMT_KERRORNAMES_INTERNAL_H_
#define COSMOPOLITAN_LIBC_FMT_KERRORNAMES_INTERNAL_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct ErrorName {
int x, s;
};
extern const struct ErrorName kStrSignal[];
extern const struct ErrorName kErrorNames[];
extern const struct ErrorName kErrorNamesLong[];
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_FMT_KERRORNAMES_INTERNAL_H_ */

View file

@ -17,8 +17,12 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/macros.internal.h"
long(labs)(long x) {
return ABS(x);
/**
* Returns absolute value of long integer.
* @note `labs(LONG_MIN)` returns `LONG_MIN` unless `-ftrapv`
* @note consider ABS() to avoid narrowing
*/
long labs(long x) {
return 0 < x ? x : -x;
}

View file

@ -17,8 +17,12 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/macros.internal.h"
long long(llabs)(long long x) {
return ABS(x);
/**
* Returns absolute value of long long integer.
* @note `llabs(LONG_LONG_MIN)` returns `LONG_LONG_MIN` unless `-ftrapv`
* @note consider ABS() to avoid narrowing
*/
long long llabs(long long x) {
return 0 < x ? x : -x;
}

View file

@ -0,0 +1,23 @@
#ifndef COSMOPOLITAN_LIBC_FMT_MAGNUMSTRS_H_
#define COSMOPOLITAN_LIBC_FMT_MAGNUMSTRS_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct MagnumStr {
int x, s;
};
extern const struct MagnumStr kErrnoDocs[];
extern const struct MagnumStr kErrnoNames[];
extern const struct MagnumStr kIpOptnames[];
extern const struct MagnumStr kSignalNames[];
extern const struct MagnumStr kSockOptnames[];
extern const struct MagnumStr kTcpOptnames[];
const char *DescribeSockLevel(int);
const char *DescribeSockOptname(int, int);
const char *GetMagnumStr(const struct MagnumStr *, int);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_FMT_MAGNUMSTRS_H_ */

View file

@ -17,23 +17,16 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/fmt.h"
#include "libc/fmt/kerrornames.internal.h"
#include "libc/fmt/magnumstrs.internal.h"
/**
* Converts errno value to descriptive sentence.
* @return non-null rodata string or null if not found
*/
privileged const char *strerror_long(int x) {
/* kprintf() weakly depends on this function */
int i;
const char *strerdoc(int x) {
if (x) {
for (i = 0; kErrorNamesLong[i].x; ++i) {
if (x ==
*(const long *)((uintptr_t)kErrorNamesLong + kErrorNamesLong[i].x)) {
return (const char *)((uintptr_t)kErrorNamesLong +
kErrorNamesLong[i].s);
}
}
return GetMagnumStr(kErrnoDocs, x);
} else {
return 0;
}
return 0;
}

View file

@ -16,22 +16,17 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/fmt.h"
#include "libc/fmt/kerrornames.internal.h"
#include "libc/fmt/magnumstrs.internal.h"
#include "libc/str/str.h"
/**
* Converts errno value to symbolic name.
* @return non-null rodata string or null if not found
*/
privileged const char *strerror_short(int x) {
/* kprintf() weakly depends on this function */
int i;
const char *strerrno(int x) {
if (x) {
for (i = 0; kErrorNames[i].x; ++i) {
if (x == *(const int *)((uintptr_t)kErrorNames + kErrorNames[i].x)) {
return (const char *)((uintptr_t)kErrorNames + kErrorNames[i].s);
}
}
return GetMagnumStr(kErrnoNames, x);
} else {
return 0;
}
return 0;
}

View file

@ -23,9 +23,9 @@
* Converts errno value to string non-reentrantly.
* @see strerror_r()
*/
noasan char *strerror(int err) {
char *strerror(int err) {
if (IsTiny()) {
return firstnonnull(strerror_short(err), "EUNKNOWN");
return firstnonnull(strerrno(err), "EUNKNOWN");
} else {
_Alignas(1) static char buf[512];
strerror_r(err, buf, sizeof(buf));

View file

@ -25,6 +25,6 @@
* @param err is error number or zero if unknown
* @return 0 on success, or error code
*/
privileged int strerror_r(int err, char *buf, size_t size) {
int strerror_r(int err, char *buf, size_t size) {
return strerror_wr(err, GetLastError(), buf, size);
}

View file

@ -32,13 +32,13 @@
* @param err is error number or zero if unknown
* @return 0 on success, or error code
*/
privileged int strerror_wr(int err, uint32_t winerr, char *buf, size_t size) {
int strerror_wr(int err, uint32_t winerr, char *buf, size_t size) {
/* kprintf() weakly depends on this function */
int c, n;
char16_t winmsg[256];
const char *sym, *msg;
sym = firstnonnull(strerror_short(err), "EUNKNOWN");
msg = firstnonnull(strerror_long(err), "No error information");
sym = firstnonnull(strerrno(err), "EUNKNOWN");
msg = firstnonnull(strerdoc(err), "No error information");
if (IsTiny()) {
if (!sym) sym = "EUNKNOWN";
for (; (c = *sym++); --size)