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