Exemple #1
0
void myhtml_tag_index_check_size(myhtml_tag_t* tags, myhtml_tag_index_t* index_tags, myhtml_tag_id_t tag_id)
{
    if(tag_id >= index_tags->tags_size) {
        size_t new_size = tag_id + 128;
        
        myhtml_tag_index_entry_t *index_entries = (myhtml_tag_index_entry_t*)myrealloc(index_tags->tags,
                                                                             sizeof(myhtml_tag_index_entry_t) *
                                                                             new_size);
        
        if(index_entries) {
            index_tags->tags = index_entries;
            
            memset(&index_tags->tags[index_tags->tags_size], 0, sizeof(myhtml_tag_index_entry_t)
                   * (new_size - index_tags->tags_size));
            
            index_tags->tags_size = new_size;
        }
        else {
            // TODO: error
        }
    }
}
Exemple #2
0
//---------------------------------------------------------------------------
//ディープコピーメソッド
void wString::copy(wString* src,const wString& dst)
{
    if( dst.len ){
        if( src->total <= dst.len ){
            char* tmp = myrealloc(src->String,dst.len+1);
            //printf("assert\n");
            assert(tmp != 0 );
            src->String = tmp;
            src->total = dst.len+1;
        }
        //ここで両者とも!= NULL
            //printf("assert\n");
        assert( src->String != 0);
        assert( dst.String != 0);
        strcpy( src->String, dst.String);
    //dstはNULL
    }else{
        if( src->len ){
            *src->String = 0;
        }
    }
    src->len = dst.len;
}
Exemple #3
0
//---------------------------------------------------------------------------
void wString::operator+=(const wString& str)
{
    unsigned int num = len+str.len;
    //
    if( str.len ){
        //領域が十分ある
        if( total >= num+1 ){
            strcpy( String+len, str.String );
            len   = num;
        //コピー元が存在し領域不十分
        }else{
            total = num+16;
            //realloc処理
            char* tmp = myrealloc(String,total);
            //printf("assert\n");
            assert( tmp != NULL );
            strcpy( tmp+len, str.String );
            len    = num;
            String = tmp;
        }
    }
    return;
}
Exemple #4
0
int graph_page_add_file (const char *fname)
{
    char **fnames;
    int ng = gpage.ngraphs + 1;

    if (ng > GRAPHS_MAX) {
	gpage_errmsg(_("The graph page is full"), 1);
	return 1;
    }

    fnames = myrealloc(gpage.fnames, ng * sizeof *fnames);
    if (fnames == NULL) {
	return E_ALLOC;
    }

    fnames[ng - 1] = g_strdup(fname);
    gpage.fnames = fnames;
    gpage.ngraphs = ng;

    mark_session_changed();

    return 0;
}
Exemple #5
0
/* Removes specifed number of characters from buffer that is readed into matlab.*/
void removefrombuff(int len)
{
    int newlen;
    if(len>0)
    {
      newlen=ipi[ipi_index].buffdatalen-len;         /* Calc new buff length;*/
      newlen=newlen>0?newlen:0;               /* Not allow negative length.*/
      memmove(&(ipi[ipi_index].buff[0]),
	      &(ipi[ipi_index].buff[len]),newlen);   /* Remove old data in buff;*/
      ipi[ipi_index].buffdatalen=newlen;             /* Set new length.*/
    }
    if(ipi[ipi_index].bufflen>MINBUFFSIZE & ipi[ipi_index].buffdatalen<=MINBUFFSIZE)
    {
      ipi[ipi_index].buff=myrealloc(ipi[ipi_index].buff,MINBUFFSIZE);
      ipi[ipi_index].bufflen=MINBUFFSIZE;
      if(ipi[ipi_index].buff==NULL)
      {
	ipi[ipi_index].bufflen=0;
	mexErrMsgTxt("Not enouth of memory for reallocation"
		       " of smaller read buffert.");
      }
    } 
}
Exemple #6
0
FS_DENT *
fs_dent_realloc(FS_DENT * fs_dent, ULONG namelen)
{
    if (fs_dent->name_max == namelen)
	return fs_dent;

    fs_dent->name = (char *) myrealloc(fs_dent->name, namelen + 1);
    if (fs_dent->name == NULL) {
	if (fs_dent->fsi)
	    fs_inode_free(fs_dent->fsi);

	if (fs_dent->shrt_name)
	    free(fs_dent->shrt_name);

	free(fs_dent);
	return NULL;
    }

    fs_dent->ent_type = FS_DENT_UNDEF;
    fs_dent->name_max = namelen;

    return fs_dent;
}
Exemple #7
0
void *__ckd_realloc__(void *ptr, size_t new_size,
                      char *caller_file, int caller_line)
{
    void *mem;

#if defined(MEM_DEBUG)
    del_ptr(ptr);
#endif

    mem = debug ? myrealloc(ptr, new_size) : realloc(ptr, new_size);
    if (mem == NULL) {
        E_FATAL("realloc(%d) failed from %s(%d)\n", new_size,
                caller_file, caller_line);
    }

#if defined(MEM_DEBUG)
    add_ptr(mem, caller_file, caller_line);
    printf ("ckd_realloc from %s(%d): %08x %d = %08x\n",
            caller_file, caller_line, ptr, new_size, mem);
    fflush (stdout);
#endif

    return mem;
}
Exemple #8
0
void parse_h0string(char ***box, long *size, long *tempsize,
                  char *thisString)
{
    long i;
    long counter = 0;
    long strsize = (long) strlen(thisString);
    counter = sprintf ((*box)[*tempsize],"[");
    for (i = 0; i < strsize; i++)
      {
        counter += sprintf ((*box)[*tempsize] + counter, "%c",thisString[i]);
        if (counter >= BOXSIZE)
          {
            (*tempsize)++;
            if(*tempsize>= *size-1)
              {
                *box = (char **) myrealloc(*box,sizeof(char*) * (*tempsize+1));
                (*box)[*tempsize] = (char *) mycalloc(LRATIO_STRINGS,sizeof(char));
                *size = *tempsize+1;
              }
            counter = sprintf((*box)[*tempsize]," ");
          }
      }
    sprintf ((*box)[*tempsize] + counter, "]");
}
Exemple #9
0
void statistics_menu(void)
{
	NEWWIN *mywin = create_popup(23, 65);
	int offset = 0, cur_line = 0;

	for(;;)
	{
		int c;
		int vmsize = get_vmsize(getpid());
		time_t now = time(NULL);
		struct tm *tmnow = localtime(&now);
		proginfo **plist = NULL;
		char     *issub = NULL;
		int      *winnr = NULL;
		int loop, nwin = 0;

		/* create list of (sub-)windows */
		for(loop=0; loop<nfd; loop++)
		{
			proginfo *cur = &pi[loop];

			while(cur)
			{
				plist = (proginfo **)myrealloc(plist, (nwin + 1) * sizeof(proginfo *));
				issub = (char *)     myrealloc(issub, (nwin + 1) * sizeof(char)      );
				winnr = (int *)      myrealloc(winnr, (nwin + 1) * sizeof(int)       );

				plist[nwin] = cur;
				issub[nwin] = (cur != &pi[loop]);
				winnr[nwin] = loop;
				nwin++;

				cur = cur -> next;
			}
		}

		werase(mywin -> win);
		win_header(mywin, "Statistics");

		for(loop=0; loop<18; loop++)
		{
			int cur_index = loop + offset;
			int is_sub_indent;

			if (cur_index >= nwin) break;

			is_sub_indent = issub[cur_index];

			if (loop == cur_line) ui_inverse_on(mywin);
			if (is_sub_indent)
				mvwprintw(mywin -> win, 2 + loop, 7, "%s", shorten_filename(plist[cur_index] -> filename, 54));
			else
				mvwprintw(mywin -> win, 2 + loop, 2, "[%02d] %s", winnr[cur_index], shorten_filename(plist[cur_index] -> filename, 56));
			if (loop == cur_line) ui_inverse_off(mywin);
		}

		mvwprintw(mywin -> win, 20, 2, "Run-time: %.2f hours  %02d:%02d", (get_ts() - mt_started) / 3600.0, tmnow -> tm_hour, tmnow -> tm_min);
		if (vmsize != -1)
		{
			char *vmsize_str = amount_to_str(vmsize);
			mvwprintw(mywin -> win, 20, 35, "Memory usage: %s", vmsize_str);
			myfree(vmsize_str);
		}
		escape_print(mywin, 21, 2, "Press ^r^ to reset counters, ^q^ to exit");

		draw_border(mywin);
		mydoupdate();

		c = toupper(wait_for_keypress(HELP_STATISTICS, popup_refresh_interval, mywin, 1));

		if (c == 'R')
		{
			for(loop=0; loop<nfd; loop++)
			{
				proginfo *cur = &pi[loop];

				while(cur)
				{
					reset_counters(&cur -> statistics);

					cur = cur -> next;
				}
			}
		}
		else if (c == KEY_UP)
		{
			if (cur_line)
				cur_line--;
			else if (offset)
				offset--;
			else
				wrong_key();
		}
		else if (c == KEY_DOWN)
		{
			if ((cur_line + offset) < (nwin - 1))
			{
				if (cur_line < (18 - 1))
					cur_line++;
				else
					offset++;
			}
			else
				wrong_key();

		}
		else if (c == 13 || c == ' ')
		{
			statistics_popup(winnr[cur_line + offset], plist[cur_line + offset]);
		}
		else if (c == 'Q' || c == abort_key)
		{
			myfree(plist);
			myfree(issub);
			myfree(winnr);
			break;
		}
		else if (c != -1)
		{
			wrong_key();
		}

		myfree(plist);
		myfree(issub);
		myfree(winnr);
	}

	delete_popup(mywin);
}
Exemple #10
0
void add_cs_re(int linenr, char *incmd, char *par)
{
    if (use_colors)
    {
        char *re = NULL, *val = NULL;
        char *cmd = &incmd[5];
        char *colon;

        if (strncmp(cmd, "_val", 4) == 0)
        {
            val = find_next_par(par);
            if (!val)
                config_error_exit(linenr, "cs_re_val...-entry malformed: value missing.\n");

            re = find_next_par(val);
        }
        else
            re = find_next_par(par);

        if (re == NULL)
            config_error_exit(linenr, "cs_re-entry malformed: color or regular expression missing.\n");

        if (cur_colorscheme_nr == -1)
            config_error_exit(linenr, "For cs_re one needs to define a color scheme name first.\n");

        /* find colorscheme */
        if (cschemes[cur_colorscheme_nr].color_script.script)
            config_error_exit(linenr, "One cannot let a color script have the same name has a color scheme.");

        /* grow/create list */
        cschemes[cur_colorscheme_nr].pentries = (color_scheme_entry *)myrealloc(cschemes[cur_colorscheme_nr].pentries, (cschemes[cur_colorscheme_nr].n + 1) * sizeof(color_scheme_entry));

        /* add to list */
        if (cmd[0] == 0x00)
            cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].flags = 0;
        else if (strcmp(cmd, "_s") == 0)
            cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].flags = CSREFLAG_SUB;
        else if (strcmp(cmd, "_val_less") == 0)
            cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].flags = CSREFLAG_CMP_VAL_LESS;
        else if (strcmp(cmd, "_val_bigger") == 0)
            cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].flags = CSREFLAG_CMP_VAL_BIGGER;
        else if (strcmp(cmd, "_val_equal") == 0)
            cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].flags = CSREFLAG_CMP_VAL_EQUAL;

        /* sanity check */
        if (cmd[0] != 0x00 && strchr(re, '(') == NULL)
            config_error_exit(linenr, "%s is missing substring selections! ('(' and ')')\n", cmd);

        if (val) cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].cmp_value = atof(val);
        cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].merge_color = incmd[0] == 'm' ? MY_TRUE : MY_FALSE;

        cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].cdef.ac_index = 0;
        colon = strchr(par, '|');
        if (colon)
        {
            *colon = 0x00;
            cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].cdef.use_alternating_colors = MY_TRUE;
            cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].cdef.attrs2 = parse_attributes(colon + 1);
        }
        else
        {
            cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].cdef.use_alternating_colors = MY_FALSE;
        }
        cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].cdef.attrs1 = parse_attributes(par);

        /* compile regular expression */
        compile_re(&cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].regex, re);

        cschemes[cur_colorscheme_nr].n++;
    }
}
Exemple #11
0
// Prints the contents of a directory and its subdirectories.
void printdircontents()
{   
    DIR * dp;

    // Failing to open the directory does not end the program and it continues
    // to look in previous directories.
    if ((dp = opendir(fullpath)) == NULL) {
        perror(fullpath);
        fail=1;
        return;
    }
    
    // An int to store wether or not an item is a directory and if it should be
    // printed or not.
    // The fullpath is reallocated 255 bytes more than its current length.
    struct dirent * currentdir;
    int adir=0;
    int shouldprint;
    myrealloc(strlen(fullpath)+255);

    // This loop occurs for every object in the folder.
    while ((currentdir = readdir(dp)) && currentdir) {
        // This if statement stops the program from recursing on the current 
        // directory or the parent directory by skipping the iterations when it
        // reads '.' and '..'.
        if ( (strcmp(currentdir->d_name, ".") == 0) || 
            (strcmp(currentdir->d_name, "..")) == 0 )
            continue;

        // Each item is given a fault value indicating it should be printed to
        // the standard output and each item is given a value, adir, storing
        // whether it is a directory or not.
        adir=currentdir->d_type & DT_DIR;
        shouldprint=0;

        // For each file, the fullpath is updated to reflect the path to, and 
        // including, the file.
        strcat(fullpath, "/");
        strcat(fullpath, currentdir->d_name);

        // If the item is a directory and the fflag indicated not to print 
        // directories, it is marked to not be printed.
        if (adir && !(fflag))
            shouldprint=1;

        // This checks if the files meet the filesize and timelimit parameters.
        if (shouldprint == 0) {
            struct stat sb;
            if (lstat(fullpath, &sb) >= 0) {
                if ((s != 0) && (sb.st_size < s))
                    shouldprint=1;
                if (m > sb.st_mtime)
                    shouldprint=1;
            }
        }

        // The item is printed unless it did not pass one of the above tests.
        if (shouldprint == 0)
            printf("%s\n", fullpath);

        // If the item is a direcory and is not passed the recursion depth, if 
        // specified by the user, then it is recursed upon and the depth count 
        // is increased by 1 before recursing into the directory.
        if ((adir) && (d > 0)) {
            d=d-1;
            printdircontents();
            d=d+1;
        }
        // Once an item has been iterated over, the fullpath variable is reset 
        // to the path without the file on the end.
        fullpath[strlen(fullpath)-strlen(currentdir->d_name) - 1] = '\0';
    }
    // The directory is closed after reading.
    closedir(dp);
}
Exemple #12
0
int32_t iguana_parser(struct iguana_info *coin,struct iguana_peer *addr,struct iguana_msghdr *H,uint8_t *data,int32_t datalen)
{
    int32_t height,i,retval,srvmsg,bloom,intvectors,len= -100; uint64_t nonce,x;
    uint32_t type,firsttxidind,firstvout,firstvin; bits256 hash2; double PoW;
    bloom = intvectors = srvmsg = -1;
    if ( addr != 0 )
    {
        addr->lastcontact = (uint32_t)time(NULL);
        strcpy(addr->lastcommand,H->command);
    }
    retval = 0;
    //printf("%s parse.(%s)\n",addr->ipaddr,H->command);
    if ( strcmp(H->command,"version") == 0 )
    {
        struct iguana_msgversion recvmv;
        if ( addr != 0 )
        {
            len = iguana_rwversion(0,data,&recvmv,addr->ipaddr);
            if ( len == datalen )
                iguana_gotversion(coin,addr,&recvmv);
            //printf("deser.(%s) len.%d datalen.%d\n",recvmv.H.command,len,datalen);
            addr->msgcounts.version++;
        }
    }
    else if ( strcmp(H->command,"verack") == 0 )
    {
        if ( addr != 0 )
        {
            iguana_gotverack(coin,addr);
            addr->msgcounts.verack++;
        }
        len = 0;
    }
    else if ( strcmp(H->command,"ping") == 0 )
    {
        if ( datalen == sizeof(uint64_t) && addr != 0 )
        {
            len = iguana_rwnum(0,data,sizeof(uint64_t),&nonce);
            if ( addr != 0 )
            {
                //printf("%u got nonce.%llx from %s\n",(uint32_t)time(NULL),(long long)nonce,addr->ipaddr);
                iguana_gotping(coin,addr,nonce,data);
                addr->msgcounts.ping++;
            }
        }
    }
    else if ( strcmp(H->command,"pong") == 0 )
    {
        len = 0;
        if ( datalen == sizeof(uint64_t) )
        {
            len = iguana_rwnum(0,data,sizeof(uint64_t),&nonce);
            iguana_gotpong(coin,addr,nonce);
        } else printf("unexpected pong datalen.%d\n",datalen);
        if ( len == datalen && addr != 0 )
            addr->msgcounts.pong++;
    }
    else if ( strcmp(H->command,"addr") == 0 )
    {
        struct iguana_msgaddress A;
        len = iguana_rwvarint(0,data,&x);
        for (i=0; i<x; i++)
        {
            memset(&A,0,sizeof(A));
            if ( addr != 0 )
                len += iguana_rwaddr(0,&data[len],&A,(int32_t)addr->protover);
            iguana_gotaddr(coin,addr,&A);
        }
        if ( len == datalen && addr != 0 )
        {
            addr->lastgotaddr = (uint32_t)time(NULL);
            addr->msgcounts.addr++;
        }
        //printf("%s -> addr datalen.%d num.%d\n",addr->ipaddr,datalen,(int32_t)x);
    }
    else if ( strcmp(H->command,"headers") == 0 )
    {
        struct iguana_msgblock msg; struct iguana_block *blocks; uint32_t n;
        len = iguana_rwvarint32(0,data,&n);
        if ( n <= IGUANA_MAXINV )
        {
            blocks = mycalloc('i',1,sizeof(*blocks) * n);
            height = -1;
            for (i=0; i<n; i++)
            {
                len += iguana_rwblock(0,&hash2,&data[len],&msg);
                if ( i == 0 )
                    height = iguana_setchainvars(coin,&firsttxidind,&firstvout,&firstvin,&PoW,hash2,msg.H.bits,msg.H.prev_block,msg.txn_count);
                iguana_convblock(&blocks[i],&msg,hash2,height,firsttxidind,firstvout,firstvin,PoW);
                if ( firsttxidind > 0 )
                {
                    height++;
                    firsttxidind += blocks[i].txn_count;
                    PoW += PoW_from_compact(blocks[i].bits,coin->chain->unitval);
                }
            }
            //printf("GOT HEADERS n.%d len.%d\n",n,len);
            iguana_gotheadersM(coin,addr,blocks,n);
            //myfree(blocks,sizeof(*blocks) * n);
            if ( len == datalen && addr != 0 )
                addr->msgcounts.headers++;
        } else printf("got unexpected n.%d for headers\n",n);
    }
    else if ( strcmp(H->command,"tx") == 0 )
    {
        struct iguana_msgtx *tx;
        tx = mycalloc('u',1,sizeof(*tx));
        len = iguana_rwtx(0,data,tx,datalen,&tx->txid,-1,coin->chain->hastimestamp);
        if ( len == datalen && addr != 0 )
        {
            iguana_gotunconfirmedM(coin,addr,tx,data,datalen);
            printf("tx datalen.%d\n",datalen);
            addr->msgcounts.tx++;
        }
    }
    else if ( strcmp(H->command,"block") == 0 )
    {
        struct iguana_block *block; struct iguana_msgtx *tx; uint8_t extra[256];
        block = mycalloc('b',1,sizeof(*block));
        memset(extra,0,sizeof(extra));
        tx = iguana_gentxarray(coin,&len,block,data,datalen,extra);
        if ( len == datalen )
        {
            if ( addr != 0 )
            {
                if ( len == datalen )
                    addr->msgcounts.block++;
                //addr->OV.reqrecv += datalen;
                //printf("%s gotblock.%d datalen.%d last.[%02x]\n",addr->ipaddr,block->height,datalen,data[len-1]);
            }
            iguana_gotblockM(coin,addr,block,tx,block->txn_count,data,datalen,extra);
        } else printf("parse error block txn_count.%d, len.%d vs datalen.%d\n",block->txn_count,len,datalen);
        //if ( tx != 0 )
        //    iguana_freetx(tx,block->txn_count);
        //myfree(block,sizeof(*block));
    }
    else if ( strcmp(H->command,"reject") == 0 )
    {
        for (i=0; i<datalen; i++)
            printf("%02x ",data[i]);
        printf("reject.(%s) datalen.%d\n",data+1,datalen);
        len = datalen;
        if ( len == datalen && addr != 0 )
            addr->msgcounts.reject++;
    }
    else if ( strcmp(H->command,"alert") == 0 )
    {
        for (i=0; i<datalen; i++)
            printf("%02x ",data[i]);
        printf("alert.(%s)\n",data+1);
        len = datalen;
        if ( len == datalen && addr != 0 )
            addr->msgcounts.alert++;
    }
    else if ( addr != 0 )
    {
        if ( strcmp(H->command,"inv") == 0 )
            intvectors = 'I', addr->msgcounts.inv++;
        else if ( strcmp(H->command,"notfound") == 0 ) // for servers
            intvectors = 'N', addr->msgcounts.notfound++;
        else if ( strcmp(H->command,"getdata") == 0 ) // for servers
            intvectors = srvmsg = 'D', addr->msgcounts.getdata++;
        else if ( strcmp(H->command,"getblocks") == 0 ) // for servers
            srvmsg = 'B', addr->msgcounts.getblocks++;
        else if ( strcmp(H->command,"getheaders") == 0 ) // for servers
            srvmsg = 'H', addr->msgcounts.getheaders++;
        else if ( strcmp(H->command,"getaddr") == 0 ) // for servers
            srvmsg = 'A', addr->msgcounts.getaddr++;
        else if ( strcmp(H->command,"mempool") == 0 ) // for servers
            srvmsg = 'M', addr->msgcounts.mempool++;
        else if ( strcmp(H->command,"filterload") == 0 ) // for bloom
            bloom = 'L', addr->msgcounts.filterload++;
        else if ( strcmp(H->command,"filteradd") == 0 ) // for bloom
            bloom = 'A', addr->msgcounts.filteradd++;
        else if ( strcmp(H->command,"filterclear") == 0 ) // for bloom
            bloom = 'C', addr->msgcounts.filterclear++;
        else if ( strcmp(H->command,"merkleblock") == 0 ) // for bloom
            bloom = 'M', addr->msgcounts.merkleblock++;
    }
    if ( bloom >= 0 || srvmsg >= 0 )
        len = datalen; // just mark as valid
    if ( intvectors >= 0 )
    {
        bits256 *txids=0,*blockhashes=0,hash; int32_t n,m;
        len = n = m = 0;
        len += iguana_rwvarint(0,&data[len],&x);
        for (i=0; i<x; i++)
        {
            len += iguana_rwnum(0,&data[len],sizeof(uint32_t),&type);
            len += iguana_rwbignum(0,&data[len],sizeof(bits256),hash.bytes);
            if ( type == MSG_TX )
            {
                if ( txids == 0 )
                    txids = mycalloc('t',(int32_t)x,sizeof(*txids));
                txids[m++] = hash;
                if ( (rand() % 1000) == 0 && i == x-1 )
                    printf("%s iv.%c %d of %d: tx.%llx len.%d\n",addr->ipaddr,intvectors,i,(int32_t)x,(long long)hash.txid,len);
            }
            else if ( type == MSG_BLOCK )
            {
                if ( blockhashes == 0 )
                    blockhashes = mycalloc('f',(int32_t)x,sizeof(*blockhashes));
                blockhashes[n++] = hash;
                //init_hexbytes_noT(hashstr,hash.bytes,sizeof(hash));
                //queue_enqueue("blocksQ",&coin->blocksQ,queueitem(hashstr),1);
                //if ( i == x-2 )
                //    queue_enqueue("hdrsQ",&coin->hdrsQ,queueitem(hashstr),1);
                //printf("iv.%d %d of %d: block.%llx len.%d %s\n",intvectors,i,(int32_t)x,(long long)hash.txid,len,bits256_str(hash));
            }
            else if ( type == MSG_FILTERED_BLOCK )
                printf("iv.%d %d of %d: merkle.%llx\n",intvectors,i,(int32_t)x,(long long)hash.txid);
            else printf("what type is %d\n",type);
        }
        if ( intvectors == 'I' )
        {
            if ( n > 0 )
            {
                if ( n != x )
                {
                    //printf("n.%d != x.%d -> realloc blockhashes\n",n,(int32_t)x);
                    blockhashes = myrealloc('f',blockhashes,(int32_t)(x*sizeof(*blockhashes)),n*sizeof(*blockhashes));
                } // else printf("n.%d == x.%d\n",n,(int32_t)x);
                iguana_gotblockhashesM(coin,addr,blockhashes,n), blockhashes = 0;
            }
            if ( m > 0 )
            {
                if ( m != x )
                    txids = myrealloc('t',txids,(int32_t)(x*sizeof(*txids)),m*sizeof(*txids));
                iguana_gottxidsM(coin,addr,txids,m), txids = 0;
            }
        }
        if ( txids != 0 )
            myfree(txids,sizeof(*txids) * x);
        if ( blockhashes != 0 )
            myfree(blockhashes,sizeof(*blockhashes) * x);
        //printf("intvectors.%c datalen.%d\n",intvectors,datalen);
    }
    if ( len != datalen && len != datalen-1 )
    {
        //printf("error.(%s) (%s): len.%d != datalen.%d\n",H->command,addr->ipaddr,len,datalen);
        //for (i=0; i<len; i++)
        //    printf("%02x",data[i]);
        if ( strcmp(H->command,"addr") != 0 )
            printf("%s.%s len mismatch %d != %d\n",addr!=0?addr->ipaddr:"local",H->command,len,datalen);
    }
    else if ( len == datalen-1 )
    {
        printf("extra byte.[%02x] command.%s len.%d datalen.%d\n",data[datalen-1],H->command,len,datalen);
        //retval = -1;
    }
    return(retval);
}
Exemple #13
0
void add_convert(int linenr, char *cmd, char *par)
{
    char *conv_name = par;
    char *conv_type = NULL;
    conversion_t type = 0;
    char *conv_script = NULL;
    char *conv_re = NULL;
    int loop;

    /* parse type */
    conv_type = find_next_par(conv_name);
    if (!conv_type)
        config_error_exit(linenr, "'convert'-entry malformed: conversion type missing.\n");

    if (strncmp(conv_type, "script:", 7) == 0)
    {
        conv_script = find_next_par(conv_type);
        if (conv_script)
            conv_re = find_next_par(conv_script);
        else
            config_error_exit(linenr, "Convert: script filename missing.\n");
    }
    else
        conv_re = find_next_par(conv_type);

    if (!conv_re)
        config_error_exit(linenr, "'convert'-entry malformed: type or regular expression missing.\n");

    /* find this conversion: is it from an already known group? */
    for(loop=0; loop<n_conversions; loop++)
    {
        if (strcmp(conversions[loop].name, conv_name) == 0)
            break;
    }
    /* create new group */
    if (loop == n_conversions)
    {
        n_conversions++;
        conversions = (conversion *)myrealloc(conversions, sizeof(conversion) * n_conversions);
        memset(&conversions[loop], 0x00, sizeof(conversion));
        conversions[loop].name = mystrdup(conv_name);
    }

    if (strcmp(conv_type, "ip4tohost") == 0)
        type = CONVTYPE_IP4TOHOST;
    else if (strcmp(conv_type, "epochtodate") == 0)
        type = CONVTYPE_EPOCHTODATE;
    else if (strcmp(conv_type, "errnotostr") == 0)
        type = CONVTYPE_ERRNO;
    else if (strcmp(conv_type, "hextodec") == 0)
        type = CONVTYPE_HEXTODEC;
    else if (strcmp(conv_type, "dectohex") == 0)
        type = CONVTYPE_DECTOHEX;
    else if (strcmp(conv_type, "tai64todate") == 0)
        type = CONVTYPE_TAI64NTODATE;
    else if (strcmp(conv_type, "script") == 0)
        type = CONVTYPE_SCRIPT;
    else if (strcmp(conv_type, "abbrtok") == 0)
        type = CONVTYPE_ABBRTOK;
    else if (strcmp(conv_type, "signrtostring") == 0)
        type = CONVTYPE_SIGNRTOSTRING;
    else
        config_error_exit(linenr, "Convert %s: '%s' is a not recognized conversion type.\n", conv_name, conv_type);

    conversions[loop].pcb = (conversion_bundle_t *)myrealloc(conversions[loop].pcb, sizeof(conversion_bundle_t) * (conversions[loop].n + 1));
    conversions[loop].pcs = (script *)myrealloc(conversions[loop].pcs, sizeof(script) * (conversions[loop].n + 1));

    conversions[loop].pcb[conversions[loop].n].type = type;


    memset(&conversions[loop].pcs[conversions[loop].n], 0x00, sizeof(script));
    conversions[loop].pcs[conversions[loop].n].script = conv_script?mystrdup(conv_script):NULL;

    compile_re(&conversions[loop].pcb[conversions[loop].n].regex, conv_re);
    conversions[loop].pcb[conversions[loop].n].match_count = 0;
    conversions[loop].n++;
}
Exemple #14
0
void merged_scrollback_with_search(char *search_for, mybool_t case_insensitive)
{
	int lc_size = nfd * sizeof(int);
	int *last_check = (int *)mymalloc(lc_size);
	int *winnr = NULL;
	buffer buf;
	regex_t reg;
	int rc;

	memset(last_check, 0x00, lc_size);
	memset(&buf, 0x00, sizeof(buf));

	/* compile the search string which is supposed to be a valid regular
	 * expression
	 */
	if (search_for)
	{
		if ((rc=regcomp(&reg, search_for, REG_EXTENDED | (case_insensitive == MY_TRUE?REG_ICASE:0))))
		{
			regexp_error_popup(rc, &reg);
			free(last_check);
			return;
		}
	}

	/* merge all windows into one */
	for(;;)
	{
		int loop;
		double chosen_ts = (double)(((long long int)1) << 62);
		int chosen_win = -1;
		int curline;
		char *string;
		int checked_all = 0;

		for(loop=0; loop<nfd; loop++)
		{
			if (lb[loop].curpos == last_check[loop])
			{
				checked_all++;
				continue;
			}

			if (search_for != NULL && lb[loop].be[last_check[loop]].Bline != NULL)
			{
				rc = regexec(&reg, lb[loop].be[last_check[loop]].Bline, 0, NULL, 0);

				/* did not match? don't add and continue */
				if (rc == REG_NOMATCH)
				{
					continue;
				}

				/* is it an error? then popup and abort */
				if (rc != 0)
				{
					regexp_error_popup(rc, &reg);
					break;
				}
			}

			if (lb[loop].be[last_check[loop]].ts <= chosen_ts)
			{
				chosen_ts = lb[loop].be[last_check[loop]].ts;
				chosen_win = loop;
			}
		}

		if (chosen_win == -1)
		{
			if (checked_all == nfd)
				break;

			for(loop=0; loop<nfd; loop++)
			{
				if (lb[loop].curpos > last_check[loop])
					last_check[loop]++;
			}

			continue;
		}

		if (!IS_MARKERLINE(lb[chosen_win].be[last_check[chosen_win]].pi))
		{
			/*** ADD LINE TO BUFFER ***/
			buf.be = (buffered_entry *)myrealloc(buf.be, sizeof(buffered_entry) * (buf.curpos + 1));
			winnr  = (int *)myrealloc(winnr, sizeof(int) * (buf.curpos + 1));
			curline = buf.curpos++;
			/* add the logline itself */
			string = lb[chosen_win].be[last_check[chosen_win]].Bline;
			if (string)
				buf.be[curline].Bline = mystrdup(string);
			else
				buf.be[curline].Bline = NULL;
			/* remember pointer to subwindow (required for setting colors etc.) */
			buf.be[curline].pi = lb[chosen_win].be[last_check[chosen_win]].pi;
			buf.be[curline].ts = lb[chosen_win].be[last_check[chosen_win]].ts;
			/* remember window nr. */
			winnr[curline] = chosen_win;
		}

		last_check[chosen_win]++;
	}

	if (buf.curpos == 0)
		error_popup("Search in all windows", -1, "Nothing found.");
	else
	{
		char *header;

		if (search_for)
		{
			char *help = "Searched for: ";
			int len = strlen(help) + strlen(search_for) + 1;
			header = mymalloc(len);
			snprintf(header, len, "%s%s", help, search_for);
		}
		else
		{
			char *help = "Merge view";
			header = mymalloc(strlen(help) + 1);
			sprintf(header, "%s", help);
		}

		scrollback_do(-1, &buf, winnr, header);

		myfree(header);
	}

	delete_be_in_buffer(&buf);

	myfree(winnr);

	myfree(last_check);
}
Exemple #15
0
int dead_assignments(struct flowgraph *fg)
/*  Findet Zuweisungen, die unnoetig sind, da die Variable nie mehr     */
/*  benutzt werden kann.                                                */
{
    int changed=0;struct IC *p;bvtype *isused;
    int i,j;
    if(DEBUG&1024) printf("searching for dead assignments\n");
    isused=mymalloc(vsize);
    while(fg){
      memcpy(isused,fg->av_out,vsize);
      p=fg->end;
      while(p){
	if(p->z.flags&VAR){
	  i=p->z.v->index;
	  if(p->z.flags&DREFOBJ) i+=vcount-rcount;
	  if(!BTST(isused,i)&&!is_volatile_ic(p)&&!(disable&1)){
	    if(DEBUG&1024){printf("dead assignment deleted:\n");pric2(stdout,p);}
	    if(*p->z.v->identifier&&p->code!=ASSIGN){ err_ic=p;error(170,i>=vcount-rcount?"*":"",p->z.v->identifier);err_ic=0;}
	    if(p->code!=GETRETURN) changed=1;
	    if(p==fg->start){remove_IC_fg(fg,p);break;}
	    p=p->prev;remove_IC_fg(fg,p->next);
	    continue;
	  }
	}
	if(p->code!=SETRETURN&&p->code!=TEST&&p->code!=COMPARE&&(p->q1.flags&VAR)&&!BTST(isused,p->q1.v->index)&&(!(p->z.flags&VAR)||!p->z.v->reg||p->z.v->identifier)){
          struct IC *m,*a;int f=p->q1.flags,dt=p->q1.dtyp;
	  p->q1.flags&=~DREFOBJ;
	  a=p->prev;if(a) m=a->prev; else m=0;
	  if(m&&a&&m->code==ASSIGN&&(a->q1.flags&(VAR|DREFOBJ))==VAR&&!compare_objs(&p->q1,&m->z,0)&&!compare_objs(&a->q1,&a->z,0)&&!compare_objs(&m->q1,&a->z,0)&&(a->q2.flags&KONST)&&!var_conflicts(a->q1.v,p)){
	    if(DEBUG&1024){
	      printf("reorder post-op(q1):\n");
	      pric2(stdout,m);pric2(stdout,a);pric(stdout,p);
	    }
	    p->q1=a->q1;
	    m->next=p;p->prev=m;
	    if(p->next) p->next->prev=a;
	    a->next=p->next;
	    a->prev=p;p->next=a;
	    if(fg->end==p) fg->end=a;
	    if(p==last_ic) last_ic=a;
	    remove_IC_fg(fg,m);
	    av_update(a,isused);
	    p->use_list=myrealloc(p->use_list,(p->use_cnt+a->use_cnt)*VLS);
	    memcpy(&p->use_list[p->use_cnt],a->use_list,a->use_cnt*VLS);
	    p->use_cnt+=a->use_cnt;
	    changed=1;
	    if((f&DREFOBJ)&&p->q1.v->index>=rcount)
	      ierror(0);
	  }
	  p->q1.flags=f;
	  p->q1.dtyp=dt;
	}
	if(p->code!=TEST&&p->code!=COMPARE&&(p->q2.flags&VAR)&&!BTST(isused,p->q2.v->index)&&(!(p->z.flags&VAR)||!p->z.v->reg||p->z.v->identifier)){
          struct IC *m,*a;int f=p->q2.flags,dt=p->q2.dtyp;
	  p->q2.flags&=~DREFOBJ;
	  a=p->prev;if(a) m=a->prev; else m=0;
	  if(m&&a&&m->code==ASSIGN&&(a->q1.flags&(VAR|DREFOBJ))==VAR&&!compare_objs(&p->q2,&m->z,0)&&!compare_objs(&a->q1,&a->z,0)&&!compare_objs(&m->q1,&a->z,0)&&(a->q2.flags&KONST)&&!var_conflicts(a->q1.v,p)){
	    if(DEBUG&1024){
	      printf("reorder post-op(q2):\n");
	      pric2(stdout,m);pric2(stdout,a);pric(stdout,p);
	    }
	    p->q2=a->q1;
	    m->next=p;p->prev=m;
	    if(p->next) p->next->prev=a;
	    a->next=p->next;
	    a->prev=p;p->next=a;
	    if(fg->end==p) fg->end=a;
	    if(p==last_ic) last_ic=a;
	    remove_IC_fg(fg,m);
	    av_update(a,isused);
	    p->use_list=myrealloc(p->use_list,(p->use_cnt+a->use_cnt)*VLS);
	    memcpy(&p->use_list[p->use_cnt],a->use_list,a->use_cnt*VLS);
	    p->use_cnt+=a->use_cnt;
	    changed=1;
	    if((f&DREFOBJ)&&p->q2.v->index>=rcount)
	      ierror(0);
	  }
	  p->q2.flags=f;
	  p->q2.dtyp=dt;
	}
	if(p->code!=TEST&&p->code!=COMPARE&&(p->z.flags&(VAR|DREFOBJ))==(VAR|DREFOBJ)&&!BTST(isused,p->z.v->index)){
          struct IC *m,*a;int f=p->z.flags,dt=p->z.dtyp;
	  p->z.flags&=~DREFOBJ;
	  a=p->prev;if(a) m=a->prev; else m=0;
	  if(m&&a&&m->code==ASSIGN&&(a->q1.flags&(VAR|DREFOBJ))==VAR&&!compare_objs(&p->z,&m->z,0)&&!compare_objs(&a->q1,&a->z,0)&&!compare_objs(&m->q1,&a->z,0)&&(a->q2.flags&KONST)&&!var_conflicts(a->q1.v,p)){
	    if(DEBUG&1024){
	      printf("reorder post-op(z):\n");
	      pric2(stdout,m);pric2(stdout,a);pric2(stdout,p);
	      printf("--");
	    }
	    p->z=a->q1;
	    m->next=p;p->prev=m;
	    if(p->next) p->next->prev=a;
	    a->next=p->next;
	    a->prev=p;p->next=a;
	    if(fg->end==p) fg->end=a;
	    if(p==last_ic) last_ic=a;
	    remove_IC_fg(fg,m);
	    av_update(a,isused);
	    p->use_list=myrealloc(p->use_list,(p->use_cnt+a->use_cnt)*VLS);
	    memcpy(&p->use_list[p->use_cnt],a->use_list,a->use_cnt*VLS);
	    p->use_cnt+=a->use_cnt;
	    changed=1;
	    if((f&DREFOBJ)&&p->z.v->index>=rcount)
	      ierror(0);
	  }
	  p->z.flags=f;
	  p->z.dtyp=dt;
	}
	av_update(p,isused);
	if(p==fg->start) break;
	p=p->prev;
      }
      fg=fg->normalout;
    }
    free(isused);
    return(changed);
}
Exemple #16
0
/** 
 * Register a physical %HMM as a member of a pseudo phone set.
 * 
 * @param root [i/o] root node of %HMM search index node.
 * @param d [in] a physical defined %HMM to be added.
 * @param cdname [in] name of the pseudo phone set.
 * 
 * @return TRUE if newly registered, FALSE if the specified physical %HMM already exists in the pseudo phone.
 */
boolean
regist_cdset(APATNODE **root, HTK_HMM_Data *d, char *cdname, BMALLOC_BASE **mroot)
{
  boolean need_new;
  CD_State_Set *tmp;
  CD_Set *lset = NULL, *lmatch = NULL;
  int j,n;
  boolean changed = FALSE;

  if (strlen(cdname) >= MAX_HMMNAME_LEN) {
    jlog("Error: cdset: HMM name exceeds limit (%d): %s!\n", MAX_HMMNAME_LEN, cdname);
    jlog("Error: cdset: Please increase the value of MAX_HMMNAME_LEN (current = %d)\n", MAX_HMMNAME_LEN);
    exit(1);
  }
  
  /* check if the cdset already exist */
  need_new = TRUE;
  if (*root != NULL) {
    lmatch = aptree_search_data(cdname, *root);
    if (lmatch != NULL && strmatch(lmatch->name, cdname)) {
      /* exist, add to it later */
      lset = lmatch;
      need_new = FALSE;
      /* if the state num is larger than allocated, expand the lset */
      if (d->state_num > lset->state_num) {
	lset->stateset = (CD_State_Set *)myrealloc(lset->stateset, sizeof(CD_State_Set) * d->state_num);
	/* 0 1 ... (lset->state_num-1) */
	/* N A ... N                   */
	/* 0 1 ...                     ... (d->state_num-1) */
	/* N A ... A ..................... N                */
	/* malloc new area to expanded state (N to A above) */
	for(j = lset->state_num - 1; j < d->state_num - 1; j++) {
	  lset->stateset[j].maxnum = CD_STATE_SET_STEP;
	  lset->stateset[j].s = (HTK_HMM_State **)mymalloc(sizeof(HTK_HMM_State *) * lset->stateset[j].maxnum);
	  lset->stateset[j].num = 0;
	}
	lset->stateset[d->state_num-1].s = NULL;
	lset->stateset[d->state_num-1].num = 0;
	lset->stateset[d->state_num-1].maxnum = 0;
	
	lset->state_num = d->state_num;

	/* update transition table */
	lset->tr = d->tr;

	changed = TRUE;
      }
    }
  }

  if (need_new) {
    /* allocate as new with blank data */
    lset = cdset_new();
    lset->name = strdup(cdname);
    lset->state_num = d->state_num;
    lset->stateset = (CD_State_Set *)mymalloc(sizeof(CD_State_Set) * lset->state_num);
    /* assume first and last state has no outprob */
    lset->stateset[0].s = lset->stateset[lset->state_num-1].s = NULL;
    lset->stateset[0].num = lset->stateset[lset->state_num-1].num = 0;
    lset->stateset[0].maxnum = lset->stateset[lset->state_num-1].maxnum = 0;
    for(j=1;j<lset->state_num-1; j++) {
      /* pre-allocate only the first step */
      lset->stateset[j].maxnum = CD_STATE_SET_STEP;
      lset->stateset[j].s = (HTK_HMM_State **)mymalloc(sizeof(HTK_HMM_State *) * lset->stateset[j].maxnum);
      lset->stateset[j].num = 0;
    }
    /* assign transition table of first found %HMM (ad-hoc?) */
    lset->tr = d->tr;
    /* add to search index tree */
    if (*root == NULL) {
      *root = aptree_make_root_node(lset, mroot);
    } else {
      aptree_add_entry(lset->name, lset, lmatch->name, root, mroot);
    }

    changed = TRUE;
  }
    
  /* register each HMM states to the lcdset */
  for (j=1;j<d->state_num-1;j++) {
    tmp = &(lset->stateset[j]);
    /* check if the state has already registered */
    for(n = 0; n < tmp->num ; n++) {
      if (tmp->s[n] == d->s[j]) { /* compare by pointer */
	/*jlog("\tstate %d has same\n", n);*/
	break;
      }
    }
    if (n < tmp->num ) continue;	/* same state found, cancel regist. */
    
    /* expand storage area if necessary */
    if (tmp->num >= tmp->maxnum) {
      tmp->maxnum += CD_STATE_SET_STEP;
      tmp->s = (HTK_HMM_State **)myrealloc(tmp->s, sizeof(HTK_HMM_State *) * tmp->maxnum);
    }
    
    tmp->s[tmp->num] = d->s[j];
    tmp->num++;

    changed = TRUE;
  }

  return(changed);
}
Exemple #17
0
int scrollback_do(int window_nr, buffer *pbuf, int *winnrs, char *header)
{
	int rc = 0;
	char *find = NULL;
	char fullscreen = scrollback_fullscreen_default;
	NEWWIN *mywin1 = NULL, *mywin2 = NULL;

	int nlines, ncols;
	compute_text_dimensions(&nlines, &ncols, fullscreen);

	int offset = max(0, pbuf -> curpos - nlines); /* FIXME: aantal regels laten afhangen van lengte */
	char redraw = 2;
	int line_offset = 0;
	char show_winnr = default_sb_showwinnr;
	mybool_t case_insensitive = re_case_insensitive;
	buffer cur_lb;
	int loop = 0;

	memset(&cur_lb, 0x00, sizeof(cur_lb));

	for(loop=0; loop<pbuf -> curpos; loop++)
	{
		if ((pbuf -> be)[loop].Bline == NULL)
			continue;

		cur_lb.be = myrealloc(cur_lb.be, (cur_lb.curpos + 1) * sizeof(buffered_entry));
		cur_lb.be[cur_lb.curpos].pi    = (pbuf -> be)[loop].pi;
		if ((pbuf -> be)[loop].pi != NULL && (!IS_MARKERLINE((pbuf -> be)[loop].pi)) && (pbuf -> be)[loop].pi -> cdef.term_emul != TERM_IGNORE)
		{
			color_offset_in_line *cmatches;
			int n_cmatches;
			cur_lb.be[cur_lb.curpos].Bline = emulate_terminal((pbuf -> be)[loop].Bline, &cmatches, &n_cmatches);
			myfree(cmatches);
		}
		else
			cur_lb.be[cur_lb.curpos].Bline = strdup((pbuf -> be)[loop].Bline);
		cur_lb.be[cur_lb.curpos].ts    = (pbuf -> be)[loop].ts;
		cur_lb.curpos++;
	}

	LOG("---\n");
	if (global_highlight_str)
	{
		find = mystrdup(global_highlight_str);
	}

	create_scrollback_windows(&mywin1, &mywin2, nlines, ncols, fullscreen);

	for(;;)
	{
		int c, uc;

		if (redraw == 2)
		{
			int index = 0;
			int lines_used = 0;

			if (mywin1)
			{
				ui_inverse_on(mywin1);
				mvwprintw(mywin1 -> win, nlines + 1, 1, "%s - %d buffered lines", shorten_filename(header, max(24, ncols - 24)), cur_lb.curpos);
				ui_inverse_off(mywin1);

				if (!no_linewrap) ui_inverse_on(mywin1);
				mvwprintw(mywin1 -> win, nlines + 1, ncols - 8, "LINEWRAP");
				if (!no_linewrap) ui_inverse_off(mywin1);
			}

			werase(mywin2 -> win);

			if (!no_linewrap && line_offset > 0)
			{
				int temp_line_offset = line_offset;
				int n_chars_to_display_left = strlen((cur_lb.be)[offset].Bline) - temp_line_offset;

				while(lines_used < nlines && n_chars_to_display_left > 0)
				{
					scrollback_displayline(winnrs?winnrs[offset]:window_nr, mywin2, &cur_lb, offset, lines_used, temp_line_offset, 1, show_winnr);

					temp_line_offset += ncols;
					n_chars_to_display_left -= ncols;

					lines_used++;
				}

				index++;
			}

			for(;(offset + index) < cur_lb.curpos && lines_used < nlines;)
			{
				int lines_needed = get_lines_needed((cur_lb.be)[offset + index].Bline, ncols);

				if (no_linewrap || lines_needed == 1)
				{
					scrollback_displayline(winnrs?winnrs[offset + index]:window_nr, mywin2, &cur_lb, offset + index, lines_used, no_linewrap?line_offset:0, no_linewrap, show_winnr);
					lines_used++;
				}
				else
				{
					int cur_line_offset = 0;

					while(lines_used < nlines && lines_needed > 0)
					{
						scrollback_displayline(winnrs?winnrs[offset + index]:window_nr, mywin2, &cur_lb, offset + index, lines_used, cur_line_offset, 1, show_winnr);
						cur_line_offset += ncols;
						lines_used++;
						lines_needed--;
					}
				}

				index++;
			}

			redraw = 1;
		}

		if (redraw == 1)
		{
			mydoupdate();

			redraw = 0;
		}

		c = wait_for_keypress(HELP_SCROLLBACK_HELP, 0, NULL, 1);
		uc = toupper(c);

		if (c == 'q' || c == abort_key || c == KEY_CLOSE || c == KEY_EXIT)
		{
			break;
		}
		else if (c == 'Q' || c == -1)		/* Q: close whole stack of scrollbackwindows, -1: something got closed */
		{
			rc = -1;
			break;
		}
		else if (c == 20 && winnrs != NULL)	/* ^t */
		{
			show_winnr = 1 - show_winnr;
			redraw = 2;
		}
		else if (c == 'x')
		{
			send_to_clipboard(pbuf);
		}
		else if (c == 'Y')
		{
			no_linewrap = !no_linewrap;
			redraw = 2;
			line_offset = 0;
		}
		else if (c == KEY_F(9) || c == 23)	/* ^w */
		{
			fullscreen = ! fullscreen;
						
			compute_text_dimensions(&nlines, &ncols, fullscreen);

			create_scrollback_windows(&mywin1, &mywin2, nlines, ncols, fullscreen);

			redraw = 2;
		}
		else if (c == 't')
		{
			statistics_menu();
		}
		else if ((c == KEY_LEFT || c == KEY_BACKSPACE) && no_linewrap)
		{
			if (line_offset > 0)
				line_offset--;

			redraw = 2;
		}
		else if (c == KEY_SLEFT && no_linewrap)
		{
			if (line_offset >= (ncols / 2))
				line_offset -= (ncols / 2);
			else
				line_offset = 0;

			redraw = 2;
		}
		else if (c == KEY_SRIGHT && no_linewrap)
		{
			line_offset += (ncols / 2);

			redraw = 2;
		}
		else if (c == KEY_BEG && no_linewrap)
		{
			if (line_offset)
			{
				line_offset = 0;
				redraw = 2;
			}
		}
		else if (c == KEY_BTAB)
		{
			if (line_offset >= 4)
				line_offset -= 4;
			else
				line_offset = 0;

			redraw = 2;
		}
		else if (c == KEY_RIGHT && no_linewrap)
		{
			line_offset++;
			redraw = 2;
		}
		else if ((c == KEY_UP ||
					c == 'y' ||
					c == 25  || /* ^y */
					c == 'k' ||
					/* c == 11  || */ /* ^k */
					c == 16)    /* ^p */
				&& (offset > 0 || (!no_linewrap && line_offset > 0)))
		{
			if (no_linewrap)
			{
				offset--;
			}
			else if (line_offset > 0)
			{
				line_offset = max(0, line_offset - ncols);
			}
			else
			{
				offset--;

				line_offset = (get_lines_needed((cur_lb.be)[offset].Bline, ncols) - 1) * ncols;
			}

			wmove(mywin2 -> win, 0, 0);
			winsdelln(mywin2 -> win, 1);

			scrollback_displayline(winnrs?winnrs[offset]:window_nr, mywin2, &cur_lb, offset, 0, line_offset, no_linewrap, show_winnr);

			redraw = 1;
		}
		else if ((c == KEY_DOWN ||
					c == 'e' ||
					c == 5   || /* ^e */
					c == 'j' ||
					c == 14  || /* ^n */
					c == 13  ||
					c == KEY_ENTER)
				&& offset < (cur_lb.curpos - 1))
		{
			if (no_linewrap)
			{
				offset++;
			}
			else if (strlen((cur_lb.be)[offset].Bline) > (line_offset + ncols))
			{
				line_offset += ncols;
			}
			else if (offset < (cur_lb.curpos - 1))
			{
				if (strlen((cur_lb.be)[offset].Bline) > (line_offset + ncols))
					line_offset += ncols;
				else
				{
					line_offset = 0;
					offset++;
				}
			}

			redraw = 2;
		}
		else if ((c == KEY_NPAGE ||
					c == 'f' ||
					c == 6   || /* ^f */
					c == ('V' - 65 + 1) || /* ^v */
					c == ' ' ||
					c == 'z' ||
					c == 'u' ||
					c == ('U' - 65 + 1))   /* ^u */
				&& offset < (cur_lb.curpos - 1))
		{
			if (no_linewrap)
			{
				offset += nlines;
				if (offset >= cur_lb.curpos)
					offset = cur_lb.curpos - 1;
			}
			else
			{
				int n_lines_to_move = nlines;

				while(n_lines_to_move > 0 && offset < (cur_lb.curpos))
				{
					if (line_offset > 0)
					{
						if (line_offset + ncols >= strlen((cur_lb.be)[offset].Bline))
						{
							line_offset = 0;
							offset++;
							n_lines_to_move--;
						}
						else
						{
							line_offset += ncols;
							n_lines_to_move--;
						}
					}
					else
					{
						n_lines_to_move -= get_lines_needed((cur_lb.be)[offset].Bline, ncols);
						offset++;
					}
				}

				if (n_lines_to_move < 0)
					line_offset = (-n_lines_to_move) * ncols;
			}

			redraw = 2;
		}
		else if ((c == KEY_PPAGE ||
					c == 'b' ||
					c == 2   ||     /* ^b */
					c == 'w' ||
					c == 'd' ||
					c == 4)         /* ^d */
				&& offset > 0)
		{
			if (no_linewrap)
			{
				offset -= nlines;
				if (offset < 0)
					offset = 0;
			}
			else
			{
				int n_lines_to_move = nlines;

				if (line_offset)
					n_lines_to_move -= line_offset / ncols;

				while(n_lines_to_move > 0 && offset > 0)
				{
					offset--;

					n_lines_to_move -= get_lines_needed((cur_lb.be)[offset].Bline, ncols);

					if (n_lines_to_move < 0)
					{
						line_offset = (get_lines_needed((cur_lb.be)[offset].Bline, ncols) + n_lines_to_move) * ncols;
					}
				}
			}

			redraw = 2;
		}
		else if ((c == KEY_HOME ||
					c == 'g' ||
					c == '<' ||
					c == KEY_SBEG)
				&& offset > 0)
		{
			line_offset = offset = 0;
			redraw = 2;
		}
		else if ((c == KEY_END ||
					c == 'G' ||
					c == '>' ||
					c == KEY_SEND)
				&& offset < (cur_lb. curpos - 1))
		{
			offset = cur_lb. curpos - 1;
			redraw = 2;
		}
		else if (uc == 'R' || c == ('R' - 65 + 1) || c == ('L' - 65 + 1) || c == KEY_REFRESH)
		{
			redraw = 2;
		}
		else if (c == ('K' - 65 + 1) || c == KEY_MARK)
		{
			scrollback_find_popup(&find, &case_insensitive);

			if (find)
			{
				int rc;

				regfree(&global_highlight_re);
				myfree(global_highlight_str);
				global_highlight_str = NULL;

				if ((rc = regcomp(&global_highlight_re, find, (case_insensitive == MY_TRUE?REG_ICASE:0) | REG_EXTENDED)))
				{
					regexp_error_popup(rc, &global_highlight_re);
					myfree(find);
				}
				else
				{
					global_highlight_str = find;
				}

				redraw = 2; /* force redraw */
			}

		}
		else if (c == 'f' || c == '/' || c == '?' || c == KEY_FIND || c == KEY_SFIND)
		{
			char direction = (c == '?' || c == KEY_SFIND) ? -1 : 1;

			scrollback_find_popup(&find, &case_insensitive);

			if (find)
			{
				if (scrollback_search_new_window)
				{
					if (scrollback_search_to_new_window(&cur_lb, header, find, case_insensitive) == -1)
					{
						/* cascaded close */
						rc = -1;
						break;
					}
				}
				else
				{
					int new_f_index;

					redraw = 2; /* force redraw */

					regfree(&global_highlight_re);
					myfree(global_highlight_str);
					global_highlight_str = NULL;

					new_f_index = find_string(&cur_lb, find, 0, direction, case_insensitive);
					if (new_f_index == -1)
					{
						wrong_key();
					}
					else
					{
						offset = new_f_index;
						line_offset = 0;
					}
				}
			}
		}
		else if (uc == 'N' || c == KEY_NEXT || c == KEY_PREVIOUS || c == KEY_SNEXT)
		{
			if (find != NULL)
			{
				char direction = (c == 'n' || c == KEY_NEXT) ? 1 : -1;
				int start_offset = offset + direction;
				int new_f_index = find_string(&cur_lb, find, start_offset, direction, case_insensitive);
				if (new_f_index == -1)
				{
					wrong_key();
				}
				else
				{
					redraw = 2; /* force redraw */
					offset = new_f_index;
					line_offset = 0;
				}
			}
			else
			{
				wrong_key();
			}
		}
		else if (c == 's' || c == KEY_SAVE)
		{
			scrollback_savefile(&cur_lb);
			redraw = 2;	/* force redraw */
		}
		else if (c == 'h')
		{
			show_help(HELP_SCROLLBACK_HELP);
		}
		else if (c == 'c')
		{
			toggle_colors();
			redraw = 2;	/* force redraw */
		}
		else if (c == 'i')
		{
			info();
		}
		else if (c == 'T')
		{
			statistics_menu();
		}
		else if (c == 20)
		{
			toggle_subwindow_nr();
			redraw = 2;	/* force redraw */
		}
		else
		{
			wrong_key();
		}
	}

	delete_popup(mywin2);
	if (mywin1)
		delete_popup(mywin1);

	myfree(find);

	delete_be_in_buffer(&cur_lb);

	return rc;
}
/** 
 * <JA>
 * @brief  第1パス平行音声認識処理のメイン
 *
 * この関数内では,漸次的な特徴量抽出および第1パスの認識が行われる. 
 * 入力データに対して窓掛け・シフトを行いMFCC計算を行いながら,
 * 音声認識を1フレームずつ並列実行する. 
 *
 * 認識処理(decode_proceed())において,音声区間終了が要求される
 * ことがある. この場合,未処理の音声を保存して第1パスを終了する
 * よう呼出元に要求する. 
 *
 * SPSEGMENT_NAIST あるいは GMM_VAD などのバックエンドVAD定義時は,デコーダベースの
 * VAD (音声区間開始検出)に伴うデコーディング制御が行われる. 
 * トリガ前は,認識処理が呼ばれるが,実際には各関数内で認識処理は
 * 行われていない. 開始を検出した時,この関数はそこまでに得られた
 * MFCC列を一定フレーム長分巻戻し,その巻戻し先から通常の認識処理を
 * 再開する. なお,複数処理インスタンス間がある場合,開始トリガは
 * どれかのインスタンスが検出した時点で全ての開始が同期される. 
 * 
 * この関数は,音声入力ルーチンのコールバックとして呼ばれる.
 * 音声データの数千サンプル録音ごとにこの関数が呼び出される. 
 * 
 * @param Speech [in] 音声データへのバッファへのポインタ
 * @param nowlen [in] 音声データの長さ
 * @param recog [i/o] engine instance
 * 
 * @return エラー時に -1 を,正常時に 0 を返す. また,第1パスを
 * 終了するよう呼出元に要求するときは 1 を返す. 
 * </JA>
 * <EN>
 * @brief  Main function of the on-the-fly 1st pass decoding
 *
 * This function performs sucessive MFCC calculation and 1st pass decoding.
 * The given input data are windowed to a certain length, then converted
 * to MFCC, and decoding for the input frame will be performed in one
 * process cycle.  The loop cycle will continue with window shift, until
 * the whole given input has been processed.
 *
 * In case of input segment request from decoding process (in
 * decode_proceed()), this function keeps the rest un-processed speech
 * to a buffer and tell the caller to stop input and end the 1st pass.
 *
 * When back-end VAD such as SPSEGMENT_NAIST or GMM_VAD is defined,  Decoder-based
 * VAD is enabled and its decoding control will be managed here.
 * In decoder-based VAD mode, the recognition will be processed but
 * no output will be done at the first un-triggering input area.
 * when speech input start is detected, this function will rewind the
 * already obtained MFCC sequence to a certain frames, and re-start
 * normal recognition at that point.  When multiple recognition process
 * instance is running, their segmentation will be synchronized.
 * 
 * This function will be called each time a new speech sample comes as
 * as callback from A/D-in routine.
 * 
 * @param Speech [in] pointer to the speech sample segments
 * @param nowlen [in] length of above
 * @param recog [i/o] engine instance
 * 
 * @return -1 on error (will close stream and terminate recognition),
 * 0 on success (allow caller to call me for the next segment).  It
 * returns 1 when telling the caller to segment now at the middle of
 * input , and 2 when input length overflow is detected.
 * </EN>
 *
 * @callgraph
 * @callergraph
 * 
 */
int
RealTimePipeLine(SP16 *Speech, int nowlen, Recog *recog) /* Speech[0...nowlen] = input */
{
  int i, now, ret;
  MFCCCalc *mfcc;
  RealBeam *r;

  r = &(recog->real);

#ifdef DEBUG_VTLN_ALPHA_TEST
  /* store speech */
  adin_cut_callback_store_buffer(Speech, nowlen, recog);
#endif

  /* window[0..windownum-1] は前回の呼び出しで残った音声データが格納されている */
  /* window[0..windownum-1] are speech data left from previous call */

  /* 処理用ポインタを初期化 */
  /* initialize pointer for local processing */
  now = 0;
  
  /* 認識処理がセグメント要求で終わったのかどうかのフラグをリセット */
  /* reset flag which indicates whether the input has ended with segmentation request */
  r->last_is_segmented = FALSE;

#ifdef RDEBUG
  printf("got %d samples\n", nowlen);
#endif

  while (now < nowlen) {	/* till whole input is processed */
    /* 入力長が maxframelen に達したらここで強制終了 */
    /* if input length reaches maximum buffer size, terminate 1st pass here */
    for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
      if (mfcc->f >= r->maxframelen) return(1);
    }
    /* 窓バッファを埋められるだけ埋める */
    /* fill window buffer as many as possible */
    for(i = min(r->windowlen - r->windownum, nowlen - now); i > 0 ; i--)
      r->window[r->windownum++] = (float) Speech[now++];
    /* もし窓バッファが埋まらなければ, このセグメントの処理はここで終わる. 
       処理されなかったサンプル (window[0..windownum-1]) は次回に持ち越し. */
    /* if window buffer was not filled, end processing here, keeping the
       rest samples (window[0..windownum-1]) in the window buffer. */
    if (r->windownum < r->windowlen) break;
#ifdef RDEBUG
    /*    printf("%d used, %d rest\n", now, nowlen - now);

	  printf("[f = %d]\n", f);*/
#endif

    for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
      mfcc->valid = FALSE;
      /* 窓内の音声波形から特徴量を計算して r->tmpmfcc に格納  */
      /* calculate a parameter vector from current waveform windows
	 and store to r->tmpmfcc */
      if ((*(recog->calc_vector))(mfcc, r->window, r->windowlen)) {
#ifdef ENABLE_PLUGIN
	/* call post-process plugin if exist */
	plugin_exec_vector_postprocess(mfcc->tmpmfcc, mfcc->param->veclen, mfcc->f);
#endif
	/* MFCC完成,登録 */
  	mfcc->valid = TRUE;
	/* now get the MFCC vector of current frame, now store it to param */
	if (param_alloc(mfcc->param, mfcc->f + 1, mfcc->param->veclen) == FALSE) {
	  jlog("ERROR: failed to allocate memory for incoming MFCC vectors\n");
	  return -1;
	}
	memcpy(mfcc->param->parvec[mfcc->f], mfcc->tmpmfcc, sizeof(VECT) * mfcc->param->veclen);
#ifdef RDEBUG
	printf("DeltaBuf: %02d: got frame %d\n", mfcc->id, mfcc->f);
#endif
      }
    }

    /* 処理を1フレーム進める */
    /* proceed one frame */
    ret = proceed_one_frame(recog);

    if (ret == 1 && recog->jconf->decodeopt.segment) {
      /* ショートポーズセグメンテーション: バッファに残っているデータを
	 別に保持して,次回の最初に処理する */
      /* short pause segmentation: there is some data left in buffer, so
	 we should keep them for next processing */
      r->rest_len = nowlen - now;
      if (r->rest_len > 0) {
	/* copy rest samples to rest_Speech */
	if (r->rest_Speech == NULL) {
	  r->rest_alloc_len = r->rest_len;
	  r->rest_Speech = (SP16 *)mymalloc(sizeof(SP16)*r->rest_alloc_len);
	} else if (r->rest_alloc_len < r->rest_len) {
	  r->rest_alloc_len = r->rest_len;
	  r->rest_Speech = (SP16 *)myrealloc(r->rest_Speech, sizeof(SP16)*r->rest_alloc_len);
	}
	memcpy(r->rest_Speech, &(Speech[now]), sizeof(SP16) * r->rest_len);
      }
    }
    if (ret != 0) return ret;

    /* 1フレーム処理が進んだのでポインタを進める */
    /* proceed frame pointer */
    for (mfcc = recog->mfcclist; mfcc; mfcc = mfcc->next) {
      if (!mfcc->valid) continue;
      mfcc->f++;
    }

    /* 窓バッファを処理が終わった分シフト */
    /* shift window */
    memmove(r->window, &(r->window[recog->jconf->input.frameshift]), sizeof(SP16) * (r->windowlen - recog->jconf->input.frameshift));
    r->windownum -= recog->jconf->input.frameshift;
  }

  /* 与えられた音声セグメントに対する認識処理が全て終了
     呼び出し元に, 入力を続けるよう伝える */
  /* input segment is fully processed
     tell the caller to continue input */
  return(0);			
}
Exemple #19
0
/**
 * Update initial cepstral mean from previous utterances for next input.
 * 
 * @param c [i/o] CMN calculation work area
 */
void
CMN_realtime_update(CMNWork *c, HTK_Param *param)
{
  float *tmp, *tmp2;
  int i, d;
  int frames;

  /* if CMN_realtime was never called before this, return immediately */
  /* this may occur by pausing just after startup */
  if (c->now.framenum == 0) return;

  /* re-calculate variance based on the final mean at the given param */
  if (c->var && param != NULL) {
    float m, x;
    if (param->samplenum != c->now.framenum) {
      jlog("InternalError: CMN_realtime_update: param->samplenum != c->now.framenum\n");
    } else if (param->veclen != c->veclen) {
      jlog("InternalError: CMN_realtime_update: param->veclen != c->veclen\n");
    } else {
      for(d=0;d<c->veclen;d++) {
	m = c->now.mfcc_sum[d] / (float) c->now.framenum;
	x = 0;
	for(i=0;i<param->samplenum;i++) {
	  x += (param->parvec[i][d] - m) * (param->parvec[i][d] - m);
	}
	c->now.mfcc_var[d] = x;
      }
    }
  }

  /* compute cepstral mean from now and previous sums up to CPMAX frames */
  for(d=0;d<c->veclen;d++) c->cmean_init[d] = c->now.mfcc_sum[d];
  frames = c->now.framenum;
  for(i=0;i<c->clist_num;i++) {
    for(d=0;d<c->veclen;d++) c->cmean_init[d] += c->clist[i].mfcc_sum[d];
    frames += c->clist[i].framenum;
    if (frames >= CPMAX) break;
  }
  for(d=0;d<c->veclen;d++) c->cmean_init[d] /= (float) frames;

  c->cmean_init_set = TRUE;

  /* also compute all and update cvar_init */
  if (c->loaded_from_file == FALSE && c->var) {
    for(d = 0; d < c->veclen; d++) {
      c->all.mfcc_var[d] = (c->all.mfcc_var[d] * c->all.framenum + c->now.mfcc_var[d]) / (c->all.framenum + c->now.framenum);
    }
    c->all.framenum += c->now.framenum;
    for(d=0;d<c->veclen;d++) c->cvar_init[d] = c->all.mfcc_var[d];
  }

  /* expand clist if neccessary */
  if (c->clist_num == c->clist_max && frames < CPMAX) {
    c->clist_max += CPSTEP;
    c->clist = (CMEAN *)myrealloc(c->clist, sizeof(CMEAN) * c->clist_max);
    for(i=c->clist_num;i<c->clist_max;i++) {
      c->clist[i].mfcc_sum = (float *)mymalloc(sizeof(float)*c->veclen);
      c->clist[i].framenum = 0;
    }
  }
  
  /* shift clist */
  tmp = c->clist[c->clist_max-1].mfcc_sum;
  if (c->var) tmp2 = c->clist[c->clist_max-1].mfcc_var;
  memmove(&(c->clist[1]), &(c->clist[0]), sizeof(CMEAN) * (c->clist_max - 1));
  c->clist[0].mfcc_sum = tmp;
  if (c->var) c->clist[0].mfcc_var = tmp2;
  /* copy now to clist[0] */
  memcpy(c->clist[0].mfcc_sum, c->now.mfcc_sum, sizeof(float) * c->veclen);
  c->clist[0].framenum = c->now.framenum;

  if (c->clist_num < c->clist_max) c->clist_num++;

}
Exemple #20
0
void    read_master(int fail_on_open_error)
{
    const char *myname = "read_master";
    char   *path;
    VSTRING *buf;
    ARGV   *argv;
    VSTREAM *fp;
    int     entry_count = 0;
    int     line_count = 0;

    /*
     * Sanity check.
     */
    if (master_table != 0)
	msg_panic("%s: master table is already initialized", myname);

    /*
     * Get the location of master.cf.
     */
    if (var_config_dir == 0)
	set_config_dir();
    path = concatenate(var_config_dir, "/", MASTER_CONF_FILE, (char *) 0);

    /*
     * We can't use the master daemon's master_ent routines in their current
     * form. They convert everything to internal form, and they skip disabled
     * services.
     * 
     * The postconf command needs to show default fields as "-", and needs to
     * know about all service names so that it can generate service-dependent
     * parameter names (transport-dependent etc.).
     */
#define MASTER_BLANKS	" \t\r\n"		/* XXX */

    /*
     * Initialize the in-memory master table.
     */
    master_table = (PC_MASTER_ENT *) mymalloc(sizeof(*master_table));

    /*
     * Skip blank lines and comment lines. Degrade gracefully if master.cf is
     * not available, and master.cf is not the primary target.
     */
    if ((fp = vstream_fopen(path, O_RDONLY, 0)) == 0) {
	if (fail_on_open_error)
	    msg_fatal("open %s: %m", path);
	msg_warn("open %s: %m", path);
    } else {
	buf = vstring_alloc(100);
	while (readlline(buf, fp, &line_count) != 0) {
	    master_table = (PC_MASTER_ENT *) myrealloc((char *) master_table,
				 (entry_count + 2) * sizeof(*master_table));
	    argv = argv_split(STR(buf), MASTER_BLANKS);
	    if (argv->argc < PC_MASTER_MIN_FIELDS)
		msg_fatal("file %s: line %d: bad field count",
			  path, line_count);
	    normalize_options(argv);
	    master_table[entry_count].name_space =
		concatenate(argv->argv[0], ".", argv->argv[1], (char *) 0);
	    master_table[entry_count].argv = argv;
	    master_table[entry_count].valid_names = 0;
	    master_table[entry_count].all_params = 0;
	    entry_count += 1;
	}
	vstream_fclose(fp);
	vstring_free(buf);
    }

    /*
     * Null-terminate the master table and clean up.
     */
    master_table[entry_count].argv = 0;
    myfree(path);
}
Exemple #21
0
int
main(int argc, char **argv)
{
	int	envc;			/* environment argument count */
	char	**envv;			/* environment argument list */
	FILE	*names;			/* name file pointer */
	int	oldnum;			/* number in old cross-ref */
	char	path[PATHLEN + 1];	/* file path */
	FILE	*oldrefs;	/* old cross-reference file */
	char	*s;
	int	c, i;
	pid_t	pid;

	/* save the command name for messages */
	argv0 = basename(argv[0]);

	/* get the current directory for build() and line-oriented P command */
	if (mygetwd(currentdir) == NULL) {
		(void) fprintf(stderr,
		    "cscope: warning: cannot get current directory name\n");
		(void) strcpy(currentdir, "<unknown>");
	}
	/* initialize any view path; (saves time since currendir is known) */
	vpinit(currentdir);
	dbvpndirs = vpndirs; /* number of directories in database view path */
	/* directories (including current) in database view path */
	dbvpdirs = vpdirs;

	/* the first source directory is the current directory */
	sourcedir(".");

	/* read the environment */
	editor = mygetenv("EDITOR", EDITOR);
	editor = mygetenv("VIEWER", editor);	/* use viewer if set */
	home = getenv("HOME");
	shell = mygetenv("SHELL", SHELL);
	tmpdir = mygetenv("TMPDIR", TMPDIR);
	/* increment nesting depth */
	cscopedepth = atoi(mygetenv("CSCOPEDEPTH", "0"));
	(void) sprintf(path, "CSCOPEDEPTH=%d", ++cscopedepth);
	(void) putenv(stralloc(path));
	if ((s = getenv("CSCOPEOPTIONS")) != NULL) {

		/* parse the environment option string */
		envc = 1;
		envv = mymalloc(sizeof (char *));
		s = strtok(stralloc(s), OPTSEPS);
		while (s != NULL) {
			envv = myrealloc(envv, ++envc * sizeof (char *));
			envv[envc - 1] = stralloc(s);
			s = strtok((char *)NULL, OPTSEPS);
		}
		/* set the environment options */
		options(envc, envv);
	}
	/* set the command line options */
	options(argc, argv);

	/* create the temporary file names */
	pid = getpid();
	(void) sprintf(temp1, "%s/cscope%d.1", tmpdir, (int)pid);
	(void) sprintf(temp2, "%s/cscope%d.2", tmpdir, (int)pid);

	/* if running in the foreground */
	if (signal(SIGINT, SIG_IGN) != SIG_IGN) {

		/* cleanup on the interrupt and quit signals */
		(void) signal(SIGINT, myexit);
		(void) signal(SIGQUIT, myexit);
	}

	/* cleanup on the hangup signal */
	(void) signal(SIGHUP, myexit);
	/* if the database path is relative and it can't be created */
	if (reffile[0] != '/' && access(".", WRITE) != 0) {

		/* if the database may not be up-to-date or can't be read */
		(void) sprintf(path, "%s/%s", home, reffile);
		if (isuptodate == NO || access(reffile, READ) != 0) {

			/* put it in the home directory */
			reffile = stralloc(path);
			(void) sprintf(path, "%s/%s", home, invname);
			invname = stralloc(path);
			(void) sprintf(path, "%s/%s", home, invpost);
			invpost = stralloc(path);
			(void) fprintf(stderr,
			    "cscope: symbol database will be %s\n", reffile);
		}
	}
	/* if the cross-reference is to be considered up-to-date */
	if (isuptodate == YES) {
		if ((oldrefs = vpfopen(reffile, "r")) == NULL) {
			cannotopen(reffile);
			exit(1);
		}
		/*
		 * get the crossref file version but skip the current
		 * directory
		 */
		if (fscanf(oldrefs, "cscope %d %*s", &fileversion) != 1) {
			(void) fprintf(stderr,
			    "cscope: cannot read file version from file %s\n",
			    reffile);
			exit(1);
		}
		if (fileversion >= 8) {

			/* override these command line options */
			compress = YES;
			invertedindex = NO;

			/* see if there are options in the database */
			for (;;) {
				/* no -q leaves multiple blanks */
				while ((c = getc(oldrefs)) == ' ') {
					;
				}
				if (c != '-') {
					(void) ungetc(c, oldrefs);
					break;
				}
				switch (c = getc(oldrefs)) {
				case 'c':	/* ASCII characters only */
					compress = NO;
					break;
				case 'q':	/* quick search */
					invertedindex = YES;
					(void) fscanf(oldrefs,
					    "%ld", &totalterms);
					break;
				case 'T':
					/* truncate symbols to 8 characters */
					dbtruncated = YES;
					truncatesyms = YES;
					break;
				}
			}
			initcompress();

			/* seek to the trailer */
			if (fscanf(oldrefs, "%ld", &traileroffset) != 1) {
				(void) fprintf(stderr,
				    "cscope: cannot read trailer offset from "
				    "file %s\n", reffile);
				exit(1);
			}
			if (fseek(oldrefs, traileroffset, 0) != 0) {
				(void) fprintf(stderr,
				    "cscope: cannot seek to trailer in "
				    "file %s\n", reffile);
				exit(1);
			}
		}
		/*
		 * read the view path for use in converting relative paths to
		 * full paths
		 *
		 * note: don't overwrite vp[n]dirs because this can cause
		 * the wrong database index files to be found in the viewpath
		 */
		if (fileversion >= 13) {
			if (fscanf(oldrefs, "%d", &dbvpndirs) != 1) {
				(void) fprintf(stderr,
				    "cscope: cannot read view path size from "
				    "file %s\n", reffile);
				exit(1);
			}
			if (dbvpndirs > 0) {
				dbvpdirs = mymalloc(
				    dbvpndirs * sizeof (char *));
				for (i = 0; i < dbvpndirs; ++i) {
					if (fscanf(oldrefs, "%s", path) != 1) {
						(void) fprintf(stderr,
						    "cscope: cannot read view "
						    "path from file %s\n",
						    reffile);
						exit(1);
					}
					dbvpdirs[i] = stralloc(path);
				}
			}
		}
		/* skip the source and include directory lists */
		skiplist(oldrefs);
		skiplist(oldrefs);

		/* get the number of source files */
		if (fscanf(oldrefs, "%d", &nsrcfiles) != 1) {
			(void) fprintf(stderr,
			    "cscope: cannot read source file size from "
			    "file %s\n", reffile);
			exit(1);
		}
		/* get the source file list */
		srcfiles = mymalloc(nsrcfiles * sizeof (char *));
		if (fileversion >= 9) {

			/* allocate the string space */
			if (fscanf(oldrefs, "%d", &oldnum) != 1) {
				(void) fprintf(stderr,
				    "cscope: cannot read string space size "
				    "from file %s\n", reffile);
				exit(1);
			}
			s = mymalloc(oldnum);
			(void) getc(oldrefs);	/* skip the newline */

			/* read the strings */
			if (fread(s, oldnum, 1, oldrefs) != 1) {
				(void) fprintf(stderr,
				    "cscope: cannot read source file names "
				    "from file %s\n", reffile);
				exit(1);
			}
			/* change newlines to nulls */
			for (i = 0; i < nsrcfiles; ++i) {
				srcfiles[i] = s;
				for (++s; *s != '\n'; ++s) {
					;
				}
				*s = '\0';
				++s;
			}
			/* if there is a file of source file names */
			if (namefile != NULL &&
			    (names = vpfopen(namefile, "r")) != NULL ||
			    (names = vpfopen(NAMEFILE, "r")) != NULL) {

				/* read any -p option from it */
				while (fscanf(names, "%s", path) == 1 &&
				    *path == '-') {
					i = path[1];
					s = path + 2;	/* for "-Ipath" */
					if (*s == '\0') {
						/* if "-I path" */
						(void) fscanf(names,
						    "%s", path);
						s = path;
					}
					switch (i) {
					case 'p':
						/* file path components */
						/* to display */
						if (*s < '0' || *s > '9') {
							(void) fprintf(stderr,
							    "cscope: -p option "
							    "in file %s: "
							    "missing or "
							    "invalid numeric "
							    "value\n",
							    namefile);
						}
						dispcomponents = atoi(s);
					}
				}
				(void) fclose(names);
			}
		} else {
			for (i = 0; i < nsrcfiles; ++i) {
				if (fscanf(oldrefs, "%s", path) != 1) {
					(void) fprintf(stderr,
					    "cscope: cannot read source file "
					    "name from file %s\n", reffile);
					exit(1);
				}
				srcfiles[i] = stralloc(path);
			}
		}
		(void) fclose(oldrefs);
	} else {
		/* get source directories from the environment */
		if ((s = getenv("SOURCEDIRS")) != NULL) {
			sourcedir(s);
		}
		/* make the source file list */
		srcfiles = mymalloc(msrcfiles * sizeof (char *));
		makefilelist();
		if (nsrcfiles == 0) {
			(void) fprintf(stderr,
			    "cscope: no source files found\n");
			printusage();
			exit(1);
		}
		/* get include directories from the environment */
		if ((s = getenv("INCLUDEDIRS")) != NULL) {
			includedir(s);
		}
		/* add /usr/include to the #include directory list */
		includedir("/usr/include");

		/* initialize the C keyword table */
		initsymtab();

		/* create the file name(s) used for a new cross-reference */
		(void) strcpy(path, reffile);
		s = basename(path);
		*s = '\0';
		(void) strcat(path, "n");
		++s;
		(void) strcpy(s, basename(reffile));
		newreffile = stralloc(path);
		(void) strcpy(s, basename(invname));
		newinvname = stralloc(path);
		(void) strcpy(s, basename(invpost));
		newinvpost = stralloc(path);

		/* build the cross-reference */
		initcompress();
		build();
		if (buildonly == YES) {
			exit(0);
		}
	}
	opendatabase();

	/*
	 * removing a database will not release the disk space if a cscope
	 * process has the file open, so a project may want unattended cscope
	 * processes to exit overnight, including their subshells and editors
	 */
	if (noacttime) {
		(void) signal(SIGALRM, timedout);
		(void) alarm(noacttime);
	}
	/*
	 * if using the line oriented user interface so cscope can be a
	 * subprocess to emacs or samuel
	 */
	if (linemode == YES) {
		if (*pattern != '\0') {		/* do any optional search */
			if (search() == YES) {
				while ((c = getc(refsfound)) != EOF) {
					(void) putchar(c);
				}
			}
		}
		if (onesearch == YES) {
			myexit(0);
		}
		for (;;) {
			char buf[PATLEN + 2];
			if (noacttime) {
				(void) alarm(noacttime);
			}
			(void) printf(">> ");
			(void) fflush(stdout);
			if (fgets(buf, sizeof (buf), stdin) == NULL) {
				myexit(0);
			}
			/* remove any trailing newline character */
			if (*(s = buf + strlen(buf) - 1) == '\n') {
				*s = '\0';
			}
			switch (*buf) {
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':	/* samuel only */
				field = *buf - '0';
				(void) strcpy(pattern, buf + 1);
				(void) search();
				(void) printf("cscope: %d lines\n", totallines);
				while ((c = getc(refsfound)) != EOF) {
					(void) putchar(c);
				}
				break;

			case 'c':	/* toggle caseless mode */
			case ctrl('C'):
				if (caseless == NO) {
					caseless = YES;
				} else {
					caseless = NO;
				}
				egrepcaseless(caseless);
				break;

			case 'r':	/* rebuild database cscope style */
			case ctrl('R'):
				freefilelist();
				makefilelist();
				/* FALLTHROUGH */

			case 'R':	/* rebuild database samuel style */
				rebuild();
				(void) putchar('\n');
				break;

			case 'C':	/* clear file names */
				freefilelist();
				(void) putchar('\n');
				break;

			case 'F':	/* add a file name */
				(void) strcpy(path, buf + 1);
				if (infilelist(path) == NO &&
				    vpaccess(path, READ) == 0) {
					addsrcfile(path);
				}
				(void) putchar('\n');
				break;

			case 'P':	/* print the path to the files */
				if (prependpath != NULL) {
					(void) puts(prependpath);
				} else {
					(void) puts(currentdir);
				}
				break;

			case 'q':	/* quit */
			case ctrl('D'):
			case ctrl('Z'):
				myexit(0);

			default:
				(void) fprintf(stderr,
				    "cscope: unknown command '%s'\n", buf);
				break;
			}
		}
		/* NOTREACHED */
	}
	/* pause before clearing the screen if there have been error messages */
	if (errorsfound == YES) {
		errorsfound = NO;
		askforreturn();
	}
	(void) signal(SIGINT, SIG_IGN);	/* ignore interrupts */
	(void) signal(SIGPIPE, SIG_IGN); /* | command can cause pipe signal */
	/* initialize the curses display package */
	(void) initscr();	/* initialize the screen */
	setfield();	/* set the initial cursor position */
	entercurses();
	(void) keypad(stdscr, TRUE);	/* enable the keypad */
	dispinit();	/* initialize display parameters */
	putmsg("");	/* clear any build progress message */
	display();	/* display the version number and input fields */

	/* do any optional search */
	if (*pattern != '\0') {
		atfield();		/* move to the input field */
		(void) command(ctrl('A'));	/* search */
		display();		/* update the display */
	} else if (reflines != NULL) {
		/* read any symbol reference lines file */
		(void) readrefs(reflines);
		display();		/* update the display */
	}
	for (;;) {
		if (noacttime) {
			(void) alarm(noacttime);
		}
		atfield();	/* move to the input field */

		/* exit if the quit command is entered */
		if ((c = mygetch()) == EOF || c == ctrl('D') ||
		    c == ctrl('Z')) {
			break;
		}
		/* execute the commmand, updating the display if necessary */
		if (command(c) == YES) {
			display();
		}
	}
	/* cleanup and exit */
	myexit(0);
	/* NOTREACHED */
	return (0);
}
Exemple #22
0
static void
build(void)
{
	int	i;
	FILE	*oldrefs;	/* old cross-reference file */
	time_t	reftime;	/* old crossref modification time */
	char	*file;			/* current file */
	char	*oldfile;		/* file in old cross-reference */
	char	newdir[PATHLEN + 1];	/* directory in new cross-reference */
	char	olddir[PATHLEN + 1];	/* directory in old cross-reference */
	char	oldname[PATHLEN + 1];	/* name in old cross-reference */
	int	oldnum;			/* number in old cross-ref */
	struct	stat statstruct;	/* file status */
	int	firstfile;		/* first source file in pass */
	int	lastfile;		/* last source file in pass */
	int	built = 0;		/* built crossref for these files */
	int	copied = 0;		/* copied crossref for these files */
	BOOL	interactive = YES;	/* output progress messages */

	/*
	 * normalize the current directory relative to the home directory so
	 * the cross-reference is not rebuilt when the user's login is moved
	 */
	(void) strcpy(newdir, currentdir);
	if (strcmp(currentdir, home) == 0) {
		(void) strcpy(newdir, "$HOME");
	} else if (strncmp(currentdir, home, strlen(home)) == 0) {
		(void) sprintf(newdir, "$HOME%s", currentdir + strlen(home));
	}
	/* sort the source file names (needed for rebuilding) */
	qsort((char *)srcfiles, (unsigned)nsrcfiles, sizeof (char *), compare);

	/*
	 * if there is an old cross-reference and its current directory
	 * matches or this is an unconditional build
	 */
	if ((oldrefs = vpfopen(reffile, "r")) != NULL && unconditional == NO &&
	    fscanf(oldrefs, "cscope %d %s", &fileversion, olddir) == 2 &&
	    (strcmp(olddir, currentdir) == 0 || /* remain compatible */
	    strcmp(olddir, newdir) == 0)) {

		/* get the cross-reference file's modification time */
		(void) fstat(fileno(oldrefs), &statstruct);
		reftime = statstruct.st_mtime;
		if (fileversion >= 8) {
			BOOL	oldcompress = YES;
			BOOL	oldinvertedindex = NO;
			BOOL	oldtruncatesyms = NO;
			int	c;

			/* see if there are options in the database */
			for (;;) {
				while ((c = getc(oldrefs)) == ' ') {
				}
				if (c != '-') {
					(void) ungetc(c, oldrefs);
					break;
				}
				switch (c = getc(oldrefs)) {
				case 'c':	/* ASCII characters only */
					oldcompress = NO;
					break;
				case 'q':	/* quick search */
					oldinvertedindex = YES;
					(void) fscanf(oldrefs,
					    "%ld", &totalterms);
					break;
				case 'T':
					/* truncate symbols to 8 characters */
					oldtruncatesyms = YES;
					break;
				}
			}
			/* check the old and new option settings */
			if (oldcompress != compress ||
			    oldtruncatesyms != truncatesyms) {
				(void) fprintf(stderr,
				    "cscope: -c or -T option mismatch between "
				    "command line and old symbol database\n");
				goto force;
			}
			if (oldinvertedindex != invertedindex) {
				(void) fprintf(stderr,
				    "cscope: -q option mismatch between "
				    "command line and old symbol database\n");
				if (invertedindex == NO) {
					removeindex();
				}
				goto outofdate;
			}
			/* seek to the trailer */
			if (fscanf(oldrefs, "%ld", &traileroffset) != 1 ||
			    fseek(oldrefs, traileroffset, 0) == -1) {
				(void) fprintf(stderr,
				    "cscope: incorrect symbol database file "
				    "format\n");
				goto force;
			}
		}
		/* if assuming that some files have changed */
		if (fileschanged == YES) {
			goto outofdate;
		}
		/* see if the view path is the same */
		if (fileversion >= 13 &&
		    samelist(oldrefs, vpdirs, vpndirs) == NO) {
			goto outofdate;
		}
		/* see if the directory lists are the same */
		if (samelist(oldrefs, srcdirs, nsrcdirs) == NO ||
		    samelist(oldrefs, incdirs, nincdirs) == NO ||
		    fscanf(oldrefs, "%d", &oldnum) != 1 ||
		    fileversion >= 9 && fscanf(oldrefs, "%*s") != 0) {
			/* skip the string space size */
			goto outofdate;
		}
		/*
		 * see if the list of source files is the same and
		 * none have been changed up to the included files
		 */
		for (i = 0; i < nsrcfiles; ++i) {
			if (fscanf(oldrefs, "%s", oldname) != 1 ||
			    strnotequal(oldname, srcfiles[i]) ||
			    vpstat(srcfiles[i], &statstruct) != 0 ||
			    statstruct.st_mtime > reftime) {
				goto outofdate;
			}
		}
		/* the old cross-reference is up-to-date */
		/* so get the list of included files */
		while (i++ < oldnum && fscanf(oldrefs, "%s", oldname) == 1) {
			addsrcfile(oldname);
		}
		(void) fclose(oldrefs);
		return;

outofdate:
		/* if the database format has changed, rebuild it all */
		if (fileversion != FILEVERSION) {
			(void) fprintf(stderr,
			    "cscope: converting to new symbol database file "
			    "format\n");
			goto force;
		}
		/* reopen the old cross-reference file for fast scanning */
		if ((symrefs = vpopen(reffile, O_RDONLY)) == -1) {
			cannotopen(reffile);
			myexit(1);
		}
		/* get the first file name in the old cross-reference */
		blocknumber = -1;
		(void) readblock();	/* read the first cross-ref block */
		(void) scanpast('\t');	/* skip the header */
		oldfile = getoldfile();
	} else {	/* force cross-referencing of all the source files */
force:
		reftime = 0;
		oldfile = NULL;
	}
	/* open the new cross-reference file */
	if ((newrefs = fopen(newreffile, "w")) == NULL) {
		cannotopen(newreffile);
		myexit(1);
	}
	if (invertedindex == YES && (postings = fopen(temp1, "w")) == NULL) {
		cannotopen(temp1);
		cannotindex();
	}
	(void) fprintf(stderr, "cscope: building symbol database\n");
	putheader(newdir);
	fileversion = FILEVERSION;
	if (buildonly == YES && !isatty(0)) {
		interactive = NO;
	} else {
		initprogress();
	}
	/* output the leading tab expected by crossref() */
	dbputc('\t');

	/*
	 * make passes through the source file list until the last level of
	 * included files is processed
	 */
	firstfile = 0;
	lastfile = nsrcfiles;
	if (invertedindex == YES) {
		srcoffset = mymalloc((nsrcfiles + 1) * sizeof (long));
	}
	for (;;) {

		/* get the next source file name */
		for (fileindex = firstfile; fileindex < lastfile; ++fileindex) {
			/* display the progress about every three seconds */
			if (interactive == YES && fileindex % 10 == 0) {
				if (copied == 0) {
					progress("%ld files built",
					    (long)built, 0L);
				} else {
					progress("%ld files built, %ld "
					    "files copied", (long)built,
					    (long)copied);
				}
			}
			/* if the old file has been deleted get the next one */
			file = srcfiles[fileindex];
			while (oldfile != NULL && strcmp(file, oldfile) > 0) {
				oldfile = getoldfile();
			}
			/*
			 * if there isn't an old database or this is
			 * a new file
			 */
			if (oldfile == NULL || strcmp(file, oldfile) < 0) {
				crossref(file);
				++built;
			} else if (vpstat(file, &statstruct) == 0 &&
			    statstruct.st_mtime > reftime) {
				/* if this file was modified */
				crossref(file);
				++built;

				/*
				 * skip its old crossref so modifying the last
				 * source file does not cause all included files
				 * to be built.  Unfortunately a new file that
				 * is alphabetically last will cause all
				 * included files to be built, but this is
				 * less likely
				 */
				oldfile = getoldfile();
			} else {	/* copy its cross-reference */
				putfilename(file);
				if (invertedindex == YES) {
					copyinverted();
				} else {
					copydata();
				}
				++copied;
				oldfile = getoldfile();
			}
		}
		/* see if any included files were found */
		if (lastfile == nsrcfiles) {
			break;
		}
		firstfile = lastfile;
		lastfile = nsrcfiles;
		if (invertedindex == YES) {
			srcoffset = myrealloc(srcoffset,
			    (nsrcfiles + 1) * sizeof (long));
		}
		/* sort the included file names */
		qsort((char *)&srcfiles[firstfile],
		    (unsigned)(lastfile - firstfile), sizeof (char *), compare);
	}
	/* add a null file name to the trailing tab */
	putfilename("");
	dbputc('\n');

	/* get the file trailer offset */

	traileroffset = dboffset;

	/*
	 * output the view path and source and include directory and
	 * file lists
	 */
	putlist(vpdirs, vpndirs);
	putlist(srcdirs, nsrcdirs);
	putlist(incdirs, nincdirs);
	putlist(srcfiles, nsrcfiles);
	if (fflush(newrefs) == EOF) {
		/* rewind doesn't check for write failure */
		cannotwrite(newreffile);
		/* NOTREACHED */
	}
	/* create the inverted index if requested */
	if (invertedindex == YES) {
		char	sortcommand[PATHLEN + 1];

		if (fflush(postings) == EOF) {
			cannotwrite(temp1);
			/* NOTREACHED */
		}
		(void) fstat(fileno(postings), &statstruct);
		(void) fprintf(stderr,
		    "cscope: building symbol index: temporary file size is "
		    "%ld bytes\n", statstruct.st_size);
		(void) fclose(postings);
	/*
	 * sort -T is broken until it is fixed we don't have too much choice
	 */
	/*
	 * (void) sprintf(sortcommand, "sort -y -T %s %s", tmpdir, temp1);
	 */
	(void) sprintf(sortcommand, "LC_ALL=C sort %s", temp1);
		if ((postings = popen(sortcommand, "r")) == NULL) {
			(void) fprintf(stderr,
			    "cscope: cannot open pipe to sort command\n");
			cannotindex();
		} else {
			if ((totalterms = invmake(newinvname, newinvpost,
			    postings)) > 0) {
				movefile(newinvname, invname);
				movefile(newinvpost, invpost);
			} else {
				cannotindex();
			}
			(void) pclose(postings);
		}
		(void) unlink(temp1);
		(void) free(srcoffset);
		(void) fprintf(stderr,
		    "cscope: index has %ld references to %ld symbols\n",
		    npostings, totalterms);
	}
	/* rewrite the header with the trailer offset and final option list */
	rewind(newrefs);
	putheader(newdir);
	(void) fclose(newrefs);

	/* close the old database file */
	if (symrefs >= 0) {
		(void) close(symrefs);
	}
	if (oldrefs != NULL) {
		(void) fclose(oldrefs);
	}
	/* replace it with the new database file */
	movefile(newreffile, reffile);
}
Exemple #23
0
void fill_beagle_instances(world_fmt *world)
{
  beagle_fmt *beagle = world->beagle;
  unsigned long i;
  unsigned long ii;
  unsigned long j;
  //unsigned long zz;
  unsigned long site;
  unsigned int numsites;
  unsigned int numstates;
  unsigned int numweights=0;
  int code;
  long locus = world->locus;
  // set partials for all tipnodes
  // use z to advance through the partial array
  unsigned long z = 0L;

  for(i=0; i<world->nummutationmodels[locus]; i++)
    {
      ii = world->sublocistarts[locus] + i;
      numstates = world->mutationmodels[ii].numstates;
      numsites = world->mutationmodels[ii].numsites;
      // will currently fail with more complex data
      if(numsites + numweights < beagle->numallocallyweights)
	{
	  beagle->numallocallyweights += numsites * 2;
	  beagle->allyweights = (int *) myrealloc(beagle->allyweights,sizeof(int) * beagle->numallocallyweights);
	}
      memcpy(beagle->allyweights+numweights, world->data->seq[0]->aliasweight, sizeof(int) * numsites);
      numweights += numsites;
      for (j = 0; j < world->sumtips; ++j)
	{
	  for(site=0;site<numsites;site++)
	    {
	      if(z > beagle->numallocpartials)
		{
		  beagle->numallocpartials += numsites * numstates;
		  beagle->partials = (double *) myrealloc(beagle->partials,sizeof(double) * beagle->numallocpartials);
		}
	      memcpy(&beagle->partials[z], 
		     &(world->nodep[j]->x.s[site][0][0]),
		     sizeof(double) * numstates);
	      beagle->numpartials++;
#if 0
	      fprintf(stdout,"Site %li Partial: %li (%f ", site, j, 
		      beagle->partials[z]);
	      for(zz=1;zz<world->mutationmodels[ii].numstates;zz++)
		{
		  fprintf(stdout,"%f ",beagle->partials[z+zz]);
		}
	      fprintf(stdout,")\n")
#endif;
	      // advance z
	      z += numstates;
	    }
	  code = beagleSetTipPartials(
			     beagle->instance_handle[i],			// instance
			     j,							// indicator for tips
			     &beagle->partials[z - numsites*numstates]);// inPartials
	  if (code != 0)
	    usererror("setTipPartials encountered a problem");
	}
    }

  
  for(i=0; i<world->nummutationmodels[locus]; i++)
    {
      ii = world->sublocistarts[locus] + i;

      code = beagleSetCategoryRates(beagle->instance_handle[i], world->mutationmodels[ii].siterates);
      if (code != 0)
	usererror("setCategoryRates encountered a problem");

      code = beagleSetEigenDecomposition(beagle->instance_handle[i],		  // instance
				   0,					  // eigenIndex,
				   (const double *)world->mutationmodels[ii].eigenvectormatrix,	// inEigenVectors,
				   (const double *)world->mutationmodels[ii].inverseeigenvectormatrix,// inInverseEigenVectors,
				   world->mutationmodels[i].eigenvalues); // inEigenValues
      
      if (code != 0)
	usererror("setEigenDecomposition encountered a problem");
    }
}
Exemple #24
0
int match_files(char *search_for, char **path, char ***found, char **isdir)
{
	DIR *dir;
	struct dirent *entry;
	char *cur_dir = mymalloc(find_path_max() + 1);
	char *fname;
	char **list = NULL;
	int nfound = 0;
	size_t fname_size;
	int path_len;
	int s1, s2;
	char *slash = strrchr(search_for, '/');
	if (slash)
	{
		fname = mystrdup(slash + 1);
		*(slash + 1) = 0x00;
		*path = mystrdup(search_for);
	}
	else
	{
		*path = mystrdup("./");
		fname = mystrdup(search_for);
	}
	fname_size = strlen(fname);
	path_len = strlen(*path);

	dir = opendir(*path);
	if (!dir)
	{
		free(cur_dir);
		return 0;
	}

	memcpy(cur_dir, *path, path_len + 1);

	while((entry = readdir(dir)) != NULL)
	{
		if ((fname_size == 0 || strncmp(entry -> d_name, fname, fname_size) == 0) && strcmp(entry -> d_name, ".") != 0 &&
				strcmp(entry -> d_name, "..") != 0)
		{
			struct stat finfo;

			/* get filename */
			list = (char **)myrealloc(list, (nfound + 1) * sizeof(char *));
			list[nfound] = mystrdup(entry -> d_name);

			/* check if the file is a directory */
			*isdir = (char *)myrealloc(*isdir, (nfound + 1) * sizeof(char));
			strncpy(&cur_dir[path_len], entry -> d_name, max(0,find_path_max() - path_len));
			if (stat(cur_dir, &finfo) == -1)
			{
				if (errno != ENOENT)	/* file did not disappear? then something is very wrong */
					error_exit(TRUE, FALSE, "Error while invoking stat() %s.\n", cur_dir);
			}
			(*isdir)[nfound] = S_ISDIR(finfo.st_mode)?1:0;

			nfound++;
		}
	}

	if (closedir(dir) == -1)
		error_exit(TRUE, FALSE, "closedir() failed\n");

	/*	qsort( (void *)list, (size_t)nfound, sizeof(char *), compare_filenames); */
	for(s1=0; s1<(nfound - 1); s1++)
	{
		for(s2=s1+1; s2<nfound; s2++)
		{
			if (strcasecmp(list[s2], list[s1]) < 0)
			{
				char *fdummy = list[s1], ddummy = (*isdir)[s1];

				list[s1] = list[s2];
				(*isdir)[s1] = (*isdir)[s2];
				list[s2] = fdummy;
				(*isdir)[s2] = ddummy;
			}
		}
	}

	*found = list;

	myfree(fname);
	myfree(cur_dir);

	return nfound;
}
Exemple #25
0
struct queueInfoEnt *
lsb_queueinfo (char **queues,
               int *numQueues,
               char *hosts,
               char *users,
               int options)
{
    mbdReqType mbdReqtype;
    static struct infoReq queueInfoReq;
    static struct queueInfoReply reply;
    static struct queueInfoEnt **qInfo = NULL;
    struct queueInfoEnt **qTmp;
    XDR xdrs;
    XDR xdrs2;
    char *request_buf;
    char *reply_buf;
    int cc;
    int i;
    static struct LSFHeader hdr;
    char *clusterName = NULL;

    if (qInfo != NULL) {
        for (i = 0; i < reply.numQueues; i++) {
            xdr_lsffree(xdr_queueInfoEnt,
                        (char*)qInfo[i],
                        &hdr);
        }
    }

    if (numQueues == NULL) {
        lsberrno = LSBE_BAD_ARG;
        return NULL;
    }
    if ((queues == NULL && *numQueues > 1) || (*numQueues < 0)) {
        lsberrno = LSBE_BAD_ARG;
        return NULL;
    }

    queueInfoReq.options = 0;

    if (queueInfoReq.names) {
        FREEUP (queueInfoReq.names);
    }

    if (numQueues == NULL || *numQueues == 0)
        queueInfoReq.options |= ALL_QUEUE;
    else if (queues == NULL && *numQueues == 1)
        queueInfoReq.options |= DFT_QUEUE;

    if ((queueInfoReq.options & ALL_QUEUE)
        || (queueInfoReq.options & DFT_QUEUE)) {
        if ((queueInfoReq.names = malloc(3 * sizeof(char *))) == NULL) {
            lsberrno = LSBE_NO_MEM;
            return NULL;
        }
        queueInfoReq.names[0] = "";
        queueInfoReq.numNames = 1;
        cc = 1;
    } else {

        if ((queueInfoReq.names = calloc(*numQueues + 2,
                                         sizeof(char*))) == NULL) {
            lsberrno = LSBE_NO_MEM;
            return NULL;
        }

        queueInfoReq.numNames = *numQueues;
        for (i = 0; i < *numQueues; i++) {

            if (queues[i] && strlen(queues[i]) + 1 < MAXHOSTNAMELEN) {
                queueInfoReq.names[i] = queues[i];
            } else {
                free (queueInfoReq.names);
                queueInfoReq.names = NULL;
                lsberrno = LSBE_BAD_QUEUE;
                *numQueues = i;
                return NULL;
            }
        }
        cc = queueInfoReq.numNames;
    }

    if (users != NULL) {
        if (strlen(users) + 1 < MAX_LSB_NAME_LEN) {
            queueInfoReq.options |= CHECK_USER;
            queueInfoReq.names[cc] = users;
            cc++;
        } else {
            lsberrno = LSBE_BAD_USER;
            *numQueues = 0;
            return NULL;
        }
    }

    if (hosts != NULL) {
        if (ls_isclustername(hosts) <= 0) {
            if (strlen (hosts) + 1 < MAXHOSTNAMELEN) {
                queueInfoReq.options |= CHECK_HOST;
                queueInfoReq.names[cc] = hosts;
                cc++;
            } else {
                lsberrno = LSBE_BAD_HOST;
                *numQueues = 0;
                return NULL;
            }
        } else
            clusterName = hosts;
    }

    queueInfoReq.resReq = "";
    mbdReqtype = BATCH_QUE_INFO;

    cc = sizeof(struct infoReq) + cc * MAXHOSTNAMELEN + cc + 128;
    if ((request_buf = malloc (cc)) == NULL) {
        lsberrno = LSBE_NO_MEM;
        return NULL;
    }

    xdrmem_create(&xdrs, request_buf, MSGSIZE, XDR_ENCODE);
    initLSFHeader_(&hdr);
    hdr.opCode = mbdReqtype;

    if (!xdr_encodeMsg(&xdrs,
                       (char *)&queueInfoReq,
                       &hdr,
                       xdr_infoReq,
                       0,
                       NULL)) {
        lsberrno = LSBE_XDR;
        xdr_destroy(&xdrs);
        free (request_buf);
        return NULL;
    }

    if ((cc = callmbd(clusterName,
                      request_buf,
                      XDR_GETPOS(&xdrs),
                      &reply_buf,
                      &hdr,
                      NULL,
                      NULL,
                      NULL)) == -1) {
        xdr_destroy(&xdrs);
        free (request_buf);
        return NULL;
    }

    xdr_destroy(&xdrs);
    free (request_buf);

    lsberrno = hdr.opCode;
    if (lsberrno == LSBE_NO_ERROR
        || lsberrno == LSBE_BAD_QUEUE) {

        xdrmem_create(&xdrs2, reply_buf, XDR_DECODE_SIZE_(cc), XDR_DECODE);

        if (!xdr_queueInfoReply(&xdrs2, &reply, &hdr)) {
            lsberrno = LSBE_XDR;
            xdr_destroy(&xdrs2);
            if (cc)
                free(reply_buf);
            *numQueues = 0;
            return NULL;
        }

        xdr_destroy(&xdrs2);

        if (cc)
            free(reply_buf);

        if (lsberrno == LSBE_BAD_QUEUE) {
            *numQueues = reply.badQueue;
            return NULL;
        }

        if ((qTmp = myrealloc(qInfo,
                              reply.numQueues
                              * sizeof(struct queueInfoEnt *))) == NULL) {
            lsberrno = LSBE_NO_MEM;
            return NULL;
        }

        qInfo = qTmp;
        for (i = 0; i < reply.numQueues; i++)
            qInfo[i] = &(reply.queues[i]);

        *numQueues = reply.numQueues;
        return qInfo[0];
    }

    if (cc)
        free(reply_buf);

    *numQueues = 0;

    return NULL;
}
Exemple #26
0
void include_source(char *inname)
{
    char *filename;
    struct include_path **nptr = &first_source;
    struct include_path *name;
    FILE *f;

    filename = convert_path(inname);

    /* check whether this source was already included */
    while (name = *nptr) {
#if defined(AMIGA) || defined(MSDOS) || defined(_WIN32)
        if (!stricmp(name->path,filename)) {
#else
        if (!strcmp(name->path,filename)) {
#endif
            myfree(filename);
            if (!ignore_multinc) {
                filename = name->path;
                /* reuse already read file from cache? */
            }
            nptr = NULL;  /* ignore including this source */
            break;
        }
        nptr = &name->next;
    }
    if (nptr) {
        name = mymalloc(sizeof(struct include_path));
        name->next = NULL;
        name->path = filename;
        *nptr = name;
    }
    else if (ignore_multinc)
        return;  /* ignore multiple inclusion of this source completely */

    if (f = locate_file(filename,"r")) {
        char *text;
        size_t size;

        for (text=NULL,size=0; ; size+=SRCREADINC) {
            size_t nchar;
            text = myrealloc(text,size+SRCREADINC);
            nchar = fread(text+size,1,SRCREADINC,f);
            if (nchar < SRCREADINC) {
                size += nchar;
                break;
            }
        }
        if (feof(f)) {
            if (size > 0) {
                cur_src = new_source(filename,myrealloc(text,size+1),size+1);
                *(cur_src->text+size) = '\n';
            }
            else {
                myfree(text);
                cur_src = new_source(filename,"\n",1);
            }
        }
        else
            general_error(29,filename);
        fclose(f);
    }
}

/* searches a section by name and attr (if secname_attr set) */
section *find_section(char *name,char *attr)
{
    section *p;
    if(secname_attr) {
        for(p=first_section; p; p=p->next) {
            if(!strcmp(name,p->name) && !strcmp(attr,p->attr))
                return p;
        }
    }
    else {
        for(p=first_section; p; p=p->next) {
            if(!strcmp(name,p->name))
                return p;
        }
    }
    return 0;
}
Exemple #27
0
static void
handle_options(int argc, char *argv[], Font *fnt)
{
  register int lastext;
  register int i;
  size_t l;
  int arginc;
  char *temp;
  char c;
  char *vpl_name = NULL;
  Boolean have_capheight = 0;
  Boolean have_sfd = 0;
  int sfd_begin, postfix_begin;
  int base_name;
  stringlist* sl;


  /* scan first whether the -q switch is set */
  for (i = 1; i < argc; i++)
    if (argv[i][0] == '-' && argv[i][1] == 'q')
      quiet = True;

  if (!quiet)
    printf("This is %s\n", ident);

  /* Make VPL file identical to that created under Unix */
  fnt->titlebuf = (char *)mymalloc(strlen(progname) + strlen(argv[1]) +
                                   1 + 1);
  sprintf(fnt->titlebuf, "%s %s", progname, argv[1]);


  /*
   *   TrueType font name.
   */

  fnt->ttfname = newstring(argv[1]);

  /*
   *   The other arguments.  We delay the final processing of some switches
   *   until the tfm font name has been scanned -- if it contains two `@'s,
   *   many switches are ignored.
   */

  while (argc > 2 && *argv[2] == '-')
  {
    arginc = 2;
    i = argv[2][1];

    switch (i)
    {
    case 'v':
      makevpl = 1;
      if (argc <= 3)
        oops("Missing parameter for -v option.");
      if (vpl_name)
        free(vpl_name);
      vpl_name = newstring(argv[3]);
      handle_extension(&vpl_name, ".vpl");
      break;

    case 'V':
      makevpl = 2;
      if (argc <= 3)
        oops("Missing parameter for -V option.");
      if (vpl_name)
        free(vpl_name);
      vpl_name = newstring(argv[3]);
      handle_extension(&vpl_name, ".vpl");
      break;

    case 'f':
      if (argc <= 3)
        oops("Missing parameter for -f option.");
      if (sscanf(argv[3], "%lu", &(fnt->fontindex)) == 0)
        oops("Invalid font index.");
      fnt->fontindexparam = argv[3];
      break;

    case 'E':
      if (argc <= 3)
        oops("Missing parameter for -E option.");
      if (sscanf(argv[3], "%hu", &(fnt->eid)) == 0)
        oops("Invalid encoding ID.");
      fnt->eidparam = argv[3];
      break;

    case 'P':
      if (argc <= 3)
        oops("Missing parameter for -P option.");
      if (sscanf(argv[3], "%hu", &(fnt->pid)) == 0)
        oops("Invalid platform ID.");
      fnt->pidparam = argv[3];
      break;

    case 'e':
      if (argc <= 3)
        oops("Missing parameter for -e option.");
      if (sscanf(argv[3], "%f", &(fnt->efactor)) == 0 || fnt->efactor < 0.01)
        oops("Bad extension factor.");
      fnt->efactorparam = argv[3];
      break;

    case 'c':
      if (argc <= 3)
        oops("Missing parameter for -c option.");
      have_capheight = True;
      if (sscanf(argv[3], "%f", &(fnt->capheight)) == 0)
        fnt->capheight = 0;
      break;

    case 's':
      if (argc <= 3)
        oops("Missing parameter for -s option.");
      if (sscanf(argv[3], "%f", &(fnt->slant)) == 0)
        oops("Bad slant parameter.");
      fnt->slantparam = argv[3];
      break;

    case 'p':
      if (argc <= 3)
        oops("Missing parameter for -p option.");
      if (fnt->inencname)
        free(fnt->inencname);
      fnt->inencname = newstring(argv[3]);
      break;

    case 'T':
      if (argc <= 3)
        oops("Missing parameter for -T option.");
      if (fnt->inencname)
        free(fnt->inencname);
      if (fnt->outencname)
        free(fnt->outencname);
      fnt->inencname = newstring(argv[3]);
      fnt->outencname = newstring(argv[3]);
      break;

    case 't':
      if (argc <= 3)
        oops("Missing parameter for -T option.");
      if (fnt->outencname)
        free(fnt->outencname);
      fnt->outencname = newstring(argv[3]);
      break;

    case 'r':
      if (argc <= 4)
        oops("Not enough parameters for -r option.");
      sl = newstringlist();
      sl->old_name = newstring(argv[3]);
      sl->new_name = newstring(argv[4]);
      sl->single_replacement = True;
      sl->next = fnt->replacements;
      fnt->replacements = sl;
      arginc = 3;
      break;

    case 'R':
      if (argc <= 3)
        oops("Missing parameter for -R option.");
      if (fnt->replacementname)
        free(fnt->replacementname);
      fnt->replacementname = newstring(argv[3]);
      break;

    case 'y':
      if (argc <= 3)
        oops("Missing parameter for -y option.");
      if (sscanf(argv[3], "%f", &(fnt->y_offset)) == 0)
        oops("Invalid y-offset.");
      fnt->y_offsetparam = argv[3];
      break;

    case 'O':
      forceoctal = True;
      arginc = 1;
      break;

    case 'n':
      fnt->PSnames = Yes;
      arginc = 1;
      break;

    case 'N':
      fnt->PSnames = Only;
      arginc = 1;
      break;

    case 'u':
      pedantic = True;
      arginc = 1;
      break;

    case 'q':
      quiet = True;
      arginc = 1;
      break;

    case 'L':
      if (argc <= 3)
        oops("Missing parameter for -L option.");
      if (fnt->ligname)
        free(fnt->ligname);
      fnt->ligname = newstring(argv[3]);
      fnt->subfont_ligs = True;
      break;

    case 'l':
      fnt->subfont_ligs = True;
      arginc = 1;
      break;

    case 'w':
      fnt->write_enc = True;
      arginc = 1;
      break;

    case 'x':
      fnt->rotate = True;
      arginc = 1;
      break;

    case 'o':
      if (argc <= 3)
        oops("Missing parameter for -o option.");
      if (vpl_name)
        free(vpl_name);
      vpl_name = newstring(argv[3]);
      handle_extension(&vpl_name, ".ovp");
      break;

    default:
      if (argc <= 3 || argv[3][0] == '-')
      {
        warning("Unknown option `%s' will be ignored.\n", argv[2]);
        arginc = 1;
      }
      else
        warning("Unknown option `%s %s' will be ignored.\n",
                argv[2], argv[3]);
    }

    for (i = 0; i < arginc; i++)
    {
      l = strlen(fnt->titlebuf);
      fnt->titlebuf = (char *)myrealloc((void *)fnt->titlebuf,
                                        l + strlen(argv[2]) + 1 + 1);
      sprintf(fnt->titlebuf + strlen(fnt->titlebuf), " %s", argv[2]);
      argv++;
      argc--;
    }
  }

  /* Read replacement glyph name file */

  get_replacements(fnt);

  if (argc > 3 || (argc == 3 && *argv[2] == '-'))
    oops("Need at most two non-option arguments.");

  /*
   *   The tfm file name.
   */

  if (argc == 2)
    temp = newstring(fnt->ttfname);
  else
  {
    temp = newstring(argv[2]);
    l = strlen(fnt->titlebuf);
    fnt->titlebuf = (char *)myrealloc((void *)fnt->titlebuf,
                                      l + strlen(argv[2]) + 1 + 1);
    sprintf(fnt->titlebuf + strlen(fnt->titlebuf), " %s", argv[2]);
  }

  handle_sfd(temp, &sfd_begin, &postfix_begin);

  if (sfd_begin > -1)
  {
    have_sfd = True;
    i = sfd_begin - 2;
  }
  else
    i = strlen(temp) - 1;

  /*
   *   Now we search the beginning of the name without directory.
   */

  for (; i >= 0; i--)
    if (temp[i] == '/' || temp[i] == ':' || temp[i] == '\\')
      break;

  base_name = i + 1;

  /*
   *   We store the path (with the final directory separator).
   */

  if (base_name > 0)
  {
    c = temp[base_name];
    temp[base_name] = '\0';
    fnt->tfm_path = newstring(temp);
    temp[base_name] = c;
  }

  if (have_sfd)
  {
    /* the prefix and the sfd file name */

    if (temp[base_name])
      fnt->outname = newstring(temp + base_name);

    fnt->sfdname = newstring(temp + sfd_begin);
  }
  else
    postfix_begin = base_name;

  /*
   *   Get the extension.
   */

  lastext = -1;
  for (i = postfix_begin; temp[i]; i++)
    if (temp[i] == '.')
      lastext = i;

  if (argc == 2 && lastext >= 0)
  {
    temp[lastext] = '\0';       /* remove TTF file extension */
    lastext = -1;
  }

  if (lastext == -1)
    fnt->tfm_ext = newstring(".tfm");
  else
  {
    fnt->tfm_ext = newstring(temp + lastext);
    temp[lastext] = '\0';
  }

  if (have_sfd)
  {
    if (temp[postfix_begin])
      fnt->outname_postfix = newstring(temp + postfix_begin);
  }
  else
  {
    if (temp[base_name])
      fnt->outname = newstring(temp + base_name);
    else
      oops("Invalid tfm file name.");
  }


  /*
   *   Now we can process the remaining parameters.
   */

  if (have_sfd)
  {
    if (makevpl)
    {
      warning("Ignoring `-v' and `-V' switches for subfonts.");
      makevpl = 0;
    }
    if (vpl_name)
      if ((fnt->vplout = fopen(vpl_name, "wb")) == NULL)
        oops("Cannot open ovp output file.");
    if (have_capheight)
      warning("Ignoring `-c' switch for subfonts.");
    if (fnt->inencname || fnt->outencname)
    {
      warning("Ignoring `-p', `-t', and `-T' switches for subfonts.");
      fnt->inencname = NULL;
      fnt->outencname = NULL;
    }
    if (fnt->y_offsetparam && !fnt->rotate)
      warning("Ignoring `-y' switch for non-rotated subfonts.");
    if (fnt->PSnames)
    {
      warning("Ignoring `-n' or '-N' switch for subfonts.");
      fnt->PSnames = No;
    }

    init_sfd(fnt, True);
  }
  else
  {
    if (have_capheight && fnt->capheight < 0.01)
      oops("Bad small caps height.");

    if (vpl_name)
      if ((fnt->vplout = fopen(vpl_name, "wb")) == NULL)
        oops("Cannot open vpl output file.");
  
    if (fnt->subfont_ligs)
    {
      warning("Ignoring `-l' switch for non-subfont.");
      fnt->subfont_ligs = False;
    }

    if (fnt->rotate)
    {
      warning("Ignoring `-x' switch for non-subfont.");
      fnt->rotate = False;
    }

    if (fnt->write_enc)
    {
      warning("Ignoring `-w' switch for non-subfont.");
      fnt->write_enc = False;
    }

    if (fnt->y_offsetparam)
      warning("Ignoring `-y' switch for non-subfont.");
  }

  if (fnt->PSnames == Only)
  {
    if (fnt->pidparam || fnt->eidparam)
    {
      warning("Ignoring `-P' and `-E' options if `-N' switch is selected.");
      fnt->pidparam = NULL;
      fnt->eidparam = NULL;
    }
  }

  if (vpl_name)
    free(vpl_name);
  free(temp);
}
/**
 * Sub function to Add a dictionary entry line to the word dictionary.
 *
 * @param buf [i/o] buffer to hold the input string, will be modified in this function
 * @param vnum_p [in] current number of words in @a winfo
 * @param linenum [in] current line number of the input
 * @param winfo [out] pointer to word dictionary to append the data.
 * @param hmminfo [in] HTK %HMM definition data.  if NULL, phonemes are ignored.
 * @param do_conv [in] TRUE if performing triphone conversion
 * @param ok_flag [out] will be set to FALSE if an error occured for this input.
 * @param headphone [in] word head silence model name
 * @param tailphone [in] word tail silence model name
 * @param contextphone [in] silence context name to be used at head and tail
 *
 * @return FALSE if buf == "DICEND", else TRUE will be returned.
 */
boolean
voca_load_wordlist_line(char *buf, WORD_ID *vnum_p, int linenum, WORD_INFO *winfo, HTK_HMM_INFO *hmminfo, boolean do_conv, boolean *ok_flag, char *headphone, char *tailphone, char *contextphone)
{
    char *ptmp, *lp = NULL, *p;
    static char cbuf[MAX_HMMNAME_LEN];
    static HMM_Logical **tmpwseq = NULL;
    static int tmpmaxlen;
    int len;
    HMM_Logical *tmplg;
    boolean pok, first;
    int vnum;

    vnum = *vnum_p;

    if (strmatch(buf, "DICEND")) return FALSE;

    /* allocate temporal work area for the first call */
    if (tmpwseq == NULL) {
        tmpmaxlen = PHONEMELEN_STEP;
        tmpwseq = (HMM_Logical **)mymalloc(sizeof(HMM_Logical *) * tmpmaxlen);
    }

    /* backup whole line for debug output */
    strcpy(bufbak, buf);

    /* Output string */
    if ((ptmp = mystrtok_quote(buf, " \t\n")) == NULL) {
        jlog("Error: voca_load_wordlist: line %d: corrupted data:\n> %s\n", linenum, bufbak);
        winfo->errnum++;
        *ok_flag = FALSE;
        return TRUE;
    }
    winfo->wname[vnum] = strcpy((char *)mybmalloc2(strlen(ptmp)+1, &(winfo->mroot)), ptmp);

    /* reset transparent flag */
    winfo->is_transparent[vnum] = FALSE;

    /* just move pointer to next token */
    if ((ptmp = mystrtok_movetonext(NULL, " \t\n")) == NULL) {
        jlog("Error: voca_load_wordlist: line %d: corrupted data:\n> %s\n", linenum, bufbak);
        winfo->errnum++;
        *ok_flag = FALSE;
        return TRUE;
    }
#ifdef CLASS_NGRAM
    winfo->cprob[vnum] = 0.0;	/* prob = 1.0, logprob = 0.0 */
#endif

    if (ptmp[0] == '@') {		/* class N-gram prob */
#ifdef CLASS_NGRAM
        /* word probability within the class (for class N-gram) */
        /* format: classname @classprob wordname [output] phoneseq */
        /* classname equals to wname, and wordname will be omitted */
        /* format: @%f (log scale) */
        /* if "@" not found or "@0", it means class == word */
        if ((ptmp = mystrtok(NULL, " \t\n")) == NULL) {
            jlog("Error: voca_load_wordlist: line %d: corrupted data:\n> %s\n", linenum, bufbak);
            winfo->errnum++;
            *ok_flag = FALSE;
            return TRUE;
        }
        if (ptmp[1] == '\0') {	/* space between '@' and figures */
            jlog("Error: voca_load_wordlist: line %d: value after '@' missing, maybe wrong space?\n> %s\n", linenum, bufbak);
            winfo->errnum++;
            *ok_flag = FALSE;
            return TRUE;
        }
        winfo->cprob[vnum] = atof(&(ptmp[1]));
        if (winfo->cprob[vnum] != 0.0) winfo->cwnum++;
        /* read next word entry (just skip them) */
        if ((ptmp = mystrtok(NULL, " \t\n")) == NULL) {
            jlog("Error: voca_load_wordlist: line %d: corrupted data:\n> %s\n", linenum,bufbak);
            winfo->errnum++;
            *ok_flag = FALSE;
            return TRUE;
        }
        /* move to the next word entry */
        if ((ptmp = mystrtok_movetonext(NULL, " \t\n")) == NULL) {
            jlog("Error: voca_load_wordlist: line %d: corrupted data:\n> %s\n", linenum, bufbak);
            winfo->errnum++;
            *ok_flag = FALSE;
            return TRUE;
        }
#else  /* ~CLASS_NGRAM */
        jlog("Error: voca_load_wordlist: line %d: cannot handle in-class word probability\n> %s\n", linenum, ptmp, bufbak);
        winfo->errnum++;
        *ok_flag = FALSE;
        return TRUE;
#endif /* CLASS_NGRAM */
    }

    /* OutputString */
    switch(ptmp[0]) {
    case '[':			/* ignore transparency */
        ptmp = mystrtok_quotation(NULL, " \t\n", '[', ']', 0);
        break;
    case '{':			/* ignore transparency */
        ptmp = mystrtok_quotation(NULL, " \t\n", '{', '}', 0);
        break;
    default:
        /* ALLOW no entry for output */
        /* same as wname is used */
        ptmp = winfo->wname[vnum];
    }
    if (ptmp == NULL) {
        jlog("Error: voca_load_htkdict: line %d: corrupted data:\n> %s\n", linenum, bufbak);
        winfo->errnum++;
        *ok_flag = FALSE;
        return TRUE;
    }
    winfo->woutput[vnum] = strcpy((char *)mybmalloc2(strlen(ptmp)+1, &(winfo->mroot)), ptmp);

    /* phoneme sequence */
    if (hmminfo == NULL) {
        /* don't read */
        winfo->wseq[vnum] = NULL;
        winfo->wlen[vnum] = 0;
    } else {

        len = 0;
        first = TRUE;
        pok = TRUE;

        for (;;) {
            if (do_conv) {
                if (first) {
                    /* init phone cycler */
                    cycle_triphone(NULL);
                    /* insert head phone at beginning of word */
                    if (contextphone) {
                        if (strlen(contextphone) >= MAX_HMMNAME_LEN) {
                            jlog("Error: voca_load_htkdict: line %d: too long phone name: %s\n", linenum, contextphone);
                            winfo->errnum++;
                            *ok_flag = FALSE;
                            return TRUE;
                        }
                        cycle_triphone(contextphone);
                    } else {
                        cycle_triphone("NULL_C");
                    }
                    if ((lp = mystrtok(NULL, " \t\n")) == NULL) {
                        jlog("Error: voca_load_wordlist: line %d: word %s has no phoneme:\n> %s\n", linenum, winfo->wname[vnum], bufbak);
                        winfo->errnum++;
                        *ok_flag = FALSE;
                        return TRUE;
                    }
                    if (strlen(lp) >= MAX_HMMNAME_LEN) {
                        jlog("Error: voca_load_htkdict: line %d: too long phone name: %s\n", linenum, lp);
                        winfo->errnum++;
                        *ok_flag = FALSE;
                        return TRUE;
                    }
                    p = cycle_triphone(lp);
                    first = FALSE;
                } else {		/* do_conv, not first */
                    if (lp != NULL) {	/* some token processed at last loop */
                        lp = mystrtok(NULL, " \t\n");
                        if (lp != NULL) {
                            /* token exist */
                            if (strlen(lp) >= MAX_HMMNAME_LEN) {
                                jlog("Error: voca_load_htkdict: line %d: too long phone name: %s\n", linenum, lp);
                                winfo->errnum++;
                                *ok_flag = FALSE;
                                return TRUE;
                            }
                            p = cycle_triphone(lp);
                        } else {
                            /* no more token, insert tail phone at end of word */
                            if (contextphone) {
                                if (strlen(contextphone) >= MAX_HMMNAME_LEN) {
                                    jlog("Error: voca_load_htkdict: line %d: too long phone name: %s\n", linenum, contextphone);
                                    winfo->errnum++;
                                    *ok_flag = FALSE;
                                    return TRUE;
                                }
                                p = cycle_triphone(contextphone);
                            } else {
                                p = cycle_triphone("NULL_C");
                            }
                        }
                    } else {		/* no more token at last input  */
                        /* flush tone cycler */
                        p = cycle_triphone_flush();
                    }
                }
            } else {			/* not do_conv */
                if (first) {
                    p = lp = headphone;
                    first = FALSE;
                } else {
                    if (lp != NULL) {	/* some token processed at last loop */
                        p = lp = mystrtok(NULL, " \t\n");
                        /* if no more token, use tailphone */
                        if (lp == NULL) p = tailphone;
                    } else {
                        /* no more token at last input, exit loop */
                        p = NULL;
                    }
                }
            }
            if (p == NULL) break;
            /* for headphone and tailphone, their context should not be handled */
            /* and when they appear as context they should be replaced by contextphone */
            if (do_conv) {
                center_name(p, cbuf);
                if (contextphone) {
                    if (strmatch(cbuf, contextphone)) {
                        if (len == 0) {
                            p = headphone;
                        } else if (lp == NULL) {
                            p = tailphone;
                        }
                    }
                } else {
                    if (strmatch(cbuf, "NULL_C")) {
                        if (len == 0) {
                            p = headphone;
                        } else if (lp == NULL) {
                            p = tailphone;
                        }
                    } else {
                        if (strnmatch(p, "NULL_C", 6)) {
                            if (strnmatch(&(p[strlen(p)-6]), "NULL_C", 6)) {
                                p = cbuf;
                            } else {
                                p = rightcenter_name(p, cbuf);
                            }
                        } else if (strnmatch(&(p[strlen(p)-6]), "NULL_C", 6)) {
                            p = leftcenter_name(p, cbuf);
                        }
                    }
                }
            }
            //printf("[[%s]]\n", p);

            /* both defined/pseudo phone is allowed */
            tmplg = htk_hmmdata_lookup_logical(hmminfo, p);
            if (tmplg == NULL) {
                /* not found */
                if (do_conv) {
                    /* logical phone was not found */
                    jlog("Error: voca_load_wordlist: line %d: logical phone \"%s\" not found\n", linenum, p);
                    snprintf(cbuf,MAX_HMMNAME_LEN,"%s", p);
                } else {
                    jlog("Error: voca_load_wordlist: line %d: phone \"%s\" not found\n", linenum, p);
                    snprintf(cbuf, MAX_HMMNAME_LEN, "%s", p);
                }
                add_to_error(winfo, cbuf);
                pok = FALSE;
            } else {
                /* found */
                if (len >= tmpmaxlen) {
                    /* expand wseq area by PHONEMELEN_STEP */
                    tmpmaxlen += PHONEMELEN_STEP;
                    tmpwseq = (HMM_Logical **)myrealloc(tmpwseq, sizeof(HMM_Logical *) * tmpmaxlen);
                }
                /* store to temporal buffer */
                tmpwseq[len] = tmplg;
            }
            len++;
        }
        if (!pok) {			/* error in phoneme */
            jlog("Error: voca_load_wordlist: the line content was: %s\n", bufbak);
            winfo->errnum++;
            *ok_flag = FALSE;
            return TRUE;
        }
        if (len == 0) {
            jlog("Error: voca_load_wordlist: line %d: no phone specified:\n> %s\n", linenum, bufbak);
            winfo->errnum++;
            *ok_flag = FALSE;
            return TRUE;
        }
        /* store to winfo */
        winfo->wseq[vnum] = (HMM_Logical **)mybmalloc2(sizeof(HMM_Logical *) * len, &(winfo->mroot));
        memcpy(winfo->wseq[vnum], tmpwseq, sizeof(HMM_Logical *) * len);
        winfo->wlen[vnum] = len;
        winfo->wton[vnum] = 0;
    }

    vnum++;
    *vnum_p = vnum;

    return(TRUE);
}
Exemple #29
0
int parsestr (unsigned char *str, unsigned char **argm, int nitems, unsigned char ** buff, int *inbuf, int *bufsize){
#define buf (*buff)
	int argc = 0;
	int space = 1;
	int comment = 0;
	unsigned char * incbegin = 0;
	int fd;
	int res, len;
	int i = 1;
	unsigned char *str1;

	for(;;str++){
	 if(*str == '\"'){
		str1 = str;
		do {
			*str1 = *(str1 + 1);
		}while(*(str1++));
		if(!comment || *str != '\"'){
			comment = !comment;
		}
	 }
         switch(*str){
		case '\0': 
			if(comment) return -1;
			argm[argc] = 0;
			return argc;
		case '$':
			if(!comment && !included){
				incbegin = str;
				*str = 0;
			}
			break;
		case '\r':
		case '\n':
		case '\t':
		case ' ':
			if(!comment){
				*str = 0;
				space = 1;
				i = 0;
				if(incbegin){
					argc--;
					if((fd = open((char *)incbegin+1, O_RDONLY)) <= 0){
						fprintf(stderr, "Failed to open %s\n", incbegin+1);
						break;
					}
					if((*bufsize - *inbuf) <STRINGBUF){
						*bufsize += STRINGBUF;
						if(!(buf = myrealloc(buf, *bufsize))){
							fprintf(stderr, "Failed to allocate memory for %s\n", incbegin+1);
							close(fd);
							break;
						}
					}
					len = 0;
					if(argm[argc]!=(incbegin+1)) {
						len = (int)strlen((char *)argm[argc]);
						memmove(buf+*inbuf, argm[argc], len);
					}
					if((res = read(fd, buf+*inbuf+len, STRINGBUF-(1+len))) <= 0) {
						perror((char *)incbegin+1);
						close(fd);
						break;
					}
					close(fd);
					buf[*inbuf+res+len] = 0;
					incbegin = buf + *inbuf;
					(*inbuf) += (res + len + 1);
					included++;
					argc+=parsestr(incbegin, argm + argc, nitems - argc, buff, inbuf, bufsize);
					included--;
					incbegin = NULL;

				}
				break;
			}
		default:
			i++;
			if(space) {
				if(comment && *str == '\"' && str[1] != '\"'){
					str++;
					comment = 0;
				}
				argm[argc++] = str;
				if(argc >= nitems) return argc;
				space = 0;
			}
	 }
	}
#undef buf
}
Exemple #30
0
Str* ParseLex(char* s,Err* err,int* i){
    int len=0;
    static char op='\0';
    Str* S=NULL;
    S=StrInit();
    int noStep=0;
    char c;
    ParseLexStates prevstate,state=BLANK;
    
    if (s!=NULL) len=strlen(s); else return NULL;
    while ((*i)<len && s[*i]==' ') (*i)++;
    for (;(state!=ERROR && (*i)<=len);){
        c=s[*i];
        prevstate=state;
        switch (state){
            case BLANK:
                if (c=='"') state=QUOTE;
                else if (CharOp(c)) {
                    if (S->len==0 && CharDoubleOp(c)){
                        if ((*i)<len-1 && s[(*i)+1]==c){
                            StrPutChar(S,c); StrPutChar(S,c); 
                            StrPutChar(S,'\0');
                            (*i)+=2;
                            state=END;
                        } else {
                          StrPutChar(S,c); StrPutChar(S,'\0');
                          (*i)++;
                          state=END;
                        }
                    }
                    if (S->len==0 && CharSimpleOp(c)){
                        StrPutChar(S,c); StrPutChar(S,'\0');
                        state=END;
                        (*i)++;
                    }
                } else if (c==' ' || c=='\0') state=BLANK;
                else {
                    StrPutChar(S,c);
                    state=WORD;
                }
                break;
            case QUOTE:
                if (c!='"') StrPutChar(S,c);
                if (c=='"') {
                    StrPutChar(S,'\0');
                    state=END;
                    (*i)++;
                } else if ((*i)==len-1) state=ERROR;
                break;
            case OPERATOR:
                if (c==op && CharDoubleOp(c)){
                    StrPutChar(S,c);StrPutChar(S,c); StrPutChar(S,'\0');
                    state=END;
                    (*i)++;
                } else {
                    StrPutChar(S,c);
                    StrPutChar(S,'\0');
                    state=END;
                }
                break;
            case WORD:
                if (c=='"') state=WORD;
                else if (c==' '){
                    /*StrPutChar(S,' ');*/
                    state=END;
                } else if (CharOp(c)) {
                    state=END;
                    op=c;
                }
                else {
                    StrPutChar(S,c);
                }
                break;
            default:
                break;
        }
        if (state==END ) {
            if (StrLast(S)!='\0') StrPutChar(S,'\0');

            /*printf("%d\n",*i);*/
            return S;
        }
        if (!noStep) (*i)++;
        if (state==ERROR){
            if (prevstate==QUOTE) {
                err->pres=1;
                (err->err)=myrealloc(err->err,50);
                strcpy(err->err,"Unfinished Quote\0");
            }
            i=NULL;
            return NULL;
        }
        if ((*i)>=len){
            if (state==BLANK) return NULL;
            if (state!=BLANK) {
                if (StrLast(S)!='\0') StrPutChar(S,'\0');
                return S;
            }
        }
    }
    i=NULL;
    return S;
}