-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbsyntax.ec
118 lines (98 loc) · 2.59 KB
/
dbsyntax.ec
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
/*
dbsyntax.ec - checks the syntax of SQL statements
Copyright (C) 1993 David A. Snyder
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef lint
static char sccsid[] = "@(#) dbsyntax.ec 1.0 93/12/24 20:19:03";
#endif /* not lint */
#include <stdio.h>
$include sqlca;
#define SUCCESS 0
char *database = NULL;
extern char *optarg;
extern int optind, opterr;
/* ARGSUSED */
main(argc, argv)
int argc;
char *argv[];
{
$char buf[4096], exec_stmt[32];
int c, dflg = 0, errflg = 0;
void perror();
/* Print copyright message */
(void)fprintf(stderr, "DBSYNTAX version 1.0, Copyright (C) 1993 David A. Snyder\n\n");
/* get command line options */
while ((c = getopt(argc, argv, "d:")) != EOF)
switch (c) {
case 'd':
dflg++;
database = optarg;
break;
default:
errflg++;
break;
}
/* validate command line options */
if (errflg || !dflg) {
(void)fprintf(stderr, "usage: %s -d dbname [files...]\n", argv[0]);
exit(1);
}
/* locate the database in the system */
sprintf(exec_stmt, "database %s", database);
$prepare db_exec from $exec_stmt;
$execute db_exec;
if (sqlca.sqlcode != SUCCESS) {
(void)fprintf(stderr, "Database not found or no system permission.\n\n");
exit(1);
}
for (c = 0; c < optind; c++, *++argv) ;
if (!*argv)
process(argc, *argv, buf);
else
do {
if (!freopen(*argv, "r", stdin)) {
perror(*argv);
continue;
}
process(argc, *argv, buf);
} while (*++argv);
return(0);
}
process(argc, argv, buf)
int argc;
char *argv, *buf;
{
$char *s1, s2[BUFSIZ], msg[BUFSIZ];
s1 = buf;
while ((*s1 = getchar()) != EOF) {
switch (*s1++) {
case '{':
*s1--;
while ((*s1 = getchar()) != '}')
;
break;
case ';':
*s1 = '\0';
s1 = buf;
$prepare stmtid from $s1;
if (sqlca.sqlcode != SUCCESS) {
if (*argv && argc - optind > 1)
(void)printf("%s: ", argv);
(void)rgetmsg(sqlca.sqlcode, msg, sizeof(msg));
(void)sprintf(s2, msg, sqlca.sqlerrm);
(void)printf("%s\n%s\n\n", s1, s2);
}
break;
}
}
}