-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
173 lines (156 loc) · 3.75 KB
/
main.cpp
File metadata and controls
173 lines (156 loc) · 3.75 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
/* this is the entry file of the source, enjoy!*/
/* @author whisly */
#include <iostream>
#include <string>
#include <map>
#include "util/trace.h"
#include "util/config.h"
#include "executor/exec.h"
#include "mdbtype.h"
#ifdef __cplusplus
extern "C" {
#endif
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#ifdef __cplusplus
}
#endif
/* global variables */
std::string versioninfo="Mdb version 0.1, enjoy!";
std::string conf_name="conf.ini";
std::string dbname="data";
std::string cwd="";
std::string prompt="mdb#";
std::map<string,Table *> tblstruct;
/*get the current working directory */
std::string mdbGetCwd(){
long size;
char *buf;
char *ptr;
size=pathconf(".",_PC_PATH_MAX);
if((buf=(char *)malloc((size_t)size))!=NULL){
ptr=getcwd(buf,(size_t)size);
std::string cwd(ptr);
free(buf);
return cwd;
}
else{
Tracer::tracePrint(ERROR,"alloc memory failed\n");
return "";
}
}
void printUsage(){
printf("usage:mdb -d <dirname> -f <conf_file> \n"\
"\tmdb -v\n\tmdb -h\n");
}
int main(int argc,char **argv){
int c;
extern char *optarg;
extern int optopt;
char *cmd=NULL;
while((c=getopt(argc,argv,":f:d:vhe:"))!=-1){
switch(c){
case 'v':
printf("%s\n",versioninfo.c_str());
exit(0);
case 'h':
printUsage();
exit(0);
case 'f':
conf_name=optarg;
break;
case 'e':
cmd=optarg;
break;
case 'd':
dbname=optarg;
break;
case ':':
Tracer::tracePrint(ERROR,"-%c without argname\n",optopt);
exit(0);
case '?':
Tracer::tracePrint(ERROR,"Invalid option %c\n",optopt);
exit(0);
}
}
cwd=mdbGetCwd();
if(cwd.empty()){
printf("get current working directory failed!\n");
exit(0);
}
if(*(dbname.c_str())!='/'){
dbname=cwd+"/"+dbname;
}
if((!conf_name.empty())&&*(conf_name.c_str())!='/'){
conf_name=cwd+"/"+conf_name;
}
Tracer::tracePrint(INFO,"configuration file name :%s",conf_name.c_str());
Tracer::tracePrint(INFO,"database directory :%s",dbname.c_str());
Config config(conf_name);
if(access(conf_name.c_str(),F_OK|R_OK)!=0){
Tracer::tracePrint(ERROR,"can not access configuration file:%s",conf_name.c_str());
exit(0);
}
/*the data directory not exists ,create it! */
if(access(dbname.c_str(),F_OK)!=0){
if(mkdir(dbname.c_str(),0700)!=0){
Tracer::tracePrint(ERROR,"can not create data directory:%s,error %s",
dbname.c_str(),strerror(errno));
exit(0);
}
}
else{
if(access(dbname.c_str(),R_OK|W_OK)!=0){
Tracer::tracePrint(ERROR,"do not have previllege to the directory:%s",dbname.c_str());
exit(0);
}
}
Tracer::tracePrint(INFO,"change to data directory : %s",dbname.c_str());
/** Be carefull, because here I choose to chdir to the data directory
* so from now on the relative path is not the mdb
* */
if(chdir(dbname.c_str())!=0){
Tracer::tracePrint(ERROR,"change to directory:%s,failed %s",
dbname.c_str(),strerror(errno));
exit(0);
}
char inputline[MAXLINESIZE]={0};
printf("%s\n",versioninfo.c_str());
if(cmd!=NULL){
Executor ex;
ex.setQuery(cmd);
ex.setParsertf(cwd+"/"+config.getConfValue("sqlparsetrace"));
ex.execute();
// Tracer::printTbls(tblstruct);
return 0;
}
int quitflag=0;
while(quitflag!=1){
printf("%s",prompt.c_str());
memset(inputline,0,MAXLINESIZE);
fgets(inputline,MAXLINESIZE,stdin);
if(strlen(inputline)>4095){
printf("the input is too long!\n");
quitflag=1;
}
if(strncmp(inputline,"quit",4)==0){
quitflag=1;
}
/*cut off the '\n' symbol */
int len=strlen(inputline);
if(inputline[len-1]=='\n'){
inputline[len-1]=0;
}
if(quitflag!=1){
// printf("%s\n",inputline);
Executor ex;
ex.setQuery(inputline);
ex.setParsertf(cwd+"/"+config.getConfValue("sqlparsetrace"));
ex.execute();
}
}
return 0;
}