Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtcm / dtcm / cm_admin.c
1 /*******************************************************************************
2 **
3 **  cm_lookup.c
4 **
5 **  $XConsortium: cm_admin.c /main/5 1996/07/09 14:48:00 drk $
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_lookup.c 1.21 94/08/05 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 <nl_types.h>
42 #include <locale.h>
43 #include "cm_tty.h"
44 #include "util.h"
45 #include "timeops.h"
46 #ifdef FNS
47 #include "dtfns.h"
48 #include "cmfns.h"
49 #endif
50
51 #if !defined(NL_CAT_LOCALE)
52 #define NL_CAT_LOCALE       0
53 #endif
54
55 typedef enum {archive, restore} Admin_mode;
56
57 static Admin_mode Mode = archive;
58 static char cm_target[MAXPATHLEN];
59 static char cm_start[MAXPATHLEN];
60 static char cm_end[MAXPATHLEN];
61 static char cm_file[MAXPATHLEN];
62 static int Delete = B_FALSE;
63 static FILE *input_stream = stdin;
64 static FILE *output_stream = stdout;
65
66 int debug = 0;
67 static nl_catd  DT_catd;
68
69 static char**
70 grab(char**argv,                                /* command line arguments */
71     char *buf,                          /* buffer for keyed data */
72     char stop_key)
73 {
74         if (!argv || !*argv) return(argv);
75         cm_strcpy (buf,*argv++);
76         while(argv && *argv) {
77                 if (*(*argv) == stop_key) break;
78                 cm_strcat(buf," ");
79                 cm_strcat(buf,*argv++);
80         }
81         argv--;
82         return(argv);
83 }
84
85 static void
86 usage_msg() {
87         fprintf(stderr,catgets(DT_catd, 1, 696, "Usage:\n\tdtcm_admin [ -d ] [ -a action ] [ -c calendar ][ -s <mm/dd/yy> ] [ -e <mm/dd/yy> ] [-f filename]\n"));
88 }
89
90 static void
91 cm_args(int argc, char **argv)
92 {
93
94         while (++argv && *argv) {
95                 switch(*(*argv+1)) {
96                 case 'a':
97                         ++argv;
98                         if (strcmp(*argv, "archive") == 0)
99                                 Mode = archive;
100                         else if (strcmp(*argv, "restore") == 0)
101                                 Mode = restore;
102                         else {
103                                 fprintf(stderr,catgets(DT_catd, 1, 697, "You have specified an unsupported administration operation \"%s\".\n"), *argv);
104                                 usage_msg();
105                                 exit(1);
106                         }
107                         break;
108                 case 'c':
109                         argv = grab(++argv,cm_target,'-');
110                         break;
111                 case 's':
112                         argv = grab(++argv,cm_start,'-');
113                         break;
114                 case 'e':
115                         argv = grab(++argv,cm_end,'-');
116                         break;
117                 case 'f':
118                         argv = grab(++argv,cm_file,'-');
119                         break;
120                 case 'd':
121                         Delete = B_TRUE;
122                         break;
123
124                 case 'h':
125                 default:
126                         usage_msg();
127                         exit(1);
128                 }
129         }
130 }
131
132 int main(int argc, char **argv)
133 {
134         int             cnt;
135         char            *target = NULL, *date = NULL, *view = NULL,
136                         *uname, *loc;
137         Props           *p = NULL;
138         CSA_session_handle      c_handle;
139         CSA_entry_handle        *list;
140         CSA_calendar_user       csa_user;
141         CSA_return_code         stat;
142 #ifdef FNS
143         char            buf[256];
144 #endif
145
146         init_time();
147         setlocale(LC_ALL, "");
148         DT_catd = catopen(DTCM_CAT, NL_CAT_LOCALE);
149         cm_tty_load_props(&p);
150         cm_args(argc,argv);             /* parse command line */
151
152         target = (cm_strlen(cm_target)) ? cm_target : cm_get_credentials();
153 #ifdef FNS
154         dtfns_init();
155         if (cmfns_use_fns(p)) {
156                 cmfns_lookup_calendar(target, buf, sizeof(buf));
157                 target = buf;
158         }
159 #endif
160         uname = cm_target2name(target);
161         loc = cm_target2location(target);
162
163         /* check to see if the file name parameter makes sense */
164
165         if (cm_file[0]) {
166                 if (Mode == archive) {
167                         if ((output_stream = fopen(cm_file, "w")) == NULL) {
168                                 fprintf(stderr, catgets(DT_catd, 1, 698, "\nCould not open output file \"%s\"\n"), cm_file);
169                                 exit(1);
170                         }
171
172                 }
173
174                 if (Mode == restore) {
175                         if ((input_stream = fopen(cm_file, "r")) == NULL) {
176                                 fprintf(stderr, catgets(DT_catd, 1, 699, "\nCould not open input file \"%s\"\n"), cm_file);
177                                 exit(1);
178                         }
179                 }
180         }
181         csa_user.user_name = target;
182         csa_user.user_type = 0;
183         csa_user.calendar_user_extensions = NULL;
184         csa_user.calendar_address = target;
185         stat = csa_logon(NULL, &csa_user, NULL, NULL, NULL, &c_handle, NULL);
186
187         if (stat != CSA_SUCCESS) {
188                 char *format = XtNewString(catgets(DT_catd, 1, 208, 
189                                          "\nCould not open calendar \"%s\"\n"));
190                 fprintf(stderr, format, 
191                         target ? target : catgets(DT_catd, 1, 209, "UNKNOWN"));
192                 XtFree(format);
193                 free(uname);
194                 free(loc);
195                 exit(1);
196         }
197
198         csa_logoff(c_handle, NULL);
199         props_clean_up(p);
200         free(p);
201         free(uname);
202         free(loc);
203         return 0;
204 }