File: maininit.c

package info (click to toggle)
saoimage 1.19-5
  • links: PTS
  • area: main
  • in suites: slink
  • size: 3,256 kB
  • ctags: 3,610
  • sloc: ansic: 36,050; makefile: 215; sh: 11
file content (298 lines) | stat: -rw-r--r-- 9,816 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#ifndef lint
static char SccsId[] = "%W%  %G%";
#endif

/* Module:	maininit.c (Main Initialize)
 * Purpose:	Start of the saoimage package
 * Program:	main()
 * Subroutine:	crash_on_error()
 * Xlib calls:	XSetErrorHandler(), XCloseDisplay(), XGetErrorText()
 * UNIX calls:	setrlimit(), ieee_handler(), abrupt_underflow_()
 * Copyright:	1989,1990,1993-1996 Smithsonian Astrophysical Observatory
 *		You may do anything you like with this file except remove
 *		this copyright.  The Smithsonian Astrophysical Observatory
 *		makes no representations about the suitability of this
 *		software for any purpose.  It is provided "as is" without
 *		express or implied warranty.
 * Modified:	{0} Michael VanHilst	initial version		   9 May 1989
 *              {1} MVH BSDonly strings.h compatability           19 Feb 1990
 *              {2} Doug Mink	version 1.08: autoscale           10 Nov 1993
 *              {3} Doug Mink	version 1.09: Add WCS tracking    19 Oct 1994
 *              {4} Doug Mink	version 1.10: Add CD to WCS       22 Dec 1994
 *              {5} Doug Mink	version 1.11: Handle narrow files 20 Jan 1995
 *              {6} Doug Mink	version 1.12: Add DSS plate sol.   1 Mar 1995
 *              {7} Doug Mink	version 1.13: Fix bugs from STScI  5 May 1995
 *              {7} Doug Mink	version 1.13: Fix DSS plate sol.   5 May 1995
 *              {8} Doug Mink	version 1.14: Add coordinate conv.16 Aug 1995
 *              {9} Doug Mink	version 1.15: Bug fixes           18 Oct 1995
 *             {10} Doug Mink	version 1.16: DSS WCS Bug fix      7 Nov 1995
 *             {11} Doug Mink	version 1.17: IRAF .imh WCS       14 Dec 1995
 *             {11} Doug Mink	version 1.17: SECPIX WCS          22 Dec 1995
 *             {11} Doug Mink	version 1.17: Initial zoom factor  2 Jan 1996
 *             {11} Doug Mink	version 1.17: Fix pixel tracking   2 Feb 1996
 *             {12} Doug Mink	version 1.18: Fix IRAF header      9 Feb 1996
 *             {13} Doug Mink	version 1.18: Fix various bugs    26 Feb 1996
 *             {14} Doug Mink	version 1.19: Improved WCS         4 Sep 1996
 *		{n} <who> -- <does what> -- <when>
 */

#include <stdio.h>		/* stderr, FILE, NULL, etc. */

#ifndef VMS
#ifdef SYSV
#include <string.h>		/* strlen, strcat, strcpy, strrchr */
#else
#include <strings.h>		/* strlen, etc. for unenlightened BSD's */
#endif
#else
#include <string.h>		/* strlen, strcat, strcpy, strrchr */
#endif

#include <sys/time.h>		/* struct timeval used in <sys/resource.h> */
#include <X11/Xlib.h>		/* X window stuff */
#include <X11/Xutil.h>		/* X window manager stuff */
#include "hfiles/constant.h"	/* define codes */
#include "hfiles/define.h"	/* YES, NO, MIN, MAX and more */
#include "hfiles/struct.h"	/* declare structure types */
#ifdef DEBUG
#ifdef SUN			/* DEBUG and SUN(OS4.xx) */
#include <floatingpoint.h>	/* SIGFPE_ABORT ieee exception switch */
#endif
#else
#ifndef VMS			/* NOT DEBUG AND NOT VMS */
#ifndef SYSV
#include <sys/resource.h>	/* rlimit, RLIMIT_CORE for setrlimit */
#endif
#endif
#endif

char *RevMsg = "release 1.19 4 September 1996";
int init_done = 0;

/* declare and initialize parameter structs */
#include "defs/control.def"
#include "defs/color.def"
#include "defs/image.def"
#include "defs/dispbox.def"
#include "defs/magnibox.def"
#include "defs/colorbox.def"
#include "defs/graphbox.def"
#include "defs/panbox.def"
#include "defs/btnbox.def"
#include "defs/desktop.def"
#include "defs/cursor.def"

/* declare uninitialized structs */
struct WorldCoor *wcs;		/* WCS data structure */
struct bufferRec buffer;
struct coordRec coord;
Display *display;		/* display connection */
char wcscoor[16];		/* Output coordinate system */
char wcscommand[64];		/* WCS command */

/*
 * Program:	main
 * Purpose:	Get things started, then call event_loop
 * Xlib calls:	XSetErrorHandler()
 * UNIX calls:	setrlimit()
 */
main(argc, argv)
  int argc;
  char **argv;
{
  static void init_params(), init_packages();
  void crash_on_error(), control_event_loop();
  void say_goodbye();
  
#ifdef DEBUG
  /* install special error handler to force crash so we can trace problem */
  XSetErrorHandler(crash_on_error);
#ifdef SUN
  /* Under SunOS4 we can force floating point exceptions to report errors */
  ieee_handler("set", "common", SIGFPE_ABORT);
  abrupt_underflow_();
#endif
#else
#ifndef VMS
#ifndef SYSV
  {
    struct rlimit rlp;	/* l: UNIX resourse limit structure */
    /* disable core dumping */
    rlp.rlim_cur = 0;
    rlp.rlim_max = 0;
    setrlimit(RLIMIT_CORE, &rlp);
  }
#endif
#endif
#endif
  /* set up, read, and check parameters */
  init_params(argc, argv);
  if( control.verbose ) {
    (void)printf("\n Smithsonian Astrophysical Observatory image utility\n");
    (void)printf(" SAOimage %s\n\n", RevMsg);
  }
  init_packages(argc, argv);
  /* CALL MAIN EVENT LOOP (uses extern.h) */
  init_done = 1;
  control_event_loop();
  say_goodbye(0);
}

/*
 * Subroutine:	say_goodbye
 * Purpose:	Free server resources before exiting
 * Xlib calls:	XCloseDisplay()
 */
void say_goodbye ( code )
     int code;		/* i: code to identify exit */
{
  void free_gc();

  if( desktop.display != NULL ) {
    free_gc();
    XCloseDisplay(desktop.display);
  }
  exit(code);
}

/*
 * Subroutine:	init_params
 * Purpose:	Initialize parameters in the records
 * Note:	Resource or default file not yet used
 */
static void init_params ( argc, argv )
     int argc;
     char **argv;
{
  char *name;		/* l: both flag for init and return display name */
  int parse_stat;
  static void init_server();
  int parse_cmdline(), check_image();
  void say_goodbye(), init_connections();

  name = NULL;
  wcs = NULL;
  /* get parameters from the command line (uses extern.h) */
  strcpy (wcscoor,"same");        /* Output coordinate system */
  strcpy (wcscommand,"rgsc_%s");      /* WCS command */

  if( (parse_stat = parse_cmdline(argc, argv, &name)) < 0 )
    say_goodbye(1);
  /* check image parameters for consistency (uses extern.h) */
  if( check_image(&img, parse_stat) )
    img.file_type = SOP_Logo;
  /* connect to the display server */
  init_server(name, &desktop, &color);
  init_connections();
}

/*
 * Subroutine:	init_packages
 * Purpose:	Initialize all of the subroutine packages and windows
 */
static void init_packages ( argc, argv )
     int argc;
     char **argv;
{
  GC gc;
  int init_image();
  GC set_gc_with_background();
  void init_windows1(), init_windows2(), init_panbox_coords(), disp_dispbox();
  void disp_panbox(), init_imaging_buffers(), init_mousepointers(), init_gc();
  void init_display_buffers(), init_region_draw(), init_color(), new_display();
  void init_software_cursors(), init_buttonmenu(), init_buttonbox_settings();
  void init_magnifier(), set_magnifier(), redraw_magnifier(), init_colorbox();
  void say_goodbye(), mount_buttonmenu(), new_panimage(), init_cgraph_text();

  /* get the image dimensions (need file name, type), exit if failure */
  if( init_image() == 0 ) {
    (void)fprintf(stderr, "No image given and no input device connected.\n");
    say_goodbye(1);
  }
  /* get the colors ready (need display) */
  init_color(&color, 1);
  /* map the desktop(need image dimensions, color) */
  init_windows1(argc, argv);
  /* set up the mouse pointer icons (need display, hardcolors) */
  init_mousepointers(desktop.display, desktop.display);
  /* initialize color bar and graph labeling text font and parameters */
  init_gc(desktop.display, desktop.ID);
  init_cgraph_text();
  /* set up the image buffer (need image) */
  init_imaging_buffers();
  /* get desktop dimensions and create windows (need colors, mouse cursors) */
  init_windows2();
  /* get the soft cursors ready(need colors, img coords) */
  init_software_cursors();
  /* set up color bar */
  init_colorbox();
  /* set up display buffers (need window dimensions) */
  init_display_buffers();
  /* set params and get image data (need colors, buffers, scope, imageinit) */
  init_panbox_coords();
  init_magnifier(control.magni_track, control.coord_track);
  new_display(0, 1, 1, 1);
  /* move image data into the panbox's buffer */
  new_panimage();
  /* set up the button menu */
  gc = set_gc_with_background(&color.gcset.menu, color.gcset.menu.background);
  init_buttonmenu(&btnbox, gc, color.visual,
		  (unsigned long)color.hard.std_black,
		  (unsigned long)color.hard.std_white);
  init_buttonbox_settings();
  /* set up region action flags and drawing stuff (draw and label on) */
  init_region_draw(1, 1);
  mount_buttonmenu();
}

#ifdef DEBUG
/*
 * Subroutine:	crash_on_error
 * Purpose:	Special error handler to force crash on error
 * Xlib calls:	XGetErrorText()
 */
void crash_on_error ( dt_display, error )
     Display *dt_display;
     XErrorEvent *error;
{
  int *foo;
  char msg[SZ_LINE];

  (void)fprintf(stderr,"XError Reported: type code: %d\n",error->error_code);
  XGetErrorText(dt_display, (int)error->error_code, msg, SZ_LINE);
  (void)fprintf(stderr, "%s\n", msg);
  /* cause fatal core dump */
  foo = 0;
  *foo = 0;
  exit( 0 );
}
#endif

/*
 * Subroutine:	init_server
 * Purpose:	Open connection with chosen or default display server
 */
static void init_server ( displayname, desktop, color )
     char *displayname;
     struct windowRec *desktop;
     struct colorRec *color;
{
  Display *dt_display;
  int dt_screen;
  char errmsg[SZ_FNAME];
  void exit_errmsg();

  /* make contact with the display server */
  if( !(dt_display = XOpenDisplay(displayname)) ) {
    (void)strcpy(errmsg, "Could not connect to display: ");
    if( displayname != NULL )
      (void)strcat(errmsg, displayname);
    exit_errmsg(errmsg);
  }
  desktop->display = dt_display;
  color->display = dt_display;
  dt_screen = DefaultScreen(dt_display);
  desktop->screen = dt_screen;
  color->screen = dt_screen;
  display = dt_display;
}