Python 0.9.1 part 19/21

Guido van Rossum guido at cwi.nl
Wed Feb 20 04:42:49 AEST 1991


: This is a shell archive.
: Extract with 'sh this_file'.
:
: Extract part 01 first since it makes all directories
echo 'Start of pack.out, part 19 out of 21:'
if test -s 'demo/scripts/xxci.py'
then echo '*** I will not over-write existing file demo/scripts/xxci.py'
else
echo 'x - demo/scripts/xxci.py'
sed 's/^X//' > 'demo/scripts/xxci.py' << 'EOF'
X#! /ufs/guido/bin/sgi/python
X
X# xxci
X#
X# check in files for which rcsdiff returns nonzero exit status
X
Ximport sys
Ximport posix
Ximport stat
Ximport path
Ximport commands
X
XMAXSIZE = 200*1024 # Files this big must be binaries and are skipped.
X
Xdef getargs():
X	args = sys.argv[1:]
X	if args:
X		return args
X	print 'No arguments, checking almost *'
X	for file in posix.listdir('.'):
X		if not skipfile(file):
X			args.append(file)
X	if not args:
X		print 'Nothing to do -- exit 1'
X		sys.exit(1)
X	args.sort()
X	return args
X
Xbadnames = ['tags', 'xyzzy']
Xbadprefixes = ['.', ',', '@', '#', 'o.']
Xbadsuffixes = \
X	['~', '.a', '.o', '.old', '.bak', '.orig', '.new', '.prev', '.not']
X# XXX Should generalize even more to use fnmatch!
X
Xdef skipfile(file):
X	if file in badnames or \
X		badprefix(file) or badsuffix(file) or \
X		path.islink(file) or path.isdir(file):
X		return 1
X	# Skip huge files -- probably binaries.
X	try:
X		st = posix.stat(file)
X	except posix.error:
X		return 1 # Doesn't exist -- skip it
X	return st[stat.ST_SIZE] >= MAXSIZE
X
Xdef badprefix(file):
X	for bad in badprefixes:
X		if file[:len(bad)] = bad: return 1
X	return 0
X
Xdef badsuffix(file):
X	for bad in badsuffixes:
X		if file[-len(bad):] = bad: return 1
X	return 0
X
Xdef go(args):
X	for file in args:
X		print file + ':'
X		if run('rcsdiff -c', file):
X			if askyesno('Check in ' + file + ' ? '):
X				sts = run('rcs -l', file) # ignored
X				# can't use run() here because it's interactive
X				sts = posix.system('ci -l ' + file)
X
Xdef run(cmd, file):
X	sts, output = commands.getstatusoutput(cmd + commands.mkarg(file))
X	if sts:
X		print output
X		print 'Exit status', sts
X	return sts
X
Xdef askyesno(prompt):
X	s = raw_input(prompt)
X	return s in ['y', 'yes']
X
Xgo(getargs())
EOF
chmod +x 'demo/scripts/xxci.py'
fi
if test -s 'demo/sgi/gl_panel/apanel/apanel.s'
then echo '*** I will not over-write existing file demo/sgi/gl_panel/apanel/apanel.s'
else
echo 'x - demo/sgi/gl_panel/apanel/apanel.s'
sed 's/^X//' > 'demo/sgi/gl_panel/apanel/apanel.s' << 'EOF'
X;;; This file was automatically generated by the panel editor.
X;;; If you read it into gnu emacs, it will automagically format itself.
X
X(panel (prop help creator:user-panel-help)
X(prop user-panel #t)
X(label "Audio Control Panel")
X(x 395)
X(y 69)
X(al (pnl_filled_vslider (name "outputgain")
X(prop help creator:user-act-help)
X(label "output gain")
X(x 6.5)
X(y 0.75)
X(w 0.4)
X(h 4.35)
X(val 0.329)
X(labeltype 13)
X(downfunc move-then-resize)
X)
X(pnl_frame (prop help creator:user-frame-help)
X(x 0.25)
X(y 2.75)
X(w 5.1)
X(h 2.3)
X(downfunc move-then-resize)
X(al (pnl_filled_hslider (name "recordsize")
X(prop help creator:user-act-help)
X(label "recording length")
X(x -0.75)
X(w 3.3)
X(h 0.4)
X(val 0.1)
X(labeltype 11)
X(downfunc move-then-resize)
X)
X(pnl_label (prop help creator:user-act-help)
X(label "(max 10 seconds)")
X(x -0.75)
X(y -0.75)
X(downfunc move-then-resize)
X)
X(pnl_wide_button (name "recordbutton")
X(prop help creator:user-act-help)
X(label "record from microphone...")
X(x -0.75)
X(y 0.75)
X(w 4.7)
X(downfunc move-then-resize)
X)
X)
X)
X(pnl_wide_button (name "playbackbutton")
X(prop help creator:user-act-help)
X(label "playback to speaker")
X(x 0.25)
X(y 2)
X(w 5.15)
X(downfunc move-then-resize)
X)
X(pnl_wide_button (name "quitbutton")
X(prop help creator:user-act-help)
X(label "quit")
X(x 0.25)
X(y 0.25)
X(w 1.75)
X(downfunc move-then-resize)
X)
X)
X)
X;;; Local Variables:
X;;; mode: scheme
X;;; eval: (save-excursion (goto-char (point-min)) (kill-line 3))
X;;; eval: (save-excursion (goto-char (point-min)) (replace-regexp "[ \n]*)" ")"))
X;;; eval: (indent-region (point-min) (point-max) nil)
X;;; eval: (progn (kill-line -3) (delete-backward-char 1) (save-buffer))
X;;; End:
EOF
fi
if test -s 'demo/sgi/gl_panel/flying/light.s'
then echo '*** I will not over-write existing file demo/sgi/gl_panel/flying/light.s'
else
echo 'x - demo/sgi/gl_panel/flying/light.s'
sed 's/^X//' > 'demo/sgi/gl_panel/flying/light.s' << 'EOF'
X;;; This file was automatically generated by the panel editor.
X;;; If you read it into gnu emacs, it will automagically format itself.
X
X(panel (prop help creator:user-panel-help)
X(prop user-panel #t)
X(label "Light Sources")
X(al (pnl_hslider (name "X")
X(prop help creator:user-act-help)
X(label "x")
X(y 4)
X(w 3.3)
X(h 0.4)
X(val 0.50)
X(labeltype 0)
X(downfunc move-then-resize)
X)
X(pnl_hslider (name "Y")
X(prop help creator:user-act-help)
X(label "y")
X(y 3.5)
X(w 3.3)
X(h 0.4)
X(val 0.50)
X(labeltype 0)
X(downfunc move-then-resize)
X)
X(pnl_hslider (name "Z")
X(prop help creator:user-act-help)
X(label "z")
X(y 3)
X(w 3.3)
X(h 0.4)
X(val 0.50)
X(labeltype 0)
X(downfunc move-then-resize)
X)
X(pnl_radio_button (name "light2")
X(prop help creator:user-act-help)
X(label "light 2")
X(x 5)
X(y 4)
X(h 0.36)
X(downfunc move-then-resize)
X)
X(pnl_radio_button (name "light1")
X(prop help creator:user-act-help)
X(label "light 1")
X(x 5)
X(y 4.5)
X(h 0.36)
X(downfunc move-then-resize)
X)
X(pnl_toggle_button (name "local")
X(prop help creator:user-act-help)
X(label "local")
X(x 5)
X(y 2.75)
X(downfunc move-then-resize)
X)
X(pnl_filled_hslider (name "B")
X(prop help creator:user-act-help)
X(label "B")
X(w 3.3)
X(h 0.4)
X(labeltype 0)
X(downfunc move-then-resize)
X)
X(pnl_filled_hslider (name "G")
X(prop help creator:user-act-help)
X(label "G")
X(y 0.5)
X(w 3.3)
X(h 0.4)
X(labeltype 0)
X(downfunc move-then-resize)
X)
X(pnl_filled_hslider (name "R")
X(prop help creator:user-act-help)
X(label "R")
X(y 1)
X(w 3.3)
X(h 0.4)
X(labeltype 0)
X(downfunc move-then-resize)
X)
X)
X)
X;;; Local Variables:
X;;; mode: scheme
X;;; eval: (save-excursion (goto-char (point-min)) (kill-line 3))
X;;; eval: (save-excursion (goto-char (point-min)) (replace-regexp "[ \n]*)" ")"))
X;;; eval: (indent-region (point-min) (point-max) nil)
X;;; eval: (progn (kill-line -3) (delete-backward-char 1) (save-buffer))
X;;; End:
EOF
fi
if test -s 'demo/sgi/gl_panel/flying/panel.s'
then echo '*** I will not over-write existing file demo/sgi/gl_panel/flying/panel.s'
else
echo 'x - demo/sgi/gl_panel/flying/panel.s'
sed 's/^X//' > 'demo/sgi/gl_panel/flying/panel.s' << 'EOF'
X;;; This file was automatically generated by the panel editor.
X;;; If you read it into gnu emacs, it will automagically format itself.
X
X(panel (prop help creator:user-panel-help)
X(prop user-panel #t)
X(label "Panel Control")
X(x 394)
X(y 622)
X(al (pnl_radio_button (name "button4")
X(prop help creator:user-act-help)
X(y 2.5)
X(h 0.36)
X(downfunc move-then-resize)
X)
X(pnl_radio_button (name "button3")
X(prop help creator:user-act-help)
X(y 3)
X(h 0.36)
X(downfunc move-then-resize)
X)
X(pnl_radio_button (name "button2")
X(prop help creator:user-act-help)
X(y 3.5)
X(h 0.36)
X(downfunc move-then-resize)
X)
X(pnl_radio_button (name "button1")
X(prop help creator:user-act-help)
X(y 4)
X(h 0.36)
X(downfunc move-then-resize)
X)
X(pnl_wide_button (name "title1")
X(prop help creator:user-act-help)
X(x 0.75)
X(y 4.75)
X(w 2.44)
X(downfunc move-then-resize)
X)
X(pnl_wide_button (name "title2")
X(prop help creator:user-act-help)
X(x 3.5)
X(y 4.75)
X(w 2.44)
X(downfunc move-then-resize)
X)
X(pnl_wide_button (name "title3")
X(prop help creator:user-act-help)
X(x 6.25)
X(y 4.75)
X(w 2.44)
X(downfunc move-then-resize)
X)
X(pnl_wide_button (name "title4")
X(prop help creator:user-act-help)
X(x 9)
X(y 4.75)
X(w 2.44)
X(downfunc move-then-resize)
X)
X(pnl_button (name "root")
X(prop help creator:user-act-help)
X(label "R")
X(y 4.75)
X(labeltype 16)
X(downfunc move-then-resize)
X)
X)
X)
X;;; Local Variables:
X;;; mode: scheme
X;;; eval: (save-excursion (goto-char (point-min)) (kill-line 3))
X;;; eval: (save-excursion (goto-char (point-min)) (replace-regexp "[ \n]*)" ")"))
X;;; eval: (indent-region (point-min) (point-max) nil)
X;;; eval: (progn (kill-line -3) (delete-backward-char 1) (save-buffer))
X;;; End:
EOF
fi
if test -s 'lib/Abstract.py'
then echo '*** I will not over-write existing file lib/Abstract.py'
else
echo 'x - lib/Abstract.py'
sed 's/^X//' > 'lib/Abstract.py' << 'EOF'
X# Abstract classes for parents and children.
X#
X# Do not use as base class -- this is for documentation only.
X#
X# Note that the tree must be built top down (create the parent,
X# then add the children).
X#
X# Also note that the creation methods are not standardized --
X# these have extra parameters dependent on the widget type.
X# For historical reasons, button creation methods are called
X# define() while split creation methods are called create().
X
Xclass AbstractParent():
X	#
X	# Upcalls from child to parent
X	#
X	def addchild(self, child): unimpl()
X	def delchild(self, child): unimpl()
X	#
X	def need_mouse(self, child): unimpl()
X	def no_mouse(self, child): unimpl()
X	#
X	def need_timer(self, child): unimpl()
X	def no_timer(self, child): unimpl()
X	#
X	# XXX need_kbd, no_kbd; focus???
X	#
X	def begindrawing(self): return unimpl()
X	def beginmeasuring(self): return unimpl()
X	#
X	def change(self, area): unimpl()
X	def scroll(self, (area, (dh, dv))): unimpl()
X	def settimer(self, itimer): unimpl()
X
Xclass AbstractChild():
X	#
X	# Downcalls from parent to child
X	#
X	def destroy(self): unimpl()
X	#
X	def minsize(self, m): return unimpl()
X	def getbounds(self): return unimpl()
X	def setbounds(self, bounds): unimpl()
X	def draw(self, (d, area)): unimpl()
X	#
X	# Downcalls only made after certain upcalls
X	#
X	def mouse_down(self, detail): unimpl()
X	def mouse_move(self, detail): unimpl()
X	def mouse_up(self, detail): unimpl()
X	#
X	def timer(self): unimpl()
X
X# A "Split" is a child that manages one or more children.
X# (This terminology is due to DEC SRC, except for CSplits.)
X# A child of a split may be another split, a button, a slider, etc.
X# Certain upcalls and downcalls can be handled transparently, but
X# for others (e.g., all geometry related calls) this is not possible.
X
Xclass AbstractSplit() = AbstractChild(), AbstractParent():
X	pass
EOF
fi
if test -s 'lib/StripChart.py'
then echo '*** I will not over-write existing file lib/StripChart.py'
else
echo 'x - lib/StripChart.py'
sed 's/^X//' > 'lib/StripChart.py' << 'EOF'
X# Module 'StripChart'
X
Ximport rect
Xfrom Buttons import LabelAppearance, NoReactivity
X
X# A StripChart doesn't really look like a label but it needs a base class.
X# LabelAppearance allows it to be disabled and hilited.
X
Xclass StripChart() = LabelAppearance(), NoReactivity():
X	#
X	def define(self, (parent, scale)):
X		self.parent = parent
X		parent.addchild(self)
X		self.init_appearance()
X		self.init_reactivity()
X		self.ydata = []
X		self.scale = scale
X		self.resetbounds()
X		return self
X	#
X	def setbounds(self, bounds):
X		LabelAppearance.setbounds(self, bounds)
X		self.resetbounds()
X	#
X	def resetbounds(self):
X		(left, top), (right, bottom) = self.bounds
X		self.width = right-left
X		self.height = bottom-top
X		excess = len(self.ydata) - self.width
X		if excess > 0:
X			del self.ydata[:excess]
X		elif excess < 0:
X			while len(self.ydata) < self.width:
X				self.ydata.insert(0, 0)
X	#
X	def append(self, y):
X		self.ydata.append(y)
X		excess = len(self.ydata) - self.width
X		if excess > 0:
X			del self.ydata[:excess]
X			if self.bounds <> rect.empty:
X				self.parent.scroll(self.bounds, (-excess, 0))
X		if self.bounds <> rect.empty:
X			(left, top), (right, bottom) = self.bounds
X			i = len(self.ydata)
X			area = (left+i-1, top), (left+i, bottom)
X			self.draw(self.parent.begindrawing(), area)
X	#
X	def draw(self, (d, area)):
X		area = rect.intersect(area, self.bounds)
X		if area = rect.empty:
X			print 'mt'
X			return
X		d.cliprect(area)
X		d.erase(self.bounds)
X		(a_left, a_top), (a_right, a_bottom) = area
X		(left, top), (right, bottom) = self.bounds
X		height = bottom - top
X		i1 = a_left - left
X		i2 = a_right - left
X		for i in range(max(0, i1), min(len(self.ydata), i2)):
X			split = bottom-self.ydata[i]*height/self.scale
X			d.paint((left+i, split), (left+i+1, bottom))
X		if not self.enabled:
X			self.flipenable(d)
X		if self.hilited:
X			self.fliphilite(d)
X		d.noclip()
EOF
fi
if test -s 'lib/cmp.py'
then echo '*** I will not over-write existing file lib/cmp.py'
else
echo 'x - lib/cmp.py'
sed 's/^X//' > 'lib/cmp.py' << 'EOF'
X# Module 'cmp'
X
X# Efficiently compare files, boolean outcome only (equal / not equal).
X
X# Tricks (used in this order):
X#	- Files with identical type, size & mtime are assumed to be clones
X#	- Files with different type or size cannot be identical
X#	- We keep a cache of outcomes of earlier comparisons
X#	- We don't fork a process to run 'cmp' but read the files ourselves
X
Ximport posix
X
Xcache = {}
X
Xdef cmp(f1, f2): # Compare two files, use the cache if possible.
X	# Return 1 for identical files, 0 for different.
X	# Raise exceptions if either file could not be statted, read, etc.
X	s1, s2 = sig(posix.stat(f1)), sig(posix.stat(f2))
X	if s1[0] <> 8 or s2[0] <> 8:
X		# Either is a not a plain file -- always report as different
X		return 0
X	if s1 = s2:
X		# type, size & mtime match -- report same
X		return 1
X	if s1[:2] <> s2[:2]: # Types or sizes differ, don't bother
X		# types or sizes differ -- report different
X		return 0
X	# same type and size -- look in the cache
X	key = f1 + ' ' + f2
X	try:
X		cs1, cs2, outcome = cache[key]
X		# cache hit
X		if s1 = cs1 and s2 = cs2:
X			# cached signatures match
X			return outcome
X		# stale cached signature(s)
X	except RuntimeError:
X		# cache miss
X		pass
X	# really compare
X	outcome = do_cmp(f1, f2)
X	cache[key] = s1, s2, outcome
X	return outcome
X
Xdef sig(st): # Return signature (i.e., type, size, mtime) from raw stat data
X	# 0-5: st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid
X	# 6-9: st_size, st_atime, st_mtime, st_ctime
X	type = st[0] / 4096
X	size = st[6]
X	mtime = st[8]
X	return type, size, mtime
X
Xdef do_cmp(f1, f2): # Compare two files, really
X	bufsize = 8096 # Could be tuned
X	fp1 = open(f1, 'r')
X	fp2 = open(f2, 'r')
X	while 1:
X		b1 = fp1.read(bufsize)
X		b2 = fp2.read(bufsize)
X		if b1 <> b2: return 0
X		if not b1: return 1
EOF
fi
if test -s 'lib/commands.py'
then echo '*** I will not over-write existing file lib/commands.py'
else
echo 'x - lib/commands.py'
sed 's/^X//' > 'lib/commands.py' << 'EOF'
X# Module 'commands'
X#
X# Various tools for executing commands and looking at their output and status.
X
Ximport rand
Ximport posix
Ximport stat
Ximport path
X
X
X# Get 'ls -l' status for an object into a string
X#
Xdef getstatus(file):
X	return getoutput('ls -ld' + mkarg(file))
X
X
X# Get the output from a shell command into a string.
X# The exit status is ignored; a trailing newline is stripped.
X# Assume the command will work with ' >tempfile 2>&1' appended.
X# XXX This should use posix.popen() instead, should it exist.
X#
Xdef getoutput(cmd):
X	return getstatusoutput(cmd)[1]
X
X
X# Ditto but preserving the exit status.
X# Returns a pair (sts, output)
X#
Xdef getstatusoutput(cmd):
X	tmp = '/usr/tmp/wdiff' + `rand.rand()`
X	sts = -1
X	try:
X		sts = posix.system(cmd + ' >' + tmp + ' 2>&1')
X		text = readfile(tmp)
X	finally:
X		altsts = posix.system('rm -f ' + tmp)
X	if text[-1:] = '\n': text = text[:-1]
X	return sts, text
X
X
X# Return a string containing a file's contents.
X#
Xdef readfile(fn):
X	st = posix.stat(fn)
X	size = st[stat.ST_SIZE]
X	if not size: return ''
X	try:
X		fp = open(fn, 'r')
X	except:
X		raise posix.error, 'readfile(' + fn + '): open failed'
X	try:
X		return fp.read(size)
X	except:
X		raise posix.error, 'readfile(' + fn + '): read failed'
X
X
X# Make command argument from directory and pathname (prefix space, add quotes).
X#
Xdef mk2arg(head, x):
X	return mkarg(path.cat(head, x))
X
X
X# Make a shell command argument from a string.
X# Two strategies: enclose in single quotes if it contains none;
X# otherwis, enclose in double quotes and prefix quotable characters
X# with backslash.
X#
Xdef mkarg(x):
X	if '\'' not in x:
X		return ' \'' + x + '\''
X	s = ' "'
X	for c in x:
X		if c in '\\$"':
X			s = s + '\\'
X		s = s + c
X	s = s + '"'
X	return s
EOF
fi
if test -s 'lib/getopt.py'
then echo '*** I will not over-write existing file lib/getopt.py'
else
echo 'x - lib/getopt.py'
sed 's/^X//' > 'lib/getopt.py' << 'EOF'
X# module getopt -- Standard command line processing.
X
X# Function getopt.getopt() has a different interface but provides the
X# same functionality as the Unix getopt() function.
X
X# It has two arguments: the first should be argv[1:] (it doesn't want
X# the script name), the second the string of option letters as passed
X# to Unix getopt() (i.e., a string of allowable option letters, with
X# options requiring an argument followed by a colon).
X
X# It raises the exception getopt.error with a string argument if it
X# detects an error.
X
X# It returns two items:
X# (1)	a list of pairs (option, option_argument) giving the options in
X#	the order in which they were specified.  (I'd use a dictionary
X#	but applications may depend on option order or multiple
X#	occurrences.)  Boolean options have '' as option_argument.
X# (2)	the list of remaining arguments (may be empty).
X
Xerror = 'getopt error'
X
Xdef getopt(args, options):
X    list = []
X    while args and args[0][0] = '-' and args[0] <> '-':
X    	if args[0] = '--':
X    	    args = args[1:]
X    	    break
X    	optstring, args = args[0][1:], args[1:]
X    	while optstring <> '':
X    	    opt, optstring = optstring[0], optstring[1:]
X    	    if classify(opt, options): # May raise exception as well
X    	    	if optstring = '':
X    	    	    if not args:
X    	    	    	raise error, 'option -' + opt + ' requires argument'
X    	    	    optstring, args = args[0], args[1:]
X    	    	optarg, optstring = optstring, ''
X    	    else:
X    	    	optarg = ''
X    	    list.append('-' + opt, optarg)
X    return list, args
X
Xdef classify(opt, options): # Helper to check type of option
X    for i in range(len(options)):
X	if opt = options[i] <> ':':
X	    return options[i+1:i+2] = ':'
X    raise error, 'option -' + opt + ' not recognized'
EOF
fi
if test -s 'lib/rect.py'
then echo '*** I will not over-write existing file lib/rect.py'
else
echo 'x - lib/rect.py'
sed 's/^X//' > 'lib/rect.py' << 'EOF'
X# Module 'rect'.
X#
X# Operations on rectangles.
X# There is some normalization: all results return the object 'empty'
X# if their result would contain no points.
X
X
X# Exception.
X#
Xerror = 'rect.error'
X
X
X# The empty rectangle.
X#
Xempty = (0, 0), (0, 0)
X
X
X# Check if a rectangle is empty.
X#
Xdef is_empty((left, top), (right, bottom)):
X	return left >= right or top >= bottom
X
X
X# Compute the intersection or two or more rectangles.
X# This works with a list or tuple argument.
X#
Xdef intersect(list):
X	if not list: raise error, 'intersect called with empty list'
X	if is_empty(list[0]): return empty
X	(left, top), (right, bottom) = list[0]
X	for rect in list[1:]:
X		if is_empty(rect):
X			return empty
X		(l, t), (r, b) = rect
X		if left < l: left = l
X		if top < t: top = t
X		if right > r: right = r
X		if bottom > b: bottom = b
X		if is_empty((left, top), (right, bottom)):
X			return empty
X	return (left, top), (right, bottom)
X
X
X# Compute the smallest rectangle containing all given rectangles.
X# This works with a list or tuple argument.
X#
Xdef union(list):
X	(left, top), (right, bottom) = empty
X	for (l, t), (r, b) in list[1:]:
X		if not is_empty((l, t), (r, b)):
X			if l < left: left = l
X			if t < top: top = t
X			if r > right: right = r
X			if b > bottom: bottom = b
X	res = (left, top), (right, bottom)
X	if is_empty(res):
X		return empty
X	return res
X
X
X# Check if a point is in a rectangle.
X#
Xdef pointinrect((h, v), ((left, top), (right, bottom))):
X	return left <= h < right and top <= v < bottom
X
X
X# Return a rectangle that is dh, dv inside another
X#
Xdef inset(((left, top), (right, bottom)), (dh, dv)):
X	left = left + dh
X	top = top + dv
X	right = right - dh
X	bottom = bottom - dv
X	r = (left, top), (right, bottom)
X	if is_empty(r):
X		return empty
X	else:
X		return r
X
X
X# Conversions between rectangles and 'geometry tuples',
X# given as origin (h, v) and dimensions (width, height).
X#
Xdef rect2geom((left, top), (right, bottom)):
X	return (left, top), (right-left, bottom-top)
X
Xdef geom2rect((h, v), (width, height)):
X	return (h, v), (h+width, v+height)
EOF
fi
if test -s 'lib/selection.py'
then echo '*** I will not over-write existing file lib/selection.py'
else
echo 'x - lib/selection.py'
sed 's/^X//' > 'lib/selection.py' << 'EOF'
X# DAWKINS' BLIND WATCHMAKER -- "METHINKS IT IS LIKE A WEASEL" EXAMPLE
X#
X# Start with a random phrase.  Successively:
X# Breed a next generation by copying with small mutations.
X# Choose the member of the next generation that most closely resembles
X# the target phrase to start breeding the next generation.
X# How many generations will it take before the target is reached?
X
Xfrom whrandom import random
X
X# Parameters determining the problem space
XTarget = 'methinks it is like a weasel'
XAlphabet = ' abcdefghijklmnopqrstuvwxyz'
X
X# Parameters to play with
XGenerationSize = 100
XMutationChance = 1.0/28.0
X
XIndexSet = range(len(Target))	# Used to speed up loops
X
Xdef choice(sequence):
X	n = len(sequence)
X	i = int(random() * float(n)) % n
X	return sequence[i]
X
Xdef random_phrase():
X	phrase = ''
X	for i in IndexSet:
X		phrase = phrase + choice(Alphabet)
X	return phrase
X
Xdef mutate_phrase(phrase):
X	mutant = phrase
X	for i in range(int(0.5 + MutationChance*float(len(phrase)))):
X		i = choice(IndexSet)
X		c = choice(Alphabet)
X		mutant = mutant[:i] + c + mutant[i+1:]
X	#print `mutant`
X	return mutant
X
Xdef matching_factor(phrase):
X	factor = 0
X	for i in IndexSet:
X		if phrase[i] = Target[i]: factor = factor + 1
X	return factor
X
Xdef breed_generation(phrase):
X	generation = [phrase]
X	while len(generation) < GenerationSize:
X		generation.append(mutate_phrase(phrase))
X	return generation
X
Xdef select_best(generation):
X	factor, selected = -1, ''
X	for phrase in generation:
X		f = matching_factor(phrase)
X		if f > factor:
X			factor, selected = f, phrase
X	return selected
X
Xdef main():
X	gen = 0
X	phrase = random_phrase()
X	print gen, `phrase`
X	while phrase <> Target:
X		next_generation = breed_generation(phrase)
X		#print next_generation
X		phrase = select_best(next_generation)
X		gen = gen+1
X		print gen, `phrase`
X
Xmain()
EOF
fi
if test -s 'lib/statcache.py'
then echo '*** I will not over-write existing file lib/statcache.py'
else
echo 'x - lib/statcache.py'
sed 's/^X//' > 'lib/statcache.py' << 'EOF'
X# Module 'statcache'
X#
X# Maintain a cache of file stats.
X# There are functions to reset the cache or to selectively remove items.
X
Ximport posix
Xfrom stat import *
X
X# The cache.
X# Keys are pathnames, values are `posix.stat' outcomes.
X#
Xcache = {}
X
X
X# Stat a file, possibly out of the cache.
X#
Xdef stat(path):
X	if cache.has_key(path):
X		return cache[path]
X	cache[path] = ret = posix.stat(path)
X	return ret
X
X
X# Reset the cache completely.
X# Hack: to reset a global variable, we import this module.
X#
Xdef reset():
X	import statcache
X	# Check that we really imported the same module
X	if cache is not statcache.cache:
X		raise 'sorry, statcache identity crisis'
X	statcache.cache = {}
X
X
X# Remove a given item from the cache, if it exists.
X#
Xdef forget(path):
X	if cache.has_key(path):
X		del cache[path]
X
X
X# Remove all pathnames with a given prefix.
X#
Xdef forget_prefix(prefix):
X	n = len(prefix)
X	for path in cache.keys():
X		if path[:n] = prefix:
X			del cache[path]
X
X
X# Forget about a directory and all entries in it, but not about
X# entries in subdirectories.
X#
Xdef forget_dir(prefix):
X	if prefix[-1:] = '/' and prefix <> '/':
X		prefix = prefix[:-1]
X	forget(prefix)
X	if prefix[-1:] <> '/':
X		prefix = prefix + '/'
X	n = len(prefix)
X	for path in cache.keys():
X		if path[:n] = prefix:
X			rest = path[n:]
X			if rest[-1:] = '/': rest = rest[:-1]
X			if '/' not in rest:
X				del cache[path]
X
X
X# Remove all pathnames except with a given prefix.
X# Normally used with prefix = '/' after a chdir().
X#
Xdef forget_except_prefix(prefix):
X	n = len(prefix)
X	for path in cache.keys():
X		if path[:n] <> prefix:
X			del cache[path]
X
X
X# Check for directory.
X#
Xdef isdir(path):
X	try:
X		st = stat(path)
X	except posix.error:
X		return 0
X	return S_ISDIR(st[ST_MODE])
EOF
fi
if test -s 'src/allobjects.h'
then echo '*** I will not over-write existing file src/allobjects.h'
else
echo 'x - src/allobjects.h'
sed 's/^X//' > 'src/allobjects.h' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/* "allobjects.c" -- Source for precompiled header "allobjects.h" */
X
X#include <stdio.h>
X#include <string.h>
X
X#include "PROTO.h"
X
X#include "object.h"
X#include "objimpl.h"
X
X#include "intobject.h"
X#include "floatobject.h"
X#include "stringobject.h"
X#include "tupleobject.h"
X#include "listobject.h"
X#include "dictobject.h"
X#include "methodobject.h"
X#include "moduleobject.h"
X#include "funcobject.h"
X#include "classobject.h"
X#include "fileobject.h"
X
X#include "errors.h"
X#include "malloc.h"
X
Xextern char *strdup PROTO((const char *));
EOF
fi
if test -s 'src/bitset.h'
then echo '*** I will not over-write existing file src/bitset.h'
else
echo 'x - src/bitset.h'
sed 's/^X//' > 'src/bitset.h' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/* Bitset interface */
X
X#define BYTE		char
X
Xtypedef BYTE *bitset;
X
Xbitset newbitset PROTO((int nbits));
Xvoid delbitset PROTO((bitset bs));
X/* int testbit PROTO((bitset bs, int ibit)); /* Now a macro, see below */
Xint addbit PROTO((bitset bs, int ibit)); /* Returns 0 if already set */
Xint samebitset PROTO((bitset bs1, bitset bs2, int nbits));
Xvoid mergebitset PROTO((bitset bs1, bitset bs2, int nbits));
X
X#define BITSPERBYTE	(8*sizeof(BYTE))
X#define NBYTES(nbits)	(((nbits) + BITSPERBYTE - 1) / BITSPERBYTE)
X
X#define BIT2BYTE(ibit)	((ibit) / BITSPERBYTE)
X#define BIT2SHIFT(ibit)	((ibit) % BITSPERBYTE)
X#define BIT2MASK(ibit)	(1 << BIT2SHIFT(ibit))
X#define BYTE2BIT(ibyte)	((ibyte) * BITSPERBYTE)
X
X#define testbit(ss, ibit) (((ss)[BIT2BYTE(ibit)] & BIT2MASK(ibit)) != 0)
EOF
fi
if test -s 'src/cgensupport.h'
then echo '*** I will not over-write existing file src/cgensupport.h'
else
echo 'x - src/cgensupport.h'
sed 's/^X//' > 'src/cgensupport.h' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/* Definitions used by cgen output */
X
Xtypedef char *string;
X
X#define mknewlongobject(x) newintobject(x)
X#define mknewshortobject(x) newintobject((long)x)
X#define mknewfloatobject(x) newfloatobject(x)
X
Xextern object *mknewcharobject PROTO((int c));
X
Xextern int getiobjectarg PROTO((object *args, int nargs, int i, object **p_a));
Xextern int getilongarg PROTO((object *args, int nargs, int i, long *p_a));
Xextern int getishortarg PROTO((object *args, int nargs, int i, short *p_a));
Xextern int getifloatarg PROTO((object *args, int nargs, int i, float *p_a));
Xextern int getistringarg PROTO((object *args, int nargs, int i, string *p_a));
EOF
fi
if test -s 'src/classobject.h'
then echo '*** I will not over-write existing file src/classobject.h'
else
echo 'x - src/classobject.h'
sed 's/^X//' > 'src/classobject.h' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/* Class object interface */
X
X/*
XClasses are really hacked in at the last moment.
XIt should be possible to use other object types as base classes,
Xbut currently it isn't.  We'll see if we can fix that later, sigh...
X*/
X
Xextern typeobject Classtype, Classmembertype, Classmethodtype;
X
X#define is_classobject(op) ((op)->ob_type == &Classtype)
X#define is_classmemberobject(op) ((op)->ob_type == &Classmembertype)
X#define is_classmethodobject(op) ((op)->ob_type == &Classmethodtype)
X
Xextern object *newclassobject PROTO((object *, object *));
Xextern object *newclassmemberobject PROTO((object *));
Xextern object *newclassmethodobject PROTO((object *, object *));
X
Xextern object *classmethodgetfunc PROTO((object *));
Xextern object *classmethodgetself PROTO((object *));
EOF
fi
if test -s 'src/compile.h'
then echo '*** I will not over-write existing file src/compile.h'
else
echo 'x - src/compile.h'
sed 's/^X//' > 'src/compile.h' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/* Definitions for compiled intermediate code */
X
X
X/* An intermediate code fragment contains:
X   - a string that encodes the instructions,
X   - a list of the constants,
X   - and a list of the names used. */
X
Xtypedef struct {
X	OB_HEAD
X	stringobject *co_code;	/* instruction opcodes */
X	object *co_consts;	/* list of immutable constant objects */
X	object *co_names;	/* list of stringobjects */
X	object *co_filename;	/* string */
X} codeobject;
X
Xextern typeobject Codetype;
X
X#define is_codeobject(op) ((op)->ob_type == &Codetype)
X
X
X/* Public interface */
Xcodeobject *compile PROTO((struct _node *, char *));
EOF
fi
if test -s 'src/dictobject.h'
then echo '*** I will not over-write existing file src/dictobject.h'
else
echo 'x - src/dictobject.h'
sed 's/^X//' > 'src/dictobject.h' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/*
XDictionary object type -- mapping from char * to object.
XNB: the key is given as a char *, not as a stringobject.
XThese functions set errno for errors.  Functions dictremove() and
Xdictinsert() return nonzero for errors, getdictsize() returns -1,
Xthe others NULL.  A successful call to dictinsert() calls INCREF()
Xfor the inserted item.
X*/
X
Xextern typeobject Dicttype;
X
X#define is_dictobject(op) ((op)->ob_type == &Dicttype)
X
Xextern object *newdictobject PROTO((void));
Xextern object *dictlookup PROTO((object *dp, char *key));
Xextern int dictinsert PROTO((object *dp, char *key, object *item));
Xextern int dictremove PROTO((object *dp, char *key));
Xextern int getdictsize PROTO((object *dp));
Xextern char *getdictkey PROTO((object *dp, int i));
Xextern object *getdictkeys PROTO((object *dp));
EOF
fi
if test -s 'src/errors.h'
then echo '*** I will not over-write existing file src/errors.h'
else
echo 'x - src/errors.h'
sed 's/^X//' > 'src/errors.h' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/* Error handling definitions */
X
Xvoid err_set PROTO((object *));
Xvoid err_setval PROTO((object *, object *));
Xvoid err_setstr PROTO((object *, char *));
Xint err_occurred PROTO((void));
Xvoid err_get PROTO((object **, object **));
Xvoid err_clear PROTO((void));
X
X/* Predefined exceptions */
X
Xextern object *RuntimeError;
Xextern object *EOFError;
Xextern object *TypeError;
Xextern object *MemoryError;
Xextern object *NameError;
Xextern object *SystemError;
Xextern object *KeyboardInterrupt;
X
X/* Some more planned for the future */
X
X#define IndexError		RuntimeError
X#define KeyError		RuntimeError
X#define ZeroDivisionError	RuntimeError
X#define OverflowError		RuntimeError
X
X/* Convenience functions */
X
Xextern int err_badarg PROTO((void));
Xextern object *err_nomem PROTO((void));
Xextern object *err_errno PROTO((object *));
Xextern void err_input PROTO((int));
X
Xextern void err_badcall PROTO((void));
EOF
fi
if test -s 'src/grammar1.c'
then echo '*** I will not over-write existing file src/grammar1.c'
else
echo 'x - src/grammar1.c'
sed 's/^X//' > 'src/grammar1.c' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/* Grammar subroutines needed by parser */
X
X#include "pgenheaders.h"
X#include "assert.h"
X#include "grammar.h"
X#include "token.h"
X
X/* Return the DFA for the given type */
X
Xdfa *
Xfinddfa(g, type)
X	grammar *g;
X	register int type;
X{
X	register int i;
X	register dfa *d;
X	
X	for (i = g->g_ndfas, d = g->g_dfa; --i >= 0; d++) {
X		if (d->d_type == type)
X			return d;
X	}
X	assert(0);
X	/* NOTREACHED */
X}
X
Xchar *
Xlabelrepr(lb)
X	label *lb;
X{
X	static char buf[100];
X	
X	if (lb->lb_type == ENDMARKER)
X		return "EMPTY";
X	else if (ISNONTERMINAL(lb->lb_type)) {
X		if (lb->lb_str == NULL) {
X			sprintf(buf, "NT%d", lb->lb_type);
X			return buf;
X		}
X		else
X			return lb->lb_str;
X	}
X	else {
X		if (lb->lb_str == NULL)
X			return tok_name[lb->lb_type];
X		else {
X			sprintf(buf, "%.32s(%.32s)",
X				tok_name[lb->lb_type], lb->lb_str);
X			return buf;
X		}
X	}
X}
EOF
fi
if test -s 'src/malloc.h'
then echo '*** I will not over-write existing file src/malloc.h'
else
echo 'x - src/malloc.h'
sed 's/^X//' > 'src/malloc.h' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/* Lowest-level memory allocation interface */
X
X#ifdef THINK_C
X#define ANY void
X#ifndef THINK_C_3_0
X#define HAVE_STDLIB
X#endif
X#endif
X
X#ifdef __STD_C__
X#define ANY void
X#define HAVE_STDLIB
X#endif
X
X#ifndef ANY
X#define ANY char
X#endif
X
X#ifndef NULL
X#define NULL 0
X#endif
X
X#define NEW(type, n) ( (type *) malloc((n) * sizeof(type)) )
X#define RESIZE(p, type, n) \
X	if ((p) == NULL) \
X		(p) =  (type *) malloc((n) * sizeof(type)); \
X	else \
X		(p) = (type *) realloc((char *)(p), (n) * sizeof(type))
X#define DEL(p) free((char *)p)
X#define XDEL(p) if ((p) == NULL) ; else DEL(p)
X
X#ifdef HAVE_STDLIB
X#include <stdlib.h>
X#else
Xextern ANY *malloc PROTO((unsigned int));
Xextern ANY *calloc PROTO((unsigned int, unsigned int));
Xextern ANY *realloc PROTO((ANY *, unsigned int));
Xextern void free PROTO((ANY *)); /* XXX sometimes int on Unix old systems */
X#endif
EOF
fi
if test -s 'src/node.h'
then echo '*** I will not over-write existing file src/node.h'
else
echo 'x - src/node.h'
sed 's/^X//' > 'src/node.h' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/* Parse tree node interface */
X
Xtypedef struct _node {
X	int		n_type;
X	char		*n_str;
X	int		n_lineno;
X	int		n_nchildren;
X	struct _node	*n_child;
X} node;
X
Xextern node *newtree PROTO((int type));
Xextern node *addchild PROTO((node *n, int type, char *str, int lineno));
Xextern void freetree PROTO((node *n));
X
X/* Node access functions */
X#define NCH(n)		((n)->n_nchildren)
X#define CHILD(n, i)	(&(n)->n_child[i])
X#define TYPE(n)		((n)->n_type)
X#define STR(n)		((n)->n_str)
X
X/* Assert that the type of a node is what we expect */
X#ifndef DEBUG
X#define REQ(n, type) { /*pass*/ ; }
X#else
X#define REQ(n, type) \
X	{ if (TYPE(n) != (type)) { \
X		fprintf(stderr, "FATAL: node type %d, required %d\n", \
X			TYPE(n), type); \
X		abort(); \
X	} }
X#endif
X
Xextern void listtree PROTO((node *));
Xextern void listnode PROTO((FILE *, node *));
EOF
fi
if test -s 'src/parser.h'
then echo '*** I will not over-write existing file src/parser.h'
else
echo 'x - src/parser.h'
sed 's/^X//' > 'src/parser.h' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/* Parser interface */
X
X#define MAXSTACK 100
X
Xtypedef struct _stackentry {
X	int		 s_state;	/* State in current DFA */
X	dfa		*s_dfa;		/* Current DFA */
X	struct _node	*s_parent;	/* Where to add next node */
X} stackentry;
X
Xtypedef struct _stack {
X	stackentry	*s_top;		/* Top entry */
X	stackentry	 s_base[MAXSTACK];/* Array of stack entries */
X					/* NB The stack grows down */
X} stack;
X
Xtypedef struct {
X	struct _stack	 p_stack;	/* Stack of parser states */
X	struct _grammar	*p_grammar;	/* Grammar to use */
X	struct _node	*p_tree;	/* Top of parse tree */
X} parser_state;
X
Xparser_state *newparser PROTO((struct _grammar *g, int start));
Xvoid delparser PROTO((parser_state *ps));
Xint addtoken PROTO((parser_state *ps, int type, char *str, int lineno));
Xvoid addaccelerators PROTO((grammar *g));
EOF
fi
if test -s 'src/pgenheaders.h'
then echo '*** I will not over-write existing file src/pgenheaders.h'
else
echo 'x - src/pgenheaders.h'
sed 's/^X//' > 'src/pgenheaders.h' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/* Include files and extern declarations used by most of the parser.
X   This is a precompiled header for THINK C. */
X
X#include <stdio.h>
X#include <string.h>
X
X#ifdef THINK_C
X/* #define THINK_C_3_0			/*** TURN THIS ON FOR THINK C 3.0 ****/
X#define label label_
X#undef label
X#endif
X
X#ifdef THINK_C_3_0
X#include <proto.h>
X#endif
X
X#ifdef THINK_C
X#ifndef THINK_C_3_0
X#include <stdlib.h>
X#endif
X#endif
X
X#include "PROTO.h"
X#include "malloc.h"
X
Xextern void fatal PROTO((char *));
EOF
fi
if test -s 'src/pythonrun.h'
then echo '*** I will not over-write existing file src/pythonrun.h'
else
echo 'x - src/pythonrun.h'
sed 's/^X//' > 'src/pythonrun.h' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/* Interfaces to parse and execute pieces of python code */
X
Xvoid initall PROTO((void));
X
Xint run PROTO((FILE *, char *));
X
Xint run_script PROTO((FILE *, char *));
Xint run_tty_1 PROTO((FILE *, char *));
Xint run_tty_loop PROTO((FILE *, char *));
X
Xint parse_string PROTO((char *, int, struct _node **));
Xint parse_file PROTO((FILE *, char *, int, struct _node **));
X
Xobject *eval_node PROTO((struct _node *, char *, object *, object *));
X
Xobject *run_string PROTO((char *, int, object *, object *));
Xobject *run_file PROTO((FILE *, char *, int, object *, object *));
Xobject *run_err_node PROTO((int, struct _node *, char *, object *, object *));
Xobject *run_node PROTO((struct _node *, char *, object *, object *));
X
Xvoid print_error PROTO((void));
X
Xvoid goaway PROTO((int));
EOF
fi
if test -s 'src/regexp.h'
then echo '*** I will not over-write existing file src/regexp.h'
else
echo 'x - src/regexp.h'
sed 's/^X//' > 'src/regexp.h' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/*
X * Definitions etc. for regexp(3) routines.
X *
X * Caveat:  this is V8 regexp(3) [actually, a reimplementation thereof],
X * not the System V one.
X */
X
X#define MULTILINE
X
X#define NSUBEXP  10
Xtypedef struct regexp {
X	char *startp[NSUBEXP];
X	char *endp[NSUBEXP];
X	char regstart;		/* Internal use only. */
X	char reganch;		/* Internal use only. */
X	char *regmust;		/* Internal use only. */
X	int regmlen;		/* Internal use only. */
X	char program[1];	/* Unwarranted chumminess with compiler. */
X} regexp;
X
Xextern regexp *regcomp();
Xextern int regexec();
X#ifdef MULTILINE
Xextern int reglexec();
X#endif
Xextern void regsub();
Xextern void regerror();
EOF
fi
if test -s 'src/sigtype.h'
then echo '*** I will not over-write existing file src/sigtype.h'
else
echo 'x - src/sigtype.h'
sed 's/^X//' > 'src/sigtype.h' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/* The type of signal handlers is somewhat problematic.
X   This file encapsulates my knowledge about it:
X   - on the Mac (THINK C), it's int for 3.0, void for 4.0
X   - on other systems, it's usually void, except it's int on vax Ultrix.
X   Pass -DSIGTYPE=... to cc if you know better. */
X
X#ifndef SIGTYPE
X
X#ifdef THINK_C
X
X#ifdef THINK_C_3_0
X#define SIGTYPE int
X#else
X#define SIGTYPE void
X#endif
X
X#else /* !THINK_C */
X
X#if defined(vax) && !defined(AMOEBA)
X#define SIGTYPE int
X#else
X#define SIGTYPE void
X#endif
X
X#endif /* !THINK_C */
X
X#endif /* !SIGTYPE */
EOF
fi
if test -s 'src/token.h'
then echo '*** I will not over-write existing file src/token.h'
else
echo 'x - src/token.h'
sed 's/^X//' > 'src/token.h' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/* Token types */
X
X#define ENDMARKER	0
X#define NAME		1
X#define NUMBER		2
X#define STRING		3
X#define NEWLINE		4
X#define INDENT		5
X#define DEDENT		6
X#define LPAR		7
X#define RPAR		8
X#define LSQB		9
X#define RSQB		10
X#define COLON		11
X#define COMMA		12
X#define SEMI		13
X#define PLUS		14
X#define MINUS		15
X#define STAR		16
X#define SLASH		17
X#define VBAR		18
X#define AMPER		19
X#define LESS		20
X#define GREATER		21
X#define EQUAL		22
X#define DOT		23
X#define PERCENT		24
X#define BACKQUOTE	25
X#define LBRACE		26
X#define RBRACE		27
X#define OP		28
X#define ERRORTOKEN	29
X#define N_TOKENS	30
X
X/* Special definitions for cooperation with parser */
X
X#define NT_OFFSET		256
X
X#define ISTERMINAL(x)		((x) < NT_OFFSET)
X#define ISNONTERMINAL(x)	((x) >= NT_OFFSET)
X#define ISEOF(x)		((x) == ENDMARKER)
X
X
Xextern char *tok_name[]; /* Token names */
Xextern int tok_1char PROTO((int));
EOF
fi
if test -s 'src/typeobject.c'
then echo '*** I will not over-write existing file src/typeobject.c'
else
echo 'x - src/typeobject.c'
sed 's/^X//' > 'src/typeobject.c' << 'EOF'
X/***********************************************************
XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
XNetherlands.
X
X                        All Rights Reserved
X
XPermission to use, copy, modify, and distribute this software and its 
Xdocumentation for any purpose and without fee is hereby granted, 
Xprovided that the above copyright notice appear in all copies and that
Xboth that copyright notice and this permission notice appear in 
Xsupporting documentation, and that the names of Stichting Mathematisch
XCentrum or CWI not be used in advertising or publicity pertaining to
Xdistribution of the software without specific, written prior permission.
X
XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X
X******************************************************************/
X
X/* Type object implementation */
X
X#include "allobjects.h"
X
X/* Type object implementation */
X
Xstatic void
Xtype_print(v, fp, flags)
X	typeobject *v;
X	FILE *fp;
X	int flags;
X{
X	fprintf(fp, "<type '%s'>", v->tp_name);
X}
X
Xstatic object *
Xtype_repr(v)
X	typeobject *v;
X{
X	char buf[100];
X	sprintf(buf, "<type '%.80s'>", v->tp_name);
X	return newstringobject(buf);
X}
X
Xtypeobject Typetype = {
X	OB_HEAD_INIT(&Typetype)
X	0,			/* Number of items for varobject */
X	"type",			/* Name of this type */
X	sizeof(typeobject),	/* Basic object size */
X	0,			/* Item size for varobject */
X	0,			/*tp_dealloc*/
X	type_print,		/*tp_print*/
X	0,			/*tp_getattr*/
X	0,			/*tp_setattr*/
X	0,			/*tp_compare*/
X	type_repr,		/*tp_repr*/
X};
EOF
fi
echo 'Part 19 out of 21 of pack.out complete.'
exit 0



More information about the Alt.sources mailing list