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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
|
/* nih-dbus-tool
*
* node.c - top-level object parsing and handling
*
* Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
* Copyright © 2009 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <expat.h>
#include <string.h>
#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/list.h>
#include <nih/string.h>
#include <nih/logging.h>
#include <nih/error.h>
#include "symbol.h"
#include "indent.h"
#include "type.h"
#include "node.h"
#include "interface.h"
#include "parse.h"
#include "errors.h"
/**
* node_path_valid:
* @path: Object path to verify.
*
* Verifies whether @path matches the specification for D-Bus object paths.
*
* Returns: TRUE if valid, FALSE if not.
**/
int
node_path_valid (const char *path)
{
nih_assert (path != NULL);
/* Path must begin with a '/' character */
if (path[0] != '/')
return FALSE;
/* We can get away with just using strlen() here even through path
* is in UTF-8 because all the valid characters are ASCII.
*/
for (size_t i = 1; i < strlen (path); i++) {
/* Path components may be separated by single '/' characters,
* multiple ones are not allowed.
*/
if (path[i] == '/') {
if (path[i-1] == '/')
return FALSE;
continue;
}
/* Valid characters are [A-Za-z0-9_] */
if ( ((path[i] < 'A') || (path[i] > 'Z'))
&& ((path[i] < 'a') || (path[i] > 'z'))
&& ((path[i] < '0') || (path[i] > '9'))
&& (path[i] != '_'))
return FALSE;
}
/* Final character may not be '/' unless it's the root object. */
if ((strlen (path) > 1) && (path[strlen (path) - 1] == '/'))
return FALSE;
return TRUE;
}
/**
* node_new:
* @parent: parent object for new node,
* @path: D-Bus path of node.
*
* Allocates a new D-Bus object Node data structure, with the path optionally
* set to @path.
*
* If @parent is not NULL, it should be a pointer to another object which
* will be used as a parent for the returned node. When all parents
* of the returned node are freed, the returned node will also be
* freed.
*
* Returns: the new node or NULL if the allocation failed.
**/
Node *
node_new (const void *parent,
const char *path)
{
Node *node;
node = nih_new (parent, Node);
if (! node)
return NULL;
if (path) {
node->path = nih_strdup (node, path);
if (! node->path) {
nih_free (node);
return NULL;
}
} else {
node->path = NULL;
}
nih_list_init (&node->interfaces);
return node;
}
/**
* node_start_tag:
* @xmlp: XML parser,
* @tag: name of XML tag being parsed,
* @attr: NULL-terminated array of attribute name and value pairs.
*
* This function is called by parse_start_tag() for a "node" start
* tag, the top-level of the introspection data and defining a D-Bus
* object.
*
* If the node does not appear at the top-level a warning is emitted
* (unless directly inside another node tag) and the tag will be ignored.
*
* Nodes may have a "name" attribute containing the D-Bus object path
* of the node.
*
* Any unknown attributes result in a warning and will be ignored.
*
* A Node object will be allocated and pushed onto the stack, this is not
* saved into the context until the end tag is found.
*
* Returns: zero on success, negative value on raised error.
**/
int
node_start_tag (XML_Parser xmlp,
const char * tag,
char * const *attr)
{
ParseContext * context;
ParseStack * parent;
nih_local Node *node = NULL;
char * const * key;
char * const * value;
const char * name = NULL;
nih_assert (xmlp != NULL);
nih_assert (tag != NULL);
nih_assert (attr != NULL);
context = XML_GetUserData (xmlp);
nih_assert (context != NULL);
/* Nodes should only appear at the top-level, unless they're within
* another node in which case we just ignore them.
*/
parent = parse_stack_top (&context->stack);
if (parent) {
if (parent->type != PARSE_NODE) {
nih_warn ("%s:%zu:%zu: %s", context->filename,
(size_t)XML_GetCurrentLineNumber (xmlp),
(size_t)XML_GetCurrentColumnNumber (xmlp),
_("Ignored unexpected <node> tag"));
}
if (! parse_stack_push (NULL, &context->stack,
PARSE_IGNORED, NULL))
nih_return_system_error (-1);
return 0;
}
/* Retrieve the name from the attributes */
for (key = attr; key && *key; key += 2) {
value = key + 1;
nih_assert (value && *value);
if (! strcmp (*key, "name")) {
name = *value;
} else {
nih_warn ("%s:%zu:%zu: %s: %s", context->filename,
(size_t)XML_GetCurrentLineNumber (xmlp),
(size_t)XML_GetCurrentColumnNumber (xmlp),
_("Ignored unknown <node> attribute"),
*key);
}
}
/* If we have a name check that it's valid */
if (name && (! node_path_valid (name)))
nih_return_error (-1, NODE_INVALID_PATH,
_(NODE_INVALID_PATH_STR));
/* Allocate a Node object and push onto the stack */
node = node_new (NULL, name);
if (! node)
nih_return_system_error (-1);
if (! parse_stack_push (NULL, &context->stack, PARSE_NODE, node)) {
nih_error_raise_system ();
return -1;
}
return 0;
}
/**
* node_end_tag:
* @xmlp: XML parser,
* @tag: name of XML tag being parsed.
*
* This function is called by parse_end_tag() for a "node" end
* tag, and matches a call to node_start_tag() made at the same parsing
* level.
*
* The node is set in the context so it can be returned once the parser
* completes.
*
* Returns: zero on success, negative value on raised error.
**/
int
node_end_tag (XML_Parser xmlp,
const char *tag)
{
ParseContext *context;
ParseStack * entry;
nih_assert (xmlp != NULL);
nih_assert (tag != NULL);
context = XML_GetUserData (xmlp);
nih_assert (context != NULL);
entry = parse_stack_top (&context->stack);
nih_assert (entry != NULL);
nih_assert (entry->type == PARSE_NODE);
nih_debug ("Set parsed node to %s", entry->node->path ?: "(unknown)");
nih_assert (context->node == NULL);
context->node = entry->node;
nih_ref (entry->node, context->parent);
nih_unref (entry->node, entry);
nih_free (entry);
return 0;
}
/**
* node_lookup_interface:
* @node: node to search,
* @symbol: interface symbol to find.
*
* Finds an interface in @node's interfaces list which has the generated
* or supplied C symbol @symbol. If @symbol is NULL, the default interface
* will be returned.
*
* Returns: interface found or NULL if no interface matches.
**/
Interface *
node_lookup_interface (Node * node,
const char *symbol)
{
nih_assert (node != NULL);
NIH_LIST_FOREACH (&node->interfaces, iter) {
Interface *interface = (Interface *)iter;
if ((interface->symbol && symbol
&& (! strcmp (interface->symbol, symbol)))
|| ((! interface->symbol) && (! symbol)))
return interface;
}
return NULL;
}
/**
* node_interfaces_array:
* @parent: parent object for new string,
* @prefix: prefix for array name,
* @node: node to generate array for,
* @object: whether array is for an object or proxy,
* @prototypes: list to append prototypes to.
*
* Generates C code to declare an array of NihDBusInterface pointers for
* the node @node, the code includes each of the NihDBusInterface structure
* definitions individually as well as the array definitions for methods,
* signals, properties and their arguments in them.
*
* If @object is TRUE, the array will be for an object definition so method
* handler function and property getter and setter function pointers will
* be filled in. If @object is FALSE, the array will be for a proxy
* definition so the signal filter function pointers will be filled in.
*
* The prototype of the returned variable declaration, and the prototypes
* of the interface structures, are returned as TypeVar objects appended
* to the @prototypes list.
*
* If @parent is not NULL, it should be a pointer to another object which
* will be used as a parent for the returned string. When all parents
* of the returned string are freed, the return string will also be
* freed.
*
* Returns: newly allocated string or NULL if insufficient memory.
**/
char *
node_interfaces_array (const void *parent,
const char *prefix,
Node * node,
int object,
NihList * prototypes)
{
nih_local char *name = NULL;
nih_local char *block = NULL;
char * code = NULL;
TypeVar * var;
nih_assert (prefix != NULL);
nih_assert (node != NULL);
nih_assert (prototypes != NULL);
name = symbol_extern (NULL, prefix, NULL, NULL, "interfaces", NULL);
if (! name)
return NULL;
/* Append the address of each of the interface structures to the
* block we build, and the code to the interfaces list.
*/
NIH_LIST_FOREACH (&node->interfaces, iter) {
Interface * interface = (Interface *)iter;
NihList struct_prototypes;
nih_local char *struct_code = NULL;
nih_list_init (&struct_prototypes);
struct_code = interface_struct (NULL, prefix, interface,
object, &struct_prototypes);
if (! struct_code) {
if (code)
nih_free (code);
return NULL;
}
nih_assert (! NIH_LIST_EMPTY (&struct_prototypes));
var = (TypeVar *)struct_prototypes.next;
if (! nih_strcat_sprintf (&code, parent,
"%s\n", struct_code)) {
if (code)
nih_free (code);
return NULL;
}
if (! nih_strcat_sprintf (&block, NULL,
"&%s,\n", var->name)) {
if (code)
nih_free (code);
return NULL;
}
/* Copy the prototypes to the list we return, since we
* want to export those as well.
*/
NIH_LIST_FOREACH_SAFE (&struct_prototypes, iter) {
var = (TypeVar *)iter;
if (! type_to_extern (&var->type, var)) {
if (code)
nih_free (code);
return NULL;
}
nih_ref (var, code);
nih_list_add (prototypes, &var->entry);
}
}
/* Append the final element to the block of elements, indent and
* surround with the structure definition.
*/
if (! nih_strcat (&block, NULL, "NULL\n")) {
if (code)
nih_free (code);
return NULL;
}
if (! indent (&block, NULL, 1)) {
if (code)
nih_free (code);
return NULL;
}
if (! nih_strcat_sprintf (&code, parent,
"const NihDBusInterface *%s[] = {\n"
"%s"
"};\n",
name,
block)) {
if (code)
nih_free (code);
return NULL;
}
/* Append the prototype to the list */
var = type_var_new (code, "const NihDBusInterface *", name);
if (! var) {
nih_free (code);
return NULL;
}
var->array = TRUE;
if (! type_to_extern (&var->type, var)) {
if (code)
nih_free (code);
return NULL;
}
nih_list_add (prototypes, &var->entry);
return code;
}
/**
* node_object_functions:
* @parent: parent object for new string,
* @prefix: prefix for function names,
* @node: node to generate functions for,
* @prototypes: list to append prototypes to,
* @handlers: list to append handler prototypes to,
* @structs: list to append structure definitions to,
* @externs: list to append prototypes of extern functions to.
*
* Generates C code for all of the functions that @node would require to
* wrap existing C functions and implement the D-Bus interfaces described
* for the object.
*
* Functions in the returned code to implement method handlers and property
* getter and setters will be declared static and their prototypes returned
* as TypeFunc objects appended to the @prototypes list. You normally ensure
* that these receive a forward declaration.
*
* Those functions will call implementation functions that other code is
* expected to provide, the names and prototypes of these expected functions
* are returned as TypeFunc objects appended to the @handlers list. You
* must implement these elsewhere, and ensure that the prototype has a
* forward declaration.
*
* Functions in the returned code to implement signal emissions are part of
* a public API that your own code may call. The names and prototypes are
* returned as TypeFunc objects appended to the @externs list, you would
* normally place these in a header file.
*
* If any of the function arguments require a structure to be defined, the
* definition is returned as a TypeStruct object appended to the @structs
* list. The name is generated from @prefix, @interface and the method,
* signal or property the function is for.
*
* If @parent is not NULL, it should be a pointer to another object which
* will be used as a parent for the returned string. When all parents
* of the returned string are freed, the return string will also be
* freed.
*
* Returns: newly allocated string or NULL if insufficient memory.
**/
char *
node_object_functions (const void *parent,
const char *prefix,
Node * node,
NihList * prototypes,
NihList * handlers,
NihList * structs,
NihList * externs)
{
char *code = NULL;
int first = TRUE;
nih_assert (prefix != NULL);
nih_assert (node != NULL);
nih_assert (prototypes != NULL);
nih_assert (handlers != NULL);
nih_assert (structs != NULL);
nih_assert (externs != NULL);
code = nih_strdup (parent, "");
if (! code)
return NULL;
NIH_LIST_FOREACH (&node->interfaces, interface_iter) {
Interface *interface = (Interface *)interface_iter;
NIH_LIST_FOREACH (&interface->methods, method_iter) {
Method * method = (Method *)method_iter;
NihList method_prototypes;
NihList method_handlers;
NihList method_structs;
NihList method_externs;
nih_local char *object_func = NULL;
nih_local char *reply_func = NULL;
nih_list_init (&method_prototypes);
nih_list_init (&method_handlers);
nih_list_init (&method_structs);
nih_list_init (&method_externs);
if (! first)
if (! nih_strcat (&code, parent, "\n\n"))
goto error;
first = FALSE;
object_func = method_object_function (
NULL, prefix, interface, method,
&method_prototypes, &method_handlers,
&method_structs);
if (! object_func)
goto error;
if (! nih_strcat_sprintf (&code, parent,
"static %s",
object_func))
goto error;
if (method->async) {
reply_func = method_reply_function (
NULL, prefix, interface, method,
&method_externs, &method_structs);
if (! reply_func)
goto error;
if (! nih_strcat_sprintf (&code, parent,
"\n"
"%s",
reply_func))
goto error;
}
NIH_LIST_FOREACH_SAFE (&method_prototypes, iter) {
TypeFunc *func = (TypeFunc *)iter;
if (! type_to_static (&func->type, func))
goto error;
nih_ref (func, code);
nih_list_add (prototypes, &func->entry);
}
NIH_LIST_FOREACH_SAFE (&method_handlers, iter) {
TypeFunc *func = (TypeFunc *)iter;
if (! type_to_extern (&func->type, func))
goto error;
nih_ref (func, code);
nih_list_add (handlers, &func->entry);
}
NIH_LIST_FOREACH_SAFE (&method_structs, iter) {
TypeStruct *structure = (TypeStruct *)iter;
nih_ref (structure, code);
nih_list_add (structs, &structure->entry);
}
NIH_LIST_FOREACH_SAFE (&method_externs, iter) {
TypeFunc *func = (TypeFunc *)iter;
nih_ref (func, code);
nih_list_add (externs, &func->entry);
}
}
NIH_LIST_FOREACH (&interface->signals, signal_iter) {
Signal * signal = (Signal *)signal_iter;
NihList signal_structs;
NihList signal_externs;
nih_local char *object_func = NULL;
nih_list_init (&signal_structs);
nih_list_init (&signal_externs);
object_func = signal_object_function (
NULL, prefix, interface, signal,
&signal_externs, &signal_structs);
if (! object_func)
goto error;
if (! first)
if (! nih_strcat (&code, parent, "\n\n"))
goto error;
first = FALSE;
if (! nih_strcat (&code, parent, object_func))
goto error;
NIH_LIST_FOREACH_SAFE (&signal_structs, iter) {
TypeStruct *structure = (TypeStruct *)iter;
nih_ref (structure, code);
nih_list_add (structs, &structure->entry);
}
NIH_LIST_FOREACH_SAFE (&signal_externs, iter) {
TypeFunc *func = (TypeFunc *)iter;
nih_ref (func, code);
nih_list_add (externs, &func->entry);
}
}
NIH_LIST_FOREACH (&interface->properties, property_iter) {
Property * property = (Property *)property_iter;
NihList property_prototypes;
NihList property_handlers;
NihList property_structs;
nih_local char *get_func = NULL;
nih_local char *set_func = NULL;
nih_list_init (&property_prototypes);
nih_list_init (&property_handlers);
nih_list_init (&property_structs);
if (! first)
if (! nih_strcat (&code, parent, "\n\n"))
goto error;
first = FALSE;
if (property->access != NIH_DBUS_WRITE) {
get_func = property_object_get_function (
NULL, prefix, interface, property,
&property_prototypes, &property_handlers,
&property_structs);
if (! get_func)
goto error;
if (! nih_strcat_sprintf (&code, parent,
"static %s",
get_func))
goto error;
}
if (property->access == NIH_DBUS_READWRITE) {
if (! nih_strcat (&code, parent, "\n"))
goto error;
/* Don't duplicate structures; these will
* get freed automatically.
*/
nih_list_init (&property_structs);
}
if (property->access != NIH_DBUS_READ) {
set_func = property_object_set_function (
NULL, prefix, interface, property,
&property_prototypes, &property_handlers,
&property_structs);
if (! set_func)
goto error;
if (! nih_strcat_sprintf (&code, parent,
"static %s",
set_func))
goto error;
}
NIH_LIST_FOREACH_SAFE (&property_prototypes, iter) {
TypeFunc *func = (TypeFunc *)iter;
if (! type_to_static (&func->type, func))
goto error;
nih_ref (func, code);
nih_list_add (prototypes, &func->entry);
}
NIH_LIST_FOREACH_SAFE (&property_handlers, iter) {
TypeFunc *func = (TypeFunc *)iter;
if (! type_to_extern (&func->type, func))
goto error;
nih_ref (func, code);
nih_list_add (handlers, &func->entry);
}
NIH_LIST_FOREACH_SAFE (&property_structs, iter) {
TypeStruct *structure = (TypeStruct *)iter;
nih_ref (structure, code);
nih_list_add (structs, &structure->entry);
}
}
}
return code;
error:
nih_free (code);
return NULL;
}
/**
* node_proxy_functions:
* @parent: parent object for new string,
* @prefix: prefix for function names,
* @node: node to generate functions for,
* @prototypes: list to append prototypes to,
* @structs: list to append structure definitions to,
* @typedefs: list to append callback typedefs to,
* @externs: list to append prototypes of extern functions to.
*
* Generates C code for all of the functions that @node would require to
* provide remote object access.
*
* Functions in the returned code to implement signal filter functions
* will be declared static and their prototypes returned as TypeFunc objects
* appended to the @prototypes list. You normally ensure that these receive
* a forward declaration.
*
* Functions in the returned code to implement method and property get/set
* proxy functions are part of a public API that your own code may call.
* The names and prototypes are returned as TypeFunc objects appended to
* the @externs list, you would normally place these in a header file.
*
* Both sets of these functions will call handler and callback functions
* that other code is expected to provide, either passed directly to the
* function (for method and proxy functions) or passed to
* nih_dbus_proxy_connect() (for signal functions). The typedef for those
* functions are returned as TypeFunc objects appended to the @typedefs list.
* You would normally place these in a header file.
*
* If any of the function arguments require a structure to be defined, the
* definition is returned as a TypeStruct object appended to the @structs
* list. The name is generated from @prefix, @interface and the method,
* signal or property the function is for.
*
* If @parent is not NULL, it should be a pointer to another object which
* will be used as a parent for the returned string. When all parents
* of the returned string are freed, the return string will also be
* freed.
*
* Returns: newly allocated string or NULL if insufficient memory.
**/
char *
node_proxy_functions (const void *parent,
const char *prefix,
Node * node,
NihList * prototypes,
NihList * structs,
NihList * typedefs,
NihList * externs)
{
char *code = NULL;
int first = TRUE;
nih_assert (prefix != NULL);
nih_assert (node != NULL);
nih_assert (prototypes != NULL);
nih_assert (structs != NULL);
nih_assert (typedefs != NULL);
nih_assert (externs != NULL);
code = nih_strdup (parent, "");
if (! code)
return NULL;
NIH_LIST_FOREACH (&node->interfaces, interface_iter) {
Interface *interface = (Interface *)interface_iter;
NihList discard;
nih_list_init (&discard);
NIH_LIST_FOREACH (&interface->methods, method_iter) {
Method * method = (Method *)method_iter;
NihList method_prototypes;
NihList method_structs;
NihList method_typedefs;
NihList method_externs;
nih_local char *proxy_func = NULL;
nih_local char *notify_func = NULL;
nih_local char *sync_func = NULL;
nih_list_init (&method_prototypes);
nih_list_init (&method_structs);
nih_list_init (&method_typedefs);
nih_list_init (&method_externs);
if (! first)
if (! nih_strcat (&code, parent, "\n\n"))
goto error;
first = FALSE;
proxy_func = method_proxy_function (
NULL, prefix, interface, method,
&method_externs, &discard);
if (! proxy_func)
goto error;
notify_func = method_proxy_notify_function (
NULL, prefix, interface, method,
&method_prototypes, &method_typedefs,
&discard);
if (! notify_func)
goto error;
sync_func = method_proxy_sync_function (
NULL, prefix, interface, method,
&method_externs, &method_structs);
if (! sync_func)
goto error;
if (! nih_strcat_sprintf (&code, parent,
"%s"
"\n"
"static %s"
"\n"
"%s",
proxy_func,
notify_func,
sync_func))
goto error;
NIH_LIST_FOREACH_SAFE (&method_prototypes, iter) {
TypeFunc *func = (TypeFunc *)iter;
if (! type_to_static (&func->type, func))
goto error;
nih_ref (func, code);
nih_list_add (prototypes, &func->entry);
}
NIH_LIST_FOREACH_SAFE (&method_structs, iter) {
TypeStruct *structure = (TypeStruct *)iter;
nih_ref (structure, code);
nih_list_add (structs, &structure->entry);
}
NIH_LIST_FOREACH_SAFE (&method_typedefs, iter) {
TypeFunc *func = (TypeFunc *)iter;
nih_ref (func, code);
nih_list_add (typedefs, &func->entry);
}
NIH_LIST_FOREACH_SAFE (&method_externs, iter) {
TypeFunc *func = (TypeFunc *)iter;
nih_ref (func, code);
nih_list_add (externs, &func->entry);
}
}
NIH_LIST_FOREACH (&interface->signals, signal_iter) {
Signal * signal = (Signal *)signal_iter;
NihList signal_prototypes;
NihList signal_structs;
NihList signal_typedefs;
nih_local char *proxy_func = NULL;
nih_list_init (&signal_prototypes);
nih_list_init (&signal_structs);
nih_list_init (&signal_typedefs);
if (! first)
if (! nih_strcat (&code, parent, "\n\n"))
goto error;
first = FALSE;
proxy_func = signal_proxy_function (
NULL, prefix, interface, signal,
&signal_prototypes, &signal_typedefs,
&signal_structs);
if (! proxy_func)
goto error;
if (! nih_strcat_sprintf (&code, parent,
"static %s",
proxy_func))
goto error;
NIH_LIST_FOREACH_SAFE (&signal_prototypes, iter) {
TypeFunc *func = (TypeFunc *)iter;
if (! type_to_static (&func->type, func))
goto error;
nih_ref (func, code);
nih_list_add (prototypes, &func->entry);
}
NIH_LIST_FOREACH_SAFE (&signal_structs, iter) {
TypeStruct *structure = (TypeStruct *)iter;
nih_ref (structure, code);
nih_list_add (structs, &structure->entry);
}
NIH_LIST_FOREACH_SAFE (&signal_typedefs, iter) {
TypeFunc *func = (TypeFunc *)iter;
nih_ref (func, code);
nih_list_add (typedefs, &func->entry);
}
}
NIH_LIST_FOREACH (&interface->properties, property_iter) {
Property * property = (Property *)property_iter;
NihList property_prototypes;
NihList property_structs;
NihList property_typedefs;
NihList property_externs;
nih_local char *get_func = NULL;
nih_local char *get_notify_func = NULL;
nih_local char *get_sync_func = NULL;
nih_local char *set_func = NULL;
nih_local char *set_notify_func = NULL;
nih_local char *set_sync_func = NULL;
nih_list_init (&property_prototypes);
nih_list_init (&property_structs);
nih_list_init (&property_typedefs);
nih_list_init (&property_externs);
if (! first)
if (! nih_strcat (&code, parent, "\n\n"))
goto error;
first = FALSE;
if (property->access != NIH_DBUS_WRITE) {
get_func = property_proxy_get_function (
NULL, prefix, interface, property,
&property_externs, &discard);
if (! get_func)
goto error;
get_notify_func = property_proxy_get_notify_function (
NULL, prefix, interface, property,
&property_prototypes, &property_typedefs,
&discard);
if (! get_notify_func)
goto error;
get_sync_func = property_proxy_get_sync_function (
NULL, prefix, interface, property,
&property_externs, &discard);
if (! get_sync_func)
goto error;
if (! nih_strcat_sprintf (&code, parent,
"%s"
"\n"
"static %s"
"\n"
"%s",
get_func,
get_notify_func,
get_sync_func))
goto error;
}
if (property->access == NIH_DBUS_READWRITE)
if (! nih_strcat (&code, parent, "\n"))
goto error;
if (property->access != NIH_DBUS_READ) {
set_func = property_proxy_set_function (
NULL, prefix, interface, property,
&property_externs, &discard);
if (! set_func)
goto error;
set_notify_func = property_proxy_set_notify_function (
NULL, prefix, interface, property,
&property_prototypes, &property_typedefs,
&discard);
if (! set_notify_func)
goto error;
set_sync_func = property_proxy_set_sync_function (
NULL, prefix, interface, property,
&property_externs,
(property->access == NIH_DBUS_WRITE
? &property_structs
: &discard));
if (! set_sync_func)
goto error;
if (! nih_strcat_sprintf (&code, parent,
"%s"
"\n"
"static %s"
"\n"
"%s",
set_func,
set_notify_func,
set_sync_func))
goto error;
}
NIH_LIST_FOREACH_SAFE (&property_prototypes, iter) {
TypeFunc *func = (TypeFunc *)iter;
if (! type_to_static (&func->type, func))
goto error;
nih_ref (func, code);
nih_list_add (prototypes, &func->entry);
}
NIH_LIST_FOREACH_SAFE (&property_structs, iter) {
TypeStruct *structure = (TypeStruct *)iter;
nih_ref (structure, code);
nih_list_add (structs, &structure->entry);
}
NIH_LIST_FOREACH_SAFE (&property_typedefs, iter) {
TypeFunc *func = (TypeFunc *)iter;
nih_ref (func, code);
nih_list_add (typedefs, &func->entry);
}
NIH_LIST_FOREACH_SAFE (&property_externs, iter) {
TypeFunc *func = (TypeFunc *)iter;
nih_ref (func, code);
nih_list_add (externs, &func->entry);
}
}
/* Functions to obtain all of the properties */
if (! NIH_LIST_EMPTY (&interface->properties)) {
NihList all_prototypes;
NihList all_structs;
NihList all_typedefs;
NihList all_externs;
nih_local char *get_all_func = NULL;
nih_local char *get_all_notify_func = NULL;
nih_local char *get_all_sync_func = NULL;
nih_list_init (&all_prototypes);
nih_list_init (&all_structs);
nih_list_init (&all_typedefs);
nih_list_init (&all_externs);
if (! first)
if (! nih_strcat (&code, parent, "\n\n"))
goto error;
first = FALSE;
get_all_func = interface_proxy_get_all_function (
NULL, prefix, interface,
&all_externs, &discard);
if (! get_all_func)
goto error;
get_all_notify_func = interface_proxy_get_all_notify_function (
NULL, prefix, interface,
&all_prototypes, &all_typedefs,
&discard);
if (! get_all_notify_func)
goto error;
get_all_sync_func = interface_proxy_get_all_sync_function (
NULL, prefix, interface,
&all_externs, &all_structs);
if (! get_all_sync_func)
goto error;
if (! nih_strcat_sprintf (&code, parent,
"%s"
"\n"
"static %s"
"\n"
"%s",
get_all_func,
get_all_notify_func,
get_all_sync_func))
goto error;
NIH_LIST_FOREACH_SAFE (&all_prototypes, iter) {
TypeFunc *func = (TypeFunc *)iter;
if (! type_to_static (&func->type, func))
goto error;
nih_ref (func, code);
nih_list_add (prototypes, &func->entry);
}
NIH_LIST_FOREACH_SAFE (&all_structs, iter) {
TypeStruct *structure = (TypeStruct *)iter;
nih_ref (structure, code);
nih_list_add (structs, &structure->entry);
}
NIH_LIST_FOREACH_SAFE (&all_typedefs, iter) {
TypeFunc *func = (TypeFunc *)iter;
nih_ref (func, code);
nih_list_add (typedefs, &func->entry);
}
NIH_LIST_FOREACH_SAFE (&all_externs, iter) {
TypeFunc *func = (TypeFunc *)iter;
nih_ref (func, code);
nih_list_add (externs, &func->entry);
}
}
}
return code;
error:
nih_free (code);
return NULL;
}
|