#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reshift(const char *in, int nesting);
static void parse_include_directive(FILE *input, int nesting)
{
char filename[32];
// Читаем имя файла в filename, пока не встретим '>'.
for (size_t i = 0; i < sizeof(filename) / sizeof(filename[0]); i++)
{
if (c == EOF)
{
fprintf(stderr
, "unexpected EOF in include directive\n"); }
if (c == '>')
{
// Нашли конец имени. Используем и выходим.
filename[i] = '\0';
reshift(filename, nesting + 1);
return;
}
filename[i] = c;
}
fprintf(stderr
, "too long filename\n"); }
void reshift(const char *in, int nesting)
{
if (nesting > 5)
{
fprintf(stderr
, "recursion too deep\n"); }
FILE
*input
= fopen(in
, "r"); if (!input)
{
perror("unable to open included file"); }
const char include_prefix[] = "include<";
int c;
size_t prefix_pos = 0;
while ((c
= fgetc(input
)) != EOF
) {
// Если символ - начало или продолжение префикса, следующий символ
// будем матчить со следующим символом префикса.
if (c == include_prefix[prefix_pos])
{
++prefix_pos;
// Если префикс кончился, обрабатываем имя.
if (include_prefix[prefix_pos] == '\0')
{
// Оно само сожрет имя файла и >.
parse_include_directive(input, nesting);
prefix_pos = 0;
}
}
else
{
// Матчили-матчили, но не сматчили. Выводим, то, что сматчилось,
// если такое было.
for (size_t i = 0; i < prefix_pos; i++)
{
}
prefix_pos = 0;
// И не сматчившийся символ тоже выводим.
}
}
}
int main(void)
{
reshift("input.txt", 0);
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKdm9pZCByZXNoaWZ0KGNvbnN0IGNoYXIgKmluLCBpbnQgbmVzdGluZyk7CgpzdGF0aWMgdm9pZCBwYXJzZV9pbmNsdWRlX2RpcmVjdGl2ZShGSUxFICppbnB1dCwgaW50IG5lc3RpbmcpCnsKICAgIGNoYXIgZmlsZW5hbWVbMzJdOwoKICAgIC8vINCn0LjRgtCw0LXQvCDQuNC80Y8g0YTQsNC50LvQsCDQsiBmaWxlbmFtZSwg0L/QvtC60LAg0L3QtSDQstGB0YLRgNC10YLQuNC8ICc+Jy4KICAgIGZvciAoc2l6ZV90IGkgPSAwOyBpIDwgc2l6ZW9mKGZpbGVuYW1lKSAvIHNpemVvZihmaWxlbmFtZVswXSk7IGkrKykKICAgIHsKICAgICAgICBpbnQgYyA9IGZnZXRjKGlucHV0KTsKICAgICAgICBpZiAoYyA9PSBFT0YpCiAgICAgICAgewogICAgICAgICAgICBmcHJpbnRmKHN0ZGVyciwgInVuZXhwZWN0ZWQgRU9GIGluIGluY2x1ZGUgZGlyZWN0aXZlXG4iKTsKICAgICAgICAgICAgZXhpdCgxKTsKICAgICAgICB9CgogICAgICAgIGlmIChjID09ICc+JykKICAgICAgICB7CiAgICAgICAgICAgIC8vINCd0LDRiNC70Lgg0LrQvtC90LXRhiDQuNC80LXQvdC4LiDQmNGB0L/QvtC70YzQt9GD0LXQvCDQuCDQstGL0YXQvtC00LjQvC4KICAgICAgICAgICAgZmlsZW5hbWVbaV0gPSAnXDAnOwogICAgICAgICAgICByZXNoaWZ0KGZpbGVuYW1lLCBuZXN0aW5nICsgMSk7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CgogICAgICAgIGZpbGVuYW1lW2ldID0gYzsKICAgIH0KCiAgICBmcHJpbnRmKHN0ZGVyciwgInRvbyBsb25nIGZpbGVuYW1lXG4iKTsKICAgIGV4aXQoMSk7Cn0KCnZvaWQgcmVzaGlmdChjb25zdCBjaGFyICppbiwgaW50IG5lc3RpbmcpCnsKICAgIGlmIChuZXN0aW5nID4gNSkKICAgIHsKICAgICAgICBmcHJpbnRmKHN0ZGVyciwgInJlY3Vyc2lvbiB0b28gZGVlcFxuIik7CiAgICAgICAgZXhpdCgxKTsKICAgIH0KCiAgICBGSUxFICppbnB1dCA9IGZvcGVuKGluLCAiciIpOwogICAgaWYgKCFpbnB1dCkKICAgIHsKICAgICAgICBwZXJyb3IoInVuYWJsZSB0byBvcGVuIGluY2x1ZGVkIGZpbGUiKTsKICAgICAgICBleGl0KDEpOwogICAgfQoKICAgIGNvbnN0IGNoYXIgaW5jbHVkZV9wcmVmaXhbXSA9ICJpbmNsdWRlPCI7CgogICAgaW50IGM7CiAgICBzaXplX3QgcHJlZml4X3BvcyA9IDA7CgogICAgd2hpbGUgKChjID0gZmdldGMoaW5wdXQpKSAhPSBFT0YpCiAgICB7CiAgICAgICAgLy8g0JXRgdC70Lgg0YHQuNC80LLQvtC7IC0g0L3QsNGH0LDQu9C+INC40LvQuCDQv9GA0L7QtNC+0LvQttC10L3QuNC1INC/0YDQtdGE0LjQutGB0LAsINGB0LvQtdC00YPRjtGJ0LjQuSDRgdC40LzQstC+0LsKICAgICAgICAvLyDQsdGD0LTQtdC8INC80LDRgtGH0LjRgtGMINGB0L4g0YHQu9C10LTRg9GO0YnQuNC8INGB0LjQvNCy0L7Qu9C+0Lwg0L/RgNC10YTQuNC60YHQsC4KICAgICAgICBpZiAoYyA9PSBpbmNsdWRlX3ByZWZpeFtwcmVmaXhfcG9zXSkKICAgICAgICB7CiAgICAgICAgICAgICsrcHJlZml4X3BvczsKCiAgICAgICAgICAgIC8vINCV0YHQu9C4INC/0YDQtdGE0LjQutGBINC60L7QvdGH0LjQu9GB0Y8sINC+0LHRgNCw0LHQsNGC0YvQstCw0LXQvCDQuNC80Y8uCiAgICAgICAgICAgIGlmIChpbmNsdWRlX3ByZWZpeFtwcmVmaXhfcG9zXSA9PSAnXDAnKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAvLyDQntC90L4g0YHQsNC80L4g0YHQvtC20YDQtdGCINC40LzRjyDRhNCw0LnQu9CwINC4ID4uCiAgICAgICAgICAgICAgICBwYXJzZV9pbmNsdWRlX2RpcmVjdGl2ZShpbnB1dCwgbmVzdGluZyk7CiAgICAgICAgICAgICAgICBwcmVmaXhfcG9zID0gMDsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICAvLyDQnNCw0YLRh9C40LvQuC3QvNCw0YLRh9C40LvQuCwg0L3QviDQvdC1INGB0LzQsNGC0YfQuNC70LguINCS0YvQstC+0LTQuNC8LCDRgtC+LCDRh9GC0L4g0YHQvNCw0YLRh9C40LvQvtGB0YwsCiAgICAgICAgICAgIC8vINC10YHQu9C4INGC0LDQutC+0LUg0LHRi9C70L4uCiAgICAgICAgICAgIGZvciAoc2l6ZV90IGkgPSAwOyBpIDwgcHJlZml4X3BvczsgaSsrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBwdXRjaGFyKGluY2x1ZGVfcHJlZml4W2ldKTsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgcHJlZml4X3BvcyA9IDA7CgogICAgICAgICAgICAvLyDQmCDQvdC1INGB0LzQsNGC0YfQuNCy0YjQuNC50YHRjyDRgdC40LzQstC+0Lsg0YLQvtC20LUg0LLRi9Cy0L7QtNC40LwuCiAgICAgICAgICAgIHB1dGNoYXIoYyk7CiAgICAgICAgfQogICAgfQp9CgppbnQgbWFpbih2b2lkKQp7CiAgICByZXNoaWZ0KCJpbnB1dC50eHQiLCAwKTsKfQo=