<commit_before>/* vim: set ts=2 expandtab: */
/**
 *       @file  client.c
 *      @brief  commotion - a client to the embedded C daemon
 *
 *     @author  <NAME> (jheretic), <EMAIL>
 *
 *   @internal
 *     Created  03/07/2013
 *    Revision  $Id: doxygen.commotion.templates,v 0.1 2013/01/01 09:00:00 jheretic Exp $
 *    Compiler  gcc/g++
 *     Company  The Open Technology Institute
 *   Copyright  Copyright (c) 2013, <NAME>
 *
 * This file is part of Commotion, Copyright (c) 2013, <NAME> 
 * 
 * Commotion is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published 
 * by the Free Software Foundation, either version 3 of the License, 
 * or (at your option) any later version.
 * 
 * Commotion 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 Commotion.  If not, see <http://www.gnu.org/licenses/>.
 *
 * =====================================================================================
 */

#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "config.h"
#include "msg.h"
#include "debug.h"
#include "util.h"
#include "socket.h"

extern co_socket_t unix_socket_proto;

static co_msg_t *cli_parse_argv(char *argv[], const int argc) {
  CHECK(argv != NULL, "No input.");
  char payload[MSG_MAX_PAYLOAD];
  memset(payload, '\0', sizeof(payload));
  co_msg_t *message;
  if(argc > 1) {
    char **args = argv + 1;
    CHECK(argv_to_string(args, argc, payload, MSG_MAX_PAYLOAD), "Failed to parse argv.");
    message = co_msg_create(argv[0], payload);
  } else {
    message = co_msg_create(argv[0], NULL);
  }
  CHECK(message != NULL, "Invalid message.");
  return message;

error:
  free(message);
  return NULL;
}

static co_msg_t *cli_parse_string(const char *input) {
  CHECK(input != NULL, "No input.");
	char *saveptr = NULL;
  int inputsize = strlen(input);
  char *input_tmp = NULL;
  co_msg_t *message = NULL;
  if(inputsize > 2) {
    input_tmp = malloc(inputsize);
    strstrip(input, input_tmp, inputsize);
	  char *command = strtok_r(input_tmp, " ", &saveptr);
    if(strlen(command) < 2) command = "help";
    char *payload = strchr(input, ' ');
    message = co_msg_create(command, payload);
  } else {
    message = co_msg_create("help", NULL);
  }
  CHECK(message != NULL, "Invalid message.");
  free(input_tmp);
  return message;

error:
  free(input_tmp);
  free(message);
  return NULL;
}

static void print_usage() {
  printf(
          "The Commotion management shell.\n"
          "http://commotionwireless.net\n\n"
          "Usage: commotion [options]\n"
          "\n"
          "Options:\n"
          " -b, --bind <uri>    Specify management socket.\n"
          " -h, --help          Print this usage message.\n"<commit_msg>Minor fixes to cli command argument handling.<commit_after>/* vim: set ts=2 expandtab: */
/**
 *       @file  client.c
 *      @brief  commotion - a client to the embedded C daemon
 *
 *     @author  <NAME> (jheretic), <EMAIL>
 *
 *   @internal
 *     Created  03/07/2013
 *    Revision  $Id: doxygen.commotion.templates,v 0.1 2013/01/01 09:00:00 jheretic Exp $
 *    Compiler  gcc/g++
 *     Company  The Open Technology Institute
 *   Copyright  Copyright (c) 2013, <NAME>
 *
 * This file is part of Commotion, Copyright (c) 2013, <NAME> 
 * 
 * Commotion is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published 
 * by the Free Software Foundation, either version 3 of the License, 
 * or (at your option) any later version.
 * 
 * Commotion 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 Commotion.  If not, see <http://www.gnu.org/licenses/>.
 *
 * =====================================================================================
 */

#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "config.h"
#include "msg.h"
#include "debug.h"
#include "util.h"
#include "socket.h"

extern co_socket_t unix_socket_proto;

static co_msg_t *cli_parse_argv(char *argv[], const int argc) {
  CHECK(argv != NULL, "No input.");
  char payload[MSG_MAX_PAYLOAD];
  memset(payload, '\0', sizeof(payload));
  co_msg_t *message;
  if(argc > 0) {
    char **args = argv + 1;
    CHECK(argv_to_string(args, argc, payload, MSG_MAX_PAYLOAD), "Failed to parse argv.");
    message = co_msg_create(argv[0], payload);
  } else {
    message = co_msg_create(argv[0], NULL);
  }
  CHECK(message != NULL, "Invalid message.");
  return message;

error:
  free(message);
  return NULL;
}

static co_msg_t *cli_parse_string(const char *input) {
  CHECK(input != NULL, "No input.");
	char *saveptr = NULL;
  int inputsize = strlen(input);
  char *input_tmp = NULL;
  co_msg_t *message = NULL;
  if(inputsize > 2) {
    input_tmp = malloc(inputsize);
    strstrip(input, input_tmp, inputsize);
    if(input_tmp[inputsize - 1] == '\n') input_tmp[inputsize - 1] ='\0';
	  char *command = strtok_r(input_tmp, " ", &saveptr);
    if(strlen(command) < 2) command = "help";
    char *payload = strchr(input, ' ');
    message = co_msg_create(command, payload);
  } else {
    message = co_msg_create("help", NULL);
  }
  CHECK(message != NULL, "Invalid message.");
  free(input_tmp);
  return message;

error:
  free(input_tmp);
  free(message);
  return NULL;
}

static void print_usage() {
  printf(
          "The Commotion management shell.\n"
          "http://commotionwireless.net\n\n"
          "Usage: commotion [options]\n"
          "\n"
          "Options:\n"
          " -b, --bind <uri>    Specify management socket.\n"
          " -h, --help          Print this usage message.\n"
{'fraction_non_alphanumeric': 0.15588281372395427, 'fraction_numerical': 0.01331662227792574, 'mean_word_length': 3.3816060398078243, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '707786', 'n_tokens_mistral': 2166, 'n_tokens_neox': 2074, 'n_words': 637}