~ubuntu-branches/debian/lenny/freetds/lenny

« back to all changes in this revision

Viewing changes to src/apps/fisql/fisql.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2008-08-02 11:49:53 UTC
  • mfrom: (2.1.10 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080802114953-0qdeowgl63k42n2c
Tags: 0.82-4
* Fix a typo in the freetds-common description, ugh
* Versioned replaces of libct4 by freetds-common, since the current one
  obviously doesn't have overlapping files.
* tdsodbc: check for /var/lib/odbc existence before removing it in the
  postinst, since there are cases where it won't exist on upgrade (i.e.,
  if the driver was never enabled in the first place).  Closes: #493303.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Free ISQL - An isql for DB-Library (C) 2007 Nicholas S. Castellano
 
2
 *
 
3
 * This program  is free software; you can redistribute it and/or
 
4
 * modify it under the terms of the GNU General Public
 
5
 * License as published by the Free Software Foundation; either
 
6
 * version 2 of the License, or (at your option) any later version.
 
7
 *
 
8
 * This library is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * Library General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public
 
14
 * License along with this library; if not, write to the
 
15
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
16
 * Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
#if HAVE_CONFIG_H
 
20
#include <config.h>
 
21
#endif /* HAVE_CONFIG_H */
 
22
 
 
23
#include <stdio.h>
 
24
#include <ctype.h>
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
#include <unistd.h>
 
28
#include <setjmp.h>
 
29
#include <signal.h>
 
30
#ifdef HAVE_READLINE
 
31
#include <readline/readline.h>
 
32
#include <readline/history.h>
 
33
#endif
 
34
#include <sybfront.h>
 
35
#include <sybdb.h>
 
36
#include "terminal.h"
 
37
#include "edit.h"
 
38
#include "handlers.h"
 
39
#include "interrupt.h"
 
40
#include "replacements.h"
 
41
 
 
42
#define READPASSPHRASE_MAXLEN 128
 
43
 
 
44
#ifndef HAVE_READLINE
 
45
static FILE *rl_outstream = NULL;
 
46
static FILE *rl_instream = NULL;
 
47
static const char *rl_readline_name = NULL;
 
48
 
 
49
static char *
 
50
fisql_readline(char *prompt)
 
51
{
 
52
        size_t sz, pos;
 
53
        char *line, *p;
 
54
 
 
55
        sz = 1024;
 
56
        pos = 0;
 
57
        line = (char*) malloc(sz);
 
58
        if (!line)
 
59
                return NULL;
 
60
 
 
61
        if (prompt && prompt[0])
 
62
                fprintf(rl_outstream ? rl_outstream : stdout, "%s", prompt);
 
63
        for (;;) {
 
64
                /* read a piece */
 
65
                if (fgets(line + pos, sz - pos, rl_instream ? rl_instream : stdin) == NULL) {
 
66
                        if (pos)
 
67
                                return line;
 
68
                        break;
 
69
                }
 
70
 
 
71
                /* got end-of-line ? */
 
72
                p = strchr(line + pos, '\n');
 
73
                if (p) {
 
74
                        *p = 0;
 
75
                        return line;
 
76
                }
 
77
 
 
78
                /* allocate space if needed */
 
79
                pos += strlen(line + pos);
 
80
                if (pos + 1024 >= sz) {
 
81
                        sz += 1024;
 
82
                        p = (char*) realloc(line, sz);
 
83
                        if (!p)
 
84
                                break;
 
85
                        line = p;
 
86
                }
 
87
        }
 
88
        free(line);
 
89
        return NULL;
 
90
}
 
91
 
 
92
static void
 
93
fisql_add_history(const char *s)
 
94
{
 
95
}
 
96
 
 
97
#define readline    fisql_readline
 
98
#define add_history fisql_add_history
 
99
 
 
100
#define rl_bind_key(c,f)      do {} while(0)
 
101
#define rl_reset_line_state() do {} while(0)
 
102
#define rl_on_new_line()      do {} while(0)
 
103
 
 
104
#endif
 
105
 
 
106
#if !HAVE_RL_ON_NEW_LINE && !defined(rl_on_new_line)
 
107
#define rl_on_new_line()      do {} while(0)
 
108
#endif
 
109
 
 
110
#if !HAVE_RL_RESET_LINE_STATE && !defined(rl_reset_line_state)
 
111
#define rl_reset_line_state() do {} while(0)
 
112
#endif
 
113
 
 
114
static void *xmalloc(size_t s);
 
115
static void *xrealloc(void *p, size_t s);
 
116
static int get_printable_size(int type, int size);
 
117
static int get_printable_column_size(DBPROCESS * dbproc, int col);
 
118
 
 
119
static void *
 
120
xmalloc(size_t s)
 
121
{
 
122
        void *p = malloc(s);
 
123
 
 
124
        if (!p) {
 
125
                fprintf(stderr, "Out of memory\n");
 
126
                exit(EXIT_FAILURE);
 
127
        }
 
128
        return p;
 
129
}
 
130
 
 
131
static void *
 
132
xrealloc(void *p, size_t s)
 
133
{
 
134
        p = realloc(p, s);
 
135
        if (!p) {
 
136
                fprintf(stderr, "Out of memory\n");
 
137
                exit(EXIT_FAILURE);
 
138
        }
 
139
        return p;
 
140
}
 
141
 
 
142
/* adapted from src/dblib/dblib.c (via src/apps/bsqldb.c) */
 
143
static int
 
144
get_printable_size(int type, int size)
 
145
{
 
146
        switch (type) {
 
147
        case SYBINTN:
 
148
                switch (size) {
 
149
                case 1:
 
150
                        return 3;
 
151
                case 2:
 
152
                        return 6;
 
153
                case 4:
 
154
                        return 11;
 
155
                case 8:
 
156
                        return 21;
 
157
                }
 
158
        case SYBINT1:
 
159
                return 3;
 
160
        case SYBINT2:
 
161
                return 6;
 
162
        case SYBINT4:
 
163
                return 11;
 
164
        case SYBINT8:
 
165
                return 21;
 
166
        case SYBVARCHAR:
 
167
        case SYBCHAR:
 
168
                return size;
 
169
        case SYBFLT8:
 
170
                return 11;      /* FIX ME -- we do not track precision */
 
171
        case SYBREAL:
 
172
                return 11;      /* FIX ME -- we do not track precision */
 
173
        case SYBMONEY:
 
174
                return 12;      /* FIX ME */
 
175
        case SYBMONEY4:
 
176
                return 12;      /* FIX ME */
 
177
        case SYBDATETIME:
 
178
                return 26;      /* FIX ME */
 
179
        case SYBDATETIME4:
 
180
                return 26;      /* FIX ME */
 
181
#if 0                           /* seems not to be exported to sybdb.h */
 
182
        case SYBBITN:
 
183
#endif
 
184
        case SYBBIT:
 
185
                return 1;
 
186
                /* FIX ME -- not all types present */
 
187
        default:
 
188
                return 0;
 
189
        }
 
190
}
 
191
 
 
192
static int
 
193
get_printable_column_size(DBPROCESS * dbproc, int col)
 
194
{
 
195
        int collen;
 
196
 
 
197
        collen = get_printable_size(dbcoltype(dbproc, col), dbcollen(dbproc, col));
 
198
        if (strlen(dbcolname(dbproc, col)) > collen) {
 
199
                collen = strlen(dbcolname(dbproc, col));
 
200
        }
 
201
        return collen;
 
202
}
 
203
 
 
204
int
 
205
main(int argc, char *argv[])
 
206
{
 
207
        int echo = 0;
 
208
 
 
209
#ifdef notyet
 
210
        int print_statistics = 0;
 
211
#endif
 
212
        int fipsflagger = 0;
 
213
        int perfstats = 0;
 
214
        int no_prompt = 0;
 
215
        int use_encryption = 0;
 
216
        int chained_transactions = 0;
 
217
        const char *display_charset = "";
 
218
        const char *cmdend = "go";
 
219
        int headers = 0;
 
220
        char *columnwidth = NULL;
 
221
        const char *colseparator = " ";
 
222
        const char *lineseparator = "\n";
 
223
        int timeout = 0;
 
224
        char *username = NULL;
 
225
        char *password = NULL;
 
226
        char *server = NULL;
 
227
        DBCHAR *char_set = NULL;
 
228
        const char *editor;
 
229
        char *hostname = NULL;
 
230
        char *sqlch;
 
231
        char *interfaces_filename = NULL;
 
232
        char *input_filename = NULL;
 
233
        char *output_filename = NULL;
 
234
        int logintime = -1;
 
235
        char *language = NULL;
 
236
        int size = 0;
 
237
        char *sybenv;
 
238
        DBPROCESS *dbproc;
 
239
        LOGINREC *login;
 
240
        char **ibuf = NULL;
 
241
        int ibuflines = 0;
 
242
        int printedlines;
 
243
        int i;
 
244
        char *line;
 
245
        int dbrc;
 
246
        char foobuf[512];
 
247
        char *firstword;
 
248
        char *cp;
 
249
        int c;
 
250
        int errflg = 0;
 
251
        char *prbuf;
 
252
        int prbuflen;
 
253
        FILE *fp;
 
254
        FILE *tmpfp;
 
255
        FILE *tmpfp2;
 
256
        char *tfn;
 
257
        char tmpfn[256];
 
258
        int num_cols;
 
259
        int selcol;
 
260
        int col;
 
261
        int collen;
 
262
        DBINT colid;
 
263
        const char *opname;
 
264
        char adbuf[512];
 
265
        DBINT convlen;
 
266
        int printedcompute = 0;
 
267
        BYTE *bylist;
 
268
        int nby;
 
269
        char adash;
 
270
 
 
271
        editor = getenv("EDITOR");
 
272
        if (!editor) {
 
273
                editor = getenv("VISUAL");
 
274
        }
 
275
        if (!editor) {
 
276
                editor = "vi";
 
277
        }
 
278
 
 
279
        opterr = 0;
 
280
        optarg = NULL;
 
281
        while (!errflg && (c = getopt(argc, argv, "eFgpnvXYa:c:E:h:H:i:I:J:l:m:o:P:s:S:t:U:w:y:z:A:"))
 
282
               != -1) {
 
283
                switch (c) {
 
284
                case 'e':
 
285
                        echo = 1;
 
286
                        break;
 
287
                case 'F':
 
288
                        fipsflagger = 1;
 
289
                        break;
 
290
                case 'g':
 
291
                        errflg++;
 
292
                        break;
 
293
                case 'p':
 
294
                        errflg++;
 
295
                        perfstats = 1;
 
296
                        break;
 
297
                case 'n':
 
298
                        no_prompt = 1;
 
299
                        break;
 
300
                case 'v':
 
301
                        puts("fisql, a free isql replacement by Nicholas S. Castellano");
 
302
                        exit(EXIT_SUCCESS);
 
303
                        break;
 
304
                case 'X':
 
305
                        /* XXX: We get a different error message than isql gives; neither seems
 
306
                         * to work
 
307
                         */
 
308
                        use_encryption = 1;
 
309
                        break;
 
310
                case 'Y':
 
311
                        chained_transactions = 1;
 
312
                        break;
 
313
                case 'a':
 
314
                        display_charset = optarg;
 
315
                        errflg++;
 
316
                        break;
 
317
                case 'c':
 
318
                        cmdend = optarg;
 
319
                        break;
 
320
                case 'E':
 
321
                        editor = optarg;
 
322
                        break;
 
323
                case 'h':
 
324
                        headers = atoi(optarg);
 
325
                        break;
 
326
                case 'H':
 
327
                        hostname = optarg;
 
328
                        break;
 
329
                case 'i':
 
330
                        input_filename = optarg;
 
331
                        break;
 
332
                case 'I':
 
333
                        interfaces_filename = optarg;
 
334
                        break;
 
335
                case 'J':
 
336
                        errflg++;
 
337
                        break;
 
338
                case 'l':
 
339
                        logintime = atoi(optarg);
 
340
                        break;
 
341
                case 'm':
 
342
                        global_errorlevel = atoi(optarg);
 
343
                        break;
 
344
                case 'o':
 
345
                        output_filename = optarg;
 
346
                        break;
 
347
                case 'P':
 
348
                        password = optarg;
 
349
                        break;
 
350
                case 's':
 
351
                        colseparator = optarg;
 
352
                        break;
 
353
                case 'S':
 
354
                        server = optarg;
 
355
                        break;
 
356
                case 't':
 
357
                        timeout = atoi(optarg);
 
358
                        break;
 
359
                case 'U':
 
360
                        username = optarg;
 
361
                        break;
 
362
                case 'w':
 
363
                        columnwidth = optarg;
 
364
                        break;
 
365
                case 'y':
 
366
                        /* XXX: this doesn't seem to be what isql does with -y...it doesn't
 
367
                         * seem to do anything actually
 
368
                         */
 
369
                        sybenv = (char *) xmalloc((strlen(optarg) + 8) * sizeof(char));
 
370
                        strcpy(sybenv, "SYBASE=");
 
371
                        strcat(sybenv, optarg);
 
372
                        putenv(sybenv);
 
373
                        break;
 
374
                case 'z':
 
375
                        language = optarg;
 
376
                        break;
 
377
                case 'A':
 
378
                        size = atoi(optarg);
 
379
                        break;
 
380
                default:
 
381
                        errflg++;
 
382
                        break;
 
383
                }
 
384
        }
 
385
 
 
386
        if (errflg) {
 
387
                fprintf(stderr, "usage: fisql [-e] [-F] [-g] [-p] [-n] [-v] [-X] [-Y]\n");
 
388
                fprintf(stderr, "\t[-a display_charset] [-c cmdend] [-E editor]\n");
 
389
                fprintf(stderr, "\t[-h headers] [-H hostname] [-i inputfile]\n");
 
390
                fprintf(stderr, "\t[-I interfaces_file] [-J client character set]\n");
 
391
                fprintf(stderr, "\t[-l login_timeout] [-m errorlevel]\n");
 
392
                fprintf(stderr, "\t[-o outputfile]\n");
 
393
                fprintf(stderr, "\t[-P password] [-s colseparator] [-S server]\n");
 
394
                fprintf(stderr, "\t[-t timeout] [-U username] [-w columnwidth]\n");
 
395
                fprintf(stderr, "\t[-y sybase_dir] [-z language]\n");
 
396
                exit(EXIT_FAILURE);
 
397
        }
 
398
        if (!(isatty(fileno(stdin)))) {
 
399
                no_prompt = 1;
 
400
                rl_outstream = fopen("/dev/null", "rw");
 
401
        }
 
402
        rl_readline_name = "fisql";
 
403
        rl_bind_key('\t', rl_insert);
 
404
        if (password == NULL) {
 
405
                password = (char *) xmalloc(READPASSPHRASE_MAXLEN);
 
406
                readpassphrase("Password: ", password, READPASSPHRASE_MAXLEN, RPP_ECHO_OFF);
 
407
        }
 
408
        if (input_filename) {
 
409
                if (freopen(input_filename, "r", stdin) == NULL) {
 
410
                        /* XXX: sybase isql generates this error while parsing the options,
 
411
                         * but doesn't redirect the input until after the Password: prompt
 
412
                         */
 
413
                        /* lack of newline for bug-compatibility with isql */
 
414
                        fprintf(stderr, "Unable to open input file '%s'.", optarg);
 
415
                        exit(EXIT_FAILURE);
 
416
                }
 
417
        }
 
418
        if (output_filename) {
 
419
                if (freopen(output_filename, "w", stdout) == NULL) {
 
420
                        /* XXX: sybase isql generates this error while parsing the options,
 
421
                         * but doesn't redirect the output until after the Password: prompt
 
422
                         */
 
423
                        /* lack of newline for bug-compatibility with isql */
 
424
                        fprintf(stderr, "Unable to open output file '%s'.", output_filename);
 
425
                        exit(EXIT_FAILURE);
 
426
                }
 
427
        }
 
428
        if (isatty(fileno(stdin))) {
 
429
                rl_outstream = stdout;
 
430
        }
 
431
        dbinit();
 
432
#if 0
 
433
#ifdef DBVERSION_100
 
434
        dbsetversion(DBVERSION_100);
 
435
#endif
 
436
#endif
 
437
        if ((login = dblogin()) == NULL) {
 
438
                reset_term();
 
439
                exit(EXIT_FAILURE);
 
440
        }
 
441
        dbmsghandle(msg_handler);
 
442
        dberrhandle(err_handler);
 
443
        DBSETLAPP(login, "fisql");
 
444
        if (username) {
 
445
                DBSETLUSER(login, username);
 
446
        }
 
447
        DBSETLPWD(login, password);
 
448
        if (char_set) {
 
449
                DBSETLCHARSET(login, char_set);
 
450
        }
 
451
        if (use_encryption) {
 
452
                DBSETLENCRYPT(login, TRUE);
 
453
        }
 
454
        if (hostname) {
 
455
                DBSETLHOST(login, hostname);
 
456
        }
 
457
        if (language) {
 
458
                DBSETLNATLANG(login, language);
 
459
        }
 
460
        if (size) {
 
461
                DBSETLPACKET(login, (short) size);
 
462
        }
 
463
        if (interfaces_filename) {
 
464
                dbsetifile(interfaces_filename);
 
465
        }
 
466
        dbsettime(timeout);
 
467
        if (logintime >= 0) {
 
468
                dbsetlogintime(logintime);
 
469
        }
 
470
        if ((dbproc = dbopen(login, server)) == NULL) {
 
471
                fprintf(stderr, "fisql: dbopen() failed.\n");
 
472
                reset_term();
 
473
                exit(EXIT_FAILURE);
 
474
        }
 
475
 
 
476
        dbsetopt(dbproc, DBPRLINESEP, lineseparator, strlen(lineseparator));
 
477
        if (colseparator) {
 
478
                dbsetopt(dbproc, DBPRCOLSEP, colseparator, strlen(colseparator));
 
479
        }
 
480
        if (columnwidth) {
 
481
                dbsetopt(dbproc, DBPRLINELEN, columnwidth, 0);
 
482
        }
 
483
        if (chained_transactions) {
 
484
                dbsetopt(dbproc, DBCHAINXACTS, NULL, 0);
 
485
        }
 
486
        if (fipsflagger) {
 
487
                dbsetopt(dbproc, DBFIPSFLAG, NULL, 0);
 
488
        }
 
489
        if (perfstats) {
 
490
                dbsetopt(dbproc, DBSTAT, "time", 0);
 
491
        }
 
492
 
 
493
        while (1) {
 
494
                if (sigsetjmp(restart, 1)) {
 
495
                        if (ibuf) {
 
496
                                for (i = 0; i < ibuflines; i++) {
 
497
                                        free(ibuf[i]);
 
498
                                }
 
499
                                ibuflines = 0;
 
500
                                free(ibuf);
 
501
                                ibuf = NULL;
 
502
                        }
 
503
                        fputc('\n', stdout);
 
504
                        rl_on_new_line();
 
505
                        rl_reset_line_state();
 
506
                }
 
507
                dbcancel(dbproc);
 
508
                signal(SIGINT, inactive_interrupt_handler);
 
509
                ibuf = (char **) xmalloc(sizeof(char *));
 
510
                ibuflines = 0;
 
511
                while (1) {
 
512
                        if (no_prompt) {
 
513
                                foobuf[0] = '\0';
 
514
                        } else {
 
515
                                sprintf(foobuf, "%d>> ", ibuflines + 1);
 
516
                        }
 
517
                        line = readline(foobuf);
 
518
                        if (line == NULL) {
 
519
                                line = "exit";
 
520
                        }
 
521
                        for (cp = line; *cp && isspace((unsigned char) *cp); cp++);
 
522
                        if (*cp) {
 
523
                                add_history(line);
 
524
                        }
 
525
                        if (!(strncasecmp(line, "!!", 2))) {
 
526
                                cp = line + 2;
 
527
                                system(cp);
 
528
                                continue;
 
529
                        }
 
530
                        /* XXX: isql off-by-one line count error for :r not duplicated */
 
531
                        if (!(strncasecmp(line, ":r", 2))) {
 
532
                                for (cp = line + 2; *cp && (isspace((unsigned char) *cp)); cp++);
 
533
                                tfn = cp;
 
534
                                for (; *cp && !(isspace((unsigned char) *cp)); cp++);
 
535
                                *cp = '\0';
 
536
                                if ((fp = fopen(tfn, "r")) == NULL) {
 
537
                                        printf("Operating system error: Failed to open %s.\n", tfn);
 
538
                                        continue;
 
539
                                }
 
540
                                tmpfp = rl_instream;
 
541
                                tmpfp2 = rl_outstream;
 
542
                                rl_instream = fp;
 
543
                                rl_outstream = fopen("/dev/null", "w");
 
544
                                while ((line = readline("")) != NULL) {
 
545
                                        ibuf[ibuflines++] = line;
 
546
                                        ibuf = (char **) xrealloc(ibuf, (ibuflines + 1) * sizeof(char *));
 
547
                                }
 
548
                                rl_instream = tmpfp;
 
549
                                fclose(rl_outstream);
 
550
                                rl_outstream = tmpfp2;
 
551
                                fclose(fp);
 
552
                                fputc('\r', stdout);
 
553
                                fflush(stdout);
 
554
                                continue;
 
555
                        }
 
556
                        firstword = (char *) xmalloc((strlen(line) + 1) * sizeof(char));
 
557
                        strcpy(firstword, line);
 
558
                        for (cp = firstword; *cp; cp++) {
 
559
                                if (isspace((unsigned char) *cp)) {
 
560
                                        *cp = '\0';
 
561
                                        break;
 
562
                                }
 
563
                        }
 
564
                        if ((!(strcasecmp(firstword, "exit")))
 
565
                            || (!(strcasecmp(firstword, "quit")))) {
 
566
                                reset_term();
 
567
                                dbexit();
 
568
                                exit(EXIT_SUCCESS);
 
569
                        }
 
570
                        if (!(strcasecmp(firstword, "reset"))) {
 
571
                                for (i = 0; i < ibuflines; i++) {
 
572
                                        free(ibuf[i]);
 
573
                                }
 
574
                                ibuflines = 0;
 
575
                                continue;
 
576
                        }
 
577
                        if (!(strcasecmp(firstword, cmdend))) {
 
578
                                if (ibuflines == 0) {
 
579
                                        continue;
 
580
                                }
 
581
                                free(firstword);
 
582
                                break;
 
583
                        }
 
584
                        if ((!(strcasecmp(firstword, "vi")))
 
585
                            || (!(strcasecmp(firstword, editor)))) {
 
586
                                int tmpfd;
 
587
 
 
588
                                strcpy(tmpfn, "/tmp/fisqlXXXXXX");
 
589
                                tmpfd = mkstemp(tmpfn);
 
590
                                if ((fp = fdopen(tmpfd, "w")) == NULL) {
 
591
                                        perror("fisql");
 
592
                                        reset_term();
 
593
                                        dbexit();
 
594
                                        exit(2);
 
595
                                }
 
596
                                if (ibuflines) {
 
597
                                        for (i = 0; i < ibuflines; i++) {
 
598
                                                fputs(ibuf[i], fp);
 
599
                                                fputc('\n', fp);
 
600
                                                free(ibuf[i]);
 
601
                                        }
 
602
                                } else {
 
603
                                        for (i = 0; ((sqlch = dbgetchar(dbproc, i)) != NULL); i++) {
 
604
                                                fputc(*sqlch, fp);
 
605
                                        }
 
606
                                }
 
607
                                fclose(fp);
 
608
                                if (!(strcmp(firstword, "vi"))) {
 
609
                                        edit("vi", tmpfn);
 
610
                                } else {
 
611
                                        edit(editor, tmpfn);
 
612
                                }
 
613
                                ibuflines = 0;
 
614
                                fp = fopen(tmpfn, "r");
 
615
                                tmpfp = rl_instream;
 
616
                                rl_instream = fp;
 
617
                                strcpy(foobuf, "1>> ");
 
618
                                while ((line = readline(foobuf)) != NULL) {
 
619
                                        ibuf[ibuflines++] = line;
 
620
                                        sprintf(foobuf, "%d>> ", ibuflines + 1);
 
621
                                        ibuf = (char **) xrealloc(ibuf, (ibuflines + 1) * sizeof(char *));
 
622
                                }
 
623
                                rl_instream = tmpfp;
 
624
                                fclose(fp);
 
625
                                fputc('\r', stdout);
 
626
                                fflush(stdout);
 
627
                                unlink(tmpfn);
 
628
                                continue;
 
629
                        }
 
630
                        free(firstword);
 
631
                        ibuf[ibuflines++] = line;
 
632
                        ibuf = (char **) xrealloc(ibuf, (ibuflines + 1) * sizeof(char *));
 
633
                }
 
634
                dbfreebuf(dbproc);
 
635
                for (i = 0; i < ibuflines; i++) {
 
636
                        if (echo) {
 
637
                                puts(ibuf[i]);
 
638
                        }
 
639
                        dbcmd(dbproc, ibuf[i]);
 
640
                        dbcmd(dbproc, "\n");
 
641
                        free(ibuf[i]);
 
642
                }
 
643
                free(ibuf);
 
644
                ibuf = NULL;
 
645
                signal(SIGINT, active_interrupt_handler);
 
646
                dbsetinterrupt(dbproc, (void *) active_interrupt_pending, (void *) active_interrupt_servhandler);
 
647
                if (dbsqlexec(dbproc) == SUCCEED) {
 
648
                        maybe_handle_active_interrupt();
 
649
                        while ((dbrc = dbresults(dbproc)) != NO_MORE_RESULTS) {
 
650
                                printedlines = 0;
 
651
#define USE_DBPRROW 0
 
652
#if USE_DBPRROW
 
653
                                dbprhead(dbproc);
 
654
                                dbprrow(dbproc);
 
655
#else
 
656
                                if ((dbrc == SUCCEED) && (DBROWS(dbproc) == SUCCEED)) {
 
657
                                        prbuflen = dbspr1rowlen(dbproc);
 
658
                                        prbuf = (char *) xmalloc(prbuflen * sizeof(char));
 
659
                                        dbsprhead(dbproc, prbuf, prbuflen);
 
660
                                        fputs(prbuf, stdout);
 
661
                                        fputc('\n', stdout);
 
662
                                        dbsprline(dbproc, prbuf, prbuflen, '-');
 
663
                                        fputs(prbuf, stdout);
 
664
                                        fputc('\n', stdout);
 
665
                                        maybe_handle_active_interrupt();
 
666
                                        while ((dbrc = dbnextrow(dbproc)) != NO_MORE_ROWS) {
 
667
                                                if (dbrc == FAIL) {
 
668
                                                        break;
 
669
                                                }
 
670
                                                if (dbrc != REG_ROW) {
 
671
                                                        num_cols = dbnumalts(dbproc, dbrc);
 
672
                                                        for (selcol = col = 1; col <= num_cols; col++) {
 
673
                                                                colid = dbaltcolid(dbproc, dbrc, col);
 
674
                                                                while (selcol < colid) {
 
675
                                                                        collen = get_printable_column_size(dbproc, selcol);
 
676
                                                                        for (i = 0; i < collen; i++) {
 
677
                                                                                putchar(' ');
 
678
                                                                        }
 
679
                                                                        selcol++;
 
680
                                                                        printf("%s", colseparator);
 
681
                                                                }
 
682
                                                                opname = dbprtype(dbaltop(dbproc, dbrc, col));
 
683
                                                                printf("%s", opname);
 
684
                                                                collen = get_printable_column_size(dbproc, colid);
 
685
                                                                collen -= strlen(opname);
 
686
                                                                while (collen-- > 0) {
 
687
                                                                        putchar(' ');
 
688
                                                                }
 
689
                                                                selcol++;
 
690
                                                                printf("%s", colseparator);
 
691
                                                        }
 
692
                                                        printf("%s", lineseparator);
 
693
                                                        for (selcol = col = 1; col <= num_cols; col++) {
 
694
                                                                colid = dbaltcolid(dbproc, dbrc, col);
 
695
                                                                while (selcol < colid) {
 
696
                                                                        collen = get_printable_column_size(dbproc, selcol);
 
697
                                                                        for (i = 0; i < collen; i++) {
 
698
                                                                                putchar(' ');
 
699
                                                                        }
 
700
                                                                        selcol++;
 
701
                                                                        printf("%s", colseparator);
 
702
                                                                }
 
703
                                                                collen = get_printable_column_size(dbproc, colid);
 
704
                                                                adash = '-';
 
705
                                                                bylist = dbbylist(dbproc, dbrc, &nby);
 
706
                                                                if (nby == 0) {
 
707
                                                                        adash = '=';
 
708
                                                                }
 
709
                                                                for (i = 0; i < collen; i++) {
 
710
                                                                        putchar(adash);
 
711
                                                                }
 
712
                                                                selcol++;
 
713
                                                                printf("%s", colseparator);
 
714
                                                        }
 
715
                                                        printf("%s", lineseparator);
 
716
                                                        for (selcol = col = 1; col <= num_cols; col++) {
 
717
                                                                colid = dbaltcolid(dbproc, dbrc, col);
 
718
                                                                while (selcol < colid) {
 
719
                                                                        collen = get_printable_column_size(dbproc, selcol);
 
720
                                                                        for (i = 0; i < collen; i++) {
 
721
                                                                                putchar(' ');
 
722
                                                                        }
 
723
                                                                        selcol++;
 
724
                                                                        printf("%s", colseparator);
 
725
                                                                }
 
726
                                                                convlen = dbconvert(dbproc,
 
727
                                                                                    dbalttype(dbproc, dbrc, col),
 
728
                                                                                    dbadata(dbproc, dbrc, col),
 
729
                                                                                    dbadlen(dbproc, dbrc, col),
 
730
                                                                                    SYBCHAR, (BYTE *) adbuf, sizeof(adbuf));
 
731
                                                                printf("%.*s", (int) convlen, adbuf);
 
732
                                                                collen = get_printable_column_size(dbproc, colid);
 
733
                                                                collen -= convlen;
 
734
                                                                while (collen-- > 0) {
 
735
                                                                        putchar(' ');
 
736
                                                                }
 
737
                                                                selcol++;
 
738
                                                                printf("%s", colseparator);
 
739
                                                        }
 
740
                                                        printf("%s", lineseparator);
 
741
                                                        printedcompute = 1;
 
742
                                                        continue;
 
743
                                                }
 
744
                                                if (printedcompute || (headers && (printedlines >= headers)
 
745
                                                                       && ((printedlines % headers) == 0))) {
 
746
                                                        fputc('\n', stdout);
 
747
                                                        dbsprhead(dbproc, prbuf, prbuflen);
 
748
                                                        fputs(prbuf, stdout);
 
749
                                                        fputc('\n', stdout);
 
750
                                                        dbsprline(dbproc, prbuf, prbuflen, '-');
 
751
                                                        fputs(prbuf, stdout);
 
752
                                                        fputc('\n', stdout);
 
753
                                                        printedcompute = 0;
 
754
                                                }
 
755
                                                printedlines++;
 
756
                                                dbspr1row(dbproc, prbuf, prbuflen);
 
757
                                                fputs(prbuf, stdout);
 
758
                                                fputc('\n', stdout);
 
759
                                                maybe_handle_active_interrupt();
 
760
                                        }
 
761
                                        fputc('\n', stdout);
 
762
                                        free(prbuf);
 
763
                                        maybe_handle_active_interrupt();
 
764
                                }
 
765
#endif
 
766
                                if (dbrc != FAIL) {
 
767
                                        if ((DBCOUNT(dbproc) >= 0) || dbhasretstat(dbproc)) {
 
768
                                                if (DBCOUNT(dbproc) >= 0) {
 
769
                                                        fprintf(stdout, "(%d rows affected", (int) DBCOUNT(dbproc));
 
770
                                                        if (dbhasretstat(dbproc)) {
 
771
                                                                dbrc = dbretstatus(dbproc);
 
772
                                                                fprintf(stdout, ", return status = %d", dbrc);
 
773
                                                        }
 
774
                                                        fprintf(stdout, ")\n");
 
775
                                                } else {
 
776
                                                        if (dbhasretstat(dbproc)) {
 
777
                                                                dbrc = dbretstatus(dbproc);
 
778
                                                                fprintf(stdout, "(return status = %d)\n", dbrc);
 
779
                                                        }
 
780
                                                }
 
781
                                        }
 
782
                                }
 
783
                        }
 
784
                }
 
785
        }
 
786
        reset_term();
 
787
        dbexit();
 
788
        exit(EXIT_FAILURE);
 
789
        return (0);
 
790
}