2 /* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.com)
5 This file is part of groff.
7 groff is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 groff is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with groff; see the file COPYING. If not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, 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 const char *number_value_to_ascii(int value
, char format
, int width
)
71 static char buf
[128]; // must be at least 21
76 else if (width
> sizeof(buf
) - 2)
77 sprintf(buf
, "%.*d", sizeof(buf
) - 2, int(value
));
79 sprintf(buf
, "%.*d", width
, int(value
));
85 // troff uses z and w to represent 10000 and 5000 in Roman
86 // numerals; I can find no historical basis for this usage
87 const char *s
= format
== 'i' ? "zwmdclxvi" : "ZWMDCLXVI";
89 if (n
>= 40000 || n
<= -40000) {
90 error("magnitude of `%1' too big for i or I format", n
);
106 for (int i
= 1000; i
> 0; i
/= 10, s
+= 2) {
163 // this is a bit tricky
170 *p
++ = format
+ d
- 1;
173 char *q
= buf
[0] == '-' ? buf
+1 : buf
;
191 const char *general_reg::get_string()
196 return number_value_to_ascii(n
, format
, width
);
200 void general_reg::increment()
207 void general_reg::decrement()
214 void general_reg::set_increment(units n
)
219 void general_reg::alter_format(char f
, int w
)
225 static const char *number_format_to_ascii(char format
, int width
)
231 if (n
> int(sizeof(buf
)) - 1)
232 n
= int(sizeof(buf
)) - 1;
233 sprintf(buf
, "%.*d", n
, 0);
246 const char *general_reg::get_format()
248 return number_format_to_ascii(format
, width
);
251 class number_reg
: public general_reg
{
255 int get_value(units
*);
256 void set_value(units
);
259 number_reg::number_reg() : value(0)
263 int number_reg::get_value(units
*res
)
269 void number_reg::set_value(units n
)
274 variable_reg::variable_reg(units
*p
) : ptr(p
)
278 void variable_reg::set_value(units n
)
283 int variable_reg::get_value(units
*res
)
289 void define_number_reg()
291 symbol nm
= get_name(1);
296 reg
*r
= (reg
*)number_reg_dictionary
.lookup(nm
);
299 if (!r
|| !r
->get_value(&prev_value
))
301 if (get_number(&v
, 'u', prev_value
)) {
304 number_reg_dictionary
.define(nm
, r
);
307 if (tok
.space() && has_arg() && get_number(&v
, 'u'))
314 void inline_define_reg()
318 if (!start
.delimiter(1))
321 symbol nm
= get_name(1);
324 reg
*r
= (reg
*)number_reg_dictionary
.lookup(nm
);
327 number_reg_dictionary
.define(nm
, r
);
331 if (!r
->get_value(&prev_value
))
333 if (get_number(&v
, 'u', prev_value
)) {
336 if (get_number(&v
, 'u')) {
339 warning(WARN_DELIM
, "closing delimiter does not match");
346 void set_number_reg(symbol nm
, units n
)
348 reg
*r
= (reg
*)number_reg_dictionary
.lookup(nm
);
351 number_reg_dictionary
.define(nm
, r
);
356 reg
*lookup_number_reg(symbol nm
)
358 reg
*r
= (reg
*)number_reg_dictionary
.lookup(nm
);
360 warning(WARN_REG
, "number register `%1' not defined", nm
.contents());
362 number_reg_dictionary
.define(nm
, r
);
369 symbol nm
= get_name(1);
374 reg
*r
= (reg
*)number_reg_dictionary
.lookup(nm
);
377 number_reg_dictionary
.define(nm
, r
);
386 } while (csdigit(tok
.ch()));
387 r
->alter_format('1', n
);
389 else if (c
== 'i' || c
== 'I' || c
== 'a' || c
== 'A')
391 else if (tok
.newline() || tok
.eof())
392 warning(WARN_MISSING
, "missing number register format");
394 error("bad number register format (got %1)", tok
.description());
401 symbol s
= get_name();
404 number_reg_dictionary
.remove(s
);
411 symbol s1
= get_name(1);
413 symbol s2
= get_name(1);
415 if (!number_reg_dictionary
.alias(s1
, s2
))
416 warning(WARN_REG
, "number register `%1' not defined", s2
.contents());
424 symbol s1
= get_name(1);
426 symbol s2
= get_name(1);
428 number_reg_dictionary
.rename(s1
, s2
);
433 void print_number_regs()
435 object_dictionary_iterator
iter(number_reg_dictionary
);
438 while (iter
.get(&s
, (object
**)&r
)) {
439 assert(!s
.is_null());
440 errprint("%1\t", s
.contents());
441 const char *p
= r
->get_string();
450 void init_reg_requests()
452 init_request("rr", remove_reg
);
453 init_request("nr", define_number_reg
);
454 init_request("af", alter_format
);
455 init_request("aln", alias_reg
);
456 init_request("rnn", rename_reg
);
457 init_request("pnr", print_number_regs
);