~ubuntu-branches/ubuntu/utopic/tcm/utopic

« back to all changes in this revision

Viewing changes to src/gl/link.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2003-07-03 20:08:21 UTC
  • Revision ID: james.westby@ubuntu.com-20030703200821-se4xtqx25e5miczi
Tags: upstream-2.20
ImportĀ upstreamĀ versionĀ 2.20

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
////////////////////////////////////////////////////////////////////////////////
 
2
//
 
3
// This file is part of Toolkit for Conceptual Modeling (TCM).
 
4
// (c) copyright 1996, Vrije Universiteit Amsterdam.
 
5
// Author: Frank Dehne (frank@cs.vu.nl).
 
6
//
 
7
// TCM is free software; you can redistribute it and/or modify
 
8
// it under the terms of the GNU General Public License as published by
 
9
// the Free Software Foundation; either version 2 of the License, or 
 
10
// (at your option) any later version.
 
11
//
 
12
// TCM is distributed in the hope that it will be useful,
 
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
// GNU General Public License for more details.
 
16
//
 
17
// You should have received a copy of the GNU General Public License
 
18
// along with TCM; if not, write to the Free Software
 
19
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
20
// 02111-1307, USA.
 
21
////////////////////////////////////////////////////////////////////////////////
 
22
//
 
23
// Adapted from the book "Motif Programming, The Essentials...and More"
 
24
// by Marshall Brain.
 
25
// Published by Digital Press, ISBN 1-55558-089-0.
 
26
// To order the book, call 1-800-DIGITAL and ask for EY-J816E-DP.
 
27
// 
 
28
// Copyright 1992, by Digital Equipment Corp.
 
29
// 
 
30
// This is the implementation of the link library. 
 
31
//
 
32
#include "link.h"
 
33
#if defined(__CYGWIN__)
 
34
#include <asm/socket.h> // for FIONREAD.
 
35
#endif
 
36
#if defined(LINUX)
 
37
#include <termios.h> // for FIONREAD.
 
38
#elif defined(HPUX) || defined(AIX) || defined(OSF1) || defined(__CYGWIN__)
 
39
#include <sys/ioctl.h>
 
40
#else
 
41
#include <sys/filio.h>
 
42
#endif
 
43
#include <unistd.h>
 
44
#include <sys/types.h>
 
45
#include <sys/wait.h>
 
46
 
 
47
void Link::Open(const char *name, const char *param) {
 
48
        pipe(pipefd1);
 
49
        pipe(pipefd2);
 
50
        if((pid = fork()) == 0) { // child
 
51
                close(pipefd1[0]);
 
52
                close(1);
 
53
                dup(pipefd1[1]);
 
54
                close(2);       // 2 new lines
 
55
                dup(pipefd1[1]);
 
56
                close(pipefd2[1]);
 
57
                close(0);
 
58
                dup(pipefd2[0]);
 
59
                execlp(name, name, param, (char *)0);
 
60
        }
 
61
        else {
 
62
                fpin = fdopen(pipefd1[0], "r");
 
63
                fpout = fdopen(pipefd2[1], "w");
 
64
                close(pipefd1[1]); 
 
65
                close(pipefd2[0]); 
 
66
        }
 
67
}
 
68
 
 
69
void Link::Close() {
 
70
        // wait((union wait *) 0);
 
71
        wait(0);
 
72
        close(pipefd1[1]);
 
73
        close(pipefd2[0]);
 
74
        fclose(fpin);
 
75
        fclose(fpout);
 
76
        pid=0;
 
77
}
 
78
 
 
79
int Link::Read(char *s) {
 
80
        int eof_flag;
 
81
        if (fgets(s, 100, fpin) == 0)
 
82
                eof_flag = 1;  
 
83
                // linked-to process has terminated on its own.
 
84
        else {
 
85
                s[strlen(s)-1] = '\0'; // lose the newline character.
 
86
                eof_flag = 0;
 
87
        }
 
88
        return(eof_flag);
 
89
}
 
90
 
 
91
int Link::InputWaiting() {
 
92
        int num;
 
93
        ioctl(pipefd1[0], FIONREAD, &num); // see how many chars in buffer.
 
94
        return num;
 
95
}
 
96
 
 
97
void Link::WriteChar(char c) {
 
98
        fprintf(fpout, "%c", c);
 
99
        fflush(fpout);
 
100
}
 
101
 
 
102
void Link::Write(const char *s) {
 
103
        fprintf(fpout, "%s\n", s);
 
104
        fflush(fpout);
 
105
}
 
106
 
 
107
void Link::Kill() {
 
108
        kill(pid, SIGKILL);
 
109
        Close();
 
110
}