#!/usr/bin/python3 from subprocess import check_output def shell_exec (cmd): try: a = check_output (cmd, shell=True) except: a = b'error\n' # decode('ascii') fails on special chars # FIXME: major hack until we handle everything as bytestrings try: a = a.decode ('utf-8') except UnicodeDecodeError: a = a.decode ('iso-8859-1') a = a.split ('\n') del a[-1] return a # enable debugging import cgitb cgitb.enable() import cgi import os from re import search, sub from collections import OrderedDict print ('Content-Type: text/html;charset=utf-8\n') form = cgi.FieldStorage() version = form.getvalue ('v') if not (version and search ('^[A-Za-z0-9.-]+$', version)): version = '4.9' url = os.environ['SCRIPT_URL'] m = search ('^/source/(.*)$', url) if m: mode = 'source' path = m.group (1) if not search ('^[A-Za-z0-9_/.,+-]*$', path): path = 'INVALID' url2 = 'source/'+path+'?' elif url == '/ident': mode = 'ident' ident = form.getvalue ('i') if not (ident and search ('^[A-Za-z0-9_]*$', ident)): ident = '' url2 = 'ident?i='+ident+'&' elif url == '/search': mode = 'search' url2 = 'search?' head = open ('template-head').read() head = sub ('\$baseurl', 'http://lxrng', head) head = sub ('\$vvar', version, head) lines = shell_exec ('cd ..; ./query.py versions') va = OrderedDict() for l in lines: if search ('^2\.6', l): m = search ('^(2\.6)(\.[0-9]*)((\.|-).*)?$', l) else: m = search ('^([0-9]*)(\.[0-9]*)((\.|-).*)?$', l) m1 = m.group(1) m2 = m.group(2) if m1 not in va: va[m1] = OrderedDict() if m1+m2 not in va[m1]: va[m1][m1+m2] = [] va[m1][m1+m2].append (l) v = '\n' head = sub ('\$versions', v, head) if mode == 'source': banner = 'Linux/' p2 = '' for p in path.split ('/'): if p == '': continue banner += ''+p+'/' p2 += p+'/' head = sub ('\$banner', banner, head) head = sub ('\$title', 'Linux/'+path+' - Linux Cross Reference - Free Electrons', head) print (head, end='') lines = ['null - -'] if path[-1:] == '/' or path == '': type = 'tree' lines += shell_exec ('cd ..; ./query.py dir '+version+' \''+path+'\'') else: type = 'blob' lines += shell_exec ('cd ..; ./query.py file '+version+' \''+path+'\'') if type == 'tree': if path != '': lines[0] = 'back - -' print ('\n') for l in lines: type, name, size = l.split (' ') if type == 'null': continue elif type == 'tree': icon = 'folder.gif' size = '' path2 = path+name+'/' name = name+'/' elif type == 'blob': icon = 'text.gif' size = size+' bytes' path2 = path+name elif type == 'back': icon = 'back.gif' size = '' path2 = os.path.dirname (path[:-1]) + '/' if path2 == '/': path2 = './' name = 'Parent directory' print (' ') print (' ') print (' ') print (' ') print (' \n') print ('
'+icon+''+name+''+size+'
', end='') elif type == 'blob': del (lines[0]) print ('
')

        width = len (str (len (lines))) 
        num = 1
        n2 = ('%'+str(width)+'d') % num
        for l in lines:
            print (''+n2+' ', end='')
            l = cgi.escape (l)
            l = sub ('"', '"', l)
            l = sub ('\033\[31m(.*?)\033\[0m', '\\1', l)
            l = sub ('\033\[32m', '', l)
            l = sub ('\033\[0m', '', l)
            print (l)
            num += 1
            n2 = ('%'+str(width)+'d') % num

        print ('
', end='') elif mode == 'ident': field = '\n
\n\nIdentifier: \n\n
\n

' + ident head = sub ('\$banner', field, head) head = sub ('\$title', 'Linux identifier search "'+ident+'" - Linux Cross Reference - Free Electrons', head) print (head, end='') lines = shell_exec ('cd .. ; ./query.py ident '+version+' '+ident) lines = iter (lines) m = search ('Defined in (\d*) file', next (lines)) if m: num = int (m.group(1)) print ('Defined in', num, 'files:') print ('') next (lines) m = search ('Referenced in (\d*) file', next (lines)) num = int (m.group(1)) print ('Referenced in', num, 'files:') print ('') else: if ident != '': print ('Not used') elif mode == 'search': head = sub ('\$banner', '', head) head = sub ('\$title', 'Linux freetext search - Linux Cross Reference - Free Electrons', head) print (head, end='') print ('
The Weblxr.free-electrons.com/source
') else: print (head) print ('Invalid request') print (open ('template-tail').read(), end='')