Esempio n. 1
0
File: file.c Progetto: franred/ofc
ofc_file_t* ofc_file_create_include(
    const char* path, ofc_lang_opts_t opts, const char* include)
{
    ofc_file_t* file = ofc_file_create(path, opts);
    if (!include) return file;
    if (!file) return NULL;

    file->include = strdup(include);
    if (!file->include)
    {
        ofc_file_delete(file);
        return NULL;
    }

    return file;
}
Esempio n. 2
0
bool ofc_cliarg_parse(
	int argc,
    const char* argv[],
	ofc_file_list_t** file_list,
	ofc_print_opts_t* print_opts,
	ofc_global_opts_t* global_opts,
	ofc_sema_pass_opts_t* sema_pass_opts)
{
	const char* program_name = argv[0];

	if (argc < 2)
	{
		fprintf(stderr, "Error: Expected source path\n");
		ofc_cliarg_print_usage(program_name);
		return false;
	}

	ofc_cliarg_path_list_t* path_list = ofc_cliarg_path_list_create();
	ofc_cliarg_list_t*      args_list = ofc_cliarg_list_create();

	unsigned i = 1;
	while (i < (unsigned)argc)
	{
		if (argv[i][0] == '-')
		{
			if (argv[i][1] == '-')
			{
				ofc_cliarg_t* resolved_arg = NULL;
				const char* arg_str = argv[i] + 2;
				const ofc_cliarg_body_t* arg_body = ofc_cliarg_arg__resolve_name(arg_str);
				if (!arg_body)
				{
					fprintf(stderr, "Error: Unable to resolve argument: %s\n", argv[i]);
					ofc_cliarg_print_usage(program_name);
					return false;
				}
				i++;

				switch (arg_body->param_type)
				{
					case OFC_CLIARG_PARAM_GLOB_NONE:
					case OFC_CLIARG_PARAM_LANG_NONE:
					case OFC_CLIARG_PARAM_PRIN_NONE:
					case OFC_CLIARG_PARAM_SEMA_PASS:
						resolved_arg = ofc_cliarg_create(arg_body, NULL);
						break;

					case OFC_CLIARG_PARAM_LANG_INT:
					case OFC_CLIARG_PARAM_PRIN_INT:
					{
						int param = -1;
						if (!ofc_cliarg_param__resolve_int(argv[i++], &param))
						{
							fprintf(stderr, "Error: Expected parameter for argument: %s\n", argv[i]);
							ofc_cliarg_print_usage(program_name);
							return false;
						}
						resolved_arg = ofc_cliarg_create(arg_body, &param);
						break;
					}

					case OFC_CLIARG_PARAM_FILE_STR:
					{
						if (ofc_cliarg_param__resolve_str(argv[i]))
						{
							resolved_arg = ofc_cliarg_create(arg_body, argv[i]);
							i++;
						}
						else
						{
							fprintf(stderr, "Error: Expected parameter for argument: %s\n", argv[i]);
							ofc_cliarg_print_usage(program_name);
							return false;
						}
						break;
					}

					default:
						break;
				}

				if (!resolved_arg
					|| !ofc_cliarg_list_add(args_list, resolved_arg))
					return false;
			}
			else
			{
				const char* arg_str = argv[i] + 1;
				unsigned flag;
				for (flag = 0; flag < strlen(arg_str); flag++)
				{
					ofc_cliarg_t* resolved_arg = NULL;

					const ofc_cliarg_body_t* arg_body
						= ofc_cliarg_arg__resolve_flag(arg_str[flag]);
					if (!arg_body)
					{
						fprintf(stderr, "Error: Cannot resolve flag: %s\n", argv[i]);
						ofc_cliarg_print_usage(program_name);
					}
					switch (arg_body->param_type)
					{
						case OFC_CLIARG_PARAM_GLOB_NONE:
						case OFC_CLIARG_PARAM_LANG_NONE:
						case OFC_CLIARG_PARAM_SEMA_PASS:
							resolved_arg = ofc_cliarg_create(arg_body, NULL);
							break;

						default:
							fprintf(stderr, "Error: Cannot group flags that require a parameter: %s\n", argv[i]);
							ofc_cliarg_print_usage(program_name);
							return false;
					}

					if (!resolved_arg
						|| !ofc_cliarg_list_add(args_list, resolved_arg))
						return false;
				}
				i++;
			}
		}
		else
		{
			/* Argument with no preceding dash is assumed to be a source file path */
			if (!ofc_cliarg_path_list_add(path_list, argv[i]))
				return false;
			i++;
		}
	}

	unsigned j;
	for (j = 0; j < path_list->count; j++)
	{
		char* path = path_list->path[j];
		const char* source_file_ext = ofc_cliarg_file_ext__get(path);

		ofc_lang_opts_t lang_opts = OFC_LANG_OPTS_F77;
		if (source_file_ext && (strcasecmp(source_file_ext, "F90") == 0))
			lang_opts = OFC_LANG_OPTS_F90;

		ofc_file_t* file = ofc_file_create(path, lang_opts);
		if (!file)
		{
			fprintf(stderr, "\nError: Failed read source file '%s'\n", path);
			ofc_cliarg_list_delete(args_list);
			ofc_cliarg_path_list_delete(path_list);
			return false;
		}

		ofc_lang_opts_t* lang_opts_ptr = ofc_file_modify_lang_opts(file);
		if (!lang_opts_ptr)
		{
			ofc_cliarg_list_delete(args_list);
			ofc_cliarg_path_list_delete(path_list);
			return false;
		}

		if (!ofc_cliarg_list__apply(global_opts, print_opts,
			lang_opts_ptr, sema_pass_opts, file, args_list))
		{
			ofc_cliarg_list_delete(args_list);
			ofc_cliarg_path_list_delete(path_list);
			return false;
		}

		if (!ofc_file_list_add(*file_list, file))
		{
			ofc_cliarg_list_delete(args_list);
			ofc_cliarg_path_list_delete(path_list);
			return false;
		}
	}

	ofc_cliarg_list_delete(args_list);
	ofc_cliarg_path_list_delete(path_list);

	return true;
}