2018-05-12 16:03:00 +00:00
|
|
|
/*
|
2009-10-17 14:14:25 +00:00
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
|
|
|
*
|
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
|
2021-02-27 15:14:19 +00:00
|
|
|
* Copyright (c) 2005-2021 Brenden Matthews, Philip Kovacs, et. al.
|
2009-10-17 14:14:25 +00:00
|
|
|
* (see AUTHORS)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2010-01-04 04:50:02 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2009-10-17 14:14:25 +00:00
|
|
|
#include "core.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "text_object.h"
|
|
|
|
|
2009-10-06 23:20:45 +00:00
|
|
|
struct combine_data {
|
2018-05-12 16:03:00 +00:00
|
|
|
char *left;
|
|
|
|
char *seperation;
|
|
|
|
char *right;
|
2009-10-06 23:20:45 +00:00
|
|
|
};
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void parse_combine_arg(struct text_object *obj, const char *arg) {
|
|
|
|
struct combine_data *cd;
|
|
|
|
unsigned int i, j;
|
|
|
|
unsigned int indenting = 0; // vars can be used as args for other vars
|
|
|
|
int startvar[2];
|
|
|
|
int endvar[2];
|
|
|
|
startvar[0] = endvar[0] = startvar[1] = endvar[1] = -1;
|
|
|
|
j = 0;
|
|
|
|
for (i = 0; arg[i] != 0 && j < 2; i++) {
|
|
|
|
if (startvar[j] == -1) {
|
2018-05-13 22:46:09 +00:00
|
|
|
if (arg[i] == '$') { startvar[j] = i; }
|
2018-05-12 16:03:00 +00:00
|
|
|
} else if (endvar[j] == -1) {
|
|
|
|
if (arg[i] == '{') {
|
|
|
|
indenting++;
|
|
|
|
} else if (arg[i] == '}') {
|
|
|
|
indenting--;
|
|
|
|
}
|
|
|
|
if (indenting == 0 &&
|
|
|
|
arg[i + 1] < 48) { //<48 has 0, $, and the most used chars not used
|
|
|
|
// in varnames but not { or }
|
|
|
|
endvar[j] = i + 1;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (startvar[0] >= 0 && endvar[0] >= 0 && startvar[1] >= 0 &&
|
|
|
|
endvar[1] >= 0) {
|
2018-05-12 23:26:31 +00:00
|
|
|
cd =
|
|
|
|
static_cast<struct combine_data *>(malloc(sizeof(struct combine_data)));
|
2018-05-12 16:03:00 +00:00
|
|
|
memset(cd, 0, sizeof(struct combine_data));
|
2009-10-06 23:20:45 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
cd->left = static_cast<char *>(malloc(endvar[0] - startvar[0] + 1));
|
|
|
|
cd->seperation = static_cast<char *>(malloc(startvar[1] - endvar[0] + 1));
|
|
|
|
cd->right = static_cast<char *>(malloc(endvar[1] - startvar[1] + 1));
|
2009-10-17 14:14:25 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
strncpy(cd->left, arg + startvar[0], endvar[0] - startvar[0]);
|
|
|
|
cd->left[endvar[0] - startvar[0]] = 0;
|
2009-10-17 14:14:25 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
strncpy(cd->seperation, arg + endvar[0], startvar[1] - endvar[0]);
|
|
|
|
cd->seperation[startvar[1] - endvar[0]] = 0;
|
2009-10-17 14:14:25 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
strncpy(cd->right, arg + startvar[1], endvar[1] - startvar[1]);
|
|
|
|
cd->right[endvar[1] - startvar[1]] = 0;
|
2009-10-17 14:14:25 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
obj->sub =
|
|
|
|
static_cast<struct text_object *>(malloc(sizeof(struct text_object)));
|
2018-05-12 16:03:00 +00:00
|
|
|
extract_variable_text_internal(obj->sub, cd->left);
|
2018-05-12 23:26:31 +00:00
|
|
|
obj->sub->sub =
|
|
|
|
static_cast<struct text_object *>(malloc(sizeof(struct text_object)));
|
2018-05-12 16:03:00 +00:00
|
|
|
extract_variable_text_internal(obj->sub->sub, cd->right);
|
|
|
|
obj->data.opaque = cd;
|
|
|
|
} else {
|
|
|
|
throw combine_needs_2_args_error();
|
|
|
|
}
|
2009-10-17 14:14:25 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_combine(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *cd = static_cast<struct combine_data *>(obj->data.opaque);
|
2018-05-12 16:03:00 +00:00
|
|
|
std::vector<std::vector<char>> buf;
|
|
|
|
buf.resize(2);
|
|
|
|
buf[0].resize(max_user_text.get(*state));
|
|
|
|
buf[1].resize(max_user_text.get(*state));
|
|
|
|
int i, j;
|
2018-05-13 22:46:09 +00:00
|
|
|
int p_len_remaining = p_max_size - 1;
|
2018-05-12 16:03:00 +00:00
|
|
|
long longest = 0;
|
|
|
|
int nextstart;
|
|
|
|
int nr_rows[2];
|
|
|
|
struct llrows {
|
|
|
|
char *row;
|
|
|
|
struct llrows *next;
|
|
|
|
};
|
|
|
|
struct llrows *ll_rows[2], *current[2];
|
|
|
|
struct text_object *objsub = obj->sub;
|
2009-10-17 14:14:25 +00:00
|
|
|
|
2018-05-13 22:46:09 +00:00
|
|
|
if ((cd == nullptr) || (p_max_size == 0)) { return; }
|
2009-10-17 14:14:25 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
p[0] = 0;
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
nr_rows[i] = 1;
|
|
|
|
nextstart = 0;
|
2018-05-12 23:26:31 +00:00
|
|
|
ll_rows[i] = static_cast<struct llrows *>(malloc(sizeof(struct llrows)));
|
2018-05-12 16:03:00 +00:00
|
|
|
current[i] = ll_rows[i];
|
2018-05-13 22:46:09 +00:00
|
|
|
for (j = 0; j < i; j++) { objsub = objsub->sub; }
|
2018-05-12 16:03:00 +00:00
|
|
|
generate_text_internal(&(buf[i][0]), max_user_text.get(*state), *objsub);
|
|
|
|
for (j = 0; buf[i][j] != 0; j++) {
|
2018-05-13 22:46:09 +00:00
|
|
|
if (buf[i][j] == '\t') { buf[i][j] = ' '; }
|
2018-05-12 23:26:31 +00:00
|
|
|
if (buf[i][j] == '\n') {
|
2018-05-12 16:03:00 +00:00
|
|
|
buf[i][j] = 0; // the vars inside combine may not have a \n at the end
|
2018-05-12 23:26:31 +00:00
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
if (buf[i][j] ==
|
|
|
|
2) { // \002 is used instead of \n to separate lines inside a var
|
|
|
|
buf[i][j] = 0;
|
|
|
|
current[i]->row = strdup(&(buf[i][0]) + nextstart);
|
2018-05-12 23:26:31 +00:00
|
|
|
if (i == 0 && static_cast<long>(strlen(current[i]->row)) > longest) {
|
|
|
|
longest = static_cast<long>(strlen(current[i]->row));
|
|
|
|
}
|
|
|
|
current[i]->next =
|
|
|
|
static_cast<struct llrows *>(malloc(sizeof(struct llrows)));
|
2018-05-12 16:03:00 +00:00
|
|
|
current[i] = current[i]->next;
|
|
|
|
nextstart = j + 1;
|
|
|
|
nr_rows[i]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current[i]->row = strdup(&(buf[i][0]) + nextstart);
|
2018-05-12 23:26:31 +00:00
|
|
|
if (i == 0 && static_cast<long>(strlen(current[i]->row)) > longest) {
|
|
|
|
longest = static_cast<long>(strlen(current[i]->row));
|
|
|
|
}
|
|
|
|
current[i]->next = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
current[i] = ll_rows[i];
|
|
|
|
}
|
|
|
|
for (j = 0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1]); j++) {
|
2018-05-12 23:26:31 +00:00
|
|
|
if (current[0] != nullptr) {
|
2018-05-13 22:46:09 +00:00
|
|
|
strncat(p, current[0]->row, p_len_remaining);
|
|
|
|
p_len_remaining -= strlen(current[0]->row);
|
2018-05-12 16:03:00 +00:00
|
|
|
i = strlen(current[0]->row);
|
2018-05-12 23:26:31 +00:00
|
|
|
} else {
|
2018-05-12 16:03:00 +00:00
|
|
|
i = 0;
|
2018-05-12 23:26:31 +00:00
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
while (i < longest) {
|
2018-05-13 22:46:09 +00:00
|
|
|
strncat(p, " ", p_len_remaining);
|
|
|
|
p_len_remaining -= 2;
|
2018-05-12 16:03:00 +00:00
|
|
|
i++;
|
|
|
|
}
|
2018-05-12 23:26:31 +00:00
|
|
|
if (current[1] != nullptr) {
|
2018-05-13 22:46:09 +00:00
|
|
|
p_len_remaining -= strlen(cd->seperation);
|
|
|
|
strncat(p, cd->seperation, p_len_remaining);
|
|
|
|
p_len_remaining -= strlen(current[1]->row);
|
|
|
|
strncat(p, current[1]->row, p_len_remaining);
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
2018-05-13 22:46:09 +00:00
|
|
|
strncat(p, "\n", p_len_remaining);
|
|
|
|
p_len_remaining -= 2;
|
2009-10-17 14:14:25 +00:00
|
|
|
#ifdef HAVE_OPENMP
|
2018-05-12 16:03:00 +00:00
|
|
|
#pragma omp parallel for schedule(dynamic, 10)
|
2009-10-17 14:14:25 +00:00
|
|
|
#endif /* HAVE_OPENMP */
|
2018-05-12 23:26:31 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
2018-05-13 22:46:09 +00:00
|
|
|
if (current[i] != nullptr) { current[i] = current[i]->next; }
|
2018-05-12 23:26:31 +00:00
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
2009-10-17 14:14:25 +00:00
|
|
|
#ifdef HAVE_OPENMP
|
2018-05-12 16:03:00 +00:00
|
|
|
#pragma omp parallel for schedule(dynamic, 10)
|
2009-10-17 14:14:25 +00:00
|
|
|
#endif /* HAVE_OPENMP */
|
2018-05-12 16:03:00 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
2018-05-12 23:26:31 +00:00
|
|
|
while (ll_rows[i] != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
current[i] = ll_rows[i];
|
|
|
|
free(current[i]->row);
|
|
|
|
ll_rows[i] = current[i]->next;
|
|
|
|
free(current[i]);
|
|
|
|
}
|
|
|
|
}
|
2009-10-17 14:14:25 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void free_combine(struct text_object *obj) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *cd = static_cast<struct combine_data *>(obj->data.opaque);
|
2009-10-06 23:20:45 +00:00
|
|
|
|
2018-05-13 22:46:09 +00:00
|
|
|
if (cd == nullptr) { return; }
|
2018-05-12 16:03:00 +00:00
|
|
|
free(cd->left);
|
|
|
|
free(cd->seperation);
|
|
|
|
free(cd->right);
|
|
|
|
free_text_objects(obj->sub->sub);
|
|
|
|
free_and_zero(obj->sub->sub);
|
|
|
|
free_text_objects(obj->sub);
|
|
|
|
free_and_zero(obj->sub);
|
|
|
|
free_and_zero(obj->data.opaque);
|
2009-10-17 14:14:25 +00:00
|
|
|
}
|