Exemple #1
0
static void readconfig () {
	const char* homedir = getenv("HOME");
	const char* relconfig = ".config/dwm/config.json";
	char* rulesFile = calloc(strlen(homedir) + strlen(relconfig) + 2, sizeof(char));
	char* content;
	const nx_json *json, *js;

	strcpy(rulesFile, homedir);
	strcat(rulesFile, "/");
	strcat(rulesFile, relconfig);

	content = load_file(rulesFile);

	if (content) {
		json = nx_json_parse_utf8(content);

		for (js = json->child; js; js = js->next) {
			if (!strcmp(js->key, "rules"))
				readjsonrules(js);
			else if (!strcmp(js->key, "tags"))
				readtags(js);
			else if (!strcmp(js->key, "font"))
				readfont(js);
		}
		nx_json_free(json);
		free(content);
	}
	if (!font) {
		font = calloc(strlen(fallbackfont) + 1, sizeof(char));
		strcpy(font, fallbackfont);
	}
}
Exemple #2
0
const nx_json* nx_json_parse(char* text, nx_json_unicode_encoder encoder) {
  nx_json js={0};
  if (!parse_value(&js, 0, text, encoder)) {
    if (js.child) nx_json_free(js.child);
    return 0;
  }
  return js.child;
}
Exemple #3
0
void nx_json_free(const nx_json* js) {
  nx_json* p=js->child;
  nx_json* p1;
  while (p) {
    p1=p->next;
    nx_json_free(p);
    p=p1;
  }
  NX_JSON_FREE(js);
}
Exemple #4
0
/*
	Parse json string to c data structure, and then store them in global config
*/
int do_parse_config(const char *json_str, unsigned int size)
{
    int ret = -1;
    char *json_str_cpy = NULL;
    const nx_json *js = NULL, *js_elem = NULL;

    if (json_str == NULL || size <= 0) {
        AC_ERROR("invalid parameters.");
        return -1;
    }

    json_str_cpy = malloc(sizeof(char) * (size + 1));
    if (json_str_cpy == NULL) {
        goto out;
    }
    bzero(json_str_cpy, size + 1);
    memcpy(json_str_cpy, json_str, size);
    js = nx_json_parse_utf8(json_str_cpy);
    if (js == NULL) {
        AC_ERROR("config parse failed.\n");
        goto out;
    }

    if (nx_json_verify(js) != 0) {
        AC_ERROR("nxjson verify failed.\n");
        goto out;
    }

    init_global_config();

    /*parse control set*/
    js_elem = nx_json_get(js, CONTROL_SET_KEY);
    if (js_elem->type != NX_JSON_NULL) {
        if (do_parse_control_set(js_elem, &s_config.control->set) == 0) {
            display_control_set(&s_config.control->set);
        }
    }

    /*parse audit set*/
    js_elem = nx_json_get(js, AUDIT_SET_KEY);
    if (js_elem->type != NX_JSON_NULL) {
        if (do_parse_audit_set(js_elem, &s_config.audit->set) == 0) {
            display_audit_set(&s_config.audit->set);
        }
    }

    /*parse control rule*/
    js_elem = nx_json_get(js, CONTROL_RULE_KEY);
    if (js_elem->type != NX_JSON_NULL) {
        if (do_parse_control_rule(js_elem, &s_config.control->rule) == 0) {
            display_control_rule(&s_config.control->rule);
        }
        else {
            goto out;
        }
    }

    /*parse audit rule*/
    js_elem = nx_json_get(js, AUDIT_RULE_KEY);
    if (js_elem->type != NX_JSON_NULL) {
        if (do_parse_audit_rule(js_elem, &s_config.audit->rule) == 0) {
            display_audit_rule(&s_config.audit->rule);
        }
        else {
            goto out;
        }
    }

    ret = 0;
out:
    if (js != NULL) {
        nx_json_free(js);
    }
    if (json_str_cpy) {
        free(json_str_cpy);
    }
    if (ret != 0) {
        free_global_config();
    }
    return ret;
}
Exemple #5
0
void trsp_update_files(gchar * input_file)
{

    FILE           *mfp;        /* transmitter information json file */
    unsigned int    mfplen;     /* size of transmitter information json file */
    int             result;
    FILE           *ffile;      /* transponder output file in gpredict format */
    char           *jsn_object; /* json array will be in this buffer before parsing */
    unsigned int    idx;	/* object index in JSON-Array */
    new_mode_t     *nmode;
    new_trsp_t     *ntrsp;
    GHashTable     *modes_hash; /* hash table to store modes */
    GHashTable     *trsp_hash;  /* hash table to store satellite list to generate trsp files */
    guint          *key = NULL;

    gchar          *userconfdir;
    gchar          *trspfile;
    gchar          *modesfile;
    gchar          *trspfolder;

    /* force decimal mark to dot when parsing JSON file */
    setlocale(LC_NUMERIC, "C");

    modes_hash =
        g_hash_table_new_full(g_int_hash, g_int_equal, g_free, free_new_mode);
    trsp_hash =
        g_hash_table_new_full(g_int_hash, g_int_equal, g_free, free_new_trsp);

    userconfdir = get_user_conf_dir();
    trspfolder = g_strconcat(userconfdir, G_DIR_SEPARATOR_S, "trsp", NULL);
    modesfile = g_strconcat(trspfolder, "/modes.json", NULL);

    mfp = fopen(modesfile, "r");

    if (mfp != NULL)
    {
        mfplen = 0;
        fseek(mfp, 0, SEEK_END);
        mfplen = ftell(mfp);
        rewind(mfp);
        jsn_object = g_malloc(mfplen);
        result = fread(jsn_object, mfplen, 1, mfp);

        if (result == 1)
        {
            const nx_json  *json = nx_json_parse(jsn_object, 0);

            if (json)
            {
                idx = 0;
                while (1)
                {
                    const nx_json  *json_obj = nx_json_item(json, idx++);
                    struct modes    m_modes;

                    if (json_obj->type == NX_JSON_NULL)
                        break;

                    m_modes.id = nx_json_get(json_obj, "id")->int_value;
                    strncpy(m_modes.name,
                            nx_json_get(json_obj, "name")->text_value, 79);
                    m_modes.name[79] = 0;

                    /* add data to hash table */
                    key = g_try_new0(guint, 1);
                    *key = m_modes.id;

                    nmode = g_hash_table_lookup(modes_hash, key);

                    if (nmode == NULL)
                    {
                        /* create new_mode structure */
                        nmode = g_try_new(new_mode_t, 1);
                        nmode->modnum = m_modes.id;
                        nmode->modname = g_strdup(m_modes.name);

                        g_hash_table_insert(modes_hash, key, nmode);

                    }

                    sat_log_log(SAT_LOG_LEVEL_INFO, _("MODE %d %s"),
                                m_modes.id, m_modes.name);
                }               // while(1)
                nx_json_free(json);
            }                   // if(json)
        }                       // if(result == 1)
        g_free(jsn_object);
        fclose(mfp);
    }                           // if(mfp)

//    guint num = 0;
//    printf("---------- PRINTING MODES LIST ------- \n");
//    g_hash_table_foreach (modes_hash, check_and_print_mode, &num);

    mfp = fopen(input_file, "r");
    if (mfp != NULL)
    {
        mfplen = 0;
        fseek(mfp, 0, SEEK_END);
        mfplen = ftell(mfp);
        rewind(mfp);
        jsn_object = g_malloc(mfplen);
        result = fread(jsn_object, mfplen, 1, mfp);

        if (result == 1)
        {
            const nx_json  *json = nx_json_parse(jsn_object, 0);

            if (json)
            {
                idx = 0;
                while (1)
                {
                    const nx_json  *json_obj = nx_json_item(json, idx++);
                    struct transponder m_trsp;

                    if (json_obj->type == NX_JSON_NULL)
                        break;

                    strncpy(m_trsp.description,
                            nx_json_get(json_obj, "description")->text_value, 79);
                    m_trsp.description[79] = 0;
                    m_trsp.catnum =
                        nx_json_get(json_obj, "norad_cat_id")->int_value;
                    m_trsp.uplink_low =
                        nx_json_get(json_obj, "uplink_low")->int_value;
                    m_trsp.uplink_high =
                        nx_json_get(json_obj, "uplink_high")->int_value;
                    m_trsp.downlink_low =
                        nx_json_get(json_obj, "downlink_low")->int_value;
                    m_trsp.downlink_high =
                        nx_json_get(json_obj, "downlink_high")->int_value;

                    key = g_try_new0(guint, 1);
                    *key = nx_json_get(json_obj, "mode_id")->int_value;
                    nmode = g_hash_table_lookup(modes_hash, key);
                    if (nmode != NULL)
                        sprintf(m_trsp.mode, "%s", nmode->modname);
                    else
                        sprintf(m_trsp.mode, "%lli",
                                nx_json_get(json_obj, "mode_id")->int_value);

                    m_trsp.invert = nx_json_get(json_obj, "invert")->int_value;
                    m_trsp.baud = nx_json_get(json_obj, "baud")->dbl_value;
                    m_trsp.alive = nx_json_get(json_obj, "alive")->int_value;

                    //strcpy(m_trsp.uuid,nx_json_get(json_obj,          "uuid")->text_value);
                    //sat_log_log(SAT_LOG_LEVEL_DEBUG, _(">>> Preparing information for transponders of cat_id %d <<<"), m_trsp.catnum, __func__);
                    ////sat_log_log (SAT_LOG_LEVEL_INFO, _("         uuid : %s"), m_trsp.uuid, __func__);
                    //sat_log_log(SAT_LOG_LEVEL_DEBUG, _("  description : %s"), m_trsp.description, __func__);
                    //sat_log_log(SAT_LOG_LEVEL_DEBUG, _("        alive : %s"), m_trsp.alive ? "true" : "false", __func__);
                    //sat_log_log(SAT_LOG_LEVEL_DEBUG, _("   uplink_low : %Ld"),m_trsp.uplink_low, __func__);
                    //sat_log_log(SAT_LOG_LEVEL_DEBUG, _("  uplink_high : %Ld"),m_trsp.uplink_high, __func__);
                    //sat_log_log(SAT_LOG_LEVEL_DEBUG, _(" downink_low  : %Ld"),m_trsp.downlink_low, __func__);
                    //sat_log_log(SAT_LOG_LEVEL_DEBUG, _("downlink_high : %Ld"),m_trsp.downlink_high, __func__);
                    //sat_log_log(SAT_LOG_LEVEL_DEBUG, _("         Mode : %s"), m_trsp.mode, __func__);
                    //sat_log_log(SAT_LOG_LEVEL_DEBUG, _("       Invert : %s"), m_trsp.invert ? "true" : "false", __func__);
                    // sat_log_log(SAT_LOG_LEVEL_DEBUG, _("         Baud : %lf"),m_trsp.baud, __func__);
                    //sat_log_log(SAT_LOG_LEVEL_DEBUG, _(" norad_cat_id : %Ld"),m_trsp.catnum, __func__);
                    char            m_catnum[20];

                    sprintf(m_catnum, "%d", m_trsp.catnum);
                    trspfile = g_strconcat(trspfolder, G_DIR_SEPARATOR_S,
                                           m_catnum, ".trsp", NULL);
                    //sat_log_log(SAT_LOG_LEVEL_DEBUG, _("%s: Writing to file : %s "), __func__, trspfile);

                    //first lets delete the file we already have, to make space for the new version
                    /* check existence and add data to hash table for future check */
                    key = g_try_new0(guint, 1);
                    *key = m_trsp.catnum;
                    ntrsp = g_hash_table_lookup(trsp_hash, key);

                    if (ntrsp == NULL)
                    {
                        /* create new_trsp structure */
                        ntrsp = g_try_new(new_trsp_t, 1);
                        ntrsp->catnum = m_trsp.catnum;
                        ntrsp->numtrsp = 1;     //our first insertion of transponder to this file
                        g_hash_table_insert(trsp_hash, key, ntrsp);
                        g_remove(trspfile);

                    }
                    else
                    {
                        //TODO: increase number of transponders here
                        //ntrsp->numtrsp += 1; //number of transponder info in this file
                        //g_hash_table_replace(trsp_hash, key, ntrsp);
                    }

                    //now lets write the new version
                    ffile = g_fopen(trspfile, "a");
                    if (ffile != NULL)
                    {
                        fprintf(ffile, "\n[%s]\n", m_trsp.description);
                        if (m_trsp.uplink_low > 0)
                            fprintf(ffile, "UP_LOW=%lld\n",
                                    m_trsp.uplink_low);
                        if (m_trsp.uplink_high > 0)
                            fprintf(ffile, "UP_HIGH=%lld\n",
                                    m_trsp.uplink_high);
                        if (m_trsp.downlink_low > 0)
                            fprintf(ffile, "DOWN_LOW=%lld\n",
                                    m_trsp.downlink_low);
                        if (m_trsp.downlink_high > 0)
                            fprintf(ffile, "DOWN_HIGH=%lld\n",
                                    m_trsp.downlink_high);
                        fprintf(ffile, "MODE=%s\n",
                                m_trsp.mode);
                        if (m_trsp.baud > 0.0)
                            fprintf(ffile, "BAUD=%.0f\n",
                                    m_trsp.baud);
                        if (m_trsp.invert)
                            fprintf(ffile, "INVERT=%s\n",
                                    "true");
                        fclose(ffile);
                    }
                    else
                    {
                        sat_log_log(SAT_LOG_LEVEL_ERROR,
                                    _
                                    ("%s: Could not open trsp file for write"),
                                    __func__);
                    }
                }               // while(1)
                nx_json_free(json);
            }                   // if(json)
        }                       // if(result == 1)
        g_free(jsn_object);
        fclose(mfp);
    }                           // if(mfp)

    g_hash_table_destroy(modes_hash);
}