/**
 * Init state for RTP statistics
 * 
 * @param[in] s
 *      Source state
 * 
 * @param[in] seq
 *      Current sequence number
 * 
 * @return 1 upon finding a valid packet part of a new state; 0 otherwise
 */
int
update_seq(source_t * s, uint16_t seq)
{
    const int MAX_DROPOUT    = 3000;
    const int MAX_MISORDER   =  100;
    
    uint16_t udelta = seq - s->max_seq;

    /*
     * Source is not valid until MIN_SEQUENTIAL packets with
     * sequential sequence numbers have been received.
     */
    
    if (s->probation) {
        /* packet is in sequence */
        if (seq == s->max_seq + 1) {
            s->probation--;
            s->max_seq = seq;
            if (s->probation == 0) {
                init_seq(s, seq);
                s->received++;
                return 1;
            }
        } else {
            s->probation = MIN_SEQUENTIAL - 1;
            s->max_seq = seq;
        }
        return 0;
        
    } else if (udelta < MAX_DROPOUT) {
        /* in order, with permissible gap */
        if (seq < s->max_seq) {
            /*
             * Sequence number wrapped - count another 64K cycle.
             */
            s->cycles += RTP_SEQ_MOD;
        }
        s->max_seq = seq;
    } else if (udelta <= RTP_SEQ_MOD - MAX_MISORDER) {
        /* the sequence number made a very large jump */
        
        if (seq == s->bad_seq) {
            /*
             * Two sequential packets -- assume that the other side
             * restarted without telling us so just re-sync
             * (i.e., pretend this was the first packet).
             */
            init_seq(s, seq);
        } else {
            s->bad_seq = (seq + 1) & (RTP_SEQ_MOD-1);
            return 0;
        }
    } else {
        /* duplicate or reordered packet */
    }
    s->received++;
    return 1;
}
Пример #2
0
//  SLOTS
void JVlibForm::on_System_OpenMidi_button_clicked()
{
    disconnect_port();
//    close_seq();
    System_PlayMidi_button->setChecked(false);
    System_PlayMidi_button->setEnabled(false);
    System_PauseMidi_button->setEnabled(false);
    SysFilePlaying->clear();
    System_MIDI_Transpose->setValue(0);
    System_MIDI_KeySig->clear();
    MIDI_length_display->setText("00:00");
    QString fn = QFileDialog::getOpenFileName(this,"Open MIDI File",MIDI_dir,"Midi files (*.mid, *.MID);;Any (*.*)");
    if (fn.isEmpty())
        return;
    strcpy(playfile, fn.toAscii().data());
    SysFilePlaying->setText(fn);
    init_seq();
    if (!queue) queue = snd_seq_alloc_named_queue(seq, "midi_play");
    check_snd("create queue", queue);
    connect_port();
    all_events.clear();
    if (!parseFile(playfile)) {
        QMessageBox::critical(this, "MIDI Player", QString("Error parsing input file"));
        return;
    }   // parseFile
    System_MIDI_progressBar->setRange(0,all_events.back().tick);
    System_MIDI_progressBar->setTickInterval(song_length_seconds<240? all_events.back().tick/song_length_seconds*10 : all_events.back().tick/song_length_seconds*30);
    System_MIDI_progressBar->setTickPosition(QSlider::TicksAbove);
    MIDI_length_display->setText(QString::number(static_cast<int>(song_length_seconds/60)).rightJustified(2,'0') + ":" + QString::number(static_cast<int>(song_length_seconds)%60).rightJustified(2,'0'));
    System_PlayMidi_button->setEnabled(true);
    System_MIDI_Transpose->setEnabled(true);
}   // end on_System_OpenMidi_button_clicked
int main() 
{ 
    Seq s; 
    init_seq(&s); 
    int data; 
    int key; 
    int r; 

    printf("请输入线性表中的数据:\n"); 
    scanf("%d",&data); 
    while(data != 1) 
    { 
        setbuf(stdin,NULL);         
        s.elem[s.length] = data; 
        s.length++; 
        scanf("%d",&data); 
    } 

    printf("线性表中的数据为:\n"); 
    printf("线性表中有%d个数据!\n",s.length); 
    for(r = 0;r < s.length ;r++) 
        printf("%d  ",s.elem[r]); 
    printf("\n"); 

    printf("请输入要查找的数值:\n"); 
    scanf("%d",&key); 
    r = search(&s,key); 

    if(r > 0) 
        printf("查找到了该数值,在位置%d\n",r); 
    else 
        printf("未找到数值!\n"); 

    return 0; 
}
Пример #4
0
// tag::oscdump[]
int main(int argc, char **argv)
{
    if(argc == 3 && !strcmp(argv[1], "--dump-oscdoc")) {
        std::ofstream file(argv[2], std::ofstream::out);
        rtosc::OscDocFormatter formatter{&ports, "rtosc-tutorial",
            "http://example.com/", "http://example.com/",
                "John", "Smith"};
        file << formatter;
        file.close();
    }
    // end::oscdump[]


    middleware_init();
	const char *client_name = "rtosc-tutorial";
	jack_options_t options = JackNullOption;
	jack_status_t status;

	client = jack_client_open(client_name, options, &status, NULL);
	if(!client)
        return 1;

	jack_set_process_callback(client, process, 0);
	//jack_on_shutdown(client, jack_shutdown, 0);

    Fs = jack_get_sample_rate(client);

	port = jack_port_register (client, "out",
					  JACK_DEFAULT_AUDIO_TYPE,
					  JackPortIsOutput, 0);
    josc = jack_port_register(client, "osc",
                      JACK_DEFAULT_OSC_TYPE,
                      JackPortIsInput, 0);

    //Setup
    init_osc(&osc);
    init_seq(&seq);
    init_lfo(&lfo);
    init_lpf(&filter);

	jack_activate(client);

    while(1)
        middleware_tick();
}
Пример #5
0
int main()
{
    init_seq(); 

    set_union(4, 3); 
    set_union(3, 8); 
    set_union(6, 5); 
    set_union(9, 4); 

    printf("%d\n", connected(8, 9)); 
    printf("%d\n", connected(5, 9)); 

    set_union(5, 0); 
    set_union(7, 2); 
    set_union(6, 1); 
    set_union(7, 3); 

    printf("%d\n", connected(2, 3)); 
    return 0; 
} 
Пример #6
0
/*
  discover the sequencer devices currently available
*/
static int alsa_sequencer_list(ClientData clientData, Tcl_Interp *interp) {
  snd_seq_client_info_t *cinfo;
  snd_seq_port_info_t *pinfo;
  Tcl_Obj *result = Tcl_NewListObj(0, NULL);
  if (init_seq(clientData, interp) != TCL_OK) {
    return TCL_ERROR;
  }
  snd_seq_client_info_alloca(&cinfo);
  snd_seq_port_info_alloca(&pinfo);

  snd_seq_client_info_set_client(cinfo, -1);
  while (snd_seq_query_next_client(seq, cinfo) >= 0) {
    int client = snd_seq_client_info_get_client(cinfo);
    snd_seq_port_info_set_client(pinfo, client);
    snd_seq_port_info_set_port(pinfo, -1);
    while (snd_seq_query_next_port(seq, pinfo) >= 0) {
      /* we need both READ and SUBS_READ */
      int capability = snd_seq_port_info_get_capability(pinfo);
      char *readable = ((capability &
			 (SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ)) ==
			(SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ)) ? "r" : "";
      char *writable = ((capability &
			 (SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE)) ==
			(SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE)) ? "w" : "";
      Tcl_Obj *element = Tcl_ObjPrintf("%3d:%-3d  %-32.32s %s %s%s",
				       snd_seq_port_info_get_client(pinfo),
				       snd_seq_port_info_get_port(pinfo),
				       snd_seq_client_info_get_name(cinfo),
				       snd_seq_port_info_get_name(pinfo),
				       readable, writable);
      Tcl_ListObjAppendElement(interp, result, element);
    }
  }
  Tcl_SetObjResult(interp, result);
  return TCL_OK;
}
Пример #7
0
static void ndpi_rtp_search(struct ndpi_detection_module_struct *ndpi_struct,
			    struct ndpi_flow_struct *flow,
			    const u_int8_t * payload, const u_int16_t payload_len)
{
  struct ndpi_packet_struct *packet = &flow->packet;
	
  u_int8_t stage;
  u_int16_t seqnum = ntohs(get_u_int16_t(payload, 2));

  NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG, "search rtp.\n");

  if (payload_len == 4 && get_u_int32_t(packet->payload, 0) == 0 && flow->packet_counter < 8) {
    NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG, "need next packet, maybe ClearSea out calls.\n");
    return;
  }

  if (payload_len == 5 && memcmp(payload, "hello", 5) == 0) {
    NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG,
	     "need next packet, initial hello packet of SIP out calls.\n");
    return;
  }

  if (payload_len == 1 && payload[0] == 0) {
    NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG,
	     "need next packet, payload_packet_len == 1 && payload[0] == 0.\n");
    return;
  }

  if (payload_len == 3 && memcmp(payload, "png", 3) == 0) {
    /* weird packet found in Ninja GlobalIP trace */
    NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG, "skipping packet with len = 3 and png payload.\n");
    return;
  }

  if (payload_len < 12) {
    NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG, "minimal packet size for rtp packets: 12.\n");
    goto exclude_rtp;
  }

  if (payload_len == 12 && get_u_int32_t(payload, 0) == 0 && get_u_int32_t(payload, 4) == 0 && get_u_int32_t(payload, 8) == 0) {
    NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG, "skipping packet with len = 12 and only 0-bytes.\n");
    return;
  }

  if ((payload[0] & 0xc0) == 0xc0 || (payload[0] & 0xc0) == 0x40 || (payload[0] & 0xc0) == 0x00) {
    NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG, "version = 3 || 1 || 0, maybe first rtp packet.\n");
    return;
  }

  if ((payload[0] & 0xc0) != 0x80) {
    NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct,
	     NDPI_LOG_DEBUG, "rtp version must be 2, first two bits of a packets must be 10.\n");
    goto exclude_rtp;
  }

  /* rtp_payload_type are the last seven bits of the second byte */
  if (flow->rtp_payload_type[packet->packet_direction] != (payload[1] & 0x7F)) {
    NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG, "payload_type has changed, reset stages.\n");
    packet->packet_direction == 0 ? (flow->rtp_stage1 = 0) : (flow->rtp_stage2 = 0);
  }
  /* first bit of first byte is not part of payload_type */
  flow->rtp_payload_type[packet->packet_direction] = payload[1] & 0x7F;

  stage = (packet->packet_direction == 0 ? flow->rtp_stage1 : flow->rtp_stage2);

  if (stage > 0) {
    NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct,
	     NDPI_LOG_DEBUG, "stage = %u.\n", packet->packet_direction == 0 ? flow->rtp_stage1 : flow->rtp_stage2);
    if (flow->rtp_ssid[packet->packet_direction] != get_u_int32_t(payload, 8)) {
      NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG, "ssid has changed, goto exclude rtp.\n");
      goto exclude_rtp;
    }

    if (seqnum == flow->rtp_seqnum[packet->packet_direction]) {
      NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG, "maybe \"retransmission\", need next packet.\n");
      return;
    } else if ((u_int16_t) (seqnum - flow->rtp_seqnum[packet->packet_direction]) < RTP_MAX_OUT_OF_ORDER) {
      NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG,
	       "new packet has larger sequence number (within valid range)\n");
      update_seq(ndpi_struct, flow, packet->packet_direction, seqnum);
    } else if ((u_int16_t) (flow->rtp_seqnum[packet->packet_direction] - seqnum) < RTP_MAX_OUT_OF_ORDER) {
      NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG,
	       "new packet has smaller sequence number (within valid range)\n");
      init_seq(ndpi_struct, flow, packet->packet_direction, seqnum, 1);
    } else {
      NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG,
	       "sequence number diff is too big, goto exclude rtp.\n");
      goto exclude_rtp;
    }
  } else {
    NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct,
	     NDPI_LOG_DEBUG, "rtp_ssid[%u] = %u.\n", packet->packet_direction,
	     flow->rtp_ssid[packet->packet_direction]);
    flow->rtp_ssid[packet->packet_direction] = get_u_int32_t(payload, 8);
    if (flow->packet_counter < 3) {
      NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG, "packet_counter < 3, need next packet.\n");
    }
    init_seq(ndpi_struct, flow, packet->packet_direction, seqnum, 1);
  }
  if (seqnum <= 3) {
    NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct,
	     NDPI_LOG_DEBUG, "sequence_number = %u, too small, need next packet, return.\n", seqnum);
    return;
  }

  if (stage == 3) {
    NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG, "add connection I.\n");
    ndpi_int_rtp_add_connection(ndpi_struct, flow);
  } else {
    packet->packet_direction == 0 ? flow->rtp_stage1++ : flow->rtp_stage2++;
    NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG, "stage[%u]++; need next packet.\n",
	     packet->packet_direction);
  }
  return;

 exclude_rtp:
#ifdef NDPI_PROTOCOL_STUN
  if (packet->detected_protocol_stack[0] == NDPI_PROTOCOL_STUN
      || packet->real_protocol_read_only == NDPI_PROTOCOL_STUN) {
    NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG, "STUN: is detected, need next packet.\n");
    return;
  }
#endif							/*  NDPI_PROTOCOL_STUN */
  NDPI_LOG(NDPI_PROTOCOL_RTP, ndpi_struct, NDPI_LOG_DEBUG, "exclude rtp.\n");
  NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_RTP);
}
Пример #8
0
Файл: main.cpp Проект: Ryzh/voc
bool on_key_press(char c)
{
	bool bNeedUpdate = false;
	QVector<word_t*>* vec;
	vec = bTickMode ? &tick_vec : &word_vec;
	int& index = bTickMode ? iTickIndex : iCurrIndex;

	switch(c)
	{
		case 't'://tick
			(*vec)[seq_vec[index]]->tick();
			bNeedUpdate = true;
			break; 
		case '+'://inc marks
			(*vec)[seq_vec[index]]->inc_marks();
			bNeedUpdate = true;
			break;
		case '-'://dec marks
			(*vec)[seq_vec[index]]->dec_marks();
			bNeedUpdate = true;
			break;
		case 'R'://ramdon
			if (bRandomMode)
				init_seq();
			else
				random_seq();
			bNeedUpdate = true;
			break;
		case 'q'://quit
			eScrType = scr_confquit;
			bNeedUpdate = true;
			break;
		case '>':
			eScrType = scr_confexport;
			bNeedUpdate = true;
			break;
		case '~'://remove ticked items
			eScrType = scr_confdelete;
			bNeedUpdate = true;
			break;
		case 'j'://next
			if (eScrType == scr_list)
			{
				if (++index >= vec->count())
				{
					--index;
				}
				bNeedUpdate = true;
				break;
			}
			else if (eScrType == scr_def)
			{
				iDefFirstLine++;
				bNeedUpdate = true;
				break;
			}
			break;
		case 'k'://previous
			if (eScrType == scr_list)
			{
				if (--index < 0)
				{
					++index;
				}
				bNeedUpdate = true;
			}
			else if (eScrType == scr_def)
			{
				iDefFirstLine --;
				bNeedUpdate = true;
				break;
			}
			break;
		case ' '://space
			if (eScrType == scr_list)
			{
				if (++index >= vec->count())
				{
					--index;
				}
				bNeedUpdate = true;
			}
			bNeedUpdate = true;
			break;
		case '\n'://enter
			if (eScrType == scr_list)
			{
				eScrType = scr_def;
				bNeedUpdate = true;
				break;
			}
			else if (eScrType == scr_def)
			{
				eScrType = scr_list;
				bNeedUpdate = true;
				break;
			}
			else if (eScrType == scr_confquit)
			{
				bQuit = true;
				break;
			}
			break;
		case 27:
			if (eScrType == scr_list)
			{
				eScrType = scr_confquit;
				bNeedUpdate = true;
				break;
			}
			else if (eScrType == scr_def)
			{
				eScrType = scr_list;
				bNeedUpdate = true;
				break;
			}
			else if (eScrType == scr_confquit)
			{
				eScrType = bDefMode ? scr_def : scr_list;
				bNeedUpdate = true;
				break;
			}
		case '*'://show tick screen
			iCurrIndex = iTickIndex = 0;
			if (bTickMode = !bTickMode)
				gen_tick_vec();
			init_seq();
			bNeedUpdate = true;
			break;
		case 'y':
			if (eScrType == scr_confquit)
			{
				bQuit = true;
			}
			else if (eScrType == scr_confexport)
			{
				QString file = file_name;
				int pos = file.lastIndexOf("/");
				if (pos > 0)
					file.replace(pos, -1, "extracted.xml");
				else
					file = "extracted.xml";
				//regenerate the tick vec and merge it to the file
				gen_tick_vec();
				merge_vec_to_file(tick_vec, file);	
				eScrType = scr_list;
				bNeedUpdate = true;
			}
			else if (eScrType == scr_confdelete)
			{
				//regenerate the tick vec and merge it to the file
				gen_tick_vec();
				remove_ticked();	
				eScrType = scr_list;
				bNeedUpdate = true;
			}	
			break;
		case 'n':
			if (eScrType == scr_confquit || eScrType == scr_confexport)
			{
				eScrType = bDefMode ? scr_def : scr_list;
				bNeedUpdate = true;
				break;
			}
			break;
		case 'C':
			for (int i = 0; i < word_vec.count(); i++)
			{
				word_vec[i]->ticked = false;
			}
			bNeedUpdate = true;
			break;
		case 'H':
			bCnMode = !bCnMode;
			bNeedUpdate = true;
			break;
		default:
			break; 
	}
	return bNeedUpdate;
}
Пример #9
0
Файл: main.cpp Проект: Ryzh/voc
int main(int argc, char* argv[])
{
	char c;
	bNeedUpdate = true;
	bTickMode = false;
	bDefMode = false;
	bRandomMode = false;
	bCnMode = false;
	bQuit = false;
	eScrType = scr_list;
	iCurrIndex = 0;
	
	setlocale(LC_ALL, "");

	if (argc < 2)
	{
		std::cout << "Please input the file name!" << std::endl;
		return 0;
	}
	else if (argc > 2)
	{
		//command line mode
		int mpos;//pos for -m
		int opos;//pos for -o
		QStringList ParamList;
		QString Object;
		QString Tmp;
		bool mfound = false, ofound = false;
		for (int i = 1; i < argc; ++i)
		{
			Tmp = argv[i];
			if (Tmp == "-m")
			{
				mfound = true;
			}
			else if (Tmp == "-o")
			{
				if (++i < argc)
				{
					ofound = true;
					Object = argv[i];
				}
			}	
			else
			{
				ParamList.push_back(QString(argv[i]));
			}
		}
		if (!mfound)
		{
			std::cout << "Too many file names. Use -m if you mean to merge these files." << std::endl;
			return 0;
		}
		else if (!ofound)
		{
			std::cout << "Use -o to specify a target file." << std::endl;
			return 0;
		}
		
		for (int i = 0; i < ParamList.size(); ++i)
		{
			load_file_to_vec(word_vec, ParamList[i]);
			merge_vec_to_file(word_vec, Object);
			clear_vecs();
		}
		return 1;
	}

	//save the file name;
	file_name = argv[1];
	//Handle the resize event
	signal(SIGWINCH, do_resize);
	//init the new word vector
	load_file_to_vec(word_vec, argv[1]);
	init_seq();

	setlocale(LC_ALL, "");
	//init the screen
	initscr();
	noecho();
	curs_set(0);
	start_color();
	//init highlight color;
	use_default_colors();
	init_pair(1, COLOR_GREEN, -1);

	//init bottom line color;
	init_pair(2, COLOR_BLUE, -1);

	timeout(500); //set getch() to non-block mode, timeout is 0.5 sec.

	while(!bQuit)
	{
		if (bNeedUpdate)
		{
			update_screen();
			bNeedUpdate = false;
		}    

		c = getch();
		if (c != ERR)
		{
			bNeedUpdate = on_key_press(c);
		} 
	}

	save_vec_to_file(word_vec, argv[1]);

	//release resourse
	clear_vecs();

	endwin();
	curs_set(1);
	return 0;
}
Пример #10
0
int
main(int argc, char *argv[])
{
  int count;
  seq_t seq1, seq2;
  hash_env_t he;
  collec_t res, rev_res;
#if defined(DEBUG) && (DEBUG > 1)
  mcheck(NULL);
  mtrace();
#endif
  argv0 = argv[0];
  if (setlocale(LC_ALL, "POSIX") == NULL)
    fprintf(stderr, "%s: Warning: could not set locale to POSIX\n", argv[0]);
  signal(SIGSEGV, bug_handler);
#ifndef __MINGW32__  
  signal(SIGBUS, bug_handler);
#endif  
  /* Default options.  */
  options.C = DEFAULT_C;
  options.cutoff = DIST_CUTOFF;
  options.gapPct = DEFAULT_GAPPCT;
  options.intron_window = 6;
  options.K = DEFAULT_K;
  options.splice_type_list = "GTAG,GCAG,GTAC,ATAC";
  options.nbSplice = 4;
  options.scoreSplice_window = 10;
  options.mismatchScore = MISMATCH;
  options.reverse = 2;
  options.matchScore = MATCH;
  options.W = DEFAULT_W;
  options.X = DEFAULT_X;
  options.filterPct = DEFAULT_FILTER;
  options.minScore_cutoff = MATCH_CUTOFF;
  while (1) {
    int c = getopt(argc, argv, "A:C:c:E:f:g:I:K:L:M:o:q:R:r:W:X:");
    if (c == -1)
      break;
    switch (c) {
    case 'A':
      options.ali_flag = atoi(optarg);
      if (options.ali_flag < 0 || options.ali_flag > 4)
	fatal("A must be one of 0, 1, 2, 3, or 4.\n");
      break;
    case 'C': {
      int val = atoi(optarg);
      if (val < 0)
	fatal("Value for option C must be non-negative.\n");
      options.C = val;
      break;
    }
    case 'c': {
      int val = atoi(optarg);
      if (val < 0)
	fatal("Value for option c must be non-negative.\n");
      options.minScore_cutoff = val;
      break;
    }
    case 'E':
      options.cutoff = atoi(optarg);
      if (options.cutoff < 3 || options.cutoff > 10)
	fatal("Cutoff (E) must be within [3,10].\n");
      break;
    case 'f':
      options.filterPct = atoi(optarg);
      if (options.filterPct > 100)
	fatal("Filter in percent (f) must be within [0,100].\n");
      break;
    case 'g':
      options.gapPct = atoi(optarg);
      break;
    case 'I':
      options.intron_window = atoi(optarg);
      break;
    case 'K': {
      int val = atoi(optarg);
      if (val < 0)
	fatal("Value for option K must be non-negative.\n");
      options.K = val;
      break;
    }
    case 'L': {
      size_t i;
      size_t len = strlen(optarg);
      options.splice_type_list = optarg;
      options.nbSplice = 1;
      if (len % 5 != 4)
	fatal("Splice types list has illegal length (%zu)\n", len);
      for (i = 0; i < len; i++)
	if (i % 5 == 4) {
	  if (options.splice_type_list[i] != ',')
	    fatal("Comma expected instead of %c at position %zu"
		  "in splice types list.\n",
		  options.splice_type_list[i], i);
	  options.nbSplice += 1;
	} else {
	  if (options.splice_type_list[i] != 'A'
	      && options.splice_type_list[i] != 'C'
	      && options.splice_type_list[i] != 'G'
	      && options.splice_type_list[i] != 'T')
	    fatal("Expected 'A', 'C', 'G' or 'T' instead of '%c' at"
		  "position %zu in splice types list.\n",
		  options.splice_type_list[i], i);
	}
      break;
    }
    case 'M': {
      int val = atoi(optarg);
      if (val < 0)
	fatal("Value for option M must be non-negative.\n");
      options.scoreSplice_window = val;
      break;
    }
    case 'o':
      options.dnaOffset = atoi(optarg);
      break;
    case 'q':
      options.mismatchScore = atoi(optarg);
      break;
    case 'R':
      options.reverse = atoi(optarg);
      if (options.reverse < 0 || options.reverse > 2)
	fatal("R must be one of 0, 1, or 2.\n");
      break;
    case 'r':
      options.matchScore = atoi(optarg);
      break;
    case 'W':
      options.W = atoi(optarg);
      if (options.W < 1 || options.W > 15)
	fatal("W must be within [1,15].\n");
      break;
    case 'X':
      options.X = atoi(optarg);
      if (options.X < 1)
	fatal("X must be positive.\n");
      break;
    case '?':
      break;
    default:
      fprintf(stderr, "?? getopt returned character code 0%o ??\n", c);
    }
  }
  if (optind + 2 != argc) {
    fprintf(stderr, Usage, argv[0], options.ali_flag, options.C,
	    options.minScore_cutoff, options.cutoff,
	    options.filterPct, options.gapPct, options.intron_window,
	    options.K, options.splice_type_list, options.scoreSplice_window,
	    options.dnaOffset, options.mismatchScore, options.reverse,
	    options.matchScore, options.W, options.X);
    return 1;
  }

  /* read seq1 */
  init_seq(argv[optind], &seq1);
  if (get_next_seq(&seq1, options.dnaOffset, 1) != 0)
    fatal("Cannot read sequence from %s.\n", argv[optind]);
  strncpy(dna_seq_head, seq1.header, 256);

  /* read seq2 */
  init_seq(argv[optind + 1], &seq2);
  if (get_next_seq(&seq2, 0, 0) != 0)
    fatal("Cannot read sequence from %s.\n", argv[optind + 1]);

  init_encoding();
  init_hash_env(&he, options.W, seq1.seq, seq1.len);
  init_col(&res, 1);
  init_col(&rev_res, 1);
  bld_table(&he);
  init_splice_junctions();

  count = 0;
  while (!count || get_next_seq(&seq2, 0, 0) == 0) {
    unsigned int curRes;
    strncpy(rna_seq_head, seq2.header, 256);
    ++count;

    switch (options.reverse) {
    case  0:
      SIM4(&he, &seq2, &res);
      break;
    case  2:
      SIM4(&he, &seq2, &res);
    case  1:
      seq_revcomp_inplace(&seq2);
      SIM4(&he, &seq2, &rev_res);
      break;
    default:
      fatal ("Unrecognized request for EST orientation.\n");
    }
    /* Keep only the best matches, according to filterPct.  */
    if (options.filterPct > 0) {
      unsigned int max_nmatches = 0;
      for (curRes = 0; curRes < rev_res.nb; curRes++) {
	result_p_t r = rev_res.e.result[curRes];
	if (r->st.nmatches > max_nmatches)
	  max_nmatches = r->st.nmatches;
      }
      for (curRes = 0; curRes < res.nb; curRes++) {
	result_p_t r = res.e.result[curRes];
	if (r->st.nmatches > max_nmatches)
	  max_nmatches = r->st.nmatches;
      }
      max_nmatches = (max_nmatches * options.filterPct) / 100;
      for (curRes = 0; curRes < rev_res.nb; curRes++) {
	result_p_t r = rev_res.e.result[curRes];
	if (r->st.nmatches < max_nmatches)
	  r->st.nmatches = 0;
      }
      for (curRes = 0; curRes < res.nb; curRes++) {
	result_p_t r = res.e.result[curRes];
	if (r->st.nmatches < max_nmatches)
	  r->st.nmatches = 0;
      }
    }
    /* Now, print results.  */
    for (curRes = 0; curRes < rev_res.nb; curRes++)
      print_res(rev_res.e.result[curRes], 1, &seq1, &seq2);
    rev_res.nb = 0;
    if (options.reverse && options.ali_flag)
      /* reverse-complement back seq2 for alignment */
      seq_revcomp_inplace(&seq2);
    for (curRes = 0; curRes < res.nb; curRes++)
      print_res(res.e.result[curRes], 0, &seq1, &seq2);
    res.nb = 0;
  }
#ifdef DEBUG
  fprintf(stderr, "DEBUG mode: freeing all memory...\n");
  fflush(stdout);
  fflush(stderr);
  free_hash_env(&he);
  free_seq(&seq1);
  free_seq(&seq2);
  free(options.splice);
  free(res.e.elt);
  free(rev_res.e.elt);
#endif
  return 0;
}
int main(int argc, char *argv[])
{
	static const char short_options[] = "hVlp:";
	static const struct option long_options[] = {
		{"help", 0, NULL, 'h'},
		{"version", 0, NULL, 'V'},
		{"list", 0, NULL, 'l'},
		{"port", 1, NULL, 'p'},
		{ }
	};

	int do_list = 0;
	struct pollfd *pfds;
	int npfds;
	int c, err;

	init_seq();

	while ((c = getopt_long(argc, argv, short_options,
				long_options, NULL)) != -1) {
		switch (c) {
		case 'h':
			help(argv[0]);
			return 0;
		case 'V':
			version();
			return 0;
		case 'l':
			do_list = 1;
			break;
		case 'p':
			parse_ports(optarg);
			break;
		default:
			help(argv[0]);
			return 1;
		}
	}
	if (optind < argc) {
		help(argv[0]);
		return 1;
	}

	if (do_list) {
		list_ports();
		return 0;
	}

	create_port();
	connect_ports();

	err = snd_seq_nonblock(seq, 1);
	check_snd("set nonblock mode", err);
	
	if (port_count > 0)
		printf("Waiting for data.");
	else
		printf("Waiting for data at port %d:0.",
		       snd_seq_client_id(seq));
	printf(" Press Ctrl+C to end.\n");
	printf("Source  Event                  Ch  Data\n");
	
	signal(SIGINT, sighandler);
	signal(SIGTERM, sighandler);

	npfds = snd_seq_poll_descriptors_count(seq, POLLIN);
	pfds = alloca(sizeof(*pfds) * npfds);
	for (;;) {
		snd_seq_poll_descriptors(seq, pfds, npfds, POLLIN);
		if (poll(pfds, npfds, -1) < 0)
			break;
		do {
			snd_seq_event_t *event;
			err = snd_seq_event_input(seq, &event);
			if (err < 0)
				break;
			if (event)
				dump_event(event);
		} while (err > 0);
		fflush(stdout);
		if (stop)
			break;
	}

	snd_seq_close(seq);
	return 0;
}