Imported Upstream version 3.2.2
[debian/gnuradio] / pmt / src / scheme / gnuradio / gen-serial-tags.scm
1 #!/usr/bin/guile \
2 -e main -s
3 !#
4 ;;; -*-scheme-*-
5 ;;;
6 ;;; Copyright 2007 Free Software Foundation, Inc.
7 ;;; 
8 ;;; This file is part of GNU Radio
9 ;;; 
10 ;;; GNU Radio is free software; you can redistribute it and/or modify
11 ;;; it under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3, or (at your option)
13 ;;; any later version.
14 ;;; 
15 ;;; GNU Radio is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;;; GNU General Public License for more details.
19 ;;; 
20 ;;; You should have received a copy of the GNU General Public License along
21 ;;; with this program; if not, write to the Free Software Foundation, Inc.,
22 ;;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 ;;;
24
25 (use-modules (ice-9 format))
26
27 (defmacro when (pred . body)
28   `(if ,pred (begin ,@body) #f))
29
30 ;; ----------------------------------------------------------------
31
32 (define (main args)
33
34   (define (usage)
35     (format 0
36             "usage: ~a <pmt-serial-tags.scm> <pmt_serial_tags.h>~%"
37             (car args)))
38
39   (when (not (= (length args) 3))
40         (usage)
41         (format 0 "args: ~s~%" args)
42         (exit 1))
43       
44   (let ((i-file (open-input-file (cadr args)))
45         (h-file (open-output-file (caddr args))))
46
47       (write-header-comment h-file "// ")
48       (display "#ifndef INCLUDED_PMT_SERIAL_TAGS_H\n" h-file)
49       (display "#define INCLUDED_PMT_SERIAL_TAGS_H\n" h-file)
50       (newline h-file)
51       (display "enum pst_tags {\n" h-file)
52
53       (for-each-in-file i-file
54        (lambda (form)
55          (let* ((name (cadr form))
56                 (c-name (string-upcase (c-ify name)))
57                 (value (caddr form)))
58            ;;(format h-file   "static const int ~a\t= 0x~x;~%" c-name value)
59            (format h-file   "  ~a\t= 0x~x,~%" c-name value))))
60
61       (display "};\n" h-file)
62       (display "#endif\n" h-file)))
63
64 (define (c-ify name)
65   (list->string (map (lambda (c)
66                        (if (eqv? c #\-) #\_ c))
67                      (string->list (symbol->string name)))))
68
69
70 (define (write-header-comment o-port prefix)
71   (for-each (lambda (comment)
72               (format o-port "~a~a~%" prefix comment))
73             header-comment))
74
75 (define header-comment
76   '(
77     ""
78     "Copyright 2007 Free Software Foundation, Inc."
79     ""
80     "This file is part of GNU Radio"
81     ""
82     "GNU Radio is free software; you can redistribute it and/or modify"
83     "it under the terms of the GNU General Public License as published by"
84     "the Free Software Foundation; either version 3, or (at your option)"
85     "any later version."
86     ""
87     "GNU Radio is distributed in the hope that it will be useful,"
88     "but WITHOUT ANY WARRANTY; without even the implied warranty of"
89     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
90     "GNU General Public License for more details."
91     ""
92     "You should have received a copy of the GNU General Public License along"
93     "with this program; if not, write to the Free Software Foundation, Inc.,"
94     "51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA."
95     ""
96     ""
97     "THIS FILE IS MACHINE GENERATED FROM pmt-serial-tags.scm. DO NOT EDIT BY HAND."
98     "See pmt-serial-tags.scm for additional commentary."
99     ""))
100
101
102
103 (define (for-each-in-file file f)
104   (let ((port (if (port? file)
105                   file
106                   (open-input-file file))))
107     (letrec
108      ((loop
109        (lambda (port form)
110          (cond ((eof-object? form)
111                 (when (not (eq? port file))
112                       (close-input-port port))
113                 #t)
114                (else
115                 (f form)
116                 (set! form #f)          ; for GC
117                 (loop port (read port)))))))
118      (loop port (read port)))))