1 ;;; -*- Mode: Emacs-Lisp -*-
3 ;;; Emacs Tooltalk Utility Functions
5 ;;; @(#)tooltalk-util.el 1.7 93/12/07
8 (defun initialize-tooltalk-message-arg (msg n mode value vtype)
9 "Initialize the Nth tooltalk message argument of MSG.
10 A new argument is created if necessary. No attempt to distinguish
11 between strings that contain binary data and ordinary strings is made;
12 all non integer argument values are converted to a string (if not a
13 string already) and loaded with tt_message_arg_val_set().
14 Applications that need to put binary data into a ToolTalk message
15 argument should initialize the argument with:
17 (set-tooltalk-message-attribute bin-string msg 'arg_bval arg-n)"
19 (- (1+ n) (get-tooltalk-message-attribute msg 'args_count))))
20 (while (> n-args-needed 0)
21 (add-tooltalk-message-arg msg mode vtype)
22 (setq n-args-needed (1- n-args-needed))))
26 (set-tooltalk-message-attribute value msg 'arg_ival n))
28 (set-tooltalk-message-attribute value msg 'arg_val n))
30 (error "The value specified for msg %s argument %d, %s, must be a string or an integer"
33 (prin1-to-string value)))))
37 (defconst tooltalk-arg-mode-ids
38 (list 'TT_IN 'TT_OUT 'TT_INOUT TT_IN TT_OUT TT_INOUT))
40 (defun initialize-tooltalk-message/pattern-args (initfn msg args)
41 "Apply INITFN to each the position mode value and type of
42 each argument in the list. The value of INITFN should be either
43 'initialize-tooltalk-message-arg or 'initialize-tooltalk-pattern-arg.
44 See `make-tooltalk-message' for a description of how arguments are specified.
45 We distinguish the short form for arguments, e.g. \"just-a-value\",
46 from the long form by checking to see if the argument is a list whose
47 car is one of the ToolTalk mode values like TT_INOUT."
50 (let* ((arg (car args))
53 (member (car arg) tooltalk-arg-mode-ids)))
55 (if long-form (car arg) TT_IN))
59 ((cdr arg) (car (cdr arg)))
65 (stringp (car (cdr (cdr arg)))))
66 (car (cdr (cdr arg))))
67 ((integerp value) "int")
69 (funcall initfn msg n mode value type))
70 (setq args (cdr args))
74 (defun initialize-tooltalk-message-attributes (msg attributes)
75 "Initialize the tooltalk message attributes. The value of
76 attributes must be a property list in the same form as for
77 make-tooltalk-message. This function can be used to reset
78 an existing message or to initialize a new one. See
79 initialize-tooltalk-message-args for a description of how
80 arguments are initialized."
81 (let ((args attributes)
82 (initfn 'initialize-tooltalk-message-arg))
83 (while (and args (cdr args))
84 (let ((indicator (car args))
85 (value (car (cdr args))))
86 (if (eq indicator 'args)
87 (initialize-tooltalk-message/pattern-args initfn msg value)
88 (set-tooltalk-message-attribute value msg indicator)))
89 (setq args (cdr (cdr args))))))
92 (defun make-tooltalk-message (attributes &optional no-callback)
93 "Create a tooltalk message and initialize its attributes.
94 The value of attributes must be a list of alternating keyword/values,
95 where keywords are symbols that name valid message attributes.
98 (make-tooltalk-message
103 args (\"arg1\" 12345 (TT_INOUT \"arg3\" \"string\"))))
105 Values must always be strings, integers, or symbols that
106 represent Tooltalk constants. Attribute names are the same as
107 those supported by set-tooltalk-message-attribute, plus 'args.
109 The value of args should be a list of message arguments where
110 each message argument has the following form:
112 (mode [value [type]]) or just value
114 Where mode is one of TT_IN, TT_OUT, TT_INOUT and type is a string.
115 If type isn't specified then \"int\" is used if the value is a
116 number otherwise \"string\" is used. If only a value is specified
117 then mode defaults to TT_IN. If mode is TT_OUT then value and
118 type don't need to be specified. You can find out more about the
119 semantics and uses of ToolTalk message arguments in chapter 4 of the
120 Tooltalk Programmer's Guide.
122 The no-callback arg is a hack to prevent the registration of the
123 C-level callback. This hack is needed by the current SPARCworks
124 tool startup mechanism. Yucko."
125 (let ((msg (create-tooltalk-message no-callback)))
126 (initialize-tooltalk-message-attributes msg attributes)
130 (defun describe-tooltalk-message (msg &optional stream)
131 "Print tooltalk message MSG's attributes and arguments to STREAM.
132 This is often useful for debugging."
156 (princ (car attrs) stream)
158 (prin1 (get-tooltalk-message-attribute msg (car attrs)) stream)
160 (setq attrs (cdr attrs))))
162 (let ((n (get-tooltalk-message-attribute msg 'args_count))
165 (princ "Argument " stream)
168 (let ((type (get-tooltalk-message-attribute msg 'arg_type i)))
172 (get-tooltalk-message-attribute msg 'arg_mode i)
173 (if (equal type "int")
174 (get-tooltalk-message-attribute msg 'arg_ival i)
175 (get-tooltalk-message-attribute msg 'arg_val i))
182 (defun initialize-tooltalk-pattern-arg (pat n mode value vtype)
183 "Add one argument to tooltalk pattern PAT.
184 No support for specifying pattern arguments whose value is a vector
185 of binary data is provided."
186 (let ((converted-value
187 (if (or (integerp value) (stringp value))
189 (prin1-to-string value))))
190 (add-tooltalk-pattern-arg pat mode vtype converted-value)))
193 (defun initialize-tooltalk-pattern-attributes (pat attributes)
194 "Initialize tooltalk pattern PAT's attributes.
195 ATTRIBUTES must be a property list in the same form as for
196 `make-tooltalk-pattern'. The value of each attribute (except 'category)
197 can either be a single value or a list of values. If a list of
198 values is provided then the pattern will match messages with
199 a corresponding attribute that matches any member of the list.
201 This function can be used to add attribute values to an existing
202 pattern or to initialize a new one. See
203 `initialize-tooltalk-message/pattern-args' for a description of how
204 arguments are initialized."
205 (let ((args attributes)
206 (initfn 'initialize-tooltalk-pattern-arg))
207 (while (and args (cdr args))
208 (let ((indicator (car args))
209 (value (car (cdr args))))
211 ((eq indicator 'args)
212 (initialize-tooltalk-message/pattern-args initfn pat value))
213 ((eq indicator 'plist)
214 (let ((values value))
216 (let ((prop (car values))
217 (propval (car (cdr values))))
218 (tooltalk-pattern-prop-set pat prop propval))
219 (setq values (cdr (cdr values))))))
221 (let ((values value))
223 (add-tooltalk-pattern-attribute (car values) pat indicator)
224 (setq values (cdr values)))))
226 (add-tooltalk-pattern-attribute value pat indicator))))
227 (setq args (cdr (cdr args))))))
231 (defun make-tooltalk-pattern (attributes)
232 "Create a tooltalk pattern and initialize its attributes.
233 The value of attributes must be a list of alternating keyword/values,
234 where keywords are symbols that name valid pattern attributes
235 or lists of valid attributes. For example:
237 (make-tooltalk-pattern
238 '(category TT_OBSERVE
240 op (\"operation1\" \"operation2\")
241 args (\"arg1\" 12345 (TT_INOUT \"arg3\" \"string\"))))
244 Values must always be strings, integers, or symbols that
245 represent Tooltalk constants or lists of same. When a list
246 of values is provided all of the list elements are added to
247 the attribute. In the example above, messages whose op
248 attribute is \"operation1\" or \"operation2\" would match the pattern.
250 The value of args should be a list of pattern arguments where
251 each pattern argument has the following form:
253 (mode [value [type]]) or just value
255 Where mode is one of TT_IN, TT_OUT, TT_INOUT and type is a string.
256 If type isn't specified then \"int\" is used if the value is a
257 number otherwise \"string\" is used. If only a value is specified
258 then mode defaults to TT_IN. If mode is TT_OUT then value and type
259 don't need to be specified. You can find out more about the semantics
260 and uses of ToolTalk pattern arguments in chapter 3 of the Tooltalk
263 (let ((pat (create-tooltalk-pattern)))
264 (initialize-tooltalk-pattern-attributes pat attributes)