Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 204 lines (174 sloc) 6.443 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
;;;; utils.scm - Utilities for scripting and file stuff
;
; Copyright (c) 2008-2011, The Chicken Team
; Copyright (c) 2000-2007, Felix L. Winkelmann
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
; conditions are met:
;
; Redistributions of source code must retain the above copyright notice, this list of conditions and the following
; disclaimer.
; Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
; disclaimer in the documentation and/or other materials provided with the distribution.
; Neither the name of the author nor the names of its contributors may be used to endorse or promote
; products derived from this software without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
; AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
; POSSIBILITY OF SUCH DAMAGE.


(declare
  (unit utils)
  (uses extras srfi-13 posix files irregex)
  (fixnum)
  (hide chop-pds)
  (disable-interrupts) )

(include "common-declarations.scm")

(register-feature! 'utils)


;;; Like `system', but allows format-string and bombs on nonzero return code:

(define system*
  (lambda (fstr . args)
    (let* ([str (apply sprintf fstr args)]
[n (system str)] )
      (unless (zero? n)
(##sys#error "shell invocation failed with non-zero return status" str n) ) ) ) )


;;; Read file as string from given filename or port:

(define (read-all . file)
  (let ([file (optional file ##sys#standard-input)])
    (if (port? file)
(read-string #f file)
(with-input-from-file file (cut read-string #f)) ) ) )


;;; Quote string for shell

(define (qs str #!optional (platform (build-platform)))
  (case platform
    ((mingw32)
     (string-append "\"" str "\""))
    (else
     (if (zero? (string-length str))
"''"
(string-concatenate
(map (lambda (c)
(if (or (char-whitespace? c)
(memq c '(#\# #\" #\' #\` # #\~ #\& #\% #\$ #\! #\* #\;
#\< #\> #\\ #\( #\) #\[ #\] #\{ #\} #\?)))
(string #\\ c)
(string c)))
(string->list str)))))))


;;; Compile and load file

(define compile-file-options (make-parameter '("-S" "-O2" "-d2")))

(define compile-file
  (let ((csc (foreign-value "C_CSC_PROGRAM" c-string))
(load-file load)
(path (foreign-value "C_INSTALL_BIN_HOME" c-string)) )
    (lambda (filename #!key (options '()) output-file (load #t) verbose)
      (let* ((cscpath (or (file-exists? (make-pathname path csc)) "csc"))
(tmpfile (and (not output-file) (create-temporary-file "so")))
(crapshell (eq? (build-platform) 'mingw32))
(cmd (sprintf "~a~a -s ~a ~a -o ~a~a"
(if crapshell "\"" "")
(qs cscpath)
(string-intersperse (append (compile-file-options) options) " ")
(qs filename)
(qs (or output-file tmpfile))
(if crapshell "\"" ""))))
(print "; compiling " filename " ...")
(when verbose (print " " cmd))
(let ((status (system cmd)))
(cond ((zero? status)
(unless output-file
(on-exit
(lambda ()
(handle-exceptions ex #f (delete-file* tmpfile)))))
(when load
(let ((f (or output-file tmpfile)))
(handle-exceptions ex
(begin
(delete-file* f)
(abort ex))
(load-file f)
f))))
(else #f)))))))


;;; Scan lines until regex or predicate matches

(define scan-input-lines
  (lambda (rx #!optional (port ##sys#standard-input))
    (let ((rx (if (procedure? rx)
rx
(cute irregex-search (irregex rx) <>))))
      (let loop ()
(let ((ln (read-line port)))
(and (not (eof-object? ln))
(or (rx ln)
(loop))))))))


;; Ask for confirmation

#>
#if defined(_WIN32) && !defined(__CYGWIN__)
# include <windows.h>
# define C_HAS_MESSAGE_BOX 1
static int
C_confirmation_dialog(char *msg, char *caption, int def, int abort)
{
  int d = 0, r;
  int t = abort ? MB_YESNOCANCEL : MB_YESNO;

  switch(def) {
  case 0: d = MB_DEFBUTTON1; break;
  case 1: d = MB_DEFBUTTON2; break;
  case 2: d = MB_DEFBUTTON3;
  }

  r = MessageBox(NULL, msg, caption, t | MB_ICONQUESTION | d);

  switch(r) {
  case IDYES: return 1;
  case IDNO: return 0;
  default: return -1;
  }
}
#else
# define C_HAS_MESSAGE_BOX 0
static int
C_confirmation_dialog(char *msg, char *caption, int def, int abort) { return -1; }
#endif
<#

;; Note: for Mac OS X, "CFUserNotificationDisplayAlert" could be used,
;; unless that requires linking any libraries. This would also
;; be useful for runtime error messages.

(define-foreign-variable C_HAS_MESSAGE_BOX bool)

(define yes-or-no?
  (let ((dialog (foreign-lambda int "C_confirmation_dialog" c-string c-string int bool)))
    (lambda (str #!key default title (abort reset))
      (let ((gui (and C_HAS_MESSAGE_BOX (not (##sys#fudge 4))))) ; C_gui_mode
(define (get-input)
(if gui
(let ((r (dialog
str
(or title "CHICKEN Runtime")
(cond ((not default) 3)
((string-ci=? default "yes") 0)
((string-ci=? default "no") 1)
(else 2))
abort)))
(case r
((0) "no")
((1) "yes")
(else "abort")))
(string-trim-both (read-line))))
(let loop ()
(unless gui
(printf "~%~A (yes/no~a) " str (if abort "/abort" ""))
(when default (printf "[~A] " default))
(flush-output))
(let ((ln (get-input)))
(cond ((eof-object? ln) (set! ln "abort"))
((and default (string=? "" ln)) (set! ln default)) )
(cond ((string-ci=? "yes" ln) #t)
((string-ci=? "no" ln) #f)
((and abort (string-ci=? "abort" ln)) (abort))
(else
(if abort
(printf "~%Please enter \"yes\", \"no\" or \"abort\".~%")
(printf "~%Please enter \"yes\" or \"no\".~%"))
(loop) ) ) ) ) ) ) ) )
  
Something went wrong with that request. Please try again.