File: cmdnew.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 (303 lines) | stat: -rw-r--r-- 8,389 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
299
300
301
302
303
#ifndef lint
static char SccsId[] = "%W%  %G%";
#endif

/* Module:	cmdnew.c (Command New)
 * Purpose:	Orchestrate remembering and forgeting commandline arguments
 * Subroutine:	init_cmdline()			return: void
 * Subroutine:	get_new_cmd()			return: void
 * Xlib calls:	none
 * Copyright:	1989 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 January 1989
 *		{1} Jay Travisano (STScI)  VMS changes		  17 Nov 1989
 *              {2} MVH BSDonly strings.h compatability           19 Feb 1990
 *		{3} MVH separate remote IO from file IO		 9 March 1990
 *		{n} <who> -- <does what> -- <when>
 */

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

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

#include <X11/Xlib.h>		/* X window stuff */
#include <X11/Xutil.h>		/* X window manager stuff */
#include "hfiles/define.h"	/* define SZ_LINE, SZ_FNAME, etc */
#include "hfiles/struct.h"	/* declare structure types */
#include "hfiles/extern.h"	/* extern main parameter structures */
#include "hfiles/constant.h"	/* define codes */
#include "hfiles/cmdparse.h"	/* define parse status bits */
#include "hfiles/edit.h"

#ifdef VMS
#define close_disk close_pipe
#endif

static char input_line[SZ_LINE];
static char fname[SZ_FNAME];

/*
 * Subroutine:	init_cmdline
 * Purpose:	Save the original command line in a static string for use
 *		as an initial command line by get_new_command
 * Called by:	parse_cmdline() in CmdParse.c
 */
void init_cmdline ( argc, argv )
     int argc;
     char *argv[];
{
  void string_cmdline();

  string_cmdline (argc, argv, input_line, SZ_LINE);
}

static EditStruct *cmd_edit;
static char *prompt = "Enter new image file or command line:";
/*
 * Subroutine:	get_new_cmd
 * Purpose:	Enter a new command line and respond to it.
 * Called by:	key_response() in MainKey.c
 * Called by:	select_environment() in MainSelect.c
 */
void get_new_cmd ( )
{
  static int new_command();
  int get_edit_input();
  EditStruct *init_edit_popup();

  if( cmd_edit == NULL )
    cmd_edit = init_edit_popup(input_line, SZ_LINE);
  if( (get_edit_input(cmd_edit, 0, 1, 1, prompt) <= 0) ||
     (cmd_edit->char_cnt == 0) )
    return;
  (void)strncpy(input_line, cmd_edit->string, cmd_edit->char_cnt);
  input_line[cmd_edit->char_cnt] = '\0';
  /* parse line and respond */
  (void)new_command (input_line);
}

/*
 * Subroutine:	new_command
 * Purpose:	Given a new command line, parse it and do what is called for
 * Returns:	1 = success, 0 = user decided not to do anything, -1 = error
 */
static int new_command ( input_line )
     char *input_line;
{
  char **argv;
  int argc;
  int parse_status;
  int headersize;
  int parse_cmdline(), check_image();
  void reinit_color(), redraw_magnifier(), touch_submenu_button();
  static char **make_argv();
  static int new_file(), form_tokens();
  static void redo_displays(), clear_params(), reset_dispparams(), free_argv();

  /* store some key initial values */
  headersize = img.headersize;
  /* reinitialize user settings */
  clear_params();
  /* get space for listing tokens */
  argv = make_argv(20);
  argc = form_tokens(input_line, argv, 20);
  /* get values from the command line (uses extern.h) */
  parse_status = parse_cmdline(argc, argv, (char **)NULL);
  /* move filename to permanent string space, then free token space */
  if( (parse_status > 0) && (parse_status & CMD_FNAME) ) {
    (void)strcpy(fname, img.filename);
    img.filename = fname;
  }
  free_argv(argv, 20);
  if( parse_status < 0 )
    return( 0 );
  if( parse_status & CMD_COLOR ) {
    /* if requesting a different hardware configuration */
    /* do this work if it won't otherwise be done */
    if( (parse_status & (CMD_FNAME | CMD_FTYPE | CMD_FREAD |
			 CMD_ROTATE | CMD_SCALIM | CMD_SCALE)) == 0 )
      reinit_color(1);
    else
      reinit_color(0);
  }
  if( parse_status & (CMD_FNAME | CMD_FTYPE | CMD_FREAD) ) {
    if( check_image(&img, parse_status) < 0 ) {
      if( parse_status & CMD_COLOR )
	redo_displays();
    } else
      (void)new_file();
  } else {
    img.headersize = headersize;
    if( parse_status & (CMD_ROTATE | CMD_SCALIM) ) {
      reset_dispparams();
      (void)new_file();
    } else if( parse_status & CMD_SCALE ) {
      touch_submenu_button(SOP, color.scale.mode);
      redo_displays();
    } else if( parse_status & CMD_COLOR ) {
      /* redraw the magnibox display if color map was changed */
      redraw_magnifier();
    }
  }
  return( 1 );
}

/*
 * Subroutine:	redo_displays
 */
static void redo_displays ( )
{
  void new_scalemap(), map_panbox(), map_dispbox(), disp_panbox();
  void disp_dispbox(), redraw_magnifier();

  /* make the new lookup table */
  new_scalemap();
  /* refill display buffers with rescaled values and display */
  map_panbox();
  disp_panbox();
  /* panbox first because it is faster */
  map_dispbox();
  disp_dispbox();
  redraw_magnifier();
}

/*
 * Subroutine:	clear_params
 * Purpose:	Undo parameters checked for defaults when loading a new image
 */
static void clear_params ( )
{
  img.headersize = 0;
  img.byte_swap = 0;
  img.fdblock = 0;
  img.fdcenX = 0.0;
  img.fdcenY = 0.0;
  img.fiX1 = 0;
  img.fiX2 = 0;
  img.fiY1 = 0;
  img.fiY2 = 0;
}

/*
 * Subroutine:	reset_dispparams
 * Purpose:	Set the current display parameters for reloading the image
 */
static void reset_dispparams ( )
{
  float zoom;
  float fcenX, fcenY;
  void d_transform();

  if( img.fdcenX == 0.0 ) {
    /* calculate file coords of center of display */
    d_transform(&coord.imgtofile,
		(double)coord.id.cenX, (double)coord.id.cenY, &fcenX, &fcenY);
    img.fdcenX = (double)fcenX;
    img.fdcenY = (double)fcenY;
  }
  if( img.fdblock == 0.0 ) {
    if( coord.filetoimg.inx_outx != 0.0 )
      zoom = coord.filetoimg.inx_outx * coord.imgtodisp.inx_outx;
    else
      zoom = coord.filetoimg.iny_outx * coord.imgtodisp.inx_outx;
    /* take abs */
    if( zoom < 0.0 )
      zoom = -zoom;
    if( zoom < 1.0 )
      img.fdblock = (int)(-1.0/zoom);
    else
      img.fdblock = (int)zoom;
  }
}

/*
 * Subroutine:	make_argv
 * Purpose:	Allocate space used for commandline
 */
static char **make_argv ( maxargs )
     int maxargs;
{
  int i;
  char **argv;
  char *calloc_errchk();

  argv = (char **)calloc_errchk(maxargs, sizeof(char **), "Parse buffer");
  for( i = 0; i < maxargs; i++ )
    argv[i] = calloc_errchk(SZ_FNAME, sizeof(char), "Parse buffer");
  return( argv );
}

/*
 * Subroutine:	free_argv
 * Purpose:	Free the space used for parsing the command line
 */
static void free_argv ( argv, maxargs )
     int maxargs;
     char **argv;
{
  int i;
  for( i=0; i<maxargs; i++ )
    free(argv[i]);
  free( (char *)argv );
}

/*
 * Subroutine:	form_tokens
 * Purpose:	Break a command string into tokens (mimic initial command line)
 * Returns:	Count of tokens
 */
static int form_tokens ( input_string, argv, maxargs )
     char *input_string;
     char **argv;
     int maxargs;
{
  int i;
  char format[SZ_LINE];

  (void)strcpy(format, "%s");
  for( i=1; i<maxargs; i++ )
    (void)strncat(format, "%s", SZ_LINE);
  return( sscanf(input_string, format,
		 argv[0], argv[1], argv[2], argv[3], argv[4],
		 argv[5], argv[6], argv[7], argv[8], argv[9],
		 argv[10], argv[11], argv[12], argv[13], argv[14],
		 argv[15], argv[16], argv[17], argv[18], argv[19]) );
}

/*
 * Subroutine:	new_file
 * Purpose:	Having the image record set, do all that is needed to load
 *		a new image file
 */
static int new_file ( )
{
  int init_image(), init_imagebuf();
  void set_tdisp(), new_display(), new_panbox(), disp_panbox();

  /* get the image dimensions (need file name, type) */
  if( init_image() == 0 )
    return(0);
  /* resize the image buffer */
  (void)init_imagebuf();
  /* set display params and force reassessing and rescaling the image */
  set_tdisp(&coord);
  buffer.mm.img_leftX = coord.id.srcX1 + 1;
  /* read new image data */
  new_display(1, 1, 1, 1);
  /* replace the panbox with a new one */
  new_panbox(1);
  disp_panbox();
  return( 1 );
}