File: syscall_nr_compat_gen.py

package info (click to toggle)
vuos 0.9.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,616 kB
  • sloc: ansic: 22,155; python: 284; makefile: 28; sh: 4
file content (66 lines) | stat: -rwxr-xr-x 1,611 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3
import sys
import os

'''
generate fake __NR_xxxx constants for syscalls not defined in the current arch
so that code like:
  if (... == __NR_xxxx)
or
  switch (...) {
		case __NR_xxxx
is just skipped. No arch #ifdef are required.
'''
__NR__first = 1000000000

if len(sys.argv) < 2 or not os.path.isfile(sys.argv[1]):
	print(f"{sys.argv[0]}: the input should be 'vu_syscalls.conf'")
	sys.exit(1)

# Parse and output

def acceptable(string):
  if string.startswith('#') or \
    string.startswith('-') or \
    string.startswith('null') or \
    string.startswith('BUILTIN'):
    return False
  else:
    return True

def get_syscall_names(string):
  syscall_list = []
  s = string.rpartition(':')
  if ':' == s[1]:
    seq = s[0].split(', ')
    for syscall in seq:
      parts = syscall.rpartition('/')
      if parts[1] == '/':
        syscall_list.append(parts[0])
      else:
        syscall_list.append(parts[2])
  return syscall_list

syscall_list = []
with open(sys.argv[1]) as f:
  for line in f:
    if acceptable(line):
      syscall_list += get_syscall_names(line)

print('''#ifndef __SYSCALL_NR_COMPAT_H
#define __SYSCALL_NR_COMPAT_H

/* Generate compat __NR for missing system calls */

''')

for syscall_nr,syscall_def in enumerate(syscall_list, start = __NR__first):
  print(f'''#ifndef __NR_{syscall_def}
# define __NR_{syscall_def}  {syscall_nr}
#endif\n''')

print(f'#define __NR__first {__NR__first}')
print(f'#define __NR__last {__NR__first + len(syscall_list) - 1}')
print('#define __NR__is_unsupp(X) ((X) >= __NR__first && (X) <= __NR__last)');
print('\n\n#endif')