📄 minedaux.c
字号:
/* ================================================================== * * Editor mined * * auxiliary part * * ================================================================== */#include "mined.h"int panic_level = 0; /* To adjust error handling to situation */FLAG pagewrapped = FALSE; /* Did output on the bottom line wrap and scroll? *//* ================================================================== * * Auxiliary routines * * ================================================================== *//* * Delete file. */voiddelete_file (file) char * file;{#ifdef unix unlink (file);#endif#ifdef msdos unlink (file);#endif#ifdef vms delete (file);#endif}/* * Delete yank file if there is one. */voiddelete_yank_file (){ if (yank_status == VALID) delete_file (yank_file);}/* * Panic () is called with a mined error msg and an optional system error msg. * It is called when something unrecoverable has happened. * It writes the message to the terminal, resets the tty and exits. * Ask the user if he wants to save his file. */#define panic_msg(msg) if (isscreenmode == TRUE) {status_msg (msg); sleep (2);} else (void) printf ("%s\n", msg);voidpanicking (message, err, signum) register char * message; register char * err; register int signum;{ int panic_written; void QUED (); panic_level ++; if (panic_level < 2) { if (loading == FALSE && modified == TRUE) { panic_written = panicwrite (); if (panic_written == ERRORS) { build_string (text_buffer, "Error writing panic file %s", panic_file); sleep (2); } else build_string (text_buffer, "Panic file %s written", panic_file); ring_bell (); panic_msg (text_buffer); } if (signum != 0) build_string (text_buffer, message, signum); else if (err == NIL_PTR) build_string (text_buffer, "%s", message); else build_string (text_buffer, "%s (Error: %s)", message, err); panic_msg (text_buffer); /* "normal" panic handling: */ if (loading == FALSE) { QUED (); /* Try to save the file and quit */ /* QUED returned: something wrong */ sleep (2); panic_msg ("Aborted writing file in panic mode - trying to continue"); panic_level --; return; } } if (panic_level < 3) { if (isscreenmode == TRUE) { set_cursor (0, YMAX); putchar ('\n'); raw_mode (OFF); } delete_yank_file (); } exit (1) /* abort () sends IOT which would again be caught */;}voidpanic (message, err) register char * message; register char * err;{ panicking (message, err, 0);}voidpanicio (message, err) register char * message; register char * err;{/* Should panic_level already be increased here ? */ panic (message, err);}voidcatch_interrupt (signum) int signum;{ panicking ("External signal %d caught - terminating", NIL_PTR, signum); catch_signals (catch_interrupt);}/*-------------------------------------------------------------------------*//* * Memory allocation */#ifdef msdos#include <alloc.h>#define allocate farmalloc#define freemem farfree#elseextern void * malloc ();extern void free ();#define allocate malloc#define freemem free#endifchar *alloc (bytes) int bytes;{ return allocate ((unsigned) bytes);/* char * p; if ((p = allocate ((unsigned) bytes)) == NIL_PTR) panic ("Out of memory", NIL_PTR); return p;*/}voidfree_space (p) char * p;{ freemem (p);}/* * free header list */#define pointersize sizeof (void *)#define blocksizeof(typ) ((sizeof (typ) + pointersize - 1) / pointersize * pointersize)LINE * free_header_list = NIL_LINE;voidalloc_headerblock (n) int n;{ LINE * new_header; LINE * new_list; int i = 0; new_list = (LINE *) alloc (n * blocksizeof (LINE)); if (new_list == NIL_LINE) free_header_list = NIL_LINE; else while (i < n) { new_header = (LINE *) ((long) new_list + i * blocksizeof (LINE)); new_header->next = free_header_list; free_header_list = new_header; i ++; }}LINE *alloc_header (){/* return (LINE *) alloc (sizeof (LINE)); */ LINE * new_header; if (free_header_list == NIL_LINE) { alloc_headerblock (64); if (free_header_list == NIL_LINE) alloc_headerblock (16); if (free_header_list == NIL_LINE) alloc_headerblock (4); if (free_header_list == NIL_LINE) alloc_headerblock (1); if (free_header_list == NIL_LINE) return NIL_LINE; } new_header = free_header_list; free_header_list = free_header_list->next; return new_header;}voidfree_header (hp) LINE * hp;{/* freemem (hp); */ hp->next = free_header_list; free_header_list = hp;}/* * Basename () finds the absolute name of the file out of a given path_name. */#ifdef UNUSEDchar *basename (path) char * path;{ register char * ptr = path; register char * last = NIL_PTR; while (* ptr != '\0') { if (* ptr == '/') last = ptr; ptr ++; } if (last == NIL_PTR) return path; if (* (last + 1) == '\0') { /* E.g. /usr/tmp/pipo/ */ * last = '\0'; return basename (path); /* Try again */ } return last + 1;}#endif/* * Unnull () changes a NULL string pointer into an empty string pointer * to allow easy feading of string results into build_string / sprintf */char *unnull (s) char * s;{ if (s == NIL_PTR) return ""; else return s;}/* * Output an (unsigned) long in a 10 digit field without leading zeros. * It returns a pointer to the first digit in the buffer. */char *num_out (number) long number;{ static char num_buf [11]; /* Buffer to build number */ register long digit; /* Next digit of number */ register long pow = 1000000000L; /* Highest ten power of long */ FLAG digit_seen = FALSE; int i; for (i = 0; i < 10; i ++) { digit = number / pow; /* Get next digit */ if (digit == 0L && digit_seen == FALSE && i != 9) num_buf [i] = ' '; else { num_buf [i] = '0' + (char) digit; number -= digit * pow; /* Erase digit */ digit_seen = TRUE; } pow /= 10L; /* Get next digit */ } num_buf [11] = '\0'; for (i = 0; num_buf [i] == ' '; i ++) /* Skip leading spaces */ ; return & num_buf [i];}/* * Build_string () prints the arguments as described in fmt, into the buffer. * %s indicates a string argument, %d indicates an integer argument. */#ifndef build_string /* otherwise build_string is sprintf *//* VARARGS */voidbuild_string (buf, fmt, args) register char * buf, * fmt; int args;{ int * argptr = & args; char * scanp; FLAG islong; while (* fmt) { if (* fmt == '%') { fmt ++; if (* fmt == 'l') {islong = TRUE; fmt ++;} else islong = FALSE; switch (* fmt ++) { case 's' : scanp = (char *) * argptr; break; case 'd' : if (islong == TRUE) { scanp = num_out ((long) * ((long *) argptr)); if (sizeof (long) > sizeof (int)) argptr ++; break; } else { scanp = num_out ((long) * argptr); break; } case 'D' : scanp = num_out ((long) * ((long *) argptr)); if (sizeof (long) > sizeof (int)) argptr ++; break; default : scanp = ""; } while (* buf ++ = * scanp ++) ; buf --; argptr ++; } else * buf ++ = * fmt ++; } * buf = '\0';}#endif /* ndef build_string *//* * make_number () converts a string into a natural number * returns the character after the last digit */intmake_number (num, str) int * num; char * str;{ register char * chpoi = str; * num = 0; while (* chpoi >= '0' && * chpoi <= '9' && quit == FALSE) { * num *= 10; * num += * chpoi - '0'; chpoi ++; } return * chpoi;}/* * Length_of () returns the number of characters in the string `string' * excluding the '\0'. */intlength_of (string) register char * string;{ register int count = 0; if (string != NIL_PTR) { while (* string ++ != '\0') count ++; } return count;}/* * text_length_of () returns the number of characters in the string `string' * up to and excluding the first '\n'. */inttext_length_of (string) register char * string;{ register int count = 0; if (string != NIL_PTR) { while (* string != '\0' && * string != '\n') { string ++; count ++; } } return count;}/* * Copy_string () copies the string `from' into the string `to'. `To' must be * long enough to hold `from'. */voidcopy_string (to, from) register char * to; register char * from;{ while ((* to ++ = * from ++) != '\0') ;}/*-------------------------------------------------------------------------*//* * serrorof delivers the error message of the given errno value. * serror delivers the error message of the current errno value. * geterrno just returns the current errno value. */#ifdef vms/* #define includeerrno */# ifdef includeerrno# include <errno.h># else extern volatile int noshare errno; extern volatile int noshare vaxc$errno; /* VMS error code when errno = EVMSERR */# define EVMSERR 65535 extern volatile int noshare sys_nerr; extern volatile char noshare * sys_errlist [];# endif#else extern int errno; extern int sys_nerr; extern char * sys_errlist [];#endifchar *serrorof (errnum) int errnum;{ if ((errnum < 0) || (errnum >= sys_nerr)) { static char s [20];#ifdef vms if (errnum == EVMSERR) build_string (s, "VMS error %d", vaxc$errno); else#endif build_string (s, "Unknown error %d", errnum); return s; } else return sys_errlist [errnum];}char *serror (){ return serrorof (errno); }intgeterrno (){ return errno; }/* ================================================================== * * Output * * ================================================================== */#ifdef msdos#define iscontrol(c) (((c) == '\177') || ((uchar) c < (uchar) ' '))#else#define iscontrol(c) (((c) == '\177') || ((uchar) ((c) & '\177') < (uchar) ' '))#endif#define controlchar(c) (((c) == '\177') ? '?' : (c) + '@')/* * Bad_write () is called when a write failed. Notify the user. */voidbad_write (fd) int fd;{ if (fd == output_fd) { /* Cannot write to terminal? */ raw_mode (OFF); panicio ("Write error on terminal", NIL_PTR); } clear_buffer (); /* out_count = 0; */ ring_bell (); error ("Write aborted (File incomplete): ", serror ());}/* * Flush the I/O buffer on filedescriptor fd.flush () is (void) flush_buffer (output_fd) */intflush_buffer (fd) int fd;{ if (out_count <= 0) /* There is nothing to flush */ return FINE;#ifdef conio if (fd == output_fd) { cputs (screen); }#else#ifdef BorlandC if (fd == output_fd) { screen [out_count] = '\0';/* don't ask me why that crazy compiler doesn't work with write () below */ printf ("%s", screen); }#endif#endif else if (write (fd, screen, out_count) != out_count) { bad_write (fd); return ERRORS; } clear_buffer (); /* Empty buffer: out_count = 0; */ return FINE;}/* * Write_char does a buffered output.putchar (c) is (void) writechar (output_fd, (c)) */intwritechar (fd, c) int fd; char c;{ if (c == '\n') if (fd == output_fd) { if (writechar (fd, '\015') == ERRORS) return ERRORS; } screen [out_count ++] = c; if (out_count == screen_BUFL) /* Flush on screen_BUFL chars */ return flush_buffer (fd);#ifdef DEBUG if (fd == output_fd) flush ();#endif return FINE;}/* * Writestring writes the given string on the given filedescriptor. * (buffered via writechar via screen !)putstring (str) is (void) writestring (output_fd, (str)) */intwritestring (fd, text) register int fd; register char * text;{ while (* text) if (writechar (fd, * text ++) == ERRORS) return ERRORS; return FINE;}/* * Print string on terminal, printing controls with ^. */int lpos = 0;voidprint_char (c) register uchar c;{ lpos ++; if (iscontrol (c)) { putchar ('^'); lpos ++; putchar (controlchar (c)); } else putchar (c);}intprintlim_char (c, limit) register uchar c; register int limit;{ if (lpos == limit) {putchar (SHIFT_MARK); return ERRORS;} lpos ++; if (iscontrol (c)) { putchar ('^'); if (lpos == limit) {putchar (SHIFT_MARK); return ERRORS;} lpos ++; putchar (controlchar (c)); } else putchar (c); return FINE;}voidprintlim_string (text, limit) register char * text; register int limit;{ lpos = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -