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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
|
/* $Id: syb.c,v 1.14 2021/01/09 22:41:48 tom Exp $ */
#include <cdk_test.h>
#include <sybfront.h>
#include <sybdb.h>
#ifdef HAVE_XCURSES
char *XCursesProgramName="syb";
#endif
#define MAXWIDTH 5000
#define MAXHISTORY 1000
/*
* This structure is used for keeping command history.
*/
struct history_st {
unsigned used;
int count;
int current;
char **cmd_history;
};
/*
* Define some global variables.
*/
char *GPUsage = "[-p Command Prompt] [-U User] [-P Password] [-S Server] [-h help]";
char *GPCurrentDatabase = 0;
extern char *dberrstr;
/*
* Because the error/message callback do not allow you to pass in
* data of your own, we have to make the screen pointer global. :(
*/
CDKSCREEN *GPCdkScreen = 0;
/*
* Define function prototypes.
*/
DBPROCESS *loginToSybase (CDKSCREEN *screen, char *login, char *password);
DBPROCESS *sybaseLogin (CDKSCREEN *screen, char *login, char *password, int attempts);
char *assembleTitle (DBPROCESS *dbProcess);
char *assembleRow (DBPROCESS *dbProcess);
char *uc (char *word);
int getColWidth (DBPROCESS *dbProcess, int col);
void runIsqlCommand (CDKSCREEN *cdkscreen, CDKSWINDOW *swindow, DBPROCESS *process);
void useDatabase (CDKSWINDOW *swindow, DBPROCESS *dbProc, char *command);
void intro (CDKSCREEN *screen);
void help (CDKENTRY *entry);
void loadHistory (struct history_st *history);
void saveHistory (struct history_st *history, int count);
/*
* Define callback prototypes.
*/
BINDFN_PROTO(viewHistoryCB);
BINDFN_PROTO(swindowHelpCB);
BINDFN_PROTO(historyUpCB);
BINDFN_PROTO(historyDownCB);
BINDFN_PROTO(listHistoryCB);
/*
* Define Sybase error/message callbacks. This is required by DBLib.
*/
int err_handler (DBPROCESS *dbProcess, DBINT mesgNumber, int mesgState,
int severity, char *mesgText);
int msg_handler (DBPROCESS *dbProcess, DBINT mesgNumber, int mesgState,
int severity, char *mesgText);
/*
* Written by: Mike Glover
* Purpose:
* This creates a very simple interface to Sybase.
*/
int main (int argc, char **argv)
{
/* Declare variables. */
CDKSWINDOW *commandOutput = 0;
CDKENTRY *commandEntry = 0;
DBPROCESS *dbProcess = 0;
char *dsquery = 0;
char *command = 0;
char *prompt = 0;
char *upper = 0;
char *login = 0;
char *password = 0;
char *server = 0;
int count = 0;
int width = 0;
int ret = 0;
struct history_st history;
char *mesg[5], temp[1000];
/* Set up the history. */
GPCurrentDatabase = copyChar ("master");
history.used = 0;
history.current = 0;
history.count = 0;
/* Check the command line for options. */
while (1)
{
/* Are there any more command line options to parse. */
if ((ret = getopt (argc, argv, "p:U:P:S:h")) == -1)
{
break;
}
switch (ret)
{
case 'p':
prompt = copyChar (optarg);
break;
case 'U':
login = copyChar (optarg);
break;
case 'P':
password = copyChar (optarg);
break;
case 'S':
server = copyChar (optarg);
break;
case 'h':
printf ("Usage: %s %s\n", argv[0], GPUsage);
ExitProgram (EXIT_SUCCESS);
break;
}
}
/* Set up the command prompt. */
if (prompt == 0)
{
if (server == 0)
{
if ((dsquery = getenv ("DSQUERY")) != 0)
{
sprintf (temp, "</B/24>[%s] Command >", dsquery);
prompt = copyChar (temp);
}
else
{
prompt = copyChar ("</B/24>Command >");
}
}
else
{
sprintf (temp, "</B/24>[%s] Command >", server);
prompt = copyChar (temp);
}
}
GPCdkScreen = initCDKScreen (NULL);
/* Start color. */
initCDKColor();
/* Initialize DB-Library. */
if (dbinit() == FAIL)
{
mesg[0] = "<C></U>Fatal Error";
mesg[1] = "<C>Could not connect to the Sybase database.";
popupLabel (GPCdkScreen, mesg, 2);
ExitProgram (EXIT_FAILURE);
}
/* Load the history. */
loadHistory (&history);
/* Create the scrolling window. */
commandOutput = newCDKSwindow (GPCdkScreen, CENTER, TOP, -8, -2,
"<C></B/5>Command Output Window",
MAXWIDTH, TRUE, FALSE);
/* Create the entry field. */
width = COLS - strlen (prompt) - 1;
commandEntry = newCDKEntry (GPCdkScreen, CENTER, BOTTOM,
0, prompt, A_BOLD|COLOR_PAIR(8),
COLOR_PAIR(24)|'_', vMIXED,
width, 1, 512, FALSE, FALSE);
/* Create the key bindings. */
bindCDKObject (vENTRY, commandEntry, KEY_UP, &historyUpCB, &history);
bindCDKObject (vENTRY, commandEntry, KEY_DOWN, &historyDownCB, &history);
bindCDKObject (vENTRY, commandEntry, CTRL('^'), &listHistoryCB, &history);
bindCDKObject (vENTRY, commandEntry, KEY_TAB, &viewHistoryCB, commandOutput);
bindCDKObject (vSWINDOW, commandOutput, '?', swindowHelpCB, commandEntry);
/* Draw the screen. */
refreshCDKScreen (GPCdkScreen);
/* Display the introduction window. */
intro (GPCdkScreen);
/* Make them login first. */
dbProcess = sybaseLogin (GPCdkScreen, login, password, 3);
if (dbProcess == 0)
{
destroyCDKScreen (GPCdkScreen);
endCDK ();
ExitProgram (EXIT_FAILURE);
}
/* Do this forever. */
for (;;)
{
/* Get the command. */
command = activateCDKEntry (commandEntry, 0);
/* Strip off leading and trailing white space. */
stripWhiteSpace (vBOTH, command);
/* Upper case the command. */
upper = uc (command);
/* Check the output of the command. */
if (strcmp (upper, "QUIT") == 0 ||
strcmp (upper, "EXIT") == 0 ||
strcmp (upper, "Q") == 0 ||
strcmp (upper, "E") == 0 ||
commandEntry->exitType == vESCAPE_HIT)
{
/* Save the history. */
saveHistory (&history, 100);
/* Exit. */
dbclose (dbProcess);
dbexit();
/* All done. */
destroyCDKEntry (commandEntry);
destroyCDKSwindow (commandOutput);
freeChar (upper);
endCDK();
ExitProgram (EXIT_SUCCESS);
}
else if (strcmp (command, "login") == 0)
{
DBPROCESS *newLogin = sybaseLogin (GPCdkScreen, 0, 0, 3);
if (newLogin == 0)
{
addCDKSwindow (commandOutput, "Login Error: Could not switch to new user.", BOTTOM);
}
else
{
/* Close the old connection. */
dbclose (dbProcess);
dbProcess = newLogin;
/* Add a message to the scrolling window. */
addCDKSwindow (commandOutput,
"Logged into database as new user.",
BOTTOM);
count = 0;
}
}
else if (strcmp (command, "logout") == 0)
{
/* Close the old connection. */
dbclose (dbProcess);
dbProcess = 0;
/* Add a message to the scrolling window. */
addCDKSwindow (commandOutput, "Logged out.", BOTTOM);
count = 0;
}
else if (strcmp (command, "clear") == 0)
{
/* Clear the scrolling window. */
cleanCDKSwindow (commandOutput);
}
else if (strcmp (command, "history") == 0)
{
listHistoryCB (vENTRY, (void *)commandEntry, (void *)&history, 0);
}
else if (strcmp (command, "tables") == 0)
{
/* Check if we are logged in. */
if (dbProcess == 0)
{
addCDKSwindow (commandOutput, "You must login first.", BOTTOM);
}
else
{
sprintf (command, "select * from sysobjects where type = 'U'");
/* Put the command into the ISQL buffer. */
dbcmd (dbProcess, command);
/* Put the command into the scrolling window. */
sprintf (temp, "</R>%d><!R> %s", count+1, command);
addCDKSwindow (commandOutput, temp, BOTTOM);
/* Increment the counter. */
count++;
}
}
else if (strcmp (command, "help") == 0)
{
/* Display the help. */
help(commandEntry);
}
else if (command[0] == 'u' && command[1] == 's' &&
command[2] == 'e' && command[3] == ' ')
{
/* They want to use a database. */
useDatabase (commandOutput, dbProcess, command);
count = 0;
}
else if (strcmp (command, "go") == 0)
{
/* Check if we are logged in. */
if (dbProcess == 0)
{
addCDKSwindow (commandOutput, "You must login first.", BOTTOM);
}
else
{
/* Put the command into the scrolling window. */
sprintf (temp, "</R>%d><!R> %s", count+1, command);
addCDKSwindow (commandOutput, temp, BOTTOM);
count = 0;
/* Run the command. */
runIsqlCommand (GPCdkScreen, commandOutput, dbProcess);
}
}
else
{
/* Check if we are logged in. */
if (dbProcess == 0)
{
addCDKSwindow (commandOutput, "You must login first.", BOTTOM);
}
else
{
/* Put the command into the ISQL buffer. */
dbcmd (dbProcess, command);
/* Put the command into the scrolling window. */
sprintf (temp, "</R>%d><!R> %s", count+1, command);
addCDKSwindow (commandOutput, temp, BOTTOM);
/* Increment the counter. */
count++;
}
}
/* Keep the history. */
history.used = CDKallocStrings(&(history.cmd_history), command, history.count++, history.used);
history.current = history.count;
/* Clear the entry field. */
cleanCDKEntry (commandEntry);
/* Free up the memory used by the upper pointer. */
freeChar (upper);
}
}
/*
* This lets a person 'use' a database.
*/
void useDatabase (CDKSWINDOW *swindow, DBPROCESS *dbProc, char *command)
{
char *database = 0;
char temp[256];
char **words;
int wordCount, x;
/* Split the command line up and get the database name. */
words = CDKsplitString (command, ' ');
wordCount = CDKcountStrings (words);
/* Look for the name. */
for (x=1; x < wordCount; x++)
{
if (strlen (words[x]) != 0)
{
database = copyChar (words[x]);
}
}
CDKfreeStrings(words);
/* Try to actually use the database. */
if (dbuse(dbProc, database) == FAIL)
{
/* We aren't allowed to use that database. */
sprintf (temp, "Command: %s", command);
addCDKSwindow (swindow, temp, BOTTOM);
addCDKSwindow (swindow, "</B/16>Error<!B!16> You are not allowed to use that database.", BOTTOM);
return;
}
/* Set the global database name. */
if (database == 0)
{
/* Put a syntax error in the scrolling window. */
sprintf (temp, "Command: %s", command);
addCDKSwindow (swindow, temp, BOTTOM);
addCDKSwindow (swindow, "</B/16>Error<!B!16> Syntax Error", BOTTOM);
return;
}
/* Clear out the old database name and set the new one. */
freeChar (GPCurrentDatabase);
GPCurrentDatabase = database;
/* Add a message into the scrolling window. */
sprintf (temp, "Command: %s", command);
addCDKSwindow (swindow, temp, BOTTOM);
sprintf (temp, "Default Database set to %s", GPCurrentDatabase);
addCDKSwindow (swindow, temp, BOTTOM);
}
/*
* This does the requisite checking for failed login attempts.
*/
DBPROCESS *sybaseLogin (CDKSCREEN *screen, char *accountName, char *accountPassword, int attemptCount)
{
DBPROCESS *dbProcess = 0;
char *login = accountName;
char *password = accountPassword;
int count = 0;
int lines = 0;
char *mesg[5];
/* Give them X attempts, then kick them out. */
while (count < attemptCount)
{
/* Try to login. */
dbProcess = loginToSybase (GPCdkScreen, login, password);
/*
* If the dbprocess is null the account/password
* pair does not exist.
*/
if (dbProcess == 0)
{
/*
* If the login and account names were provided,
* set them to null and allow the user to enter
* the name and password by hand.
*/
login = 0;
password = 0;
/* Spit out the login error message. */
lines = 0;
mesg[lines++] = "<C></B/5>Login Error";
mesg[lines++] = " ";
mesg[lines++] = "<C>The login/password pair does not exist.";
mesg[lines++] = " ";
mesg[lines++] = "<C>Please try again.";
popupLabel (GPCdkScreen, mesg, lines);
eraseCDKScreen (GPCdkScreen);
refreshCDKScreen (GPCdkScreen);
count++;
}
else
{
break;
}
}
/* Did we expire the login attempts? */
if (count > attemptCount-1)
{
lines = 0;
mesg[lines++] = "<C>Login Error";
mesg[lines++] = " ";
mesg[lines++] = "<C>Too many attempyts to login.";
mesg[lines++] = "<C>Exiting.";
popupLabel (GPCdkScreen, mesg, lines);
return 0;
}
return dbProcess;
}
/*
* Let the user login.
*/
DBPROCESS *loginToSybase (CDKSCREEN *screen, char *accountName, char *accountPassword)
{
CDKENTRY *loginEntry = 0;
CDKENTRY *passwordEntry = 0;
LOGINREC *dbLogin = 0;
char *hostAccount = 0;
char *login = accountName;
char *password = accountPassword;
char *mesg[10], temp[256];
/* Draw the screen. */
refreshCDKScreen (screen);
/* Define the login entry field. */
if (login == 0)
{
loginEntry = newCDKEntry (screen, CENTER, CENTER,
"\n<C></B/5>Sybase Login\n",
"Account Name: ",
A_BOLD|COLOR_PAIR(8),
COLOR_PAIR(24)|'_', vMIXED,
20, 1, 20, TRUE, FALSE);
/* Use the current account name as the default answer. */
hostAccount = getlogin();
setCDKEntryValue (loginEntry, hostAccount);
/* Get the login. */
while (1)
{
/* Redraw the screen. */
eraseCDKScreen (ScreenOf(loginEntry));
refreshCDKScreen (ScreenOf(loginEntry));
/* Get the login to the sybase account. */
login = copyChar (activateCDKEntry (loginEntry, 0));
/* Check if they hit escape. */
if (loginEntry->exitType == vESCAPE_HIT)
{
mesg[0] = "<C></U>Error";
mesg[1] = "A user name must be provided.";
popupLabel (screen, mesg, 2);
}
else
{
break;
}
}
/* Destroy the widget. */
destroyCDKEntry (loginEntry);
}
/* Get the password if we need too. */
if (password == 0)
{
sprintf (temp, "\n<C></B/5>%s's Password\n", login);
passwordEntry = newCDKEntry (screen, CENTER, CENTER,
temp, "Account Password: ",
A_BOLD|COLOR_PAIR(8),
COLOR_PAIR(24)|'_', vHMIXED,
20, 0, 20, TRUE, FALSE);
setCDKEntryHiddenChar (passwordEntry, '*');
/* Get the password. (the account may not have a password.) */
password = copyChar (activateCDKEntry (passwordEntry, 0));
if ((passwordEntry->exitType == vESCAPE_HIT) ||
(strlen(password) == 0))
{
password = "";
}
/* Destroy the widget. */
destroyCDKEntry (passwordEntry);
}
/*
* Try to connect to the database and get a LOGINREC structure.
*/
if ((dbLogin = dblogin()) == 0)
{
mesg[0] = "<C></U>Fatal Error";
mesg[1] = "<C>Could not connect to the Sybase database.";
popupLabel (screen, mesg, 2);
refreshCDKScreen (screen);
ExitProgram (EXIT_FAILURE);
}
/*
* Set the login and password and try to login to the database.
*/
DBSETLUSER (dbLogin, login);
DBSETLPWD (dbLogin, password);
DBSETLAPP (dbLogin, "cdk_syb");
/* Create a dbprocess structure to communicate with the database. */
return dbopen (dbLogin, 0);
}
/*
* This actually runs the command.
*/
void runIsqlCommand (CDKSCREEN *screen, CDKSWINDOW *swindow, DBPROCESS *dbProcess)
{
/* Declare local variables. */
RETCODE returnCode;
int rowCount;
/* Add in a output separation line. */
addCDKSwindow (swindow, "<C><#HL(5)> Start of Output <#HL(5)>", BOTTOM);
/* Run the command. */
dbsqlexec (dbProcess);
/* Check the return code of the commands. */
while ((returnCode = dbresults (dbProcess)) != NO_MORE_RESULTS)
{
if (returnCode == FAIL)
{
/* Oops, the command bombed. */
addCDKSwindow (swindow, "</5/16>Command failed.", BOTTOM);
}
else
{
if (!(DBCMDROW (dbProcess)))
{
/* The command could not return any rows. */
addCDKSwindow (swindow, "</5/16>Command could not return rows.", BOTTOM);
}
else
{
/*
* The command returned some rows, print out the title.
*/
char *row = assembleTitle (dbProcess);
addCDKSwindow (swindow, row, BOTTOM);
freeChar (row);
/* For each row returned, assemble the info. */
rowCount = 0;
while (dbnextrow (dbProcess) != NO_MORE_ROWS)
{
row = assembleRow (dbProcess);
addCDKSwindow (swindow, row, BOTTOM);
freeChar (row);
}
}
}
}
/* Add in a output separation line. */
addCDKSwindow (swindow, "<C><#HL(5)> End of Output <#HL(5)>", BOTTOM);
addCDKSwindow (swindow, "", BOTTOM);
/* Can the query... */
dbcanquery (dbProcess);
}
/*
* This creates a single line from the column widths and values.
*/
char *assembleTitle (DBPROCESS *dbProc)
{
char *colName = 0;
int colWidth = 0;
int colNameLen = 0;
int colCount = dbnumcols (dbProc);
int x = 0;
char temp[MAXWIDTH];
char row[MAXWIDTH];
/* Clean the row out. */
memset (row, '\0', MAXWIDTH);
/* Start assembling the row. */
for (x=1; x <= colCount; x++)
{
colName = dbcolname (dbProc, x);
colWidth = getColWidth (dbProc, x);
colNameLen = strlen (colName);
/* If we need to pad, then pad. */
if (colNameLen < colWidth)
{
/* Create a string the same length as the col width. */
memset (temp, '\0', MAXWIDTH);
memset (temp, ' ', (colWidth-colNameLen));
/* Copy the name. */
sprintf (row, "%s %s%s", row, colName, temp);
}
else
{
/* Copy the name. */
sprintf (row, "%s %s", row, colName);
}
}
return copyChar (row);
}
/*
* This assembles a single row.
*/
char *assembleRow (DBPROCESS *dbProcess)
{
char *dataValue = 0;
int colCount = dbnumcols (dbProcess);
int columnType = 0;
int colWidth = 0;
int valueLen = 0;
char value[MAXWIDTH];
char temp[MAXWIDTH];
char row[MAXWIDTH];
char format[20];
int x;
/* Clean out the row. */
memset (row, '\0', MAXWIDTH);
/* Start assembling the row. */
for (x=1; x <= colCount; x++)
{
columnType = (int)dbcoltype (dbProcess, x);
colWidth = (int)getColWidth (dbProcess, x);
valueLen = (int)dbdatlen (dbProcess, x);
/* Check the column type. */
if (columnType == SYBINT1)
{
DBINT object_id = *((DBINT *)dbdata(dbProcess, x));
sprintf (format, "%%-%dd", colWidth);
sprintf (value, format, (int)object_id);
}
else if (columnType == SYBINT2)
{
DBINT object_id = *((DBINT *)dbdata(dbProcess, x));
sprintf (format, "%%-%dd", colWidth);
sprintf (value, format, (int)object_id);
}
else if (columnType == SYBINT4)
{
DBINT object_id = *((DBINT *)dbdata(dbProcess, x));
sprintf (format, "%%-%dd", colWidth);
sprintf (value, format, (int)object_id);
}
else if (columnType == SYBREAL)
{
DBREAL object_id = *((DBREAL *)dbdata(dbProcess, x));
sprintf (format, "%%-%d.2f", colWidth);
sprintf (value, format, object_id);
}
else if (columnType == SYBFLT8)
{
DBFLT8 object_id = *((DBFLT8 *)dbdata(dbProcess, x));
sprintf (format, "%%-%d.2f", colWidth);
sprintf (value, format, object_id);
}
else
{
if (valueLen <= 0)
{
strcpy (value, " ");
}
else
{
memset (value, '\0', MAXWIDTH);
dataValue = (DBCHAR *)dbdata (dbProcess, x);
strncpy (value, dataValue, valueLen);
}
}
/* If we need to pad, then pad. */
if (valueLen < colWidth)
{
/* Copy the value into the string. */
memset (temp, '\0', MAXWIDTH);
memset (temp, ' ', (colWidth-valueLen));
sprintf (row, "%s %s%s", row, value, temp);
}
else
{
sprintf (row, "%s %s", row, value);
}
}
return copyChar (row);
}
/*
* This function returns the correct width of a column, taking
* into account the width of the title, the width of the data
* element.
*/
int getColWidth (DBPROCESS *dbProcess, int col)
{
char *colName = dbcolname (dbProcess, col);
int colNameLen = strlen(colName);
int colWidth = dbcollen (dbProcess, col);
int columnType = (int)dbcoltype (dbProcess, col);
/* If the colType is int/real/float adjust accordingly. */
if (columnType == SYBINT1 || columnType == SYBINT2 ||
columnType == SYBINT4)
{
colWidth = 5;
}
else if (columnType == SYBREAL || columnType == SYBFLT8)
{
colWidth = 8;
}
/* Is the name of the column wider than the col width? */
if (colNameLen >= colWidth)
{
return (colNameLen+1);
}
return colWidth;
}
/*
* This callback allows the user to play with the scrolling window.
*/
int viewHistoryCB (EObjectType cdktype, void *object, void *clientData, chtype key)
{
CDKSWINDOW *swindow = (CDKSWINDOW *)clientData;
CDKENTRY *entry = (CDKENTRY *)object;
/* Let them play... */
activateCDKSwindow (swindow, 0);
/* Redraw the entry field. */
drawCDKEntry (entry, ObjOf(entry)->box);
return (TRUE);
}
/*
* This displays a little introduction screen.
*/
void intro (CDKSCREEN *screen)
{
int lines = 0;
char *mesg[10];
/* Create the message. */
mesg[lines++] = "";
mesg[lines++] = "<C></B/16>Sybase Command Interface";
mesg[lines++] = "<C>Written by Mike Glover";
mesg[lines++] = "";
mesg[lines++] = "<C>Type </B>help<!B> to get help.";
/* Display the message. */
popupLabel (screen, mesg, lines);
}
/*
* This function displays help.
*/
void help (CDKENTRY *entry)
{
int lines = 0;
char *mesg[25];
/* Create the help message. */
mesg[lines++] = "<C></B/29>Help";
mesg[lines++] = "";
mesg[lines++] = "</B/24>When in the command line.";
mesg[lines++] = "<B=Up Arrow > Scrolls back one command.";
mesg[lines++] = "<B=Down Arrow> Scrolls forward one command.";
mesg[lines++] = "<B=Tab > Activates the scrolling window.";
mesg[lines++] = "<B=help > Displays this help window.";
mesg[lines++] = "";
mesg[lines++] = "</B/24>When in the scrolling window.";
mesg[lines++] = "<B=l or L > Loads a file into the window.";
mesg[lines++] = "<B=s or S > Saves the contents of the window to a file.";
mesg[lines++] = "<B=Up Arrow > Scrolls up one line.";
mesg[lines++] = "<B=Down Arrow> Scrolls down one line.";
mesg[lines++] = "<B=Page Up > Scrolls back one page.";
mesg[lines++] = "<B=Page Down > Scrolls forward one page.";
mesg[lines++] = "<B=Tab or Esc> Returns to the command line.";
mesg[lines++] = "<B=? > Displays this help window.";
mesg[lines++] = "";
mesg[lines++] = "<C> (</B/24>Refer to the scrolling window online manual for more help<!B!24>.)";
/* Pop up the help message. */
popupLabel (ScreenOf(entry), mesg, lines);
}
/*
* This converts a word to upper case.
*/
char *uc (char *word)
{
int length = strlen (word);
char *upper = (char *)malloc (sizeof (char *) * (length+2));
int x;
/* Start converting the case. */
for (x=0; x < length; x++)
{
int ch = (unsigned char)(word[x]);
if (isalpha (ch))
{
upper[x] = toupper(ch);
}
else
{
upper[x] = word[x];
}
}
upper[length] = '\0';
return upper;
}
/*
* The following two functions are the error and message handler callbacks.
* which will be called by Sybase when and error occurs.
*/
int err_handler (DBPROCESS *dbProcess, DBINT mesgNumber, int mesgState, int severity, char *mesgText)
{
/* Declare local variables. */
char *mesg[10], temp[256];
int errorCount = 0;
/* Check if the process is dead. */
if ((dbProcess == 0) || (DBDEAD(dbProcess)))
{
mesg[0] = "</B/32>Database Process Error";
mesg[1] = "<C>The database process seems to have died.";
mesg[2] = "<C>Try logging in again with the </R>login<!R> command";
popupLabel (GPCdkScreen, mesg, 3);
return INT_EXIT;
}
else
{
mesg[0] = "</B/32>DB-Library Error";
sprintf (temp, "<C>%s", dberrstr);
mesg[1] = copyChar (temp);
errorCount = 2;
}
/* Display the message if we have an error. */
if (errorCount > 0)
{
popupLabel (GPCdkScreen, mesg, --errorCount);
}
/* Clean up. */
if (errorCount == 2)
{
freeChar (mesg[1]);
}
return (1);
}
int mesg_handler (DBPROCESS *dbProcess, DBINT mesgNumber, int mesgState, int severity, char *mesgText)
{
/* Declare local variables. */
char *mesg[10], temp[256];
/* Create the message. */
mesg[0] = "<C></B/16>SQL Server Message";
sprintf (temp, "</R>Message Number<!R> %ld", mesgNumber);
mesg[1] = copyChar (temp);
sprintf (temp, "</R>State <!R> %d", mesgState);
mesg[2] = copyChar (temp);
sprintf (temp, "</R>Severity <!R> %d", severity);
mesg[3] = copyChar (temp);
sprintf (temp, "</R>Message <!R> %s", mesgText);
mesg[4] = copyChar (temp);
popupLabel (GPCdkScreen, mesg, 5);
/* Clean up. */
freeChar (mesg[1]); freeChar (mesg[2]);
freeChar (mesg[3]); freeChar (mesg[4]);
return (1);
}
/*
* This is for the scrolling window help callback.
*/
int swindowHelpCB (EObjectType cdktype, void *object, void *clientData, chtype key)
{
CDKENTRY *entry = (CDKENTRY *)clientData;
help(entry);
return (TRUE);
}
/*
* This is the callback for the down arrow.
*/
int historyUpCB (EObjectType cdktype, void *object, void *clientData, chtype key)
{
CDKENTRY *entry = (CDKENTRY *)object;
struct history_st *history = (struct history_st *) clientData;
/* Make sure we don't go out of bounds. */
if (history->current == 0)
{
Beep();
return (TRUE);
}
/* Decrement the counter. */
history->current--;
/* Display the command. */
setCDKEntryValue (entry, history->cmd_history[history->current]);
drawCDKEntry (entry, ObjOf(entry)->box);
return (TRUE);
}
/*
* This is the callback for the down arrow.
*/
int historyDownCB (EObjectType cdktype, void *object, void *clientData, chtype key)
{
CDKENTRY *entry = (CDKENTRY *)object;
struct history_st *history = (struct history_st *) clientData;
/* Make sure we don't go out of bounds. */
if (history->current == history->count)
{
Beep();
return (TRUE);
}
/* Increment the counter... */
history->current++;
/* If we are at the end, clear the entry field. */
if (history->current == history->count)
{
cleanCDKEntry (entry);
drawCDKEntry (entry, ObjOf(entry)->box);
return (TRUE);
}
/* Display the command. */
setCDKEntryValue (entry, history->cmd_history[history->current]);
drawCDKEntry (entry, ObjOf(entry)->box);
return (TRUE);
}
/*
* This callback allows the user to pick from the history list from a
* scrolling list.
*/
int listHistoryCB (EObjectType cdktype, void *object, void *clientData, chtype key)
{
CDKSCROLL *scrollList = 0;
CDKENTRY *entry = (CDKENTRY *)object;
struct history_st *history = (struct history_st *) clientData;
int height = (history->count < 10 ? history->count+3 : 13);
int selection;
/* No history, no list. */
if (history->count == 0)
{
/* Popup a little window telling the user there are no commands. */
char *mesg[] = {"<C></B/16>No Commands Entered", "<C>No History"};
popupLabel (ScreenOf(entry), mesg, 2);
/* Redraw the screen. */
eraseCDKEntry (entry);
drawCDKScreen (ScreenOf(entry));
/* And leave... */
return (TRUE);
}
/* Create the scrolling list of previous commands. */
scrollList = newCDKScroll (ScreenOf(entry), CENTER, CENTER, RIGHT,
height, -10, "<C></B/29>Command History",
history->cmd_history, history->count,
NUMBERS, A_REVERSE, TRUE, FALSE);
/* Get the command to execute. */
selection = activateCDKScroll (scrollList, 0);
destroyCDKScroll (scrollList);
/* Check the results of the selection. */
if (selection >= 0)
{
/* Get the command and stick it back in the entry field. */
setCDKEntryValue (entry, history->cmd_history[selection]);
}
/* Redraw the screen. */
eraseCDKEntry (entry);
drawCDKScreen (ScreenOf(entry));
return (TRUE);
}
/*
* This loads the history into the editor from the RC file.
*/
void loadHistory (struct history_st *history)
{
char *home = 0;
char filename[1000];
/* Create the RC filename. */
if ((home = getenv ("HOME")) == 0)
{
home = ".";
}
sprintf (filename, "%s/.sybrc", home);
/* Set some variables. */
history->current = 0;
history->count = 0;
/* Read the file. */
if ((history->count = CDKreadFile (filename, &(history->cmd_history))) != -1)
{
history->current = history->count;
}
return;
}
/*
* This saves the history into RC file.
*/
void saveHistory (struct history_st *history, int count)
{
FILE *fd = 0;
char *home = 0;
char filename[1000];
int x;
/* Create the RC filename. */
if ((home = getenv ("HOME")) == 0)
{
home = ".";
}
sprintf (filename, "%s/.sybrc", home);
/* Open the file for writing. */
if ((fd = fopen (filename, "w")) == 0)
{
return;
}
/* Start saving the history. */
for (x=0; x < history->count; x++)
{
fprintf (fd, "%s\n", history->cmd_history[x]);
}
fclose (fd);
return;
}
|