dbquery

Query RDBMS and return S-expression
git clone https://logand.com/git/dbquery.git/
Log | Files | Refs | README

common.c (2668B)


      1 /*
      2   Copyright (C) 2013 Tomas Hlavaty <tom@logand.com>
      3 
      4   Permission is hereby granted, free of charge, to any person
      5   obtaining a copy of this software and associated documentation
      6   files (the "Software"), to deal in the Software without
      7   restriction, including without limitation the rights to use, copy,
      8   modify, merge, publish, distribute, sublicense, and/or sell copies
      9   of the Software, and to permit persons to whom the Software is
     10   furnished to do so, subject to the following conditions:
     11 
     12   The above copyright notice and this permission notice shall be
     13   included in all copies or substantial portions of the Software.
     14 
     15   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     16   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     17   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     18   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
     19   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
     20   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     21   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     22   DEALINGS IN THE SOFTWARE.
     23 */
     24 
     25 #include <stdarg.h>
     26 #include <stdlib.h>
     27 #include <stdio.h>
     28 
     29 #define HLEN 102400
     30 
     31 enum Command {ERROR, QUERY, PREPARE, EXECUTE, DEALLOCATE};
     32 
     33 static void die(const char *format, ...) {
     34   va_list argv;
     35   va_start(argv, format);
     36   vfprintf(stderr, format, argv);
     37   va_end(argv);
     38   fprintf(stderr, "\n");
     39   fflush(stderr);
     40   fflush(stdout);
     41   exit(-1);
     42 }
     43 
     44 static void pnum(int x) {printf("%d\n", x);}
     45 static void pqstr(char *x) {
     46   putchar('"');
     47   for(; *x; x++) {
     48     switch(*x) {
     49     case '\\': putchar('\\'); putchar('\\'); break;
     50     case '"': putchar('\\'); putchar('"'); break;
     51     default: putchar(*x);
     52     }
     53   }
     54   putchar('"');
     55   putchar('\n');
     56 }
     57 static void pnil() {putchar('?'); putchar('\n');}
     58 
     59 static int rnum(void) { // TODO limit interval
     60   int z = 0, y = 0;
     61   for(;;) {
     62     int c = getchar();
     63     if('0' <= c && c <= '9') {z = 10 * z + c - '0'; y = 1;}
     64     else if('\n' == c && y) goto done;
     65     else die("expected number");
     66   }
     67  done:
     68   return z;
     69 }
     70 
     71 static char heap[HLEN];
     72 static char *buf;
     73 
     74 static char *rstr(void) {
     75   char *z = buf;
     76   char c = getchar();
     77   if('?' == c) { // :null value
     78     if('\n' != getchar()) die("expected eol");
     79     return NULL;
     80   }
     81   if('"' != c) die("expected string");
     82   for(;;) {
     83     if(heap + HLEN <= buf) die("out of memory");
     84     int c = getchar();
     85     switch(c) {
     86     case '\\': *buf++ = getchar(); break;
     87     case '"': if('\n' != getchar()) die("expected eol"); *buf++ = 0; goto done;
     88     default: *buf++ = c;
     89     }
     90   }
     91  done:
     92   return z;
     93 }
     94 
     95 static int eof(void) {return EOF == ungetc(getchar(), stdin);}