home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / cmdnew.c < prev    next >
C/C++ Source or Header  |  1990-05-01  |  8KB  |  304 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    cmdnew.c (Command New)
  6.  * Purpose:    Orchestrate remembering and forgeting commandline arguments
  7.  * Subroutine:    init_cmdline()            return: void
  8.  * Subroutine:    get_new_cmd()            return: void
  9.  * Xlib calls:    none
  10.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  11.  *        You may do anything you like with this file except remove
  12.  *        this copyright.  The Smithsonian Astrophysical Observatory
  13.  *        makes no representations about the suitability of this
  14.  *        software for any purpose.  It is provided "as is" without
  15.  *        express or implied warranty.
  16.  * Modified:    {0} Michael VanHilst    initial version           9 January 1989
  17.  *        {1} Jay Travisano (STScI)  VMS changes          17 Nov 1989
  18.  *              {2} MVH BSDonly strings.h compatability           19 Feb 1990
  19.  *        {3} MVH separate remote IO from file IO         9 March 1990
  20.  *        {n} <who> -- <does what> -- <when>
  21.  */
  22.  
  23. #include <stdio.h>        /* stderr, NULL, etc. */
  24.  
  25. #ifndef VMS
  26. #ifdef SYSV
  27. #include <string.h>
  28. #else
  29. #include <strings.h>        /* strlen, etc. for unenlightened BSD's */
  30. #endif
  31. #else
  32. #include <string.h>
  33. #endif
  34.  
  35. #include <X11/Xlib.h>        /* X window stuff */
  36. #include <X11/Xutil.h>        /* X window manager stuff */
  37. #include "hfiles/define.h"    /* define SZ_LINE, SZ_FNAME, etc */
  38. #include "hfiles/struct.h"    /* declare structure types */
  39. #include "hfiles/extern.h"    /* extern main parameter structures */
  40. #include "hfiles/constant.h"    /* define codes */
  41. #include "hfiles/cmdparse.h"    /* define parse status bits */
  42. #include "hfiles/edit.h"
  43.  
  44. #ifdef VMS
  45. #define close_disk close_pipe
  46. #endif
  47.  
  48. static char input_line[SZ_LINE];
  49. static char fname[SZ_FNAME];
  50.  
  51. /*
  52.  * Subroutine:    init_cmdline
  53.  * Purpose:    Save the original command line in a static string for use
  54.  *        as an initial command line by get_new_command
  55.  * Called by:    parse_cmdline() in CmdParse.c
  56.  */
  57. void init_cmdline ( argc, argv )
  58.      int argc;
  59.      char *argv[];
  60. {
  61.   void string_cmdline();
  62.  
  63.   string_cmdline (argc, argv, input_line, SZ_LINE);
  64. }
  65.  
  66. static EditStruct *cmd_edit;
  67. static char *prompt = "Enter new image file or command line:";
  68. /*
  69.  * Subroutine:    get_new_cmd
  70.  * Purpose:    Enter a new command line and respond to it.
  71.  * Called by:    key_response() in MainKey.c
  72.  * Called by:    select_environment() in MainSelect.c
  73.  */
  74. void get_new_cmd ( )
  75. {
  76.   static int new_command();
  77.   int get_edit_input();
  78.   EditStruct *init_edit_popup();
  79.  
  80.   if( cmd_edit == NULL )
  81.     cmd_edit = init_edit_popup(input_line, SZ_LINE);
  82.   if( (get_edit_input(cmd_edit, 0, 1, 1, prompt) <= 0) ||
  83.      (cmd_edit->char_cnt == 0) )
  84.     return;
  85.   (void)strncpy(input_line, cmd_edit->string, cmd_edit->char_cnt);
  86.   input_line[cmd_edit->char_cnt] = '\0';
  87.   /* parse line and respond */
  88.   (void)new_command (input_line);
  89. }
  90.  
  91. /*
  92.  * Subroutine:    new_command
  93.  * Purpose:    Given a new command line, parse it and do what is called for
  94.  * Returns:    1 = success, 0 = user decided not to do anything, -1 = error
  95.  */
  96. static int new_command ( input_line )
  97.      char *input_line;
  98. {
  99.   char **argv;
  100.   int argc;
  101.   int parse_status;
  102.   int headersize;
  103.   int parse_cmdline(), check_image();
  104.   void reinit_color(), redraw_magnifier(), touch_submenu_button();
  105.   static char **make_argv();
  106.   static int new_file(), form_tokens();
  107.   static void redo_displays(), clear_params(), reset_dispparams(), free_argv();
  108.  
  109.   /* store some key initial values */
  110.   headersize = img.headersize;
  111.   /* reinitialize user settings */
  112.   clear_params();
  113.   /* get space for listing tokens */
  114.   argv = make_argv(20);
  115.   argc = form_tokens(input_line, argv, 20);
  116.   /* get values from the command line (uses extern.h) */
  117.   parse_status = parse_cmdline(argc, argv, (char **)NULL);
  118.   /* move filename to permanent string space, then free token space */
  119.   if( (parse_status > 0) && (parse_status & CMD_FNAME) ) {
  120.     (void)strcpy(fname, img.filename);
  121.     img.filename = fname;
  122.   }
  123.   free_argv(argv, 20);
  124.   if( parse_status < 0 )
  125.     return( 0 );
  126.   if( parse_status & CMD_COLOR ) {
  127.     /* if requesting a different hardware configuration */
  128.     /* do this work if it won't otherwise be done */
  129.     if( (parse_status & (CMD_FNAME | CMD_FTYPE | CMD_FREAD |
  130.              CMD_ROTATE | CMD_SCALIM | CMD_SCALE)) == 0 )
  131.       reinit_color(1);
  132.     else
  133.       reinit_color(0);
  134.   }
  135.   if( parse_status & (CMD_FNAME | CMD_FTYPE | CMD_FREAD) ) {
  136.     if( check_image(&img, parse_status) < 0 ) {
  137.       if( parse_status & CMD_COLOR )
  138.     redo_displays();
  139.     } else
  140.       (void)new_file();
  141.   } else {
  142.     img.headersize = headersize;
  143.     if( parse_status & (CMD_ROTATE | CMD_SCALIM) ) {
  144.       reset_dispparams();
  145.       (void)new_file();
  146.     } else if( parse_status & CMD_SCALE ) {
  147.       touch_submenu_button(SOP, color.scale.mode);
  148.       redo_displays();
  149.     } else if( parse_status & CMD_COLOR ) {
  150.       /* redraw the magnibox display if color map was changed */
  151.       redraw_magnifier();
  152.     }
  153.   }
  154.   return( 1 );
  155. }
  156.  
  157. /*
  158.  * Subroutine:    redo_displays
  159.  */
  160. static void redo_displays ( )
  161. {
  162.   void new_scalemap(), map_panbox(), map_dispbox(), disp_panbox();
  163.   void disp_dispbox(), redraw_magnifier();
  164.  
  165.   /* make the new lookup table */
  166.   new_scalemap();
  167.   /* refill display buffers with rescaled values and display */
  168.   map_panbox();
  169.   disp_panbox();
  170.   /* panbox first because it is faster */
  171.   map_dispbox();
  172.   disp_dispbox();
  173.   redraw_magnifier();
  174. }
  175.  
  176. /*
  177.  * Subroutine:    clear_params
  178.  * Purpose:    Undo parameters checked for defaults when loading a new image
  179.  */
  180. static void clear_params ( )
  181. {
  182.   img.headersize = 0;
  183.   img.byte_swap = 0;
  184.   img.fdblock = 0;
  185.   img.fdcenX = 0.0;
  186.   img.fdcenY = 0.0;
  187.   img.fiX1 = 0;
  188.   img.fiX2 = 0;
  189.   img.fiY1 = 0;
  190.   img.fiY2 = 0;
  191. }
  192.  
  193. /*
  194.  * Subroutine:    reset_dispparams
  195.  * Purpose:    Set the current display parameters for reloading the image
  196.  */
  197. static void reset_dispparams ( )
  198. {
  199.   float zoom;
  200.   float fcenX, fcenY;
  201.   void d_transform();
  202.  
  203.   if( img.fdcenX == 0.0 ) {
  204.     /* calculate file coords of center of display */
  205.     d_transform(&coord.imgtofile,
  206.         (double)coord.id.cenX, (double)coord.id.cenY, &fcenX, &fcenY);
  207.     img.fdcenX = (double)fcenX;
  208.     img.fdcenY = (double)fcenY;
  209.   }
  210.   if( img.fdblock == 0.0 ) {
  211.     if( coord.filetoimg.inx_outx != 0.0 )
  212.       zoom = coord.filetoimg.inx_outx * coord.imgtodisp.inx_outx;
  213.     else
  214.       zoom = coord.filetoimg.iny_outx * coord.imgtodisp.inx_outx;
  215.     /* take abs */
  216.     if( zoom < 0.0 )
  217.       zoom = -zoom;
  218.     if( zoom < 1.0 )
  219.       img.fdblock = (int)(-1.0/zoom);
  220.     else
  221.       img.fdblock = (int)zoom;
  222.   }
  223. }
  224.  
  225. /*
  226.  * Subroutine:    make_argv
  227.  * Purpose:    Allocate space used for commandline
  228.  */
  229. static char **make_argv ( maxargs )
  230.      int maxargs;
  231. {
  232.   int i;
  233.   char **argv;
  234.   char *calloc_errchk();
  235.  
  236.   argv = (char **)calloc_errchk(maxargs, sizeof(char **), "Parse buffer");
  237.   for( i = 0; i < maxargs; i++ )
  238.     argv[i] = calloc_errchk(SZ_FNAME, sizeof(char), "Parse buffer");
  239.   return( argv );
  240. }
  241.  
  242. /*
  243.  * Subroutine:    free_argv
  244.  * Purpose:    Free the space used for parsing the command line
  245.  */
  246. static void free_argv ( argv, maxargs )
  247.      int maxargs;
  248.      char **argv;
  249. {
  250.   int i;
  251.   for( i=0; i<maxargs; i++ )
  252.     free(argv[i]);
  253.   free( (char *)argv );
  254. }
  255.  
  256. /*
  257.  * Subroutine:    form_tokens
  258.  * Purpose:    Break a command string into tokens (mimic initial command line)
  259.  * Returns:    Count of tokens
  260.  */
  261. static int form_tokens ( input_string, argv, maxargs )
  262.      char *input_string;
  263.      char **argv;
  264.      int maxargs;
  265. {
  266.   int i;
  267.   char format[SZ_LINE];
  268.  
  269.   (void)strcpy(format, "%s");
  270.   for( i=1; i<maxargs; i++ )
  271.     (void)strncat(format, "%s", SZ_LINE);
  272.   return( sscanf(input_string, format,
  273.          argv[0], argv[1], argv[2], argv[3], argv[4],
  274.          argv[5], argv[6], argv[7], argv[8], argv[9],
  275.          argv[10], argv[11], argv[12], argv[13], argv[14],
  276.          argv[15], argv[16], argv[17], argv[18], argv[19]) );
  277. }
  278.  
  279. /*
  280.  * Subroutine:    new_file
  281.  * Purpose:    Having the image record set, do all that is needed to load
  282.  *        a new image file
  283.  */
  284. static int new_file ( )
  285. {
  286.   int init_image(), init_imagebuf();
  287.   void set_tdisp(), new_display(), new_panbox(), disp_panbox();
  288.  
  289.   /* get the image dimensions (need file name, type) */
  290.   if( init_image() == 0 )
  291.     return(0);
  292.   /* resize the image buffer */
  293.   (void)init_imagebuf();
  294.   /* set display params and force reassessing and rescaling the image */
  295.   set_tdisp(&coord);
  296.   buffer.mm.img_leftX = coord.id.srcX1 + 1;
  297.   /* read new image data */
  298.   new_display(1, 1, 1, 1);
  299.   /* replace the panbox with a new one */
  300.   new_panbox(1);
  301.   disp_panbox();
  302.   return( 1 );
  303. }
  304.