1/*
2 * Copyright (C) 2002 Cyrus Patel <cyp@fb14.uni-mainz.de>
3 *           (C) 2007 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License 2.1 as published by the Free Software Foundation.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20/* ParseFTPList() parses lines from an FTP LIST command.
21**
22** Written July 2002 by Cyrus Patel <cyp@fb14.uni-mainz.de>
23** with acknowledgements to squid, lynx, wget and ftpmirror.
24**
25** Arguments:
26**   'line':       line of FTP data connection output. The line is assumed
27**                 to end at the first '\0' or '\n' or '\r\n'.
28**   'state':      a structure used internally to track state between
29**                 lines. Needs to be bzero()'d at LIST begin.
30**   'result':     where ParseFTPList will store the results of the parse
31**                 if 'line' is not a comment and is not junk.
32**
33** Returns one of the following:
34**    'd' - LIST line is a directory entry ('result' is valid)
35**    'f' - LIST line is a file's entry ('result' is valid)
36**    'l' - LIST line is a symlink's entry ('result' is valid)
37**    '?' - LIST line is junk. (cwd, non-file/dir/link, etc)
38**    '"' - its not a LIST line (its a "comment")
39**
40** It may be advisable to let the end-user see "comments" (particularly when
41** the listing results in ONLY such lines) because such a listing may be:
42** - an unknown LIST format (NLST or "custom" format for example)
43** - an error msg (EPERM,ENOENT,ENFILE,EMFILE,ENOTDIR,ENOTBLK,EEXDEV etc).
44** - an empty directory and the 'comment' is a "total 0" line or similar.
45**   (warning: a "total 0" can also mean the total size is unknown).
46**
47** ParseFTPList() supports all known FTP LISTing formats:
48** - '/bin/ls -l' and all variants (including Hellsoft FTP for NetWare);
49** - EPLF (Easily Parsable List Format);
50** - Windows NT's default "DOS-dirstyle";
51** - OS/2 basic server format LIST format;
52** - VMS (MultiNet, UCX, and CMU) LIST format (including multi-line format);
53** - IBM VM/CMS, VM/ESA LIST format (two known variants);
54** - SuperTCP FTP Server for Win16 LIST format;
55** - NetManage Chameleon (NEWT) for Win16 LIST format;
56** - '/bin/dls' (two known variants, plus multi-line) LIST format;
57** If there are others, then I'd like to hear about them (send me a sample).
58**
59** NLSTings are not supported explicitely because they cannot be machine
60** parsed consistantly: NLSTings do not have unique characteristics - even
61** the assumption that there won't be whitespace on the line does not hold
62** because some nlistings have more than one filename per line and/or
63** may have filenames that have spaces in them. Moreover, distinguishing
64** between an error message and an NLST line would require ParseList() to
65** recognize all the possible strerror() messages in the world.
66*/
67
68// This was originally Mozilla code, titled ParseFTPList.h
69// Original version of this file can currently be found at: http://mxr.mozilla.org/mozilla1.8/source/netwerk/streamconv/converters/ParseFTPList.h
70
71#ifndef FTPDirectoryParser_h
72#define FTPDirectoryParser_h
73
74#include <wtf/text/WTFString.h>
75
76#include <time.h>
77
78#define SUPPORT_LSL  /* Support for /bin/ls -l and dozens of variations therof */
79#define SUPPORT_DLS  /* Support for /bin/dls format (very, Very, VERY rare) */
80#define SUPPORT_EPLF /* Support for Extraordinarily Pathetic List Format */
81#define SUPPORT_DOS  /* Support for WinNT server in 'site dirstyle' dos */
82#define SUPPORT_VMS  /* Support for VMS (all: MultiNet, UCX, CMU-IP) */
83#define SUPPORT_CMS  /* Support for IBM VM/CMS,VM/ESA (z/VM and LISTING forms) */
84#define SUPPORT_OS2  /* Support for IBM TCP/IP for OS/2 - FTP Server */
85#define SUPPORT_W16  /* Support for win16 hosts: SuperTCP or NetManage Chameleon */
86
87namespace WebCore {
88
89typedef struct tm FTPTime;
90
91struct ListState {
92    ListState()
93        : now(0)
94        , listStyle(0)
95        , parsedOne(false)
96        , carryBufferLength(0)
97        , numLines(0)
98    {
99        memset(&nowFTPTime, 0, sizeof(FTPTime));
100    }
101
102    double      now;               /* needed for year determination */
103    FTPTime     nowFTPTime;
104    char        listStyle;         /* LISTing style */
105    bool        parsedOne;         /* returned anything yet? */
106    char        carryBuffer[84];   /* for VMS multiline */
107    int         carryBufferLength; /* length of name in carry_buf */
108    int64_t     numLines;          /* number of lines seen */
109};
110
111enum FTPEntryType {
112    FTPDirectoryEntry,
113    FTPFileEntry,
114    FTPLinkEntry,
115    FTPMiscEntry,
116    FTPJunkEntry
117};
118
119struct ListResult
120{
121    ListResult()
122    {
123        clear();
124    }
125
126    void clear()
127    {
128        valid = false;
129        type = FTPJunkEntry;
130        filename = 0;
131        filenameLength = 0;
132        linkname = 0;
133        linknameLength = 0;
134        fileSize.truncate(0);
135        caseSensitive = false;
136        memset(&modifiedTime, 0, sizeof(FTPTime));
137    }
138
139    bool valid;
140    FTPEntryType type;
141
142    const char* filename;
143    uint32_t filenameLength;
144
145    const char* linkname;
146    uint32_t linknameLength;
147
148    String fileSize;
149    FTPTime modifiedTime;
150    bool caseSensitive; // file system is definitely case insensitive
151};
152
153FTPEntryType parseOneFTPLine(const char* inputLine, ListState&, ListResult&);
154
155} // namespace WebCore
156
157#endif // FTPDirectoryParser_h
158