#!/usr/bin/env python
# Command line search interface for Synopsis XREF file
# Copyright (c) 2002 by Stephen Davies
# Copyright (c) 2005 by Stefan Seefeld

import sys, cPickle

def usage(status):
    print "Usage: search-xref <data file> list"
    print "  print content of <data file>"
    print "Usage: search-xref <data file> <mode> <string>"
    print "  mode is one of:"
    print "   full    - search for fully scoped name,"
    print "     eg: 'Ptree::Ptree()' will only match that declaration"
    print "   name    - search for name of declaration,"
    print "     eg: 'Ptree' will only match class Ptree or a constructor"
    sys.exit(status)

if len(sys.argv) < 3: usage(1)
mode = sys.argv[2]

if len(sys.argv) == 3 and mode != 'list': usage(1)
if len(sys.argv) == 4:
    if mode not in ['full', 'name']: usage(1)
    else: search = sys.argv[3]


f = open(sys.argv[1], 'rb')
data, index = cPickle.load(f)
f.close()

def dump(name, detail=True):
    if not data.has_key(name): return
    print '::'.join(name)
    if not detail: return
    target_data = data[name]
    if target_data[0]:
	print '  Defined at:'
	for file, line, scope in target_data[0]:
	    print '    %s:%s: %s'%(file,line,'::'.join(scope) or '<global scope>')
    if target_data[1]:
	print '  Called from:'
	for file, line, scope in target_data[1]:
	    print '    %s:%s: %s'%(file,line,'::'.join(scope) or '<global scope>')
    if target_data[2]:
	print '  Referenced from:'
	for file, line, scope in target_data[2]:
	    print '    %s:%s: %s'%(file,line,'::'.join(scope) or '<global scope>')


if mode == 'list':
    for name in data:
	dump(name, detail=False)

elif mode == 'full':
    name = tuple(search.split('::'))
    found = 0
    # Check for exact match
    if data.has_key(name):
	print "Found exact match:"
	dump(name)
	found = 1
    # Search for last part of name in index
    if index.has_key(name[-1]):
	matches = index[name[-1]]
	print "Found (%d) possible matches:"%(len(matches))
	for name in matches:
	    dump(name)
	found = 1
    if not found:
	print "No matches found"

elif mode == 'name':
    if index.has_key(search):
	matches = index[search]
	print "Found (%d) possible matches:"%(len(matches))
	for name in matches:
	    dump(name)
    else:
	print "No matches found"
