Example #1
0
void faup_options_free(faup_options_t *opts)
{
	int i;
	if (opts) {
		faup_options_disable_tld_above_one(opts);		
	}

	// Clear modules arguments
	for (i = 0; i < opts->modules_argc; i++) {
		free(opts->modules_argv[i]);
	}

	free(opts);
}
Example #2
0
int main(int argc, char **argv)
{
    faup_handler_t *fh;
    int ret = 0;

    char *tld_file=NULL;

    faup_options_t *faup_opts;
    int opt;
    int skip_file_check = 0;

    int tld_pos;

    bool has_module = false;


    faup_opts = faup_options_new();
    if (!faup_opts) {
        fprintf(stderr, "Error: cannot allocate faup options!\n" );
        return -1;
    }

    if ((argc > 1) && (!strcmp(argv[1],"$"))) {
        // Handle some specific shell operations
        return faup_handle_shell(argc, argv);
    }

    while ((opt = getopt(argc, argv, "apld:vo:utf:m:")) != -1) {
        switch(opt) {
        case 'a':
            skip_file_check = 1;
            break;
        case 'd':
            if (optarg) {
                faup_opts->sep_char = optarg[0];
            }
            break;
        case 'p':
            faup_opts->print_header = 1;
            break;
        case 'l':
            faup_opts->fields |= FAUP_URL_FIELD_LINE;
            faup_opts->print_line = 1;
            break;
        case 'm':
            for (optind--; optind < argc - 1;) {
                // In case we parse the next option, we shall stop!
                if (argv[optind][0] == '-') {
                    break;
                }

                optind++;

                if (!has_module) {
                    // This is our first argument
                    // we push our first arg: 'optarg'
                    faup_opts->modules_argv = realloc(faup_opts->modules_argv, faup_opts->modules_argc + 1);
                    faup_opts->modules_argv[faup_opts->modules_argc] = strdup(optarg);

                    faup_opts->modules_argc++;
                    has_module = 1;
                }


                // In case we parse the next option, we shall stop!
                // In case the next argument is the last, that means this is the url, not the module
                if ((argv[optind][0] == '-') | (optind == argc - 1)) {
                    break;
                }

                faup_opts->modules_argv = realloc(faup_opts->modules_argv, faup_opts->modules_argc + 1);
                faup_opts->modules_argv[faup_opts->modules_argc] = strdup(argv[optind]);
                faup_opts->modules_argc++;
            }

            if (has_module) {
                faup_opts->exec_modules = FAUP_MODULES_EXECARG;
            } else {
                faup_opts->exec_modules = FAUP_MODULES_NOEXEC;
            }
            break;
        case 'f':
            faup_opts->fields = faup_options_get_field_from_name(optarg);
            break;
        case 'o':
            faup_opts->output = faup_options_get_output_from_name(optarg);

            if (faup_opts->output == FAUP_OUTPUT_NONE) {
                fprintf(stderr, "invalid output option '%s'!\n", optarg);
                faup_options_free(faup_opts);
                exit(1);
            }

            break;
        case 't':
            faup_options_disable_tld_above_one(faup_opts);
            break;
        case 'u':
            faup_tld_update();
            faup_options_free(faup_opts);
            exit(0);
            break;
        case 'v':
            printf("faup v%s\n", faup_get_version());
            faup_tld_datadir_print();
            faup_options_free(faup_opts);
            exit(0);
            break;
        default:
            print_help(argv);
            faup_options_free(faup_opts);
            exit(1);
        }
    }

    fh = faup_init(faup_opts);

//	printf("Lart arg:'%s'\n", argv[optind+1]);


    if (isatty(fileno(stdin))) {
        faup_opts->input_source = FAUP_INPUT_SOURCE_ARGUMENT;

        if (argc < 2) {
            print_help(argv);
            faup_options_free(faup_opts);
            faup_terminate(fh);
            exit(1);
        }

        if (!argv[optind]) {
            faup_options_free(faup_opts);
            exit(1);
        }

        if (!skip_file_check) {
            if (strlen(argv[optind]) < FAUP_MAXPATHLEN) {
                FILE *fp = fopen(argv[optind], "r");
                if (fp) {
                    faup_opts->input_source = FAUP_INPUT_SOURCE_FILE;
                    ret = run_from_stream(fh, fp);
                    fclose(fp);
                    goto terminate;
                }
            }
        }

        faup_decode(fh, argv[optind], strlen(argv[optind]));

        faup_output(fh, stdout);
    } else {       	/* We read from stdin */
        faup_opts->input_source = FAUP_INPUT_SOURCE_PIPE;
        run_from_stream(fh, stdin);
    }

terminate:
    faup_options_free(faup_opts);
    faup_terminate(fh);

    return ret;
}