Nincs mit, de ha azzal kezded, hogy az a feladat, hogy "igazából az lesz a program, hogy egy ascii-artot oszlopokra hasogassak és paramétertől függően csal egy részét mutassam rajzoljam ki a szabvány terminál bal oldalán", akkor már rég mondtam volna, hogy ne szenvedj shell-el. Ezt jobb megírni mondjuk C-ben:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
int f2mem(char *path, char **dst_out, size_t *size)
{
FILE *f;
size_t l;
char *buf;
*dst_out = NULL;
f = fopen(path, "rb");
if (f == NULL)
{
return 1;
}
fseek(f, 0, SEEK_END);
l = ftell(f);
buf = malloc(l);
if (buf == NULL)
{
fclose(f);
return 2;
}
fseek(f, 0, SEEK_SET);
fread(buf, 1, l, f);
fclose(f);
*size = l;
*dst_out = buf;
return 0;
}
int main(int argc, char *argv[])
{
int r, i;
char *buf, c, col, row, start, stop;
bool copy;
size_t size;
if (argc != 4)
{
fprintf(stderr, "Usage: getcols <file> <start> <stop>\n");
return 0;
}
start = strtol(argv[2], NULL, 10);
if (errno != 0)
{
fprintf(stderr, "Erroneous start column: %s\n", argv[2]);
return 3;
}
stop = strtol(argv[3], NULL, 10);
if (errno != 0)
{
fprintf(stderr, "Erroneous stop column: %s\n", argv[3]);
return 4;
}
r = f2mem(argv[1], &buf, &size);
if (r == 1)
{
fprintf(stderr, "Cannot open file: %s\n", argv[1]);
return 1;
}
if (r == 2)
{
fprintf(stderr, "Cannot allocate memory.\n");
return 2;
}
i = 0;
row = 0;
col = 0;
copy = false;
while (i < size)
{
c = buf[i++];
if (c == 10)
{
col = 0;
++row;
copy = false;
}
else
{
if ((col == start) && (!copy))
{
copy = true;
}
if (copy)
{
fprintf(stdout, "\033[%d;%dH%c", row, col - start, c);
}
if ((col == stop) && (copy))
{
copy = false;
}
++col;
}
}
free(buf);
}Ez pontosan azt csinálja, amit szeretnél: N db oszlopot kicopy-z a fájlból és kiírja a terminálra. Leteszteltem, működik, persze bug lehet benne.
Sz*rk: Frissítettem a programot: beleraktam, hogy magától mozgassa a képernyő bal felére a tartalmat, escape szekvenciákkal.