First work on request queue.
[ir-tcl-moved-to-github.git] / queue.c
1
2 /*
3  * IR toolkit for tcl/tk
4  * (c) Index Data 1995
5  * See the file LICENSE for details.
6  * Sebastian Hammer, Adam Dickmeiss
7  *
8  * $Log: queue.c,v $
9  * Revision 1.1  1995-07-28 10:28:39  adam
10  * First work on request queue.
11  *
12  */
13
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <ctype.h>
17 #include <assert.h>
18
19 #include "ir-tclp.h"
20
21 void *ir_tcl_malloc (size_t size)
22 {
23     void *p = malloc (size);
24     if (!p)
25     {
26         logf (LOG_FATAL, "Out of memory. %d bytes requested", size);
27         exit (1);
28     }
29     return p;
30 }
31
32 int ir_tcl_send (Tcl_Interp *interp, IrTcl_Obj *p, Z_APDU *apdu,
33                  const char *msg)
34 {
35     IrTcl_Request **rp;
36     int empty;
37
38     if (!z_APDU (p->odr_out, &apdu, 0))
39     {
40         Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
41                           NULL);
42         odr_reset (p->odr_out);
43         return TCL_ERROR;
44     }
45     rp = &p->request_queue;
46     empty = *rp ? 0 : 1;
47     while (*rp)
48         rp = &(*rp)->next;
49     *rp = ir_tcl_malloc (sizeof(**rp));
50     (*rp)->next = NULL;
51     (*rp)->state = IR_TCL_R_Queue;
52     (*rp)->buf_out = odr_getbuf (p->odr_out, &(*rp)->len_out, NULL);
53     odr_reset (p->odr_out);
54     if (empty)
55     {
56         int r;
57
58         r = cs_put (p->cs_link, (*rp)->buf_out, (*rp)->len_out);
59         if (r < 0)
60         {
61             sprintf (interp->result, "cs_put failed in %s", msg);
62             return TCL_ERROR;
63         } 
64         else if (r == 1)
65         {
66             ir_select_add_write (cs_fileno (p->cs_link), p);
67             logf (LOG_DEBUG, "Send part of %s", msg);
68             (*rp)->state = IR_TCL_R_Writing;
69         }
70         else
71         {
72             logf (LOG_DEBUG, "Send %s (%d bytes)", msg, (*rp)->len_out);
73             (*rp)->state = IR_TCL_R_Waiting;
74         }
75     }
76     return TCL_OK;
77 }
78