3 //============================================================================
7 // Implementation of the various CDL utility member functions.
9 //============================================================================
10 //####COPYRIGHTBEGIN####
12 // ----------------------------------------------------------------------------
13 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
15 // This file is part of the eCos host tools.
17 // This program is free software; you can redistribute it and/or modify it
18 // under the terms of the GNU General Public License as published by the Free
19 // Software Foundation; either version 2 of the License, or (at your option)
22 // This program is distributed in the hope that it will be useful, but WITHOUT
23 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
27 // You should have received a copy of the GNU General Public License along with
28 // this program; if not, write to the Free Software Foundation, Inc.,
29 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 // ----------------------------------------------------------------------------
33 //####COPYRIGHTEND####
34 //============================================================================
35 //#####DESCRIPTIONBEGIN####
42 //####DESCRIPTIONEND####
43 //============================================================================
48 // ----------------------------------------------------------------------------
49 #include "cdlconfig.h"
51 // Get the infrastructure types, assertions, tracing and similar
53 #include <cyg/infra/cyg_ass.h>
54 #include <cyg/infra/cyg_trac.h>
56 // <cdlcore.hxx> defines everything implemented in this module.
57 // It implicitly supplies <string>, <vector> and <map> because
58 // the class definitions rely on these headers.
59 #include <cdlcore.hxx>
61 // For access to the isdigit(), isupper(), tolower(), ... functions
64 // For access to sprintf(), specifically double to string conversions.
67 // For access to strtod()
70 // strtod() involves errno...
73 // For access to fmod()
76 // For access to DBL_DIG
81 //{{{ Cdl::is_valid_xxx()
83 // ---------------------------------------------------------------------------
86 Cdl::is_valid_value_flavor(CdlValueFlavor data)
91 case CdlValueFlavor_None :
92 case CdlValueFlavor_Bool :
93 case CdlValueFlavor_BoolData :
94 case CdlValueFlavor_Data :
106 Cdl::is_valid_value_source(CdlValueSource data)
111 case CdlValueSource_Default :
112 case CdlValueSource_User :
113 case CdlValueSource_Wizard :
114 case CdlValueSource_Inferred :
125 // ----------------------------------------------------------------------------
126 // For now CDL names are restricted to what is acceptable to the C
127 // preprocessor. This may cause problems in future, e.g. i18n.
130 Cdl::is_valid_cdl_name(const std::string& name)
132 CYG_REPORT_FUNCNAMETYPE("Cdl::is_valid_cdl_name", "result %d");
134 bool result = is_valid_c_preprocessor_symbol(name);
136 CYG_REPORT_RETVAL(result);
141 Cdl::is_valid_c_preprocessor_symbol(const std::string& symbol)
143 CYG_REPORT_FUNCNAMETYPE("Cdl::is_valid_c_preprocessor_symbol", "result %d");
149 // A valid preprocessor symbol should begin with either an underscore
150 // or a letter. It should then be followed by some number of underscores,
151 // letters, or digits.
153 // In some locales isalpha() may succeed for characters which are not
154 // legal for C preprocessor symbols. Instead ASCII is assumed here.
155 if (('_' != symbol[0]) &&
156 !(('a' <= symbol[0]) && (symbol[0] <= 'z')) &&
157 !(('A' <= symbol[0]) && (symbol[0] <= 'Z'))) {
161 for (unsigned int i = 1; i < symbol.size(); i++) {
162 if (('_' != symbol[i]) &&
163 !(('a' <= symbol[i]) && (symbol[i] <= 'z')) &&
164 !(('A' <= symbol[i]) && (symbol[i] <= 'Z')) &&
165 !(('0' <= symbol[i]) && (symbol[i] <= '9'))) {
174 CYG_REPORT_RETVAL(result);
179 //{{{ Cdl::xxx_to_yyy() - strings, ints, doubles, ...
181 // ---------------------------------------------------------------------------
182 // Conversion routines between strings, integers, doubles, bools, ...
184 // Conversions to/from integers are complicated somewhat because the
185 // data type in question is cdl_int. In the initial implementation this
186 // is 64 bits. In the long term it will be arbitrary precision and
187 // the conversion routines will need to be reimplemented.
189 // ASCII rather than EBCDIC is assumed.
191 // Some of the routines may fail, e.g. string to integer conversions.
192 // Others are guaranteed to succeed.
194 //{{{ string_to_integer()
196 // ----------------------------------------------------------------------------
198 Cdl::string_to_integer(std::string data, cdl_int& target)
200 CYG_REPORT_FUNCNAMETYPE("Cdl::string_to_integer", "success %d");
202 bool negative = false;
203 bool seen_plus = false;
204 bool seen_minus = false;
206 // Life is a bit easier if I can check for '\0'
207 const char* ptr = data.c_str();
209 // Not essential but harmless.
210 while (isspace(*ptr))
216 CYG_REPORT_RETVAL(false);
226 CYG_REPORT_RETVAL(false);
236 // This happens sufficiently often to be worth a special case.
237 if ('\0' == ptr[1]) {
239 CYG_REPORT_RETVAL(true);
242 // Hex is always worth supporting.
243 if (('x' == ptr[1]) || ('X' == ptr[1])) {
245 if (!isxdigit(*ptr)) {
246 CYG_REPORT_RETVAL(false);
249 while (isxdigit(*ptr)) {
250 cdl_int new_acc = acc * 16;
252 new_acc += (*ptr - '0');
253 } else if (('a' <= *ptr) && (*ptr <= 'f')) {
254 new_acc += (*ptr + 10 - 'a');
255 } else if (('A' <= *ptr) && (*ptr <= 'F')) {
256 new_acc += (*ptr + 10 - 'A');
258 CYG_FAIL("this platform's implementation of isxdigit() is broken");
261 CYG_REPORT_RETVAL(false);
268 CYG_REPORT_RETVAL(false);
272 cdl_int new_acc = 0 - acc;
274 CYG_REPORT_RETVAL(false);
281 CYG_REPORT_RETVAL(true);
285 // Octal? Oh well, might as well be complete.
286 if (('0' <= ptr[1]) && (ptr[1] <= '7')) {
289 cdl_int new_acc = 8 * acc;
290 new_acc += (*ptr - '0');
292 CYG_REPORT_RETVAL(false);
297 } while (('0' <= *ptr) && (*ptr <= '7'));
299 CYG_REPORT_RETVAL(false);
303 cdl_int new_acc = 0 - acc;
305 CYG_REPORT_RETVAL(false);
313 CYG_REPORT_RETVAL(true);
317 // Drop through for the case of a decimal.
320 while(isdigit(*ptr)) {
321 cdl_int new_acc = 10 * acc;
322 new_acc += (*ptr - '0');
324 CYG_REPORT_RETVAL(false);
331 CYG_REPORT_RETVAL(false);
335 cdl_int new_acc = 0 - acc;
337 CYG_REPORT_RETVAL(false);
344 CYG_REPORT_RETVAL(true);
349 //{{{ string_to_double()
351 // ----------------------------------------------------------------------------
352 // There is no point in doing this the hard way, just use standard
355 // There is an obvious question as to how much precision can get lost
356 // doing the conversion to a string. In practice this should not matter
357 // too much, since the expression handling code generally keeps the
358 // original double precision lying around to be re-used. However it may
359 // be desirable to keep the libcdl behaviour in synch with Tcl's
360 // tcl_precision variable.
363 Cdl::string_to_double(std::string value, double& target)
365 CYG_REPORT_FUNCNAMETYPE("Cdl::string_to_double", "success %d");
368 const char* start_ptr = value.c_str();
370 int old_errno = errno;
373 double conv = strtod(start_ptr, &end_ptr);
375 CYG_ASSERT(ERANGE == errno, "standard-compliant C library");
377 } else if ('\0' != *end_ptr) {
385 CYG_REPORT_RETVAL(result);
390 //{{{ string_to_bool()
392 // ----------------------------------------------------------------------------
393 // Conversions to and from bools. The only real issue here is exactly
394 // what strings should be accepted as synonyms for true and false.
395 // It is not actually clear that these functions are useful.
397 Cdl::string_to_bool(std::string data, bool& target)
399 CYG_REPORT_FUNCNAMETYPE("Cdl::string_to_bool", "success %d");
401 // Arguably there should be a precondition ( "" != data )
405 if (( data == "1" ) || (data == "true") ||
406 ( data == "True") || (data == "TRUE") ) {
409 } else if ((data == "0" ) || (data == "false") ||
410 (data == "False") || (data == "FALSE") ) {
415 CYG_REPORT_RETVAL(result);
421 //{{{ integer_to_string()
423 // ----------------------------------------------------------------------------
426 Cdl::integer_to_string(cdl_int value, CdlValueFormat format)
429 Cdl::integer_to_string(value, result, format);
434 Cdl::integer_to_string(cdl_int value, std::string& target, CdlValueFormat format)
436 CYG_REPORT_FUNCNAME("Cdl::integer_to_string");
437 CYG_REPORT_FUNCARG2XV((long) value, format);
439 // Optimise this special case.
441 if (CdlValueFormat_Hex == format) {
446 CYG_REPORT_RETVAL(true);
450 // A local buffer to construct partial strings. This avoids
451 // unnecessary std::string reallocation.
452 // 64 bits and three bits at a time for octal numbers gives 21 digits,
453 // plus spares for the leading '0' and the terminator.
455 char *local_ptr = &(local_buf[23]);
458 if (CdlValueFormat_Hex == format) {
460 // Output the data as 0x... with either 8 or 16 digits,
461 // depending on the size.
463 for (i = 0; i < 8; i++) {
464 int tmp = (int) (value & 0x0F);
467 *local_ptr-- = '0' + tmp;
469 *local_ptr-- = 'A' + (tmp - 10);
472 // Beware of right shifts that preserve the sign bit.
474 int tmp1 = (value & 0x0FFFF);
475 int tmp2 = ((value >> 16) & 0x0FFFF);
476 value = (tmp2 << 16) + tmp1;
479 for (i = 0; i < 8; i++) {
480 int tmp = (int) (value & 0x0F);
483 *local_ptr-- = '0' + tmp;
485 *local_ptr-- = 'A' + (tmp - 10);
491 target = std::string(local_ptr);
493 } else if (CdlValueFormat_Octal == format) {
495 // Simply output the data three bits at a time, do not worry about any
496 // particular width restrictions. However it is necessary to worry
498 cdl_int mask = 0x1FFFFFFF;
499 mask = (mask << 16) | 0x0FFFF;
500 mask = (mask << 16) | 0x0FFFF;
504 int tmp = value & 0x07;
505 value = (value >> 3) & mask;
506 *local_ptr-- = '0' + tmp;
509 target = std::string(local_ptr);
512 // A simple decimal number
513 // Switch to positive arithmetic.
514 bool negative = false;
518 // Only MININT cannot be converted using the above line
520 target = "-9223372036854775808";
521 CYG_REPORT_RETVAL(true);
527 int rem = (int) (value % 10);
529 *local_ptr-- = '0' + rem;
535 target = std::string(local_ptr);
543 //{{{ double_to_string()
545 // ----------------------------------------------------------------------------
548 Cdl::double_to_string(double value, CdlValueFormat format)
551 Cdl::double_to_string(value, result, format);
556 Cdl::double_to_string(double value, std::string& result, CdlValueFormat format)
558 CYG_REPORT_FUNCNAME("Cdl::double_to_String");
560 char buf[256]; // This should be plenty :-)
561 sprintf(buf, "%.*G", DBL_DIG, value);
564 CYG_UNUSED_PARAM(CdlValueFormat, format);
569 //{{{ bool_to_string()
571 // ----------------------------------------------------------------------------
572 // Should the string results be 1/0 or true/false? Not that
573 // it really matters. The testcase in cdl1.cxx expects 1/0.
575 Cdl::bool_to_string(bool value)
578 Cdl::bool_to_string(value, result);
583 Cdl::bool_to_string(bool value, std::string& target)
585 CYG_REPORT_FUNCNAME("Cdl::bool_to_string");
586 CYG_REPORT_FUNCARG1( "value arg %ld", (long) value);
598 //{{{ integer_to_double()
600 // ----------------------------------------------------------------------------
601 // Currently integer to double cannot fail, although there may well be loss
602 // of accurary. Eventually cdl_int may be an arbitrary precision integer
603 // in which case conversion to double is not guaranteed.
605 Cdl::integer_to_double(cdl_int value)
607 CYG_REPORT_FUNCNAME("Cdl::integer_to_double");
609 double result = (double) value;
616 Cdl::integer_to_double(cdl_int value, double& target)
618 CYG_REPORT_FUNCNAME("Cdl::integer_to_double");
620 target = (double) value;
626 //{{{ double_to_integer()
628 // Conversion from double to integer is only allowed if there is no loss
629 // of data. modf() is useful here
631 Cdl::double_to_integer(double value, cdl_int& target)
633 CYG_REPORT_FUNCNAMETYPE("Cdl::double_to_integer", "result %d");
640 frac = modf(value, &integral);
642 // Looking good, but integral may still be too big.
643 cdl_int tmp = (cdl_int) integral;
645 // No fraction, no loss of data, everything looking good
651 CYG_REPORT_RETVAL(result);
658 //{{{ Cdl::xxx_to_yyy() - CDL-specific data types
660 // ----------------------------------------------------------------------------
661 // Conversions between strings and flavors.
665 CdlValueFlavor flavor;
666 } valid_flavors[] = {
667 { "none", CdlValueFlavor_None },
668 { "bool", CdlValueFlavor_Bool },
669 { "booldata", CdlValueFlavor_BoolData },
670 { "data", CdlValueFlavor_Data },
671 { 0, CdlValueFlavor_Invalid }
675 Cdl::string_to_flavor(std::string name, CdlValueFlavor& target)
677 CYG_REPORT_FUNCNAMETYPE("Cdl::string_to_flavor", "success %d");
681 // First convert the argument to lower case. Arguably this is incorrect,
682 // Tcl is a case-sensitive language, but the conversion is unlikely ever
684 for (std::string::iterator str_i = name.begin(); str_i != name.end(); str_i++) {
685 if (isupper(*str_i)) {
686 *str_i = tolower(*str_i);
690 // Now look for a match in the table.
693 const char* c_str = name.c_str();
694 int len = strlen(c_str);
696 for (i = 0; 0 != valid_flavors[i].name; i++) {
697 if (0 == strncmp(c_str, valid_flavors[i].name, len)) {
698 // Check for an ambiguous string match.
699 // This cannot actually happen with the current flavor names.
708 target = valid_flavors[match].flavor;
711 CYG_REPORT_RETVAL(result);
716 Cdl::flavor_to_string(CdlValueFlavor flavor, std::string& target)
718 CYG_REPORT_FUNCNAMETYPE("Cdl::flavor_to_string", "success %d");
722 for (int i = 0; 0 != valid_flavors[i].name; i++) {
723 if (flavor == valid_flavors[i].flavor) {
724 target = valid_flavors[i].name;
730 CYG_REPORT_RETVAL(result);
734 // ----------------------------------------------------------------------------
735 // Similar support for value sources.
739 CdlValueSource source;
740 } valid_sources[] = {
741 { "default", CdlValueSource_Default },
742 { "inferred", CdlValueSource_Inferred },
743 { "wizard", CdlValueSource_Wizard },
744 { "user", CdlValueSource_User },
745 { 0, CdlValueSource_Invalid }
749 Cdl::string_to_source(std::string name, CdlValueSource& target)
751 CYG_REPORT_FUNCNAMETYPE("Cdl::string_to_source", "success %d");
755 // First convert the argument to lower case. Arguably this is incorrect,
756 // Tcl is a case-sensitive language, but the conversion is unlikely ever
758 for (std::string::iterator str_i = name.begin(); str_i != name.end(); str_i++) {
759 if (isupper(*str_i)) {
760 *str_i = tolower(*str_i);
764 // Now look for a match in the table.
767 const char* c_str = name.c_str();
768 int len = strlen(c_str);
770 for (i = 0; 0 != valid_sources[i].name; i++) {
771 if (0 == strncmp(c_str, valid_sources[i].name, len)) {
772 // Check for an ambiguous string match.
773 // This cannot actually happen with the current source names.
782 target = valid_sources[match].source;
785 CYG_REPORT_RETVAL(result);
790 Cdl::source_to_string(CdlValueSource source, std::string& target)
792 CYG_REPORT_FUNCNAMETYPE("Cdl::source_to_string", "success %d");
796 for (int i = 0; 0 != valid_sources[i].name; i++) {
797 if (source == valid_sources[i].source) {
798 target = valid_sources[i].name;
804 CYG_REPORT_RETVAL(result);
809 //{{{ Cdl::get_library_version()
811 // ----------------------------------------------------------------------------
812 // The version of the library actually lives inside configure.in. It gets
813 // exported into cdlconfig.h
815 Cdl::get_library_version(void)
817 return std::string(CYGNUM_LIBCDL_VERSION);
821 //{{{ Cdl::set_interactive()
823 // ----------------------------------------------------------------------------
824 // Some CDL scripts and some bits of the library may want to adapt depending
825 // on whether or not the application is running fully interactively or in
826 // batch mode. The primary distinction is that a batch program should never
827 // attempt to obtain user input, whether via Tk widgets or by other means.
829 bool Cdl::interactive = false;
832 Cdl::set_interactive(bool value)
834 CYG_REPORT_FUNCNAME("Cdl::set_interactive");
835 CYG_REPORT_FUNCARG1D(value);
841 Cdl::is_interactive(void)
843 CYG_REPORT_FUNCNAMETYPE("Cdl::is_interactive", "interactive %d");
844 CYG_REPORT_RETVAL(interactive);
849 //{{{ version support()
851 // ----------------------------------------------------------------------------
852 // Packages may need to impose constraints on which versions of other
853 // packages they can coexist with. This requires some way of achieving
854 // a partial ordering of version numbers. Unfortunately there are many
855 // different ways of specifying a version number, and we cannot impose
856 // a single model on all third party package developers. Instead this
857 // routine performs some semi-intelligent comparisons of two version
858 // strings which should work in the vast majority of cases.
860 // The return value is as per strcmp(), -1 if the first entry is
861 // smaller (i.e. the more recent and hence hopefully the first in
862 // a list), +1 if the second entry is smaller, 0 if the two are
865 // There is a big ambiguity between "derived" versions and "experimental"
866 // versions. Something like v0.3beta is experimental, i.e. it is older
867 // than the full release v0.3. On the other hand v0.3.p1 is a patched
868 // version of v0.3 and hence newer. This code uses the presence or otherwise
869 // of a separator to decide between the two cases.
871 // A utility routine which checks whether or not a character counts
872 // as a separator. Currently the characters . - and _ are all accepted
873 // as field separators.
875 // Arguably - should not be accepted as a separator. Instead if it preceeds
876 // a digit it could be interpreted as part of a prerelease number.
881 return ('.' == ch) || ('-' == ch) || ('_' == ch);
885 Cdl::compare_versions(std::string arg1, std::string arg2)
887 CYG_REPORT_FUNCNAMETYPE("Cdl::compare_versions", "result %d");
890 CYG_REPORT_RETVAL(0);
894 // The version number "current" is special, it always indicates the most
895 // recent version e.g. as checked out from a CVS repository.
896 if ("current" == arg1) {
897 CYG_REPORT_RETVAL(-1);
900 if ("current" == arg2) {
901 CYG_REPORT_RETVAL(1);
905 const char* ptr1 = arg1.c_str();
906 const char* ptr2 = arg2.c_str();
910 // If both strings start with 'v' or 'V', skip this. A minor variation in
911 // case at the start of a string should be ignored.
912 if ((('v' == *ptr1) || ('V' == *ptr1)) &&
913 (('v' == *ptr2) || ('V' == *ptr2))) {
918 // Now process the rest of the version string, one unit at a time.
921 if (('\0' == *ptr1) && ('\0' == *ptr2)) {
922 // Both strings have terminated at the same time. There
923 // may have been some spurious leading zeroes in numbers,
924 // or irrelevant differences in the separators.
925 CYG_REPORT_RETVAL(0);
930 // The first string has ended first. If the second string currently
931 // points at a separator then arg2 is a derived version, e.g.
932 // v0.3.p1, and hence newer. Otherwise arg2 is an experimental
933 // version v0.3beta and hence older.
934 if (is_separator(*ptr2)) {
935 CYG_REPORT_RETVAL(1);
938 CYG_REPORT_RETVAL(-1);
944 // As per the previous test.
945 if (is_separator(*ptr1)) {
946 CYG_REPORT_RETVAL(-1);
949 CYG_REPORT_RETVAL(1);
954 // If both strings currently point at numerical data, do a conversion and
955 // a numerical comparison.
956 if (isdigit(*ptr1) && isdigit(*ptr2)) {
959 // Strictly speaking there should be some overflow tests here, but it
960 // is not worth the trouble.
962 num1 = (10 * num1) + (*ptr1++ - '0');
963 } while(isdigit(*ptr1));
965 num2 = (10 * num2) + (*ptr2++ - '0');
966 } while(isdigit(*ptr2));
967 // v2.0 is newer than v1.0
969 CYG_REPORT_RETVAL(1);
971 } else if (num1 > num2) {
972 CYG_REPORT_RETVAL(-1);
979 // Non-numerical data. If the two characters are the same then
980 // move on. Note: this has to happen after numerical conversions
981 // to distinguish v10.0 and v1.0
982 if (*ptr1 == *ptr2) {
987 // If both strings are currently at a separator then move on. All
988 // separators can be used interchangeably.
989 if (is_separator(*ptr1) && is_separator(*ptr2)) {
994 // If only one string is at a separator, special action
995 // is needed. v1.1alpha is interpreted as earlier than
996 // v1.1, but v1.1.3 is a later release.
997 if (is_separator(*ptr1)) {
999 } else if (is_separator(*ptr2)) {
1003 // Two different characters, e.g. v1.0alpha vs. v1.0beta
1004 if (*ptr1 < *ptr2) {
1005 CYG_REPORT_RETVAL(1);
1008 CYG_REPORT_RETVAL(-1);
1016 // ----------------------------------------------------------------------------
1017 // Given a version string, extract major, minor and release numbers.
1018 // Some or all of these may be absent. Basically the code just
1019 // iterates through the string looking for sequences of numbers.
1022 version_extract_number(const std::string& version, unsigned int& index, std::string& result)
1024 CYG_REPORT_FUNCNAME("version_extract_number");
1026 // The calling code is expected to supply a sensible default.
1027 // Search for a digit
1028 for ( ; index < version.size(); index++) {
1029 if (isdigit(version[index])) {
1033 if (index != version.size()) {
1035 if ((index > 0) && ('-' == version[index-1])) {
1039 result += version[index++];
1040 } while ((index < version.size()) && isdigit(version[index]));
1043 CYG_REPORT_RETURN();
1047 Cdl::split_version_string(const std::string& version, std::string& major, std::string& minor, std::string& release)
1049 CYG_REPORT_FUNCNAME("CdlMisc::split_version_string");
1051 unsigned int index = 0;
1052 version_extract_number(version, index, major);
1053 version_extract_number(version, index, minor);
1054 version_extract_number(version, index, release);
1056 CYG_REPORT_RETURN();
1060 //{{{ Cdl::get_short_form()
1062 // ----------------------------------------------------------------------------
1063 // It is occasionally useful to take a full CDL name such as CYgpkg_KERNEL
1064 // and turn it into a short form such as "kernel". This involves discarding
1065 // everything up to and including the first underscore, then lowercasing
1066 // all subsequent characters.
1068 Cdl::get_short_form(const std::string& original)
1070 CYG_REPORT_FUNCNAME("CdlMisc::get_short_form");
1072 std::string result = "";
1073 unsigned int size = original.size();
1075 for (i = 0; i < size; i++) {
1076 if ('_' == original[i]) {
1082 // Either at end of string, or just past the first underscore
1083 for ( ; i < size; i++) {
1084 if (isupper(original[i])) {
1085 result += tolower(original[i]);
1087 result += original[i];
1091 CYG_REPORT_RETURN();