Back to index

im-sdk  12.3.91
IMSvrCfg.cpp
Go to the documentation of this file.
00001 #include <config.h>
00002 #include <stdio.h>
00003 #ifdef HAVE_PWENT
00004 #include <pwd.h>
00005 #endif
00006 #ifdef HAVE_SYS_TYPES_H
00007 #include <sys/types.h>
00008 #endif
00009 #ifdef HAVE_UNISTD_H
00010 #include <unistd.h>
00011 #endif
00012 
00013 #include "IMSvrCfg.hh"
00014 #include "IMUtil.hh"
00015 #include "IMLog.hh"
00016 
00017 bool
00018 IMSvrCfg::get_home_dir(
00019     string &dir
00020 )
00021 {
00022 
00023 #ifdef HAVE_PWENT
00024     struct passwd *pass;
00025 #  if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
00026 #ifndef NSS_BUFLEN_PASSWD
00027 #define NSS_BUFLEN_PASSWD 1024
00028 #endif
00029     struct passwd pass_buf;
00030     char buf[NSS_BUFLEN_PASSWD];
00031 
00032 #    ifdef HAVE_POSIX_GETPWUID_R
00033     if (getpwuid_r(geteuid(), &pass_buf, buf, NSS_BUFLEN_PASSWD, &pass) != 0) {
00034         return false;
00035     }
00036 #else
00037     if ((pass = getpwuid_r(geteuid(), &pass_buf, buf, NSS_BUFLEN_PASSWD))
00038        != NULL) {
00039         return false;
00040     }
00041 #endif
00042 #else /* !HAVE_GETPWUID_R || !HAVE_NONPOSIX_GETPWUID_R */
00043     if ((pass = getpwuid(geteuid())) == NULL) {
00044         return false;
00045     } 
00046 #endif
00047     dir = string(pass->pw_dir);
00048     return true;
00049 #else
00050     return false;
00051 #endif
00052 }
00053 bool
00054 IMSvrCfg::get_process_user(
00055      string &username
00056 )
00057 {
00058 
00059 #ifdef HAVE_PWENT
00060     struct passwd *pass;
00061 #  if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
00062 #ifndef NSS_BUFLEN_PASSWD
00063 #define NSS_BUFLEN_PASSWD 1024
00064 #endif
00065     struct passwd pass_buf;
00066     char buf[NSS_BUFLEN_PASSWD];
00067 
00068 #    ifdef HAVE_POSIX_GETPWUID_R
00069     if (getpwuid_r(geteuid(), &pass_buf, buf, NSS_BUFLEN_PASSWD, &pass) != 0) {
00070         return false;
00071     }
00072 #else
00073     if ((pass = getpwuid_r(geteuid(), &pass_buf, buf, NSS_BUFLEN_PASSWD))
00074        != NULL) {
00075         return false;
00076     }
00077 #endif
00078 #else /* !HAVE_GETPWUID_R || !HAVE_NONPOSIX_GETPWUID_R */
00079     if ((pass = getpwuid(geteuid())) == NULL) {
00080         return false;
00081     } 
00082 #endif
00083     username = string(pass->pw_name);
00084     return true;
00085 #else
00086     return false;
00087 #endif
00088 }
00089 
00090 void
00091 IMSvrCfg::initialize()
00092 {
00093     initstr(IMDIR, "IM main directory.", IIIMLIBDIR);
00094     initstr(BASICLOCALE, "Locale used by default", "C");
00095 #ifdef WIN32
00096     initstr(IFPATHNAME, "Language Engine module directory",
00097           "D:\\WINNT\\System32\\iiimf");
00098 #else
00099     initstr(IFPATHNAME, "Language Engine module directory",
00100           IIIMLEDIR);
00101 #endif
00102     initstr(IFNAME, "Language Engine name", "");
00103     initstr(PORT, "Port or service name", "9010");
00104     initstr(HOSTNAME, "host name", "localhost");
00105     initstr(NSMAP_CFG_FILE, "Config file for Namespace Mapping", XMLCONFDIR "/ns_map.cfg");
00106 #ifdef WIN32
00107     initstr(CONFIGFILE, "Configuration file", "D:\\WINNT\\System32\\iiimd.xml.conf");
00108 #else
00109     initstr(CONFIGFILE, "Configuration file", XMLCONFDIR "/iiimd.xml.conf");
00110 #endif
00111 #ifdef WIN32
00112     initstr(LECONFFILE, "LE-specific Configuration file", "D:\\WINNT\\System32\\le.xml.conf");
00113 #else
00114     initstr(LECONFFILE, "LE-specific Configuration file", XMLCONFDIR "/le.xml.conf");
00115 #endif
00116     initstr(CONVERSIONKEYS, "Conversion ON keys", "<Ctrl>space");
00117     initstr(LABEL, "Label type", "numeric");
00118     initbool(SETTRIGGEROFFKEYS, "set trigger off keys", false);
00119     // initstr(IMCONFIG, "/usr/lib/iiim/etc/im.config");
00120     // initbool(DIRECT_XSUNIM, true);
00121     initstr(LOG_FACILITY, "syslog facility", "USER");
00122     initstr(LOG_LEVEL, "log level", "NORMAL");
00123     initstr(MESSAGE_LOCALE, "message locale", "C");
00124     initnum(LOOKUPROW, "The number of lookup-choice rows.", 4);
00125     initnum(LOOKUPCOL, "The number of lookup-choice columns.", 4);
00126     initbool(NODAEMON, "Don't transit to daemon mode", false);
00127     initbool(USER, "behave as a per-user server", false);
00128     initbool(DEBUGFLAG, "debug mode", false);
00129     initstr(VARDIR, "IIIM volatile data directory", "");
00130 #if defined(HAVE_UNIX_SOCKET)
00131     initstr(UDSFILE, "Use specified unix domain socket", "");
00132 #endif /* HAVE_UNIX_SOCKET */
00133     initbool(DESKTOP, "exit if no desktop exists", false);
00134     initbool(PREFERRED_LOADING, "load LEs only you prefer", false);
00135 }
00136 
00137 void
00138 IMSvrCfg::
00139 initbool(
00140     enum IMSvrCfgOpt opt,
00141     const char* desc,
00142     bool boolval
00143 )
00144 {
00145     popts[opt].opt = opt;
00146     popts[opt].type = ARG_BOOL;
00147     popts[opt].description = desc;
00148     popts[opt].v.b = boolval;
00149 }
00150  
00151 void
00152 IMSvrCfg::
00153 initnum(
00154     enum IMSvrCfgOpt opt,
00155     const char* desc,
00156     int numval
00157 )
00158 {
00159     popts[opt].opt = opt;
00160     popts[opt].type = ARG_NUMBER;
00161     popts[opt].description = desc;
00162     popts[opt].v.n = numval;
00163 }
00164 
00165 void
00166 IMSvrCfg::
00167 initstr(
00168     enum IMSvrCfgOpt opt,
00169     const char* desc,
00170     const char* strval
00171 )
00172 {
00173     popts[opt].opt = opt;
00174     popts[opt].type = ARG_STRING;
00175     popts[opt].description = desc;
00176     popts[opt].v.s = strval;
00177 }
00178 
00179 void
00180 IMSvrCfg::
00181 setbool(
00182     enum IMSvrCfgOpt opt,
00183     bool boolval
00184 )
00185 {
00186     if (popts[opt].type != ARG_BOOL) {
00187        ERROR_INTERNAL("Invalid argument access.");
00188     }
00189     popts[opt].v.b = boolval;
00190 }
00191  
00192 void
00193 IMSvrCfg::
00194 setnum(
00195     enum IMSvrCfgOpt opt,
00196     int numval
00197 )
00198 {
00199     if (popts[opt].type != ARG_NUMBER) {
00200        ERROR_INTERNAL("Invalid argument access.");
00201     }
00202     popts[opt].v.n = numval;
00203 }
00204 
00205 void
00206 IMSvrCfg::
00207 setstr(
00208     enum IMSvrCfgOpt opt,
00209     const char* strval
00210 )
00211 {
00212     if (popts[opt].type != ARG_STRING) {
00213        ERROR_INTERNAL("Invalid argument access.");
00214     }
00215     popts[opt].v.s = strval;
00216 }
00217 
00218 
00219 bool
00220 IMSvrCfg::
00221 get_boolval(
00222     enum IMSvrCfgOpt opt
00223 ) const
00224 {
00225     if ((opt >= CFGOPT_LAST)
00226        || (popts[opt].type != ARG_BOOL)) {
00227        ERROR_INTERNAL("Invalid argument access.");
00228     }
00229 
00230     return popts[opt].v.b;
00231 }
00232 
00233 int
00234 IMSvrCfg::
00235 get_numval(
00236     enum IMSvrCfgOpt opt
00237 ) const
00238 {
00239     if ((opt >= CFGOPT_LAST)
00240        || (popts[opt].type != ARG_NUMBER)) {
00241        ERROR_INTERNAL("Invalid argument access.");
00242     }
00243 
00244     return popts[opt].v.n;
00245 }
00246 
00247 const char*
00248 IMSvrCfg::
00249 get_strval(
00250     enum IMSvrCfgOpt opt
00251 ) const
00252 {
00253     if ((opt >= CFGOPT_LAST)
00254        || (popts[opt].type != ARG_STRING)) {
00255        ERROR_INTERNAL("Invalid argument access.");
00256     }
00257 
00258     return popts[opt].v.s.c_str();
00259 }
00260 
00261 void
00262 IMSvrCfg::showall() const
00263 {
00264     int i;
00265     for (i = 0; i < CFGOPT_LAST; i++) {
00266        switch(popts[i].type) {
00267          case ARG_BOOL:
00268           if (popts[i].v.b) {
00269               LOG_DEBUG("%s[%d]:true", popts[i].description.c_str(), (int)popts[i].opt);
00270           } else {
00271               LOG_DEBUG("%s[%d]:false", popts[i].description.c_str(), (int)popts[i].opt);
00272           }
00273           break;
00274          case ARG_NUMBER:
00275           LOG_DEBUG("%s[%d]:%d", popts[i].description.c_str(), (int)popts[i].opt, popts[i].v.n);
00276           break;
00277          case ARG_STRING:
00278           LOG_DEBUG("%s[%d]:%s", popts[i].description.c_str(), (int)popts[i].opt, popts[i].v.s.c_str());
00279           break;
00280          default:
00281           ERROR_INTERNAL("Invalid arg type.");
00282           break;
00283        }
00284     }
00285 
00286     return;
00287 }
00288 
00289 IMSvrCfg::IMSvrCfg(
00290        const IMSvrCfg& base
00291 )
00292 {
00293     command_name = base.command_name;
00294     popts = base.popts;
00295     pbase = &base;
00296 }
00297 
00298 IMSvrCfg::IMSvrCfg(
00299     string cmdname
00300 )
00301 {
00302     command_name = cmdname;
00303     popts = new CfgVal[CFGOPT_LAST];
00304     pbase = NULL;
00305     initialize();
00306 }
00307 
00308 IMSvrCfg::~IMSvrCfg()
00309 {
00310     if (!pbase) delete[] popts;
00311 }
00312 
00313 /* Local Variables: */
00314 /* c-file-style: "iiim-project" */
00315 /* End: */