| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python | 
 | 2 | # | 
 | 3 |  | 
 | 4 | import sys, os, string, re | 
 | 5 |  | 
 | 6 | def usage(): | 
 | 7 |     print """\ | 
| Elliott Hughes | e6ddfc5 | 2013-03-25 14:09:52 -0700 | [diff] [blame] | 8 |   usage:  genserv < /etc/services > libc/netbsd/net/services.h | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 9 |  | 
 | 10 |   this program is used to generate the hard-coded internet service list for the | 
 | 11 |   Bionic C library. | 
 | 12 | """ | 
 | 13 |  | 
 | 14 | re_service = re.compile(r"([\d\w\-_]+)\s+(\d+)/(tcp|udp)(.*)") | 
 | 15 | re_alias   = re.compile(r"([\d\w\-_]+)(.*)") | 
 | 16 |  | 
 | 17 | class Service: | 
 | 18 |     def __init__(self,name,port,proto): | 
 | 19 |         self.name    = name | 
 | 20 |         self.port    = port | 
 | 21 |         self.proto   = proto | 
 | 22 |         self.aliases = [] | 
 | 23 |  | 
 | 24 |     def add_alias(self,alias): | 
 | 25 |         self.aliases.append(alias) | 
 | 26 |  | 
 | 27 |     def __str__(self): | 
 | 28 |         result  = "\\%0o%s" % (len(self.name),self.name) | 
 | 29 |         result += "\\%0o\\%0o" % (((self.port >> 8) & 255), self.port & 255) | 
 | 30 |         if self.proto == "tcp": | 
 | 31 |             result += "t" | 
 | 32 |         else: | 
 | 33 |             result += "u" | 
 | 34 |  | 
 | 35 |         result += "\\%0o" % len(self.aliases) | 
 | 36 |         for alias in self.aliases: | 
 | 37 |             result += "\\%0o%s" % (len(alias), alias) | 
 | 38 |  | 
 | 39 |         return result | 
 | 40 |  | 
 | 41 | def parse(f): | 
 | 42 |     result = []  # list of Service objects | 
 | 43 |     for line in f.xreadlines(): | 
 | 44 |         if len(line) > 0 and line[-1] == "\n": | 
 | 45 |             line = line[:-1] | 
 | 46 |         if len(line) > 0 and line[-1] == "\r": | 
 | 47 |             line = line[:-1] | 
 | 48 |  | 
 | 49 |         line = string.strip(line) | 
 | 50 |         if len(line) == 0 or line[0] == "#": | 
 | 51 |             continue | 
 | 52 |  | 
 | 53 |         m = re_service.match(line) | 
 | 54 |         if m: | 
 | 55 |             service = Service( m.group(1), int(m.group(2)), m.group(3) ) | 
 | 56 |             rest    = string.strip(m.group(4)) | 
 | 57 |  | 
 | 58 |             while 1: | 
 | 59 |                 m = re_alias.match(rest) | 
 | 60 |                 if not m: | 
 | 61 |                     break | 
 | 62 |                 service.add_alias(m.group(1)) | 
 | 63 |                 rest = string.strip(m.group(2)) | 
 | 64 |  | 
 | 65 |             result.append(service) | 
 | 66 |  | 
 | 67 |     return result | 
 | 68 |  | 
 | 69 | services = parse(sys.stdin) | 
 | 70 | line = '/* generated by genserv.py - do not edit */\nstatic const char  _services[] = "\\\n' | 
 | 71 | for s in services: | 
 | 72 |     line += str(s)+"\\\n" | 
 | 73 | line += '\\0";\n' | 
 | 74 | print line |