[mw-devel] [Git][arthur/mw][master] 3 commits: duktape: Implement termsize()

Andrew Price welshbyte at sucs.org
Tue Jul 11 12:58:57 BST 2017


Andrew Price pushed to branch master at Justin Mitchell / mw


Commits:
e828bf45 by Andrew Price at 2017-07-11T12:13:32+01:00
duktape: Implement termsize()

- - - - -
d2e403b5 by Andrew Price at 2017-07-11T12:39:14+01:00
duktape: Implement ipc()

- - - - -
7ee44b4e by Andrew Price at 2017-07-11T12:58:53+01:00
duktape: Implement rpc()

- - - - -


3 changed files:

- src/client/js-duk.c
- src/client/talker.c
- src/client/talker.h


Changes:

=====================================
src/client/js-duk.c
=====================================
--- a/src/client/js-duk.c
+++ b/src/client/js-duk.c
@@ -177,6 +177,62 @@ static duk_ret_t js_wholist(duk_context *cx)
 	return 1; /* Array is returned at top of stack */
 }
 
+static duk_ret_t js_rpc(duk_context *cx)
+{
+	const char *username = NULL;
+	const char *rpc_type = NULL;
+	const char *msg = NULL;
+	int broadcast = 0;
+
+	if (duk_is_undefined(cx, -1) ||
+	    duk_is_undefined(cx, -2) ||
+	    duk_is_undefined(cx, -3)) {
+		fprintf(stderr, "mwjs error: rpc() expects 3 arguments\n");
+		return DUK_RET_SYNTAX_ERROR;
+	}
+	if (duk_is_number(cx, -3)) {
+		if (duk_get_int(cx, -3) == K_BROADCAST)
+			broadcast = 1;
+	} else if (duk_is_string(cx, -3)) {
+		username = duk_get_string(cx, -3);
+	}
+	rpc_type = duk_safe_to_string(cx, -2);
+	msg = duk_safe_to_string(cx, -1);
+
+	if((!broadcast && username[0] == '\0') || rpc_type[0] == '\0') {
+		fprintf(stderr, "Error: javascript rpc(): invalid arguments - [%s] [%s]",
+		        (broadcast ? "K_BROADCAST" : username), rpc_type);
+		return DUK_RET_ERROR;
+	}
+	sendrpc(username, rpc_type, msg, broadcast);
+	return 0;
+}
+
+static duk_ret_t js_ipc(duk_context *cx)
+{
+	const char *username = NULL;
+	const char *msg = NULL;
+	int broadcast = 0;
+
+	if (duk_is_undefined(cx, -1) || duk_is_undefined(cx, -2)) {
+		fprintf(stderr, "mwjs error: ipc() expects 2 arguments\n");
+		return DUK_RET_SYNTAX_ERROR;
+	}
+	if (duk_is_number(cx, -2)) {
+		if (duk_get_int(cx, -2) == K_BROADCAST)
+			broadcast = 1;
+	} else if (duk_is_string(cx, -2)) {
+		username = duk_get_string(cx, -2);
+	}
+	msg = duk_safe_to_string(cx, -1);
+	if (broadcast == 0 && (!username || username[0] == '\0')) {
+		fprintf(stderr, "mwjs error: ipc() expects either a username or K_BROADCAST\n");
+		return DUK_RET_ERROR;
+	}
+	sendipc(username, msg, broadcast);
+	return 0;
+}
+
 static duk_ret_t js_beep(duk_context *cx)
 {
 	int i, beeps;
@@ -191,6 +247,18 @@ static duk_ret_t js_beep(duk_context *cx)
 	return 0;
 }
 
+static duk_ret_t js_termsize(duk_context *cx)
+{
+	int idx;
+
+	idx = duk_push_bare_object(cx);
+	duk_push_int(cx, screen_w());
+	duk_put_prop_string(cx, idx, "width");
+	duk_push_int(cx, screen_h());
+	duk_put_prop_string(cx, idx, "height");
+	return 1; /* Result is at top of stack */
+}
+
 static duk_ret_t js_bind(duk_context *cx)
 {
 	const char *bind_name = NULL;
@@ -452,7 +520,10 @@ int setup_js(void)
 	define_func("exec", js_mwexec, 1);
 	define_func("say", js_say, 1);
 	define_func("wholist", js_wholist, 0);
+	define_func("rpc", js_rpc, 3);
+	define_func("ipc", js_ipc, 2);
 	define_func("beep", js_beep, 1);
+	define_func("termsize", js_termsize, 0);
 	define_func("bind", js_bind, 3);
 	define_func("unbind", js_unbind, 2);
 	duk_pop(ctx);


=====================================
src/client/talker.c
=====================================
--- a/src/client/talker.c
+++ b/src/client/talker.c
@@ -1182,7 +1182,7 @@ void set_talk_rights(void)
 	current_rights = RIGHTS_TALK;
 }
 
-void sendipc(char *to, char *text, int bcast)
+void sendipc(const char *to, const char *text, int bcast)
 {
 	int count;
 
@@ -1206,7 +1206,7 @@ void sendipc(char *to, char *text, int bcast)
 	}
 }
 
-void sendrpc(char *to, char *type, char *text, int bcast)
+void sendrpc(const char *to, const char *type, const char *text, int bcast)
 {
 	char buff[MAXTEXTLENGTH];
 	int count;


=====================================
src/client/talker.h
=====================================
--- a/src/client/talker.h
+++ b/src/client/talker.h
@@ -72,8 +72,8 @@ void enter_talker(int logontype);
 int screen_h(void);
 int screen_w(void);
 
-void sendipc(char *to, char *text, int broadcast);
-void sendrpc(char *to, char *type, char *text, int broadcast);
+void sendipc(const char *to, const char *text, int broadcast);
+void sendrpc(const char *to, const char *type, const char *text, int broadcast);
 
 int ChangeRoom(char *room, int quiet);
 void enter_room(int room);



View it on GitLab: https://projects.sucs.org/arthur/mw/compare/9c2720294671d09a5f54e59f71e1f0a0291eebce...7ee44b4ee0dbafb43277eddadad00e5441ef1801

---
View it on GitLab: https://projects.sucs.org/arthur/mw/compare/9c2720294671d09a5f54e59f71e1f0a0291eebce...7ee44b4ee0dbafb43277eddadad00e5441ef1801
You're receiving this email because of your account on projects.sucs.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/mw-devel/attachments/20170711/6e584f6a/attachment-0001.html>


More information about the mw-devel mailing list