plptools
Loading...
Searching...
No Matches
main.cc
Go to the documentation of this file.
1/*
2 * This file is part of plptools.
3 *
4 * Copyright (C) 1999 Philip Proudman <philip.proudman@btinternet.com>
5 * Copyright (C) 1999-2002 Fritz Elfert <felfert@to.com>
6 *
7 * This program 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 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program 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 along
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
19 *
20 */
21#include "config.h"
22
23#include <cliutils.h>
24#include <memory>
25#include <rfsv.h>
26#include <rfsvfactory.h>
27#include <rpcs.h>
28#include <rpcsfactory.h>
29#include <rclip.h>
30#include <plpintl.h>
31#include <tcpsocket.h>
32#include <bufferstore.h>
33
34#include <iostream>
35#include <vector>
36
37#include <stdlib.h>
38#include <stdio.h>
39
40#include "ftp.h"
41
42#ifndef _GNU_SOURCE
43#define _GNU_SOURCE
44#endif
45#include <getopt.h>
46
47using namespace std;
48
49static void
51{
52 cout << _(
53 "Usage: plpftp [OPTIONS]... [FTPCOMMAND]\n"
54 "\n"
55 "If FTPCOMMAND is given, connect; run FTPCOMMAND and\n"
56 "terminate afterwards. If no FTPCOMMAND is given, start up\n"
57 "in interactive mode. For help on supported FTPCOMMANDs,\n"
58 "use `?' or `help' as FTPCOMMAND.\n"
59 "\n"
60 "Supported options:\n"
61 "\n"
62 " -h, --help Display this text.\n"
63 " -V, --version Print version and exit.\n"
64 " -p, --port=[HOST:]PORT Connect to port PORT on host HOST.\n"
65 " Default for HOST is 127.0.0.1\n"
66 " Default for PORT is "
67 ) << DPORT << "\n\n";
68}
69
70static void usage() {
71 cerr << _("Try `plpftp --help' for more information") << endl;
72}
73
74static struct option opts[] = {
75 {"help", no_argument, nullptr, 'h'},
76 {"version", no_argument, nullptr, 'V'},
77 {"port", required_argument, nullptr, 'p'},
78 {NULL, 0, nullptr, 0 }
79};
80
81void ftpHeader() {
82 cout << _("PLPFTP Version ") << VERSION << endl;
83 cout << _("FTP like interface started. Type \"?\" for help.") << endl;
84 cout << endl;
85}
86
87int main(int argc, char **argv) {
88 FTP ftp;
89 string host = "127.0.0.1";
91
92 setlocale (LC_ALL, "");
93 textdomain(PACKAGE);
94
95 while (1) {
96 int c = getopt_long(argc, argv, "hVp:", opts, NULL);
97 if (c == -1)
98 break;
99 switch (c) {
100 case '?':
101 usage();
102 return -1;
103 case 'V':
104 cout << _("plpftp Version ") << VERSION << endl;
105 return 0;
106 case 'h':
107 help();
108 return 0;
109 case 'p':
110 if (!cli_utils::parse_port(optarg, &host, &port)) {
111 cout << _("Invalid port definition.") << endl;
112 return 1;
113 }
114 break;
115 }
116 }
117 if (optind == argc) {
118 ftpHeader();
119 }
120
122 auto rfsv = std::unique_ptr<RFSV>(RFSV::connect(host, port, &error));
123 if (!rfsv) {
124 cerr << "plpftp: " << error << endl;
125 return EXIT_FAILURE;
126 }
127 auto rpcs = std::unique_ptr<RPCS>(RPCS::connect(host, port, &error));
128 if (!rpcs) {
129 cerr << "plpftp: " << error << endl;
130 return EXIT_FAILURE;
131 }
132 auto clipboard = std::unique_ptr<rclip>(rclip::connect(host, port, &error));
133 if (!clipboard) {
134 cerr << "plpftp: " << error << endl;
135 return EXIT_FAILURE;
136 }
137 vector<char *> args(argv + optind, argv + argc);
138 return ftp.session(*rfsv, *rpcs, *clipboard, args);
139}
Wrapper class featuring range-checking and string representation of enumerated values.
Definition: Enum.h:135
Definition: ftp.h:36
int session(RFSV &rfsv, RPCS &rpcs, rclip &clipboard, std::vector< char * > argv)
Definition: ftp.cc:538
static RFSV * connect(const std::string &host, int port, Enum< ConnectionError > *error)
Definition: rfsv.cc:115
static RPCS * connect(const std::string &host, int port, Enum< ConnectionError > *error)
Definition: rpcs.cc:93
static rclip * connect(const std::string &host, int port, Enum< ConnectionError > *error=nullptr)
Definition: rclip.cc:33
bool parse_port(const std::string &arg, std::string *host, int *port)
Definition: cliutils.cc:53
int lookup_default_port()
Definition: cliutils.cc:44
Definition: doctest.h:522
int main(int argc, char **argv)
Definition: main.cc:87
static void usage()
Definition: main.cc:70
static struct option opts[]
Definition: main.cc:74
void ftpHeader()
Definition: main.cc:81
static void help()
Definition: main.cc:50
#define _(String)
Definition: plpintl.h:34
#define textdomain(Domain)
Definition: plpintl.h:36
static void error(int line)
Definition: sismain.cpp:44