File: form.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (369 lines) | stat: -rw-r--r-- 10,997 bytes parent folder | download | duplicates (2)
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
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h> 
#include <tcl.h>
#include <tk.h>
#include <locale.h>
#include "gis.h"
#include "dbmi.h"
#include "form.h"

/* Structure to store column names and values */
typedef struct {
    char *name;
    int  ctype;
    char *value;
} COLUMN;

char  *Drvname, *Dbname, *Tblname, *Key;

COLUMN *Cols = NULL; 
int aCols = 0;    /* allocated space */
int nCols = 0;  

int form_open = 0;

/* Close form */
int
close_form ( ClientData cdata, Tcl_Interp *interp, int argc, char *argv[] ) 
{
    G_debug ( 3, "close_form()" );
    form_open = 0;
    return TCL_OK;
}

/* Start new sql update */
int
reset_values ( ClientData cdata, Tcl_Interp *interp, int argc, char *argv[] ) 
{
    nCols = 0;
    Drvname = NULL;
    Dbname = NULL;
    Tblname = NULL;
    Key = NULL;

    return TCL_OK;
}

int
set_value ( ClientData cdata, Tcl_Interp *interp, int argc, char *argv[])
{
    G_debug ( 2, "set_value(): %s %s", argv[1], argv[2] );
 
    if ( strcmp(argv[1], F_DRIVER_FNAME ) == 0 ) {
	Drvname = G_store ( argv[2] );
    } else if ( strcmp(argv[1], F_DATABASE_FNAME ) == 0 ) {
	Dbname = G_store ( argv[2] );
    } else if ( strcmp(argv[1], F_TABLE_FNAME ) == 0 ) {
	Tblname = G_store ( argv[2] );
    } else if ( strcmp(argv[1], F_KEY_FNAME ) == 0 ) {
	Key = G_store ( argv[2] );
    } else { 
        if ( nCols == aCols ) {
	    aCols += 100;
	    Cols = (COLUMN*) G_realloc ( Cols, (aCols) * sizeof(COLUMN) );
        }
	Cols[nCols].name = G_store ( argv[1] );
	Cols[nCols].value = G_store ( argv[2] );
	nCols++;
    }

    return TCL_OK;
}

/* Update table, use the data previously stored by set_value() */
int
submit ( ClientData cdata, Tcl_Interp *interp, int argc, char *argv[])
{
    int i, first, ncols, found, col, sqltype, keyval = 0, ret;
    char buf[2001];
    dbString sql, table_name, strval;
    dbDriver *driver;
    dbHandle handle;
    dbTable  *table;
    dbColumn *column;

    G_debug ( 2, "submit()");
    
    db_init_string (&sql);
    db_init_string(&table_name);
    db_init_string(&strval);
    
    /* Check if all internal values are set */
    if ( Drvname == NULL || Dbname == NULL || Tblname == NULL || Key == NULL ) {
	G_warning ("db connection was not set by form\n");
        sprintf ( buf, "set submit_msg \"db connection was not set by form.\"" );
        Tcl_Eval(interp, buf);
        Tcl_Eval(interp, "set submit_result 0" );
	return TCL_OK; 
    }
    
    /* Get column types */
    G_debug ( 2, "Open driver" );
    driver = db_start_driver(Drvname);
    if (driver == NULL) { 
	G_warning ("Cannot open driver\n"); 
        sprintf ( buf, "set submit_msg \"Cannot open driver '%s'\"", Drvname );
        Tcl_Eval(interp, buf);
        Tcl_Eval(interp, "set submit_result 0" );
	return TCL_OK; 
    }
    G_debug ( 2, "Driver opened" );

    db_init_handle (&handle);
    db_set_handle (&handle, Dbname, NULL);
    G_debug ( 2, "Open database" );
    if (db_open_database(driver, &handle) != DB_OK){
	G_warning ("Cannot open database\n"); 
	db_shutdown_driver(driver); 
	sprintf ( buf, "set submit_msg \"Cannot open database '%s' by driver '%s'\"", Dbname, Drvname);
        Tcl_Eval(interp, buf);
        Tcl_Eval(interp, "set submit_result 0" );
	return TCL_OK;
    }
    G_debug ( 2, "Database opened" );
    
    db_set_string(&table_name, Tblname);
    if(db_describe_table (driver, &table_name, &table) != DB_OK) {
	G_warning ("Cannot describe table\n"); 
	db_shutdown_driver(driver); 
        db_close_database(driver);
	sprintf ( buf, "set submit_msg \"Cannot describe table '%s'\"", Tblname);
        Tcl_Eval(interp, buf);
        Tcl_Eval(interp, "set submit_result 0" );
	return TCL_OK;
    }
    ncols = db_get_table_number_of_columns(table);

    /* For each column get ctype */
    for ( i = 0; i < nCols; i++) {
	found = 0;
	for ( col = 0; col < ncols; col++) {
	    /* get keyval */
	    if ( G_strcasecmp( Cols[i].name, Key ) == 0 ) {
                keyval = atoi ( Cols[i].value );  
	    }
	    column = db_get_table_column (table, col);
	    if ( G_strcasecmp( db_get_column_name(column), Cols[i].name ) == 0 ) { 
		sqltype = db_get_column_sqltype (column);
		Cols[i].ctype = db_sqltype_to_Ctype(sqltype);
		found = 1;
		break;
	    }
	}
	if ( !found && (G_strcasecmp(Cols[i].name, F_ENCODING) != 0)) { 
	    G_warning ( "Cannot find column type" );
	    db_close_database(driver);
	    db_shutdown_driver(driver); 
	    sprintf ( buf, "set submit_msg \"Cannot find column type\"" );
	    Tcl_Eval(interp, buf);
	    Tcl_Eval(interp, "set submit_result 0" );
	    return TCL_OK;
	}
    }
    
    /* Construct update statement */
    sprintf (buf, "update %s set ", Tblname );
    db_set_string (&sql, buf);

    first = 1;
    for ( i = 0; i < nCols; i++ ) {
	if ( G_strcasecmp( Cols[i].name, Key ) == 0  )  continue;
		
	if (G_strcasecmp(Cols[i].name, F_ENCODING) == 0) {

	    G_debug(3, "env is %s, val is %s", G__getenv("GRASS_DB_ENCODING"),
		    Cols[i].value);

	    if (G_strcasecmp(Cols[i].value, G__getenv("GRASS_DB_ENCODING")) ==
		0)
		continue;
	    else {
		G_setenv("GRASS_DB_ENCODING", Cols[i].value);
		if ( Tcl_SetSystemEncoding(interp, Cols[i].value) == TCL_ERROR ) {
			fprintf(stderr, 
				"Could not set Tcl system encoding to %s\n", Cols[i].value);
		}
		db_close_database(driver);
		db_shutdown_driver(driver);
		sprintf(buf,
			"set submit_msg \"View data encoding now is %s\"",
			Cols[i].value);
		Tcl_Eval(interp, buf);
		Tcl_Eval(interp, "set submit_result 0");
		return TCL_OK;
	    }
	}

	if ( !first ) { db_append_string (&sql, ", "); }
	if ( Cols[i].ctype == DB_C_TYPE_INT || Cols[i].ctype == DB_C_TYPE_DOUBLE ) {
            sprintf (buf, "%s = %s", Cols[i].name, Cols[i].value );
	} else {
	    memset(buf, '\0', strlen(buf));
	    ret = Tcl_UtfToExternal(interp,
	    		      Tcl_GetEncoding(interp, G__getenv("GRASS_DB_ENCODING")),
	                      Cols[i].value, strlen(Cols[i].value), 0, NULL,
	                      buf, 2000, NULL, NULL, NULL);
	    
	    if ( ret != TCL_OK ) {
		G_warning ("Could not convert UTF to external.");
		db_set_string ( &strval, Cols[i].value );
	    } else {
	        db_set_string ( &strval, buf );
	    }

	    db_double_quote_string (&strval);
            sprintf (buf, "%s = '%s'", Cols[i].name, db_get_string(&strval) );
	}
        db_append_string (&sql, buf);
	first = 0;
    }

    sprintf (buf, " where %s = %d", Key, keyval );
    db_append_string (&sql, buf);
    
    G_debug ( 2, "SQL: %s", db_get_string(&sql) );

    /* Update table */
    ret = db_execute_immediate (driver, &sql);
	
    db_close_database(driver);
    db_shutdown_driver(driver); 
    
    if ( ret != DB_OK) {
	G_warning ("Cannot update table");
        Tcl_VarEval(interp, "set submit_msg \"Cannot update table:\n", db_get_error_msg(), "\"", NULL );
        Tcl_Eval(interp, "set submit_result 0" );
    } else { 
        Tcl_Eval(interp, "set submit_msg \"Record successfully updated\"" );
        Tcl_Eval(interp, "set submit_result 1" );
    }

    return TCL_OK;
}

/* 
*  Form 
*/
int 
main ( int argc, char *argv[] ) 
{
    int        length;
    int         ret;
    char        buf[5000];
    char        *child_html, *child_title;
    static FILE *child_send, *child_recv;
    static      Tcl_Interp *interp;
    static int  frmid = 0;
    char * encoding_val;

    G_gisinit ("form");

    G_debug ( 2, "Form: main()" );
    
    setlocale(LC_CTYPE, "");
        
    child_recv = stdin;
    child_send = stdout;

    while ( 1 ) {
	ret = read ( fileno(stdin) , &(buf[0]), 1);
	fcntl ( fileno(child_recv), F_SETFL, O_NONBLOCK); /* Don't wait if pipe is empty */
	if ( ret == 0 ) break; /* Pipe was closed by parent -> quit */
	if ( ret == 1 ) {
	    G_debug ( 3, "Form: received = '%c'", buf[0] );
	    if ( buf[0] == 'O' ) { 
		if ( !form_open ) {
	            G_debug ( 3, "Form is not opened" ); 
		    /* Open the window and display the form */
		    interp = Tcl_CreateInterp();
		    if (Tcl_Init(interp) == TCL_ERROR) G_fatal_error ( "Tcl_Init failed: %s\n", 
			                                      interp->result) ;
		    if (Tk_Init(interp) == TCL_ERROR) G_fatal_error ("Tk_Init failed: %s\n", 
			                                      interp->result);

		    Tcl_CreateCommand(interp, "submit", (Tcl_CmdProc*) submit, (ClientData) NULL, 
							(Tcl_CmdDeleteProc*) NULL);
		    Tcl_CreateCommand(interp, "set_value", (Tcl_CmdProc*) set_value, (ClientData) NULL, 
							(Tcl_CmdDeleteProc*) NULL);
		    Tcl_CreateCommand(interp, "reset_values", (Tcl_CmdProc*) reset_values, 
			                             (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
		    Tcl_CreateCommand(interp, "close_form", (Tcl_CmdProc*) close_form, 
			                             (ClientData) NULL,  (Tcl_CmdDeleteProc*) NULL);
		    
		    sprintf(buf,"%s/etc/form/form.tcl", G_gisbase());
		    ret = Tcl_EvalFile(interp, buf);
		    if ( ret == TCL_ERROR) {
			if (interp->result != NULL) G_fatal_error ( "Cannot open form: %s\n", interp->result);
			else G_fatal_error ( "Cannot open form\n");
		    }


                    form_open = 1;
		}
		G_debug ( 2, "Open form %d", frmid );
		/* Read title */
		fgets ( buf, 1000, child_recv ); 
		length = atoi ( buf ); /* length of the string */
		G_debug ( 2, "length = %d", length );
		child_title = (char *) G_malloc ( length + 1 ); 
		fread ( child_title, length, 1, child_recv);
		child_title[length] = '\0';

		/* Read html */
		fgets ( buf, 1000, child_recv ); 
		length = atoi ( buf ); /* length of the string */
		G_debug ( 2, "length = %d", length );
		child_html = (char *) G_malloc ( length + 1 ); 
		fread ( child_html, length, 1, child_recv);
		child_html[length] = '\0';
		
		memset(buf, '\0', strlen(buf));
		
		encoding_val =  G__getenv("GRASS_DB_ENCODING");
		Tcl_ExternalToUtf(interp,
		Tcl_GetEncoding(interp, encoding_val),
		child_html, strlen(child_html), 0, NULL,
		buf, strlen(child_html) * 2, NULL, NULL,
			NULL);

		G_debug(3,"Current GRASS_DB_ENCODING: %s", encoding_val);	
	        if ( Tcl_SetSystemEncoding(interp, encoding_val) == TCL_ERROR ) {
	 		fprintf(stderr, 
			"Could not set Tcl system encoding to %s\n", encoding_val);
    		}

		G_debug ( 2, "Form: html = %s", buf );

		/* Insert new page */
		Tcl_SetVar ( interp, "html", buf, 0);
		sprintf (buf, "add_form %d \"%s\"", frmid, child_title );
		Tcl_Eval( interp, buf );
		
		fprintf ( child_send, "O" ); /* OK */
		fflush ( child_send );
		frmid++;
		G_debug ( 2, "Form displayed\n" );
	    } else if ( buf[0] == 'C' ) { /* clear old forms */
		Tcl_Eval(interp, "clear_nb");          
		fprintf ( child_send, "O" ); /* OK */
		fflush ( child_send );
	    } else if ( buf[0] == 'D' ) { /* done! */
		Tcl_Eval(interp, "clear_nb");          
		fprintf ( child_send, "O" ); /* OK */
		fflush ( child_send );
		break;
	    }
	}
	
	Tcl_Eval(interp, "update");
    }

    Tcl_Eval(interp, "destroy .");
    G_debug(3, "Form: end\n");
    exit (0);

    return 0;
}