<commit_before>/*
 * Disnix - A Nix-based distributed service deployment tool
 * Copyright (C) 2008-2016  <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "disnix-client.h"
#include <stdio.h>
#include <stdlib.h>

#include <string.h>

#include "disnix-dbus.h"
#define BUFFER_SIZE 1024

char *logdir;

static void print_log(const gint pid)
{
    char pidStr[15], buf[BUFFER_SIZE];
    gchar *logfile;
    FILE *file;
    
    sprintf(pidStr, "%d", pid);
    logfile = g_strconcat(logdir, "/", pidStr, NULL);
    file = fopen(logfile, "r");
    
    if(file == NULL)
        fprintf(stderr, "Cannot display logfile of pid: %d\n", pid);
    else
    {
        size_t bytes_read;
        
        while(!feof(file) && (bytes_read = fread(buf, 1, BUFFER_SIZE, file)) > 0)
            fwrite(buf, 1, bytes_read, stderr);
        
        fclose(file);
    }
    
    g_free(logfile);
}

/* Signal handlers */

static void disnix_finish_signal_handler(GDBusProxy *proxy, const gint pid, gpointer user_data)
{
    gint my_pid = *((gint*)user_data);

    if(pid == my_pid)
        exit(0);
}

static void disnix_success_signal_handler(GDBusProxy *proxy, const gint pid, gchar **derivation, gpointer user_data)
{
    gint my_pid = *((gint*)user_data);

    if(pid == my_pid)
    {
        unsigned int i;
    
        for(i = 0; i < g_strv_length(derivation); i++)
        {
            g_print("%s", derivation[i]);
            
            /* Add linefeed if it does not end with a linefeed. TODO: get rid of this */
            if(strlen(derivation[i]) > 0 && derivation[i][strlen(derivation[i]) - 1] != '\n')
                g_print("\n");
        }
        
        exit(0);
    }
}

static void disnix_failure_signal_handler(GDBusProxy *proxy, const gint pid, gpointer user_data)
{
    gint my_pid = *((gint*)user_data);
    
    if(pid == my_pid)
    {
        print_log(pid);
        exit(1);
    }
}

static void cleanup(OrgNixosDisnixDisnix *proxy, gchar **derivation, gchar **arguments)<commit_msg>Get rid of obsolete compatibility workaround<commit_after>/*
 * Disnix - A Nix-based distributed service deployment tool
 * Copyright (C) 2008-2016  <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "disnix-client.h"
#include <stdio.h>
#include <stdlib.h>

#include <string.h>

#include "disnix-dbus.h"
#define BUFFER_SIZE 1024

char *logdir;

static void print_log(const gint pid)
{
    char pidStr[15], buf[BUFFER_SIZE];
    gchar *logfile;
    FILE *file;
    
    sprintf(pidStr, "%d", pid);
    logfile = g_strconcat(logdir, "/", pidStr, NULL);
    file = fopen(logfile, "r");
    
    if(file == NULL)
        fprintf(stderr, "Cannot display logfile of pid: %d\n", pid);
    else
    {
        size_t bytes_read;
        
        while(!feof(file) && (bytes_read = fread(buf, 1, BUFFER_SIZE, file)) > 0)
            fwrite(buf, 1, bytes_read, stderr);
        
        fclose(file);
    }
    
    g_free(logfile);
}

/* Signal handlers */

static void disnix_finish_signal_handler(GDBusProxy *proxy, const gint pid, gpointer user_data)
{
    gint my_pid = *((gint*)user_data);

    if(pid == my_pid)
        exit(0);
}

static void disnix_success_signal_handler(GDBusProxy *proxy, const gint pid, gchar **derivation, gpointer user_data)
{
    gint my_pid = *((gint*)user_data);

    if(pid == my_pid)
    {
        unsigned int i;
    
        for(i = 0; i < g_strv_length(derivation); i++)
            g_print("%s\n", derivation[i]);
        
        exit(0);
    }
}

static void disnix_failure_signal_handler(GDBusProxy *proxy, const gint pid, gpointer user_data)
{
    gint my_pid = *((gint*)user_data);
    
    if(pid == my_pid)
    {
        print_log(pid);
        exit(1);
    }
}

static void cleanup(OrgNixosDisnixDisnix *proxy, gchar **derivation, gchar **arguments)
{'fraction_non_alphanumeric': 0.11047180667433831, 'fraction_numerical': 0.013425393172228616, 'mean_word_length': 2.959757023538345, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2248197', 'n_tokens_mistral': 1765, 'n_tokens_neox': 1673, 'n_words': 545}