]> sjero.net Git - wget/blob - src/exits.c
23a66ac1fba86f696dffa0ae1a7ef09c83ca7c5f
[wget] / src / exits.c
1 /* Command line parsing.
2    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3    2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
5    This file is part of GNU Wget.
6
7    GNU Wget is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    GNU Wget is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Wget.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "wget.h"
22 #include "exits.h"
23
24 /* Final exit code possibilities. Exit codes 1 and 2 are reserved
25  * for situations that lead to direct exits from Wget, not using the
26  * value of final_exit_status. */
27 enum
28   {
29     WGET_EXIT_SUCCESS = 0,
30
31     WGET_EXIT_MINIMUM = 3,
32     WGET_EXIT_IO_FAIL = WGET_EXIT_MINIMUM,
33     WGET_EXIT_NETWORK_FAIL = 4,
34     WGET_EXIT_SSL_AUTH_FAIL = 5,
35     WGET_EXIT_SERVER_AUTH_FAIL = 6,
36     WGET_EXIT_PROTOCOL_ERROR = 7,
37     WGET_EXIT_SERVER_ERROR = 8,
38
39     WGET_EXIT_UNKNOWN
40   };
41
42 static int final_exit_status = WGET_EXIT_SUCCESS;
43
44 /* XXX: I don't like that newly-added uerr_t codes will doubtless fall
45    through the craccks, or the fact that we seem to have way more
46    codes than we know what to do with. Need to go through and sort
47    through the truly essential codes, and merge the rest with
48    those. Quite a few are never even used!
49
50    Quite a few of the codes below would have no business being
51    returned to retrieve_url's caller, but since it's very difficult to
52    determine which do and which don't, I grab virtually all of them to
53    be safe. */
54 static int
55 get_status_for_err (uerr_t err)
56 {
57   switch (err)
58     {
59     case RETROK:
60       return WGET_EXIT_SUCCESS;
61     case FOPENERR: case FOPEN_EXCL_ERR: case FWRITEERR: case WRITEFAILED:
62     case UNLINKERR:
63       return WGET_EXIT_IO_FAIL;
64     case NOCONERROR: case HOSTERR: case CONSOCKERR: case CONERROR:
65     case CONSSLERR: case CONIMPOSSIBLE: case FTPRERR: case FTPINVPASV:
66     case READERR: case TRYLIMEXC:
67       return WGET_EXIT_NETWORK_FAIL;
68     case VERIFCERTERR:
69       return WGET_EXIT_SSL_AUTH_FAIL;
70     case FTPLOGINC: case FTPLOGREFUSED: case AUTHFAILED:
71       return WGET_EXIT_SERVER_AUTH_FAIL;
72     case HEOF: case HERR:
73       return WGET_EXIT_PROTOCOL_ERROR;
74     case WRONGCODE: case FTPPORTERR: case FTPSYSERR:
75     case FTPNSFOD: case FTPUNKNOWNTYPE: case FTPSRVERR:
76     case FTPRETRINT: case FTPRESTFAIL: case FTPNOPASV:
77     case CONTNOTSUPPORTED: case RANGEERR: case RETRBADPATTERN:
78     case PROXERR:
79       return WGET_EXIT_SERVER_ERROR;
80     case URLERROR: case QUOTEXC: case SSLINITFAILED:
81     default:
82       return WGET_EXIT_UNKNOWN;
83     }
84 }
85
86 /* inform_exit_status
87  *
88  * Ensure that Wget's exit status will reflect the problem indicated
89  * by ERR, unless the exit status has already been set to reflect a more
90  * important problem. */
91 void
92 inform_exit_status (uerr_t err)
93 {
94   int new_status = get_status_for_err (err);
95
96   if (new_status != WGET_EXIT_SUCCESS
97       && (final_exit_status == WGET_EXIT_SUCCESS
98           || new_status < final_exit_status))
99     {
100       final_exit_status = new_status;
101     }
102 }
103
104 int
105 get_exit_status (void)
106 {
107   return
108     (final_exit_status == WGET_EXIT_UNKNOWN)
109       ? 1
110       : final_exit_status;
111 }
112