home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / the25.zip / thesrc251.zip / the.c < prev    next >
C/C++ Source or Header  |  1998-07-31  |  50KB  |  1,377 lines

  1. /***********************************************************************/
  2. /* THE.C - The Hessling Editor                                         */
  3. /***********************************************************************/
  4. /*
  5.  * THE - The Hessling Editor. A text editor similar to VM/CMS xedit.
  6.  * Copyright (C) 1991-1997 Mark Hessling
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License as
  10.  * published by the Free Software Foundation; either version 2 of
  11.  * the License, or any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16.  * General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to:
  20.  *
  21.  *    The Free Software Foundation, Inc.
  22.  *    675 Mass Ave,
  23.  *    Cambridge, MA 02139 USA.
  24.  *
  25.  *
  26.  * If you make modifications to this software that you feel increases
  27.  * it usefulness for the rest of the community, please email the
  28.  * changes, enhancements, bug fixes as well as any and all ideas to me.
  29.  * This software is going to be maintained and enhanced as deemed
  30.  * necessary by the community.
  31.  *
  32.  * Mark Hessling                    Email:       M.Hessling@qut.edu.au
  33.  * PO Box 203                       Phone:              +617 3802 0800
  34.  * Bellara                          http://www.lightlink.com/hessling/
  35.  * QLD 4507                         **** Author of THE & Rexx/SQL ****
  36.  * Australia                        ****** Maintainer PDCurses *******
  37.  */
  38.  
  39. /*
  40. $Id: the.c 2.1 1995/06/24 16:31:24 MH Rel MH $
  41. */
  42.  
  43. #define MAIN 1
  44. #include <the.h>
  45. #include <proto.h>
  46.  
  47. #if defined(DOS) || defined(OS2)
  48. #  if !defined(EMX) && !defined(GO32)
  49. #    include <direct.h>
  50. #  endif
  51. #endif
  52.  
  53. #ifdef HAVE_PROTO
  54. static RETSIGTYPE handle_signal(int);
  55. static void display_info(CHARTYPE *);
  56. static void init_signals(void);
  57. #else
  58. static RETSIGTYPE handle_signal();
  59. static void display_info();
  60. static void init_signals();
  61. #endif
  62.  
  63. /*--------------------------- global data -----------------------------*/
  64.  WINDOW *statarea=NULL,*error_window=NULL,*divider=NULL;
  65.  VIEW_DETAILS *vd_current=(VIEW_DETAILS *)NULL;
  66.  VIEW_DETAILS *vd_first=(VIEW_DETAILS *)NULL;
  67.  VIEW_DETAILS *vd_last=(VIEW_DETAILS *)NULL;
  68.  VIEW_DETAILS *vd_mark=(VIEW_DETAILS *)NULL;
  69.  CHARTYPE number_of_views = 0;                      /* number of views */
  70.  CHARTYPE number_of_files = 0;                      /* number of files */
  71.  CHARTYPE display_screens = 1;                    /* number of screens */
  72.  CHARTYPE current_screen = 0;
  73.  SCREEN_DETAILS screen[MAX_SCREENS];            /* 2 screen structures */
  74.  bool rexx_support;                          /* initially REXX support */
  75.  bool batch_only=FALSE;                   /* are we running in batch ? */
  76.  bool horizontal=TRUE;
  77.  bool first_screen_display=FALSE;
  78.  short save_coord_x[VIEW_WINDOWS];
  79.  short save_coord_y[VIEW_WINDOWS];
  80.  
  81.  LINE *next_line=NULL,*curr_line=NULL;
  82.  LINE *first_file_name=NULL,*current_file_name=NULL;
  83.  LINE *editv=NULL;
  84.  bool error_on_screen=FALSE;
  85.  bool colour_support=TRUE;         /* indicates if colour is supported */
  86.  bool initial=TRUE;
  87.  bool been_interactive=FALSE;
  88.  CHARTYPE *rec=NULL;
  89.  LENGTHTYPE rec_len = 0;                              /* length of rec */
  90.  CHARTYPE *trec=NULL;
  91.  LENGTHTYPE trec_len = 0;
  92.  CHARTYPE *cmd_rec=NULL;
  93.  unsigned short cmd_rec_len = 0;                  /* length of cmd_rec */
  94.  CHARTYPE *pre_rec=NULL;
  95.  unsigned short pre_rec_len = 0;                  /* length of cmd_rec */
  96.  CHARTYPE *profile_command_line=NULL;
  97.  CHARTYPE *target_buffer=NULL;            /* used in get_item_values() */
  98.  unsigned short target_buffer_len=0;        /* length of target buffer */
  99.  bool focus_changed = FALSE;    /* indicates if focus line has changed */
  100.  bool current_changed = FALSE;/* indicates if current line has changed */
  101.  bool in_profile=FALSE;             /* indicates if processing profile */
  102.  bool in_nomsg=FALSE;       /* indicates if running from NOMSG command */
  103.  bool in_reprofile=FALSE;         /* indicates if reprocessing profile */
  104.  int profile_file_executions=0; /* number of times profile file has executed */
  105.  bool execute_profile=TRUE;/* indicates if we are to process a profile */
  106.  bool in_macro=FALSE;            /* indicates if processing REXX macro */
  107.  bool in_repeat=FALSE;          /* indicates if running REPEAT command */
  108.  bool in_readv=FALSE;         /* indicates if processing READV CMDLINE */
  109.  bool file_read=FALSE;           /* indicates if we have read the file */
  110.  bool curses_started=FALSE;         /* indicates if curses has started */
  111.  bool readonly=FALSE;     /* indicates if running THE in readonly mode */
  112.  
  113.  CHARTYPE *the_version = (CHARTYPE *)"2.5.1";
  114.  CHARTYPE *the_release = (CHARTYPE *)"28-Jul-1998";
  115.  CHARTYPE *the_copyright = (CHARTYPE *)"Copyright 1991-1998 Mark Hessling";
  116.  CHARTYPE term_name[20];  /* $TERM value */
  117.  CHARTYPE *tempfilename = (CHARTYPE *)NULL;
  118. #if defined(UNIX)
  119.  CHARTYPE user_home_dir[MAX_FILE_NAME+1];
  120. # define THE_PROFILE_FILE ".therc"
  121. #else
  122. # define THE_PROFILE_FILE "PROFILE.THE"
  123. #endif
  124. #if !defined(MULTIPLE_PSEUDO_FILES)
  125.  CHARTYPE *rexxoutname = (CHARTYPE *)"REXX.$$$";
  126.  CHARTYPE *keyfilename = (CHARTYPE *)"KEY$$$.$$$";
  127.  CHARTYPE rexx_pathname[MAX_FILE_NAME+1];
  128.  CHARTYPE rexx_filename[10];
  129. # ifdef VMS
  130.  CHARTYPE *dirfilename = (CHARTYPE *)"DIR.THE";
  131. # else
  132.  CHARTYPE *dirfilename = (CHARTYPE *)"DIR.DIR";
  133. # endif
  134. #endif
  135.  
  136. #ifndef XCURSES
  137. # define XTERM_PROGRAM "N/A"
  138. #endif
  139.  
  140.  CHARTYPE xterm_program[MAX_FILE_NAME+1]; /* default shell for XCURSES */
  141.  
  142.  CHARTYPE macro_suffix[12] = ".the";   /* default extension for macros */
  143. #if !defined(MULTIPLE_PSEUDO_FILES)
  144.  CHARTYPE dir_pathname[MAX_FILE_NAME+1];
  145.  CHARTYPE dir_filename[10];
  146.  CHARTYPE key_pathname[MAX_FILE_NAME+1];
  147.  CHARTYPE key_filename[15];
  148. #endif
  149.  CHARTYPE curr_path[MAX_FILE_NAME+1];
  150.  CHARTYPE sp_path[MAX_FILE_NAME+1];
  151.  CHARTYPE sp_fname[MAX_FILE_NAME+1];
  152.  CHARTYPE dir_path[MAX_FILE_NAME+1];        /* for dir and ls commands */
  153.  CHARTYPE dir_files[MAX_FILE_NAME+1];       /* for dir and ls commands */
  154.  CHARTYPE rexx_macro_name[MAX_FILE_NAME+1]; /* current rexx macro name */
  155.  CHARTYPE rexx_macro_parameters[MAX_FILE_NAME+1]; /* current rexx macro parameters */
  156.  
  157.  CHARTYPE the_home_dir[MAX_FILE_NAME+1];
  158.  CHARTYPE the_help_file[MAX_FILE_NAME+1];
  159.  
  160.  CHARTYPE the_macro_path[MAX_FILE_NAME+1];        
  161.  CHARTYPE the_macro_path_buf[MAX_FILE_NAME+1];
  162.  CHARTYPE *the_macro_dir[MAX_MACRO_DIRS];
  163.  int max_macro_dirs = 0;
  164.  
  165.  CHARTYPE *prf_arg=(CHARTYPE *)NULL;
  166.  CHARTYPE *local_prf=(CHARTYPE *)NULL;
  167.  CHARTYPE *specified_prf=(CHARTYPE *)NULL;
  168.  
  169.  CHARTYPE tabkey_insert='C';
  170.  CHARTYPE tabkey_overwrite='T';
  171.  unsigned short file_start = 38;
  172.  
  173. #if defined(UNIX) || defined(OS2) || defined(EMX)
  174.  CHARTYPE *spooler_name=NULL;
  175. #endif
  176.  
  177.  struct stat stat_buf;
  178.  
  179.  LENGTHTYPE display_length=0;
  180.  
  181.  short lastrc=0;
  182.  short compatible_look=COMPAT_THE;
  183.  short compatible_feel=COMPAT_THE;
  184.  short compatible_keys=COMPAT_THE;
  185.  short prefix_width=DEFAULT_PREFIX_WIDTH;
  186.  short prefix_gap=DEFAULT_PREFIX_GAP;
  187.  
  188.  chtype etmode_table[256];
  189.  bool   etmode_flag[256];
  190.  
  191. #define DEFAULT_LINES 24
  192. #define DEFAULT_COLS  80
  193.  
  194.  short terminal_lines=DEFAULT_LINES;
  195.  short terminal_cols=DEFAULT_COLS;
  196. /*---------------------------------------------------------------------*/
  197. /* Following are for getopt function(s).                               */
  198. /*---------------------------------------------------------------------*/
  199. extern char *optarg;
  200. extern int optind;
  201. /*---------------------------------------------------------------------*/
  202. /* Following are for original cursor position for EXTRACT /CURSOR/     */
  203. /*---------------------------------------------------------------------*/
  204.  LINETYPE original_screen_line = (-1L);
  205.  LINETYPE original_screen_column = (-1L);
  206.  LINETYPE original_file_line = (-1L);
  207.  LINETYPE original_file_column = (-1L);
  208. /*---------------------------------------------------------------------*/
  209. /* Following are for startup LINE and COLUMN                           */
  210. /*---------------------------------------------------------------------*/
  211.  LINETYPE startup_line = 0L;
  212.  LENGTHTYPE startup_column = 0;
  213.  
  214. #ifdef XCURSES
  215.  char *XCursesProgramName = "the";
  216. #endif
  217. /***********************************************************************/
  218. #ifdef MSWIN
  219. int Themain(argc,argv)
  220. int argc;
  221. char *argv[];
  222. #else
  223. #ifdef HAVE_PROTO
  224. int main(int argc, char *argv[])
  225. #else
  226. int main(argc,argv)
  227. short argc;
  228. char *argv[];
  229. #endif
  230. #endif
  231. /***********************************************************************/
  232. {
  233. /*-------------------------- external data ----------------------------*/
  234.  extern CHARTYPE *last_message;
  235.  extern DEFINE *first_define;
  236.  extern DEFINE *first_mouse_define;
  237.  extern bool CLEARSCREENx;
  238.  extern LINE *first_prefix_synonym;
  239.  extern CHARTYPE last_change_command[MAX_COMMAND_LENGTH];
  240.  extern CHARTYPE last_target[MAX_COMMAND_LENGTH];
  241.  extern bool SLKx;
  242.  extern bool SBx;
  243.  
  244. #ifdef MSWIN
  245.  extern void efree();
  246.  extern char far *emalloc(unsigned long);
  247.  extern char far *erealloc(void far *,unsigned long);
  248.  extern char far *ecalloc();
  249. #endif
  250. /*--------------------------- local data ------------------------------*/
  251.  register short i=0;
  252.  short c=0;
  253.  bool trap_signals=TRUE;
  254.  bool pause_for_errors=FALSE;
  255.  short rc=RC_OK;
  256.  char *envptr=NULL,*tmpptr=NULL;
  257. /*--------------------------- processing ------------------------------*/
  258. #ifdef __EMX__
  259.  _wildcard(&argc,&argv);
  260. #endif
  261.  
  262. #ifdef TRACE
  263.  trace_initialise();
  264.  trace_function("the.c:     main");
  265. #endif
  266. /*---------------------------------------------------------------------*/
  267. /* Ensure that CURRENT_VIEW is NULL before starting. This is to ensure */
  268. /* that any errors generated before CURRENT_VIEW is assigned are       */
  269. /* handled gracefully.                                                 */
  270. /*---------------------------------------------------------------------*/
  271.  CURRENT_VIEW = (VIEW_DETAILS *)NULL;
  272. /*---------------------------------------------------------------------*/
  273. /* Set up our memory management calls. This is where you can specify a */
  274. /* debugging memory manager.                                           */
  275. /*---------------------------------------------------------------------*/
  276. #ifdef MSWIN
  277.  the_malloc  = emalloc;
  278.  the_calloc  = ecalloc;
  279.  the_free    = efree;
  280.  the_realloc = erealloc;
  281.  Win31Startup();
  282. #else
  283.  if (getenv("NO_FLISTS"))
  284.    {
  285.     the_malloc  = malloc;
  286.     the_calloc  = calloc;
  287.     the_free    = free;
  288.     the_realloc = realloc;
  289.    }
  290.  else
  291.    {
  292.     the_malloc  = get_a_block;
  293.     the_calloc  = NULL;
  294.     the_free    = give_a_block;
  295.     the_realloc = resize_a_block;
  296.     init_memory_table();
  297.    }
  298. #endif
  299. /*---------------------------------------------------------------------*/
  300. /* Set up flag to indicate that we are not interactive...yet.          */
  301. /*---------------------------------------------------------------------*/
  302.  in_profile = TRUE;
  303.  execute_profile = TRUE;
  304.  in_macro = FALSE;
  305. /*---------------------------------------------------------------------*/
  306. /* Initialise the printer spooler.                                     */
  307. /*---------------------------------------------------------------------*/
  308. #if defined(UNIX) || defined(OS2) || defined(EMX)
  309.  if ((spooler_name = (CHARTYPE *)(*the_malloc)(5*sizeof(CHARTYPE))) == NULL)
  310.    {
  311.     cleanup();
  312.     display_error(30,(CHARTYPE *)"",FALSE);
  313.     return(1);
  314.    }
  315. #  ifdef UNIX
  316.  strcpy((DEFCHAR *)spooler_name,(DEFCHAR *)"lpr");
  317. #  else
  318.  strcpy((DEFCHAR *)spooler_name,(DEFCHAR *)"LPT1");
  319. #  endif
  320. #endif
  321. /*---------------------------------------------------------------------*/
  322. /* Get all environment variables here. Some may be overridden by       */
  323. /* command-line switches. (future possibility)                         */
  324. /*---------------------------------------------------------------------*/
  325. #if defined(UNIX)
  326.  if ((envptr = getenv("HOME")) != NULL)
  327.     strcpy((DEFCHAR *)user_home_dir,envptr);
  328.  else
  329.     strcpy((DEFCHAR *)user_home_dir,"./");
  330.  if (*(user_home_dir+strlen((DEFCHAR *)user_home_dir)-1) != ISLASH)
  331.     strcat((DEFCHAR *)user_home_dir,(DEFCHAR *)ISTR_SLASH);
  332.  if ((envptr = getenv("TERM")) != NULL)
  333.     strcpy((DEFCHAR *)term_name,envptr);
  334.  else
  335.     strcpy((DEFCHAR *)term_name,"default");
  336. #endif
  337. #if defined(__EMX__)
  338.  if (_osmode == DOS_MODE)
  339.     strcpy((DEFCHAR *)term_name,"DOS");
  340.  else
  341.     strcpy((DEFCHAR *)term_name,"OS2");
  342. #elif defined(DOS)
  343.  strcpy((DEFCHAR *)term_name,"DOS");
  344. #elif defined(OS2)
  345.  strcpy((DEFCHAR *)term_name,"OS2");
  346. #elif defined(XCURSES)
  347.  strcpy((DEFCHAR *)term_name,"X11");
  348. #elif defined(WIN32)
  349.  strcpy((DEFCHAR *)term_name,"WIN32");
  350. #endif
  351.  strcpy((DEFCHAR *)xterm_program,XTERM_PROGRAM);
  352. /*---------------------------------------------------------------------*/
  353. /* Get THE_HOME_DIR first (as all other paths rely on this value)      */
  354. /*---------------------------------------------------------------------*/
  355.  if ((envptr = getenv("THE_HOME_DIR")) != NULL)
  356.    {
  357.     strcpy((DEFCHAR *)the_home_dir,envptr);
  358.     (void *)strrmdup(strtrans(the_home_dir,OSLASH,ISLASH),ISLASH);
  359.     if ((the_home_dir[strlen((DEFCHAR *)the_home_dir)-1]) != ISLASH)
  360.        strcat((DEFCHAR *)the_home_dir,(DEFCHAR *)ISTR_SLASH);
  361.    }
  362.  else
  363.    {
  364. #if defined(UNIX)
  365.     strcpy((DEFCHAR *)the_home_dir,(DEFCHAR *)THE_HOME_DIRECTORY);
  366. #else
  367.     strcpy((DEFCHAR *)the_home_dir,(DEFCHAR *)argv[0]);
  368.     (void *)strrmdup(strtrans(the_home_dir,OSLASH,ISLASH),ISLASH);
  369.     i = strzreveq(the_home_dir,ISLASH);
  370.     if (i != (-1))
  371.        the_home_dir[i+1] = '\0';
  372.     else
  373.        the_home_dir[0] = '\0';
  374. #endif
  375.    }
  376. /*---------------------------------------------------------------------*/
  377. /* Get THE_MACRO_PATH environment variable. If not set set up default  */
  378. /* to be THE_HOME_DIR followed by the current directory.               */
  379. /*---------------------------------------------------------------------*/
  380.  if ((envptr = getenv("THE_MACRO_PATH")) != NULL)
  381.     Macropath((CHARTYPE*)envptr);
  382.  else
  383.    {
  384.     strcpy((DEFCHAR *)the_macro_path,(DEFCHAR *)the_home_dir);
  385.     if (strcmp((DEFCHAR *)the_macro_path,"") == 0)
  386.        strcpy((DEFCHAR *)the_macro_path,".");
  387. #if defined(UNIX)
  388.     strcat((DEFCHAR *)the_macro_path,":.");
  389. #else
  390.     strcat((DEFCHAR *)the_macro_path,";.");
  391. #endif
  392.     Macropath(the_macro_path);
  393.    }
  394. /*---------------------------------------------------------------------*/
  395. /* Set up help file name.                                              */
  396. /*---------------------------------------------------------------------*/
  397.  if ((envptr = getenv("THE_HELP_FILE")) != NULL)
  398.     strcpy((DEFCHAR *)the_help_file,envptr);
  399.  else
  400.    {
  401.     strcpy((DEFCHAR *)the_help_file,(DEFCHAR *)the_home_dir);
  402. /*  strcat((DEFCHAR *)the_help_file,(DEFCHAR *)term_name); */
  403.     strcat((DEFCHAR *)the_help_file,"THE_Help.txt");
  404.    }
  405.  (void *)strrmdup(strtrans(the_help_file,OSLASH,ISLASH),ISLASH);
  406. /*---------------------------------------------------------------------*/
  407. /* Process the command line arguments.                                 */
  408. /*---------------------------------------------------------------------*/
  409.  while ((c = getopt(argc,argv,"ksSbmnrl:c:p:w:a:u:h?")) != EOF)
  410.    switch((char)c)
  411.      {
  412.       case 's':        /* don't trap signals */
  413.                trap_signals = FALSE;
  414.                break;
  415.       case 'k':        /* allow Soft Label Keys */
  416.                SLKx = TRUE;
  417.                break;
  418.       case 'S':        /* allow scrollbar */
  419.                SBx = TRUE;
  420.                break;
  421.       case 'l':        /* set current line on startup */
  422.                startup_line = (LINETYPE)atol(optarg);
  423.                if (startup_line < 0L)
  424.                  {
  425.                   cleanup();
  426.                   display_error(5,(CHARTYPE *)"startup line MUST be > 0",FALSE);
  427.                   return(4);
  428.                  }
  429.                break;
  430.       case 'c':        /* set current column on startup */
  431.                startup_column = (LENGTHTYPE)atoi(optarg);
  432.                if (startup_column == 0)
  433.                  {
  434.                   cleanup();
  435.                   display_error(5,(CHARTYPE *)"startup column MUST be > 0",FALSE);
  436.                   return(4);
  437.                  }
  438.                break;
  439.       case 'b':        /* batch processing */
  440.                batch_only = TRUE;
  441.                break;
  442.       case 'm':        /* force into MONO */
  443.                colour_support = FALSE;
  444.                break;
  445.       case 'n':        /* do not execute any profile file */
  446.                execute_profile = FALSE;
  447.                break;
  448.       case 'r':        /* run in readonly mode */
  449.                readonly = TRUE;
  450.                break;
  451.       case 'p':        /* profile file name */
  452.                if ((specified_prf = (CHARTYPE *)(*the_malloc)((strlen(optarg)+1)*sizeof(CHARTYPE))) == NULL)
  453.                  {
  454.                   cleanup();
  455.                   display_error(30,(CHARTYPE *)"",FALSE);
  456.                   return(2);
  457.                  }
  458.                strcpy((DEFCHAR *)specified_prf,(DEFCHAR *)optarg);
  459.                break;
  460.       case 'a':        /* profile arguments */
  461.                if ((prf_arg = (CHARTYPE *)(*the_malloc)((strlen(optarg)+1)*sizeof(CHARTYPE))) == NULL)
  462.                  {
  463.                   cleanup();
  464.                   display_error(30,(CHARTYPE *)"",FALSE);
  465.                   return(3);
  466.                  }
  467.                strcpy((DEFCHAR *)prf_arg,(DEFCHAR *)optarg);
  468.                break;
  469.       case 'w':        /* width of line */
  470.                max_line_length = (unsigned short)atoi(optarg);
  471.                if (max_line_length < 10)
  472.                  {
  473.                   cleanup();
  474.                   display_error(5,(CHARTYPE *)"- width MUST be >= 10",FALSE);
  475.                   return(4);
  476.                  }
  477.                if (max_line_length > 32700)
  478.                  {
  479.                   cleanup();
  480.                   display_error(6,(CHARTYPE *)"- width MUST be <= 32700",FALSE);
  481.                   return(5);
  482.                  }
  483.                break;
  484.       case 'u':        /* display length */
  485.                display_length = (unsigned short)atoi(optarg);
  486.                if (display_length == 0)
  487.                  {
  488.                   cleanup();
  489.                   display_error(5,(CHARTYPE *)"- display length MUST be > 0",FALSE);
  490.                   return(4);
  491.                  }
  492.                break;
  493.       case 'h':
  494.       case '?':
  495.                cleanup();
  496.                display_info((CHARTYPE *)argv[0]);
  497.                return(0);
  498.                break;
  499.       default:
  500.                break;
  501.      }
  502.  
  503.  if (optind<argc)
  504.    {
  505.     while(optind<argc)
  506.       {
  507.        if ((current_file_name = add_line(first_file_name,
  508.                                          current_file_name,
  509.                                          strrmdup(strtrans((CHARTYPE *)argv[optind],OSLASH,ISLASH),ISLASH),
  510.                                          strlen(argv[optind]),0,TRUE)) == NULL)
  511.            {
  512.             cleanup();
  513.             display_error(30,(CHARTYPE *)"",FALSE);
  514.             return(6);
  515.            }
  516.        if (first_file_name == NULL)
  517.           first_file_name = current_file_name;
  518.        optind++;
  519.       }
  520.    }
  521.  else
  522.    {
  523. #if defined(MSWIN)
  524.     return(1);
  525. #else
  526.     if ((current_file_name = add_line(first_file_name,
  527.                                       current_file_name,
  528.                                       CURRENT_DIR,
  529.                                       strlen((DEFCHAR *)CURRENT_DIR),0,TRUE)) == NULL)
  530.         {
  531.          cleanup();
  532.          display_error(30,(CHARTYPE *)"",FALSE);
  533.          return(7);
  534.         }
  535.     if (first_file_name == NULL)
  536.        first_file_name = current_file_name;
  537. #endif
  538.    }
  539. /*---------------------------------------------------------------------*/
  540. /* Check any command line conflicts...                                 */
  541. /*---------------------------------------------------------------------*/
  542.  if (display_length > 0
  543.  &&  display_length > max_line_length)
  544.    {
  545.     cleanup();
  546.     display_error(6,(CHARTYPE *)"- width MUST be >= display length",FALSE);
  547.     return(8);
  548.    }
  549. /*---------------------------------------------------------------------*/
  550. /* Override any default paths,filenames etc if supplied on command line*/
  551. /*---------------------------------------------------------------------*/
  552.  if (setup_profile_files(specified_prf) != RC_OK)
  553.    {
  554.     cleanup();
  555.     return(8);
  556.    }
  557.  if (specified_prf != NULL)
  558.     (*the_free)(specified_prf);
  559. /*---------------------------------------------------------------------*/
  560. /* Allocate some memory to working variables...                        */
  561. /*---------------------------------------------------------------------*/
  562.  rc = allocate_working_memory();
  563.  if (rc)
  564.    {
  565.     cleanup();
  566.     display_error(30,(CHARTYPE *)"",FALSE);
  567.     return(rc);
  568.    }
  569. /*---------------------------------------------------------------------*/
  570. /* Allocate memory to pre_rec and set it to blanks.                    */
  571. /*---------------------------------------------------------------------*/
  572.  if ((pre_rec = (CHARTYPE *)(*the_malloc)((MAX_PREFIX_WIDTH+1)*sizeof(CHARTYPE))) == NULL)
  573.    {
  574.     cleanup();
  575.     display_error(30,(CHARTYPE *)"",FALSE);
  576.     return(16);
  577.    }
  578.  memset(pre_rec,' ',MAX_PREFIX_WIDTH+1);
  579.  pre_rec_len = 0;
  580. /*---------------------------------------------------------------------*/
  581. /* Set up saved command buffers to empty...                            */
  582. /*---------------------------------------------------------------------*/
  583.  strcpy((DEFCHAR *)last_target,"");
  584.  strcpy((DEFCHAR *)last_change_command,"");
  585. /*---------------------------------------------------------------------*/
  586. /* Determine the current working directory.                            */
  587. /* Do this before any other file setting up so that we don't change the*/
  588. /* current directory from the default.                                 */
  589. /*---------------------------------------------------------------------*/
  590. #if defined(EMX)
  591.  _getcwd2(curr_path,MAX_FILE_NAME);
  592. #else
  593.  getcwd((DEFCHAR *)curr_path,MAX_FILE_NAME);
  594. #endif
  595.  
  596. #if !defined(MULTIPLE_PSEUDO_FILES)
  597. /*---------------------------------------------------------------------*/
  598. /* Set up filename for directory temporary file (DIR.DIR).             */
  599. /* Set up filename for REXX capture file (REXX.$$$).                   */
  600. /*---------------------------------------------------------------------*/
  601. #ifdef UNIX
  602.  strcpy((DEFCHAR *)dir_pathname,(DEFCHAR *)user_home_dir);
  603. #endif
  604. #if defined(DOS) || defined(OS2) || defined(WIN32)
  605.  strcpy((DEFCHAR *)dir_pathname,(DEFCHAR *)ISTR_SLASH);
  606. #endif
  607. #ifdef VMS
  608.  strcpy((DEFCHAR *)dir_pathname,"");
  609. #endif
  610.  
  611.  strcpy((DEFCHAR *)rexx_pathname,(DEFCHAR *)dir_pathname);
  612.  strcpy((DEFCHAR *)key_pathname,(DEFCHAR *)dir_pathname);
  613.  strcat((DEFCHAR *)dir_pathname,(DEFCHAR *)dirfilename);
  614.  if (splitpath(dir_pathname) != RC_OK)
  615.    {
  616.     cleanup();
  617.     return(18);
  618.    }
  619.  strcpy((DEFCHAR *)dir_pathname,(DEFCHAR *)sp_path);
  620.  strcpy((DEFCHAR *)dir_filename,(DEFCHAR *)sp_fname);
  621.  
  622.  strcat((DEFCHAR *)rexx_pathname,(DEFCHAR *)rexxoutname);
  623.  if (splitpath(rexx_pathname) != RC_OK)
  624.    {
  625.     cleanup();
  626.     return(19);
  627.    }
  628.  strcpy((DEFCHAR *)rexx_pathname,(DEFCHAR *)sp_path);
  629.  strcpy((DEFCHAR *)rexx_filename,(DEFCHAR *)sp_fname);
  630.  
  631.  strcat((DEFCHAR *)key_pathname,(DEFCHAR *)keyfilename);
  632.  if (splitpath(key_pathname) != RC_OK)
  633.    {
  634.     cleanup();
  635.     return(20);
  636.    }
  637.  strcpy((DEFCHAR *)key_pathname,(DEFCHAR *)sp_path);
  638.  strcpy((DEFCHAR *)key_filename,(DEFCHAR *)sp_fname);
  639. #endif
  640. /*---------------------------------------------------------------------*/
  641. /* Set up a temporary file name for output from PUT command to go...   */
  642. /*---------------------------------------------------------------------*/
  643. #if defined(HAVE_BROKEN_TMPNAM)
  644. /*---------------------------------------------------------------------*/
  645. /* Some compliers tmpnam() creates a temporary file name in the current*/
  646. /* directory only, which is not necessarily writeable. We have to work */
  647. /* around this :-(                                                     */
  648. /*---------------------------------------------------------------------*/
  649.  if ((envptr = getenv("TMP")) == NULL)
  650.    {
  651.     if ((envptr = getenv("TEMP")) == NULL)
  652.       {
  653.        if ((envptr = getenv("TMPDIR")) == NULL)
  654.           envptr = "C:";
  655.       }
  656.    }
  657.  if ((tmpptr = tmpnam(NULL)) == NULL)
  658.    {
  659.     cleanup();
  660.     return(31);
  661.    }
  662.  if ((tempfilename = (CHARTYPE *)(*the_malloc)(L_tmpnam+strlen(envptr)+2)) == NULL)
  663.    {
  664.     cleanup();
  665.     return(30);
  666.    }
  667.  strcpy((DEFCHAR *)tempfilename,envptr);
  668.  (void *)strrmdup(strtrans(tempfilename,OSLASH,ISLASH),ISLASH);
  669.  if ((tempfilename[strlen((DEFCHAR *)tempfilename)-1]) != ISLASH)
  670.     strcat((DEFCHAR *)tempfilename,(DEFCHAR *)ISTR_SLASH);
  671.  strcat((DEFCHAR *)tempfilename,tmpptr);
  672. #else
  673.  if ((tempfilename = (CHARTYPE *)(*the_malloc)(L_tmpnam)) == NULL)
  674.    {
  675.     cleanup();
  676.     return(30);
  677.    }
  678.  if ((tempfilename = (CHARTYPE *)tmpnam((DEFCHAR *)tempfilename)) == NULL)
  679.    {
  680.     cleanup();
  681.     return(31);
  682.    }
  683. #endif
  684. /*---------------------------------------------------------------------*/
  685. /* Trap signals to exit gracefully, unless user has specified they not */
  686. /* be trapped.                                                         */
  687. /*---------------------------------------------------------------------*/
  688.  if (trap_signals)
  689.     init_signals();
  690. /*---------------------------------------------------------------------*/
  691. /* Set SCREEN values up...                                             */
  692. /*---------------------------------------------------------------------*/
  693.  for (i=0;i<VIEW_WINDOWS;i++)
  694.      CURRENT_SCREEN.win[i] = (WINDOW *)NULL;
  695. /*---------------------------------------------------------------------*/
  696. /* Set up global defaults.                                             */
  697. /*---------------------------------------------------------------------*/
  698.  set_global_defaults();
  699. /*---------------------------------------------------------------------*/
  700. /* Initialise command array to empty strings.                          */
  701. /*---------------------------------------------------------------------*/
  702.  init_command();
  703. /*---------------------------------------------------------------------*/
  704. /* Initialise rexx support. If no REXX available, set flag...          */
  705. /*---------------------------------------------------------------------*/
  706. #ifndef MSWIN
  707.  rexx_support = TRUE;
  708.  if (initialise_rexx() != RC_OK)
  709.     rexx_support = FALSE;
  710. #endif
  711. /*---------------------------------------------------------------------*/
  712. /* Set up default screens using the default values of terminal_lines   */
  713. /* and terminal_cols. These will be altered after initscr().           */
  714. /*---------------------------------------------------------------------*/
  715.  screen[0].sl = screen[1].sl = NULL;
  716.  if (batch_only)
  717.    {
  718.     set_screen_defaults();
  719. /*---------------------------------------------------------------------*/
  720. /* Read each file into memory and apply the profile file to each of the*/
  721. /* files.                                                              */
  722. /*---------------------------------------------------------------------*/
  723.     current_file_name = first_file_name;
  724.     while(current_file_name != NULL)
  725.       {
  726.       if ((rc = get_file((CHARTYPE *)current_file_name->line)) != RC_OK)
  727.         {
  728.          cleanup();
  729.          if (rc == RC_DISK_FULL)
  730.             display_error(57,(CHARTYPE *)"...probably",FALSE);
  731.          return(21);
  732.         }
  733.       pre_process_line(CURRENT_VIEW,0L,(LINE *)NULL);
  734.       if (execute_profile)
  735.         {
  736.          if (local_prf != (CHARTYPE *)NULL)
  737.             rc = get_profile(local_prf,prf_arg);
  738.          if (error_on_screen)
  739.            {
  740.             pause_for_errors = TRUE;
  741.             error_on_screen = FALSE;
  742.            }
  743. #ifdef MSWIN
  744.          (void)get_user_profile();
  745.          if (error_on_screen)
  746.            {
  747.             pause_for_errors = TRUE;
  748.             error_on_screen = FALSE;
  749.            }
  750. #endif
  751.         }
  752.       current_file_name = current_file_name->next;
  753.       }
  754.     first_file_name = lll_free(first_file_name);
  755. /*---------------------------------------------------------------------*/
  756. /* If THE has been used only in batch, exit here.                      */
  757. /*---------------------------------------------------------------------*/
  758.    if (number_of_files != 0)
  759.      {
  760.       sprintf((DEFCHAR *)rec,"%d",number_of_files);
  761.       display_error(77,rec,FALSE);
  762.      }
  763.    cleanup();
  764.    return(0);
  765.   } /* if (batch_only) */
  766. /*---------------------------------------------------------------------*/
  767. /* If the platform supports the mouse, set up the default commands.    */
  768. /*---------------------------------------------------------------------*/
  769. #if defined(MOUSE_SUPPORT_ENABLED)
  770.  initialise_mouse_commands();
  771. #endif
  772. /*---------------------------------------------------------------------*/
  773. /* Start up curses. This is done ONLY for interactive sessions!        */
  774. /*---------------------------------------------------------------------*/
  775. /* traceon();*/
  776. #if defined(HAVE_SLK_INIT)
  777. # if MAX_SLK == 10
  778.  if (SLKx) slk_init(55);
  779. # else
  780.  if (SLKx) slk_init(1);
  781. # endif
  782. #endif
  783. #if defined(HAVE_SB_INIT)
  784.  if (SBx) sb_init();
  785. #endif
  786.  initscr();
  787.  curses_started = TRUE;
  788. /*---------------------------------------------------------------------*/
  789. /* Save the value of LINES and COLS and use these for all screen       */
  790. /* sizing calculations. This is because BSD scrolls if a character is  */
  791. /* displayed in the bottom right corner of the screen :-(              */
  792. /*---------------------------------------------------------------------*/
  793.  terminal_lines=LINES;
  794.  terminal_cols=COLS;
  795. #ifdef HAVE_BSD_CURSES
  796.  terminal_lines--;
  797. #endif
  798. /*---------------------------------------------------------------------*/
  799. /* Determine if colour support available.                              */
  800. /*---------------------------------------------------------------------*/
  801.  if (colour_support) /* if default setting not overridden on command line */
  802.    {
  803.     colour_support = FALSE;
  804. #ifdef A_COLOR
  805.     if (has_colors())
  806.       {
  807.        start_color();
  808.        colour_support = TRUE;
  809.        init_colour_pairs();
  810.       }
  811. #endif
  812.    }
  813. /*---------------------------------------------------------------------*/
  814. /* Set various terminal characteristics...                             */
  815. /*---------------------------------------------------------------------*/
  816.  cbreak();
  817.  raw();
  818. #if defined(USE_EXTCURSES)
  819.  extended(FALSE);
  820. #endif
  821. #if defined(PDCURSES)
  822.  raw_output(TRUE);
  823. #endif
  824.  nonl();
  825.  noecho();
  826.  keypad(stdscr,TRUE);
  827.  notimeout(stdscr,TRUE);
  828.  (void)THETypeahead((CHARTYPE *)"OFF");
  829. /*---------------------------------------------------------------------*/
  830. /* Set up mouse support if enabled in curses library.                  */
  831. /*---------------------------------------------------------------------*/
  832. #if defined(MOUSE_SUPPORT_ENABLED)
  833.  mouse_set(ALL_MOUSE_EVENTS);
  834. #endif
  835. /*---------------------------------------------------------------------*/
  836. /* Set up variables and values dependent on LINES and COLS now with    */
  837. /* values set by initscr().                                            */
  838. /*---------------------------------------------------------------------*/
  839.  set_screen_defaults();
  840. #if 0
  841. /*---------------------------------------------------------------------*/
  842. /* For each file being edited, set up the curses windows for each of  */
  843. /* them.                                                               */
  844. /*---------------------------------------------------------------------*/
  845.  if (set_up_windows(0) != RC_OK)
  846.    {
  847.     cleanup();
  848.     return(22);
  849.    }
  850. #endif
  851. #if 0
  852.  CURRENT_VIEW = CURRENT_SCREEN.screen_view = vd_first;
  853. #endif
  854.  
  855. #if defined(HAVE_BROKEN_SYSVR4_CURSES)
  856.  force_curses_background();
  857.  refresh();
  858. #endif
  859. /*---------------------------------------------------------------------*/
  860. /* wnoutrefresh() is called here so that the first call to getch() on  */
  861. /* stdscr does not clear the screen.                                   */
  862. /*---------------------------------------------------------------------*/
  863.  wnoutrefresh(stdscr);
  864. #if defined(HAVE_SLK_INIT)
  865.  if (SLKx) slk_noutrefresh();
  866. #endif
  867. /*---------------------------------------------------------------------*/
  868. /* Create the statusline window...                                     */
  869. /*---------------------------------------------------------------------*/
  870.  if (create_statusline_window() != RC_OK)
  871.    {
  872.     cleanup();
  873.     display_error(0,(CHARTYPE *)"creating status line window",FALSE);
  874.     return(23);
  875.    }
  876. /*---------------------------------------------------------------------*/
  877. /* Set up ETMODE tables...                                             */
  878. /*---------------------------------------------------------------------*/
  879. #if defined(DOS) || defined(OS2)  || defined(WIN32)
  880.  (void)Etmode((CHARTYPE *)"ON");
  881. #elif defined(XCURSES)
  882.  (void)Etmode((CHARTYPE *)"ON 32-255");
  883. #elif defined(UNIX)
  884.  (void)Etmode((CHARTYPE *)"OFF");
  885. #endif
  886. /*---------------------------------------------------------------------*/
  887. /* Read each file into memory and apply the profile file to each of the*/
  888. /* files.                                                              */
  889. /*---------------------------------------------------------------------*/
  890.  current_file_name = first_file_name;
  891.  while(current_file_name != NULL)
  892.    {
  893.     rc = Xedit((CHARTYPE *)current_file_name->line);
  894.     if (rc != RC_OK)
  895.       {
  896.        cleanup();
  897.        return(24);
  898.       }
  899.     current_file_name = current_file_name->next;
  900.    }
  901.  first_file_name = lll_free(first_file_name);
  902. /*---------------------------------------------------------------------*/
  903. /* If THE has only been used to process a profile file, then exit.     */
  904. /*---------------------------------------------------------------------*/
  905.  if (number_of_files == 0)
  906.    {
  907.     cleanup();
  908.     return(0);
  909.    }
  910. /*---------------------------------------------------------------------*/
  911. /* We are no longer in the profile status.                             */
  912. /*---------------------------------------------------------------------*/
  913.  in_profile = FALSE;
  914.  been_interactive = TRUE;
  915. /*---------------------------------------------------------------------*/
  916. /* This is where it all happens.                                       */
  917. /*---------------------------------------------------------------------*/
  918.  editor();
  919. /*---------------------------------------------------------------------*/
  920. /* Finalise rexx support...                                            */
  921. /*---------------------------------------------------------------------*/
  922. #ifndef MSWIN
  923.  finalise_rexx();
  924. #endif
  925. /*---------------------------------------------------------------------*/
  926. /* Free up the dynamically allocated memory.                           */
  927. /*---------------------------------------------------------------------*/
  928.  if (first_define != NULL)
  929.     first_define = dll_free(first_define);
  930.  if (first_prefix_synonym != NULL)
  931.     first_prefix_synonym = lll_free(first_prefix_synonym);
  932.  if (first_mouse_define != NULL)
  933.     first_define = dll_free(first_mouse_define);
  934.  (*the_free)(rec);
  935.  (*the_free)(trec);
  936.  (*the_free)(cmd_rec);
  937.  (*the_free)(pre_rec);
  938.  if (profile_command_line != NULL)
  939.    (*the_free)(profile_command_line);
  940.  if (screen[0].sl != NULL)
  941.     (*the_free)(screen[0].sl);
  942.  if (screen[1].sl != NULL)
  943.     (*the_free)(screen[1].sl);
  944. /*---------------------------------------------------------------------*/
  945. /* Free memory for temp_params and tmp_cmd                             */
  946. /*---------------------------------------------------------------------*/
  947.  free_temp_space(TEMP_PARAM);
  948.  free_temp_space(TEMP_MACRO);
  949.  free_temp_space(TEMP_TEMP_CMD);
  950.  free_temp_space(TEMP_TMP_CMD);
  951.  
  952.  if (local_prf != NULL)
  953.     (*the_free)(local_prf);
  954.  if (prf_arg != NULL)
  955.     (*the_free)(prf_arg);
  956.  free_recovery_list();
  957.  
  958.  if (target_buffer != NULL)
  959.     (*the_free)(target_buffer);
  960. #if defined(UNIX) || defined(OS2) || defined(EMX)
  961.  (*the_free)(spooler_name);
  962. #endif
  963.  
  964.  if (divider != (WINDOW *)NULL)
  965.    {
  966.     delwin(divider);
  967.     divider = (WINDOW *)NULL;
  968.    }
  969.  if (error_window != (WINDOW *)NULL)
  970.    {
  971.     delwin(error_window);
  972.     error_window = (WINDOW *)NULL;
  973.    }
  974.  if (last_message != NULL)
  975.     (*the_free)(last_message);
  976. /*---------------------------------------------------------------------*/
  977. /* If the user wants a clearscreen done before exiting, do it...       */
  978. /*---------------------------------------------------------------------*/
  979.  if (CLEARSCREENx)
  980.    {
  981.     wclear(stdscr);
  982.     move(0,0);
  983.     attrset(A_NORMAL);
  984.     refresh();
  985.    }
  986.  else
  987. /*---------------------------------------------------------------------*/
  988. /* ...otherwise, get the cursor to the bottom line.                    */
  989. /*---------------------------------------------------------------------*/
  990.    {
  991.     if (statarea != (WINDOW *)NULL)
  992.       {
  993.        mvwaddstr(statarea,0,4,"     ");
  994.        wattrset(statarea,A_NORMAL);
  995.        mvwaddstr(statarea,0,0,"THE - END");
  996.        wrefresh(statarea);
  997.       }
  998.    }
  999.  if (statarea != (WINDOW *)NULL)
  1000.    {
  1001.     delwin(statarea);
  1002.     statarea = (WINDOW *)NULL;
  1003.    }
  1004. #ifdef MSWIN
  1005.  Win31Cleanup();
  1006.  return(0);
  1007. #endif
  1008.  cleanup();
  1009.  return(0);
  1010. }
  1011. /***********************************************************************/
  1012. #ifdef HAVE_PROTO
  1013. static void init_signals(void)
  1014. #else
  1015. static void init_signals()
  1016. #endif
  1017. /***********************************************************************/
  1018. {
  1019. /*--------------------------- local data ------------------------------*/
  1020. /*--------------------------- processing ------------------------------*/
  1021. #ifdef TRACE
  1022.  trace_function("the.c:     init_signals");
  1023. #endif
  1024. #ifdef UNIX
  1025.  signal(SIGQUIT,handle_signal);
  1026.  signal(SIGHUP,handle_signal);
  1027.  signal(SIGABRT,handle_signal);
  1028.  signal(SIGFPE,handle_signal);
  1029.  signal(SIGSEGV,handle_signal);
  1030.  signal(SIGINT,handle_signal);
  1031.  signal(SIGTERM,handle_signal);
  1032. # if defined(SIGBUS)
  1033.  signal(SIGBUS,handle_signal);
  1034. # endif
  1035. #endif
  1036. #ifdef TRACE
  1037.  trace_return();
  1038. #endif
  1039.  return;
  1040. }
  1041. /***********************************************************************/
  1042. #ifdef HAVE_PROTO
  1043. void init_colour_pairs(void)
  1044. #else
  1045. void init_colour_pairs()
  1046. #endif
  1047. /***********************************************************************/
  1048. {
  1049. /*--------------------------- local data ------------------------------*/
  1050.  register int fg=0,bg=0;
  1051. /*--------------------------- processing ------------------------------*/
  1052. #ifdef TRACE
  1053.  trace_function("the.c:     init_colour_pairs");
  1054. #endif
  1055.  
  1056. #ifdef A_COLOR
  1057.  for (fg=0;fg<COLORS;fg++)
  1058.    {
  1059.     for (bg=0;bg<COLORS;bg++)
  1060.       {
  1061.        if (ATTR2PAIR(fg,bg) <= COLOR_PAIRS)
  1062.           init_pair(ATTR2PAIR(fg,bg),fg,bg);
  1063.       }
  1064.    }
  1065. #endif
  1066.  
  1067. #ifdef TRACE
  1068.  trace_return();
  1069. #endif
  1070.  return;
  1071. }
  1072. /***********************************************************************/
  1073. #ifdef HAVE_PROTO
  1074. int setup_profile_files(CHARTYPE *specified_prf)
  1075. #else
  1076. int setup_profile_files(specified_prf)
  1077. CHARTYPE *specified_prf;
  1078. #endif
  1079. /***********************************************************************/
  1080. {
  1081. /*--------------------------- local data ------------------------------*/
  1082.  int rc=RC_OK;
  1083.  char *envptr=NULL;
  1084. /*--------------------------- processing ------------------------------*/
  1085. /*---------------------------------------------------------------------*/
  1086. /* If a profile specified on the command line, set it up as the local  */
  1087. /* profile. It MUST exist and be readable, otherwise an error.         */
  1088. /*---------------------------------------------------------------------*/
  1089.  if (specified_prf != (CHARTYPE *)NULL)
  1090.    {
  1091.     if ((local_prf = (CHARTYPE *)(*the_malloc)((MAX_FILE_NAME+1)*sizeof(CHARTYPE))) == NULL)
  1092.        return(RC_OUT_OF_MEMORY);
  1093.     strcpy((DEFCHAR *)local_prf,(DEFCHAR *)specified_prf);
  1094.     if (!file_exists(local_prf))
  1095.       {
  1096.        display_error(9,local_prf,FALSE);
  1097.        return(RC_FILE_NOT_FOUND);
  1098.       }
  1099. /*---------------------------------------------------------------------*/
  1100. /* If the file is not readable, error.                                 */
  1101. /*---------------------------------------------------------------------*/
  1102.     if (!file_readable(local_prf))
  1103.       {
  1104.        display_error(8,local_prf,FALSE);
  1105.        return(RC_ACCESS_DENIED);
  1106.       }
  1107.     return(rc);
  1108.    }
  1109. /*---------------------------------------------------------------------*/
  1110. /* If a profile specified with THE_PROFILE_FILE, set it up as the local*/
  1111. /* profile. It does not have to exist.                                 */
  1112. /*---------------------------------------------------------------------*/
  1113.  if ((envptr = getenv("THE_PROFILE_FILE")) != NULL)
  1114.    {
  1115.     if ((local_prf = (CHARTYPE *)(*the_malloc)((MAX_FILE_NAME+1)*sizeof(CHARTYPE))) == NULL)
  1116.        return(RC_OUT_OF_MEMORY);
  1117.     strcpy((DEFCHAR *)local_prf,envptr);
  1118.     return(rc);
  1119.    }
  1120. /*---------------------------------------------------------------------*/
  1121. /* No specific profile, so check for default profiles.                 */
  1122. /*---------------------------------------------------------------------*/
  1123.  if ((local_prf = (CHARTYPE *)(*the_malloc)((MAX_FILE_NAME+1)*sizeof(CHARTYPE))) == NULL)
  1124.     return(RC_OUT_OF_MEMORY);
  1125. /*---------------------------------------------------------------------*/
  1126. /* For Unix, use a local profile first...                              */
  1127. /*---------------------------------------------------------------------*/
  1128. #if defined(UNIX)
  1129.  strcpy((DEFCHAR *)local_prf,(DEFCHAR *)user_home_dir);
  1130.  strcat((DEFCHAR *)local_prf,(DEFCHAR *)THE_PROFILE_FILE);
  1131.  (void *)strrmdup(strtrans(local_prf,OSLASH,ISLASH),ISLASH);
  1132.  if (file_readable(local_prf))
  1133.     return(rc);
  1134. #endif
  1135. /*---------------------------------------------------------------------*/
  1136. /* If no local profile, see if a global profile exists...              */
  1137. /*---------------------------------------------------------------------*/
  1138.  strcpy((DEFCHAR *)local_prf,(DEFCHAR *)the_home_dir);
  1139.  strcat((DEFCHAR *)local_prf,(DEFCHAR *)THE_PROFILE_FILE);
  1140.  (void *)strrmdup(strtrans(local_prf,OSLASH,ISLASH),ISLASH);
  1141.  if (file_readable(local_prf))
  1142.     return(rc);
  1143. /*---------------------------------------------------------------------*/
  1144. /* To get here, no profile files to be executed.                       */
  1145. /*---------------------------------------------------------------------*/
  1146.  (*the_free)(local_prf);
  1147.  local_prf = (CHARTYPE *)NULL;
  1148.  return(rc);
  1149. }
  1150. /***********************************************************************/
  1151. #ifdef HAVE_PROTO
  1152. static void display_info(CHARTYPE *argv0)
  1153. #else
  1154. static void display_info(argv0)
  1155. CHARTYPE *argv0;
  1156. #endif
  1157. /***********************************************************************/
  1158. {
  1159. /*--------------------------- local data ------------------------------*/
  1160. /*--------------------------- processing ------------------------------*/
  1161.  
  1162.  fprintf(stderr,"\nTHE %s %2s %s. All rights reserved.\n",the_version,the_release,the_copyright);
  1163.  fprintf(stderr,"THE is distributed under the terms of the GNU General Public License \n");
  1164.  fprintf(stderr,"and comes with NO WARRANTY. See the file COPYING for details.\n");
  1165.  fprintf(stderr,"\nUsage:\n\n%s [-h?nmrsbk] [-p profile] [-a profile_arg] [-w width] [-u display_length] [[dir] [file [...]]]\n",argv0);
  1166.  fprintf(stderr,"\nwhere:\n\n");
  1167.  fprintf(stderr,"-h,-?                  show this message\n");
  1168.  fprintf(stderr,"-n                     do not execute a profile file\n");
  1169.  fprintf(stderr,"-m                     force display into mono\n");
  1170.  fprintf(stderr,"-r                     run THE in read-only mode\n");
  1171.  fprintf(stderr,"-s                     turn off signal trapping (Unix only)\n");
  1172.  fprintf(stderr,"-b                     run in batch mode\n");
  1173.  fprintf(stderr,"-k                     allow Soft Label Key display\n");
  1174.  fprintf(stderr,"-p profile             filename of profile file\n");
  1175.  fprintf(stderr,"-a profile_arg         argument(s) to profile file (only with REXX)\n");
  1176.  fprintf(stderr,"-w width               maximum width of line (default 512)\n");
  1177.  fprintf(stderr,"-u display_length      display length in non-line mode\n");
  1178.  fprintf(stderr,"[dir [file [...]]]     file(s) and/or directory to be edited\n\n");
  1179.  fflush(stderr);
  1180.  return;
  1181. }
  1182. /***********************************************************************/
  1183. #ifdef HAVE_PROTO
  1184. int allocate_working_memory(void)
  1185. #else
  1186. int allocate_working_memory()
  1187. #endif
  1188. /***********************************************************************/
  1189. {
  1190. /*-------------------------- external data ----------------------------*/
  1191. /*--------------------------- local data ------------------------------*/
  1192. /*--------------------------- processing ------------------------------*/
  1193. #ifdef TRACE
  1194.  trace_function("the.c:     allocate_working_memory");
  1195. #endif
  1196. /*---------------------------------------------------------------------*/
  1197. /* Allocate some memory to rec.                                        */
  1198. /*---------------------------------------------------------------------*/
  1199.  if (rec == NULL)
  1200.     rec = (CHARTYPE *)(*the_malloc)((max_line_length+5)*sizeof(CHARTYPE));
  1201.  else
  1202.     rec = (CHARTYPE *)(*the_realloc)(rec,(max_line_length+5)*sizeof(CHARTYPE));
  1203.  if (rec == NULL)
  1204.     return(10);
  1205. /*---------------------------------------------------------------------*/
  1206. /* Allocate some memory to trec; buffer for reading files.             */
  1207. /*---------------------------------------------------------------------*/
  1208.  trec_len = max((LENGTHTYPE)(30*80),(LENGTHTYPE)((max_line_length+2)*2));
  1209.  if (trec == NULL)
  1210.     trec = (CHARTYPE *)(*the_malloc)(trec_len*sizeof(CHARTYPE));
  1211.  else
  1212.     trec = (CHARTYPE *)(*the_realloc)(trec,trec_len*sizeof(CHARTYPE));
  1213.  if (trec == NULL)
  1214.     return(11);
  1215. /*---------------------------------------------------------------------*/
  1216. /* Allocate memory to cmd_rec and set it to blanks.                    */
  1217. /*---------------------------------------------------------------------*/
  1218.  if (cmd_rec == NULL)
  1219.     cmd_rec = (CHARTYPE *)(*the_malloc)((max_line_length+2)*sizeof(CHARTYPE));
  1220.  else
  1221.     cmd_rec = (CHARTYPE *)(*the_realloc)(cmd_rec,(max_line_length+2)*sizeof(CHARTYPE));
  1222.  if (cmd_rec == NULL)
  1223.     return(12);
  1224.  memset(cmd_rec,' ',max_line_length);
  1225.  cmd_rec_len = 0;
  1226. /*---------------------------------------------------------------------*/
  1227. /* Allocate some memory for temporary space...                         */
  1228. /*---------------------------------------------------------------------*/
  1229.  if (allocate_temp_space(max_line_length,TEMP_PARAM) != RC_OK)
  1230.     return(13);
  1231.  if (allocate_temp_space(max_line_length,TEMP_TMP_CMD) != RC_OK)
  1232.     return(14);
  1233.  if (allocate_temp_space(max_line_length,TEMP_TEMP_CMD) != RC_OK)
  1234.     return(15);
  1235. /*---------------------------------------------------------------------*/
  1236. /* Allocate some memory to profile_command_line. (only really need to  */
  1237. /* do this if NO REXX support).                                        */
  1238. /*---------------------------------------------------------------------*/
  1239.  if (profile_command_line == NULL)
  1240.     profile_command_line = (CHARTYPE *)(*the_malloc)((max_line_length+2)*sizeof(CHARTYPE));
  1241.  else
  1242.     profile_command_line = (CHARTYPE *)(*the_realloc)(profile_command_line,(max_line_length+2)*sizeof(CHARTYPE));
  1243.  if (profile_command_line == NULL)
  1244.     return(17);
  1245. return(0);
  1246. }
  1247.  
  1248. /***********************************************************************/
  1249. #ifdef HAVE_PROTO
  1250. void cleanup(void)
  1251. #else
  1252. void cleanup()
  1253. #endif
  1254. /***********************************************************************/
  1255. {
  1256. /*-------------------------- external data ----------------------------*/
  1257. #ifndef XCURSES
  1258. extern bool CLEARSCREENx;
  1259. #endif
  1260. extern bool INSERTMODEx;
  1261. /*--------------------------- local data ------------------------------*/
  1262. /*--------------------------- processing ------------------------------*/
  1263. #ifdef TRACE
  1264.  trace_function("the.c:     cleanup");
  1265. #endif
  1266.  
  1267.  if (curses_started)
  1268.    {
  1269. #ifdef XCURSES
  1270.     if (error_on_screen)
  1271.       {
  1272.        display_error(0,(CHARTYPE *)HIT_ANY_KEY,FALSE);
  1273.        wrefresh(error_window);
  1274.        (void)getch();
  1275.       }
  1276. #else
  1277.     if (error_on_screen)
  1278.        wrefresh(error_window);
  1279. #endif
  1280.     INSERTMODEx=FALSE;
  1281.     draw_cursor(TRUE);
  1282. #ifdef HAVE_BSD_CURSES
  1283.     nl();
  1284.     echo();
  1285. #endif
  1286.     endwin();
  1287. #ifdef XCURSES
  1288.     XCursesExit();
  1289. #endif
  1290.     curses_started = FALSE;
  1291.    }
  1292. #ifndef XCURSES
  1293.  if (!CLEARSCREENx
  1294.  && been_interactive)
  1295.     printf("\n");
  1296. #endif
  1297.  if (tempfilename)
  1298.    {
  1299.     if (file_exists(tempfilename))
  1300.        remove_file(tempfilename);
  1301.     (*the_free)(tempfilename);
  1302.    }
  1303.  /*
  1304.   * Free up the working memory
  1305.   */
  1306.  the_free_flists();
  1307. #ifdef TRACE
  1308.  trace_return();
  1309. #endif
  1310.  return;
  1311. }
  1312.  
  1313. #ifdef UNIX
  1314. /***********************************************************************/
  1315. #ifdef HAVE_PROTO
  1316. static RETSIGTYPE handle_signal(int err)
  1317. #else
  1318. static RETSIGTYPE handle_signal(err)
  1319. int err;
  1320. #endif
  1321. /***********************************************************************/
  1322. {
  1323. /*--------------------------- local data ------------------------------*/
  1324.  FILE_DETAILS *cf;
  1325.  VIEW_DETAILS *curr;
  1326.  bool process_view=FALSE;
  1327.  FILE_DETAILS *first_view_file=NULL;
  1328.  register int j=0;
  1329. /*--------------------------- processing ------------------------------*/
  1330. #ifdef TRACE
  1331.  trace_function("the.c:     handle_signal");
  1332. #endif
  1333. /*
  1334.  * For each file in the ring, execute an AUTOSAVE on it and then
  1335.  * die.
  1336.  */
  1337.  if (curses_started)
  1338.    {
  1339.     endwin();
  1340. #ifdef XCURSES
  1341.     XCursesExit();
  1342. #endif
  1343.     curses_started = FALSE;
  1344.    }
  1345.  fprintf(stderr,"\nTHE terminated with signal: %d\n\n",err);
  1346.  curr = vd_current;
  1347.  for (j=0;j<number_of_files;)
  1348.    {
  1349.     process_view = TRUE;
  1350.     if (curr->file_for_view->file_views > 1)
  1351.       {
  1352.        if (first_view_file == curr->file_for_view)
  1353.           process_view = FALSE;
  1354.        else
  1355.           first_view_file = curr->file_for_view;
  1356.       }
  1357.     if (process_view)
  1358.       {
  1359.        j++;
  1360.        cf = curr->file_for_view;
  1361.        if (!cf->pseudo_file)
  1362.          {
  1363.           fprintf(stderr,"Attempting to autosave: %s%s\n",cf->fpath,cf->fname);
  1364.           save_file(cf,cf->autosave_fname,TRUE,cf->number_lines,1L,NULL,FALSE,0,max_line_length,TRUE,FALSE);
  1365.          }
  1366.       }
  1367.     curr = curr->next;
  1368.     if (curr == NULL)
  1369.        curr = vd_first;
  1370.    }
  1371. /*
  1372.  * Lets not push our luck in the signal handler, and just die...
  1373.  */
  1374.  exit(25);
  1375. }
  1376. #endif
  1377.