1 /* copy-mode character interpretation */
7 static int cp_blkdep
; /* input block depth (text in \{ and \}) */
8 static int cp_cpmode
; /* disable the interpretation \w and \E */
9 static int cp_reqln
; /* a request line; replace \{ with an space */
10 static int cp_reqdep
; /* the block depth of current request line */
12 static void cparg(char *d
, int len
)
19 } else if (!n_cp
&& c
== '[') {
21 while (i
< NMLEN
- 1 && c
>= 0 && c
!= ']') {
31 static int regid(void)
34 cparg(regname
, sizeof(regname
));
38 /* interpolate \n(xy */
39 static void cp_num(void)
43 if (c
!= '-' && c
!= '+')
46 if (c
== '-' || c
== '+')
47 num_inc(id
, c
== '+');
49 in_push(num_str(id
), NULL
);
52 /* interpolate \*(xy */
53 static void cp_str(void)
57 char *args
[NARGS
] = {NULL
};
58 cparg(arg
, sizeof(arg
));
59 if (strchr(arg
, ' ')) {
61 sstr_push(strchr(arg
, ' ') + 1);
62 tr_readargs(args
, &sbuf
, sstr_next
, sstr_back
);
64 *strchr(arg
, ' ') = '\0';
65 if (str_get(map(arg
)))
66 in_push(str_get(map(arg
)), args
);
69 if (str_get(map(arg
)))
70 in_push(str_get(map(arg
)), NULL
);
74 /* interpolate \g(xy */
75 static void cp_numfmt(void)
77 in_push(num_getfmt(regid()), NULL
);
81 static void cp_arg(void)
86 cparg(argname
, sizeof(argname
));
87 argnum
= atoi(argname
);
88 if (argnum
> 0 && argnum
< NARGS
+ 1)
94 /* interpolate \w'xyz' */
95 static void cp_width(void)
98 sprintf(wid
, "%d", ren_wid(cp_next
, cp_back
));
102 /* define a register as \R'xyz expr' */
103 static void cp_numdef(void)
107 quotednext(arg
, cp_next
, cp_back
);
109 while (*s
&& *s
!= ' ')
114 num_set(map(arg
), eval_re(s
, num_get(map(arg
)), 'u'));
117 /* conditional interpolation as \?'cond@expr1@expr2@' */
118 static void cp_cond(void)
121 char delim
[GNLEN
], cs
[GNLEN
];
125 quotednext(arg
, cp_next
, cp_back
);
126 n
= eval_up(&s
, '\0');
127 if (charread(&s
, delim
) < 0)
129 if (!strcmp(delim
, "\\&") && charread(&s
, delim
) < 0)
133 while (charread_delim(&s
, cs
, delim
) >= 0)
138 while (charread_delim(&s
, cs
, delim
) >= 0)
141 in_push(n
> 0 ? s1
: s2
, NULL
);
144 static int cp_raw(void)
172 if (c
== '}' && !cp_cpmode
) {
176 if (c
== '{' && !cp_cpmode
) {
178 return cp_reqln
? ' ' : cp_raw();
196 if (c
== 'E' && !cp_cpmode
)
199 while (c
>= 0 && c
!= '\n')
201 } else if (c
== 'w' && !cp_cpmode
) {
204 } else if (c
== 'n') {
207 } else if (c
== '*') {
210 } else if (c
== 'g') {
213 } else if (c
== '$') {
216 } else if (c
== 'R' && !cp_cpmode
) {
219 } else if (c
== '?' && !cp_cpmode
) {
230 void cp_blk(int skip
)
234 while (c
>= 0 && (c
!= '\n' || cp_blkdep
> cp_reqdep
))
238 while ((c
== ' ' || c
== '\t') && cp_blkdep
<= cp_reqdep
)
240 /* push back if the space is not inserted because of cp_reqln */
241 if (c
!= ' ' && c
!= '\t')
246 void cp_copymode(int mode
)
251 /* beginning of a request line; replace \{ with a space until an EOL */
252 void cp_reqline(void)
255 cp_reqdep
= cp_blkdep
;