Esempio n. 1
0
File: tobl2.c Progetto: yumm007/C
int main(void)
{
	int c;
	
	readline();

	while ((c = get_c()) != '\n') {
		switch (c){
			case '0': case '1': case '2': case '3': case '4':
			case '5': case '6': case '7': case '8': case '9':
				unget_c();
				readn();
				break;
			case '+': case '-':  /* 遇到加减法时,先读入下一组数字,再存入运算符号 */
				readn();
				pushstr(c);
				break;
			case '*': case '/':  /* 遇到乘除法时,现把当前的预算符出栈,存入操作数,再存入乘除符号 */
				pushtemp(popstr());
				readn();
				pushstr(c);
				pushstr(poptemp()); /* 原先的预算符号进栈 */
			default:
				break;
		}
	}
	
	/* 输出逆波兰数序 */
	pushstr('\0');
	printf("%s\n", str);

	return 0;
}
Esempio n. 2
0
File: tobl2.c Progetto: yumm007/C
/* 读入一组数字,依次入栈,并且以逗号结尾*/
void readn(void) {
	int c;
	while ((c = get_c()) && c >= '0' && c <= '9') 
		pushstr(c);
	pushstr(',');
	unget_c();
}
Esempio n. 3
0
int
db2sqldelete( Dbptr db, char *sync, Tbl **tbl, long flags )
{
	void	*stk = 0;
	char	*table;
	char	*cmd;

	if( *tbl == (Tbl *) NULL ) {
		
		*tbl = newtbl( 0 );
	}

	dbquery( db, dbTABLE_NAME, &table );

	pushstr( &stk, "DELETE from `" );
	pushstr( &stk, table );

	pushstr( &stk, "` WHERE " );

	pushstr( &stk, "  `" );
	pushstr( &stk, Db2sql_syncfield_name );
	pushstr( &stk, "` = '" );
	pushstr( &stk, sync );
	pushstr( &stk, "'" );
	pushstr( &stk, ";\n" );

	cmd = popstr( &stk, 1 );

	pushtbl( *tbl, cmd );

	return 1;
}
Esempio n. 4
0
/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  int n = 0;
  for (;;) {
    const char *e = strchr(fmt, '%');
    if (e == NULL) break;
    setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
    incr_top(L);
    switch (*(e+1)) {
      case 's': {
        const char *s = va_arg(argp, char *);
        if (s == NULL) s = "(null)";
        pushstr(L, s, strlen(s));
        break;
      }
      case 'c': {
        char buff;
        buff = cast(char, va_arg(argp, int));
        pushstr(L, &buff, 1);
        break;
      }
      case 'd': {
        setnvalue(L->top, cast_num(va_arg(argp, int)));
        incr_top(L);
        break;
      }
      case 'f': {
      	// Floats passed through ... are promoted to doubles...
        setnvalue(L->top, cast_num(va_arg(argp, double)));
        incr_top(L);
        break;
      }
      case 'p': {
        char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
        int l = sprintf(buff, "%p", va_arg(argp, void *));
        pushstr(L, buff, l);
        break;
      }
      case '%': {
        pushstr(L, "%", 1);
        break;
      }
      default: {
        luaG_runerror(L,
            "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
            *(e + 1));
      }
    }
    n += 2;
    fmt = e+2;
  }
  pushstr(L, fmt, strlen(fmt));
  if (n > 0) luaV_concat(L, n + 1);
  return svalue(L->top - 1);
}
Esempio n. 5
0
static void
add_endtag( void **vstack, char *tagname )
{
	char	*copy;

	copy = strdup( tagname );
	strtrim( copy );

	pushstr( vstack, "</" );
	pushstr( vstack, copy );
	pushstr( vstack, ">" );

	free( copy );
}
Esempio n. 6
0
void postfix_to_infix(void)
{
  int i,j,k,n=0;
  char t,op1[20],op2[30],temp[30];
  for(t=gettoken(&n);t!='\0';t=gettoken(&n))
  {
    if(t>='0' && t<='9' || t>='a' && t<='z' || t>='A' && t<='Z')
    push(t);
    else if(t=='+' || t=='-' || t=='*' || t=='/' || t=='%')
    {
    pop(temp);
    strcpy(op2,temp);
    pop(temp);
    strcpy(op1,temp);
    for(k=strlen(op1)+1;k>0;k--)
      op1[k]=op1[k-1];
      op1[0]='(';
      j=strlen(op1);
      op1[j]=t;
      op1[j+1]='\0';
      for(i=strlen(op1),j=0;op2[j]!='\0';i++,j++)
      op1[i]=op2[j];
      op1[i]=')';
      op1[i+1]='\0';
      pushstr(op1);
    }
   }
 }
Esempio n. 7
0
/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  int n = 1;
  pushstr(L, "");
  for (;;) {
    const char *e = strchr(fmt, '%');
    if (e == NULL) break;
    setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
    incr_top(L);
    switch (*(e+1)) {
      case 's': {
        const char *s = va_arg(argp, char *);
        if (s == NULL) s = "(null)";
        pushstr(L, s);
        break;
      }
      case 'c': {
        char buff[2];
        buff[0] = cast(char, va_arg(argp, int));
        buff[1] = '\0';
        pushstr(L, buff);
        break;
      }
      case 'd': {
        setnvalue(L->top, cast_num(va_arg(argp, int)));
        incr_top(L);
        break;
      }
      case 'f': {
		
        setnvalue(L->top, cast_num(va_arg(argp, double)));
        incr_top(L);
        break;
      }
      case 'p': {
        char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
        sprintf(buff, "%p", va_arg(argp, void *));
        pushstr(L, buff);
        break;
      }
      case '%': {
        pushstr(L, "%");
        break;
      }
      default: {
        char buff[3];
        buff[0] = '%';
        buff[1] = *(e+1);
        buff[2] = '\0';
        pushstr(L, buff);
        break;
      }
    }
    n += 2;
    fmt = e+2;
  }
  pushstr(L, fmt);
  luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);
  L->top -= n;
  return svalue(L->top - 1);
}
Esempio n. 8
0
char *
translate_function(char *name)
{
        char *output = search_function(name);
        if (output == NULL) {
                printf("\nUnknown function: %s\n", name);
                pushstr("unknown");
                output = poolp;
        }
        return output;
}
Esempio n. 9
0
void
get_function(void)
{
        function = read_function();
        function_symbol = search_symbol(function, &function_code, 0);
        if (function_symbol == NULL) {
                function_symbol = poolp;
                pushstr("ECL_NIL");
                pushc('\0');
        }
        function_c_name = translate_function(function);
}
Esempio n. 10
0
char *
search_symbol(char *name, int *symbol_code, int code)
{
        int i;
        for (i = 0; cl_symbols[i].name != NULL; i++) {
                if (!strcasecmp(name, cl_symbols[i].name)) {
                        name = poolp;
                        if (code) {
                                pushstr("ecl_make_fixnum(/*");
                                pushstr(cl_symbols[i].name);
                                pushstr("*/");
                                if (i >= 1000)
                                        pushc((i / 1000) % 10 + '0');
                                if (i >= 100)
                                        pushc((i / 100) % 10 + '0');
                                if (i >= 10)
                                        pushc((i / 10) % 10 + '0');
                                pushc(i % 10 + '0');
                                pushstr(")");
                                pushc(0);
                        } else if (i == 0) {
                                pushstr("ECL_NIL");
                                pushc(0);
                        } else {
                                pushstr("ECL_SYM(\"");
                                pushstr(cl_symbols[i].name);
                                pushstr("\",");
                                if (i >= 1000)
                                        pushc((i / 1000) % 10 + '0');
                                if (i >= 100)
                                        pushc((i / 100) % 10 + '0');
                                if (i >= 10)
                                        pushc((i / 10) % 10 + '0');
                                pushc(i % 10 + '0');
                                pushstr(")");
                                pushc(0);
                        }
                        if (symbol_code)
                                *symbol_code = i;
                        return name;
                }
        }
        return NULL;
}
Esempio n. 11
0
char *
search_function(char *name)
{
        int i;
        for (i = 0; cl_symbols[i].name != NULL; i++) {
                if (cl_symbols[i].translation != NULL &&
                    !strcasecmp(name, cl_symbols[i].name)) {
                        name = poolp;
                        pushstr(cl_symbols[i].translation);
                        pushc(0);
                        return name;
                }
        }
        return name;
}
Esempio n. 12
0
static void
add_dataelement( void **vstack, char *tagname, char *value )
{
	char	*copy;

	copy = strdup( value );
	strtrim( copy );

	safe_strsub( copy, "&", "&amp;", copy );
	safe_strsub( copy, "<", "&lt;", copy );
	safe_strsub( copy, ">", "&gt;", copy );

	add_starttag( vstack, tagname );
	pushstr( vstack, copy );
	add_endtag( vstack, tagname );

	free( copy );
}
Esempio n. 13
0
/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  int n = 0;
  for (;;) {
    const char *e = strchr(fmt, '%');
    if (e == NULL) break;
    luaD_checkstack(L, 2);  /* fmt + item */
    pushstr(L, fmt, e - fmt);
    switch (*(e+1)) {
      case 's': {
        const char *s = va_arg(argp, char *);
        if (s == NULL) s = "(null)";
        pushstr(L, s, strlen(s));
        break;
      }
      case 'c': {
        char buff;
        buff = cast(char, va_arg(argp, int));
        pushstr(L, &buff, 1);
        break;
      }
      case 'd': {
        setnvalue(L->top++, cast_num(va_arg(argp, int)));
        break;
      }
      case 'f': {
        setnvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
        break;
      }
      case 'p': {
        char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
        int l = sprintf(buff, "%p", va_arg(argp, void *));
        pushstr(L, buff, l);
        break;
      }
      case '%': {
        pushstr(L, "%", 1);
        break;
      }
      default: {
        luaG_runerror(L,
            "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
            *(e + 1));
      }
    }
    n += 2;
    fmt = e+2;
  }
  luaD_checkstack(L, 1);
  pushstr(L, fmt, strlen(fmt));
  if (n > 0) luaV_concat(L, n + 1);
  return svalue(L->top - 1);
}
Esempio n. 14
0
char *
read_symbol(int code)
{
        char c, *name = poolp;
        char end = code? ']' : '\'';

        c = readc();
        while (c != end) {
                if (c == '_') c = '-';
                pushc(c); 
                c = readc();
        }
        pushc(0);

        name = search_symbol(poolp = name, 0, code);
        if (name == NULL) {
                name = poolp;
                printf("\nUnknown symbol: %s\n", name);
                pushstr("unknown");
        }
        return name;
}
	ScriptObject* TypeDescriber::describeTraits(Traitsp traits, uint32_t flags)
	{
		if (!(flags & INCLUDE_TRAITS))
			return NULL;
			
		AvmCore* core = m_toplevel->core();
		GC* gc = core->GetGC();
		TraitsBindingsp tb = traits->getTraitsBindings();
		TraitsMetadatap tm = traits->getTraitsMetadata();

		ScriptObject* o = new_object();

		ArrayObject* bases = NULL;
		ArrayObject* metadata = NULL;
		ArrayObject* interfaces = NULL;
		ArrayObject* methods = NULL;
		ArrayObject* accessors = NULL;
		ArrayObject* variables = NULL;
		ScriptObject* constructor = NULL;

		if (flags & INCLUDE_BASES)
		{
			metadata = new_array();
			PoolObject* class_mdpool;
			const uint8_t* class_md = tm->getMetadataPos(class_mdpool);
			if (class_md)
				addDescribeMetadata(metadata, class_mdpool, class_md);
		}
		
		if (flags & INCLUDE_BASES)
		{
			bases = new_array();
			for (Traitsp b = traits->base; b; b = b->base) 
				pushstr(bases, describeClassName(b));
		}
		
		if (flags & INCLUDE_INTERFACES)
		{
			interfaces = new_array();
			// our TraitsBindings only includes our own interfaces, not any we might have inherited, 
			// so walk the tree. there might be redundant interfaces listed in the inheritance, 
			// so use a list to remove dupes
			List<Traitsp> unique(gc);
			for (Traitsp b = traits; b; b = b->base) 
			{
				TraitsBindingsp tbi = b->getTraitsBindings();
				for (uint32_t i = 0; i < tbi->interfaceCapacity; ++i)
				{
					Traitsp ti = tbi->getInterface(i);
					if (ti && ti->isInterface && unique.indexOf(ti) < 0)
					{
						unique.add(ti);
						pushstr(interfaces, describeClassName(ti));
					}
				}
			}
		}

		// constructor
		if (flags & INCLUDE_CONSTRUCTOR)
		{
			AbstractFunction* initMethod = traits->init;
			if (initMethod && initMethod->param_count)
			{
				constructor = describeParams(initMethod);
			}
		}
		
		if (flags & (INCLUDE_ACCESSORS | INCLUDE_METHODS | INCLUDE_VARIABLES))
		{
			// recover slot/method metadata and method-declarer information.
			
			// make a flattened set of bindings so we don't have to check for overrides as we go.
			// This is not terribly efficient, but doesn't need to be.
			MultinameHashtable* mybind = new (gc) MultinameHashtable();
			addBindings(mybind, tb, flags);

			// Don't want interface methods, so post-process and wipe out any
			// bindings that were added
			for (uint32_t i = 0; i < tb->interfaceCapacity; ++i)
			{
				Traitsp ti = tb->getInterface(i);
				if (ti && ti->isInterface)
				{
					TraitsBindingsp tbi = ti->getTraitsBindings();
					for (int32_t index = 0; (index = tbi->next(index)) != 0; )
					{
						Stringp name = tbi->keyAt(index);
						Namespacep ns = tbi->nsAt(index);
						mybind->add(name, ns, BIND_NONE);
					}
				}
			}
			
			// yuck, replicate buggy behavior in FP9/10
			List<Namespacep> nsremoval(gc);
			if (flags & HIDE_NSURI_METHODS)
			{
				for (uint32_t i = 0; i < tb->interfaceCapacity; ++i)
				{
					Traitsp ti = tb->getInterface(i);
					// already did interfaces, don't need to do them again
					if (ti && !ti->isInterface)
					{
						TraitsBindingsp tbi = ti->getTraitsBindings();
						for (int32_t index = 0; (index = tbi->next(index)) != 0; )
						{
							Namespacep ns = tbi->nsAt(index);
							if (ns->getURI()->length() > 0 && nsremoval.indexOf(ns) < 0)
								nsremoval.add(ns);
						}
					}
				}
			}

			for (int32_t index = 0; (index = mybind->next(index)) != 0; )
			{
				Binding binding = mybind->valueAt(index);
				Stringp name = mybind->keyAt(index);
				Namespacep ns = mybind->nsAt(index);
				Stringp nsuri = ns->getURI();
				TraitsMetadata::MetadataPtr md1 = NULL;
				TraitsMetadata::MetadataPtr md2 = NULL;
				PoolObject* md1pool = NULL;
				PoolObject* md2pool = NULL;

				// We only display public members -- exposing private namespaces could compromise security.
				if (ns->getType() != Namespace::NS_Public) 
					continue;
				
				if ((flags & HIDE_NSURI_METHODS) && nsremoval.indexOf(ns) >= 0)
					continue;
			
				ScriptObject* v = new_object();

				const BindingKind bk = AvmCore::bindingKind(binding);
				switch (bk)
				{
					case BKIND_CONST:
					case BKIND_VAR:
					{
						if (!(flags & INCLUDE_VARIABLES))
							continue;

						const uint32_t slotID = AvmCore::bindingToSlotId(binding);
						const KVPair props[] = {
							{ kstrid_access, strAtom(str(bk == BKIND_CONST ? kstrid_readonly : kstrid_readwrite)) },
							{ kstrid_type, strAtom(describeClassName(tb->getSlotTraits(slotID))) },
						};
						setpropmulti(v, props, elem_count(props));
						if (!variables) variables = new_array();
						pushobj(variables, v);
						md1 = tm->getSlotMetadataPos(slotID, md1pool);
						break;
					}

					case BKIND_METHOD:
					{
						if (!(flags & INCLUDE_METHODS))
							continue;

						const uint32_t methodID = AvmCore::bindingToMethodId(binding);
						const AbstractFunction* af = tb->getMethod(methodID);

						Traitsp declaringTraits = af->declaringTraits;

						const KVPair props[] = {
							{ kstrid_declaredBy, strAtom(describeClassName(declaringTraits)) },
							{ kstrid_returnType, strAtom(describeClassName(af->returnTraits())) },
							{ kstrid_parameters, objAtom(describeParams(af)) },
						};
						setpropmulti(v, props, elem_count(props));
						if (!methods) methods = new_array();
						pushobj(methods, v);
						md1 = tm->getMethodMetadataPos(methodID, md1pool);
						break;
					}
					
					case BKIND_GET:
					case BKIND_SET:
					case BKIND_GETSET:
					{
						if (!(flags & INCLUDE_ACCESSORS))
							continue;
							
						const uint32_t methodID = AvmCore::hasGetterBinding(binding) ?
													AvmCore::bindingToGetterId(binding) :
													AvmCore::bindingToSetterId(binding);

						const AbstractFunction* af = tb->getMethod(methodID);

						Traitsp declaringTraits = af->declaringTraits;

						Traitsp accessorType = AvmCore::hasGetterBinding(binding) ?
													af->returnTraits() :
													af->paramTraits(1);

						static const uint8_t bk2str[8] = 
						{
							uint8_t(kstrid_emptyString),	// BKIND_NONE
							uint8_t(kstrid_emptyString),	// BKIND_METHOD
							uint8_t(kstrid_emptyString),	// BKIND_VAR
							uint8_t(kstrid_emptyString),	// BKIND_CONST
							uint8_t(kstrid_emptyString),	// BKIND_ITRAMP
							uint8_t(kstrid_readonly),		// BKIND_GET
							uint8_t(kstrid_writeonly),		// BKIND_SET
							uint8_t(kstrid_readwrite)		// BKIND_GETSET
						};
						const KVPair props[] = {
							{ kstrid_declaredBy, strAtom(describeClassName(declaringTraits)) },
							{ kstrid_access, strAtom(str(StringId(bk2str[bk]))) },
							{ kstrid_type, strAtom(describeClassName(accessorType)) },
						};
						setpropmulti(v, props, elem_count(props));
						if (AvmCore::hasGetterBinding(binding))
							md1 = tm->getMethodMetadataPos(AvmCore::bindingToGetterId(binding), md1pool);
						if (AvmCore::hasSetterBinding(binding))
							md2 = tm->getMethodMetadataPos(AvmCore::bindingToSetterId(binding), md2pool);
						if (!accessors) accessors = new_array();
						pushobj(accessors, v);
						break;
					}
					case BKIND_NONE:
						break;
				}

				ArrayObject* vm = NULL;
				if ((flags & INCLUDE_METADATA) && (md1 || md2))
				{
					vm = new_array();
					addDescribeMetadata(vm, md1pool, md1);
					addDescribeMetadata(vm, md2pool, md2);
				}
				const KVPair props[] = {
					{ kstrid_name, strAtom(name) },
					{ kstrid_uri, strAtom(nsuri->length() == 0 ? NULL : nsuri) },
					{ kstrid_metadata, objAtom(vm) },
				};
				setpropmulti(v, props, elem_count(props));
			}
		}

		const KVPair props[] = {
			{ kstrid_bases, objAtom(bases) },
			{ kstrid_interfaces, objAtom(interfaces) },
			{ kstrid_metadata, objAtom(metadata) },
			{ kstrid_accessors, objAtom(accessors) },
			{ kstrid_methods, objAtom(methods) },
			{ kstrid_variables, objAtom(variables) },
			{ kstrid_constructor, objAtom(constructor) },
		};
		setpropmulti(o, props, elem_count(props));

		return o;
	}
Esempio n. 16
0
static char *
generate_sqlrow_insert( Dbptr db, char *(*createsync)(Dbptr db), long flags )
{
	void	*stk = 0;
	char	*table;
	Tbl	*fields;
	Dbvalue	dbvalue;
	char	*field;
	long	ifield;
	long	ftype;
	char	*fformat;
	char	part[STRSZ];
	char	*sync;
	char	*copy;

	if( db.record < 0 && 
	    db.record != dbSCRATCH && 
	    db.record != dbNULL ) {

		return NULL;
	}

	dbquery( db, dbTABLE_NAME, &table );
	dbquery( db, dbTABLE_FIELDS, &fields );

	pushstr( &stk, "INSERT INTO `" );
	pushstr( &stk, table );

	pushstr( &stk, "` VALUES(" );

	for( ifield = 0; ifield < maxtbl( fields ); ifield++ ) {
		
		field = gettbl( fields, ifield );

		db = dblookup( db, "", "", field, "" );

		dbquery( db, dbFIELD_TYPE, &ftype );
		dbquery( db, dbFIELD_FORMAT, &fformat );

		dbgetv( db, 0, field, &dbvalue, NULL );

		if( ifield > 0 ) {

			pushstr( &stk, ", " );
		}

		if( ! ( flags & DB2SQL_USE_DATASCOPE_NULLS ) && dbfield_isnull( db ) ) {

			pushstr( &stk, "NULL" );

			continue;
		}

		memset( part, '\0', STRSZ );

		switch( ftype ) {

		case dbSTRING:
			pushstr( &stk, "'" );
			if( strchr( dbvalue.s, '\'' ) == (char *) NULL ) {
				pushstr( &stk, dbvalue.s );
			} else {
				allot( char *, copy, 2 * strlen( dbvalue.s ) );
				strsub( dbvalue.s, "'", "\\'", copy );
				pushstr( &stk, copy );
				free( copy );
			}
			pushstr( &stk, "'" );
			break;

		case dbREAL:
		case dbTIME:
			sprintf( part, fformat, dbvalue.d );
			pushstr( &stk, part );
			break;

		case dbINTEGER:
		case dbYEARDAY:
			sprintf( part, fformat, dbvalue.i );
			pushstr( &stk, part );
			break;

		case dbDBPTR:
			sprintf( part, "%ld %ld %ld %ld", 
					dbvalue.db.database, 
					dbvalue.db.table, 
					dbvalue.db.field, 
					dbvalue.db.record );
			pushstr( &stk, part );
			break;
		}
	}

	if( ! ( flags & DB2SQL_OMIT_SYNC ) ) {

		if( createsync != (char *(*)(Dbptr db)) NULL ) {

			sync = (*createsync)( db );

		} else {

			sync = (char *) NULL;
		}

		pushstr( &stk, ", '" );

		if( sync != (char *) NULL ) {

			pushstr( &stk, sync );

		} else {

			pushstr( &stk, DB2SQL_SYNCFIELD_NULL );
		}

		pushstr( &stk, "'" );

		if( sync != (char *) NULL ) {

			free( sync );
		}
	}

	pushstr( &stk, ");\n" );

	return popstr( &stk, 1 );
}
Esempio n. 17
0
static char *
generate_sqltable_create( Dbptr db, long flags )
{
	char	*table;
	char	part[STRSZ];
	void	*stk = 0;
	Tbl	*primary;
	Tbl	*fields;
	long	ifield;
	char	*field;
	char	field_a[STRSZ];
	char	field_b[STRSZ];
	char	*fnull;
	long	fsize;
	long	ftype;
	char	*fformat;
	int	precision;
	int	scale;
	long	longest = 0;

	if( db.table < 0 ) {

		return NULL;
	}

	dbquery( db, dbTABLE_NAME, &table );

	pushstr( &stk, "CREATE TABLE `" );
	pushstr( &stk, table );
	pushstr( &stk, "`\n  (\n" );

	dbquery( db, dbTABLE_FIELDS, &fields );

	applytbl( fields, find_longest, (void *) &longest );

	if( ! ( flags & DB2SQL_OMIT_SYNC ) && longest < strlen( Db2sql_syncfield_name ) ) {

		longest = strlen( Db2sql_syncfield_name );
	}

	for( ifield = 0; ifield < maxtbl( fields ); ifield++ ) {

		if( ifield > 0 ) {

			pushstr( &stk, ",\n" );
		}

		field = gettbl( fields, ifield );

		db = dblookup( db, "", table, field, "" );

		pushstr( &stk, "  `" );
		pushstr( &stk, field );
		pushstr( &stk, "`" );
		pushstr( &stk, spaces( longest - strlen(field) + 2 ) );

		dbquery( db, dbFIELD_SIZE, &fsize );
		dbquery( db, dbFIELD_TYPE, &ftype );
		dbquery( db, dbFIELD_FORMAT, &fformat );
		dbquery( db, dbNULL, &fnull );

		memset( part, '\0', STRSZ );

		switch( ftype ) {

		case dbSTRING:
			if( fsize < 256 ) {
				sprintf( part, "CHAR(%ld)", fsize );
			} else {
				sprintf( part, "TEXT(%ld)", fsize );
			}
			pushstr( &stk, part );
			break;

		case dbREAL:
		case dbTIME:
			if( fnull != (char *) NULL && strcontains( fnull, "[eE]", 0, 0, 0 ) ) {
				sprintf( part, "DOUBLE" );
			} else {
				sscanf( fformat, "%%%d.%d", &precision, &scale );
				sprintf( part, "DECIMAL(%d,%d)", precision, scale );
			}
			pushstr( &stk, part );
			break;

		case dbINTEGER:
		case dbYEARDAY:
			sprintf( part, "INTEGER(%ld)", fsize );
			pushstr( &stk, part );
			break;

		case dbDBPTR:
			pushstr( &stk, "CHAR(32)" );
			break;
		}

		/* Disallow defaults for SQL TEXT fields */

		if( fnull != (char *) NULL && strncmp( part, "TEXT", 4 ) ) {

			pushstr( &stk, " DEFAULT " );

			if( ftype == dbSTRING ) {
		
				pushstr( &stk, "'" );
				pushstr( &stk, fnull );
				pushstr( &stk, "'" );

			} else {

				pushstr( &stk, fnull );
			}
		}
	}

	if( ! ( flags & DB2SQL_OMIT_SYNC ) ) {

		pushstr( &stk, ",\n" );

		pushstr( &stk, "  `" );
		pushstr( &stk, Db2sql_syncfield_name );
		pushstr( &stk, "`" );
		pushstr( &stk, spaces( longest - strlen(Db2sql_syncfield_name) + 2 ) );

		pushstr( &stk, DB2SQL_SYNCFIELD_SPEC );
	}

	dbquery( db, dbPRIMARY_KEY, &primary );

	if( maxtbl( primary ) > 0 ) {

		pushstr( &stk, ",\n  PRIMARY KEY (" );

		for( ifield = 0; ifield < maxtbl( primary ); ifield++ ) {

			if( ifield > 0 ) {

				pushstr( &stk, ", " );
			}
	
			field = gettbl( primary, ifield );

			if( strcontains( field, "::", 0, 0, 0 ) ) {

				strsub( field, "::", "  ", field );

				sscanf( field, "%s %s", field_a, field_b );

				pushstr( &stk, "`" );
				pushstr( &stk, field_a );
				pushstr( &stk, "`" );

				/* end-of-range keys can be NULL in Datascope,
				   so don't use it as primary in SQL: 

				pushstr( &stk, ", `" );
				pushstr( &stk, field_b );
				pushstr( &stk, "`" );
				*/

			} else {

				pushstr( &stk, "`" );
				pushstr( &stk, field );
				pushstr( &stk, "`" );
			}
		}

		pushstr( &stk, ")" );
	}

	pushstr( &stk, "\n  );\n" );

	return popstr( &stk, 1 );
}
Esempio n. 18
0
/* this function handles only `%d', `%c', %f, %p, and `%s' formats
   and now it handles %x and %X as well */
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp)
{
#if LUAXS_CORE_FORMAT == 2
    size_t alloclen = _vscprintf(fmt, argp) + 1;
    char* buf = luaM_malloc(L, alloclen);
    vsprintf_s(buf, alloclen, fmt, argp);
    pushstr(L, buf);
    luaM_freemem(L, buf, alloclen);
    return svalue(L->top - 1);
#else
    int n = 1;
    pushstr(L, "");
    for (;;) {
        const char *e = strchr(fmt, '%');
        if (e == NULL) break;
        setsvalue2s(L, L-&gt;top, luaS_newlstr(L, fmt, e-fmt));
        incr_top(L);
        switch (*(e+1)) {
        case 's': {
            const char *s = va_arg(argp, char *);
            if (s == NULL) s = "(null)";
            pushstr(L, s);
            break;
            }
        case 'c': {
            char buff[2];
            buff[0] = cast(char, va_arg(argp, int));
            buff[1] = '\0';
            pushstr(L, buff);
            break;
            }
        case 'd': {
            setnvalue(L-&gt;top, cast_num(va_arg(argp, int)));
            incr_top(L);
            break;
            }
        case 'f': {
            setnvalue(L-&gt;top, cast_num(va_arg(argp, l_uacNumber)));
            incr_top(L);
            break;
            }
        case 'p': {
            char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
            sprintf(buff, "%p", va_arg(argp, void *));
            pushstr(L, buff);
            break;
            }
#if LUAXS_CORE_FORMAT == 1
        case 'X': {
            char buff[sizeof(int)*2+3];
            sprintf(buff, "%X", va_arg(argp, int));
            pushstr(L, buff);
            break;
            }
        case 'x': {
            char buff[sizeof(int)*2+3];
            sprintf(buff, "%x", va_arg(argp, int));
            pushstr(L, buff);
            break;
            }
#endif
        case '%': {
            pushstr(L, "%");
            break;
            }
        default: {
            char buff[3];
            buff[0] = '%';
            buff[1] = *(e+1);
            buff[2] = '\0';
            pushstr(L, buff);
            break;
            }
        }
        n += 2;
        fmt = e+2;
    }
    pushstr(L, fmt);
    luaV_concat(L, n+1, cast_int(L-&gt;top - L-&gt;base) - 1);
    L-&gt;top -= n;
    return svalue(L-&gt;top - 1);
#endif
}