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