-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdlineParser.cpp
More file actions
134 lines (117 loc) · 3.16 KB
/
Copy pathcmdlineParser.cpp
File metadata and controls
134 lines (117 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
*
* (C) 2012 Markus Dittrich
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License Version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License Version 3 for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
#include <string>
#include <iostream>
#include <termios.h>
#include <unistd.h>
#include <vector>
#include "cmdlineParser.hpp"
#include "misc.hpp"
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::getline;
using std::vector;
//
// function for checking for parsing and checking the provided
// command line arguments.
//
int
parseCommandline(int argc, char** argv, CmdLineOpts& cmdLineOpts)
{
// we need at least a password and one specific request
if (argc < 4) {
usage();
return 1;
}
// check command line arguments
cmdLineOpts.action = Action::NONE;
cmdLineOpts.password = "";
int c;
while ((c = getopt(argc,argv, "d:p:ucfla") ) != -1 ) {
switch (c) {
case 'd':
cmdLineOpts.dataBasePath = string(optarg);
break;
case 'u':
cmdLineOpts.action = Action::UPDATE_FILE_REQUEST;
break;
case 'c':
cmdLineOpts.action = Action::CHECK_REQUEST;
break;
case 'l':
cmdLineOpts.action = Action::LIST_PATH_REQUEST;
break;
case 'f':
cmdLineOpts.action = Action::UPDATE_PATH_REQUEST;
break;
case 'a':
cmdLineOpts.action = Action::APPEND_REMOVE_PATH_REQUEST;
break;
case 'p':
cmdLineOpts.password = string(optarg);
break;
case '?':
usage();
break;
case ':':
usage();
break;
default:
usage();
break;
}
}
// make sure user specified one of u or c
if (cmdLineOpts.action == Action::NONE) {
errMsg("Please specify at least one option!");
usage();
return 1;
}
if (cmdLineOpts.password.empty()) {
// turn off echoing
termios oldt;
tcgetattr(STDIN_FILENO, &oldt);
termios newt = oldt;
newt.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
cout << "Please enter your password: ";
getline(cin, cmdLineOpts.password);
cout << endl;
}
if (cmdLineOpts.dataBasePath.empty()) {
errMsg("You must provide a path to the database file.");
usage();
return 1;
}
// grab list of paths if requested
if (cmdLineOpts.action == Action::UPDATE_PATH_REQUEST ||
cmdLineOpts.action == Action::APPEND_REMOVE_PATH_REQUEST) {
// need at least one path
if (optind == argc) {
return 1;
}
for (int i=optind; i<argc; ++i) {
cmdLineOpts.pathList.push_back(string(argv[i]));
}
}
return 0;
}