File: utils.vala

package info (click to toggle)
valabind 0.8.0-2.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 440 kB
  • ctags: 83
  • sloc: sh: 295; makefile: 126; python: 72; java: 68; cs: 10; cpp: 8; perl: 6; ruby: 5
file content (67 lines) | stat: -rw-r--r-- 2,138 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-  */

/* Copyleft 2009-2012 -- pancake */
/* Copyleft 2014 -- Ritesh Khadgaray <khadgaray@gmail.com> */

public void notice (string msg) {
	stderr.printf ("\x1b[34;1mNOTICE\x1b[0m %s\n", msg);
}

public void warning (string msg) {
	stderr.printf ("\x1b[33;1mWARNING\x1b[0m %s\n", msg);
}

public void error (string msg) {
	stderr.printf ("\x1b[31;1mERROR\x1b[0m %s\n", msg);
	Posix.exit (1);
}

// TODO: check out if this is really required ?
public int array_length (Vala.ArrayType array) {
	if (array.fixed_length && array.length is Vala.IntegerLiteral) {
		Vala.IntegerLiteral lit = (Vala.IntegerLiteral) array.length;
		return int.parse (lit.value);
	}

	return -1;
}

// TODO: make it reusable for other backends
public string get_enums_for (string str, GLib.List<string> includefiles) {
	string enums_exec, enums_out = "";
	try {
		FileUtils.close (FileUtils.open_tmp ("vbeXXXXXX", out enums_exec));
	} catch (FileError e) {
		error (e.message);
	}
	string[] gcc_args = {"gcc", "-x", "c", "-o", enums_exec, "-"};
	foreach (var i in include_dirs)
		gcc_args += "-I"+i;
	try {
		Pid gcc_pid;
		int gcc_stdinfd;
		Process.spawn_async_with_pipes (null, gcc_args, null,
				SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
				null, out gcc_pid, out gcc_stdinfd);
		var gcc_stdin = FileStream.fdopen (gcc_stdinfd, "w");
		if (gcc_stdin == null)
			throw new SpawnError.IO ("Cannot open gcc's stdin");
		foreach (string i in includefiles)
			gcc_stdin.printf ("#include <%s>\n", i);
		gcc_stdin.printf ("int main(){%s;return 0;}\n", str);
		gcc_stdin = null;
		int status;
		Posix.waitpid (gcc_pid, out status, 0);
		Process.close_pid (gcc_pid);
		if (status != 0)
			throw new SpawnError.FAILED ("gcc exited with status %d", status);
		Process.spawn_sync (null, {enums_exec}, null, 0, null, out enums_out, null, out status);
		if (status != 0)
			throw new SpawnError.FAILED ("enums helper exited with status %d", status);
	} catch (SpawnError e) {
		FileUtils.unlink (enums_exec);
		error (e.message);
	}
	FileUtils.unlink (enums_exec);
	return enums_out;
}