Compare commits

...
4 Commits
11 changed files with 1363 additions and 1159 deletions

No files matched your search

+1 -1
View File
@@ -4,7 +4,7 @@
#define __COSMOPOLITAN_MAJOR__ 3
#define __COSMOPOLITAN_MINOR__ 3
#define __COSMOPOLITAN_PATCH__ 8
#define __COSMOPOLITAN_PATCH__ 9
#define __COSMOPOLITAN__ \
(100000000 * __COSMOPOLITAN_MAJOR__ + 1000000 * __COSMOPOLITAN_MINOR__ + \
__COSMOPOLITAN_PATCH__)
+5
View File
@@ -17,10 +17,12 @@
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/stdio/internal.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/s.h"
#include "libc/thread/thread.h"
/**
@@ -31,11 +33,14 @@ FILE *stdin;
static FILE __stdin;
__attribute__((__constructor__(60))) static textstartup void initin(void) {
struct stat st;
stdin = &__stdin;
stdin->fd = STDIN_FILENO;
stdin->iomode = O_RDONLY;
stdin->buf = stdin->mem;
stdin->size = sizeof(stdin->mem);
stdin->lock._type = PTHREAD_MUTEX_RECURSIVE;
if (fstat(STDIN_FILENO, &st) || !S_ISREG(st.st_mode))
stdin->bufmode = _IONBF;
__fflush_register(stdin);
}
+1011 -1045
View File
File diff suppressed because it is too large. Load diff
+1
View File
@@ -15,6 +15,7 @@ LICENSE
LOCAL CHANGES
- Introduce -T FILE, --time-log=FILE flag
- Introduce $(uniq token...) native function
- Remove code that forces slow path if not using /bin/sh
+1
View File
@@ -1949,6 +1949,7 @@ func_shell_base (char *o, char **argv, int trim_newlines)
child.output.out = pipedes[1];
child.output.err = errfd;
child.timelog = timelog_begin (command_argv);
pid = child_execute_job (&child, 1, command_argv);
if (pid < 0)
+3
View File
@@ -1104,6 +1104,8 @@ reap_children (int block, int err)
void
free_childbase (struct childbase *child)
{
timelog_end (child->timelog);
if (child->environment != 0)
{
char **ep = child->environment;
@@ -1463,6 +1465,7 @@ start_job_command (struct child *child)
jobserver_pre_child (ANY_SET (flags, COMMANDS_RECURSE));
child->timelog = timelog_begin (argv);
child->pid = child_execute_job ((struct childbase *)child,
child->good_stdin, argv);
+2
View File
@@ -15,6 +15,7 @@ You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>. */
#include "output.h"
#include "timelog.h"
/* Structure describing a running or dead child process. */
@@ -31,6 +32,7 @@ this program. If not, see <https://www.gnu.org/licenses/>. */
#define CHILDBASE \
char *cmd_name; /* Allocated copy of command run. */ \
char **environment; /* Environment for commands. */ \
struct timelog *timelog; /* [jart] the lorde of all time. */ \
VMSCHILD \
struct output output /* Output for this child. */
+8
View File
@@ -27,6 +27,7 @@ this program. If not, see <https://www.gnu.org/licenses/>. */
#include "getopt.h"
#include "libc/runtime/runtime.h"
#include "shuffle.h"
#include "timelog.h"
#include <assert.h>
#ifdef HAVE_FCNTL_H
@@ -374,6 +375,8 @@ static const char *const usage[] =
N_("\
--trace Print tracing information.\n"),
N_("\
-T FILE, --time-log=FILE Log command invocation microseconds to FILE.\n"),
N_("\
-v, --version Print the version number of make and exit.\n"),
N_("\
-w, --print-directory Print the current directory.\n"),
@@ -471,6 +474,7 @@ static struct command_switch switches[] =
{ 'o', filename, &old_files, 0, 0, 0, 0, 0, 0, "old-file", 0 },
{ 'O', string, &output_sync_option, 1, 1, 0, 0, "target", 0, "output-sync", 0 },
{ 'W', filename, &new_files, 0, 0, 0, 0, 0, 0, "what-if", 0 },
{ 'T', string, &timelog_path, 0, 0, 0, 0, 0, 0, "timelog", 0 }, // [jart]
/* These are long-style options. */
{ CHAR_MAX+1, strlist, &db_flags, 1, 1, 0, 0, "basic", 0, "debug", 0 },
@@ -2258,6 +2262,10 @@ main (int argc, char **argv, char **envp)
}
#endif
/* [jart] Setup command latency log. */
timelog_init ();
/* Set up MAKEFLAGS and MFLAGS again, so they will be right. */
define_makeflags (0);
+186
View File
@@ -0,0 +1,186 @@
/* Copyright 2024 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. */
#include "timelog.h"
#include <time.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
struct timelog
{
struct timespec started;
size_t command_length;
char command[4000];
};
char *timelog_path;
int timelog_fd = -1;
void timelog_init (void)
{
if (!timelog_path)
return;
if ((timelog_fd = open (timelog_path, O_APPEND | O_WRONLY | O_CREAT, 0644)) == -1)
{
perror (timelog_path);
exit (1);
}
}
static int is_shell_safe (int c)
{
if (c > 127)
return true;
if (isalnum(c))
return true;
switch (c)
{
case '+':
case '-':
case '.':
case '/':
case '_':
case '=':
case ':':
return true;
default:
return false;
}
}
static int needs_quotes (const char *s)
{
int c;
if (!*s)
return true;
while ((c = *s++ & 255))
if (!is_shell_safe (c))
return true;
return false;
}
static long long get_microseconds (struct timespec beg, struct timespec end)
{
end.tv_sec -= beg.tv_sec;
if (end.tv_nsec < beg.tv_nsec)
{
end.tv_nsec += 1000000000;
end.tv_sec--;
}
end.tv_nsec -= beg.tv_nsec;
return end.tv_sec * 1000000ll + end.tv_nsec / 1000ll;
}
struct timelog *timelog_begin (char **argv)
{
int i, j, q;
char *p, *pe;
/* don't bother if disabled */
if (timelog_fd == -1)
return NULL;
/* allocate object */
struct timelog *tl;
if (!(tl = malloc (sizeof (struct timelog))))
return NULL;
/* stringify command */
p = tl->command;
pe = tl->command + sizeof (tl->command) - 1;
for (i = 0; argv[i]; ++i)
{
if (i && p < pe)
*p++ = ' ';
q = needs_quotes (argv[i]);
if (q && p < pe)
*p++ = '\'';
for (j = 0; argv[i][j]; ++j)
{
if (isspace(argv[i][j]) ||
iscntrl(argv[i][j]))
{
if (p < pe)
*p++ = ' ';
}
else if (argv[i][j] == '\'')
{
if (p < pe)
*p++ = '\'';
if (p < pe)
*p++ = '"';
if (p < pe)
*p++ = '\'';
if (p < pe)
*p++ = '"';
if (p < pe)
*p++ = '\'';
}
else
{
if (p < pe)
*p++ = argv[i][j];
}
}
if (q && p < pe)
*p++ = '\'';
}
if (p == pe)
{
p[-3] = '.';
p[-2] = '.';
p[-1] = '.';
}
*p++ = '\n';
tl->command_length = p - tl->command;
/* record starting timestamp */
clock_gettime (CLOCK_REALTIME, &tl->started);
/* return object */
return tl;
}
void timelog_end (struct timelog *tl)
{
long long us;
char ibuf[22];
struct iovec iov[2];
struct timespec ended;
/* don't bother if disabled */
if (tl == NULL)
return;
/* get elapsed microseconds string */
clock_gettime (CLOCK_REALTIME, &ended);
us = get_microseconds (tl->started, ended);
snprintf (ibuf, sizeof(ibuf), "% 20lld ", us);
// write to log
iov[0].iov_base = ibuf;
iov[0].iov_len = 21;
iov[1].iov_base = tl->command;
iov[1].iov_len = tl->command_length;
writev (timelog_fd, iov, 2);
// free object
free (tl);
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef MAKE_TIMELOG_H_
#define MAKE_TIMELOG_H_
struct timelog;
extern int timelog_fd;
extern char *timelog_path;
void timelog_init (void);
struct timelog *timelog_begin (char **);
void timelog_end (struct timelog *);
#endif /* MAKE_TIMELOG_H_ */
+132 -113
View File
@@ -18,33 +18,39 @@
╚─────────────────────────────────────────────────────────────────────────────*/
#include "dsp/scale/scale.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/calls/struct/winsize.h"
#include "libc/calls/termios.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/fmt/conv.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/str/locale.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/exit.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/termios.h"
#include "libc/stdio/stdio.h"
#include "libc/stdio/sysparam.h"
#include "third_party/getopt/getopt.internal.h"
#include "third_party/stb/stb_image.h"
#define SQR(X) ((X) * (X))
#define UNCUBE(x) x < 48 ? 0 : x < 115 ? 1 : (x - 35) / 40
#define ORDIE(X) \
do { \
if (!(X)) \
perror(#X), exit(1); \
} while (0)
int want24bit_;
int wantfullsize_;
#define USAGE \
" IMAGE...\n\
\n\
SYNOPSIS\n\
\n\
Prints Image\n\
\n\
FLAGS\n\
\n\
-? help\n\
-t true color mode\n\
-f print image at full size\n\
-w INT specify printed width in pixels (or cols)\n\
-h INT specify printed height in pixels (or rows*2)\n\
\n"
int FLAG_width;
int FLAG_height;
bool FLAG_true;
bool FLAG_full;
const char *prog;
/**
* Quantizes 24-bit RGB to xterm256 code range [16,256).
@@ -58,11 +64,9 @@ int rgb2xterm256(int r, int g, int b) {
qg = cube[(ig = UNCUBE(g))];
qb = cube[(ib = UNCUBE(b))];
if (SQR(qr - r) + SQR(qg - g) + SQR(qb - b) <=
SQR(ql - r) + SQR(ql - g) + SQR(ql - b)) {
SQR(ql - r) + SQR(ql - g) + SQR(ql - b))
return ir * 36 + ig * 6 + ib + 020;
} else {
return il + 0350;
}
return il + 0350;
}
/**
@@ -72,7 +76,7 @@ void PrintImage(int yn, int xn, unsigned char rgb[yn][xn][3]) {
unsigned y, x;
for (y = 0; y < yn; y += 2) {
for (x = 0; x < xn; ++x) {
if (want24bit_) {
if (FLAG_true) {
printf("\033[48;2;%hhu;%hhu;%hhu;38;2;%hhu;%hhu;%hhum▄",
rgb[y + 0][x][0], rgb[y + 0][x][1], rgb[y + 0][x][2],
rgb[MIN(y + 1, yn - 1)][x][0], rgb[MIN(y + 1, yn - 1)][x][1],
@@ -90,102 +94,117 @@ void PrintImage(int yn, int xn, unsigned char rgb[yn][xn][3]) {
}
}
/**
* Determines dimensions of teletypewriter.
*/
void GetTermSize(int *out_rows, int *out_cols) {
struct winsize ws;
ws.ws_row = 20;
ws.ws_col = 80;
tcgetwinsize(STDOUT_FILENO, &ws);
tcgetwinsize(STDIN_FILENO, &ws);
*out_rows = ws.ws_row;
*out_cols = ws.ws_col;
}
void ReadAll(int fd, char *p, size_t n) {
ssize_t rc;
size_t got;
do {
ORDIE((rc = read(fd, p, n)) != -1);
got = rc;
if (!got && n) {
fprintf(stderr, "error: expected eof\n");
exit(EXIT_FAILURE);
}
p += got;
n -= got;
} while (n);
}
static void Deblinterlace(long zn, long yn, long xn,
unsigned char dst[zn][yn][xn],
const unsigned char src[yn][xn][zn]) {
long y, x, z;
for (y = 0; y < yn; ++y) {
for (x = 0; x < xn; ++x) {
for (z = 0; z < zn; ++z) {
void Deblinterlace(int zn, int yn, int xn, unsigned char dst[zn][yn][xn],
const unsigned char src[yn][xn][zn]) {
int y, x, z;
for (y = 0; y < yn; ++y)
for (x = 0; x < xn; ++x)
for (z = 0; z < zn; ++z)
dst[z][y][x] = src[y][x][z];
}
}
}
}
static void Reblinterlace(long zn, long yn, long xn,
unsigned char dst[yn][xn][zn],
const unsigned char src[zn][yn][xn]) {
long y, x, z;
for (y = 0; y < yn; ++y) {
for (x = 0; x < xn; ++x) {
for (z = 0; z < zn; ++z) {
void Reblinterlace(int zn, int yn, int xn, unsigned char dst[yn][xn][zn],
const unsigned char src[zn][yn][xn]) {
int y, x, z;
for (y = 0; y < yn; ++y)
for (x = 0; x < xn; ++x)
for (z = 0; z < zn; ++z)
dst[y][x][z] = src[z][y][x];
}
}
}
}
wontreturn void PrintUsage(int rc, int fd) {
tinyprint(fd, "usage: ", prog, USAGE, NULL);
exit(rc);
}
int main(int argc, char *argv[]) {
struct stat st;
void *map, *data, *data2;
int i, fd, tyn, txn, yn, xn, cn;
setlocale(LC_ALL, "C.UTF-8");
for (i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-t") == 0) {
want24bit_ = 1;
} else if (strcmp(argv[i], "-f") == 0) {
wantfullsize_ = 1;
} else {
fd = open(argv[i], O_RDONLY);
if (fd == -1) {
perror(argv[i]);
exit(1);
}
fstat(fd, &st);
map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
data = stbi_load_from_memory(map, st.st_size, &xn, &yn, &cn, 3);
munmap(map, st.st_size);
close(fd);
if (!wantfullsize_) {
GetTermSize(&tyn, &txn);
--tyn;
tyn *= 2;
data2 = memalign(32, 3 * yn * xn);
Deblinterlace(3, yn, xn, data2, data);
free(data);
data = memalign(32, 3 * tyn * txn);
EzGyarados(3, tyn, txn, data, 3, yn, xn, data2, 0, 3, tyn, txn, yn, xn,
0, 0, 0, 0);
free(data2);
data2 = memalign(32, 3 * yn * xn);
Reblinterlace(3, tyn, txn, data2, data);
free(data);
data = data2;
yn = tyn;
xn = txn;
}
PrintImage(yn, xn, data);
free(data);
// get program name
prog = argv[0];
if (!prog)
prog = "basicidea";
// get flags
int opt;
while ((opt = getopt(argc, argv, "?tfw:h:")) != -1) {
switch (opt) {
case 't':
FLAG_true = true;
break;
case 'f':
FLAG_full = true;
break;
case 'w':
FLAG_width = atoi(optarg);
break;
case 'h':
FLAG_height = atoi(optarg);
break;
case '?':
PrintUsage(0, 1);
default:
PrintUsage(1, 2);
}
}
return 0;
if (argc == optind) {
tinyprint(2, prog, ": missing image path (pass -? for help)\n", NULL);
exit(1);
}
// process arguments
for (int i = optind; i < argc; ++i) {
const char *path = argv[i];
// open image
void *data;
int cn, iyn, ixn;
if (!(data = stbi_load(path, &ixn, &iyn, &cn, 3))) {
perror(path);
exit(1);
}
// determine size
int yn, xn;
struct winsize ws;
if (FLAG_full) {
yn = iyn;
xn = ixn;
} else if (FLAG_width > 0 || FLAG_height > 0) {
if (FLAG_width <= 0) {
yn = FLAG_height;
xn = (double)ixn / iyn * FLAG_height + .5;
} else if (FLAG_height <= 0) {
yn = (double)iyn / ixn * FLAG_width + .5;
xn = FLAG_width;
} else {
yn = FLAG_height;
xn = FLAG_width;
}
} else if (!tcgetwinsize(1, &ws)) {
yn = (ws.ws_row - 1) * 2;
xn = ws.ws_col;
} else {
yn = iyn;
xn = ixn;
}
// scale image
if (yn != iyn || xn != ixn) {
void *data2 = memalign(32, 3l * iyn * ixn);
Deblinterlace(3, iyn, ixn, data2, data);
free(data);
data = memalign(32, 3l * yn * xn);
EzGyarados(3, yn, xn, data, 3, iyn, ixn, data2, 0, 3, yn, xn, iyn, ixn, 0,
0, 0, 0);
free(data2);
data2 = memalign(32, 3l * yn * xn);
Reblinterlace(3, yn, xn, data2, data);
free(data);
data = data2;
}
// print image
PrintImage(yn, xn, data);
free(data);
}
}