1 
2 /******************************************************************************
3 * MODULE     : tm_link.hpp
4 * DESCRIPTION: Links between TeXmacs and extern programs
5 * COPYRIGHT  : (C) 2000  Joris van der Hoeven
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11 
12 #ifndef TM_LINK_H
13 #define TM_LINK_H
14 #include "tree.hpp"
15 #include "command.hpp"
16 
17 #define CONNECTION_DEAD    0
18 #define CONNECTION_DYING   1
19 #define WAITING_FOR_INPUT  2
20 #define WAITING_FOR_OUTPUT 3
21 
22 #define DATA_ABORT   ((char) 1)
23 #define DATA_BEGIN   ((char) 2)
24 #define DATA_END     ((char) 5)
25 #define DATA_COMMAND ((char) 16)
26 #define DATA_ESCAPE  ((char) 27)
27 
28 #define LINK_IN   0
29 #define LINK_OUT  0
30 #define LINK_ERR  1
31 
32 #define SOCKET_DEFAULT  0
33 #define SOCKET_CLIENT   1
34 #define SOCKET_SERVER   2
35 
36 /******************************************************************************
37 * The tm_link class
38 ******************************************************************************/
39 
40 struct tm_link_rep: abstract_struct {
41   bool   alive;   // link is alive
42   string secret;  // empty string or secret key for encrypted connections
43 
44   command feed_cmd; // called when async data available
45 
46 public:
tm_link_reptm_link_rep47   inline tm_link_rep () {}
~tm_link_reptm_link_rep48   inline virtual ~tm_link_rep () {}
49 
50   virtual string  start () = 0;
51   virtual void    write (string s, int channel) = 0;
52   virtual string& watch (int channel) = 0;
53   virtual string  read (int channel) = 0;
54   virtual void    listen (int msecs) = 0;
55   virtual void    interrupt () = 0;
56   virtual void    stop () = 0;
57 
58   void write_packet (string s, int channel);
59   bool complete_packet (int channel);
60   string read_packet (int channel, int timeout, bool& success);
61   void secure_server (string cmd);
62   void secure_client ();
63 
set_commandtm_link_rep64   void set_command (command _cmd) { feed_cmd = _cmd; }
apply_commandtm_link_rep65   void apply_command () { if (!is_nil (feed_cmd)) feed_cmd->apply (); }
66 
67   friend class tm_link;
68 };
69 
70 class tm_link {
71 public:
72   ABSTRACT_NULL(tm_link);
73   inline bool operator == (tm_link l);
74   inline bool operator != (tm_link l);
75 };
76 
77 ABSTRACT_NULL_CODE(tm_link);
operator ==(tm_link l)78 inline bool tm_link::operator == (tm_link l) { return rep == l.rep; }
operator !=(tm_link l)79 inline bool tm_link::operator != (tm_link l) { return rep != l.rep; }
80 
81 tm_link make_pipe_link (string cmd);
82 tm_link make_dynamic_link (string lib, string symb, string init, string ses);
83 tm_link make_socket_link (string h, int p, int t= SOCKET_DEFAULT, int fd= -1);
84 tm_link make_socket_server (int port);
85 tm_link find_socket_link (int fd);
86 
87 void close_all_pipes ();
88 void process_all_pipes ();
89 void close_all_sockets ();
90 void close_all_servers ();
91 int  number_of_servers ();
92 
93 #endif // TM_LINK_H
94