shithub: npe

Download patch

ref: 7944a8444f40c267b9f622bfbe7f835115f8155a
parent: c72d3e3d80352a947f7e2aecfb8768ba4b84c87d
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed Mar 17 05:52:39 EDT 2021

plan9.h → npe.h

--- a/include/npe/SDL2/SDL.h
+++ b/include/npe/SDL2/SDL.h
@@ -1,7 +1,7 @@
 #ifndef _SDL_h_
 #define _SDL_h_
 
-#include "plan9.h"
+#include <npe.h>
 #include <keyboard.h>
 
 #pragma lib "libnpe_sdl2.a"
--- a/include/npe/alloca.h
+++ b/include/npe/alloca.h
@@ -1,1 +1,1 @@
-#include "plan9.h"
+#include <npe.h>
--- a/include/npe/assert.h
+++ b/include/npe/assert.h
@@ -1,1 +1,1 @@
-#include "plan9.h"
+#include <npe.h>
--- a/include/npe/ctype.h
+++ b/include/npe/ctype.h
@@ -1,1 +1,1 @@
-#include "plan9.h"
+#include <npe.h>
--- a/include/npe/dirent.h
+++ b/include/npe/dirent.h
@@ -1,7 +1,7 @@
-#ifndef _dirent_h_
-#define _dirent_h_
+#ifndef _npe_dirent_h_
+#define _npe_dirent_h_
 
-#include "plan9.h"
+#include <npe.h>
 
 enum {
 	DT_LNK = -1,
--- a/include/npe/iconv.h
+++ b/include/npe/iconv.h
@@ -1,7 +1,7 @@
-#ifndef _iconv_h_
-#define _iconv_h_
+#ifndef _npe_iconv_h_
+#define _npe_iconv_h_
 
-#include "plan9.h"
+#include <npe.h>
 
 typedef struct iconv_s *iconv_t;
 #pragma incomplete struct iconv_s
--- a/include/npe/malloc.h
+++ b/include/npe/malloc.h
@@ -1,1 +1,1 @@
-#include "plan9.h"
+#include <npe.h>
--- a/include/npe/math.h
+++ b/include/npe/math.h
@@ -1,7 +1,7 @@
-#ifndef _math_h_
-#define _math_h_
+#ifndef _npe_math_h_
+#define _npe_math_h_
 
-#include "plan9.h"
+#include <npe.h>
 
 #define isnan isNaN
 
--- /dev/null
+++ b/include/npe/npe.h
@@ -1,0 +1,43 @@
+#ifndef __npe_h__
+#define __npe_h__
+
+#pragma lib "libnpe.a"
+
+#include <u.h>
+#include <libc.h>
+
+/* programs quite often do not include these directly */
+#include </sys/include/stdio.h>
+#include </sys/include/ctype.h>
+#include <stddef.h>
+
+/* some programs do not #include <strings.h> */
+#define strcasecmp cistrcmp
+#define strncasecmp cistrncmp
+
+#ifndef __attribute__
+#define __attribute__(a)
+#endif
+
+/* some programs do not #include <stdlib.h> */
+#ifndef getenv
+#define getenv npe_getenv
+char *npe_getenv(char *s);
+#endif
+
+/* to override "main" with our own threadmain */
+#ifndef __plan9_keep_main__
+#include <thread.h>
+#define main npe_main_renamed
+#pragma lib "libthread.a"
+#ifndef exit
+#define exit(x) threadexitsall(x == 0 ? nil : "error")
+#endif
+#endif
+
+#ifndef exit
+void exits(char *);
+#define exit(x) exits(x == 0 ? nil : "error")
+#endif
+
+#endif
--- a/include/npe/plan9.h
+++ /dev/null
@@ -1,46 +1,0 @@
-#ifndef __plan9_h__
-#define __plan9_h__
-
-#pragma lib "libnpe.a"
-
-#include <u.h>
-#include <libc.h>
-
-/* programs quite often do not include these directly */
-#include </sys/include/stdio.h>
-#include </sys/include/ctype.h>
-#include <stddef.h>
-
-/* some programs do not #include <strings.h> */
-#define strcasecmp cistrcmp
-#define strncasecmp cistrncmp
-
-#ifndef __attribute__
-#define __attribute__(a)
-#endif
-
-/* some programs do not #include <stdlib.h> */
-#ifndef getenv
-#define getenv npe_getenv
-char *npe_getenv(char *s);
-#endif
-
-/* to override "main" with our own threadmain */
-#ifndef __plan9_keep_main__
-#include <thread.h>
-#define main npe_main_renamed
-#pragma lib "libthread.a"
-#ifndef exit
-#define exit(x) threadexitsall(x == 0 ? nil : "error")
-#endif
-#endif
-
-#ifndef exit
-void exits(char *);
-#define exit(x) exits(x == 0 ? nil : "error")
-#endif
-
-uvlong npe_nanosec(void);
-void npe_nsleep(uvlong ns);
-
-#endif
--- a/include/npe/signal.h
+++ b/include/npe/signal.h
@@ -1,7 +1,7 @@
-#ifndef _signal_h_
-#define _signal_h_
+#ifndef _npe_signal_h_
+#define _npe_signal_h_
 
-#include "plan9.h"
+#include <npe.h>
 
 enum {
 	SA_RESETHAND = 1<<0, /* not used */
--- a/include/npe/stdarg.h
+++ b/include/npe/stdarg.h
@@ -1,1 +1,1 @@
-#include "plan9.h"
+#include <npe.h>
--- a/include/npe/stddef.h
+++ b/include/npe/stddef.h
@@ -1,7 +1,7 @@
-#ifndef _stddef_h_
-#define _stddef_h_
+#ifndef _npe_stddef_h_
+#define _npe_stddef_h_
 
-#include "plan9.h"
+#include <npe.h>
 
 typedef long ssize_t;
 typedef ulong size_t;
--- a/include/npe/stdint.h
+++ b/include/npe/stdint.h
@@ -1,7 +1,7 @@
-#ifndef _stdint_h_
-#define _stdint_h_
+#ifndef _npe_stdint_h_
+#define _npe_stdint_h_
 
-#include "plan9.h"
+#include <npe.h>
 #include <limits.h>
 
 typedef s8int int8_t;
--- a/include/npe/stdio.h
+++ b/include/npe/stdio.h
@@ -1,7 +1,7 @@
-#ifndef _stdio_h_
-#define _stdio_h_
+#ifndef _npe_stdio_h_
+#define _npe_stdio_h_
 
-#include "plan9.h"
+#include <npe.h>
 
 int rename(char *old, char *new);
 
--- a/include/npe/stdlib.h
+++ b/include/npe/stdlib.h
@@ -1,1 +1,1 @@
-#include "plan9.h"
+#include <npe.h>
--- a/include/npe/string.h
+++ b/include/npe/string.h
@@ -1,1 +1,1 @@
-#include "plan9.h"
+#include <npe.h>
--- a/include/npe/strings.h
+++ b/include/npe/strings.h
@@ -1,1 +1,1 @@
-#include "plan9.h"
+#include <npe.h>
--- a/include/npe/sys/stat.h
+++ b/include/npe/sys/stat.h
@@ -1,7 +1,7 @@
-#ifndef _sys_stat_h_
-#define _sys_stat_h_
+#ifndef _npe_sys_stat_h_
+#define _npe_sys_stat_h_
 
-#include "plan9.h"
+#include <npe.h>
 
 int mkdir(char *path, int perm);
 
--- a/include/npe/sys/time.h
+++ b/include/npe/sys/time.h
@@ -1,1 +1,1 @@
-#include "plan9.h"
+#include <npe.h>
--- a/include/npe/sys/types.h
+++ b/include/npe/sys/types.h
@@ -1,1 +1,1 @@
-#include "plan9.h"
+#include <npe.h>
--- a/include/npe/time.h
+++ b/include/npe/time.h
@@ -1,7 +1,7 @@
-#ifndef _time_h_
-#define _time_h_
+#ifndef _npe_time_h_
+#define _npe_time_h_
 
-#include "plan9.h"
+#include <npe.h>
 
 typedef long time_t;
 
--- a/include/npe/unistd.h
+++ b/include/npe/unistd.h
@@ -1,7 +1,7 @@
-#ifndef _unistd_h_
-#define _unistd_h_
+#ifndef _npe_unistd_h_
+#define _npe_unistd_h_
 
-#include "plan9.h"
+#include <npe.h>
 
 enum {
 	S_IFMT = 0xff,
--- a/include/npe/wchar.h
+++ b/include/npe/wchar.h
@@ -1,1 +1,1 @@
-#include "plan9.h"
+#include <npe.h>
--- /dev/null
+++ b/libnpe/_npe.c
@@ -1,0 +1,79 @@
+#include <npe.h>
+#include <tos.h>
+#include <sys/stat.h>
+#include "_npe.h"
+
+/*
+ * nsec() is wallclock and can be adjusted by timesync
+ * so need to use cycles() instead, but fall back to
+ * nsec() in case we can't
+ */
+uvlong
+npe_nanosec(void)
+{
+	static uvlong fasthz, xstart;
+	uvlong x, div;
+
+	if(fasthz == ~0ULL)
+		return nsec() - xstart;
+
+	if(fasthz == 0){
+		if(_tos->cyclefreq){
+			cycles(&xstart);
+			fasthz = _tos->cyclefreq;
+		} else {
+			xstart = nsec();
+			fasthz = ~0ULL;
+			fprint(2, "cyclefreq not available, falling back to nsec()\n");
+			fprint(2, "you might want to disable aux/timesync\n");
+			return 0;
+		}
+	}
+	cycles(&x);
+	x -= xstart;
+
+	/* this is ugly */
+	for(div = Nsec; x < 0x1999999999999999ULL && div > 1 ; div /= 10ULL, x *= 10ULL);
+
+	return x / (fasthz / div);
+}
+
+void
+npe_nsleep(uvlong ns)
+{
+	uvlong start, end;
+
+	start = npe_nanosec();
+	end = start + ns;
+	ns = start;
+	do{
+		if(end - ns > 750000000ULL)
+			sleep(70);
+		else if (end - ns > 25000000ULL)
+			sleep(20);
+		else if (end - ns > 10000000ULL)
+			sleep(1);
+		else
+			break;
+		ns = npe_nanosec();
+	}while(ns < end);
+}
+
+int
+npe_mkdirp(char *s, int perm)
+{
+	char *p;
+	int n;
+
+	for(p = strchr(s+1, '/'); p; p = strchr(p+1, '/')){
+		*p = 0;
+		n = access(s, AEXIST) == 0 || mkdir(s, perm) == 0 ? 0 : -1;
+		*p = '/';
+		if(n != 0)
+			return n;
+	}
+	if(access(s, AEXIST) != 0)
+		return mkdir(s, perm);
+
+	return 0;
+}
--- /dev/null
+++ b/libnpe/_npe.h
@@ -1,0 +1,7 @@
+enum {
+	Nsec = 1000000000ULL,
+};
+
+uvlong npe_nanosec(void);
+void npe_nsleep(uvlong ns);
+int npe_mkdirp(char *s, int perm);
--- a/libnpe/_plan9.c
+++ /dev/null
@@ -1,59 +1,0 @@
-#include "plan9.h"
-#include "_plan9.h"
-#include <tos.h>
-
-/*
- * nsec() is wallclock and can be adjusted by timesync
- * so need to use cycles() instead, but fall back to
- * nsec() in case we can't
- */
-uvlong
-npe_nanosec(void)
-{
-	static uvlong fasthz, xstart;
-	uvlong x, div;
-
-	if(fasthz == ~0ULL)
-		return nsec() - xstart;
-
-	if(fasthz == 0){
-		if(_tos->cyclefreq){
-			cycles(&xstart);
-			fasthz = _tos->cyclefreq;
-		} else {
-			xstart = nsec();
-			fasthz = ~0ULL;
-			fprint(2, "cyclefreq not available, falling back to nsec()\n");
-			fprint(2, "you might want to disable aux/timesync\n");
-			return 0;
-		}
-	}
-	cycles(&x);
-	x -= xstart;
-
-	/* this is ugly */
-	for(div = Nsec; x < 0x1999999999999999ULL && div > 1 ; div /= 10ULL, x *= 10ULL);
-
-	return x / (fasthz / div);
-}
-
-void
-npe_nsleep(uvlong ns)
-{
-	uvlong start, end;
-
-	start = npe_nanosec();
-	end = start + ns;
-	ns = start;
-	do{
-		if(end - ns > 750000000ULL)
-			sleep(70);
-		else if (end - ns > 25000000ULL)
-			sleep(20);
-		else if (end - ns > 10000000ULL)
-			sleep(1);
-		else
-			break;
-		ns = npe_nanosec();
-	}while(ns < end);
-}
--- a/libnpe/_plan9.h
+++ /dev/null
@@ -1,3 +1,0 @@
-enum {
-	Nsec = 1000000000ULL,
-};
--- a/libnpe/mkfile
+++ b/libnpe/mkfile
@@ -6,11 +6,11 @@
 HFILES=\
 	_iconv.h\
 	_dirent.h\
-	_plan9.h\
+	_npe.h\
 
 OFILES=\
 	_main.$O\
-	_plan9.$O\
+	_npe.$O\
 	closedir.$O\
 	exp2.$O\
 	getenv.$O\
--- a/libnpe/usleep.c
+++ b/libnpe/usleep.c
@@ -1,4 +1,5 @@
 #include <unistd.h>
+#include "_npe.h"
 
 void
 usleep(useconds_t us)
--- a/libnpe_sdl2/mkfile
+++ b/libnpe_sdl2/mkfile
@@ -1,7 +1,7 @@
 </$objtype/mkfile
 
 LIB=/$objtype/lib/libnpe_sdl2.a
-CFLAGS=$CFLAGS -p -I../include/npe -D__plan9__ -D__${objtype}__
+CFLAGS=$CFLAGS -p -I../include/npe -I../libnpe -D__plan9__ -D__${objtype}__
 
 HFILES=\
 
--- a/libnpe_sdl2/sdl2.c
+++ b/libnpe_sdl2/sdl2.c
@@ -1,4 +1,4 @@
-#include "plan9.h"
+#include <npe.h>
 #include <stdint.h>
 #include <tos.h>
 #include <draw.h>
@@ -8,6 +8,7 @@
 #include <plumb.h>
 #include <SDL2/SDL.h>
 #include <sys/stat.h>
+#include "_npe.h"
 
 typedef struct Audiodev Audiodev;
 
@@ -1339,25 +1340,6 @@
 	return strdup(basepath);
 }
 
-static int
-mkdirp(char *s, int perm)
-{
-	char *p;
-	int n;
-
-	for(p = strchr(s+1, '/'); p; p = strchr(p+1, '/')){
-		*p = 0;
-		n = access(s, AEXIST) == 0 || mkdir(s, perm) == 0 ? 0 : -1;
-		*p = '/';
-		if(n != 0)
-			return n;
-	}
-	if(access(s, AEXIST) != 0)
-		return mkdir(s, perm);
-
-	return 0;
-}
-
 char *
 SDL_GetPrefPath(char *org, char *app)
 {
@@ -1366,7 +1348,7 @@
 	p = nil;
 	if((home = getenv("home")) != nil){
 		if((p = smprint("%s/lib/%s/%s", home, org, app)) != nil)
-			mkdirp(cleanname(p), 0755);
+			npe_mkdirp(cleanname(p), 0755);
 		free(home);
 	}