--- /dev/null
+/* $Id: js_syslog.c,v 1.1 2006/10/28 22:00:43 mmondor Exp $ */
+
+/*
+ * Copyright (c) 2006, Matthew Mondor
+ * ALL RIGHTS RESERVED.
+ */
+
+
+
+#include <sys/types.h>
+
+#include <stdint.h>
+
+#include <assert.h>
+#include <dirent.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+
+#include <jsapi.h>
+
+#include <js_syslog.h>
+
+
+
+#define QUEUE_EXCEPTION(s) do { \
+ JS_SetPendingException(cx, \
+ STRING_TO_JSVAL(JS_NewStringCopyZ(cx, (s)))); \
+} while (/* CONSTCOND */0)
+
+
+struct property_spec {
+ const char *name;
+ int value;
+};
+
+#define SP(n) \
+ { #n, n }
+
+
+
+/*
+ * Static prototypes
+ */
+static JSBool syslog_constructor(JSContext *, JSObject *, uintN, jsval *,
+ jsval *);
+static void syslog_finalize(JSContext *, JSObject *);
+
+static JSBool syslog_sm_open(JSContext *, JSObject *, uintN, jsval *,
+ jsval *);
+static JSBool syslog_sm_close(JSContext *, JSObject *, uintN, jsval *,
+ jsval *);
+static JSBool syslog_sm_setmask(JSContext *, JSObject *, uintN, jsval *,
+ jsval *);
+static JSBool syslog_sm_mask(JSContext *, JSObject *, uintN, jsval *,
+ jsval *);
+static JSBool syslog_sm_mask_upto(JSContext *, JSObject *, uintN, jsval *,
+ jsval *);
+static JSBool syslog_sm_log(JSContext *, JSObject *, uintN, jsval *,
+ jsval *);
+
+
+
+/*
+ * Static globals
+ */
+
+/* Syslog class */
+static JSClass syslog_class = {
+ "Syslog", 0, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
+ JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub,
+ syslog_finalize
+};
+
+/* Provided methods */
+static JSFunctionSpec syslog_smethods[] = {
+ { "open", syslog_sm_open, 3, 0, 0 },
+ { "close", syslog_sm_close, 0, 0, 0 },
+ { "setMask", syslog_sm_setmask, 1, 0, 0 },
+ { "mask", syslog_sm_mask, 1, 0, 0 },
+ { "maskUpto", syslog_sm_mask_upto, 1, 0, 0 },
+ { "log", syslog_sm_log, 2, 0, 0 },
+ { NULL, NULL, 0, 0, 0 },
+};
+
+/* Static properties */
+
+static struct property_spec syslog_sprops[] = {
+ SP(LOG_EMERG),
+ SP(LOG_ALERT),
+ SP(LOG_CRIT),
+ SP(LOG_ERR),
+ SP(LOG_WARNING),
+ SP(LOG_NOTICE),
+ SP(LOG_INFO),
+ SP(LOG_DEBUG),
+
+ SP(LOG_CONS),
+ SP(LOG_NDELAY),
+ SP(LOG_PERROR),
+ SP(LOG_PID),
+
+ SP(LOG_AUTH),
+ SP(LOG_AUTHPRIV),
+ SP(LOG_CRON),
+ SP(LOG_DAEMON),
+ SP(LOG_FTP),
+ SP(LOG_KERN),
+ SP(LOG_LPR),
+ SP(LOG_MAIL),
+ SP(LOG_NEWS),
+ SP(LOG_SYSLOG),
+ SP(LOG_USER),
+ SP(LOG_UUCP),
+ SP(LOG_LOCAL0),
+ SP(LOG_LOCAL1),
+ SP(LOG_LOCAL2),
+ SP(LOG_LOCAL3),
+ SP(LOG_LOCAL4),
+ SP(LOG_LOCAL5),
+ SP(LOG_LOCAL6),
+ SP(LOG_LOCAL7),
+ { NULL, 0 }
+};
+
+
+
+/*
+ * Syslog object control
+ */
+
+JSObject *
+js_InitSyslogClass(JSContext *cx, JSObject *obj)
+{
+ JSObject *proto, *ctor;
+ struct property_spec *sp;
+
+ if ((proto = JS_InitClass(cx, obj, NULL, &syslog_class,
+ syslog_constructor, 0, NULL, NULL, NULL, syslog_smethods))
+ == NULL) {
+ (void) fprintf(stderr, "Error initializing Syslog class\n");
+ goto err;
+ }
+
+ /* Create static properties. */
+ if ((ctor = JS_GetConstructor(cx, proto)) == NULL) {
+ (void) fprintf(stderr, "Syslog: JS_GetConstructor == NULL\n");
+ return NULL;
+ }
+ for (sp = syslog_sprops; sp->name != NULL; sp++) {
+ if (JS_DefineProperty(cx, ctor, sp->name,
+ INT_TO_JSVAL(sp->value), NULL, NULL,
+ JSPROP_READONLY | JSPROP_PERMANENT) == JS_FALSE) {
+ (void) fprintf(stderr,
+ "Syslog: Error defining property %s\n", sp->name);
+ return NULL;
+ }
+ }
+
+ return proto;
+
+err:
+ return NULL;
+}
+
+static JSBool
+syslog_constructor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
+ jsval *rval)
+{
+
+ QUEUE_EXCEPTION("Not user instanciable");
+ return JS_FALSE;
+}
+
+static void
+syslog_finalize(JSContext *cx, JSObject *obj)
+{
+
+ /* NOOP */
+}
+
+
+
+/* Static methods */
+static JSBool
+syslog_sm_open(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
+ jsval *rval)
+{
+ char *id;
+
+ *rval = OBJECT_TO_JSVAL(NULL);
+
+ if (argc != 3) {
+ QUEUE_EXCEPTION("Wrong number of arguments");
+ return JS_FALSE;
+ }
+ if (!JSVAL_IS_STRING(argv[0]) ||
+ (id = JS_GetStringBytes(JSVAL_TO_STRING(argv[0]))) == NULL) {
+ QUEUE_EXCEPTION("Argument 1 not a String");
+ return JS_FALSE;
+ }
+ if (!JSVAL_IS_INT(argv[1])) {
+ QUEUE_EXCEPTION("Argument 2 not an Integer");
+ return JS_FALSE;
+ }
+ if (!JSVAL_IS_INT(argv[2])) {
+ QUEUE_EXCEPTION("Argument 3 not an Integer");
+ return JS_FALSE;
+ }
+
+ openlog(id, JSVAL_TO_INT(argv[1]), JSVAL_TO_INT(argv[2]));
+
+ return JS_TRUE;
+}
+
+static JSBool
+syslog_sm_close(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
+ jsval *rval)
+{
+
+ *rval = OBJECT_TO_JSVAL(NULL);
+
+ if (argc != 0) {
+ QUEUE_EXCEPTION("Wrong number of arguments");
+ return JS_FALSE;
+ }
+
+ closelog();
+
+ return JS_TRUE;
+}
+
+static JSBool
+syslog_sm_setmask(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
+ jsval *rval)
+{
+ int v;
+
+ if (argc != 1) {
+ QUEUE_EXCEPTION("Wrong number of arguments");
+ goto err;
+ }
+ if (!JSVAL_IS_INT(argv[0])) {
+ QUEUE_EXCEPTION("Argument 1 not an Integer");
+ goto err;
+ }
+
+ v = setlogmask(JSVAL_TO_INT(argv[0]));
+ *rval = INT_TO_JSVAL(v);
+
+ return JS_TRUE;
+
+err:
+ *rval = OBJECT_TO_JSVAL(NULL);
+
+ return JS_FALSE;
+}
+
+static JSBool
+syslog_sm_mask(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
+ jsval *rval)
+{
+ int v;
+
+ if (argc != 1) {
+ QUEUE_EXCEPTION("Wrong number of arguments");
+ goto err;
+ }
+ if (!JSVAL_IS_INT(argv[0])) {
+ QUEUE_EXCEPTION("Argument 1 not an Integer");
+ goto err;
+ }
+
+ v = JSVAL_TO_INT(argv[0]);
+ v = LOG_MASK(v);
+ *rval = INT_TO_JSVAL(v);
+
+ return JS_TRUE;
+
+err:
+ *rval = OBJECT_TO_JSVAL(NULL);
+
+ return JS_FALSE;
+}
+
+static JSBool
+syslog_sm_mask_upto(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
+ jsval *rval)
+{
+ int v;
+
+ if (argc != 1) {
+ QUEUE_EXCEPTION("Wrong number of arguments");
+ goto err;
+ }
+ if (!JSVAL_IS_INT(argv[0])) {
+ QUEUE_EXCEPTION("Argument 1 not an Integer");
+ goto err;
+ }
+
+ v = JSVAL_TO_INT(argv[0]);
+ v = LOG_UPTO(v);
+ *rval = INT_TO_JSVAL(v);
+
+ return JS_TRUE;
+
+err:
+ *rval = OBJECT_TO_JSVAL(NULL);
+
+ return JS_FALSE;
+}
+
+static JSBool
+syslog_sm_log(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
+ jsval *rval)
+{
+ char *str;
+
+ *rval = OBJECT_TO_JSVAL(NULL);
+
+ if (argc != 2) {
+ QUEUE_EXCEPTION("Wrong number of arguments");
+ return JS_FALSE;
+ }
+ if (!JSVAL_IS_INT(argv[0])) {
+ QUEUE_EXCEPTION("Argument 1 not an Integer");
+ return JS_FALSE;
+ }
+ if (!JSVAL_IS_STRING(argv[1]) ||
+ (str = JS_GetStringBytes(JSVAL_TO_STRING(argv[1]))) == NULL) {
+ QUEUE_EXCEPTION("Argument 2 not a String");
+ return JS_FALSE;
+ }
+
+ syslog(JSVAL_TO_INT(argv[0]), "%s", str);
+
+ return JS_TRUE;
+}