1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
/* NRSS - ncurses RSS reader
Copyright (C) 2007 Jack Miller <jjm2n4@umr.edu>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
*/
#include "nrss.h"
#ifdef BSD
char *strndup(const char *line, int len)
{
char *buf = xmalloc(len + 1);
int i = 0;
for (i = 0; i < len; i++) {
if (line[i] == 0)
break;
buf[i] = line[i];
}
buf[i] = 0;
return buf;
}
#endif
char *xstrdup(const char *s)
{
char *temp;
if ((temp = strdup(s)) == NULL) {
logit("xstrdup: No memory! Bailing.\n");
cleanup();
}
return temp;
}
char *xstrndup(const char *s, size_t n)
{
char *temp;
if ((temp = strndup(s, n)) == NULL) {
logit("xstrndup: No memory! Bailing.\n");
cleanup();
}
return temp;
}
void *xmalloc(size_t n)
{
void *temp;
if ((temp = malloc(n)) == NULL) {
logit("xmalloc: No memory! Bailing.\n");
cleanup();
}
return temp;
}
char *concat(char *one, char *two)
{
char *line = NULL;
line = xmalloc(strlen(one) + strlen(two) + 1);
line = strcpy(line, one);
line = strcat(line, two);
return line;
}
int paren_type(char *str, int idx)
{
if ((idx) && (str[idx - 1] != '\\')) {
if (str[idx] == '(')
return 1;
else if (str[idx] == ')')
return -1;
}
return 0;
}
void get_tern_range(char *tern, int *idx, int *len, int *end, int status)
{
int paren_count = 1;
int i = 4;
while (tern[i]) {
if ((tern[i] == ':') && (paren_count == 1)
&& (tern[i - 1] != '\\')) {
if (status) {
*idx = 4;
*len = i - 4;
} else
*idx = i + 1;
break;
}
paren_count += paren_type(tern, i);
i++;
}
while ((tern[i]) && (paren_count)) {
paren_count += paren_type(tern, i);
i++;
}
if (!status)
*len = i - *idx - 1;
*end = i;
}
char *get_ternary(char *line, int *idx, int *len, int *end,
struct ternary *terns, int num_terns)
{
char *tern = line;
int i = 0;
while ((tern = strstr(tern, "%?")) != NULL) {
if ((tern == line) || (tern[-1] != '\\')) {
for (i = 0; i < num_terns; i++) {
if (terns[i].ident == tern[2]) {
get_tern_range(tern, idx, len, end, terns[i].status);
break;
}
}
}
return tern;
}
return NULL;
}
char *ternary_string(char *format, struct ternary *terns, int num_terns)
{
char *line = NULL;
char *sub = NULL;
int idx = 0;
int len = 0;
int end = 0;
int temp = 0;
if (!format)
return NULL;
line = xstrdup(format);
while ((sub = get_ternary(line, &idx, &len, &end,
terns, num_terns)) != NULL) {
temp = strlen(sub + end);
memmove(sub, sub + idx, len);
memmove(sub + len, sub + end, temp + 1);
}
sub = xmalloc(strlen(line) + 1);
strcpy(sub, line);
free(line);
return sub;
}
char *get_escape(char *line, int *idx, struct escape *escapes, int num_esc)
{
char *esc = line;
int i = 0;
while ((esc = strstr(esc, "%")) != NULL) {
if ((esc == line) || (esc[-1] != '\\')) {
for (i = 0; i < num_esc; i++) {
if (escapes[i].ident[1] == esc[1]) {
*idx = i;
return esc;
}
}
}
esc += 2;
}
return NULL;
}
char *escape_string(char *format, struct escape *escapes, int num_esc)
{
char *line = NULL;
char *sub = NULL;
char *temp = NULL;
int idx = 0;
if (!format)
return NULL;
line = xstrdup(format);
while ((sub = get_escape(line, &idx, escapes, num_esc)) != NULL) {
if (!escapes[idx].replacement)
escapes[idx].replacement = " ";
temp =
xmalloc(strlen(line) + strlen(escapes[idx].replacement) + 1);
memset(temp, 0,
strlen(line) + strlen(escapes[idx].replacement) + 1);
temp = strncpy(temp, line, (sub - line));
strcat(temp, escapes[idx].replacement);
strcpy(strlen(temp) + temp, sub + 2);
free(line);
line = temp;
}
return line;
}
|