#!/bin/sh
exec rep "$0" "$@"
!#

;; build-dispatch

;; $Id: build-dispatch,v 1.12 2004/07/16 19:09:24 jharper Exp $
;; $XFree86: xc/lib/GL/apple/build-dispatch,v 1.2 2003/10/13 00:30:53 torrey Exp $

;; Copyright (c) 2002 Apple Computer, Inc. All rights reserved.

;; Permission is hereby granted, free of charge, to any person
;; obtaining a copy of this software and associated documentation files
;; (the "Software"), to deal in the Software without restriction,
;; including without limitation the rights to use, copy, modify, merge,
;; publish, distribute, sublicense, and/or sell copies of the Software,
;; and to permit persons to whom the Software is furnished to do so,
;; subject to the following conditions:

;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.

;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT.  IN NO EVENT SHALL THE THE ABOVE LISTED COPYRIGHT
;; HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;; DEALINGS IN THE SOFTWARE.

;; Except as contained in this notice, the name(s) of the above
;; copyright holders shall not be used in advertising or otherwise to
;; promote the sale, use or other dealings in this Software without
;; prior written authorization.

;; This lisp script is used to build the C dispatch table from the
;; .defs file. It currently uses my (jsh's) rep interpreter. The
;; created file is checked into cvs, so it only needs to be run when
;; the .defs file is touched

(structure ()

    (open rep
	  rep.regexp)

  (defmacro @ args `(format standard-output ,@args))

  (define stubs '())

  (define (strip-args s)
    (mapcar (lambda (x)
	      (and (string-match "[ \t\n*]+([a-zA-Z0-9_]+)$" x)
		   (expand-last-match "\\1")))
	    (string-split "," s)))

  (@ "/* dri_dispatch.h -- built automatically, DO NOT EDIT\n   $%s$\n   $%s$ */\n\n" "Id" "XFree86: ")

  (condition-case nil
      (while t
	(let* ((form (read standard-input))
	       (args (unless (string= (nth 3 form) "void")
		       (strip-args (nth 3 form))))
	       (internal (>= (nth 2 form) 0))
	       (alias (nth 4 form))
	       (prefix (if (eq (nth 0 form) 'void)
			   "_VOID \("
			 (format nil " \(%s, " (nth 0 form)))))
	  (when (or internal (not alias))
	    (@ "DEFUN_%s%s%s,\n    (%s),\n    (%s)\)\n\n"
	       (if internal "LOCAL" "EXTERN")
	       prefix
	       (nth 1 form)
	       (if (string= (nth 3 form) "void")
		   (if internal "void *rend" "")
		 (if internal (format nil "void *rend, %s" (nth 3 form)) (nth 3 form)))
	       (mapconcat identity args ", ")))
	  (when alias
	    (@ "DEFUN_ALIAS%s%s, %s,\n    (%s),\n    (%s)\)\n\n"
	       prefix (nth 1 form) alias (nth 3 form)
	       (mapconcat identity args ", ")))
	  (when internal
	    (setq stubs (cons form stubs)))))

    (end-of-stream))

  (setq stubs (nreverse stubs))

  (@ "#define INDIRECT_DISPATCH_INIT(d,p) \\\n")
  (@ "do { \\\n")

  (setq stubs (sort stubs (lambda (a b)
			    (< (nth 2 a) (nth 2 b)))))

  (let loop ((rest stubs))
   (when rest
     (let ((form (car rest)))
       (@ "    (d)[%s] = (void *) &p ## %s; \\\n" (nth 2 form) (nth 1 form)))
     (loop (cdr rest))))

  (@ "} while (0)\n"))
