Пример #1
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;
}
Пример #2
0
Файл: faup.c Проект: adulau/faup
int main(int argc, char **argv)
{
	faup_handler_t *fh;
	char *strbuf=NULL;

	char *tld_file=NULL;

	faup_options_t faup_opts;
	int opt;

	int tld_pos;

	faup_options_defaults(&faup_opts);

	fh = faup_init();


//	tld_tree = faup_tld_tree_new();
//	tld_pos = get_tld_pos(tld_tree, argv[1]);
//	printf("TLD Pos:%d\n", tld_pos);

	while ((opt = getopt(argc, argv, "pld:vo:ut")) != -1) {
	  switch(opt) {
	  case 'p':
	    faup_opts.print_header = 1;
	    break;
	  case 'l':
	    faup_opts.print_line = 1;
	    break;
	  case 'd':
	    faup_opts.sep_char = optarg[0];
	    break;
	  case 'o':
	  	if (!strcmp("csv", optarg)) {
	  		faup_opts.output = FAUP_OUTPUT_CSV;
	  	} else if (!strcmp("json", optarg)) {
	  		faup_opts.output = FAUP_OUTPUT_JSON;
	  	} else {
	  		fprintf(stderr, "invalid output option '%s'!\n", optarg);
	  		exit(1);
	  	}
	  	break;
	  case 't':
	  	faup_opts.tld_tree = faup_tld_tree_new();
	  	faup_opts.tld_greater_extraction = 1;
	  	break;
	  case 'u':
	  	faup_tld_update();
	  	exit(0);
	  	break;
	  case 'v':
	    printf("faup v%s\n", faup_get_version());
	    exit(0);
	    break;
	  default:
	    print_help(argv);
	    exit(1);
	  }
	}

	if (isatty(fileno(stdin))) {
		if (argc < 2) {
			print_help(argv);
			exit(1);
		}
		if (faup_opts.print_header) {
		        print_header(faup_opts.print_header, faup_opts.sep_char);
		}

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

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

		switch(faup_opts.output) {
			case FAUP_OUTPUT_JSON:
				faup_output_json(fh, stdout);
				break;
			default:
				if (faup_opts.print_line) {
		 			printf("0%c", faup_opts.sep_char);
				}
				faup_output_csv(fh, faup_opts.sep_char, stdout);
				printf("\n");
				break;			
		}
	} else {       	/* We read from stdin */
		long line_nb = 1;
		if (faup_opts.print_header) {
		        print_header(faup_opts.print_line, faup_opts.sep_char);
		}
		while (!feof(stdin)) {
			strbuf = readline(stdin);
			if (!strbuf) {
				break;
			}
			if (strbuf[0] == '\0') {
				break;
			}

			faup_decode(fh, strbuf, strlen(strbuf), &faup_opts);		
			switch(faup_opts.output) {
				case FAUP_OUTPUT_JSON:
					faup_output_json(fh, stdout);
					break;
				default:
					if (faup_opts.print_line) {
				        printf("%ld%c", line_nb, faup_opts.sep_char);
					}
					faup_output_csv(fh, ',', stdout);
					printf("\n");
					break;
			}	

			free(strbuf);
			line_nb++;
		}
	}

	faup_terminate(fh);

	return 0;
}