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
+ 2] = {reg
};
71 if (cparg(reg
, sizeof(reg
))) {
72 buf
= tr_args(args
+ 1, ']', cp_noninext
, cp_back
);
75 if (str_get(map(reg
)))
76 in_push(str_get(map(reg
)), buf
? args
: NULL
);
78 tr_req(map(reg
), args
);
82 /* interpolate \g(xy */
83 static void cp_numfmt(void)
85 in_push(num_getfmt(regid()), NULL
);
88 /* interpolate \$*, \$@, and \$^ */
89 static void cp_args(int quote
, int escape
)
95 for (i
= 1; i
< in_nargs(); i
++) {
96 sbuf_append(&sb
, i
> 1 ? " " : "");
97 sbuf_append(&sb
, quote
? "\"" : "");
100 sbuf_append(&sb
, escape
&& *s
== '"' ? "\"" : "");
101 sbuf_add(&sb
, (unsigned char) *s
++);
103 sbuf_append(&sb
, quote
? "\"" : "");
105 in_push(sbuf_buf(&sb
), NULL
);
109 /* interpolate \$1 */
110 static void cp_arg(void)
115 cparg(argname
, sizeof(argname
));
116 if (!strcmp("@", argname
)) {
120 if (!strcmp("*", argname
)) {
124 if (!strcmp("^", argname
)) {
128 argnum
= atoi(argname
);
129 if (argnum
>= 0 && argnum
< NARGS
)
130 arg
= in_arg(argnum
);
135 /* interpolate \w'xyz' */
136 static void cp_width(void)
139 sprintf(wid
, "%d", ren_wid(cp_next
, cp_back
));
143 /* define a register as \R'xyz expr' */
144 static void cp_numdef(void)
146 char *arg
= quotednext(cp_noninext
, cp_back
);
148 while (*s
&& *s
!= ' ')
155 num_set(map(arg
), eval_re(s
, num_get(map(arg
)), 'u'));
159 /* conditional interpolation as \?'cond@expr1@expr2@' */
160 static void cp_cond(void)
162 char delim
[GNLEN
], cs
[GNLEN
];
166 char *arg
= quotednext(cp_noninext
, cp_back
);
168 n
= eval_up(&s
, '\0');
169 if (charread(&s
, delim
) < 0) {
173 if (!strcmp(delim
, "\\&") && charread(&s
, delim
) < 0) {
179 while (charread_delim(&s
, cs
, delim
) >= 0)
184 while (charread_delim(&s
, cs
, delim
) >= 0)
187 in_push(n
> 0 ? s1
: s2
, NULL
);
191 static int cp_raw(void)
219 /* replace \{ and \} with a space if not in copy mode */
220 if (c
== '}' && !cp_cpmode
) {
224 if (c
== '{' && !cp_cpmode
) {
242 if (c
== 'E' && !cp_cpmode
)
245 while (c
>= 0 && c
!= '\n')
247 } else if (c
== 'w' && !cp_cpmode
) {
250 } else if (c
== 'n') {
253 } else if (c
== '*') {
256 } else if (c
== 'g') {
259 } else if (c
== '$') {
262 } else if (c
== '?') {
265 } else if (c
== 'R' && !cp_cpmode
) {
276 void cp_blk(int skip
)
280 while (c
>= 0 && (c
!= '\n' || cp_blkdep
> cp_reqdep
))
286 /* push back if the space is not inserted due to \{ and \} */
292 void cp_copymode(int mode
)
297 /* beginning of a request; save current cp_blkdep */
300 cp_reqdep
= cp_blkdep
;