Page MenuHomeFreeBSD
Authored By
hselasky
Dec 1 2021, 4:15 PM
Size
3 KB
Referenced Files
None
Subscribers
None

config_compress.c

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/queue.h>
struct line;
typedef TAILQ_HEAD(,line) line_head_t;
typedef TAILQ_ENTRY(line) line_entry_t;
struct line {
line_entry_t entry;
char *line;
char *alt;
};
static char buffer[65536];
static void
new_line(const char *ptr, line_head_t *phead)
{
struct line *line = malloc(sizeof(*line));
line->line = strdup(ptr);
line->alt = NULL;
TAILQ_INSERT_TAIL(phead, line, entry);
}
static void
skip(const char **pp)
{
loop:;
char ch = **pp;
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\\' || ch == '\r') {
(*pp)++;
goto loop;
}
}
static void
not_skip(const char **pp)
{
loop:;
char ch = **pp;
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\\' || ch == '\r' || ch == 0)
return;
(*pp)++;
goto loop;
}
static bool
squash_line(const char *a, const char *b, char **ptr)
{
if (strstr(a, "include") == a)
return (false);
if (strstr(b, "include") == b)
return (false);
if (strstr(a, "#") == a)
return (false);
if (strstr(b, "#") == b)
return (false);
const char *ao = strstr(a, "optional");
const char *as = strstr(a, "standard");
if (ao == NULL && as == NULL)
return (false);
const char *bo = strstr(b, "optional");
const char *bs = strstr(b, "standard");
if (bo == NULL && bs == NULL)
return (false);
const char *aa;
if ((uintptr_t)ao < (uintptr_t)as)
aa = ao;
else
aa = as;
if (aa == NULL && ao != NULL)
aa = ao;
if (aa == NULL && as != NULL)
aa = as;
const char *bb;
if ((uintptr_t)bo < (uintptr_t)bs)
bb = bo;
else
bb = bs;
if (bb == NULL && bo != NULL)
bb = bo;
if (bb == NULL && bs != NULL)
bb = bs;
if (bb[0] != aa[0])
return (false);
while (1) {
skip(&aa);
skip(&bb);
if (*aa != *bb)
return (false);
if (*aa == 0)
break;
aa++;
bb++;
}
if (ptr != NULL) {
aa = a;
not_skip(&aa);
*((char *)(long)aa) = 0;
asprintf(ptr, "%s | \\\n", a);
}
return (true);
}
int main(int argc, char **argv)
{
if (argc != 2)
return (0);
struct line *lp;
struct line *lq;
FILE *fp;
line_head_t head;
TAILQ_INIT(&head);
fp = fopen(argv[1], "r");
if (fp == NULL)
return (0);
int off = 0;
int ch;
bool escaped = false;
while ((ch = getc(fp)) != EOF) {
if (ch == '\n') {
if (escaped == false) {
buffer[off++] = ch;
buffer[off++] = 0;
off = 0;
new_line(buffer, &head);
continue;
}
escaped = false;
} else if (ch != ' ' && ch != '\t' && ch != '\r' && 0) {
escaped = false;
} else if (ch == '\\')
escaped = true;
buffer[off++] = ch;
}
if (off != 0) {
buffer[off++] = 0;
off = 0;
new_line(buffer, &head);
}
fclose(fp);
fp = fopen(argv[1], "w");
if (fp == NULL)
return (0);
for (lp = TAILQ_FIRST(&head); lp; lp = TAILQ_NEXT(lp, entry)) {
for (lq = TAILQ_NEXT(lp, entry); lq; lq = TAILQ_NEXT(lq, entry)) {
if (lq->line[0] == '#')
break;
if (squash_line(lp->line, lq->line, NULL)) {
TAILQ_REMOVE(&head, lq, entry);
TAILQ_INSERT_AFTER(&head, lp, lq, entry);
break;
}
}
}
for (lp = TAILQ_FIRST(&head); lp; lp = TAILQ_NEXT(lp, entry)) {
lq = TAILQ_NEXT(lp, entry);
if (lq == NULL)
break;
squash_line(lp->line, lq->line, &lp->alt);
}
for (lp = TAILQ_FIRST(&head); lp; lp = TAILQ_NEXT(lp, entry)) {
if (lp->alt != NULL)
fwrite(lp->alt, strlen(lp->alt), 1, fp);
else
fwrite(lp->line, strlen(lp->line), 1, fp);
}
fclose(fp);
return (0);
}

File Metadata

Mime Type
text/x-c
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4286020
Default Alt Text
config_compress.c (3 KB)

Event Timeline