Example #1
0
// AddFile compiles rules from a file. Rules are added to the
// specified namespace.
func (c *Compiler) AddFile(file *os.File, namespace string) (err error) {
	fd := C.dup(C.int(file.Fd()))
	fh, err := C.fdopen(fd, C.CString("r"))
	if err != nil {
		return err
	}
	defer C.fclose(fh)
	var ns *C.char
	if namespace != "" {
		ns = C.CString(namespace)
		defer C.free(unsafe.Pointer(ns))
	}
	filename := C.CString(file.Name())
	defer C.free(unsafe.Pointer(filename))
	id := callbackData.Put(c)
	defer callbackData.Delete(id)
	C.yr_compiler_set_callback(c.cptr, C.YR_COMPILER_CALLBACK_FUNC(C.compilerCallback), unsafe.Pointer(id))
	numErrors := int(C.yr_compiler_add_file(c.cptr, fh, ns, filename))
	if numErrors > 0 {
		var buf [1024]C.char
		msg := C.GoString(C.yr_compiler_get_error_message(
			c.cptr, (*C.char)(unsafe.Pointer(&buf[0])), 1024))
		err = errors.New(msg)
	}
	return
}
Example #2
0
File: run.go Project: xushiwei/gopy
func RunFile(filename string, start StartToken, globals, locals Object) (Object, error) {
	name := C.CString(filename)
	defer C.free(unsafe.Pointer(name))

	mode := C.CString("r")
	defer C.free(unsafe.Pointer(mode))

	var token C.int
	switch start {
	case EvalInput:
		token = C.Py_eval_input
	case FileInput:
		token = C.Py_file_input
	case SingleInput:
		token = C.Py_single_input
	}

	file, err := C.fopen(name, mode)
	if file == nil {
		return nil, err
	}
	defer C.fclose(file)

	obj := C.PyRun_FileExFlags(file, name, token, c(globals), c(locals), 0, nil)
	if obj == nil {
		return nil, exception()
	}

	return newObject(obj), nil
}
Example #3
0
func BlockComplexFloatFprintf(stream *os.File, b *GslBlockComplexFloat, format string) int32 {
	_file_0 := C.fdopen(C.dup(C.int(stream.Fd())), (*C.char)(unsafe.Pointer(gogsl.APPEND_ONLY.Ptr())))
	_string_2 := C.CString(format)
	_result := int32(C.gsl_block_complex_float_fprintf(_file_0, (*C.gsl_block_complex_float)(unsafe.Pointer(b.Ptr())), _string_2))
	C.fclose(_file_0)
	C.free(unsafe.Pointer(_string_2))
	return _result
}
Example #4
0
func Fprintf(stream *os.File, c *GslCombination, format string) int32 {
	_file_0 := C.fdopen(C.dup(C.int(stream.Fd())), (*C.char)(unsafe.Pointer(gogsl.APPEND_ONLY.Ptr())))
	_string_2 := C.CString(format)
	_result := int32(C.gsl_combination_fprintf(_file_0, (*C.gsl_combination)(unsafe.Pointer(c.Ptr())), _string_2))
	C.fclose(_file_0)
	C.free(unsafe.Pointer(_string_2))
	return _result
}
Example #5
0
func Histogram2dFprintf(stream *os.File, h *GslHistogram2d, rangeFormat string, binFormat string) int32 {
	_file_0 := C.fdopen(C.dup(C.int(stream.Fd())), (*C.char)(unsafe.Pointer(gogsl.APPEND_ONLY.Ptr())))
	_string_2 := C.CString(rangeFormat)
	_string_3 := C.CString(binFormat)
	_result := int32(C.gsl_histogram2d_fprintf(_file_0, (*C.gsl_histogram2d)(unsafe.Pointer(h.Ptr())), _string_2, _string_3))
	C.fclose(_file_0)
	C.free(unsafe.Pointer(_string_2))
	C.free(unsafe.Pointer(_string_3))
	return _result
}
Example #6
0
func logLinuxStats() {
	if !log.V(1) {
		return
	}

	// We don't know which fields in struct mallinfo are most relevant to us yet,
	// so log it all for now.
	//
	// A major caveat is that mallinfo() returns stats only for the main arena.
	// glibc uses multiple allocation arenas to increase malloc performance for
	// multithreaded processes, so mallinfo may not report on significant parts
	// of the heap.
	mi := C.mallinfo()
	log.Infof("mallinfo stats: ordblks=%s, smblks=%s, hblks=%s, hblkhd=%s, usmblks=%s, fsmblks=%s, "+
		"uordblks=%s, fordblks=%s, keepcost=%s",
		humanize.IBytes(uint64(mi.ordblks)),
		humanize.IBytes(uint64(mi.smblks)),
		humanize.IBytes(uint64(mi.hblks)),
		humanize.IBytes(uint64(mi.hblkhd)),
		humanize.IBytes(uint64(mi.usmblks)),
		humanize.IBytes(uint64(mi.fsmblks)),
		humanize.IBytes(uint64(mi.uordblks)),
		humanize.IBytes(uint64(mi.fordblks)),
		humanize.IBytes(uint64(mi.fsmblks)))

	// malloc_info() emits a *lot* of XML, partly because it generates stats for
	// all arenas, unlike mallinfo().
	//
	// TODO(cdo): extract the useful bits and record to time-series DB.
	if !log.V(2) {
		return
	}

	// Create a memstream and make malloc_info() output to it.
	var buf *C.char
	var bufSize C.size_t
	memstream := C.open_memstream(&buf, &bufSize)
	if memstream == nil {
		log.Warning("couldn't open memstream")
		return
	}
	defer func() {
		C.fclose(memstream)
		C.free(unsafe.Pointer(buf))
	}()
	if rc := C.malloc_info(0, memstream); rc != 0 {
		log.Warningf("malloc_info returned %d", rc)
		return
	}
	if rc := C.fflush(memstream); rc != 0 {
		log.Warningf("fflush returned %d", rc)
		return
	}
	log.Infof("malloc_info: %s", C.GoString(buf))
}
Example #7
0
File: gd.go Project: trajber/go-gd
func (p *Image) Gif(out string) {
	file := C.fopen(C.CString(out), C.CString("wb"))

	if file != nil {
		defer C.fclose(file)

		C.gdImageGif(p.img, file)
	} else {
		panic(errors.New("Error occurred while opening file for writing."))
	}
}
Example #8
0
File: gd.go Project: trajber/go-gd
func (p *Image) Jpeg(out string, quality int) {
	file := C.fopen(C.CString(out), C.CString("wb"))

	if file != nil {
		defer C.fclose(file)

		C.gdImageJpeg(p.img, file, C.int(quality))
	} else {
		panic(errors.New("Error occurred while opening file for writing."))
	}
}
Example #9
0
File: gd.go Project: trajber/go-gd
func CreateFromGif(infile string) *Image {
	file := C.fopen(C.CString(infile), C.CString("rb"))

	if file != nil {
		defer C.fclose(file)

		return img(C.gdImageCreateFromGif(file))
	}

	panic(errors.New("Error occurred while opening file."))
}
Example #10
0
File: gd.go Project: trajber/go-gd
func (p *Image) Wbmp(out string, foreground Color) {
	file := C.fopen(C.CString(out), C.CString("wb"))

	if file != nil {
		defer C.fclose(file)

		C.gdImageWBMP(p.img, C.int(foreground), file)
	} else {
		panic(errors.New("Error occurred while opening file for writing."))
	}
}
Example #11
0
func testErrno(t *testing.T) {
	p := C.CString("no-such-file")
	m := C.CString("r")
	f, err := C.fopen(p, m)
	C.free(unsafe.Pointer(p))
	C.free(unsafe.Pointer(m))
	if err == nil {
		C.fclose(f)
		t.Fatalf("C.fopen: should fail")
	}
	if err != syscall.ENOENT {
		t.Fatalf("C.fopen: unexpected error: %v", err)
	}
}
Example #12
0
func Init() {
	filename := C.CString("/etc/sensors3.conf")
	defer C.free(unsafe.Pointer(filename))

	mode := C.CString("r")
	defer C.free(unsafe.Pointer(mode))

	fp, err := C.fopen(filename, mode)
	defer C.fclose(fp)

	if fp == nil {
		log.Fatal(err)
	}

	C.sensors_init(fp)
}
Example #13
0
// Parses a file and returns a CMarkNode
// Returns an error if the file can't be opened
func ParseFile(filename string, options int) (*CMarkNode, error) {
	fname := C.CString(filename)
	access := C.CString("r")
	defer C.free(unsafe.Pointer(fname))
	defer C.free(unsafe.Pointer(access))
	file := C.fopen(fname, access)
	if file == nil {
		return nil, errors.New("Unable to open file with name: " + filename)
	}
	defer C.fclose(file)
	n := &CMarkNode{
		node: C.cmark_parse_file(file, C.int(options)),
	}
	runtime.SetFinalizer(n, (*CMarkNode).Free)
	return n, nil
}
Example #14
0
File: code.go Project: MogeiWang/py
func CompileFile(name string, start StartToken) (*Code, error) {
	fn := C.CString(name)
	defer C.free(unsafe.Pointer(fn))

	file, err := C.openFile(fn)
	if file == nil {
		return nil, err
	}
	defer C.fclose(file)

	ret := C.compileFile(file, fn, C.int(start))
	if ret == nil {
		return nil, exception()
	}
	return newCode(ret), nil
}
Example #15
0
// PyRun_SimpleFile executes the given python script synchronously.  Note that
// unlike the corresponding C API, this will internally open and close the file
// for you.
func PyRun_SimpleFile(filename string) error {
	cfname := C.CString(filename)
	defer C.free(unsafe.Pointer(cfname))

	cronly := C.CString("r")
	defer C.free(unsafe.Pointer(cronly))

	cfile, err := C.fopen(cfname, cronly)
	if err != nil || cfile == nil {
		return fmt.Errorf("python: could not open %s: %v", filename, err)
	}
	defer C.fclose(cfile)

	retcode := C.PyRun_SimpleFileExFlags(cfile, cfname, 0, nil)
	if retcode != 0 {
		return fmt.Errorf("error %d executing script %s", int(retcode),
			filename)
	}
	return nil
}
Example #16
0
func (c *Compiler) AddFile(ns, path string) error {
	cpath := C.CString(path)
	cmode := C.CString("r")
	cns := C.CString(ns)

	defer C.free(unsafe.Pointer(cpath))
	defer C.free(unsafe.Pointer(cmode))
	defer C.free(unsafe.Pointer(cns))

	fd := C.fopen(cpath, cmode)
	if fd == nil {
		return fmt.Errorf("libyara: failed to open %q", path)
	}

	defer C.fclose(fd)

	errors := C.yr_compiler_add_file(c.handle, fd, nil, cpath)
	if errors > 0 {
		return fmt.Errorf("libyara: failed to compile %q", path)
	}

	return nil
}
Example #17
0
func parseMountTable() ([]*Info, error) {
	mnttab := C.fopen(C.CString(C.MNTTAB), C.CString("r"))
	if mnttab == nil {
		return nil, fmt.Errorf("Failed to open %s", C.MNTTAB)
	}

	var out []*Info
	var mp C.struct_mnttab

	ret := C.getmntent(mnttab, &mp)
	for ret == 0 {
		var mountinfo Info
		mountinfo.Mountpoint = C.GoString(mp.mnt_mountp)
		mountinfo.Source = C.GoString(mp.mnt_special)
		mountinfo.Fstype = C.GoString(mp.mnt_fstype)
		mountinfo.Opts = C.GoString(mp.mnt_mntopts)
		out = append(out, &mountinfo)
		ret = C.getmntent(mnttab, &mp)
	}

	C.fclose(mnttab)
	return out, nil
}
Example #18
0
func BlockComplexFscanf(stream *os.File, b *GslBlockComplex) int32 {
	_file_0 := C.fdopen(C.dup(C.int(stream.Fd())), (*C.char)(unsafe.Pointer(gogsl.APPEND_ONLY.Ptr())))
	_result := int32(C.gsl_block_complex_fscanf(_file_0, (*C.gsl_block_complex)(unsafe.Pointer(b.Ptr()))))
	C.fclose(_file_0)
	return _result
}
Example #19
0
func BlockUcharFwrite(stream *os.File, b *GslBlockUchar) int32 {
	_file_0 := C.fdopen(C.dup(C.int(stream.Fd())), (*C.char)(unsafe.Pointer(gogsl.APPEND_ONLY.Ptr())))
	_result := int32(C.gsl_block_uchar_fwrite(_file_0, (*C.gsl_block_uchar)(unsafe.Pointer(b.Ptr()))))
	C.fclose(_file_0)
	return _result
}
Example #20
0
func Fwrite(stream *os.File, c *GslMultiset) int32 {
	_file_0 := C.fdopen(C.dup(C.int(stream.Fd())), (*C.char)(unsafe.Pointer(gogsl.APPEND_ONLY.Ptr())))
	_result := int32(C.gsl_multiset_fwrite(_file_0, (*C.gsl_multiset)(unsafe.Pointer(c.Ptr()))))
	C.fclose(_file_0)
	return _result
}
Example #21
0
File: rng.go Project: postfix/gsl-1
func Fread(stream *os.File, r *GslRng) int32 {
	_file_0 := C.fdopen(C.dup(C.int(stream.Fd())), (*C.char)(unsafe.Pointer(gogsl.APPEND_ONLY.Ptr())))
	_result := int32(C.gsl_rng_fread(_file_0, (*C.gsl_rng)(unsafe.Pointer(r.Ptr()))))
	C.fclose(_file_0)
	return _result
}
Example #22
0
func Fread(stream *os.File, p *GslPermutation) int32 {
	_file_0 := C.fdopen(C.dup(C.int(stream.Fd())), (*C.char)(unsafe.Pointer(gogsl.APPEND_ONLY.Ptr())))
	_result := int32(C.gsl_permutation_fread(_file_0, (*C.gsl_permutation)(unsafe.Pointer(p.Ptr()))))
	C.fclose(_file_0)
	return _result
}
Example #23
0
func Histogram2dFscanf(stream *os.File, h *GslHistogram2d) int32 {
	_file_0 := C.fdopen(C.dup(C.int(stream.Fd())), (*C.char)(unsafe.Pointer(gogsl.APPEND_ONLY.Ptr())))
	_result := int32(C.gsl_histogram2d_fscanf(_file_0, (*C.gsl_histogram2d)(unsafe.Pointer(h.Ptr()))))
	C.fclose(_file_0)
	return _result
}
Example #24
0
func BlockFloatFread(stream *os.File, b *GslBlockFloat) int32 {
	_file_0 := C.fdopen(C.dup(C.int(stream.Fd())), (*C.char)(unsafe.Pointer(gogsl.APPEND_ONLY.Ptr())))
	_result := int32(C.gsl_block_float_fread(_file_0, (*C.gsl_block_float)(unsafe.Pointer(b.Ptr()))))
	C.fclose(_file_0)
	return _result
}
Example #25
0
func Fscanf(stream *os.File, c *GslCombination) int32 {
	_file_0 := C.fdopen(C.dup(C.int(stream.Fd())), (*C.char)(unsafe.Pointer(gogsl.APPEND_ONLY.Ptr())))
	_result := int32(C.gsl_combination_fscanf(_file_0, (*C.gsl_combination)(unsafe.Pointer(c.Ptr()))))
	C.fclose(_file_0)
	return _result
}