Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add option parsing

Note that the options are not actually implemented yet.
  • Loading branch information...
commit 1af2f7e30c31d9dc977030c01b70b2400e0c386b 1 parent 84bcee9
@rosslagerwall authored
Showing with 170 additions and 1 deletion.
  1. +2 −1  Makefile.am
  2. +5 −0 src/main.c
  3. +125 −0 src/options.c
  4. +38 −0 src/options.h
View
3  Makefile.am
@@ -1,7 +1,8 @@
bin_PROGRAMS=yummycacher
yummycacher_SOURCES=src/main.c src/pserv.c src/pserv.h src/dns.c src/dns.h \
src/protoshttp.c src/protoshttp.h src/pclient.c src/pclient.h \
- src/protochttp.c src/protochttp.h src/cxmap.h src/cxmap.c
+ src/protochttp.c src/protochttp.h src/cxmap.h src/cxmap.c \
+ src/options.c src/options.h
AM_CPPFLAGS = $(DEPS_CFLAGS) $(DEPS2_CFLAGS)
yummycacher_LDADD = -levent_core -levent_extra $(DEPS2_LIBS)
View
5 src/main.c
@@ -18,6 +18,7 @@
#include "cxmap.h"
#include "pserv.h"
#include "dns.h"
+#include "options.h"
#include <glib.h>
#include <event2/event.h>
@@ -82,6 +83,10 @@ start_loop(void)
}
int main(int argc, char **argv) {
+ if (!parse_options(argc, argv) || y_options.help) {
+ print_usage(argv[0]);
+ return 1;
+ }
cxmap_init();
return start_loop();
}
View
125 src/options.c
@@ -0,0 +1,125 @@
+/*
+ Copyright (C) 2012 The yummycacher Authors
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "options.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <getopt.h>
+
+struct y_options y_options;
+
+int
+parse_options(int argc, char **argv)
+{
+ int c;
+
+ /* Set defaults */
+ y_options.root = ".";
+ y_options.url = "";
+ y_options.port = "8080";
+ y_options.log = "-";
+ y_options.foreground = 0;
+ y_options.help = 0;
+
+ for (;;) {
+ int option_index = 0;
+ static struct option long_options[] = {
+ {"root", required_argument, NULL, 0},
+ {"log", required_argument, NULL, 0},
+ {"port", required_argument, NULL, 0},
+ {"url", required_argument, NULL, 0},
+ {"foreground", no_argument, NULL, 0},
+ {"help", no_argument, NULL, 0},
+ {0, 0, 0, 0}
+ };
+
+ c = getopt_long(argc, argv, "r::u:p::l::fh", long_options,
+ &option_index);
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 0:
+ switch (option_index) {
+ case 0:
+ y_options.root = optarg;
+ break;
+ case 1:
+ y_options.log = optarg;
+ break;
+ case 2:
+ y_options.port = optarg;
+ break;
+ case 3:
+ y_options.url = optarg;
+ break;
+ case 4:
+ y_options.foreground = 1;
+ break;
+ case 5:
+ y_options.help = 1;
+ default:
+ break;
+ }
+ break;
+ case 'r':
+ y_options.root = optarg;
+ break;
+ case 'l':
+ y_options.log = optarg;
+ break;
+ case 'p':
+ y_options.port = optarg;
+ break;
+ case 'u':
+ y_options.url = optarg;
+ break;
+ case 'f':
+ y_options.foreground = 1;
+ break;
+ case 'h':
+ y_options.help = 1;
+ break;
+ case '?':
+ default:
+ return 0;
+ }
+ }
+
+ /* Check for required options */
+ if (!strcmp(y_options.url, ""))
+ return 0;
+
+ return 1;
+}
+
+void
+print_usage(const char *program)
+{
+ fprintf(stderr, "usage: %s [options] -u <url>\n", program);
+ fprintf(stderr, "\nOptions:\n");
+ fprintf(stderr, " -h, --help this help text\n");
+ fprintf(stderr, " -f, --foreground don't daemonize\n");
+ fprintf(stderr, " -r, --root <root> use <root> to store cached content"
+ " (default: .)\n");
+ fprintf(stderr, " -l, --log <logfile> log to <logfile> (default -,"
+ " stdin)\n");
+ fprintf(stderr, " -p, --port <port> listen on <port> (default 8080)\n");
+ fprintf(stderr, " -u, --url <url> mirror content for <url>\n");
+}
View
38 src/options.h
@@ -0,0 +1,38 @@
+/*
+ Copyright (C) 2012 The yummycacher Authors
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef OPTIONS_H
+#define OPTIONS_H
+
+struct y_options {
+ char *root;
+ char *url;
+ char *port;
+ char *log;
+ int foreground;
+ int help;
+};
+
+extern struct y_options y_options;
+
+int
+parse_options(int argc, char **argv);
+
+void
+print_usage(const char *program);
+
+#endif
Please sign in to comment.
Something went wrong with that request. Please try again.