home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2813 < prev    next >
Encoding:
Internet Message Format  |  1991-02-20  |  60.7 KB

  1. From: guido@cwi.nl (Guido van Rossum)
  2. Newsgroups: alt.sources
  3. Subject: Python 0.9.1 part 18/21
  4. Message-ID: <2980@charon.cwi.nl>
  5. Date: 19 Feb 91 17:42:44 GMT
  6.  
  7. : This is a shell archive.
  8. : Extract with 'sh this_file'.
  9. :
  10. : Extract part 01 first since it makes all directories
  11. echo 'Start of pack.out, part 18 out of 21:'
  12. if test -s 'demo/sgi/gl/backface.py'
  13. then echo '*** I will not over-write existing file demo/sgi/gl/backface.py'
  14. else
  15. echo 'x - demo/sgi/gl/backface.py'
  16. sed 's/^X//' > 'demo/sgi/gl/backface.py' << 'EOF'
  17. X#! /ufs/guido/bin/sgi/python
  18. X
  19. X#   backface
  20. X#
  21. X#   draw a cube that can run with backface() turned on or off.
  22. X#   cube is moved when LEFTMOUSE is pressed and mouse itself is moved.
  23. X
  24. Xfrom gl import *
  25. Xfrom DEVICE import *
  26. Xfrom GL import *
  27. X
  28. XCUBE_SIZE = 200.0
  29. XCUBE_OBJ = 1
  30. X
  31. Xdef main () :
  32. X    #
  33. X    x = 0
  34. X    y = 0
  35. X    moveit = 0
  36. X    #
  37. X    initialize()
  38. X    #
  39. X    while (1) :
  40. X        #
  41. X        while (qtest()) :
  42. X            dev, val = qread()
  43. X            #
  44. X            if dev = ESCKEY :
  45. X                backface(0)
  46. X                return
  47. X                #
  48. X            elif dev = REDRAW :
  49. X                reshapeviewport()
  50. X                drawcube(x,y)
  51. X                #
  52. X            elif dev = LEFTMOUSE :
  53. X                #
  54. X                # LEFTMOUSE down
  55. X                moveit = val
  56. X                #
  57. X            elif dev = BKEY :
  58. X                backface(1)
  59. X                drawcube(x,y)
  60. X                #
  61. X            elif dev = FKEY :
  62. X                backface(0)
  63. X                drawcube(x,y)
  64. X                #
  65. X        if moveit :
  66. X            x = getvaluator(MOUSEX)
  67. X            y = getvaluator(MOUSEY)
  68. X            drawcube(x,y)
  69. X
  70. X
  71. Xdef initialize () :
  72. X    foreground ()
  73. X    keepaspect (1, 1)
  74. X    gid = winopen('backface')
  75. X    winset(gid)
  76. X    winconstraints()
  77. X    #
  78. X    doublebuffer()
  79. X    gconfig()
  80. X    shademodel(FLAT)
  81. X    #
  82. X    ortho(-1024.0, 1024.0, -1024.0, 1024.0, -1024.0, 1024.0)
  83. X    #
  84. X    qdevice(ESCKEY)
  85. X    qdevice(REDRAW)
  86. X    qdevice(LEFTMOUSE)
  87. X    qdevice(BKEY)
  88. X    qdevice(FKEY)
  89. X    qenter(REDRAW,gid)
  90. X    #
  91. X    backface(1)
  92. X
  93. X#
  94. X# define a cube
  95. Xdef cube () :
  96. X    #
  97. X    # front face
  98. X    pushmatrix()
  99. X    translate(0.0,0.0,CUBE_SIZE)
  100. X    color(RED)
  101. X    rectf(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE)
  102. X    popmatrix()
  103. X    #
  104. X    # right face
  105. X    pushmatrix()
  106. X    translate(CUBE_SIZE, 0.0, 0.0)
  107. X    rotate(900, 'y')
  108. X    color(GREEN)
  109. X    rectf(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE)
  110. X    popmatrix()
  111. X    #
  112. X    # back face
  113. X    pushmatrix()
  114. X    translate(0.0, 0.0, -CUBE_SIZE)
  115. X    rotate(1800, 'y')
  116. X    color(BLUE)
  117. X    rectf(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE)
  118. X    popmatrix()
  119. X    #
  120. X    # left face
  121. X    pushmatrix()
  122. X    translate(-CUBE_SIZE, 0.0, 0.0)
  123. X    rotate(-900, 'y')
  124. X    color(CYAN)
  125. X    rectf(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE)
  126. X    popmatrix()
  127. X    #
  128. X    # top face
  129. X    pushmatrix()
  130. X    translate(0.0, CUBE_SIZE, 0.0)
  131. X    rotate(-900, 'x')
  132. X    color(MAGENTA)
  133. X    rectf(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE)
  134. X    popmatrix()
  135. X    #
  136. X    # bottom face
  137. X    pushmatrix()
  138. X    translate(0.0, -CUBE_SIZE, 0.0)
  139. X    rotate(900, 'x')
  140. X    color(YELLOW)
  141. X    rectf(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE)
  142. X    popmatrix()
  143. X
  144. Xdef drawcube(x,y) :
  145. X    #
  146. X    pushmatrix()
  147. X    rotate(2*x, 'x')
  148. X    rotate(2*y, 'y')
  149. X    color(BLACK)
  150. X    clear()
  151. X    cube()        
  152. X    popmatrix()
  153. X    swapbuffers()
  154. X
  155. X
  156. Xmain ()
  157. EOF
  158. chmod +x 'demo/sgi/gl/backface.py'
  159. fi
  160. if test -s 'demo/sgi/gl/mclock.doc'
  161. then echo '*** I will not over-write existing file demo/sgi/gl/mclock.doc'
  162. else
  163. echo 'x - demo/sgi/gl/mclock.doc'
  164. sed 's/^X//' > 'demo/sgi/gl/mclock.doc' << 'EOF'
  165. XNewsgroups: cwi.sgi
  166. XSubject: Re: new clock
  167. XDistribution: cwi.sgi
  168. XReferences: <2246@charon.cwi.nl>
  169. X
  170. XLast week I wrote:
  171. X
  172. X>For your enjoyment I have implemented a colorful clock.
  173. X
  174. XThe clock has now been extended with some new facilities: a menu, an
  175. Xalarm and a gong.  These may require some explanation beyond what's in
  176. Xthe usage message.
  177. X
  178. XMenu
  179. X----
  180. XThe right mouse button now pops up a menu that allows you to turn the
  181. Xseconds hand on or off and to switch the alarm off.
  182. X
  183. XAlarm
  184. X-----
  185. X
  186. XThe left and middle buttons set the alarm.  When it is on, the alarm
  187. Xtime is displayed as a time on a 24 hour clock in the bottom left
  188. Xcorner.  It is also indicated by two red triangles, corresponding to the
  189. Xlittle (hours) and big (minutes) hand.  These hands can be moved around:
  190. Xthe left mouse button moves the minutes hand, the middle button moves
  191. Xthe hourds hand.  Watch out for differences of twelve hours (always
  192. Xcheck the digital display); these can be corrected by dragging the hours
  193. Xhand once around the dial.
  194. X
  195. XWhen the alarm goes off, two things happen: a shell command specified on
  196. Xthe command line with the -a option is executed (in the background), and
  197. Xthe clock's colors change every two seconds, for five minutes.  You can
  198. Xalso turn the alarm off by using the menu accessible through the right
  199. Xmouse button.
  200. X
  201. XThere is no default command for the -a option; if it is not specified,
  202. Xonly the changing of the colors happens.  If you have an 8 ohm speaker
  203. Xconnected to the audio output of your Personal Iris, a suitable command
  204. Xwould be:
  205. X
  206. X    mclock -a '/ufs/guido/bin/sgi/play /ufs/guido/lib/sounds/alarm'
  207. X
  208. XGong
  209. X----
  210. X
  211. XSome people like a clock that makes noises every hour, or even more
  212. Xoften.  This is supported by the -g and -G options.  With -g you specify
  213. Xa shell command to be executed to sound the gong; with -G you can
  214. Xspecify the interval between gong calls, in seconds (default is one hour).
  215. XThe shell command is executed in the background.  It is given two
  216. Xarguments: the hours (on a 24 hour clock!) and the minutes.  The
  217. Xexecutable Python script /ufs/guido/bin/sgi/chime is a suitable example.
  218. XAgain, this only works if you have installed a speaker (I bet 8 ohm
  219. Xspeakers are going to be in demand!)
  220. X
  221. X--
  222. XGuido van Rossum, Centre for Mathematics and Computer Science (CWI), Amsterdam
  223. Xguido@cwi.nl or ..!hp4nl!cwi.nl!guido or guido%cwi.nl@uunet.uu.net
  224. X"A thing of beauty is a joy till sunrise"
  225. EOF
  226. fi
  227. if test -s 'demo/sgi/gl/mixing.py'
  228. then echo '*** I will not over-write existing file demo/sgi/gl/mixing.py'
  229. else
  230. echo 'x - demo/sgi/gl/mixing.py'
  231. sed 's/^X//' > 'demo/sgi/gl/mixing.py' << 'EOF'
  232. X#! /ufs/guido/bin/sgi/python
  233. X
  234. X# Use Gouraud shading to mix colors.  Requires Z-buffer.
  235. X# It changes the color assignments so fast that you see white.
  236. X# Left button pauses, middle rotates the square.  ESC to quit.
  237. X# Experiment with a larger window (too slow) or smaller window (really white).
  238. X
  239. Xfrom GL import *
  240. Xfrom gl import *
  241. Ximport DEVICE
  242. Xfrom math import *
  243. X
  244. X#
  245. X#  tekenvlak : draw a square. with bgnpolygon
  246. X#
  247. Xdef tekenvlak (vc) :
  248. X    bgnpolygon()
  249. X    #vcarray (vc)
  250. X    for i in vc :
  251. X        c3f (i[1])
  252. X        v3f (i[0])
  253. X    endpolygon()
  254. X
  255. X#
  256. X# tekendoos : draw a box
  257. X#
  258. Xdef tekendoos (col) :
  259. X    v = [(-5.0,0.0,0.0),(0.0,5.0,0.0),(5.0,0.0,0.0),(0.0,-5.0,0.0)]
  260. X    vc = [(v[0],col[0]),(v[1],col[1]),(v[2],col[2]),(v[3],col[1])]
  261. X    tekenvlak (vc)
  262. X
  263. X#
  264. X# initialize gl
  265. X#
  266. Xdef initgl () :
  267. X    #
  268. X    # open window
  269. X    #
  270. X    foreground ()
  271. X    keepaspect (1, 1)
  272. X    prefposition (100, 500, 100, 500)
  273. X    w = winopen ('PYTHON RGB')
  274. X    keepaspect (1, 1)
  275. X    winconstraints()
  276. X    #
  277. X    # configure pipeline (2buf, GOURAUD and RGBmode)
  278. X    #
  279. X    doublebuffer ()
  280. X    zbuffer (1)
  281. X    shademodel (GOURAUD)
  282. X    RGBmode ()
  283. X    gconfig ()
  284. X    #
  285. X    # set viewing
  286. X    #
  287. X    perspective (900, 1, 1.0, 10.0)
  288. X    polarview (10.0, 0, 0, 0)
  289. X    #
  290. X    # ask for the REDRAW and ESCKEY events
  291. X    #
  292. X    qdevice(DEVICE.MOUSE2)
  293. X    qdevice(DEVICE.MOUSE3)
  294. X    qdevice(DEVICE.REDRAW)
  295. X    qdevice(DEVICE.ESCKEY)
  296. X
  297. X
  298. X#
  299. X# the color black
  300. X#
  301. Xblack = 0
  302. X#
  303. X# GoForIT : use 2buf to redraw the object 2n times. index i is used as 
  304. X# the (smoothly changing) rotation angle
  305. X#
  306. Xdef GoForIt(i) :
  307. X    col = [(255.0,0.0,0.0), (0.0,255.0,0.0), (0.0,0.0,255.0)]
  308. X    twist = 0
  309. X    freeze = 1
  310. X    while 1 :
  311. X        if freeze <> 0 :
  312. X            col[0],col[1],col[2] = col[1],col[2],col[0]
  313. X        #
  314. X        # clear z-buffer and clear background to light-blue
  315. X        #
  316. X        zclear()
  317. X        cpack (black)
  318. X        clear()
  319. X        #
  320. X        tekendoos (col)
  321. X        #
  322. X        swapbuffers()
  323. X        #
  324. X        if qtest() <> 0 :
  325. X            dev, val = qread()
  326. X            if dev = DEVICE.ESCKEY :
  327. X                break
  328. X            elif dev = DEVICE.REDRAW :
  329. X                reshapeviewport ()
  330. X            elif dev = DEVICE.MOUSE2 and val <> 0 :
  331. X                twist = twist + 30
  332. X                perspective (900, 1, 1.0, 10.0)
  333. X                polarview (10.0, 0, 0, twist)
  334. X            elif dev = DEVICE.MOUSE3 and val <> 0 :
  335. X                freeze = 1 - freeze
  336. X
  337. X
  338. X# the main program
  339. X#
  340. Xdef main () :
  341. X    initgl ()
  342. X    GoForIt (0)
  343. X
  344. X#
  345. X# exec main
  346. X#
  347. Xmain  ()
  348. EOF
  349. chmod +x 'demo/sgi/gl/mixing.py'
  350. fi
  351. if test -s 'demo/sgi/gl_panel/twoview/observer.s'
  352. then echo '*** I will not over-write existing file demo/sgi/gl_panel/twoview/observer.s'
  353. else
  354. echo 'x - demo/sgi/gl_panel/twoview/observer.s'
  355. sed 's/^X//' > 'demo/sgi/gl_panel/twoview/observer.s' << 'EOF'
  356. X;;; This file was automatically generated by the panel editor.
  357. X;;; If you read it into gnu emacs, it will automagically format itself.
  358. X
  359. X(panel (prop help creator:user-panel-help)
  360. X(prop user-panel #t)
  361. X(label "Observer Control")
  362. X(x 876)
  363. X(y 10)
  364. X(al (pnl_right_arrow_button (name "right")
  365. X(prop help creator:user-act-help)
  366. X(x 3.2)
  367. X(y 2.09)
  368. X(downfunc move-then-resize)
  369. X)
  370. X(pnl_up_double_arrow_button (name "fast_forward")
  371. X(prop help creator:user-act-help)
  372. X(label "step forward")
  373. X(x 2.66)
  374. X(y 3.13)
  375. X(h 0.45)
  376. X(labeltype 4)
  377. X(downfunc move-then-resize)
  378. X)
  379. X(pnl_up_arrow_button (name "forward")
  380. X(prop help creator:user-act-help)
  381. X(x 2.66)
  382. X(y 2.64)
  383. X(downfunc move-then-resize)
  384. X)
  385. X(pnl_down_arrow_button (name "reverse")
  386. X(prop help creator:user-act-help)
  387. X(x 2.66)
  388. X(y 1.49)
  389. X(h 0.45)
  390. X(labeltype 12)
  391. X(downfunc move-then-resize)
  392. X)
  393. X(pnl_down_double_arrow_button (name "fast_reverse")
  394. X(prop help creator:user-act-help)
  395. X(label "step back")
  396. X(x 2.66)
  397. X(y 1)
  398. X(labeltype 12)
  399. X(downfunc move-then-resize)
  400. X)
  401. X(pnl_left_arrow_button (name "left")
  402. X(prop help creator:user-act-help)
  403. X(x 2.11)
  404. X(y 2.09)
  405. X(downfunc move-then-resize)
  406. X)
  407. X(pnl_right_double_arrow_button (name "fast_right")
  408. X(prop help creator:user-act-help)
  409. X(label "turn right")
  410. X(x 3.75)
  411. X(y 2.09)
  412. X(downfunc move-then-resize)
  413. X)
  414. X(pnl_left_double_arrow_button (name "fast_left")
  415. X(prop help creator:user-act-help)
  416. X(label "turn left")
  417. X(x 1.57)
  418. X(y 2.09)
  419. X(labeltype 8)
  420. X(downfunc move-then-resize)
  421. X)
  422. X(pnl_vslider (name "ypos")
  423. X(prop help creator:user-act-help)
  424. X(x 6.25)
  425. X(y 1)
  426. X(w 0.4)
  427. X(h 2.9)
  428. X(val 0.0758)
  429. X(downfunc move-then-resize)
  430. X)
  431. X(pnl_down_arrow_button (name "down")
  432. X(prop help creator:user-act-help)
  433. X(label "eye height")
  434. X(x 6.25)
  435. X(y 0.5)
  436. X(labeltype 12)
  437. X(downfunc move-then-resize)
  438. X)
  439. X(pnl_up_arrow_button (name "up")
  440. X(prop help creator:user-act-help)
  441. X(x 6.25)
  442. X(y 4)
  443. X(downfunc move-then-resize)
  444. X)
  445. X)
  446. X)
  447. X;;; Local Variables:
  448. X;;; mode: scheme
  449. X;;; eval: (save-excursion (goto-char (point-min)) (kill-line 3))
  450. X;;; eval: (save-excursion (goto-char (point-min)) (replace-regexp "[ \n]*)" ")"))
  451. X;;; eval: (indent-region (point-min) (point-max) nil)
  452. X;;; eval: (progn (kill-line -3) (delete-backward-char 1) (save-buffer))
  453. X;;; End:
  454. EOF
  455. fi
  456. if test -s 'lib/CSplit.py'
  457. then echo '*** I will not over-write existing file lib/CSplit.py'
  458. else
  459. echo 'x - lib/CSplit.py'
  460. sed 's/^X//' > 'lib/CSplit.py' << 'EOF'
  461. X# A CSplit is a Clock-shaped split: the children are grouped in a circle.
  462. X# The numbering is a little different from a real clock: the 12 o'clock
  463. X# position is called 0, not 12.  This is a little easier since Python
  464. X# usually counts from zero.  (BTW, there needn't be exactly 12 children.)
  465. X
  466. X
  467. Xfrom math import pi, sin, cos
  468. Xfrom Split import Split
  469. X
  470. Xclass CSplit() = Split():
  471. X    #
  472. X    def minsize(self, m):
  473. X        # Since things look best if the children are spaced evenly
  474. X        # along the circle (and often all children have the same
  475. X        # size anyway) we compute the max child size and assume
  476. X        # this is each child's size.
  477. X        width, height = 0, 0
  478. X        for child in self.children:
  479. X            wi, he = child.minsize(m)
  480. X            width = max(width, wi)
  481. X            height = max(height, he)
  482. X        # In approximation, the diameter of the circle we need is
  483. X        # (diameter of box) * (#children) / pi.
  484. X        # We approximate pi by 3 (so we slightly overestimate
  485. X        # our minimal size requirements -- not so bad).
  486. X        # Because the boxes stick out of the circle we add the
  487. X        # box size to each dimension.
  488. X        # Because we really deal with ellipses, do everything
  489. X        # separate in each dimension.
  490. X        n = len(self.children)
  491. X        return width + (width*n + 2)/3, height + (height*n + 2)/3
  492. X    #
  493. X    def getbounds(self):
  494. X        return self.bounds
  495. X    #
  496. X    def setbounds(self, bounds):
  497. X        self.bounds = bounds
  498. X        # Place the children.  This involves some math.
  499. X        # Compute center positions for children as if they were
  500. X        # ellipses with a diameter about 1/N times the
  501. X        # circumference of the big ellipse.
  502. X        # (There is some rounding involved to make it look
  503. X        # reasonable for small and large N alike.)
  504. X        # XXX One day Python will have automatic conversions...
  505. X        n = len(self.children)
  506. X        fn = float(n)
  507. X        if n = 0: return
  508. X        (left, top), (right, bottom) = bounds
  509. X        width, height = right-left, bottom-top
  510. X        child_width, child_height = width*3/(n+4), height*3/(n+4)
  511. X        half_width, half_height = \
  512. X            float(width-child_width)/2.0, \
  513. X            float(height-child_height)/2.0
  514. X        center_h, center_v = center = (left+right)/2, (top+bottom)/2
  515. X        fch, fcv = float(center_h), float(center_v)
  516. X        alpha = 2.0 * pi / fn
  517. X        for i in range(n):
  518. X            child = self.children[i]
  519. X            fi = float(i)
  520. X            fh, fv = \
  521. X                fch + half_width*sin(fi*alpha), \
  522. X                fcv - half_height*cos(fi*alpha)
  523. X            left, top = \
  524. X                int(fh) - child_width/2, \
  525. X                int(fv) - child_height/2
  526. X            right, bottom = \
  527. X                left + child_width, \
  528. X                top + child_height
  529. X            child.setbounds((left, top), (right, bottom))
  530. X    #
  531. EOF
  532. fi
  533. if test -s 'lib/TransParent.py'
  534. then echo '*** I will not over-write existing file lib/TransParent.py'
  535. else
  536. echo 'x - lib/TransParent.py'
  537. sed 's/^X//' > 'lib/TransParent.py' << 'EOF'
  538. X# A class that sits transparently between a parent and one child.
  539. X# First create the parent, then this thing, then the child.
  540. X# Use this as a base class for objects that are almost transparent.
  541. X# Don't use as a base class for parents with multiple children.
  542. X
  543. XError = 'TransParent.Error'    # Exception
  544. X
  545. Xclass ManageOneChild():
  546. X    #
  547. X    # Upcalls shared with other single-child parents
  548. X    #
  549. X    def addchild(self, child):
  550. X        if self.child:
  551. X            raise Error, 'addchild: one child only'
  552. X        if not child:
  553. X            raise Error, 'addchild: bad child'
  554. X        self.child = child
  555. X    #
  556. X    def delchild(self, child):
  557. X        if not self.child:
  558. X            raise Error, 'delchild: no child'
  559. X        if child <> self.child:
  560. X            raise Error, 'delchild: not my child'
  561. X        self.child = 0
  562. X
  563. Xclass TransParent() = ManageOneChild():
  564. X    #
  565. X    # Calls from creator
  566. X    # NB derived classes may add parameters to create()
  567. X    #
  568. X    def create(self, parent):
  569. X        parent.addchild(self)
  570. X        self.parent = parent
  571. X        self.child = 0 # No child yet
  572. X    #
  573. X    # Downcalls from parent to child
  574. X    #
  575. X    def destroy(self):
  576. X        del self.parent
  577. X        if self.child: self.child.destroy()
  578. X        del self.child
  579. X    #
  580. X    def minsize(self, m):
  581. X        if not self.child:
  582. X            return 0, 0
  583. X        else:
  584. X            return self.child.minsize(m)
  585. X    def getbounds(self, bounds):
  586. X        if not self.child:
  587. X            raise Error, 'getbounds w/o child'
  588. X        else:
  589. X            return self.child.getbounds()
  590. X    def setbounds(self, bounds):
  591. X        if not self.child:
  592. X            raise Error, 'setbounds w/o child'
  593. X        else:
  594. X            self.child.setbounds(bounds)
  595. X    def draw(self, args):
  596. X        if self.child:
  597. X            self.child.draw(args)
  598. X    #
  599. X    # Downcalls only made after certain upcalls
  600. X    #
  601. X    def mouse_down(self, detail):
  602. X        if self.child: self.child.mouse_down(detail)
  603. X    def mouse_move(self, detail):
  604. X        if self.child: self.child.mouse_move(detail)
  605. X    def mouse_up(self, detail):
  606. X        if self.child: self.child.mouse_up(detail)
  607. X    #
  608. X    def timer(self):
  609. X        if self.child: self.child.timer()
  610. X    #
  611. X    # Upcalls from child to parent
  612. X    #
  613. X    def need_mouse(self, child):
  614. X        self.parent.need_mouse(self)
  615. X    def no_mouse(self, child):
  616. X        self.parent.no_mouse(self)
  617. X    #
  618. X    def need_timer(self, child):
  619. X        self.parent.need_timer(self)
  620. X    def no_timer(self, child):
  621. X        self.parent.no_timer(self)
  622. X    #
  623. X    def begindrawing(self):
  624. X        return self.parent.begindrawing()
  625. X    def beginmeasuring(self):
  626. X        return self.parent.beginmeasuring()
  627. X    #
  628. X    def change(self, area):
  629. X        self.parent.change(area)
  630. X    def scroll(self, args):
  631. X        self.parent.scroll(args)
  632. X    def settimer(self, itimer):
  633. X        self.parent.settimer(itimer)
  634. EOF
  635. fi
  636. if test -s 'lib/auds.py'
  637. then echo '*** I will not over-write existing file lib/auds.py'
  638. else
  639. echo 'x - lib/auds.py'
  640. sed 's/^X//' > 'lib/auds.py' << 'EOF'
  641. Ximport audio
  642. X
  643. XRATE = 8192
  644. X
  645. X# Initialize the audio stuff
  646. Xaudio.setrate(3)
  647. Xaudio.setoutgain(100)    # for speaker
  648. X
  649. Xplay = audio.write
  650. X
  651. Xdef samp(n):
  652. X    savegain = audio.getoutgain()
  653. X    try:
  654. X        audio.setoutgain(0)
  655. X        x = raw_input('Hit Enter to sample ' + `n` + ' seconds: ')
  656. X        return audio.read(n*RATE)
  657. X    finally:
  658. X        audio.setoutgain(savegain)
  659. X
  660. Xdef echo(s, delay, gain):
  661. X    return s[:delay] + audio.add(s[delay:], audio.amplify(s, gain, gain))
  662. X
  663. Xdef save(s, file):
  664. X    f = open(file, 'w')
  665. X    f.write(s)
  666. X
  667. Xdef load(file):
  668. X    return loadfp(open(file, 'r'))
  669. X
  670. Xdef loadfp(fp):
  671. X    s = ''
  672. X    while 1:
  673. X        buf = fp.read(16*1024)
  674. X        if not buf: break
  675. X        s = s + buf
  676. X    return s
  677. X
  678. Xdef unbias(s):
  679. X    if not s: return s
  680. X    a = audio.chr2num(s)
  681. X    sum = 0
  682. X    for i in a: sum = sum + i
  683. X    bias = (sum + len(a)/2) / len(a)
  684. X    print 'Bias value:', bias
  685. X    if bias:
  686. X        for i in range(len(a)):
  687. X            a[i] = a[i] - bias
  688. X        s = audio.num2chr(a)
  689. X    return s
  690. X
  691. X# Stretch by a/b.
  692. X# Think of this as converting the sampling rate from a samples/sec
  693. X# to b samples/sec.  Or, if the input is a bytes long, the output
  694. X# will be b bytes long.
  695. X#
  696. Xdef stretch(s, a, b):
  697. X    y = audio.chr2num(s)
  698. X    m = len(y)
  699. X    out = []
  700. X    n = m * b / a
  701. X    # i, j will walk through y and out (step 1)
  702. X    # ib, ja are i*b, j*a and are kept as close together as possible
  703. X    i, ib = 0, 0
  704. X    j, ja = 0, 0
  705. X    for j in range(n):
  706. X        ja = ja+a
  707. X        while ib < ja:
  708. X            i = i+1
  709. X            ib = ib+b
  710. X        if i >= m:
  711. X            break
  712. X        if ib = ja:
  713. X            out.append(y[i])
  714. X        else:
  715. X            out.append((y[i]*(ja-(ib-b)) + y[i-1]*(ib-ja)) / b)
  716. X    return audio.num2chr(out)
  717. X
  718. Xdef sinus(freq): # return a 1-second sine wave
  719. X    from math import sin, pi
  720. X    factor = 2.0*pi*float(freq)/float(RATE)
  721. X    list = range(RATE)
  722. X    for i in list:
  723. X        list[i] = int(sin(float(i) * factor) * 127.0)
  724. X    return audio.num2chr(list)
  725. X
  726. Xdef softclip(s):
  727. X    if '\177' not in s and '\200' not in s:
  728. X        return s
  729. X    num = audio.chr2num(s)
  730. X    extremes = (-128, 127)
  731. X    for i in range(1, len(num)-1):
  732. X        if num[i] in extremes:
  733. X            num[i] = (num[i-1] + num[i+1]) / 2
  734. X    return audio.num2chr(num)
  735. X
  736. Xdef demo():
  737. X    gday = load('gday')[1000:6000]
  738. X    save(gday, 'gday0')
  739. X    gg = [gday]
  740. X    for i in range(1, 10):
  741. X        for g in gg: play(g)
  742. X        g = stretch(gday, 10, 10-i)
  743. X        save(g, 'gday' + `i`)
  744. X        gg.append(g)
  745. X    while 1:
  746. X        for g in gg: play(g)
  747. EOF
  748. fi
  749. if test -s 'lib/listwin.py'
  750. then echo '*** I will not over-write existing file lib/listwin.py'
  751. else
  752. echo 'x - lib/listwin.py'
  753. sed 's/^X//' > 'lib/listwin.py' << 'EOF'
  754. X# Module 'listwin'
  755. X# List windows, a subclass of gwin
  756. X
  757. Ximport gwin
  758. Ximport stdwin
  759. X
  760. Xdef maxlinewidth(a): # Compute maximum textwidth of lines in a sequence
  761. X    max = 0
  762. X    for line in a:
  763. X        width = stdwin.textwidth(line)
  764. X        if width > max: max = width
  765. X    return max
  766. X
  767. Xdef action(w, string, i, detail): # Default item selection method
  768. X    pass
  769. X
  770. Xdef mup(w, detail): # Mouse up method
  771. X    (h, v), clicks, button, mask = detail
  772. X    i = divmod(v, w.lineheight)[0]
  773. X    if 0 <= i < len(w.data):
  774. X        w.action(w, w.data[i], i, detail)
  775. X
  776. Xdef draw(w, ((left, top), (right, bottom))): # Text window draw method
  777. X    data = w.data
  778. X    d = w.begindrawing()
  779. X    lh = w.lineheight
  780. X    itop = top/lh
  781. X    ibot = (bottom-1)/lh + 1
  782. X    if itop < 0: itop = 0
  783. X    if ibot > len(data): ibot = len(data)
  784. X    for i in range(itop, ibot): d.text((0, i*lh), data[i])
  785. X
  786. Xdef open(title, data): # Display a list of texts in a window
  787. X    lineheight = stdwin.lineheight()
  788. X    h, v = maxlinewidth(data), len(data)*lineheight
  789. X    h0, v0 = h + stdwin.textwidth(' '), v + lineheight
  790. X    if h0 > stdwin.textwidth(' ')*80: h0 = 0
  791. X    if v0 > stdwin.lineheight()*24: v0 = 0
  792. X    stdwin.setdefwinsize(h0, v0)
  793. X    w = gwin.open(title)
  794. X    w.setdocsize(h, v)
  795. X    w.lineheight = lineheight
  796. X    w.data = data
  797. X    w.draw = draw
  798. X    w.action = action
  799. X    w.mup = mup
  800. X    return w
  801. EOF
  802. fi
  803. if test -s 'lib/macpath.py'
  804. then echo '*** I will not over-write existing file lib/macpath.py'
  805. else
  806. echo 'x - lib/macpath.py'
  807. sed 's/^X//' > 'lib/macpath.py' << 'EOF'
  808. X# module 'macpath' -- pathname (or -related) operations for the Macintosh
  809. X
  810. Ximport mac
  811. X
  812. Xfrom stat import *
  813. X
  814. X
  815. X# Return true if a path is absolute.
  816. X# On the Mac, relative paths begin with a colon,
  817. X# but as a special case, paths with no colons at all are also relative.
  818. X# Anything else is absolute (the string up to the first colon is the
  819. X# volume name).
  820. X
  821. Xdef isabs(s):
  822. X    return ':' in s and s[0] <> ':'
  823. X
  824. X
  825. X# Concatenate two pathnames.
  826. X# The result is equivalent to what the second pathname would refer to
  827. X# if the first pathname were the current directory.
  828. X
  829. Xdef cat(s, t):
  830. X    if (not s) or isabs(t): return t
  831. X    if t[:1] = ':': t = t[1:]
  832. X    if ':' not in s:
  833. X        s = ':' + s
  834. X    if s[-1:] <> ':':
  835. X        s = s + ':'
  836. X    return s + t
  837. X
  838. X
  839. X# Split a pathname in two parts: the directory leading up to the final bit,
  840. X# and the basename (the filename, without colons, in that directory).
  841. X# The result (s, t) is such that cat(s, t) yields the original argument.
  842. X
  843. Xdef split(s):
  844. X    if ':' not in s: return '', s
  845. X    colon = 0
  846. X    for i in range(len(s)):
  847. X        if s[i] = ':': colon = i+1
  848. X    return s[:colon], s[colon:]
  849. X
  850. X
  851. X# Normalize a pathname: get rid of '::' sequences by backing up,
  852. X# e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
  853. X# Raise the exception norm_error below if backing up is impossible,
  854. X# e.g., for '::foo'.
  855. X
  856. Xnorm_error = 'macpath.norm_error: path cannot be normalized'
  857. X
  858. Xdef norm(s):
  859. X    import string
  860. X    if ':' not in s:
  861. X        return ':' + s
  862. X    f = string.splitfields(s, ':')
  863. X    pre = []
  864. X    post = []
  865. X    if not f[0]:
  866. X        pre = f[:1]
  867. X        f = f[1:]
  868. X    if not f[len(f)-1]:
  869. X        post = f[-1:]
  870. X        f = f[:-1]
  871. X    res = []
  872. X    for seg in f:
  873. X        if seg:
  874. X            res.append(seg)
  875. X        else:
  876. X            if not res: raise norm_error, 'path starts with ::'
  877. X            del res[len(res)-1]
  878. X            if not (pre or res):
  879. X                raise norm_error, 'path starts with volume::'
  880. X    if pre: res = pre + res
  881. X    if post: res = res + post
  882. X    s = res[0]
  883. X    for seg in res[1:]:
  884. X        s = s + ':' + seg
  885. X    return s
  886. X
  887. X
  888. X# Return true if the pathname refers to an existing directory.
  889. X
  890. Xdef isdir(s):
  891. X    try:
  892. X        st = mac.stat(s)
  893. X    except mac.error:
  894. X        return 0
  895. X    return S_ISDIR(st[ST_MODE])
  896. X
  897. X
  898. X# Return true if the pathname refers to an existing regular file.
  899. X
  900. Xdef isfile(s):
  901. X    try:
  902. X        st = mac.stat(s)
  903. X    except mac.error:
  904. X        return 0
  905. X    return S_ISREG(st[ST_MODE])
  906. X
  907. X
  908. X# Return true if the pathname refers to an existing file or directory.
  909. X
  910. Xdef exists(s):
  911. X    try:
  912. X        st = mac.stat(s)
  913. X    except mac.error:
  914. X        return 0
  915. X    return 1
  916. EOF
  917. fi
  918. if test -s 'lib/textwin.py'
  919. then echo '*** I will not over-write existing file lib/textwin.py'
  920. else
  921. echo 'x - lib/textwin.py'
  922. sed 's/^X//' > 'lib/textwin.py' << 'EOF'
  923. X# Module 'textwin'
  924. X
  925. X# Text windows, a subclass of gwin
  926. X
  927. Ximport stdwin
  928. Ximport stdwinsupport
  929. Ximport gwin
  930. X
  931. XS = stdwinsupport            # Shorthand
  932. X
  933. X
  934. Xdef fixsize(w):
  935. X    docwidth, docheight = w.text.getrect()[1]
  936. X    winheight = w.getwinsize()[1]
  937. X    if winheight > docheight: docheight = winheight
  938. X    w.setdocsize(0, docheight)
  939. X    fixeditmenu(w)
  940. X
  941. Xdef cut(w, m, id):
  942. X    s = w.text.getfocustext()
  943. X    if s:
  944. X        stdwin.setcutbuffer(0, s)
  945. X        w.text.replace('')
  946. X        fixsize(w)
  947. X
  948. Xdef copy(w, m, id):
  949. X    s = w.text.getfocustext()
  950. X    if s:
  951. X        stdwin.setcutbuffer(0, s)
  952. X        fixeditmenu(w)
  953. X
  954. Xdef paste(w, m, id):
  955. X    w.text.replace(stdwin.getcutbuffer(0))
  956. X    fixsize(w)
  957. X
  958. Xdef addeditmenu(w):
  959. X    m = w.editmenu = w.menucreate('Edit')
  960. X    m.action = []
  961. X    m.additem('Cut', 'X')
  962. X    m.action.append(cut)
  963. X    m.additem('Copy', 'C')
  964. X    m.action.append(copy)
  965. X    m.additem('Paste', 'V')
  966. X    m.action.append(paste)
  967. X
  968. Xdef fixeditmenu(w):
  969. X    m = w.editmenu
  970. X    f = w.text.getfocus()
  971. X    can_copy = (f[0] < f[1])
  972. X    m.enable(1, can_copy)
  973. X    if not w.readonly:
  974. X        m.enable(0, can_copy)
  975. X        m.enable(2, (stdwin.getcutbuffer(0) <> ''))
  976. X
  977. Xdef draw(w, area):            # Draw method
  978. X    w.text.draw(area)
  979. X
  980. Xdef size(w, newsize):            # Size method
  981. X    w.text.move((0, 0), newsize)
  982. X    fixsize(w)
  983. X
  984. Xdef close(w):                # Close method
  985. X    del w.text  # Break circular ref
  986. X    gwin.close(w)
  987. X
  988. Xdef char(w, c):                # Char method
  989. X    w.text.replace(c)
  990. X    fixsize(w)
  991. X
  992. Xdef backspace(w):            # Backspace method
  993. X    void = w.text.event(S.we_command, w, S.wc_backspace)
  994. X    fixsize(w)
  995. X
  996. Xdef arrow(w, detail):            # Arrow method
  997. X    w.text.arrow(detail)
  998. X    fixeditmenu(w)
  999. X
  1000. Xdef mdown(w, detail):            # Mouse down method
  1001. X    void = w.text.event(S.we_mouse_down, w, detail)
  1002. X    fixeditmenu(w)
  1003. X
  1004. Xdef mmove(w, detail):            # Mouse move method
  1005. X    void = w.text.event(S.we_mouse_move, w, detail)
  1006. X
  1007. Xdef mup(w, detail):            # Mouse up method
  1008. X    void = w.text.event(S.we_mouse_up, w, detail)
  1009. X    fixeditmenu(w)
  1010. X
  1011. Xdef activate(w):            # Activate method
  1012. X    fixeditmenu(w)
  1013. X
  1014. Xdef open(title, str):            # Display a string in a window
  1015. X    w = gwin.open(title)
  1016. X    w.readonly = 0
  1017. X    w.text = w.textcreate((0, 0), w.getwinsize())
  1018. X    w.text.replace(str)
  1019. X    w.text.setfocus(0, 0)
  1020. X    addeditmenu(w)
  1021. X    fixsize(w)
  1022. X    w.draw = draw
  1023. X    w.size = size
  1024. X    w.close = close
  1025. X    w.mdown = mdown
  1026. X    w.mmove = mmove
  1027. X    w.mup = mup
  1028. X    w.char = char
  1029. X    w.backspace = backspace
  1030. X    w.arrow = arrow
  1031. X    w.activate = activate
  1032. X    return w
  1033. X
  1034. Xdef open_readonly(title, str):        # Same with char input disabled
  1035. X    w = open(title, str)
  1036. X    w.readonly = 1
  1037. X    w.char = w.backspace = gwin.nop
  1038. X    # Disable Cut and Paste menu item; leave Copy alone
  1039. X    w.editmenu.enable(0, 0)
  1040. X    w.editmenu.enable(2, 0)
  1041. X    return w
  1042. EOF
  1043. fi
  1044. if test -s 'lib/zmod.py'
  1045. then echo '*** I will not over-write existing file lib/zmod.py'
  1046. else
  1047. echo 'x - lib/zmod.py'
  1048. sed 's/^X//' > 'lib/zmod.py' << 'EOF'
  1049. X# module 'zmod'
  1050. X
  1051. X# Compute properties of mathematical "fields" formed by taking
  1052. X# Z/n (the whole numbers modulo some whole number n) and an 
  1053. X# irreducible polynomial (i.e., a polynomial with only complex zeros),
  1054. X# e.g., Z/5 and X**2 + 2.
  1055. X#
  1056. X# The field is formed by taking all possible linear combinations of
  1057. X# a set of d base vectors (where d is the degree of the polynomial).
  1058. X#
  1059. X# Note that this procedure doesn't yield a field for all combinations
  1060. X# of n and p: it may well be that some numbers have more than one
  1061. X# inverse and others have none.  This is what we check.
  1062. X#
  1063. X# Remember that a field is a ring where each element has an inverse.
  1064. X# A ring has commutative addition and multiplication, a zero and a one:
  1065. X# 0*x = x*0 = 0, 0+x = x+0 = x, 1*x = x*1 = x.  Also, the distributive
  1066. X# property holds: a*(b+c) = a*b + b*c.
  1067. X# (XXX I forget if this is an axiom or follows from the rules.)
  1068. X
  1069. Ximport poly
  1070. X
  1071. X
  1072. X# Example N and polynomial
  1073. X
  1074. XN = 5
  1075. XP = poly.plus(poly.one(0, 2), poly.one(2, 1)) # 2 + x**2
  1076. X
  1077. X
  1078. X# Return x modulo y.  Returns >= 0 even if x < 0.
  1079. X
  1080. Xdef mod(x, y):
  1081. X    return divmod(x, y)[1]
  1082. X
  1083. X
  1084. X# Normalize a polynomial modulo n and modulo p.
  1085. X
  1086. Xdef norm(a, n, p):
  1087. X    a = poly.modulo(a, p)
  1088. X    a = a[:]
  1089. X    for i in range(len(a)): a[i] = mod(a[i], n)
  1090. X    a = poly.normalize(a)
  1091. X    return a
  1092. X
  1093. X
  1094. X# Make a list of all n^d elements of the proposed field.
  1095. X
  1096. Xdef make_all(mat):
  1097. X    all = []
  1098. X    for row in mat:
  1099. X        for a in row:
  1100. X            all.append(a)
  1101. X    return all
  1102. X
  1103. Xdef make_elements(n, d):
  1104. X    if d = 0: return [poly.one(0, 0)]
  1105. X    sub = make_elements(n, d-1)
  1106. X    all = []
  1107. X    for a in sub:
  1108. X        for i in range(n):
  1109. X            all.append(poly.plus(a, poly.one(d-1, i)))
  1110. X    return all
  1111. X
  1112. Xdef make_inv(all, n, p):
  1113. X    x = poly.one(1, 1)
  1114. X    inv = []
  1115. X    for a in all:
  1116. X        inv.append(norm(poly.times(a, x), n, p))
  1117. X    return inv
  1118. X
  1119. Xdef checkfield(n, p):
  1120. X    all = make_elements(n, len(p)-1)
  1121. X    inv = make_inv(all, n, p)
  1122. X    all1 = all[:]
  1123. X    inv1 = inv[:]
  1124. X    all1.sort()
  1125. X    inv1.sort()
  1126. X    if all1 = inv1: print 'BINGO!'
  1127. X    else:
  1128. X        print 'Sorry:', n, p
  1129. X        print all
  1130. X        print inv
  1131. X
  1132. Xdef rj(s, width):
  1133. X    if type(s) <> type(''): s = `s`
  1134. X    n = len(s)
  1135. X    if n >= width: return s
  1136. X    return ' '*(width - n) + s
  1137. X
  1138. Xdef lj(s, width):
  1139. X    if type(s) <> type(''): s = `s`
  1140. X    n = len(s)
  1141. X    if n >= width: return s
  1142. X    return s + ' '*(width - n)
  1143. EOF
  1144. fi
  1145. if test -s 'src/PROTO.h'
  1146. then echo '*** I will not over-write existing file src/PROTO.h'
  1147. else
  1148. echo 'x - src/PROTO.h'
  1149. sed 's/^X//' > 'src/PROTO.h' << 'EOF'
  1150. X/***********************************************************
  1151. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  1152. XNetherlands.
  1153. X
  1154. X                        All Rights Reserved
  1155. X
  1156. XPermission to use, copy, modify, and distribute this software and its 
  1157. Xdocumentation for any purpose and without fee is hereby granted, 
  1158. Xprovided that the above copyright notice appear in all copies and that
  1159. Xboth that copyright notice and this permission notice appear in 
  1160. Xsupporting documentation, and that the names of Stichting Mathematisch
  1161. XCentrum or CWI not be used in advertising or publicity pertaining to
  1162. Xdistribution of the software without specific, written prior permission.
  1163. X
  1164. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  1165. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  1166. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  1167. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  1168. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  1169. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  1170. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  1171. X
  1172. X******************************************************************/
  1173. X
  1174. X/*
  1175. XThe macro PROTO(x) is used to put function prototypes in the source.
  1176. XThis is defined differently for compilers that support prototypes than
  1177. Xfor compilers that don't.  It should be used as follows:
  1178. X    int some_function PROTO((int arg1, char *arg2));
  1179. XA variant FPROTO(x) is used for cases where Standard C allows prototypes
  1180. Xbut Think C doesn't (mostly function pointers).
  1181. X
  1182. XThis file also defines the macro HAVE_PROTOTYPES if and only if
  1183. Xthe PROTO() macro expands the prototype.  It is also allowed to predefine
  1184. XHAVE_PROTOTYPES to force prototypes on.
  1185. X*/
  1186. X
  1187. X#ifndef PROTO
  1188. X
  1189. X#ifdef __STDC__
  1190. X#define HAVE_PROTOTYPES
  1191. X#endif
  1192. X
  1193. X#ifdef THINK_C
  1194. X#undef HAVE_PROTOTYPES
  1195. X#define HAVE_PROTOTYPES
  1196. X#endif
  1197. X
  1198. X#ifdef sgi
  1199. X#ifdef mips
  1200. X#define HAVE_PROTOTYPES
  1201. X#endif
  1202. X#endif
  1203. X
  1204. X#ifdef HAVE_PROTOTYPES
  1205. X#define PROTO(x) x
  1206. X#else
  1207. X#define PROTO(x) ()
  1208. X#endif
  1209. X
  1210. X#endif /* PROTO */
  1211. X
  1212. X
  1213. X/* FPROTO() is for cases where Think C doesn't like prototypes */
  1214. X
  1215. X#ifdef THINK_C
  1216. X#define FPROTO(arglist) ()
  1217. X#else /* !THINK_C */
  1218. X#define FPROTO(arglist) PROTO(arglist)
  1219. X#endif /* !THINK_C */
  1220. X
  1221. X#ifndef HAVE_PROTOTYPES
  1222. X#define const /*empty*/
  1223. X#else /* HAVE_PROTOTYPES */
  1224. X#ifdef THINK_C
  1225. X#undef const
  1226. X#define const /*empty*/
  1227. X#endif /* THINK_C */
  1228. X#endif /* HAVE_PROTOTYPES */
  1229. EOF
  1230. fi
  1231. if test -s 'src/bitset.c'
  1232. then echo '*** I will not over-write existing file src/bitset.c'
  1233. else
  1234. echo 'x - src/bitset.c'
  1235. sed 's/^X//' > 'src/bitset.c' << 'EOF'
  1236. X/***********************************************************
  1237. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  1238. XNetherlands.
  1239. X
  1240. X                        All Rights Reserved
  1241. X
  1242. XPermission to use, copy, modify, and distribute this software and its 
  1243. Xdocumentation for any purpose and without fee is hereby granted, 
  1244. Xprovided that the above copyright notice appear in all copies and that
  1245. Xboth that copyright notice and this permission notice appear in 
  1246. Xsupporting documentation, and that the names of Stichting Mathematisch
  1247. XCentrum or CWI not be used in advertising or publicity pertaining to
  1248. Xdistribution of the software without specific, written prior permission.
  1249. X
  1250. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  1251. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  1252. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  1253. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  1254. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  1255. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  1256. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  1257. X
  1258. X******************************************************************/
  1259. X
  1260. X/* Bitset primitives used by the parser generator */
  1261. X
  1262. X#include "pgenheaders.h"
  1263. X#include "bitset.h"
  1264. X
  1265. Xbitset
  1266. Xnewbitset(nbits)
  1267. X    int nbits;
  1268. X{
  1269. X    int nbytes = NBYTES(nbits);
  1270. X    bitset ss = NEW(BYTE, nbytes);
  1271. X    
  1272. X    if (ss == NULL)
  1273. X        fatal("no mem for bitset");
  1274. X    
  1275. X    ss += nbytes;
  1276. X    while (--nbytes >= 0)
  1277. X        *--ss = 0;
  1278. X    return ss;
  1279. X}
  1280. X
  1281. Xvoid
  1282. Xdelbitset(ss)
  1283. X    bitset ss;
  1284. X{
  1285. X    DEL(ss);
  1286. X}
  1287. X
  1288. Xint
  1289. Xaddbit(ss, ibit)
  1290. X    bitset ss;
  1291. X    int ibit;
  1292. X{
  1293. X    int ibyte = BIT2BYTE(ibit);
  1294. X    BYTE mask = BIT2MASK(ibit);
  1295. X    
  1296. X    if (ss[ibyte] & mask)
  1297. X        return 0; /* Bit already set */
  1298. X    ss[ibyte] |= mask;
  1299. X    return 1;
  1300. X}
  1301. X
  1302. X#if 0 /* Now a macro */
  1303. Xint
  1304. Xtestbit(ss, ibit)
  1305. X    bitset ss;
  1306. X    int ibit;
  1307. X{
  1308. X    return (ss[BIT2BYTE(ibit)] & BIT2MASK(ibit)) != 0;
  1309. X}
  1310. X#endif
  1311. X
  1312. Xint
  1313. Xsamebitset(ss1, ss2, nbits)
  1314. X    bitset ss1, ss2;
  1315. X    int nbits;
  1316. X{
  1317. X    int i;
  1318. X    
  1319. X    for (i = NBYTES(nbits); --i >= 0; )
  1320. X        if (*ss1++ != *ss2++)
  1321. X            return 0;
  1322. X    return 1;
  1323. X}
  1324. X
  1325. Xvoid
  1326. Xmergebitset(ss1, ss2, nbits)
  1327. X    bitset ss1, ss2;
  1328. X    int nbits;
  1329. X{
  1330. X    int i;
  1331. X    
  1332. X    for (i = NBYTES(nbits); --i >= 0; )
  1333. X        *ss1++ |= *ss2++;
  1334. X}
  1335. EOF
  1336. fi
  1337. if test -s 'src/configmac.c'
  1338. then echo '*** I will not over-write existing file src/configmac.c'
  1339. else
  1340. echo 'x - src/configmac.c'
  1341. sed 's/^X//' > 'src/configmac.c' << 'EOF'
  1342. X/***********************************************************
  1343. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  1344. XNetherlands.
  1345. X
  1346. X                        All Rights Reserved
  1347. X
  1348. XPermission to use, copy, modify, and distribute this software and its 
  1349. Xdocumentation for any purpose and without fee is hereby granted, 
  1350. Xprovided that the above copyright notice appear in all copies and that
  1351. Xboth that copyright notice and this permission notice appear in 
  1352. Xsupporting documentation, and that the names of Stichting Mathematisch
  1353. XCentrum or CWI not be used in advertising or publicity pertaining to
  1354. Xdistribution of the software without specific, written prior permission.
  1355. X
  1356. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  1357. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  1358. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  1359. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  1360. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  1361. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  1362. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  1363. X
  1364. X******************************************************************/
  1365. X
  1366. X/* Configuration using STDWIN on the Mac (THINK_C or MPW) */
  1367. X
  1368. X#ifdef THINK_C
  1369. X#define USE_STDWIN
  1370. X#endif
  1371. X
  1372. X#ifdef USE_STDWIN
  1373. X#include "stdwin.h"
  1374. X#endif
  1375. X
  1376. Xvoid
  1377. Xinitargs(p_argc, p_argv)
  1378. X    int *p_argc;
  1379. X    char ***p_argv;
  1380. X{
  1381. X#ifdef USE_STDWIN
  1382. X
  1383. X#ifdef THINK_C_3_0
  1384. X    wsetstdio(1);
  1385. X#else
  1386. X    /* This printf() statement is really needed only
  1387. X       to initialize THINK C 4.0's stdio: */
  1388. X    printf(
  1389. X"Python 4.0, Copyright 1990 Stichting Mathematisch Centrum, Amsterdam\n");
  1390. X#endif
  1391. X
  1392. X    wargs(p_argc, p_argv);
  1393. X#endif
  1394. X}
  1395. X
  1396. Xvoid
  1397. Xinitcalls()
  1398. X{
  1399. X}
  1400. X
  1401. Xvoid
  1402. Xdonecalls()
  1403. X{
  1404. X#ifdef USE_STDWIN
  1405. X    wdone();
  1406. X#endif
  1407. X}
  1408. X
  1409. X#ifndef PYTHONPATH
  1410. X/* On the Mac, the search path is a space-separated list of directories */
  1411. X#define PYTHONPATH ": :lib :lib:stdwin :lib:mac :lib:demo"
  1412. X#endif
  1413. X
  1414. Xchar *
  1415. Xgetpythonpath()
  1416. X{
  1417. X    return PYTHONPATH;
  1418. X}
  1419. X
  1420. X
  1421. X/* Table of built-in modules.
  1422. X   These are initialized when first imported. */
  1423. X
  1424. X/* Standard modules */
  1425. Xextern void inittime();
  1426. Xextern void initmath();
  1427. Xextern void initregexp();
  1428. X    
  1429. X/* Mac-specific modules */
  1430. Xextern void initmac();
  1431. X#ifdef USE_STDWIN
  1432. Xextern void initstdwin();
  1433. X#endif
  1434. X
  1435. Xstruct {
  1436. X    char *name;
  1437. X    void (*initfunc)();
  1438. X} inittab[] = {
  1439. X    /* Standard modules */
  1440. X    {"time",    inittime},
  1441. X    {"math",    initmath},
  1442. X    {"regexp",    initregexp},
  1443. X    
  1444. X    /* Mac-specific modules */
  1445. X    {"mac",        initmac},
  1446. X#ifdef USE_STDWIN
  1447. X    {"stdwin",    initstdwin},
  1448. X#endif
  1449. X    {0,        0}        /* Sentinel */
  1450. X};
  1451. EOF
  1452. fi
  1453. if test -s 'src/fgetsintr.c'
  1454. then echo '*** I will not over-write existing file src/fgetsintr.c'
  1455. else
  1456. echo 'x - src/fgetsintr.c'
  1457. sed 's/^X//' > 'src/fgetsintr.c' << 'EOF'
  1458. X/***********************************************************
  1459. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  1460. XNetherlands.
  1461. X
  1462. X                        All Rights Reserved
  1463. X
  1464. XPermission to use, copy, modify, and distribute this software and its 
  1465. Xdocumentation for any purpose and without fee is hereby granted, 
  1466. Xprovided that the above copyright notice appear in all copies and that
  1467. Xboth that copyright notice and this permission notice appear in 
  1468. Xsupporting documentation, and that the names of Stichting Mathematisch
  1469. XCentrum or CWI not be used in advertising or publicity pertaining to
  1470. Xdistribution of the software without specific, written prior permission.
  1471. X
  1472. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  1473. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  1474. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  1475. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  1476. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  1477. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  1478. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  1479. X
  1480. X******************************************************************/
  1481. X
  1482. X/* Interruptable version of fgets().
  1483. X   Return < 0 for interrupted, 1 for EOF, 0 for valid input. */
  1484. X
  1485. X/* XXX This uses longjmp() from a signal out of fgets().
  1486. X   Should use read() instead?! */
  1487. X
  1488. X#include "pgenheaders.h"
  1489. X
  1490. X#include <signal.h>
  1491. X#include <setjmp.h>
  1492. X
  1493. X#include "errcode.h"
  1494. X#include "sigtype.h"
  1495. X#include "fgetsintr.h"
  1496. X
  1497. X#ifndef AMOEBA
  1498. X#define sig_block()    /*empty*/
  1499. X#define sig_unblock()    /*empty*/
  1500. X#endif
  1501. X
  1502. Xstatic jmp_buf jback;
  1503. X
  1504. Xstatic void catcher PROTO((int));
  1505. X
  1506. Xstatic void
  1507. Xcatcher(sig)
  1508. X    int sig;
  1509. X{
  1510. X    longjmp(jback, 1);
  1511. X}
  1512. X
  1513. Xint
  1514. Xfgets_intr(buf, size, fp)
  1515. X    char *buf;
  1516. X    int size;
  1517. X    FILE *fp;
  1518. X{
  1519. X    int ret;
  1520. X    SIGTYPE (*sigsave)();
  1521. X    
  1522. X    if (setjmp(jback)) {
  1523. X        clearerr(fp);
  1524. X        signal(SIGINT, sigsave);
  1525. X#ifdef THINK_C_3_0
  1526. X        Set_Echo(1);
  1527. X#endif
  1528. X        return E_INTR;
  1529. X    }
  1530. X    
  1531. X    /* The casts to (SIGTYPE(*)()) are needed by THINK_C only */
  1532. X    
  1533. X    sigsave = signal(SIGINT, (SIGTYPE(*)()) SIG_IGN);
  1534. X    if (sigsave != (SIGTYPE(*)()) SIG_IGN)
  1535. X        signal(SIGINT, (SIGTYPE(*)()) catcher);
  1536. X    
  1537. X#ifndef THINK_C
  1538. X    if (intrcheck())
  1539. X        ret = E_INTR;
  1540. X    else
  1541. X#endif
  1542. X    {
  1543. X        sig_block();
  1544. X        ret = (fgets(buf, size, fp) == NULL) ? E_EOF : E_OK;
  1545. X        sig_unblock();
  1546. X    }
  1547. X    
  1548. X    if (sigsave != (SIGTYPE(*)()) SIG_IGN)
  1549. X        signal(SIGINT, (SIGTYPE(*)()) sigsave);
  1550. X    return ret;
  1551. X}
  1552. EOF
  1553. fi
  1554. if test -s 'src/getcwd.c'
  1555. then echo '*** I will not over-write existing file src/getcwd.c'
  1556. else
  1557. echo 'x - src/getcwd.c'
  1558. sed 's/^X//' > 'src/getcwd.c' << 'EOF'
  1559. X/***********************************************************
  1560. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  1561. XNetherlands.
  1562. X
  1563. X                        All Rights Reserved
  1564. X
  1565. XPermission to use, copy, modify, and distribute this software and its 
  1566. Xdocumentation for any purpose and without fee is hereby granted, 
  1567. Xprovided that the above copyright notice appear in all copies and that
  1568. Xboth that copyright notice and this permission notice appear in 
  1569. Xsupporting documentation, and that the names of Stichting Mathematisch
  1570. XCentrum or CWI not be used in advertising or publicity pertaining to
  1571. Xdistribution of the software without specific, written prior permission.
  1572. X
  1573. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  1574. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  1575. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  1576. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  1577. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  1578. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  1579. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  1580. X
  1581. X******************************************************************/
  1582. X
  1583. X/* Two PD getcwd() implementations.
  1584. X   Author: Guido van Rossum, CWI Amsterdam, Jan 1991, <guido@cwi.nl>. */
  1585. X
  1586. X/* #define NO_GETWD /* Turn this on to popen pwd instead of calling getwd() */
  1587. X
  1588. X#include <stdio.h>
  1589. X#include <errno.h>
  1590. X
  1591. Xextern int errno;
  1592. X
  1593. X#ifndef NO_GETWD
  1594. X
  1595. X/* Default: Version for BSD systems -- use getwd() */
  1596. X
  1597. X#include "sys/param.h"
  1598. X
  1599. Xextern char *getwd();
  1600. X
  1601. Xchar *
  1602. Xgetcwd(buf, size)
  1603. X    char *buf;
  1604. X    int size;
  1605. X{
  1606. X    char localbuf[MAXPATHLEN+1];
  1607. X    char *ret;
  1608. X    
  1609. X    if (size <= 0) {
  1610. X        errno = EINVAL;
  1611. X        return NULL;
  1612. X    }
  1613. X    ret = getwd(localbuf);
  1614. X    if (ret != NULL && strlen(localbuf) >= size) {
  1615. X        errno = ERANGE;
  1616. X        return NULL;
  1617. X    }
  1618. X    if (ret == NULL) {
  1619. X        errno = EACCES; /* Most likely error */
  1620. X        return NULL;
  1621. X    }
  1622. X    strncpy(buf, localbuf, size);
  1623. X    return buf;
  1624. X}
  1625. X
  1626. X#else
  1627. X
  1628. X/* NO_GETWD defined: Version for backward UNIXes -- popen /bin/pwd */
  1629. X
  1630. X#define PWD_CMD "/bin/pwd"
  1631. X
  1632. Xchar *
  1633. Xgetcwd(buf, size)
  1634. X    char *buf;
  1635. X    int size;
  1636. X{
  1637. X    FILE *fp;
  1638. X    char *p;
  1639. X    int sts;
  1640. X    if (size <= 0) {
  1641. X        errno = EINVAL;
  1642. X        return NULL;
  1643. X    }
  1644. X    if ((fp = popen(PWD_CMD, "r")) == NULL)
  1645. X        return NULL;
  1646. X    if (fgets(buf, size, fp) == NULL || (sts = pclose(fp)) != 0) {
  1647. X        errno = EACCES; /* Most likely error */
  1648. X        return NULL;
  1649. X    }
  1650. X    for (p = buf; *p != '\n'; p++) {
  1651. X        if (*p == '\0') {
  1652. X            errno = ERANGE;
  1653. X            return NULL;
  1654. X        }
  1655. X    }
  1656. X    *p = '\0';
  1657. X    return buf;
  1658. X}
  1659. X
  1660. X#endif
  1661. EOF
  1662. fi
  1663. if test -s 'src/graminit.h'
  1664. then echo '*** I will not over-write existing file src/graminit.h'
  1665. else
  1666. echo 'x - src/graminit.h'
  1667. sed 's/^X//' > 'src/graminit.h' << 'EOF'
  1668. X/***********************************************************
  1669. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  1670. XNetherlands.
  1671. X
  1672. X                        All Rights Reserved
  1673. X
  1674. XPermission to use, copy, modify, and distribute this software and its 
  1675. Xdocumentation for any purpose and without fee is hereby granted, 
  1676. Xprovided that the above copyright notice appear in all copies and that
  1677. Xboth that copyright notice and this permission notice appear in 
  1678. Xsupporting documentation, and that the names of Stichting Mathematisch
  1679. XCentrum or CWI not be used in advertising or publicity pertaining to
  1680. Xdistribution of the software without specific, written prior permission.
  1681. X
  1682. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  1683. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  1684. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  1685. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  1686. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  1687. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  1688. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  1689. X
  1690. X******************************************************************/
  1691. X
  1692. X#define single_input 256
  1693. X#define file_input 257
  1694. X#define expr_input 258
  1695. X#define eval_input 259
  1696. X#define funcdef 260
  1697. X#define parameters 261
  1698. X#define fplist 262
  1699. X#define fpdef 263
  1700. X#define stmt 264
  1701. X#define simple_stmt 265
  1702. X#define expr_stmt 266
  1703. X#define print_stmt 267
  1704. X#define del_stmt 268
  1705. X#define pass_stmt 269
  1706. X#define flow_stmt 270
  1707. X#define break_stmt 271
  1708. X#define return_stmt 272
  1709. X#define raise_stmt 273
  1710. X#define import_stmt 274
  1711. X#define compound_stmt 275
  1712. X#define if_stmt 276
  1713. X#define while_stmt 277
  1714. X#define for_stmt 278
  1715. X#define try_stmt 279
  1716. X#define except_clause 280
  1717. X#define suite 281
  1718. X#define test 282
  1719. X#define and_test 283
  1720. X#define not_test 284
  1721. X#define comparison 285
  1722. X#define comp_op 286
  1723. X#define expr 287
  1724. X#define term 288
  1725. X#define factor 289
  1726. X#define atom 290
  1727. X#define trailer 291
  1728. X#define subscript 292
  1729. X#define exprlist 293
  1730. X#define testlist 294
  1731. X#define classdef 295
  1732. X#define baselist 296
  1733. X#define arguments 297
  1734. EOF
  1735. fi
  1736. if test -s 'src/listnode.c'
  1737. then echo '*** I will not over-write existing file src/listnode.c'
  1738. else
  1739. echo 'x - src/listnode.c'
  1740. sed 's/^X//' > 'src/listnode.c' << 'EOF'
  1741. X/***********************************************************
  1742. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  1743. XNetherlands.
  1744. X
  1745. X                        All Rights Reserved
  1746. X
  1747. XPermission to use, copy, modify, and distribute this software and its 
  1748. Xdocumentation for any purpose and without fee is hereby granted, 
  1749. Xprovided that the above copyright notice appear in all copies and that
  1750. Xboth that copyright notice and this permission notice appear in 
  1751. Xsupporting documentation, and that the names of Stichting Mathematisch
  1752. XCentrum or CWI not be used in advertising or publicity pertaining to
  1753. Xdistribution of the software without specific, written prior permission.
  1754. X
  1755. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  1756. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  1757. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  1758. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  1759. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  1760. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  1761. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  1762. X
  1763. X******************************************************************/
  1764. X
  1765. X/* List a node on a file */
  1766. X
  1767. X#include "pgenheaders.h"
  1768. X#include "token.h"
  1769. X#include "node.h"
  1770. X
  1771. X/* Forward */
  1772. Xstatic void list1node PROTO((FILE *, node *));
  1773. X
  1774. Xvoid
  1775. Xlisttree(n)
  1776. X    node *n;
  1777. X{
  1778. X    listnode(stdout, n);
  1779. X}
  1780. X
  1781. Xstatic int level, atbol;
  1782. X
  1783. Xvoid
  1784. Xlistnode(fp, n)
  1785. X    FILE *fp;
  1786. X    node *n;
  1787. X{
  1788. X    level = 0;
  1789. X    atbol = 1;
  1790. X    list1node(fp, n);
  1791. X}
  1792. X
  1793. Xstatic void
  1794. Xlist1node(fp, n)
  1795. X    FILE *fp;
  1796. X    node *n;
  1797. X{
  1798. X    if (n == 0)
  1799. X        return;
  1800. X    if (ISNONTERMINAL(TYPE(n))) {
  1801. X        int i;
  1802. X        for (i = 0; i < NCH(n); i++)
  1803. X            list1node(fp, CHILD(n, i));
  1804. X    }
  1805. X    else if (ISTERMINAL(TYPE(n))) {
  1806. X        switch (TYPE(n)) {
  1807. X        case INDENT:
  1808. X            ++level;
  1809. X            break;
  1810. X        case DEDENT:
  1811. X            --level;
  1812. X            break;
  1813. X        default:
  1814. X            if (atbol) {
  1815. X                int i;
  1816. X                for (i = 0; i < level; ++i)
  1817. X                    fprintf(fp, "\t");
  1818. X                atbol = 0;
  1819. X            }
  1820. X            if (TYPE(n) == NEWLINE) {
  1821. X                if (STR(n) != NULL)
  1822. X                    fprintf(fp, "%s", STR(n));
  1823. X                fprintf(fp, "\n");
  1824. X                atbol = 1;
  1825. X            }
  1826. X            else
  1827. X                fprintf(fp, "%s ", STR(n));
  1828. X            break;
  1829. X        }
  1830. X    }
  1831. X    else
  1832. X        fprintf(fp, "? ");
  1833. X}
  1834. EOF
  1835. fi
  1836. if test -s 'src/listobject.h'
  1837. then echo '*** I will not over-write existing file src/listobject.h'
  1838. else
  1839. echo 'x - src/listobject.h'
  1840. sed 's/^X//' > 'src/listobject.h' << 'EOF'
  1841. X/***********************************************************
  1842. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  1843. XNetherlands.
  1844. X
  1845. X                        All Rights Reserved
  1846. X
  1847. XPermission to use, copy, modify, and distribute this software and its 
  1848. Xdocumentation for any purpose and without fee is hereby granted, 
  1849. Xprovided that the above copyright notice appear in all copies and that
  1850. Xboth that copyright notice and this permission notice appear in 
  1851. Xsupporting documentation, and that the names of Stichting Mathematisch
  1852. XCentrum or CWI not be used in advertising or publicity pertaining to
  1853. Xdistribution of the software without specific, written prior permission.
  1854. X
  1855. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  1856. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  1857. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  1858. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  1859. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  1860. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  1861. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  1862. X
  1863. X******************************************************************/
  1864. X
  1865. X/* List object interface */
  1866. X
  1867. X/*
  1868. X123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
  1869. X
  1870. XAnother generally useful object type is an list of object pointers.
  1871. XThis is a mutable type: the list items can be changed, and items can be
  1872. Xadded or removed.  Out-of-range indices or non-list objects are ignored.
  1873. X
  1874. X*** WARNING *** setlistitem does not increment the new item's reference
  1875. Xcount, but does decrement the reference count of the item it replaces,
  1876. Xif not nil.  It does *decrement* the reference count if it is *not*
  1877. Xinserted in the list.  Similarly, getlistitem does not increment the
  1878. Xreturned item's reference count.
  1879. X*/
  1880. X
  1881. Xtypedef struct {
  1882. X    OB_VARHEAD
  1883. X    object **ob_item;
  1884. X} listobject;
  1885. X
  1886. Xextern typeobject Listtype;
  1887. X
  1888. X#define is_listobject(op) ((op)->ob_type == &Listtype)
  1889. X
  1890. Xextern object *newlistobject PROTO((int size));
  1891. Xextern int getlistsize PROTO((object *));
  1892. Xextern object *getlistitem PROTO((object *, int));
  1893. Xextern int setlistitem PROTO((object *, int, object *));
  1894. Xextern int inslistitem PROTO((object *, int, object *));
  1895. Xextern int addlistitem PROTO((object *, object *));
  1896. Xextern int sortlist PROTO((object *));
  1897. X
  1898. X/* Macro, trading safety for speed */
  1899. X#define GETLISTITEM(op, i) ((op)->ob_item[i])
  1900. EOF
  1901. fi
  1902. if test -s 'src/node.c'
  1903. then echo '*** I will not over-write existing file src/node.c'
  1904. else
  1905. echo 'x - src/node.c'
  1906. sed 's/^X//' > 'src/node.c' << 'EOF'
  1907. X/***********************************************************
  1908. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  1909. XNetherlands.
  1910. X
  1911. X                        All Rights Reserved
  1912. X
  1913. XPermission to use, copy, modify, and distribute this software and its 
  1914. Xdocumentation for any purpose and without fee is hereby granted, 
  1915. Xprovided that the above copyright notice appear in all copies and that
  1916. Xboth that copyright notice and this permission notice appear in 
  1917. Xsupporting documentation, and that the names of Stichting Mathematisch
  1918. XCentrum or CWI not be used in advertising or publicity pertaining to
  1919. Xdistribution of the software without specific, written prior permission.
  1920. X
  1921. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  1922. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  1923. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  1924. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  1925. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  1926. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  1927. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  1928. X
  1929. X******************************************************************/
  1930. X
  1931. X/* Parse tree node implementation */
  1932. X
  1933. X#include "pgenheaders.h"
  1934. X#include "node.h"
  1935. X
  1936. Xnode *
  1937. Xnewtree(type)
  1938. X    int type;
  1939. X{
  1940. X    node *n = NEW(node, 1);
  1941. X    if (n == NULL)
  1942. X        return NULL;
  1943. X    n->n_type = type;
  1944. X    n->n_str = NULL;
  1945. X    n->n_lineno = 0;
  1946. X    n->n_nchildren = 0;
  1947. X    n->n_child = NULL;
  1948. X    return n;
  1949. X}
  1950. X
  1951. X#define XXX 3 /* Node alignment factor to speed up realloc */
  1952. X#define XXXROUNDUP(n) ((n) == 1 ? 1 : ((n) + XXX - 1) / XXX * XXX)
  1953. X
  1954. Xnode *
  1955. Xaddchild(n1, type, str, lineno)
  1956. X    register node *n1;
  1957. X    int type;
  1958. X    char *str;
  1959. X    int lineno;
  1960. X{
  1961. X    register int nch = n1->n_nchildren;
  1962. X    register int nch1 = nch+1;
  1963. X    register node *n;
  1964. X    if (XXXROUNDUP(nch) < nch1) {
  1965. X        n = n1->n_child;
  1966. X        nch1 = XXXROUNDUP(nch1);
  1967. X        RESIZE(n, node, nch1);
  1968. X        if (n == NULL)
  1969. X            return NULL;
  1970. X        n1->n_child = n;
  1971. X    }
  1972. X    n = &n1->n_child[n1->n_nchildren++];
  1973. X    n->n_type = type;
  1974. X    n->n_str = str;
  1975. X    n->n_lineno = lineno;
  1976. X    n->n_nchildren = 0;
  1977. X    n->n_child = NULL;
  1978. X    return n;
  1979. X}
  1980. X
  1981. X/* Forward */
  1982. Xstatic void freechildren PROTO((node *));
  1983. X
  1984. X
  1985. Xvoid
  1986. Xfreetree(n)
  1987. X    node *n;
  1988. X{
  1989. X    if (n != NULL) {
  1990. X        freechildren(n);
  1991. X        DEL(n);
  1992. X    }
  1993. X}
  1994. X
  1995. Xstatic void
  1996. Xfreechildren(n)
  1997. X    node *n;
  1998. X{
  1999. X    int i;
  2000. X    for (i = NCH(n); --i >= 0; )
  2001. X        freechildren(CHILD(n, i));
  2002. X    if (n->n_child != NULL)
  2003. X        DEL(n->n_child);
  2004. X    if (STR(n) != NULL)
  2005. X        DEL(STR(n));
  2006. X}
  2007. EOF
  2008. fi
  2009. if test -s 'src/stringobject.h'
  2010. then echo '*** I will not over-write existing file src/stringobject.h'
  2011. else
  2012. echo 'x - src/stringobject.h'
  2013. sed 's/^X//' > 'src/stringobject.h' << 'EOF'
  2014. X/***********************************************************
  2015. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  2016. XNetherlands.
  2017. X
  2018. X                        All Rights Reserved
  2019. X
  2020. XPermission to use, copy, modify, and distribute this software and its 
  2021. Xdocumentation for any purpose and without fee is hereby granted, 
  2022. Xprovided that the above copyright notice appear in all copies and that
  2023. Xboth that copyright notice and this permission notice appear in 
  2024. Xsupporting documentation, and that the names of Stichting Mathematisch
  2025. XCentrum or CWI not be used in advertising or publicity pertaining to
  2026. Xdistribution of the software without specific, written prior permission.
  2027. X
  2028. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  2029. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  2030. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  2031. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  2032. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  2033. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  2034. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  2035. X
  2036. X******************************************************************/
  2037. X
  2038. X/* String object interface */
  2039. X
  2040. X/*
  2041. X123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
  2042. X
  2043. XType stringobject represents a character string.  An extra zero byte is
  2044. Xreserved at the end to ensure it is zero-terminated, but a size is
  2045. Xpresent so strings with null bytes in them can be represented.  This
  2046. Xis an immutable object type.
  2047. X
  2048. XThere are functions to create new string objects, to test
  2049. Xan object for string-ness, and to get the
  2050. Xstring value.  The latter function returns a null pointer
  2051. Xif the object is not of the proper type.
  2052. XThere is a variant that takes an explicit size as well as a
  2053. Xvariant that assumes a zero-terminated string.  Note that none of the
  2054. Xfunctions should be applied to nil objects.
  2055. X*/
  2056. X
  2057. X/* NB The type is revealed here only because it is used in dictobject.c */
  2058. X
  2059. Xtypedef struct {
  2060. X    OB_VARHEAD
  2061. X    char ob_sval[1];
  2062. X} stringobject;
  2063. X
  2064. Xextern typeobject Stringtype;
  2065. X
  2066. X#define is_stringobject(op) ((op)->ob_type == &Stringtype)
  2067. X
  2068. Xextern object *newsizedstringobject PROTO((char *, int));
  2069. Xextern object *newstringobject PROTO((char *));
  2070. Xextern unsigned int getstringsize PROTO((object *));
  2071. Xextern char *getstringvalue PROTO((object *));
  2072. Xextern void joinstring PROTO((object **, object *));
  2073. Xextern int resizestring PROTO((object **, int));
  2074. X
  2075. X/* Macro, trading safety for speed */
  2076. X#define GETSTRINGVALUE(op) ((op)->ob_sval)
  2077. EOF
  2078. fi
  2079. if test -s 'src/structmember.h'
  2080. then echo '*** I will not over-write existing file src/structmember.h'
  2081. else
  2082. echo 'x - src/structmember.h'
  2083. sed 's/^X//' > 'src/structmember.h' << 'EOF'
  2084. X/***********************************************************
  2085. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  2086. XNetherlands.
  2087. X
  2088. X                        All Rights Reserved
  2089. X
  2090. XPermission to use, copy, modify, and distribute this software and its 
  2091. Xdocumentation for any purpose and without fee is hereby granted, 
  2092. Xprovided that the above copyright notice appear in all copies and that
  2093. Xboth that copyright notice and this permission notice appear in 
  2094. Xsupporting documentation, and that the names of Stichting Mathematisch
  2095. XCentrum or CWI not be used in advertising or publicity pertaining to
  2096. Xdistribution of the software without specific, written prior permission.
  2097. X
  2098. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  2099. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  2100. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  2101. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  2102. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  2103. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  2104. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  2105. X
  2106. X******************************************************************/
  2107. X
  2108. X/* Interface to map C struct members to Python object attributes */
  2109. X
  2110. X/* The offsetof() macro calculates the offset of a structure member
  2111. X   in its structure.  Unfortunately this cannot be written down
  2112. X   portably, hence it is provided by a Standard C header file.
  2113. X   For pre-Standard C compilers, here is a version that usually works
  2114. X   (but watch out!): */
  2115. X
  2116. X#ifndef offsetof
  2117. X#define offsetof(type, member) ( (int) & ((type*)0) -> member )
  2118. X#endif
  2119. X
  2120. X/* An array of memberlist structures defines the name, type and offset
  2121. X   of selected members of a C structure.  These can be read by
  2122. X   getmember() and set by setmember() (except if their READONLY flag
  2123. X   is set).  The array must be terminated with an entry whose name
  2124. X   pointer is NULL. */
  2125. X
  2126. Xstruct memberlist {
  2127. X    char *name;
  2128. X    int type;
  2129. X    int offset;
  2130. X    int readonly;
  2131. X};
  2132. X
  2133. X/* Types */
  2134. X#define T_SHORT        0
  2135. X#define T_INT        1
  2136. X#define T_LONG        2
  2137. X#define T_FLOAT        3
  2138. X#define T_DOUBLE    4
  2139. X#define T_STRING    5
  2140. X#define T_OBJECT    6
  2141. X
  2142. X/* Readonly flag */
  2143. X#define READONLY    1
  2144. X#define RO        READONLY        /* Shorthand */
  2145. X
  2146. Xobject *getmember PROTO((char *, struct memberlist *, char *));
  2147. Xint setmember PROTO((char *, struct memberlist *, char *, object *));
  2148. EOF
  2149. fi
  2150. if test -s 'src/tokenizer.h'
  2151. then echo '*** I will not over-write existing file src/tokenizer.h'
  2152. else
  2153. echo 'x - src/tokenizer.h'
  2154. sed 's/^X//' > 'src/tokenizer.h' << 'EOF'
  2155. X/***********************************************************
  2156. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  2157. XNetherlands.
  2158. X
  2159. X                        All Rights Reserved
  2160. X
  2161. XPermission to use, copy, modify, and distribute this software and its 
  2162. Xdocumentation for any purpose and without fee is hereby granted, 
  2163. Xprovided that the above copyright notice appear in all copies and that
  2164. Xboth that copyright notice and this permission notice appear in 
  2165. Xsupporting documentation, and that the names of Stichting Mathematisch
  2166. XCentrum or CWI not be used in advertising or publicity pertaining to
  2167. Xdistribution of the software without specific, written prior permission.
  2168. X
  2169. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  2170. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  2171. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  2172. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  2173. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  2174. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  2175. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  2176. X
  2177. X******************************************************************/
  2178. X
  2179. X/* Tokenizer interface */
  2180. X
  2181. X#include "token.h"    /* For token types */
  2182. X
  2183. X#define MAXINDENT 100    /* Max indentation level */
  2184. X
  2185. X/* Tokenizer state */
  2186. Xstruct tok_state {
  2187. X    /* Input state; buf <= cur <= inp <= end */
  2188. X    /* NB an entire token must fit in the buffer */
  2189. X    char *buf;    /* Input buffer */
  2190. X    char *cur;    /* Next character in buffer */
  2191. X    char *inp;    /* End of data in buffer */
  2192. X    char *end;    /* End of input buffer */
  2193. X    int done;    /* 0 normally, 1 at EOF, -1 after error */
  2194. X    FILE *fp;    /* Rest of input; NULL if tokenizing a string */
  2195. X    int tabsize;    /* Tab spacing */
  2196. X    int indent;    /* Current indentation index */
  2197. X    int indstack[MAXINDENT];    /* Stack of indents */
  2198. X    int atbol;    /* Nonzero if at begin of new line */
  2199. X    int pendin;    /* Pending indents (if > 0) or dedents (if < 0) */
  2200. X    char *prompt, *nextprompt;    /* For interactive prompting */
  2201. X    int lineno;    /* Current line number */
  2202. X};
  2203. X
  2204. Xextern struct tok_state *tok_setups PROTO((char *));
  2205. Xextern struct tok_state *tok_setupf PROTO((FILE *, char *ps1, char *ps2));
  2206. Xextern void tok_free PROTO((struct tok_state *));
  2207. Xextern int tok_get PROTO((struct tok_state *, char **, char **));
  2208. EOF
  2209. fi
  2210. if test -s 'src/tupleobject.h'
  2211. then echo '*** I will not over-write existing file src/tupleobject.h'
  2212. else
  2213. echo 'x - src/tupleobject.h'
  2214. sed 's/^X//' > 'src/tupleobject.h' << 'EOF'
  2215. X/***********************************************************
  2216. XCopyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  2217. XNetherlands.
  2218. X
  2219. X                        All Rights Reserved
  2220. X
  2221. XPermission to use, copy, modify, and distribute this software and its 
  2222. Xdocumentation for any purpose and without fee is hereby granted, 
  2223. Xprovided that the above copyright notice appear in all copies and that
  2224. Xboth that copyright notice and this permission notice appear in 
  2225. Xsupporting documentation, and that the names of Stichting Mathematisch
  2226. XCentrum or CWI not be used in advertising or publicity pertaining to
  2227. Xdistribution of the software without specific, written prior permission.
  2228. X
  2229. XSTICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  2230. XTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  2231. XFITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  2232. XFOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  2233. XWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  2234. XACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  2235. XOF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  2236. X
  2237. X******************************************************************/
  2238. X
  2239. X/* Tuple object interface */
  2240. X
  2241. X/*
  2242. X123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
  2243. X
  2244. XAnother generally useful object type is an tuple of object pointers.
  2245. XThis is a mutable type: the tuple items can be changed (but not their
  2246. Xnumber).  Out-of-range indices or non-tuple objects are ignored.
  2247. X
  2248. X*** WARNING *** settupleitem does not increment the new item's reference
  2249. Xcount, but does decrement the reference count of the item it replaces,
  2250. Xif not nil.  It does *decrement* the reference count if it is *not*
  2251. Xinserted in the tuple.  Similarly, gettupleitem does not increment the
  2252. Xreturned item's reference count.
  2253. X*/
  2254. X
  2255. Xtypedef struct {
  2256. X    OB_VARHEAD
  2257. X    object *ob_item[1];
  2258. X} tupleobject;
  2259. X
  2260. Xextern typeobject Tupletype;
  2261. X
  2262. X#define is_tupleobject(op) ((op)->ob_type == &Tupletype)
  2263. X
  2264. Xextern object *newtupleobject PROTO((int size));
  2265. Xextern int gettuplesize PROTO((object *));
  2266. Xextern object *gettupleitem PROTO((object *, int));
  2267. Xextern int settupleitem PROTO((object *, int, object *));
  2268. X
  2269. X/* Macro, trading safety for speed */
  2270. X#define GETTUPLEITEM(op, i) ((op)->ob_item[i])
  2271. EOF
  2272. fi
  2273. echo 'Part 18 out of 21 of pack.out complete.'
  2274. exit 0
  2275.