1#!/usr/bin/env python 2# 3 4import sys, os, string, re 5 6def usage(): 7 print """\ 8 usage: genserv < /etc/services > libc/netbsd/net/services.h 9 10 this program is used to generate the hard-coded internet service list for the 11 Bionic C library. 12""" 13 14re_service = re.compile(r"([\d\w\-_]+)\s+(\d+)/(tcp|udp)(.*)") 15re_alias = re.compile(r"([\d\w\-_]+)(.*)") 16 17class 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 41def 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 69services = parse(sys.stdin) 70line = '/* generated by genserv.py - do not edit */\nstatic const char _services[] = "\\\n' 71for s in services: 72 line += str(s)+"\\\n" 73line += '\\0";\n' 74print line 75