Esempio n. 1
0
/**
 * Removes superfluous semicolons:
 *  - after brace close whose parent is IF, ELSE, SWITCH, WHILE, FOR, NAMESPACE
 *  - after another semicolon where parent is not FOR
 *  - (D) after brace close whose parent is ENUM/STRUCT/UNION
 *  - after an open brace
 *  - when not in a #DEFINE
 */
void remove_extra_semicolons(void)
{
   chunk_t *pc;
   chunk_t *next;
   chunk_t *prev;

   pc = chunk_get_head();
   while (pc != NULL)
   {
      next = chunk_get_next_ncnl(pc);

      if ((pc->type == CT_SEMICOLON) && !(pc->flags & PCF_IN_PREPROC) &&
          ((prev = chunk_get_prev_ncnl(pc)) != NULL))
      {
         LOG_FMT(LSCANSEMI, "Semi on %d:%d, prev = '%s' [%s/%s]\n",
                 pc->orig_line, pc->orig_col, prev->str.c_str(),
                 get_token_name(prev->type), get_token_name(prev->parent_type));

         if ((prev->type == CT_BRACE_CLOSE) &&
             ((prev->parent_type == CT_IF) ||
              (prev->parent_type == CT_ELSEIF) ||
              (prev->parent_type == CT_ELSE) ||
              (prev->parent_type == CT_SWITCH) ||
              (prev->parent_type == CT_WHILE) ||
              (prev->parent_type == CT_USING_STMT) ||
              (prev->parent_type == CT_FOR) ||
              (prev->parent_type == CT_FUNC_DEF) ||
              (prev->parent_type == CT_OC_MSG_DECL) ||
              (prev->parent_type == CT_FUNC_CLASS) ||
              (prev->parent_type == CT_NAMESPACE)))
         {
            remove_semicolon(pc);
         }
         else if ((prev->type == CT_BRACE_CLOSE) &&
                  (prev->parent_type == CT_NONE))
         {
            check_unknown_brace_close(pc, prev);
         }
         else if ((prev->type == CT_SEMICOLON) &&
                  (prev->parent_type != CT_FOR))
         {
            remove_semicolon(pc);
         }
         else if ((cpd.lang_flags & LANG_D) &&
                  ((prev->parent_type == CT_ENUM) ||
                   (prev->parent_type == CT_UNION) ||
                   (prev->parent_type == CT_STRUCT)))
         {
            remove_semicolon(pc);
         }
         else if (prev->type == CT_BRACE_OPEN)
         {
            remove_semicolon(pc);
         }
      }

      pc = next;
   }
}
Esempio n. 2
0
/**
 * We are on a semicolon that is after an unidentified brace close.
 * Check for what is before the brace open.
 * Do not remove if it is a square close, word or type.
 */
static void check_unknown_brace_close(chunk_t *semi, chunk_t *brace_close)
{
   chunk_t *pc;

   pc = chunk_get_prev_type(brace_close, CT_BRACE_OPEN, brace_close->level);
   pc = chunk_get_prev_ncnl(pc);
   if ((pc != NULL) &&
       (pc->type != CT_WORD) &&
       (pc->type != CT_TYPE) &&
       (pc->type != CT_SQUARE_CLOSE) &&
       (pc->type != CT_TSQUARE))
   {
      remove_semicolon(semi);
   }
}
Esempio n. 3
0
static void check_unknown_brace_close(chunk_t *semi, chunk_t *brace_close)
{
   LOG_FUNC_ENTRY();
   chunk_t *pc = chunk_get_prev_type(brace_close, CT_BRACE_OPEN, brace_close->level);
   pc = chunk_get_prev_ncnl(pc);
   if (  pc != nullptr
      && pc->type != CT_RETURN
      && pc->type != CT_WORD
      && pc->type != CT_TYPE
      && pc->type != CT_SQUARE_CLOSE
      && pc->type != CT_ANGLE_CLOSE
      && pc->type != CT_TSQUARE
      && !chunk_is_paren_close(pc))
   {
      remove_semicolon(semi);
   }
}
Esempio n. 4
0
void remove_extra_semicolons(void)
{
   LOG_FUNC_ENTRY();

   chunk_t *pc = chunk_get_head();
   while (pc != nullptr)
   {
      chunk_t *next = chunk_get_next_ncnl(pc);
      chunk_t *prev;
      if (  pc->type == CT_SEMICOLON
         && !(pc->flags & PCF_IN_PREPROC)
         && (prev = chunk_get_prev_ncnl(pc)) != nullptr)
      {
         LOG_FMT(LSCANSEMI, "Semi on %zu:%zu parent=%s, prev = '%s' [%s/%s]\n",
                 pc->orig_line, pc->orig_col, get_token_name(pc->parent_type),
                 prev->text(),
                 get_token_name(prev->type), get_token_name(prev->parent_type));

         if (pc->parent_type == CT_TYPEDEF)
         {
            // keep it
         }
         else if (  prev->type == CT_BRACE_CLOSE
                 && (  prev->parent_type == CT_IF
                    || prev->parent_type == CT_ELSEIF
                    || prev->parent_type == CT_ELSE
                    || prev->parent_type == CT_SWITCH
                    || prev->parent_type == CT_WHILE
                    || prev->parent_type == CT_USING_STMT
                    || prev->parent_type == CT_FOR
                    || prev->parent_type == CT_FUNC_DEF
                    || prev->parent_type == CT_OC_MSG_DECL
                    || prev->parent_type == CT_FUNC_CLASS_DEF
                    || prev->parent_type == CT_NAMESPACE))
         {
            LOG_FUNC_CALL();
            remove_semicolon(pc);
         }
         else if (  prev->type == CT_BRACE_CLOSE
                 && prev->parent_type == CT_NONE)
         {
            check_unknown_brace_close(pc, prev);
         }
         else if (prev->type == CT_SEMICOLON && prev->parent_type != CT_FOR)
         {
            LOG_FUNC_CALL();
            remove_semicolon(pc);
         }
         else if (  (cpd.lang_flags & LANG_D)
                 && (  prev->parent_type == CT_ENUM
                    || prev->parent_type == CT_UNION
                    || prev->parent_type == CT_STRUCT))
         {
            LOG_FUNC_CALL();
            remove_semicolon(pc);
         }
         else if (  (cpd.lang_flags & LANG_JAVA)
                 && prev->parent_type == CT_SYNCHRONIZED)
         {
            LOG_FUNC_CALL();
            remove_semicolon(pc);
         }
         else if (prev->type == CT_BRACE_OPEN)
         {
            LOG_FUNC_CALL();
            remove_semicolon(pc);
         }
      }

      pc = next;
   }
} // remove_extra_semicolons
Esempio n. 5
0
void shell()
{
	current_database = -1;
	char entrada[1000], nomeBD[TAM_NOME_BANCO];
    int resultado = 0, codDB = -1;
    nomeBD[0]='\0';

    char *current_db_name = strdup(">");//inicializa com nenhum banco conectado
	char *start;
    
    start = strdup("dbms-start");//este comando posteriormente como start do banco, no momento ele é automatico
	printf("\nWelcome to the DBMS Interface.\nType 'help' '\\h' for help.\n\n");	

	/**
	 * ****************************
	 * 
	 *   Comandos do shell
	 *
	 * ****************************
	 */
	using_history ();//função para usar o histórico
	read_history (".history_file");

	while(1)
	{
		int nTokens;

		strcpy(entrada, readline(current_db_name));

		/**
		 * Adiciona ao histórico
		 */
		if (entrada[0])
        {
			char *expansion;
			int result;

			result = history_expand (entrada, &expansion);
			if (result)
			fprintf (stderr, "%s", expansion);

			if (result < 0 || result == 2)
			{
			  free (expansion);
			  continue;
			}

			add_history (expansion);
			strncpy (entrada, expansion, sizeof (entrada) - 1);
			free (expansion);

			write_history (".history_file");//adiciona no histórico
        }

		char **tokens = tokenize( trim_white_space(remove_newline(entrada)),' ',&nTokens);
		
		/**
		 * Opção para criar tabela e banco de dados
		 */
		if (strcmp(strtolower(tokens[0]),"create")==0)
		{
			if(strcmp(strtolower(tokens[1]),"table")==0)
			{
				if (current_database == -1)
				{
					printf("Not connected to any database.\n");
					continue;
				}
				createTable(entrada,current_database);
			}
			else if(strcmp(strtolower(tokens[1]),"database")==0)
			{
				if (nTokens >= 5)
				{
					printf("Invalid command. Type help to show de interface usage.\n");
					continue;
				}
				if (strlen(tokens[2]) > TAM_NOME_BANCO )
				{
					printf("Database name too big.\n");
					continue;
				}

				resultado = checkCreateDB( remove_semicolon(tokens[2]) );//verifica a existência do nome e grava-o no arquivo
				
				if(resultado==-1) 
				{
					printf("Error creating database file.\n");
				}
				if(resultado==-3) 
				{
					printf("Database exists.\n");
				}
				else
				{
					printf("Database created successfully.\n");
				}
			} 
			else
			{
				printf("Invalid command. Type help to show de interface usage.\n");
				continue;
			}   
		}
		/**
		 * Conecta ao banco de dados passado como parâmetro
		 */
		else if(strcmp(strtolower(tokens[0]),"\\c") == 0){
				
			if (nTokens != 2)
			{
				printf("Invalid number of arguments. Type help to show the interface usage.\n");

				continue;
			}
			char *name_db = remove_semicolon(tokens[1]);
			codDB = busca(name_db,1); //função chamada para conecção no banco, retorna o codigo do banco ao conectar
		
			if (codDB >= 0)
			{
				strcpy(nomeBD, name_db);  //passa o nome do bd, para a variavel mostrar ao usuario qual o banco conectado
				free(current_db_name);
				
				current_db_name = (char*) malloc (sizeof(char)*(strlen(name_db)+3));

				if (current_db_name == NULL)
				{
					printf("Out of memory.\nAborting...\n");
				}

				strcpy(current_db_name,name_db);
				current_database = codDB;

				strcat(current_db_name,"=#");
				current_db_name[strlen(current_db_name)] = '\0'; 
			}
			else
			{
				printf("No such database '%s'.\n", name_db);
				continue;
			}
		}
		/**
		 * Insere tuplas em uma tabela
		 */
		else if(strcmp(strtolower(tokens[0]),"insert")==0)
		{
			if (current_database == -1)
			{
				printf("Not connected to any database.\n");
				continue;
			}
			
			insert(entrada,current_database);
		}
		/**
		 * Imprime as tabelas do banco de dados atual
		 * ou o esquema de uma tabela
		 */
		else if(strcmp(strtolower(tokens[0]),"\\d")==0)
		{
			if (current_database == -1)
			{
				printf("Not connected to any database.\n");
				continue;
			}
			if (nTokens >= 3)
			{
				printf("Invalid number of arguments. Type help to show the interface usage.\n");

				continue;
			}
			else if (nTokens == 1)
			{
				//imprime tabelas do banco de dados
				listaTabelas(current_database);
			}
			else
			{
				//imprime o esquema de uma tabela
				char *t = table_name_real(remove_semicolon(tokens[1]),current_database);

				if(!verificaNomeTabela(t)){
			        printf("Invalid table name.\n");
					free(t);
			        continue;			    
			    }

				struct fs_objects objeto = leObjeto(t);//para verificar se a tabela esta no banco 						
				

				show_schema(objeto,tokens[1]);

				free(t);
			}
		   
		} 
		/**
		 * Comando temporário para imprimir tabela
		 */
		else if (strcmp(strtolower(tokens[0]),"select")==0)
		{
			if (current_database == -1)
			{
				printf("Not connected to any database.\n");
				continue;
			}
			
			selectTable(entrada,current_database);
		}
		/**
		 * Imprime os registros da tabela passada
		 */
		else if (strcmp(strtolower(tokens[0]),"show")==0)
		{
			if (nTokens != 2)
			{
				printf("Invalid number of arguments. Type help to show the interface usage.\n");

				continue;
			}
			if (current_database == -1)
			{
				printf("Not connected to any database.\n");
				continue;
			}
			if (verificaNomeTabela(table_name_real(remove_semicolon(tokens[1]),current_database) ) == 0 )
			{
				printf("Table %s doesn't exist.\n",remove_semicolon(tokens[1]));
				continue;
			}

			char *t = table_name_real(remove_semicolon(tokens[1]),current_database);

			char *file = table_name_real(remove_semicolon(tokens[1]),current_database);
			strcat(file,".dat");
			
			if (existeArquivo(file) == 0)
			{
				printf("Table is empty.\n" );
				continue;
			}

			imprime(t);
			free(file);
			free(t);
		}  
		/**
		 * Lista os bancos existentes
		 */
		else if(strcmp(strtolower(tokens[0]),"\\l")==0)
		{
			if (nTokens != 1)
			{
				printf("Invalid number of arguments. Type help to show the interface usage.\n");

				continue;
			}
			//LISTA os bancos existentes
			listaBancos();
		}   
		/**
		 * Opção para deletar o banco de dados e tabelas
		 */
		else if(strcmp(strtolower(tokens[0]),"drop")==0)
		{
			if (nTokens != 3)
			{
				printf("Invalid number of arguments. Type help to show the interface usage.\n");

				continue;
			}
			else if(strcmp(strtolower(tokens[1]),"table") == 0){
				
				if (current_database == -1)
				{
					printf("Not connected to any database.\n");
					continue;
				}
				if (verificaNomeTabela(table_name_real(remove_semicolon(tokens[2]),current_database) ) == 0 )
				{
					printf("Table %s doesn't exist.\n",remove_semicolon(tokens[2]));
					continue;
				}
				
				char *t = table_name_real(remove_semicolon(tokens[2]),current_database);
				char *exist = table_name_real(remove_semicolon(tokens[2]),current_database);
			
				int ok = excluirTabela(t);

				if (ok == SUCCESS)
				{
					printf("Table deleted successfully.\n");
				}

				free(exist);
				free(t);
			}
			else if(strcmp(strtolower(tokens[1]),"database") == 0){
				
				char *exist = table_name_real(remove_semicolon(tokens[2]),current_database);
				strcat(exist,".dat");
				
				if (existeArquivo(exist) != 0)
				{
					printf("The database is not empty for drop, there are existing tables.\n" );
					continue;
				}
				
				exist = remove_semicolon(tokens[2]);
				codDB = busca(exist,1);
				
				if(codDB == current_database)
				{
                	printf("Cannot drop the currently open database.\n");
                    continue;
				}

				int drop = dropDatabase(remove_semicolon(tokens[2]));

				if(drop == 1)printf("Database deleted successfully.\n");
                
                free(exist);
			}
		}
		/**
		 * Ajuda ao usuário com exemplos da sintaxe dos comandos
		 */
		else if (strcmp(strtolower(tokens[0]),"help")==0 || strcmp(strtolower(tokens[0]),"\\h")==0)
		{
			if (nTokens != 1)
			{
				printf("Invalid number of arguments. Type help to show the interface usage.\n");
			}

			help();
		}
		/**
		 * Imprime mensagem de copyright
		 */
		else if(strcmp(strtolower(remove_semicolon(tokens[0])),"\\copyright")==0)
		{
			printf("\nDatabase Management System\n");
			printf("\nPermission to use, copy, modify, and distribute this software and its\ndocumentation for any purpose, without fee, and without a written agreement\nis hereby granted, provided that the above copyright notice and this\nparagraph and the following two paragraphs appear in all copies.\n");
			printf("\nTHIS SOFTWARE IS BEING DEVELOPED BY STUDENTS OF DATABASE II CLASS AT UNIVERSIDADE FEDERAL DA FRONTEIRA SUL.\n\n");	
		}
		/**
		 * Comando de saída
		 */
		else if(strcmp(strtolower(remove_semicolon(tokens[0])),"exit")==0)
		{
			break;
		} 
		else if(strcmp(strtolower(remove_semicolon(tokens[0])),"quit")==0)
		{
			break;
		}
		else if(strcmp(strtolower(remove_semicolon(tokens[0])),"bye")==0)
		{
			break;
		}
		else if(strcmp(strtolower(remove_semicolon(tokens[0])),"\\q")==0)
		{
			break;
		}
		else
		{
			printf("Invalid command. Type help to show the interface usage.\n");
			continue;
		}
	}  

	free(start);
	free(current_db_name);
}