home *** CD-ROM | disk | FTP | other *** search
- /*
- * $Header: init.c,v 2.1 89/05/09 14:23:19 billr Exp $
- */
- /*
- * init.c
- *
- * Author: Philip Heller, Sun Microsystems. Inc. <terrapin!heller@sun.com>
- *
- * Original source Copyright (C) 1987, Sun Microsystems, Inc.
- * All Rights Reserved
- * Permission is hereby granted to use and modify this program in source
- * or binary form as long as it is not sold for profit and this copyright
- * notice remains intact.
- *
- *
- * Changes/additions by: Bill Randle, Tektronix, Inc. <billr@saab.CNA.TEK.COM>
- *
- * Changes and additions Copyright (C) 1988, 1989 Tektronix, Inc.
- * All Rights Reserved
- * Permission is hereby granted to use and modify the modifications in source
- * or binary form as long as they are not sold for profit and this copyright
- * notice remains intact.
- */
-
- /***************************************************
- * *
- * Initialization routines, for memory pixrects *
- * and files. *
- * *
- ***************************************************/
-
-
- #include <stdio.h>
- #include <suntool/sunview.h>
- #include <suntool/menu.h>
- #include <sunwindow/defaults.h>
- #include <sys/file.h>
- #include <pwd.h>
- #include "ct.h"
-
-
- extern int dayslot_width, dayslot_height;
- extern int weekslot_width, weekslot_height;
- extern int ybox_width, ybox_height;
- extern Pixrect *daybox_pr, *timeslot_pr, *gray_pr;
- extern Pixrect *weekslot_pr, *weekarrow_pr, *smallarrow_pr;
- extern Pixrect *arrowshaft_pr, *arrowhead_pr;
- extern Pixrect *weekarrowshaft_pr, *weekarrowhead_pr;
- extern Pixrect *triangle_pr;
- extern Pixrect *ydaybox_pr, *ymonthbox_pr;
- extern Frame frame;
- extern Frame prompt_frame;
- extern int otherfile, mailto, one_based, read_only, version2;
- extern char *othername;
- extern char version[];
- Pixrect *daybox_td_pr, *weekslot_td_pr, *weekarrowshaft_td_pr;
- Pixrect *weekarrowhead_td_pr, *ydaybox_td_pr, *timeslot_td_pr;
- Pixrect *leftarrow, *rightarrow;
- char t_title[160];
- char apts_pathname[160], tmpapts_pathname[160];
- char apts_dir[128], lib_dir[128];
- char printer[64];
-
- struct passwd *getpwnam();
-
- init_pixrects()
- {
- init_daybox_pr();
- init_timeslot_pr();
- init_weekslot_pr();
- init_weekarrow_pr();
- init_arrow_prs();
- init_triangle_pr();
- init_ydaybox_pr();
- init_ymonthbox_pr();
- }
-
- /* Concentric squares for drawing days of month calendar. */
- init_daybox_pr()
- {
- Pixrect *pattern;
- int i;
-
- daybox_pr = mem_create(64,64,1);
- pr_vector(daybox_pr,0,0,63,0,PIX_SET,1);
- pr_vector(daybox_pr,63,0,63,63,PIX_SET,1);
- pr_vector(daybox_pr,63,63,0,63,PIX_SET,1);
- pr_vector(daybox_pr,0,63,0,0,PIX_SET,1);
- pr_vector(daybox_pr,2,2,61,2,PIX_SET,1);
- pr_vector(daybox_pr,61,2,61,61,PIX_SET,1);
- pr_vector(daybox_pr,61,61,2,61,PIX_SET,1);
- pr_vector(daybox_pr,2,61,2,2,PIX_SET,1);
- pattern = mem_create(64,2,1);
- for (i=3; i<60; i+=8)
- pr_put(pattern, i, 0, 1);
- for (i=7; i<60; i+=8)
- pr_put(pattern, i, 1, 1);
- daybox_td_pr = mem_create(64,64,1);
- pr_vector(daybox_td_pr,0,0,63,0,PIX_SET,1);
- pr_vector(daybox_td_pr,63,0,63,63,PIX_SET,1);
- pr_vector(daybox_td_pr,63,63,0,63,PIX_SET,1);
- pr_vector(daybox_td_pr,0,63,0,0,PIX_SET,1);
- pr_vector(daybox_td_pr,2,2,61,2,PIX_SET,1);
- pr_vector(daybox_td_pr,61,2,61,61,PIX_SET,1);
- pr_vector(daybox_td_pr,61,61,2,61,PIX_SET,1);
- pr_vector(daybox_td_pr,2,61,2,2,PIX_SET,1);
- pr_replrop(daybox_td_pr, 0, 2, 60, 60, PIX_SRC|PIX_DST, pattern, 0, 0);
- pr_destroy(pattern);
-
- }
-
- /* Box for 30-minute slot in picture of day. */
- init_timeslot_pr()
- {
- int i;
- Pixrect *pattern;
-
- timeslot_pr = mem_create(dayslot_width, dayslot_height, 1);
- timeslot_td_pr = mem_create(dayslot_width, dayslot_height, 1);
- pattern = mem_create(dayslot_width, 2, 1);
- for (i=1; i<dayslot_width; i+=8)
- pr_put(pattern, i, 0, 1);
- for (i=5; i<dayslot_width; i+=8)
- pr_put(pattern, i, 1, 1);
- pr_vector(timeslot_pr,0,0,0,dayslot_height-1,PIX_SET,1);
- pr_vector(timeslot_pr,dayslot_width-1,0,
- dayslot_width-1,dayslot_height-1,PIX_SET,1);
- pr_vector(timeslot_td_pr,0,0,0,dayslot_height-1,PIX_SET,1);
- pr_vector(timeslot_td_pr,dayslot_width-1,0,
- dayslot_width-1,dayslot_height-1,PIX_SET,1);
- for (i=0; i<dayslot_width-1; i=i+2) {
- pr_put(timeslot_pr, i, 0, 1);
- pr_put(timeslot_pr, i, dayslot_height-1, 1);
- pr_put(timeslot_td_pr, i, 0, 1);
- pr_put(timeslot_td_pr, i, dayslot_height-1, 1);
- }
- pr_replrop(timeslot_td_pr, 1, 2, dayslot_width-1, dayslot_height-3, PIX_SRC|PIX_DST, pattern, 0, 0);
- pr_destroy(pattern);
- }
-
-
- /* Box for 30-minute slot in picture of week. */
- init_weekslot_pr()
- {
- int i;
- Pixrect *pattern;
-
- weekslot_pr = mem_create(weekslot_width,weekslot_height,1);
- weekslot_td_pr = mem_create(weekslot_width,weekslot_height,1);
- pattern = mem_create(weekslot_width, 2, 1);
- for (i=1; i<weekslot_width; i+=8)
- pr_put(pattern, i, 0, 1);
- for (i=5; i<weekslot_width; i+=8)
- pr_put(pattern, i, 1, 1);
- pr_vector(weekslot_pr,0,0,0,weekslot_width-1,PIX_SET,1);
- pr_vector(weekslot_pr,weekslot_width-1,0,weekslot_width-1,weekslot_height-1,PIX_SET,1);
- pr_vector(weekslot_td_pr,0,0,0,weekslot_height-1,PIX_SET,1);
- pr_vector(weekslot_td_pr,weekslot_width-1,0,weekslot_width-1,weekslot_height-1,PIX_SET,1);
- for (i=0; i<weekslot_width-1; i=i+2) {
- pr_put(weekslot_pr,i,0,1);
- pr_put(weekslot_pr,i,weekslot_height-1,1);
- pr_put(weekslot_td_pr,i,0,1);
- pr_put(weekslot_td_pr,i,weekslot_height-1,1);
- }
- pr_replrop(weekslot_td_pr, 1, 2, weekslot_width-1, weekslot_height-3, PIX_SRC|PIX_DST, pattern, 0, 0);
- pr_destroy(pattern);
- }
-
- /* Arrow pointing to a week in picture of month. */
- init_weekarrow_pr()
- {
- int i;
-
- weekarrow_pr = mem_create(42,29,1);
- pr_vector(weekarrow_pr,0,0,27,0,PIX_SET,1);
- pr_vector(weekarrow_pr,27,0,41,14,PIX_SET,1);
- pr_vector(weekarrow_pr,41,14,27,28,PIX_SET,1);
- pr_vector(weekarrow_pr,27,28,0,28,PIX_SET,1);
- pr_vector(weekarrow_pr,0,28,0,0,PIX_SET,1);
- pr_vector(weekarrow_pr,2,2,26,2,PIX_SET,1);
- pr_vector(weekarrow_pr,26,2,38,14,PIX_SET,1);
- pr_vector(weekarrow_pr,38,14,26,26,PIX_SET,1);
- pr_vector(weekarrow_pr,26,26,2,26,PIX_SET,1);
- pr_vector(weekarrow_pr,2,26,2,2,PIX_SET,1);
- smallarrow_pr = mem_create(42,29,1);
- pr_rop(smallarrow_pr, 3, 3, 25, 23, PIX_SET, 0, 0, 0);
- for (i=0; i<14; i++) {
- pr_vector(smallarrow_pr, 27, i+3, 27+i, i+3, PIX_SET,1);
- pr_vector(smallarrow_pr, 27, 25-i, 27+i, 25-i, PIX_SET,1);
- }
- }
-
-
- init_arrow_prs()
- {
- int lmid, rmid;
-
- arrowshaft_pr = mem_create(dayslot_width-2, dayslot_height-2, 1);
- arrowhead_pr = mem_create(dayslot_width-2, dayslot_height-2, 1);
- weekarrowshaft_pr = mem_create(weekslot_width-2, weekslot_height-2, 1);
- weekarrowhead_pr = mem_create(weekslot_width-2, weekslot_height-2, 1);
-
- lmid = (dayslot_width-2) / 2;
- rmid = lmid + 1;
- pr_vector(arrowshaft_pr, lmid-4, 0, lmid-4, dayslot_height-3, PIX_SET, 1);
- pr_vector(arrowshaft_pr, lmid-3, 0, lmid-3, dayslot_height-3, PIX_SET, 1);
- pr_vector(arrowshaft_pr, rmid+3, 0, rmid+3, dayslot_height-3, PIX_SET, 1);
- pr_vector(arrowshaft_pr, rmid+4, 0, rmid+4, dayslot_height-3, PIX_SET, 1);
-
- pr_vector(arrowhead_pr, lmid, dayslot_height-3, lmid-7, dayslot_height-10,
- PIX_SET, 1);
- pr_vector(arrowhead_pr, lmid, dayslot_height-4, lmid-7, dayslot_height-11,
- PIX_SET, 1);
- pr_vector(arrowhead_pr, rmid, dayslot_height-3, rmid+7, dayslot_height-10,
- PIX_SET, 1);
- pr_vector(arrowhead_pr, rmid, dayslot_height-4, rmid+7, dayslot_height-11,
- PIX_SET, 1);
- pr_vector(arrowhead_pr, lmid-4, dayslot_height-8, lmid-4, 0, PIX_SET, 1);
- pr_vector(arrowhead_pr, lmid-3, dayslot_height-7, lmid-3, 0, PIX_SET, 1);
- pr_vector(arrowhead_pr, rmid+3, dayslot_height-7, rmid+3, 0, PIX_SET, 1);
- pr_vector(arrowhead_pr, rmid+4, dayslot_height-8, rmid+4, 0, PIX_SET, 1);
-
- lmid = (weekslot_width-2) / 2;
- rmid = lmid + 1;
-
- pr_vector(weekarrowshaft_pr, lmid-4, 0, lmid-4, weekslot_height-3, PIX_SET, 1);
- pr_vector(weekarrowshaft_pr, lmid-3, 0, lmid-3, weekslot_height-3, PIX_SET, 1);
- pr_vector(weekarrowshaft_pr, rmid+3, 0, rmid+3, weekslot_height-3, PIX_SET, 1);
- pr_vector(weekarrowshaft_pr, rmid+4, 0, rmid+4, weekslot_height-3, PIX_SET, 1);
-
- pr_vector(weekarrowhead_pr, lmid, weekslot_height-3, lmid-7, weekslot_height-10,
- PIX_SET, 1);
- pr_vector(weekarrowhead_pr, lmid, weekslot_height-4, lmid-7, weekslot_height-11,
- PIX_SET, 1);
- pr_vector(weekarrowhead_pr, rmid, weekslot_height-3, rmid+7, weekslot_height-10,
- PIX_SET, 1);
- pr_vector(weekarrowhead_pr, rmid, weekslot_height-4, rmid+7, weekslot_height-11,
- PIX_SET, 1);
- pr_vector(weekarrowhead_pr, lmid-4, weekslot_height-8, lmid-4, 0, PIX_SET, 1);
- pr_vector(weekarrowhead_pr, lmid-3, weekslot_height-7, lmid-3, 0, PIX_SET, 1);
- pr_vector(weekarrowhead_pr, rmid+3, weekslot_height-7, rmid+3, 0, PIX_SET, 1);
- pr_vector(weekarrowhead_pr, rmid+4, weekslot_height-8, rmid+4, 0, PIX_SET, 1);
-
- leftarrow = mem_create(19, 14, 1);
- rightarrow = mem_create(19, 14, 1);
- pr_vector(leftarrow, 3, 5, 16, 5, PIX_SET, 1);
- pr_vector(leftarrow, 2, 6, 16, 6, PIX_SET, 1);
- pr_vector(leftarrow, 3, 7, 16, 7, PIX_SET, 1);
- pr_vector(leftarrow, 4, 4, 4, 8, PIX_SET, 1);
- pr_vector(leftarrow, 5, 3, 5, 9, PIX_SET, 1);
- pr_vector(leftarrow, 6, 2, 6, 10, PIX_SET, 1);
- pr_vector(leftarrow, 0, 0, 18, 0, PIX_SET, 1);
- pr_vector(leftarrow, 18, 0, 18, 13, PIX_SET, 1);
- pr_vector(leftarrow, 18, 13, 0, 13, PIX_SET, 1);
- pr_vector(leftarrow, 0, 13, 0, 0, PIX_SET, 1);
-
- pr_vector(rightarrow, 2, 5, 15, 5, PIX_SET, 1);
- pr_vector(rightarrow, 2, 6, 16, 6, PIX_SET, 1);
- pr_vector(rightarrow, 2, 7, 15, 7, PIX_SET, 1);
- pr_vector(rightarrow, 14, 4, 14, 8, PIX_SET, 1);
- pr_vector(rightarrow, 13, 3, 13, 9, PIX_SET, 1);
- pr_vector(rightarrow, 12, 2, 12, 10, PIX_SET, 1);
- pr_vector(rightarrow, 0, 0, 18, 0, PIX_SET, 1);
- pr_vector(rightarrow, 18, 0, 18, 13, PIX_SET, 1);
- pr_vector(rightarrow, 18, 13, 0, 13, PIX_SET, 1);
- pr_vector(rightarrow, 0, 13, 0, 0, PIX_SET, 1);
- }
-
-
- init_triangle_pr()
- {
- int i;
-
- triangle_pr = mem_create(32, 32, 1);
- for (i=0; i<32; i++)
- pr_vector(triangle_pr, i, i, 31, i, PIX_SET, 1);
- }
-
- /* squares for drawing days of month on the year calendar */
- init_ydaybox_pr()
- {
- Pixrect *pattern;
- int i;
-
- ydaybox_pr = mem_create(ybox_width,ybox_height,1);
- pr_vector(ydaybox_pr,0,0,ybox_width-1,0,PIX_SET,1);
- pr_vector(ydaybox_pr,ybox_width-1,0,ybox_width-1,ybox_height-1,PIX_SET,1);
- pr_vector(ydaybox_pr,ybox_width-1,ybox_height-1,0,ybox_height-1,PIX_SET,1);
- pr_vector(ydaybox_pr,0,ybox_height-1,0,0,PIX_SET,1);
- pattern = mem_create(ybox_width,2,1);
- for (i=1; i<ybox_width; i+=4)
- pr_put(pattern, i, 0, 1);
- for (i=3; i<ybox_width; i+=4)
- pr_put(pattern, i, 1, 1);
- ydaybox_td_pr = mem_create(ybox_width,ybox_height,1);
- pr_vector(ydaybox_td_pr,0,0,ybox_width-1,0,PIX_SET,1);
- pr_vector(ydaybox_td_pr,ybox_width-1,0,ybox_width-1,ybox_height-1,PIX_SET,1);
- pr_vector(ydaybox_td_pr,ybox_width-1,ybox_height-1,0,ybox_height-1,PIX_SET,1);
- pr_vector(ydaybox_td_pr,0,ybox_height-1,0,0,PIX_SET,1);
- pr_replrop(ydaybox_td_pr, 1, 1, ybox_width-3, ybox_height-2, PIX_SRC|PIX_DST, pattern, 0, 0);
- pr_destroy(pattern);
- }
-
- /* label box for each month on the year calendar */
- init_ymonthbox_pr()
- {
- int ymbox_width;
-
- ymbox_width = 7 * ybox_width;
- ymonthbox_pr = mem_create(ymbox_width,ybox_height,1);
- pr_vector(ymonthbox_pr,0,0,ymbox_width-1,0,PIX_SET,1);
- pr_vector(ymonthbox_pr,ymbox_width-1,0,ymbox_width-1,ybox_height-1,PIX_SET,1);
- pr_vector(ymonthbox_pr,ymbox_width-1,ybox_height-1,0,ybox_height-1,PIX_SET,1);
- pr_vector(ymonthbox_pr,0,ybox_height-1,0,0,PIX_SET,1);
- pr_vector(ymonthbox_pr,0,1,ymbox_width-1,1,PIX_SET,1);
- }
-
- do_files(window_prompt)
- int window_prompt;
- {
- char *strcpy(), *rindex(), *getenv();
- char *slash, *default_ptr, *envptr;
- char buff[80];
- int to_slash, getpid(), fd, errflag;
- struct passwd *pw;
- FILE *appts;
-
- /* the tmp file */
- sprintf(tmpapts_pathname, "/tmp/appts%d", getpid());
- if (otherfile) {
- strcpy(apts_pathname, othername);
- if ((slash = rindex(apts_pathname, '/')) != NULL) {
- to_slash = slash - apts_pathname;
- strncpy(apts_dir, apts_pathname, to_slash);
- apts_dir[to_slash] = '\0';
- } else {
- strcpy(apts_dir, ".");
- }
- } else {
- if ((default_ptr = defaults_get_string("/CalenTool/Appts", NULL, 0)) != NULL) {
- if ((slash = rindex(default_ptr, '/')) != NULL) {
- to_slash = slash - default_ptr;
- strncpy(apts_dir, default_ptr, to_slash);
- apts_dir[to_slash] = '\0';
- } else {
- strcpy(apts_dir, ".");
- }
- } else if ((envptr = getenv("CALENTOOL_DIR")) != NULL) {
- strcpy(apts_dir, envptr);
- } else if (mailto) {
- if ((pw = getpwnam(mailto)) == NULL)
- /* no entry */
- exit(1);
- envptr = pw->pw_dir; /* home directory */
- strcpy(apts_dir, envptr);
- } else if ((envptr = getenv("HOME")) != NULL) {
- strcpy(apts_dir, envptr);
- } else {
- apts_dir[0] = '\0';
- }
- if (*apts_dir) {
- /* prepend directory on pathnames */
- sprintf(apts_pathname, "%s/.appointments", apts_dir);
- } else {
- /* use current directory */
- strcpy(apts_pathname, ".appointments");
- }
- }
-
- /* directory for date/event data files */
- if ((default_ptr = defaults_get_string("/CalenTool/LibDir", NULL, 0)) != NULL)
- strcpy(lib_dir, default_ptr);
- else
- strcpy(lib_dir, DATELIB_DIR);
-
- errflag = 0;
- if (access(apts_pathname, R_OK) == -1) {
- if (window_prompt) {
- fprintf(stderr, "nonexistant file\n");
- sprintf(buff, "Cannot access calendar file %s - create?", apts_pathname);
- create_prompt_frame(buff, TRUE);
- if ((int)window_loop(prompt_frame) == 0) {
- /* try to create the file */
- fprintf(stderr, "creating file\n");
- if ((fd=open(apts_pathname, O_CREAT|O_RDWR, 0664)) <= 0) {
- perror(apts_pathname);
- errflag = 1;
- fprintf(stderr, "..error\n");
- } else {
- if (write(fd, HEADER, sizeof(HEADER)) != sizeof(HEADER)) {
- perror("writing header");
- errflag = 1;
- }
- close(fd);
- fprintf(stderr, "..wrote header\n");
- one_based = 1;
- }
- } else {
- fprintf(stderr, "window_ret != 0\n");
- errflag = 1;
- }
- window_set(prompt_frame, WIN_SHOW, FALSE, 0);
- if (errflag)
- return(1);
- } else {
- fprintf(stderr, "Cannot access calendar file %s - create? ", apts_pathname);
- fgets(buff, 80, stdin);
- if (buff[0] == 'y' || buff[0] == 'Y') {
- if ((fd=open(apts_pathname, O_CREAT|O_RDWR, 0664)) <= 0) {
- perror(apts_pathname);
- return(1);
- } else {
- if (write(fd, HEADER, sizeof(HEADER)) != sizeof(HEADER)) {
- perror("writing header");
- close(fd);
- return(1);
- }
- close(fd);
- one_based = 1;
- }
- } else
- return(1);
- }
- }
- if (access(apts_pathname, W_OK) == -1)
- read_only = 1;
-
- /* update base frame label, if the tool is running */
- if (frame) {
- strcpy(t_title, version);
- if (read_only)
- strcat(t_title, " [Read Only]");
- strcat(t_title, " - ");
- strcat(t_title, apts_pathname);
- window_set(frame, FRAME_LABEL, t_title, 0);
- }
-
- /* check first line of appts file to see if it is the new style */
- if ((appts = fopen(apts_pathname, "r")) != NULL) {
- fgets(buff, 80, appts);
- if (!strcmp(buff, HEADER)) {
- version2 = 1;
- one_based = 1;
- }
- fclose(appts);
- }
-
- /* Convert from old format to new. There may some appts files
- * that are one-based, but are still old style. These are
- * also handled. (Note: for old-style one-based appts files
- * the user MUST start calentool with the -z flag.)
- */
- if (!version2)
- ver1to2();
-
- return(0);
- }
-
- /*
- * get/set printer variable to correspond to the command to print
- * a raster or Postscript file
- */
- get_printer()
- {
- #ifndef NO_PRINTER
- char *ptr, *getenv();
-
- strcpy(printer, "lpr -P");
- if ((ptr = defaults_get_string("/CalenTool/Printer", NULL, 0)) != NULL)
- strcat(printer, ptr);
- else if ((ptr = getenv("PRINTER")) != NULL)
- strcat(printer, ptr);
- else
- strcpy(printer, PRINT_CMD);
- #endif
- }
-