2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: lookup.c /main/4 1995/11/09 12:46:28 rswiston $ */
25 * (c) Copyright 1993, 1994 Hewlett-Packard Company
26 * (c) Copyright 1993, 1994 International Business Machines Corp.
27 * (c) Copyright 1993, 1994 Novell, Inc.
28 * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
31 #include <EUSCompat.h>
43 #include <sys/systeminfo.h>
46 #include "cmscalendar.h"
62 #define TIME_BUF_LEN 20
64 /******************************************************************************
65 * forward declaration of static functions used within the file
66 ******************************************************************************/
67 static CSA_return_code _AddToLinkedEntries(cms_entry *eptr, cms_entry **head,
68 cms_entry **tail, boolean_t sort, boolean_t time_only);
69 static CSA_return_code _EnumerateSequence(char *sender, uint access,
70 List_node *lnode, time_t start1,
72 boolean_t no_end_time_range, time_t end1,
73 time_t end2, CSA_uint32 num_attrs,
74 cms_attribute *attrs, CSA_enum *ops,
75 cms_entry **head, cms_entry **tail);
76 static CSA_return_code _GetEntryAttrsByName(_DtCmsCalendar *cal,
77 cms_entry *entry, uint num_names,
78 cms_attr_name *names, uint *num_attrs_r,
79 cms_attribute **attrs_r);
80 static CSA_return_code _GetAllEntryAttrs(cms_entry *entry, uint *num_attrs_r,
81 cms_attribute **attrs_r);
83 /*****************************************************************************
84 * extern functions used in the library
85 *****************************************************************************/
87 extern CSA_return_code
88 _DtCmsLookupEntriesById(
92 boolean_t no_start_time_range,
93 boolean_t no_end_time_range,
100 cms_attribute *attrs,
104 CSA_return_code stat = CSA_SUCCESS;
105 CSA_return_code stat2 = CSA_SUCCESS;
106 cms_entry *eptr, *head = NULL, *tail = NULL;
111 if (no_start_time_range) {
116 /* do lookup on repeating entries first */
117 stat = _DtCmsEnumerateSequenceById(cal, sender, access,
118 no_start_time_range, no_end_time_range, start1, start2,
119 end1, end2, id, num_attrs, attrs, ops, &head);
121 if (stat != CSA_X_DT_E_ENTRY_NOT_FOUND) {
122 if (stat == CSA_SUCCESS)
130 while ((eptr = (cms_entry *)rb_lookup_next_larger(cal->tree,
131 (caddr_t)&key)) && eptr->key.time < start2) {
132 if (eptr->key.id != id) {
133 key.time = eptr->key.time;
134 key.id = eptr->key.id;
136 aptr = &eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I];
137 if (!no_end_time_range) {
138 if (aptr->value == NULL)
139 return (CSA_SUCCESS);
141 _csa_iso8601_to_tick(
142 aptr->value->item.date_time_value,
145 if (endtick <= end1 || endtick >= end2)
146 return (CSA_SUCCESS);
149 if (_DtCmsMatchAttributes(eptr, num_attrs, attrs, ops)
150 && ( ((stat2 = _DtCmsCheckViewAccess(sender, access, eptr)) == CSA_SUCCESS) || (stat2 == CSA_E_TIME_ONLY) ) ) {
151 stat = _AddToLinkedEntries(eptr, &head, &tail,
159 return (CSA_SUCCESS);
162 extern CSA_return_code
169 boolean_t no_end_time_range,
172 CSA_uint32 num_attrs,
173 cms_attribute *attrs,
177 CSA_return_code stat = CSA_SUCCESS;
178 CSA_return_code stat2 = CSA_SUCCESS;
179 cms_entry *eptr, *head = NULL, *tail = NULL;
185 /* do lookup on one-time entries first */
188 while ((eptr = (cms_entry *)rb_lookup_next_larger(cal->tree,
189 (caddr_t)&key)) && eptr->key.time < start2) {
191 aptr = &eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I];
193 if (!no_end_time_range) {
194 if (aptr->value == NULL)
197 _csa_iso8601_to_tick(aptr->value->item.date_time_value,
200 if (endtick <= end1 || endtick >= end2)
204 if (_DtCmsMatchAttributes(eptr, num_attrs, attrs, ops) &&
205 _DtCmsCheckViewAccess(sender, access, eptr) == CSA_SUCCESS)
207 /* the last argument "sort" is set to B_FALSE,
208 * because the entries are in order already
209 * as we get them out from the tree;
210 * we don't want to sort it again
212 if ((stat = _AddToLinkedEntries(eptr, &head, &tail,
213 B_FALSE, B_FALSE)) != CSA_SUCCESS)
218 key.time = eptr->key.time;
219 key.id = eptr->key.id;
222 /* do lookup on repeating entries */
223 lnode = cal->list->root;
224 while (lnode != NULL && stat == CSA_SUCCESS) {
226 stat = _EnumerateSequence(sender, access, lnode, start1, start2,
227 no_end_time_range, end1, end2, num_attrs, attrs,
230 lnode = hc_lookup_next(lnode);
233 if (stat == CSA_SUCCESS)
236 _DtCm_free_cms_entries(head);
241 extern CSA_return_code
242 _DtCmsEnumerateSequenceById(
246 boolean_t no_start_time_range,
247 boolean_t no_end_time_range,
253 CSA_uint32 num_attrs,
254 cms_attribute *attrs,
258 CSA_return_code stat;
259 cms_entry *head = NULL, *tail = NULL;
264 lnode = hc_lookup_node(cal->list, (caddr_t)&key);
266 return (CSA_X_DT_E_ENTRY_NOT_FOUND);
268 if ( ((stat = _DtCmsCheckViewAccess(sender, access,
269 (cms_entry *)lnode->data)) != CSA_SUCCESS) && (stat != CSA_E_TIME_ONLY) )
273 if (no_start_time_range && no_end_time_range) {
274 if (_DtCmsMatchAttributes((cms_entry *)lnode->data, num_attrs,
276 stat = _DtCmsGetCmsEntryForClient(
277 (cms_entry *)lnode->data, entries,B_FALSE);
280 stat = _EnumerateSequence(sender, access, lnode, start1, start2,
281 no_end_time_range, end1, end2, num_attrs, attrs, ops,
284 if (stat == CSA_SUCCESS)
287 _DtCm_free_cms_entries(head);
293 extern CSA_return_code
294 _DtCmsLookupEntriesByKey(
301 cms_attr_name *names,
302 cms_get_entry_attr_res_item **res)
304 CSA_return_code stat = CSA_SUCCESS;
305 cms_get_entry_attr_res_item *eptr, *head;
308 for (i = 0, head = NULL; i < num_keys; i++) {
309 if ((stat = _DtCmsGetEntryAttrByKey(cal, user, access, keys[i],
310 num_names, names, NULL, &eptr)) == CSA_SUCCESS) {
314 if (head) _DtCmsFreeEntryAttrResItem(head);
320 return (CSA_SUCCESS);
324 * this routine returns either a cms_entry or
325 * an cms_get_entry_attr_res_item structure
326 * depending which output argument is not null
328 extern CSA_return_code
329 _DtCmsGetEntryAttrByKey(
335 cms_attr_name *names,
337 cms_get_entry_attr_res_item **res_r)
339 CSA_return_code stat;
340 CSA_return_code stat2;
341 cms_entry *entry = NULL;
343 char sbuf[TIME_BUF_LEN], ebuf[TIME_BUF_LEN];
344 time_t firsttick = 0;
346 cms_get_entry_attr_res_item *res;
348 if (entry_r == NULL && res_r == NULL)
349 return (CSA_E_INVALID_PARAMETER);
351 if (res_r && (res = (cms_get_entry_attr_res_item *)calloc(1,
352 sizeof(cms_get_entry_attr_res_item))) == NULL)
353 return (CSA_E_INSUFFICIENT_MEMORY);
355 /* do lookup on one-time entries first */
356 if ((entry = (cms_entry *)rb_lookup(cal->tree, (caddr_t)&key)) == NULL)
358 if ((lnode = (List_node *)hc_lookup_node(cal->list,
359 (caddr_t)&key)) != NULL)
361 entry = (cms_entry *)lnode->data;
362 if (_DtCmsInExceptionList(entry, key.time))
367 if ( (entry == NULL) || ( ((stat2 = _DtCmsCheckViewAccess(user, access, entry)) != CSA_SUCCESS) && (stat2 != CSA_E_TIME_ONLY)) ) {
369 stat = CSA_X_DT_E_ENTRY_NOT_FOUND;
371 stat = CSA_E_NO_AUTHORITY;
378 return (CSA_SUCCESS);
382 if (entry->key.time != key.time) {
383 /* set start and end time of the instance */
384 firsttick = entry->key.time;
385 entry->key.time = key.time;
387 stime = entry->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
388 item.date_time_value;
389 _csa_tick_to_iso8601(key.time, sbuf);
390 entry->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
391 item.date_time_value = sbuf;
393 if (entry->attrs[CSA_ENTRY_ATTR_END_DATE_I].value) {
394 etime = entry->attrs[CSA_ENTRY_ATTR_END_DATE_I].\
395 value->item.date_time_value;
396 _csa_tick_to_iso8601(key.time + lnode->duration, ebuf);
397 entry->attrs[CSA_ENTRY_ATTR_END_DATE_I].value->\
398 item.date_time_value = ebuf;
403 stat = _DtCmsGetCmsEntryForClient(entry, entry_r,B_FALSE);
406 stat = _GetAllEntryAttrs(entry, &res->num_attrs,
409 stat = _GetEntryAttrsByName(cal, entry, num_names,
410 names, &res->num_attrs, &res->attrs);
412 if (stat == CSA_SUCCESS) {
420 entry->key.time = firsttick;
421 entry->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
422 item.date_time_value = stime;
423 if (entry->attrs[CSA_ENTRY_ATTR_END_DATE_I].value)
424 entry->attrs[CSA_ENTRY_ATTR_END_DATE_I].value->\
425 item.date_time_value = etime;
431 /*****************************************************************************
432 * static functions used within the file
433 *****************************************************************************/
435 static CSA_return_code
443 CSA_return_code stat;
444 cms_entry *newptr, *prev, *ptr;
446 if ((stat = _DtCmsGetCmsEntryForClient(eptr, &newptr,time_only)) != CSA_SUCCESS)
449 if (sort == B_FALSE) {
450 /* just add to the end of the list */
455 (*tail)->next = newptr;
459 /* add item in ascending order */
460 for (prev = NULL, ptr = *head; ptr != NULL;
461 prev = ptr, ptr = ptr->next) {
462 if (eptr->key.time <= ptr->key.time)
476 return (CSA_SUCCESS);
479 static CSA_return_code
486 boolean_t no_end_time_range,
489 CSA_uint32 num_attrs,
490 cms_attribute *attrs,
495 CSA_return_code stat = CSA_SUCCESS;
496 CSA_return_code stat2 = CSA_SUCCESS;
498 time_t fsttick, tick;
499 RepeatEventState *restate;
501 eptr = (cms_entry *)lnode->data;
502 fsttick = eptr->key.time;
504 if (lnode->lasttick == 0) {
505 lnode->lasttick = LastTick(fsttick, lnode->re);
506 lnode->duration = _DtCmsGetDuration(eptr);
509 if (lnode->lasttick <= start1 || fsttick >= start2 ||
510 (!no_end_time_range &&
511 eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].value == NULL) ||
512 (!no_end_time_range && (lnode->lasttick+lnode->duration) <= end1) ||
513 (!no_end_time_range && (fsttick + lnode->duration) >= end2) ||
514 _DtCmsCheckViewAccess(sender, access, eptr) ||
515 !_DtCmsMatchAttributes(eptr, num_attrs, attrs, ops))
516 return (CSA_SUCCESS);
518 if (!no_end_time_range && start1 < (end1 - lnode->duration))
519 start1 = end1 - lnode->duration;
521 if (!no_end_time_range && ((end2 - lnode->duration) < start2))
522 start2 = end2 - lnode->duration;
524 for (tick = ClosestTick(start1, fsttick, lnode->re, &restate);
525 stat == CSA_SUCCESS && tick < start2;
526 tick = NextTick(tick, fsttick, lnode->re, restate))
529 char sbuf[TIME_BUF_LEN], ebuf[TIME_BUF_LEN];
531 if (tick <= 0 || tick > lnode->lasttick)
534 if (tick <= start1 || _DtCmsInExceptionList(eptr, tick))
537 /* set start and end time of the instance */
538 eptr->key.time = tick;
540 stime = eptr->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
541 item.date_time_value;
542 _csa_tick_to_iso8601(tick, sbuf);
543 eptr->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
544 item.date_time_value = sbuf;
546 if (eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].value) {
547 etime = eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].\
548 value->item.date_time_value;
549 _csa_tick_to_iso8601(tick + lnode->duration, ebuf);
550 eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].value->\
551 item.date_time_value = ebuf;
554 stat = _AddToLinkedEntries(eptr, head, tail, B_TRUE,B_FALSE);
556 eptr->key.time = fsttick;
557 eptr->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
558 item.date_time_value = stime;
559 if (eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].value)
560 eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].value->\
561 item.date_time_value = etime;
567 static CSA_return_code
568 _GetEntryAttrsByName(
572 cms_attr_name *names,
574 cms_attribute **attrs_r)
576 CSA_return_code stat = CSA_SUCCESS;
577 cms_attribute *attrs;
580 if ((attrs = calloc(1, sizeof(cms_attribute)*num_names)) == NULL)
581 return (CSA_E_INSUFFICIENT_MEMORY);
583 /* return all attrs names with hash number
584 * so that caller knows which one does not
585 * have value and which one does not even exist
587 for (i = 0; i < num_names && stat == CSA_SUCCESS; i++) {
588 if (names[i].name == NULL)
591 if (names[i].num <= 0)
592 names[i].num = _DtCm_get_index_from_table(
593 cal->entry_tbl, names[i].name);
595 if (names[i].num > 0) {
596 stat = _DtCm_copy_cms_attribute(&attrs[i],
597 &entry->attrs[names[i].num], B_TRUE);
599 attrs[i].name.num = -1;
600 if ((attrs[i].name.name = strdup(names[i].name))==NULL)
601 stat = CSA_E_INSUFFICIENT_MEMORY;
605 if (stat == CSA_SUCCESS) {
606 *num_attrs_r = num_names;
609 _DtCm_free_cms_attributes(i, attrs);
616 static CSA_return_code
620 cms_attribute **attrs_r)
622 CSA_return_code stat = CSA_SUCCESS;
623 cms_attribute *attrs;
626 if ((attrs = calloc(1, sizeof(cms_attribute)*entry->num_attrs)) == NULL)
627 return (CSA_E_INSUFFICIENT_MEMORY);
629 /* first element is not used */
630 for (i = 1, j = 0; i <= entry->num_attrs && stat == CSA_SUCCESS; i++) {
631 if (entry->attrs[i].value) {
632 if ((stat = _DtCm_copy_cms_attribute(&attrs[j],
633 &entry->attrs[i], B_TRUE))
641 if (stat == CSA_SUCCESS) {
651 _DtCm_free_cms_attributes(j, attrs);