2 /* Copyright (C) 1989-2014 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 3 of the License, or
10 (at your option) any later version.
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "dictionary.h"
26 object_dictionary number_reg_dictionary(101);
28 int reg::get_value(units * /*d*/)
35 error("can't increment read-only register");
40 error("can't decrement read-only register");
43 void reg::set_increment(units /*n*/)
45 error("can't auto increment read-only register");
48 void reg::alter_format(char /*f*/, int /*w*/)
50 error("can't alter format of read-only register");
53 const char *reg::get_format()
58 void reg::set_value(units /*n*/)
60 error("can't write read-only register");
63 general_reg::general_reg() : format('1'), width(0), inc(0)
67 static char uppercase_array[] = {
68 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
69 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
70 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
74 static char lowercase_array[] = {
75 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
76 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
77 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
81 static const char *number_value_to_ascii(int value, char format, int width)
83 static char buf[128]; // must be at least 21
88 else if (width > int(sizeof(buf) - 2))
89 sprintf(buf, "%.*d", int(sizeof(buf) - 2), int(value));
91 sprintf(buf, "%.*d", width, int(value));
97 // troff uses z and w to represent 10000 and 5000 in Roman
98 // numerals; I can find no historical basis for this usage
99 const char *s = format == 'i' ? "zwmdclxvi" : "ZWMDCLXVI";
101 if (n >= 40000 || n <= -40000) {
102 error("magnitude of `%1' too big for i or I format", n);
118 for (int i = 1000; i > 0; i /= 10, s += 2) {
175 // this is a bit tricky
182 *p++ = format == 'a' ? lowercase_array[d - 1] :
183 uppercase_array[d - 1];
186 char *q = buf[0] == '-' ? buf + 1 : buf;
204 const char *general_reg::get_string()
209 return number_value_to_ascii(n, format, width);
213 void general_reg::increment()
220 void general_reg::decrement()
227 void general_reg::set_increment(units n)
232 void general_reg::alter_format(char f, int w)
238 static const char *number_format_to_ascii(char format, int width)
244 if (n > int(sizeof(buf)) - 1)
245 n = int(sizeof(buf)) - 1;
246 sprintf(buf, "%.*d", n, 0);
259 const char *general_reg::get_format()
261 return number_format_to_ascii(format, width);
264 class number_reg : public general_reg {
268 int get_value(units *);
269 void set_value(units);
272 number_reg::number_reg() : value(0)
276 int number_reg::get_value(units *res)
282 void number_reg::set_value(units n)
287 variable_reg::variable_reg(units *p) : ptr(p)
291 void variable_reg::set_value(units n)
296 int variable_reg::get_value(units *res)
302 void define_number_reg()
304 symbol nm = get_name(1);
309 reg *r = (reg *)number_reg_dictionary.lookup(nm);
312 if (!r || !r->get_value(&prev_value))
314 if (get_number(&v, 'u', prev_value)) {
317 number_reg_dictionary.define(nm, r);
320 if (tok.space() && has_arg() && get_number(&v, 'u'))
327 void inline_define_reg()
331 if (!start.delimiter(1))
334 symbol nm = get_name(1);
337 reg *r = (reg *)number_reg_dictionary.lookup(nm);
340 number_reg_dictionary.define(nm, r);
344 if (!r->get_value(&prev_value))
346 if (get_number(&v, 'u', prev_value)) {
349 if (get_number(&v, 'u')) {
352 warning(WARN_DELIM, "closing delimiter does not match");
359 void set_number_reg(symbol nm, units n)
361 reg *r = (reg *)number_reg_dictionary.lookup(nm);
364 number_reg_dictionary.define(nm, r);
369 reg *lookup_number_reg(symbol nm)
371 reg *r = (reg *)number_reg_dictionary.lookup(nm);
373 warning(WARN_REG, "number register `%1' not defined", nm.contents());
375 number_reg_dictionary.define(nm, r);
382 symbol nm = get_name(1);
387 reg *r = (reg *)number_reg_dictionary.lookup(nm);
390 number_reg_dictionary.define(nm, r);
399 } while (csdigit(tok.ch()));
400 r->alter_format('1', n);
402 else if (c == 'i' || c == 'I' || c == 'a' || c == 'A')
404 else if (tok.newline() || tok.eof())
405 warning(WARN_MISSING, "missing number register format");
407 error("bad number register format (got %1)", tok.description());
414 symbol s = get_name();
417 number_reg_dictionary.remove(s);
424 symbol s1 = get_name(1);
426 symbol s2 = get_name(1);
428 if (!number_reg_dictionary.alias(s1, s2))
429 warning(WARN_REG, "number register `%1' not defined", s2.contents());
437 symbol s1 = get_name(1);
439 symbol s2 = get_name(1);
441 number_reg_dictionary.rename(s1, s2);
446 void print_number_regs()
448 object_dictionary_iterator iter(number_reg_dictionary);
451 while (iter.get(&s, (object **)&r)) {
452 assert(!s.is_null());
453 errprint("%1\t", s.contents());
454 const char *p = r->get_string();
463 void init_reg_requests()
465 init_request("rr", remove_reg);
466 init_request("nr", define_number_reg);
467 init_request("af", alter_format);
468 init_request("aln", alias_reg);
469 init_request("rnn", rename_reg);
470 init_request("pnr", print_number_regs);