File: fortune.cpp

package info (click to toggle)
sip-tester 1%3A3.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,932 kB
  • sloc: cpp: 29,383; xml: 13,436; ansic: 2,381; python: 2,218; sh: 505; makefile: 13
file content (87 lines) | stat: -rw-r--r-- 2,456 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Author : Richard GAYRAUD - 04 Nov 2003
 *           Olivier Jacques
 *           From Hewlett Packard Company.
 *           Charles P Wright from IBM Research
 */
#include "sipp.hpp"

/* This is a fun sample of creating your own extensible keyword. */
int fortune(call *call, MessageComponent *comp, char *buf, int len)
{
    int pipes[2];
    char localbuf[SIPP_MAX_MSG_SIZE];
    char *p = localbuf;
    int ret;
    int written = 0;

    if (pipe(pipes) == -1) {
        ERROR("Could not create pipes!");
    }

    switch (fork()) {
    case -1:
        ERROR("Fork failed: %s", strerror(errno));
    case 0:
        /* We are the child. */
        close(pipes[0]);
        dup2(pipes[1], fileno(stdout));
        dup2(pipes[1], fileno(stderr));
        close(fileno(stdin));
        system("/usr/bin/fortune");
        exit (127);
    default:
        /* We are the parent*/
        close(pipes[1]);
        while ((ret = read(pipes[0], p, sizeof(localbuf) - (p - localbuf))) > 0) {
            p += ret;
        }
        *p = '\0';
        close(pipes[0]);

        if (len > p - localbuf) {
            len = p -localbuf;
        }

        p = localbuf;
        while(len-- > 0) {
            if (*p == '\n') {
                if (len < 3) {
                    break;
                }
                *buf++ = '\r';
                *buf++ = '\n';
                *buf++ = ' ';
                written += 3;
                p++;
            } else {
                *buf++ = *p++;
                written++;
            }
        }
        break;
    }

    return written;
}

/* On initialization we register our keywords. */
extern "C" int init(void)
{
    registerKeyword("fortune", fortune);
    return 0;
}