ref: bbbbd7b9ae6daf32d72ba67fed5ba54ff673480e
parent: edebdf6014dc79fd13cf98b6ea27ef23c091af0a
author: Jacob Moody <[email protected]>
date: Tue Feb 28 04:30:13 EST 2023
no more npe dependency
--- a/tools/common.h
+++ b/tools/common.h
@@ -1,14 +1,9 @@
#ifndef GUARD_COMMON_H
#define GUARD_COMMON_H
+#include <u.h>
+#include <libc.h>
#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <stdbool.h>
-#include <stddef.h>
-#include <inttypes.h>
-#include <string.h>
-#include <errno.h>
#include <getopt.h>
#ifndef PROGRAM_NAME
@@ -20,36 +15,47 @@
#define COUNTOF(...) (sizeof(__VA_ARGS__) / sizeof(*(__VA_ARGS__)))
+#ifdef __plan9__
+typedef uchar uint8_t;
+typedef u32int uint32_t;
+typedef u16int uint16_t;
+#define PRIu32 "ud"
+typedef uchar bool;
+enum{
+ false,
+ true,
+};
+
+#define error_exit(...) (sysfatal(PROGRAM_NAME ": " __VA_ARGS__))
+#else
#define error_exit(...) exit((fprintf(stderr, PROGRAM_NAME ": " __VA_ARGS__), 1))
+#endif
void usage_exit(int status) {
fprintf(stderr, "Usage: " PROGRAM_NAME " " USAGE_OPTS "\n");
- exit(status);
+ exits("usage");
}
void *xmalloc(size_t size) {
- errno = 0;
void *m = malloc(size);
if (!m) {
- error_exit("Could not allocate %zu bytes: %s\n", size, strerror(errno));
+ error_exit("Could not allocate %lud bytes: %r\n", size);
}
return m;
}
void *xcalloc(size_t size) {
- errno = 0;
void *m = calloc(size, 1);
if (!m) {
- error_exit("Could not allocate %zu bytes: %s\n", size, strerror(errno));
+ error_exit("Could not allocate %lud bytes: %r\n", size);
}
return m;
}
void *xrealloc(void *m, size_t size) {
- errno = 0;
m = realloc(m, size);
if (!m) {
- error_exit("Could not allocate %zu bytes: %s\n", size, strerror(errno));
+ error_exit("Could not allocate %lud bytes: %r\n", size);
}
return m;
}
@@ -56,33 +62,29 @@
FILE *xfopen(const char *filename, char rw) {
char mode[3] = {rw, 'b', '\0'};
- errno = 0;
FILE *f = fopen(filename, mode);
if (!f) {
- error_exit("Could not open file \"%s\": %s\n", filename, strerror(errno));
+ error_exit("Could not open file \"%s\": %r\n", filename);
}
return f;
}
void xfread(uint8_t *data, size_t size, const char *filename, FILE *f) {
- errno = 0;
if (fread(data, 1, size, f) != size) {
fclose(f);
- error_exit("Could not read from file \"%s\": %s\n", filename, strerror(errno));
+ error_exit("Could not read from file \"%s\": %r\n", filename);
}
}
void xfwrite(const uint8_t *data, size_t size, const char *filename, FILE *f) {
- errno = 0;
if (fwrite(data, 1, size, f) != size) {
fclose(f);
- error_exit("Could not write to file \"%s\": %s\n", filename, strerror(errno));
+ error_exit("Could not write to file \"%s\": %r\n", filename);
}
}
long xfsize(const char *filename, FILE *f) {
long size = -1;
- errno = 0;
if (!fseek(f, 0, SEEK_END)) {
size = ftell(f);
if (size != -1) {
@@ -90,7 +92,7 @@
}
}
if (size == -1) {
- error_exit("Could not measure file \"%s\": %s\n", filename, strerror(errno));
+ error_exit("Could not measure file \"%s\": %r\n", filename);
}
return size;
}
--- a/tools/getopt.c
+++ b/tools/getopt.c
@@ -23,12 +23,9 @@
/* This implementation was taken from musl and modified for RGBDS */
-#include <stddef.h>
-#include <stdlib.h>
-#include <limits.h>
+#include <u.h>
+#include <libc.h>
#include <stdio.h>
-#include <string.h>
-#include <wchar.h>
#include "getopt.h"
--- a/tools/getopt.h
+++ b/tools/getopt.h
@@ -30,6 +30,14 @@
extern "C" {
#endif
+#ifdef __plan9__
+typedef ulong size_t;
+typedef Rune wchar_t;
+enum { MB_LEN_MAX = 4 };
+#define mblen(r,n) (utfnlen(r, n))
+#define mbtowc(r,s,n) (chartorune(r,s))
+#endif
+
extern char *optarg;
extern int optind, musl_opterr, musl_optopt, musl_optreset;
--- a/tools/gfx.c
+++ b/tools/gfx.c
@@ -258,7 +258,7 @@
free(interleaved);
}
-int main(int argc, char *argv[]) {
+void main(int argc, char *argv[]) {
parse_args(argc, argv);
argc -= optind;
@@ -300,5 +300,5 @@
}
free(graphic.data);
- return 0;
+ exits(nil);
}
--- a/tools/lz/main.c
+++ b/tools/lz/main.c
@@ -1,6 +1,6 @@
#include "proto.h"
-int main (int argc, char ** argv) {
+void main (int argc, char ** argv) {
struct options options = get_options(argc, argv);
unsigned short size;
unsigned char * file_buffer = read_file_into_buffer(options.input, &size);
@@ -22,7 +22,7 @@
}
free(file_buffer);
free(commands);
- return 0;
+ exits(nil);
}
struct command * compress (const unsigned char * data, unsigned short * size, unsigned method) {
--- a/tools/lz/mkfile
+++ b/tools/lz/mkfile
@@ -14,9 +14,6 @@
uncomp.$O\
util.$O\
-# ThIs MaKeS It PoRtAbLe
-POSIX=-D PRIu32="ud" -DPRId32="d" -DPRIx32="x" -DPRIX32="X" -DPRIo32="o" -DSTDOUT_FILENO=1 -DSTDIN_FILENO=0 -DPRIu8="ud" -DPRIu16="ud" -DPRId16="d" -DPRIx16="x" -DPRIX16="X" -DMB_LEN_MAX=4 -DUINT32_C='(uint32_t)'
-
-CFLAGS=$CFLAGS -p -I/sys/include/npe -D__plan9__ -D__${objtype}__ $POSIX
+CFLAGS=$CFLAGS -p
</sys/src/cmd/mkone
--- a/tools/lz/nullcomp.c
+++ b/tools/lz/nullcomp.c
@@ -6,7 +6,7 @@
Flags values: 0 = split a trailing 33-to-64-byte block at the end into two short blocks; 1 = don't
*/
-struct command * store_uncompressed (__attribute__((unused)) const unsigned char * data, __attribute__((unused)) const unsigned char * bitflipped, unsigned short * size, unsigned flags) {
+struct command * store_uncompressed (const unsigned char * data, const unsigned char * bitflipped, unsigned short * size, unsigned flags) {
unsigned short position, block, remainder = *size;
struct command _tmp;
struct command * result = NULL;
--- a/tools/lz/options.c
+++ b/tools/lz/options.c
@@ -118,7 +118,7 @@
fputs(" cleared (default: 0).\n", stderr);
fputs("The source and output filenames can be given as - (or omitted) to use standard\n", stderr);
fputs("input and output. Use -- to indicate that subsequent arguments are file names.\n", stderr);
- exit(3);
+ exits("usage");
}
noreturn list_compressors (void) {
@@ -137,5 +137,5 @@
fputs("Note: the offset indicates the compressor's lowest method number when the\n", stderr);
fputs("--compressor option is not given. When that option is used, every compressor's\n", stderr);
fputs("methods are numbered from zero.\n", stderr);
- exit(3);
+ exits("usage");
}
--- a/tools/lz/proto.h
+++ b/tools/lz/proto.h
@@ -1,7 +1,6 @@
+#include <u.h>
+#include <libc.h>
#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdarg.h>
#define NUM_COMPRESSORS 4
#define COMPRESSION_METHODS 96 /* sum of all values for the methods field in compressors */
@@ -45,7 +44,7 @@
extern char option_name_buffer[];
// main.c
-int main(int, char **);
+void main(int, char **);
struct command * compress(const unsigned char *, unsigned short *, unsigned);
// merging.c
--- a/tools/lz/repcomp.c
+++ b/tools/lz/repcomp.c
@@ -7,7 +7,7 @@
(lowest bit to highest: repeat single byte (1), repeat two bytes (2), repeat zeros (3)).
*/
-struct command * try_compress_repetitions (const unsigned char * data, __attribute__((unused)) const unsigned char * bitflipped, unsigned short * size, unsigned flags) {
+struct command * try_compress_repetitions (const unsigned char * data, const unsigned char * bitflipped, unsigned short * size, unsigned flags) {
unsigned short pos = 0, skipped = 0;
struct command _tmp;
struct command * result = malloc(*size * sizeof(struct command));
--- a/tools/lz/util.c
+++ b/tools/lz/util.c
@@ -7,7 +7,7 @@
vfprintf(stderr, error, ap);
va_end(ap);
fputc('\n', stderr);
- exit(error_code);
+ exits("error");
}
unsigned char * read_file_into_buffer (const char * file, unsigned short * size) {
--- a/tools/mkfile
+++ b/tools/mkfile
@@ -10,9 +10,6 @@
pokemon_animation_graphics\
stadium\
-# ThIs MaKeS It PoRtAbLe
-POSIX=-D PRIu32="ud" -DPRId32="d" -DPRIx32="x" -DPRIX32="X" -DPRIo32="o" -DSTDOUT_FILENO=1 -DSTDIN_FILENO=0 -DPRIu8="ud" -DPRIu16="ud" -DPRId16="d" -DPRIx16="x" -DPRIX16="X" -DMB_LEN_MAX=4 -DUINT32_C='(uint32_t)'
-
-CFLAGS=$CFLAGS -I/sys/include/npe -D__plan9__ -D__${objtype}__ $POSIX
+CFLAGS=$CFLAGS -p -D__plan9__
</sys/src/cmd/mkmany
--- a/tools/png_dimensions.c
+++ b/tools/png_dimensions.c
@@ -12,7 +12,7 @@
return (width_tiles << 4) | width_tiles;
}
-int main(int argc, char *argv[]) {
+void main(int argc, char *argv[]) {
if (argc < 3) {
usage_exit(1);
}
@@ -19,5 +19,5 @@
uint8_t output_byte = read_png_dimensions(argv[1]);
write_u8(argv[2], &output_byte, 1);
- return 0;
+ exits(nil);
}
--- a/tools/pokemon_animation.c
+++ b/tools/pokemon_animation.c
@@ -164,7 +164,7 @@
}
}
-int main(int argc, char *argv[]) {
+void main(int argc, char *argv[]) {
struct Options options = {0};
parse_args(argc, argv, &options);
@@ -191,5 +191,5 @@
}
free(tilemap);
- return 0;
+ exits(nil);
}
--- a/tools/pokemon_animation_graphics.c
+++ b/tools/pokemon_animation_graphics.c
@@ -144,7 +144,7 @@
free(data);
}
-int main(int argc, char *argv[]) {
+void main(int argc, char *argv[]) {
struct Options options = {0};
parse_args(argc, argv, &options);
@@ -167,5 +167,5 @@
}
free(tiles);
- return 0;
+ exits(nil);
}
--- a/tools/stadium.c
+++ b/tools/stadium.c
@@ -182,7 +182,7 @@
SET_U16BE(file + GLOBAL_OFF, globalsum);
}
-int main(int argc, char *argv[]) {
+void main(int argc, char *argv[]) {
bool european = false;
parse_args(argc, argv, &european);
@@ -199,5 +199,5 @@
calculate_checksums(file, european);
}
write_u8(filename, file, filesize);
- return 0;
+ exits(nil);
}