1/* Command line parsing.
2   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3   2004, 2005, 2006, 2007, 2008, 2009 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. */
27enum
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
42static 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. */
54static int
55get_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      return WGET_EXIT_IO_FAIL;
63    case NOCONERROR: case HOSTERR: case CONSOCKERR: case CONERROR:
64    case CONSSLERR: case CONIMPOSSIBLE: case FTPRERR: case FTPINVPASV:
65    case READERR: case TRYLIMEXC:
66      return WGET_EXIT_NETWORK_FAIL;
67    case VERIFCERTERR:
68      return WGET_EXIT_SSL_AUTH_FAIL;
69    case FTPLOGINC: case FTPLOGREFUSED: case AUTHFAILED:
70      return WGET_EXIT_SERVER_AUTH_FAIL;
71    case HEOF: case HERR:
72      return WGET_EXIT_PROTOCOL_ERROR;
73    case WRONGCODE: case FTPPORTERR: case FTPSYSERR:
74    case FTPNSFOD: case FTPUNKNOWNTYPE: case FTPSRVERR:
75    case FTPRETRINT: case FTPRESTFAIL: case FTPNOPASV:
76    case CONTNOTSUPPORTED: case RANGEERR: case RETRBADPATTERN:
77    case PROXERR:
78      return WGET_EXIT_SERVER_ERROR;
79    case URLERROR: case QUOTEXC: case SSLINITFAILED:
80    default:
81      return WGET_EXIT_UNKNOWN;
82    }
83}
84
85/* inform_exit_status
86 *
87 * Ensure that Wget's exit status will reflect the problem indicated
88 * by ERR, unless the exit status has already been set to reflect a more
89 * important problem. */
90void
91inform_exit_status (uerr_t err)
92{
93  int new_status = get_status_for_err (err);
94
95  if (new_status != WGET_EXIT_SUCCESS
96      && (final_exit_status == WGET_EXIT_SUCCESS
97          || new_status < final_exit_status))
98    {
99      final_exit_status = new_status;
100    }
101}
102
103int
104get_exit_status (void)
105{
106  return
107    (final_exit_status == WGET_EXIT_UNKNOWN)
108      ? 1
109      : final_exit_status;
110}
111
112