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 <iostream>
24#include <memory>
25#include <vector>
26
27#include <bufferstore.h>
28#include <cliutils.h>
29#include <device.h>
30#include <deviceendpoint.h>
31#include <deviceconfiguration.h>
32#include <plpintl.h>
33#include <rclip.h>
34#include <rfsv.h>
35#include <rfsvfactory.h>
36#include <rpcs.h>
37#include <rpcsfactory.h>
38#include <tcpsocket.h>
39
40#include <stdlib.h>
41#include <stdio.h>
42
43#include "ftp.h"
44
45#ifndef _GNU_SOURCE
46#define _GNU_SOURCE
47#endif
48#include <getopt.h>
49
50using namespace std;
51
52static void
54{
55 cout << _(
56 "Usage: plpftp [OPTIONS]... [FTPCOMMAND]\n"
57 "\n"
58 "If FTPCOMMAND is given, connect; run FTPCOMMAND and\n"
59 "terminate afterwards. If no FTPCOMMAND is given, start up\n"
60 "in interactive mode. For help on supported FTPCOMMANDs,\n"
61 "use `?' or `help' as FTPCOMMAND.\n"
62 "\n"
63 "Supported options:\n"
64 "\n"
65 " -h, --help Display this text.\n"
66 " -V, --version Print version and exit.\n"
67 " -p, --port=[HOST:]PORT Connect to port PORT on host HOST.\n"
68 " Default for HOST is 127.0.0.1\n"
69 " Default for PORT is "
70 ) << DPORT << "\n\n";
71}
72
73static void usage() {
74 cerr << _("Try `plpftp --help' for more information") << endl;
75}
76
77static struct option opts[] = {
78 {"help", no_argument, nullptr, 'h'},
79 {"version", no_argument, nullptr, 'V'},
80 {"port", required_argument, nullptr, 'p'},
81 {NULL, 0, nullptr, 0 }
82};
83
84void ftpHeader() {
85 cout << _("PLPFTP Version ") << VERSION << endl;
86 cout << _("FTP like interface started. Type \"?\" for help.") << endl;
87 cout << endl;
88}
89
90int main(int argc, char **argv) {
91 FTP ftp;
92 string host = "127.0.0.1";
94
95 setlocale (LC_ALL, "");
96 textdomain(PACKAGE);
97
98 while (1) {
99 int c = getopt_long(argc, argv, "hVp:", opts, NULL);
100 if (c == -1)
101 break;
102 switch (c) {
103 case '?':
104 usage();
105 return -1;
106 case 'V':
107 cout << _("plpftp Version ") << VERSION << endl;
108 return 0;
109 case 'h':
110 help();
111 return 0;
112 case 'p':
113 if (!cli_utils::parse_port(optarg, &host, &port)) {
114 cout << _("Invalid port definition.") << endl;
115 return 1;
116 }
117 break;
118 }
119 }
120 if (optind == argc) {
121 ftpHeader();
122 }
123
125 auto deviceEndpoint = DeviceEndpoint::connect(host, port, &error);
126 if (!deviceEndpoint) {
127 std::cerr << "plpftp: " << error << std::endl;
128 return EXIT_FAILURE;
129 }
130 vector<char *> args(argv + optind, argv + argc);
131 return ftp.session(*deviceEndpoint, *deviceEndpoint->rfsv_, *deviceEndpoint->rpcs_, *deviceEndpoint->clip_, args);
132}
static std::unique_ptr< DeviceEndpoint > connect(const std::string host, int port, Enum< ConnectionError > *error)
Wrapper class featuring range-checking and string representation of enumerated values.
Definition: Enum.h:135
Definition: ftp.h:36
int session(DeviceEndpoint &deviceEndpoint, RFSV &rfsv, RPCS &rpcs, rclip &clipboard, std::vector< char * > argv)
Definition: ftp.cc:545
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:90
static void usage()
Definition: main.cc:73
static struct option opts[]
Definition: main.cc:77
void ftpHeader()
Definition: main.cc:84
static void help()
Definition: main.cc:53
#define _(String)
Definition: plpintl.h:34
#define textdomain(Domain)
Definition: plpintl.h:36
static void error(int line)
Definition: sismain.cpp:44