<commit_before>
// Copyright (c) 2008-2009, Regents of the University of Colorado.
// This work was supported by NASA contracts NNJ05HE10G, NNC06CB40C, and
// NNC07CB47C.


#define _XOPEN_SOURCE
#define _BSD_SOURCE

#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>

#include "bdm-client.h"


extern int bdm_fd;

static int str_to_int(const char * str) {
    char * endptr;
    int i;

    i = strtol(str, &endptr, 10);
    if(*endptr != '\0') {
        printf("error parsing int string '%s': Unknown characters '%s'\n", 
            str, endptr);
        exit(1);
    }
    return i;
}

void str_to_timeval(const char *str, struct timeval *tv) {
    struct tm tm;
    char *p;
    char *old_tz;

    p = strptime(str, "%Y-%m-%d %T", &tm);
    if (p == NULL) {
        printf("error parsing time string '%s': %s\n", str, strerror(errno));
        exit(1);
    }

    if (*p == '\0') {
        tv->tv_usec = 0;
    } else {
        int r;
        unsigned int val;
        int consumed;

        if (*p != '.') {
            printf("error parsing fractional seconds from time string '%s': expected decimal point after seconds\n", str);
            exit(1);
        }
        p ++;

        if ((*p < '0') || (*p > '9')) {
            printf("error parsing fractional seconds from time string '%s': expected number after decimal point\n", str);
            exit(1);
        }

        r = sscanf(p, "%6u%n", &val, &consumed);
        // the effect of %n on the returned conversion count is ambiguous
        if ((r != 1) && (r != 2)) {
            printf("error parsing fractional seconds from time string '%s': didn't find \"%%u\"\n", str);
            exit(1);
        }
        if (consumed != strlen(p)) {
            printf("error parsing fractional seconds from time string '%s': garbage at end\n", str);
            exit(1);
        }
        if (consumed > 6) {
            printf("error parsing fractional seconds from time string '%s': number too long\n", str);
            exit(1);
        }

        tv->tv_usec = val * pow(10, 6-consumed);
    }

    old_tz = getenv("TZ");
    setenv("TZ", "UTC", 1);
    tv->tv_sec = mktime(&tm);
    if (old_tz) {
	char sanitized[128];
	snprintf(sanitized, 128, "%s", old_tz);
	setenv("TZ", sanitized, 1);
    } else {
	unsetenv("TZ");
    }
}




void usage(void) {
    fprintf(stderr,
	    "'bdm-client' reads data from the BDM\n"
	    "\n"
	    "usage: bdm-client [OPTIONS]\n"
	    "\n"
	    " -?,--help                          Show this usage information\n"
	    " -v,--version                       Show the version number\n"
	    " -E,--entry-start <entryStart>      Timestamp of datapoint entry in this local BDM\n"
	    "                                    must be equal to or newer than the \"entry start\"\n"
	    "                                    time (default: infinite past)\n"
	    " -e,--entry-end <entryEnd>          Timestamp of datapoint entry in this local BDM\n"
	    "                                    must be equal to or older than the \"entry end\"\n"
	    "                                    time (default: infinite future)\n"
	    " -r,--resources <Resources>         Resource name pattern of resources to retrieve.\n"
	    "                                    May contain wildcards. (default: \"*.*.*:*\")\n"
	    " -s,--server <server>               BDM server hostname (default: localhost)\n"
	    " -T,--datapoint-start <entryStart>  Timestamp of datapoint as reported by the HAB\n"
	    "                                    must be equal to or newer than the \"entry start\"\n"
	    "                                    time (default: infinite past)\n"
	    " -t,--datapoint-end <entryEnd>      Timestamp of datapoint as reported by the HAB\n"
	    "                                    must be equal to or older than the \"entry end\"\n"
	    "                                    time (default: infinite future)\n"
	    "\n"
	    "note: StartTime and EndTime are given in this format: \"YYYY-MM-DD hh:mm:ss\"\n"
	    "      YYYY is the four-digit year, for example 2008\n"
	    "      MM is the two-digit month, with 01 meaning January and 12 meaning December\n"
	    "      DD is the two-digit day-of-the-month, with 01 meaning the first day\n"
	    "      hh:mm:ss is the 24-hour time\n"
	    );
}




int main(int argc, char *argv[]) {
    char *bdm_hostname = NULL;
    uint16_t bdm_port = BDM_PORT;
    char *resource_name_pattern = "*.*.*:*";
    struct timeval datapointStart, datapointEnd;
    int entryStart = -1, entryEnd = -1;
    struct timeval * pDatapointStart = NULL;
    struct timeval * pDatapointEnd = NULL;


    memset(&datapointStart, 0, sizeof(struct timeval));
    memset(&datapointEnd, 0, sizeof(struct timeval));

    GPtrArray *hab_list;

    int i;
    int c;

    while(1) {
	static struct option long_options[] = {
	    {"help", 0, 0, '?'},
	    {"version", 0, 0, 'v'},
	    {"datapoint-start", 1, 0, 'T'},
	    {"datapoint-end", 1, 0, 't'},
	    {"entry-start", 1, 0, 'E'},
	    {"entry-stop", 1, 0, 'e'},
	    {"resources", 1, 0, 'r'},
	    {"server", 1, 0, 's'},
	    {0, 0, 0, 0} //this must be last in the list
	};

	c = getopt_long(argc, argv, "?hvT:t:E:e:", long_options, &i);
	if (c == -1) {
	    break;
	}

	switch (c) {
	case '?':
	case 'h':
	    usage();
       	    return 0;

	case 'E':
	    entryStart = str_to_int(optarg);
	    break;

	case 'e':
	    entryEnd = str_to_int(optarg);
	    break;

	case 'r':
	    resource_name_pattern = optarg;
	    break;

	case 's':
	    bdm_hostname = optarg;
	    break;

	case 'T':
	    str_to_timeval(optarg, &datapointStart);
	    pDatapointStart = &datapointStart;
	    break;

	case 't':
	    str_to_timeval(optarg, &datapointEnd);
	    pDatapointEnd = &datapointEnd;
	    break;
<commit_msg>add BDM port command line parameter to the BDM Client.<commit_after>
// Copyright (c) 2008-2009, Regents of the University of Colorado.
// This work was supported by NASA contracts NNJ05HE10G, NNC06CB40C, and
// NNC07CB47C.


#define _XOPEN_SOURCE
#define _BSD_SOURCE

#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>

#include "bdm-client.h"


extern int bdm_fd;

static int str_to_int(const char * str) {
    char * endptr;
    int i;

    i = strtol(str, &endptr, 10);
    if(*endptr != '\0') {
        printf("error parsing int string '%s': Unknown characters '%s'\n", 
            str, endptr);
        exit(1);
    }
    return i;
}

void str_to_timeval(const char *str, struct timeval *tv) {
    struct tm tm;
    char *p;
    char *old_tz;

    p = strptime(str, "%Y-%m-%d %T", &tm);
    if (p == NULL) {
        printf("error parsing time string '%s': %s\n", str, strerror(errno));
        exit(1);
    }

    if (*p == '\0') {
        tv->tv_usec = 0;
    } else {
        int r;
        unsigned int val;
        int consumed;

        if (*p != '.') {
            printf("error parsing fractional seconds from time string '%s': expected decimal point after seconds\n", str);
            exit(1);
        }
        p ++;

        if ((*p < '0') || (*p > '9')) {
            printf("error parsing fractional seconds from time string '%s': expected number after decimal point\n", str);
            exit(1);
        }

        r = sscanf(p, "%6u%n", &val, &consumed);
        // the effect of %n on the returned conversion count is ambiguous
        if ((r != 1) && (r != 2)) {
            printf("error parsing fractional seconds from time string '%s': didn't find \"%%u\"\n", str);
            exit(1);
        }
        if (consumed != strlen(p)) {
            printf("error parsing fractional seconds from time string '%s': garbage at end\n", str);
            exit(1);
        }
        if (consumed > 6) {
            printf("error parsing fractional seconds from time string '%s': number too long\n", str);
            exit(1);
        }

        tv->tv_usec = val * pow(10, 6-consumed);
    }

    old_tz = getenv("TZ");
    setenv("TZ", "UTC", 1);
    tv->tv_sec = mktime(&tm);
    if (old_tz) {
	char sanitized[128];
	snprintf(sanitized, 128, "%s", old_tz);
	setenv("TZ", sanitized, 1);
    } else {
	unsetenv("TZ");
    }
}




void usage(void) {
    fprintf(stderr,
	    "'bdm-client' reads data from the BDM\n"
	    "\n"
	    "usage: bdm-client [OPTIONS]\n"
	    "\n"
	    " -?,--help                          Show this usage information\n"
	    " -v,--version                       Show the version number\n"
	    " -E,--entry-start <entryStart>      Timestamp of datapoint entry in this local BDM\n"
	    "                                    must be equal to or newer than the \"entry start\"\n"
	    "                                    time (default: infinite past)\n"
	    " -e,--entry-end <entryEnd>          Timestamp of datapoint entry in this local BDM\n"
	    "                                    must be equal to or older than the \"entry end\"\n"
	    "                                    time (default: infinite future)\n"
	    " -p,--port <Port>                   Port number to connect on the server\n"
	    " -r,--resources <Resources>         Resource name pattern of resources to retrieve.\n"
	    "                                    May contain wildcards. (default: \"*.*.*:*\")\n"
	    " -s,--server <server>               BDM server hostname (default: localhost)\n"
	    " -T,--datapoint-start <entryStart>  Timestamp of datapoint as reported by the HAB\n"
	    "                                    must be equal to or newer than the \"entry start\"\n"
	    "                                    time (default: infinite past)\n"
	    " -t,--datapoint-end <entryEnd>      Timestamp of datapoint as reported by the HAB\n"
	    "                                    must be equal to or older than the \"entry end\"\n"
	    "                                    time (default: infinite future)\n"
	    "\n"
	    "note: StartTime and EndTime are given in this format: \"YYYY-MM-DD hh:mm:ss\"\n"
	    "      YYYY is the four-digit year, for example 2008\n"
	    "      MM is the two-digit month, with 01 meaning January and 12 meaning December\n"
	    "      DD is the two-digit day-of-the-month, with 01 meaning the first day\n"
	    "      hh:mm:ss is the 24-hour time\n"
	    );
}




int main(int argc, char *argv[]) {
    char *bdm_hostname = NULL;
    uint16_t bdm_port = BDM_PORT;
    char *resource_name_pattern = "*.*.*:*";
    struct timeval datapointStart, datapointEnd;
    int entryStart = -1, entryEnd = -1;
    struct timeval * pDatapointStart = NULL;
    struct timeval * pDatapointEnd = NULL;


    memset(&datapointStart, 0, sizeof(struct timeval));
    memset(&datapointEnd, 0, sizeof(struct timeval));

    GPtrArray *hab_list;

    int i;
    int c;

    while(1) {
	static struct option long_options[] = {
	    {"help", 0, 0, '?'},
	    {"version", 0, 0, 'v'},
	    {"datapoint-start", 1, 0, 'T'},
	    {"datapoint-end", 1, 0, 't'},
	    {"entry-start", 1, 0, 'E'},
	    {"entry-stop", 1, 0, 'e'},
	    {"port", 1, 0, 'p'},
	    {"resources", 1, 0, 'r'},
	    {"server", 1, 0, 's'},
	    {0, 0, 0, 0} //this must be last in the list
	};

	c = getopt_long(argc, argv, "?hvT:t:E:e:p:", long_options, &i);
	if (c == -1) {
	    break;
	}

	switch (c) {
	case '?':
	case 'h':
	    usage();
       	    return 0;

	case 'E':
	    entryStart = str_to_int(optarg);
	    break;

	case 'e':
	    entryEnd = str_to_int(optarg);
	    break;

	case 'p':
	    bdm_port = str_to_int(optarg);
	    break;

	case 'r':
	    resource_name_pattern = optarg;
	    break;

	case 's':
	    bdm_hostname = optarg;
	    break;

	case 'T':
	    str_to_timeval(optarg, &datapointStart);
	    pDatapointStart = &datapointStart;
	    break;

	case 't':
	    str_to_timeval(optarg, &datapointEnd);
	    pDatapointEnd = &datapointEnd;
	    break;

{'fraction_non_alphanumeric': 0.14410114711546512, 'fraction_numerical': 0.015239052164447793, 'mean_word_length': 1.8997329448895364, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5979447', 'n_tokens_mistral': 4298, 'n_tokens_neox': 3898, 'n_words': 1185}