static void task_queue_debug(task_queue *q)
{
	int	i;
	GET_GOUT;
	STRLIT("task_queue_debug ");
	for (i = 1; i <= q->curn; i++) {
		NDBG(i, d);
		PTREXP(q->x[i]);
		STREXP(q->x[i]->name);
		NDBG(q->x[i]->heap_pos, d);
		NDBG(q->x[i]->terminate, d);
		NDBG(q->x[i]->time, f);
	}
  PRINT_GOUT;
  FREE_GOUT;
}
/* Initialize socket probe */
static int init_sock_probe(sock_probe *s)
{
  int i= 0, ifrpsize= 0, bufsize= 0;
  bool_t abrt= FALSE;
  char* ptr= NULL, *end= NULL;
  struct ifreq* ifrecc= NULL;
#if TASK_DBUG_ON
  char* if_name  MY_ATTRIBUTE((unused))= NULL;
#endif
#if defined(SA_LEN) || defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
    struct sockaddr *sa MY_ATTRIBUTE((unused))= NULL;
#endif

  /* reset the fields of the structure */
  reset_sock_probe(s);

  /*
   ioctl may overflow without returning an error. Thence we iterate to
   make sure that we don't fill up the buffer. Then, when finally ifc_len
   is smaller than the buffer size, we break the loop.
  */
  do
  {
    bufsize+= IF_INIT_BUF_SIZE;
    if (!(s->ifbuf= (char*)realloc(s->ifbuf, (size_t)bufsize)))
    {
      abrt= TRUE;
      /* Out of memory. */
      goto err;
    }
    memset(&s->ifc,  0, sizeof(s->ifc));
    memset(s->ifbuf, 0, (size_t)bufsize);

    if ((s->tmp_socket = xcom_checked_socket(AF_INET, SOCK_DGRAM, 0).val) == INVALID_SOCKET)
      goto err;

    s->ifc.ifc_len= bufsize;
    s->ifc.ifc_buf= s->ifbuf;
    /* Get information about IP interfaces on this machine.*/
    if (ioctl(s->tmp_socket, SIOCGIFCONF, (char *)&s->ifc)< 0)
    {
      DBGOUT(NUMEXP(errno); STREXP(strerror(errno)););
      abrt= TRUE;
      goto err;
    }
/* Basic operations on tasks */
static task_env *activate(task_env *t)
{
	if (t) {
		MAY_DBG(FN;
		    STRLIT("activating task ");
		    PTREXP(t);
		    STREXP(t->name);
		    NDBG(t->heap_pos, d);
		    NDBG(t->time, f);
		    );
		assert(ash_nazg_gimbatul.type == type_hash("task_env"));
		if (t->heap_pos)
			task_queue_remove(&task_time_q, t->heap_pos);
		link_into(&t->l, &tasks);
		t->time = 0.0;
		t->heap_pos = 0;
		assert(ash_nazg_gimbatul.type == type_hash("task_env"));
	}
int main(int argc, char *argv[])
{
    int sock;
    char *dest, *port = STREXP(I2CBRIDGE_PORT);
    int verbose = 0;
    
    int opt, ret;
    struct addrinfo hints, *hres, *iter;
    struct i2cbridge_request req;
    struct i2cbridge_response res;
    
    while((opt = getopt(argc, argv, "vp:")) != -1)
    {
        switch(opt)
        {
        case 'p':
            if(strspn(optarg, "1234567890") != strlen(optarg))
            {
                printf("port not numeric\n");
                return -1;
            }
            port = optarg;
            break;
        case 'v':
            verbose = 1;
            break;
        default:
            usage(argv[0]);
        }
    }
    
    if(argc-optind < 4 || argc-optind > 5)
        usage(argv[0]);
    
    dest = argv[optind];
    memset(&req, 0, sizeof(struct i2cbridge_request));
    
    if(!strcmp("read8", argv[optind+1]))
        req.cmd = I2CBRIDGE_CMD_READ8;
    else if(!strcmp("read16", argv[optind+1]))
        req.cmd = I2CBRIDGE_CMD_READ16;
    else if(!strcmp("write8", argv[optind+1]))
        req.cmd = I2CBRIDGE_CMD_WRITE8;
    else if(!strcmp("write16", argv[optind+1]))
        req.cmd = I2CBRIDGE_CMD_WRITE16;
    else
        usage(argv[0]);
    
    if(!sscanf(argv[optind+2], "%hhx", &req.addr))
    {
        printf("addr not hex\n");
        return -1;
    }
    if(!sscanf(argv[optind+3], "%hhx", &req.reg))
    {
        printf("reg not hex\n");
        return -1;
    }
    if(optind == argc-5 && !sscanf(argv[optind+4], "%hx", &req.data))
    {
        printf("data not hex\n");
        return -1;
    }
    
    if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        perror("Failed to open socket");
        return -2;
    }
    
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    
    if((ret = getaddrinfo(dest, port, &hints, &hres)) != 0)
    {
        printf("Failed to resolve server: %s\n", gai_strerror(ret));
        return -3;
    }
    
    for(iter=hres; iter; iter=iter->ai_next)
        if(!(ret = connect(sock, iter->ai_addr, iter->ai_addrlen)))
            break;
    
    freeaddrinfo(hres);
    
    if(ret == -1)
    {
        perror("Failed to connect");
        return -4;
    }
    
    if(send(sock, &req, sizeof(struct i2cbridge_request), 0) == -1)
    {
        perror("Failed to send");
        return -5;
    }
    
    if(recv(sock, &res, sizeof(struct i2cbridge_response), 0) == -1)
    {
        perror("Failed to recv");
        return -6;
    }
    
    switch(res.status)
    {
    case I2CBRIDGE_ERROR_OK:
        printfv("response: ok\ndata: 0x");
        printf("%04hx\n", res.data);
        break;
    case I2CBRIDGE_ERROR_INTERNAL:
        printfv("response: internal error\n");
        break;
    case I2CBRIDGE_ERROR_COMMAND:
        printfv("response: unknown command\n");
        break;
    case I2CBRIDGE_ERROR_ADDRESS:
        printfv("response: device with address not found\n");
        break;
    case I2CBRIDGE_ERROR_I2C:
        printfv("response: error while accessing i2c bus\n");
        break;
    }
    
    close(sock);
    
    return res.status;
}