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>
41 #if !defined(CSRG_BASED)
45 #include <sys/systeminfo.h>
48 #include "cmscalendar.h"
64 #define TIME_BUF_LEN 20
66 /******************************************************************************
67 * forward declaration of static functions used within the file
68 ******************************************************************************/
69 static CSA_return_code _AddToLinkedEntries(cms_entry *eptr, cms_entry **head,
70 cms_entry **tail, boolean_t sort, boolean_t time_only);
71 static CSA_return_code _EnumerateSequence(char *sender, uint access,
72 List_node *lnode, time_t start1,
74 boolean_t no_end_time_range, time_t end1,
75 time_t end2, CSA_uint32 num_attrs,
76 cms_attribute *attrs, CSA_enum *ops,
77 cms_entry **head, cms_entry **tail);
78 static CSA_return_code _GetEntryAttrsByName(_DtCmsCalendar *cal,
79 cms_entry *entry, uint num_names,
80 cms_attr_name *names, uint *num_attrs_r,
81 cms_attribute **attrs_r);
82 static CSA_return_code _GetAllEntryAttrs(cms_entry *entry, uint *num_attrs_r,
83 cms_attribute **attrs_r);
85 /*****************************************************************************
86 * extern functions used in the library
87 *****************************************************************************/
89 extern CSA_return_code
90 _DtCmsLookupEntriesById(
94 boolean_t no_start_time_range,
95 boolean_t no_end_time_range,
101 CSA_uint32 num_attrs,
102 cms_attribute *attrs,
106 CSA_return_code stat = CSA_SUCCESS;
107 CSA_return_code stat2 = CSA_SUCCESS;
108 cms_entry *eptr, *head = NULL, *tail = NULL;
113 if (no_start_time_range) {
118 /* do lookup on repeating entries first */
119 stat = _DtCmsEnumerateSequenceById(cal, sender, access,
120 no_start_time_range, no_end_time_range, start1, start2,
121 end1, end2, id, num_attrs, attrs, ops, &head);
123 if (stat != CSA_X_DT_E_ENTRY_NOT_FOUND) {
124 if (stat == CSA_SUCCESS)
132 while ((eptr = (cms_entry *)rb_lookup_next_larger(cal->tree,
133 (caddr_t)&key)) && eptr->key.time < start2) {
134 if (eptr->key.id != id) {
135 key.time = eptr->key.time;
136 key.id = eptr->key.id;
138 aptr = &eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I];
139 if (!no_end_time_range) {
140 if (aptr->value == NULL)
141 return (CSA_SUCCESS);
143 _csa_iso8601_to_tick(
144 aptr->value->item.date_time_value,
147 if (endtick <= end1 || endtick >= end2)
148 return (CSA_SUCCESS);
151 if (_DtCmsMatchAttributes(eptr, num_attrs, attrs, ops)
152 && ( ((stat2 = _DtCmsCheckViewAccess(sender, access, eptr)) == CSA_SUCCESS) || (stat2 == CSA_E_TIME_ONLY) ) ) {
153 stat = _AddToLinkedEntries(eptr, &head, &tail,
161 return (CSA_SUCCESS);
164 extern CSA_return_code
171 boolean_t no_end_time_range,
174 CSA_uint32 num_attrs,
175 cms_attribute *attrs,
179 CSA_return_code stat = CSA_SUCCESS;
180 CSA_return_code stat2 = CSA_SUCCESS;
181 cms_entry *eptr, *head = NULL, *tail = NULL;
187 /* do lookup on one-time entries first */
190 while ((eptr = (cms_entry *)rb_lookup_next_larger(cal->tree,
191 (caddr_t)&key)) && eptr->key.time < start2) {
193 aptr = &eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I];
195 if (!no_end_time_range) {
196 if (aptr->value == NULL)
199 _csa_iso8601_to_tick(aptr->value->item.date_time_value,
202 if (endtick <= end1 || endtick >= end2)
206 if (_DtCmsMatchAttributes(eptr, num_attrs, attrs, ops) &&
207 _DtCmsCheckViewAccess(sender, access, eptr) == CSA_SUCCESS)
209 /* the last argument "sort" is set to B_FALSE,
210 * because the entries are in order already
211 * as we get them out from the tree;
212 * we don't want to sort it again
214 if ((stat = _AddToLinkedEntries(eptr, &head, &tail,
215 B_FALSE, B_FALSE)) != CSA_SUCCESS)
220 key.time = eptr->key.time;
221 key.id = eptr->key.id;
224 /* do lookup on repeating entries */
225 lnode = cal->list->root;
226 while (lnode != NULL && stat == CSA_SUCCESS) {
228 stat = _EnumerateSequence(sender, access, lnode, start1, start2,
229 no_end_time_range, end1, end2, num_attrs, attrs,
232 lnode = hc_lookup_next(lnode);
235 if (stat == CSA_SUCCESS)
238 _DtCm_free_cms_entries(head);
243 extern CSA_return_code
244 _DtCmsEnumerateSequenceById(
248 boolean_t no_start_time_range,
249 boolean_t no_end_time_range,
255 CSA_uint32 num_attrs,
256 cms_attribute *attrs,
260 CSA_return_code stat;
261 cms_entry *head = NULL, *tail = NULL;
266 lnode = hc_lookup_node(cal->list, (caddr_t)&key);
268 return (CSA_X_DT_E_ENTRY_NOT_FOUND);
270 if ( ((stat = _DtCmsCheckViewAccess(sender, access,
271 (cms_entry *)lnode->data)) != CSA_SUCCESS) && (stat != CSA_E_TIME_ONLY) )
275 if (no_start_time_range && no_end_time_range) {
276 if (_DtCmsMatchAttributes((cms_entry *)lnode->data, num_attrs,
278 stat = _DtCmsGetCmsEntryForClient(
279 (cms_entry *)lnode->data, entries,B_FALSE);
282 stat = _EnumerateSequence(sender, access, lnode, start1, start2,
283 no_end_time_range, end1, end2, num_attrs, attrs, ops,
286 if (stat == CSA_SUCCESS)
289 _DtCm_free_cms_entries(head);
295 extern CSA_return_code
296 _DtCmsLookupEntriesByKey(
303 cms_attr_name *names,
304 cms_get_entry_attr_res_item **res)
306 CSA_return_code stat = CSA_SUCCESS;
307 cms_get_entry_attr_res_item *eptr, *head;
310 for (i = 0, head = NULL; i < num_keys; i++) {
311 if ((stat = _DtCmsGetEntryAttrByKey(cal, user, access, keys[i],
312 num_names, names, NULL, &eptr)) == CSA_SUCCESS) {
316 if (head) _DtCmsFreeEntryAttrResItem(head);
322 return (CSA_SUCCESS);
326 * this routine returns either a cms_entry or
327 * an cms_get_entry_attr_res_item structure
328 * depending which output argument is not null
330 extern CSA_return_code
331 _DtCmsGetEntryAttrByKey(
337 cms_attr_name *names,
339 cms_get_entry_attr_res_item **res_r)
341 CSA_return_code stat;
342 CSA_return_code stat2;
343 cms_entry *entry = NULL;
345 char sbuf[TIME_BUF_LEN], ebuf[TIME_BUF_LEN];
346 time_t firsttick = 0;
348 cms_get_entry_attr_res_item *res;
350 if (entry_r == NULL && res_r == NULL)
351 return (CSA_E_INVALID_PARAMETER);
353 if (res_r && (res = (cms_get_entry_attr_res_item *)calloc(1,
354 sizeof(cms_get_entry_attr_res_item))) == NULL)
355 return (CSA_E_INSUFFICIENT_MEMORY);
357 /* do lookup on one-time entries first */
358 if ((entry = (cms_entry *)rb_lookup(cal->tree, (caddr_t)&key)) == NULL)
360 if ((lnode = (List_node *)hc_lookup_node(cal->list,
361 (caddr_t)&key)) != NULL)
363 entry = (cms_entry *)lnode->data;
364 if (_DtCmsInExceptionList(entry, key.time))
369 if ( (entry == NULL) || ( ((stat2 = _DtCmsCheckViewAccess(user, access, entry)) != CSA_SUCCESS) && (stat2 != CSA_E_TIME_ONLY)) ) {
371 stat = CSA_X_DT_E_ENTRY_NOT_FOUND;
373 stat = CSA_E_NO_AUTHORITY;
380 return (CSA_SUCCESS);
384 if (entry->key.time != key.time) {
385 /* set start and end time of the instance */
386 firsttick = entry->key.time;
387 entry->key.time = key.time;
389 stime = entry->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
390 item.date_time_value;
391 _csa_tick_to_iso8601(key.time, sbuf);
392 entry->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
393 item.date_time_value = sbuf;
395 if (entry->attrs[CSA_ENTRY_ATTR_END_DATE_I].value) {
396 etime = entry->attrs[CSA_ENTRY_ATTR_END_DATE_I].\
397 value->item.date_time_value;
398 _csa_tick_to_iso8601(key.time + lnode->duration, ebuf);
399 entry->attrs[CSA_ENTRY_ATTR_END_DATE_I].value->\
400 item.date_time_value = ebuf;
405 stat = _DtCmsGetCmsEntryForClient(entry, entry_r,B_FALSE);
408 stat = _GetAllEntryAttrs(entry, &res->num_attrs,
411 stat = _GetEntryAttrsByName(cal, entry, num_names,
412 names, &res->num_attrs, &res->attrs);
414 if (stat == CSA_SUCCESS) {
422 entry->key.time = firsttick;
423 entry->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
424 item.date_time_value = stime;
425 if (entry->attrs[CSA_ENTRY_ATTR_END_DATE_I].value)
426 entry->attrs[CSA_ENTRY_ATTR_END_DATE_I].value->\
427 item.date_time_value = etime;
433 /*****************************************************************************
434 * static functions used within the file
435 *****************************************************************************/
437 static CSA_return_code
445 CSA_return_code stat;
446 cms_entry *newptr, *prev, *ptr;
448 if ((stat = _DtCmsGetCmsEntryForClient(eptr, &newptr,time_only)) != CSA_SUCCESS)
451 if (sort == B_FALSE) {
452 /* just add to the end of the list */
457 (*tail)->next = newptr;
461 /* add item in ascending order */
462 for (prev = NULL, ptr = *head; ptr != NULL;
463 prev = ptr, ptr = ptr->next) {
464 if (eptr->key.time <= ptr->key.time)
478 return (CSA_SUCCESS);
481 static CSA_return_code
488 boolean_t no_end_time_range,
491 CSA_uint32 num_attrs,
492 cms_attribute *attrs,
497 CSA_return_code stat = CSA_SUCCESS;
498 CSA_return_code stat2 = CSA_SUCCESS;
500 time_t fsttick, tick;
501 RepeatEventState *restate;
503 eptr = (cms_entry *)lnode->data;
504 fsttick = eptr->key.time;
506 if (lnode->lasttick == 0) {
507 lnode->lasttick = LastTick(fsttick, lnode->re);
508 lnode->duration = _DtCmsGetDuration(eptr);
511 if (lnode->lasttick <= start1 || fsttick >= start2 ||
512 (!no_end_time_range &&
513 eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].value == NULL) ||
514 (!no_end_time_range && (lnode->lasttick+lnode->duration) <= end1) ||
515 (!no_end_time_range && (fsttick + lnode->duration) >= end2) ||
516 _DtCmsCheckViewAccess(sender, access, eptr) ||
517 !_DtCmsMatchAttributes(eptr, num_attrs, attrs, ops))
518 return (CSA_SUCCESS);
520 if (!no_end_time_range && start1 < (end1 - lnode->duration))
521 start1 = end1 - lnode->duration;
523 if (!no_end_time_range && ((end2 - lnode->duration) < start2))
524 start2 = end2 - lnode->duration;
526 for (tick = ClosestTick(start1, fsttick, lnode->re, &restate);
527 stat == CSA_SUCCESS && tick < start2;
528 tick = NextTick(tick, fsttick, lnode->re, restate))
531 char sbuf[TIME_BUF_LEN], ebuf[TIME_BUF_LEN];
533 if (tick <= 0 || tick > lnode->lasttick)
536 if (tick <= start1 || _DtCmsInExceptionList(eptr, tick))
539 /* set start and end time of the instance */
540 eptr->key.time = tick;
542 stime = eptr->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
543 item.date_time_value;
544 _csa_tick_to_iso8601(tick, sbuf);
545 eptr->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
546 item.date_time_value = sbuf;
548 if (eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].value) {
549 etime = eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].\
550 value->item.date_time_value;
551 _csa_tick_to_iso8601(tick + lnode->duration, ebuf);
552 eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].value->\
553 item.date_time_value = ebuf;
556 stat = _AddToLinkedEntries(eptr, head, tail, B_TRUE,B_FALSE);
558 eptr->key.time = fsttick;
559 eptr->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
560 item.date_time_value = stime;
561 if (eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].value)
562 eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].value->\
563 item.date_time_value = etime;
569 static CSA_return_code
570 _GetEntryAttrsByName(
574 cms_attr_name *names,
576 cms_attribute **attrs_r)
578 CSA_return_code stat = CSA_SUCCESS;
579 cms_attribute *attrs;
582 if ((attrs = calloc(1, sizeof(cms_attribute)*num_names)) == NULL)
583 return (CSA_E_INSUFFICIENT_MEMORY);
585 /* return all attrs names with hash number
586 * so that caller knows which one does not
587 * have value and which one does not even exist
589 for (i = 0; i < num_names && stat == CSA_SUCCESS; i++) {
590 if (names[i].name == NULL)
593 if (names[i].num <= 0)
594 names[i].num = _DtCm_get_index_from_table(
595 cal->entry_tbl, names[i].name);
597 if (names[i].num > 0) {
598 stat = _DtCm_copy_cms_attribute(&attrs[i],
599 &entry->attrs[names[i].num], B_TRUE);
601 attrs[i].name.num = -1;
602 if ((attrs[i].name.name = strdup(names[i].name))==NULL)
603 stat = CSA_E_INSUFFICIENT_MEMORY;
607 if (stat == CSA_SUCCESS) {
608 *num_attrs_r = num_names;
611 _DtCm_free_cms_attributes(i, attrs);
618 static CSA_return_code
622 cms_attribute **attrs_r)
624 CSA_return_code stat = CSA_SUCCESS;
625 cms_attribute *attrs;
628 if ((attrs = calloc(1, sizeof(cms_attribute)*entry->num_attrs)) == NULL)
629 return (CSA_E_INSUFFICIENT_MEMORY);
631 /* first element is not used */
632 for (i = 1, j = 0; i <= entry->num_attrs && stat == CSA_SUCCESS; i++) {
633 if (entry->attrs[i].value) {
634 if ((stat = _DtCm_copy_cms_attribute(&attrs[j],
635 &entry->attrs[i], B_TRUE))
643 if (stat == CSA_SUCCESS) {
653 _DtCm_free_cms_attributes(j, attrs);