dtcm: Resolve CID 87408
[oweals/cde.git] / cde / programs / dtcm / dtcm / cm_delete.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_delete.c
26 **
27 **  $TOG: cm_delete.c /main/7 1998/04/20 12:48:43 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_delete.c 1.29 95/03/17 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 <csa.h>
64 #include <nl_types.h>
65 #include <locale.h>
66 #include <Dt/Dt.h>
67 #include <Dt/EnvControlP.h>
68 #include "util.h"
69 #include "timeops.h"
70 #include "cm_tty.h"
71 #ifdef FNS
72 #include "dtfns.h"
73 #include "cmfns.h"
74 #endif
75
76 #if !defined(NL_CAT_LOCALE)
77 #define NL_CAT_LOCALE       0
78 #endif
79
80 int debug = 0;
81 static nl_catd  DT_catd;
82 static char cm_target[256] = "";        /* target for table (user@host) */
83 static char cm_date[256] = "";          /* appointment date */
84 static char cm_view[16] = "day";                /* view span (day,week,month) */
85 static int cm_today = 0;                /* today's date (in epoch time) */
86 static int cm_index = 0;                /* index to change/delete */
87
88 static char**
89 grab(char**argv,                                /* command line arguments */
90     char *buf,                          /* buffer for keyed data */
91     char stop_key)
92 {
93         if (!argv || !*argv) return(argv);
94         cm_strcpy (buf,*argv++);
95         while(argv && *argv) {
96                 if (*(*argv) == stop_key) break;
97                 cm_strcat(buf," ");
98                 cm_strcat(buf,*argv++);
99         }
100         argv--;
101         return(argv);
102 }
103
104 static void
105 cm_args(int argc, char **argv)
106 {
107
108         while (++argv && *argv) {
109                 switch(*(*argv+1)) {
110                 case 't':
111                 case 'c':
112                         argv = grab(++argv,cm_target,'-');
113                         break;
114                 case 'd':
115                         argv = grab(++argv,cm_date,'-');
116                         break;
117                 case 'v':
118                         argv = grab(++argv,cm_view,'-');
119                         break;
120                 default:
121                         fprintf(stderr, "%s", catgets(DT_catd, 1, 187, "Usage:\n\tdtcm_delete [-c calendar] [-d <mm/dd/yy>] [-v view]\n"));
122                         exit(1);
123                 }
124         }
125 }
126
127 int main(int argc, char **argv)
128 {
129         int             cnt;
130         char            index[10], *target = NULL, *date = NULL, *view = NULL,
131                         *uname, *loc;
132         Props           *p = NULL;
133         CSA_entry_handle        *list;
134         CSA_session_handle      c_handle;
135         CSA_return_code         stat;
136         CSA_calendar_user       csa_user;
137         int                     version;
138 #ifdef FNS
139         char            buf[256];
140 #endif
141
142         init_time();
143         setlocale(LC_ALL, "");
144         _DtEnvControl(DT_ENV_SET); /* set up environment variables */
145         DT_catd = catopen(DTCM_CAT, NL_CAT_LOCALE);
146         cm_tty_load_props(&p);
147         cm_args(argc,argv);             /* parse command line */
148         target = (cm_strlen(cm_target)) ? cm_target : cm_get_credentials();
149
150 #ifdef FNS
151         dtfns_init();
152         if (cmfns_use_fns(p)) {
153                 cmfns_lookup_calendar(target, buf, sizeof(buf));
154                 target = buf;
155         }
156 #endif
157         if (cm_strlen(cm_date)) date = cm_date;
158         if (cm_strlen(cm_view)) view = cm_view;
159
160         uname = cm_target2name(target);
161         loc = cm_target2location(target);
162
163         csa_user.user_name = target;
164         csa_user.user_type = 0;
165         csa_user.calendar_user_extensions = NULL;
166         csa_user.calendar_address = target;
167         stat = csa_logon(NULL, &csa_user, NULL, NULL, NULL, &c_handle, NULL);
168
169         if (stat != CSA_SUCCESS) {
170                 char *format = strdup(catgets(DT_catd, 1, 188,
171                                            "\nCould not open calendar %s\n"));
172                 fprintf(stderr, format,
173                         target ? target : catgets(DT_catd, 1, 189, "UNKNOWN"));
174                 free(format);
175                 free(uname);
176                 free(loc);
177                 exit(1);
178         }
179         version = get_data_version(c_handle);
180
181         while (!cm_index) {
182                 if ((cnt = cm_tty_lookup(DT_catd, c_handle, version, date, 
183                                         view, &list, p)) <= 0) {
184                         csa_logoff(c_handle, NULL);
185                         free(uname);
186                         free(loc);
187                         exit(0);
188                 }
189                 fprintf(stdout, "%s", catgets(DT_catd, 1, 190,
190                                        "Item to delete (number)? "));
191                 fgets(index, 9, stdin);
192                 fprintf(stdout,"\n\n");
193                 if (*index < '0' || *index > '9') {
194                         csa_free(list);
195                         break;
196                 }
197                 cm_index = atoi(index);
198                 if (cnt >= cm_index)
199                         cm_tty_delete(DT_catd, c_handle, version, 
200                                       cm_index - 1, list);
201                 memset(index, '\0', 10);
202                 csa_free(list);
203                 cm_index = 0;
204         }
205
206         csa_logoff(c_handle, NULL);
207         props_clean_up(p);
208         free(p);
209         free(uname);
210         free(loc);
211         return 0;
212 }