ref: 140bd854dc4d2fba8083929d3688fdc072e47379
parent: 65520b32b06bc03f4dd23383a5e064d4be1107e9
author: Ali Gholami Rudi <ali@rudi.ir>
date: Thu Jul 25 12:42:41 EDT 2013
in: add .lf and \n(.c
--- a/in.c
+++ b/in.c
@@ -12,6 +12,7 @@
int un; /* number of unread characters */
int pos;
int len;
+ int lnum; /* file line number */
int nl; /* read \n, if the previous char was not */
struct inbuf *prev;
};
@@ -55,11 +56,22 @@
if (fin) {
in_new();
buf->fin = fin;
+ buf->lnum = 1;
if (path)
snprintf(buf->path, sizeof(buf->path) - 1, "%s", path);
}
}
+void in_lf(char *path, int lnum)
+{
+ struct inbuf *cur = buf;
+ while (cur && !cur->fin)
+ cur = cur->prev;
+ if (path)
+ strcpy(cur->path, path);
+ cur->lnum = lnum;
+}
+
void in_queue(char *path)
{
if (nfiles < NFILES)
@@ -110,8 +122,11 @@
return '\n';
if (buf->buf && buf->pos < buf->len)
break;
- if (!buf->buf && (c = getc(buf->fin)) >= 0)
+ if (!buf->buf && (c = getc(buf->fin)) >= 0) {
+ if (c == '\n')
+ buf->lnum++;
return c;
+ }
in_pop();
}
return buf ? (unsigned char) buf->buf[buf->pos++] : -1;
@@ -166,6 +181,14 @@
while (cur && !cur->fin)
cur = cur->prev;
return cur && cur->path[0] ? cur->path : "-";
+}
+
+int in_lnum(void)
+{
+ struct inbuf *cur = buf;
+ while (cur && !cur->fin)
+ cur = cur->prev;
+ return cur->lnum;
}
static char **args_init(char **args)
--- a/reg.c
+++ b/reg.c
@@ -55,6 +55,9 @@
case REG('.', 'b'):
sprintf(numbuf, "%d", dev_getbd(n_f));
break;
+ case REG('.', 'c'):
+ sprintf(numbuf, "%d", in_lnum());
+ break;
case REG('.', 'k'):
sprintf(numbuf, "%d", f_hpos());
break;
--- a/roff.h
+++ b/roff.h
@@ -145,6 +145,7 @@
void in_so(char *path); /* .so request */
void in_nx(char *path); /* .nx request */
void in_ex(void); /* .nx request */
+void in_lf(char *path, int ln); /* .lf request */
void in_queue(char *path); /* .ex request */
char *in_arg(int i); /* look up argument */
int in_nargs(void); /* number of arguments */
@@ -151,6 +152,7 @@
void in_back(int c); /* push back input character */
int in_top(void); /* the first pushed-back character */
char *in_filename(void); /* current filename */
+int in_lnum(void); /* current line number */
void cp_blk(int skip); /* skip or read the next line or block */
void cp_wid(int enable); /* control inlining \w requests */
--- a/tr.c
+++ b/tr.c
@@ -502,6 +502,11 @@
}
}
+static void tr_lf(char **args)
+{
+ in_lf(args[2], eval(args[1], 0));
+}
+
static char *arg_regname(char *s, int len)
{
char *e = n_cp ? s + 2 : s + len;
@@ -710,6 +715,7 @@
{"in", tr_in},
{"it", tr_it},
{"kn", tr_kn},
+ {"lf", tr_lf},
{"lg", tr_lg},
{"ll", tr_ll},
{"ls", tr_ls},