Skip to content

Commit

Permalink
merge main branch into dev/1.0.0 (after "moved_as_ptr" update) (#600)
Browse files Browse the repository at this point in the history
* fix: Rename `bump.bash` to `bump-and-tag.bash`

* feat: Add `version.txt` and infer version in `bump-and-tag.bash`

* fix: Clone repository using actions/checkout

* fix: Add `CMakeFiles` to `.gitignore`

* fix: Add `debug` and `release` to `.gitignore`

* fix: Provide default release number for testing

* fix: Don't bump deps when pattern is undefined

* fix sizes of zcu_owned_matching_listener_t and z_owned_reply_t

* build: Sync  with eclipse-zenoh/zenoh@580f0b6 from 2024-04-11 (#330)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* fix: Specify git remote when pushing the tag

* fix: Require `VERSION`in `bump-and-tag.bash`

* fix: Override release tag if it already exists

* feat(tracing): using tracing and zenoh-util init_log (#308)

* feat(tracing): using tracing and zenoh-util init_log

Signed-off-by: gabrik <[email protected]>

* chore: adding Cargo.lock

Signed-off-by: gabrik <[email protected]>

* chore: updated Cargo.toml.in

Signed-off-by: gabrik <[email protected]>

* feat(tracing): using zenoh main branch

Signed-off-by: gabrik <[email protected]>

---------

Signed-off-by: gabrik <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@580f0b6 from 2024-04-11 (#335)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@23c5932 from 2024-04-16 (#337)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* fix: Support jq 1.6

ubuntu-22.04 runners use jq 1.6 which doesn't recognize a dot for `[]` value iterator.

See: jqlang/jq#1168.

* chore: using new try_init_log_from_env

Signed-off-by: gabrik <[email protected]>

* Align examples and remove reading from stdin (#255)

* Remove reading from stdin, align example implementations

* Add argument parsing implementation for examples

* Add argument parsing to examples, format files

* Replace getchar with sleep in z_pong example

* Fix typo in include

* Use null-pointers instead of empty strings, remove unnecessary mallocs

* Free returned pointer after parse_pos_args usage

* Add common and positional args parsing to z_ping example

* Add formatting for parsed config options

* Add const to function parameters

* Update mode option help

* Fix pos_args memory leak

* Refactor parse_args, remove possible strcpy buffer overflow

* Change parse_args function returns to const where applicable

* Fix const initialization warning

* Remove redundant const for value parameters

* Fix buf variable memory leak

* Update insert json-list config error message

* Add usage example for -e and -l arguments in help

* Update example notation in help message

Co-authored-by: Alexander <[email protected]>

* Update example notation in help message (2/2)

* Fix parameter in error message

Co-authored-by: Alexander <[email protected]>

---------

Co-authored-by: Alexander <[email protected]>

* Bugfix: Unable to build z_queryable_with_channels.c (#340)

Signed-off-by: ChenYing Kuo <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@0283aaa from 2024-04-19 (#341)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@e8916bf from 2024-04-26 (#343)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* Update README and specify Rust version (#342)

* Clean up the Markdown format.

Signed-off-by: ChenYing Kuo <[email protected]>

* Specify Rust version in README.

Signed-off-by: ChenYing Kuo <[email protected]>

---------

Signed-off-by: ChenYing Kuo <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@ea604b6 from 2024-04-29 (#344)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@371ca6b from 2024-04-30 (#347)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@7a47445 from 2024-05-03 (#348)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@f5195c0 from 2024-05-03 (#350)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@e53364f from 2024-05-04 (#351)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@7e5d5e8 from 2024-05-07 (#355)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@b8dd01d from 2024-05-07 (#356)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@45e05f0 from 2024-05-13 (#360)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* Fix build with CMAKE_BUILD_TYPE=None

This is the default build type for debhelper (Debian).

* build: Sync  with eclipse-zenoh/zenoh@763a05f from 2024-05-14 (#363)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@75aa273 from 2024-05-15 (#364)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@25f06bd from 2024-05-21 (#369)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@3118d31 from 2024-05-28 (#399)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@009f666 from 2024-05-30 (#411)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@d574654 from 2024-06-03 (#420)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* chore: Update artifacts action to v4 (#421)

artifacts actions v3 are deprecated

* build: Sync  with eclipse-zenoh/zenoh@c279982 from 2024-06-05 (#424)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@d8e66de from 2024-06-10 (#436)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@9d09742 from 2024-06-11 (#446)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@ed6c636 from 2024-06-12 (#450)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@8160b01 from 2024-06-13 (#457)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* Enable releasing from any branch (#456)

* build: Sync  with eclipse-zenoh/zenoh@7adad94 from 2024-06-14 (#460)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* Update to latest zenoh

* Replace `-rc` with `-pre` and document versioning (#466)

* build: Sync  with eclipse-zenoh/zenoh@2500e5a from 2024-06-20 (#467)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* macro generation

* moved types added to decl

* moved type drop

* switched rust to z_moved

* moved closures

* build macros fixed

* z_move name restored

* into_rust_type for moved, payloads

* tests updated

* cargo fmt

* moved structs in some drops/undeclares

* moved as separate parameter

* removed asref from moved

* moved unfinished

* build: Sync  with eclipse-zenoh/zenoh@869ace6 from 2024-07-02 (#494)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* moved type with into_rust_type trait, comiles without shm

* build with shm passed

* option added to some decl_c_type

* clippy fix

* build fix

* moved types added

* task moved used

* build: Sync  with eclipse-zenoh/zenoh@b93ca84 from 2024-07-03 (#500)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* some examples fixes

* macros corrected to use auto derive loaned type from owned feature

* optional comma allowed in macros where forgotten

* property moved get

* put options move

* publisher delete options made simpler

* put options with moved

* delete options timestamp simplified

* more moved in options, timestamp simplified

* examples,tests updated

* tests compile fixes

* fix for test failure due to calling z_moved_xxx_t destructor on unitialized memory

* cargo fmt imports

* build fixes

* build: Sync  with eclipse-zenoh/zenoh@b3e42ce from 2024-07-08 (#508)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* Ensure that find_package(zenohc) can be called two times (#470)

* Update CMakeLists.txt (#473)

* Install zenohc.dll in <prefix>/bin on Windows (#471)

* build: Sync  with eclipse-zenoh/zenoh@0a969cb from 2024-07-25 (#546)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* build: Sync  with eclipse-zenoh/zenoh@e587aa9 from 2024-07-26 (#552)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* some xompile errors fixed

* some build errors fixed

* some build errors fixed

* build fixes

* cargo fmt

* into_rust_type usage fixes

* encoding drop fixed

* restored headers

* zcu renamed back to zc

* zcu renamed back to zc

* z_xxx_move is static inline, cpp fixes

* build: Sync  with eclipse-zenoh/zenoh@2d88c7b from 2024-07-29 (#556)

Co-authored-by: eclipse-zenoh-bot <[email protected]>

* clang format from start

* cargo fmt

* macros contains funcions now, it needs types defined

* removed zenoh_macros include

* zenoh_macros include returned back to place

* C++ build test added, fails for now

* C++ enabling correction

* C++ compilation for tests added

* C++ build test

* cargo lock update

* retrun value if not void from template functions

* cargo fmt

* build fixes

* build fix after cargo.lock update

* moved types for buffer creation functions

* clippy fix

* clippy fix: c_char can be i8 or u8 depending on platform

* headers restored

* cargo fmt

* -c c++ flag for clang only

* c++ build fix - brackets removed

* type specific take functions added, _ptr in moved

* generic_take_cpp

* z_take impls at the end

* take funcs before generics

* take moved after null

* names fix

* missing null functioj added

* tests fixed for c++

* explicit null calls

* fix generic parameter names c compilation

* null call fix

* misprint fixed

* return removed

* Rename `close` to `undeclare` for Publication Cache and Querying Subscriber

* Temporarily use original pull request branch

* Update to eclipse-zenoh/zenoh@ce4e9bf

* Fix `z_ref_shm_client_storage_global`

* Update Cargo.toml

* decl_c_type corrected

* cargo check run

* borrow error fix

* compilation fix

* parse arg fix

* example compilation fix

* examples compile fix

* examples build fixes

* removed duplicated z_config_default (it's called in parsing args later)

* clang format

* clang format

* cargo.toml restore

---------

Signed-off-by: gabrik <[email protected]>
Signed-off-by: ChenYing Kuo <[email protected]>
Co-authored-by: Mahmoud Mazouz <[email protected]>
Co-authored-by: Denis Biryukov <[email protected]>
Co-authored-by: eclipse-zenoh-bot <[email protected]>
Co-authored-by: eclipse-zenoh-bot <[email protected]>
Co-authored-by: Mahmoud Mazouz <[email protected]>
Co-authored-by: Gabriele Baldoni <[email protected]>
Co-authored-by: gabrik <[email protected]>
Co-authored-by: oteffahi <[email protected]>
Co-authored-by: Alexander <[email protected]>
Co-authored-by: ChenYing Kuo (CY) <[email protected]>
Co-authored-by: Jochen Sprickerhof <[email protected]>
Co-authored-by: Diogo Matsubara <[email protected]>
Co-authored-by: OlivierHecart <[email protected]>
Co-authored-by: Silvio Traversaro <[email protected]>
Co-authored-by: Luca Cominardi <[email protected]>
  • Loading branch information
16 people authored Aug 21, 2024
1 parent f3f352a commit e626980
Show file tree
Hide file tree
Showing 26 changed files with 1,451 additions and 566 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ include/zenoh_opaque.h

# Build resources
.build_resources*
src/opaque_types/mod.rs
src/opaque_types/mod.rs

# CMake
CMakeFiles/
debug/
release/
216 changes: 216 additions & 0 deletions examples/parse_args.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
//
// Copyright (c) 2024 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "zenoh.h"

#define COMMON_HELP \
"\
-c <CONFIG> (optional, string): The path to a configuration file for the session. If this option isn't passed, the default configuration will be used.\n\
-m <MODE> (optional, string, default='peer'): The zenoh session mode. [possible values: peer, client, router]\n\
-e <CONNECT> (optional, string): endpoint to connect to. Repeat option to pass multiple endpoints. If none are given, endpoints will be discovered through multicast-scouting if it is enabled.\n\
e.g.: '-e tcp/192.168.1.1:7447'\n\
-l <LISTEN> (optional, string): locator to listen on. Repeat option to pass multiple locators. If none are given, the default configuration will be used.\n\
e.g.: '-l tcp/192.168.1.1:7447'\n\
--no-multicast-scouting (optional): By default zenohd replies to multicast scouting messages for being discovered by peers and clients. This option disables this feature.\n\
"

/**
* Parse an option of format `-f`, `--flag`, `-f <value>` or `--flag <value>` from `argv`. If found, the option and its
* eventual value are each replaced by NULL in `argv`
* @param argc: argc passed from `main` function
* @param argv: argv passed from `main` function
* @param opt: option to parse (without `-` or `--` prefix)
* @param opt_has_value: if true, the option is of format `-f <value>` or `--flag <value>` and `value` will be returned
* if found, else an error message is printed and program will exit. If false, option has no value and a non-null
* pointer will be returned if option is found.
* @returns NULL if option was not found, else a non-null value depending on if `opt_has_value`.
*/
const char* parse_opt(int argc, char** argv, const char* opt, bool opt_has_value) {
size_t optlen = strlen(opt);
for (int i = 1; i < argc; i++) {
if (argv[i] == NULL) {
continue;
}
size_t len = strlen(argv[i]);
if (len < 2) {
continue;
}
if (optlen == 1) {
if (argv[i][0] == '-' && argv[i][1] == opt[0]) {
argv[i] = NULL;
if (!opt_has_value) {
return (char*)opt;
} else if (i + 1 < argc && argv[i + 1]) {
char* value = argv[i + 1];
argv[i + 1] = NULL;
return value;
} else {
printf("Option -%s given without a value\n", opt);
exit(-1);
}
}
} else if (optlen > 1 && len > 3 && argv[i][0] == '-' && argv[i][1] == '-') {
// Note: support for '--arg=<value>' syntax can be added here
if (strcmp(argv[i] + 2, opt) == 0) {
argv[i] = NULL;
if (!opt_has_value) {
return (char*)opt;
} else if (i + 1 < argc && argv[i + 1]) {
char* value = argv[i + 1];
argv[i + 1] = NULL;
return value;
} else {
printf("Option --%s given without a value\n", opt);
exit(-1);
}
}
}
}
return NULL;
}

/**
* Check if any options remains in `argv`. Must be called after all expected options are parsed
* @param argc
* @param argv
* @returns NULL if no option was found, else the first option string that was found
*/
const char* check_unknown_opts(int argc, char** const argv) {
for (int i = 1; i < argc; i++) {
if (argv[i] && argv[i][0] == '-') {
return argv[i];
}
}
return NULL;
}

/**
* Parse positional arguments from `argv`. Must be called after all expected options are parsed, and after checking that
* no unknown options remain in `argv`
* @param argc
* @param argv
* @param nb_args: number of expected positional arguments
* @returns NULL if found more positional arguments than `nb_args`. Else an array of found arguments in order, followed
* by NULL values if found less positional arguments than `nb_args`
* @note Returned pointer is dynamically allocated and must be freed
*/
char** parse_pos_args(const int argc, char** argv, const size_t nb_args) {
char** pos_argv = (char**)calloc(nb_args, sizeof(char*));
size_t pos_argc = 0;
for (int i = 1; i < argc; i++) {
if (argv[i]) {
pos_argc++;
if (pos_argc > nb_args) {
free(pos_argv);
return NULL;
}
pos_argv[pos_argc - 1] = argv[i];
}
}
return pos_argv;
}

/**
* Parse zenoh options that require a JSON-serialized list (-e, -l from common args) and add them to
* `config`. Prints error message and exits if fails to insert parsed values
* @param argc
* @param argv
* @param opt: option to parse (without `-` or `--` prefix)
* @param config: address of an owned zenoh configuration
* @param config_key: zenoh configuration key under which the parsed values will be inserted
*/
void parse_zenoh_json_list_config(int argc, char** argv, const char* opt, const char* config_key,
z_owned_config_t* config) {
char* buf = (char*)calloc(1, sizeof(char));
const char* value = parse_opt(argc, argv, opt, true);
while (value) {
size_t len_newbuf = strlen(buf) + strlen(value) + 4; // value + quotes + comma + nullbyte
char* newbuf = (char*)malloc(len_newbuf);
snprintf(newbuf, len_newbuf, "%s'%s',", buf, value);
free(buf);
buf = newbuf;
value = parse_opt(argc, argv, opt, true);
}
size_t buflen = strlen(buf);
if (buflen > 0) {
// remove trailing comma
buf[buflen - 1] = '\0';
buflen--;
// add list delimiters
size_t json_list_len = buflen + 3; // buf + brackets + nullbyte
char* json_list = (char*)malloc(json_list_len);
snprintf(json_list, json_list_len, "[%s]", buf);
// insert in config
if (zc_config_insert_json(z_loan_mut(*config), config_key, json_list) < 0) {
printf(
"Couldn't insert value `%s` in configuration at `%s`\n`%s` is either not a JSON-serialized list of "
"strings, or values within the list do not respect expected format for `%s`\n",
json_list, config_key, json_list, config_key);
free(json_list);
exit(-1);
}
free(json_list);
}
free(buf);
}

/**
* Parse zenoh options that are common to all examples (-c, -m, -e, -l, --no-multicast-scouting) and add them to
* `config`
* @param argc
* @param argv
* @param config: address of an owned zenoh configuration
*/
void parse_zenoh_common_args(const int argc, char** argv, z_owned_config_t* config) {
// -c: A configuration file.
const char* config_file = parse_opt(argc, argv, "c", true);
if (config_file) {
zc_config_from_file(config, config_file);
} else {
z_config_default(config);
}
// -m: The Zenoh session mode [default: peer].
const char* mode = parse_opt(argc, argv, "m", true);
if (mode) {
size_t buflen = strlen(mode) + 3; // mode + quotes + nullbyte
char* buf = (char*)malloc(buflen);
snprintf(buf, buflen, "'%s'", mode);
if (zc_config_insert_json(z_loan_mut(*config), Z_CONFIG_MODE_KEY, buf) < 0) {
printf(
"Couldn't insert value `%s` in configuration at `%s`. Value must be one of: 'client', 'peer' or "
"'router'\n",
mode, Z_CONFIG_MODE_KEY);
free(buf);
exit(-1);
}
free(buf);
}
// -e: Endpoint to connect to. Can be repeated
parse_zenoh_json_list_config(argc, argv, "e", Z_CONFIG_CONNECT_KEY, config);
// -l: Endpoint to listen on. Can be repeated
parse_zenoh_json_list_config(argc, argv, "l", Z_CONFIG_LISTEN_KEY, config);
// --no-multicast-scrouting: Disable the multicast-based scouting mechanism.
const char* no_multicast_scouting = parse_opt(argc, argv, "no-multicast-scouting", false);
if (no_multicast_scouting &&
zc_config_insert_json(z_loan_mut(*config), Z_CONFIG_MULTICAST_SCOUTING_KEY, "false") < 0) {
printf("Couldn't disable multicast-scouting.\n");
exit(-1);
}
}
63 changes: 48 additions & 15 deletions examples/z_delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,19 @@
#include <stdio.h>
#include <string.h>

#include "parse_args.h"
#include "zenoh.h"

int main(int argc, char **argv) {
char *keyexpr = "demo/example/zenoh-c-put";
#define DEFAULT_KEYEXPR "demo/example/zenoh-c-put"

if (argc > 1) keyexpr = argv[1];
struct args_t {
char* keyexpr; // -k
};
struct args_t parse_args(int argc, char** argv, z_owned_config_t* config);

int main(int argc, char** argv) {
z_owned_config_t config;
z_config_default(&config);
if (argc > 3) {
if (zc_config_insert_json(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, argv[3]) < 0) {
printf(
"Couldn't insert value `%s` in configuration at `%s`. This is likely because `%s` expects a "
"JSON-serialized list of strings\n",
argv[3], Z_CONFIG_CONNECT_KEY, Z_CONFIG_CONNECT_KEY);
exit(-1);
}
}
struct args_t args = parse_args(argc, argv, &config);

printf("Opening session...\n");
z_owned_session_t s;
Expand All @@ -40,9 +35,9 @@ int main(int argc, char **argv) {
exit(-1);
}

printf("Deleting resources matching '%s'...\n", keyexpr);
printf("Deleting resources matching '%s'...\n", args.keyexpr);
z_view_keyexpr_t ke;
z_view_keyexpr_from_str(&ke, keyexpr);
z_view_keyexpr_from_str(&ke, args.keyexpr);
int res = z_delete(z_loan(s), z_loan(ke), NULL);
if (res < 0) {
printf("Delete failed...\n");
Expand All @@ -51,3 +46,41 @@ int main(int argc, char **argv) {
z_close(z_move(s));
return 0;
}

void print_help() {
printf(
"\
Usage: z_delete [OPTIONS]\n\n\
Options:\n\
-k <KEY> (optional, string, default='%s'): The key expression to write to\n",
DEFAULT_KEYEXPR);
printf(COMMON_HELP);
printf(
"\
-h: print help\n");
}

struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
if (parse_opt(argc, argv, "h", false)) {
print_help();
exit(1);
}
const char* keyexpr = parse_opt(argc, argv, "k", true);
if (!keyexpr) {
keyexpr = DEFAULT_KEYEXPR;
}
parse_zenoh_common_args(argc, argv, config);
const char* arg = check_unknown_opts(argc, argv);
if (arg) {
printf("Unknown option %s\n", arg);
exit(-1);
}
char** pos_args = parse_pos_args(argc, argv, 1);
if (!pos_args || pos_args[0]) {
printf("Unexpected positional arguments\n");
free(pos_args);
exit(-1);
}
free(pos_args);
return (struct args_t){.keyexpr = (char*)keyexpr};
}
Loading

0 comments on commit e626980

Please sign in to comment.