main.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* main.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2018/09/20 23:08:43 by bchanot #+# #+# */
  9. /* Updated: 2018/10/26 10:54:30 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "ft_ssl.h"
  13. static void sl_free_inf(t_inf *inf)
  14. {
  15. t_files *tmp;
  16. while (inf->files)
  17. {
  18. tmp = inf->files->next;
  19. ft_memdel((void **)&inf->files->string);
  20. ft_memdel((void **)&inf->files->name);
  21. ft_memdel((void **)&inf->files);
  22. inf->files = tmp;
  23. tmp = NULL;
  24. }
  25. ft_memdel2((void ***)&inf->opts);
  26. }
  27. static void sl_error(int state, char *str)
  28. {
  29. const char *(hash[NB_HASH + 1]) = {HASH_TAB, NULL};
  30. size_t i;
  31. i = 0;
  32. if (state == 1)
  33. ft_printf(2, "Usage: ft_ssl command [command opts] [command args]");
  34. else if (state == 2)
  35. {
  36. ft_printf(2, "ft_ssl: Error: '%s' is an invalid command.\n\n\
  37. Standard commands:\n\nMessage Digest commands:\n", str);
  38. while (hash[i])
  39. {
  40. ft_printf(2, "%s\n", hash[i]);
  41. i++;
  42. }
  43. ft_printf(2, "\nCipher commands:\n");
  44. }
  45. }
  46. static char **sl_get_args(t_bool in, char **av)
  47. {
  48. char *line;
  49. char *tmp;
  50. char **args;
  51. if (in)
  52. {
  53. line = NULL;
  54. ft_printf(1, "ft_SSL> ");
  55. get_next_line(0, &line);
  56. if (!line)
  57. return (NULL);
  58. tmp = ft_strtrim(line);
  59. ft_memdel((void **)&line);
  60. args = ft_strsplit(tmp, ' ');
  61. ft_memdel((void **)&tmp);
  62. return (args);
  63. }
  64. return (av + 1);
  65. }
  66. static int sl_start(t_bool in, char **args, int ac, t_int8 *ret)
  67. {
  68. t_inf inf;
  69. ft_bzero(&inf, sizeof(t_inf));
  70. *ret = sl_get_inf(&inf, in ? (int)ft_tablen(args) : ac - 1, args);
  71. if (!(*ret))
  72. {
  73. sl_choose_hash(&inf);
  74. sl_prepare_launch(&inf);
  75. }
  76. else
  77. sl_error(*ret, in ? args[0] : NULL);
  78. sl_free_inf(&inf);
  79. return (in ? 1 : 0);
  80. }
  81. int main(int ac, char **av)
  82. {
  83. t_int8 ret;
  84. t_bool in;
  85. char **args;
  86. in = ac == 1 ? true : false;
  87. while (true)
  88. {
  89. if (!(args = sl_get_args(in, av)))
  90. break ;
  91. if (!sl_start(in, args, ac, &ret))
  92. break ;
  93. if (in)
  94. ft_memdel2((void ***)&args);
  95. }
  96. return (ret);
  97. }