cfg.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* SLiM - Simple Login Manager
  2. Copyright (C) 2004-05 Simone Rota <sip@varlock.com>
  3. Copyright (C) 2004-05 Johannes Winkelmann <jw@tks6.net>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. */
  9. #include <fstream>
  10. #include <string>
  11. #include <iostream>
  12. #include <unistd.h>
  13. #include "cfg.h"
  14. using namespace std;
  15. typedef pair<string,string> option;
  16. Cfg::Cfg() {
  17. // Configuration options
  18. options.insert(option("default_path","./:/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin"));
  19. options.insert(option("default_xserver","/usr/X11R6/bin/X"));
  20. options.insert(option("xserver_arguments",""));
  21. options.insert(option("login_cmd","exec /bin/bash -login ~/.xinitrc %session"));
  22. options.insert(option("halt_cmd","/sbin/shutdown -h now"));
  23. options.insert(option("reboot_cmd","/sbin/shutdown -r now"));
  24. options.insert(option("suspend_cmd",""));
  25. options.insert(option("sessionstart_cmd",""));
  26. options.insert(option("sessionstop_cmd",""));
  27. options.insert(option("console_cmd","/usr/X11R6/bin/xterm -C -fg white -bg black +sb -g %dx%d+%d+%d -fn %dx%d -T ""Console login"" -e /bin/sh -c ""/bin/cat /etc/issue; exec /bin/login"""));
  28. options.insert(option("screenshot_cmd","import -window root /login.app.png"));
  29. options.insert(option("welcome_msg","Welcome to %host"));
  30. options.insert(option("default_user",""));
  31. options.insert(option("current_theme","default"));
  32. options.insert(option("lockfile","/var/run/slim.lock"));
  33. options.insert(option("logfile","/var/log/slim.log"));
  34. options.insert(option("shutdown_msg","The system is halting..."));
  35. options.insert(option("reboot_msg","The system is rebooting..."));
  36. options.insert(option("sessions","wmaker,blackbox,icewm"));
  37. // Theme stuff
  38. options.insert(option("input_panel_x","50%"));
  39. options.insert(option("input_panel_y","40%"));
  40. options.insert(option("input_name_x","200"));
  41. options.insert(option("input_name_y","154"));
  42. options.insert(option("input_pass_x","-1")); // default is single inputbox
  43. options.insert(option("input_pass_y","-1"));
  44. options.insert(option("input_font","Verdana:size=11"));
  45. options.insert(option("input_color", "#000000"));
  46. options.insert(option("input_cursor_height","20"));
  47. options.insert(option("input_maxlength_name","20"));
  48. options.insert(option("input_maxlength_passwd","20"));
  49. options.insert(option("input_shadow_xoffset", "0"));
  50. options.insert(option("input_shadow_yoffset", "0"));
  51. options.insert(option("input_shadow_color","#FFFFFF"));
  52. options.insert(option("welcome_font","Verdana:size=14"));
  53. options.insert(option("welcome_color","#FFFFFF"));
  54. options.insert(option("welcome_x","-1"));
  55. options.insert(option("welcome_y","-1"));
  56. options.insert(option("welcome_shadow_xoffset", "0"));
  57. options.insert(option("welcome_shadow_yoffset", "0"));
  58. options.insert(option("welcome_shadow_color","#FFFFFF"));
  59. options.insert(option("intro_msg",""));
  60. options.insert(option("intro_font","Verdana:size=14"));
  61. options.insert(option("intro_color","#FFFFFF"));
  62. options.insert(option("intro_x","-1"));
  63. options.insert(option("intro_y","-1"));
  64. options.insert(option("background_style","stretch"));
  65. options.insert(option("background_color","#CCCCCC"));
  66. options.insert(option("username_font","Verdana:size=12"));
  67. options.insert(option("username_color","#FFFFFF"));
  68. options.insert(option("username_x","-1"));
  69. options.insert(option("username_y","-1"));
  70. options.insert(option("username_msg","Please enter your username"));
  71. options.insert(option("username_shadow_xoffset", "0"));
  72. options.insert(option("username_shadow_yoffset", "0"));
  73. options.insert(option("username_shadow_color","#FFFFFF"));
  74. options.insert(option("password_x","-1"));
  75. options.insert(option("password_y","-1"));
  76. options.insert(option("password_msg","Please enter your password"));
  77. options.insert(option("msg_color","#FFFFFF"));
  78. options.insert(option("msg_font","Verdana:size=16:bold"));
  79. options.insert(option("msg_x","40"));
  80. options.insert(option("msg_y","40"));
  81. options.insert(option("msg_shadow_xoffset", "0"));
  82. options.insert(option("msg_shadow_yoffset", "0"));
  83. options.insert(option("msg_shadow_color","#FFFFFF"));
  84. error = "";
  85. }
  86. /*
  87. * Creates the Cfg object and parses
  88. * known options from the given configfile / themefile
  89. */
  90. bool Cfg::readConf(string configfile) {
  91. int n = -1;
  92. string line, fn(configfile);
  93. map<string,string>::iterator it;
  94. string op;
  95. ifstream cfgfile( fn.c_str() );
  96. if (cfgfile) {
  97. while (getline( cfgfile, line )) {
  98. it = options.begin();
  99. while (it != options.end()) {
  100. op = it->first;
  101. n = line.find(op);
  102. if (n == 0)
  103. options[op] = parseOption(line, op);
  104. it++;
  105. }
  106. }
  107. cfgfile.close();
  108. return true;
  109. } else {
  110. error = "Cannot read configuration file: " + configfile;
  111. return false;
  112. }
  113. }
  114. /* Returns the option value, trimmed */
  115. string Cfg::parseOption(string line, string option ) {
  116. return Trim( line.substr(option.size(), line.size() - option.size()));
  117. }
  118. const string& Cfg::getError() const {
  119. return error;
  120. }
  121. string& Cfg::getOption(string option) {
  122. return options[option];
  123. }
  124. /* return a trimmed string */
  125. string Cfg::Trim( const string& s ) {
  126. if ( s.empty() ) {
  127. return s;
  128. }
  129. int pos = 0;
  130. string line = s;
  131. string::size_type len = line.length();
  132. while ( pos < len && isspace( line[pos] ) ) {
  133. ++pos;
  134. }
  135. line.erase( 0, pos );
  136. pos = line.length()-1;
  137. while ( pos > -1 && isspace( line[pos] ) ) {
  138. --pos;
  139. }
  140. if ( pos != -1 ) {
  141. line.erase( pos+1 );
  142. }
  143. return line;
  144. }
  145. /* Return the welcome message with replaced vars */
  146. string Cfg::getWelcomeMessage(){
  147. string s = getOption("welcome_msg");
  148. int n = -1;
  149. n = s.find("%host");
  150. if (n >= 0) {
  151. string tmp = s.substr(0, n);
  152. char host[40];
  153. gethostname(host,40);
  154. tmp = tmp + host;
  155. tmp = tmp + s.substr(n+5, s.size() - n);
  156. s = tmp;
  157. }
  158. n = s.find("%domain");
  159. if (n >= 0) {
  160. string tmp = s.substr(0, n);;
  161. char domain[40];
  162. getdomainname(domain,40);
  163. tmp = tmp + domain;
  164. tmp = tmp + s.substr(n+7, s.size() - n);
  165. s = tmp;
  166. }
  167. return s;
  168. }
  169. int Cfg::string2int(const char* string, bool* ok) {
  170. char* err = 0;
  171. int l = (int)strtol(string, &err, 10);
  172. if (ok) {
  173. *ok = (*err == 0);
  174. }
  175. return (*err == 0) ? l : 0;
  176. }
  177. // Get absolute position
  178. int Cfg::absolutepos(const string& position, int max, int width) {
  179. int n = -1;
  180. n = position.find("%");
  181. if (n>0) { // X Position expressed in percentage
  182. const char* tmp = position.substr(0, n).c_str();
  183. int result = (max*string2int(tmp)/100) - (width / 2);
  184. return result < 0 ? 0 : result ;
  185. } else { // Absolute X position
  186. return string2int(position.c_str());
  187. }
  188. }
  189. // split a comma separated string into a vector of strings
  190. void Cfg::split(vector<string>& v, const string& str, char c) {
  191. v.clear();
  192. string::const_iterator s = str.begin();
  193. while (true) {
  194. string::const_iterator begin = s;
  195. while (*s != c && s != str.end()) { ++s; }
  196. v.push_back(string(begin, s));
  197. if (s == str.end()) {
  198. break;
  199. }
  200. if (++s == str.end()) {
  201. v.push_back("");
  202. break;
  203. }
  204. }
  205. }
  206. string Cfg::nextSession(string current) {
  207. vector<string> sessions;
  208. split(sessions, getOption("sessions"), ',');
  209. if (sessions.size() <= 1)
  210. return current;
  211. for (int i=0; i<sessions.size()-1; i++) {
  212. if (current == sessions[i]) {
  213. return sessions[i+1];
  214. }
  215. }
  216. return sessions[0];
  217. }