From 810c64caa9d1f9c1cfb5e6986091143d7d9f1c27 Mon Sep 17 00:00:00 2001 From: fiplox Date: Tue, 4 Jan 2022 13:12:29 +0100 Subject: [PATCH] colors, options/flags, fixes --- Makefile | 1 + README.md | 6 + src/cmdline.c | 639 ++++++++++++++++++++++++++++++++++++++++++++++++ src/cmdline.ggo | 13 + src/cmdline.h | 196 +++++++++++++++ src/colors.h | 14 ++ src/main.c | 95 ++++--- src/matcom.c | 36 +-- src/matcom.h | 2 +- src/vec.c | 7 +- 10 files changed, 958 insertions(+), 51 deletions(-) create mode 100644 src/cmdline.c create mode 100644 src/cmdline.ggo create mode 100644 src/cmdline.h create mode 100644 src/colors.h diff --git a/Makefile b/Makefile index 4716987..17600bc 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,7 @@ MKDIR = mkdir CHMOD = chmod CP = rsync -Rr DOT = dot -Tpdf -Grankdir=LR +# DOT = sfdp -Tpdf -Grankdir=LR -Goverlap=scale EXTRAFILES = LICENSE README.md DISTFILES = $(SRCDIR) $(TESTDIR) Makefile $(EXTRAFILES) distdir = $(AUTHOR)-$(NAME)-$(VERSION) diff --git a/README.md b/README.md index 42308cc..d26f06c 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,9 @@ ## Sujet Un programme peut être considéré comme un graphe orienté où chaque fonction est un noeud. Prendre un programme important (plusieurs dizaines de fichiers, plusieurs dizaines de milliers de lignes de code) et l’analyser sous la forme d’un graphe (Mco, Vec). On doit chercher les composantes connexes et les cycles. + +## Programmes testés + +* [st](https://st.suckless.org/) +* [dwm](https://dwm.suckless.org/) +* [neovim](https://github.com/neovim/neovim) diff --git a/src/cmdline.c b/src/cmdline.c new file mode 100644 index 0000000..48c6323 --- /dev/null +++ b/src/cmdline.c @@ -0,0 +1,639 @@ +/* + File autogenerated by gengetopt version 2.23 + generated with the following command: + gengetopt + + The developers of gengetopt consider the fixed text that goes in all + gengetopt output files to be in the public domain: + we make no copyright claims on it. +*/ + +/* If we use autoconf. */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#ifndef FIX_UNUSED +#define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */ +#endif + +#include + +#include "cmdline.h" + +const char *gengetopt_args_info_purpose = "Analyze source code in a form of a graph.\n\nBy Volodymyr Patuta "; + +const char *gengetopt_args_info_usage = "Usage: code-analyzer [OPTION]..."; + +const char *gengetopt_args_info_versiontext = ""; + +const char *gengetopt_args_info_description = ""; + +const char *gengetopt_args_info_help[] = { + " -h, --help Print help and exit", + " -V, --version Print version and exit", + " -p, --path=STRING Path to the source-code directory.", + " -f, --file=STRING Path to the .c file.", + " -m, --main-graph Generate main graph separated from other CC. (default=on)", + " --print-vec Print vector graph. (default=off)", + " --print-mc Print compact matrice graph. (default=off)", + " --cycles-vec Find and print cycles in a vector graph. (default=off)", + " --cycles-mc Find and print cycles in a compact matrice graph.\n (default=off)", + 0 +}; + +typedef enum {ARG_NO + , ARG_FLAG + , ARG_STRING +} cmdline_parser_arg_type; + +static +void clear_given (struct gengetopt_args_info *args_info); +static +void clear_args (struct gengetopt_args_info *args_info); + +static int +cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info, + struct cmdline_parser_params *params, const char *additional_error); + + +static char * +gengetopt_strdup (const char *s); + +static +void clear_given (struct gengetopt_args_info *args_info) +{ + args_info->help_given = 0 ; + args_info->version_given = 0 ; + args_info->path_given = 0 ; + args_info->file_given = 0 ; + args_info->main_graph_given = 0 ; + args_info->print_vec_given = 0 ; + args_info->print_mc_given = 0 ; + args_info->cycles_vec_given = 0 ; + args_info->cycles_mc_given = 0 ; +} + +static +void clear_args (struct gengetopt_args_info *args_info) +{ + FIX_UNUSED (args_info); + args_info->path_arg = NULL; + args_info->path_orig = NULL; + args_info->file_arg = NULL; + args_info->file_orig = NULL; + args_info->main_graph_flag = 1; + args_info->print_vec_flag = 0; + args_info->print_mc_flag = 0; + args_info->cycles_vec_flag = 0; + args_info->cycles_mc_flag = 0; + +} + +static +void init_args_info(struct gengetopt_args_info *args_info) +{ + + + args_info->help_help = gengetopt_args_info_help[0] ; + args_info->version_help = gengetopt_args_info_help[1] ; + args_info->path_help = gengetopt_args_info_help[2] ; + args_info->file_help = gengetopt_args_info_help[3] ; + args_info->main_graph_help = gengetopt_args_info_help[4] ; + args_info->print_vec_help = gengetopt_args_info_help[5] ; + args_info->print_mc_help = gengetopt_args_info_help[6] ; + args_info->cycles_vec_help = gengetopt_args_info_help[7] ; + args_info->cycles_mc_help = gengetopt_args_info_help[8] ; + +} + +void +cmdline_parser_print_version (void) +{ + printf ("%s %s\n", + (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE), + CMDLINE_PARSER_VERSION); + + if (strlen(gengetopt_args_info_versiontext) > 0) + printf("\n%s\n", gengetopt_args_info_versiontext); +} + +static void print_help_common(void) +{ + size_t len_purpose = strlen(gengetopt_args_info_purpose); + size_t len_usage = strlen(gengetopt_args_info_usage); + + if (len_usage > 0) { + printf("%s\n", gengetopt_args_info_usage); + } + if (len_purpose > 0) { + printf("%s\n", gengetopt_args_info_purpose); + } + + if (len_usage || len_purpose) { + printf("\n"); + } + + if (strlen(gengetopt_args_info_description) > 0) { + printf("%s\n\n", gengetopt_args_info_description); + } +} + +void +cmdline_parser_print_help (void) +{ + int i = 0; + print_help_common(); + while (gengetopt_args_info_help[i]) + printf("%s\n", gengetopt_args_info_help[i++]); +} + +void +cmdline_parser_init (struct gengetopt_args_info *args_info) +{ + clear_given (args_info); + clear_args (args_info); + init_args_info (args_info); +} + +void +cmdline_parser_params_init(struct cmdline_parser_params *params) +{ + if (params) + { + params->override = 0; + params->initialize = 1; + params->check_required = 1; + params->check_ambiguity = 0; + params->print_errors = 1; + } +} + +struct cmdline_parser_params * +cmdline_parser_params_create(void) +{ + struct cmdline_parser_params *params = + (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params)); + cmdline_parser_params_init(params); + return params; +} + +static void +free_string_field (char **s) +{ + if (*s) + { + free (*s); + *s = 0; + } +} + + +static void +cmdline_parser_release (struct gengetopt_args_info *args_info) +{ + + free_string_field (&(args_info->path_arg)); + free_string_field (&(args_info->path_orig)); + free_string_field (&(args_info->file_arg)); + free_string_field (&(args_info->file_orig)); + + + + clear_given (args_info); +} + + +static void +write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[]) +{ + FIX_UNUSED (values); + if (arg) { + fprintf(outfile, "%s=\"%s\"\n", opt, arg); + } else { + fprintf(outfile, "%s\n", opt); + } +} + + +int +cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info) +{ + int i = 0; + + if (!outfile) + { + fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE); + return EXIT_FAILURE; + } + + if (args_info->help_given) + write_into_file(outfile, "help", 0, 0 ); + if (args_info->version_given) + write_into_file(outfile, "version", 0, 0 ); + if (args_info->path_given) + write_into_file(outfile, "path", args_info->path_orig, 0); + if (args_info->file_given) + write_into_file(outfile, "file", args_info->file_orig, 0); + if (args_info->main_graph_given) + write_into_file(outfile, "main-graph", 0, 0 ); + if (args_info->print_vec_given) + write_into_file(outfile, "print-vec", 0, 0 ); + if (args_info->print_mc_given) + write_into_file(outfile, "print-mc", 0, 0 ); + if (args_info->cycles_vec_given) + write_into_file(outfile, "cycles-vec", 0, 0 ); + if (args_info->cycles_mc_given) + write_into_file(outfile, "cycles-mc", 0, 0 ); + + + i = EXIT_SUCCESS; + return i; +} + +int +cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info) +{ + FILE *outfile; + int i = 0; + + outfile = fopen(filename, "w"); + + if (!outfile) + { + fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename); + return EXIT_FAILURE; + } + + i = cmdline_parser_dump(outfile, args_info); + fclose (outfile); + + return i; +} + +void +cmdline_parser_free (struct gengetopt_args_info *args_info) +{ + cmdline_parser_release (args_info); +} + +/** @brief replacement of strdup, which is not standard */ +char * +gengetopt_strdup (const char *s) +{ + char *result = 0; + if (!s) + return result; + + result = (char*)malloc(strlen(s) + 1); + if (result == (char*)0) + return (char*)0; + strcpy(result, s); + return result; +} + +int +cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info) +{ + return cmdline_parser2 (argc, argv, args_info, 0, 1, 1); +} + +int +cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info, + struct cmdline_parser_params *params) +{ + int result; + result = cmdline_parser_internal (argc, argv, args_info, params, 0); + + if (result == EXIT_FAILURE) + { + cmdline_parser_free (args_info); + exit (EXIT_FAILURE); + } + + return result; +} + +int +cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required) +{ + int result; + struct cmdline_parser_params params; + + params.override = override; + params.initialize = initialize; + params.check_required = check_required; + params.check_ambiguity = 0; + params.print_errors = 1; + + result = cmdline_parser_internal (argc, argv, args_info, ¶ms, 0); + + if (result == EXIT_FAILURE) + { + cmdline_parser_free (args_info); + exit (EXIT_FAILURE); + } + + return result; +} + +int +cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name) +{ + FIX_UNUSED (args_info); + FIX_UNUSED (prog_name); + return EXIT_SUCCESS; +} + + +static char *package_name = 0; + +/** + * @brief updates an option + * @param field the generic pointer to the field to update + * @param orig_field the pointer to the orig field + * @param field_given the pointer to the number of occurrence of this option + * @param prev_given the pointer to the number of occurrence already seen + * @param value the argument for this option (if null no arg was specified) + * @param possible_values the possible values for this option (if specified) + * @param default_value the default value (in case the option only accepts fixed values) + * @param arg_type the type of this option + * @param check_ambiguity @see cmdline_parser_params.check_ambiguity + * @param override @see cmdline_parser_params.override + * @param no_free whether to free a possible previous value + * @param multiple_option whether this is a multiple option + * @param long_opt the corresponding long option + * @param short_opt the corresponding short option (or '-' if none) + * @param additional_error possible further error specification + */ +static +int update_arg(void *field, char **orig_field, + unsigned int *field_given, unsigned int *prev_given, + char *value, const char *possible_values[], + const char *default_value, + cmdline_parser_arg_type arg_type, + int check_ambiguity, int override, + int no_free, int multiple_option, + const char *long_opt, char short_opt, + const char *additional_error) +{ + char *stop_char = 0; + const char *val = value; + int found; + char **string_field; + FIX_UNUSED (field); + + stop_char = 0; + found = 0; + + if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given))) + { + if (short_opt != '-') + fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", + package_name, long_opt, short_opt, + (additional_error ? additional_error : "")); + else + fprintf (stderr, "%s: `--%s' option given more than once%s\n", + package_name, long_opt, + (additional_error ? additional_error : "")); + return 1; /* failure */ + } + + FIX_UNUSED (default_value); + + if (field_given && *field_given && ! override) + return 0; + if (prev_given) + (*prev_given)++; + if (field_given) + (*field_given)++; + if (possible_values) + val = possible_values[found]; + + switch(arg_type) { + case ARG_FLAG: + *((int *)field) = !*((int *)field); + break; + case ARG_STRING: + if (val) { + string_field = (char **)field; + if (!no_free && *string_field) + free (*string_field); /* free previous string */ + *string_field = gengetopt_strdup (val); + } + break; + default: + break; + }; + + FIX_UNUSED(stop_char); + + /* store the original value */ + switch(arg_type) { + case ARG_NO: + case ARG_FLAG: + break; + default: + if (value && orig_field) { + if (no_free) { + *orig_field = value; + } else { + if (*orig_field) + free (*orig_field); /* free previous string */ + *orig_field = gengetopt_strdup (value); + } + } + }; + + return 0; /* OK */ +} + + +int +cmdline_parser_internal ( + int argc, char **argv, struct gengetopt_args_info *args_info, + struct cmdline_parser_params *params, const char *additional_error) +{ + int c; /* Character of the parsed option. */ + + int error_occurred = 0; + struct gengetopt_args_info local_args_info; + + int override; + int initialize; + int check_required; + int check_ambiguity; + + package_name = argv[0]; + + /* TODO: Why is this here? It is not used anywhere. */ + override = params->override; + FIX_UNUSED(override); + + initialize = params->initialize; + check_required = params->check_required; + + /* TODO: Why is this here? It is not used anywhere. */ + check_ambiguity = params->check_ambiguity; + FIX_UNUSED(check_ambiguity); + + if (initialize) + cmdline_parser_init (args_info); + + cmdline_parser_init (&local_args_info); + + optarg = 0; + optind = 0; + opterr = params->print_errors; + optopt = '?'; + + while (1) + { + int option_index = 0; + + static struct option long_options[] = { + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'V' }, + { "path", 1, NULL, 'p' }, + { "file", 1, NULL, 'f' }, + { "main-graph", 0, NULL, 'm' }, + { "print-vec", 0, NULL, 0 }, + { "print-mc", 0, NULL, 0 }, + { "cycles-vec", 0, NULL, 0 }, + { "cycles-mc", 0, NULL, 0 }, + { 0, 0, 0, 0 } + }; + + c = getopt_long (argc, argv, "hVp:f:m", long_options, &option_index); + + if (c == -1) break; /* Exit from `while (1)' loop. */ + + switch (c) + { + case 'h': /* Print help and exit. */ + cmdline_parser_print_help (); + cmdline_parser_free (&local_args_info); + exit (EXIT_SUCCESS); + + case 'V': /* Print version and exit. */ + cmdline_parser_print_version (); + cmdline_parser_free (&local_args_info); + exit (EXIT_SUCCESS); + + case 'p': /* Path to the source-code directory.. */ + + + if (update_arg( (void *)&(args_info->path_arg), + &(args_info->path_orig), &(args_info->path_given), + &(local_args_info.path_given), optarg, 0, 0, ARG_STRING, + check_ambiguity, override, 0, 0, + "path", 'p', + additional_error)) + goto failure; + + break; + case 'f': /* Path to the .c file.. */ + + + if (update_arg( (void *)&(args_info->file_arg), + &(args_info->file_orig), &(args_info->file_given), + &(local_args_info.file_given), optarg, 0, 0, ARG_STRING, + check_ambiguity, override, 0, 0, + "file", 'f', + additional_error)) + goto failure; + + break; + case 'm': /* Generate main graph separated from other CC.. */ + + + if (update_arg((void *)&(args_info->main_graph_flag), 0, &(args_info->main_graph_given), + &(local_args_info.main_graph_given), optarg, 0, 0, ARG_FLAG, + check_ambiguity, override, 1, 0, "main-graph", 'm', + additional_error)) + goto failure; + + break; + + case 0: /* Long option with no short option */ + /* Print vector graph.. */ + if (strcmp (long_options[option_index].name, "print-vec") == 0) + { + + + if (update_arg((void *)&(args_info->print_vec_flag), 0, &(args_info->print_vec_given), + &(local_args_info.print_vec_given), optarg, 0, 0, ARG_FLAG, + check_ambiguity, override, 1, 0, "print-vec", '-', + additional_error)) + goto failure; + + } + /* Print compact matrice graph.. */ + else if (strcmp (long_options[option_index].name, "print-mc") == 0) + { + + + if (update_arg((void *)&(args_info->print_mc_flag), 0, &(args_info->print_mc_given), + &(local_args_info.print_mc_given), optarg, 0, 0, ARG_FLAG, + check_ambiguity, override, 1, 0, "print-mc", '-', + additional_error)) + goto failure; + + } + /* Find and print cycles in a vector graph.. */ + else if (strcmp (long_options[option_index].name, "cycles-vec") == 0) + { + + + if (update_arg((void *)&(args_info->cycles_vec_flag), 0, &(args_info->cycles_vec_given), + &(local_args_info.cycles_vec_given), optarg, 0, 0, ARG_FLAG, + check_ambiguity, override, 1, 0, "cycles-vec", '-', + additional_error)) + goto failure; + + } + /* Find and print cycles in a compact matrice graph.. */ + else if (strcmp (long_options[option_index].name, "cycles-mc") == 0) + { + + + if (update_arg((void *)&(args_info->cycles_mc_flag), 0, &(args_info->cycles_mc_given), + &(local_args_info.cycles_mc_given), optarg, 0, 0, ARG_FLAG, + check_ambiguity, override, 1, 0, "cycles-mc", '-', + additional_error)) + goto failure; + + } + + break; + case '?': /* Invalid option. */ + /* `getopt_long' already printed an error message. */ + goto failure; + + default: /* bug: option not considered. */ + fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : "")); + abort (); + } /* switch */ + } /* while */ + + + + FIX_UNUSED(check_required); + + cmdline_parser_release (&local_args_info); + + if ( error_occurred ) + return (EXIT_FAILURE); + + return 0; + +failure: + + cmdline_parser_release (&local_args_info); + return (EXIT_FAILURE); +} +/* vim: set ft=c noet ts=8 sts=8 sw=8 tw=80 nojs spell : */ diff --git a/src/cmdline.ggo b/src/cmdline.ggo new file mode 100644 index 0000000..7a0be29 --- /dev/null +++ b/src/cmdline.ggo @@ -0,0 +1,13 @@ +version "1.0.1" +package "code-analyzer" +purpose "Analyze source code in a form of a graph. + +By Volodymyr Patuta " + +option "path" p "Path to the source-code directory." string optional +option "file" f "Path to the .c file." string optional +option "main-graph" m "Generate main graph separated from other CC." flag on +option "print-vec" - "Print vector graph." flag off +option "print-mc" - "Print compact matrice graph." flag off +option "cycles-vec" - "Find and print cycles in a vector graph." flag off +option "cycles-mc" - "Find and print cycles in a compact matrice graph." flag off diff --git a/src/cmdline.h b/src/cmdline.h new file mode 100644 index 0000000..eafea9d --- /dev/null +++ b/src/cmdline.h @@ -0,0 +1,196 @@ +/** @file cmdline.h + * @brief The header file for the command line option parser + * generated by GNU Gengetopt version 2.23 + * http://www.gnu.org/software/gengetopt. + * DO NOT modify this file, since it can be overwritten + * @author GNU Gengetopt */ + +#ifndef CMDLINE_H +#define CMDLINE_H + +/* If we use autoconf. */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include /* for FILE */ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifndef CMDLINE_PARSER_PACKAGE +/** @brief the program name (used for printing errors) */ +#define CMDLINE_PARSER_PACKAGE "code-analyzer" +#endif + +#ifndef CMDLINE_PARSER_PACKAGE_NAME +/** @brief the complete program name (used for help and version) */ +#define CMDLINE_PARSER_PACKAGE_NAME "code-analyzer" +#endif + +#ifndef CMDLINE_PARSER_VERSION +/** @brief the program version */ +#define CMDLINE_PARSER_VERSION "1.0.1" +#endif + +/** @brief Where the command line options are stored */ +struct gengetopt_args_info +{ + const char *help_help; /**< @brief Print help and exit help description. */ + const char *version_help; /**< @brief Print version and exit help description. */ + char * path_arg; /**< @brief Path to the source-code directory.. */ + char * path_orig; /**< @brief Path to the source-code directory. original value given at command line. */ + const char *path_help; /**< @brief Path to the source-code directory. help description. */ + char * file_arg; /**< @brief Path to the .c file.. */ + char * file_orig; /**< @brief Path to the .c file. original value given at command line. */ + const char *file_help; /**< @brief Path to the .c file. help description. */ + int main_graph_flag; /**< @brief Generate main graph separated from other CC. (default=on). */ + const char *main_graph_help; /**< @brief Generate main graph separated from other CC. help description. */ + int print_vec_flag; /**< @brief Print vector graph. (default=off). */ + const char *print_vec_help; /**< @brief Print vector graph. help description. */ + int print_mc_flag; /**< @brief Print compact matrice graph. (default=off). */ + const char *print_mc_help; /**< @brief Print compact matrice graph. help description. */ + int cycles_vec_flag; /**< @brief Find and print cycles in a vector graph. (default=off). */ + const char *cycles_vec_help; /**< @brief Find and print cycles in a vector graph. help description. */ + int cycles_mc_flag; /**< @brief Find and print cycles in a compact matrice graph. (default=off). */ + const char *cycles_mc_help; /**< @brief Find and print cycles in a compact matrice graph. help description. */ + + unsigned int help_given ; /**< @brief Whether help was given. */ + unsigned int version_given ; /**< @brief Whether version was given. */ + unsigned int path_given ; /**< @brief Whether path was given. */ + unsigned int file_given ; /**< @brief Whether file was given. */ + unsigned int main_graph_given ; /**< @brief Whether main-graph was given. */ + unsigned int print_vec_given ; /**< @brief Whether print-vec was given. */ + unsigned int print_mc_given ; /**< @brief Whether print-mc was given. */ + unsigned int cycles_vec_given ; /**< @brief Whether cycles-vec was given. */ + unsigned int cycles_mc_given ; /**< @brief Whether cycles-mc was given. */ + +} ; + +/** @brief The additional parameters to pass to parser functions */ +struct cmdline_parser_params +{ + int override; /**< @brief whether to override possibly already present options (default 0) */ + int initialize; /**< @brief whether to initialize the option structure gengetopt_args_info (default 1) */ + int check_required; /**< @brief whether to check that all required options were provided (default 1) */ + int check_ambiguity; /**< @brief whether to check for options already specified in the option structure gengetopt_args_info (default 0) */ + int print_errors; /**< @brief whether getopt_long should print an error message for a bad option (default 1) */ +} ; + +/** @brief the purpose string of the program */ +extern const char *gengetopt_args_info_purpose; +/** @brief the usage string of the program */ +extern const char *gengetopt_args_info_usage; +/** @brief the description string of the program */ +extern const char *gengetopt_args_info_description; +/** @brief all the lines making the help output */ +extern const char *gengetopt_args_info_help[]; + +/** + * The command line parser + * @param argc the number of command line options + * @param argv the command line options + * @param args_info the structure where option information will be stored + * @return 0 if everything went fine, NON 0 if an error took place + */ +int cmdline_parser (int argc, char **argv, + struct gengetopt_args_info *args_info); + +/** + * The command line parser (version with additional parameters - deprecated) + * @param argc the number of command line options + * @param argv the command line options + * @param args_info the structure where option information will be stored + * @param override whether to override possibly already present options + * @param initialize whether to initialize the option structure my_args_info + * @param check_required whether to check that all required options were provided + * @return 0 if everything went fine, NON 0 if an error took place + * @deprecated use cmdline_parser_ext() instead + */ +int cmdline_parser2 (int argc, char **argv, + struct gengetopt_args_info *args_info, + int override, int initialize, int check_required); + +/** + * The command line parser (version with additional parameters) + * @param argc the number of command line options + * @param argv the command line options + * @param args_info the structure where option information will be stored + * @param params additional parameters for the parser + * @return 0 if everything went fine, NON 0 if an error took place + */ +int cmdline_parser_ext (int argc, char **argv, + struct gengetopt_args_info *args_info, + struct cmdline_parser_params *params); + +/** + * Save the contents of the option struct into an already open FILE stream. + * @param outfile the stream where to dump options + * @param args_info the option struct to dump + * @return 0 if everything went fine, NON 0 if an error took place + */ +int cmdline_parser_dump(FILE *outfile, + struct gengetopt_args_info *args_info); + +/** + * Save the contents of the option struct into a (text) file. + * This file can be read by the config file parser (if generated by gengetopt) + * @param filename the file where to save + * @param args_info the option struct to save + * @return 0 if everything went fine, NON 0 if an error took place + */ +int cmdline_parser_file_save(const char *filename, + struct gengetopt_args_info *args_info); + +/** + * Print the help + */ +void cmdline_parser_print_help(void); +/** + * Print the version + */ +void cmdline_parser_print_version(void); + +/** + * Initializes all the fields a cmdline_parser_params structure + * to their default values + * @param params the structure to initialize + */ +void cmdline_parser_params_init(struct cmdline_parser_params *params); + +/** + * Allocates dynamically a cmdline_parser_params structure and initializes + * all its fields to their default values + * @return the created and initialized cmdline_parser_params structure + */ +struct cmdline_parser_params *cmdline_parser_params_create(void); + +/** + * Initializes the passed gengetopt_args_info structure's fields + * (also set default values for options that have a default) + * @param args_info the structure to initialize + */ +void cmdline_parser_init (struct gengetopt_args_info *args_info); +/** + * Deallocates the string fields of the gengetopt_args_info structure + * (but does not deallocate the structure itself) + * @param args_info the structure to deallocate + */ +void cmdline_parser_free (struct gengetopt_args_info *args_info); + +/** + * Checks that all the required options were specified + * @param args_info the structure to check + * @param prog_name the name of the program that will be used to print + * possible errors + * @return + */ +int cmdline_parser_required (struct gengetopt_args_info *args_info, + const char *prog_name); + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* CMDLINE_H */ diff --git a/src/colors.h b/src/colors.h new file mode 100644 index 0000000..4fdbe3f --- /dev/null +++ b/src/colors.h @@ -0,0 +1,14 @@ +#ifndef COLORS_H + +#define COLORS_H + +#define RED "\x1b[31m" +#define GREEN "\x1b[32m" +#define YELLOW "\x1b[33m" +#define BLUE "\x1b[34m" +#define MAGENTA "\x1b[35m" +#define CYAN "\x1b[36m" +#define WHITE "\x1b[37m" +#define RESET_COLOR "\x1b[0m" + +#endif /* end of include guard: COLORS_H */ diff --git a/src/main.c b/src/main.c index 266fc89..3a2fcc3 100644 --- a/src/main.c +++ b/src/main.c @@ -14,49 +14,82 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#include "cmdline.h" +#include "colors.h" #include "cparse.h" #include "matcom.h" #include "vec.h" #include +#include #include int main(int argc, char *argv[]) { - (void)argc; - (void)argv; - graph_vec gv = vnew_graph(); - graph_mco gmc = mcgragh(); - // find_funcs("/home/user/files/dot_files/st/x.c", &gv, &gmc); - find_funcs("test.c", &gv, &gmc); - // findAndParse("/home/user/files/dot_files/st/", &gv, &gmc); - // for (int i = 0; i <= gmc.nba; ++i) { - // printf("%d.%s -> %d.%s\n", gmc.vec[i].i, gmc.func[gmc.vec[i].i], gmc.vec[i].j, gmc.func[gmc.vec[i].j]); - // } - // puts("\n"); - - for (int i = 0; i <= gmc.nbs; ++i) { - if (!gmc.call[i]) { - mcprint(gmc, i); - puts("\n"); - } - } - for (int i = 0; i <= gv.n; ++i) { - if (!gv.node[i]->call) { - vprint(gv.node[i], &gv); - puts("\n"); - } - } - // vprint(gv.node[8], &gv); - // mcprint(gmc, 8); - // - // vfind_cycles(&gv); - // mcfind_cycles(&gmc); + struct gengetopt_args_info ai; + char *path; + graph_vec gv; + graph_mco gmc; + int i, n; - vdot_graph(gv, true); - mcdot_graph(gmc, true); + if (cmdline_parser(argc, argv, &ai) != 0) { + exit(1); + } + if (ai.path_given && ai.file_given) { + fprintf(stderr, "Cannot use -p and -f together.\n"); + exit(2); + } + if (!ai.path_given && !ai.file_given) { + fprintf(stderr, "You have to give a -p or -f option.\n"); + exit(2); + } + + gv = vnew_graph(); + gmc = mcgragh(); + + if (ai.path_given) { + path = ai.path_arg; + findAndParse(path, &gv, &gmc); + } + if (ai.file_given) { + path = ai.file_arg; + find_funcs(path, &gv, &gmc); + } + + if (ai.print_vec_flag) { + n = 0; + puts("Printing with vector:\n"); + for (i = 0; i <= gv.n; ++i) { + if (!gv.node[i]->call) { + printf(GREEN "CC %d:\n" RESET_COLOR, n++); + vprint(gv.node[i], &gv); + puts(""); + } + } + } + + if (ai.print_mc_flag) { + n = 0; + puts("Printing with compact matrice:"); + for (i = 0; i <= gmc.nbs; ++i) { + if (!gmc.call[i]) { + printf(GREEN "CC %d:\n" RESET_COLOR, n++); + mcprint(gmc, i); + puts(""); + } + } + } + + if (ai.cycles_mc_flag) + mcfind_cycles(&gmc); + if (ai.cycles_vec_flag) + vfind_cycles(&gv); + + vdot_graph(gv, ai.main_graph_flag); + mcdot_graph(&gmc, ai.main_graph_flag); mcdelete_graph(&gmc); vdelete_graph(&gv); + cmdline_parser_free(&ai); return 0; } diff --git a/src/matcom.c b/src/matcom.c index bce301e..60da178 100644 --- a/src/matcom.c +++ b/src/matcom.c @@ -1,3 +1,4 @@ +#include "colors.h" #include "matcom.h" #include "stack.h" #include @@ -14,7 +15,7 @@ graph_mco mcgragh(void) g.nba = -1; g.func = malloc((g.fcap = 10) * sizeof(fix_str)); assert(g.func); - g.call = malloc((g.fcap = 10) * sizeof(bool)); + g.call = calloc((g.fcap = 10), sizeof(bool)); assert(g.call); g.vec = malloc((g.vcap = 10) * sizeof(struct edge)); assert(g.vec); @@ -61,10 +62,13 @@ void mcdelete_graph(graph_mco *g) free(g->vec); if (g->vu) free(g->vu); + if (g->call) + free(g->call); g->func = NULL; g->vec = NULL; g->vu = NULL; + g->call = NULL; } int mcis_in(graph_mco g, fix_str func) @@ -82,7 +86,7 @@ int mcis_in(graph_mco g, fix_str func) static void mcprint_succ(graph_mco g, int succ) { - printf(" Succ: %s\n", g.func[succ]); + printf(YELLOW " Succ:" RESET_COLOR CYAN " %s"RESET_COLOR"\n" , g.func[succ]); } static void mcprint_edges(graph_mco g, int n) @@ -91,7 +95,7 @@ static void mcprint_edges(graph_mco g, int n) if (g.vu[n]) return; g.vu[n] = 1; - printf("NODE: %s\n", g.func[n]); + printf(BLUE "NODE:" RESET_COLOR MAGENTA " %s\n" RESET_COLOR, g.func[n]); for (i = 0; i <= g.nba; ++i) { if (g.vec[i].i == n) { j++; @@ -115,8 +119,8 @@ static void mcprint_edges(graph_mco g, int n) void mcprint(graph_mco g, int n) { if (g.vu == NULL) { - g.vu = calloc(g.nbs, sizeof(int)); - } + g.vu = calloc(g.nbs + 1, sizeof(int)); + } mcprint_edges(g, n); @@ -147,16 +151,16 @@ static void main_graph(FILE *fp, graph_mco g, int n) } } -void mcdot_graph(graph_mco g, bool main) +void mcdot_graph(graph_mco *g, bool main) { FILE *fp, *fp1; int i; - if (g.vu == NULL) { - g.vu = calloc(g.nbs, sizeof(int)); + if (g->vu == NULL) { + g->vu = calloc(g->nbs + 1, sizeof(int)); } else { - for (i = 0; i <= g.nbs; i++) { - g.vu[i] = 0; + for (i = 0; i <= g->nbs; i++) { + g->vu[i] = 0; } } @@ -168,21 +172,21 @@ void mcdot_graph(graph_mco g, bool main) if (main) { fix_str m = "main"; - int n = mcis_in(g, m); + int n = mcis_in(*g, m); if (n != -1) { - main_graph(fp, g, n); + main_graph(fp, *g, n); } else { fprintf(stderr, "main not found.\n"); exit(1); } - for (i = 0; i <= g.nbs; ++i) { - if (!g.vu[i]) { - main_graph(fp1, g, i); + for (i = 0; i <= g->nbs; ++i) { + if (!g->vu[i]) { + main_graph(fp1, *g, i); } } } else { - write_node_dot(fp, g); + write_node_dot(fp, *g); } fprintf(fp, "}"); diff --git a/src/matcom.h b/src/matcom.h index 51f0f2c..2aeed0d 100644 --- a/src/matcom.h +++ b/src/matcom.h @@ -29,7 +29,7 @@ void mcinsert_edge(graph_mco *g, int dep, int arr); void mcdelete_graph(graph_mco *g); int mcis_in(graph_mco g, fix_str func); void mcprint(graph_mco g, int n); -void mcdot_graph(graph_mco g, bool main); +void mcdot_graph(graph_mco *g, bool main); void mcfind_cycles(graph_mco *g); #endif /* end of include guard: MATCOM_H */ diff --git a/src/vec.c b/src/vec.c index e0ef5be..6f7bd79 100644 --- a/src/vec.c +++ b/src/vec.c @@ -1,3 +1,4 @@ +#include "colors.h" #include "stack.h" #include "vec.h" #include @@ -67,7 +68,7 @@ int vis_in(graph_vec g, fix_str func) static void vprint_succ(nodept node) { - printf(" Succ: %s\n", node->func); + printf(YELLOW " Succ:" RESET_COLOR CYAN "%s\n" RESET_COLOR, node->func); } static void vprint_nodes(nodept node) @@ -76,7 +77,7 @@ static void vprint_nodes(nodept node) if (node->vu) return; node->vu = 1; - printf("NODE: %s\n", node->func); + printf(BLUE "NODE:" RESET_COLOR MAGENTA" %s\n" RESET_COLOR, node->func); for (i = 0; i < node->nbs; ++i) { vprint_succ(node->succ[i]); } @@ -144,7 +145,7 @@ void vdot_graph(graph_vec g, bool main) exit(1); } for (i = 0; i <= g.n; ++i) { - if (!g.node[i]->vu) { + if (!g.node[i]->call) { main_graph(fp1, g.node[i]); } }