NCBI C++ ToolKit
common.c
Go to the documentation of this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

1 #include <config.h>
2 
3 #include <stdio.h>
4 
5 #if HAVE_STDLIB_H
6 #include <stdlib.h>
7 #endif /* HAVE_STDLIB_H */
8 
9 #if HAVE_STRING_H
10 #include <string.h>
11 #endif /* HAVE_STRING_H */
12 
13 #if HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16 
17 #ifndef DBNTWIN32
18 #include "replacements.h"
19 #endif
20 
21 #include <ctpublic.h>
22 #include "common.h"
23 #ifdef TDS_STATIC_CAST
24 #include "ctlib.h"
25 #endif
26 
27 #include <common/test_assert.h>
28 
29 char USER[512];
30 char SERVER[512];
31 char PASSWORD[512];
32 char DATABASE[512];
33 
35 
36 static char *DIRNAME = NULL;
37 static const char *BASENAME = NULL;
38 
39 static const char *PWD = "../../../PWD";
40 
41 
45 
47 
48 #if defined(__MINGW32__) || defined(_MSC_VER)
49 static char *
50 tds_dirname(char* path)
51 {
52  char *p, *p2;
53 
54  for (p = path + strlen(path); --p > path && (*p == '/' || *p == '\\');)
55  *p = '\0';
56 
57  p = strrchr(path, '/');
58  if (!p)
59  p = path;
60  p2 = strrchr(p, '\\');
61  if (p2)
62  p = p2;
63  *p = 0;
64  return path;
65 }
66 #define dirname tds_dirname
67 
68 #endif
69 
72 {
73  FILE *in = NULL;
74  char line[512];
75  char *s1, *s2;
76 
77  if (common_pwd.initialized) {
78  strcpy(USER, common_pwd.USER);
79  strcpy(PASSWORD, common_pwd.PASSWORD);
80  strcpy(SERVER, common_pwd.SERVER);
81  strcpy(DATABASE, common_pwd.DATABASE);
82  return CS_SUCCEED;
83  }
84 
85  s1 = getenv("TDSPWDFILE");
86  if (s1 && s1[0])
87  in = fopen(s1, "r");
88  if (!in)
89  in = fopen(PWD, "r");
90  if (!in) {
91  fprintf(stderr, "Can not open PWD file \"%s\"\n\n", PWD);
92  return CS_FAIL;
93  }
94 
95  while (fgets(line, 512, in)) {
96  s1 = strtok(line, "=");
97  s2 = strtok(NULL, "\n");
98  if (!s1 || !s2) {
99  continue;
100  }
101  if (!strcmp(s1, "UID")) {
102  strcpy(USER, s2);
103  } else if (!strcmp(s1, "SRV")) {
104  strcpy(SERVER, s2);
105  } else if (!strcmp(s1, "PWD")) {
106  strcpy(PASSWORD, s2);
107  } else if (!strcmp(s1, "DB")) {
108  strcpy(DATABASE, s2);
109  }
110  }
111  fclose(in);
112  return CS_SUCCEED;
113 }
114 
115 static CS_RETCODE
116 establish_login(int argc, char **argv)
117 {
118  extern char *optarg;
119  extern int optind;
120  COMMON_PWD options = {0};
121 #if !defined(__MINGW32__) && !defined(_MSC_VER)
122  int ch;
123 #endif
124 
125  BASENAME = basename((char *)argv[0]);
126  DIRNAME = dirname((char *)argv[0]);
127 
128 #if !defined(__MINGW32__) && !defined(_MSC_VER)
129  /* process command line options (handy for manual testing) */
130  while ((ch = getopt(argc, argv, "U:P:S:D:f:m:v")) != -1) {
131  switch (ch) {
132  case 'U':
133  strcpy(options.USER, optarg);
134  break;
135  case 'P':
136  strcpy(options.PASSWORD, optarg);
137  break;
138  case 'S':
139  strcpy(options.SERVER, optarg);
140  break;
141  case 'D':
142  strcpy(options.DATABASE, optarg);
143  break;
144  case 'f': /* override default PWD file */
145  PWD = strdup(optarg);
146  break;
147  case 'm':
148  common_pwd.maxlength = strtol(optarg, NULL, 10);
149  case 'v':
150  common_pwd.fverbose = 1;
151  break;
152  case '?':
153  default:
154  fprintf(stderr, "usage: %s [-v] [-f PWD]\n"
155  " [-U username] [-P password]\n"
156  " [-S servername] [-D database]\n"
157  , BASENAME);
158  exit(1);
159  }
160  }
161 #endif
162  read_login_info();
163 
164  /* override PWD file with command-line options */
165 
166  if (*options.USER)
167  strcpy(USER, options.USER);
168  if (*options.PASSWORD)
169  strcpy(PASSWORD, options.PASSWORD);
170  if (*options.SERVER)
171  strcpy(SERVER, options.SERVER);
172  if (*options.DATABASE)
173  strcpy(DATABASE, options.DATABASE);
174 
175  return (*USER && *SERVER && *DATABASE)? CS_SUCCEED : CS_FAIL;
176 }
177 
180 {
181  CS_RETCODE ret;
182 
183  if ((ret = establish_login(argc, argv)) != CS_SUCCEED) {
184  if (verbose) {
185  fprintf(stderr, "read_login_info() failed!\n");
186  }
187  return ret;
188  }
190 }
191 
192 /* old way: because I'm too lazy to change every unit test */
195 {
196  CS_RETCODE ret;
197 
198  if ((ret = read_login_info()) != CS_SUCCEED) {
199  if (verbose) {
200  fprintf(stderr, "read_login_info() failed!\n");
201  }
202  return ret;
203  }
205 }
206 
209 {
210  CS_RETCODE ret;
211  char query[30];
212 #ifdef TDS_STATIC_CAST
213  TDSCONTEXT *tds_ctx;
214 #endif
215 
217  if (ret != CS_SUCCEED) {
218  if (verbose) {
219  fprintf(stderr, "Context Alloc failed!\n");
220  }
221  return ret;
222  }
223 
224 #ifdef TDS_STATIC_CAST
225  /* Force default date format, some tests rely on it */
226  tds_ctx = (TDSCONTEXT *) (*ctx)->tds_ctx;
227  if (tds_ctx && tds_ctx->locale && tds_ctx->locale->date_fmt) {
228  free(tds_ctx->locale->date_fmt);
229  tds_ctx->locale->date_fmt = strdup("%b %d %Y %I:%M%p");
230  }
231 #endif
232 
233  ret = ct_init(*ctx, CS_VERSION_100);
234  if (ret != CS_SUCCEED) {
235  if (verbose) {
236  fprintf(stderr, "Library Init failed!\n");
237  }
238  return ret;
239  }
240  if ((ret = ct_callback(*ctx, NULL, CS_SET, CS_CLIENTMSG_CB,
241  (CS_VOID*) clientmsg_cb)) != CS_SUCCEED) {
242  fprintf(stderr, "ct_callback() failed\n");
243  return ret;
244  }
246  fprintf(stderr, "ct_callback() failed\n");
247  return ret;
248  }
249  ret = ct_con_alloc(*ctx, conn);
250  if (ret != CS_SUCCEED) {
251  if (verbose) {
252  fprintf(stderr, "Connect Alloc failed!\n");
253  }
254  return ret;
255  }
257  if (ret != CS_SUCCEED) {
258  if (verbose) {
259  fprintf(stderr, "ct_con_props() SET USERNAME failed!\n");
260  }
261  return ret;
262  }
264  if (ret != CS_SUCCEED) {
265  if (verbose) {
266  fprintf(stderr, "ct_con_props() SET PASSWORD failed!\n");
267  }
268  return ret;
269  }
270 
271  printf("connecting as %s to %s.%s\n", USER, SERVER, DATABASE);
272 
273  ret = ct_connect(*conn, SERVER, CS_NULLTERM);
274  if (ret != CS_SUCCEED) {
275  if (verbose) {
276  fprintf(stderr, "Connection failed!\n");
277  }
278  ct_con_drop(*conn);
279  *conn = NULL;
280  cs_ctx_drop(*ctx);
281  *ctx = NULL;
282  return ret;
283  }
284  ret = ct_cmd_alloc(*conn, cmd);
285  if (ret != CS_SUCCEED) {
286  if (verbose) {
287  fprintf(stderr, "Command Alloc failed!\n");
288  }
289  ct_con_drop(*conn);
290  *conn = NULL;
291  cs_ctx_drop(*ctx);
292  *ctx = NULL;
293  return ret;
294  }
295 
296  strcpy(query, "use ");
297  strncat(query, DATABASE, 20);
298 
299  ret = run_command(*cmd, query);
300  if (ret != CS_SUCCEED)
301  return ret;
302 
303  return CS_SUCCEED;
304 }
305 
306 
309 {
310 CS_RETCODE ret;
311 
312  ret = ct_cancel(conn, NULL, CS_CANCEL_ALL);
313  if (ret != CS_SUCCEED) {
314  if (verbose) {
315  fprintf(stderr, "ct_cancel() failed!\n");
316  }
317  return ret;
318  }
319  ct_cmd_drop(cmd);
321  ct_con_drop(conn);
323  cs_ctx_drop(ctx);
324 
325  return CS_SUCCEED;
326 }
327 
328 /* Run commands from which we expect no results returned */
330 run_command(CS_COMMAND * cmd, const char *sql)
331 {
332 CS_RETCODE ret, results_ret;
333 CS_INT result_type;
334 
335  if (cmd == NULL) {
336  return CS_FAIL;
337  }
338 
340  if (ret != CS_SUCCEED) {
341  fprintf(stderr, "ct_command() failed\n");
342  return ret;
343  }
344  ret = ct_send(cmd);
345  if (ret != CS_SUCCEED) {
346  fprintf(stderr, "ct_send() failed\n");
347  return ret;
348  }
349  while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
350  switch ((int) result_type) {
351  case CS_CMD_SUCCEED:
352  break;
353  case CS_CMD_DONE:
354  break;
355  case CS_CMD_FAIL:
356  fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
357  /* return CS_FAIL; */
358  break;
359  default:
360  fprintf(stderr, "ct_results() unexpected result_type.\n");
361  return CS_FAIL;
362  }
363  }
364  switch ((int) results_ret) {
365  case CS_END_RESULTS:
366  break;
367  case CS_FAIL:
368  fprintf(stderr, "ct_results() failed.\n");
369  return CS_FAIL;
370  break;
371  default:
372  fprintf(stderr, "ct_results() unexpected return.\n");
373  return CS_FAIL;
374  }
375 
376  return CS_SUCCEED;
377 }
378 
379 CS_INT
380 cslibmsg_cb(CS_CONTEXT * connection, CS_CLIENTMSG * errmsg)
381 {
383  fprintf(stderr, "\nCS-Library Message:\n");
384  fprintf(stderr, "number %d layer %d origin %d severity %d number %d\n",
385  errmsg->msgnumber,
386  CS_LAYER(errmsg->msgnumber),
387  CS_ORIGIN(errmsg->msgnumber), CS_SEVERITY(errmsg->msgnumber), CS_NUMBER(errmsg->msgnumber));
388  fprintf(stderr, "msgstring: %s\n", errmsg->msgstring);
389  fprintf(stderr, "osstring: %s\n", (errmsg->osstringlen > 0)
390  ? errmsg->osstring : "(null)");
391  return CS_SUCCEED;
392 }
393 
394 
395 
397 clientmsg_cb(CS_CONTEXT * context, CS_CONNECTION * connection, CS_CLIENTMSG * errmsg)
398 {
400  fprintf(stderr, "\nOpen Client Message:\n");
401  fprintf(stderr, "number %d layer %d origin %d severity %d number %d\n",
402  errmsg->msgnumber,
403  CS_LAYER(errmsg->msgnumber),
404  CS_ORIGIN(errmsg->msgnumber), CS_SEVERITY(errmsg->msgnumber), CS_NUMBER(errmsg->msgnumber));
405  fprintf(stderr, "msgstring: %s\n", errmsg->msgstring);
406  fprintf(stderr, "osstring: %s\n", (errmsg->osstringlen > 0)
407  ? errmsg->osstring : "(null)");
408  return CS_SUCCEED;
409 }
410 
412 servermsg_cb(CS_CONTEXT * context, CS_CONNECTION * connection, CS_SERVERMSG * srvmsg)
413 {
415 
416  if (srvmsg->msgnumber == 5701 || srvmsg->msgnumber == 5703) {
417  fprintf(stderr, "%s\n", srvmsg->text);
418  return CS_SUCCEED;
419  }
420 
421  fprintf(stderr, "%s Message %d severity %d state %d line %d:\n",
422  srvmsg->svrnlen > 0? srvmsg->svrname : "Server",
423  srvmsg->msgnumber, srvmsg->severity, srvmsg->state, srvmsg->line);
424  if (srvmsg->proclen > 0)
425  fprintf(stderr, "proc %s: ", srvmsg->proc);
426  fprintf(stderr, "\t\"%s\"\n", srvmsg->text);
427 
428  return CS_SUCCEED;
429 }
430 
431 const char *
433 {
434  static char str[64];
435 
436 #define S(s) case s: return #s;
437  switch ((int) ret) {
443  S(CS_CMD_DONE)
444  S(CS_CMD_FAIL)
446 #undef S
447  }
448 
449  sprintf(str, "?? (%d)", (int) ret);
450  return str;
451 }
#define CS_SERVERMSG_CB
Definition: cspublic.h:411
CS_RETCODE cs_ctx_alloc(CS_INT version, CS_CONTEXT **ctx)
Definition: cs.c:342
#define CS_CMD_DONE
Definition: cspublic.h:436
#define CS_CLIENTMSG_CB
Definition: cspublic.h:412
@ CS_USERNAME
Definition: cspublic.h:183
@ CS_PASSWORD
Definition: cspublic.h:185
#define CS_CMD_SUCCEED
Definition: cspublic.h:437
#define CS_FAIL
Definition: cspublic.h:41
#define CS_PARAM_RESULT
Definition: cspublic.h:540
#define CS_LANG_CMD
Definition: cspublic.h:441
#define CS_CANCEL_ALL
Definition: cspublic.h:466
#define CS_MSG_RESULT
Definition: cspublic.h:545
#define CS_NUMBER(x)
Definition: cspublic.h:66
#define CS_STATUS_RESULT
Definition: cspublic.h:542
CS_RETCODE cs_ctx_drop(CS_CONTEXT *ctx)
Definition: cs.c:385
#define CS_LAYER(x)
Definition: cspublic.h:63
#define CS_UNUSED
Definition: cspublic.h:425
#define CS_SET
Definition: cspublic.h:429
#define CS_ROW_RESULT
Definition: cspublic.h:541
#define CS_COMPUTE_RESULT
Definition: cspublic.h:538
#define CS_SEVERITY(x)
Definition: cspublic.h:65
#define CS_SUCCEED
Definition: cspublic.h:40
#define CS_CMD_FAIL
Definition: cspublic.h:438
#define CS_NULLTERM
Definition: cspublic.h:422
#define CS_ORIGIN(x)
Definition: cspublic.h:64
#define CS_END_RESULTS
Definition: cspublic.h:56
#define CS_VERSION_100
Definition: cspublic.h:446
Int4 CS_INT
Definition: cstypes.h:41
void CS_VOID
Definition: cstypes.h:53
CS_INT CS_RETCODE
Definition: cstypes.h:63
static CS_COMMAND * cmd
Definition: ct_dynamic.c:26
static CS_CONNECTION * conn
Definition: ct_dynamic.c:25
int cslibmsg_cb_invoked
Definition: common.c:42
CS_INT cslibmsg_cb(CS_CONTEXT *connection, CS_CLIENTMSG *errmsg)
Definition: common.c:380
char PASSWORD[512]
Definition: common.c:31
CS_RETCODE try_ctlogin(CS_CONTEXT **ctx, CS_CONNECTION **conn, CS_COMMAND **cmd, int verbose)
Definition: common.c:194
char DATABASE[512]
Definition: common.c:32
static const char * BASENAME
Definition: common.c:37
static CS_RETCODE continue_logging_in(CS_CONTEXT **ctx, CS_CONNECTION **conn, CS_COMMAND **cmd, int verbose)
Definition: common.c:208
int clientmsg_cb_invoked
Definition: common.c:43
CS_RETCODE try_ctlogin_with_options(int argc, char **argv, CS_CONTEXT **ctx, CS_CONNECTION **conn, CS_COMMAND **cmd, int verbose)
Definition: common.c:179
static char * DIRNAME
Definition: common.c:36
static CS_RETCODE establish_login(int argc, char **argv)
Definition: common.c:116
CS_RETCODE run_command(CS_COMMAND *cmd, const char *sql)
Definition: common.c:330
const char * res_type_str(CS_RETCODE ret)
Definition: common.c:432
#define S(s)
char USER[512]
Definition: common.c:29
int servermsg_cb_invoked
Definition: common.c:44
CS_RETCODE clientmsg_cb(CS_CONTEXT *context, CS_CONNECTION *connection, CS_CLIENTMSG *errmsg)
Definition: common.c:397
CS_RETCODE servermsg_cb(CS_CONTEXT *context, CS_CONNECTION *connection, CS_SERVERMSG *srvmsg)
Definition: common.c:412
CS_RETCODE try_ctlogout(CS_CONTEXT *ctx, CS_CONNECTION *conn, CS_COMMAND *cmd, int verbose)
Definition: common.c:308
CS_RETCODE read_login_info(void)
Definition: common.c:71
static const char * PWD
Definition: common.c:39
COMMON_PWD common_pwd
Definition: common.c:34
char SERVER[512]
Definition: common.c:30
CS_CONTEXT * ctx
Definition: t0006.c:12
CS_RETCODE ct_command(CS_COMMAND *cmd, CS_INT type, const CS_VOID *buffer, CS_INT buflen, CS_INT option)
Definition: ct.c:760
CS_RETCODE ct_connect(CS_CONNECTION *con, CS_CHAR *servername, CS_INT snamelen)
Definition: ct.c:633
CS_RETCODE ct_results(CS_COMMAND *cmd, CS_INT *result_type)
Definition: ct.c:1172
CS_RETCODE ct_callback(CS_CONTEXT *ctx, CS_CONNECTION *con, CS_INT action, CS_INT type, CS_VOID *func)
Definition: ct.c:306
CS_RETCODE ct_cmd_drop(CS_COMMAND *cmd)
Definition: ct.c:1881
CS_RETCODE ct_cmd_alloc(CS_CONNECTION *con, CS_COMMAND **cmd)
Definition: ct.c:728
CS_RETCODE ct_con_alloc(CS_CONTEXT *ctx, CS_CONNECTION **con)
Definition: ct.c:273
CS_RETCODE ct_con_drop(CS_CONNECTION *con)
Definition: ct.c:1934
CS_RETCODE ct_close(CS_CONNECTION *con, CS_INT option)
Definition: ct.c:1923
CS_RETCODE ct_send(CS_COMMAND *cmd)
Definition: ct.c:913
CS_RETCODE ct_cancel(CS_CONNECTION *conn, CS_COMMAND *cmd, CS_INT type)
Definition: ct.c:2201
CS_RETCODE ct_con_props(CS_CONNECTION *con, CS_INT action, CS_INT property, CS_VOID *buffer, CS_INT buflen, CS_INT *out_len)
Definition: ct.c:351
CS_RETCODE ct_init(CS_CONTEXT *ctx, CS_INT version)
Definition: ct.c:260
CS_RETCODE ct_exit(CS_CONTEXT *ctx, CS_INT unused)
Definition: ct.c:252
#define NULL
Definition: ncbistd.hpp:225
exit(2)
if(yy_accept[yy_current_state])
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
#define strdup
Definition: ncbi_ansi_ext.h:70
std::istream & in(std::istream &in_, double &x_)
true_type verbose
Definition: processing.cpp:890
static char sql[1024]
Definition: putdata.c:19
#define optarg
#define optind
#define getopt
Definition: replacements.h:157
#define basename(path)
Definition: replacements.h:116
static const char * str(char *buf, int n)
Definition: stats.c:84
int initialized
Definition: common.h:15
char PASSWORD[512]
Definition: common.h:19
long maxlength
Definition: common.h:21
char DATABASE[512]
Definition: common.h:17
char USER[512]
Definition: common.h:18
char fverbose
Definition: common.h:20
char SERVER[512]
Definition: common.h:16
CS_CHAR osstring[1024]
Definition: cstypes.h:191
CS_MSGNUM msgnumber
Definition: cstypes.h:187
CS_CHAR msgstring[1024]
Definition: cstypes.h:188
CS_INT osstringlen
Definition: cstypes.h:192
CS_CHAR proc[132]
Definition: cstypes.h:207
CS_CHAR text[1024]
Definition: cstypes.h:203
CS_MSGNUM msgnumber
Definition: cstypes.h:200
CS_INT line
Definition: cstypes.h:209
CS_INT svrnlen
Definition: cstypes.h:206
CS_CHAR svrname[132]
Definition: cstypes.h:205
CS_INT severity
Definition: cstypes.h:202
CS_INT proclen
Definition: cstypes.h:208
CS_INT state
Definition: cstypes.h:201
static string query
TDSLOCALE * locale
Definition: tds.h:1099
char * date_fmt
Definition: tds.h:650
void free(voidpf ptr)
Modified on Tue Apr 09 07:57:50 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 10 07:33:13 2024 by modify_doxy.py rev. 669887
Modified on Thu Apr 11 15:07:05 2024 by modify_doxy.py rev. 669887