Implement new URL scheme

This commit is contained in:
Mikaël Bouillot 2017-04-04 16:27:18 +02:00
parent 84bd5a0a16
commit e594999925
2 changed files with 56 additions and 44 deletions

View file

@ -20,7 +20,7 @@
<h2><a href="http://free-electrons.com">Free Electrons</a></h2>
<h2>Embedded Linux Experts</h2>
<p>
&nbsp;&bull;&nbsp;<a href="source?v=$vvar">source navigation</a>&nbsp;&bull;&nbsp;<a href="ident?v=$vvar">identifier search</a>&nbsp;&bull;&nbsp;<a href="search?v=$vvar">freetext search</a>&nbsp;&bull;&nbsp;<a href="http://lxr.free-electrons.com" style="color:#00FF00">Back to LXR</a>&nbsp;&bull;&nbsp;
&nbsp;&bull;&nbsp;<a href="linux/$vvar/source">source navigation</a>&nbsp;&bull;&nbsp;<a href="linux/$vvar/ident">identifier search</a>&nbsp;&bull;&nbsp;<a href="linux/$vvar/search">freetext search</a>&nbsp;&bull;&nbsp;<a href="http://lxr.free-electrons.com" style="color:#00FF00">Back to LXR</a>&nbsp;&bull;&nbsp;
</p>
</div>
</div>

View file

@ -54,30 +54,51 @@ from collections import OrderedDict
status = 200
form = cgi.FieldStorage()
version = form.getvalue ('v')
if not (version and search ('^[A-Za-z0-9.-]+$', version)):
version = 'v4.10'
url = os.environ['SCRIPT_URL']
m = search ('^/source(.*)$', url)
m = search ('^/e/linux/([^/]*)/([^/]*)(.*)$', url)
if m:
mode = 'source'
path = m.group (1)
if not search ('^[A-Za-z0-9_/.,+-]*$', path):
path = 'INVALID'
url2 = 'source'+path+'?'
version = m.group (1)
cmd = m.group (2)
arg = m.group (3)
if not (version and search ('^[A-Za-z0-9.-]+$', version)):
status = 302
location = '/e/linux/v4.10/'+cmd+arg
cmd = ''
if cmd == 'source':
path = arg
if len (path) > 0 and path[-1] == '/':
path = path[:-1]
status = 301
location = '/e/linux/'+version+'/source'+path
else:
mode = 'source'
if not search ('^[A-Za-z0-9_/.,+-]*$', path):
path = 'INVALID'
url2 = 'source'+path
elif cmd == 'ident':
ident = arg[1:]
form = cgi.FieldStorage()
ident2 = form.getvalue ('i')
if ident == '' and ident2:
status = 302
location = '/e/linux/'+version+'/ident/'+ident2
else:
mode = 'ident'
if not (ident and search ('^[A-Za-z0-9_-]*$', ident)):
ident = ''
url2 = 'ident/'+ident
elif cmd == 'search':
mode = 'search'
url2 = 'search'
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?'
if status == 301:
realprint ('Status: 301 Moved Permanently')
realprint ('Location: '+location+'\n')
exit()
elif status == 302:
realprint ('Status: 302 Found')
realprint ('Location: '+location+'\n')
exit()
head = open ('template-head').read()
head = sub ('\$baseurl', 'http://' + os.environ['HTTP_HOST'], head)
@ -112,7 +133,7 @@ for v1k in va:
v += ' <li onclick="mf2(this);"><span class="mel2">'+v2k+'</span>\n'
v += ' <ul class="subsubmenu">\n'
for v3 in v2v:
v += ' <li><a href="'+url2+'v='+v3+'">'+v3+'</a></li>\n'
v += ' <li><a href="e/linux/'+v3+'/'+url2+'">'+v3+'</a></li>\n'
v += ' </ul></li>\n'
v += ' </ul></li>\n'
v += '</ul>\n'
@ -120,12 +141,12 @@ v += '</ul>\n'
head = sub ('\$versions', v, head)
if mode == 'source':
banner = '<a href="source?v='+version+'">Linux</a>'
banner = '<a href="e/linux/'+version+'/source">Linux</a>'
p2 = ''
p3 = path.split ('/') [1:]
for p in p3:
banner += '/<a href="source'+p2+'/'+p+'?v='+version+'">'+p+'</a>'
p2 += '/'+p
banner += '/<a href="e/linux/'+version+'/source'+p2+'">'+p+'</a>'
head = sub ('\$banner', banner, head)
head = sub ('\$title', 'Linux'+path+' - Linux Cross Reference - Free Electrons', head)
@ -137,12 +158,7 @@ if mode == 'source':
if len (type) == 1:
type = type[0]
if type == 'tree':
if path[-1:] != '/':
lines += shell_exec ('cd ..; ./query.py dir '+version+' \''+path+'\'')
else:
status = 302
path = path[:-1]
location = '/source'+path+'?v='+version
lines += shell_exec ('cd ..; ./query.py dir '+version+' \''+path+'\'')
elif type == 'blob':
lines += shell_exec ('cd ..; ./query.py file '+version+' \''+path+'\'')
else:
@ -176,8 +192,8 @@ if mode == 'source':
name = 'Parent directory'
print (' <tr>')
print (' <td><a href="source'+path2+'?v='+version+'"><img src="/icons/'+icon+'" width="20" height="22" border="0" alt="'+icon+'"/></a></td>')
print (' <td><a href="source'+path2+'?v='+version+'">'+name+'</a></td>')
print (' <td><a href="e/linux/'+version+'/source'+path2+'"><img src="/icons/'+icon+'" width="20" height="22" border="0" alt="'+icon+'"/></a></td>')
print (' <td><a href="e/linux/'+version+'/source'+path2+'">'+name+'</a></td>')
print (' <td>'+size+'</td>')
print (' </tr>\n')
@ -195,7 +211,7 @@ if mode == 'source':
width2 = len (str (num))
space = ' ' * (width1 - width2)
for l in lines:
print (' '+space+'<a name="L'+str(num)+'" href="source'+path+'?v='+version+'#L'+str(num)+'">'+str(num)+'</a> ')
print (' '+space+'<a name="L'+str(num)+'" href="e/linux/'+version+'/source'+path+'#L'+str(num)+'">'+str(num)+'</a> ')
num += 1
width2 = len (str (num))
space = ' ' * (width1 - width2)
@ -205,7 +221,7 @@ if mode == 'source':
for l in lines:
l = cgi.escape (l)
l = sub ('"', '&quot;', l)
l = sub ('\033\[31m(.*?)\033\[0m', '<a href="ident?v='+version+'&i=\\1">\\1</a>', l)
l = sub ('\033\[31m(.*?)\033\[0m', '<a href="e/linux/'+version+'/ident/\\1">\\1</a>', l)
l = sub ('\033\[32m', '<i>', l)
l = sub ('\033\[33m', '<i>', l)
l = sub ('\033\[0m', '</i>', l)
@ -216,7 +232,7 @@ if mode == 'source':
elif mode == 'ident':
field = '</h1>\n<form method="get" action="ident">\n<input type=hidden name="v" value="'+version+'">\nIdentifier: <input type="text" name="i" value="'+ident+'"size="60"/>\n<input type="submit" value="Go get it"/>\n</form>\n<h1>' + ident
field = '</h1>\n<form method="get" action="e/linux/'+version+'/ident">\nIdentifier: <input type="text" name="i" value="'+ident+'"size="60"/>\n<input type="submit" value="Go get it"/>\n</form>\n<h1>' + ident
head = sub ('\$banner', field, head)
head = sub ('\$title', 'Linux identifier search "'+ident+'" - Linux Cross Reference - Free Electrons', head)
print (head, end='')
@ -236,7 +252,7 @@ elif mode == 'ident':
l = next (lines)
m = search ('^(.*): (\d*) \((.*)\)$', l)
f, n, t = m.groups()
print ('<li><a href="source/'+f+'?v='+version+'#L'+n+'">'+f+', line '+n+' (as a '+t+')</a>', end='')
print ('<li><a href="e/linux/'+version+'/source/'+f+'#L'+n+'">'+f+', line '+n+' (as a '+t+')</a>', end='')
print ('</ul>')
next (lines)
@ -253,16 +269,16 @@ elif mode == 'ident':
ln = m.group (2).split (',')
if len (ln) == 1:
n = ln[0]
print ('<li><a href="source/'+f+'?v='+version+'#L'+str(n)+'">'+f+', line '+str(n)+'</a>', end='')
print ('<li><a href="e/linux/'+version+'/source/'+f+'#L'+str(n)+'">'+f+', line '+str(n)+'</a>', end='')
else:
if num > 100: # Concise display
n = len (ln)
print ('<li><a href="source/'+f+'?v='+version+'">'+f+'</a>, '+str(n)+' times')
print ('<li><a href="e/linux/'+version+'/source/'+f+'">'+f+'</a>, '+str(n)+' times')
else: # Verbose display
print ('<li>'+f)
print ('<ul>')
for n in ln:
print ('<li><a href="source/'+f+'?v='+version+'#L'+str(n)+'">line '+str(n)+'</a>')
print ('<li><a href="e/linux/'+version+'/source/'+f+'#L'+str(n)+'">line '+str(n)+'</a>')
print ('</ul>')
print ('</ul>')
else:
@ -285,10 +301,6 @@ print (open ('template-tail').read(), end='')
if status == 404:
realprint ('Status: 404 Not Found')
elif status == 302:
realprint ('Status: 302 Found')
realprint ('Location: '+location+'\n')
exit()
realprint ('Content-Type: text/html;charset=utf-8\n')
realprint (outputBuffer, end='')