示例#1
0
文件: scene7.c 项目: artyfarty/bb-osx
static void ui_do_fractal(int mode)
{
    int time, time1;
    if (mode < fastmode + 1)
	interruptiblemode = 1;
    else
	interruptiblemode = 0;
    if (tbreak)
	tl_reset_timer(maintimer);
    incalculation = 1;
    starttime1 = 0;
    endtime1 = 0;
    set_view(zcontext, &zcontext->s);
    waitcount = tl_lookup_timer(maintimer) / WAITTIME + 2;
    waitcount1 = tl_lookup_timer(maintimer) / WAITTIME1 + 1;
    do_fractal(zcontext, interruptiblemode);
    bbupdate();
    if (interruptiblemode) {
	bbupdate();
	time1 = time = tl_lookup_timer(maintimer);
	time -= endtime1 - starttime1;
	maxtime = time * 5;
	if (maxtime > 1000000 / 10)
	    maxtime = time * 3;
	if (maxtime < 1000000 / 30)
	    maxtime = 1000000 / 30;
	maxtime -= time1 - endtime1;
    }
    updatestatus();
}
示例#2
0
文件: status.c 项目: Bgods/r-source
void setstatus(const char *text)
{
    strncpy(MDIStatusText, text, 255);
    if (!MDIStatus || !current_window) return;
    strncpy(current_window->status, text, 255);
    updatestatus(text);
}
示例#3
0
MyWidget::MyWidget(QGLWidget *parent)
  : QGLWidget(QGLFormat(), parent)
{

  setMouseTracking(true);

  setPaletteBackgroundColor(QColor(255,255,255));

  setstatus();

  menu = new QMenuBar(this, "Menu bar");
  file = new QPopupMenu( this , "file");
  save = new QPopupMenu( this , "save");
  placement = new QPopupMenu( this , "placement");
  options = new QPopupMenu( this , "options");
  view = new QPopupMenu( this , "viewing");
  save->insertItem( "Save &eps file", this, SLOT(savedepsfile()), CTRL+Key_E );
  save->insertItem( "Save &stl file", this, SLOT(savedstlfile()), CTRL+Key_S );
  file->insertItem( "&Load", this, SLOT(openedfile()), CTRL+Key_L );
  generate_id = placement->insertItem( "&Generate", &p, SLOT(generate()), CTRL+Key_G );
  generatefirst_id = placement->insertItem( "Generate &First", &p, SLOT(generateFirst()), CTRL+Key_F );
  generatenext_id = placement->insertItem( "Generate &Next", &p, SLOT(generateNext()), CTRL+Key_N );
  generateten_id = placement->insertItem( "Next &ten streamlines", &p, SLOT(generateTen()), CTRL+Key_T );
  generateresume_id = placement->insertItem( "&Resume the placement", &p, SLOT(generateAll()), CTRL+Key_C );
  clear_id = placement->insertItem( "&Clear", &p, SLOT(purge()), CTRL+Key_M );
  drawstl_id = view->insertItem( "&Draw streamlines", &p, SLOT(draw_stl()), CTRL+Key_D );
  drawpq_id = view->insertItem( "Draw &queue elements", &p, SLOT(draw_pq()), CTRL+Key_Q );
  drawtr_id = view->insertItem( "Draw t&riangulation", &p, SLOT(draw_tr()), CTRL+Key_R );
  drawbc_id = view->insertItem( "Draw &biggest circle", &p, SLOT(draw_bc()), CTRL+Key_B );
  addimage_id = placement->insertItem( "&Image", this, SLOT(openedimage()), CTRL+Key_I );
  options->insertItem( "Density...", &p, SLOT(density()));
  options->insertItem( "Saturation ration...", &p, SLOT(ratio()));
  options->insertItem( "Sampling step...", &p, SLOT(sampling()));
  options->insertItem( "Integrating step...", &p, SLOT(integrating()));
  placement->insertItem( "&Options ", options );
  save_id = file->insertItem( "&Save", save );
  menu->insertItem( "&File", file );
  menu->insertItem( "&Placement", placement );
  view_id = menu->insertItem( "&View ", view );
  file->insertItem( "&Quit", qApp, SLOT(quit()), ALT+Key_F4 );

  // desable all generator menu items
  placement->setItemEnabled(generate_id, false);
  placement->setItemEnabled(generatefirst_id, false);
  placement->setItemEnabled(generatenext_id, false);
  placement->setItemEnabled(generateten_id, false);
  placement->setItemEnabled(generateresume_id, false);
  placement->setItemEnabled(clear_id, false);

  menu->setItemEnabled(view_id, false);

  placement->setItemEnabled(addimage_id, false);
  file->setItemEnabled(save_id, false);


  connect(this, SIGNAL(fileloaded(const QString &)), &p, SLOT(load(const QString &)));
  connect(this, SIGNAL(imageloaded(const QString &)), &p, SLOT(image(const QString &)));
  connect(&p, SIGNAL(optionschanged()), this, SLOT(updatestatus()));

}
示例#4
0
文件: gui.c 项目: FliPPeh/dcpu16tools
void updategui(dcpu16 *cpu) {
    mvprintw(0, 0, "dcpu16emu");
    mvhline(1, 0, ACS_BULLET, 10);


    updatescreen(cpu);
    updatestatus(cpu);
}
示例#5
0
void updatetext()
{
  QString text="";
  for (int i = 0; i < textresult.size(); ++i)
  {
   text+="["+QString::number(i+1)+"] "+textresult[i];
  }
  textEdt->setText(text);
  updatestatus();
}
示例#6
0
文件: main.c 项目: jralls/Wish2
static int update_state(int dir, int hc, int uc, int fc,unsigned char *buf, int len)
{
  int i,ret=0;

  dsyslog(LOG_INFO,"update_state:  dir=%d, hc=0x%x, uc=0x%x, fc=0x%x\n",dir,hc,uc,fc);
  hc &= 0x0f;

  sem_getvalue(&state.lock,&i);
  dsyslog(LOG_INFO,"semaphore value:  %d\n",i);
  sem_wait(&state.lock);
  dsyslog(LOG_INFO,"Got the state lock\n");

  // if we get a preset dim high/low, we can store the level only if:
  //   the previous housecode was stored
  //   the previous message received was not a function (had to be unit)
  //   a valid unit is stored
  if ((fc == X10_CMD_PRESETDIMHIGH || fc == X10_CMD_PRESETDIMLOW)
      && (state.fcsent[state.hc] != 1)
      && (state.lastuc != -1)) {
    updatestatus(state.hc,state.lastuc,fc,fc == X10_CMD_PRESETDIMLOW ? preset_low[hc] : preset_high[hc]);
  }
  if (hc != state.hc) {    // restart if new housecode received
    state.hc = hc;
    state.lastuc = -1;
    state.fc = -1;
  }
  if (uc >= 0 && uc < MAX_UNITS) {
    if (state.fcsent[state.hc]) {
      clear_uc(state.hc);
      state.fcsent[state.hc] = 0;
    }
    state.uc[state.hc][uc] = 1;
    state.lastuc = uc;
  }
  if (fakereceive == 1 || dir == 0)
    updatelog(dir,hc,uc,fc);
  // something just changed so now update the queue head to wake everyone up

  if (fc >= 0) {
    // we have a completed command
    // we may never have received a unit code so it may still be -1
//              if (fc != X10_CMD_STATUS)
      state.fcsent[state.hc] = 1;
    state.fc = fc;
    switch (fc) {
    case X10_CMD_ALLLIGHTSOFF:      // we don't know which ones are lights
    case X10_CMD_ALLUNITSOFF:
      for (i = 0; i < MAX_HOUSECODES; i++) {
        updatestatus(state.hc,i,fc,0);
      }
      break;
    case X10_CMD_ALLLIGHTSON:
      for (i = 0; i < MAX_HOUSECODES; i++) {
        updatestatus(state.hc,i,fc,100);
      }
      break;
    case X10_CMD_ON:
      for (i = 0; i < MAX_UNITS; i++)
        if (state.uc[state.hc][i]) {
          updatestatus(state.hc,i,fc,100);
        }
      break;
    case X10_CMD_OFF:
      for (i = 0; i < MAX_UNITS; i++)
        if (state.uc[hc][i]) {
          updatestatus(state.hc,i,fc,0);
        }
      break;
    case X10_CMD_DIM:
      for (i = 0; i < MAX_UNITS; i++)
        if (state.uc[hc][i]) {
          int value = state.status[state.hc][i];
          value -= 6;
          if (value < 0)
            value = 0;
          updatestatus(state.hc,i,fc,value);
        }
      break;
    case X10_CMD_BRIGHT:
      for (i = 0; i < MAX_UNITS; i++)
        if (state.uc[hc][i]) {
          int value = state.status[state.hc][i];
          value += 6;
          if (value > 100)
            value = 100;
          updatestatus(state.hc,i,fc,value);
        }
      break;
    case X10_CMD_STATUS:
      dsyslog(LOG_INFO,"status request to housecode %c",'A'+state.hc);
      break;
    case X10_CMD_STATUSOFF:
      dsyslog(LOG_INFO,"status of %c%d is OFF",'A'+state.hc,state.lastuc+1);
      if (state.hc < MAX_HOUSECODES && state.lastuc < MAX_UNITS) {
        updatestatus(state.hc,state.lastuc,fc,0);
      }
      break;
    case X10_CMD_STATUSON:
      dsyslog(LOG_INFO,"status of %c%d is ON",'A'+state.hc,state.lastuc+1);
      if (state.hc < MAX_HOUSECODES && state.lastuc < MAX_UNITS) {
        updatestatus(state.hc,state.lastuc,fc,100);
      }
      break;
    case X10_CMD_HAILREQUEST:
      dsyslog(LOG_INFO,"hail request to housecode %c",'A'+state.hc);
      break;
    case X10_CMD_HAILACKNOWLEDGE:
      dsyslog(LOG_INFO,"hail ACK to housecode %c",'A'+state.hc);
      break;
    case X10_CMD_EXTENDEDCODE:      // extended code
      dsyslog(LOG_INFO,"extended code to housecode %c",'A'+state.hc);
      if (len > 0){
        ret = -1;
      }
      break;
    case X10_CMD_EXTENDEDDATAA:     // extended data (analog)
      dsyslog(LOG_INFO,"extended data (analog) to housecode %c", 'A' + state.hc);
      if (len > 0){
        ret = -1;
      }
      break;
    case X10_CMD_PRESETDIMHIGH:     // already handled
      dsyslog(LOG_INFO,"preset dim low %c%d",'A'+state.hc,state.lastuc);
      break;
    case X10_CMD_PRESETDIMLOW:      // already handled
      dsyslog(LOG_INFO,"preset dim low %c%d",'A'+state.hc,state.lastuc);
      break;
    default:
      ret = -1;
      break;
    }  // end of switch statement
  } // end of if (fc >= 0)
  dsyslog(LOG_INFO,"Released the state lock\n");
  sem_post(&state.lock);
  return ret;
}
示例#7
0
文件: logger.cpp 项目: koo5/lemon-2
    void slogit(char *s)
    {
	if(!strcmp(s,status))
	    dirty = 1;
	updatestatus(s);
    }
示例#8
0
 void paintGL()
 {
   updatestatus();
   drawing();
 }
示例#9
0
static int send_file( int fd, ATP atp, int lastfile)
{
    struct timeval	stv, tv;
    struct sockaddr_at	ssat;
    struct atp_block	atpb;
    fd_set		fds;
    int			fiovcnt = 0, eof = 0, senteof = 0, to = 0;
    int			cc, i;
    unsigned short	netseq;

    if ( gettimeofday( &stv, NULL ) < 0 ) {
	perror( "gettimeofday" );
	exit( 2 );
    }

    /*
     * Ask for more data.
     */
    cbuf[ 0 ] = connid;
    cbuf[ 1 ] = PAP_READ;
    if ( ++seq == 0 ) seq = 1;
    netseq = htons( seq );
    memcpy( cbuf +  2, &netseq, sizeof( netseq ));
    atpb.atp_saddr = &sat;
    atpb.atp_sreqdata = cbuf;
    atpb.atp_sreqdlen = 4;		/* bytes in SendData request */
    atpb.atp_sreqto = 15;		/* retry timer */
    atpb.atp_sreqtries = -1;		/* retry count */
    if ( atp_sreq( atp, &atpb, oquantum, ATP_XO ) < 0 ) {
	perror( "atp_sreq" );
	exit( 1 );
    }

	if(debug){ printf( "READ %d >\n", seq ), fflush( stdout );}

    for (;;) {
	if ( gettimeofday( &tv, NULL ) < 0 ) {
	    perror( "gettimeofday" );
	    exit( 2 );
	}

	if (( tv.tv_sec - stv.tv_sec ) >= 60 ) {
	    stv = tv;

	    /*
	     * Send a tickle.
	     */
	    cbuf[ 0 ] = connid;
	    cbuf[ 1 ] = PAP_TICKLE;
	    cbuf[ 2 ] = cbuf[ 3 ] = 0;
	    atpb.atp_saddr = &sat;
	    atpb.atp_sreqdata = cbuf;
	    atpb.atp_sreqdlen = 4;		/* bytes in Tickle request */
	    atpb.atp_sreqto = 0;		/* retry timer */
	    atpb.atp_sreqtries = 1;		/* retry count */
	    if ( atp_sreq( satp, &atpb, 0, 0 ) < 0 ) {
		perror( "atp_sreq" );
		exit( 1 );
	    }

	if(debug){ printf( "TICKLE >\n" ), fflush( stdout );}
	}

	tv.tv_sec = stv.tv_sec + 60 - tv.tv_sec;
	tv.tv_usec = 0;

	FD_ZERO( &fds );
	if ( !waitforprinter && !eof && fiovcnt == 0 ) {
	    FD_SET( fd, &fds );
	}
	FD_SET( atp_fileno( atp ), &fds );

	if (( cc = select( FD_SETSIZE, &fds, NULL, NULL, &tv )) < 0 ) {
	    perror( "select" );
	    exit( 2 );
	}

	/*
	 * A timeout has occured. Keep track of it.
	 */
	if ( cc == 0 ) {
	    if ( to++ > 2 ) {
		fprintf( stderr, "Connection timed out.\n" );
		exit( 1 );
	    }
	    continue;
	}

	/*
	 * Read data.
	 */
	if ( !fiovcnt && FD_ISSET( fd, &fds )) {
	    for ( i = 0; i < quantum; i++ ) {
		rfiov[ i ].iov_len = PAP_MAXDATA;
	    }
	    if (( cc = readv( fd, rfiov, quantum )) < 0 ) {
		perror( "readv" );
		exit( 2 );
	    }
	    if ( cc == 0 ) {
		eof = 1;
	    }
	    fiovcnt = cc / PAP_MAXDATA + ( cc % PAP_MAXDATA > 0 );
	    for ( i = 0; cc > 0; i++ ) {
		rfiov[ i ].iov_len = ( cc > PAP_MAXDATA ) ? PAP_MAXDATA : cc;
		cc -= ( cc > PAP_MAXDATA ) ? PAP_MAXDATA : cc;
	    }
	}

	if ( FD_ISSET( atp_fileno( atp ), &fds )) {
	    ssat = sat;
	    ssat.sat_port = ATADDR_ANYPORT;
	    switch( atp_rsel( atp, &ssat, ATP_TRESP | ATP_TREQ )) {
	    case ATP_TREQ :
		atpb.atp_saddr = &ssat;
		atpb.atp_rreqdata = cbuf;
		atpb.atp_rreqdlen = sizeof( cbuf );
		if ( atp_rreq( atp, &atpb ) < 0 ) {
		    perror( "atp_rreq" );
		    exit( 1 );
		}

		if ( (unsigned char)cbuf[ 0 ] != connid ) {
		    break;
		}

		/* reset timeout counter for all valid requests */
		to = 0;

		switch ( cbuf[ 1 ] ) {
		case PAP_READ :
		    memcpy( cbuf +  2, &netseq, sizeof( netseq ));
	if(debug){ printf( "< READ %d\n", ntohs( netseq )), fflush( stdout );}
#ifdef notdef
		    if ( netseq != 0 ) {
			if ( rseq != ntohs( netseq )) {
	if(debug){ printf( "| DUP %d\n", rseq ), fflush( stdout );}
			    break;
			}
			if ( rseq++ == 0xffff ) rseq = 1;
		    }
#endif /* notdef */

		    data = 1;
		    port = ssat.sat_port;
		    break;

		case PAP_CLOSE :

	if(debug){ printf( "< CLOSE\n" ), fflush( stdout );}

		    /*
		     * Respond to the close request, and fail.
		     */
		    sniov[ 0 ].iov_len = 4;
		    ((char *)sniov[ 0 ].iov_base)[ 0 ] = connid;
		    ((char *)sniov[ 0 ].iov_base)[ 1 ] = PAP_CLOSEREPLY;
		    ((char *)sniov[ 0 ].iov_base)[ 2 ] =
			    ((char *)sniov[ 0 ].iov_base)[ 3 ] = 0;
		    atpb.atp_sresiov = sniov;
		    atpb.atp_sresiovcnt = 1;
		    if ( atp_sresp( atp, &atpb ) < 0 ) {
			perror( "atp_sresp" );
			exit( 1 );
		    }

	if(debug){ printf( "CLOSEREPLY >\n" ), fflush( stdout );}

		    fprintf( stderr, "Connection closed by foreign host.\n" );
		    exit( 1 );

		case PAP_TICKLE :

	if(debug){ printf( "< TICKLE\n" ), fflush( stdout );}

		    break;
		default :
		    fprintf( stderr, "Bad PAP request!\n" );
		    exit( 1 );
		}
		break;

	    case ATP_TRESP :
		/* reset timeout counter for all valid requests */
		to = 0;

		atpb.atp_saddr = &ssat;
		for ( i = 0; i < oquantum; i++ ) {
		    rniov[ i ].iov_len = PAP_MAXDATA + 4;
		}
		atpb.atp_rresiov = rniov;
		atpb.atp_rresiovcnt = oquantum;
		if ( atp_rresp( atp, &atpb ) < 0 ) {
		    perror( "atp_rresp" );
		    exit( 1 );
		}

#ifndef ZEROCONNID
		/*
		 * The HP LJIIISI w/ BridgePort LocalTalk card sends
		 * zero instead of the connid.
		 */
		if ( ((unsigned char *)rniov[ 0 ].iov_base)[ 0 ] != connid ) {
		    fprintf( stderr, "Bad data response!\n" );
		    exit( 1 );
		}
#endif /* ZEROCONNID */
		if ( ((char *)rniov[ 0 ].iov_base)[ 1 ] != PAP_DATA ) {
		    fprintf( stderr, "Bad data response!\n" );
		    exit( 1 );
		}

		for ( cc = 0, i = 0; i < atpb.atp_rresiovcnt; i++ ) {
		    sfiov[ i ].iov_len = rniov[ i ].iov_len - 4;
		    cc += sfiov[ i ].iov_len;
		}
		if ( cc && writev( 1, sfiov, atpb.atp_rresiovcnt ) < cc ) {
		    perror( "writev" );
		    exit( 2 );
		}

		/* eof */
		if ( ((char *)rniov[ 0 ].iov_base)[ 2 ] ) {

	if(debug){ printf( "< DATA (eof)\n" ), fflush( stdout );}

		    return( 0 );
		}

	if(debug){ printf( "< DATA\n" ), fflush( stdout );}


		/*
		 * Ask for more data.
		 */
		cbuf[ 0 ] = connid;
		cbuf[ 1 ] = PAP_READ;
		if ( ++seq == 0 ) seq = 1;
		netseq = htons( seq );
		memcpy( cbuf +  2, &netseq, sizeof( netseq ));
		atpb.atp_saddr = &sat;
		atpb.atp_sreqdata = cbuf;
		atpb.atp_sreqdlen = 4;		/* bytes in SendData request */
		atpb.atp_sreqto = 15;		/* retry timer */
		atpb.atp_sreqtries = -1;	/* retry count */
		if ( atp_sreq( atp, &atpb, oquantum, ATP_XO ) < 0 ) {
		    perror( "atp_sreq" );
		    exit( 1 );
		}

	if(debug){ printf( "READ %d >\n", seq ), fflush( stdout );}

		break;

	    case 0:

	if(debug){ printf( "| RETRANS\n" ), fflush( stdout );}

		break;

	    default:
		perror( "atp_rsel" );
		exit( 1 );
	    }
	}

	/*
	 * Send whatever is pending.
	 */
	if ( !waitforprinter && !senteof && data && ( fiovcnt || eof )) {
	    ssat.sat_port = port;
	    atpb.atp_saddr = &ssat;
	    if ( fiovcnt ) {
		for ( i = 0; i < fiovcnt; i++ ) {
		    sniov[ i ].iov_len = rfiov[ i ].iov_len + 4;
		    ((char *)sniov[ i ].iov_base)[ 0 ] = connid;
		    ((char *)sniov[ i ].iov_base)[ 1 ] = PAP_DATA;
		    senteof = ((char *)sniov[ i ].iov_base)[ 2 ] = eof;
		    ((char *)sniov[ i ].iov_base)[ 3 ] = 0;
		}
	    } else {
		sniov[ 0 ].iov_len = 4;
		((char *)sniov[ 0 ].iov_base)[ 0 ] = connid;
		((char *)sniov[ 0 ].iov_base)[ 1 ] = PAP_DATA;
		senteof = ((char *)sniov[ 0 ].iov_base)[ 2 ] = eof;
		((char *)sniov[ 0 ].iov_base)[ 3 ] = 0;
	    }
	    atpb.atp_sresiov = sniov;
	    atpb.atp_sresiovcnt = fiovcnt ? fiovcnt : 1;
	    if ( atp_sresp( atp, &atpb ) < 0 ) {
		perror( "atp_sresp" );
		exit( 1 );
	    }
	    data = fiovcnt = 0;

	if(debug){ printf( "DATA %s\n", eof ? "(eof) >" : ">" ), fflush( stdout );}

	    /*
	     * The Apple LaserWriter IIf, the HP LWIIISi, and IV, don't
	     * seem to send us an EOF on large jobs.  To work around
	     * this heinous protocol violation, we won't wait for their
	     * EOF before closing.
	     */
	    if ( eof && noeof && lastfile ) {
		return( 0 );
	    }
	} else {
	    /*
	     * If we can't send data right now, go ahead and get the
	     * status. This is cool, because we get here reliably
	     * if there is a problem.
	     */
	    cbuf[ 0 ] = 0;
	    cbuf[ 1 ] = PAP_SENDSTATUS;
	    cbuf[ 2 ] = cbuf[ 3 ] = 0;
	    atpb.atp_saddr = &nn.nn_sat;
	    atpb.atp_sreqdata = cbuf;
	    atpb.atp_sreqdlen = 4;	/* bytes in SendStatus request */
	    atpb.atp_sreqto = 2;		/* retry timer */
	    atpb.atp_sreqtries = 5;		/* retry count */
	    if ( atp_sreq( satp, &atpb, 1, 0 ) < 0 ) {
		perror( "atp_sreq" );
		exit( 1 );
	    }

	if(debug){ printf( "SENDSTATUS >\n" ), fflush( stdout );}

	    atpb.atp_saddr = &nn.nn_sat;
	    rniov[ 0 ].iov_len = PAP_MAXDATA + 4;
	    atpb.atp_rresiov = rniov;
	    atpb.atp_rresiovcnt = 1;
	    if ( atp_rresp( satp, &atpb ) < 0 ) {
		perror( "atp_rresp" );
		continue;
	    }

#ifndef NONZEROSTATUS
	    /*
	     * The stinking LaserWriter IINTX puts crap in this
	     * field.
	     */
	    if ( ((char *)rniov[ 0 ].iov_base)[ 0 ] != 0 ) {
		fprintf( stderr, "Bad status response!\n" );
		exit( 1 );
	    }
#endif /* NONZEROSTATUS */

	    if ( ((char *)rniov[ 0 ].iov_base)[ 1 ] != PAP_STATUS ||
		    atpb.atp_rresiovcnt != 1 ) {
		fprintf( stderr, "Bad status response!\n" );
		exit( 1 );
	    }

	if(debug){ printf( "< STATUS\n" ), fflush( stdout );}

#ifdef F****D
	    if ( waitforprinter ) {
		char	st_buf[ 1024 ];	/* XXX too big */

		memcpy( st_buf, (char *) rniov[ 0 ].iov_base + 9, 
			((char *)rniov[ 0 ].iov_base)[ 8 ] );
		st_buf[ (int) ((char *)rniov[ 0 ].iov_base)[ 8 ]] = '\0';
		if ( strstr( st_buf, "waiting" ) != NULL ) {
		    waitforprinter = 0;
		}
	    }
#endif /* F****D */

	    updatestatus( (char *) rniov[ 0 ].iov_base + 9,
		    ((char *)rniov[ 0 ].iov_base)[ 8 ] );
	}
    }
}
示例#10
0
int main( int ac, char	**av)
{
    ATP			atp;
    struct atp_block	atpb;
    int			c, err = 0, fd, cuts = 0;
    char		*obj = NULL, *type = "LaserWriter", *zone = "*";
    struct timeval	stv, tv;
    char		rbuf[ ATP_MAXDATA ];
    struct iovec	iov;
    unsigned short	waiting, result;
    int			connattempts = 10;
    int			waitforidle = 0;
    struct at_addr      addr;

    extern char		*optarg;
    extern int		optind;

    memset(&addr, 0, sizeof(addr));
    while (( c = getopt( ac, av, "dWwcep:s:EA:" )) != EOF ) {
	switch ( c ) {
#ifdef F****D
	case 'w' :
	    waitforprinter = 1;
	    break;

	case 'W' :
	    waitforidle = 1;
	    break;
#endif /* F****D */

	/* enable debugging */
	case 'd' :
	    debug++;
	    break;

	case 'c' :
	    cuts++;
	    break;

	case 'e' :	/* send stdout to stderr */
	    dup2( 2, 1 );
	    break;

	case 'p' :
	    printer = optarg;
	    break;

	case 's' :
	    status = optarg;
	    break;

	case 'E' :
	    noeof = 1;
	    break;
	   
	case 'A':
	    if (!atalk_aton(optarg, &addr)) {
	      fprintf(stderr, "Bad address.\n");
	      exit(1);
	    }
	    break;

	default :
	    err++;
	}
    }
    if ( err ) {
	usage( *av );
    }
    if ( printer == NULL && (( printer = paprc()) == NULL )) {
	fprintf( stderr, "No printer specified and ./.paprc not found.\n" );
	exit( 2 );
    }

    /*
     * Open connection.
     */
    if ( nbp_name( printer, &obj, &type, &zone ) < 0 ) {
	fprintf( stderr, "%s: Bad name\n", printer );
	exit( 2 );
    }
    if ( obj == NULL ) {
	fprintf( stderr, "%s: Bad name\n", printer );
	exit( 2 );
    }

    if ( nbp_lookup( obj, type, zone, &nn, 1, &addr ) <= 0 ) {
	if ( errno != 0 ) {
	    perror( "nbp_lookup" );
	    exit( 2 );
	}
	fprintf( stderr, "%s:%s@%s: NBP Lookup failed\n", obj, type, zone );
	exit( 1 );
    }

    if ( isatty( 1 )) {
	printf( "Trying %u.%d:%d ...\n", ntohs( nn.nn_sat.sat_addr.s_net ),
		nn.nn_sat.sat_addr.s_node, nn.nn_sat.sat_port );
    }

    if (( atp = atp_open( ATADDR_ANYPORT, &addr )) == NULL ) {
	perror( "atp_open" );
	exit( 2 );
    }
    if (( satp = atp_open( ATADDR_ANYPORT, &addr )) == NULL ) {
	perror( "atp_open" );
	exit( 2 );
    }

    while ( waitforidle ) {
	char	st_buf[ 1024 ];	/* XXX too big */

	cbuf[ 0 ] = 0;
	cbuf[ 1 ] = PAP_SENDSTATUS;
	cbuf[ 2 ] = cbuf[ 3 ] = 0;
	atpb.atp_saddr = &nn.nn_sat;
	atpb.atp_sreqdata = cbuf;
	atpb.atp_sreqdlen = 4;	/* bytes in SendStatus request */
	atpb.atp_sreqto = 2;		/* retry timer */
	atpb.atp_sreqtries = 5;		/* retry count */
	if ( atp_sreq( satp, &atpb, 1, 0 ) < 0 ) {
	    perror( "atp_sreq" );
	    exit( 1 );
	}

	if(debug){ printf( "SENDSTATUS >\n" ), fflush( stdout );}

	atpb.atp_saddr = &nn.nn_sat;
	rniov[ 0 ].iov_len = PAP_MAXDATA + 4;
	atpb.atp_rresiov = rniov;
	atpb.atp_rresiovcnt = 1;
	if ( atp_rresp( satp, &atpb ) < 0 ) {
	    perror( "atp_rresp" );
	    continue;
	}

#ifndef NONZEROSTATUS
	/*
	 * The stinking LaserWriter IINTX puts crap in this
	 * field.
	 */
	if ( ((char *)rniov[ 0 ].iov_base)[ 0 ] != 0 ) {
	    fprintf( stderr, "Bad status response!\n" );
	    exit( 1 );
	}
#endif /* NONZEROSTATUS */

	if ( ((char *)rniov[ 0 ].iov_base)[ 1 ] != PAP_STATUS ||
		atpb.atp_rresiovcnt != 1 ) {
	    fprintf( stderr, "Bad status response!\n" );
	    exit( 1 );
	}

	if(debug){ printf( "< STATUS\n" ), fflush( stdout );}

	memcpy( st_buf, (char *) rniov[ 0 ].iov_base + 9, 
		((char *)rniov[ 0 ].iov_base)[ 8 ] );
	st_buf[ (int) ((char *)rniov[ 0 ].iov_base)[ 8 ]] = '\0';
	if ( strstr( st_buf, "idle" ) != NULL ) {
	    waitforidle = 0;
	} else {
	    updatestatus( (char *) rniov[ 0 ].iov_base + 9,
		    ((char *)rniov[ 0 ].iov_base)[ 8 ] );
	    sleep( 5 );
	}
    }

    cbuf[ 0 ] = connid = getpid() & 0xff;
    cbuf[ 1 ] = PAP_OPEN;
    cbuf[ 2 ] = cbuf[ 3 ] = 0;
    cbuf[ 4 ] = atp_sockaddr( atp )->sat_port;
    cbuf[ 5 ] = oquantum;	/* flow quantum */
    if ( gettimeofday( &stv, NULL ) < 0 ) {
	perror( "gettimeofday" );
	exit( 2 );
    }
    for (;;) {
	if ( cuts ) {
	    waiting = 0xffff;
	} else {
	    if ( gettimeofday( &tv, NULL ) < 0 ) {
		perror( "gettimeofday" );
		exit( 2 );
	    }
	    waiting = htons( tv.tv_sec - stv.tv_sec );
	}
	memcpy(cbuf +  6, &waiting, sizeof( waiting ));

	atpb.atp_saddr = &nn.nn_sat;
	atpb.atp_sreqdata = cbuf;
	atpb.atp_sreqdlen = 8;		/* bytes in OpenConn request */
	atpb.atp_sreqto = 2;		/* retry timer */
	atpb.atp_sreqtries = 5;		/* retry count */
	if ( atp_sreq( atp, &atpb, 1, ATP_XO ) < 0 ) {
	    perror( "atp_sreq" );
	    exit( 1 );
	}

	if(debug){ printf( "OPEN >\n" ), fflush( stdout );}

	iov.iov_base = rbuf;
	iov.iov_len = sizeof( rbuf );
	atpb.atp_rresiov = &iov;
	atpb.atp_rresiovcnt = 1;
	if ( atp_rresp( atp, &atpb ) < 0 ) {
	    perror( "atp_rresp" );
	    if ( connattempts-- <= 0 ) {
		fprintf( stderr, "Can't connect!\n" );
		exit( 1 );
	    }
	    continue;
	}

	/* sanity */
	if ( iov.iov_len < 8 || (unsigned char)rbuf[ 0 ] != connid ||
		rbuf[ 1 ] != PAP_OPENREPLY ) {
	    fprintf( stderr, "Bad response!\n" );
	    continue;	/* This is weird, since TIDs must match... */
	}

	if(debug){ printf( "< OPENREPLY\n" ), fflush( stdout );}

	if ( isatty( 1 )) {
	    printf( "%.*s\n", (int)iov.iov_len - 9, (char *) iov.iov_base + 9 );
	}
	updatestatus( (char *) iov.iov_base + 9, iov.iov_len - 9 );

	memcpy( &result, rbuf +  6,  sizeof( result ));
	if ( result != 0 ) {
	    sleep( 2 );
	} else {
	    memcpy( &sat, &nn.nn_sat, sizeof( struct sockaddr_at ));
	    sat.sat_port = rbuf[ 4 ];
	    quantum = rbuf[ 5 ];
	    break;
	}
    }

    if ( isatty( 1 )) {
	printf( "Connected to %.*s:%.*s@%.*s.\n",
		nn.nn_objlen, nn.nn_obj,
		nn.nn_typelen, nn.nn_type,
		nn.nn_zonelen, nn.nn_zone );
    }

    if ( optind == ac ) {
	send_file( 0, atp, 1 );
    } else {
	for (; optind < ac; optind++ ) {
	    if ( strcmp( av[ optind ], "-" ) == 0 ) {
		fd = 0;
	    } else if (( fd = open( av[ optind ], O_RDONLY )) < 0 ) {
		perror( av[ optind ] );
		continue;
	    }
	    send_file( fd, atp, ( optind == ac - 1 ) ? 1 : 0 );
	    if ( fd != 0 ) {
		close( fd );
	    }
	}
    }

    /*
     * Close connection.
     */
    cbuf[ 0 ] = connid;
    cbuf[ 1 ] = PAP_CLOSE;
    cbuf[ 2 ] = cbuf[ 3 ] = 0;

    atpb.atp_saddr = &sat;
    atpb.atp_sreqdata = cbuf;
    atpb.atp_sreqdlen = 4;		/* bytes in CloseConn request */
    atpb.atp_sreqto = 2;		/* retry timer */
    atpb.atp_sreqtries = 5;		/* retry count */
    if ( atp_sreq( atp, &atpb, 1, ATP_XO ) < 0 ) {
	perror( "atp_sreq" );
	exit( 1 );
    }

	if(debug){ printf( "CLOSE >\n" ), fflush( stdout );}

    iov.iov_base = rbuf;
    iov.iov_len = sizeof( rbuf );
    atpb.atp_rresiov = &iov;
    atpb.atp_rresiovcnt = 1;
    if ( atp_rresp( atp, &atpb ) < 0 ) {
	perror( "atp_rresp" );
	exit( 1 );
    }

    /* sanity */
    if ( iov.iov_len != 4 || rbuf[ 1 ] != PAP_CLOSEREPLY ) {
	fprintf( stderr, "Bad response!\n" );
	exit( 1 );
    }

#ifndef ZEROCONNID
    /*
     * The AGFA Viper Rip doesn't have the connection id in the close request.
     */
    if ((unsigned char)rbuf[ 0 ] != connid ) {
	fprintf( stderr, "Bad connid in close!\n" );
	exit( 1 );
    }
#endif /* ZEROCONNID */

	if(debug){ printf( "< CLOSEREPLY\n" ), fflush( stdout );}

    if ( isatty( 1 )) {
	printf( "Connection closed.\n" );
    }
    exit( 0 );
}