-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathpdu.c
More file actions
executable file
·226 lines (202 loc) · 6.25 KB
/
pdu.c
File metadata and controls
executable file
·226 lines (202 loc) · 6.25 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* PDU - PostgreSQL Data Unloader
* Copyright (c) 2024-2025 ZhangChen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "read.h"
#include "tools.h"
int USR_CMD;
char former[50];
char latter[50];
char third[50];
char fourth[10240];
/**
* parseCmd - Parse user input command string
*
* @command: Input command string buffer
*
* Parses the command string, extracting up to 4 tokens (former, latter,
* third, fourth) and sets the global USR_CMD variable to the appropriate
* command type constant.
*
* Returns: The parsed command type (USR_CMD value)
*/
int parseCmd(char command[MiddleAllocSize])
{
memset(former,0,50);
memset(latter,0,50);
memset(third,0,50);
memset(fourth,0,10240);
if ( strcmp(command,"b") == 0){
USR_CMD=CMD_BOOTSTRAP;
}
else if ( strcmp(command,"exit") == 0 || strcmp(command,"\\q") == 0){
USR_CMD=CMD_EXIT;
}
else if ( strncmp(command, "use", 3) == 0 ||
strncmp(command, "set", 3) == 0 ||
strncmp(command, "\\", 1) == 0 ||
strncmp(command, "unload", 6) == 0 ||
strncmp(command, "scan", 4) == 0 ||
strncmp(command, "add", 3) == 0 ||
strncmp(command, "param", 5) == 0 ||
strncmp(command, "show",4) == 0 ||
strncmp(command, "reset",5) == 0 ||
strncmp(command, "dropscan",8) == 0 ||
strncmp(command, "info",4) == 0 ||
strncmp(command, "restore", 7) == 0 ||
strncmp(command,"ds",2) == 0 ||
strcmp(command,"ckwal") == 0 ||
strncmp(command,"meta",4) == 0 ||
shortCMDMatched(command)
)
{
int cmdcount=1;
char *token=strtok(command, " ");
while (token != NULL)
{
if( cmdcount==1 ){
snprintf(former,sizeof(former),"%s",token);
}
else if (cmdcount ==2){
snprintf(latter,sizeof(latter),"%s",token);
}
else if (cmdcount ==3){
snprintf(third,sizeof(third),"%s",token);
}
else if (cmdcount ==4){
snprintf(fourth,sizeof(fourth),"%s",token);
}
token = strtok(NULL, " ");
cmdcount++;
}
if( strcmp(former,"use")==0 ){
USR_CMD=CMD_USE;
}
else if( strcmp(former,"set")==0 ){
USR_CMD=CMD_SET;
}
else if( strcmp(former,"\\d")==0 ||strcmp(former,"\\d+")==0 ){
USR_CMD=CMD_DESC;
}
else if( strncmp(former,"\\" , 1)==0 && strcmp(latter,"") == 0){
USR_CMD=CMD_SHOW;
}
else if( strcmp(former,"unload")==0 || strcmp(former,"u")==0){
USR_CMD=CMD_UNLOAD;
}
else if( strcmp(former,"t") == 0 && strcmp(latter,"") == 0){
USR_CMD=CMD_SHOWTYPE;
}
else if( strcmp(former,"param")==0 || strcmp(former,"p")==0){
USR_CMD=CMD_PARAM;
}
else if( strcmp(former,"reset")==0 ){
USR_CMD=CMD_RESETPARAM;
}
else if( strcmp(former,"scan")==0 ){
USR_CMD=CMD_SCAN;
}
else if( strcmp(former,"dropscan")==0 || strcmp(former,"ds")==0 ){
USR_CMD=CMD_DROPSCAN;
}
else if( strcmp(former,"info")==0 || strcmp(former,"i")==0){
USR_CMD=CMD_INFO;
}
else if( strcmp(former,"restore")==0 ){
USR_CMD=CMD_RESTORE;
}
else if( strcmp(former,"ckwal")==0 ){
USR_CMD=CMD_CHECKWAL;
}
else if( strcmp(former,"meta")==0 ){
USR_CMD=CMD_META;
}
else if( strcmp(former,"add")==0 ){
USR_CMD=CMD_ADD;
}
else if( strcmp(former,"show")==0 && strcmp(latter,"")==0 ){
USR_CMD=CMD_SHOWPARAM;
}
else{
USR_CMD=CMD_UNKNOWN;
}
}
else{
USR_CMD=CMD_UNKNOWN;
}
return USR_CMD;
}
/**
* execute_command_string - Execute a semicolon-separated command string
*
* @cmd_str: Command string containing one or more semicolon-separated commands
*
* Parses and executes each command in the string sequentially.
* Commands are separated by semicolons. Whitespace around commands is trimmed.
*/
void execute_command_string(char *cmd_str) {
char *saveptr = NULL;
char *token = strtok_r(cmd_str, ";", &saveptr);
while (token != NULL) {
trim_whitespace(token);
if (strlen(token) > 0) {
parseCmd(token);
execCmd(USR_CMD, former, latter, third, fourth);
}
token = strtok_r(NULL, ";", &saveptr);
}
}
/**
* main - Application entry point
*
* @argc: Argument count
* @argv: Argument vector
*
* Main entry point for PDU (PostgreSQL Data Unloader). Supports both
* interactive mode and command-line mode. In command-line mode, all
* arguments are concatenated and executed as semicolon-separated commands.
*
* Returns: 0 on success, 1 on initialization failure
*/
int main(int argc, char *argv[]) {
setup_crash_handlers();
if(getInit() != 1){
exit(1);
}
if (argc > 1) {
char *cmd_str = (char *)malloc(10240 * sizeof(char));
cmd_str[0] = '\0';
for (int i = 1; i < argc; i++) {
strcat(cmd_str, argv[i]);
if (i < argc - 1) {
strcat(cmd_str, " ");
}
}
size_t len = strlen(cmd_str);
if (len > 0 && cmd_str[len - 1] != ';') {
strcat(cmd_str, ";");
}
execute_command_string(cmd_str);
free(cmd_str);
exit(0);
}
while (1) {
char *cmd=NULL;
cmd=getinput();
parseCmd(cmd);
execCmd(USR_CMD,former,latter,third,fourth);
}
return 0;
}