2 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2004
3 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.com)
6 This file is part of groff.
8 groff is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 groff is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with groff; see the file COPYING. If not, write to the Free Software
20 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include "dictionary.h"
28 object_dictionary
number_reg_dictionary(101);
30 int reg::get_value(units
* /*d*/)
37 error("can't increment read-only register");
42 error("can't decrement read-only register");
45 void reg::set_increment(units
/*n*/)
47 error("can't auto increment read-only register");
50 void reg::alter_format(char /*f*/, int /*w*/)
52 error("can't alter format of read-only register");
55 const char *reg::get_format()
60 void reg::set_value(units
/*n*/)
62 error("can't write read-only register");
65 general_reg::general_reg() : format('1'), width(0), inc(0)
69 static char uppercase_array
[] = {
70 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
71 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
72 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
76 static char lowercase_array
[] = {
77 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
78 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
79 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
83 static const char *number_value_to_ascii(int value
, char format
, int width
)
85 static char buf
[128]; // must be at least 21
90 else if (width
> int(sizeof(buf
) - 2))
91 sprintf(buf
, "%.*d", int(sizeof(buf
) - 2), int(value
));
93 sprintf(buf
, "%.*d", width
, int(value
));
99 // troff uses z and w to represent 10000 and 5000 in Roman
100 // numerals; I can find no historical basis for this usage
101 const char *s
= format
== 'i' ? "zwmdclxvi" : "ZWMDCLXVI";
103 if (n
>= 40000 || n
<= -40000) {
104 error("magnitude of `%1' too big for i or I format", n
);
120 for (int i
= 1000; i
> 0; i
/= 10, s
+= 2) {
177 // this is a bit tricky
184 *p
++ = format
== 'a' ? lowercase_array
[d
- 1] :
185 uppercase_array
[d
- 1];
188 char *q
= buf
[0] == '-' ? buf
+ 1 : buf
;
206 const char *general_reg::get_string()
211 return number_value_to_ascii(n
, format
, width
);
215 void general_reg::increment()
222 void general_reg::decrement()
229 void general_reg::set_increment(units n
)
234 void general_reg::alter_format(char f
, int w
)
240 static const char *number_format_to_ascii(char format
, int width
)
246 if (n
> int(sizeof(buf
)) - 1)
247 n
= int(sizeof(buf
)) - 1;
248 sprintf(buf
, "%.*d", n
, 0);
261 const char *general_reg::get_format()
263 return number_format_to_ascii(format
, width
);
266 class number_reg
: public general_reg
{
270 int get_value(units
*);
271 void set_value(units
);
274 number_reg::number_reg() : value(0)
278 int number_reg::get_value(units
*res
)
284 void number_reg::set_value(units n
)
289 variable_reg::variable_reg(units
*p
) : ptr(p
)
293 void variable_reg::set_value(units n
)
298 int variable_reg::get_value(units
*res
)
304 void define_number_reg()
306 symbol nm
= get_name(1);
311 reg
*r
= (reg
*)number_reg_dictionary
.lookup(nm
);
314 if (!r
|| !r
->get_value(&prev_value
))
316 if (get_number(&v
, 'u', prev_value
)) {
319 number_reg_dictionary
.define(nm
, r
);
322 if (tok
.space() && has_arg() && get_number(&v
, 'u'))
329 void inline_define_reg()
333 if (!start
.delimiter(1))
336 symbol nm
= get_name(1);
339 reg
*r
= (reg
*)number_reg_dictionary
.lookup(nm
);
342 number_reg_dictionary
.define(nm
, r
);
346 if (!r
->get_value(&prev_value
))
348 if (get_number(&v
, 'u', prev_value
)) {
351 if (get_number(&v
, 'u')) {
354 warning(WARN_DELIM
, "closing delimiter does not match");
361 void set_number_reg(symbol nm
, units n
)
363 reg
*r
= (reg
*)number_reg_dictionary
.lookup(nm
);
366 number_reg_dictionary
.define(nm
, r
);
371 reg
*lookup_number_reg(symbol nm
)
373 reg
*r
= (reg
*)number_reg_dictionary
.lookup(nm
);
375 warning(WARN_REG
, "number register `%1' not defined", nm
.contents());
377 number_reg_dictionary
.define(nm
, r
);
384 symbol nm
= get_name(1);
389 reg
*r
= (reg
*)number_reg_dictionary
.lookup(nm
);
392 number_reg_dictionary
.define(nm
, r
);
401 } while (csdigit(tok
.ch()));
402 r
->alter_format('1', n
);
404 else if (c
== 'i' || c
== 'I' || c
== 'a' || c
== 'A')
406 else if (tok
.newline() || tok
.eof())
407 warning(WARN_MISSING
, "missing number register format");
409 error("bad number register format (got %1)", tok
.description());
416 symbol s
= get_name();
419 number_reg_dictionary
.remove(s
);
426 symbol s1
= get_name(1);
428 symbol s2
= get_name(1);
430 if (!number_reg_dictionary
.alias(s1
, s2
))
431 warning(WARN_REG
, "number register `%1' not defined", s2
.contents());
439 symbol s1
= get_name(1);
441 symbol s2
= get_name(1);
443 number_reg_dictionary
.rename(s1
, s2
);
448 void print_number_regs()
450 object_dictionary_iterator
iter(number_reg_dictionary
);
453 while (iter
.get(&s
, (object
**)&r
)) {
454 assert(!s
.is_null());
455 errprint("%1\t", s
.contents());
456 const char *p
= r
->get_string();
465 void init_reg_requests()
467 init_request("rr", remove_reg
);
468 init_request("nr", define_number_reg
);
469 init_request("af", alter_format
);
470 init_request("aln", alias_reg
);
471 init_request("rnn", rename_reg
);
472 init_request("pnr", print_number_regs
);