⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fisql.c

📁 在Linux/Unix下面访问WINDOWS SQLSERVER 的ODBC驱动程序
💻 C
📖 第 1 页 / 共 2 页
字号:
	}	if (!(isatty(fileno(stdin)))) {		no_prompt = 1;		rl_outstream = fopen("/dev/null", "rw");	}	rl_readline_name = "fisql";	rl_bind_key('\t', rl_insert);	if (password == NULL) {		password = (char *) xmalloc(READPASSPHRASE_MAXLEN);		readpassphrase("Password: ", password, READPASSPHRASE_MAXLEN, RPP_ECHO_OFF);	}	if (input_filename) {		if (freopen(input_filename, "r", stdin) == NULL) {			/* XXX: sybase isql generates this error while parsing the options,			 * but doesn't redirect the input until after the Password: prompt			 */			/* lack of newline for bug-compatibility with isql */			fprintf(stderr, "Unable to open input file '%s'.", optarg);			exit(EXIT_FAILURE);		}	}	if (output_filename) {		if (freopen(output_filename, "w", stdout) == NULL) {			/* XXX: sybase isql generates this error while parsing the options,			 * but doesn't redirect the output until after the Password: prompt			 */			/* lack of newline for bug-compatibility with isql */			fprintf(stderr, "Unable to open output file '%s'.", output_filename);			exit(EXIT_FAILURE);		}	}	if (isatty(fileno(stdin))) {		rl_outstream = stdout;	}	dbinit();#if 0#ifdef DBVERSION_100	dbsetversion(DBVERSION_100);#endif#endif	if ((login = dblogin()) == NULL) {		reset_term();		exit(EXIT_FAILURE);	}	dbmsghandle(msg_handler);	dberrhandle(err_handler);	DBSETLAPP(login, "fisql");	if (username) {		DBSETLUSER(login, username);	}	DBSETLPWD(login, password);	if (char_set) {		DBSETLCHARSET(login, char_set);	}	if (use_encryption) {		DBSETLENCRYPT(login, TRUE);	}	if (hostname) {		DBSETLHOST(login, hostname);	}	if (language) {		DBSETLNATLANG(login, language);	}	if (size) {		DBSETLPACKET(login, (short) size);	}	if (interfaces_filename) {		dbsetifile(interfaces_filename);	}	dbsettime(timeout);	if (logintime >= 0) {		dbsetlogintime(logintime);	}	if ((dbproc = dbopen(login, server)) == NULL) {		fprintf(stderr, "fisql: dbopen() failed.\n");		reset_term();		exit(EXIT_FAILURE);	}	dbsetopt(dbproc, DBPRLINESEP, lineseparator, strlen(lineseparator));	if (colseparator) {		dbsetopt(dbproc, DBPRCOLSEP, colseparator, strlen(colseparator));	}	if (columnwidth) {		dbsetopt(dbproc, DBPRLINELEN, columnwidth, 0);	}	if (chained_transactions) {		dbsetopt(dbproc, DBCHAINXACTS, NULL, 0);	}	if (fipsflagger) {		dbsetopt(dbproc, DBFIPSFLAG, NULL, 0);	}	if (perfstats) {		dbsetopt(dbproc, DBSTAT, "time", 0);	}	while (1) {		if (sigsetjmp(restart, 1)) {			if (ibuf) {				for (i = 0; i < ibuflines; i++) {					free(ibuf[i]);				}				ibuflines = 0;				free(ibuf);				ibuf = NULL;			}			fputc('\n', stdout);			rl_on_new_line();			rl_reset_line_state();		}		dbcancel(dbproc);		signal(SIGINT, inactive_interrupt_handler);		ibuf = (char **) xmalloc(sizeof(char *));		ibuflines = 0;		while (1) {			if (no_prompt) {				foobuf[0] = '\0';			} else {				sprintf(foobuf, "%d>> ", ibuflines + 1);			}			line = readline(foobuf);			if (line == NULL) {				line = "exit";			}			for (cp = line; *cp && isspace((unsigned char) *cp); cp++);			if (*cp) {				add_history(line);			}			if (!(strncasecmp(line, "!!", 2))) {				cp = line + 2;				system(cp);				continue;			}			/* XXX: isql off-by-one line count error for :r not duplicated */			if (!(strncasecmp(line, ":r", 2))) {				for (cp = line + 2; *cp && (isspace((unsigned char) *cp)); cp++);				tfn = cp;				for (; *cp && !(isspace((unsigned char) *cp)); cp++);				*cp = '\0';				if ((fp = fopen(tfn, "r")) == NULL) {					printf("Operating system error: Failed to open %s.\n", tfn);					continue;				}				tmpfp = rl_instream;				tmpfp2 = rl_outstream;				rl_instream = fp;				rl_outstream = fopen("/dev/null", "w");				while ((line = readline("")) != NULL) {					ibuf[ibuflines++] = line;					ibuf = (char **) xrealloc(ibuf, (ibuflines + 1) * sizeof(char *));				}				rl_instream = tmpfp;				fclose(rl_outstream);				rl_outstream = tmpfp2;				fclose(fp);				fputc('\r', stdout);				fflush(stdout);				continue;			}			firstword = (char *) xmalloc((strlen(line) + 1) * sizeof(char));			strcpy(firstword, line);			for (cp = firstword; *cp; cp++) {				if (isspace((unsigned char) *cp)) {					*cp = '\0';					break;				}			}			if ((!(strcasecmp(firstword, "exit")))			    || (!(strcasecmp(firstword, "quit")))) {				reset_term();				dbexit();				exit(EXIT_SUCCESS);			}			if (!(strcasecmp(firstword, "reset"))) {				for (i = 0; i < ibuflines; i++) {					free(ibuf[i]);				}				ibuflines = 0;				continue;			}			if (!(strcasecmp(firstword, cmdend))) {				if (ibuflines == 0) {					continue;				}				free(firstword);				break;			}			if ((!(strcasecmp(firstword, "vi")))			    || (!(strcasecmp(firstword, editor)))) {				int tmpfd;				strcpy(tmpfn, "/tmp/fisqlXXXXXX");				tmpfd = mkstemp(tmpfn);				if ((fp = fdopen(tmpfd, "w")) == NULL) {					perror("fisql");					reset_term();					dbexit();					exit(2);				}				if (ibuflines) {					for (i = 0; i < ibuflines; i++) {						fputs(ibuf[i], fp);						fputc('\n', fp);						free(ibuf[i]);					}				} else {					for (i = 0; ((sqlch = dbgetchar(dbproc, i)) != NULL); i++) {						fputc(*sqlch, fp);					}				}				fclose(fp);				if (!(strcmp(firstword, "vi"))) {					edit("vi", tmpfn);				} else {					edit(editor, tmpfn);				}				ibuflines = 0;				fp = fopen(tmpfn, "r");				tmpfp = rl_instream;				rl_instream = fp;				strcpy(foobuf, "1>> ");				while ((line = readline(foobuf)) != NULL) {					ibuf[ibuflines++] = line;					sprintf(foobuf, "%d>> ", ibuflines + 1);					ibuf = (char **) xrealloc(ibuf, (ibuflines + 1) * sizeof(char *));				}				rl_instream = tmpfp;				fclose(fp);				fputc('\r', stdout);				fflush(stdout);				unlink(tmpfn);				continue;			}			free(firstword);			ibuf[ibuflines++] = line;			ibuf = (char **) xrealloc(ibuf, (ibuflines + 1) * sizeof(char *));		}		dbfreebuf(dbproc);		for (i = 0; i < ibuflines; i++) {			if (echo) {				puts(ibuf[i]);			}			dbcmd(dbproc, ibuf[i]);			dbcmd(dbproc, "\n");			free(ibuf[i]);		}		free(ibuf);		ibuf = NULL;		signal(SIGINT, active_interrupt_handler);		dbsetinterrupt(dbproc, (void *) active_interrupt_pending, (void *) active_interrupt_servhandler);		if (dbsqlexec(dbproc) == SUCCEED) {			maybe_handle_active_interrupt();			while ((dbrc = dbresults(dbproc)) != NO_MORE_RESULTS) {				printedlines = 0;#define USE_DBPRROW 0#if USE_DBPRROW				dbprhead(dbproc);				dbprrow(dbproc);#else				if ((dbrc == SUCCEED) && (DBROWS(dbproc) == SUCCEED)) {					prbuflen = dbspr1rowlen(dbproc);					prbuf = (char *) xmalloc(prbuflen * sizeof(char));					dbsprhead(dbproc, prbuf, prbuflen);					fputs(prbuf, stdout);					fputc('\n', stdout);					dbsprline(dbproc, prbuf, prbuflen, '-');					fputs(prbuf, stdout);					fputc('\n', stdout);					maybe_handle_active_interrupt();					while ((dbrc = dbnextrow(dbproc)) != NO_MORE_ROWS) {						if (dbrc == FAIL) {							break;						}						if (dbrc != REG_ROW) {							num_cols = dbnumalts(dbproc, dbrc);							for (selcol = col = 1; col <= num_cols; col++) {								colid = dbaltcolid(dbproc, dbrc, col);								while (selcol < colid) {									collen = get_printable_column_size(dbproc, selcol);									for (i = 0; i < collen; i++) {										putchar(' ');									}									selcol++;									printf("%s", colseparator);								}								opname = dbprtype(dbaltop(dbproc, dbrc, col));								printf("%s", opname);								collen = get_printable_column_size(dbproc, colid);								collen -= strlen(opname);								while (collen-- > 0) {									putchar(' ');								}								selcol++;								printf("%s", colseparator);							}							printf("%s", lineseparator);							for (selcol = col = 1; col <= num_cols; col++) {								colid = dbaltcolid(dbproc, dbrc, col);								while (selcol < colid) {									collen = get_printable_column_size(dbproc, selcol);									for (i = 0; i < collen; i++) {										putchar(' ');									}									selcol++;									printf("%s", colseparator);								}								collen = get_printable_column_size(dbproc, colid);								adash = '-';								bylist = dbbylist(dbproc, dbrc, &nby);								if (nby == 0) {									adash = '=';								}								for (i = 0; i < collen; i++) {									putchar(adash);								}								selcol++;								printf("%s", colseparator);							}							printf("%s", lineseparator);							for (selcol = col = 1; col <= num_cols; col++) {								colid = dbaltcolid(dbproc, dbrc, col);								while (selcol < colid) {									collen = get_printable_column_size(dbproc, selcol);									for (i = 0; i < collen; i++) {										putchar(' ');									}									selcol++;									printf("%s", colseparator);								}								convlen = dbconvert(dbproc,										    dbalttype(dbproc, dbrc, col),										    dbadata(dbproc, dbrc, col),										    dbadlen(dbproc, dbrc, col),										    SYBCHAR, (BYTE *) adbuf, sizeof(adbuf));								printf("%.*s", (int) convlen, adbuf);								collen = get_printable_column_size(dbproc, colid);								collen -= convlen;								while (collen-- > 0) {									putchar(' ');								}								selcol++;								printf("%s", colseparator);							}							printf("%s", lineseparator);							printedcompute = 1;							continue;						}						if (printedcompute || (headers && (printedlines >= headers)								       && ((printedlines % headers) == 0))) {							fputc('\n', stdout);							dbsprhead(dbproc, prbuf, prbuflen);							fputs(prbuf, stdout);							fputc('\n', stdout);							dbsprline(dbproc, prbuf, prbuflen, '-');							fputs(prbuf, stdout);							fputc('\n', stdout);							printedcompute = 0;						}						printedlines++;						dbspr1row(dbproc, prbuf, prbuflen);						fputs(prbuf, stdout);						fputc('\n', stdout);						maybe_handle_active_interrupt();					}					fputc('\n', stdout);					free(prbuf);					maybe_handle_active_interrupt();				}#endif				if (dbrc != FAIL) {					if ((DBCOUNT(dbproc) >= 0) || dbhasretstat(dbproc)) {						if (DBCOUNT(dbproc) >= 0) {							fprintf(stdout, "(%d rows affected", (int) DBCOUNT(dbproc));							if (dbhasretstat(dbproc)) {								dbrc = dbretstatus(dbproc);								fprintf(stdout, ", return status = %d", dbrc);							}							fprintf(stdout, ")\n");						} else {							if (dbhasretstat(dbproc)) {								dbrc = dbretstatus(dbproc);								fprintf(stdout, "(return status = %d)\n", dbrc);							}						}					}				}			}		}	}	reset_term();	dbexit();	exit(EXIT_FAILURE);	return (0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -