Sys::Syslog goes the XS way (from Tom Hughes <tom@compton.nu>)
[p5sagit/p5-mst-13.2.git] / ext / Sys / Syslog / Syslog.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include <sys/syslog.h>
6
7 static double
8 constant_LOG_NO(char *name, int len, int arg)
9 {
10     switch (name[6 + 0]) {
11     case 'T':
12         if (strEQ(name + 6, "TICE")) {  /* LOG_NO removed */
13 #ifdef LOG_NOTICE
14             return LOG_NOTICE;
15 #else
16             goto not_there;
17 #endif
18         }
19     case 'W':
20         if (strEQ(name + 6, "WAIT")) {  /* LOG_NO removed */
21 #ifdef LOG_NOWAIT
22             return LOG_NOWAIT;
23 #else
24             goto not_there;
25 #endif
26         }
27     }
28     errno = EINVAL;
29     return 0;
30
31 not_there:
32     errno = ENOENT;
33     return 0;
34 }
35
36 static double
37 constant_LOG_N(char *name, int len, int arg)
38 {
39     switch (name[5 + 0]) {
40     case 'D':
41         if (strEQ(name + 5, "DELAY")) { /* LOG_N removed */
42 #ifdef LOG_NDELAY
43             return LOG_NDELAY;
44 #else
45             goto not_there;
46 #endif
47         }
48     case 'E':
49         if (strEQ(name + 5, "EWS")) {   /* LOG_N removed */
50 #ifdef LOG_NEWS
51             return LOG_NEWS;
52 #else
53             goto not_there;
54 #endif
55         }
56     case 'F':
57         if (strEQ(name + 5, "FACILITIES")) {    /* LOG_N removed */
58 #ifdef LOG_NFACILITIES
59             return LOG_NFACILITIES;
60 #else
61             goto not_there;
62 #endif
63         }
64     case 'O':
65         return constant_LOG_NO(name, len, arg);
66     }
67     errno = EINVAL;
68     return 0;
69
70 not_there:
71     errno = ENOENT;
72     return 0;
73 }
74
75 static double
76 constant_LOG_P(char *name, int len, int arg)
77 {
78     switch (name[5 + 0]) {
79     case 'I':
80         if (strEQ(name + 5, "ID")) {    /* LOG_P removed */
81 #ifdef LOG_PID
82             return LOG_PID;
83 #else
84             goto not_there;
85 #endif
86         }
87     case 'R':
88         if (strEQ(name + 5, "RIMASK")) {        /* LOG_P removed */
89 #ifdef LOG_PRIMASK
90             return LOG_PRIMASK;
91 #else
92             goto not_there;
93 #endif
94         }
95     }
96     errno = EINVAL;
97     return 0;
98
99 not_there:
100     errno = ENOENT;
101     return 0;
102 }
103
104 static double
105 constant_LOG_AU(char *name, int len, int arg)
106 {
107     if (6 + 2 >= len ) {
108         errno = EINVAL;
109         return 0;
110     }
111     switch (name[6 + 2]) {
112     case '\0':
113         if (strEQ(name + 6, "TH")) {    /* LOG_AU removed */
114 #ifdef LOG_AUTH
115             return LOG_AUTH;
116 #else
117             goto not_there;
118 #endif
119         }
120     case 'P':
121         if (strEQ(name + 6, "THPRIV")) {        /* LOG_AU removed */
122 #ifdef LOG_AUTHPRIV
123             return LOG_AUTHPRIV;
124 #else
125             goto not_there;
126 #endif
127         }
128     }
129     errno = EINVAL;
130     return 0;
131
132 not_there:
133     errno = ENOENT;
134     return 0;
135 }
136
137 static double
138 constant_LOG_A(char *name, int len, int arg)
139 {
140     switch (name[5 + 0]) {
141     case 'L':
142         if (strEQ(name + 5, "LERT")) {  /* LOG_A removed */
143 #ifdef LOG_ALERT
144             return LOG_ALERT;
145 #else
146             goto not_there;
147 #endif
148         }
149     case 'U':
150         return constant_LOG_AU(name, len, arg);
151     }
152     errno = EINVAL;
153     return 0;
154
155 not_there:
156     errno = ENOENT;
157     return 0;
158 }
159
160 static double
161 constant_LOG_CR(char *name, int len, int arg)
162 {
163     switch (name[6 + 0]) {
164     case 'I':
165         if (strEQ(name + 6, "IT")) {    /* LOG_CR removed */
166 #ifdef LOG_CRIT
167             return LOG_CRIT;
168 #else
169             goto not_there;
170 #endif
171         }
172     case 'O':
173         if (strEQ(name + 6, "ON")) {    /* LOG_CR removed */
174 #ifdef LOG_CRON
175             return LOG_CRON;
176 #else
177             goto not_there;
178 #endif
179         }
180     }
181     errno = EINVAL;
182     return 0;
183
184 not_there:
185     errno = ENOENT;
186     return 0;
187 }
188
189 static double
190 constant_LOG_C(char *name, int len, int arg)
191 {
192     switch (name[5 + 0]) {
193     case 'O':
194         if (strEQ(name + 5, "ONS")) {   /* LOG_C removed */
195 #ifdef LOG_CONS
196             return LOG_CONS;
197 #else
198             goto not_there;
199 #endif
200         }
201     case 'R':
202         return constant_LOG_CR(name, len, arg);
203     }
204     errno = EINVAL;
205     return 0;
206
207 not_there:
208     errno = ENOENT;
209     return 0;
210 }
211
212 static double
213 constant_LOG_D(char *name, int len, int arg)
214 {
215     switch (name[5 + 0]) {
216     case 'A':
217         if (strEQ(name + 5, "AEMON")) { /* LOG_D removed */
218 #ifdef LOG_DAEMON
219             return LOG_DAEMON;
220 #else
221             goto not_there;
222 #endif
223         }
224     case 'E':
225         if (strEQ(name + 5, "EBUG")) {  /* LOG_D removed */
226 #ifdef LOG_DEBUG
227             return LOG_DEBUG;
228 #else
229             goto not_there;
230 #endif
231         }
232     }
233     errno = EINVAL;
234     return 0;
235
236 not_there:
237     errno = ENOENT;
238     return 0;
239 }
240
241 static double
242 constant_LOG_U(char *name, int len, int arg)
243 {
244     switch (name[5 + 0]) {
245     case 'S':
246         if (strEQ(name + 5, "SER")) {   /* LOG_U removed */
247 #ifdef LOG_USER
248             return LOG_USER;
249 #else
250             goto not_there;
251 #endif
252         }
253     case 'U':
254         if (strEQ(name + 5, "UCP")) {   /* LOG_U removed */
255 #ifdef LOG_UUCP
256             return LOG_UUCP;
257 #else
258             goto not_there;
259 #endif
260         }
261     }
262     errno = EINVAL;
263     return 0;
264
265 not_there:
266     errno = ENOENT;
267     return 0;
268 }
269
270 static double
271 constant_LOG_E(char *name, int len, int arg)
272 {
273     switch (name[5 + 0]) {
274     case 'M':
275         if (strEQ(name + 5, "MERG")) {  /* LOG_E removed */
276 #ifdef LOG_EMERG
277             return LOG_EMERG;
278 #else
279             goto not_there;
280 #endif
281         }
282     case 'R':
283         if (strEQ(name + 5, "RR")) {    /* LOG_E removed */
284 #ifdef LOG_ERR
285             return LOG_ERR;
286 #else
287             goto not_there;
288 #endif
289         }
290     }
291     errno = EINVAL;
292     return 0;
293
294 not_there:
295     errno = ENOENT;
296     return 0;
297 }
298
299 static double
300 constant_LOG_F(char *name, int len, int arg)
301 {
302     switch (name[5 + 0]) {
303     case 'A':
304         if (strEQ(name + 5, "ACMASK")) {        /* LOG_F removed */
305 #ifdef LOG_FACMASK
306             return LOG_FACMASK;
307 #else
308             goto not_there;
309 #endif
310         }
311     case 'T':
312         if (strEQ(name + 5, "TP")) {    /* LOG_F removed */
313 #ifdef LOG_FTP
314             return LOG_FTP;
315 #else
316             goto not_there;
317 #endif
318         }
319     }
320     errno = EINVAL;
321     return 0;
322
323 not_there:
324     errno = ENOENT;
325     return 0;
326 }
327
328 static double
329 constant_LOG_LO(char *name, int len, int arg)
330 {
331     if (6 + 3 >= len ) {
332         errno = EINVAL;
333         return 0;
334     }
335     switch (name[6 + 3]) {
336     case '0':
337         if (strEQ(name + 6, "CAL0")) {  /* LOG_LO removed */
338 #ifdef LOG_LOCAL0
339             return LOG_LOCAL0;
340 #else
341             goto not_there;
342 #endif
343         }
344     case '1':
345         if (strEQ(name + 6, "CAL1")) {  /* LOG_LO removed */
346 #ifdef LOG_LOCAL1
347             return LOG_LOCAL1;
348 #else
349             goto not_there;
350 #endif
351         }
352     case '2':
353         if (strEQ(name + 6, "CAL2")) {  /* LOG_LO removed */
354 #ifdef LOG_LOCAL2
355             return LOG_LOCAL2;
356 #else
357             goto not_there;
358 #endif
359         }
360     case '3':
361         if (strEQ(name + 6, "CAL3")) {  /* LOG_LO removed */
362 #ifdef LOG_LOCAL3
363             return LOG_LOCAL3;
364 #else
365             goto not_there;
366 #endif
367         }
368     case '4':
369         if (strEQ(name + 6, "CAL4")) {  /* LOG_LO removed */
370 #ifdef LOG_LOCAL4
371             return LOG_LOCAL4;
372 #else
373             goto not_there;
374 #endif
375         }
376     case '5':
377         if (strEQ(name + 6, "CAL5")) {  /* LOG_LO removed */
378 #ifdef LOG_LOCAL5
379             return LOG_LOCAL5;
380 #else
381             goto not_there;
382 #endif
383         }
384     case '6':
385         if (strEQ(name + 6, "CAL6")) {  /* LOG_LO removed */
386 #ifdef LOG_LOCAL6
387             return LOG_LOCAL6;
388 #else
389             goto not_there;
390 #endif
391         }
392     case '7':
393         if (strEQ(name + 6, "CAL7")) {  /* LOG_LO removed */
394 #ifdef LOG_LOCAL7
395             return LOG_LOCAL7;
396 #else
397             goto not_there;
398 #endif
399         }
400     }
401     errno = EINVAL;
402     return 0;
403
404 not_there:
405     errno = ENOENT;
406     return 0;
407 }
408
409 static double
410 constant_LOG_L(char *name, int len, int arg)
411 {
412     switch (name[5 + 0]) {
413     case 'F':
414         if (strEQ(name + 5, "FMT")) {   /* LOG_L removed */
415 #ifdef LOG_LFMT
416             return LOG_LFMT;
417 #else
418             goto not_there;
419 #endif
420         }
421     case 'O':
422         return constant_LOG_LO(name, len, arg);
423     case 'P':
424         if (strEQ(name + 5, "PR")) {    /* LOG_L removed */
425 #ifdef LOG_LPR
426             return LOG_LPR;
427 #else
428             goto not_there;
429 #endif
430         }
431     }
432     errno = EINVAL;
433     return 0;
434
435 not_there:
436     errno = ENOENT;
437     return 0;
438 }
439
440 static double
441 constant(char *name, int len, int arg)
442 {
443     errno = 0;
444     if (0 + 4 >= len ) {
445         errno = EINVAL;
446         return 0;
447     }
448     switch (name[0 + 4]) {
449     case 'A':
450         if (!strnEQ(name + 0,"LOG_", 4))
451             break;
452         return constant_LOG_A(name, len, arg);
453     case 'C':
454         if (!strnEQ(name + 0,"LOG_", 4))
455             break;
456         return constant_LOG_C(name, len, arg);
457     case 'D':
458         if (!strnEQ(name + 0,"LOG_", 4))
459             break;
460         return constant_LOG_D(name, len, arg);
461     case 'E':
462         if (!strnEQ(name + 0,"LOG_", 4))
463             break;
464         return constant_LOG_E(name, len, arg);
465     case 'F':
466         if (!strnEQ(name + 0,"LOG_", 4))
467             break;
468         return constant_LOG_F(name, len, arg);
469     case 'I':
470         if (strEQ(name + 0, "LOG_INFO")) {      /*  removed */
471 #ifdef LOG_INFO
472             return LOG_INFO;
473 #else
474             goto not_there;
475 #endif
476         }
477     case 'K':
478         if (strEQ(name + 0, "LOG_KERN")) {      /*  removed */
479 #ifdef LOG_KERN
480             return LOG_KERN;
481 #else
482             goto not_there;
483 #endif
484         }
485     case 'L':
486         if (!strnEQ(name + 0,"LOG_", 4))
487             break;
488         return constant_LOG_L(name, len, arg);
489     case 'M':
490         if (strEQ(name + 0, "LOG_MAIL")) {      /*  removed */
491 #ifdef LOG_MAIL
492             return LOG_MAIL;
493 #else
494             goto not_there;
495 #endif
496         }
497     case 'N':
498         if (!strnEQ(name + 0,"LOG_", 4))
499             break;
500         return constant_LOG_N(name, len, arg);
501     case 'O':
502         if (strEQ(name + 0, "LOG_ODELAY")) {    /*  removed */
503 #ifdef LOG_ODELAY
504             return LOG_ODELAY;
505 #else
506             goto not_there;
507 #endif
508         }
509     case 'P':
510         if (!strnEQ(name + 0,"LOG_", 4))
511             break;
512         return constant_LOG_P(name, len, arg);
513     case 'S':
514         if (strEQ(name + 0, "LOG_SYSLOG")) {    /*  removed */
515 #ifdef LOG_SYSLOG
516             return LOG_SYSLOG;
517 #else
518             goto not_there;
519 #endif
520         }
521     case 'U':
522         if (!strnEQ(name + 0,"LOG_", 4))
523             break;
524         return constant_LOG_U(name, len, arg);
525     case 'W':
526         if (strEQ(name + 0, "LOG_WARNING")) {   /*  removed */
527 #ifdef LOG_WARNING
528             return LOG_WARNING;
529 #else
530             goto not_there;
531 #endif
532         }
533     }
534     errno = EINVAL;
535     return 0;
536
537 not_there:
538     errno = ENOENT;
539     return 0;
540 }
541
542
543 MODULE = Sys::Syslog            PACKAGE = Sys::Syslog           
544
545 char *
546 _PATH_LOG()
547     CODE:
548 #ifdef _PATH_LOG
549         RETVAL = _PATH_LOG;
550 #else
551         croak("Your vendor has not defined the Sys::Syslog macro _PATH_LOG");
552 #endif
553     OUTPUT:
554         RETVAL
555
556 int
557 LOG_FAC(p)
558     INPUT:
559         int             p
560     CODE:
561 #ifdef LOG_FAC
562         RETVAL = LOG_FAC(p);
563 #else
564         croak("Your vendor has not defined the Sys::Syslog macro LOG_FAC");
565 #endif
566     OUTPUT:
567         RETVAL
568
569 int
570 LOG_PRI(p)
571     INPUT:
572         int             p
573     CODE:
574 #ifdef LOG_PRI
575         RETVAL = LOG_PRI(p);
576 #else
577         croak("Your vendor has not defined the Sys::Syslog macro LOG_PRI");
578 #endif
579     OUTPUT:
580         RETVAL
581
582 int
583 LOG_MAKEPRI(fac,pri)
584     INPUT:
585         int             fac
586         int             pri
587     CODE:
588 #ifdef LOG_MAKEPRI
589         RETVAL = LOG_MAKEPRI(fac,pri);
590 #else
591         croak("Your vendor has not defined the Sys::Syslog macro LOG_MAKEPRI");
592 #endif
593     OUTPUT:
594         RETVAL
595
596 int
597 LOG_MASK(pri)
598     INPUT:
599         int             pri
600     CODE:
601 #ifdef LOG_MASK
602         RETVAL = LOG_MASK(pri);
603 #else
604         croak("Your vendor has not defined the Sys::Syslog macro LOG_MASK");
605 #endif
606     OUTPUT:
607         RETVAL
608
609 int
610 LOG_UPTO(pri)
611     INPUT:
612         int             pri
613     CODE:
614 #ifdef LOG_UPTO
615         RETVAL = LOG_UPTO(pri);
616 #else
617         croak("Your vendor has not defined the Sys::Syslog macro LOG_UPTO");
618 #endif
619     OUTPUT:
620         RETVAL
621
622
623 double
624 constant(sv,arg)
625     PREINIT:
626         STRLEN          len;
627     INPUT:
628         SV *            sv
629         char *          s = SvPV(sv, len);
630         int             arg
631     CODE:
632         RETVAL = constant(s,len,arg);
633     OUTPUT:
634         RETVAL
635