1 /* copy-mode character interpretation */
7 static int cp_blkdep
; /* input block depth (text in \{ and \}) */
8 static int cp_cpmode
; /* disable the interpretation of \w and \E */
9 static int cp_reqdep
; /* the block depth of current request line */
11 /* just like cp_next(), but remove c_ni characters */
12 static int cp_noninext(void)
20 /* return 1 if \*[] includes a space */
21 static int cparg(char *d
, int len
)
23 int c
= cp_noninext();
26 i
+= utf8next(d
+ i
, cp_noninext
);
27 i
+= utf8next(d
+ i
, cp_noninext
);
28 } else if (!n_cp
&& c
== '[') {
30 while (c
>= 0 && c
!= ']' && c
!= ' ') {
39 utf8next(d
, cp_noninext
);
44 static int regid(void)
47 cparg(regname
, sizeof(regname
));
51 /* interpolate \n(xy */
52 static void cp_num(void)
55 int c
= cp_noninext();
56 if (c
!= '-' && c
!= '+')
59 if (c
== '-' || c
== '+')
60 num_inc(id
, c
== '+');
62 in_push(num_str(id
), NULL
);
65 /* interpolate \*(xy */
66 static void cp_str(void)
69 char *args
[NARGS
+ 1] = {NULL
};
70 if (cparg(reg
, sizeof(reg
))) {
71 char *buf
= tr_args(args
, ']', cp_noninext
, cp_back
);
73 if (str_get(map(reg
)))
74 in_push(str_get(map(reg
)), args
);
77 if (str_get(map(reg
)))
78 in_push(str_get(map(reg
)), NULL
);
82 /* interpolate \g(xy */
83 static void cp_numfmt(void)
85 in_push(num_getfmt(regid()), NULL
);
89 static void cp_arg(void)
94 cparg(argname
, sizeof(argname
));
95 argnum
= atoi(argname
);
96 if (argnum
> 0 && argnum
< NARGS
+ 1)
102 /* interpolate \w'xyz' */
103 static void cp_width(void)
106 sprintf(wid
, "%d", ren_wid(cp_next
, cp_back
));
110 /* define a register as \R'xyz expr' */
111 static void cp_numdef(void)
113 char *arg
= quotednext(cp_noninext
, cp_back
);
115 while (*s
&& *s
!= ' ')
122 num_set(map(arg
), eval_re(s
, num_get(map(arg
)), 'u'));
126 /* conditional interpolation as \?'cond@expr1@expr2@' */
127 static void cp_cond(void)
129 char delim
[GNLEN
], cs
[GNLEN
];
133 char *arg
= quotednext(cp_noninext
, cp_back
);
135 n
= eval_up(&s
, '\0');
136 if (charread(&s
, delim
) < 0) {
140 if (!strcmp(delim
, "\\&") && charread(&s
, delim
) < 0) {
146 while (charread_delim(&s
, cs
, delim
) >= 0)
151 while (charread_delim(&s
, cs
, delim
) >= 0)
154 in_push(n
> 0 ? s1
: s2
, NULL
);
158 static int cp_raw(void)
186 /* replace \{ and \} with a space if not in copy mode */
187 if (c
== '}' && !cp_cpmode
) {
191 if (c
== '{' && !cp_cpmode
) {
209 if (c
== 'E' && !cp_cpmode
)
212 while (c
>= 0 && c
!= '\n')
214 } else if (c
== 'w' && !cp_cpmode
) {
217 } else if (c
== 'n') {
220 } else if (c
== '*') {
223 } else if (c
== 'g') {
226 } else if (c
== '$') {
229 } else if (c
== 'R' && !cp_cpmode
) {
232 } else if (c
== '?' && !cp_cpmode
) {
243 void cp_blk(int skip
)
247 while (c
>= 0 && (c
!= '\n' || cp_blkdep
> cp_reqdep
))
253 /* push back if the space is not inserted due to \{ and \} */
259 void cp_copymode(int mode
)
264 /* beginning of a request; save current cp_blkdep */
267 cp_reqdep
= cp_blkdep
;