head     1.3;
branch   ;
access   ;
symbols  C_Demo_1:1.3;
locks    sp:1.3; strict;
comment  @ * @;


1.3
date     89.09.05.17.22.47;  author mao;  state C_Demo_1;
branches ;
next     1.2;

1.2
date     89.05.23.16.18.31;  author sp;  state Exp;
branches ;
next     1.1;

1.1
date     89.02.20.16.27.26;  author hirohama;  state Stab;
branches ;
next     ;


desc
@@


1.3
log
@Working version of C-only demo
@
text
@static	char	RuleLockOut_c[] = "$Header: /usr6/postgres/mao/postgres/src/rules/locks/RCS/print.c,v 1.2 89/05/23 16:18:31 sp Exp $";

#include <stdio.h>
#include "postgres.h"
#include "log.h"
#include "rlock.h"
#include "internal.h"

#define DEFAULT_MAXLENGTH 100
#define MAXDIGITS 100

/*
 * GLOBAL VARIABLES
 */

static char *lock_string;	/* The buffer where the result is stored */
static long current_length;	/* the *current* length of the lock_string. */
static long maxlength;		/* the current length of the buffer */


char *
RuleLockOut(RuleLockInternal)
RuleLock RuleLockInternal;
{

    RuleLockParseStatus *scan_status;
    ObjectId ruleid;
    char priority;
    char ruletype;
    bool isearly;
    char locktype;
    long attrno;
    long planno;
    ObjectId prev_ruleid = (ObjectId)0;
    int first_lock;


    maxlength = DEFAULT_MAXLENGTH;
    lock_string = (char *)MyAlloc((unsigned long)maxlength);
    current_length = 0;

    first_lock=1;
    RuleLockPrintChar('[');
    scan_status = RuleLockBeginScan(RuleLockInternal);
    while(RuleLockGetNextLock(scan_status, &ruleid, &priority,
			&ruletype, &isearly, &locktype, &attrno, &planno)) {
	if (first_lock || prev_ruleid != ruleid) {
	    if (!first_lock)
		RuleLockPrintChar('}');
	    first_lock = 0;
	    prev_ruleid = ruleid;
	    RuleLockPrintChar('{');
	    RuleLockPrintOid(ruleid);
	    RuleLockPrintChar(',');
	    RuleLockPrintByte(priority);
	    RuleLockPrintChar(',');
	    RuleLockPrintRuleType(ruletype);
	    RuleLockPrintChar(',');
	    RuleLockPrintIsEarly(isearly);
	}
	RuleLockPrintChar(',');
	RuleLockPrintChar('(');
	RuleLockPrintLockType(locktype);
	RuleLockPrintChar(',');
	RuleLockPrintLong((unsigned long)attrno);
	RuleLockPrintChar(',');
	RuleLockPrintLong((unsigned long)planno);
	RuleLockPrintChar(')');
    }
    RuleLockEndScan(scan_status);

    if (!first_lock)
	RuleLockPrintChar('}');
    RuleLockPrintChar(']');
    RuleLockPrintChar('\0');

    return(lock_string);

}


RuleLockPrintChar(byte)
char byte;
{
    register char *p1, *p2;
    register int i;
    char *temp;

    if (current_length>=maxlength) {
	/*
	 * Need to reallocate more space...
	 */
	maxlength = 2*maxlength;
	temp = (char *) MyAlloc((unsigned long)maxlength);
	/*
	 * Copy "lock_string" to "temp".
	 */
	p1 = lock_string;
	p2 = temp;
	for (i=0; i<maxlength ; i++)
	    *(p2++) = *(p1++);

	MyFree(lock_string);
	lock_string = temp;

    }

    lock_string[current_length++] = byte;
}

RuleLockPrintLong(ul)
unsigned long ul;
{
    char digits[MAXDIGITS];
    int ndigits;

    ndigits=0;
    while(ul >0) {
	digits[ndigits++] = '0' + ul % 10;
	ul /= 10;
    };

    if (ndigits == 0) {
	RuleLockPrintChar('0');
    }
    else  {
	for ( ndigits-- ; ndigits>=0 ; ndigits--)
	    RuleLockPrintChar(digits[ndigits]);
    }
}

RuleLockPrintByte(byte)
char byte;
{
    RuleLockPrintLong((unsigned long)byte);
}

RuleLockPrintOid(oid)
ObjectId oid;
{
    RuleLockPrintLong((unsigned long)oid);
}

RuleLockPrintIsEarly(isearly)
bool isearly;
{
    isearly ? RuleLockPrintChar('E') : RuleLockPrintChar('L');
}

RuleLockPrintLockType(locktype)
char locktype;
{
    switch ((int)locktype) {
	case RuleLockTypeWrite:
	    RuleLockPrintChar('W');
	    break;
	case RuleLockTypeRead1:
	    RuleLockPrintChar('R');
	    RuleLockPrintChar('1');
	    break;
	case RuleLockTypeRead2:
	    RuleLockPrintChar('R');
	    RuleLockPrintChar('2');
	    break;
	case RuleLockTypeRead3:
	    RuleLockPrintChar('R');
	    RuleLockPrintChar('3');
	    break;
	default:
	    elog(WARN, "Error Corrupted locktype %d\n", (int)locktype);
	    RuleLockPrintChar('*');
    }
}

RuleLockPrintRuleType(ruletype)
char ruletype;
{
    switch ((int)ruletype) {
	case 0:
	    RuleLockPrintChar('R');
	    break;
	case 1:
	    RuleLockPrintChar('M');
	    break;
	case 2:
	    RuleLockPrintChar('A');
	    break;
	case 3:
	    RuleLockPrintChar('D');
	    break;
	case 4:
	    RuleLockPrintChar('R');
	    RuleLockPrintChar('R');
	    break;
	case 5:
	    RuleLockPrintChar('R');
	    RuleLockPrintChar('M');
	    break;
	case 6:
	    RuleLockPrintChar('R');
	    RuleLockPrintChar('A');
	    break;
	case 7:
	    RuleLockPrintChar('R');
	    RuleLockPrintChar('D');
	    break;
	default:
	    elog(WARN, "Error Corrupted ruletype %d\n", (int)ruletype);
	    RuleLockPrintChar('*');
    }
}

@


1.2
log
@global variables are declared static (local to this file)
@
text
@d1 1
a1 1
static	char	RuleLockOut_c[] = "$Header: print.c,v 1.2 89/05/23 11:56:54 sp Exp $";
@


1.1
log
@Initial revision
@
text
@d1 1
a1 1
static	char	RuleLockOut_c[] = "$Header: RuleLockOut.c,v 1.2 89/02/02 15:46:18 aoki Exp $";
d16 3
a18 3
char *lock_string;	/* The buffer where the result is stored */
long current_length;	/* the *current* length of the lock_string. */
long maxlength;		/* the current length of the buffer */
@
