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
|
/* nih-dbus-tool
*
* output.c - source and header file output
*
* 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 <ctype.h>
#include <string.h>
#include <unistd.h>
#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/string.h>
#include <nih/list.h>
#include <nih/main.h>
#include <nih/logging.h>
#include <nih/error.h>
#include "type.h"
#include "node.h"
#include "interface.h"
#include "method.h"
#include "signal.h"
#include "property.h"
#include "output.h"
/* Prototypes for static functions */
static int output_write (int fd, const char *str)
__attribute__ ((warn_unused_result));
/**
* output_package:
*
* Package name to use when generating header and source file comments
* and header file sentinel macro. Defaults to libnih when not set.
**/
char *output_package = NULL;
/**
* output:
* @source_path: path of source file to write,
* @source_fd: file descriptor open to write to @source_path,
* @header_path: path of header file to write,
* @header_fd: file descriptor open to write to @header_path,
* @prefix: prefix to prepend to symbols,
* @node: node to output,
* @object: whether to output for an object or proxy.
*
* Writes a valid C source file to @source_fd and its accompanying header
* file to @header_fd; which should file descriptors open to writing to
* @source_path and @header_path respectively.
*
* If @object is TRUE, the output code provides D-Bus bindings that wrap
* externally defined C functions providing an implementation of @node.
* When @object is FALSE, the output code instead provides API functions
* that access a remote D-Bus object @node.
*
* Externally available symbols will all be prefixed with @prefix.
*
* Returns: zero on success, negative value on raised error.
**/
int
output (const char *source_path,
int source_fd,
const char *header_path,
int header_fd,
const char *prefix,
Node * node,
int object)
{
NihList prototypes;
NihList handlers;
NihList structs;
NihList typedefs;
NihList vars;
NihList externs;
nih_local char *array = NULL;
nih_local char *code = NULL;
nih_local char *source = NULL;
nih_local char *header = NULL;
nih_local char *sentinel = NULL;
nih_assert (source_path != NULL);
nih_assert (source_fd >= 0);
nih_assert (header_path != NULL);
nih_assert (header_fd >= 0);
nih_assert (prefix != NULL);
nih_assert (node != NULL);
nih_list_init (&prototypes);
nih_list_init (&handlers);
nih_list_init (&structs);
nih_list_init (&typedefs);
nih_list_init (&vars);
nih_list_init (&externs);
/* Start off the text of the source file with the copyright preamble
* and the list of includes.
*/
source = output_preamble (NULL, source_path);
if (! source) {
nih_error_raise_no_memory ();
return -1;
}
if (! nih_strcat (&source, NULL,
"#ifdef HAVE_CONFIG_H\n"
"# include <config.h>\n"
"#endif /* HAVE_CONFIG_H */\n"
"\n"
"\n"
"#include <dbus/dbus.h>\n"
"\n"
"#include <stdint.h>\n"
"#include <string.h>\n"
"\n"
"#include <nih/macros.h>\n"
"#include <nih/alloc.h>\n"
"#include <nih/string.h>\n"
"#include <nih/logging.h>\n"
"#include <nih/error.h>\n"
"\n"
"#include <nih-dbus/dbus_error.h>\n"
"#include <nih-dbus/dbus_message.h>\n")) {
nih_error_raise_no_memory ();
return -1;
}
/* Start off the text of the header file with the copyright preamble,
* sentinel and list of includes.
*/
header = output_preamble (NULL, NULL);
if (! header) {
nih_error_raise_no_memory ();
return -1;
}
sentinel = output_sentinel (NULL, header_path);
if (! sentinel) {
nih_error_raise_no_memory ();
return -1;
}
if (! nih_strcat_sprintf (&header, NULL,
"#ifndef %s\n"
"#define %s\n"
"\n",
sentinel,
sentinel)) {
nih_error_raise_no_memory ();
return -1;
}
if (! nih_strcat (&header, NULL,
"#include <dbus/dbus.h>\n"
"\n"
"#include <stdint.h>\n"
"\n"
"#include <nih/macros.h>\n"
"\n"
"#include <nih-dbus/dbus_interface.h>\n"
"#include <nih-dbus/dbus_message.h>\n")) {
nih_error_raise_no_memory ();
return -1;
}
/* Obtain the interfaces array for the source file */
array = node_interfaces_array (NULL, prefix, node, object, &vars);
if (! array) {
nih_error_raise_no_memory ();
return -1;
}
/* Add any object/proxy-specific headers, and obtain the code
* for the functions, as well as the prototypes, typedefs, handler
* prototypes, extern prototypes, etc.
*/
if (object) {
if (! nih_strcat (&source, NULL,
"#include <nih-dbus/dbus_object.h>\n")) {
nih_error_raise_no_memory ();
return -1;
}
code = node_object_functions (NULL, prefix, node,
&prototypes, &handlers,
&structs, &externs);
if (! code) {
nih_error_raise_no_memory ();
return -1;
}
} else {
if (! nih_strcat (&source, NULL,
"#include <nih-dbus/dbus_pending_data.h>\n"
"#include <nih-dbus/dbus_proxy.h>\n")) {
nih_error_raise_no_memory ();
return -1;
}
if (! nih_strcat (&header, NULL,
"#include <nih-dbus/dbus_pending_data.h>\n"
"#include <nih-dbus/dbus_proxy.h>\n")) {
nih_error_raise_no_memory ();
return -1;
}
code = node_proxy_functions (NULL, prefix, node,
&prototypes,
&structs, &typedefs, &externs);
if (! code) {
nih_error_raise_no_memory ();
return -1;
}
}
/* errors.h is always the last header by style, followed by the
* header itself.
*/
if (! nih_strcat_sprintf (&source, NULL,
"#include <nih-dbus/errors.h>\n"
"\n"
"#include \"%s\"\n"
"\n"
"\n",
header_path)) {
nih_error_raise_no_memory ();
return -1;
}
if (! nih_strcat (&header, NULL,
"\n"
"\n")) {
nih_error_raise_no_memory ();
return -1;
}
/* Declare the prototypes of static functions defined here in the
* source file. These are the handler and getter/setter functions
* referred to in the array structures.
*/
if (! NIH_LIST_EMPTY (&prototypes)) {
nih_local char *block = NULL;
block = type_func_layout (NULL, &prototypes);
if (! block) {
nih_error_raise_no_memory ();
return -1;
}
if (! nih_strcat_sprintf (&source, NULL,
"/* Prototypes for static functions */\n"
"%s"
"\n"
"\n",
block)) {
nih_error_raise_no_memory ();
return -1;
}
}
/* Declare the prototypes of external handler functions that we
* expect other source files to implement.
*/
if (! NIH_LIST_EMPTY (&handlers)) {
nih_local char *block = NULL;
block = type_func_layout (NULL, &handlers);
if (! block) {
nih_error_raise_no_memory ();
return -1;
}
if (! nih_strcat_sprintf (&source, NULL,
"/* Prototypes for externally implemented handler functions */\n"
"%s"
"\n"
"\n",
block)) {
nih_error_raise_no_memory ();
return -1;
}
}
/* Define the arrays of methods and signals and their arguments,
* prototypes, interfaces, etc. for the node. These refer to the
* above prototypes.
*/
if (! nih_strcat_sprintf (&source, NULL,
"%s"
"\n"
"\n",
array)) {
nih_error_raise_no_memory ();
return -1;
}
/* Finally append all of the function code.
*/
if (! nih_strcat (&source, NULL, code)) {
nih_error_raise_no_memory ();
return -1;
}
/* Write it */
if (output_write (source_fd, source) < 0)
return -1;
/* Define each of the structures in the header file, each is
* a typdef so gets its own line.
*/
if (! NIH_LIST_EMPTY (&structs)) {
NIH_LIST_FOREACH (&structs, iter) {
TypeStruct * structure = (TypeStruct *)iter;
nih_local char *block = NULL;
block = type_struct_to_string (NULL, structure);
if (! block) {
nih_error_raise_no_memory ();
return -1;
}
if (! nih_strcat_sprintf (&header, NULL,
"%s"
"\n",
block)) {
nih_error_raise_no_memory ();
return -1;
}
}
if (! nih_strcat (&header, NULL, "\n")) {
nih_error_raise_no_memory ();
return -1;
}
}
/* Define each of the typedefs in the header file, some of these
* are actually required in the prototypes while others serve as
* documentation for what to pass to nih_dbus_proxy_connect()
*/
if (! NIH_LIST_EMPTY (&typedefs)) {
NIH_LIST_FOREACH (&typedefs, iter) {
TypeFunc * func = (TypeFunc *)iter;
nih_local char *block = NULL;
block = type_func_to_typedef (NULL, func);
if (! block) {
nih_error_raise_no_memory ();
return -1;
}
if (! nih_strcat_sprintf (&header, NULL,
"%s"
"\n",
block)) {
nih_error_raise_no_memory ();
return -1;
}
}
if (! nih_strcat (&header, NULL, "\n")) {
nih_error_raise_no_memory ();
return -1;
}
}
if (! nih_strcat (&header, NULL,
"NIH_BEGIN_EXTERN\n")) {
nih_error_raise_no_memory ();
return -1;
}
/* Declare global variables defined in the source file, these are
* the interface structures and the array of them for the node.
*/
if (! NIH_LIST_EMPTY (&vars)) {
nih_local char *block = NULL;
block = type_var_layout (NULL, &vars);
if (! block) {
nih_error_raise_no_memory ();
return -1;
}
if (! nih_strcat_sprintf (&header, NULL,
"\n"
"%s"
"\n",
block)) {
nih_error_raise_no_memory ();
return -1;
}
}
/* Declare the prototypes of the functions defined in the source
* file.
*/
if (! NIH_LIST_EMPTY (&externs)) {
nih_local char *block = NULL;
block = type_func_layout (NULL, &externs);
if (! block) {
nih_error_raise_no_memory ();
return -1;
}
if (! nih_strcat_sprintf (&header, NULL,
"\n"
"%s"
"\n",
block)) {
nih_error_raise_no_memory ();
return -1;
}
}
if (! nih_strcat_sprintf (&header, NULL,
"NIH_END_EXTERN\n"
"\n"
"#endif /* %s */\n",
sentinel)) {
nih_error_raise_no_memory ();
return -1;
}
/* Write it */
if (output_write (header_fd, header) < 0)
return -1;
return 0;
}
/**
* output_preamble:
* @parent: parent object for new string,
* @path: path of source file.
*
* Generates the preamble header of a source or header file, containing the
* package name of the software being built, @path if specified, the author's
* copyright and a statement to see the source for copying conditions.
*
* 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 returned string will also be
* freed.
*
* Returns: newly allocated string or NULL if allocation failed.
**/
char *
output_preamble (const void *parent,
const char *path)
{
char *code;
code = nih_sprintf (parent, "/* %s\n *\n",
output_package ?: package_name);
if (! code)
return NULL;
if (path) {
if (! nih_strcat_sprintf (&code, parent,
" * %s - auto-generated D-Bus bindings\n"
" *\n",
path)) {
nih_free (code);
return NULL;
}
}
if (! nih_strcat_sprintf (&code, parent,
" * %s\n"
" *\n"
" * This file was automatically generated; see the source for copying\n"
" * conditions.\n"
" */\n"
"\n",
package_copyright)) {
nih_free (code);
return NULL;
}
return code;
}
/**
* output_sentinel:
* @parent: parent object for new string,
* @path: path of header file.
*
* Generates the name of header sentinel macro, used to ensure that a header
* is not accidentally included twice (thus making out-of-order includes
* possible).
*
* The name is the path, prefixed with the package name of the software being
* built, uppercased and unrecognised characters replaced by underscores.
*
* 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 returned string will also be
* freed.
*
* Returns: newly allocated string or NULL if allocation failed.
**/
char *
output_sentinel (const void *parent,
const char *path)
{
char *sentinel, *s;
nih_assert (path != NULL);
sentinel = nih_sprintf (parent, "%s_%s",
output_package ?: package_name, path);
if (! sentinel)
return NULL;
for (s = sentinel; *s; s++) {
if (((*s < 'A') || (*s > 'Z'))
&& ((*s < 'a') || (*s > 'z'))
&& ((*s < '0') || (*s > '9'))) {
*s = '_';
} else {
*s = toupper (*s);
}
}
return sentinel;
}
/**
* output_write:
* @fd: file descriptor to write to,
* @str: string to write.
*
* Wraps the write() syscall to ensure that the entire string @str is written
* to @fd, since write() may perform short writes.
*
* Returns: zero on success, negative value on raised error.
**/
static int
output_write (int fd,
const char *str)
{
ssize_t len;
size_t count;
nih_assert (fd >= 0);
nih_assert (str != NULL);
count = strlen (str);
while (count) {
len = write (fd, str, count);
if (len < 0) {
nih_error_raise_system ();
return -1;
}
count -= len;
str += len;
}
return 0;
}
|