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 static void cparg(char *d
, int len
)
22 int c
= cp_noninext();
25 d
[i
++] = cp_noninext();
26 d
[i
++] = cp_noninext();
27 } else if (!n_cp
&& c
== '[') {
29 while (i
< NMLEN
- 1 && c
>= 0 && c
!= ']') {
39 static int regid(void)
42 cparg(regname
, sizeof(regname
));
46 /* interpolate \n(xy */
47 static void cp_num(void)
50 int c
= cp_noninext();
51 if (c
!= '-' && c
!= '+')
54 if (c
== '-' || c
== '+')
55 num_inc(id
, c
== '+');
57 in_push(num_str(id
), NULL
);
60 /* interpolate \*(xy */
61 static void cp_str(void)
65 char *args
[NARGS
] = {NULL
};
66 cparg(arg
, sizeof(arg
));
67 if (strchr(arg
, ' ')) {
69 sstr_push(strchr(arg
, ' ') + 1);
70 tr_readargs(args
, &sbuf
, sstr_next
, sstr_back
);
72 *strchr(arg
, ' ') = '\0';
73 if (str_get(map(arg
)))
74 in_push(str_get(map(arg
)), args
);
77 if (str_get(map(arg
)))
78 in_push(str_get(map(arg
)), 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)
115 quotednext(arg
, cp_noninext
, cp_back
);
117 while (*s
&& *s
!= ' ')
122 num_set(map(arg
), eval_re(s
, num_get(map(arg
)), 'u'));
125 /* conditional interpolation as \?'cond@expr1@expr2@' */
126 static void cp_cond(void)
129 char delim
[GNLEN
], cs
[GNLEN
];
133 quotednext(arg
, cp_noninext
, cp_back
);
134 n
= eval_up(&s
, '\0');
135 if (charread(&s
, delim
) < 0)
137 if (!strcmp(delim
, "\\&") && charread(&s
, delim
) < 0)
141 while (charread_delim(&s
, cs
, delim
) >= 0)
146 while (charread_delim(&s
, cs
, delim
) >= 0)
149 in_push(n
> 0 ? s1
: s2
, NULL
);
152 static int cp_raw(void)
180 /* replace \{ and \} with a space if not in copy mode */
181 if (c
== '}' && !cp_cpmode
) {
185 if (c
== '{' && !cp_cpmode
) {
203 if (c
== 'E' && !cp_cpmode
)
206 while (c
>= 0 && c
!= '\n')
208 } else if (c
== 'w' && !cp_cpmode
) {
211 } else if (c
== 'n') {
214 } else if (c
== '*') {
217 } else if (c
== 'g') {
220 } else if (c
== '$') {
223 } else if (c
== 'R' && !cp_cpmode
) {
226 } else if (c
== '?' && !cp_cpmode
) {
237 void cp_blk(int skip
)
241 while (c
>= 0 && (c
!= '\n' || cp_blkdep
> cp_reqdep
))
245 while ((c
== ' ' || c
== '\t'))
247 /* push back if the space is not inserted due to \{ and \} */
248 if (c
!= ' ' && c
!= '\t')
253 void cp_copymode(int mode
)
258 /* beginning of a request; save current cp_blkdep */
261 cp_reqdep
= cp_blkdep
;