eyedbgetenv.cc

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021   Author: Eric Viara <viara@sysra.com>
00022 */
00023 
00024 
00025 #include <eyedb/eyedb.h>
00026 #include "eyedb/DBM_Database.h"
00027 #include <signal.h>
00028 #include <errno.h>
00029 #include <fcntl.h>
00030 #include <GetOpt.h>
00031 #include <sys/stat.h>
00032 
00033 using namespace eyedb;
00034 
00035 Bool _server = False;
00036 
00037 static int
00038 usage(const char *prog)
00039 {
00040   fprintf(stderr, "usage: %s [--server] [--config|--csh|--sh] [--export] [--expand-user] [--expand-vars] [VARIABLES]\n", prog);
00041   print_use_help(std::cerr);
00042   return 1;
00043 }
00044 
00045 static std::string
00046 capstring(const char *s)
00047 {
00048   char *sx = (char *)malloc(strlen(s)+1);
00049   char *x = sx, c;
00050 
00051   while (c = *s++)
00052     *x++ = (c >= 'a' && c <= 'z') ? c + 'A' - 'a' : c;
00053   *x = 0;
00054   std::string rs = sx;
00055   free(sx);
00056   return rs;
00057 }
00058 
00059 int
00060 main(int argc, char *argv[])
00061 {
00062   for (int n = 1; n < argc; n++) {
00063     const char *s = argv[n];
00064 
00065     if (!strcmp(s, "--server")) {
00066       _server = True;
00067       break;
00068     }
00069   }
00070 
00071   try {
00072     if (_server) {
00073       std::string sv_listen;
00074       eyedb::init(argc, argv, &sv_listen, true);
00075     }
00076     else
00077       eyedb::init(argc, argv);
00078 
00079     if (argc < 2)
00080       return usage(argv[0]);
00081 
00082     LinkedList list;
00083     Bool shell, C_shell, _export, conf, expand_user, expand_vars;
00084 
00085     shell = C_shell =  _export = _server = False, conf = False, expand_user = expand_vars = False;
00086 
00087     std::string value;
00088 
00089     for (int n = 1; n < argc; n++) {
00090       const char *s = argv[n];
00091 
00092       if (!strcmp(s, "--sh")) {
00093         if (C_shell || shell)
00094           return usage(argv[0]);
00095         shell = True;
00096       }
00097       else if (!strcmp(s, "--csh")) {
00098         if (C_shell || shell)
00099           return usage(argv[0]);
00100         C_shell = True;
00101       }
00102       else if (!strcmp(s, "--export"))
00103         _export = True;
00104       else if (!strcmp(s, "--server"))
00105         _server = True;
00106       else if (!strcmp(s, "--config"))
00107         conf = True;
00108       else if (!strcmp(s, "--expand-user"))
00109         expand_user = True;
00110       else if (!strcmp(s, "--expand-vars"))
00111         expand_vars = True;
00112       else if (*s == '-')
00113         return usage(argv[0]);
00114       else
00115         list.insertObject((void *)s);
00116     }
00117 
00118     if (_export && !shell && !C_shell)
00119       return usage(argv[0]);
00120 
00121     if (!list.getCount() && !shell && !C_shell && !conf)
00122       return usage(argv[0]);
00123 
00124     if (shell + C_shell + conf > 1)
00125       return usage(argv[0]);
00126 
00127     unsigned int item_cnt;
00128     Config::Item *items;
00129 
00130     Config *cfg;
00131     if (_server)
00132       cfg = ServerConfig::getInstance();
00133     else
00134       cfg = ClientConfig::getInstance();
00135 
00136     if (!list.getCount())
00137       items = cfg->getValues(item_cnt, expand_vars ? true : false);
00138     else  {
00139       item_cnt = list.getCount();
00140       items = new Config::Item[list.getCount()];
00141       LinkedListCursor c(list);
00142       const char *s;
00143       for (int n = 0; c.getNext((void *&)s); n++) {
00144         const char *v = cfg->getValue(s);
00145         items[n] = Config::Item(strdup(s), strdup(v ? v : ""));
00146       }
00147     }
00148 
00149     if (expand_user) {
00150       for (int n = 0; n < item_cnt; n++) {
00151         if (!strcmp(items[n].name, "user")) {
00152           items[n].value = strdup(Connection::makeUser(items[n].value).c_str());
00153           break;
00154         }
00155       }
00156     }
00157 
00158     if (shell) {
00159       fprintf(stdout, "#\n");
00160       fprintf(stdout, "# Bourne Shell EyeDB Environment\n");
00161       fprintf(stdout, "#\n\n");
00162 
00163       for (int n = 0; n < item_cnt; n++) {
00164         if (!Config::isBuiltinVar(items[n].name) && !Config::isUserVar(items[n].name)) {
00165           std::string var = std::string("EYEDB") + capstring(items[n].name);
00166           fprintf(stdout, "%s=%s", var.c_str(), items[n].value);
00167           if (_export)
00168             fprintf(stdout, "; export %s", var.c_str());
00169           fprintf(stdout, "\n");
00170         }
00171       }
00172 
00173       delete [] items;
00174       return 0;
00175     }
00176 
00177     if (C_shell) {
00178       fprintf(stdout, "#\n");
00179       fprintf(stdout, "# C-Shell EyeDB Environment\n");
00180       fprintf(stdout, "#\n\n");
00181 
00182       for (int n = 0; n < item_cnt; n++) {
00183         if (!Config::isBuiltinVar(items[n].name) && !Config::isUserVar(items[n].name)) {
00184           std::string var = std::string("EYEDB") + capstring(items[n].name);
00185           if (_export)
00186             fprintf(stdout, "setenv %s %s\n", var.c_str(),
00187                     items[n].value);
00188           else
00189             fprintf(stdout, "set %s=%s\n", var.c_str(), items[n].value);
00190         }
00191       }
00192 
00193       delete [] items;
00194       return 0;
00195     }
00196 
00197     if (conf) {
00198       fprintf(stdout, "#\n");
00199       fprintf(stdout, "# EyeDB %s Configuration File\n",
00200               _server ? "Server" : "Client");
00201       fprintf(stdout, "#\n\n");
00202 
00203       for (int n = 0; n < item_cnt; n++)
00204         fprintf(stdout, "%s = %s;\n", items[n].name, items[n].value);
00205 
00206       delete [] items;
00207       return 0;
00208     }
00209 
00210     for (int n = 0; n < item_cnt; n++)
00211       fprintf(stdout, "%s\n", items[n].value);
00212 
00213     delete [] items;
00214   }
00215   catch(Exception &e) {
00216     std::cerr << e << std::flush;
00217     return 1;
00218   }
00219 
00220   return 0;
00221 }

Generated on Mon Dec 22 18:15:54 2008 for eyedb by  doxygen 1.5.3