00001
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00033
00034 #ifndef GROOVX_TCL_TCLPKG_MISC_CC_UTC20050628161246_DEFINED
00035 #define GROOVX_TCL_TCLPKG_MISC_CC_UTC20050628161246_DEFINED
00036
00037 #include "tcl/tclpkg-misc.h"
00038
00039
00040
00041
00042
00043 #include "tcl/commandgroup.h"
00044 #include "tcl/list.h"
00045 #include "tcl/pkg.h"
00046
00047 #include "rutz/backtrace.h"
00048 #include "rutz/backtraceformat.h"
00049 #include "rutz/error.h"
00050 #include "rutz/fstring.h"
00051 #include "rutz/rand.h"
00052 #include "rutz/sfmt.h"
00053
00054 #include <unistd.h>
00055
00056 #include "rutz/trace.h"
00057
00058 namespace
00059 {
00060 rutz::urand generator;
00061
00062 void usleepr(unsigned int usecs, unsigned int reps)
00063 {
00064 for ( ; reps > 0; --reps)
00065 ::usleep(usecs);
00066 }
00067
00068 rutz::fstring backTrace()
00069 {
00070 rutz::backtrace bt;
00071 rutz::error::get_last_backtrace(bt);
00072 return rutz::format(bt);
00073 }
00074
00075 rutz::fstring cmdUsage(tcl::call_context& ctx)
00076 {
00077 const char* name = ctx.get_arg<const char*>(1);
00078 tcl::command_group* cmd =
00079 tcl::command_group::lookup_original(ctx.interp(), name);
00080
00081 if (cmd == 0)
00082 throw rutz::error("no such tcl::command_group", SRC_POS);
00083
00084 return rutz::sfmt("%s resolves to %s\n%s",
00085 name, cmd->resolved_name().c_str(),
00086 cmd->usage().c_str());
00087 }
00088
00089 unsigned long get_default_seed() { return rutz::default_rand_seed; }
00090 void set_default_seed(unsigned long x) { rutz::default_rand_seed = x; }
00091
00092 rutz::fstring tcl_valuetype(tcl::obj obj)
00093 {
00094 return obj.tcltype_name();
00095 }
00096
00097 double rand_draw(double min, double max)
00098 {
00099 return generator.fdraw_range(min, max);
00100 }
00101
00102 tcl::list rand_draw_n(double min, double max, int n)
00103 {
00104 tcl::list result;
00105 for (int i = 0; i < n; ++i)
00106 result.append(rand_draw(min, max));
00107 return result;
00108 }
00109 }
00110
00111 extern "C"
00112 int Misc_Init(Tcl_Interp* interp)
00113 {
00114 GVX_TRACE("Misc_Init");
00115
00116 using namespace rutz;
00117
00118 GVX_PKG_CREATE(pkg, interp, "Misc", "4.$Revision: 10065 $");
00119
00120 pkg->def( "::rand", "min max", &rand_draw, SRC_POS);
00121 pkg->def( "::rand", "min max ?n=1?", &rand_draw_n, SRC_POS);
00122 pkg->def( "::srand", "seed",
00123 bind_first(mem_func(&rutz::urand::seed), &generator),
00124 SRC_POS );
00125
00126
00127
00128
00129
00130
00131 pkg->def( "::sleep", "secs", &::sleep, SRC_POS );
00132
00133
00134
00135
00136
00137
00138
00139 pkg->def( "::usleep", "usecs", &::usleep, SRC_POS );
00140
00141
00142
00143
00144
00145
00146
00147
00148 pkg->def( "::usleepr", "usecs reps", &usleepr, SRC_POS );
00149
00150 pkg->def( "::bt", "", &backTrace, SRC_POS );
00151
00152 pkg->def( "::default_rand_seed", "", &get_default_seed, SRC_POS );
00153 pkg->def( "::default_rand_seed", "seed", &set_default_seed, SRC_POS );
00154
00155 pkg->def( "::tcl_valuetype", "value", &tcl_valuetype, SRC_POS );
00156
00157 pkg->def_raw( "::?", tcl::arg_spec(2), "cmd_name", &cmdUsage, SRC_POS );
00158
00159 GVX_PKG_RETURN(pkg);
00160 }
00161
00162 static const char __attribute__((used)) vcid_groovx_tcl_tclpkg_misc_cc_utc20050628161246[] = "$Id: tclpkg-misc.cc 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00163 #endif // !GROOVX_TCL_TCLPKG_MISC_CC_UTC20050628161246_DEFINED