예제 #1
0
파일: ffs_vnops.c 프로젝트: mosconi/openbsd
/*
 * Synch an open file.
 */
int
ffs_fsync(void *v)
{
	struct vop_fsync_args *ap = v;
	struct vnode *vp = ap->a_vp;
	struct buf *bp, *nbp;
	int s, error, passes, skipmeta;

	if (vp->v_type == VBLK &&
	    vp->v_specmountpoint != NULL &&
	    (vp->v_specmountpoint->mnt_flag & MNT_SOFTDEP))
		softdep_fsync_mountdev(vp, ap->a_waitfor);

	/*
	 * Flush all dirty buffers associated with a vnode.
	 */
	passes = NIADDR + 1;
	skipmeta = 0;
	if (ap->a_waitfor == MNT_WAIT)
		skipmeta = 1;
	s = splbio();
loop:
	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp;
	     bp = LIST_NEXT(bp, b_vnbufs))
		bp->b_flags &= ~B_SCANNED;
	for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
		nbp = LIST_NEXT(bp, b_vnbufs);
		/* 
		 * Reasons to skip this buffer: it has already been considered
		 * on this pass, this pass is the first time through on a
		 * synchronous flush request and the buffer being considered
		 * is metadata, the buffer has dependencies that will cause
		 * it to be redirtied and it has not already been deferred,
		 * or it is already being written.
		 */
		if (bp->b_flags & (B_BUSY | B_SCANNED))
			continue;
		if ((bp->b_flags & B_DELWRI) == 0)
			panic("ffs_fsync: not dirty");
		if (skipmeta && bp->b_lblkno < 0)
			continue;
		if (ap->a_waitfor != MNT_WAIT &&
		    LIST_FIRST(&bp->b_dep) != NULL &&
		    (bp->b_flags & B_DEFERRED) == 0 &&
		    buf_countdeps(bp, 0, 1)) {
			bp->b_flags |= B_DEFERRED;
			continue;
		}

		bremfree(bp);
		buf_acquire(bp);
		bp->b_flags |= B_SCANNED;
		splx(s);
		/*
		 * On our final pass through, do all I/O synchronously
		 * so that we can find out if our flush is failing
		 * because of write errors.
		 */
		if (passes > 0 || ap->a_waitfor != MNT_WAIT)
			(void) bawrite(bp);
		else if ((error = bwrite(bp)) != 0)
			return (error);
		s = splbio();
		/*
		 * Since we may have slept during the I/O, we need
		 * to start from a known point.
		 */
		nbp = LIST_FIRST(&vp->v_dirtyblkhd);
	}
	if (skipmeta) {
		skipmeta = 0;
		goto loop;
	}
	if (ap->a_waitfor == MNT_WAIT) {
		vwaitforio(vp, 0, "ffs_fsync", 0);

		/*
		 * Ensure that any filesystem metadata associated
		 * with the vnode has been written.
		 */
		splx(s);
		if ((error = softdep_sync_metadata(ap)) != 0)
			return (error);
		s = splbio();
		if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
			/*
			 * Block devices associated with filesystems may
			 * have new I/O requests posted for them even if
			 * the vnode is locked, so no amount of trying will
			 * get them clean. Thus we give block devices a
			 * good effort, then just give up. For all other file
			 * types, go around and try again until it is clean.
			 */
			if (passes > 0) {
				passes -= 1;
				goto loop;
			}
#ifdef DIAGNOSTIC
			if (vp->v_type != VBLK)
				vprint("ffs_fsync: dirty", vp);
#endif
		}
	}
	splx(s);
	return (UFS_UPDATE(VTOI(vp), ap->a_waitfor == MNT_WAIT));
}
void BuildWhiteList()
{
	// Search for unusedcontent.cfg file
	if ( !g_pFileSystem->FileExists( WHITELIST_FILE ) )
	{
		vprint( 1, "Running with no whitelist.cfg file!!!\n" );
		return;
	}

	vprint( 1, "\nBuilding whitelist\n" );

	KeyValues *kv = new KeyValues( WHITELIST_FILE );
	if ( kv )
	{
		if ( kv->LoadFromFile( g_pFileSystem, WHITELIST_FILE, NULL ) )
		{
			for ( KeyValues *sub = kv->GetFirstSubKey(); sub; sub = sub->GetNextKey() )
			{
				if ( !Q_stricmp( sub->GetName(), "add" ) )
				{
					AddToWhiteList( sub->GetString() );
				}
				else if ( !Q_stricmp( sub->GetName(), "remove" ) )
				{
					RemoveFromWhiteList( sub->GetString() );
				}
				else
				{
					vprint( 1, "Unknown subkey '%s' in %s\n", sub->GetName(), WHITELIST_FILE );
				}
			}
		}

		kv->deleteThis();
	}

	if ( verbose || printwhitelist )
	{
		vprint( 1, "Whitelist:\n\n" );


		for ( int i = g_WhiteList.FirstInorder(); 
			i != g_WhiteList.InvalidIndex(); 
			i = g_WhiteList.NextInorder( i ) )
		{
			UnusedContent::CUtlSymbol& sym = g_WhiteList[ i ];

			char const *resolved = g_Analysis.symbols.String( sym );
			vprint( 2, "  %s\n", resolved );
		}
	}

	// dump the whitelist file list anyway
	{
		filesystem->RemoveFile( "whitelist_files.txt", "GAME" );
		for ( int i = g_WhiteList.FirstInorder(); 
			i != g_WhiteList.InvalidIndex(); 
			i = g_WhiteList.NextInorder( i ) )
		{
			UnusedContent::CUtlSymbol& sym = g_WhiteList[ i ];
			char const *resolved = g_Analysis.symbols.String( sym );
			logprint( "whitelist_files.txt", "\"%s\"\n", resolved );
		}
	}


	vprint( 1, "Whitelist resolves to %d files (added %i/removed %i)\n\n", g_WhiteList.Count(), wl_added, wl_removed );
}
예제 #3
0
파일: suffix_array.cpp 프로젝트: ericl/bwt
uint32_t *idc3(uint32_t *T, size_t n, size_t *retsz) {
    vprint("args", T, n);
    T[n++] = 0;

    // step 0: construct a sample
    size_t B0len = (n+1)/3;
    #define toC(i) ((i < B0len) ? (1 + 3*i) : (2 + 3*(i-B0len)))

    // step 1: sort sample suffixes
    triplet *R = new triplet[n * 3 / 2 + 3];
    triplet *Rptr = R;
    for (int j = 1; j <= 2; j++) {
        size_t i = j;
        for (; i < n - 2; i += 3) {
            *Rptr++ = triplet(T[i], T[i+1], T[i+2]);
        }
        for (; i < n - 1; i += 3) {
            *Rptr++ = triplet(T[i], T[i+1], 0);
        }
        for (; i < n; i += 3) {
            *Rptr++ = triplet(T[i], 0, 0);
        }
    }
    bool unique = false;
    size_t Rsz = Rptr - R;
    size_t Rrsz, SARsz;
    uint32_t *Rr = ranks(R, Rsz, &Rrsz, &unique);
    vprint("Rr", Rr, Rrsz)
    delete[] R;
    uint32_t *SAR = unique ? toSA(Rr, Rrsz, &SARsz) : idc3(Rr, Rrsz, &SARsz);
    vprint("SAR", SAR, SARsz)
    delete[] Rr;
    uint32_t *rank = new uint32_t[n+2];
    for (size_t i = 1; i < SARsz; i++) {
        rank[toC(SAR[i])] = i;
    }

    // step 2: sort nonsample suffixes
    triplet *SB0 = new triplet[n/3 + 3];
    triplet *SB0ptr = SB0;
    for (size_t i = 0; i < n; i += 3) {
        *SB0ptr++ = triplet(T[i], rank[i+1], i);
    }
    size_t SB0sz = SB0ptr - SB0;
    lsd_sort(SB0, SB0sz);

    // step 3: merge
    uint32_t *Sc = new uint32_t[SARsz - 1];
    size_t Scsz = SARsz - 1;
    uint32_t *buf = new uint32_t[n + 1];
    uint32_t *buf_ptr = buf;
    for (size_t i = 1; i < SARsz; i++) {
        Sc[i-1] = toC(SAR[i]);
    }
    delete[] SAR;
    size_t sbi = 0;
    size_t sci = 0;
    while (sbi < SB0sz && sci < Scsz) {
        uint32_t i = Sc[sci];
        uint32_t j = SB0[sbi].arr[2];
        if (i % 3 == 1) {
            if (T[i] < T[j] || (T[i] == T[j] && rank[i+1] <= rank[j+1])) {
                *buf_ptr++ = i;
                sci++;
            } else {
                *buf_ptr++ = j;
                sbi++;
            }
        } else if (i % 3 == 2) {
            if (LE(T[i], T[i+1], rank[i+2], T[j], T[j+1], rank[j+2])) {
                *buf_ptr++ = i;
                sci++;
            } else {
                *buf_ptr++ = j;
                sbi++;
            }
        } else {
            assert(false);
        }
    }
    while (sci < Scsz) {
        uint32_t i = Sc[sci++];
        *buf_ptr++ = i;
    }
    while (sbi < SB0sz) {
        uint32_t j = SB0[sbi++].arr[2];
        *buf_ptr++ = j;
    }
    delete[] Sc;
    delete[] SB0;
    delete[] rank;
    *retsz = buf_ptr - buf;
    return buf;
}
예제 #4
0
int zfs_make_lustre(struct mkfs_opts *mop)
{
	zfs_handle_t *zhp;
	zpool_handle_t *php;
	char *pool = NULL;
	char *mkfs_cmd = NULL;
	char *mkfs_tmp = NULL;
	char *ds = mop->mo_device;
	int pool_exists = 0, ret;

	if (osd_check_zfs_setup() == 0)
		return EINVAL;

	/* no automatic index with zfs backend */
	if (mop->mo_ldd.ldd_flags & LDD_F_NEED_INDEX) {
		fatal();
		fprintf(stderr, "The target index must be specified with "
				"--index\n");
		return EINVAL;
	}

	pool = strdup(ds);
	if (pool == NULL)
		return ENOMEM;

	mkfs_cmd = malloc(PATH_MAX);
	if (mkfs_cmd == NULL) {
		ret = ENOMEM;
		goto out;
	}

	mkfs_tmp = malloc(PATH_MAX);
	if (mkfs_tmp == NULL) {
		ret = ENOMEM;
		goto out;
	}

	/* Due to zfs_prepare_lustre() check the '/' must exist */
	strchr(pool, '/')[0] = '\0';

	/* If --reformat was given attempt to destroy the previous dataset */
	if ((mop->mo_flags & MO_FORCEFORMAT) &&
	    ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_FILESYSTEM)) != NULL)) {

		ret = zfs_destroy(zhp, 0);
		if (ret) {
			zfs_close(zhp);
			fprintf(stderr, "Failed destroy zfs dataset %s (%d)\n",
				ds, ret);
			goto out;
		}

		zfs_close(zhp);
	}

	/*
	 * Create the zpool if the vdevs have been specified and the pool
	 * does not already exists.  The pool creation itself will be done
	 * with the zpool command rather than the zpool_create() library call
	 * so the existing zpool error handling can be leveraged.
	 */
	php = zpool_open(g_zfs, pool);
	if (php) {
		pool_exists = 1;
		zpool_close(php);
	}

	if ((mop->mo_pool_vdevs != NULL) && (pool_exists == 0)) {

		memset(mkfs_cmd, 0, PATH_MAX);
		snprintf(mkfs_cmd, PATH_MAX,
			"zpool create -f -O canmount=off %s", pool);

		/* Append the vdev config and create file vdevs as required */
		while (*mop->mo_pool_vdevs != NULL) {
			strscat(mkfs_cmd, " ", PATH_MAX);
			strscat(mkfs_cmd, *mop->mo_pool_vdevs, PATH_MAX);

			ret = zfs_create_vdev(mop, *mop->mo_pool_vdevs);
			if (ret)
				goto out;

			mop->mo_pool_vdevs++;
		}

		vprint("mkfs_cmd = %s\n", mkfs_cmd);
		ret = run_command(mkfs_cmd, PATH_MAX);
		if (ret) {
			fatal();
			fprintf(stderr, "Unable to create pool %s (%d)\n",
				pool, ret);
			goto out;
		}
	}

	/*
	 * Create the ZFS filesystem with any required mkfs options:
	 * - canmount=off is set to prevent zfs automounting
	 * - version=4 is set because SA are not yet handled by the osd
	 */
	memset(mkfs_cmd, 0, PATH_MAX);
	snprintf(mkfs_cmd, PATH_MAX,
		 "zfs create -o canmount=off -o xattr=sa%s %s",
		 zfs_mkfs_opts(mop, mkfs_tmp, PATH_MAX),
		 ds);

	vprint("mkfs_cmd = %s\n", mkfs_cmd);
	ret = run_command(mkfs_cmd, PATH_MAX);
	if (ret) {
		fatal();
		fprintf(stderr, "Unable to create filesystem %s (%d)\n",
			ds, ret);
		goto out;
	}

out:
	if (pool != NULL)
		free(pool);

	if (mkfs_cmd != NULL)
		free(mkfs_cmd);

	if (mkfs_tmp != NULL)
		free(mkfs_tmp);

	return ret;
}
예제 #5
0
/*
 * Last reference to an inode.  If necessary, write or delete it.
 */
int
ufs_inactive(void *v)
{
	struct vop_inactive_args *ap = v;
	struct vnode *vp = ap->a_vp;
	struct inode *ip = VTOI(vp);
	struct proc *p = ap->a_p;
	mode_t mode;
	int error = 0;
#ifdef DIAGNOSTIC
	extern int prtactive;

	if (prtactive && vp->v_usecount != 0)
		vprint("ffs_inactive: pushing active", vp);
#endif

	/*
	 * Ignore inodes related to stale file handles.
	 */
	if (ip->i_din1 == NULL || DIP(ip, mode) == 0)
		goto out;

	if (DIP(ip, nlink) <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
		if (getinoquota(ip) == 0)
			(void)ufs_quota_free_inode(ip, NOCRED);

		error = UFS_TRUNCATE(ip, (off_t)0, IO_EXT | IO_NORMAL, NOCRED);

		DIP_ASSIGN(ip, rdev, 0);
		mode = DIP(ip, mode);
		DIP_ASSIGN(ip, mode, 0);
		ip->i_flag |= IN_CHANGE | IN_UPDATE;

		/*
		 * Setting the mode to zero needs to wait for the inode to be
		 * written just as does a change to the link count. So, rather
		 * than creating a new entry point to do the same thing, we
		 * just use softdep_change_linkcnt(). Also, we can't let
		 * softdep co-opt us to help on its worklist, as we may end up
		 * trying to recycle vnodes and getting to this same point a
		 * couple of times, blowing the kernel stack. However, this
		 * could be optimized by checking if we are coming from
		 * vrele(), vput() or vclean() (by checking for VXLOCK) and
		 * just avoiding the co-opt to happen in the last case.
		 */
		if (DOINGSOFTDEP(vp))
			softdep_change_linkcnt(ip, 1);

		UFS_INODE_FREE(ip, ip->i_number, mode);
	}

	if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) {
		UFS_UPDATE(ip, 0);
	}
out:
	VOP_UNLOCK(vp, 0, p);

	/*
	 * If we are done with the inode, reclaim it
	 * so that it can be reused immediately.
	 */
	if (ip->i_din1 == NULL || DIP(ip, mode) == 0)
		vrecycle(vp, p);

	return (error);
}
예제 #6
0
/*
 * System filesystem synchronizer daemon.
 */
void
sched_sync(struct proc *p)
{
	struct synclist *slp;
	struct vnode *vp;
	time_t starttime;
	int s;

	syncerproc = curproc;

	for (;;) {
		starttime = time_second;

		/*
		 * Push files whose dirty time has expired.
		 */
		s = splbio();
		slp = &syncer_workitem_pending[syncer_delayno];

		syncer_delayno += 1;
		if (syncer_delayno == syncer_maxdelay)
			syncer_delayno = 0;

		while ((vp = LIST_FIRST(slp)) != NULL) {
			if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT, p)) {
				/*
				 * If we fail to get the lock, we move this
				 * vnode one second ahead in time.
				 * XXX - no good, but the best we can do.
				 */
				vn_syncer_add_to_worklist(vp, 1);
				continue;
			}
			splx(s);
			(void) VOP_FSYNC(vp, p->p_ucred, MNT_LAZY);
			vput(vp);
			s = splbio();
			if (LIST_FIRST(slp) == vp) {
				/*
				 * Note: disk vps can remain on the
				 * worklist too with no dirty blocks, but
				 * since sync_fsync() moves it to a different
				 * slot we are safe.
				 */
#ifdef DIAGNOSTIC
				if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
				    vp->v_type != VBLK) {
					vprint("fsync failed", vp);
					if (vp->v_mount != NULL)
						printf("mounted on: %s\n",
						    vp->v_mount->mnt_stat.f_mntonname);
					panic("sched_sync: fsync failed");
				}
#endif /* DIAGNOSTIC */
				/*
				 * Put us back on the worklist.  The worklist
				 * routine will remove us from our current
				 * position and then add us back in at a later
				 * position.
				 */
				vn_syncer_add_to_worklist(vp, syncdelay);
			}

			sched_pause();
		}

		splx(s);

#ifdef FFS_SOFTUPDATES
		/*
		 * Do soft update processing.
		 */
		softdep_process_worklist(NULL);
#endif

		/*
		 * The variable rushjob allows the kernel to speed up the
		 * processing of the filesystem syncer process. A rushjob
		 * value of N tells the filesystem syncer to process the next
		 * N seconds worth of work on its queue ASAP. Currently rushjob
		 * is used by the soft update code to speed up the filesystem
		 * syncer process when the incore state is getting so far
		 * ahead of the disk that the kernel memory pool is being
		 * threatened with exhaustion.
		 */
		if (rushjob > 0) {
			rushjob -= 1;
			continue;
		}
		/*
		 * If it has taken us less than a second to process the
		 * current work, then wait. Otherwise start right over
		 * again. We can still lose time if any single round
		 * takes more than two seconds, but it does not really
		 * matter as we are just trying to generally pace the
		 * filesystem activity.
		 */
		if (time_second == starttime)
			tsleep(&lbolt, PPAUSE, "syncer", 0);
	}
}
예제 #7
0
static int
smbfs_node_alloc(struct mount *mp, struct vnode *dvp, const char *dirnm, 
	int dirlen, const char *name, int nmlen, char sep, 
	struct smbfattr *fap, struct vnode **vpp)
{
	struct vattr vattr;
	struct thread *td = curthread;	/* XXX */
	struct smbmount *smp = VFSTOSMBFS(mp);
	struct smbnode *np, *dnp;
	struct vnode *vp, *vp2;
	struct smbcmp sc;
	char *p, *rpath;
	int error, rplen;

	sc.n_parent = dvp;
	sc.n_nmlen = nmlen;
	sc.n_name = name;	
	if (smp->sm_root != NULL && dvp == NULL) {
		SMBERROR("do not allocate root vnode twice!\n");
		return EINVAL;
	}
	if (nmlen == 2 && bcmp(name, "..", 2) == 0) {
		if (dvp == NULL)
			return EINVAL;
		vp = VTOSMB(VTOSMB(dvp)->n_parent)->n_vnode;
		error = vget(vp, LK_EXCLUSIVE, td);
		if (error == 0)
			*vpp = vp;
		return error;
	} else if (nmlen == 1 && name[0] == '.') {
		SMBERROR("do not call me with dot!\n");
		return EINVAL;
	}
	dnp = dvp ? VTOSMB(dvp) : NULL;
	if (dnp == NULL && dvp != NULL) {
		vprint("smbfs_node_alloc: dead parent vnode", dvp);
		return EINVAL;
	}
	error = vfs_hash_get(mp, smbfs_hash(name, nmlen), LK_EXCLUSIVE, td,
	    vpp, smbfs_vnode_cmp, &sc);
	if (error)
		return (error);
	if (*vpp) {
		np = VTOSMB(*vpp);
		/* Force cached attributes to be refreshed if stale. */
		(void)VOP_GETATTR(*vpp, &vattr, td->td_ucred);
		/*
		 * If the file type on the server is inconsistent with
		 * what it was when we created the vnode, kill the
		 * bogus vnode now and fall through to the code below
		 * to create a new one with the right type.
		 */
		if (((*vpp)->v_type == VDIR && 
		    (np->n_dosattr & SMB_FA_DIR) == 0) ||
	    	    ((*vpp)->v_type == VREG && 
		    (np->n_dosattr & SMB_FA_DIR) != 0)) {
			vgone(*vpp);
			vput(*vpp);
		}
		else {
			SMBVDEBUG("vnode taken from the hashtable\n");
			return (0);
		}
	}
	/*
	 * If we don't have node attributes, then it is an explicit lookup
	 * for an existing vnode.
	 */
	if (fap == NULL)
		return ENOENT;

	error = getnewvnode("smbfs", mp, &smbfs_vnodeops, vpp);
	if (error)
		return (error);
	vp = *vpp;
	np = malloc(sizeof *np, M_SMBNODE, M_WAITOK | M_ZERO);
	rplen = dirlen;
	if (sep != '\0')
		rplen++;
	rplen += nmlen;
	rpath = malloc(rplen + 1, M_SMBNODENAME, M_WAITOK);
	p = rpath;
	bcopy(dirnm, p, dirlen);
	p += dirlen;
	if (sep != '\0')
		*p++ = sep;
	if (name != NULL) {
		bcopy(name, p, nmlen);
		p += nmlen;
	}
	*p = '\0';
	MPASS(p == rpath + rplen);
	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
	/* Vnode initialization */
	vp->v_type = fap->fa_attr & SMB_FA_DIR ? VDIR : VREG;
	vp->v_data = np;
	np->n_vnode = vp;
	np->n_mount = VFSTOSMBFS(mp);
	np->n_rpath = rpath;
	np->n_rplen = rplen;
	np->n_nmlen = nmlen;
	np->n_name = smbfs_name_alloc(name, nmlen);
	np->n_ino = fap->fa_ino;
	if (dvp) {
		ASSERT_VOP_LOCKED(dvp, "smbfs_node_alloc");
		np->n_parent = dvp;
		np->n_parentino = VTOSMB(dvp)->n_ino;
		if (/*vp->v_type == VDIR &&*/ (dvp->v_vflag & VV_ROOT) == 0) {
			vref(dvp);
			np->n_flag |= NREFPARENT;
		}
	} else if (vp->v_type == VREG)
		SMBERROR("new vnode '%s' born without parent ?\n", np->n_name);
	error = insmntque(vp, mp);
	if (error) {
		free(np, M_SMBNODE);
		return (error);
	}
	error = vfs_hash_insert(vp, smbfs_hash(name, nmlen), LK_EXCLUSIVE,
	    td, &vp2, smbfs_vnode_cmp, &sc);
	if (error) 
		return (error);
	if (vp2 != NULL)
		*vpp = vp2;
	return (0);
}
예제 #8
0
파일: knockd.c 프로젝트: bhuvi8/knock
int main(int argc, char **argv)
{
	char pcapErr[PCAP_ERRBUF_SIZE] = "";
	int opt, ret, optidx = 1;

	static struct option opts[] =
	{
		{"verbose",   no_argument,       0, 'v'},
		{"debug",     no_argument,       0, 'D'},
		{"daemon",    no_argument,       0, 'd'},
		{"lookup",    no_argument,       0, 'l'},
		{"interface", required_argument, 0, 'i'},
		{"config",    required_argument, 0, 'c'},
		{"help",      no_argument,       0, 'h'},
		{"pidfile",   required_argument, 0, 'p'},
		{"logfile",   required_argument, 0, 'g'},
		{"version",   no_argument,       0, 'V'},
		{0, 0, 0, 0}
	};
	
	while((opt = getopt_long(argc, argv, "vDdli:c:p:g:hV", opts, &optidx))) {
		if(opt < 0) {
			break;
		}
		switch(opt) {
			case 0:   break;
			case 'v': o_verbose = 1; break;
			case 'D': o_debug = 1; break;
			case 'd': o_daemon = 1; break;
			case 'l': o_lookup = 1; break;
			case 'i': strncpy(o_int, optarg, sizeof(o_int)-1);
								o_int[sizeof(o_int)-1] = '\0';
								break;
			case 'c': strncpy(o_cfg, optarg, sizeof(o_cfg)-1);
								o_cfg[sizeof(o_cfg)-1] = '\0';
								break;
			case 'p': strncpy(o_pidfile, optarg, sizeof(o_pidfile)-1);
								o_pidfile[sizeof(o_pidfile)-1] = '\0';
								break;
			case 'g': strncpy(o_logfile, optarg, sizeof(o_logfile)-1);
								o_logfile[sizeof(o_logfile)-1] = '\0';
								break;
			case 'V': ver();
			case 'h': /* fallthrough */
			default: usage(0);
		}
	}

	if(parseconfig(o_cfg)) {
		usage(1);
	}

	/* set o_int to a default value if it has not been set by the -i switch nor by
	 * the config file */
	if(strlen(o_int) == 0) {
		strncpy(o_int, "eth0", sizeof(o_int));	/* no explicit termination needed */
	}
	if(o_usesyslog) {
		openlog("knockd", 0, LOG_USER);
	}
	if(strlen(o_logfile)) {
		/* open the log file */
		logfd = fopen(o_logfile, "a");
		if(logfd == NULL) {
			perror("warning: cannot open logfile");
		}
	}

	/* 50ms timeout for packet capture. See pcap(3pcap) manpage, which
	 * recommends that a timeout of 0 not be used. */
	cap = pcap_open_live(o_int, 65535, 0, 50, pcapErr);
	if(strlen(pcapErr)) {
		fprintf(stderr, "could not open %s: %s\n", o_int, pcapErr);
	}
	if(cap == NULL) {
		exit(1);
	}

	lltype = pcap_datalink(cap);
	switch(lltype) {
		case DLT_EN10MB:
			dprint("ethernet interface detected\n");
			break;
		case DLT_LINUX_SLL:
			dprint("ppp interface detected (linux \"cooked\" encapsulation)\n");
			break;
		case DLT_RAW:
			dprint("raw interface detected, no encapsulation\n");
			break;
		default:
			fprintf(stderr, "error: unsupported link-layer type: %d\n", lltype);
			cleanup(1);
			break;
	}

	/* get our local IP address */
	if(get_ip(o_int, myip, 32) == NULL) {
		fprintf(stderr, "could not get IP address for %s\n", o_int);
		cleanup(1);
	} else {
		dprint("Local IP: %s\n", myip);
	}

	generate_pcap_filter();

	if(o_daemon) {
		FILE *pidfp;
		if(daemon(0, 0) < 0) {
			perror("daemon");
			cleanup(1);
		}
		/* write our PID to the pidfile*/
		if((pidfp = fopen(o_pidfile, "w"))) {
			fprintf(pidfp, "%d\n", getpid());
			fclose(pidfp);
		} else {
			dprint("could not create pid file %s: %s\n", o_pidfile, strerror(errno));
			logprint("could not create pid file %s: %s", o_pidfile, strerror(errno));
		}
	}

	signal(SIGINT, cleanup);
	signal(SIGTERM, cleanup);
	signal(SIGCHLD, child_exit);
	signal(SIGHUP, reload);

	vprint("listening on %s...\n", o_int);
	logprint("starting up, listening on %s", o_int);
	ret = 1;
	while(ret >= 0) {
		ret = pcap_dispatch(cap, -1, sniff, NULL);
	}
	dprint("bailed out of main loop! (ret=%d)\n", ret);
	pcap_perror(cap, "pcap");

	cleanup(0);
	/* notreached */
	exit(0);
}
void CCodeProcessor::ProcessModule( bool forcequiet, int depth, int& maxdepth, int& numheaders, int& skippedfiles, const char *baseroot, const char *root, const char *module )
{
	char filename[ 256 ];

	bool checkroot = false;

	if ( depth > maxdepth )
	{
		maxdepth = depth;
	}

	// Load the base module
	sprintf( filename, "%s\\%s", root, module );
	strlwr( filename );

	bool firstheader = true;
retry:

	// Check module list
	for ( int i = 0; i < m_nModuleCount; i++ )
	{
		if ( !stricmp( m_Modules[ i ].name, filename ) )
		{
			if ( forcequiet )
			{
				m_nHeadersProcessed++;
				numheaders++;

				if ( m_Modules[ i ].skipped )
				{
					skippedfiles++;
				}
			}

			AddHeader( depth, filename, m_szCurrentCPP );

			return;
		}
	}

	int filelength;
	char *buffer = (char *)COM_LoadFile( filename, &filelength );
	if ( !buffer )
	{
		if ( !checkroot )
		{
			checkroot = true;
			// Load the base module
			sprintf( filename, "%s\\%s", baseroot, module );
			goto retry;
		}
		m_Modules[ m_nModuleCount ].skipped = true;
		strcpy( m_Modules[ m_nModuleCount++ ].name, filename );
		
		skippedfiles++;
		return;
	}

	m_nBytesProcessed += filelength;

	m_Modules[ m_nModuleCount ].skipped = false;
	strcpy( m_Modules[ m_nModuleCount++ ].name, filename );

	bool readonly = false;
	bool madechanges = false;
	CreateBackup( filename, readonly );

	if ( !forcequiet )
	{
		strcpy( m_szCurrentCPP, filename );
		
		vprint( 0, "- %s\n", (char *)&filename[ m_nOffset ] );
	}

	// Parse tokens looking for #include directives or class starts
	char *current = buffer;
	char *startofline;

	current = CC_ParseToken( current );
	while ( current )
	{
		// No more tokens
		if ( strlen( com_token ) <= 0 )
			break;

		if ( !stricmp( com_token, "#include" ) )
		{
			startofline = current - strlen( "#include" );

			current = CC_ParseToken( current );

			if ( strlen( com_token ) > 0)
			{
				vprint( 1, "#include %s", com_token );
				m_nHeadersProcessed++;
				numheaders++;				

				AddHeader( depth, filename, m_szCurrentCPP );

				bool dobuild = true;
				if ( firstheader )
				{
					if ( !stricmp( com_token, "cbase.h" ) )
					{
						dobuild = false;
					}

					if ( !TryBuild( baseroot, filename, (unsigned char *)buffer, filelength ) )
					{
						// build is broken, stop
						assert( 0 );
					}
				}

				firstheader = false;

				if ( dobuild )
				{
					// Try removing the header and compiling
					char saveinfo[2];
					memcpy( saveinfo, startofline, 2 );
					startofline[ 0 ] = '/';
					startofline[ 1 ] = '/';

					if ( TryBuild( baseroot, filename, (unsigned char *)buffer, filelength ) )
					{
						vprint( 0, ", unnecessary\n" );
						madechanges = true;
					}
					else
					{
						// Restore line
						memcpy( startofline, saveinfo, 2 );
						vprint( 0, "\n" );
					}
				}
				else
				{
					vprint( 0, "\n" );
				}
			}
		}

		current = CC_ParseToken( current );
	}

	// Save out last set of changes
	{
		FILE *fp;
		fp = fopen( filename, "wb" );
		if ( fp )
		{
			fwrite( buffer, filelength, 1, fp );
			fclose( fp );
		}
	}

	COM_FreeFile( (unsigned char *)buffer );

	if ( !madechanges )
	{
		RestoreBackup( filename, readonly );
	}

	if ( !forcequiet && !GetQuiet() )
	{
		vprint( 0, " %s: headers (%i)", (char *)&filename[ m_nOffset ], numheaders );
		if ( maxdepth > 1 )
		{
			vprint( 0, ", depth %i", maxdepth );
		}
		vprint( 0, "\n" );
	}

	m_nLinesOfCode += linesprocessed;
	linesprocessed = 0;
}
예제 #10
0
파일: knockd.c 프로젝트: bhuvi8/knock
/**
 * Process a knock attempt to see if the knocker has graduated to the next
 * sequence. If they've completed all sequences correctly, then we open the
 * door.
 */
void process_attempt(knocker_t *attempt)
{
	/* level up! */
	attempt->stage++;
	if(attempt->srchost) {
		vprint("%s (%s): %s: Stage %d\n", attempt->src, attempt->srchost, attempt->door->name, attempt->stage);
		logprint("%s (%s): %s: Stage %d", attempt->src, attempt->srchost, attempt->door->name, attempt->stage);
	} else {
		vprint("%s: %s: Stage %d\n", attempt->src, attempt->door->name, attempt->stage);
		logprint("%s: %s: Stage %d", attempt->src, attempt->door->name, attempt->stage);
	}
	if(attempt->stage >= attempt->door->seqcount) {
		if(attempt->srchost) {
			vprint("%s (%s): %s: OPEN SESAME\n", attempt->src, attempt->srchost, attempt->door->name);
			logprint("%s (%s): %s: OPEN SESAME", attempt->src, attempt->srchost, attempt->door->name);
		} else {
			vprint("%s: %s: OPEN SESAME\n", attempt->src, attempt->door->name);
			logprint("%s: %s: OPEN SESAME", attempt->src, attempt->door->name);
		}
		if(attempt->door->start_command && strlen(attempt->door->start_command)) {
			/* run the associated command */
			if(fork() == 0) {
				/* child */
				char parsed_start_cmd[PATH_MAX];
				char parsed_stop_cmd[PATH_MAX];
				size_t cmd_len = 0;

				setsid();

				/* parse start and stop command and check if the parsed commands fit in the given buffer. Don't
				 * execute any command if one of them has been truncated */
				cmd_len = parse_cmd(parsed_start_cmd, sizeof(parsed_start_cmd), attempt->door->start_command, attempt->src);
				if(cmd_len >= sizeof(parsed_start_cmd)) {	/* command has been truncated --> do NOT execute it */
					fprintf(stderr, "error: parsed start command has been truncated! --> won't execute it\n");
					logprint("error: parsed start command has been truncated! --> won't execute it");
					exit(0); /* exit child */
				}
				if(attempt->door->stop_command) {
					cmd_len = parse_cmd(parsed_stop_cmd, sizeof(parsed_stop_cmd), attempt->door->stop_command, attempt->src);
					if(cmd_len >= sizeof(parsed_stop_cmd)) {	/* command has been truncated --> do NOT execute it */
						fprintf(stderr, "error: parsed stop command has been truncated! --> won't execute start command\n");
						logprint("error: parsed stop command has been truncated! --> won't execute start command");
						exit(0); /* exit child */
					}
				}

				/* all parsing ok --> execute the parsed (%IP% = source IP) command */
				exec_cmd(parsed_start_cmd, attempt->door->name);
				/* if stop_command is set, sleep for cmd_timeout and run it*/
				if(attempt->door->stop_command){
					sleep(attempt->door->cmd_timeout);
					if(attempt->srchost) {
						vprint("%s (%s): %s: command timeout\n", attempt->src, attempt->srchost, attempt->door->name);
						logprint("%s (%s): %s: command timeout", attempt->src, attempt->srchost, attempt->door->name);
					} else {
						vprint("%s: %s: command timeout\n", attempt->src, attempt->door->name);
						logprint("%s: %s: command timeout", attempt->src, attempt->door->name);
					}
					exec_cmd(parsed_stop_cmd, attempt->door->name);
				}

				exit(0); /* exit child */
			}
		}
		/* change to next sequence if one time sequences are used.
		 * Note that here the door will eventually be closed in
		 * get_new_one_time_sequence() if no more sequences are left */
		if(attempt->door->one_time_sequences_fd) {
			disable_used_one_time_sequence(attempt->door);
			get_new_one_time_sequence(attempt->door);

			/* update pcap filter */
			free(attempt->door->pcap_filter_exp);
			attempt->door->pcap_filter_exp = NULL;
			generate_pcap_filter();
		}
	}
}
예제 #11
0
파일: knockd.c 프로젝트: bhuvi8/knock
/* Sniff an interface, looking for port-knock sequences
 */
void sniff(u_char* arg, const struct pcap_pkthdr* hdr, const u_char* packet)
{
	/* packet structs */
	struct ether_header* eth = NULL;
	struct ip* ip = NULL;
	struct tcphdr* tcp = NULL;
	struct udphdr* udp = NULL;
	char proto[8];
	/* TCP/IP data */	
	struct in_addr inaddr;
	unsigned short sport, dport;
	char srcIP[16], dstIP[16];
	/* timestamp */
	time_t pkt_secs = hdr->ts.tv_sec;
	struct tm* pkt_tm;
	char pkt_date[11];
	char pkt_time[9];
	PMList *lp;
	knocker_t *attempt = NULL;

	if(lltype == DLT_EN10MB) {
		eth = (struct ether_header*)packet;
		if(ntohs(eth->ether_type) != ETHERTYPE_IP) {
			return;
		}

		ip = (struct ip*)(packet + sizeof(struct ether_header));
#ifdef __linux__
	} else if(lltype == DLT_LINUX_SLL) {
		ip = (struct ip*)((u_char*)packet + 16);
#endif
	} else if(lltype == DLT_RAW) {
		ip = (struct ip*)((u_char*)packet);
	} else {
		dprint("link layer header type of packet not recognized, ignoring...\n");
		return;
	}

	if(ip->ip_v != 4) {
		/* no IPv6 yet */
		dprint("packet is not IPv4, ignoring...\n");
		return;
	}
	if(ip->ip_p == IPPROTO_ICMP) {
		/* we don't do ICMP */
		return;
	}

	sport = dport = 0;

	if(ip->ip_p == IPPROTO_TCP) {
		strncpy(proto, "tcp", sizeof(proto));
		tcp = (struct tcphdr*)((u_char*)ip + (ip->ip_hl *4));
		sport = ntohs(tcp->th_sport);
		dport = ntohs(tcp->th_dport);
	}
	if(ip->ip_p == IPPROTO_UDP) {
		strncpy(proto, "udp", sizeof(proto));
		udp = (struct udphdr*)((u_char*)ip + (ip->ip_hl * 4));
		sport = ntohs(udp->uh_sport);
		dport = ntohs(udp->uh_dport);
	}

	/* get the date/time */
	pkt_tm = localtime(&pkt_secs);
	snprintf(pkt_date, 11, "%04d-%02d-%02d", pkt_tm->tm_year+1900, pkt_tm->tm_mon,
			pkt_tm->tm_mday);
	snprintf(pkt_time, 9, "%02d:%02d:%02d", pkt_tm->tm_hour, pkt_tm->tm_min,
			pkt_tm->tm_sec);

	/* convert IPs from binary to string */
	inaddr.s_addr = ip->ip_src.s_addr;
	strncpy(srcIP, inet_ntoa(inaddr), sizeof(srcIP)-1);
	srcIP[sizeof(srcIP)-1] = '\0';
	inaddr.s_addr = ip->ip_dst.s_addr;
	strncpy(dstIP, inet_ntoa(inaddr), sizeof(dstIP)-1);
	dstIP[sizeof(dstIP)-1] = '\0';

	dprint("%s %s: %s: %s:%d -> %s:%d %d bytes\n", pkt_date, pkt_time,
			proto, srcIP, sport, dstIP, dport, hdr->len);

	/* clean up expired/completed/failed attempts */
	for(lp = attempts; lp; lp = lp->next) {
		int nix = 0;
		attempt = (knocker_t*)lp->data;
		if(attempt->stage >= attempt->door->seqcount) {
			dprint("removing successful knock attempt (%s)\n", attempt->src);
			nix = 1;
		}
		if(attempt->stage < 0) {
			dprint("removing failed knock attempt (%s)\n", attempt->src);
			nix = 1;
		}
		if(!nix && (pkt_secs - attempt->seq_start) >= attempt->door->seq_timeout) {
			if(attempt->srchost) {
				vprint("%s (%s): %s: sequence timeout (stage %d)\n", attempt->src, attempt->srchost,
						attempt->door->name, attempt->stage);
				logprint("%s (%s): %s: sequence timeout (stage %d)\n", attempt->src, attempt->srchost,
						attempt->door->name, attempt->stage);
			} else {
				vprint("%s: %s: sequence timeout (stage %d)\n", attempt->src,
						attempt->door->name, attempt->stage);
				logprint("%s: %s: sequence timeout (stage %d)\n", attempt->src,
						attempt->door->name, attempt->stage);
			}
			nix = 1;
		}
		if(nix) {
			knocker_t *k = (knocker_t*)lp->data;
			/* splice this entry out of the list */
			if(lp->prev) lp->prev->next = lp->next;
			if(lp->next) lp->next->prev = lp->prev;
			if(lp == attempts) attempts = NULL;
			lp->prev = lp->next = NULL;
			free(k->srchost);
			list_free(lp);
			continue;
		}
	}

	attempt = NULL;
	/* look for this guy in our attempts list */
	for(lp = attempts; lp; lp = lp->next) {
		knocker_t *att = (knocker_t*)lp->data;
		if(!strncmp(att->src, srcIP, sizeof(srcIP)) &&
		   !strncmp(att->door->target ? att->door->target : myip, dstIP, sizeof(dstIP))) {
			attempt = att;
			break;
		}
	}

	if(attempt) {
		int flagsmatch = flags_match(attempt->door, ip, tcp);
		if(flagsmatch && ip->ip_p == attempt->door->protocol[attempt->stage] &&
				dport == attempt->door->sequence[attempt->stage]) {
			process_attempt(attempt);
		} else if(flagsmatch == 0) {
			/* TCP flags didn't match -- just ignore this packet, don't
			 * invalidate the knock.
			 */
		} else {
			/* invalidate the knock sequence, it will be removed in the
			 * next sniff() call.
			 */
			attempt->stage = -1;
		}
	} else {
		/* did they hit the first port correctly? */
		for(lp = doors; lp; lp = lp->next) {
			opendoor_t *door = (opendoor_t*)lp->data;
			/* if we're working with TCP, try to match the flags */
			if(!flags_match(door, ip, tcp)) {
				continue;
			}
			if(ip->ip_p == door->protocol[0] && dport == door->sequence[0] &&
			   !strcmp(dstIP, door->target ? door->target : myip)) {
				struct hostent *he;
				/* create a new entry */
				attempt = (knocker_t*)malloc(sizeof(knocker_t));
				attempt->srchost = NULL;
				if(attempt == NULL) {
					perror("malloc");
					exit(1);
				}
				strcpy(attempt->src, srcIP);
				/* try a reverse lookup if enabled  */
				if (o_lookup) {
					inaddr.s_addr = ip->ip_src.s_addr;
					he = gethostbyaddr((void *)&inaddr, sizeof(inaddr), AF_INET);
					if(he) {
						attempt->srchost = strdup(he->h_name);
					}
				}

				attempt->stage = 0;
				attempt->seq_start = pkt_secs;
				attempt->door = door;
				attempts = list_add(attempts, attempt);
				process_attempt(attempt);
			}
		}
	}
}
예제 #12
0
파일: Stream.cpp 프로젝트: jiangxilong/yari
void Stream::vprint_cr(const char* format, va_list argptr) {
  vprint(format, argptr);
  cr();
}
예제 #13
0
int _readline(struct rls *rls)
{
	int flags;
	struct termios termios;
	int i, j;
	int fkey;
	char *kb;
	int ihist;
	int rc;
	char **tab;
	char *p;
	int off = 0;
	
	if (isatty(rls->fdin))
		set_terminal(rls->fdin, &termios);
	
	memset(rls->kbuffer, 0, rls->maxbuflen);
	rls->pos = 0;
	/*
	flags = fcntl(0, F_GETFL);
	fcntl(0, F_SETFL, flags);
	*/

	flags = 0;
	ihist = 0;
	kb = rls->kb;
	rc = 0;
	do {	
		if (off >= rc) {
			memset(kb, 0, sizeof(rls->kb));
			rc = read(rls->fdin, kb, sizeof(rls->kb));
			if (rc <= 0) {
				//usleep(1);
				continue;
			}
			off = 0;
		} else {
			memmove(kb, kb + off, rc - off);
			rc -= off;
		}
#if 0
		printf("\n%2.2x - %2.2x - %2.2x - %2.2x - %2.2x - %2.2x - %2.2x - %2.2x\n",
			kb[0], kb[1], kb[2], kb[3], kb[4], kb[5], kb[6], kb[6]);
		fflush(stdout);
#endif		
		if (kb[0] == ESC && kb[1] == ESC_PAD) {
			fkey = kb[2] | (kb[3] << 8);
			if (kb[2] == KEY_UP) {
				off = 3;
				if (flags == 0) {
					/* set history-mode */
					flags = 1;
					ihist = rls->hist_total;
					if (rls->pos != 0)
						strcpy(rls->history[rls->maxhistnum], rls->kbuffer);
					else
						rls->history[rls->maxhistnum][0] = '\0';
				}
				if (ihist == 0)
					continue;
						
				i = findhistory(rls, 0 - ihist);
				
				if (i == -1)
					continue;
				ihist = i;
				
				/*
				while (rls->pos-- > 0)
				*/
				i = strlen(rls->kbuffer);
				while (rls->pos++ < i)
					vprint(rls->fdout, " ", 1);
				while (i-- > 0)
					vprint(rls->fdout, "\b \b", 3);
				
				memset(rls->kbuffer, 0, rls->maxbuflen);
				strcpy(rls->kbuffer, rls->history[ihist]);

				rls->pos = strlen(rls->kbuffer);
				vprint(rls->fdout, rls->kbuffer, rls->pos);

				continue;
			}			
			if (kb[2] == KEY_DOWN){
				off = 3;
				if (flags == 0)
					continue;
				if ((ihist + 1) >= rls->hist_total) {
					i = strlen(rls->kbuffer);
					while (rls->pos++ < i)
						vprint(rls->fdout, " ", 1);
					while (i-- > 0)
						vprint(rls->fdout, "\b \b", 3);
					memset(rls->kbuffer, 0, rls->maxbuflen);
					rls->pos = 0;
					flags = 0;
					continue;
				}
				//printf("ihist = %d, rls->hist_total = %d\n",
				//    ihist, rls->hist_total);	
				i = findhistory(rls, ihist);
				if (i == -1)
					continue;
				ihist = i;
				
				i = strlen(rls->kbuffer);
				while (rls->pos++ < i)
					vprint(rls->fdout, " ", 1);
				while (i-- > 0)
					vprint(rls->fdout, "\b \b", 3);
				
				memset(rls->kbuffer, 0, rls->maxbuflen);
				strcpy(rls->kbuffer, rls->history[ihist]);

				rls->pos = strlen(rls->kbuffer);
				vprint(rls->fdout, rls->kbuffer, rls->pos);

				continue;
			}
			if (kb[2] == KEY_RIGHT) {
				off = 3;
				if (rls->pos < strlen(rls->kbuffer))
					vprint(rls->fdout, &(rls->kbuffer[rls->pos++]), 1);
				continue;
			}
			if (kb[2] == KEY_LEFT) {
				off = 3;
				if (rls->pos > 0) {
					vprint(rls->fdout, "\b", 1);
					rls->pos --;
				}
				continue;
			}
			if (fkey == KEY_HOME) {
				off = 4;
				while (rls->pos > 0) {
					vprint(rls->fdout, "\b", 1);
					rls->pos --;
				}
				continue;
			}
			if (fkey == KEY_END) {
				off = 4;
				while (rls->pos < strlen(rls->kbuffer)) {
					vprint(rls->fdout, &(rls->kbuffer[rls->pos++]), 1);
				}
				continue;
			}	
			
		}
		flags = 0;
				
		/* 'enter' */
		if (kb[0] == LF || kb[0] == CR) {
			off = 1;
			trimspace(rls->kbuffer);
			rls->pos = strlen(rls->kbuffer);
			if (rls->pos == 0)
				break;
			if (rls->hist_total == rls->maxhistnum) {
				memmove(rls->history[0], rls->history[1], 
				    rls->maxbuflen * (rls->maxhistnum - 1));
				rls->hist_total--;
			}
			strcpy(rls->history[rls->hist_total++], rls->kbuffer);
			break;
		} 
		
		if (kb[0] == CTRLC) {
			off = 1;
			rls->pos = 0;
			rls->kbuffer[0] = '\0';
			break;
		}
		
		if (kb[0] == '\t') {
			off = 1;
			if (rls->tab_callback == NULL)
				continue;
			
			p= NULL;
			if (rls->pos != 0) {
				p = rls->kbuffer + rls->pos;
				while (p > rls->kbuffer) {
					if (*(p - 1) == ' ')
						break;
					p--;
				}
			}
			
			tab = rls->tab_callback(rls->kbuffer, p);
			if (tab == NULL)
				continue;
			
			/* only one */	
			if (*tab != NULL && *(tab + 1) == NULL) {
				
				for (i = 0; i < strlen(p); i++)
					vprint(rls->fdout, "\b \b", 3);
				i = strlen(*tab);
				vprint(rls->fdout, *tab, i);
				if (p - rls->kbuffer + i < rls->maxbuflen) {
					strcpy(p, *tab);
					rls->pos = strlen(rls->kbuffer);
				}
				
				free(*tab);
				free(tab);
				continue;			
			}
			/* more than one */
			vprint(rls->fdout, "\n", 1);
			i = 0;
			while (*(tab + i)) {
				vprint(rls->fdout, *(tab + i), strlen(*(tab + i)));
				vprint(rls->fdout, " ", 1);
				free(*(tab + i));
				i++;
			}
			vprint(rls->fdout, "\n", 1);
			free(tab);
			
			vprint(rls->fdout, rls->prompt, strlen(rls->prompt));
			vprint(rls->fdout, rls->kbuffer, rls->pos);
			continue;
		}
		
		/* backspace */
		if ((kb[0] == BACKSP0)|| (kb[0] == BACKSP1)) {
			off = 1;
			if (rls->pos > 0) {
				i = strlen(rls->kbuffer);
				j = rls->pos;
				while (j < i) {
					rls->kbuffer[j - 1] = rls->kbuffer[j];
					j++;
				}
				rls->kbuffer[j - 1] = '\0';
				
				rls->pos--;
				vprint(rls->fdout, "\b", 1);
				vprint(rls->fdout, &rls->kbuffer[rls->pos], strlen(&rls->kbuffer[rls->pos]));
				vprint(rls->fdout, " \b", 2);
				for (i = 0; i < strlen(rls->kbuffer) - rls->pos; i++)
					vprint(rls->fdout, "\b", 1);
			}
			continue;
		}
		
		/* normal key */	
		off = 1;
		i = kb[0];
		if (isprint(i)) {
			if (rls->pos < strlen(rls->kbuffer) - 1) {
				j = strlen(rls->kbuffer);
				/* avoid overflow */
				if (j < rls->maxbuflen - 1) {
					while (j > rls->pos) {
						rls->kbuffer[j] = rls->kbuffer[j-1];
						j--;
					}
						
					rls->kbuffer[rls->pos] = kb[0];
					//rls->kbuffer[rls->pos + 1] = '\0';
					vprint(rls->fdout, &rls->kbuffer[rls->pos], 
					    strlen(&rls->kbuffer[rls->pos]));
					for (j = 0; j < strlen(rls->kbuffer) - rls->pos - 1; j++)
						vprint(rls->fdout, "\b", 1);
				}
			} else {
				rls->kbuffer[rls->pos] = kb[0];
				rls->kbuffer[rls->pos + 1] = '\0';
				vprint(rls->fdout, &kb[0], 1);
			}
			rls->pos++;
		}
	} while (kb[0] != CTRLP);
		
	if (isatty(rls->fdin))
		reset_terminal(rls->fdin, &termios);

	return (rls->pos > 0 ? 1 : 0);
}
예제 #14
0
void Printer::print(const char *fmt, ...) {
	va_list ap;
	va_start(ap, fmt);
	vprint(fmt, ap);
}
예제 #15
0
파일: ccd.c 프로젝트: avsm/openbsd-xen-sys
int
ccdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
{
	int unit = ccdunit(dev);
	int i, j, lookedup = 0, error = 0;
	int part, pmask, s;
	struct ccd_softc *cs;
	struct ccd_ioctl *ccio = (struct ccd_ioctl *)data;
	struct ccddevice ccd;
	char **cpp;
	struct vnode **vpp;
	vaddr_t min, max;

	if (unit >= numccd)
		return (ENXIO);

	cs = &ccd_softc[unit];
	if (cmd != CCDIOCSET && !(cs->sc_flags & CCDF_INITED))
		return (ENXIO);

	/* access control */
	switch (cmd) {
	case CCDIOCSET:
	case CCDIOCCLR:
	case DIOCWDINFO:
	case DIOCSDINFO:
	case DIOCWLABEL:
		if ((flag & FWRITE) == 0)
			return (EBADF);
	}

	bzero(&ccd, sizeof(ccd));
	switch (cmd) {
	case CCDIOCSET:
		if (cs->sc_flags & CCDF_INITED)
			return (EBUSY);

		if (ccio->ccio_ndisks == 0 || ccio->ccio_ndisks > INT_MAX ||
		    ccio->ccio_ileave < 0)
			return (EINVAL);

		if ((error = ccdlock(cs)) != 0)
			return (error);

		/* Fill in some important bits. */
		ccd.ccd_unit = unit;
		ccd.ccd_interleave = ccio->ccio_ileave;
		ccd.ccd_flags = ccio->ccio_flags & CCDF_USERMASK;

		/* XXX the new code is unstable still */
		ccd.ccd_flags |= CCDF_OLD;

		/*
		 * Interleaving which is not a multiple of the click size
		 * must use the old I/O code (by design)
		 */
		if (ccio->ccio_ileave % (PAGE_SIZE / DEV_BSIZE) != 0)
			ccd.ccd_flags |= CCDF_OLD;

		/*
		 * Allocate space for and copy in the array of
		 * componet pathnames and device numbers.
		 */
		cpp = malloc(ccio->ccio_ndisks * sizeof(char *),
		    M_DEVBUF, M_WAITOK);
		vpp = malloc(ccio->ccio_ndisks * sizeof(struct vnode *),
		    M_DEVBUF, M_WAITOK);

		error = copyin((caddr_t)ccio->ccio_disks, (caddr_t)cpp,
		    ccio->ccio_ndisks * sizeof(char **));
		if (error) {
			free(vpp, M_DEVBUF);
			free(cpp, M_DEVBUF);
			ccdunlock(cs);
			return (error);
		}

		for (i = 0; i < ccio->ccio_ndisks; ++i) {
			CCD_DPRINTF(CCDB_INIT,
			    ("ccdioctl: component %d: %p, lookedup = %d\n",
				i, cpp[i], lookedup));
			if ((error = ccdlookup(cpp[i], p, &vpp[i])) != 0) {
				for (j = 0; j < lookedup; ++j)
					(void)vn_close(vpp[j], FREAD|FWRITE,
					    p->p_ucred, p);
				free(vpp, M_DEVBUF);
				free(cpp, M_DEVBUF);
				ccdunlock(cs);
				return (error);
			}
			++lookedup;
		}
		ccd.ccd_cpp = cpp;
		ccd.ccd_vpp = vpp;
		ccd.ccd_ndev = ccio->ccio_ndisks;

		/*
		 * Initialize the ccd.  Fills in the softc for us.
		 */
		if ((error = ccdinit(&ccd, cpp, p)) != 0) {
			for (j = 0; j < lookedup; ++j)
				(void)vn_close(vpp[j], FREAD|FWRITE,
				    p->p_ucred, p);
			bzero(&ccd_softc[unit], sizeof(struct ccd_softc));
			free(vpp, M_DEVBUF);
			free(cpp, M_DEVBUF);
			ccdunlock(cs);
			return (error);
		}

		/*
		 * The ccd has been successfully initialized, so
		 * we can place it into the array.  Don't try to
		 * read the disklabel until the disk has been attached,
		 * because space for the disklabel is allocated
		 * in disk_attach();
		 */
		bcopy(&ccd, &ccddevs[unit], sizeof(ccd));
		ccio->ccio_unit = unit;
		ccio->ccio_size = cs->sc_size;

		/*
		 * If we use the optimized protocol we need some kvm space
		 * for the component buffers.  Allocate it here.
		 *
		 * XXX I'd like to have a more dynamic way of acquiring kvm
		 * XXX space, but that is problematic as we are not allowed
		 * XXX to lock the kernel_map in interrupt context.  It is
		 * XXX doable via a freelist implementation though.
		 */
		if (!ccdmap && !(ccd.ccd_flags & CCDF_OLD)) {
			min = vm_map_min(kernel_map);
			ccdmap = uvm_km_suballoc(kernel_map, &min, &max,
			    CCD_CLUSTERS * MAXBSIZE, VM_MAP_INTRSAFE,
			    FALSE, NULL);
		}

		/* Attach the disk. */
		cs->sc_dkdev.dk_name = cs->sc_xname;
		disk_attach(&cs->sc_dkdev);

		/* Try and read the disklabel. */
		ccdgetdisklabel(dev, cs, cs->sc_dkdev.dk_label,
		    cs->sc_dkdev.dk_cpulabel, 0);

		ccdunlock(cs);
		break;

	case CCDIOCCLR:
		if ((error = ccdlock(cs)) != 0)
			return (error);

		/*
		 * Don't unconfigure if any other partitions are open
		 * or if both the character and block flavors of this
		 * partition are open.
		 */
		part = DISKPART(dev);
		pmask = (1 << part);
		if ((cs->sc_dkdev.dk_openmask & ~pmask) ||
		    ((cs->sc_dkdev.dk_bopenmask & pmask) &&
		    (cs->sc_dkdev.dk_copenmask & pmask))) {
			ccdunlock(cs);
			return (EBUSY);
		}

		/*
		 * Free ccd_softc information and clear entry.
		 */

		/* Close the components and free their pathnames. */
		for (i = 0; i < cs->sc_nccdisks; ++i) {
			/*
			 * XXX: this close could potentially fail and
			 * cause Bad Things.  Maybe we need to force
			 * the close to happen?
			 */
#ifdef DIAGNOSTIC
			CCD_DCALL(CCDB_VNODE, vprint("CCDIOCCLR: vnode info",
			    cs->sc_cinfo[i].ci_vp));
#endif

			(void)vn_close(cs->sc_cinfo[i].ci_vp, FREAD|FWRITE,
			    p->p_ucred, p);
			free(cs->sc_cinfo[i].ci_path, M_DEVBUF);
		}

		/* Free interleave index. */
		for (i = 0; cs->sc_itable[i].ii_ndisk; ++i)
			free(cs->sc_itable[i].ii_index, M_DEVBUF);

		/* Free component info and interleave table. */
		free(cs->sc_cinfo, M_DEVBUF);
		free(cs->sc_itable, M_DEVBUF);
		cs->sc_flags &= ~CCDF_INITED;

		/*
		 * Free ccddevice information and clear entry.
		 */
		free(ccddevs[unit].ccd_cpp, M_DEVBUF);
		free(ccddevs[unit].ccd_vpp, M_DEVBUF);
		bcopy(&ccd, &ccddevs[unit], sizeof(ccd));

		/* Detatch the disk. */
		disk_detach(&cs->sc_dkdev);

		/* This must be atomic. */
		s = splhigh();
		ccdunlock(cs);
		bzero(cs, sizeof(struct ccd_softc));
		splx(s);
		break;

	case DIOCGPDINFO: {
		struct cpu_disklabel osdep;

		if ((error = ccdlock(cs)) != 0)
			return (error);

		ccdgetdisklabel(dev, cs, (struct disklabel *)data,
		    &osdep, 1);

		ccdunlock(cs);
		break;
	}

	case DIOCGDINFO:
		*(struct disklabel *)data = *(cs->sc_dkdev.dk_label);
		break;

	case DIOCGPART:
		((struct partinfo *)data)->disklab = cs->sc_dkdev.dk_label;
		((struct partinfo *)data)->part =
		    &cs->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
		break;

	case DIOCWDINFO:
	case DIOCSDINFO:
		if ((error = ccdlock(cs)) != 0)
			return (error);

		cs->sc_flags |= CCDF_LABELLING;

		error = setdisklabel(cs->sc_dkdev.dk_label,
		    (struct disklabel *)data, 0, cs->sc_dkdev.dk_cpulabel);
		if (error == 0) {
			if (cmd == DIOCWDINFO)
				error = writedisklabel(CCDLABELDEV(dev),
				    ccdstrategy, cs->sc_dkdev.dk_label,
				    cs->sc_dkdev.dk_cpulabel);
		}

		cs->sc_flags &= ~CCDF_LABELLING;

		ccdunlock(cs);

		if (error)
			return (error);
		break;

	case DIOCWLABEL:
		if (*(int *)data != 0)
			cs->sc_flags |= CCDF_WLABEL;
		else
			cs->sc_flags &= ~CCDF_WLABEL;
		break;

	default:
		return (ENOTTY);
	}

	return (0);
}
예제 #16
0
/*
 * Last reference to an inode.  If necessary, write or delete it.
 */
int
ufs_inactive(void *v)
{
	struct vop_inactive_args *ap = v;
	struct vnode *vp = ap->a_vp;
	struct inode *ip = VTOI(vp);
	struct fs *fs = ip->i_fs;
	struct proc *p = curproc;
	mode_t mode;
	int error = 0, logged = 0, truncate_error = 0;
#ifdef DIAGNOSTIC
	extern int prtactive;

	if (prtactive && vp->v_usecount != 0)
		vprint("ufs_inactive: pushing active", vp);
#endif

	UFS_WAPBL_JUNLOCK_ASSERT(vp->v_mount);

	/*
	 * Ignore inodes related to stale file handles.
	 */
	if (ip->i_din1 == NULL || DIP(ip, mode) == 0)
		goto out;

	if (DIP(ip, nlink) <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
		error = UFS_WAPBL_BEGIN(vp->v_mount);
		if (error)
			goto out;
		logged = 1;
		if (getinoquota(ip) == 0)
			(void)ufs_quota_free_inode(ip, NOCRED);
		if (DIP(ip, size) != 0 && vp->v_mount->mnt_wapbl) {
			/*
			 * When journaling, only truncate one indirect block at
			 * a time.
			 */
			uint64_t incr = MNINDIR(ip->i_ump) << fs->fs_bshift;
			uint64_t base = NDADDR << fs->fs_bshift;
			while (!error && DIP(ip, size) > base + incr) {
				/*
				 * round down to next full indirect block
				 * boundary.
				 */
				uint64_t nsize = base +
				    ((DIP(ip, size) - base - 1) &
				    ~(incr - 1));
				error = UFS_TRUNCATE(ip, nsize, 0, NOCRED);
				if (error)
					break;
				UFS_WAPBL_END(vp->v_mount);
				error = UFS_WAPBL_BEGIN(vp->v_mount);
				if (error)
					goto out;
			}
		}

		if (error == 0) {
			truncate_error = UFS_TRUNCATE(ip, (off_t)0, 0, NOCRED);
			/* XXX pedro: remove me */
			if (truncate_error)
				printf("UFS_TRUNCATE()=%d\n", truncate_error);
		}

		DIP_ASSIGN(ip, rdev, 0);
		mode = DIP(ip, mode);
		DIP_ASSIGN(ip, mode, 0);
		ip->i_flag |= IN_CHANGE | IN_UPDATE;

		/*
		 * Setting the mode to zero needs to wait for the inode to be
		 * written just as does a change to the link count. So, rather
		 * than creating a new entry point to do the same thing, we
		 * just use softdep_change_linkcnt(). Also, we can't let
		 * softdep co-opt us to help on its worklist, as we may end up
		 * trying to recycle vnodes and getting to this same point a
		 * couple of times, blowing the kernel stack. However, this
		 * could be optimized by checking if we are coming from
		 * vrele(), vput() or vclean() (by checking for VXLOCK) and
		 * just avoiding the co-opt to happen in the last case.
		 */
		if (DOINGSOFTDEP(vp))
			softdep_change_linkcnt(ip, 1);

		UFS_INODE_FREE(ip, ip->i_number, mode);
	}

	if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) {
		if (!logged++) {
			int err;
			err = UFS_WAPBL_BEGIN(vp->v_mount);
			if (err) {
				error = err;
				goto out;
			}
		}
		UFS_UPDATE(ip, 0);
	}
	if (logged)
		UFS_WAPBL_END(vp->v_mount);
out:
	VOP_UNLOCK(vp, 0);

	/*
	 * If we are done with the inode, reclaim it
	 * so that it can be reused immediately.
	 */
	if (error == 0 && truncate_error == 0 &&
	    (ip->i_din1 == NULL || DIP(ip, mode) == 0))
		vrecycle(vp, p);

	return (truncate_error ? truncate_error : error);
}
예제 #17
0
파일: model_test.cpp 프로젝트: NCIP/visda
/* Main */ 
int main()
{
	matrix MX;
	mnew(&MX, 200, 8);
	sampleLoad("data_200x8.txt", &MX);
	printf("data=\n");
	mprint(&MX);

	double mean[2][8]=
	{{4.427227e-001,	7.671556e-001,	-9.523772e-001,	3.867558e-001,	-7.916976e-001,	1.165247e-001,	-1.261666e-001,	8.550054e-002},	
	{-2.383899e-001,	-4.130850e-001,	5.128200e-001,	-2.082537e-001,	4.263000e-001,	-6.274427e-002,	6.793605e-002,	-4.603889e-002}};
	matrix MUK;
	mnew(&MUK, 2, 8);
	MUK.pr = *mean;

	matrix *Var0;
	Var0 = new matrix[2];
	for(int i=0; i<2; i++) {
		mnew(Var0+i, 8, 8);
	}
	sampleLoad("var0.txt", Var0);
	printf("var0=\n");
	mprint(Var0);
	sampleLoad("var1.txt", Var0+1);
	printf("var1=\n");
	mprint(Var0+1);
	
	double W[] = {3.500007e-001,	6.499993e-001};
	vector w0;
	vnew(&w0, 2);
	w0.pr = W;

	matrix Gxn;
	mnew(&Gxn, 200, 2);
	vector Fx;
	vnew(&Fx, 200);
	veModel(&MX, &MUK, Var0, &w0, &Gxn, &Fx);

	printf("Gxn=\n");
	mprint(&Gxn);
	printf("Fx=\n");
	vprint(&Fx);

	matrix tmp;
	mnew(&tmp, 1, 8);
	matrix tmp2;
	mnew(&tmp2, 1, 1);
	matrix cen_Dj;
	mnew(&cen_Dj, 1, 8);
	matrix cen_Dj_t;
	mnew(&cen_Dj_t, 8, 1);

	for(i=0; i<8; i++) {
			*(cen_Dj.pr+i) = *(MX.pr+0+i) - *(MUK.pr+8+i);
	}
	transpose(&cen_Dj, &cen_Dj_t);
	printf("\ncen_Dj=\n");
	mprint(&cen_Dj);
	mprint(&cen_Dj_t);

	matrix inv_Var1;
	mnew(&inv_Var1, 8, 8);
	sampleLoad("inv_var1.txt", &inv_Var1);
	mprint(&inv_Var1);

	mmMul(&cen_Dj, &inv_Var1, &tmp);
	printf("\ntmp=\n");
	mprint(&tmp);
	mmMul(&tmp, &cen_Dj_t, &tmp2);
	printf("\ntmp2=\n");
	mprint(&tmp2);
	double val = *(tmp2.pr);
	printf("\nval=%e\n", val);

	mdelete(&MX);
	mdelete(&MUK);
	mdelete(Var0);
	mdelete(Var0+1);
	vdelete(&w0);
	mdelete(&Gxn);
	vdelete(&Fx);

	return 0;

}
예제 #18
0
void CCodeProcessor::ProcessModule( bool forcequiet, int depth, int& maxdepth, int& numheaders, int& skippedfiles, 
	const char *srcroot, const char *baseroot, const char *root, const char *module )
{
	char filename[ 256 ];

	if ( depth > maxdepth )
	{
		maxdepth = depth;
	}
	int filelength;
	char *buffer = NULL;
		
	// Always skip these particular modules/headers
	if ( SkipFile( module ) )
	{
		CODE_MODULE module;
		module.skipped = true;

		m_Modules.Insert( filename, module );

		skippedfiles++;
		return;
	}

	if ( !LoadFile( &buffer, filename, module, forcequiet, depth, filelength, numheaders, skippedfiles,
		srcroot, root, baseroot ) )
	{
		CODE_MODULE module;
		module.skipped = true;
		m_Modules.Insert( filename, module );
		skippedfiles++;
		return;
	}

	assert( buffer );

	m_nBytesProcessed += filelength;

	CODE_MODULE m;
	m.skipped = false;
	m_Modules.Insert( filename, m );

	if ( !forcequiet )
	{
		strcpy( m_szCurrentCPP, filename );
	}

	AddHeader( depth, filename, m_szCurrentCPP );

	bool onclient = !strnicmp( m_szBaseEntityClass, "C_", 2 ) ? true : false;

	// Parse tokens looking for #include directives or class starts
	char *current = buffer;

	current = CC_ParseToken( current );
	while ( 1 )
	{
		// No more tokens
		if ( !current )
			break;

		if ( !stricmp( com_token, "#include" ) )
		{
			current = CC_ParseToken( current );
			if ( strlen( com_token ) > 0 &&
				com_token[ 0 ] != '<' )
			{
				//vprint( "#include %s\n", com_token );
				m_nHeadersProcessed++;
				numheaders++;
				ProcessModule( true, depth + 1, maxdepth, numheaders, skippedfiles, srcroot, baseroot, root, com_token );
			}
		}
		else if ( !stricmp( com_token, "class" ) ||
			 !stricmp( com_token, "struct" ) )
		{
			current = CC_ParseToken( current );
			if ( strlen( com_token ) > 0 )
			{
				//vprint( depth, "class %s\n", com_token );

				CClass *cl = AddClass( com_token );

				// Now see if there's a base class
				current = CC_ParseToken( current );
				if ( !stricmp( com_token, ":" ) )
				{
					// Parse out public and then classname an
					current = CC_ParseToken( current );
					if ( !stricmp( com_token, "public" ) )
					{
						current = CC_ParseToken( current );
						if ( strlen( com_token ) > 0 )
						{
							cl->SetBaseClass( com_token );

							do
							{
								current = CC_ParseToken( current );
							} while ( strlen( com_token ) && stricmp( com_token, "{" ) );

							if ( !stricmp( com_token, "{" ) )
							{
								current = cl->ParseClassDeclaration( current );
							}
						}
					}
				}
				else if ( !stricmp( com_token, "{" ) )
				{
					current = cl->ParseClassDeclaration( current );
				}
			}
		}
		else if ( !strnicmp( com_token, "PREDICTABLE_CLASS", strlen( "PREDICTABLE_CLASS" ) ) )
		{
			char prefix[ 32 ];
			prefix[ 0 ] = 0;
			int type = 0;
			int bases = 1;
			int usebase = 0;

			if ( !stricmp( com_token, "PREDICTABLE_CLASS_ALIASED" ) )
			{
				type = 2;
				bases = 2;
				if ( onclient )
				{
					strcpy( prefix, "C_" );
				}
				else
				{
					strcpy( prefix, "C" );
					usebase = 1;
				}
			}
			else if ( !stricmp( com_token, "PREDICTABLE_CLASS_SHARED" ) )
			{
				type = 1;
				bases = 1;
			}
			else if ( !stricmp( com_token, "PREDICTABLE_CLASS" ) )
			{
				type = 0;
				bases = 1;
				if ( onclient )
				{
					strcpy( prefix, "C_" );
				}
				else
				{
					strcpy( prefix, "C" );
				}
			}
			else if ( !stricmp( com_token, "PREDICTABLE_CLASS_ALIASED_PREFIXED" ) )
			{
				// Nothing
			}
			else
			{
				vprint( 0, "PREDICTABLE_CLASS of unknown type!!! %s\n", com_token );
			}

			// parse the (
			current = CC_ParseToken( current );
			if ( !strcmp( com_token, "(" ) )
			{
				// Now the classname
				current = CC_ParseToken( current );
				if ( strlen( com_token ) > 0 )
				{
					//vprint( depth, "class %s\n", com_token );

					CClass *cl = AddClass( com_token );

					// Now see if there's a base class
					current = CC_ParseToken( current );
					if ( !stricmp( com_token, "," ) )
					{
						// Parse out public and then classname an
						current = CC_ParseToken( current );
						if ( strlen( com_token ) > 0 )
						{
							char basename[ 256 ];
							sprintf( basename, "%s%s", prefix, com_token );

							bool valid = true;

							if ( bases == 2 )
							{
								valid = false;

								current = CC_ParseToken( current );
								if ( !stricmp( com_token, "," ) )
								{
									current = CC_ParseToken( current );
									if ( strlen( com_token ) > 0 )
									{
										valid = true;
										if ( usebase == 1 )
										{
											sprintf( basename, "%s%s", prefix, com_token );
										}
									}
								}
							}

							if ( valid )
							{
								cl->SetBaseClass( basename );
								strcpy( cl->m_szTypedefBaseClass, basename );
							}
							
							do
							{
								current = CC_ParseToken( current );
							} while ( strlen( com_token ) && stricmp( com_token, ")" ) );

							if ( !stricmp( com_token, ")" ) )
							{
								current = cl->ParseClassDeclaration( current );
							}
						}
					}
					else if ( !stricmp( com_token, ")" ) )
					{
						current = cl->ParseClassDeclaration( current );
					}
				}
			}
		}
		else if ( !strcmp( com_token, "TYPEDESCRIPTION" ) || 
			    !strcmp( com_token, "typedescription_t" ) )
		{
			current = ParseTypeDescription( current, false );
		}
		else if ( !strcmp( com_token, "BEGIN_DATADESC" ) ||
				  !strcmp( com_token, "BEGIN_DATADESC_NO_BASE" ) ||
				  !strcmp( com_token, "BEGIN_SIMPLE_DATADESC" ) )
		{
			current = ParseTypeDescription( current, true );
		}
		else if ( !strcmp( com_token, "BEGIN_PREDICTION_DATA" ) ||
			!strcmp( com_token, "BEGIN_EMBEDDED_PREDDESC" ) )
		{
			current = ParsePredictionTypeDescription( current );
		}
		else if (	!strcmp( com_token, "BEGIN_RECV_TABLE" ) ||
					!strcmp( com_token, "BEGIN_RECV_TABLE_NOBASE" ) ||
					!strcmp( com_token, "IMPLEMENT_CLIENTCLASS_DT" ) ||
					!strcmp( com_token, "IMPLEMENT_CLIENTCLASS_DT_NOBASE" ) )
		{
			current = ParseReceiveTable( current );
		}
		else if ( !strcmp( com_token, "IMPLEMENT_PREDICTABLE_NODATA" ) )
		{
			current = CC_ParseToken( current );
			if ( !strcmp( com_token, "(" ) )
			{
				current = CC_ParseToken( current );

				CClass *cl = FindClass( com_token );
				if ( cl )
				{
					if ( cl->m_bHasPredictionData )
					{
						if ( !forcequiet )
						{
							vprint( 0, "Class %s declared predictable and implemented with IMPLEMENT_PREDICTABLE_NODATA in typedescription\n",
								cl->m_szName );
						}

						cl->m_bHasPredictionData = false;
					}
				}

				current = CC_ParseToken( current );
			}
		}

		current = CC_ParseToken( current );
	}

	COM_FreeFile( (unsigned char *)buffer );

	if ( !forcequiet && !GetQuiet() )
	{
		vprint( 0, " %s: headers (%i game / %i total)", (char *)&filename[ m_nOffset ], numheaders - skippedfiles, numheaders );
		if ( maxdepth > 1 )
		{
			vprint( 0, ", depth %i", maxdepth );
		}
		vprint( 0, "\n" );
	}

	m_nLinesOfCode += linesprocessed;
	linesprocessed = 0;
}
예제 #19
0
파일: veCov.cpp 프로젝트: NCIP/visda
/*******************************************************************
 Subroutine to compute the inverse matrix and determinant
   matrix *cov:        the pointer to the covariance matrix
   matrix *inv_cov:    the pointer to the inverse covariance matrix
   matrix *cov_mat:    the pointer to the approximate covariance matrix
                       when singular. If unsingular, it equals to cov
   double *det_cov:    the pointer to determinant

 return value: '1' - successfully exit
               '0' - exit with waring/error
*******************************************************************/
int veCov(matrix *cov, matrix *inv_cov, matrix *cov_mat, 
		  double *det_cov)
{
	int i; //, j;
	matrix eigvec_re;
	matrix eigvec_im;
	vector eigval_re;
	vector eigval_im;
    int *eig_order;
	int eig_info;
	int num_v;  // the number of eigenvalue
	int rank_c; 
	double sum_v;
	double factor = 0.02;
	double ass_value;
	double min_real;

    mnew(&eigvec_re, cov->m, cov->n);
    mnew(&eigvec_im, cov->m, cov->n);
	vnew(&eigval_re, cov->n);
	vnew(&eigval_im, cov->n);
    eig_order = new int[cov->n];
    

    // the eigenvector and eigenvalue of covariance matrix
    eig_info = eig(cov, &eigvec_re, &eigvec_im, &eigval_re, &eigval_im);
	#ifdef _DEBUG
	printf("\n original eigenval_re = \n");
	vprint(&eigval_re);
	printf("\n original eigenval_im = \n");
	vprint(&eigval_im);
	printf("\n original eigenvec_re = \n");
	mprint(&eigvec_re);
	printf("\n original eigenvec_im = \n");
	mprint(&eigvec_im);
	#endif

	if (!eig_info) {
		printf(" The eigenvalue computation failed! \n");
		return 0;
		//....
	}
	
	// the rank of covariance matrix
	num_v = cov->n;

	rank_c = rank(cov, TOLERANCE);

	// compute the inverse and determinate
    if (rank_c == num_v) {  // nonsingular
		inv(cov, inv_cov);
		mcopy(cov, cov_mat);
		*det_cov = det(cov);
	
	} else {  // singular
		min_real = pow(10, (((double)-250) / ((double) cov->m)));

		/*for (i=0; i<num_v; i++) {
			if ((*(eigval_re.pr+i) < ZEROTHRESH) || (*(eigval_im.pr+i) != 0)) {
				*(eigval_re.pr+i) = 0;  // ???? keep the real part of complex or not
				*(eigval_im.pr+i) = 0;
			}
		}
		sort(&eigval_re, eig_order, 'd'); */

		for (i=0; i<num_v; i++) {
			// when negtive real eigenvalue, change to absolute value
			//   to ensure all the real eigenvalues are positive
			if ((eigval_re.pr[i] < 0) && (eigval_im.pr[i] == 0)) {
				//eigval_re.pr[i] *= -1;
				eigval_re.pr[i] = 0;
			}
		}

		// sort real eigenvalues descendingly, put complex ones at the end
		sorteig(&eigval_re, &eigval_im, eig_order);

		sum_v = 0;
		for (i=0; i<rank_c; i++) {
			sum_v += *(eigval_re.pr+i);
		}
		//for (i=rank_c; i<num_v; i++) {
		//		*(eigval_re.pr+i) = 0;
		//		*(eigval_im.pr+i) = 0;
		//}

		//sum_v = vsum(&eigval_re);

		ass_value = factor * sum_v / (num_v - rank_c);

		if (ass_value < (0.5 * (*(eigval_re.pr+rank_c)) * (1 - factor))) {
			if (ass_value > min_real) {
				for (i=rank_c; i<num_v; i++) {
					*(eigval_re.pr+i) = ass_value;
				}
				for (i=0; i<rank_c; i++) {
					*(eigval_re.pr+i) *= 1 - factor;
				}
			} else {
				for (i=rank_c; i<num_v; i++) {
					*(eigval_re.pr+i) = min_real;
				}
			}
		} else {
			ass_value = 0.5 * (*(eigval_re.pr+rank_c)) * (1 - factor);
			if (ass_value > min_real) {
				for (i=rank_c; i<num_v; i++) {
					*(eigval_re.pr+i) = ass_value;
				}
				for (i=0; i<rank_c; i++) {
					*(eigval_re.pr+i) = *(eigval_re.pr+i) - ass_value * (num_v - rank_c) * (*(eigval_re.pr+i)) / sum_v;
				}
			} else {
				for (i=rank_c; i<num_v; i++) {
					*(eigval_re.pr+i) = min_real;
				}
			}
		}
        
		matrix eigvec_re_sorted;
		matrix eigvec_re_sorted_t;
		mnew(&eigvec_re_sorted, num_v, num_v);
		mnew(&eigvec_re_sorted_t, num_v, num_v);

		sortcols(eig_order, &eigvec_re, &eigvec_re_sorted);
		transpose(&eigvec_re_sorted, &eigvec_re_sorted_t); 

		#ifdef _DEBUG
		printf("\n modified eigenval_re = \n");
		vprint(&eigval_re);
		printf("\n modified eigenval_im = \n");
		vprint(&eigval_im);
		printf("\n modified eigenvec_re = \n");
		mprint(&eigvec_re_sorted);
		#endif

		matrix inv_eig_vl_s;
		mnew(&inv_eig_vl_s, num_v, num_v);
		for (i=0; i<num_v; i++) {
			*(inv_eig_vl_s.pr + i*num_v + i) = 1 / (*(eigval_re.pr+i));    
		}
		
		matrix tmp;
		mnew(&tmp, num_v, num_v);

		mmMul(&eigvec_re_sorted, &inv_eig_vl_s, &tmp);
		mmMul(&tmp, &eigvec_re_sorted_t, inv_cov);

		matrix diag_eigval;
		mnew(&diag_eigval, num_v, num_v);
		for (i=0; i<num_v; i++) {
			*(diag_eigval.pr + i*num_v + i) = *(eigval_re.pr+i);    
		}
		mmMul(&eigvec_re_sorted, &diag_eigval, &tmp);
		mmMul(&tmp, &eigvec_re_sorted_t, cov_mat);

		*det_cov = 1;
		for (i=0; i<num_v; i++) {
			*det_cov = (*det_cov) * (*(eigval_re.pr+i)); 
		}

		mdelete(&inv_eig_vl_s);
		mdelete(&eigvec_re_sorted);
		mdelete(&eigvec_re_sorted_t);
		mdelete(&tmp);
		mdelete(&diag_eigval);

	}

	#ifdef _DEBUG
	printf("\n rank = %d \n", rank_c);
	printf("\n det_cov = %e \n", *det_cov);
	printf("\n inv_cov = \n");
	mprint(inv_cov);
	printf("\n cov_mat = \n");
	mprint(cov_mat);
	#endif

    mdelete(&eigvec_re);
	mdelete(&eigvec_im);
	vdelete(&eigval_re);
	vdelete(&eigval_im);
    delete []eig_order;

    return 1;
}
예제 #20
0
void CCodeProcessor::PrintMissingTDFields( void ) const
{
	int classcount;
	int fieldcount;
	int c;

	CClass *cl;

	if ( GetPrintTDs() )
	{
		classcount = 0;
		fieldcount = 0;
		cl = m_pClassList;
		while ( cl )
		{
			if ( cl->m_bDerivedFromCBaseEntity || cl->m_bHasSaveRestoreData )
			{
				if ( cl->CheckForMissingTypeDescriptionFields( c ) )
				{
					classcount++;
					fieldcount += c;
				}
			}
			cl = cl->m_pNext;
		}

		if ( fieldcount )
		{
			vprint( 0, "\nSummary:  %i fields missing from %i classes\n", fieldcount, classcount );
		}
		else
		{
			if ( !classcount )
			{
				vprint( 0, "\nSummary:  no saverestore info present\n");
			}
			else
			{
				vprint( 0, "\nSummary:  no errors for %i classes\n", classcount );
			}
		}

		vprint( 0, "\n" );
	}

	if ( GetPrintPredTDs() )
	{
		//Now check prediction stuff
		classcount = 0;
		fieldcount = 0;
		cl = m_pClassList;
		while ( cl )
		{
			if ( cl->m_bDerivedFromCBaseEntity || cl->m_bHasPredictionData )
			{
				if ( cl->CheckForMissingPredictionFields( c, false ) )
				{
					classcount++;
					fieldcount += c;
				}
			}
			cl = cl->m_pNext;
		}

		if ( fieldcount )
		{
			vprint( 0, "\nSummary:  %i prediction fields missing from %i classes\n", fieldcount, classcount );
		}
		else
		{
			if ( !classcount )
			{
				vprint( 0, "\nSummary:  no prediction info present\n");
			}
			else
			{
				vprint( 0, "\nSummary:  no errors for %i predictable classes\n", classcount );
			}
		}

		vprint( 0, "\n" );
	}

	if ( GetPrintCreateMissingTDs() )
	{
		//Now check prediction stuff
		classcount = 0;
		fieldcount = 0;
		cl = m_pClassList;
		while ( cl )
		{
			if ( cl->m_bDerivedFromCBaseEntity || cl->m_bHasSaveRestoreData )
			{
				if ( cl->CheckForMissingTypeDescriptionFields( c, true ) )
				{
					classcount++;
					fieldcount += c;
				}
			}
			cl = cl->m_pNext;
		}

		if ( fieldcount )
		{
			vprint( 0, "\nSummary:  %i saverestore fields missing from %i classes\n", fieldcount, classcount );
		}
		else
		{
			if ( !classcount )
			{
				vprint( 0, "\nSummary:  no saverestore info present\n");
			}
			else
			{
				vprint( 0, "\nSummary:  no errors for %i classes\n", classcount );
			}
		}

		vprint( 0, "\n" );
	}

	if ( GetPrintCreateMissingPredTDs() )
	{
		//Now check prediction stuff
		classcount = 0;
		fieldcount = 0;
		cl = m_pClassList;
		while ( cl )
		{
			if ( cl->m_bDerivedFromCBaseEntity || cl->m_bHasPredictionData )
			{
				if ( cl->CheckForMissingPredictionFields( c, true ) )
				{
					classcount++;
					fieldcount += c;
				}
			}
			cl = cl->m_pNext;
		}

		if ( fieldcount )
		{
			vprint( 0, "\nSummary:  %i prediction fields missing from %i classes\n", fieldcount, classcount );
		}
		else
		{
			if ( !classcount )
			{
				vprint( 0, "\nSummary:  no prediction info present\n");
			}
			else
			{
				vprint( 0, "\nSummary:  no errors for %i predictable classes\n", classcount );
			}
		}

		vprint( 0, "\n" );
	}

	// Now check for things that are in the prediction TD but not marked correctly as being part of the sendtable
	{
		//Now check prediction stuff
		classcount = 0;
		fieldcount = 0;
		cl = m_pClassList;
		while ( cl )
		{
			if ( cl->m_bDerivedFromCBaseEntity || cl->m_bHasPredictionData )
			{
				if ( cl->CheckForPredictionFieldsInRecvTableNotMarkedAsSuchCorrectly( c ) )
				{
					classcount++;
					fieldcount += c;
				}
			}
			cl = cl->m_pNext;
		}

		vprint( 0, "\n" );
	}

	// Print stuff derived from CBaseEntity that doesn't have save/restore data
	vprint( 0, "\nMissing DATADESC tables:\n\n" );
	cl = m_pClassList;
	while ( cl )
	{
		if ( cl->m_bDerivedFromCBaseEntity && !cl->m_bHasSaveRestoreData && cl->m_nVarCount )
		{
			vprint( 0, "\t%s\n", cl->m_szName );
		}
		cl = cl->m_pNext;
	}
	vprint( 0, "\n" );
}
예제 #21
0
/* Write the server config as properties associated with the dataset */
int zfs_write_ldd(struct mkfs_opts *mop)
{
	struct lustre_disk_data *ldd = &mop->mo_ldd;
	char *ds = mop->mo_device;
	zfs_handle_t *zhp;
	int ret = EINVAL;

	if (osd_check_zfs_setup() == 0)
		return EINVAL;

	zhp = zfs_open(g_zfs, ds, ZFS_TYPE_FILESYSTEM);
	if (zhp == NULL) {
		fprintf(stderr, "Failed to open zfs dataset %s\n", ds);
		goto out;
	}

	vprint("Writing %s properties\n", ds);

	ret = zfs_set_prop_int(zhp, LDD_VERSION_PROP, ldd->ldd_config_ver);
	if (ret)
		goto out_close;

	ret = zfs_set_prop_int(zhp, LDD_FLAGS_PROP, ldd->ldd_flags);
	if (ret)
		goto out_close;

	ret = zfs_set_prop_int(zhp, LDD_INDEX_PROP, ldd->ldd_svindex);
	if (ret)
		goto out_close;

	ret = zfs_set_prop_str(zhp, LDD_FSNAME_PROP, ldd->ldd_fsname);
	if (ret)
		goto out_close;

	ret = zfs_set_prop_str(zhp, LDD_SVNAME_PROP, ldd->ldd_svname);
	if (ret)
		goto out_close;

	ret = zfs_set_prop_str(zhp, LDD_UUID_PROP, (char *)ldd->ldd_uuid);
	if (ret)
		goto out_close;

	ret = zfs_set_prop_str(zhp, LDD_USERDATA_PROP, ldd->ldd_userdata);
	if (ret)
		goto out_close;

	ret = zfs_set_prop_str(zhp, LDD_MOUNTOPTS_PROP, ldd->ldd_mount_opts);
	if (ret)
		goto out_close;

	if (strlen(ldd->ldd_params) > ZAP_MAXVALUELEN) {
		ret = E2BIG;
		goto out_close;
	}
	ret = zfs_set_prop_str(zhp, LDD_PARAMS_PROP, ldd->ldd_params);

out_close:
	zfs_close(zhp);
out:
	return ret;
}
예제 #22
0
void CCodeProcessor::PrintClassList( void ) const
{
	if ( GetPrintHierarchy() )
	{
		vprint( 0, "\nClass Summary\n\n" );
	}

	CClass *cl = m_pClassList;
	
	while ( cl )
	{
		if ( cl->m_bDerivedFromCBaseEntity )
		{
			bool missing = false;
			char missingwarning[ 128 ];

			missingwarning[0]=0;
			if ( cl->m_szTypedefBaseClass[0] )
			{
				if ( stricmp( cl->m_szBaseClass, cl->m_szTypedefBaseClass ) )
				{
					vprint( 0, "class %s has incorrect typedef %s BaseClass\n", cl->m_szName, cl->m_szTypedefBaseClass );
				}
			}
			else if ( cl->m_szBaseClass[ 0 ] )
			{
				missing = true;
				sprintf( missingwarning, ", missing typedef %s BaseClass", cl->m_szBaseClass );
			}

			if ( GetPrintHierarchy() || missing )
			{
				vprint( 0, "class %s%s\n", cl->m_szName, missing ? missingwarning : "" );
			}

			int level = 1;
			CClass *base = cl->m_pBaseClass;
			while ( base )
			{
				if ( GetPrintHierarchy() )
				{
					vprint( level++, "public %s\n", base->m_szName );
				}
				base = base->m_pBaseClass;
			}

			int i; 

			if ( GetPrintHierarchy() && GetPrintMembers() )
			{

				if ( cl->m_nMemberCount )
				{
					vprint( 1, "\nMember functions:\n\n" );
				}

				for ( i = 0; i < cl->m_nMemberCount; i++ )
				{
					CClassMemberFunction *member = cl->m_Members[ i ];

					if ( member->m_szType[0] )
					{
						vprint( 1, "%s %s();\n", member->m_szType, member->m_szName );
					}
					else
					{
						char *p = member->m_szName;
						if ( *p == '~' )
							p++;

						if ( stricmp( p, cl->m_szName ) )
						{
							vprint( 0, "class %s has member function %s with no return type!!!\n",
								cl->m_szName, member->m_szName );
						}
						vprint( 1, "%s();\n", member->m_szName );
					}
				}

				if ( cl->m_nVarCount )
				{
					vprint( 1, "\nMember Variables\n\n" );
				}

				
				for ( i = 0; i < cl->m_nVarCount; i++ )
				{
					CClassVariable *var = cl->m_Variables[ i ];

					if ( var->m_bIsArray )
					{
						if ( var->m_szArraySize[0]==0 )
						{
							vprint( 1, "%s %s[];\n", var->m_szType, var->m_szName );
						}
						else
						{
							vprint( 1, "%s %s[ %s ];\n", var->m_szType, var->m_szName, var->m_szArraySize );
						}
					}
					else
					{
						vprint( 1, "%s %s;\n", var->m_szType, var->m_szName );
					}
				}

				if ( cl->m_nTDCount )
				{
					vprint( 1, "\nSave/Restore TYPEDESCRIPTION\n\n" );
				}

				for ( i = 0; i < cl->m_nTDCount; i++ )
				{
					CTypeDescriptionField *td = cl->m_TDFields[ i ];
					if ( td->m_bCommentedOut )
					{
						vprint( 1, "// " );
					}
					else
					{
						vprint( 1, "" );
					}

					vprint( 0, "%s( %s, %s, %s, ... )\n", td->m_szDefineType, cl->m_szName, td->m_szVariableName, td->m_szType );
				}

				if ( !cl->m_bHasSaveRestoreData )
				{
				//	vprint( 1, "\nSave/Restore TYPEDESCRIPTION not specified for class\n\n" );
				}

				if ( cl->m_nPredTDCount )
				{
					vprint( 1, "\nPrediction TYPEDESCRIPTION\n\n" );
				}

				for ( i = 0; i < cl->m_nPredTDCount; i++ )
				{
					CTypeDescriptionField *td = cl->m_PredTDFields[ i ];
					if ( td->m_bCommentedOut )
					{
						vprint( 1, "// " );
					}
					else
					{
						vprint( 1, "" );
					}

					vprint( 0, "%s( %s, %s, %s, ... )\n", td->m_szDefineType, cl->m_szName, td->m_szVariableName, td->m_szType );
				}

				if ( !cl->m_bHasPredictionData )
				{
				//	vprint( 1, "\nPrediction TYPEDESCRIPTION not specified for class\n\n" );
				}
			}
		
			if ( GetPrintHierarchy() )
			{
				vprint( 0, "\n" );
			}
		}

		cl = cl->m_pNext;
	}
}
예제 #23
0
파일: veSubEM.cpp 프로젝트: NCIP/visda
/*******************************************************************
 Subroutine to do the EM algorithm
   matrix *D:       the pointer to the matrix data
   matrix *mean0_x: the pointer to a matrix containing the initial Means of clusters
   vector *w0:		the pointer to a vector containing the initial mixing proportion of clusters
   double vv:       the value for initializing the Covariance matrix of clusters
   double error:    the error threshold
   vector *Zjk_up:  the pointer to a vector containing Posterior probabilities of the up-level 
                         cluster samples
   matrix *mean1_x: the pointer to a matrix containing the Means of clusters in t-space
   vector *w0_t:	the pointer to a vector containing the mixing proportions of the identified 
                         clusters in t-space
   matrix *cov_mat: the pointer to a group of matrixs containing the Covariance
                         matrix of clusters in t-space
   matrix *Zjk:     the pointer to a matrix containing Posterior probabilities of all samples 
                         belonging to all the sub-level clusters, each column is for one cluster.
   
 return value: '1' - successfully exit
               '0' - exit with waring/error
*******************************************************************/
int veSubEM(matrix *D, matrix *mean0_x, vector *w0, double vv, double error, vector *Zjk_up, //input
			matrix *mean1_x, vector *w0_t, matrix *cov_mat, matrix *Zjk)  //output
{
	int k0, kc, n, p;
	int i, j, k, u, s;
	matrix *Var0;
	matrix Gxn;
	vector Fx;
	matrix MUK;
	matrix MU1;
	int zeroFx_num = 1;
	//double error = 0.01;
	double err = error + (double)1;
	vector Zjk_temp;

	n = D->m;
	p = D->n;
	k0 = mean0_x->m;
	kc = mean0_x->n;
	
	Var0 = new matrix[k0];
	for(i=0; i<k0; i++) {
		mnew(Var0+i, p, p);
	}
	mnew(&Gxn, n, k0);
	vnew(&Fx, n);
	vnew(&Zjk_temp, n);
	mnew(&MUK, k0, p);
	mcopy(mean0_x, &MUK);
	mnew(&MU1, k0, p);

	vector D_j;
	vector Zjk_k;
	double sum_tmp = 0;
	matrix Ck;
	vector D_i;
	vector MUK_k;
	vector cen_D_i;
	matrix mtmp;
	vector vtmp;

	vnew(&D_j, n);
	vnew(&Zjk_k, n);
	mnew(&Ck, p, p);
	vnew(&D_i, p);
	vnew(&MUK_k, p);
	vnew(&cen_D_i, p);
	mnew(&mtmp, p, p);
	vnew(&vtmp, n);

	//Initializing the parameters of mixture of Gaussians
	//Initinalize the covariance matrix
	//Use EM algorithm to perform the local training.
	
	//Test intialization of covarinace matrix 
	//printf("Testing covariance matrix initialization... \n");

	while (zeroFx_num != 0) {
		for(i=0; i<k0; i++) {
			meye(Var0+i);
			for (j=0; j<p; j++) {
				*((Var0+i)->pr+j*p+j) = vv;
			}
		}
	
		veModel(D, mean0_x, Var0, w0, &Gxn, &Fx);
		//printf("\n Gxn = :\n");
		//mprint(&Gxn);
		//printf("\n Fx = :\n");
		//vprint(&Fx);

		zeroFx_num = 0;
		for (i=0; i<n; i++) {
			if (*(Fx.pr+i) == 0) {
				zeroFx_num++;
			}
		}

		vv *= 2;
	
	}

	vones(&Zjk_temp);

	//printf("\n EM in t-space starts ... \n");
	//printf("\n Data = \n");
	//mprint(D);

	int l = 0;
	while (err > error) {
		
		#ifdef _DEBUG
		printf(" \n...... in EM loop %d ......\n", ++l);

		printf("\n L%d: w0 = \n", l);
		vprint(w0);
		printf("\n L%d: MUK = \n", l);
		mprint(&MUK);
		printf("\n L%d: Var0 = \n", l);
		for(i=0; i<k0; i++) {
			mprint(Var0+i);
			printf("\n");
		}
		printf("\n L%d: Zjk = \n", l);
		mprint(Zjk);
		#endif

		veModel(D, &MUK, Var0, w0, &Gxn, &Fx);
		
		#ifdef _DEBUG
		printf("\n L%d: Gxn = \n", l);
		mprint(&Gxn);
		printf("\n L%d: Fx = \n", l);
		vprint(&Fx);
		#endif

		for (k=0; k<k0; k++) {
			u = k*p;

			double zz = 0;
			double zz_up = 0;
			for (i=0; i<n; i++) {
				*(Zjk->pr+i*k0+k) = (*(w0->pr+k)) * Zjk_up->pr[i] * (*(Gxn.pr+i*k0+k)) / (*(Fx.pr+i));
				zz += *(Zjk->pr+i*k0+k);
				zz_up += Zjk_up->pr[i];
			}
			*(w0->pr+k) = zz/zz_up;

			for (j=0; j<p; j++) {
				getcolvec(D, j, &D_j);
				getcolvec(Zjk, k, &Zjk_k);
				sum_tmp = 0;
				for (i=0; i<n; i++) {
					sum_tmp += (*(Zjk_k.pr+i)) * (*(D_j.pr+i));
				}
				*(MU1.pr+u+j) = sum_tmp / zz;
			}

			mzero(&Ck);
			for (i=0; i<n; i++) {
				getrowvec(D, i, &D_i);
				getrowvec(&MUK, k, &MUK_k);
				for (j=0; j<p; j++) {
					*(cen_D_i.pr+j) = *(D_i.pr+j) - *(MUK_k.pr+j);
				}

				vvMul(&cen_D_i, &cen_D_i, &mtmp);
				
				for (j=0; j<p; j++) {
					for (s=0; s<p; s++) {
						*(Ck.pr+j*p+s) += (*(Zjk->pr+i*k0+k)) * (*(mtmp.pr+j*p+s));
					}
				}
			}
			for (j=0; j<p; j++) {
				for (s=0; s<p; s++) {
					*(Var0[k].pr+j*p+s) = (*(Ck.pr+j*p+s)) / zz;
				}
			}
		}   // for (k...

		mcopy(&MU1, &MUK);

		for (i=0; i<n; i++) {
			*(vtmp.pr+i) = fabs(*(Zjk_k.pr+i) - *(Zjk_temp.pr+i));
		}
		err = vmean(&vtmp);
		vcopy(&Zjk_k, &Zjk_temp);
		
		
    }  // while

	vcopy(w0, w0_t);
	mcopy(&MUK, mean1_x);
	for(i=0; i<k0; i++) {
		mcopy(Var0+i, cov_mat+i);
	}

	for(i=0; i<k0; i++) {
		mdelete(Var0+i);
	} 
	mdelete(&Gxn);
	vdelete(&Fx);
	vdelete(&Zjk_temp);
	mdelete(&MUK);
	mdelete(&MU1);
    vdelete(&D_j);
	vdelete(&Zjk_k);
	mdelete(&Ck);
	vdelete(&D_i);
	vdelete(&MUK_k);
	vdelete(&cen_D_i);
	mdelete(&mtmp);
	vdelete(&vtmp);

    return 1;
}
예제 #24
0
char *CCodeProcessor::ParseTypeDescription( char *current, bool fIsMacroized )
{
	// Next token is classname then :: then variablename then braces then = then {
	char classname[ 256 ];
	char variablename[ 256 ];

	if ( !fIsMacroized )
	{
		current = CC_ParseToken( current );
		if ( strlen( com_token ) <= 0 )
			return current;

		strcpy( classname, com_token );
		if ( classname[0]=='*' )
			return current;

		current = CC_ParseToken( current );
		if (stricmp( com_token, ":" ) )
		{
			return current;
		}

		current = CC_ParseToken( current );
		assert( !stricmp( com_token, ":" ) );

		current = CC_ParseToken( current );
		if ( strlen( com_token ) <= 0 )
			return current;

		strcpy( variablename, com_token );
	}
	else
	{
		current = CC_ParseToken( current );
		if (stricmp( com_token, "(" ) )
		{
			return current;
		}

		current = CC_ParseToken( current );
		if ( strlen( com_token ) <= 0 )
			return current;

		strcpy( classname, com_token );
		if ( classname[0]=='*' )
			return current;

		current = CC_ParseToken( current );
		if (stricmp( com_token, ")" ) )
		{
			return current;
		}

		// It's macro-ized
		strcpy( variablename, "m_DataDesc" );
	}
	if ( !fIsMacroized )
	{
		char ch;
		current = CC_RawParseChar( current, "{", &ch );
		assert( ch == '{' );
		if ( strlen( com_token ) <= 0 )
			return current;
	}

	com_ignoreinlinecomment = true;
	bool insidecomment = false;

	// Now parse typedescription line by line
	while ( 1 )
	{
		current = CC_ParseToken( current );
		if ( strlen( com_token ) <= 0 )
			break;

		// Go to next line
		if ( !stricmp( com_token, "," ) )
			continue;

		// end
		if ( !fIsMacroized )
		{
			if ( !stricmp( com_token, "}" ) )
				break;
		}
		else
		{
			if ( !stricmp( com_token, "END_DATADESC" ) )
				break;
		}

		// skip #ifdef's inside of typedescs
		if ( com_token[0]=='#' )
		{
			current = CC_ParseUntilEndOfLine( current );
			continue;
		}

		if ( !stricmp( com_token, "/" ) )
		{
			current = CC_ParseToken( current );
			if ( !stricmp( com_token, "/" ) )
			{
				// There are two styles supported. One is to have the member definition present but commented out:
				//		DEFINE_FIELD( CMyClass, m_member, FIELD_INTEGER ),
				// the other is to have a comment where the first token of the comment is a member name:
				//		m_member
				current = CC_ParseToken( current );
				if ( !strnicmp( com_token, "DEFINE_", 7 ) )
				{
					CC_UngetToken();
					insidecomment = true;
				}
				else
				{
					char commentedvarname[ 256 ];
					strcpy( commentedvarname, com_token );

					CClass *cl = FindClass( classname );
					if ( cl )
					{
						if ( !cl->FindTD( commentedvarname ) )
						{
							cl->AddTD( commentedvarname, "", "", true );
						}
						// Mark that it has a data table
						cl->m_bHasSaveRestoreData = true;
					}
					current = CC_ParseUntilEndOfLine( current );
				}
				continue;
			}
		}

		com_ignoreinlinecomment = false;

		// Parse a typedescription line
		char definetype[ 256 ];
		strcpy( definetype, com_token );

		current = CC_ParseToken( current );
		if ( stricmp( com_token, "(" ) )
			break;

		// skip classname
		current = CC_ParseToken( current );
		if( stricmp( com_token, classname ) )
		{
			vprint( 0, "TYPEDESCRIPTION for class %s uses offset from class %s\n",
				classname, com_token );
		}
		// skip comma
		current = CC_ParseToken( current );
		if ( !stricmp( com_token, ":" ) )
		{
			// Scoped class name here...
			current = CC_ParseToken( current );
			assert( !stricmp( com_token, ":" ) );
			current = CC_ParseToken( current );

			// skip comma
			current = CC_ParseToken( current );
		}

		char varname[ 256 ];
		current = CC_ParseToken( current );

		strcpy( varname, com_token );

		
		char vartype[ 256 ];

		vartype[0]=0;

		if ( !stricmp( definetype, "DEFINE_FUNCTION" ) ||
			!stricmp( definetype, "DEFINE_THINKFUNC" ) ||
			!stricmp( definetype, "DEFINE_ENTITYFUNC" ) ||
			!stricmp( definetype, "DEFINE_USEFUNC" ) ||
			!stricmp( definetype, "DEFINE_OUTPUT" ) ||
			!stricmp( definetype, "DEFINE_INPUTFUNC" ) )
		{
			strcpy( vartype, "funcptr" );
		}
		else if ( !stricmp(definetype, "DEFINE_FIELD") || 
			!stricmp(definetype, "DEFINE_KEYFIELD") || 
			!stricmp(definetype, "DEFINE_KEYFIELD_NOT_SAVED") || 
			!stricmp(definetype, "DEFINE_UTLVECTOR") || 
			!stricmp(definetype, "DEFINE_GLOBAL_FIELD") || 
			!stricmp(definetype, "DEFINE_GLOBAL_KEYFIELD") || 
			!stricmp(definetype, "DEFINE_INPUT") ||
			!stricmp(definetype, "DEFINE_AUTO_ARRAY") ||
			!stricmp(definetype, "DEFINE_AUTO_ARRAY2D") ||
			!stricmp(definetype, "DEFINE_ARRAY") )
		{
			// skip comma
			current = CC_ParseToken( current );
			if (!strcmp( com_token, "[" ))
			{
				// Read array...
				current = CC_ParseToken( current );
				strcat( varname, "[" );
				strcat( varname, com_token );
				current = CC_ParseToken( current );
				assert (!strcmp( com_token, "]" ));
 				strcat( varname, "]" );

				// skip comma
				current = CC_ParseToken( current );
			}

			current = CC_ParseToken( current );

			strcpy( vartype, com_token );
		}

		// Jump to end of definition
		int nParenCount = 1;
		do
		{
			current = CC_ParseToken( current );
			if ( strlen( com_token ) <= 0 )
				break;

			if ( !stricmp( com_token, "(" ) )
			{
				++nParenCount; 
			}
			else if ( !stricmp( com_token, ")" ) )
			{
				if ( --nParenCount == 0 )
				{
					break;
				}
			}

		} while ( 1 );

//		vprint( 2, "%s%s::%s %s %s %s\n",
//			insidecomment ? "// " : "",
//			classname, variablename,
//			definetype, varname, vartype );

		CClass *cl = FindClass( classname );
		if ( cl )
		{
			if ( strcmp( vartype, "funcptr" ) && cl->FindTD( varname ) )
			{
				vprint( 0, "class %s::%s already has typedescription entry for field %s\n", classname, variablename, varname );
			}
			else
			{
				cl->AddTD( varname, vartype, definetype, insidecomment );
			}
			// Mark that it has a data table
			cl->m_bHasSaveRestoreData = true;
		}
		insidecomment = false;
		com_ignoreinlinecomment = true;
	}

	com_ignoreinlinecomment = false;

	return current;
}
void BuildFileList_R( int depth, CUtlVector< FileEntry >& files, CUtlVector< FileEntry > * otherfiles, char const *dir, char const *wild, int skipchars )
{
	WIN32_FIND_DATA wfd;

	char directory[ 256 ];
	char filename[ 256 ];
	HANDLE ff;

	bool canrecurse = true;
	if ( !Q_stricmp( wild, "..." ) )
	{
		canrecurse = true;
		sprintf( directory, "%s%s%s", dir[0] == '\\' ? dir + 1 : dir, dir[0] != 0 ? "\\" : "", "*.*" );
	}
	else
	{
		sprintf( directory, "%s%s%s", dir, dir[0] != 0 ? "\\" : "", wild );
	}
	int dirlen = Q_strlen( dir );

	if ( ( ff = FindFirstFile( directory, &wfd ) ) == INVALID_HANDLE_VALUE )
		return;

	do
	{
		if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
		{
			bool useOtherFiles = false;

			if ( wfd.cFileName[ 0 ] == '.' )
				continue;

			if ( depth == 0 && !ShouldCheckDir( wfd.cFileName ) && otherfiles )
			{
				if ( !ShouldIgnoreDir( wfd.cFileName ) )
				{
					useOtherFiles = true;
				}
			}

			if ( !canrecurse )
				continue;

			// Recurse down directory
			if ( dir[0] )
			{
				sprintf( filename, "%s\\%s", dir, wfd.cFileName );
			}
			else
			{
				sprintf( filename, "%s", wfd.cFileName );
			}
			BuildFileList_R( depth + 1, useOtherFiles ? *otherfiles: files, NULL, filename, wild, skipchars );
		}
		else
		{
			if (!stricmp(wfd.cFileName, "vssver.scc"))
				continue;

			char filename[ MAX_PATH ];
			if ( dirlen <= skipchars )
			{
				Q_snprintf( filename, sizeof( filename ), "%s", wfd.cFileName );
			}
			else
			{
				Q_snprintf( filename, sizeof( filename ), "%s\\%s", &dir[ skipchars ], wfd.cFileName );
			}
			_strlwr( filename );

			Q_FixSlashes( filename );

			UnusedContent::CUtlSymbol sym = g_Analysis.symbols.AddString( filename );

			FileEntry entry;
			entry.sym = sym;
			int size = g_pFileSystem->Size( filename );
			entry.size = size >= 0 ? (unsigned int)size : 0;

			files.AddToTail( entry );

			if ( !( files.Count() % 3000 ) )
			{
				vprint( 0, "...found %i files\n", files.Count() );
			}
		}
	} while ( FindNextFile( ff, &wfd ) );
}
예제 #26
0
char *CCodeProcessor::ParseReceiveTable( char *current )
{
	// Next token is open paren, then classname close paren, then {
	char classname[ 256 ];

	current = CC_ParseToken( current );
	if (stricmp( com_token, "(" ) )
	{
		return current;
	}

	current = CC_ParseToken( current );
	if ( strlen( com_token ) <= 0 )
		return current;

	strcpy( classname, com_token );
	if ( classname[0]=='*' )
		return current;
	if ( !strcmp( classname, "className" ) )
		return current;
	if ( !strcmp( classname, "clientClassName" ) )
		return current;

	CClass *cl = FindClass( classname );
	if ( cl )
	{
		cl->m_bHasRecvTableData = true;
	}

	CClass *leafClass = cl;

	// parse until end of line
	current = CC_ParseUntilEndOfLine( current );

	// Now parse recvtable entries line by line
	while ( 1 )
	{
		cl = leafClass;

		current = CC_ParseToken( current );
		if ( strlen( com_token ) <= 0 )
			break;

		// Go to next line
		if ( !stricmp( com_token, "," ) )
			continue;

		// end
		if ( !stricmp( com_token, "END_RECV_TABLE" ) )
			break;

		// skip #ifdef's inside of recv tables
		if ( com_token[0]=='#' )
		{
			current = CC_ParseUntilEndOfLine( current );
			continue;
		}

		// Parse recproxy line
		char recvproptype[ 256 ];
		strcpy( recvproptype, com_token );

		if ( strnicmp( recvproptype, "RecvProp", strlen( "RecvProp" ) ) )
		{
			current = CC_ParseUntilEndOfLine( current );
			continue;
		}

		if ( !strcmp( recvproptype, "RecvPropArray" ) )
		{
			current = CC_ParseToken( current );
			if ( stricmp( com_token, "(" ) )
				break;

			current = CC_ParseToken( current );
			if ( strnicmp( recvproptype, "RecvProp", strlen( "RecvProp" ) ) )
			{
				current = CC_ParseUntilEndOfLine( current );
				continue;
			}
		}

		current = CC_ParseToken( current );
		if ( stricmp( com_token, "(" ) )
			break;

		// Read macro or fieldname
		current = CC_ParseToken( current );

		char varname[ 256 ];

		if ( !strnicmp( com_token, "RECVINFO", strlen( "RECVINFO" ) ) )
		{
			current = CC_ParseToken( current );
			if ( stricmp( com_token, "(" ) )
				break;
			current = CC_ParseToken( current );
		}
		else
		{
			current = CC_ParseUntilEndOfLine( current );
			continue;
		}

		strcpy( varname, com_token );

		current = CC_ParseUntilEndOfLine( current );

		if ( cl )
		{
			// Look up the var
			CClassVariable *classVar = cl->FindVar( varname, true );
			if ( classVar )
			{
				classVar->m_bInRecvTable = true;
			}
			else
			{
				char cropped[ 256 ];
				char root[ 256 ];
				strcpy( cropped, varname );

				while ( 1 )
				{
					// See if varname is an embedded var
					char *spot = strstr( cropped, "." );
					if ( spot )
					{
						strcpy( root, cropped );
						root[ spot - cropped ] = 0;
						strcpy( cropped, spot + 1 );

						classVar = cl->FindVar( root, true );	
					}
					else
					{
						classVar = cl->FindVar( cropped, true );
						break;
					}

					if ( classVar )
						break;
				}

				if ( !classVar )
				{
					vprint( 0, "class %s::%s missing, but referenced by RecvTable!!!\n", classname, varname );
				}
				else
				{
					classVar->m_bInRecvTable = true;
				}
			}
		}
		else
		{
			vprint( 0, "class %s::%s found in RecvTable, but no such class is known!!!\n", classname, varname );
		}
	}

	return current;
}
예제 #27
0
파일: mount_utils.c 프로젝트: DCteam/lustre
int get_mountdata(char *dev, struct lustre_disk_data *mo_ldd)
{

        char tmpdir[] = "/tmp/lustre_tmp.XXXXXX";
        char cmd[256];
        char filepnm[128];
        FILE *filep;
        int ret = 0;
        int ret2 = 0;
        int cmdsz = sizeof(cmd);

        /* Make a temporary directory to hold Lustre data files. */
        if (!mkdtemp(tmpdir)) {
                verrprint("%s: Can't create temporary directory %s: %s\n",
                        progname, tmpdir, strerror(errno));
                return errno;
        }

        snprintf(cmd, cmdsz, "%s -c -R 'dump /%s %s/mountdata' %s",
                 DEBUGFS, MOUNT_DATA_FILE, tmpdir, dev);

        ret = run_command(cmd, cmdsz);
        if (ret) {
                verrprint("%s: Unable to dump %s dir (%d)\n",
                          progname, MOUNT_CONFIGS_DIR, ret);
                goto out_rmdir;
        }

        sprintf(filepnm, "%s/mountdata", tmpdir);
        filep = fopen(filepnm, "r");
        if (filep) {
                size_t num_read;
                vprint("Reading %s\n", MOUNT_DATA_FILE);
                num_read = fread(mo_ldd, sizeof(*mo_ldd), 1, filep);
                if (num_read < 1 && ferror(filep)) {
                        fprintf(stderr, "%s: Unable to read from file (%s): %s\n",
                                progname, filepnm, strerror(errno));
                        goto out_close;
                }
        } else {
                verrprint("%s: Unable to read %d.%d config %s.\n",
                          progname, LUSTRE_MAJOR, LUSTRE_MINOR, filepnm);
                ret = 1;
                goto out_rmdir;
        }

out_close:
        fclose(filep);

out_rmdir:
        snprintf(cmd, cmdsz, "rm -rf %s", tmpdir);
        ret2 = run_command(cmd, cmdsz);
        if (ret2) {
                verrprint("Failed to remove temp dir %s (%d)\n", tmpdir, ret2);
                /* failure return from run_command() is more important
                 * than the failure to remove a dir */
                if (!ret)
                        ret = ret2;
        }

        return ret;
}
예제 #28
0
char *CCodeProcessor::ParsePredictionTypeDescription( char *current )
{
	// Next token is open paren, then classname close paren, then {
	char classname[ 256 ];
	char variablename[ 256 ];

	current = CC_ParseToken( current );
	if (stricmp( com_token, "(" ) )
	{
		return current;
	}

	current = CC_ParseToken( current );
	if ( strlen( com_token ) <= 0 )
		return current;

	strcpy( classname, com_token );
	if ( classname[0]=='*' )
		return current;

	CClass *cl = FindClass( classname );
	if ( cl )
	{
		cl->m_bHasPredictionData = true;
	}

	current = CC_ParseToken( current );
	if (stricmp( com_token, ")" ) )
	{
		return current;
	}

	// It's macro-ized
	strcpy( variablename, "m_PredDesc" );

	com_ignoreinlinecomment = true;
	bool insidecomment = false;

	// Now parse typedescription line by line
	while ( 1 )
	{
		current = CC_ParseToken( current );
		if ( strlen( com_token ) <= 0 )
			break;

		// Go to next line
		if ( !stricmp( com_token, "," ) )
			continue;

		// end
		if ( !stricmp( com_token, "END_PREDICTION_DATA" ) )
			break;

		// skip #ifdef's inside of typedescs
		if ( com_token[0]=='#' )
		{
			current = CC_ParseUntilEndOfLine( current );
			continue;
		}

		if ( !stricmp( com_token, "/" ) )
		{
			current = CC_ParseToken( current );
			if ( !stricmp( com_token, "/" ) )
			{
				current = CC_ParseToken( current );
				if ( !strnicmp( com_token, "DEFINE_", 7 ) )
				{
					CC_UngetToken();
					insidecomment = true;
				}
				else
				{
					current = CC_ParseUntilEndOfLine( current );
				}
				continue;
			}
		}

		com_ignoreinlinecomment = false;

		// Parse a typedescription line
		char definetype[ 256 ];
		strcpy( definetype, com_token );

		current = CC_ParseToken( current );
		if ( stricmp( com_token, "(" ) )
			break;

		// skip classname
		current = CC_ParseToken( current );
		if( stricmp( com_token, classname ) )
		{
			vprint( 0, "PREDICTION TYPEDESCRIPTION for class %s uses offset from class %s\n",
				classname, com_token );
		}
		// skip comma
		current = CC_ParseToken( current );

		char varname[ 256 ];
		current = CC_ParseToken( current );

		strcpy( varname, com_token );

		
		char vartype[ 256 ];

		vartype[0]=0;

		if ( stricmp( definetype, "DEFINE_FUNCTION" ) )
		{
			// skip comma
			current = CC_ParseToken( current );

			current = CC_ParseToken( current );

			strcpy( vartype, com_token );
		}
		else
		{
			strcpy( vartype, "funcptr" );
		}

		bool inrecvtable = false;
		// Jump to end of definition
		int nParenCount = 1;
		do
		{
			current = CC_ParseToken( current );
			if ( strlen( com_token ) <= 0 )
				break;

			if ( !stricmp( com_token, "(" ) )
			{
				++nParenCount; 
			}
			else if ( !stricmp( com_token, ")" ) )
			{
				if ( --nParenCount == 0 )
				{
					break;
				}
			}

			if ( !stricmp( com_token, "FTYPEDESC_INSENDTABLE" ) )
			{
				inrecvtable = true;
			}

		} while ( 1 );

		/*
		vprint( 2, "%s%s::%s %s %s %s\n",
			insidecomment ? "// " : "",
			classname, variablename,
			definetype, varname, vartype );
		*/

		if ( cl )
		{
			if ( cl->FindPredTD( varname ) )
			{
				vprint( 0, "class %s::%s already has prediction typedescription entry for field %s\n", classname, variablename, varname );
			}
			else
			{
				cl->AddPredTD( varname, vartype, definetype, insidecomment, inrecvtable );
			}
		}
		insidecomment = false;
		com_ignoreinlinecomment = true;
	}

	com_ignoreinlinecomment = false;

	return current;
}
예제 #29
0
int eval(PSTATE *ps, OpCodes *opcodes, 
		 ScopeChain *scope, Value *currentScope, 	/* scope chain */
		 Value *_this,
		 Value *vret)
{
	int context_id = ps->_context_id++;
	OpCode *ip = &opcodes->codes[0];
	OpCode *end = &opcodes->codes[opcodes->code_len];
	TryList *trylist = NULL;
	
	if (currentScope->vt != VT_OBJECT) {
		bug("Eval: current scope is not a object\n");
	}
	
	while(ip < end) {
#ifdef DEBUG
		int i;
		printf("STACK%d: ", sp);
		for (i = 0; i < sp; ++i) {
			printf("%s ", vprint(&stack[i]));
		}
		printf("\tthis: %s ", vprint(_this));
		TryList *tlt = trylist;
		for (i = 0; tlt; tlt = tlt->next) i++;
		printf("TL: %d, excpt: %s\n", i, vprint(&ps->last_exception));
		code_decode(ip, ip - opcodes->codes);

		
#endif
		switch(ip->op) {
			case OP_NOP:
			case OP_LASTOP:
				break;
			case OP_PUSHNUM:
				value_make_number(stack[sp], (*((double *)ip->data)));
				sp++;
				break;
			case OP_PUSHSTR:
				value_make_string(stack[sp], unistrdup(ip->data));
				sp++;
				break;
			case OP_PUSHVAR: {
				FastVar *n = ip->data;
				Value *v = NULL;
				if (n->context_id == context_id) {
					v = n->var.lval;
				} else {
					unichar *varname = n->var.varname;
					v = value_object_lookup(currentScope, (ObjKey *)varname, NULL);
					if (!v) v = scope_chain_object_lookup(scope, (ObjKey *)varname);

					if (!v) {	/* add to global scope */
						Value *global_scope = scope->chains_cnt > 0 ? scope->chains[0]:currentScope;
						Value key;
						value_make_string(key, varname);	/* varname is not dupped, do not erase*/
						Value val;
						value_make_undef(val);
						v = value_object_key_assign(global_scope, &key, &val, OM_DONTEMU);
						
						/* key assign dup key and insert into object, so release ourself */
					}
					n->context_id = context_id;
					n->var.lval = v;
				}
				stack[sp].vt = VT_VARIABLE;
				stack[sp].d.lval = v;
				sp++;
				break;
			}
			case OP_PUSHUND:
				value_make_undef(stack[sp]);
				sp++;
				break;
			case OP_PUSHBOO:
				value_make_bool(stack[sp], (int)ip->data);
				sp++;
				break;
			case OP_PUSHFUN: {
				FuncObj *fo = funcobj_new((Func *)ip->data);
				fo->scope =	scope_chain_dup_next(scope, currentScope);
				
				Object *obj = object_new();
				obj->ot = OT_FUNCTION;
				obj->d.fobj = fo;
				obj->__proto__ = Function_prototype;
				
				Value *fun_prototype = value_object_utils_new_object();
				fun_prototype->d.obj->__proto__ = Object_prototype;
				
				value_make_object(stack[sp], obj);
				
				value_object_utils_insert(&stack[sp], PROTOTYPE.unistr, fun_prototype, 0, 1, 0);
				/* todo: make own prototype and prototype.constructor */
				sp++;
				break;
			}
			case OP_PUSHREG: {
				Object *obj = object_new();
				obj->ot = OT_REGEXP;
				obj->d.robj = (regex_t *)ip->data;
				obj->__proto__ = RegExp_prototype;

				value_make_object(stack[sp], obj);
				sp++;
				break;
			}
			case OP_PUSHARG:
				value_copy(stack[sp], *currentScope);
				sp++;
				break;
			case OP_PUSHTHS:
				value_copy(stack[sp], *_this);
				sp++;
				break;
			case OP_PUSHTOP:
				value_copy(stack[sp], TOP);
				sp++;
				break;
			case OP_UNREF:
				topeval1();
				break;
			case OP_PUSHTOP2:
				value_copy(stack[sp], TOQ);
				value_copy(stack[sp+1], TOP);
				sp += 2;
				break;
			case OP_CHTHIS: {
				int t = sp - 2;
				if (ip->data) {
					value_erase(obj_this[t]);
					value_copy(obj_this[t], TOQ);
					if (obj_this[t].vt == VT_VARIABLE) {
						Value *v = obj_this[t].d.lval;
						value_copy(obj_this[t], *v);
					}
					value_toobject(&obj_this[t]);
				}
				break;
			}
			case OP_LOCAL: {
				ObjKey *strkey = objkey_new((const unichar *)ip->data, OM_DONTEMU);
				value_object_insert(currentScope, strkey, value_new());
				
				/* make all FastVar to be relocated */
				context_id = ps->_context_id++;
				break;
			}
			case OP_POP:
				pop_n(ip->data);
				break;
			case OP_NEG:
				topeval1();
				value_tonumber(&TOP);
				TOP.d.num = -(TOP.d.num);
				break;
			case OP_POS:
				topeval1();
				value_tonumber(&TOP);
				break;
			case OP_NOT: {
				int val = 0;
				topeval1();
				
				val = value_istrue(&TOP);
				
				value_erase(TOP);
				value_make_bool(TOP, !val);
				break;
			}
			case OP_BNOT: {
				topeval1();
				value_toint32(&TOP);
				TOP.d.num = (double)(~((int)TOP.d.num));
				break;
			}
			case OP_ADD: {
				topeval2();
				value_toprimitive(&TOP);
				value_toprimitive(&TOQ);
				
				if (TOP.vt == VT_STRING || TOQ.vt == VT_STRING) {
					value_tostring(&TOP);
					value_tostring(&TOQ);
					
					unichar *v = unistrcat(TOQ.d.str, TOP.d.str);
					
					value_erase(TOQ);
					value_make_string(TOQ, v);
				} else {
					value_tonumber(&TOP);
					value_tonumber(&TOQ);
					double n = TOP.d.num + TOQ.d.num;
					value_erase(TOQ);
					value_make_number(TOQ, n);
				}
				pop();
				break;
			}
			case OP_SUB:	/* god, the notes in ecma is so long, pray to run correctly */
				common_math_opr(-); break;
			case OP_MUL:
				common_math_opr(*); break;
			case OP_DIV:
				common_math_opr(/); break;
			case OP_MOD: {
				topeval2();
				if (!is_number(&TOP)) value_tonumber(&TOP);
				if (!is_number(&TOQ)) value_tonumber(&TOQ);

				TOQ.d.num = fmod(TOQ.d.num, TOP.d.num);
				pop();
				break;
			}
			case OP_LESS:
				topeval2();
				logic_less(TOQ, TOP, TOQ);
				pop();
				break;
			case OP_GREATER:
				topeval2();
				logic_less(TOP, TOQ, TOQ);
				pop();
				break;
			case OP_LESSEQU:
				topeval2();
				logic_less(TOP, TOQ, TOQ);
				TOQ.d.val = !TOQ.d.val;
				pop();
				break;
			case OP_GREATEREQU:
				topeval2();
				logic_less(TOQ, TOP, TOQ);
				TOQ.d.val = !TOQ.d.val;
				pop();
				break;
			case OP_EQUAL:
			case OP_NOTEQUAL: {		/* awful, equal opration */
				int r = 0;
				topeval2();
				if (TOP.vt != TOQ.vt) {
					value_toprimitive(&TOP);
					value_toprimitive(&TOQ);
				}
				if (TOP.vt != TOQ.vt) {
					if ((is_undef(&TOP) || is_null(&TOP)) && 
						(is_undef(&TOQ) || is_null(&TOQ))) {
						r = 1;
					} else {
						value_tonumber(&TOP);
						value_tonumber(&TOQ);
						r = (TOP.d.num == TOQ.d.num);
					}
				} else {
					switch (TOP.vt) {
						case VT_NUMBER:
							r = (TOP.d.num == TOQ.d.num);
							break;
						case VT_BOOL:
							r = (TOP.d.val == TOQ.d.val);
							break;
						case VT_STRING:
							r = (unistrcmp(TOQ.d.str, TOP.d.str) == 0);
							break;
						case VT_OBJECT:
							/* todo: refer to objects joined to each other */
							r = (TOP.d.obj == TOQ.d.obj);
							break;
						case VT_UNDEF:
						case VT_NULL:
							r = 1;
							break;
						default:
							bug("Unexpected value type\n");
					}
				}
				r = (ip->op == OP_EQUAL ? r : !r);
				value_erase(TOQ);
				value_make_bool(TOQ, r);
				pop();
				break;
			}
			case OP_STRICTEQU:
			case OP_STRICTNEQ: {
				int r = 0;
				topeval2();
				if (TOP.vt == TOQ.vt) {
					switch (TOP.vt) {
						case VT_NUMBER:
							r = (TOP.d.num == TOQ.d.num);
							break;
						case VT_BOOL:
							r = (TOP.d.val == TOQ.d.val);
							break;
						case VT_STRING:
							r = (unistrcmp(TOQ.d.str, TOP.d.str) == 0);
							break;
						case VT_OBJECT:
							/* todo: refer to objects joined to each other */
							r = (TOP.d.obj == TOQ.d.obj);
							break;
						case VT_UNDEF:
						case VT_NULL:
							r = 1;
							break;
						default:
							bug("Unexpected value type\n");
					}
				}
				r = (ip->op == OP_STRICTEQU ? r : !r);
				value_erase(TOQ);
				value_make_bool(TOQ, r);
				pop();
				break;
			}
			case OP_BAND: 
				common_bitwise_opr(&); break;
			case OP_BOR:
				common_bitwise_opr(|); break;
			case OP_BXOR:
				common_bitwise_opr(^); break;
			case OP_SHF: {
				topeval2();
				value_toint32(&TOQ);
				value_toint32(&TOP);
				int t1 = (int)TOQ.d.num;
				int t2 = ((unsigned int)TOP.d.num) & 0x1f;
				if (ip->data) {					/* thift right */
					if ((int)ip->data == 2) {	/* unsigned shift */
						unsigned int t3 = (unsigned int)t1;
						t3 >>= t2;
						value_make_number(TOQ, t3);
					} else {
						t1 >>= t2;
						value_make_number(TOQ, t1);
					}
				} else {
					t1 <<= t2;
					value_make_number(TOQ, t1);
				}
				pop();
				break;
			}
예제 #30
0
/*
 * Caller holds I/O reference on vnode
 */
int
vnode_label(struct mount *mp, struct vnode *dvp, struct vnode *vp,
	    struct componentname *cnp, int flags, vfs_context_t ctx)
{
	int error = 0;


	/* fast path checks... */

	/* are we labeling vnodes? If not still notify of create */
	if (mac_label_vnodes == 0) {
		if (flags & VNODE_LABEL_CREATE)
			error = mac_vnode_notify_create(ctx,
			    mp, dvp, vp, cnp);
		return 0;
	}

	/* if already VL_LABELED */
	if (vp->v_lflag & VL_LABELED)
		return (0);

	vnode_lock_spin(vp);

	/*
	 * must revalidate state once we hold the lock
	 * since we could have blocked and someone else
	 * has since labeled this vnode
	 */
	if (vp->v_lflag & VL_LABELED) {
		vnode_unlock(vp);
		return (0);
	}

	if ((vp->v_lflag & VL_LABEL) == 0) {
		vp->v_lflag |= VL_LABEL;

		/* Could sleep on disk I/O, drop lock. */
		vnode_unlock(vp);

		if (vp->v_label == NULL)
			vp->v_label = mac_vnode_label_alloc();

		if (flags & VNODE_LABEL_CREATE)
			error = mac_vnode_notify_create(ctx,
			    mp, dvp, vp, cnp);
		else
			error = mac_vnode_label_associate(mp, vp, ctx);

		vnode_lock_spin(vp);

		if ((error == 0) && (vp->v_flag & VNCACHEABLE))
			vp->v_lflag |= VL_LABELED;
		vp->v_lflag &= ~VL_LABEL;

		if (vp->v_lflag & VL_LABELWAIT) {
			vp->v_lflag &= ~VL_LABELWAIT;
			wakeup(&vp->v_label);
		}
	} else {
		struct timespec ts;

		ts.tv_sec = 10;
		ts.tv_nsec = 0;

		while (vp->v_lflag & VL_LABEL) {
			vp->v_lflag |= VL_LABELWAIT;

			error = msleep(&vp->v_label, &vp->v_lock, PVFS|PDROP,
				       "vnode_label", &ts);
			vnode_lock_spin(vp);

			if (error == EWOULDBLOCK) {
				vprint("vnode label timeout", vp);
				break;
			}
		}
		/* XXX: what should be done if labeling failed (above)? */
	}
	vnode_unlock(vp);

	return (error);
}