summaryrefslogtreecommitdiffstats
path: root/src/stream.c
blob: ec2ac6ba6cd555bc5cb1e60169696ac3fb68a5c6 (plain)
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
/* $Id$ */
 
/***
  This file is part of bidilink.
 
  bidilink 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.
   
  bidilink 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 bidilink; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***/
 
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>

#include "stream.h"
#include "std.h"
#include "exec.h"
#include "client-tty.h"
#include "server-tty.h"
#include "client-tcp.h"
#include "client-tcp6.h"
#include "server-tcp.h"
#include "server-tcp6.h"
#include "client-unix.h"
#include "server-unix.h"

struct stream* stream_open(const char *spec) {
    if (!spec || !strncmp(spec, "std:",4))
        return stream_std(spec+4);
    else if (!strncmp(spec, "exec:", 5))
        return stream_exec(spec+5);
    else if (!strncmp(spec, "tty:", 4))
        return stream_client_tty(spec+4);
    else if (!strncmp(spec, "pty:", 4))
        return stream_server_tty(spec+4);
    else if (!strncmp(spec, "tcp-client:", 11))
        return stream_client_tcp(spec+11);
    else if (!strncmp(spec, "tcp6-client:", 12))
        return stream_client_tcp6(spec+12);
    else if (!strncmp(spec, "tcp-server:", 11))
        return stream_server_tcp(spec+11);
    else if (!strncmp(spec, "tcp6-server:", 12))
        return stream_server_tcp6(spec+12);
    else if (!strncmp(spec, "unix-client:", 12))
        return stream_client_unix(spec+12);
    else if (!strncmp(spec, "unix-server:", 12))
        return stream_server_unix(spec+12);

    fprintf(stderr, "Found no stream implementation for '%s'.\n", spec);
    return NULL;
}


void stream_close(struct stream *s) {
    assert(s);

    if (s->destruct)
        s->destruct(s);
    
    if (s->input_fd != -1)
        close(s->input_fd);
    if (s->output_fd != -1 && s->input_fd != s->output_fd)
        close(s->output_fd);

    free(s);
}