bool handle_possible_undefine(char* words[])
{
    if (strcmp(words[0], "#undef") == 0)
    {
        undefine(words[1]);
        return true;
    }

    return false;
}
Ejemplo n.º 2
0
int
main(void) {

    g_client.connect();

    srand(time(0));
    for(int i = 0; i<1000; i++) {
        int sz = g_procs.size();
        int test = rand() % 100;
        if(sz == 0 || test < 10) {
            define();
            continue;
        }

        list();

        int proc = rand() % g_procs.size();
        SimpleCpcClient::Process & p = g_procs[proc];
        if(p.m_status == "running" && test > 50) {
            ndbout_c("undefine %d: %s (running)", p.m_id, p.m_name.c_str());
            undefine(p);
            g_procs.erase(proc);
            continue;
        }
        if(p.m_status == "running" && test <= 50) {
            ndbout_c("stop %d: %s(running)", p.m_id, p.m_name.c_str());
            stop(p);
            continue;
        }
        if(p.m_status == "stopped" && test > 50) {
            ndbout_c("undefine %d: %s(stopped)", p.m_id, p.m_name.c_str());
            undefine(p);
            g_procs.erase(proc);
            continue;
        }
        if(p.m_status == "stopped" && test <= 50) {
            ndbout_c("start %d %s(stopped)", p.m_id, p.m_name.c_str());
            start(p);
            continue;
        }
        ndbout_c("Unknown: %s", p.m_status.c_str());
    }
}
Ejemplo n.º 3
0
void init_dictionary(scope v){
     unsigned int i;
     keyword_T = newtype(NULL,NULL,TRUE);
     interntype(keyword_T);
     type__T = newtype(NULL,NULL,TRUE);
     interntype(type__T);
     #define f(name,var) {					\
	  node sym = newsymbol(var##_S = UniqueString(name),	\
	       keyword_T,v,					\
	       intern_F|keyword_F|defined_F);		\
	  var##_K = sym;					\
	  }
     #define g(var) f(#var,var)
     #include "keywords.h"
     keyword_T->body.type.name = keyword__K;
     type__K->body.symbol.value = type__T;
     type__T->body.type.name = type__K;
     init_chk();
     int_T = basictype(int_K);
     one__K->body.symbol.type = int_T;
     zero__K->body.symbol.type = int_T;
     one__K->body.symbol.Cname = "1";
     zero__K->body.symbol.Cname = "0";
     char_T = basictype(char_K);
     double_T = basictype(double_K);
     package_T = basictype(package_K);
     bool_T = basictype(bool_K);
     bool_T->body.type.Cname = "char";
     true_K->body.symbol.type = bool_T;
     true_K->body.symbol.Cname = "1";
     false_K->body.symbol.type = bool_T;
     false_K->body.symbol.Cname = "0";
     void_T = basictype(void_K);
     returns_T = basictype(returns_K);
     exits_T = basictype(exits_K);
     _returnedThing_K->body.symbol.type = returns_T;
     _returnedThing_K->body.symbol.flags &= ~keyword_F;
     bad_or_undefined_T = basictype(undefined__K);
     deferred__T = basictype(deferred__K); /* the type of a symbol whose type is not known yet */
     undefine(deferred__K);
     symbol_T = basictype(symbol__K);
     null_T = basictype(null_K);
     null_T->body.type.Cname = "void *";
     exits_T->body.type.Cname = "void";
     bad__K->body.symbol.type = bad_or_undefined_T;
     double_T->body.type.flags |= arithmetic_type_F;
     int_T->body.type.flags |= arithmetic_type_F;
     char_T->body.type.flags |= arithmetic_type_F;
     int_T->body.type.flags |= integer_type_F;
     char_T->body.type.flags |= integer_type_F;
     for (i=0; i<numberof(Csymbols); i++) uniquify(Csymbols[i]);
     for (i=0; i<numberof(CXXkeywords); i++) uniquifyCXX(CXXkeywords[i]);
     }
Ejemplo n.º 4
0
void
mgclearall(void)			/* clear all variable settings */
{
	int  j;
	register IVAR  *iv;
	register int  i;

	for (iv = ivhead; iv != NULL; iv = iv->next) {
		dremove(iv->name);
		freestr(iv->name);
		freestr(iv->dfn);
		efree((char *)iv);
	}
	ivhead = NULL;

	for (i = 0; i < NVARS; i++)
		if (gparam[i].flags & DEFINED)
			undefine(&gparam[i]);
	
	for (j = 0; j < MAXCUR; j++)
		for (i = 0; i < NCVARS; i++)
			if (cparam[j][i].flags & DEFINED)
				undefine(&cparam[j][i]);
}
Ejemplo n.º 5
0
void    un_predefine(
    int clearall                            /* TRUE for -N option   */
)
/*
 * Remove predefined symbols from the symbol table.
 */
{
    PRESET *    pp;
    DEFBUF *    defp;
    int     n = sizeof preset / sizeof (PRESET);

    for (pp = preset; pp < preset + n; pp++) {
        if (pp->name) {
            if (*(pp->name) && (defp = look_id( pp->name)) != NULL
                    && defp->nargs >= DEF_NOARGS_PREDEF)
                undefine( pp->name);
        } else if (clearall == FALSE) {             /* -S<n> option */
            break;
        }
    }
}
Ejemplo n.º 6
0
static u_i4 
pp_directive( FILE *input, STACK_FRAME *stack , bool ifdef )
{
	i4 n, len;
	char *words[ MAX_LINE / 2 ], *temp, *p;
	char *parse_val, *parse_end;
	STACK_FRAME stack_frame;
	bool def_dir;
	u_i4 rtn = NONE;

	stack_frame.prev = stack;
	stack_frame.display_mode = TRUE;

	p = temp = STalloc( infile->yytext );
	CMnext( p );
	n = sizeof( words );
	STgetwords( p, &n, words ); 

	/* process the directive, watch out for the empty directive */
	if (n == 0)
	{
		;       /* empty directive */
	}
	else if( STequal( words[ 0 ], ERx( "define" ) ) != 0 )
	{
		/* If a symbol was specified look for the value to give it */
		if (n > 1) 
		{
			/* Scan for the 'define' keyword */
			parse_val = infile->yytext;
			CMnext(parse_val);         /* Skip over the '#' */
			while (CMwhite(parse_val))
				CMnext(parse_val);

			/* Scan past the 'define' keyword */
			while (!CMwhite(parse_val))
				CMnext(parse_val);

			/* Scan over white space separating 'define' */
			/* keyword and the specified symbol name */
			while (CMwhite(parse_val))
				CMnext(parse_val);

			/* Skip over the symbol name */
			while (!CMwhite(parse_val))
				CMnext(parse_val);

			/* Skip over white space after the symbol name */
			while (CMwhite(parse_val))
				CMnext(parse_val);

			/* At this point we have scanned to the beginning */
			/* of the defined value for the symbol, Trim off */
			/* any trailing white space */
			STtrmwhite(parse_val);

			/* Define value found, could be the empty string, "" */
			if( active( &stack_frame ) )
			{
				define( words[ 1 ], parse_val, FALSE );
			}
		}
		rtn = DEFINE;
	}

	else if( active( &stack_frame ) &&
		STequal( words[ 0 ], ERx( "undef" ) ) != 0 )
	{
		if (n > 1)
		{
			undefine( words[ 1 ] );
		}
		rtn = UNDEF;
	}

	else if( STequal( words[ 0 ], ERx( "if" ) ) != 0 )
	{
		/* If an expression was specified look for its evaluation */
		if (n > 1) 
		{
			/* Scan for the 'if' keyword */
			parse_val = infile->yytext;
			CMnext(parse_val);         /* Skip over the '#' */
			while (CMwhite(parse_val))
				CMnext(parse_val);

			/* Scan past the 'if' keyword */
			while (!CMwhite(parse_val))
				CMnext(parse_val);

			/* Scan over white space separating 'if' */
			/* keyword and the expression */
			while (CMwhite(parse_val))
				CMnext(parse_val);

			/* At this point we have scanned to the beginning */
			/* of the expression.*/
			if( active( &stack_frame ) )
			{
				/* Evaluate boolean expression found */
				stack_frame.display_mode = 
					pp_eval_boolexp( &parse_val, ERx("") );
			}
			(void) pp_input( input, &stack_frame, TRUE );
		}
		rtn = IF;
	}

	else if( STequal( words[ 0 ], ERx( "ifdef" ) ) != 0 )
	{
		if( active(&stack_frame) && is_defined(words[1], NULL) == OK)
		{
			stack_frame.display_mode = TRUE;
		}
		else
			stack_frame.display_mode = FALSE;
		(void) pp_input( input, &stack_frame, TRUE );
		rtn = IFDEF;
	}

	else if( STequal( words[ 0 ], ERx( "ifndef" ) ) != 0 )
	{
		if( active(&stack_frame) && is_defined(words[1], NULL) != OK)
		{
			stack_frame.display_mode = TRUE;
		}
		else
			stack_frame.display_mode = FALSE;
		(void) pp_input( input, &stack_frame, TRUE );
		rtn = IFNDEF;
	}

	else if( STequal( words[ 0 ], ERx( "else" ) ) != 0 )
	{
		if( !ifdef )
		{
			SIfprintf( stderr, E_YAPP007 );
			yydump();
		}
		stack_frame.prev->display_mode =
			( stack_frame.prev->display_mode == TRUE ) ?
			FALSE : TRUE;
		rtn = ELSE;
	}

	else if( STequal( words[ 0 ], ERx( "endif" ) ) != 0 )
	{
		rtn = ENDIF;
	}

	else if( STequal( words[ 0 ], ERx( "include" ) ) != 0 )
	{
		/* Look for the include filename */
		if (n > 1) 
		{
			/* Scan for the 'include' keyword */
			parse_val = infile->yytext;
			CMnext(parse_val);         /* Skip over the '#' */
			while (CMwhite(parse_val))
				CMnext(parse_val);

			/* Scan past the include keyword */
			while (!CMwhite(parse_val))
				CMnext(parse_val);

			/* Scan over white space separating 'include' */
			/* keyword and the specified filename */
			while (CMwhite(parse_val))
				CMnext(parse_val);

			/* At this point were expecting "file" or <file> */
			/* remember the character which ends the filename */
			def_dir = TRUE;
			if (CMcmpcase(parse_val, ERx("\"")) == 0)
			{
				parse_end = ERx("\"");
			}
			else if (CMcmpcase(parse_val, ERx("<")) == 0)
			{
				parse_end = ERx(">");
				def_dir = FALSE;
			}
			else
			{
				parse_end = ERx("");
			}

			/* Save the include file name in the temp string. */
			/* Note, this overwrites the parsed words of the  */
			/* record since but these are no longer needed.   */
			p = temp;
			CMnext(parse_val);
			while (*parse_val != EOS)
			{
				if (CMcmpcase(parse_val, parse_end) == 0) 
				{
					/* Terminate the file name and call */
					/* pp_file to process the file. */
					STcopy(ERx(""), p);
					pp_file(stack, temp, def_dir);
					break;
				}
				CMcpychar(parse_val, p);
				CMnext(parse_val);
				CMnext(p);
			}
		}
		rtn = DEFINE;
	}

	/* display everthing but legal directives */ 
	if (rtn == NONE && active( &stack_frame ) )
		yyputline( infile->yytext );

	MEfree( temp );
	return( rtn );
}
Ejemplo n.º 7
0
ATF_TC_BODY(symtab_grow, tc) {
	isc_result_t result;
	isc_symtab_t *st = NULL;
	isc_symvalue_t value;
	isc_symexists_t policy = isc_symexists_reject;
	int i;

	UNUSED(tc);

	result = isc_test_begin(NULL, ISC_TRUE);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_symtab_create(mctx, 3, undefine, NULL, ISC_FALSE, &st);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	ATF_REQUIRE(st != NULL);

	/* Nothing should be in the table yet */

	/*
	 * Put 1024 entries in the table (this should necessate
	 * regrowing the hash table several times
	 */
	for (i = 0; i < 1024; i++) {
		char str[16], *key;

		snprintf(str, sizeof(str), "%04x", i);
		key = isc_mem_strdup(mctx, str);
		ATF_REQUIRE(key != NULL);
		value.as_pointer = isc_mem_strdup(mctx, str);
		ATF_REQUIRE(value.as_pointer != NULL);
		result = isc_symtab_define(st, key, 1, value, policy);
		ATF_CHECK_EQ(result, ISC_R_SUCCESS);
		if (result != ISC_R_SUCCESS)
			undefine(key, 1, value, NULL);
	}

	/*
	 * Try to put them in again; this should fail
	 */
	for (i = 0; i < 1024; i++) {
		char str[16], *key;

		snprintf(str, sizeof(str), "%04x", i);
		key = isc_mem_strdup(mctx, str);
		ATF_REQUIRE(key != NULL);
		value.as_pointer = isc_mem_strdup(mctx, str);
		ATF_REQUIRE(value.as_pointer != NULL);
		result = isc_symtab_define(st, key, 1, value, policy);
		ATF_CHECK_EQ(result, ISC_R_EXISTS);
		undefine(key, 1, value, NULL);
	}

	/*
	 * Retrieve them; this should succeed
	 */
	for (i = 0; i < 1024; i++) {
		char str[16];

		snprintf(str, sizeof(str), "%04x", i);
		result = isc_symtab_lookup(st, str, 0, &value);
		ATF_CHECK_EQ(result, ISC_R_SUCCESS);
		ATF_CHECK_STREQ(str, value.as_pointer);
	}

	/*
	 * Undefine them
	 */
	for (i = 0; i < 1024; i++) {
		char str[16];

		snprintf(str, sizeof(str), "%04x", i);
		result = isc_symtab_undefine(st, str, 1);
		ATF_CHECK_EQ(result, ISC_R_SUCCESS);
	}

	/*
	 * Retrieve them again; this should fail
	 */
	for (i = 0; i < 1024; i++) {
		char str[16];

		snprintf(str, sizeof(str), "%04x", i);
		result = isc_symtab_lookup(st, str, 0, &value);
		ATF_CHECK_EQ(result, ISC_R_NOTFOUND);
	}

	isc_symtab_destroy(&st);
	isc_test_end();
}
Ejemplo n.º 8
0
void unwind(node *symbols){
     while (*symbols != NULL) {
          undefine(car(*symbols));
          *symbols = cdr(*symbols);
     }
}
Ejemplo n.º 9
0
int find_includes (struct filepointer *filep, struct inclist *file,
                   struct inclist *file_red, int recursion, bool failOK)
{
    register char *line;
    register int type;
    bool recfailOK;

    while ((line = getline (filep)))
    {
        switch (type = deftype (line, filep, file_red, file, true))
        {
        case IF:
doif:
            type = find_includes (filep, file,
                                  file_red, recursion + 1, failOK);
            while ((type == ELIF) || (type == ELIFFALSE) ||
                    (type == ELIFGUESSFALSE))
                type = gobble (filep, file, file_red);
            if (type == ELSE)
                gobble (filep, file, file_red);
            break;
        case IFFALSE:
        case IFGUESSFALSE:
doiffalse:
            if (type == IFGUESSFALSE || type == ELIFGUESSFALSE)
                recfailOK = true;
            else
                recfailOK = failOK;
            type = gobble (filep, file, file_red);
            if (type == ELSE)
                find_includes (filep, file,
                               file_red, recursion + 1, recfailOK);
            else if (type == ELIF)
                goto doif;
            else if ((type == ELIFFALSE) || (type == ELIFGUESSFALSE))
                goto doiffalse;
            break;
        case IFDEF:
        case IFNDEF:
            if ((type == IFDEF && isdefined (line, file_red, NULL))
                    || (type == IFNDEF && !isdefined (line, file_red, NULL)))
            {
                debug (1, (type == IFNDEF ?
                           "line %d: %s !def'd in %s via %s%s\n" : "",
                           filep->f_line, line,
                           file->i_file, file_red->i_file, ": doit"));
                type = find_includes (filep, file,
                                      file_red, recursion + 1, failOK);
                while (type == ELIF || type == ELIFFALSE || type == ELIFGUESSFALSE)
                    type = gobble (filep, file, file_red);
                if (type == ELSE)
                    gobble (filep, file, file_red);
            }
            else
            {
                debug (1, (type == IFDEF ?
                           "line %d: %s !def'd in %s via %s%s\n" : "",
                           filep->f_line, line,
                           file->i_file, file_red->i_file, ": gobble"));
                type = gobble (filep, file, file_red);
                if (type == ELSE)
                    find_includes (filep, file,
                                   file_red, recursion + 1, failOK);
                else if (type == ELIF)
                    goto doif;
                else if (type == ELIFFALSE || type == ELIFGUESSFALSE)
                    goto doiffalse;
            }
            break;
        case ELSE:
        case ELIFFALSE:
        case ELIFGUESSFALSE:
        case ELIF:
            if (!recursion)
                gobble (filep, file, file_red);
        case ENDIF:
            if (recursion)
                return (type);
        case DEFINE:
            define (line, file);
            break;
        case UNDEF:
            if (!*line)
            {
                warning ("%s, line %d: incomplete undef == \"%s\"\n",
                         file->i_file, filep->f_line, line);
                display_included (file_red, file);
                break;
            }
            undefine (line, file_red);
            break;
        case INCLUDE:
            add_include (filep, file, file_red, line, false, failOK);
            break;
        case INCLUDEDOT:
            add_include (filep, file, file_red, line, true, failOK);
            break;
        case ERROR:
        case WARNING:
            warning ("%s: %d: %s\n", file->i_file, filep->f_line, line);
            display_included (file_red, file);
            break;

        case PRAGMA:
        case IDENT:
        case SCCS:
        case EJECT:
            break;
        case -1:
            warning ("%s", file_red->i_file);
            if (file_red != file)
                warning1 (" (reading %s)", file->i_file);
            warning1 (", line %d: unknown directive == \"%s\"\n",
                      filep->f_line, line);
            break;
        case -2:
            warning ("%s", file_red->i_file);
            if (file_red != file)
                warning1 (" (reading %s)", file->i_file);
            warning1 (", line %d: incomplete include == \"%s\"\n",
                      filep->f_line, line);
            break;
        }
    }
    file->i_flags |= FINISHED;
    return (-1);
}
Ejemplo n.º 10
0
int cli(FILE* mainio)
{
	char buf[1024];
	List<Word> *inputData;
	List<Data> memory;
	Tree *my;
	int i = 0;
	while (strcmp(buf,"q\n")){
		my = new Tree;
		
		fprintf(stdout,"#");
		fgets(buf,1023,mainio);
		if (!strcmp(buf,"q\n"))
			continue;
		if (!strncmp(buf,"h\n",2)){
			printHelp(stdout);
			continue;
		}
		if (!strncmp(buf,"mem",3) || buf[4] == '\n' || buf[4] == ' '){
			memOut(stdout,memory);
			continue;
		}
		if (!strncmp(buf,"undef",5)){
			if (buf[5]==' '){
				undefine(buf+5,memory);
				continue;
			}
		}
		if (!strncmp(buf,"undefine",8)){
			if (buf[8]==' '){
				undefine(buf+8,memory);
				continue;
			}
		}
		if (!strncmp(buf,"delete",6)){
			if (buf[6]==' '){
				undefine(buf+6,memory);
				continue;
			}
		}
		if (!strncmp(buf,"del",3)){
			if (buf[3]==' '){
				undefine(buf+3,memory);
				continue;
			}
		}
		if (buf[0]=='\n'){
			fprintf(stdout,"Empty. Type \"h\" for help.\n");
			continue;
		}
		//TODO: Implement all CLI commands
		inputData = stringParse(buf);
		improveInput(*inputData);

		my->AltBuild(inputData->tail);
		Node *p = my->root;
		std::cout << my->getTreeCharArr() << std::endl;
		Number rez;
		rez=my->EvaluteTree(memory);
		char out[100]("") ;
		strcpy(out, rez.getNumberString());
		std::cout  << out<<std::endl;
		delete inputData;
	}
	return 0;
}
Ejemplo n.º 11
0
void
setmgvar(		/* set a variable */
char  *fname,
FILE  *fp,
char  *string
)
{
	char  name[128];
	FILE  *fp2;
	register int  i;
	register char  *s;
	register VARIABLE  *vp;

	if (!strncmp(string, "include=", 8)) {	/* include file */
		if ((s = findfile(string+8, libpath)) == NULL) {
			fprintf(stderr, "%s\n", string);
			fprintf(stderr, "%s: %s: File not found: %s\n",
					progname, fname, string+8);
			quit(1);
		}
		strcpy(name, s);
		mgload(name);
		return;
	}
	s = string;
	i = 0;
	while (i < sizeof(name)-1 && isid(*s))
		name[i++] = *s++;
	name[i] = '\0';
	vp = vlookup(name);
	if (vp != NULL) {
		undefine(vp);
		switch (vp->type) {
		case REAL:
		case FUNCTION:
			if ((*s == '(') != (vp->type == FUNCTION)) {
				fprintf(stderr, "%s\n", string);
				fprintf(stderr,
					"%s: %s: Bad %s declaration: %s\n",
					progname, fname,
					vp->type == FUNCTION ?
					"function" : "variable",
					name);
				quit(1);
			}
			scompile(string, fname, 0);
			vp->v.dfn = savestr(string);
			break;
		case STRING:
			if (*s++ != '=') {
				fprintf(stderr, "%s\n", string);
				fprintf(stderr, "%s: %s: Missing '='\n",
						progname, fname);
				quit(1);
			}
			vp->v.s = savestr(s);
			break;
		case DATA:
			if (*s++ != '=') {
				fprintf(stderr, "%s\n", string);
				fprintf(stderr, "%s: %s: Missing '='\n",
						progname, fname);
				quit(1);
			}
			if (!*s) {
				loaddata(fname, fp, &vp->v.d);
			} else if (*s == '!') {
				if ((fp2 = popen(s+1, "r")) == NULL) {
					fprintf(stderr, "%s\n", string);
					fprintf(stderr,
					"%s: %s: Cannot execute: %s\n",
							progname, fname, s+1);
					quit(1);
				}
				loaddata(s, fp2, &vp->v.d);
				pclose(fp2);
			} else {
				if ((fp2 = fopen(s, "r")) == NULL) {
				    fprintf(stderr, "%s\n", string);
				    fprintf(stderr,
					    "%s: %s: Data file not found: %s\n",
					    progname, fname, s);
				    quit(1);
				}
				loaddata(s, fp2, &vp->v.d);
				fclose(fp2);
			}
			break;
		}
		vp->flags |= DEFINED;
	} else
		setivar(name, fname, string);		/* intermediate */
}
Ejemplo n.º 12
0
int
find_includes(struct filepointer *filep, struct inclist *file,
	      struct inclist *file_red, int recursion, boolean failOK)
{
	struct inclist	*inclistp;
	char		**includedirsp;
	register char	*line;
	register int	type;
	boolean recfailOK;

	while ((line = getnextline(filep))) {
		switch(type = deftype(line, filep, file_red, file, TRUE)) {
		case IF:
		doif:
			type = find_includes(filep, file,
				file_red, recursion+1, failOK);
			while ((type == ELIF) || (type == ELIFFALSE) ||
			       (type == ELIFGUESSFALSE))
				type = gobble(filep, file, file_red);
			if (type == ELSE)
				gobble(filep, file, file_red);
			break;
		case IFFALSE:
		case IFGUESSFALSE:
		    doiffalse:
			if (type == IFGUESSFALSE || type == ELIFGUESSFALSE)
			    recfailOK = TRUE;
			else
			    recfailOK = failOK;
			type = gobble(filep, file, file_red);
			if (type == ELSE)
			    find_includes(filep, file,
					  file_red, recursion+1, recfailOK);
			else
			if (type == ELIF)
			    goto doif;
			else
			if ((type == ELIFFALSE) || (type == ELIFGUESSFALSE))
			    goto doiffalse;
			break;
		case IFDEF:
		case IFNDEF:
		    {
			int isdef = (isdefined(line, file_red, NULL) != NULL);
			if (type == IFNDEF) isdef = !isdef;

			if (isdef) {
				debug(1,(type == IFNDEF ?
				    "line %d: %s !def'd in %s via %s%s\n" : "",
				    filep->f_line, line,
				    file->i_file, file_red->i_file, ": doit"));
				type = find_includes(filep, file,
					file_red, recursion+1, failOK);
				while (type == ELIF || type == ELIFFALSE || type == ELIFGUESSFALSE)
					type = gobble(filep, file, file_red);
				if (type == ELSE)
					gobble(filep, file, file_red);
			}
			else {
				debug(1,(type == IFDEF ?
				    "line %d: %s !def'd in %s via %s%s\n" : "",
				    filep->f_line, line,
				    file->i_file, file_red->i_file, ": gobble"));
				type = gobble(filep, file, file_red);
				if (type == ELSE)
					find_includes(filep, file,
						file_red, recursion+1, failOK);
				else if (type == ELIF)
					goto doif;
				else if (type == ELIFFALSE || type == ELIFGUESSFALSE)
					goto doiffalse;
			}
		    }
		    break;
		case ELSE:
		case ELIFFALSE:
		case ELIFGUESSFALSE:
		case ELIF:
			if (!recursion)
				gobble(filep, file, file_red);
		case ENDIF:
			if (recursion)
				return(type);
		case DEFINE:
			define(line, file);
			break;
		case UNDEF:
			if (!*line) {
			    warning("%s", file_red->i_file);
			    if (file_red != file)
				warning1(" (reading %s)", file->i_file);
			    warning1(", line %d: incomplete undef == \"%s\"\n",
				filep->f_line, line);
			    break;
			}
			undefine(line, file_red);
			break;
		case INCLUDE:
		case INCLUDEDOT:
		case INCLUDENEXT:
		case INCLUDENEXTDOT:
			inclistp = inclistnext;
			includedirsp = includedirsnext;
			debug(2,("%s, reading %s, includes %s\n",
				file_red->i_file, file->i_file, line));
			add_include(filep, file, file_red, line, type, failOK);
			inclistnext = inclistp;
			includedirsnext = includedirsp;
			break;
		case ERROR:
		case WARNING:
			warning("%s", file_red->i_file);
			if (file_red != file)
				warning1(" (reading %s)", file->i_file);
			warning1(", line %d: %s\n",
				 filep->f_line, line);
			break;

		case PRAGMA:
		case IDENT:
		case SCCS:
		case EJECT:
			break;
		case -1:
			warning("%s", file_red->i_file);
			if (file_red != file)
			    warning1(" (reading %s)", file->i_file);
			warning1(", line %d: unknown directive == \"%s\"\n",
				 filep->f_line, line);
			break;
		case -2:
			warning("%s", file_red->i_file);
			if (file_red != file)
			    warning1(" (reading %s)", file->i_file);
			warning1(", line %d: incomplete include == \"%s\"\n",
				 filep->f_line, line);
			break;
		}
	}
	file->i_flags |= FINISHED;
	debug(2,("finished with %s\n", file->i_file));
	return(-1);
}
Ejemplo n.º 13
0
int main(int argc, char **argv) {
#define FIND_LONG_OPTION(ARRAY) \
	{															\
		for(i = 0; i < sizeof (ARRAY) / sizeof(struct option); i++) {							\
			struct option *o = (ARRAY) + i;										\
			if(o->arg < 2) {											\
				if(strcmp(arg, o->opt) == 0) {									\
					const char *a = o->arg ? *++v : argv[0];						\
					if(o->arg && !a) {									\
						fprintf(stderr, "%s: option '%s' need an argument\n", argv[0], *v);		\
						return 1;									\
					}											\
					if(o->act) o->act(a);									\
					goto first_loop;									\
				}												\
			} else {												\
				size_t len = strlen(o->opt);									\
				if(strncmp(arg, o->opt, len) == 0) {								\
					if(arg[len] && arg[len] != '=') continue;						\
					if(!arg[len] || !arg[len + 1]) {							\
						fprintf(stderr, "%s: option '%s' need an argument\n", argv[0], *v);		\
						return 1;									\
					}											\
					const char *a = arg + len + 1;								\
					if(o->act) o->act(a);									\
					goto first_loop;									\
				}												\
			}													\
		}														\
	}

#define UNRECOGNIZED_OPTION(O) \
	do {									\
		fprintf(stderr, "%s: error: unrecognized option '%s'\n",	\
			argv[0], (O));						\
		return 1;							\
	} while(0)


	int verbose = 0;
	int no_link = 0;
	int preprocess_only = -1;
	int no_warning = -1;
	int end_of_options = 0;
	const char *output_file = NULL;
	char **v = argv;
	init_argv();

	const char *vs_path = getenv("VS_PATH");
	if(!vs_path) vs_path = getenv("VSINSTALLDIR");
	if(!getenv("INCLUDE")) {
		if(vs_path) {
			size_t len = strlen(vs_path);
			if(vs_path[len - 1] == '/' || vs_path[len - 1] == '\\') len--; 
			char buffer[len + 12 + len + 24 + 1];
			memcpy(buffer, vs_path, len);
			memcpy(buffer + len, "/VC/include;", 12);
			memcpy(buffer + len + 12, vs_path, len);
			strcpy(buffer + len + 12 + len, "/VC/PlatformSDK/include;");
			setenv("INCLUDE", buffer, 0);
		} else {
			no_warning = find_argv(argv, "-w");
			if(!no_warning) fprintf(stderr, "%s: warning: no system include path set\n", argv[0]);
		}
	}
	if(!getenv("LIB")) {
		if(vs_path) {
			size_t len = strlen(vs_path);
			if(vs_path[len - 1] == '/' || vs_path[len - 1] == '\\') len--; 
			char buffer[len + 8 + len + 20 + 1];
			memcpy(buffer, vs_path, len);
			memcpy(buffer + len, "/VC/lib;", 8);
			memcpy(buffer + len + 12, vs_path, len);
			strcpy(buffer + len + 12 + len, "/VC/PlatformSDK/lib;");
			setenv("LIB", buffer, 0);
		} else {
			if(no_warning == -1) no_warning = find_argv(argv, "-w");
			if(!no_warning) fprintf(stderr, "%s: warning: no system library path set\n", argv[0]);
		}
	}

first_loop:
	while(*++v) {
		if(!end_of_options && **v == '-') {
			int i;
			if((*v)[1] == '-') {
				const char *arg = *v + 2;
				if(!*arg) {
					end_of_options = 1;
					continue;
				}
				FIND_LONG_OPTION(double_dash_long_options);
				if(strcmp(arg, "verbose") == 0) {
					verbose = 1;
				} else UNRECOGNIZED_OPTION(*v);
			} else {
				const char *arg = *v + 1;
				FIND_LONG_OPTION(singal_dash_long_options);
				switch(*arg) {
					case 0:
						goto not_an_option;
					case 'c':
						if(arg[1]) UNRECOGNIZED_OPTION(*v);
						add_to_argv("-c");
						no_link = 1;
						break;
					case 'D':
						if(arg[1]) add_to_argv(*v);
						else {
							const char *d = *++v;
							if(!d) {
								fprintf(stderr, "%s: error: macro name missing after '-D'\n",
									argv[0]);
								return 1;
							}
							define(d);
						}
						break;
					case 'E':
						if(arg[1]) UNRECOGNIZED_OPTION(*v);
						add_to_argv("-E");
						preprocess_only = 1;						
						break;
					case 'f':
						if(arg[1]) set_feature(arg + 1);
						else {
							const char *feature = *++v;
							if(!feature) {
								fprintf(stderr, "%s: error: option '-f' need an argument\n",
									argv[0]);
								return -1;
							}
							set_feature(feature);
						}
						break;
					case 'g':
						// -g[coff][<level>] (level: 0~3)
						if(arg[1]) {
							const char *level = arg + 1;
							if(strncmp(level, "coff", 4) == 0) level += 4;
							if(*level && *level != '-') {
								int i = 0;
								do {
									if(!isdigit(level[i])) {
										fprintf(stderr, "%s: error: unrecognized debug output level \"%s\"\n",
											argv[0], level);
										return 1;
									}
								} while(level[++i]);
								/*
								if(i > 1 || *level > '3') {
									fprintf(stderr, "%s: error: debug output level %s is too high\n",
										argv[0], level);
									return 1;
								}
								if(*level == '0') break;
								*/
								int l = atoi(level);
								if(!l) break;
								if(l > 3) {
									fprintf(stderr, "%s: error: debug output level %s is too high\n",
										argv[0], level);
									return 1;
								}
							}
						}
						add_to_argv("-Zi");
						break;
					case 'I':
						if(no_warning == -1) no_warning = find_argv(argv, "-w");
						//if(arg[1]) add_to_argv(*v);
						if(arg[1]) add_include_path(arg + 1, no_warning);
						else {
							const char *path = *++v;
							if(!path) {
								fprintf(stderr, "%s: error: option '-I' need an argument\n",
									argv[0]);
								return 1;
							}
							add_include_path(path, no_warning);
						}
						break;
					case 'L':
						if(no_warning == -1) no_warning = find_argv(argv, "-w");
						if(arg[1]) add_library_path(arg + 1, no_warning);
						else {
							const char *path = *++v;
							if(!path) {
								fprintf(stderr, "%s: error: option '-L' need an argument\n",
									argv[0]);
								return 1;
							}
							add_library_path(path, no_warning);
						}
						break;
					case 'l':
						if(arg[1]) add_library(arg + 1);
						else {
							const char *path = *++v;
							if(!path) {
								fprintf(stderr, "%s: error: option '-l' need an argument\n",
									argv[0]);
								return 1;
							}
							add_library(path);
						}
						break;
					case 'M':
						if(arg[1]) UNRECOGNIZED_OPTION(*v);
						add_to_argv("-showIncludes");
						break;
					case 'm':
						if(arg[1]) set_machine(arg + 1);
						else {
							const char *machine = *++v;
							if(!machine) {
								fprintf(stderr, "%s: argument to `-m' is missing\n",
									argv[0]);
								return 1;
							}
							set_machine(machine);
						}
						break;
					case 'O':
						if(arg[1]) {
							const char *o = arg + 1;
							if(strcmp(o, "0") == 0) add_to_argv("-Od");
							else if(strcmp(o, "1") == 0) add_to_argv("-O2");
							else if(strcmp(o, "3") == 0) add_to_argv("-Ox");
							else if(strcmp(o, "s") == 0) add_to_argv("-O1");
							else if(strcmp(o, "fast") == 0) add_to_argv("-O2");
							else add_to_argv(*v);
						} else add_to_argv("-O2");
						break;
					case 'o':
						if(arg[1]) output_file = arg + 1;
						else {
							output_file = *++v;
							if(!output_file) {
								fprintf(stderr, "%s: error: option '-o' need an argument\n",
									argv[0]);
								return 1;
							}
						}
						break;
					case 'P':
						if(preprocess_only == -1) preprocess_only = find_argv(argv, "-E");
						if(preprocess_only) add_to_argv("-EP");
						break;
					case 's':
						if(arg[1]) UNRECOGNIZED_OPTION(*v);
						break;
					case 'U':
						if(arg[1]) add_to_argv(*v);
						else {
							const char *u = *++v;
							if(!u) {
								fprintf(stderr, "%s: error: macro name missing after '-U'\n",
									argv[0]);
								return 1;
							}
							undefine(u);
						}
						break;
					case 'v':
						if(arg[1]) UNRECOGNIZED_OPTION(*v);
						verbose = 1;
						break;
					case 'W':
						if(!arg[1]) {
							if(no_warning == -1) no_warning = find_argv(argv, "-w");
							if(!no_warning) {
								fprintf(stderr, "%s: warning: option '-W' is deprecated; use '-Wextra' instead\n",
									argv[0]);
							}
							add_to_argv("-Wall");
							break;
						}
						if(strncmp(arg, "Wa,", 3) == 0 || strncmp(arg, "Wp,", 3) == 0 || strncmp(arg, "Wl,", 3) == 0) {
							(*v)[3] = 0;		// XXX
							fprintf(stderr, "%s: warning: option '%s' is not supported\n", argv[0], *v);
							break;
						} 
						if(set_warning(arg + 1)) break;
						add_to_argv(*v);
						break;
					case 'w':
						if(arg[1]) UNRECOGNIZED_OPTION(*v);
						add_to_argv("-w");
						no_warning = 1;
						break;
					case 'x':
						if(arg[1]) set_language(arg + 1);
						else {
							const char *lang = *++v;
							if(!lang) {
								fprintf(stderr, "%s: error: missing argument to ‘-x’",
									argv[0]);
								return 4;
							}
							set_language(lang);
						}
						break;
					default:
						fprintf(stderr, "%s: error: unrecognized option '%s'\n", argv[0], *v);
						return 1;
				}
			}
		} else {
not_an_option:
#if defined __INTERIX && !defined _NO_CONV_PATH
			if(**v == '/') {
				char buffer[PATH_MAX + 1];
				if(unixpath2win(*v, 0, buffer, sizeof buffer) == 0) {
					add_input_file(buffer);
				} else {
					if(no_warning == -1) no_warning = find_argv(argv, "-w");
					if(!no_warning) {
						fprintf(stderr, "%s: warning: cannot convert '%s' to Windows path name, %s\n",
							argv[0], *v, strerror(errno));
					}
					add_input_file(*v);
				}
			} else
#endif
			add_input_file(*v);
		}
	}
	setvbuf(stdout, NULL, _IOLBF, 0);
	if(preprocess_only == -1) preprocess_only = 0;
	if(no_warning == -1) no_warning = 0;
	if(last_language && last_language_unused && !no_warning) {
		fprintf(stderr, "%s: warning: '-x %s' after last input file has no effect\n", argv[0], last_language);
	}
	if(!first_input_file) {
		if(verbose) {
			if(!no_link) add_to_argv("-c");
			start_cl();
			return 0;
		}
		fprintf(stderr, "%s: no input files\n", argv[0]);
		return 1;
	}
	if(multiple_input_files && (preprocess_only || no_link)) {
		if(output_file) {
			fprintf(stderr, "%s: error: cannot specify -o with -c or -E with multiple files\n", argv[0]);
			return 4;
		} else if(no_link) {
			fprintf(stderr, "%s: error: '-c' with multiple files is currently not supported\n", argv[0]);
			return -1;
		}
	}
	if(!output_file && !preprocess_only) {
		if(no_link) {
			size_t len = strlen(first_input_file);
			int n = get_last_dot(first_input_file, len);
			if(n >= 0) len = n;
			char *p = malloc(len + 3);
			if(!p) {
				perror(argv[0]);
				return 1;
			}
			memcpy(p, first_input_file, len);
			strcpy(p + len, ".o");
			output_file = p;
		} else output_file = DEFAULT_OUTPUT_FILENAME;
	}
	if(!verbose) add_to_argv("-nologo");
	if(preprocess_only) {
		if(output_file) target.name = output_file;
		target.type = PREPROCESSED_SOURCE;
	} else set_output_file(output_file, no_link, no_warning);
	//if(no_static_link) add_to_argv("-MD");
	add_to_argv(no_static_link ? "-MD" : "-MT");
	add_libraries_to_argv();
	if(verbose) print_argv();
	return start_cl();
}
Ejemplo n.º 14
0
int main (int argc, char **argv)
{
    register char **fp = filelist;
    register char **incp = includedirs;
    register char *p;
    register struct inclist *ip;
    char *makefile = NULL;
    struct filepointer *filecontent;
    struct symtab *psymp = predefs;
    const char *endmarker = NULL;
    char *defincdir = NULL;
    char **undeflist = NULL;
    int i, numundefs = 0;

#if defined (INITIALIZE)	// initialization code
    INITIALIZE;
#endif

    ProgramName = argv[0];

    while (psymp->s_name)
    {
        define2 (psymp->s_name, psymp->s_value, &maininclist);
        psymp++;
    }
    if (argc == 2 && argv[1][0] == '@')
    {
        struct stat ast;
        int afd;
        char *args;
        char **nargv;
        int nargc;
        char quotechar = '\0';

        nargc = 1;
        if ((afd = open (argv[1] + 1, O_RDONLY)) < 0)
            fatalerr ("cannot open \"%s\"\n", argv[1] + 1);
        fstat (afd, &ast);
        args = (char *) malloc (ast.st_size + 1);
        if ((ast.st_size = read (afd, args, ast.st_size)) < 0)
            fatalerr ("failed to read %s\n", argv[1] + 1);
        args[ast.st_size] = '\0';
        close (afd);
        for (p = args; *p; p++)
        {
            if (quotechar)
            {
                if (quotechar == '\\' ||
                    (*p == quotechar && p[-1] != '\\'))
                    quotechar = '\0';
                continue;
            }
            switch (*p)
            {
                case '\\':
                case '"':
                case '\'':
                    quotechar = *p;
                    break;
                case ' ':
                case '\n':
                    *p = '\0';
                    if (p > args && p[-1])
                        nargc++;
                    break;
            }
        }
        if (p[-1])
            nargc++;
        nargv = (char **) malloc (nargc * sizeof (char *));

        nargv[0] = argv[0];
        argc = 1;
        for (p = args; argc < nargc; p += strlen (p) + 1)
            if (*p)
                nargv[argc++] = p;
        argv = nargv;
    }
    for (argc--, argv++; argc; argc--, argv++)
    {
        /* if looking for endmarker then check before parsing */
        if (endmarker && strcmp (endmarker, *argv) == 0)
        {
            endmarker = NULL;
            continue;
        }
        if (**argv != '-')
        {
            /* treat +thing as an option for C++ */
            if (endmarker && **argv == '+')
                continue;
            *fp++ = argv[0];
            continue;
        }
        switch (argv[0][1])
        {
            case '-':
                endmarker = &argv[0][2];
                if (endmarker[0] == '\0')
                    endmarker = "--";
                break;
            case 'D':
                if (argv[0][2] == '\0')
                {
                    argv++;
                    argc--;
                }
                for (p = argv[0] + 2; *p; p++)
                    if (*p == '=')
                    {
                        *p = ' ';
                        break;
                    }
                define (argv[0] + 2, &maininclist);
                break;
            case 'I':
                if (incp >= includedirs + MAXDIRS)
                    fatalerr ("Too many -I flags.\n");
                *incp++ = argv[0] + 2;
                if (**(incp - 1) == '\0')
                {
                    *(incp - 1) = *(++argv);
                    argc--;
                }
                break;
            case 'U':
                /* Undef's override all -D's so save them up */
                numundefs++;
                if (numundefs == 1)
                    undeflist = (char **)malloc (sizeof (char *));
                else
                    undeflist = (char **)realloc (undeflist, numundefs * sizeof (char *));

                if (argv[0][2] == '\0')
                {
                    argv++;
                    argc--;
                }
                undeflist[numundefs - 1] = argv[0] + 2;
                break;
            case 'Y':
                defincdir = argv[0] + 2;
                break;
                /* do not use if endmarker processing */
            case 'a':
                if (endmarker)
                    break;
                opt_append = true;
                break;
            case 'b':
                if (endmarker)
                    break;
                opt_backup = true;
                break;
            case 'c':
                if (endmarker)
                    break;
                opt_create = true;
                break;
            case 'r':
                if (endmarker)
                    break;
                opt_remove_prefix = true;
                break;
            case 'w':
                if (endmarker)
                    break;
                if (argv[0][2] == '\0')
                {
                    argv++;
                    argc--;
                    width = atoi (argv[0]);
                }
                else
                    width = atoi (argv[0] + 2);
                break;
            case 'o':
                if (endmarker)
                    break;
                if (argv[0][2] == '\0')
                {
                    argv++;
                    argc--;
                    objsuffix = argv[0];
                }
                else
                    objsuffix = argv[0] + 2;
                break;
            case 'p':
                if (endmarker)
                    break;
                if (argv[0][2] == '\0')
                {
                    argv++;
                    argc--;
                    objprefix = argv[0];
                }
                else
                    objprefix = argv[0] + 2;
                break;
            case 'S':
                if (endmarker)
                    break;
                opt_sysincludes = true;
                break;
            case 'v':
                if (endmarker)
                    break;
                opt_verbose = true;
#ifdef CS_DEBUG
                if (argv[0][2])
                    _debugmask = atoi (argv[0] + 2);
#endif
                break;
            case 's':
                if (endmarker)
                    break;
                startat = argv[0] + 2;
                if (*startat == '\0')
                {
                    startat = *(++argv);
                    argc--;
                }
                if (*startat != '#')
                    fatalerr ("-s flag's value should start %s\n",
                              "with '#'.");
                break;
            case 'f':
                if (endmarker)
                    break;
                makefile = argv[0] + 2;
                if (*makefile == '\0')
                {
                    makefile = *(++argv);
                    argc--;
                }
                break;

            case 'm':
                opt_warn_multiple = true;
                break;

                /* Ignore -O, -g so we can just pass ${CFLAGS} to
                 makedepend
                 */
            case 'O':
            case 'g':
                break;
            case 'h':
                if (endmarker)
                    break;
                display_help ();
                return 0;
            case 'V':
                if (endmarker)
                    break;
                display_version ();
                return 0;
            default:
                if (endmarker)
                    break;
                /* fatalerr("unknown opt = %s\n", argv[0]); */
                warning ("ignoring option %s\n", argv[0]);
        }
    }
    /* Now do the undefs from the command line */
    for (i = 0; i < numundefs; i++)
        undefine (undeflist[i], &maininclist);
    if (numundefs > 0)
        free (undeflist);

    if (!defincdir)
    {
#ifdef PREINCDIR
        add_include (PREINCDIR, incp);
#endif
#if defined (__GNUC__) || defined (_WIN32)
        add_include (
#  if defined (__GNUC__)
       getenv ("C_INCLUDE_PATH"),
#  else
       getenv ("INCLUDE"),
#  endif
       incp);
#else /* !__GNUC__, does not use INCLUDEDIR at all */
        if (incp >= includedirs + MAXDIRS)
            fatalerr ("Too many -I flags.\n");
        *incp++ = INCLUDEDIR;
#endif

#ifdef POSTINCDIR
        add_include (POSTINCDIR, incp);
#endif
    }
    else if (*defincdir)
        add_include (defincdir, incp);

    /* if nothing to do, abort */
    if (!*filelist)
        fatalerr ("No files specified, try \"makedep -h\"\n");

    redirect (startat, makefile);

    /* catch signals */
#ifdef USGISH
    /*  should really reset SIGINT to SIG_IGN if it was.  */
#ifdef SIGHUP
    signal (SIGHUP, sighandler);
#endif
    signal (SIGINT, sighandler);
#ifdef SIGQUIT
    signal (SIGQUIT, sighandler);
#endif
    signal (SIGILL, sighandler);
#ifdef SIGBUS
    signal (SIGBUS, sighandler);
#endif
    signal (SIGSEGV, sighandler);
#ifdef SIGSYS
    signal (SIGSYS, sighandler);
#endif
#else
    sig_act.sa_handler = sighandler;
#ifdef _POSIX_SOURCE
    sigemptyset (&sig_act.sa_mask);
    sigaddset (&sig_act.sa_mask, SIGINT);
    sigaddset (&sig_act.sa_mask, SIGQUIT);
#ifdef SIGBUS
    sigaddset (&sig_act.sa_mask, SIGBUS);
#endif
    sigaddset (&sig_act.sa_mask, SIGILL);
    sigaddset (&sig_act.sa_mask, SIGSEGV);
    sigaddset (&sig_act.sa_mask, SIGHUP);
    sigaddset (&sig_act.sa_mask, SIGPIPE);
#ifdef SIGSYS
    sigaddset (&sig_act.sa_mask, SIGSYS);
#endif
#else
    sig_act.sa_mask = ((1 << (SIGINT - 1))
#ifdef SIGQUIT
                       | (1 << (SIGQUIT - 1))
#endif
#ifdef SIGBUS
                       | (1 << (SIGBUS - 1))
#endif
                       | (1 << (SIGILL - 1))
                       | (1 << (SIGSEGV - 1))
#ifdef SIGHUP
                       | (1 << (SIGHUP - 1))
#endif
#ifdef SIGPIPE
                       | (1 << (SIGPIPE - 1))
#endif
#ifdef SIGSYS
                       | (1 << (SIGSYS - 1))
#endif
                      );
#endif /* _POSIX_SOURCE */
    sig_act.sa_flags = 0;
#ifdef SIGHUP
    sigaction (SIGHUP, &sig_act, (struct sigaction *) 0);
#endif
    sigaction (SIGINT, &sig_act, (struct sigaction *) 0);
#ifdef SIGQUIT
    sigaction (SIGQUIT, &sig_act, (struct sigaction *) 0);
#endif
    sigaction (SIGILL, &sig_act, (struct sigaction *) 0);
#ifdef SIGBUS
    sigaction (SIGBUS, &sig_act, (struct sigaction *) 0);
#endif
    sigaction (SIGSEGV, &sig_act, (struct sigaction *) 0);
#ifdef SIGSYS
    sigaction (SIGSYS, &sig_act, (struct sigaction *) 0);
#endif
#endif /* USGISH */

    /*
     * now peruse through the list of files.
     */
    for (fp = filelist; *fp; fp++)
    {
        filecontent = getfile (*fp);
        ip = newinclude (*fp, (char *) NULL);

        find_includes (filecontent, ip, ip, 0, false);
        freefile (filecontent);
        recursive_pr_include (ip, ip->i_file, base_name (*fp));
        inc_clean ();
    }
    if (opt_printed)
        printf ("\n");
    return 0;
}