usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / usbmodeswitch / jim / jim-syslog.c
blob4e14910549823706d9f78fa09389d9800ca8c7c0
2 /* Syslog interface for tcl
3 * Copyright Victor Wagner <vitus@ice.ru> at
4 * http://www.ice.ru/~vitus/works/tcl.html#syslog
6 * Slightly modified by Steve Bennett <steveb@snapgear.com>
7 * Ported to Jim by Steve Bennett <steveb@workware.net.au>
8 */
9 #include <syslog.h>
10 #include <string.h>
12 #include "jim.h"
13 #include "jimautoconf.h"
15 typedef struct
17 int logOpened;
18 int facility;
19 int options;
20 char ident[32];
21 } SyslogInfo;
23 #ifndef LOG_AUTHPRIV
24 # define LOG_AUTHPRIV LOG_AUTH
25 #endif
27 static const char * const facilities[] = {
28 [LOG_AUTHPRIV] = "authpriv",
29 [LOG_CRON] = "cron",
30 [LOG_DAEMON] = "daemon",
31 [LOG_KERN] = "kernel",
32 [LOG_LPR] = "lpr",
33 [LOG_MAIL] = "mail",
34 [LOG_NEWS] = "news",
35 [LOG_SYSLOG] = "syslog",
36 [LOG_USER] = "user",
37 [LOG_UUCP] = "uucp",
38 [LOG_LOCAL0] = "local0",
39 [LOG_LOCAL1] = "local1",
40 [LOG_LOCAL2] = "local2",
41 [LOG_LOCAL3] = "local3",
42 [LOG_LOCAL4] = "local4",
43 [LOG_LOCAL5] = "local5",
44 [LOG_LOCAL6] = "local6",
45 [LOG_LOCAL7] = "local7",
48 static const char * const priorities[] = {
49 [LOG_EMERG] = "emerg",
50 [LOG_ALERT] = "alert",
51 [LOG_CRIT] = "crit",
52 [LOG_ERR] = "error",
53 [LOG_WARNING] = "warning",
54 [LOG_NOTICE] = "notice",
55 [LOG_INFO] = "info",
56 [LOG_DEBUG] = "debug",
59 /**
60 * Deletes the syslog command.
62 static void Jim_SyslogCmdDelete(Jim_Interp *interp, void *privData)
64 SyslogInfo *info = (SyslogInfo *) privData;
66 if (info->logOpened) {
67 closelog();
69 Jim_Free(info);
72 /* Syslog_Log -
73 * implements syslog tcl command. General format: syslog ?options? level text
74 * options -facility -ident -options
76 * syslog ?-facility cron|daemon|...? ?-ident string? ?-options int? ?debug|info|...? text
78 int Jim_SyslogCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
80 int priority = LOG_INFO;
81 int i = 1;
82 SyslogInfo *info = Jim_CmdPrivData(interp);
84 if (argc <= 1) {
85 wrongargs:
86 Jim_WrongNumArgs(interp, 1, argv,
87 "?-facility cron|daemon|...? ?-ident string? ?-options int? ?debug|info|...? message");
88 return JIM_ERR;
90 while (i < argc - 1) {
91 if (Jim_CompareStringImmediate(interp, argv[i], "-facility")) {
92 int entry =
93 Jim_FindByName(Jim_String(argv[i + 1]), facilities,
94 sizeof(facilities) / sizeof(*facilities));
95 if (entry < 0) {
96 Jim_SetResultString(interp, "Unknown facility", -1);
97 return JIM_ERR;
99 if (info->facility != entry) {
100 info->facility = entry;
101 if (info->logOpened) {
102 closelog();
103 info->logOpened = 0;
107 else if (Jim_CompareStringImmediate(interp, argv[i], "-options")) {
108 long tmp;
110 if (Jim_GetLong(interp, argv[i + 1], &tmp) == JIM_ERR) {
111 return JIM_ERR;
113 info->options = tmp;
114 if (info->logOpened) {
115 closelog();
116 info->logOpened = 0;
119 else if (Jim_CompareStringImmediate(interp, argv[i], "-ident")) {
120 strncpy(info->ident, Jim_String(argv[i + 1]), sizeof(info->ident));
121 info->ident[sizeof(info->ident) - 1] = 0;
122 if (info->logOpened) {
123 closelog();
124 info->logOpened = 0;
127 else {
128 break;
130 i += 2;
133 /* There should be either 0, 1 or 2 args left */
134 if (i == argc) {
135 /* No args, but they have set some options, so OK */
136 return JIM_OK;
139 if (i < argc - 1) {
140 priority =
141 Jim_FindByName(Jim_String(argv[i]), priorities,
142 sizeof(priorities) / sizeof(*priorities));
143 if (priority < 0) {
144 Jim_SetResultString(interp, "Unknown priority", -1);
145 return JIM_ERR;
147 i++;
150 if (i != argc - 1) {
151 goto wrongargs;
153 if (!info->logOpened) {
154 if (!info->ident[0]) {
155 Jim_Obj *argv0 = Jim_GetGlobalVariableStr(interp, "argv0", JIM_NONE);
157 if (argv0) {
158 strncpy(info->ident, Jim_String(argv0), sizeof(info->ident));
160 else {
161 strcpy(info->ident, "Tcl script");
163 info->ident[sizeof(info->ident) - 1] = 0;
165 openlog(info->ident, info->options, info->facility);
166 info->logOpened = 1;
168 syslog(priority, "%s", Jim_String(argv[i]));
170 return JIM_OK;
173 int Jim_syslogInit(Jim_Interp *interp)
175 SyslogInfo *info;
177 if (Jim_PackageProvide(interp, "syslog", "1.0", JIM_ERRMSG))
178 return JIM_ERR;
180 info = Jim_Alloc(sizeof(*info));
182 info->logOpened = 0;
183 info->options = 0;
184 info->facility = LOG_USER;
185 info->ident[0] = 0;
187 Jim_CreateCommand(interp, "syslog", Jim_SyslogCmd, info, Jim_SyslogCmdDelete);
189 return JIM_OK;