summaryrefslogtreecommitdiff
path: root/guile/make-header.scm
blob: ec51216b4bfbdabfe795b29ab56ae26c22d5c838 (plain)
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
(define cpu-type '(("name" . "cpu")
		   ("label" . "CPU Usage")
		   ("fields" . (("unsigned long" ("total" "Total CPU Usage")
						 ("user") ("nice") ("sys") ("idle"))
				("const char" ("name"))
				)
			     )
		   )
  )

;; computes constant for struct field (eg. GLIBTOP_CPU_TOTAL)

(define field-name-constant
  (lambda (name field)
    (string "GLIBTOP_"
	    (string-upcase! (string name))
	    "_"
	    (string-upcase! (string field)))
    )
  )

;; computes structure name (eg. glibtop_cpu)

(define make-struct-name
  (lambda (type)
    (string "glibtop_" (assoc-ref type "name"))
    )
  )

(define tab-pad-string
  (lambda (string tabs)
    (string-append string (make-string (- (* tabs 8) (string-length string)) #\space))
    )
  )

(define make-field-list
  (lambda (name type fields)
    (let* ((output (string)) (pos 1))
      (map
       (lambda (x)
	 (let* ((sep   (if (= pos (length fields)) ";" ","))
		(start (if (= pos 1)
			   (string "\t"
				   (tab-pad-string (string type) 2))
			   (string "\t\t"))
		       )
		(comment (string (if (= pos 1) "" "\t") "/* "
				 (tab-pad-string (field-name-constant name (car x)) 3)
				 " */"))
		(field (tab-pad-string (string-append (string (car x)) sep) 2))
		)
	   (set! pos (+ pos 1))
	   (string-append start field comment "\n")
	   )
	 )
       fields)
      )
    )
  )

(define make-struct-body
  (lambda (type)
    (let* ((name   (assoc-ref type "name"))
	   (data   (assoc-ref type "fields"))
	   (output (string))
	   )
      (for-each
       (lambda (y)
	 (for-each
	  (lambda (z)
	    (set! output (string-append output z))
	    )
	  y)
	 )
       (map
	(lambda (x) (make-field-list name (car x) (cdr x)))
	data)
       )
      output)
    )
  )

(define make-struct
  (lambda (type)
    (let* ((name   (assoc-ref type "name"))
	   (data   (assoc-ref type "fields"))
	   (output (string-append
		    (tab-pad-string
		     (string "typedef struct _glibtop_" name)
		     5)
		    (string "glibtop_" name ";\n\n"
			    "struct glibtop_" name "\n{\n\t"
			    "unsigned long\tflags;\n")
		    )
		   )
	   )
      (string-append output (make-struct-body type) "};\n")
      )
    )
  )

(define make-field-name-list
  (lambda (type)
    (let* ((name   (assoc-ref type "name"))
	   (data   (assoc-ref type "fields"))
	   (return (list))
	   )
      (map
       (lambda (x)
	 (map
	  (lambda (y) (set! return (append return (list (car y)))))
	  (cdr x)
	  )
	 )
       data)
      return)
    )
  )

(define make-field-constants
  (lambda (type)
    (let* ((name   (assoc-ref type "name"))
	   (data   (make-field-name-list type))
	   (output (string
		    (tab-pad-string
		     (string "#define GLIBTOP_MAX_"
			     (string-upcase! (string name))
			     ) 5)
		    (number->string (length data))
		    "\n\n"
		    )
		   )
	   (pos 0)
	   )
      (for-each
       (lambda (x)
	 (set! output (string-append output (string
					     (tab-pad-string
					      (string "#define GLIBTOP_"
						      (string-upcase! (string name))
						      "_"
						      (string-upcase! (string x))
						      ) 5)
					     (number->string pos)
					     "\n"
					     )
				     )
	       )
	 (set! pos (+ pos 1))
	 )
       data)
      output)
    )
  )

(define make-extern-defs
  (lambda (type)
    (let* ((name   (assoc-ref type "name"))
	   )
      (string
       (tab-pad-string (string "extern void glibtop_get_" name) 6)
       "__P(glibtop *, glibtop_" name " *);\n\n"
       "#ifdef HAVE_GUILE\n\n"
       "/* You need to link with -lgtop_guile to get this stuff here. */\n\n"
       (tab-pad-string (string "extern SCM glibtop_get_" name) 6)
       "__P(void);\n\n"
       "#endif /* HAVE_GUILE */\n\n"
       "#ifdef GLIBTOP_GUILE_NAMES\n\n"
       "/* You need to link with -lgtop_guile_names to get this stuff here. */\n\n"
       (tab-pad-string (string "extern SCM glibtop_guile_names_" name) 6)
       "__P(void);\n"
       (tab-pad-string (string "extern SCM glibtop_guile_labels_" name) 6)
       "__P(void);\n"
       (tab-pad-string (string "extern SCM glibtop_guile_descriptions_" name) 6)
       "__P(void);\n\n"
       "#endif /* GLIBTOP_GUILE_NAMES */\n\n"
       "#ifdef GLIBTOP_NAMES\n\n"
       "/* You need to link with -lgtop_names to get this stuff here. */\n\n"
       (tab-pad-string (string "extern const char *glibtop_names_" name) 6)
       "[];\n"
       (tab-pad-string (string "extern const char *glibtop_labels_" name) 6)
       "[];\n"
       (tab-pad-string (string "extern const char *glibtop_descriptions_" name) 6)
       "[];\n\n"
       "#endif /* GLIBTOP_NAMES */\n\n")
      )
    )
  )

(begin
  (display (make-field-constants cpu-type)) (newline)
  (display (make-struct cpu-type)) (newline)
  (display (make-extern-defs cpu-type)) (newline)
  )