MailRc.C 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. *+SNOTICE
  25. *
  26. * $TOG: MailRc.C /main/5 1998/07/23 18:02:46 mgreess $
  27. *
  28. * RESTRICTED CONFIDENTIAL INFORMATION:
  29. *
  30. * The information in this document is subject to special
  31. * restrictions in a confidential disclosure agreement between
  32. * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
  33. * document outside HP, IBM, Sun, USL, SCO, or Univel without
  34. * Sun's specific written approval. This document and all copies
  35. * and derivative works thereof must be returned or destroyed at
  36. * Sun's request.
  37. *
  38. * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
  39. *
  40. *+ENOTICE
  41. */
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <ctype.h>
  45. #include <EUSCompat.h>
  46. #include <unistd.h>
  47. #include <pwd.h>
  48. #include <DtMail/DtMail.hh>
  49. #include <DtMail/Threads.hh>
  50. #include <DtMail/IO.hh>
  51. #define MAXIMUM_PATH_LENGTH 2048
  52. DtMail::MailRc::MailRc(DtMailEnv & error, Session * session)
  53. : _vars(20)
  54. {
  55. _session = session;
  56. error.clear();
  57. passwd pw;
  58. GetPasswordEntry(pw);
  59. char *rc_name = new char[MAXIMUM_PATH_LENGTH];
  60. strcpy(rc_name, pw.pw_dir);
  61. strcat(rc_name, "/.mailrc");
  62. _obj_mutex = MutexInit();
  63. _rc_file = fopen(rc_name, "r");
  64. if (!_rc_file) { // No rc file. This will be easy!
  65. delete [] rc_name;
  66. return;
  67. }
  68. parse_file();
  69. delete [] rc_name;
  70. }
  71. DtMail::MailRc::~MailRc(void)
  72. {
  73. if (_rc_file) {
  74. fclose(_rc_file);
  75. }
  76. MutexLock lock_scope(_obj_mutex);
  77. if (_vars.length()) {
  78. for (int i = 0; i < _vars.length(); i++) {
  79. delete _vars[i]->variable;
  80. delete _vars[i]->value;
  81. delete _vars[i];
  82. }
  83. }
  84. }
  85. void
  86. DtMail::MailRc::getValue(DtMailEnv & error, const char * var, const char ** value)
  87. {
  88. MutexLock lock_scope(_obj_mutex);
  89. error.clear();
  90. char * table_val;
  91. if (_vars.length()) {
  92. table_val = getVar(var);
  93. }
  94. else {
  95. table_val = NULL;
  96. }
  97. if (!table_val) {
  98. // Check the environment.
  99. table_val = getenv(var);
  100. if (!table_val) {
  101. error.setError(DTME_NoObjectValue);
  102. return;
  103. }
  104. }
  105. *value = table_val;
  106. }
  107. void
  108. DtMail::MailRc::setVar(const char * variable, const char * value)
  109. {
  110. MutexLock lock_scope(_obj_mutex);
  111. // See if we can find this variable.
  112. for (int i = 0; i < _vars.length(); i++) {
  113. if (strcmp(variable, _vars[i]->variable) == 0) {
  114. break;
  115. }
  116. }
  117. // Create only if necessary.
  118. if (i == _vars.length()) {
  119. Variable * var = new Variable;
  120. var->variable = strdup(variable);
  121. var->value = strdup(value);
  122. _vars.append(var);
  123. return;
  124. }
  125. free(_vars[i]->value);
  126. _vars[i]->value = strdup(value);
  127. }
  128. char *
  129. DtMail::MailRc::getVar(const char * var)
  130. {
  131. for (int i = 0; i < _vars.length(); i++) {
  132. if (strcmp(var, _vars[i]->variable) == 0) {
  133. return(_vars[i]->value);
  134. }
  135. }
  136. return(NULL);
  137. }
  138. void
  139. DtMail::MailRc::parse_file(void)
  140. {
  141. char line[2000]; // Big enough, I think.
  142. // Loop through each line in the file.
  143. while(fgets(line, sizeof(line), _rc_file)) {
  144. // If the first character is a #, then ignore it and go on.
  145. if (line[0] == '#') {
  146. continue;
  147. }
  148. // If this starts with "set", then parse it as a variable.
  149. if (strncmp(line, "set ", 4) == 0) {
  150. parse_var(line);
  151. }
  152. // There are lot's more options, but we don't do them yet.
  153. }
  154. }
  155. void
  156. DtMail::MailRc::parse_var(const char * line)
  157. {
  158. const char *cur_c;
  159. char *variable = new char[100];
  160. char *value = new char[2000];
  161. // Find the start of the variable.
  162. for (cur_c = &line[3]; isspace(*cur_c); cur_c++) {
  163. continue;
  164. }
  165. const char * var_start = cur_c;
  166. // The rules say the variable name ends at = or the end of the line.
  167. for (;*cur_c && *cur_c != '=' && (!isspace(*cur_c)); cur_c++) {
  168. continue;
  169. }
  170. memcpy(variable, var_start, (cur_c - var_start));
  171. variable[cur_c - var_start] = 0;
  172. // If there is no equal, then we are done.
  173. if (*cur_c != '=') {
  174. value[0] = 0;
  175. setVar(variable, value);
  176. delete [] variable;
  177. delete [] value;
  178. return;
  179. }
  180. // Oh well, more work to do. It is easy though. The value is the next character
  181. // after the = to the end of the line.
  182. strcpy(value, (cur_c + 1));
  183. value[strlen(value) - 1] = 0; // Strip newline.
  184. // Okay, finally, if we have quotes, remove those too.
  185. if (*value == '\'' || *value == '"') {
  186. memmove(value, &value[1], strlen(value));
  187. value[strlen(value) - 1] = 0;
  188. }
  189. setVar(variable, value);
  190. delete [] variable;
  191. delete [] value;
  192. }
  193. const char *
  194. DtMail::MailRc::getAlias(DtMailEnv & error, const char * name)
  195. {
  196. error.clear();
  197. return(NULL);
  198. }
  199. const char *
  200. DtMail::MailRc::getAlternates(DtMailEnv & error)
  201. {
  202. error.clear();
  203. return(NULL);
  204. }