<commit_before>/**
 * @file ccndc-main.c
 * @brief Bring up a link to another ccnd.
 *
 * A CCNx program.
 *
 * Copyright (C) 2009-2012 Palo Alto Research Center, Inc.
 *
 * This work 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 work 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.
 */

#include "ccndc.h"
#include "ccndc-log.h"
#include "ccndc-srv.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <strings.h>
#include <errno.h>

static int
read_configfile(struct ccndc_data *ccndc, const char *filename);

static void
usage(const char *progname)
{
    fprintf(stderr,
            "Usage:\n"
            "   %s [-h] [-d] [-v] [-t <lifetime>] (-f <configfile> | COMMAND)\n"
            "       -h print usage and exit\n"
            "       -d enter dynamic mode and create FIB entries based on DNS SRV records\n"
            "       -f <configfile> add or delete FIB entries based on the content of <configfile>\n"
            "       -t use value in seconds for lifetime of prefix registration\n"
            "       -v increase logging level\n"
            "\n"
            "   COMMAND can be one of following:\n"
            "       (add|del) <uri> (udp|tcp) <host> [<port> [<flags> [<mcastttl> [<mcastif>]]]])\n"
            "           to add prefix to or delete prefix from face identified by parameters\n"
            "       (add|del) <uri> face <faceid>\n"
            "           to add prefix to or delete prefix from face identified by number\n"
            "       (create|destroy) (udp|tcp) <host> [<port> [<mcastttl> [<mcastif>]]])\n"
            "           create or destroy a face identified by parameters\n"
            "       destroy face <faceid>\n"
            "           destroy face identified by number\n"
            "       srv\n"
            "           add ccnx:/ prefix to face created from parameters in SRV\n"
            "           record of a domain in DNS search list\n"
            ,
            progname);
}

char *
create_command_from_command_line(int argc, char **argv)
{
    int len = 0;
    char *out = NULL;
    int i;
    
    if (argc == 0)
        return NULL;
    
    for (i = 0; i < argc; i++) {
        len += strlen(argv[i]) + 1;
    }
    
    out = calloc(len, sizeof(char));
    if (out == 0) {
        ccndc_fatal(__LINE__, "Cannot allocate memory\n");
    }
    
    len = 0;
    for (i = 0; i < argc; i++) {
        memcpy(out+len, argv[i], strlen(argv[i]));
        len += strlen(argv[i]) + 1;
        out[len-1] = ' '; // separate everything with one space
    }
    out[len-1] = 0;
    
    return out;
}



int
main(int argc, char **argv)
{
    struct ccndc_data *ccndc;
    const char *progname = NULL;
    const char *configfile = NULL;
    int res = 1;
    int opt, disp_res;
    char *cmd = NULL;
    int dynamic = 0;
    int lifetime = -1;
    
    progname = argv[0];
    
    while ((opt = getopt(argc, argv, "hdvt:f:")) != -1) {
        switch (opt) {
            case 'f':
                configfile = optarg;
                break;
            case 't':
                lifetime = atoi(optarg);
                break;
            case 'v':
                verbose = 1;
                break;
            case 'd':
                dynamic = 1;
                break;
            case 'h':
            default:
                usage(progname);
                goto Cleanup;
        }
    }
    
    if (configfile == NULL && !dynamic && optind == argc) {
        usage(progname);
        goto Cleanup;
    }
    
    ccndc = ccndc_initialize_data();<commit_msg>Refs #100839 Check -t lifetime value for validity.<commit_after>/**
 * @file ccndc-main.c
 * @brief Bring up a link to another ccnd.
 *
 * A CCNx program.
 *
 * Copyright (C) 2009-2012 Palo Alto Research Center, Inc.
 *
 * This work 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 work 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.
 */

#include "ccndc.h"
#include "ccndc-log.h"
#include "ccndc-srv.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <strings.h>
#include <errno.h>

static int
read_configfile(struct ccndc_data *ccndc, const char *filename);

static void
usage(const char *progname)
{
    fprintf(stderr,
            "Usage:\n"
            "   %s [-h] [-d] [-v] [-t <lifetime>] (-f <configfile> | COMMAND)\n"
            "       -h print usage and exit\n"
            "       -d enter dynamic mode and create FIB entries based on DNS SRV records\n"
            "       -f <configfile> add or delete FIB entries based on the content of <configfile>\n"
            "       -t use value in seconds for lifetime of prefix registration\n"
            "       -v increase logging level\n"
            "\n"
            "   COMMAND can be one of following:\n"
            "       (add|del) <uri> (udp|tcp) <host> [<port> [<flags> [<mcastttl> [<mcastif>]]]])\n"
            "           to add prefix to or delete prefix from face identified by parameters\n"
            "       (add|del) <uri> face <faceid>\n"
            "           to add prefix to or delete prefix from face identified by number\n"
            "       (create|destroy) (udp|tcp) <host> [<port> [<mcastttl> [<mcastif>]]])\n"
            "           create or destroy a face identified by parameters\n"
            "       destroy face <faceid>\n"
            "           destroy face identified by number\n"
            "       srv\n"
            "           add ccnx:/ prefix to face created from parameters in SRV\n"
            "           record of a domain in DNS search list\n"
            ,
            progname);
}

char *
create_command_from_command_line(int argc, char **argv)
{
    int len = 0;
    char *out = NULL;
    int i;
    
    if (argc == 0)
        return NULL;
    
    for (i = 0; i < argc; i++) {
        len += strlen(argv[i]) + 1;
    }
    
    out = calloc(len, sizeof(char));
    if (out == 0) {
        ccndc_fatal(__LINE__, "Cannot allocate memory\n");
    }
    
    len = 0;
    for (i = 0; i < argc; i++) {
        memcpy(out+len, argv[i], strlen(argv[i]));
        len += strlen(argv[i]) + 1;
        out[len-1] = ' '; // separate everything with one space
    }
    out[len-1] = 0;
    
    return out;
}



int
main(int argc, char **argv)
{
    struct ccndc_data *ccndc;
    const char *progname = NULL;
    const char *configfile = NULL;
    int res = 1;
    int opt, disp_res;
    char *cmd = NULL;
    int dynamic = 0;
    int lifetime = -1;
    
    progname = argv[0];
    
    while ((opt = getopt(argc, argv, "hdvt:f:")) != -1) {
        switch (opt) {
            case 'f':
                configfile = optarg;
                break;
            case 't':
                lifetime = atoi(optarg);
                if (lifetime <= 0) {
                    usage(progname);
                    goto Cleanup;
                }
                break;
            case 'v':
                verbose = 1;
                break;
            case 'd':
                dynamic = 1;
                break;
            case 'h':
            default:
                usage(progname);
                goto Cleanup;
        }
    }
    
    if (configfile == NULL && !dynamic && optind == argc) {
        usage(progname);
        goto Cleanup;
    }
    
    ccndc = ccndc_initialize_data();
{'fraction_non_alphanumeric': 0.11425514207585305, 'fraction_numerical': 0.009868029960765664, 'mean_word_length': 1.7553226334752703, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2851207', 'n_tokens_mistral': 2675, 'n_tokens_neox': 2537, 'n_words': 888}