Example #1
0
static void
log_c_test (const char *filename, const char *action)
{
  char *source;
  char **lines;
  int i;

  source = jb_read_file_or_exit(filename);
  lines = g_strsplit(source, "\n", 0);
  g_free(source);

  jb_log("attempting to %s program %s:", action, filename);

  for (i = 0; lines[i] != NULL; i++)
    {
      const char *line = lines[i];

      /* do not output a spurious empty last line */
      if (*line == '\0' && lines[i + 1] == NULL)
	break;

      jb_log("%4i %s", i + 1, lines[i]);
    }

  jb_log(JB_SEPARATOR);

  g_strfreev(lines);
}
Example #2
0
void
jb_message_result_string (const char *result)
{
  g_return_if_fail(result != NULL);
  g_return_if_fail(printing_action);

  g_print(" %s\n", result);
  jb_log("result: %s", result);
  jb_log("");

  printing_action = FALSE;
}
Example #3
0
print_warning_or_error (const char *prefix, const char *format, va_list args)
{
  char *message;
  char **lines;
  int i;

  /*
   * We allow to interrupt an action print, in case the caller is a
   * library function which does not know that an action is in
   * progress.
   */
  finish_printing_action();

  message = g_strdup_vprintf(format, args);
  lines = g_strsplit(message, "\n", 0);
  g_free(message);

  for (i = 0; lines[i] != NULL; i++)
    {
      const char *line = lines[i];

      g_printerr("%s: %s\n", prefix, line);
      jb_log("%s: %s", prefix, line);
    }

  g_strfreev(lines);
}
Example #4
0
void
jb_message (const char *format, ...)
{
  va_list args;
  char *message;

  g_return_if_fail(format != NULL);
  g_return_if_fail(! printing_action);

  va_start(args, format);
  message = g_strdup_vprintf(format, args);
  va_end(args);

  g_print("%s\n", message);
  jb_log("%s", message);

  g_free(message);
}
Example #5
0
/*
 * Returns TRUE if the command exited with status 0.
 *
 * The trailing newline of @standard_output and @standard_error will
 * be stripped.
 *
 * @standard_output and @standard_error will be set even if FALSE is
 * returned, since a command can produce an output even if it exits
 * with a non-zero status.
 */
gboolean
jb_exec (char **standard_output,
	 char **standard_error,
	 const char *format,
	 ...)
{
  char *command;
  va_list args;
  int command_status;
  gboolean status = FALSE;
  char *_stdout;
  char *_stderr;
  char *converted_stdout = NULL;
  char *converted_stderr = NULL;
  char *shell_argv[4];
  GError *err = NULL;

  g_return_val_if_fail(format != NULL, FALSE);

  va_start(args, format);
  command = g_strdup_vprintf(format, args);
  va_end(args);

  shell_argv[0] = "/bin/sh";
  shell_argv[1] = "-c";
  shell_argv[2] = command;
  shell_argv[3] = NULL;

  if (g_spawn_sync(NULL,
		   shell_argv,
		   NULL,
		   G_SPAWN_SEARCH_PATH,
		   NULL,
		   NULL,
		   &_stdout,
		   &_stderr,
		   &command_status,
		   &err))
    {
      converted_stdout = convert_process_output(_stdout);
      g_free(_stdout);

      converted_stderr = convert_process_output(_stderr);
      g_free(_stderr);

      if (WIFEXITED(command_status))
	{
	  int exit_status;

	  exit_status = WEXITSTATUS(command_status);
	  if (exit_status == 0)
	    {
	      jb_log("command \"%s\" succeeded", command);
	      status = TRUE;
	    }
	  else
	    jb_log("command \"%s\" failed with status %i", command, exit_status);

	  if (*converted_stdout != '\0')
	    {
	      jb_log("standard output:");
	      jb_log("%s", converted_stdout);
	      jb_log(JB_SEPARATOR);
	    }
	  if (*converted_stderr != '\0')
	    {
	      jb_log("standard error output:");
	      jb_log("%s", converted_stderr);
	      jb_log(JB_SEPARATOR);
	    }
	}
      else
	jb_log("command exited abnormally");
    }
  else
    /* fatal error: it should not happend since we exec the shell */
    jb_error("cannot execute command \"%s\": %s", command, err->message);

  g_free(command);

  if (standard_output)
    *standard_output = converted_stdout;
  else
    g_free(converted_stdout);

  if (standard_error)
    *standard_error = converted_stderr;
  else
    g_free(converted_stderr);

  return status;
}