dtcm: Resolve CID 87562
[oweals/cde.git] / cde / programs / dtcm / dtcm / cm_lookup.c
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 librararies 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 **
25 **  cm_lookup.c
26 **
27 **  $TOG: cm_lookup.c /main/8 1998/04/21 10:21:26 mgreess $
28 **
29 **  RESTRICTED CONFIDENTIAL INFORMATION:
30 **
31 **  The information in this document is subject to special
32 **  restrictions in a confidential disclosure agreement between
33 **  HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
34 **  document outside HP, IBM, Sun, USL, SCO, or Univel without
35 **  Sun's specific written approval.  This document and all copies
36 **  and derivative works thereof must be returned or destroyed at
37 **  Sun's request.
38 **
39 **  Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
40 **
41 *******************************************************************************/
42
43 /*                                                                      *
44  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
45  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
46  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
47  * (c) Copyright 1993, 1994 Novell, Inc.                                *
48  */
49
50 #ifndef lint
51 static  char sccsid[] = "@(#)cm_lookup.c 1.30 95/03/08 Copyr 1993 Sun Microsystems, Inc.";
52 #endif
53
54 #include <EUSCompat.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <ctype.h>
58 #include <string.h>
59 #include <rpc/rpc.h>
60 #include <unistd.h>
61 #include <sys/resource.h>
62 #include <sys/wait.h>
63 #include <nl_types.h>
64 #include <locale.h>
65 #include "cm_tty.h"
66 #include "util.h"
67 #include "timeops.h"
68 #ifdef FNS
69 #include "dtfns.h"
70 #include "cmfns.h"
71 #endif
72 #include <Dt/Dt.h>
73 #include <Dt/EnvControlP.h>
74
75 #if !defined(NL_CAT_LOCALE)
76 #define NL_CAT_LOCALE       0
77 #endif
78
79 int debug = 0;
80 static char cm_target[256] = "";        /* target for table (user@host) */
81 static char cm_date[256] = "";          /* appointment date */
82 static char cm_view[16] = "";           /* view span (day,week,month) */
83 static nl_catd  DT_catd;
84
85 static char**
86 grab(char**argv,                                /* command line arguments */
87     char *buf,                          /* buffer for keyed data */
88     char stop_key)
89 {
90         if (!argv || !*argv) return(argv);
91         cm_strcpy (buf,*argv++);
92         while(argv && *argv) {
93                 if (*(*argv) == stop_key) break;
94                 cm_strcat(buf," ");
95                 cm_strcat(buf,*argv++);
96         }
97         argv--;
98         return(argv);
99 }
100
101 static void
102 cm_args(int argc, char **argv)
103 {
104
105         while (++argv && *argv) {
106                 switch(*(*argv+1)) {
107                 case 't':
108                 case 'c':
109                         argv = grab(++argv,cm_target,'-');
110                         break;
111                 case 'd':
112                         argv = grab(++argv,cm_date,'-');
113                         break;
114                 case 'v':
115                         argv = grab(++argv,cm_view,'-');
116                         break;
117                 default:
118                         fprintf(stderr, "%s", catgets(DT_catd, 1, 207, "Usage:\n\tdtcm_lookup [ -c calendar ][ -d <mm/dd/yy> ] [ -v view ]\n"));
119                         exit(1);
120                 }
121         }
122 }
123
124 int main(int argc, char **argv)
125 {
126         int             cnt;
127         char            *target = NULL, *date = NULL, *view = NULL,
128                         *uname, *loc;
129         Props           *p = NULL;
130         CSA_session_handle      c_handle;
131         CSA_entry_handle        *list;
132         CSA_return_code         stat;
133         CSA_calendar_user       csa_user;
134         int                     version;
135 #ifdef FNS
136         char            buf[256];
137 #endif
138
139         init_time();
140         _DtEnvControl(DT_ENV_SET); /* set up environment variables */
141         setlocale(LC_ALL, "");
142         DT_catd = catopen(DTCM_CAT, NL_CAT_LOCALE);
143         cm_tty_load_props(&p);
144         cm_args(argc,argv);             /* parse command line */
145
146         target = (cm_strlen(cm_target)) ? cm_target : cm_get_credentials();
147 #ifdef FNS
148         dtfns_init();
149         if (cmfns_use_fns(p)) {
150                 cmfns_lookup_calendar(target, buf, sizeof(buf));
151                 target = buf;
152         }
153 #endif
154         uname = cm_target2name(target);
155         loc = cm_target2location(target);
156
157         csa_user.user_name = target;
158         csa_user.user_type = 0;
159         csa_user.calendar_user_extensions = NULL;
160         csa_user.calendar_address = target;
161         stat = csa_logon(NULL, &csa_user, NULL, NULL, NULL, &c_handle, NULL);
162
163         if (stat != CSA_SUCCESS) {
164                 char *format = cm_strdup(catgets(DT_catd, 1, 208, 
165                                    "\nCould not open calendar \"%s\"\n"));
166                 fprintf(stderr, format, 
167                         target ? target : catgets(DT_catd, 1, 209, "UNKNOWN"));
168                 free(format);
169                 free(uname);
170                 free(loc);
171                 exit(1);
172         }
173         version = get_data_version(c_handle);
174         if (cm_strlen(cm_date)) date = cm_date;
175         if (cm_strlen(cm_view)) view = cm_view;
176
177         if ((cnt = cm_tty_lookup(DT_catd, c_handle, version, date, view, 
178                                  &list, p)) > 0)
179                 csa_free(list);
180         csa_logoff(c_handle, NULL);
181         props_clean_up(p);
182         free(p);
183         free(uname);
184         free(loc);
185         return 0;
186 }