Beispiel #1
0
static void
replay_to_json (gint64      time,
                const char *name,
                const char *signature,
                GValue     *arg,
                gpointer    user_data)
{
  ReplayToJsonClosure *closure = user_data;
  char *event_str;

  if (closure->error != NULL)
    return;

  if (!closure->first)
    {
      if (!write_string (closure->out, ",\n  ", &closure->error))
        return;
    }

  closure->first = FALSE;

  if (strcmp (signature, "") == 0)
    {
      event_str = g_strdup_printf ("[%" G_GINT64_FORMAT ", \"%s\"]", time, name);
    }
  else if (strcmp (signature, "i") == 0)
    {
      event_str = g_strdup_printf ("[%" G_GINT64_FORMAT ", \"%s\", %i]",
                                   time,
                                   name,
                                   g_value_get_int (arg));
    }
  else if (strcmp (signature, "x") == 0)
    {
      event_str = g_strdup_printf ("[%" G_GINT64_FORMAT ", \"%s\", %"G_GINT64_FORMAT "]",
                                   time,
                                   name,
                                   g_value_get_int64 (arg));
    }
  else if (strcmp (signature, "s") == 0)
    {
      const char *arg_str = g_value_get_string (arg);
      char *escaped = escape_quotes (arg_str);

      event_str = g_strdup_printf ("[%" G_GINT64_FORMAT ", \"%s\", \"%s\"]",
                                   time,
                                   name,
                                   g_value_get_string (arg));

      if (escaped != arg_str)
        g_free (escaped);
    }
  else
    {
      g_assert_not_reached ();
    }

  if (!write_string (closure->out, event_str, &closure->error))
      return;
}
Beispiel #2
0
void llog(const loglevel_t level, ...) {
  FILE* outfile = stdout;
  va_list vargs; va_start(vargs, level);
  time_t now; time(&now);
  char* current;
  int i = 0;
  if (strcmp(hostname, "localhost") == 0) gethostname(hostname, 256);
  if (log_json) {
    fprintf(outfile, "{\"version\":\"1.1\",\"host\":\"ljspawn@%s\",\"timestamp\":%ld,\"level\":%d", hostname, now, level);
    char* prev = malloc(1);
    while ((current = va_arg(vargs, char*)) != NULL) {
      i++;
      char* new_current = escape_quotes(current);
      if (i % 2 == 0) {
        if (strcmp(prev, "jid") == 0 || strcmp(prev, "status") == 0) {
          fprintf(outfile, "%s", new_current);
        } else {
          fprintf(outfile, "\"%s\"", new_current);
        }
      } else {
        if (strcmp(new_current, "message") == 0) {
          fprintf(outfile, ",\"short_message\":");
        } else {
          fprintf(outfile, ",\"_%s\":", new_current);
        }
      }
      free(prev);
      prev = malloc(strlen(new_current));
      memmove(prev, new_current, strlen(new_current) + 1);
      free(new_current);
    }
    if (i % 2 != 0) fprintf(outfile, "\"\"");
    fprintf(outfile, "}");
  } else {
Beispiel #3
0
std::string get_process_json(const Process& process, bool prettyprint)
{
    std::stringstream ss;
    ss << "{";

    if (prettyprint) ss << "\n ";

    ss << "\"cmdline\":";
    ss << "\"" << escape_quotes(process.cmdline) << "\"";
    ss << ",";

    if (prettyprint) ss << "\n ";

    ss << "\"uid\":";
    ss << process.userid;
    ss << ",";

    if (prettyprint) ss << "\n ";

    ss << "\"pid\":";
    ss << process.processid;
    ss << ",";

    if (prettyprint) ss << "\n ";

    ss << "\"wchan\":";
    ss << "\"" << process.wchan << "\"";

    if (prettyprint)
        ss << "\n";

    ss << "}";

    return ss.str();
}
Beispiel #4
0
void run(int type)
{
    escape_quotes(plugin->irc->message.trailing);
    if (type == PLUGIN_TYPE_COMMAND) {
        handle_command();
    } else if (type == PLUGIN_TYPE_GREP) {
        handle_grep();
    }
}
char *error_to_json(const spidermonkey_error *error) {
  /* Allocate 1K to build error (hopefully that's enough!) */
  int size = sizeof(char) * 1024;
  char *retval = (char *) driver_alloc(size);
  char *escaped_source = escape_quotes(error->offending_source);
  snprintf(retval, size, "{\"error\": {\"lineno\": %d, \"message\": \"%s\", \"source\": \"%s\"}}", error->lineno,
	   error->msg, escaped_source);
  driver_free(escaped_source);
  return retval;
}
char *error_to_json(const jaegermonkey_error *error) {
  char *escaped_source = escape_quotes(error->offending_source);
  int size = (strlen(escaped_source) + strlen(error->msg) + 128) * sizeof(char);
  char *retval = (char *) driver_alloc(size);

  snprintf(retval, size, "{\"error\": {\"lineno\": %d, \"message\": \"%s\", \"source\": \"%s\"}}", error->lineno,
	   error->msg, escaped_source);
  driver_free(escaped_source);
  return retval;
}
Beispiel #7
0
char *error_to_json(const spidermonkey_error *error) {
  char *escaped_source = escape_quotes(error->offending_source);
  /* size = length(escaped source) + length(error msg) + JSON formatting */
  size_t size = strlen(escaped_source) + strlen(error->msg) + 80;
  char *retval = ejs_alloc(size);

  snprintf(retval, size, "{\"error\": {\"lineno\": %d, \"message\": \"%s\", \"source\": \"%s\"}}",
           error->lineno, error->msg, escaped_source);
  driver_free(escaped_source);
  return retval;
}
Beispiel #8
0
int
mkcccmd(char *buf, int argc, char *argv[])
{
	char *bp = buf;
	char **args = argv;

	bp += sprintf(bp, arg_array[ARG_CC]);

	while (argc) {
		if (strcmp(*args, "-g") == 0) {
			bp += sprintf(bp, arg_array[ARG_DEBUG]);
		} else if (strcmp(*args, "-W1") == 0) {
			bp += sprintf(bp, arg_array[ARG_WARN1]);
		} else if (strcmp(*args, "-W2") == 0) {
			bp += sprintf(bp, arg_array[ARG_WARN2]);
		} else if (strncmp(*args, "-std=", 5) == 0) {
			static const char *stds[] = { "c99", "c89", NULL };
			int std = 0;

			while (stds[std] && strcmp(*args + 5, stds[std])) {
				std++;
			}
			if (stds[std] != NULL) {
				bp += sprintf(bp, arg_array[ARG_C99 + std]);
			} else { /* just copy */
				fprintf(stderr, "Argument '%s' not translated - just copying ...\n", *args);
				bp += sprintf(bp, " %s", *args);
			}
		} else if (strcmp(*args, "-fpic") == 0) {
			bp += sprintf(bp, arg_array[ARG_PIC1]);
		} else if (strcmp(*args, "-fPIC") == 0) {
			bp += sprintf(bp, arg_array[ARG_PIC2]);
		} else if (strcmp(*args, "-rpath") == 0) {
			args++; argc--;
			if (!argc) {
				ERR(EINVAL, "-rpath requires an argument");
				return -1;
			}
			bp += sprintf(bp, arg_array[ARG_RPATH], *args);
		} else {
			/* Just copy the argument */
			bp += sprintf(bp, " %s", escape_quotes(*args));
		}
		args++; argc--;
	}

	return bp - buf;
}
Beispiel #9
0
int				replace_dollars(char **str)
{
	int		i;

	i = 0;
	while ((*str)[i])
	{
		if ((*str)[i] == 0x27)
			escape_quotes(*str, &i, (*str)[i]);
		else if ((*str)[i] == '$')
			replace_dollar(str, &i);
		else if ((*str)[i] != '\0')
			i = ((*str)[i] == 0x5c) ? i + 2 : i + 1;
	}
	return (0);
}
Beispiel #10
0
char* word_to_string(word_t* wd, BOOL ghil)
{
	char* first_word = strdup(wd->string);
	char* ret;
	DIE(!first_word, "No memory in word_to_string()");

	if (wd->expand) {
		free(first_word);
		first_word = ExpandVariable(wd->string);
	}
	if (ghil) {
		char* tmp;
		ret = calloc(strlen(first_word) + 5, 1); 
		DIE(!ret, "No memory in word_to_string()");

		tmp = first_word;
		first_word = escape_quotes(first_word);
		free(tmp);

		_snprintf_s(ret, strlen(first_word) + 5, strlen(first_word) + 5, "\"%s\"", first_word);
		free(first_word);
	}
	else 
		ret = first_word;
	if (wd->next_word) {
		char* next_word = word_to_string(wd->next_word, ghil);
		DIE(!next_word, "No memory in word_to_string()");
		append(&ret, next_word);
		free(next_word);
	}

	if (wd->next_part) {
		char* next_part = word_to_string(wd->next_part, FALSE);
		char* tmp = malloc(strlen(ret) + strlen(next_part) + 1);
		if (!strcmp(next_part, "="))
			printf("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n");

		memcpy(tmp, ret, strlen(ret));
		memcpy(tmp + strlen(ret), next_part, strlen(next_part) + 1);
		free(next_part);
		ret = tmp;
	}

	return ret;
}
Beispiel #11
0
/**
 * shell_perf_log_dump_events:
 * @perf_log: a #ShellPerfLog
 * @out: output stream into which to write the event definitions
 * @error: location to store #GError, or %NULL
 *
 * Dump the definition of currently defined events and statistics, formatted
 * as JSON, to the specified output stream. The JSON output is an array,
 * with each element being a dictionary of the form:
 *
 * { name: <name of event>,
 *   description: <descrition of string,
 *   statistic: true } (only for statistics)
 *
 * Return value: %TRUE if the dump succeeded. %FALSE if an IO error occurred
 */
gboolean
shell_perf_log_dump_events (ShellPerfLog   *perf_log,
                            GOutputStream  *out,
                            GError        **error)
{
  GString *output;
  int i;

  output = g_string_new (NULL);
  g_string_append (output, "[ ");

  for (i = 0; i < perf_log->events->len; i++)
    {
      ShellPerfEvent *event = g_ptr_array_index (perf_log->events, i);
      char *escaped_description = escape_quotes (event->description);
      gboolean is_statistic = g_hash_table_lookup (perf_log->statistics_by_name, event->name) != NULL;

      if (i != 0)
        g_string_append (output, ",\n  ");

      g_string_append_printf (output,
                                "{ \"name\": \"%s\",\n"
                              "    \"description\": \"%s\"",
                              event->name, escaped_description);
      if (is_statistic)
        g_string_append (output, ",\n    \"statistic\": true");

      g_string_append (output, " }");

      if (escaped_description != event->description)
        g_free (escaped_description);
    }

  g_string_append (output, " ]");

  return write_string (out, g_string_free (output, FALSE), error);
}
Beispiel #12
0
/*
 * Create a recovery.conf file in memory using a PQExpBuffer
 */
static void
GenerateRecoveryConf(PGconn *conn)
{
	PQconninfoOption *connOptions;
	PQconninfoOption *option;
	PQExpBufferData conninfo_buf;
	char	   *escaped;

	recoveryconfcontents = createPQExpBuffer();
	if (!recoveryconfcontents)
	{
		fprintf(stderr, _("%s: out of memory\n"), progname);
		disconnect_and_exit(1);
	}

	connOptions = PQconninfo(conn);
	if (connOptions == NULL)
	{
		fprintf(stderr, _("%s: out of memory\n"), progname);
		disconnect_and_exit(1);
	}

	appendPQExpBufferStr(recoveryconfcontents, "standby_mode = 'on'\n");

	initPQExpBuffer(&conninfo_buf);
	for (option = connOptions; option && option->keyword; option++)
	{
		/*
		 * Do not emit this setting if: - the setting is "replication",
		 * "dbname" or "fallback_application_name", since these would be
		 * overridden by the libpqwalreceiver module anyway. - not set or
		 * empty.
		 */
		if (strcmp(option->keyword, "replication") == 0 ||
			strcmp(option->keyword, "dbname") == 0 ||
			strcmp(option->keyword, "fallback_application_name") == 0 ||
			(option->val == NULL) ||
			(option->val != NULL && option->val[0] == '\0'))
			continue;

		/* Separate key-value pairs with spaces */
		if (conninfo_buf.len != 0)
			appendPQExpBufferStr(&conninfo_buf, " ");

		/*
		 * Write "keyword=value" pieces, the value string is escaped and/or
		 * quoted if necessary.
		 */
		escaped = escapeConnectionParameter(option->val);
		appendPQExpBuffer(&conninfo_buf, "%s=%s", option->keyword, escaped);
		free(escaped);
	}

	/*
	 * Escape the connection string, so that it can be put in the config file.
	 * Note that this is different from the escaping of individual connection
	 * options above!
	 */
	escaped = escape_quotes(conninfo_buf.data);
	appendPQExpBuffer(recoveryconfcontents, "primary_conninfo = '%s'\n", escaped);
	free(escaped);

	if (PQExpBufferBroken(recoveryconfcontents) ||
		PQExpBufferDataBroken(conninfo_buf))
	{
		fprintf(stderr, _("%s: out of memory\n"), progname);
		disconnect_and_exit(1);
	}

	termPQExpBuffer(&conninfo_buf);

	PQconninfoFree(connOptions);
}