Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtcm / dtcm / cm_admin.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 **  $XConsortium: cm_admin.c /main/5 1996/07/09 14:48:00 drk $
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.21 94/08/05 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
73 #if !defined(NL_CAT_LOCALE)
74 #define NL_CAT_LOCALE       0
75 #endif
76
77 typedef enum {archive, restore} Admin_mode;
78
79 static Admin_mode Mode = archive;
80 static char cm_target[MAXPATHLEN];
81 static char cm_start[MAXPATHLEN];
82 static char cm_end[MAXPATHLEN];
83 static char cm_file[MAXPATHLEN];
84 static int Delete = B_FALSE;
85 static FILE *input_stream = stdin;
86 static FILE *output_stream = stdout;
87
88 int debug = 0;
89 static nl_catd  DT_catd;
90
91 static char**
92 grab(char**argv,                                /* command line arguments */
93     char *buf,                          /* buffer for keyed data */
94     char stop_key)
95 {
96         if (!argv || !*argv) return(argv);
97         cm_strcpy (buf,*argv++);
98         while(argv && *argv) {
99                 if (*(*argv) == stop_key) break;
100                 cm_strcat(buf," ");
101                 cm_strcat(buf,*argv++);
102         }
103         argv--;
104         return(argv);
105 }
106
107 static void
108 usage_msg() {
109         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"));
110 }
111
112 static void
113 cm_args(int argc, char **argv)
114 {
115
116         while (++argv && *argv) {
117                 switch(*(*argv+1)) {
118                 case 'a':
119                         ++argv;
120                         if (strcmp(*argv, "archive") == 0)
121                                 Mode = archive;
122                         else if (strcmp(*argv, "restore") == 0)
123                                 Mode = restore;
124                         else {
125                                 fprintf(stderr,catgets(DT_catd, 1, 697, "You have specified an unsupported administration operation \"%s\".\n"), *argv);
126                                 usage_msg();
127                                 exit(1);
128                         }
129                         break;
130                 case 'c':
131                         argv = grab(++argv,cm_target,'-');
132                         break;
133                 case 's':
134                         argv = grab(++argv,cm_start,'-');
135                         break;
136                 case 'e':
137                         argv = grab(++argv,cm_end,'-');
138                         break;
139                 case 'f':
140                         argv = grab(++argv,cm_file,'-');
141                         break;
142                 case 'd':
143                         Delete = B_TRUE;
144                         break;
145
146                 case 'h':
147                 default:
148                         usage_msg();
149                         exit(1);
150                 }
151         }
152 }
153
154 int main(int argc, char **argv)
155 {
156         int             cnt;
157         char            *target = NULL, *date = NULL, *view = NULL,
158                         *uname, *loc;
159         Props           *p = NULL;
160         CSA_session_handle      c_handle;
161         CSA_entry_handle        *list;
162         CSA_calendar_user       csa_user;
163         CSA_return_code         stat;
164 #ifdef FNS
165         char            buf[256];
166 #endif
167
168         init_time();
169         setlocale(LC_ALL, "");
170         DT_catd = catopen(DTCM_CAT, NL_CAT_LOCALE);
171         cm_tty_load_props(&p);
172         cm_args(argc,argv);             /* parse command line */
173
174         target = (cm_strlen(cm_target)) ? cm_target : cm_get_credentials();
175 #ifdef FNS
176         dtfns_init();
177         if (cmfns_use_fns(p)) {
178                 cmfns_lookup_calendar(target, buf, sizeof(buf));
179                 target = buf;
180         }
181 #endif
182         uname = cm_target2name(target);
183         loc = cm_target2location(target);
184
185         /* check to see if the file name parameter makes sense */
186
187         if (cm_file[0]) {
188                 if (Mode == archive) {
189                         if ((output_stream = fopen(cm_file, "w")) == NULL) {
190                                 fprintf(stderr, catgets(DT_catd, 1, 698, "\nCould not open output file \"%s\"\n"), cm_file);
191                                 exit(1);
192                         }
193
194                 }
195
196                 if (Mode == restore) {
197                         if ((input_stream = fopen(cm_file, "r")) == NULL) {
198                                 fprintf(stderr, catgets(DT_catd, 1, 699, "\nCould not open input file \"%s\"\n"), cm_file);
199                                 exit(1);
200                         }
201                 }
202         }
203         csa_user.user_name = target;
204         csa_user.user_type = 0;
205         csa_user.calendar_user_extensions = NULL;
206         csa_user.calendar_address = target;
207         stat = csa_logon(NULL, &csa_user, NULL, NULL, NULL, &c_handle, NULL);
208
209         if (stat != CSA_SUCCESS) {
210                 char *format = XtNewString(catgets(DT_catd, 1, 208, 
211                                          "\nCould not open calendar \"%s\"\n"));
212                 fprintf(stderr, format, 
213                         target ? target : catgets(DT_catd, 1, 209, "UNKNOWN"));
214                 XtFree(format);
215                 free(uname);
216                 free(loc);
217                 exit(1);
218         }
219
220         csa_logoff(c_handle, NULL);
221         props_clean_up(p);
222         free(p);
223         free(uname);
224         free(loc);
225         return 0;
226 }