Fix MSVC errors and warnings
This commit is contained in:
		@@ -10,13 +10,10 @@
 | 
			
		||||
#define CMD_BUF_LEN 8192
 | 
			
		||||
#define OUT_BUF_LEN 4096
 | 
			
		||||
 | 
			
		||||
internal inline CMDError build_command_from_args(char *cmd, u64 buf_len,
 | 
			
		||||
                                                 va_list args);
 | 
			
		||||
internal inline CMDResult execute_command(const char *cmd,
 | 
			
		||||
                                          CMDOutHandling out_handling,
 | 
			
		||||
internal inline CMDError build_command_from_args(char *cmd, u64 buf_len, va_list args);
 | 
			
		||||
internal inline CMDResult execute_command(const char *cmd, CMDOutHandling out_handling,
 | 
			
		||||
                                          char *out_buf, u64 buf_size);
 | 
			
		||||
internal inline CMDError get_command_output(FILE *fp,
 | 
			
		||||
                                            CMDOutHandling out_handling,
 | 
			
		||||
internal inline CMDError get_command_output(FILE *fp, CMDOutHandling out_handling,
 | 
			
		||||
                                            char *out_buf, u64 buf_size);
 | 
			
		||||
internal inline CMDError get_output_status(FILE *fp, i32 *status_out);
 | 
			
		||||
 | 
			
		||||
@@ -42,8 +39,8 @@ internal inline CMDError build_command_from_args(char *cmd, u64 buf_len,
 | 
			
		||||
  u64 size = 0;
 | 
			
		||||
  u64 arg_len = 0;
 | 
			
		||||
 | 
			
		||||
  const char *arg;
 | 
			
		||||
  while ((arg = va_arg(args, const char *))) {
 | 
			
		||||
  const char *arg = va_arg(args, const char *);
 | 
			
		||||
  while (arg) {
 | 
			
		||||
    arg_len = strlen(arg);
 | 
			
		||||
    if (arg_len >= buf_len - size) {
 | 
			
		||||
      return SHELL_ERR_CMD_BUF_FULL;
 | 
			
		||||
@@ -53,6 +50,8 @@ internal inline CMDError build_command_from_args(char *cmd, u64 buf_len,
 | 
			
		||||
    cmd[size + arg_len] = ' ';
 | 
			
		||||
 | 
			
		||||
    size += arg_len + 1;
 | 
			
		||||
 | 
			
		||||
    arg = va_arg(args, const char *);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return SHELL_ERR_NO_ERROR;
 | 
			
		||||
@@ -66,24 +65,35 @@ internal inline CMDResult execute_command(const char *cmd,
 | 
			
		||||
    return CMD_NO_EXIT(SHELL_ERR_PROC_START_FAIL);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  CMDResult output;
 | 
			
		||||
 | 
			
		||||
  CMDError err = get_command_output(fp, out_handling, out_buf, buf_size);
 | 
			
		||||
  if (err > SHELL_ERR_NO_ERROR) {
 | 
			
		||||
    // Ensure process is closed on failure
 | 
			
		||||
    wapp_shell_utils_pclose(fp);
 | 
			
		||||
    return CMD_NO_EXIT(err);
 | 
			
		||||
    output = CMD_NO_EXIT(err);
 | 
			
		||||
    goto EXECUTE_COMMAND_CLOSE;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  i32 st = EXIT_SUCCESS;
 | 
			
		||||
  err = get_output_status(fp, &st);
 | 
			
		||||
  if (err > SHELL_ERR_NO_ERROR) {
 | 
			
		||||
    return CMD_NO_EXIT(err);
 | 
			
		||||
    output = CMD_NO_EXIT(err);
 | 
			
		||||
    goto EXECUTE_COMMAND_CLOSE;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return (CMDResult){
 | 
			
		||||
  // Process is already closed in get_output_status
 | 
			
		||||
  fp = NULL;
 | 
			
		||||
 | 
			
		||||
  output = (CMDResult){
 | 
			
		||||
      .exited = true,
 | 
			
		||||
      .exit_code = st,
 | 
			
		||||
      .error = SHELL_ERR_NO_ERROR,
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
EXECUTE_COMMAND_CLOSE:
 | 
			
		||||
  if (fp) {
 | 
			
		||||
    wapp_shell_utils_pclose(fp);
 | 
			
		||||
  }
 | 
			
		||||
  return output;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
internal inline CMDError get_command_output(FILE *fp,
 | 
			
		||||
@@ -93,7 +103,7 @@ internal inline CMDError get_command_output(FILE *fp,
 | 
			
		||||
  u64 max_out_length = OUT_BUF_LEN - 1;
 | 
			
		||||
 | 
			
		||||
  u64 buf_filled = 0;
 | 
			
		||||
  while (fgets(out, max_out_length, fp)) {
 | 
			
		||||
  while (fgets(out, (i32)max_out_length, fp)) {
 | 
			
		||||
    if (out_handling == SHELL_OUTPUT_CAPTURE && out_buf != NULL) {
 | 
			
		||||
      buf_filled += strlen(out);
 | 
			
		||||
      if (buf_filled >= buf_size) {
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@
 | 
			
		||||
#define COMMANDER_H
 | 
			
		||||
 | 
			
		||||
#include "aliases.h"
 | 
			
		||||
#include "platform.h"
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
@@ -30,9 +31,14 @@ typedef enum {
 | 
			
		||||
 | 
			
		||||
typedef struct commander_result CMDResult;
 | 
			
		||||
struct commander_result {
 | 
			
		||||
  bool exited;
 | 
			
		||||
  int exit_code;
 | 
			
		||||
  i32 exit_code;
 | 
			
		||||
  CMDError error;
 | 
			
		||||
  bool exited;
 | 
			
		||||
 | 
			
		||||
#ifdef WAPP_PLATFORM_WINDOWS
 | 
			
		||||
  #include "misc_utils.h"
 | 
			
		||||
  wapp_misc_utils_padding_size(sizeof(bool) + sizeof(i32) + sizeof(CMDError));
 | 
			
		||||
#endif // !WAPP_PLATFORM_WINDOWS
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// clang-format off
 | 
			
		||||
 
 | 
			
		||||
@@ -20,7 +20,7 @@ struct termcolour_data {
 | 
			
		||||
  wapp_misc_utils_padding_size(sizeof(HANDLE) + sizeof(WORD) + sizeof(WORD));
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
internal TermcolourData init_data(void);
 | 
			
		||||
internal void init_data(TermcolourData *data);
 | 
			
		||||
 | 
			
		||||
internal WORD colours[COUNT_TERM_COLOUR] = {
 | 
			
		||||
    [WAPP_TERM_COLOUR_FG_BLACK]      = 0,
 | 
			
		||||
@@ -42,31 +42,30 @@ internal WORD colours[COUNT_TERM_COLOUR] = {
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void print_coloured_text(const char *text, TerminalColour colour) {
 | 
			
		||||
  persistent TermcolourData data = init_data();
 | 
			
		||||
 | 
			
		||||
  if (color == WAPP_TERM_COLOUR_CLEAR) {
 | 
			
		||||
    data->current_colour = data->default_colour;
 | 
			
		||||
  } else {
 | 
			
		||||
    data->current_colour = colours[colour];
 | 
			
		||||
  persistent TermcolourData data = {0};
 | 
			
		||||
  if (data.handle == 0) {
 | 
			
		||||
    init_data(&data);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  SetConsoleTextAttribute(data->handle, data->current_colour);
 | 
			
		||||
  if (colour == WAPP_TERM_COLOUR_CLEAR) {
 | 
			
		||||
    data.current_colour = data.default_colour;
 | 
			
		||||
  } else {
 | 
			
		||||
    data.current_colour = colours[colour];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  SetConsoleTextAttribute(data.handle, data.current_colour);
 | 
			
		||||
  printf("%s", text);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
internal TermcolourData init_data(void) {
 | 
			
		||||
  TermcolourData data = {0};
 | 
			
		||||
 | 
			
		||||
internal void init_data(TermcolourData *data) {
 | 
			
		||||
  // create handle
 | 
			
		||||
  data.handle = GetStdHandle(STD_OUTPUT_HANDLE);
 | 
			
		||||
  data->handle = GetStdHandle(STD_OUTPUT_HANDLE);
 | 
			
		||||
 | 
			
		||||
  // get console colour information
 | 
			
		||||
  CONSOLE_SCREEN_BUFFER_INFO csbi;
 | 
			
		||||
  GetConsoleScreenBufferInfo(data.handle, &csbi);
 | 
			
		||||
  data.default_colour = csbi.wAttributes;
 | 
			
		||||
  data.current_colour = data.default_colour;
 | 
			
		||||
 | 
			
		||||
  return data;
 | 
			
		||||
  GetConsoleScreenBufferInfo(data->handle, &csbi);
 | 
			
		||||
  data->default_colour = csbi.wAttributes;
 | 
			
		||||
  data->current_colour = data->default_colour;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif // !WAPP_PLATFORM_WINDOWS
 | 
			
		||||
 
 | 
			
		||||
@@ -2,15 +2,14 @@
 | 
			
		||||
#define SHELL_UTILS_H
 | 
			
		||||
 | 
			
		||||
#include "platform.h"
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
// clang-format off
 | 
			
		||||
#ifdef WAPP_PLATFORM_WINDOWS
 | 
			
		||||
  #define wapp_shell_utils_popen _popen
 | 
			
		||||
  #define wapp_shell_utils_popen  _popen
 | 
			
		||||
  #define wapp_shell_utils_pclose _pclose
 | 
			
		||||
#else
 | 
			
		||||
  #define wapp_shell_utils_popen popen
 | 
			
		||||
  #define wapp_shell_utils_popen  popen
 | 
			
		||||
  #define wapp_shell_utils_pclose pclose
 | 
			
		||||
#endif /* ifdef WAPP_PLATFORM_WINDOWS */
 | 
			
		||||
// clang-format on
 | 
			
		||||
 | 
			
		||||
#endif // !SHELL_UTILS_H
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user