rulex
1/* Simple text markup utility */
2
3/*
4* Copyright (C) 2012 Igor B. Poretsky <poretsky@mlbox.ru>
5*
6* This program is free software; you can redistribute it and/or modify
7* it under the terms of the GNU General Public License as published by
8* the Free Software Foundation; either version 2 of the License, or
9* (at your option) any later version.
10*
11* This program is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14* GNU General Public License for more details.
15*/
16
17
18#include <unistd.h>19#include <stdlib.h>20#include <string.h>21#include <stdio.h>22#include <locale.h>23#include <ctype.h>24#include <limits.h>25#include <sys/types.h>26#include <sys/stat.h>27#include <fcntl.h>28
29#include "lexdb.h"30
31
32#ifdef _WIN3233static const char *charset = "Russian_Russia.20866";34#else35static const char *charset = "ru_RU.koi8r";36#endif37
38static const char symbols[] =39{40'+', '=',410xC1, 0xC2, 0xD7, /* а, б, в, */420xC7, 0xC4, 0xC5, /* г, д, е, */430xA3, 0xD6, 0xDA, /* ё, ж, з, */440xC9, 0xCA, 0xCB, /* и, й, к, */450xCC, 0xCD, 0xCE, /* л, м, н, */460xCF, 0xD0, 0xD2, /* о, п, р, */470xD3, 0xD4, 0xD5, /* с, т, у, */480xC6, 0xC8, 0xC3, /* ф, х, ц, */490xDE, 0xDB, 0xDD, /* ч, ш, щ, */500xDF, 0xD9, 0xD8, /* ъ, ы, ь, */510xDC, 0xC0, 0xD1, /* э, ю, я */52053};54
55static const char *alphabet;56
57static void *xrealloc(void *p, unsigned int n)58{
59void *u = realloc(p, n);60if (u) return u;61else62{63perror("Memory allocation error");64exit(EXIT_FAILURE);65}66}
67
68static void *xmalloc(unsigned int n)69{
70void *u = NULL;71return xrealloc(u, n);72}
73
74int main(int argc, char **argv)75{
76unsigned int size = 64;77char *s, *text;78FILE *slog = NULL;79RULEXDB *db = NULL;80
81if (argc < 2)82{83fprintf(stderr, "Usage:\n");84fprintf(stderr, "%s <rulex_db> [<logfile>]\n", argv[0]);85return EXIT_FAILURE;86}87
88alphabet = symbols + 2;89if (!setlocale(LC_CTYPE, charset))90{91fprintf(stderr, "Cannot set %s locale.\n", charset);92fprintf(stderr, "Probably you have to generate it by command:\n");93fprintf(stderr, "localedef -f KOI8-R -i ru_RU ru_RU.KOI8-R\n");94return EXIT_FAILURE;95}96
97db = rulexdb_open(argv[1], RULEXDB_SEARCH);98if (!db)99{100perror(argv[1]);101return EXIT_FAILURE;102}103
104if (argc > 2)105{106slog = fopen(argv[2], "a");107if (!slog)108perror(argv[2]);109}110
111/* doing text filtering in the loop */112text = xmalloc(size);113s = text;114while (fgets(s, text + size - s, stdin))115{116s = text + strlen(text) - 1;117if (*s == '\n')118{119char *stressed = xmalloc(strlen(text) << 1);120char *t;121unsigned int n;122for (s = text; *s; s++)123if (isupper(*s))124*s = tolower(*s);125s = text;126t = stressed;127while (*s)128{129if ((n = strcspn(s, symbols)))130{131strncpy(t, s, n);132s += n;133t += n;134}135if ((n = strspn(s, symbols)))136{137if ((n <= RULEXDB_MAX_KEY_SIZE) && (n <= strspn(s, alphabet)))138{139char *key = xmalloc(RULEXDB_BUFSIZE);140strncpy(key, s, n);141key[n] = 0;142if (rulexdb_search(db, key, t, 0)143== RULEXDB_SPECIAL)144if (slog) (void)fprintf(slog, "%s\n", key);145free(key);146}147else148{149strncpy(t, s, n);150t[n] = 0;151}152s += n;153t += strlen(t);154}155}156*t = 0;157puts(stressed);158free(stressed);159s = text;160}161else162{163text = xrealloc(text, size <<= 1);164s = text + strlen(text);165}166}167
168rulexdb_close(db);169if (slog)170fclose(slog);171free(text);172
173return EXIT_SUCCESS;174}
175