when no option is specified it should print help message
[csql.git] / src / tools / csqlds.cxx
blobc34f43cf298c8d17b176b883f365e8827b8e5b32
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #include <os.h>
17 #include <CSql.h>
18 #include <TableConfig.h>
20 void printUsage()
22 printf("Usage: csqlds [-U username] [-P passwd] [-D dsnname] [-N tdbname] \n"
23 " [-a] [-r] \n");
24 printf(" username -> Username of TDB .\n");
25 printf(" passwd -> Password of TDB.\n");
26 printf(" dsnname -> DSN to connect to tdb.\n");
27 printf(" tdbname -> Name of Target database\n");
28 printf(" -a -> Add entry to csqlds.conf\n");
29 printf(" -r -> Remove entry from csqlds.conf\n");
30 return;
32 DbRetVal removeEntryFromCsqlds(char *dsn);
33 DbRetVal addEntryToCsqlds(char *dsn, char *uname, char *pwd, char *tbdname);
34 Config Conf::config;
35 int shouldadd=0;
36 int main(int argc, char **argv)
38 Conf::config.readAllValues(os::getenv("CSQL_CONFIG_FILE"));
39 char username[IDENTIFIER_LENGTH];
40 username [0] = '\0';
41 char password[IDENTIFIER_LENGTH];
42 password [0] = '\0';
43 int c = 0, opt = 10;
44 char dsn[IDENTIFIER_LENGTH];
45 char tdbname[IDENTIFIER_LENGTH];
46 bool isUserNameSpecified=false;
47 bool isPasswordSpecified=false;
48 bool isTDBSpecified=false;
49 bool isDSNSpecified=false;
50 bool showmsg=false;
51 while ((c = getopt(argc, argv, "U:P:D:N:ar?")) != EOF)
53 switch (c)
55 case 'U' : { strcpy(username, argv[optind - 1]);
56 isUserNameSpecified=true; opt=10; break; }
57 case 'P' : { strcpy(password, argv[optind - 1]);
58 isPasswordSpecified=true;opt=10; break; }
59 case 'D' : { strcpy(dsn, argv[optind - 1]);
60 opt = 2;
61 isDSNSpecified = true;
62 break;
64 case 'N' : { strcpy(tdbname, argv[optind - 1]);
65 if(opt == 2) opt=3;
66 else opt=10;
67 isTDBSpecified = true;
68 break;
70 case '?' : {showmsg=true; break; } //print help
71 case 'a' : { shouldadd=1; break; }
72 case 'r' : { shouldadd=2; break;}
73 default: opt=10;
75 }//while options
76 if (opt == 10 || true==showmsg) {
77 printUsage();
78 return 0;
81 if( !isUserNameSpecified || !isPasswordSpecified)
83 strcpy(username, "NULL");
84 strcpy(password, "NULL");
87 if(shouldadd == 1)
89 if(opt==3){
90 addEntryToCsqlds( dsn,username,password,tdbname);
92 else{
93 printUsage();
94 return 0;
97 }else if (shouldadd == 2)
99 removeEntryFromCsqlds(dsn);
100 }else
102 printf("Invalid option passed\n");
103 printUsage();
105 return 0;
108 DbRetVal addEntryToCsqlds(char *dsn, char *uname, char *pwd, char *tbdname)
110 FILE *fp = fopen(Conf::config.getDsConfigFile(), "a+");
111 if (NULL == fp) {
112 printError(ErrSysInit, "Invalid path/filename in DS_CONFIG_FILE.\n");
113 return ErrOS;
115 bool isDsnExist=false;
116 char usertmp[IDENTIFIER_LENGTH];
117 char pwdtmp[IDENTIFIER_LENGTH];
118 char dsntmp[IDENTIFIER_LENGTH];
119 char tdbname[IDENTIFIER_LENGTH];
120 while(!feof(fp))
122 fscanf(fp, "%s %s %s %s\n",dsntmp,usertmp,pwdtmp,tdbname );
123 if (strcmp (dsn, dsntmp) == 0) {
124 isDsnExist=true;
125 printf("Applied DSN already Exists\n");
126 return ErrAlready;
129 fprintf(fp,"%s %s %s %s\n",dsn, uname, pwd, tbdname);
130 fclose(fp);
131 return OK;
134 DbRetVal removeEntryFromCsqlds(char *dsn)
136 FILE *fp,*tmpfp;
137 char tmpFileName[MAX_FILE_PATH_LEN];
138 sprintf(tmpFileName, "%s.tmp", Conf::config.getDsConfigFile());
139 char usertmp[IDENTIFIER_LENGTH];
140 char pwdtmp[IDENTIFIER_LENGTH];
141 char dsntmp[IDENTIFIER_LENGTH];
142 char tdbname[IDENTIFIER_LENGTH];
143 tmpfp = fopen(tmpFileName,"w");
144 if( tmpfp == NULL ) {
145 printError(ErrSysInit, "Invalid path/filename in TABLE_CONFIG_FILE.\n");
146 return ErrSysInit;
148 fp = fopen(Conf::config.getDsConfigFile(),"r");
149 if( fp == NULL ) {
150 printError(ErrSysInit, "csqltable.conf file does not exist");
151 return ErrSysInit;
154 while(!feof(fp))
156 fscanf(fp, "%s %s %s %s\n",dsntmp,usertmp,pwdtmp,tdbname );
157 if (strcmp (dsn, dsntmp) == 0) continue;
158 fprintf(tmpfp, "%s %s %s %s\n",dsntmp,usertmp,pwdtmp,tdbname);
160 fclose(fp);
161 fclose(tmpfp);
162 char sysCommand[MAX_FILE_PATH_LEN * 2];
163 sprintf(sysCommand, "mv %s %s", tmpFileName, Conf::config.getDsConfigFile());
164 int ret = system(sysCommand);
165 if (ret != 0)
167 printError(ErrSysInit, "Check csqltable.conf file permission. unable to remove %s from file", dsn);
168 return ErrSysInit;
170 return OK;