Implement maxSideBuffering.
[polipo.git] / main.c
blob186cdb93cff1a432f8860ce9c648982de458f391
1 /*
2 Copyright (c) 2003-2006 by Juliusz Chroboczek
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
23 #include "polipo.h"
25 AtomPtr configFile = NULL;
26 AtomPtr pidFile = NULL;
27 int daemonise = 0;
29 static void
30 usage(char *argv0)
32 fprintf(stderr,
33 "%s [ -h ] [ -v ] [ -x ] [ -c filename ] [ -- ] [ var=val... ]\n",
34 argv0);
35 fprintf(stderr, " -h: display this message.\n");
36 fprintf(stderr, " -v: display the list of configuration variables.\n");
37 fprintf(stderr, " -x: perform expiry on the disk cache.\n");
38 fprintf(stderr, " -c: specify the configuration file to use.\n");
41 int
42 main(int argc, char **argv)
44 FdEventHandlerPtr listener;
45 int i;
46 int rc;
47 int expire = 0, printConfig = 0;
49 initAtoms();
50 CONFIG_VARIABLE(daemonise, CONFIG_BOOLEAN, "Run as a daemon");
51 CONFIG_VARIABLE(pidFile, CONFIG_ATOM, "File with pid of running daemon.");
53 preinitChunks();
54 preinitLog();
55 preinitObject();
56 preinitIo();
57 preinitDns();
58 preinitServer();
59 preinitHttp();
60 preinitDiskcache();
61 preinitLocal();
62 preinitForbidden();
63 preinitSocks();
65 i = 1;
66 while(i < argc) {
67 if(argv[i][0] != '-')
68 break;
69 if(strcmp(argv[i], "--") == 0) {
70 i++;
71 break;
72 } else if(strcmp(argv[i], "-h") == 0) {
73 usage(argv[0]);
74 exit(0);
75 } else if(strcmp(argv[i], "-v") == 0) {
76 printConfig = 1;
77 i++;
78 } else if(strcmp(argv[i], "-x") == 0) {
79 expire = 1;
80 i++;
81 } else if(strcmp(argv[i], "-c") == 0) {
82 i++;
83 if(i >= argc) {
84 usage(argv[0]);
85 exit(1);
87 if(configFile)
88 releaseAtom(configFile);
89 configFile = internAtom(argv[i]);
90 i++;
91 } else {
92 usage(argv[0]);
93 exit(1);
97 if(configFile)
98 configFile = expandTilde(configFile);
100 if(configFile == NULL) {
101 configFile = expandTilde(internAtom("~/.polipo"));
102 if(configFile)
103 if(access(configFile->string, F_OK) < 0) {
104 releaseAtom(configFile);
105 configFile = NULL;
109 if(configFile == NULL) {
110 if(access("/etc/polipo/config", F_OK) >= 0)
111 configFile = internAtom("/etc/polipo/config");
112 if(configFile && access(configFile->string, F_OK) < 0) {
113 releaseAtom(configFile);
114 configFile = NULL;
118 rc = parseConfigFile(configFile);
119 if(rc < 0)
120 exit(1);
122 while(i < argc) {
123 rc = parseConfigLine(argv[i], "command line", 0, 0);
124 if(rc < 0)
125 exit(1);
126 i++;
129 initChunks();
130 initLog();
131 initObject();
132 if(!expire && !printConfig)
133 initEvents();
134 initIo();
135 initDns();
136 initHttp();
137 initServer();
138 initDiskcache();
139 initForbidden();
140 initSocks();
142 if(printConfig) {
143 printConfigVariables(stdout, 0);
144 exit(0);
147 if(expire) {
148 expireDiskObjects();
149 exit(0);
152 if(daemonise)
153 do_daemonise(logFile == NULL || logFile->length == 0);
155 if(pidFile)
156 writePid(pidFile->string);
158 listener = create_listener(proxyAddress->string,
159 proxyPort, httpAccept, NULL);
160 if(!listener) {
161 if(pidFile) unlink(pidFile->string);
162 exit(1);
165 eventLoop();
167 if(pidFile) unlink(pidFile->string);
168 return 0;