home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / stex2-18.zip / SeeTeX / Xtex / dialog.c < prev    next >
C/C++ Source or Header  |  1992-05-20  |  7KB  |  285 lines

  1. /*
  2.  * Copyright 1989 Dirk Grunwald
  3.  * 
  4.  * Permission to use, copy, modify, distribute, and sell this software
  5.  * and its documentation for any purpose is hereby granted without fee,
  6.  * provided that the above copyright notice appear in all copies and that
  7.  * both that copyright notice and this permission notice appear in
  8.  * supporting documentation, and that the name of Dirk Grunwald or M.I.T.
  9.  * not be used in advertising or publicity pertaining to distribution of
  10.  * the software without specific, written prior permission.  Dirk
  11.  * Grunwald and M.I.T. makes no representations about the suitability of
  12.  * this software for any purpose.  It is provided "as is" without express
  13.  * or implied warranty.
  14.  * 
  15.  * DIRK GRUNWALD AND M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17.  * FITNESS, IN NO EVENT SHALL M.I.T.  BE LIABLE FOR ANY SPECIAL, INDIRECT
  18.  * OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  19.  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  20.  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  21.  * OR PERFORMANCE OF THIS SOFTWARE.
  22.  * 
  23.  * Author:
  24.  *     Dr. Dirk Grunwald
  25.  *     Dept. of Computer Science
  26.  *     Campus Box 430
  27.  *     Univ. of Colorado, Boulder
  28.  *     Boulder, CO 80309
  29.  * 
  30.  *     grunwald@colorado.edu
  31.  *     
  32.  */ 
  33.  
  34. /*
  35.  * Print an error message with an optional system error number, and
  36.  * optionally quit.
  37.  *
  38.  */
  39.  
  40. #include <stdio.h>
  41. #include <varargs.h>
  42. #include <string.h>
  43. #include <X11/IntrinsicP.h>
  44. #include <X11/StringDefs.h>
  45. #include <X11/ShellP.h>
  46.  
  47. #include <X11/Xaw/AsciiText.h>
  48.  
  49. #include "xtex.h"
  50. #include "widgets.h"
  51.  
  52. #ifdef __STDC__
  53. /*
  54. extern char* XtMalloc(int);
  55. */
  56. extern char* realloc(char *, int);
  57. #else
  58. extern char* malloc();
  59. extern char* realloc();
  60. #endif
  61.  
  62. #define ERROR_BUFFER_SIZE 2048
  63. static char errorMessageBuffer[ ERROR_BUFFER_SIZE ];
  64.  
  65. #if defined(VSPRINTF) || defined( vsprintf ) || defined(sun) || defined(hpux) || defined(ultrix)
  66. #  define VSPRINTF
  67. #else
  68. #  undef  VSPRINTF
  69. #endif
  70.  
  71. void
  72.   DialogMessage(ipStr, addNewLine)
  73. char *ipStr;
  74. int addNewLine;
  75. {
  76.   /*
  77.    * I had problems dealing with string constants as messages ---
  78.    * one can't add a newline to them. So I malloced space.
  79.    * Shankar 04 / 20 / 1992
  80.    */
  81.   if ( errorText ) {
  82.     char *str;
  83.     int len = strlen(ipStr);
  84.     char *p;
  85.     char *currentString;
  86.  
  87.     Arg argList[20];
  88.     Cardinal args;
  89.  
  90.     XawTextPosition start;
  91.     XawTextBlock tblk, empty_tblk;
  92.     
  93.     if ( len <= 0 )
  94.       return;
  95.     
  96.     str = XtMalloc ((len + 2) * sizeof (char));
  97.     strcpy (str, ipStr);
  98.     
  99.     tblk.firstPos = 0;
  100.     tblk.length = len;
  101.     tblk.ptr = str;
  102.     tblk.format = FMT8BIT;
  103.     
  104.     if ( len > 0 && addNewLine && str[len-1] != '\n' ) {
  105.       str[len] = '\n';
  106.       tblk.length = len+1;
  107.       str[tblk.length] = 0;
  108.     }
  109.     
  110.     /* empty text block for deletion */
  111.     empty_tblk.firstPos = 0;
  112.     empty_tblk.length = 0;
  113.     empty_tblk.ptr = "";
  114.     empty_tblk.format = FMT8BIT;
  115.  
  116.     args = 0;
  117.     XtSetArg(argList[args], XtNstring, ¤tString); args++;
  118.     XtGetValues(errorText, argList, args);
  119.     
  120.     start = strlen( currentString );
  121.     
  122.     XawTextDisableRedisplay(errorText); 
  123.  
  124.     while ( start + len > ERROR_BUFFER_SIZE ) {
  125.       
  126.       /* find first line and delete it */
  127.       
  128.       char *eol = strchr( currentString, '\n');
  129.       
  130.       if ( eol != 0 && eol < ¤tString[ERROR_BUFFER_SIZE] ) {
  131.     XawTextReplace( errorText, 0, eol-currentString+1, &empty_tblk );
  132.         start -= eol - currentString + 1;
  133.       }
  134.       else {
  135.     XawTextReplace( errorText, 0, start, &empty_tblk );
  136.     start = 0;
  137.     break;
  138.       }
  139.     }
  140.     
  141.     XawTextReplace( errorText, start, start, &tblk );
  142.     XawTextSetInsertionPoint(errorText, start + len);
  143.     XawTextEnableRedisplay(errorText);
  144.  
  145.     XtFree (str);
  146.   }
  147. }
  148.  
  149. static void
  150.   pushClearButton(w, clientData,  callData)
  151. Widget w;
  152. caddr_t clientData;    /* unused */
  153. caddr_t callData; /* unused */
  154. {
  155.   bzero( errorMessageBuffer , ERROR_BUFFER_SIZE );
  156.   
  157.   XawTextSetInsertionPoint(errorText, 0);
  158.   XawTextEnableRedisplay(errorText);
  159.  
  160. void
  161.   BuildErrorBox()
  162. {
  163.   Arg argList[20];
  164.   Cardinal args;
  165.   
  166.   if (xtexResources.helpText) {
  167.     strncpy(errorMessageBuffer, xtexResources.helpText, ERROR_BUFFER_SIZE-1);
  168.   }
  169.   
  170.   args = 0;
  171.   
  172.   XtSetArg(argList[args], XtNeditType, (XtArgVal) XawtextEdit); args++;
  173.   XtSetArg(argList[args], XtRAsciiType, XawAsciiString); args++;
  174.   
  175.   XtSetArg(argList[args], XtNstring, (XtArgVal) errorMessageBuffer); args++;
  176.   XtSetArg(argList[args], XtNlength, (XtArgVal) ERROR_BUFFER_SIZE); args++;
  177.   
  178.   errorText = 
  179.     XtCreateManagedWidget("dialogText",
  180.               asciiTextWidgetClass, topPane,
  181.               argList, args);
  182. }
  183.  
  184.  
  185. #ifdef lint
  186.  
  187. /* VARARGS3 ARGSUSED */
  188. error(quit, e, fmt) int quit, e; char *fmt; {;}
  189.      
  190.      /* VARARGS1 ARGSUSED */
  191.      panic(fmt) char *fmt; { exit(1); /* NOTREACHED */ }
  192.      
  193. #else lint
  194.      
  195.      extern char *ProgName;
  196.      extern int errno;
  197.      extern char *sys_errlist[];
  198.      extern int sys_nerr;
  199.      
  200. #define ERROR_MSG_LEN 1024
  201.      static char ErrorMsgBuffer[ ERROR_MSG_LEN ];
  202.      
  203.      error(va_alist)
  204.      va_dcl
  205. {
  206.   va_list l;
  207.   int quit, e;
  208.   char *fmt;
  209.   char *p;
  210.   
  211.   p = ErrorMsgBuffer;
  212.   
  213.   va_start(l);
  214.   /* pick up the constant arguments: quit, errno, printf format */
  215.   quit = va_arg(l, int);
  216.   e = va_arg(l, int);
  217.   if (e < 0)
  218.     e = errno;
  219.   
  220.   fmt = va_arg(l, char *);
  221.   
  222. #if defined(VSPRINTF)
  223.   (void) vsprintf(p, fmt, l);
  224. #else
  225.   /*    Does someone know how to do this properly? */
  226.   bzero(p, ERROR_MSG_LEN);
  227.   
  228.   {
  229.     struct _iobuf ungodlyhack;
  230.     ungodlyhack._cnt = ERROR_MSG_LEN;
  231.     ungodlyhack._ptr = p;
  232.     ungodlyhack._base = p;
  233.     ungodlyhack._bufsiz = ERROR_MSG_LEN;
  234.     ungodlyhack._flag = _IOSTRG;
  235.     ungodlyhack._file = 255;
  236.     _doprnt(fmt, l, &ungodlyhack);
  237.   }
  238. #endif
  239.   
  240.   va_end(l);
  241.   
  242.   DialogMessage( ErrorMsgBuffer, 1);
  243. }
  244.  
  245. panic(va_alist)
  246.      va_dcl
  247. {
  248.   va_list l;
  249.   char *fmt;
  250.   char *p;
  251.   
  252.   p = ErrorMsgBuffer;
  253.   sprintf(p, "panic: ");
  254.   p += strlen(p);
  255.   
  256.   va_start(l);
  257.   /* pick up the constant arguments: quit, errno, printf format */
  258.   fmt = va_arg(l, char *);
  259.   
  260. #if defined(VSPRINTF)
  261.   (void) vsprintf(p, fmt, l);
  262. #else
  263.   /*    Does someone know how to do this properly? */
  264.   
  265.   {
  266.     struct _iobuf ungodlyhack;
  267.     ungodlyhack._cnt = ERROR_MSG_LEN;
  268.     ungodlyhack._ptr = p;
  269.     ungodlyhack._base = p;
  270.     ungodlyhack._bufsiz = ERROR_MSG_LEN;
  271.     ungodlyhack._flag = _IOSTRG;
  272.     ungodlyhack._file = 255;
  273.     _doprnt(fmt, l, &ungodlyhack);
  274.   }
  275. #endif
  276.   
  277.   va_end(l);
  278.   
  279.   DialogMessage(ErrorMsgBuffer, 1);
  280.   exit(1);
  281. }
  282.  
  283. #endif /* lint */
  284.