dtcm: Resolve CID 87408
[oweals/cde.git] / cde / programs / dtcm / dtcm / cm_insert.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_insert.c
26 **
27 **  $TOG: cm_insert.c /main/8 1998/04/20 13:08:52 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_insert.c 1.31 95/05/19 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 "util.h"
66 #include "getdate.h"
67 #include "timeops.h"
68 #include "props.h"
69 #include "cm_tty.h"
70 #ifdef FNS
71 #include "dtfns.h"
72 #include "cmfns.h"
73 #endif
74 #include <Dt/Dt.h>
75 #include <Dt/EnvControlP.h>
76
77 #if !defined(NL_CAT_LOCALE)
78 #define NL_CAT_LOCALE       0
79 #endif
80
81 int debug = 0;
82 static nl_catd  DT_catd;
83 static char cm_target[256] = "";        /* target for table (user@host) */
84 static int cm_today = 0;                /* today's date (in epoch time) */
85 static char cm_date[256] = "";          /* appointment date */
86 static char cm_view[16] = "day";        /* view span (day,week,month) */
87 static char cm_start[16] = "";          /* start time for appointment list*/
88 static char cm_end[16] = "";            /* end time for appointment list */
89 static char cm_repeatstr[256] = "One Time";/* repeat period */
90 static char cm_for[256] = "";           /* number of repeats */
91 static char cm_what[1024] = "Appointment";
92                                         /* what appointment is about */
93 static char cm_appt_file[1024] = "";
94                                         /* file to find an appointment template in */
95 static int cm_repeat = 0;               /* how often to repeat this */
96 static int cm_flash = 0;                /* flash reminder */
97 static int cm_beep = 0;                 /* beep reminder */
98 static int cm_open = 0;                 /* open reminder */
99 static int cm_mail = 0;                 /* mail reminder */
100
101 static void cm_args();                  /* parse command line */
102
103 static char**
104 grab(char **argv,                               /* command line arguments */
105     char *buf,                          /* buffer for keyed data */
106     char stop_key)
107 {
108         if (!argv || !*argv) return(argv);
109         cm_strcpy (buf,*argv++);
110         while(argv && *argv) {
111                 if (*(*argv) == stop_key) break;
112                 cm_strcat(buf," ");
113                 cm_strcat(buf,*argv++);
114         }
115         argv--;
116         return(argv);
117 }
118
119 static void
120 cm_args(int argc, char **argv)
121 {
122
123         while (++argv && *argv) {
124                 switch(*(*argv+1)) {
125                 case 't':
126                 case 'c':
127                         argv = grab(++argv,cm_target,'-');
128                         break;
129                 case 'd':
130                         argv = grab(++argv,cm_date,'-');
131                         break;
132                 case 'v':
133                         argv = grab(++argv,cm_view,'-');
134                         break;
135                 case 's':
136                         argv = grab(++argv,cm_start,'-');
137                         break;
138                 case 'e':
139                         argv = grab(++argv,cm_end,'-');
140                         break;
141                 case 'w':
142                         argv = grab(++argv,cm_what,'-');
143                         break;
144                 case 'a':
145                         argv = grab(++argv,cm_appt_file,'-');
146                         break;
147                 default:
148                         fprintf(stderr, "%s", catgets(DT_catd, 1, 191, "Usage:\n\tdtcm_insert [ -c calendar ] [-d <mm/dd/yy>] [ -v view ]\n"));
149                         fprintf(stderr, "%s", catgets(DT_catd, 1, 192, "                   [-w what string] [-s <HH:MMam/pm>] [-e <HH:MMam/pm>]\n"));
150                         exit(1);
151                 }
152         }
153 }
154
155 static void
156 prompt_for_line(char *prompt, char *defval, char *buffer)
157 {
158         char input_buf[1024];
159
160         printf(prompt, defval);
161
162         *input_buf = '\0';
163         fgets (input_buf, sizeof(input_buf), stdin);
164         if (strlen(input_buf) && input_buf[strlen(input_buf)-1] == '\n')
165           input_buf[strlen(input_buf)-1] = '\0';
166
167         if (input_buf[0] && (input_buf[0] != '\n'))
168                 cm_strcpy(buffer, input_buf);
169         else
170                 cm_strcpy(buffer, defval);
171 }
172
173 static void
174 prompt_for_insert(Props *p) {
175         char            date_str[BUFSIZ], what_buffer[BUFSIZ], buf[BUFSIZ], *timecopy;
176         int             index, next, valid = FALSE;
177         DisplayType     dt = get_int_prop(p, CP_DEFAULTDISP);
178
179         format_tick(now(), get_int_prop(p, CP_DATEORDERING),
180                     get_int_prop(p, CP_DATESEPARATOR), date_str);
181
182         printf("%s", catgets(DT_catd, 1, 193, "Please enter the information for the appointment you wish to add.\nDefaults will be shown in parentheses.\n"));
183         prompt_for_line(catgets(DT_catd, 1, 194, 
184                 "Calendar (%s): "), cm_get_credentials(), cm_target);
185         prompt_for_line(catgets(DT_catd, 1, 195, 
186                 "Date (%s): "), date_str, cm_date);
187         while (valid != TRUE)
188         {
189                 format_time(now(), dt, cm_start);
190                 prompt_for_line(catgets(DT_catd, 1, 196, 
191                         "Start (%s): "), cm_start, cm_start);
192                 if (cm_start && cm_start[0])
193                 {
194                         timecopy = (char *)cm_strdup(cm_start);
195                         if (valid_time(p, timecopy))
196                                 valid = TRUE;
197                         else
198                                 printf("%s", catgets(DT_catd, 1, 197, "You have entered an invalid time.  Please try again:\n"));
199                         free(timecopy);
200                 }
201         }
202
203         sprintf(buf, "%s %s", date_str, cm_start);
204         next = (int) cm_getdate(buf, NULL);
205         next = next + hrsec;
206
207         format_time(next, dt, cm_end);
208         if (cm_start && cm_start[0])
209                 prompt_for_line(
210                         catgets(DT_catd, 1, 198, "End (%s): "), cm_end, cm_end);
211         else
212                 prompt_for_line(
213                         catgets(DT_catd, 1, 199, "End (%s): "), "None", cm_end);
214
215         strcpy(cm_repeatstr, catgets(DT_catd, 1, 200, "One Time"));
216
217         prompt_for_line(catgets(DT_catd, 1, 201, 
218                         "Repeat (%s): "), cm_repeatstr, cm_repeatstr);
219
220         if (strcmp(cm_repeatstr, catgets(DT_catd, 1, 200, "One Time"))) {
221                 sprintf(buf, "%s", catgets(DT_catd, 1, 203, "no default"));
222                 prompt_for_line(
223                         catgets(DT_catd, 1, 204, "For (%s): "), buf, cm_for);
224         }
225
226         printf("%s", catgets(DT_catd, 1, 205, 
227                 "What (you may enter up to 5 lines, use ^D to finish):\n"));
228         cm_what[0] = '\0';
229         for (index = 0; index < 5; index++)
230         {
231                 *what_buffer = '\0';
232                 fgets (what_buffer, sizeof(what_buffer), stdin);
233                 if (strlen(what_buffer) &&
234                     what_buffer[strlen(what_buffer)-1] == '\n')
235                   what_buffer[strlen(what_buffer)-1] = '\0';
236
237                 if (what_buffer[0] == '\000')
238                         break;
239                 else
240                 {
241                         strcat(cm_what, what_buffer);
242                         strcat(cm_what, "\\n");
243                 }
244                 memset(what_buffer, '\000', 256);
245         }
246         
247 }
248
249 void
250 main(int argc, char **argv)
251 {
252         int             cnt, status = 0;
253         char            *date = NULL, *view = NULL, *target = NULL,
254                         *start = NULL, *end = NULL, *repeat = NULL,
255                         *numrepeat = NULL, *what = NULL, *uname, *loc;
256         Props           *p = NULL;
257         CSA_entry_handle        *list;
258         CSA_session_handle      c_handle;
259         CSA_return_code         stat;
260         CSA_calendar_user       csa_user;
261         DisplayType             dt;
262         int                     version;
263         char                    date_str[256];
264 #ifdef FNS
265         char            buf[256];
266 #endif
267
268         init_time();
269         _DtEnvControl(DT_ENV_SET); /* set up environment variables */
270         setlocale(LC_ALL, "");
271         DT_catd = catopen(DTCM_CAT, NL_CAT_LOCALE);
272         cm_tty_load_props(&p);
273         dt = get_int_prop(p, CP_DEFAULTDISP);
274 #ifdef FNS
275         dtfns_init();
276 #endif
277
278         if (argc > 1)
279         {
280                 cm_args(argc,argv);             /* parse command line */
281                 if (cm_strlen(cm_target)) 
282                         target = cm_target;
283                 else
284                         target = cm_get_credentials();
285 #ifdef FNS
286                 if (cmfns_use_fns(p)) {
287                         cmfns_lookup_calendar(target, buf, sizeof(buf));
288                         target = buf;
289                 }
290 #endif
291                 uname = cm_target2name(target);
292                 loc = cm_target2location(target);
293
294                 csa_user.user_name = target;
295                 csa_user.user_type = 0;
296                 csa_user.calendar_user_extensions = NULL;
297                 csa_user.calendar_address = target;
298                 stat = csa_logon(NULL, &csa_user, NULL, NULL, NULL, &c_handle, NULL);
299                 if (stat != CSA_SUCCESS) {
300                         char *format = cm_strdup(catgets(DT_catd, 1, 206, 
301                                            "\nCould not open calendar %s\n"));
302                         fprintf(stderr, format,
303                                 target ? target : 
304                                 catgets(DT_catd, 1, 209, "UNKNOWN"));
305                         free(format);
306                         free(uname);
307                         free(loc);
308                         exit(1);
309                 }
310                 version = get_data_version(c_handle);
311                 if (!cm_date[0])
312                         format_tick(now(), get_int_prop(p, CP_DATEORDERING),
313                                     get_int_prop(p, CP_DATESEPARATOR), cm_date);
314                 if (cm_strlen(cm_date)) date = cm_date;
315                 if (cm_strlen(cm_view)) view = cm_view;
316                 if (cm_strlen(cm_start)) start = cm_start;
317
318                 if (!cm_end[0] && cm_start[0]) {
319                         format_time((int)cm_getdate(cm_start, NULL) + hrsec,
320                                     dt, cm_end);
321                 }
322
323                 if (cm_strlen(cm_end)) end = cm_end;
324                 if (cm_strlen(cm_repeatstr)) repeat = cm_repeatstr;
325                 if (cm_strlen(cm_for)) numrepeat = cm_for;
326                 if (cm_strlen(cm_what)) what = cm_what;
327                 if (!cm_appt_file[0])
328                         status = cm_tty_insert(DT_catd, c_handle, version, 
329                                       date, start, end, repeat, numrepeat,
330                                       what, NULL, p);
331                 else
332                         status = cm_tty_insert(DT_catd, c_handle, version, date,
333                                       start, end, repeat, numrepeat,
334                                       what, cm_appt_file, p);
335         } else {
336                 prompt_for_insert(p);
337                 if (cm_strlen(cm_target)) target = cm_target;
338                 uname = cm_target2name(target);
339                 loc = cm_target2location(target);
340
341                 csa_user.user_name = target;
342                 csa_user.user_type = 0;
343                 csa_user.calendar_user_extensions = NULL;
344                 csa_user.calendar_address = target;
345                 stat = csa_logon(NULL, &csa_user, NULL, NULL, NULL, &c_handle, NULL);
346                 if (stat !=CSA_SUCCESS) {
347                         char *format = cm_strdup(catgets(DT_catd, 1, 206, 
348                                            "\nCould not open calendar %s\n"));
349                         fprintf(stderr, format, 
350                                 target ? target : 
351                                 catgets(DT_catd, 1, 209, "UNKNOWN"));
352                         free(format);
353                         free(uname);
354                         free(loc);
355                         exit(1);
356                 }
357                 version = get_data_version(c_handle);
358                 if (cm_strlen(cm_date)) date = cm_date;
359                 if (cm_strlen(cm_view)) view = cm_view;
360                 if (cm_strlen(cm_start)) start = cm_start;
361                 if (cm_strlen(cm_end)) end = cm_end;
362                 if (cm_strlen(cm_repeatstr)) repeat = cm_repeatstr;
363                 if (cm_strlen(cm_for)) numrepeat = cm_for;
364                 if (cm_strlen(cm_what)) what = cm_what;
365                 status = cm_tty_insert(DT_catd, c_handle, version, date, 
366                               start, end, repeat, numrepeat, what, NULL, p);
367         }
368         if ((cnt = cm_tty_lookup(DT_catd, c_handle, version, date, view, 
369                                         &list, p)) > 0)
370                 csa_free(list);
371         csa_logoff(c_handle, NULL);
372         props_clean_up(p);
373         free(p);
374         free(uname);
375         free(loc);
376         exit(status);
377 }