+ ct->status = CL_CONNECTED;
+ ct->btrn[0] = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = "client recv", .parent = NULL, .child = child));
+ ct->btrn[1] = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = "client send", .parent = parent, .child = NULL));
+
+ ct->task = task_register(&(struct task_info) {
+ .name = "client",
+ .pre_select = client_pre_select,
+ .post_select = client_post_select,
+ .context = ct,
+ }, s);
+ return 1;
+err_out:
+ close(ct->scc.fd);
+ ct->scc.fd = -1;
+ return ret;
+}
+
+__noreturn static void print_help_and_die(struct client_task *ct)
+{
+ struct ggo_help h = DEFINE_GGO_HELP(client);
+ bool d = ct->conf.detailed_help_given;
+
+ ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
+ exit(0);
+}
+
+/**
+ * Parse a client configuration.
+ *
+ * \param argc Usual argument count.
+ * \param argv Usual argument vector.
+ * \param ct_ptr Filled in by this function.
+ * \param loglevel If not \p NULL, the number of the loglevel is stored here.
+ *
+ * This checks the command line options given by \a argc and \a argv, sets
+ * default values for the user name and the name of the rsa key file and reads
+ * further options from the config file.
+ *
+ * Upon successful return, \a ct_ptr points to a dynamically allocated and
+ * initialized client task struct.
+ *
+ * \return The number of non-option arguments in \a argc/argv on success,
+ * negative on errors.
+ */
+int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
+ int *loglevel)
+{
+ char *home = para_homedir();
+ int ret;
+ struct client_task *ct = para_calloc(sizeof(struct client_task));
+
+ *ct_ptr = ct;
+ ct->scc.fd = -1;
+ ret = -E_CLIENT_SYNTAX;
+ if (client_cmdline_parser(argc, argv, &ct->conf))
+ goto out;
+ version_handle_flag("client", ct->conf.version_given);
+ if (ct->conf.help_given || ct->conf.detailed_help_given)
+ print_help_and_die(ct);
+
+ ct->config_file = ct->conf.config_file_given?
+ para_strdup(ct->conf.config_file_arg) :
+ make_message("%s/.paraslash/client.conf", home);
+ ret = file_exists(ct->config_file);
+ if (!ret && ct->conf.config_file_given) {
+ ret = -E_NO_CONFIG;
+ goto out;
+ }
+ if (ret) {
+ struct client_cmdline_parser_params params = {
+ .override = 0,
+ .initialize = 0,
+ .check_required = 0,
+ .check_ambiguity = 0,
+ .print_errors = 0
+ };
+ ret = -E_BAD_CONFIG;
+ if (client_cmdline_parser_config_file(ct->config_file,
+ &ct->conf, ¶ms))
+ goto out;
+ }
+ ct->user = ct->conf.user_given?
+ para_strdup(ct->conf.user_arg) : para_logname();
+
+ if (ct->conf.key_file_given)
+ ct->key_file = para_strdup(ct->conf.key_file_arg);
+ else {
+ ct->key_file = make_message("%s/.paraslash/key.%s",
+ home, ct->user);
+ if (!file_exists(ct->key_file)) {
+ free(ct->key_file);
+ ct->key_file = make_message("%s/.ssh/id_rsa", home);
+ }
+ }
+
+ if (loglevel)
+ *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
+ PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
+ PARA_INFO_LOG("config_file: %s\n", ct->config_file);
+ PARA_INFO_LOG("key_file: %s\n", ct->key_file);
+ ret = ct->conf.inputs_num;
+out:
+ free(home);
+ if (ret < 0) {
+ PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ client_close(ct);
+ *ct_ptr = NULL;
+ }
+ return ret;
+}
+
+/**
+ * Parse the client configuration and open a connection to para_server.
+ *
+ * \param argc See \ref client_parse_config.
+ * \param argv See \ref client_parse_config.
+ * \param ct_ptr See \ref client_parse_config.
+ * \param loglevel See \ref client_parse_config.
+ * \param parent See \ref client_connect().
+ * \param child See \ref client_connect().
+ * \param sched See \ref client_connect().
+ *
+ * This function combines client_parse_config() and client_connect(). It is
+ * considered a syntax error if no command was given, i.e. if the number
+ * of non-option arguments is zero.
+ *
+ * \return Standard.
+ */
+int client_open(int argc, char *argv[], struct client_task **ct_ptr,
+ int *loglevel, struct btr_node *parent, struct btr_node *child,
+ struct sched *sched)
+{
+ int ret = client_parse_config(argc, argv, ct_ptr, loglevel);
+