Megatest

Hex Artifact Content
Login

Artifact 52b73adafb313cb241d18c82f6d2e0f9bf86deb2:


0000: 0a 3b 3b 20 43 6f 70 79 72 69 67 68 74 20 32 30  .;; Copyright 20
0010: 30 36 2d 32 30 31 34 2c 20 4d 61 74 74 68 65 77  06-2014, Matthew
0020: 20 57 65 6c 6c 61 6e 64 2e 0a 3b 3b 20 0a 3b 3b   Welland..;; .;;
0030: 20 20 54 68 69 73 20 70 72 6f 67 72 61 6d 20 69    This program i
0040: 73 20 6d 61 64 65 20 61 76 61 69 6c 61 62 6c 65  s made available
0050: 20 75 6e 64 65 72 20 74 68 65 20 47 4e 55 20 47   under the GNU G
0060: 50 4c 20 76 65 72 73 69 6f 6e 20 32 2e 30 20 6f  PL version 2.0 o
0070: 72 0a 3b 3b 20 20 67 72 65 61 74 65 72 2e 20 53  r.;;  greater. S
0080: 65 65 20 74 68 65 20 61 63 63 6f 6d 70 61 6e 79  ee the accompany
0090: 69 6e 67 20 66 69 6c 65 20 43 4f 50 59 49 4e 47  ing file COPYING
00a0: 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 3b 3b   for details..;;
00b0: 20 0a 3b 3b 20 20 54 68 69 73 20 70 72 6f 67 72   .;;  This progr
00c0: 61 6d 20 69 73 20 64 69 73 74 72 69 62 75 74 65  am is distribute
00d0: 64 20 57 49 54 48 4f 55 54 20 41 4e 59 20 57 41  d WITHOUT ANY WA
00e0: 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75 74 20  RRANTY; without 
00f0: 65 76 65 6e 20 74 68 65 0a 3b 3b 20 20 69 6d 70  even the.;;  imp
0100: 6c 69 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66  lied warranty of
0110: 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54 59   MERCHANTABILITY
0120: 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52 20   or FITNESS FOR 
0130: 41 20 50 41 52 54 49 43 55 4c 41 52 0a 3b 3b 20  A PARTICULAR.;; 
0140: 20 50 55 52 50 4f 53 45 2e 0a 0a 28 72 65 71 75   PURPOSE...(requ
0150: 69 72 65 2d 65 78 74 65 6e 73 69 6f 6e 20 28 73  ire-extension (s
0160: 72 66 69 20 31 38 29 20 65 78 74 72 61 73 20 74  rfi 18) extras t
0170: 63 70 20 73 31 31 6e 29 0a 0a 28 75 73 65 20 73  cp s11n)..(use s
0180: 71 6c 69 74 65 33 20 73 72 66 69 2d 31 20 70 6f  qlite3 srfi-1 po
0190: 73 69 78 20 73 72 66 69 2d 36 39 20 68 6f 73 74  six srfi-69 host
01a0: 69 6e 66 6f 20 64 6f 74 2d 6c 6f 63 6b 69 6e 67  info dot-locking
01b0: 20 7a 33 29 0a 28 69 6d 70 6f 72 74 20 28 70 72   z3).(import (pr
01c0: 65 66 69 78 20 73 71 6c 69 74 65 33 20 73 71 6c  efix sqlite3 sql
01d0: 69 74 65 33 3a 29 29 0a 0a 28 64 65 63 6c 61 72  ite3:))..(declar
01e0: 65 20 28 75 6e 69 74 20 70 6f 72 74 6c 6f 67 67  e (unit portlogg
01f0: 65 72 29 29 0a 28 64 65 63 6c 61 72 65 20 28 75  er)).(declare (u
0200: 73 65 73 20 64 62 29 29 0a 0a 3b 3b 20 6c 73 6f  ses db))..;; lso
0210: 66 20 2d 69 0a 0a 0a 28 64 65 66 69 6e 65 20 28  f -i...(define (
0220: 70 6f 72 74 6c 6f 67 67 65 72 3a 6f 70 65 6e 2d  portlogger:open-
0230: 64 62 20 66 6e 61 6d 65 29 0a 20 20 28 6c 65 74  db fname).  (let
0240: 2a 20 28 28 61 76 61 69 6c 20 20 20 20 28 74 61  * ((avail    (ta
0250: 73 6b 73 3a 77 61 69 74 2d 6f 6e 2d 6a 6f 75 72  sks:wait-on-jour
0260: 6e 61 6c 20 66 6e 61 6d 65 20 35 20 72 65 6d 6f  nal fname 5 remo
0270: 76 65 3a 20 23 74 29 29 20 3b 3b 20 77 61 69 74  ve: #t)) ;; wait
0280: 20 75 70 20 74 6f 20 61 62 6f 75 74 20 31 30 20   up to about 10 
0290: 73 65 63 6f 6e 64 73 20 66 6f 72 20 74 68 65 20  seconds for the 
02a0: 6a 6f 75 72 6e 61 6c 20 74 6f 20 67 6f 20 61 77  journal to go aw
02b0: 61 79 0a 09 20 28 65 78 69 73 74 73 20 20 20 28  ay.. (exists   (
02c0: 66 69 6c 65 2d 65 78 69 73 74 73 3f 20 66 6e 61  file-exists? fna
02d0: 6d 65 29 29 0a 09 20 28 64 62 20 20 20 20 20 20  me)).. (db      
02e0: 20 28 69 66 20 61 76 61 69 6c 20 0a 09 09 20 20   (if avail ...  
02f0: 20 20 20 20 20 28 73 71 6c 69 74 65 33 3a 6f 70       (sqlite3:op
0300: 65 6e 2d 64 61 74 61 62 61 73 65 20 66 6e 61 6d  en-database fnam
0310: 65 29 0a 09 09 20 20 20 20 20 20 20 28 62 65 67  e)...       (beg
0320: 69 6e 0a 09 09 09 20 28 73 79 73 74 65 6d 20 28  in.... (system (
0330: 63 6f 6e 63 20 22 72 6d 20 2d 66 20 22 20 66 6e  conc "rm -f " fn
0340: 61 6d 65 29 29 0a 09 09 09 20 28 73 71 6c 69 74  ame)).... (sqlit
0350: 65 33 3a 6f 70 65 6e 2d 64 61 74 61 62 61 73 65  e3:open-database
0360: 20 66 6e 61 6d 65 29 29 29 29 0a 09 20 28 68 61   fname)))).. (ha
0370: 6e 64 6c 65 72 20 20 28 6d 61 6b 65 2d 62 75 73  ndler  (make-bus
0380: 79 2d 74 69 6d 65 6f 75 74 20 31 33 36 30 30 30  y-timeout 136000
0390: 29 29 0a 09 20 28 63 61 6e 77 72 69 74 65 20 28  )).. (canwrite (
03a0: 66 69 6c 65 2d 77 72 69 74 65 2d 61 63 63 65 73  file-write-acces
03b0: 73 3f 20 66 6e 61 6d 65 29 29 29 0a 09 20 3b 3b  s? fname))).. ;;
03c0: 20 28 64 62 2d 69 6e 69 74 20 20 28 6c 61 6d 62   (db-init  (lamb
03d0: 64 61 20 28 29 0a 09 20 3b 3b 20 20 20 20 20 20  da ().. ;;      
03e0: 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 3a         (sqlite3:
03f0: 65 78 65 63 75 74 65 20 0a 09 20 3b 3b 20 20 20  execute .. ;;   
0400: 20 20 20 20 20 20 20 20 20 20 20 64 62 0a 09 20             db.. 
0410: 3b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;;              
0420: 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 49 46  "CREATE TABLE IF
0430: 20 4e 4f 54 20 45 58 49 53 54 53 20 70 6f 72 74   NOT EXISTS port
0440: 73 20 28 0a 20 20 20 20 20 20 20 20 20 3b 3b 20  s (.         ;; 
0450: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0460: 70 6f 72 74 20 49 4e 54 45 47 45 52 20 50 52 49  port INTEGER PRI
0470: 4d 41 52 59 20 4b 45 59 2c 0a 20 20 20 20 20 20  MARY KEY,.      
0480: 20 20 20 3b 3b 20 20 20 20 20 20 20 20 20 20 20     ;;           
0490: 20 20 20 20 20 20 73 74 61 74 65 20 54 45 58 54        state TEXT
04a0: 20 44 45 46 41 55 4c 54 20 27 6e 6f 74 2d 75 73   DEFAULT 'not-us
04b0: 65 64 27 2c 0a 20 20 20 20 20 20 20 20 20 3b 3b  ed',.         ;;
04c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
04d0: 20 66 61 69 6c 5f 63 6f 75 6e 74 20 49 4e 54 45   fail_count INTE
04e0: 47 45 52 20 44 45 46 41 55 4c 54 20 30 2c 0a 20  GER DEFAULT 0,. 
04f0: 20 20 20 20 20 20 20 20 3b 3b 20 20 20 20 20 20          ;;      
0500: 20 20 20 20 20 20 20 20 20 20 20 75 70 64 61 74             updat
0510: 65 5f 74 69 6d 65 20 54 49 4d 45 53 54 41 4d 50  e_time TIMESTAMP
0520: 20 44 45 46 41 55 4c 54 20 28 73 74 72 66 74 69   DEFAULT (strfti
0530: 6d 65 28 27 25 73 27 2c 27 6e 6f 77 27 29 29 20  me('%s','now')) 
0540: 29 3b 22 29 29 29 29 0a 20 20 20 20 28 73 71 6c  );")))).    (sql
0550: 69 74 65 33 3a 73 65 74 2d 62 75 73 79 2d 68 61  ite3:set-busy-ha
0560: 6e 64 6c 65 72 21 20 64 62 20 68 61 6e 64 6c 65  ndler! db handle
0570: 72 29 0a 20 20 20 20 28 64 62 3a 73 65 74 2d 73  r).    (db:set-s
0580: 79 6e 63 20 64 62 29 20 3b 3b 20 28 73 71 6c 69  ync db) ;; (sqli
0590: 74 65 33 3a 65 78 65 63 75 74 65 20 64 62 20 22  te3:execute db "
05a0: 50 52 41 47 4d 41 20 73 79 6e 63 68 72 6f 6e 6f  PRAGMA synchrono
05b0: 75 73 20 3d 20 30 3b 22 29 0a 20 20 20 20 3b 3b  us = 0;").    ;;
05c0: 20 28 69 66 20 28 6e 6f 74 20 65 78 69 73 74 73   (if (not exists
05d0: 29 20 3b 3b 20 6e 65 65 64 65 64 20 77 69 74 68  ) ;; needed with
05e0: 20 49 46 20 4e 4f 54 20 45 58 49 53 54 53 3f 0a   IF NOT EXISTS?.
05f0: 20 20 20 20 28 73 71 6c 69 74 65 33 3a 65 78 65      (sqlite3:exe
0600: 63 75 74 65 20 0a 20 20 20 20 20 64 62 0a 20 20  cute .     db.  
0610: 20 20 20 22 43 52 45 41 54 45 20 54 41 42 4c 45     "CREATE TABLE
0620: 20 49 46 20 4e 4f 54 20 45 58 49 53 54 53 20 70   IF NOT EXISTS p
0630: 6f 72 74 73 20 28 0a 20 20 20 20 20 20 20 20 20  orts (.         
0640: 20 20 20 70 6f 72 74 20 49 4e 54 45 47 45 52 20     port INTEGER 
0650: 50 52 49 4d 41 52 59 20 4b 45 59 2c 0a 20 20 20  PRIMARY KEY,.   
0660: 20 20 20 20 20 20 20 20 20 73 74 61 74 65 20 54           state T
0670: 45 58 54 20 44 45 46 41 55 4c 54 20 27 6e 6f 74  EXT DEFAULT 'not
0680: 2d 75 73 65 64 27 2c 0a 20 20 20 20 20 20 20 20  -used',.        
0690: 20 20 20 20 66 61 69 6c 5f 63 6f 75 6e 74 20 49      fail_count I
06a0: 4e 54 45 47 45 52 20 44 45 46 41 55 4c 54 20 30  NTEGER DEFAULT 0
06b0: 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 70  ,.            up
06c0: 64 61 74 65 5f 74 69 6d 65 20 54 49 4d 45 53 54  date_time TIMEST
06d0: 41 4d 50 20 44 45 46 41 55 4c 54 20 28 73 74 72  AMP DEFAULT (str
06e0: 66 74 69 6d 65 28 27 25 73 27 2c 27 6e 6f 77 27  ftime('%s','now'
06f0: 29 29 20 29 3b 22 29 0a 20 20 20 20 64 62 29 29  )) );").    db))
0700: 0a 0a 28 64 65 66 69 6e 65 20 28 70 6f 72 74 6c  ..(define (portl
0710: 6f 67 67 65 72 3a 6f 70 65 6e 2d 72 75 6e 2d 63  ogger:open-run-c
0720: 6c 6f 73 65 20 70 72 6f 63 20 2e 20 70 61 72 61  lose proc . para
0730: 6d 73 29 0a 20 20 28 6c 65 74 2a 20 28 28 66 6e  ms).  (let* ((fn
0740: 61 6d 65 20 20 28 63 6f 6e 63 20 22 2f 74 6d 70  ame  (conc "/tmp
0750: 2f 2e 22 20 28 63 75 72 72 65 6e 74 2d 75 73 65  /." (current-use
0760: 72 2d 6e 61 6d 65 29 20 22 2d 70 6f 72 74 6c 6f  r-name) "-portlo
0770: 67 67 65 72 2e 64 62 22 29 29 0a 09 20 28 61 76  gger.db")).. (av
0780: 61 69 6c 20 20 28 74 61 73 6b 73 3a 77 61 69 74  ail  (tasks:wait
0790: 2d 6f 6e 2d 6a 6f 75 72 6e 61 6c 20 66 6e 61 6d  -on-journal fnam
07a0: 65 20 31 30 29 29 29 20 3b 3b 20 77 61 69 74 20  e 10))) ;; wait 
07b0: 75 70 20 74 6f 20 61 62 6f 75 74 20 31 30 20 73  up to about 10 s
07c0: 65 63 6f 6e 64 73 20 66 6f 72 20 74 68 65 20 6a  econds for the j
07d0: 6f 75 72 6e 61 6c 20 74 6f 20 67 6f 20 61 77 61  ournal to go awa
07e0: 79 0a 20 20 20 20 28 68 61 6e 64 6c 65 2d 65 78  y.    (handle-ex
07f0: 63 65 70 74 69 6f 6e 73 0a 20 20 20 20 20 65 78  ceptions.     ex
0800: 6e 0a 20 20 20 20 20 28 62 65 67 69 6e 0a 20 20  n.     (begin.  
0810: 20 20 20 20 20 3b 3b 20 28 72 65 6c 65 61 73 65       ;; (release
0820: 2d 64 6f 74 2d 6c 6f 63 6b 20 66 6e 61 6d 65 29  -dot-lock fname)
0830: 0a 20 20 20 20 20 20 20 28 64 65 62 75 67 3a 70  .       (debug:p
0840: 72 69 6e 74 20 30 20 2a 64 65 66 61 75 6c 74 2d  rint 0 *default-
0850: 6c 6f 67 2d 70 6f 72 74 2a 20 22 45 52 52 4f 52  log-port* "ERROR
0860: 3a 20 70 6f 72 74 6c 6f 67 67 65 72 3a 6f 70 65  : portlogger:ope
0870: 6e 2d 72 75 6e 2d 63 6c 6f 73 65 20 66 61 69 6c  n-run-close fail
0880: 65 64 2e 20 22 20 70 72 6f 63 20 22 20 22 20 70  ed. " proc " " p
0890: 61 72 61 6d 73 29 0a 20 20 20 20 20 20 20 28 64  arams).       (d
08a0: 65 62 75 67 3a 70 72 69 6e 74 20 30 20 2a 64 65  ebug:print 0 *de
08b0: 66 61 75 6c 74 2d 6c 6f 67 2d 70 6f 72 74 2a 20  fault-log-port* 
08c0: 22 20 6d 65 73 73 61 67 65 3a 20 22 20 28 28 63  " message: " ((c
08d0: 6f 6e 64 69 74 69 6f 6e 2d 70 72 6f 70 65 72 74  ondition-propert
08e0: 79 2d 61 63 63 65 73 73 6f 72 20 27 65 78 6e 20  y-accessor 'exn 
08f0: 27 6d 65 73 73 61 67 65 29 20 65 78 6e 29 29 0a  'message) exn)).
0900: 20 20 20 20 20 20 20 28 64 65 62 75 67 3a 70 72         (debug:pr
0910: 69 6e 74 20 30 20 2a 64 65 66 61 75 6c 74 2d 6c  int 0 *default-l
0920: 6f 67 2d 70 6f 72 74 2a 20 22 65 78 6e 3d 22 20  og-port* "exn=" 
0930: 28 63 6f 6e 64 69 74 69 6f 6e 2d 3e 6c 69 73 74  (condition->list
0940: 20 65 78 6e 29 29 0a 20 20 20 20 20 20 20 28 69   exn)).       (i
0950: 66 20 28 66 69 6c 65 2d 65 78 69 73 74 73 3f 20  f (file-exists? 
0960: 66 6e 61 6d 65 29 28 64 65 6c 65 74 65 2d 66 69  fname)(delete-fi
0970: 6c 65 20 66 6e 61 6d 65 29 29 20 3b 3b 20 62 72  le fname)) ;; br
0980: 75 74 61 6c 6c 79 20 67 65 74 20 72 69 64 20 6f  utally get rid o
0990: 66 20 69 74 0a 20 20 20 20 20 20 20 28 70 72 69  f it.       (pri
09a0: 6e 74 2d 63 61 6c 6c 2d 63 68 61 69 6e 20 28 63  nt-call-chain (c
09b0: 75 72 72 65 6e 74 2d 65 72 72 6f 72 2d 70 6f 72  urrent-error-por
09c0: 74 29 29 29 0a 20 20 20 20 20 28 6c 65 74 2a 20  t))).     (let* 
09d0: 28 3b 3b 20 28 6c 6f 63 6b 20 20 20 28 6f 62 74  (;; (lock   (obt
09e0: 61 69 6e 2d 64 6f 74 2d 6c 6f 63 6b 20 66 6e 61  ain-dot-lock fna
09f0: 6d 65 20 32 20 39 20 31 30 29 29 0a 09 20 20 20  me 2 9 10))..   
0a00: 20 28 64 62 20 20 20 20 20 28 70 6f 72 74 6c 6f   (db     (portlo
0a10: 67 67 65 72 3a 6f 70 65 6e 2d 64 62 20 66 6e 61  gger:open-db fna
0a20: 6d 65 29 29 0a 09 20 20 20 20 28 72 65 73 20 20  me))..    (res  
0a30: 20 20 28 61 70 70 6c 79 20 70 72 6f 63 20 64 62    (apply proc db
0a40: 20 70 61 72 61 6d 73 29 29 29 0a 20 20 20 20 20   params))).     
0a50: 20 20 28 73 71 6c 69 74 65 33 3a 66 69 6e 61 6c    (sqlite3:final
0a60: 69 7a 65 21 20 64 62 29 0a 20 20 20 20 20 20 20  ize! db).       
0a70: 3b 3b 20 28 72 65 6c 65 61 73 65 2d 64 6f 74 2d  ;; (release-dot-
0a80: 6c 6f 63 6b 20 66 6e 61 6d 65 29 0a 20 20 20 20  lock fname).    
0a90: 20 20 20 72 65 73 29 29 29 29 0a 0a 3b 3b 20 28     res))))..;; (
0aa0: 66 6f 6c 64 2d 72 6f 77 20 50 52 4f 43 20 49 4e  fold-row PROC IN
0ab0: 49 54 20 44 41 54 41 42 41 53 45 20 53 51 4c 20  IT DATABASE SQL 
0ac0: 2e 20 50 41 52 41 4d 45 54 45 52 53 29 20 0a 28  . PARAMETERS) .(
0ad0: 64 65 66 69 6e 65 20 28 70 6f 72 74 6c 6f 67 67  define (portlogg
0ae0: 65 72 3a 74 61 6b 65 2d 70 6f 72 74 20 64 62 20  er:take-port db 
0af0: 70 6f 72 74 6e 75 6d 29 0a 20 20 28 6c 65 74 2a  portnum).  (let*
0b00: 20 28 28 71 72 79 31 20 28 73 71 6c 69 74 65 33   ((qry1 (sqlite3
0b10: 3a 70 72 65 70 61 72 65 20 64 62 20 22 49 4e 53  :prepare db "INS
0b20: 45 52 54 20 49 4e 54 4f 20 70 6f 72 74 73 20 28  ERT INTO ports (
0b30: 70 6f 72 74 2c 73 74 61 74 65 29 20 56 41 4c 55  port,state) VALU
0b40: 45 53 20 28 3f 2c 3f 29 3b 22 29 29 0a 09 20 28  ES (?,?);")).. (
0b50: 71 72 79 32 20 28 73 71 6c 69 74 65 33 3a 70 72  qry2 (sqlite3:pr
0b60: 65 70 61 72 65 20 64 62 20 22 55 50 44 41 54 45  epare db "UPDATE
0b70: 20 70 6f 72 74 73 20 53 45 54 20 73 74 61 74 65   ports SET state
0b80: 3d 3f 2c 75 70 64 61 74 65 5f 74 69 6d 65 3d 73  =?,update_time=s
0b90: 74 72 66 74 69 6d 65 28 27 25 73 27 2c 27 6e 6f  trftime('%s','no
0ba0: 77 27 29 20 57 48 45 52 45 20 70 6f 72 74 3d 3f  w') WHERE port=?
0bb0: 3b 22 29 29 0a 09 20 28 71 72 79 33 20 28 73 71  ;")).. (qry3 (sq
0bc0: 6c 69 74 65 33 3a 70 72 65 70 61 72 65 20 64 62  lite3:prepare db
0bd0: 20 22 53 45 4c 45 43 54 20 73 74 61 74 65 20 46   "SELECT state F
0be0: 52 4f 4d 20 70 6f 72 74 73 20 57 48 45 52 45 20  ROM ports WHERE 
0bf0: 70 6f 72 74 3d 3f 3b 22 29 29 0a 09 20 28 72 65  port=?;")).. (re
0c00: 73 20 20 28 73 71 6c 69 74 65 33 3a 77 69 74 68  s  (sqlite3:with
0c10: 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a 09 09 64  -transaction...d
0c20: 62 0a 09 09 28 6c 61 6d 62 64 61 20 28 29 0a 09  b...(lambda ()..
0c30: 09 20 20 3b 3b 20 28 66 6f 6c 64 2d 72 6f 77 20  .  ;; (fold-row 
0c40: 28 6c 61 6d 62 64 61 20 28 76 61 72 20 63 75 72  (lambda (var cur
0c50: 72 29 20 28 6f 72 20 76 61 72 20 63 75 72 72 29  r) (or var curr)
0c60: 29 20 23 66 20 64 62 20 22 53 45 4c 45 43 54 20  ) #f db "SELECT 
0c70: 76 61 72 20 46 52 4f 4d 20 66 6f 6f 20 57 48 45  var FROM foo WHE
0c80: 52 45 20 69 64 3d 31 30 30 3b 22 29 0a 09 09 20  RE id=100;")... 
0c90: 20 28 6c 65 74 2a 20 28 28 63 75 72 72 20 23 66   (let* ((curr #f
0ca0: 29 0a 09 09 09 20 28 72 65 73 20 20 23 66 29 29  ).... (res  #f))
0cb0: 0a 09 09 20 20 20 20 28 73 65 74 21 20 63 75 72  ...    (set! cur
0cc0: 72 20 28 73 71 6c 69 74 65 33 3a 66 6f 6c 64 2d  r (sqlite3:fold-
0cd0: 72 6f 77 0a 09 09 09 09 28 6c 61 6d 62 64 61 20  row.....(lambda 
0ce0: 28 76 61 72 20 63 75 72 72 29 0a 09 09 09 09 20  (var curr)..... 
0cf0: 20 28 6f 72 20 63 75 72 72 20 76 61 72 20 63 75   (or curr var cu
0d00: 72 72 29 29 0a 09 09 09 09 22 6e 6f 74 2d 74 72  rr))....."not-tr
0d10: 69 65 64 22 0a 09 09 09 09 71 72 79 33 0a 09 09  ied".....qry3...
0d20: 09 09 70 6f 72 74 6e 75 6d 29 29 0a 09 09 20 20  ..portnum))...  
0d30: 20 20 3b 3b 20 28 70 72 69 6e 74 20 22 63 75 72    ;; (print "cur
0d40: 72 3d 22 20 63 75 72 72 29 0a 09 09 20 20 20 20  r=" curr)...    
0d50: 28 73 65 74 21 20 72 65 73 20 28 63 61 73 65 20  (set! res (case 
0d60: 28 73 74 72 69 6e 67 2d 3e 73 79 6d 62 6f 6c 20  (string->symbol 
0d70: 63 75 72 72 29 0a 09 09 09 09 28 28 72 65 6c 65  curr).....((rele
0d80: 61 73 65 64 29 20 20 28 73 71 6c 69 74 65 33 3a  ased)  (sqlite3:
0d90: 65 78 65 63 75 74 65 20 71 72 79 32 20 22 74 61  execute qry2 "ta
0da0: 6b 65 6e 22 20 70 6f 72 74 6e 75 6d 29 20 27 74  ken" portnum) 't
0db0: 61 6b 65 6e 29 0a 09 09 09 09 28 28 6e 6f 74 2d  aken).....((not-
0dc0: 74 72 69 65 64 29 20 28 73 71 6c 69 74 65 33 3a  tried) (sqlite3:
0dd0: 65 78 65 63 75 74 65 20 71 72 79 31 20 70 6f 72  execute qry1 por
0de0: 74 6e 75 6d 20 22 74 61 6b 65 6e 22 29 20 27 74  tnum "taken") 't
0df0: 61 6b 65 6e 29 0a 09 09 09 09 28 28 74 61 6b 65  aken).....((take
0e00: 6e 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n)              
0e10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0e20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 27 61                'a
0e30: 6c 72 65 61 64 79 2d 74 61 6b 65 6e 29 0a 09 09  lready-taken)...
0e40: 09 09 28 28 66 61 69 6c 65 64 29 20 20 20 20 20  ..((failed)     
0e50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0e60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0e70: 20 20 20 20 20 20 27 66 61 69 6c 65 64 29 0a 09        'failed)..
0e80: 09 09 09 28 65 6c 73 65 20 20 20 20 20 20 20 20  ...(else        
0e90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0ea0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0eb0: 20 20 20 20 20 20 20 27 65 72 72 6f 72 29 29 29         'error)))
0ec0: 0a 09 09 20 20 20 20 3b 3b 20 28 70 72 69 6e 74  ...    ;; (print
0ed0: 20 22 72 65 73 3d 22 20 72 65 73 29 0a 09 09 20   "res=" res)... 
0ee0: 20 20 20 72 65 73 29 29 29 29 29 0a 20 20 20 20     res))))).    
0ef0: 28 73 71 6c 69 74 65 33 3a 66 69 6e 61 6c 69 7a  (sqlite3:finaliz
0f00: 65 21 20 71 72 79 31 29 0a 20 20 20 20 28 73 71  e! qry1).    (sq
0f10: 6c 69 74 65 33 3a 66 69 6e 61 6c 69 7a 65 21 20  lite3:finalize! 
0f20: 71 72 79 32 29 0a 20 20 20 20 28 73 71 6c 69 74  qry2).    (sqlit
0f30: 65 33 3a 66 69 6e 61 6c 69 7a 65 21 20 71 72 79  e3:finalize! qry
0f40: 33 29 0a 20 20 20 20 72 65 73 29 29 0a 0a 28 64  3).    res))..(d
0f50: 65 66 69 6e 65 20 28 70 6f 72 74 6c 6f 67 67 65  efine (portlogge
0f60: 72 3a 67 65 74 2d 70 72 65 76 2d 75 73 65 64 2d  r:get-prev-used-
0f70: 70 6f 72 74 20 64 62 29 0a 20 20 28 68 61 6e 64  port db).  (hand
0f80: 6c 65 2d 65 78 63 65 70 74 69 6f 6e 73 0a 20 20  le-exceptions.  
0f90: 20 65 78 6e 0a 20 20 20 28 62 65 67 69 6e 0a 20   exn.   (begin. 
0fa0: 20 20 20 20 28 64 65 62 75 67 3a 70 72 69 6e 74      (debug:print
0fb0: 20 30 20 2a 64 65 66 61 75 6c 74 2d 6c 6f 67 2d   0 *default-log-
0fc0: 70 6f 72 74 2a 20 22 45 58 43 45 50 54 49 4f 4e  port* "EXCEPTION
0fd0: 3a 20 70 6f 72 74 6c 6f 67 67 65 72 20 64 61 74  : portlogger dat
0fe0: 61 62 61 73 65 20 70 72 6f 62 61 62 6c 79 20 6f  abase probably o
0ff0: 76 65 72 6c 6f 61 64 65 64 20 6f 72 20 75 6e 72  verloaded or unr
1000: 65 61 64 61 62 6c 65 2e 20 49 66 20 79 6f 75 20  eadable. If you 
1010: 73 65 65 20 74 68 69 73 20 6d 65 73 73 61 67 65  see this message
1020: 20 61 67 61 69 6e 20 72 65 6d 6f 76 65 20 2f 74   again remove /t
1030: 6d 70 2f 2e 24 55 53 45 52 2d 70 6f 72 74 6c 6f  mp/.$USER-portlo
1040: 67 67 65 72 2e 64 62 22 29 0a 20 20 20 20 20 28  gger.db").     (
1050: 64 65 62 75 67 3a 70 72 69 6e 74 20 30 20 2a 64  debug:print 0 *d
1060: 65 66 61 75 6c 74 2d 6c 6f 67 2d 70 6f 72 74 2a  efault-log-port*
1070: 20 22 20 6d 65 73 73 61 67 65 3a 20 22 20 28 28   " message: " ((
1080: 63 6f 6e 64 69 74 69 6f 6e 2d 70 72 6f 70 65 72  condition-proper
1090: 74 79 2d 61 63 63 65 73 73 6f 72 20 27 65 78 6e  ty-accessor 'exn
10a0: 20 27 6d 65 73 73 61 67 65 29 20 65 78 6e 29 29   'message) exn))
10b0: 0a 20 20 20 20 20 28 64 65 62 75 67 3a 70 72 69  .     (debug:pri
10c0: 6e 74 20 30 20 2a 64 65 66 61 75 6c 74 2d 6c 6f  nt 0 *default-lo
10d0: 67 2d 70 6f 72 74 2a 20 22 65 78 6e 3d 22 20 28  g-port* "exn=" (
10e0: 63 6f 6e 64 69 74 69 6f 6e 2d 3e 6c 69 73 74 20  condition->list 
10f0: 65 78 6e 29 29 0a 20 20 20 20 20 28 70 72 69 6e  exn)).     (prin
1100: 74 2d 63 61 6c 6c 2d 63 68 61 69 6e 20 28 63 75  t-call-chain (cu
1110: 72 72 65 6e 74 2d 65 72 72 6f 72 2d 70 6f 72 74  rrent-error-port
1120: 29 29 0a 20 20 20 20 20 28 64 65 62 75 67 3a 70  )).     (debug:p
1130: 72 69 6e 74 20 30 20 2a 64 65 66 61 75 6c 74 2d  rint 0 *default-
1140: 6c 6f 67 2d 70 6f 72 74 2a 20 22 43 6f 6e 74 69  log-port* "Conti
1150: 6e 75 69 6e 67 20 61 6e 79 77 61 79 2e 22 29 0a  nuing anyway.").
1160: 20 20 20 20 20 23 66 29 0a 20 20 20 28 73 71 6c       #f).   (sql
1170: 69 74 65 33 3a 66 6f 6c 64 2d 72 6f 77 0a 20 20  ite3:fold-row.  
1180: 20 20 28 6c 61 6d 62 64 61 20 28 76 61 72 20 63    (lambda (var c
1190: 75 72 72 29 0a 20 20 20 20 20 20 28 6f 72 20 63  urr).      (or c
11a0: 75 72 72 20 76 61 72 20 63 75 72 72 29 29 0a 20  urr var curr)). 
11b0: 20 20 20 23 66 0a 20 20 20 20 64 62 0a 20 20 20     #f.    db.   
11c0: 20 22 53 45 4c 45 43 54 20 28 70 6f 72 74 29 20   "SELECT (port) 
11d0: 46 52 4f 4d 20 70 6f 72 74 73 20 57 48 45 52 45  FROM ports WHERE
11e0: 20 73 74 61 74 65 3d 27 72 65 6c 65 61 73 65 64   state='released
11f0: 27 20 4c 49 4d 49 54 20 31 3b 22 29 29 29 0a 0a  ' LIMIT 1;")))..
1200: 28 64 65 66 69 6e 65 20 28 70 6f 72 74 6c 6f 67  (define (portlog
1210: 67 65 72 3a 66 69 6e 64 2d 70 6f 72 74 20 64 62  ger:find-port db
1220: 29 0a 20 20 28 6c 65 74 2a 20 28 28 6c 6f 77 70  ).  (let* ((lowp
1230: 6f 72 74 20 28 6c 65 74 20 28 28 76 61 6c 20 28  ort (let ((val (
1240: 63 6f 6e 66 69 67 66 3a 6c 6f 6f 6b 75 70 20 2a  configf:lookup *
1250: 63 6f 6e 66 69 67 64 61 74 2a 20 22 73 65 72 76  configdat* "serv
1260: 65 72 22 20 22 6c 6f 77 70 6f 72 74 22 29 29 29  er" "lowport")))
1270: 0a 09 09 20 20 20 20 28 69 66 20 28 61 6e 64 20  ...    (if (and 
1280: 76 61 6c 20 0a 09 09 09 20 20 20 20 20 28 73 74  val ....     (st
1290: 72 69 6e 67 2d 3e 6e 75 6d 62 65 72 20 76 61 6c  ring->number val
12a0: 29 29 0a 09 09 09 28 73 74 72 69 6e 67 2d 3e 6e  ))....(string->n
12b0: 75 6d 62 65 72 20 76 61 6c 29 0a 09 09 09 33 32  umber val)....32
12c0: 37 36 38 29 29 29 0a 09 20 28 70 6f 72 74 6e 75  768))).. (portnu
12d0: 6d 20 28 6f 72 20 28 70 6f 72 74 6c 6f 67 67 65  m (or (portlogge
12e0: 72 3a 67 65 74 2d 70 72 65 76 2d 75 73 65 64 2d  r:get-prev-used-
12f0: 70 6f 72 74 20 64 62 29 0a 09 09 20 20 20 20 20  port db)...     
1300: 20 28 2b 20 6c 6f 77 70 6f 72 74 20 3b 3b 20 74   (+ lowport ;; t
1310: 6f 70 20 6f 66 20 72 65 67 69 73 74 65 72 65 64  op of registered
1320: 20 70 6f 72 74 73 20 69 73 20 34 39 31 35 32 20   ports is 49152 
1330: 62 75 74 20 6c 65 74 73 20 75 73 65 20 70 6f 72  but lets use por
1340: 74 73 20 69 6e 20 74 68 65 20 72 65 67 69 73 74  ts in the regist
1350: 65 72 65 64 20 72 61 6e 67 65 0a 09 09 09 20 28  ered range.... (
1360: 72 61 6e 64 6f 6d 20 28 2d 20 36 34 30 30 30 20  random (- 64000 
1370: 6c 6f 77 70 6f 72 74 29 29 29 29 29 29 0a 20 20  lowport)))))).  
1380: 20 20 28 68 61 6e 64 6c 65 2d 65 78 63 65 70 74    (handle-except
1390: 69 6f 6e 73 0a 20 20 20 20 20 65 78 6e 0a 20 20  ions.     exn.  
13a0: 20 20 20 28 62 65 67 69 6e 0a 20 20 20 20 20 20     (begin.      
13b0: 20 28 64 65 62 75 67 3a 70 72 69 6e 74 20 30 20   (debug:print 0 
13c0: 2a 64 65 66 61 75 6c 74 2d 6c 6f 67 2d 70 6f 72  *default-log-por
13d0: 74 2a 20 22 45 58 43 45 50 54 49 4f 4e 3a 20 70  t* "EXCEPTION: p
13e0: 6f 72 74 6c 6f 67 67 65 72 20 64 61 74 61 62 61  ortlogger databa
13f0: 73 65 20 70 72 6f 62 61 62 6c 79 20 6f 76 65 72  se probably over
1400: 6c 6f 61 64 65 64 20 6f 72 20 75 6e 72 65 61 64  loaded or unread
1410: 61 62 6c 65 2e 20 49 66 20 79 6f 75 20 73 65 65  able. If you see
1420: 20 74 68 69 73 20 6d 65 73 73 61 67 65 20 61 67   this message ag
1430: 61 69 6e 20 72 65 6d 6f 76 65 20 2f 74 6d 70 2f  ain remove /tmp/
1440: 2e 24 55 53 45 52 2d 70 6f 72 74 6c 6f 67 67 65  .$USER-portlogge
1450: 72 2e 64 62 22 29 0a 20 20 20 20 20 20 20 28 64  r.db").       (d
1460: 65 62 75 67 3a 70 72 69 6e 74 20 30 20 2a 64 65  ebug:print 0 *de
1470: 66 61 75 6c 74 2d 6c 6f 67 2d 70 6f 72 74 2a 20  fault-log-port* 
1480: 22 20 6d 65 73 73 61 67 65 3a 20 22 20 28 28 63  " message: " ((c
1490: 6f 6e 64 69 74 69 6f 6e 2d 70 72 6f 70 65 72 74  ondition-propert
14a0: 79 2d 61 63 63 65 73 73 6f 72 20 27 65 78 6e 20  y-accessor 'exn 
14b0: 27 6d 65 73 73 61 67 65 29 20 65 78 6e 29 29 0a  'message) exn)).
14c0: 20 20 20 20 20 20 20 28 64 65 62 75 67 3a 70 72         (debug:pr
14d0: 69 6e 74 20 30 20 2a 64 65 66 61 75 6c 74 2d 6c  int 0 *default-l
14e0: 6f 67 2d 70 6f 72 74 2a 20 22 65 78 6e 3d 22 20  og-port* "exn=" 
14f0: 28 63 6f 6e 64 69 74 69 6f 6e 2d 3e 6c 69 73 74  (condition->list
1500: 20 65 78 6e 29 29 0a 20 20 20 20 20 20 20 28 70   exn)).       (p
1510: 72 69 6e 74 2d 63 61 6c 6c 2d 63 68 61 69 6e 20  rint-call-chain 
1520: 28 63 75 72 72 65 6e 74 2d 65 72 72 6f 72 2d 70  (current-error-p
1530: 6f 72 74 29 29 0a 20 20 20 20 20 20 20 28 64 65  ort)).       (de
1540: 62 75 67 3a 70 72 69 6e 74 20 30 20 2a 64 65 66  bug:print 0 *def
1550: 61 75 6c 74 2d 6c 6f 67 2d 70 6f 72 74 2a 20 22  ault-log-port* "
1560: 43 6f 6e 74 69 6e 75 69 6e 67 20 61 6e 79 77 61  Continuing anywa
1570: 79 2e 22 29 29 0a 20 20 20 20 20 28 70 6f 72 74  y.")).     (port
1580: 6c 6f 67 67 65 72 3a 74 61 6b 65 2d 70 6f 72 74  logger:take-port
1590: 20 64 62 20 70 6f 72 74 6e 75 6d 29 29 0a 20 20   db portnum)).  
15a0: 20 20 70 6f 72 74 6e 75 6d 29 29 0a 0a 3b 3b 20    portnum))..;; 
15b0: 73 65 74 20 70 6f 72 74 20 74 6f 20 22 72 65 6c  set port to "rel
15c0: 65 61 73 65 64 22 2c 20 22 66 61 69 6c 65 64 22  eased", "failed"
15d0: 20 65 74 63 2e 0a 3b 3b 20 0a 28 64 65 66 69 6e   etc..;; .(defin
15e0: 65 20 28 70 6f 72 74 6c 6f 67 67 65 72 3a 73 65  e (portlogger:se
15f0: 74 2d 70 6f 72 74 20 64 62 20 70 6f 72 74 6e 75  t-port db portnu
1600: 6d 20 76 61 6c 75 65 29 0a 20 20 28 73 71 6c 69  m value).  (sqli
1610: 74 65 33 3a 65 78 65 63 75 74 65 20 64 62 20 22  te3:execute db "
1620: 55 50 44 41 54 45 20 70 6f 72 74 73 20 53 45 54  UPDATE ports SET
1630: 20 73 74 61 74 65 3d 3f 2c 75 70 64 61 74 65 5f   state=?,update_
1640: 74 69 6d 65 3d 73 74 72 66 74 69 6d 65 28 27 25  time=strftime('%
1650: 73 27 2c 27 6e 6f 77 27 29 20 57 48 45 52 45 20  s','now') WHERE 
1660: 70 6f 72 74 3d 3f 3b 22 20 76 61 6c 75 65 20 70  port=?;" value p
1670: 6f 72 74 6e 75 6d 29 29 0a 0a 3b 3b 20 73 65 74  ortnum))..;; set
1680: 20 70 6f 72 74 20 74 6f 20 66 61 69 6c 65 64 20   port to failed 
1690: 28 61 74 74 65 6d 70 74 65 64 20 74 6f 20 74 61  (attempted to ta
16a0: 6b 65 20 62 75 74 20 67 6f 74 20 65 72 72 6f 72  ke but got error
16b0: 29 0a 3b 3b 0a 28 64 65 66 69 6e 65 20 28 70 6f  ).;;.(define (po
16c0: 72 74 6c 6f 67 67 65 72 3a 73 65 74 2d 66 61 69  rtlogger:set-fai
16d0: 6c 65 64 20 64 62 20 70 6f 72 74 6e 75 6d 29 0a  led db portnum).
16e0: 20 20 28 73 71 6c 69 74 65 33 3a 65 78 65 63 75    (sqlite3:execu
16f0: 74 65 20 64 62 20 22 55 50 44 41 54 45 20 70 6f  te db "UPDATE po
1700: 72 74 73 20 53 45 54 20 73 74 61 74 65 3d 27 66  rts SET state='f
1710: 61 69 6c 65 64 27 2c 66 61 69 6c 5f 63 6f 75 6e  ailed',fail_coun
1720: 74 3d 66 61 69 6c 5f 63 6f 75 6e 74 2b 31 2c 75  t=fail_count+1,u
1730: 70 64 61 74 65 5f 74 69 6d 65 3d 73 74 72 66 74  pdate_time=strft
1740: 69 6d 65 28 27 25 73 27 2c 27 6e 6f 77 27 29 20  ime('%s','now') 
1750: 57 48 45 52 45 20 70 6f 72 74 3d 3f 3b 22 20 70  WHERE port=?;" p
1760: 6f 72 74 6e 75 6d 29 29 0a 0a 3b 3b 3d 3d 3d 3d  ortnum))..;;====
1770: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
1780: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
1790: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
17a0: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
17b0: 3d 3d 0a 3b 3b 20 4d 41 49 4e 0a 3b 3b 3d 3d 3d  ==.;; MAIN.;;===
17c0: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
17d0: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
17e0: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
17f0: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
1800: 3d 3d 3d 0a 0a 28 64 65 66 69 6e 65 20 28 70 6f  ===..(define (po
1810: 72 74 6c 6f 67 67 65 72 3a 6d 61 69 6e 20 2e 20  rtlogger:main . 
1820: 61 72 67 73 29 0a 20 20 28 6c 65 74 2a 20 28 28  args).  (let* ((
1830: 64 62 66 6e 61 6d 65 20 28 63 6f 6e 63 20 22 2f  dbfname (conc "/
1840: 74 6d 70 2f 2e 22 20 28 63 75 72 72 65 6e 74 2d  tmp/." (current-
1850: 75 73 65 72 2d 6e 61 6d 65 29 20 22 2d 70 6f 72  user-name) "-por
1860: 74 6c 6f 67 67 65 72 2e 64 62 22 29 29 0a 09 20  tlogger.db")).. 
1870: 28 64 62 20 20 20 20 20 20 28 70 6f 72 74 6c 6f  (db      (portlo
1880: 67 67 65 72 3a 6f 70 65 6e 2d 64 62 20 64 62 66  gger:open-db dbf
1890: 6e 61 6d 65 29 29 0a 09 20 28 6e 75 6d 61 72 67  name)).. (numarg
18a0: 73 20 28 6c 65 6e 67 74 68 20 61 72 67 73 29 29  s (length args))
18b0: 0a 09 20 28 72 65 73 75 6c 74 20 20 0a 09 20 20  .. (result  ..  
18c0: 28 68 61 6e 64 6c 65 2d 65 78 63 65 70 74 69 6f  (handle-exceptio
18d0: 6e 73 0a 09 20 20 20 65 78 6e 0a 09 20 20 20 28  ns..   exn..   (
18e0: 62 65 67 69 6e 0a 09 20 20 20 20 20 28 64 65 62  begin..     (deb
18f0: 75 67 3a 70 72 69 6e 74 20 30 20 2a 64 65 66 61  ug:print 0 *defa
1900: 75 6c 74 2d 6c 6f 67 2d 70 6f 72 74 2a 20 22 45  ult-log-port* "E
1910: 58 43 45 50 54 49 4f 4e 3a 20 70 6f 72 74 6c 6f  XCEPTION: portlo
1920: 67 67 65 72 20 64 61 74 61 62 61 73 65 20 61 74  gger database at
1930: 20 22 20 64 62 66 6e 61 6d 65 20 22 20 70 72 6f   " dbfname " pro
1940: 62 61 62 6c 79 20 6f 76 65 72 6c 6f 61 64 65 64  bably overloaded
1950: 20 6f 72 20 75 6e 72 65 61 64 61 62 6c 65 2e 20   or unreadable. 
1960: 54 72 79 20 72 65 6d 6f 76 69 6e 67 20 69 74 2e  Try removing it.
1970: 22 29 0a 09 20 20 20 20 20 28 64 65 62 75 67 3a  ")..     (debug:
1980: 70 72 69 6e 74 20 30 20 2a 64 65 66 61 75 6c 74  print 0 *default
1990: 2d 6c 6f 67 2d 70 6f 72 74 2a 20 22 20 6d 65 73  -log-port* " mes
19a0: 73 61 67 65 3a 20 22 20 28 28 63 6f 6e 64 69 74  sage: " ((condit
19b0: 69 6f 6e 2d 70 72 6f 70 65 72 74 79 2d 61 63 63  ion-property-acc
19c0: 65 73 73 6f 72 20 27 65 78 6e 20 27 6d 65 73 73  essor 'exn 'mess
19d0: 61 67 65 29 20 65 78 6e 29 29 0a 09 20 20 20 20  age) exn))..    
19e0: 20 28 70 72 69 6e 74 20 22 65 78 6e 3d 22 20 28   (print "exn=" (
19f0: 63 6f 6e 64 69 74 69 6f 6e 2d 3e 6c 69 73 74 20  condition->list 
1a00: 65 78 6e 29 29 0a 09 20 20 20 20 20 28 64 65 62  exn))..     (deb
1a10: 75 67 3a 70 72 69 6e 74 20 30 20 2a 64 65 66 61  ug:print 0 *defa
1a20: 75 6c 74 2d 6c 6f 67 2d 70 6f 72 74 2a 20 22 20  ult-log-port* " 
1a30: 73 74 61 74 75 73 3a 20 20 22 20 28 28 63 6f 6e  status:  " ((con
1a40: 64 69 74 69 6f 6e 2d 70 72 6f 70 65 72 74 79 2d  dition-property-
1a50: 61 63 63 65 73 73 6f 72 20 27 73 71 6c 69 74 65  accessor 'sqlite
1a60: 33 20 27 73 74 61 74 75 73 29 20 65 78 6e 29 29  3 'status) exn))
1a70: 0a 09 20 20 20 20 20 28 70 72 69 6e 74 2d 63 61  ..     (print-ca
1a80: 6c 6c 2d 63 68 61 69 6e 20 28 63 75 72 72 65 6e  ll-chain (curren
1a90: 74 2d 65 72 72 6f 72 2d 70 6f 72 74 29 29 0a 09  t-error-port))..
1aa0: 20 20 20 20 20 23 66 29 0a 09 20 20 20 28 63 61       #f)..   (ca
1ab0: 73 65 20 28 73 74 72 69 6e 67 2d 3e 73 79 6d 62  se (string->symb
1ac0: 6f 6c 20 28 63 61 72 20 61 72 67 73 29 29 20 3b  ol (car args)) ;
1ad0: 3b 20 63 6f 6d 6d 61 6e 64 73 20 77 69 74 68 20  ; commands with 
1ae0: 74 77 6f 20 6f 72 20 6d 6f 72 65 20 70 61 72 61  two or more para
1af0: 6d 73 0a 09 20 20 20 20 20 28 28 74 61 6b 65 29  ms..     ((take)
1b00: 28 70 6f 72 74 6c 6f 67 67 65 72 3a 74 61 6b 65  (portlogger:take
1b10: 2d 70 6f 72 74 20 64 62 20 28 73 74 72 69 6e 67  -port db (string
1b20: 2d 3e 6e 75 6d 62 65 72 20 28 63 61 64 72 20 61  ->number (cadr a
1b30: 72 67 73 29 29 29 29 0a 09 20 20 20 20 20 28 28  rgs))))..     ((
1b40: 66 69 6e 64 29 28 70 6f 72 74 6c 6f 67 67 65 72  find)(portlogger
1b50: 3a 66 69 6e 64 2d 70 6f 72 74 20 64 62 29 29 0a  :find-port db)).
1b60: 09 20 20 20 20 20 28 28 73 65 74 29 20 28 6c 65  .     ((set) (le
1b70: 74 20 28 28 70 6f 72 74 20 20 28 63 61 64 72 20  t ((port  (cadr 
1b80: 20 61 72 67 73 29 29 0a 09 09 09 20 20 28 73 74   args))....  (st
1b90: 61 74 65 20 28 63 61 64 64 72 20 61 72 67 73 29  ate (caddr args)
1ba0: 29 29 0a 09 09 20 20 20 20 20 20 28 70 6f 72 74  ))...      (port
1bb0: 6c 6f 67 67 65 72 3a 73 65 74 2d 70 6f 72 74 20  logger:set-port 
1bc0: 64 62 20 0a 09 09 09 09 09 20 20 20 28 69 66 20  db ......   (if 
1bd0: 28 6e 75 6d 62 65 72 3f 20 70 6f 72 74 29 20 70  (number? port) p
1be0: 6f 72 74 20 28 73 74 72 69 6e 67 2d 3e 6e 75 6d  ort (string->num
1bf0: 62 65 72 20 70 6f 72 74 29 29 0a 09 09 09 09 09  ber port))......
1c00: 20 20 20 73 74 61 74 65 29 0a 09 09 20 20 20 20     state)...    
1c10: 20 20 73 74 61 74 65 29 29 0a 09 20 20 20 20 20    state))..     
1c20: 28 28 66 61 69 6c 65 64 29 28 70 6f 72 74 6c 6f  ((failed)(portlo
1c30: 67 67 65 72 3a 73 65 74 2d 66 61 69 6c 65 64 20  gger:set-failed 
1c40: 64 62 20 28 73 74 72 69 6e 67 2d 3e 6e 75 6d 62  db (string->numb
1c50: 65 72 20 28 63 61 64 72 20 61 72 67 73 29 29 29  er (cadr args)))
1c60: 20 27 66 61 69 6c 65 64 29 29 29 29 29 0a 20 20   'failed))))).  
1c70: 20 20 28 73 71 6c 69 74 65 33 3a 66 69 6e 61 6c    (sqlite3:final
1c80: 69 7a 65 21 20 64 62 29 0a 20 20 20 20 72 65 73  ize! db).    res
1c90: 75 6c 74 29 29 0a 20 20 20 20 20 0a 3b 3b 20 28  ult)).     .;; (
1ca0: 70 72 69 6e 74 20 28 61 70 70 6c 79 20 70 6f 72  print (apply por
1cb0: 74 6c 6f 67 67 65 72 3a 6d 61 69 6e 20 28 63 64  tlogger:main (cd
1cc0: 72 20 28 61 72 67 76 29 29 29 29 0a              r (argv)))).