2020-08-14 22:04:26 +00:00
|
|
|
/*
|
2020-09-08 00:58:40 +00:00
|
|
|
* Copyright (c) 2020, yzrh <yzrh@noema.org>
|
2020-08-14 22:04:26 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include "extern.h"
|
|
|
|
#include "version.h"
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv, char **envp)
|
|
|
|
{
|
|
|
|
cnki_t *param = NULL;
|
|
|
|
|
|
|
|
if (cnki_create(¶m) != 0) {
|
|
|
|
fprintf(stderr, "%s: %s\n", argv[0], strerror(errno));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"output", required_argument, 0, 'o'},
|
|
|
|
{"buffer", required_argument, 0, 'b'},
|
|
|
|
{"verbose", no_argument, 0, 'v'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
int option_index = 0;
|
|
|
|
|
|
|
|
c = getopt_long(argc, argv, "o:b:v",
|
|
|
|
long_options, &option_index);
|
|
|
|
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'o':
|
|
|
|
if ((param->fp_o = fopen(optarg, "w")) == NULL) {
|
|
|
|
fprintf(stderr, "%s: %s\n", argv[0],
|
|
|
|
strerror(errno));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
param->size_buf = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
param->stat += 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc - optind == 1) {
|
|
|
|
if (param->fp_o == NULL) {
|
|
|
|
if (param->stat == 0) {
|
|
|
|
param->fp_o = stdout;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s: --verbose ", argv[0]);
|
|
|
|
fprintf(stderr, "must not be set ");
|
|
|
|
fprintf(stderr, "when using stdout\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((param->fp_i = fopen(argv[optind], "r")) == NULL) {
|
|
|
|
fprintf(stderr, "%s: %s\n", argv[0],
|
|
|
|
strerror(errno));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-12-29 02:10:17 +00:00
|
|
|
if (param->stat > 0)
|
|
|
|
printf("Melon " VERSION "." RELEASE "." PATCH EXTRA "\n"
|
|
|
|
"Copyright (c) 2020, yzrh <yzrh@noema.org>\n\n");
|
|
|
|
|
2020-08-14 22:04:26 +00:00
|
|
|
cnki_info(¶m);
|
|
|
|
|
2020-12-30 03:09:00 +00:00
|
|
|
if (strncmp(param->file_stat->type, "%PDF", 4) == 0) {
|
2020-08-14 22:04:26 +00:00
|
|
|
if (cnki_pdf(¶m) != 0) {
|
|
|
|
fprintf(stderr, "%s: %s\n", argv[0],
|
|
|
|
strerror(errno));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2020-12-30 03:09:00 +00:00
|
|
|
} else if (strncmp(param->file_stat->type, "CAJ", 3) == 0) {
|
2020-08-14 22:04:26 +00:00
|
|
|
if (cnki_caj(¶m) != 0) {
|
|
|
|
fprintf(stderr, "%s: %s\n", argv[0],
|
|
|
|
strerror(errno));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2020-12-30 03:09:00 +00:00
|
|
|
} else if (strncmp(param->file_stat->type, "HN", 2) == 0) {
|
2020-12-29 02:10:17 +00:00
|
|
|
if (cnki_hn(¶m) != 0) {
|
2020-08-14 22:04:26 +00:00
|
|
|
fprintf(stderr, "%s: %s\n", argv[0],
|
|
|
|
strerror(errno));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2020-12-30 03:09:00 +00:00
|
|
|
} else if (strncmp(param->file_stat->type, "KDH ", 4) == 0) {
|
2020-08-14 22:04:26 +00:00
|
|
|
if (cnki_kdh(¶m) != 0) {
|
|
|
|
fprintf(stderr, "%s: %s\n", argv[0],
|
|
|
|
strerror(errno));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s: %s\n", argv[0],
|
|
|
|
"Invalid file");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(param->fp_i);
|
|
|
|
fclose(param->fp_o);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Usage: %s ", argv[0]);
|
|
|
|
fprintf(stderr, "[--output --buffer --verbose] file\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
cnki_destroy(¶m);
|
|
|
|
}
|