a9ee487bf22dae07c006eafeb99c1ad8be323f44
[ir-tcl-moved-to-github.git] / ir-tcl.c
1 /*
2  * IR toolkit for tcl/tk
3  * (c) Index Data 1995
4  *
5  * $Id: ir-tcl.c,v 1.1 1995-03-06 17:05:34 adam Exp $
6  */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <sys/time.h>
11
12 #include <comstack.h>
13 #include <tcpip.h>
14 #include <xmosi.h>
15
16 #include <odr.h>
17 #include <proto.h>
18
19 #include <tcl.h>
20
21 #include "ir-tcl.h"
22
23 typedef struct {
24     COMSTACK cs_link;
25 } IRObj;
26
27 /* 
28  * Object method
29  */
30 static int ir_obj_handle (ClientData clientData, Tcl_Interp *interp,
31                       int argc, char **argv)
32 {
33     IRObj *ir = clientData;
34     if (argc < 2)
35     {
36         interp->result = "wrong # args";
37         return TCL_ERROR;
38     }
39     if (!strcmp (argv[1], "comstack"))
40     {
41         if (argc == 3)
42         {
43             if (!strcmp (argv[2], "tcpip"))
44                 ir->cs_link = cs_create (tcpip_type);
45             else if (!strcmp (argv[2], "mosi"))
46                 ir->cs_link = cs_create (mosi_type);
47             else
48             {
49                 interp->result = "wrong comstack type";
50                 return TCL_ERROR;
51             }
52         }
53         if (cs_type(ir->cs_link) == tcpip_type)
54             interp->result = "tcpip";
55         else if (cs_type(ir->cs_link) == mosi_type)
56             interp->result = "comstack";
57     }
58     else if (!strcmp (argv[1], "connect"))
59     {
60         void *addr;
61
62         if (argc < 3)
63         {
64             interp->result = "missing hostname after connect";
65             return TCL_ERROR;
66         }
67         if (cs_type(ir->cs_link) == tcpip_type)
68         {
69             addr = tcpip_strtoaddr (argv[2]);
70             if (!addr)
71             {
72                 interp->result = "tcpip_strtoaddr fail";
73                 return TCL_ERROR;
74             }
75         }
76         else if (cs_type (ir->cs_link) == mosi_type)
77         {
78             addr = mosi_strtoaddr (argv[2]);
79             if (!addr)
80             {
81                 interp->result = "mosi_strtoaddr fail";
82                 return TCL_ERROR;
83             }
84         }
85         if (cs_connect (ir->cs_link, addr) < 0)
86         {
87             interp->result = "cs_connect fail";
88             cs_close (ir->cs_link);
89             return TCL_ERROR;
90         }
91     }
92     return TCL_OK;
93 }
94
95 /* 
96  * Object disposal
97  */
98 static void ir_obj_delete (ClientData clientData)
99 {
100     free ( (void*) clientData);
101 }
102
103 /* 
104  * Object create
105  */
106 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
107               int argc, char **argv)
108 {
109     IRObj *obj;
110
111     if (argc != 2)
112     {
113         interp->result = "wrong # args";
114         return TCL_ERROR;
115     }
116     obj = malloc (sizeof(*obj));
117     if (!obj)
118         return TCL_ERROR;
119     obj->cs_link = cs_create (tcpip_type);
120
121     Tcl_CreateCommand (interp, argv[1], ir_obj_handle,
122                        (ClientData) obj, ir_obj_delete);
123     return TCL_OK;
124 }
125
126 /*
127  * Registration
128  */
129 int ir_tcl_init (Tcl_Interp *interp)
130 {
131     Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
132                        (Tcl_CmdDeleteProc *) NULL);
133     return TCL_OK;
134 }