/[vdw]/trunk/exec_sql.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/exec_sql.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (hide annotations)
Sun Feb 6 05:28:38 2005 UTC (19 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 4671 byte(s)
initial import into svn

1 dpavlin 1 /*
2     * This code is copied from the O'Reilly Lex & Yacc book, 2nd Edition,
3     * 1995, by John R. Levine, Tony Mason, and Doug Brown.
4     *
5     * Enhancements added: exec_sql() and embedded perl.
6     * Jeremy Hickerson, 5/8/2002
7     */
8    
9     #include <stdio.h>
10     #include <string.h>
11     #include <EXTERN.h>
12    
13     #include <perl.h>
14     #include "flexdef.h"
15    
16     #include "obj_srvr_tab.h"
17    
18     #include "obj_srvr.h"
19    
20     static PerlInterpreter *my_perl;
21    
22     extern FILE *yyout; /* lex output file */
23    
24     #define EMBED_PERL(arg_expr, call_expr) \
25     dSP; \
26     int i, count; \
27     ENTER; \
28     SAVETMPS; \
29     PUSHMARK(SP); \
30     arg_expr \
31     PUTBACK; \
32     call_expr \
33     SPAGAIN ; \
34     if (count != 1) \
35     croak("Big trouble\n") ; \
36     strcpy(perl_buf, POPp); \
37     PUTBACK ; \
38     FREETMPS; \
39     LEAVE;
40    
41     char save_buf[SQL_SIZE]; /* buffer for SQL command */
42     char *savebp; /* current buffer pointer */
43     char buf[BUFSIZE];
44     char perl_buf[BUFSIZE];
45     char args[10][ARGSIZE];
46    
47     /* external prototypes */
48     EXTERN_C void xs_init (pTHXo);
49    
50     /* local prototypes */
51     int exec_sql();
52     int call_perl_get_data();
53     int call_perl_sub(char *sub, int argc);
54    
55    
56     /* start an embedded command after EXEC SQL */
57     start_save(void) {
58     savebp = save_buf;
59     glb_errflag = 0;
60     savebp[0] = '\0'; /* discard previous text */
61     }
62    
63     /* save a SQL token */
64     save_str(char *s) {
65    
66     strcpy(savebp, s);
67     savebp += strlen(s);
68    
69     strcpy(yylval.strval, strdup(s) );
70    
71     }
72    
73    
74     /* end of SQL command, now write it out */
75     end_sql(void) {
76     int i;
77     register char *cp;
78     savebp--; /* back over the closing semicolon */ /* jhjh - don't want? */
79    
80     /* jdh, 5/8/2002: call "exec_sql" function */
81     exec_sql();
82    
83     /* return scanner to regular mode */
84     un_sql();
85     }
86    
87    
88     /* jdh, 10/22/2002 */
89     int exec_sql() {
90    
91     if (glb_errflag)
92     return 0; /* don't abort; error message will have been returned */
93    
94     /* fall-through */
95     call_perl_get_data();
96    
97     return 0;
98     }
99    
100     /* jdh, 10/22/2002 */
101     int call_perl_get_data() {
102    
103     strcpy(args[0], parsed_sql.column_list);
104     strcpy(args[1], parsed_sql.table_list);
105     strcpy(args[2], parsed_sql.where_clause);
106     strcpy(args[3], parsed_sql.order_by);
107    
108     call_perl_sub("get_data", 4);
109    
110     return 0;
111     }
112    
113     char *call_perl_like2re(char *word1, char *word2, char *word3, char *word4) {
114    
115     EMBED_PERL(
116    
117     XPUSHs(sv_2mortal(newSVpv(word1, 0)));
118     XPUSHs(sv_2mortal(newSVpv(word2, 0)));
119     XPUSHs(sv_2mortal(newSVpv(word3, 0)));
120     XPUSHs(sv_2mortal(newSVpv(word4, 0)));,
121    
122     count = call_pv("like2re", G_SCALAR); )
123    
124     return perl_buf;
125     }
126    
127     char *call_perl_tr_op(char *table, char *word1, char *word2, char *word3) {
128    
129     EMBED_PERL(
130    
131     XPUSHs(sv_2mortal(newSVpv(table, 0)));
132     XPUSHs(sv_2mortal(newSVpv(word1, 0)));
133     XPUSHs(sv_2mortal(newSVpv(word2, 0)));
134     XPUSHs(sv_2mortal(newSVpv(word3, 0)));,
135    
136     count = call_pv("tr_op", G_SCALAR); )
137    
138     return perl_buf;
139     }
140    
141     int call_perl_connect2client(char *remote, char *port) {
142    
143     strcpy(args[0], remote);
144     strcpy(args[1], port);
145    
146     call_perl_sub("connect2client", 2);
147    
148     return 0;
149     }
150    
151     char *call_perl_get_yyin(int size) {
152    
153     EMBED_PERL(XPUSHs(sv_2mortal(newSViv(size))); ,
154    
155     count = call_pv("get_yyin", G_SCALAR); )
156    
157     return perl_buf;
158     }
159    
160     int call_perl_send_yyout(char *s) {
161    
162     strcpy(args[0], s);
163    
164     call_perl_sub("send_yyout", 1);
165    
166     return 0;
167     }
168    
169     /* jdh, 10/22/2002 */
170     /* Note: all args must be (char *); we will convert these in perl sub.
171     * Sub must return (char *) */
172     int call_perl_sub(char *sub, int argc) {
173    
174     EMBED_PERL(
175    
176     for(i = 0; i < argc; i++) {
177     XPUSHs(sv_2mortal(newSVpv(args[i], 0)));
178     },
179    
180     count = call_pv(sub, G_SCALAR); )
181    
182     return 0;
183     }
184    
185    
186     /* jdh, 5/8/2002 */
187     /* note to self: embed_perl is called from C code generated from obj_srvr.l */
188     /* thus: embed_perl_obj_srvr(2, perl_argv, NULL); */
189     /* where static char *perl_argv[] = {"obj_srvr", "obj_srvr.pl", "uid", */
190     /* "passwd", stdout}; */
191     int embed_perl_obj_srvr(int argc, char **argv, char **env) {
192    
193     my_perl = perl_alloc();
194     perl_construct(my_perl);
195    
196     perl_parse(my_perl, xs_init, argc, argv, env);
197     perl_run(my_perl); /* just to initialize */
198    
199     return 0;
200     }
201    
202    
203     /* jdh, 5/8/2002 */
204     end_perl(void) {
205     perl_destruct(my_perl);
206     perl_free(my_perl);
207     }
208    
209    
210     int char2str(int *c, char *str) { /* str must have size = 3 chars */
211    
212     str[0] = *c;
213     str[1] = (c + 1) == '=' || (c + 1) == '>' ? *(c + 1) : '\0';
214     str[2] = '\0';
215    
216     return 0;
217    
218     }

  ViewVC Help
Powered by ViewVC 1.1.26