spec.C 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. //%% (c) Copyright 1993, 1994 Hewlett-Packard Company
  24. //%% (c) Copyright 1993, 1994 International Business Machines Corp.
  25. //%% (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  26. //%% (c) Copyright 1993, 1994 Novell, Inc.
  27. //%% $XConsortium: spec.C /main/3 1995/10/20 16:26:34 rswiston $
  28. /*
  29. *
  30. * spec.cc
  31. *
  32. * Copyright (c) 1990 by Sun Microsystems, Inc.
  33. */
  34. #include <string.h>
  35. #include <stddef.h>
  36. #include <stdio.h>
  37. #include <errno.h>
  38. #include <sys/stat.h>
  39. #include "spec.h"
  40. #include "options_tt.h"
  41. #include "util/tt_gettext.h"
  42. #include "ttdbck.h"
  43. #include "tt_db_server_consts.h"
  44. Spec::
  45. Spec()
  46. {
  47. key = (Binkey *) 0;
  48. filename = "";
  49. type = "";
  50. is_filespec = 0;
  51. props = new Prop_table(_tt_prop_name);
  52. propnames = new _Tt_string_list;
  53. }
  54. void Spec::
  55. add_prop_and_value(_Tt_string propname, _Tt_string value)
  56. {
  57. // the reason we need to keep the list of names in
  58. // propnames is that we want to preserve the order in
  59. // which they were encountered, so that adding them
  60. // back in goes smoothly. A tt_table method that
  61. // returned a sorted list of keys would be nice, but
  62. // right now I don't have time... RFM 1/10/91
  63. Prop_ptr sp = props->lookup(propname);
  64. if (sp.is_null()) {
  65. sp = new Prop(propname);
  66. props->insert(sp);
  67. propnames->append(propname);
  68. }
  69. sp->_values->append(value);
  70. }
  71. void Spec::
  72. print(FILE *f) const
  73. {
  74. print_key(f);
  75. print_mand(f);
  76. print_props(f);
  77. }
  78. void Spec::
  79. print_key(FILE *f) const
  80. {
  81. fprintf(f,
  82. "-----------\n"
  83. "objkey: ");
  84. key->print(f);
  85. fprintf(f, "\n");
  86. }
  87. void Spec::
  88. print_mand(FILE *f) const
  89. {
  90. if (is_filespec) {
  91. fprintf(f, "file: %s\n", (char *)filename);
  92. } else {
  93. fprintf(f,
  94. "type: %s\nfile: %s\n",
  95. (char *)type, (char *)filename);
  96. }
  97. }
  98. void Spec::
  99. print_props(FILE *f) const
  100. {
  101. _Tt_string_list_cursor c;
  102. Prop_ptr sp;
  103. _Tt_string_list_cursor v;
  104. int i;
  105. c.reset(propnames);
  106. while(c.next()) {
  107. sp = props->lookup(*c);
  108. v.reset(sp->_values);
  109. i = 0;
  110. while(v.next()) {
  111. fprintf(f,
  112. "prop %32s[%2d] = ",
  113. (char *)sp->_name, i++);
  114. v->print(f);
  115. fprintf(f,"\n");
  116. }
  117. }
  118. }
  119. //
  120. // Called when all info for a spec has been accumulated; see if the
  121. // spec is selected under the current selection options, diagnose
  122. // according to diagnostic options, print according to printing
  123. // options.
  124. //
  125. void Spec::
  126. process_spec()
  127. {
  128. // Possible things to go wrong. Done as a bitmap not so much
  129. // to save space, but to make the determination whether any
  130. // bad things were found easy, by seeing if bad_flags is nonzero.
  131. int bad_flags = 0;
  132. const int BAD_NOFILE = 1;
  133. const int BAD_NOTYPE = 2;
  134. const int BAD_MULTITYPE = 4;
  135. const int BAD_FILE_STAT = 8;
  136. const int BAD_TYPE = 0x10;
  137. const int BAD_TYPED_FILESPEC = 0x20;
  138. int save_errno = 0;
  139. Prop_ptr sp;
  140. _Tt_otype_ptr ot;
  141. _Tt_string_list_ptr type_list;
  142. // All the props have been accumulated, now we can pick out
  143. // the type if it's there.
  144. sp = props->lookup(_Tt_string(TT_DB_OBJECT_TYPE_PROPERTY));
  145. if (!sp.is_null()) {
  146. type_list = sp->_values;
  147. } else {
  148. type_list = new _Tt_string_list;
  149. }
  150. if (type_list->count()==1) {
  151. type = type_list->top();
  152. } else {
  153. type = "";
  154. }
  155. _Tt_string filehostname, filelocalpath;
  156. filelocalpath = filename.split(':',filehostname);
  157. if (opts->selecting_p()) {
  158. if (opts->sel_filename_p()) {
  159. if (!filelocalpath.sh_match(opts->sel_filename())) {
  160. return;
  161. }
  162. }
  163. if (opts->sel_type_p()) {
  164. if (!type.sh_match(opts->sel_type())) {
  165. return;
  166. }
  167. }
  168. if (opts->sel_objid_p()) {
  169. if (*key != *opts->sel_objid_key()) {
  170. return;
  171. }
  172. }
  173. }
  174. if (opts->diagnosing_p()) {
  175. if (opts->diag_badform_p()) {
  176. if (filename=="") {
  177. bad_flags |= BAD_NOFILE;
  178. }
  179. switch (type_list->count()) {
  180. case 0:
  181. // It's OK if docoids have no type.
  182. if (!is_filespec) {
  183. bad_flags |= BAD_NOTYPE;
  184. }
  185. break;
  186. case 1:
  187. ot = (*tdb_ptr)->otable->lookup(type);
  188. if (ot.is_null()) {
  189. bad_flags |= BAD_TYPE;
  190. } else if (is_filespec) {
  191. // docoids should not have a type,
  192. // it's an error if they do, even
  193. // if it's a valid type
  194. bad_flags |= BAD_TYPED_FILESPEC;
  195. }
  196. break;
  197. default:
  198. bad_flags |= BAD_MULTITYPE;
  199. }
  200. }
  201. if (opts->diag_exist_p() && filename!="") {
  202. struct stat statbuf;
  203. // HACK: ought to check that filehostname is
  204. // same as localhost, but that's expensive
  205. // due to aliases, possibly domain-qualified
  206. // names, etc.
  207. if (-1==stat((char *)filelocalpath,&statbuf)) {
  208. save_errno = errno;
  209. bad_flags |= BAD_FILE_STAT;
  210. }
  211. }
  212. }
  213. // If the spec has a forward pointer, then it's OK for
  214. // it to be missing information...
  215. sp = props->lookup(_Tt_string(TT_DB_FORWARD_POINTER_PROPERTY));
  216. if (!sp.is_null()) {
  217. bad_flags = 0;
  218. }
  219. // a spec is eligible for display or repair only if it passes all
  220. // selection options (if not, we returned earlier) and if either the
  221. // spec is bad somehow, or we aren't diagnosing in which case we
  222. // always display or repair all selected specs.
  223. if (bad_flags || !opts->diagnosing_p()) {
  224. if (opts->displaying_p() || bad_flags) {
  225. print_key(stdout);
  226. }
  227. if (opts->disp_mand_p()) {
  228. print_mand(stdout);
  229. }
  230. if (opts->disp_prop_p()) {
  231. print_props(stdout);
  232. }
  233. // if we are diagnosing, now is the time to print the
  234. // diagnostics. Note that bad_flags will be zero if
  235. // we aren't diagnosing, but we still must fall all
  236. // way through and put the spec on the repair list.
  237. if ((bad_flags & BAD_NOFILE)!=0) {
  238. printf("%s", catgets(_ttcatd, 6, 20,
  239. "Error: no file for spec.\n"));
  240. }
  241. if ((bad_flags & BAD_NOTYPE)!=0) {
  242. printf("%s", catgets(_ttcatd, 6, 21,
  243. "Error: no type for spec.\n"));
  244. }
  245. if ((bad_flags & BAD_TYPE)!=0) {
  246. printf(catgets(_ttcatd, 6, 22,"Error: \"%s\" is not "
  247. "an installed otype.\n"),
  248. type.operator const char *());
  249. }
  250. if ((bad_flags & BAD_MULTITYPE)!=0) {
  251. printf("%s", catgets(_ttcatd, 6, 23,"Error: spec has multiple "
  252. "values for type property.\n"));
  253. }
  254. if ((bad_flags & BAD_FILE_STAT)!=0) {
  255. printf("%s", catgets(_ttcatd, 6, 24,"Error: "));
  256. printf("%s: %s", (char *)filename, strerror(save_errno));
  257. }
  258. if ((bad_flags & BAD_TYPED_FILESPEC)!=0) {
  259. printf("%s", catgets(_ttcatd, 6, 26,"Error: "));
  260. printf("%s: internal spec for file has an otype.\n",
  261. (char *)filename);
  262. }
  263. // put the spec on the list for later processing, unless
  264. // it is a filespec, which cannot be repaired (hope we
  265. // never get a bad filespec!)
  266. if (!is_filespec && opts->repairing_p()) {
  267. specs_to_repair->append(this);
  268. }
  269. }
  270. }