From f09d66f4481f1da27098409da3c43e22b9186720 Mon Sep 17 00:00:00 2001 From: Victor Wagner Date: Fri, 24 Feb 2006 14:13:07 +0000 Subject: [PATCH] reapplyed 2.0 changes --- Makefile | 13 ++++--- README | 9 +++-- syslog.n | 4 +- tclsyslog.c | 103 +++++++++++++++++++++++++++++++--------------------- 4 files changed, 76 insertions(+), 53 deletions(-) diff --git a/Makefile b/Makefile index 1ec1f84..fc32a3c 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION=1.1 +VERSION=2.0 # This is root of installation tree PREFIX=/usr/local CC=gcc @@ -6,10 +6,11 @@ CC=gcc # Don't forget to change if your CC is not gcc # CFLAGS=-Wall -fPIC -LDFLAGS=-shared -# No need to link with libtcl8.0 on ELF system. Your setup might be -# different -LOADLIBES= +LDFLAGS=-shared -L/usr/local/lib +# Have to set it manually becouse otherwise gcc would pick includes from +# 8.0 on my system +INCLUDES=-I/usr/local/include +LOADLIBES=-ltclstub8.2 # This is where package would be installed LIBDIR=${PREFIX}/lib # On my Debian system this would be @@ -28,7 +29,7 @@ INSTALL=/usr/bin/install all: libsyslog.so.${VERSION} pkgIndex.tcl libsyslog.so.${VERSION}: tclsyslog.o - gcc ${LDFLAGS} -o libsyslog.so.${VERSION} -DVERSION=\"${VERSION}\" tclsyslog.o ${LOADLIBES} + gcc ${LDFLAGS} -o libsyslog.so.${VERSION} -DUSE_TCL_STUBS -DVERSION=\"${VERSION}\" tclsyslog.o ${LOADLIBES} tclsyslog.o: tclsyslog.c ${CC} ${CFLAGS} ${INCLUDES} -DVERSION=\"${VERSION}\" -c tclsyslog.c diff --git a/README b/README index 76e411f..41ebb70 100644 --- a/README +++ b/README @@ -8,10 +8,11 @@ got time for writing proper autoconf configuration, but should be fairly easy to configure and build by anybody who knows what syslog is and why to use it. -This version is designed for Tcl 8.0 or below. It uses old string-based -API and should be compatible with any Tcl version down to 7.4 +This version is designed for Tcl 8.1 and above. It is unicode-aware and +convert messages to current system encoding before sending them to +syslog. And it uses stubs. -If you use tcl 8.1 or above go and get tclsyslog-2.0 +If you use tcl 8.0.x go and get tclsyslog-1.1 Home site of this package is at @@ -21,7 +22,7 @@ Installation 1. Edit variables on the top of Makefile 2. Do make all -3. Test extension by loading it into tclsh via load ./libsyslog.so.1.1 +3. Test extension by loading it into tclsh via load ./libsyslog.so.2.0 and sending couple of messages from command line 4. Verify that PREFIX variable set so that ${PREFIX}/lib is included in your tcl_pkgPath (or set LIBDIR to directory in tcl_pkgPath) diff --git a/syslog.n b/syslog.n index ae90ce0..b74903f 100644 --- a/syslog.n +++ b/syslog.n @@ -2,7 +2,7 @@ '\" Copyright (c) 1999 Victor B. Wagner '\" '\" -'\" RCS: @(#) $Id: syslog.n,v 1.1 2006-02-24 14:11:33 vitus Exp $ +'\" RCS: @(#) $Id: syslog.n,v 1.2 2006-02-24 14:13:07 vitus Exp $ '\" '\" The definitions below are for supplemental macros used in Tcl/Tk '\" manual entries. @@ -65,7 +65,7 @@ '\" .UL arg1 arg2 '\" Print arg1 underlined, then print arg2 normally. '\" -'\" RCS: @(#) $Id: syslog.n,v 1.1 2006-02-24 14:11:33 vitus Exp $ +'\" RCS: @(#) $Id: syslog.n,v 1.2 2006-02-24 14:13:07 vitus Exp $ '\" '\" # Set up traps and other miscellaneous stuff for Tcl/Tk man pages. .if t .wh -1.3i ^B diff --git a/tclsyslog.c b/tclsyslog.c index ca97600..c240905 100644 --- a/tclsyslog.c +++ b/tclsyslog.c @@ -30,22 +30,23 @@ void SyslogHelp(Tcl_Interp *interp,char *cmdname) */ -int Syslog_Log(ClientData data, Tcl_Interp *interp, int argc, char **argv) +int Syslog_Log(ClientData data, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { SyslogInfo *info=(SyslogInfo *)data; - char *message = NULL; + Tcl_DString *message = NULL; int priority; int i=1; - if (argc<=1) { - SyslogHelp(interp,argv[0]); + if (objc<=1) { + SyslogHelp(interp,Tcl_GetString(objv[0])); return TCL_ERROR; } - while (ifacilities,argv[i+1]); + while (ifacilities,facility_name); if (!entry) { - Tcl_AppendResult(interp,"Invalid facility name: \"",argv[i+1], - "\" available facilities:", - NULL); + Tcl_AppendResult(interp,"Invalid facility name: \"",Tcl_GetString(objv[i+1]),"\"", + " available facilities: ", + NULL); Syslog_ListHash(interp,info->facilities); return TCL_ERROR; } @@ -54,43 +55,56 @@ int Syslog_Log(ClientData data, Tcl_Interp *interp, int argc, char **argv) closelog(); info-> logOpened=0; } - } else if (!strcmp(argv[i],"-options")) { - int tmp; - if (Tcl_GetInt(interp,argv[i+1],&tmp)==TCL_ERROR) + } else if (!strncmp(Tcl_GetString(objv[i]),"-options",9)) { + long tmp; + if (Tcl_GetLongFromObj(interp,objv[i+1],&tmp)==TCL_ERROR) return TCL_ERROR; info->options=tmp; if (info->logOpened) { closelog(); info->logOpened=0; } - } else if (!strcmp(argv[i],"-ident")) { - strncpy(info->ident, argv[i+1],32); + } else if (!strncmp(Tcl_GetStringFromObj(objv[i],NULL),"-ident",7)) { + char *ident_name=Tcl_GetString(objv[i+1]); + Tcl_DString *dstring=(Tcl_DString *)Tcl_Alloc(sizeof(Tcl_DString)); + Tcl_DStringInit(dstring); + Tcl_UtfToExternalDString(NULL,ident_name,strlen(ident_name), + dstring); + strncpy(info->ident,Tcl_DStringValue(dstring),32); + Tcl_DStringFree(dstring); + Tcl_Free((char *)dstring); info->ident[31]=0; if (info->logOpened) { closelog(); info->logOpened=0; } } else { - Tcl_HashEntry *entry=Tcl_FindHashEntry(info->priorities,argv[i]); + char *messageutf; + Tcl_HashEntry *entry=Tcl_FindHashEntry(info->priorities,Tcl_GetString(objv[i])); if (!entry) { - Tcl_AppendResult(interp,"Invalid syslog level \"",argv[i], - "\" available levels:", + Tcl_AppendResult(interp,"Invalid syslog level \"",Tcl_GetString(objv[i]),"\"", + " available levels: ", NULL); Syslog_ListHash(interp,info->priorities); return TCL_ERROR; } priority=(int)Tcl_GetHashValue(entry); - message=argv[i+1]; + message=(Tcl_DString *)Tcl_Alloc(sizeof(Tcl_DString)); + Tcl_DStringInit(message); + messageutf=Tcl_GetString(objv[i+1]); + Tcl_UtfToExternalDString(NULL,messageutf,strlen(messageutf), + message); + i+=2; - if (iident,info->options,info->facility); info->logOpened=1; } - syslog(priority,"%s",message); + syslog(priority,"%s",Tcl_DStringValue(message)); + Tcl_DStringFree(message); + Tcl_Free((char *)message); } return TCL_OK; } +/* + * Syslog_Delete - Tcl_CmdDeleteProc for syslog command. + * Frees all hash tables and closes log if it was opened. + */ +void Syslog_Delete(ClientData data) +{ SyslogInfo *info=(SyslogInfo *)data; + Tcl_DeleteHashTable(info->facilities); + Tcl_Free((char *)info->facilities); + Tcl_DeleteHashTable(info->priorities); + Tcl_Free((char *)info->priorities); + if (info->logOpened) { + closelog(); + } + Tcl_Free((char *)info); +} + /* * Syslog_ListHash - appends to interp result all the values of given * hash table @@ -120,21 +152,6 @@ void Syslog_ListHash(Tcl_Interp *interp,Tcl_HashTable *table) } Tcl_Free((char *)searchPtr); } -/* - * Syslog_Delete - Tcl_CmdDeleteProc for syslog command. - * Frees all hash tables and closes log if it was opened. - */ -void Syslog_Delete(ClientData data) -{ SyslogInfo *info=(SyslogInfo *)data; - Tcl_DeleteHashTable(info->facilities); - Tcl_Free((char *)info->facilities); - Tcl_DeleteHashTable(info->priorities); - Tcl_Free((char *)info->priorities); - if (info->logOpened) { - closelog(); - } - Tcl_Free((char *)info); -} /* * My simplified wrapper for add values into hash * @@ -152,7 +169,11 @@ void AddEntry(Tcl_HashTable *table,char *key,int value) */ int Syslog_Init(Tcl_Interp *interp) { char *argv0; - SyslogInfo *info=(SyslogInfo *)Tcl_Alloc(sizeof(SyslogInfo)); + SyslogInfo *info; + if (Tcl_InitStubs(interp,"8.1",0)==NULL) { + return TCL_ERROR; + } + info=(SyslogInfo *)Tcl_Alloc(sizeof(SyslogInfo)); info->logOpened=0; info->options=0; info->facility=LOG_USER; @@ -198,7 +219,7 @@ int Syslog_Init(Tcl_Interp *interp) AddEntry(info->priorities,"notice",LOG_NOTICE); AddEntry(info->priorities,"info",LOG_INFO); AddEntry(info->priorities,"debug",LOG_DEBUG); - Tcl_CreateCommand(interp,"syslog",Syslog_Log,(ClientData) info, + Tcl_CreateObjCommand(interp,"syslog",Syslog_Log,(ClientData) info, Syslog_Delete); return Tcl_PkgProvide(interp,"Syslog",VERSION); } -- 2.39.2