loctrack.c
1.39 KB
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
#include <mailutils/mailutils.h>
#include <mailutils/locus.h>
int
main (int argc, char **argv)
{
unsigned long max_lines;
char *end;
mu_locus_track_t trk;
int rc;
char *buf = NULL;
size_t size, n;
mu_set_program_name (argv[0]);
mu_stdstream_setup (MU_STDSTREAM_RESET_NONE);
if (argc != 3)
{
mu_error ("usage: %s FILE LINES", mu_program_name);
return 1;
}
max_lines = strtoul (argv[2], &end, 10);
if (*end || max_lines == 0)
{
mu_error ("invalid number of lines");
return 1;
}
MU_ASSERT (mu_locus_track_create (&trk, argv[1], max_lines));
while ((rc = mu_stream_getline (mu_strin, &buf, &size, &n)) == 0 && n > 0)
{
struct mu_locus_range lr;
char *tok;
n = mu_rtrim_class (buf, MU_CTYPE_SPACE);
if (n == 0)
continue;
if (buf[0] == '\\' && buf[1] == '-')
{
long x = strtol (buf+2, &end, 10);
if (*end || x == 0)
{
mu_error ("bad number");
continue;
}
rc = mu_locus_tracker_retreat (trk, x);
if (rc == ERANGE)
mu_error ("retreat count too big");
else if (rc)
mu_diag_funcall (MU_DIAG_ERROR, "mu_locus_tracker_retreat", buf+2,
rc);
}
else
{
mu_c_str_unescape (buf, "\\\n", "\\n", &tok);
mu_locus_tracker_advance (trk, &lr, tok, strlen (tok));
free (tok);
mu_stream_lprintf (mu_strout, &lr, "%s\n", buf);
}
}
return 0;
}