5 /************************************************************************
9 * Includes for the global database facility (gdb)
11 * Author: Noah Mendelsohn
12 * Copyright: 1986 MIT Project Athena
14 ************************************************************************/
17 * Note: following include may safely be done redundantly, so it doesn't
18 * matter if caller does it too. We need it for fd_set.
20 #include <sys/types.h>
32 #define max(a,b) ((a)>(b)?(a):(b))
36 #define min(a,b) ((a)<(b)?(a):(b))
43 #define howmany(x, y) (((x)+((y)-1))/(y))
46 #define FD_SETSIZE 256
49 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
50 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
51 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
52 #define FD_ZERO(p) bzero(p, sizeof(*(p)))
54 typedef struct fd_set {fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];} fd_set;
59 /*----------------------------------------------------------*
63 * This macro is called with a string argument whenever a
64 * fatal error is encounterd. If you re-define this
65 * macro, you can control the disposition of fatal gdb
68 * The gdb library must be recompiled for the change to
69 * take effect. That will have to be fixed sometime.
71 *----------------------------------------------------------*/
73 #define GDB_GIVEUP(errormsg) g_givup(errormsg);
75 /*----------------------------------------------------------*
79 * Round a number up to the next specified boundary.
81 *----------------------------------------------------------*/
83 #define GDB_ROUNDUP(n,b) ((((n)+(b)-1)/(b))*(b))
85 extern int gdb_Debug; /* debugging flags are */
87 extern FILE *gdb_log; /* file descriptor for */
88 /* logging gdb output */
92 * connection failure indicator
94 * This variable is used to communicate between gdb_move_data and
95 * g_con_progress without passing an extra parameter through lots
96 * of procedure calls. When set to FALSE, it indicates that the
97 * connection currently being processed has encountered a fatal error
98 * and should be severed.
100 extern int gdb_conok;
101 /************************************************************************
103 * USER IDENTIFICATION
105 * gdb_init puts the user's i.d. and hostname as strings here.
107 ************************************************************************/
109 extern char *gdb_uname; /* user's string name */
110 extern char *gdb_host; /* name of local host */
115 /************************************************************************
120 * Declarations used to control the definition and use of 'types'
121 * as supported by the global database system. Most of the
122 * declarations for 'System Defined Types' will not be of concern
123 * to typical users, with the exception of the type names like
124 * INTEGER_T which are defined below.
126 * In this implementation, user defined types are added
127 * dynamically during execution by calling the appropriate
128 * functions. The define GDB_MAX_TYPES below sets the maximum
129 * total number of types, including both system and user defined,
130 * which the system can support for any one application. When
131 * GDB_MAX_TYPES is changed, the libary must be re-built. Space
132 * for a two dimensional array, with one word for each property
133 * of each possible type, is allocated statically in the library.
135 ************************************************************************/
139 * Maximum number of types we can support, including both system and
143 #define GDB_MAX_TYPES 50
145 typedef int FIELD_TYPE; /* data needed to repre- */
146 /* sent a system or user */
147 /* defined data type */
148 /* This is actualy just */
149 /* a row index in the type */
150 /* definition table */
152 /*----------------------------------------------------------
154 * System defined types
156 * WARNING: Any changes to these type definitions must be
157 * carefully matched with the initializations in the
158 * gdb_i_stype routine in gdb_stype.c. Mistakes in these
159 * tables may be VERY difficult to debug. Be careful!
161 *----------------------------------------------------------*/
164 * Primitive types for ingres data
167 #define INTEGER_T (0)
176 #define TUPLE_DESCRIPTOR_T (4)
178 #define TUPLE_DATA_T (6)
179 #define RELATION_T (7)
182 * Number of system defined types
184 * This will always be equal to one more than index of last system type
187 #define SYSTEM_TYPE_COUNT 8
189 /*----------------------------------------------------------
191 * Type descriptor tables
193 *----------------------------------------------------------*/
198 * Each entry in the type definition table is a union of this form,
199 * which allows us to store a choice of an integer, a function
200 * pointer, or a pointer to a character string.
203 union gdb_prop_union {
204 int i; /* when we want as an */
206 int (*f)(); /* as a function pointer */
207 char *cp; /* character pointer */
210 #define TYPE_PROPERTY_COUNT 8 /* number of properties */
214 * Uses of the type properties. Each type has a set of properties.
215 * Some are integers, some are functions. The defines below descrive
216 * respectively the 0'th,1'st, 2'nd, etc. properties of EACH type.
218 * Note: TYPE_PROPERTY_COUNT (above) must be changed when new properties
219 * are added. For system defined types, bindings for the properties
220 * are done in gdb_i_stype in the gdb_stype.c source file.
223 #define LENGTH_PROPERTY 0 /* integer */
224 #define ALIGNMENT_PROPERTY 1 /* integer */
225 #define NULL_PROPERTY 2 /* function */
226 #define CODED_LENGTH_PROPERTY 3 /* function */
227 #define ENCODE_PROPERTY 4 /* function */
228 #define DECODE_PROPERTY 5 /* function */
229 #define FORMAT_PROPERTY 6 /* function */
230 #define NAME_PROPERTY 7 /* char pointer */
235 * Information to describe a single type
238 typedef union gdb_prop_union gdb_type_def[TYPE_PROPERTY_COUNT];
244 * This is the table where the actual definitions for the types are
248 extern gdb_type_def g_type_table[GDB_MAX_TYPES];
249 extern int gdb_n_types; /* number of entries in */
252 /*----------------------------------------------------------
254 * Macros for accessing properties
256 *----------------------------------------------------------*/
258 #define INT_PROPERTY(type, prop) (g_type_table[type][prop].i)
259 #define STR_PROPERTY(type, prop) (g_type_table[type][prop].cp)
260 #define FCN_PROPERTY(type, prop) (*g_type_table[type][prop].f)
263 /************************************************************************
267 * Stuff needed to declare and manage TUPLES, TUPLE_DESCRIPTORS
270 ************************************************************************/
272 /*----------------------------------------------------------
276 *----------------------------------------------------------*/
278 #define GDB_DESC_ID 0x54504400 /* "TPD" */
280 struct tupld_var { /* the variable length */
281 /* stuff in a tuple */
283 char *name; /* string name of field */
284 FIELD_TYPE type; /* type of this field */
285 int offset; /* byte offset of this field */
286 /* relative to first byte of */
287 /* data (not start of whole */
289 int length; /* Length of the actual data */
290 /* for this field. Note that */
291 /* alignment requirements of */
292 /* following field are NOT */
293 /* included in this length */
296 long id; /* this should say TPD\0 */
297 int ref_count; /* when this goes to zero, */
298 /* the descriptor may really */
300 int field_count; /* number of fields in */
302 int data_len; /* length of the data in */
303 /* the actual tuple */
304 int str_len; /* length of the strings */
305 /* stored off the end of */
307 struct tupld_var var[1]; /* one of these for each */
308 /* field, but the C compiler */
309 /* won't accept the[] decl, */
310 /* because it's afraid of */
311 /* uncertain length*/
314 typedef struct tupl_desc *TUPLE_DESCRIPTOR; /* use this to declare a */
315 /* tuple descriptor anchor */
317 #define gdb_descriptor_length(num_flds) (sizeof(struct tupl_desc) + ((num_flds)-1) * sizeof(struct tupld_var))
319 /*----------------------------------------------------------
323 * tuple_dat is allocated by the create_tuple routine.
325 * TUPLE may be used in user code to declare a handle
328 *----------------------------------------------------------*/
330 #define GDB_TUP_ID 0x54555000
332 typedef struct tuple_dat *TUPLE; /* handle on a tuple */
335 TUPLE next, prev; /* chain pointers when */
336 /* tuples are linked, as in */
337 /* a relation. Convention is*/
338 /* double linked, circular.*/
339 long id; /* should say TUP\0 */
340 TUPLE_DESCRIPTOR desc; /* pointer to the descriptor */
341 char data[1]; /* data goes here, word */
342 /* aligned. Should be [] */
343 /* if compiler would allow */
347 /*----------------------------------------------------------
351 * rel_dat is allocated by the create_relation
354 * RELATION may be used in user code to declare a handle
357 *----------------------------------------------------------*/
359 #define GDB_REL_ID 0x52454c00
362 TUPLE first, last; /* chain pointers to tuples */
363 /* note that first->prev and */
364 /* last->next are both == */
365 /* &rel-dat. Maintenance is */
366 /* simplified by keeping */
367 /* as the first fields in */
368 /* both rel_dat and tuple_dat*/
369 /* a minor non-portability */
370 long id; /* should contain REL\0 */
371 TUPLE_DESCRIPTOR desc; /* descriptor for the tuples */
372 /* in this relation. Should */
373 /* == that in each tuple */
377 typedef struct rel_dat *RELATION; /* handle on a relation */
379 /************************************************************************
381 * transport LAYER DECLARATIONS
383 * Declares the state maintenance structures for full duplex
384 * connections with asynchronous transmission capability. Most
385 * users need only know that the type CONNECTION is defined, and
386 * that it may be treated as a pointer for most purposes (i.e. it
387 * is compact, and copying it does not actually copy the connection
390 ************************************************************************/
392 #define GDB_PROTOCOL_VERSION 0x01 /* version of the gdb */
393 /* protocols that we're */
395 #define GDB_STREAM_BUFFER_SIZE 4096 /* amount to read in */
396 /* one chunk from tcp stream*/
397 #define GDB_PORT htons(9425) /* temporary until we use */
398 /* services properly */
399 #define GDB_BIND_RETRY_COUNT 5 /* number of times to */
400 /* retry a bind before */
401 /* giving up. Used when */
402 /* accepting connections */
403 #define GDB_BIND_RETRY_INTERVAL 10 /* Number of seconds to wait */
404 /* between attempts to bind */
405 /* the listening socket */
406 #define GDB_MAX_CONNECTIONS 25 /* maximum number of */
407 /* connections that */
408 /* any one process can */
409 /* control simultaneously */
411 * Circumvent bug in ACIS 4.2 socket support
414 #define GDB_MAX_SOCK_WRITE 2047 /* rt can't do socket */
415 /* writes longer than this */
418 #define GDB_MAX_SOCK_WRITE 0x00ffffff
421 /*----------------------------------------------------------
423 * Declarations for HALF_CONNECTIONS
425 * Each full duplex connection has associated with it
426 * two simplex half-connections, each of which
427 * has its own queue of pending operations. The
428 * following describes the state of a half-connection.
430 *----------------------------------------------------------*/
432 struct half_con_data {
434 * these two must be first to match position in OPERATION
436 struct oper_data *op_q_first; /* first item in q of pending*/
437 /* operations for this */
438 /* half-connection. (chained */
439 /* circularly to half con, */
441 struct oper_data *op_q_last; /* last item in q of pending*/
442 /* operations for this */
444 int status; /* values are of type */
445 /* OPSTATUS. tells whether */
446 /* transmit/receive is */
448 long flags; /* bit flags */
449 #define HCON_PROGRESS 0x00000001 /* used by selection */
450 /* routines to detect */
452 #define HCON_LISTEN 0x00000002 /* indicates that this is */
453 /* a special half connection */
454 /* used only for listenting */
455 /* to incoming connection */
457 #define HCON_UNUSED 0x00000004 /* on a one-way connection, */
458 /* this flag marks an unused */
460 #define HCON_PENDING_LISTEN 0x00000008 /* a queued op on this half */
461 /* connection is actually */
462 /* trying to listen */
463 #define HCON_BUSY 0x00000010 /* we are currently making */
464 /* progress on this half */
465 /* connection. Used to */
466 /* detect re-entrance of */
468 int fd; /* main half duplex file */
469 /* descriptor for this h_conn*/
470 int oob_fd; /* file descriptor for */
471 /* out of band signals*/
472 char *next_byte; /* next byte to send/recv */
473 int remaining; /* number of bytes remaining */
474 /* to send/receive */
475 char *stream_buffer; /* points to a buffer */
476 /* used to pre-read/write */
477 /* the stream to avoid */
478 /* window thrashing */
479 int stream_buffer_length; /* length of the stream */
481 char *stream_buffer_next; /* next byte to read in */
483 int stream_buffer_remaining; /* number of bytes currently */
484 /* in use in stream buffer*/
485 int *accepted_fdp; /* used only for listening */
486 /* connections. latest */
487 /* accepted fd is put where*/
489 int *accepted_len; /* ptr to length of 'from' */
494 typedef struct half_con_data *HALF_CONNECTION;
497 /*----------------------------------------------------------
499 * Declarations for CONNECTIONS
501 *----------------------------------------------------------*/
503 #define GDB_CON_ID 0x434f4e00 /*"CON"*/
506 long id; /* should contain CON\0 */
507 int status; /* See definitions below. */
508 /* Do not confuse with */
509 /* the status sub-fields of */
510 /* in and out half-cons. */
511 int version; /* the version of the */
512 /* protocol being observed */
513 /* on this connection */
514 int errno; /* the real errno gets */
515 /* copied here if it causes */
516 /* the connection to die */
517 int (*oob_fcn)(); /* pointer to function to */
518 /* call when something */
519 /* arrives on the out of */
521 struct half_con_data in, out; /* states of the inbound */
522 /* and outbound half */
526 typedef struct con_data *CONNECTION; /* the only externally */
527 /* visible handle on a */
531 * Definitions of status fields.
533 * WARNING: If you change any of the following, there are coordinated
534 * changes to be made in gdb_debug.c
537 #define CON_STOPPED 1 /* never started, terminated */
538 #define CON_UP 2 /* ready to use */
539 #define CON_STARTING 3 /* trying to start */
540 #define CON_STOPPING 4 /* trying to stop */
543 * The following are used as keywords when distinguishing input from output
546 #define CON_INPUT 1 /* this is an input half */
548 #define CON_OUTPUT 2 /* this is an output half */
552 /*----------------------------------------------------------
556 * This is the array of connection control data
557 * structures for gdb. Every connection has its
558 * structure stored here, but they are in no
559 * particular order. Because the connection data
560 * itself cannot be moved (due to possible dangling
561 * pointers), there may be some unused connections
562 * in the middle of this array. gdb_mcons is the
563 * 1 based number of the highest connection which is
564 * actually in use at this time. This is a considerable
565 * optimization for the typical case where very few
566 * are in use, and turnover is low.
568 * These are externs for globals defined in gdb_lib.h
569 * and included by gdb.c.
571 *----------------------------------------------------------*/
573 extern int gdb_mcons; /* one based number of the */
574 /* highest connection */
575 /* descriptor we're using */
578 extern int gdb_mfd; /* number of the highest */
579 /* file descriptor in use */
580 /* for a connection */
581 extern struct con_data gdb_cons[GDB_MAX_CONNECTIONS];
582 /* actual connection data */
585 extern fd_set gdb_crfds, gdb_cwfds, gdb_cefds; /* connection related file */
586 /* descriptor maps to be */
588 extern fd_set last_crfds, last_cwfds, last_cefds;/* these file desc. bit */
589 /* masks are set up */
590 /* for each select call */
591 /* to include the user */
592 /* supplied and the */
593 /* connection related */
596 /*----------------------------------------------------------
600 *----------------------------------------------------------*/
602 #define GDB_OP_ID 0x4f505200
605 struct oper_data *next, *prev; /* forward and back chain */
606 long id; /* should contain OPR\0 */
607 int tag; /* unique identifier for */
609 int status; /* current state of this */
612 #define OPF_MARKED_COMPLETE 0x00000001 /* operation was already */
613 /* complete when opsel was */
615 #define OPF_MARKED_CANCELLED 0x00000002 /* operation was already */
616 /* cancelled when opsel was */
618 int result; /* when the operation */
619 /* completes, this field is */
620 /* set to reflect its dispos-*/
621 /* ition. Contents of this */
622 /* field will depend on the */
623 /* operation being performed */
624 HALF_CONNECTION halfcon; /* the half connection on */
625 /* which this operation is */
627 /* May be meaningless if not */
629 char *arg; /* pointer to user data */
631 int (*init)(); /* pointer to routine to */
632 /* call to start this */
635 int (*cont)(); /* pointer to function to */
636 /* be called when this */
637 /* logical operation */
641 int (*cancel)(); /* this routine gets called */
642 /* to handle a cancel request*/
643 /* this field is normally */
644 /* set to NULL when the */
645 /* operation is queued and */
646 /* updated as required by */
647 /* the init and cont funcs. */
650 typedef struct oper_data OPERATION_DATA; /* an actual operation */
651 /* descritor, creator is */
652 /* responsible for making */
653 /* sure that memory is not */
654 /* freed while in use */
655 typedef OPERATION_DATA *OPERATION; /* a handle on an operation */
656 /* this is what most */
657 /* applications will use */
659 /*----------------------------------------------------------
661 * STATES OF AN OPERATION
663 * These represent the state of an asynchronous, queued
664 * operation. For convenience of the application programmer,
665 * some of these are folded together when queried through the
666 * operation_status routine. In particular, operation status
667 * returns only one of:
669 * OP_NOT_RUNNING, OP_RUNNING, OP_COMPLETE, or
672 * Any other status is reported as OP_RUNNING. This is
673 * done on the assumption that it makes correct coding
674 * of applications less error-prone, as there are fewer
675 * cases to check, and most of them would not be of
678 * Note that OP_CANCELLED may be generated by the system
679 * even when no explicit request for cancellation has been
680 * issued. For example, this may occur when a connection
681 * is severed unexpectedly.
683 * WARNING: If you change any of the following, be sure
684 * to make the equivalent changes to gdb_debug.c.
686 * We also define here certain standard values of OP_RESULT,
687 * since some return conventions presume that op_status
688 * and op_result values are orthogonal.
690 *----------------------------------------------------------*/
692 #define OP_SUCCESS 0 /* this value is actually */
693 /* used only in result */
694 /* fields, but it is */
695 /* sometimes convenient to */
696 /* have status and result */
697 /* have orthogonal values */
698 #define OP_NOT_STARTED 1 /* this operation has been */
699 /* initialized but is not on */
700 /* any connection's queue */
701 #define OP_QUEUED 2 /* this operation is on */
702 /* some connection's queue */
703 /* but it has not yet */
704 /* reached the head of the q */
705 #define OP_RUNNING 3 /* op is at head of q trying */
707 #define OP_COMPLETE 4 /* operation has run to */
708 /* completion. result field */
710 #define OP_CANCELLING 5 /* we are in the process of */
711 /* (trying to) cancel this */
713 #define OP_CANCELLED 6 /* operation was prematurely */
714 /* terminated. result field */
716 #define OP_MARKED 7 /* used by op_select_all */
717 #define OP_REQUEUED 8 /* returned by an init or */
718 /* continuation routine to */
719 /* indicate that the */
720 /* operation has requeued */
722 #define OP_PREEMPTED 9 /* returned by an init or */
723 /* continuation routine to */
724 /* indicate that the op has */
725 /* preempted itself by */
726 /* queueing a new operation */
727 /* ahead of itself */
729 /*----------------------------------------------------------
733 *----------------------------------------------------------*/
736 int count; /* number of operations */
738 OPERATION op[1]; /* really op[n], but */
739 /* structs must have a */
740 /* definite length */
743 typedef struct oper_list *LIST_OF_OPERATIONS; /* handle on a list */
745 #define size_of_list_of_operations(n) \
746 (sizeof(struct oper_list) + (n-1)*sizeof(OPERATION))
748 /*----------------------------------------------------------
752 * Pass this to select when doing a poll.
754 *----------------------------------------------------------*/
756 extern struct timeval gdb_notime;
759 /************************************************************************
761 * CHECKING ROUTINES IMPLEMENTED AS MACROS
763 ************************************************************************/
765 extern char g_errstr[150]; /* build emsgs here */
767 #define GDB_INIT_CHECK g_chk_init(); /* make sure gdb_init */
770 #define GDB_CHECK_CON(con, where) if ((con)->id != GDB_CON_ID) \
771 { (void) sprintf(g_errstr, "Invalid connection descriptor passed to \"%s\"\n", where); \
772 GDB_GIVEUP(g_errstr) }
775 #define GDB_CHECK_TUP(tup, where) if ((tup)->id != GDB_TUP_ID) \
776 { (void) sprintf(g_errstr, "Invalid tuple passed to \"%s\"\n", where); \
777 GDB_GIVEUP(g_errstr) }
780 #define GDB_CHECK_TPD(tpd, where) if ((tpd)->id != GDB_DESC_ID) \
781 { (void) sprintf(g_errstr, "Invalid tuple descriptor passed to \"%s\"\n", where); \
782 GDB_GIVEUP(g_errstr) }
785 #define GDB_CHECK_REL(rel, where) if ((rel)->id != GDB_REL_ID) \
786 { (void) sprintf(g_errstr, "Invalid relation passed to \"%s\"\n", where); \
787 GDB_GIVEUP(g_errstr) }
789 #define GDB_CHECK_OP(op, where) if ((op)->id != GDB_OP_ID) \
790 { (void) sprintf(g_errstr, "Invalid operation passed to \"%s\"\n", where); \
791 GDB_GIVEUP(g_errstr) }
793 #define GDB_CHECK_DB(db, where) if (db->id != GDB_DB_ID) \
794 { (void) sprintf(g_errstr, "Invalid database handle passed to \"%s\"\n", where); \
795 GDB_GIVEUP(g_errstr) }
800 /************************************************************************
802 * TRANSPORT ROUTINES IMPLEMENTED AS MACROS
804 ************************************************************************/
806 /*----------------------------------------------------------
810 * Returns the status of the indicated connection.
811 * Possible return values are:
813 * CON_STOPPED never started or terminated
814 * CON_UP currently usable
815 * CON_STARTING transient state on way up
816 * CON_STOPPING transient state on way down
818 *----------------------------------------------------------*/
820 #define connection_status(con) ((con)->status)
822 /*----------------------------------------------------------
826 * When a connection dies due to an error on a system
827 * call, the corresponding errno is recorded in the
828 * connection descriptor. This macro returns that value.
830 *----------------------------------------------------------*/
832 #define connection_errno(con) ((con)->errno)
836 /************************************************************************
838 * SERVER/CLIENT MANAGEMENT
840 * Definitions used in starting and maintaining communication
841 * between servers and clients (as opposed to peers.)
843 ************************************************************************/
845 #define GDB_MAX_SERVER_RETRIES 3 /* maximum number of times */
846 /* clients will accept */
847 /* forwarding requests from */
851 extern TUPLE_DESCRIPTOR gdb_tosrv; /* descriptor for request */
852 /* tuples sent to the */
853 /* server during negotiation*/
855 extern TUPLE_DESCRIPTOR gdb_fmsrv; /* descriptor for request */
856 /* tuples sent from the */
857 /* server during negotiation*/
859 #define GDB_MAX_SERVER_ID_SIZE 255 /* longest name of a server */
860 /* that we can handle */
861 #define GDB_MAX_SERVER_PARMS_SIZE 1023 /* longest parm string we */
862 /* can exchange between */
863 /* server and client*/
865 /*----------------------------------------------------------
867 * The following are values returned in the disposition
868 * field of the response tuple to indicate what the
869 * server has decided to do about the connection
872 *----------------------------------------------------------*/
874 #define GDB_ACCEPTED 1
875 #define GDB_REFUSED 2
876 #define GDB_FORWARDED 3
878 /*----------------------------------------------------------
880 * Global variables inherited by a child from a server
883 *----------------------------------------------------------*/
885 extern TUPLE gdb_client_tuple; /* request tuple sent from */
888 extern char gdb_sockaddr_of_client[100]; /* this should really be */
889 /* sockaddr_in, but I don't */
890 /* want everyone to have */
891 /* to include all those */
893 extern int gdb_socklen; /* length of above */
897 /************************************************************************
899 * DATABASE MANAGEMENT
901 * This layer of GDB provides access to the services of a relational
902 * database from anywhere in a GDB network.
904 ************************************************************************/
906 /*----------------------------------------------------------
910 * The name of the service, as found in /etc/services,
911 * for GDB database servers.
913 *----------------------------------------------------------*/
915 #define GDB_DB_SERVICE "#9420"
917 /*----------------------------------------------------------
921 * Describes a client's active connection to a database.
923 *----------------------------------------------------------*/
925 #define GDB_DB_ID 0x44420000 /* eye catcher */
928 long id; /* eye catcher */
929 CONNECTION connection; /* the GDB connection */
930 /* used to get at this */
932 int status; /* status of this */
933 /* database connection */
934 #define DB_OPEN 1 /* database opened */
936 #define DB_CLOSED 2 /* not open */
937 char *name; /* pointer to string name */
938 /* of the database, for */
940 char *server; /* pointer to the i.d. */
941 /* of the server, for */
945 typedef struct db_struct *DATABASE;
947 /*----------------------------------------------------------
949 * Return codes from DB operations
951 *----------------------------------------------------------*/
953 #define DB_NO_SUCH_OP 3
955 /*----------------------------------------------------------
957 * Parameters which limit sizes of things
959 *----------------------------------------------------------*/
961 #define GDB_MAX_QUERY_SIZE 2048 /* length of the longest */
962 /* substituted query */
963 /* string we can make */
964 #define GDB_MAX_QUERY_FIELDS 100 /* maximum number of fields */
965 /* we can retrieve in one */
967 #define GDB_SIZE_OF_INGRES_TEXT 2001 /* number of chars in */
968 /* largest ingres text */
970 #define GDB_MAX_RETRIEVED_TEXT_FIELDS 60 /* maximum number of text */
971 /* type fields we can */
972 /* retrieve in a single */
973 /* query. we hold this */
974 /* down because stack space */
975 /* is taken for max size of */
976 /* each during query. */
978 /*----------------------------------------------------------
980 * Return codes from database operations
982 *----------------------------------------------------------*/
984 #define DB_PARSE_FAIL (-3) /* couldn't parse */
985 /* the request string*/
988 /************************************************************************
990 * DATABASE OPERATIONS IMPLEMENTED
993 ************************************************************************/
995 /*----------------------------------------------------------
999 *----------------------------------------------------------*/
1001 #define DB_STATUS(dbhandle) ((dbhandle)->status)
1006 /************************************************************************
1010 * To allow dynamic manipulation of strings in gdb without
1011 * excessive memory re-allocation, we define a string as a
1012 * counted byte space. Though this space will frequently be used
1013 * to store a standard null terminated string, that is not
1016 * Current representation for a string is a pointer followed by
1017 * an integer length. A null pointer indicates a null string, in
1018 * which case the length is arbitrary. Any other pointer is to
1019 * memory which was allocated by db_alloc in which must be free'd
1020 * eventually with db_free.
1022 ************************************************************************/
1024 typedef struct str_dat {
1025 char *ptr; /* pointer to the data */
1026 int length; /* length of the allocated */
1027 /* memory (not necessarily */
1028 /* length of null-term'd */
1029 /* string stored there) */
1032 /*----------------------------------------------------------
1034 * Macros for manipulating strings. These return
1035 * the actual data from the string and the size of
1036 * the data space respectively. To get the length of
1037 * the null terminated string which might be stored
1038 * there, use strlen(STRING_DATA(string)).
1040 *----------------------------------------------------------*/
1043 #define STRING_DATA(str) ((str).ptr)
1044 #define MAX_STRING_SIZE(str) ((str).length)
1047 /************************************************************************
1051 * db_alloc and db_free are the externally visible names of
1052 * the memory allocation services. These actually call the
1053 * routines pointed to by the vectors gdb_amv and gdb_fmv, which
1054 * default to the supplied routines gdb_am and gdb_fm. Users
1055 * may supply their own memory allocation by storing over the
1056 * vectors. This may be done prior to calling gdb_init to
1057 * insure that all dynamic memory is controlled by the user.
1059 ************************************************************************/
1061 #define db_alloc (*gdb_amv)
1062 #define db_free (*gdb_fmv)
1064 extern char *gdb_am();
1065 extern int gdb_fm();
1067 extern char *((*gdb_amv)());
1068 extern int (*gdb_fmv)();
1071 /************************************************************************
1073 * STRUCTURED DATA ROUTINES IMPLEMENTED AS MACROS
1075 ************************************************************************/
1077 /*----------------------------------------------------------
1079 * ADD_TUPLE_TO_RELATION
1081 *----------------------------------------------------------*/
1083 #define ADD_TUPLE_TO_RELATION(relation, tuple) \
1085 (tuple)->prev = (relation)->last; \
1086 (tuple)->next = (TUPLE)(relation); \
1087 (relation)->last = tuple; \
1088 (tuple)->prev->next = tuple; \
1091 /*----------------------------------------------------------
1093 * ADD_TUPLE_AFTER_TUPLE
1095 *----------------------------------------------------------*/
1097 #define ADD_TUPLE_AFTER_TUPLE(relation, tuple, prev_tuple) \
1099 (tuple)->prev = (prev_tuple)->next->prev; \
1100 (tuple)->next = (prev_tuple)->next; \
1101 (tuple)->next->prev = tuple; \
1102 (prev_tuple)->next = tuple; \
1105 /*----------------------------------------------------------
1107 * REMOVE_TUPLE_FROM_RELATION
1109 *----------------------------------------------------------*/
1111 #define REMOVE_TUPLE_FROM_RELATION(relation, tuple) \
1113 (tuple)->prev->next = (tuple)->next; \
1114 (tuple)->next->prev = (tuple)->prev; \
1118 /*----------------------------------------------------------
1120 * DESCRIPTOR_FROM_TUPLE
1122 *----------------------------------------------------------*/
1124 #define DESCRIPTOR_FROM_TUPLE(tuple) ((tuple)->desc)
1126 /*----------------------------------------------------------
1128 * DESCRIPTOR_FROM_RELATION
1130 *----------------------------------------------------------*/
1132 #define DESCRIPTOR_FROM_RELATION(relation) ((relation)->desc)
1134 /*----------------------------------------------------------
1136 * REFERENCE_TUPLE_DESCRIPTOR
1138 * Bumps the reference count for a tuple descriptor.
1139 * Intended only for internal use of GDB.
1141 *----------------------------------------------------------*/
1143 #define REFERENCE_TUPLE_DESCRIPTOR(tpd) (++((tpd)->ref_count))
1145 /*----------------------------------------------------------
1147 * UNREFERENCE_TUPLE_DESCRIPTOR
1149 * Decrements the reference count for a tuple descriptor.
1150 * Intended only for internal use of GDB. Warning: it
1151 * is the user's responsibility to use delete_tuple_descriptor
1152 * instead of this macro in any situation in which the
1153 * reference count might go to zero.
1155 *----------------------------------------------------------*/
1157 #define UNREFERENCE_TUPLE_DESCRIPTOR(tpd) (--((tpd)->ref_count))
1159 /*----------------------------------------------------------
1163 *----------------------------------------------------------*/
1165 #define FIELD_FROM_TUPLE(tuple, field_index) \
1166 (((tuple)->data) + (((tuple)->desc)->var[field_index].offset))
1168 /*----------------------------------------------------------
1170 * FIELD_OFFSET_IN_TUPLE
1172 *----------------------------------------------------------*/
1174 #define FIELD_OFFSET_IN_TUPLE(tuple_descriptor, field_index) \
1175 ((tuple_descriptor)->var[field_index].offset)
1177 /*----------------------------------------------------------
1179 * FIELD_TYPE_IN_TUPLE
1181 *----------------------------------------------------------*/
1183 #define FIELD_TYPE_IN_TUPLE(tuple_descriptor, field_index) \
1184 ((tuple_descriptor)->var[field_index].type)
1186 /*----------------------------------------------------------
1188 * FIRST_TUPLE_IN_RELATION
1190 *----------------------------------------------------------*/
1192 #define FIRST_TUPLE_IN_RELATION(relation) \
1193 (((relation)->first) == (TUPLE)relation ? NULL : (relation)-> first)
1195 /*----------------------------------------------------------
1197 * NEXT_TUPLE_IN_RELATION
1199 *----------------------------------------------------------*/
1201 #define NEXT_TUPLE_IN_RELATION(relation, prev) \
1202 (((prev)->next) == (TUPLE)relation ? NULL : prev->next )
1204 /*----------------------------------------------------------
1206 * PREV_TUPLE_IN_RELATION
1208 *----------------------------------------------------------*/
1210 #define PREV_TUPLE_IN_RELATION(relation, next) \
1211 (((next)->prev) == (TUPLE) relation ? NULL : next->prev)
1216 /************************************************************************
1218 * TRANSPORT and OPERATION SERVICES IMPLEMENTED AS MACROS
1220 ************************************************************************/
1222 /*----------------------------------------------------------
1224 * OPERATION_FROM_DATA
1226 * Given OPERATION_DATA, return the corresponding handle
1227 * of type OPERATION. Currently, OPERATION is just
1228 * implemented as a pointer to OPERATION_DATA.
1230 *----------------------------------------------------------*/
1232 #define OPERATION_FROM_DATA(op_data) \
1233 ((OPERATION)&(op_data))
1235 /*----------------------------------------------------------
1239 * Return the tag for a given operation.
1241 *----------------------------------------------------------*/
1243 #define OP_TAG(operation) ((operation)->tag)
1245 /*----------------------------------------------------------
1249 * Return the status of a given operation. Note that
1250 * status describes an operations progress through
1251 * execution. It has the same values for all operations.
1252 * Result describes the final outcome of an operation.
1253 * It's values depend on the particular operation which
1256 *----------------------------------------------------------*/
1258 #define OP_STATUS(operation) ((operation)->status)
1260 /*----------------------------------------------------------
1264 * True iff the operation is either OP_COMPLETE or
1267 *----------------------------------------------------------*/
1269 #define OP_DONE(op) ((op)->status == OP_COMPLETE || (op)->status == OP_CANCELLED)
1271 /*----------------------------------------------------------
1275 * Return the result of a given operation. Note that
1276 * status describes an operations progress through
1277 * execution. It has the same values for all operations.
1278 * Result describes the final outcome of an operation.
1279 * It's values depend on the particular operation which
1280 * was attempted. The special result value -1 is used
1281 * to indicate an invalid value for result. Generally,
1282 * -1 is returned when result is accidently queried at
1283 * a time when OP_STATUS != OPERATION_COMPLETE.
1285 *----------------------------------------------------------*/
1287 #define OP_RESULT(operation) ((operation)->result)
1290 /************************************************************************
1292 * Debugging Interfaces
1294 ************************************************************************/
1296 /*----------------------------------------------------------
1298 * The following operation codes my be passed to
1299 * gdb_debug to set special debugging modes of operation.
1301 * Note that most of these are toggles
1303 *----------------------------------------------------------*/
1305 #define GDB_LOG 0x00000001 /* turn on tracing to */
1307 #define GDB_NOFORK 0x00000002 /* don't fork forking */
1311 /************************************************************************
1313 * Things which have to be at the end because they require
1314 * the various types to be defined first.
1316 ************************************************************************/
1319 #ifdef DUMB_7_CHAR_LOADER
1320 /*----------------------------------------------------------
1322 * Long Names for Routines
1324 * External names in Unix must generally be unique
1325 * within the first 7 characters or so, at least for
1326 * some versions of ld. To account for this without
1327 * making all our routine names terribly short and
1328 * cryptic, we use the following defines.
1330 *----------------------------------------------------------*/
1332 #define string_alloc gdb_sta
1333 #define string_free gdb_stf
1335 #define create_tuple_descriptor gdb_ctd
1336 #define delete_tuple_descriptor gdb_dtd
1337 #define field_index gdb_fi
1338 #define create_tuple gdb_ctup
1339 #define delete_tuple gdb_dtup
1340 #define initialize_tuple gdb_itup
1341 #define null_tuple_strings gdb_ntps
1343 #define create_relation gdb_crel
1344 #define delete_relation gdb_drel
1345 #define tuples_in_relation gdb_trel
1351 #define create_operation gdb_crop
1352 #define delete_operation gdb_dop
1353 #define initialize_operation gdb_inop
1354 #define reset_operation gdb_rsop
1355 #define cancel_operation gdb_cnop
1357 #define create_list_of_operations gdb_clop
1358 #define delete_list_of_operations gdb_dlop
1360 #define op_select gdb_opsel
1361 #define op_select_any gdb_opsel
1362 #define op_select_all gdb_aopsel
1363 #define con_select gdb_cnsel
1365 #define gdb_receive_data gdb_rcdat
1366 #define gdb_send_data gdb_sndat
1367 #define gdb_start_listening gdb_stl
1368 #define start_accepting_client gdb_stac
1371 #define gdb_listen gdb_lis
1374 * Connection management
1376 #define start_peer_connection gdb_spconn
1377 #define sever_connection gdb_svconn
1378 #define start_server_connection gdb_stsrv
1379 #define create_listening_connection gdb_clc
1380 #define start_replying_to_client gdb_strtc
1381 #define create_forking_server gdb_cfs
1385 * Asynchronous operations
1387 #define start_sending_object gdb_snobj
1388 #define start_receiving_object gdb_rcobj
1389 #define preempt_and_start_receiving_object gdb_prcobj
1391 #define queue_operation gdb_qop
1393 #define requeue_operation g_req_op
1395 #define complete_operation gdb_cmpo
1397 * Synchronous operations
1399 #define send_object gdb_sobj
1400 #define receive_object gdb_robj
1402 * Database operations
1404 #define access_db gdb_adb
1405 #define start_accessing_db gdb_sadb
1406 #define perform_db_operation gdb_pdbo
1407 #define db_query gdb_dbq
1408 #define start_performing_db_operation gdb_spdb
1409 #define start_db_query gdb_sdbq
1411 #define op_select_any op_select
1412 #endif /* DUMB_7_CHAR_LOADER */
1414 extern char *string_alloc();
1415 extern int string_free();
1416 extern TUPLE_DESCRIPTOR create_tuple_descriptor();
1417 extern int delete_tuple_descriptor();
1418 extern int field_index();
1419 extern TUPLE create_tuple();
1420 extern int delete_tuple();
1421 extern int initialize_tuple();
1422 extern int null_tuple_strings();
1423 extern RELATION create_relation();
1424 extern int delete_relation();
1425 extern OPERATION create_operation();
1426 extern LIST_OF_OPERATIONS create_list_of_operations();
1427 extern OPERATION g_op_newhead();
1428 extern CONNECTION start_peer_connection();
1429 extern CONNECTION sever_connection();
1430 extern CONNECTION start_server_connection();
1431 extern CONNECTION create_listening_connection();
1432 extern CONNECTION create_forking_server();
1433 extern int start_sending_object();
1434 extern int start_receiving_object();
1435 extern int preempt_and_start_receiving_object();
1436 extern int queue_operation();
1437 extern int requeue_operation();
1438 extern int complete_operation();
1439 extern int send_object();
1440 extern int receive_object();
1443 /*----------------------------------------------------------
1445 * Other routines requiring extern to avoid forward
1446 * reference to non integer type functions.
1448 *----------------------------------------------------------*/
1450 extern CONNECTION g_make_con();