shithub: snippets

Download patch

ref: 6669b2c0a19c010493f5343ec136987081672efd
parent: 1d039d6c0478743045e954522c65d549cd65791e
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed Mar 3 05:43:29 EST 2021

add helpmeport script

--- a/README.md
+++ b/README.md
@@ -12,4 +12,5 @@
 * `xml.[ch]` XML parser, works as a streaming parser as well
 
 * `clear` removes all program output from the terminal, leaving only commands used
+* `helpmeport` generates files needed to port a Unix library or a binary
 * `watch` watch-like tool, imagine
--- /dev/null
+++ b/helpmeport
@@ -1,0 +1,92 @@
+#!/bin/rc
+# usage: rm -rf mkfile dir/plan9; helpmeport dir | rc
+rfork ne
+
+dir=$1
+shift
+
+if(! test -d $"dir){
+	echo directory should be specified
+	exit no
+}
+
+echo '# HEADERS'
+echo mkdir -p $dir/plan9
+g -h '#[ 	]*include[ 	]*<[^>/]+/' | sed 's☭.*<(.*)/[^/]*>.*☭mkdir -p '$dir'/plan9/\1☭g' | sort -u
+g -h '#[ 	]*include[ 	]*<[^>/]+' | sed 's☭.*<(.*)>.*☭echo ''#include "plan9.h"'' > '$dir'/plan9/\1☭g' | sort -u
+echo 'cat >'$dir'/plan9/plan9.h <<EOF
+#ifndef __plan9_h__
+#define __plan9_h__
+
+#include <u.h>
+#include <libc.h>
+#include </sys/include/stdio.h>
+
+typedef enum { false, true } bool;
+
+typedef s8int int8_t;
+typedef u8int uint8_t;
+typedef s16int int16_t;
+typedef u16int uint16_t;
+typedef s32int int32_t;
+typedef u32int uint32_t;
+typedef s64int int64_t;
+typedef u64int uint64_t;
+typedef long ssize_t;
+typedef ulong size_t;
+typedef intptr ptrdiff_t;
+typedef intptr intptr_t;
+typedef uintptr uintptr_t;
+
+#define INT16_MAX 0x7fff
+#define INT16_MIN ((int16_t)0x8000)
+#define INT64_MIN ((int64_t)0x8000000000000000ULL)
+#define INT_MAX 0x7fffffff
+#define INT_MIN (-INT_MAX-1)
+#define SHRT_MAX 0x7fff
+#define SHRT_MIN (-SHRT_MAX-1)
+#define SIZE_MAX 0xffffffffU
+#define UINT_MAX 0xffffffffU
+
+#define PRId64 "lld"
+#define PRIu64 "llud"
+#define PRIx64 "llx"
+
+#define __attribute__(a)
+
+#define exit(x) exits(x == 0 ? nil : "error")
+#define setenv(k,v,x) putenv(k,v)
+#define unsetenv(k) putenv(k,"")
+
+#endif
+EOF'
+
+fn c2o {
+	echo 'HEADERS=\'
+	walk -f $* | grep '\.h$' | sed 's☭.*☭	&\\☭g'
+	echo
+	echo 'OFILES=\'
+	walk -f $* | grep '\.c$' | sed 's☭(.*)\.c☭	\1.$O\\☭g'
+	echo
+#	walk -f $* | grep '\.c$' | sed 's☭(.*/)*([^/]+)\.c$☭	\2.$O\\☭g'
+#	echo
+#	walk -f $* | grep '/.*\.c$' | sed 's☭/[^/]+$☭☭g' | sort -u | sed 's☭.*☭%.$O: &/$stem.c\n	$CC $CFLAGS &/$stem.c\n☭g'
+}
+
+echo
+echo '# MKFILE'
+echo 'cat >mkfile <<''EOF'''
+echo '</$objtype/mkfile'
+echo
+echo '#LIB=libFIXME.a$O'
+echo '#BIN=/$objtype/bin'
+echo '#TARG=FIXME'
+echo 'CFLAGS=$CFLAGS -p -I'$dir' -I'$dir'/plan9 -DNDEBUG -D__plan9__ -D__${objtype}__'
+echo
+c2o $dir
+echo 'default:V: all'
+echo
+echo '#FIXME'
+echo '#</sys/src/cmd/mklib'
+echo '#</sys/src/cmd/mkone'
+echo EOF