1erl_eterm(3) C Library Functions erl_eterm(3)
2
3
4
6 erl_eterm - Functions for Erlang term construction.
7
9 Note:
10 The support for VxWorks is deprecated as of OTP 22, and will be removed
11 in OTP 23.
12
13
14 Note:
15 The old legacy erl_interface library (functions with prefix erl_) is
16 deprecated as of OTP 22, and will be removed in OTP 23. This does not
17 apply to the ei library. Reasonably new gcc compilers will issue depre‐
18 cation warnings. In order to disable these warnings, define the macro
19 EI_NO_DEPR_WARN.
20
21
22 This module provides functions for creating and manipulating Erlang
23 terms.
24
25 An Erlang term is represented by a C structure of type ETERM. Applica‐
26 tions should not reference any fields in this structure directly, as it
27 can be changed in future releases to provide faster and more compact
28 term storage. Instead, applications should use the macros and functions
29 provided.
30
31 Each of the following macros takes a single ETERM pointer as an argu‐
32 ment. The macros return a non-zero value if the test is true, otherwise
33 0.
34
35 ERL_IS_INTEGER(t):
36 True if t is an integer.
37
38 ERL_IS_UNSIGNED_INTEGER(t):
39 True if t is an integer.
40
41 ERL_IS_FLOAT(t):
42 True if t is a floating point number.
43
44 ERL_IS_ATOM(t):
45 True if t is an atom.
46
47 ERL_IS_PID(t):
48 True if t is a pid (process identifier).
49
50 ERL_IS_PORT(t):
51 True if t is a port.
52
53 ERL_IS_REF(t):
54 True if t is a reference.
55
56 ERL_IS_TUPLE(t):
57 True if t is a tuple.
58
59 ERL_IS_BINARY(t):
60 True if t is a binary.
61
62 ERL_IS_LIST(t):
63 True if t is a list with zero or more elements.
64
65 ERL_IS_EMPTY_LIST(t):
66 True if t is an empty list.
67
68 ERL_IS_CONS(t):
69 True if t is a list with at least one element.
70
71 The following macros can be used for retrieving parts of Erlang terms.
72 None of these do any type checking. Results are undefined if you pass
73 an ETERM* containing the wrong type. For example, passing a tuple to
74 ERL_ATOM_PTR() likely results in garbage.
75
76 char *ERL_ATOM_PTR(t):
77
78
79 char *ERL_ATOM_PTR_UTF8(t):
80 A string representing atom t.
81
82 int ERL_ATOM_SIZE(t):
83
84
85 int ERL_ATOM_SIZE_UTF8(t):
86 The length (in bytes) of atom t.
87
88 void *ERL_BIN_PTR(t):
89 A pointer to the contents of t.
90
91 int ERL_BIN_SIZE(t):
92 The length (in bytes) of binary object t.
93
94 int ERL_INT_VALUE(t):
95 The integer of t.
96
97 unsigned int ERL_INT_UVALUE(t):
98 The unsigned integer value of t.
99
100 double ERL_FLOAT_VALUE(t):
101 The floating point value of t.
102
103 ETERM *ERL_PID_NODE(t):
104
105
106 ETERM *ERL_PID_NODE_UTF8(t):
107 The node in pid t.
108
109 int ERL_PID_NUMBER(t):
110 The sequence number in pid t.
111
112 int ERL_PID_SERIAL(t):
113 The serial number in pid t.
114
115 int ERL_PID_CREATION(t):
116 The creation number in pid t.
117
118 int ERL_PORT_NUMBER(t):
119 The sequence number in port t.
120
121 int ERL_PORT_CREATION(t):
122 The creation number in port t.
123
124 ETERM *ERL_PORT_NODE(t):
125
126
127 ETERM *ERL_PORT_NODE_UTF8(t):
128 The node in port t.
129
130 int ERL_REF_NUMBER(t):
131 The first part of the reference number in ref t. Use only for com‐
132 patibility.
133
134 int ERL_REF_NUMBERS(t):
135 Pointer to the array of reference numbers in ref t.
136
137 int ERL_REF_LEN(t):
138 The number of used reference numbers in ref t.
139
140 int ERL_REF_CREATION(t):
141 The creation number in ref t.
142
143 int ERL_TUPLE_SIZE(t):
144 The number of elements in tuple t.
145
146 ETERM *ERL_CONS_HEAD(t):
147 The head element of list t.
148
149 ETERM *ERL_CONS_TAIL(t):
150 A list representing the tail elements of list t.
151
153 ETERM *erl_cons(head, tail)
154
155 Types:
156
157 ETERM *head;
158 ETERM *tail;
159
160 Concatenates two Erlang terms, prepending head onto tail and
161 thereby creating a cons cell. To make a proper list, tail is
162 always to be a list or an empty list. Notice that NULL is not a
163 valid list.
164
165 * head is the new term to be added.
166
167 * tail is the existing list to which head is concatenated.
168
169 The function returns a new list.
170
171 ERL_CONS_HEAD(list) and ERL_CONS_TAIL(list) can be used to
172 retrieve the head and tail components from the list.
173 erl_hd(list) and erl_tl(list) do the same thing, but check that
174 the argument really is a list.
175
176 Example:
177
178 ETERM *list,*anAtom,*anInt;
179 anAtom = erl_mk_atom("madonna");
180 anInt = erl_mk_int(21);
181 list = erl_mk_empty_list();
182 list = erl_cons(anAtom, list);
183 list = erl_cons(anInt, list);
184 ... /* do some work */
185 erl_free_compound(list);
186
187
188 ETERM *erl_copy_term(term)
189
190 Types:
191
192 ETERM *term;
193
194 Creates and returns a copy of the Erlang term term.
195
196 ETERM *erl_element(position, tuple)
197
198 Types:
199
200 int position;
201 ETERM *tuple;
202
203 Extracts a specified element from an Erlang tuple.
204
205 * position specifies which element to retrieve from tuple. The
206 elements are numbered starting from 1.
207
208 * tuple is an Erlang term containing at least position ele‐
209 ments.
210
211 Returns a new Erlang term corresponding to the requested ele‐
212 ment, or NULL if position was greater than the arity of tuple.
213
214 ETERM *erl_hd(list)
215
216 Types:
217
218 ETERM *list;
219
220 Extracts the first element from a list.
221
222 list is an Erlang term containing a list.
223
224 Returns an Erlang term corresponding to the head head element in
225 the list, or a NULL pointer if list was not a list.
226
227 void erl_init(NULL, 0)
228
229 Types:
230
231 void *NULL;
232 int 0;
233
234 This function must be called before any of the others in the
235 Erl_Interface library to initialize the library functions. The
236 arguments must be specified as erl_init(NULL,0).
237
238 int erl_iolist_length(list)
239
240 Types:
241
242 ETERM *list;
243
244 Returns the length of an I/O list.
245
246 list is an Erlang term containing an I/O list.
247
248 Returns the length of list, or -1 if list is not an I/O list.
249
250 For the definition of an I/O list, see erl_iolist_to_binary.
251
252 ETERM *erl_iolist_to_binary(term)
253
254 Types:
255
256 ETERM *list;
257
258 Converts an I/O list to a binary term.
259
260 list is an Erlang term containing a list.
261
262 Returns an Erlang binary term, or NULL if list was not an I/O
263 list.
264
265 Informally, an I/O list is a deep list of characters and bina‐
266 ries that can be sent to an Erlang port. In BNF, an I/O list is
267 formally defined as follows:
268
269 iolist ::= []
270 | Binary
271 | [iohead | iolist]
272 ;
273 iohead ::= Binary
274 | Byte (integer in the range [0..255])
275 | iolist
276 ;
277
278
279 char *erl_iolist_to_string(list)
280
281 Types:
282
283 ETERM *list;
284
285 Converts an I/O list to a NULL-terminated C string.
286
287 list is an Erlang term containing an I/O list. The I/O list must
288 not contain the integer 0, as C strings may not contain this
289 value except as a terminating marker.
290
291 Returns a pointer to a dynamically allocated buffer containing a
292 string. If list is not an I/O list, or if list contains the
293 integer 0, NULL is returned. It is the caller's responsibility
294 to free the allocated buffer with erl_free().
295
296 For the definition of an I/O list, see erl_iolist_to_binary.
297
298 int erl_length(list)
299
300 Types:
301
302 ETERM *list;
303
304 Determines the length of a proper list.
305
306 list is an Erlang term containing a proper list. In a proper
307 list, all tails except the last point to another list cell, and
308 the last tail points to an empty list.
309
310 Returns -1 if list is not a proper list.
311
312 ETERM *erl_mk_atom(string)
313
314 Types:
315
316 const char *string;
317
318 Creates an atom.
319
320 string is the sequence of characters that will be used to create
321 the atom.
322
323 Returns an Erlang term containing an atom. Notice that it is the
324 caller's responsibility to ensure that string contains a valid
325 name for an atom.
326
327 ERL_ATOM_PTR(atom) and ERL_ATOM_PTR_UTF8(atom) can be used to
328 retrieve the atom name (as a NULL-terminated string).
329 ERL_ATOM_SIZE(atom) and ERL_ATOM_SIZE_UTF8(atom) return the
330 length of the atom name.
331
332 Note:
333 The UTF-8 variants were introduced in Erlang/OTP R16 and the
334 string returned by ERL_ATOM_PTR(atom) was not NULL-terminated on
335 older releases.
336
337
338 ETERM *erl_mk_binary(bptr, size)
339
340 Types:
341
342 char *bptr;
343 int size;
344
345 Produces an Erlang binary object from a buffer containing a
346 sequence of bytes.
347
348 * bptr is a pointer to a buffer containing data to be con‐
349 verted.
350
351 * size indicates the length of bptr.
352
353 Returns an Erlang binary object.
354
355 ERL_BIN_PTR(bin) retrieves a pointer to the binary data.
356 ERL_BIN_SIZE(bin) retrieves the size.
357
358 ETERM *erl_mk_empty_list()
359
360 Creates and returns an empty Erlang list. Notice that NULL is
361 not used to represent an empty list; Use this function instead.
362
363 ETERM *erl_mk_estring(string, len)
364
365 Types:
366
367 char *string;
368 int len;
369
370 Creates a list from a sequence of bytes.
371
372 * string is a buffer containing a sequence of bytes. The buf‐
373 fer does not need to be NULL-terminated.
374
375 * len is the length of string.
376
377 Returns an Erlang list object corresponding to the character
378 sequence in string.
379
380 ETERM *erl_mk_float(f)
381
382 Types:
383
384 double f;
385
386 Creates an Erlang float.
387
388 f is a value to be converted to an Erlang float.
389
390 Returns an Erlang float object with the value specified in f or
391 NULL if f is not finite.
392
393 ERL_FLOAT_VALUE(t) can be used to retrieve the value from an
394 Erlang float.
395
396 ETERM *erl_mk_int(n)
397
398 Types:
399
400 int n;
401
402 Creates an Erlang integer.
403
404 n is a value to be converted to an Erlang integer.
405
406 Returns an Erlang integer object with the value specified in n.
407
408 ERL_INT_VALUE(t) can be used to retrieve the value from an
409 Erlang integer.
410
411 ETERM *erl_mk_list(array, arrsize)
412
413 Types:
414
415 ETERM **array;
416 int arrsize;
417
418 Creates an Erlang list from an array of Erlang terms, such that
419 each element in the list corresponds to one element in the
420 array.
421
422 * array is an array of Erlang terms.
423
424 * arrsize is the number of elements in array.
425
426 The function creates an Erlang list object, whose length arrsize
427 and whose elements are taken from the terms in array.
428
429 ETERM *erl_mk_long_ref(node, n1, n2, n3, creation)
430
431 Types:
432
433 const char *node;
434 unsigned int n1, n2, n3;
435 unsigned int creation;
436
437 Creates an Erlang reference, with 82 bits.
438
439 * node is the name of the C-node.
440
441 * n1, n2, and n3 can be seen as one big number
442 n1*2^64+n2*2^32+n3, which is to be chosen uniquely for each
443 reference created for a given C-node.
444
445 * creation is an arbitrary number.
446
447 Notice that n3 and creation are limited in precision, so only
448 the low 18 and 2 bits of these numbers are used.
449
450 Returns an Erlang reference object.
451
452 ERL_REF_NODE(ref), ERL_REF_NUMBERS(ref), ERL_REF_LEN(ref), and
453 ERL_REF_CREATION(ref) can be used to retrieve the values used to
454 create the reference.
455
456 ETERM *erl_mk_pid(node, number, serial, creation)
457
458 Types:
459
460 const char *node;
461 unsigned int number;
462 unsigned int serial;
463 unsigned int creation;
464
465 Creates an Erlang process identifier (pid). The resulting pid
466 can be used by Erlang processes wishing to communicate with the
467 C-node.
468
469 * node is the name of the C-node.
470
471 * number, serial, and creation are arbitrary numbers. Notice
472 that these are limited in precision, so only the low 15, 3,
473 and 2 bits of these numbers are used.
474
475 Returns an Erlang pid object.
476
477 ERL_PID_NODE(pid), ERL_PID_NUMBER(pid), ERL_PID_SERIAL(pid), and
478 ERL_PID_CREATION(pid) can be used to retrieve the four values
479 used to create the pid.
480
481 ETERM *erl_mk_port(node, number, creation)
482
483 Types:
484
485 const char *node;
486 unsigned int number;
487 unsigned int creation;
488
489 Creates an Erlang port identifier.
490
491 * node is the name of the C-node.
492
493 * number and creation are arbitrary numbers. Notice that these
494 are limited in precision, so only the low 18 and 2 bits of
495 these numbers are used.
496
497 Returns an Erlang port object.
498
499 ERL_PORT_NODE(port), ERL_PORT_NUMBER(port), and ERL_PORT_CRE‐
500 ATION can be used to retrieve the three values used to create
501 the port.
502
503 ETERM *erl_mk_ref(node, number, creation)
504
505 Types:
506
507 const char *node;
508 unsigned int number;
509 unsigned int creation;
510
511 Creates an old Erlang reference, with only 18 bits - use
512 erl_mk_long_ref instead.
513
514 * node is the name of the C-node.
515
516 * number is to be chosen uniquely for each reference created
517 for a given C-node.
518
519 * creation is an arbitrary number.
520
521 Notice that number and creation are limited in precision, so
522 only the low 18 and 2 bits of these numbers are used.
523
524 Returns an Erlang reference object.
525
526 ERL_REF_NODE(ref), ERL_REF_NUMBER(ref), and ERL_REF_CRE‐
527 ATION(ref) can be used to retrieve the three values used to cre‐
528 ate the reference.
529
530 ETERM *erl_mk_string(string)
531
532 Types:
533
534 char *string;
535
536 Creates a list from a NULL-terminated string.
537
538 string is a NULL-terminated sequence of characters (that is, a C
539 string) from which the list will be created.
540
541 Returns an Erlang list.
542
543 ETERM *erl_mk_tuple(array, arrsize)
544
545 Types:
546
547 ETERM **array;
548 int arrsize;
549
550 Creates an Erlang tuple from an array of Erlang terms.
551
552 * array is an array of Erlang terms.
553
554 * arrsize is the number of elements in array.
555
556 The function creates an Erlang tuple, whose arity is size and
557 whose elements are taken from the terms in array.
558
559 To retrieve the size of a tuple, either use function erl_size
560 (which checks the type of the checked term and works for a
561 binary as well as for a tuple) or ERL_TUPLE_SIZE(tuple) returns
562 the arity of a tuple. erl_size() does the same thing, but it
563 checks that the argument is a tuple. erl_element(index,tuple)
564 returns the element corresponding to a given position in the
565 tuple.
566
567 ETERM *erl_mk_uint(n)
568
569 Types:
570
571 unsigned int n;
572
573 Creates an Erlang unsigned integer.
574
575 n is a value to be converted to an Erlang unsigned integer.
576
577 Returns an Erlang unsigned integer object with the value speci‐
578 fied in n.
579
580 ERL_INT_UVALUE(t) can be used to retrieve the value from an
581 Erlang unsigned integer.
582
583 ETERM *erl_mk_var(name)
584
585 Types:
586
587 char *name;
588
589 Creates an unbound Erlang variable. The variable can later be
590 bound through pattern matching or assignment.
591
592 name specifies a name for the variable.
593
594 Returns an Erlang variable object with the name name.
595
596 int erl_print_term(stream, term)
597
598 Types:
599
600 FILE *stream;
601 ETERM *term;
602
603 Prints the specified Erlang term to the specified output stream.
604
605 * stream indicates where the function is to send its output.
606
607 * term is the Erlang term to print.
608
609 Returns the number of characters written on success, otherwise a
610 negative value.
611
612 void erl_set_compat_rel(release_number)
613
614 Types:
615
616 unsigned release_number;
617
618 By default, the Erl_Interface library is only guaranteed to be
619 compatible with other Erlang/OTP components from the same
620 release as the Erl_Interface library itself. For example,
621 Erl_Interface from Erlang/OTP R10 is not compatible with an
622 Erlang emulator from Erlang/OTP R9 by default.
623
624 A call to erl_set_compat_rel(release_number) sets the Erl_Inter‐
625 face library in compatibility mode of release release_number.
626 Valid range of release_number is [7, current release]. This
627 makes it possible to communicate with Erlang/OTP components from
628 earlier releases.
629
630 Note:
631 If this function is called, it may only be called once directly
632 after the call to function erl_init().
633
634
635 Warning:
636 You may run into trouble if this feature is used carelessly.
637 Always ensure that all communicating components are either from
638 the same Erlang/OTP release, or from release X and release Y
639 where all components from release Y are in compatibility mode of
640 release X.
641
642
643 int erl_size(term)
644
645 Types:
646
647 ETERM *term;
648
649 Returns either the arity of an Erlang tuple or the number of
650 bytes in an Erlang binary object.
651
652 term is an Erlang tuple or an Erlang binary object.
653
654 Returns the size of term as described above, or -1 if term is
655 not one of the two supported types.
656
657 ETERM *erl_tl(list)
658
659 Types:
660
661 ETERM *list;
662
663 Extracts the tail from a list.
664
665 list is an Erlang term containing a list.
666
667 Returns an Erlang list corresponding to the original list minus
668 the first element, or NULL pointer if list was not a list.
669
670 ETERM *erl_var_content(term, name)
671
672 Types:
673
674 ETERM *term;
675 char *name;
676
677 Returns the contents of the specified variable in an Erlang
678 term.
679
680 * term is an Erlang term. In order for this function to suc‐
681 ceed, term must either be an Erlang variable with the speci‐
682 fied name, or it must be an Erlang list or tuple containing
683 a variable with the specified name. Other Erlang types can‐
684 not contain variables.
685
686 * name is the name of an Erlang variable.
687
688 Returns the Erlang object corresponding to the value of name in
689 term. If no variable with the name name is found in term, or if
690 term is not a valid Erlang term, NULL is returned.
691
692
693
694Ericsson AB erl_interface 3.13.2 erl_eterm(3)