Path: blob/main/editors/bpatch/files/patch-bpatch.c
16461 views
--- bpatch.c.orig 1990-01-17 15:33:33.000000000 +01001+++ bpatch.c 2012-11-30 15:20:18.000000000 +01002@@ -90,10 +90,15 @@3/*E*/4/*S includes, globals, and defines */5/*Page Eject*/6-#include <curses.h>7+#include <ncurses.h>8#include <fcntl.h>9#include <signal.h>10#include <ctype.h>11+#include <stdlib.h>12+#include <string.h>13+#include <unistd.h>14+#include <stdio.h>15+#include <termios.h>16#include <sys/types.h>17#include <sys/stat.h>1819@@ -103,14 +108,18 @@20void icc ();21void copyrec ();22void schwapp ();23-24-/* added declarations, mostly for cleanliness */25-extern long lseek();26-extern long atol();27-extern char *strncpy();28-extern void exit();29-extern unsigned sleep(); /* signal problems ??? if sleep called */30-extern void perror();31+void breakp ();32+void reset ();33+int ckfile (char *, long *);34+int bread (int, char *, int, int);35+void errmsg (char *, unsigned);36+void show (int, char[16][16], char *, long, int, long, char, int, int, int, int, int, int);37+void dbg_msg (register char *);38+void dohelp ();39+void search (register int);40+int arrow (register int, register int*, register int *);41+void outstr (char *);42+void outch (register char);4344/* set up for calls to outstr and errmsg, etc, */45/* by use of sprintf to fill outbuf */46@@ -190,7 +199,7 @@47#ifdef MOD_HAX48#else /* use original code... */49/* plus some more for restoring terminal function */50- struct termio asis, aswas;51+ struct termios asis, aswas;52#endif /* MOD_HAX */5354/*S main - control all the work from here */55@@ -202,6 +211,7 @@56*57*********************************************************************/58/*E*/59+int60main (argc, argv)61int argc;62char *argv[];63@@ -210,12 +220,10 @@64extern WINDOW *newwin ();6566register char *cp; /* general purpose char ptr */67- extern char *gets (); /* get string from stdin */68char m = '\017'; /* mask for hex edit */69char response[512]; /* general purpose buffer */70int z; /* character read in */7172- int breakp (); /* signal trapping function */73int c; /* current screen column */74int change = 0; /* true if cmd line option toggled */75int fid; /* file descriptor */76@@ -234,8 +242,7 @@77extern char *optarg; /* getopt pointer to opt arg */7879extern long getnum ();80- extern char *instr (); /* get a string from the cmd line */81- extern int reset (); /* exit function - reset terminal */82+ extern char *myinstr (); /* get a string from the cmd line */8384/* ------------------------------------------------------------ */85/* set up signal handling */86@@ -333,19 +340,27 @@87alphawin = subwin (stdscr, 16, 16, 4, 57);88keypad (alphawin, TRUE);89errwin = subwin (stdscr, 1, 80, 23, 0);90+ if (errwin == NULL)91+ errwin = stdscr;92+ if (hexwin == NULL || alphawin == NULL)93+ {94+ fprintf(stderr, "Can't create all necessary curses windows.\n");95+ reset (0);96+ exit (2);97+ }9899#ifdef MOD_HAX100/* This is not exactly what the original code does,101but it's good enough. -r$ */102raw();103#else /* use original code... */104- ioctl (0, TCGETA, &asis);105+ tcgetattr (0, &asis);106aswas = asis; /* save termio stuff for later restore */107asis.c_cc[VINTR] = '\0';108asis.c_iflag &= ~IXON;109asis.c_iflag &= ~IXOFF;110asis.c_iflag &= ~IXANY;111- ioctl (0, TCSETA, &asis);112+ tcsetattr (0, TCSANOW, &asis);113#endif /* MOD_HAX */114}115116@@ -398,7 +413,7 @@117{118position = lseek (fid, ((long )recno) * 256, 0);119120- if ((bytes = bread (fid, record, 256, block)) < 0)121+ if ((bytes = bread (fid, (char *)record, 256, block)) < 0)122{123sprintf(outbuf, "error on reading file %s", filename);124errmsg (outbuf, SLEEP_TIME);125@@ -478,7 +493,9 @@126}127pbrk = 0;128fprintf (stderr, "\007");129- gets (response);130+ fgets (response, 512, stdin);131+ if ((cp = strchr (response, '\n')))132+ *cp = 0;133134if (pbrk) status = EOF;135}136@@ -617,7 +634,7 @@137138case '\006': /* new file (^F) */139close (fid);140- fid = ckfile (cp = instr (), &size);141+ fid = ckfile (cp = myinstr (), &size);142if (fid < 0)143{144fid = ckfile (filename, &size);145@@ -832,7 +849,8 @@146touchwin (alphawin);147wrefresh (alphawin);148149- while ((z = wgetch (alphawin)) != DEL)150+ while ((z = wgetch (alphawin)) != KEY_DC &&151+ z != KEY_BACKSPACE)152{153if (!arrow (z, &r, &c))154{155@@ -902,7 +920,8 @@156touchwin (hexwin);157wrefresh (hexwin);158159- while ((z = wgetch (hexwin)) != DEL)160+ while ((z = wgetch (hexwin)) != KEY_DC161+ && z != KEY_BACKSPACE)162{163if (!arrow (z, &r, &c))164{165@@ -1083,21 +1102,9 @@166/*E*/167/*checked typing of parameters as declared in fucntion */168/* versus declarations at call */169-show (bytes, record, filename, size, recno, position,170- m,reclen, dump, ebcdic, swab_opt, block, honly)171-int bytes;172-char record[16][16];173-char *filename;174-long size;175-int recno;176-long position;177-char m;178-int reclen;179-int dump;180-int ebcdic;181-int swab_opt;182-int block;183-int honly;184+void185+show (int bytes, char record[16][16], char *filename, long size, int recno, long position,186+ char m, int reclen, int dump, int ebcdic, int swab_opt, int block, int honly)187{188int i;189int j;190@@ -1281,7 +1288,7 @@191/*S breakp - set pbrk on interrupt */192/*H breakp */193/*E*/194-int breakp (i)195+void breakp (i)196int i;197{198int s;199@@ -1448,6 +1455,7 @@200/*S ckfile - check on existence, accessibility, and type of file */201/*H ckfile */202/*E*/203+int204ckfile (filename, sizep)205char *filename;206long *sizep;207@@ -1515,6 +1523,7 @@208/*S dohelp - display help text */209/*H dohelp */210/*E*/211+void212dohelp ()213{214static char *helptxt[] = {215@@ -1592,6 +1601,7 @@216/*S reset - reset terminal to original state */217/*H reset */218/*E*/219+void220reset (sig)221int sig;222{223@@ -1600,7 +1610,7 @@224move (23, 0);225refresh ();226#ifndef MOD_HAX227- ioctl (0, TCSETA, &aswas);228+ tcsetattr (0, TCSANOW, &aswas);229#endif230endwin ();231}232@@ -1610,17 +1620,17 @@233fprintf (stderr, "killed with signal %d\n", sig);234exit (sig);235}236- return (0);237}238/*S arrow - determine if current character is a cursor control key */239/*H arrow */240/*E*/241+int242arrow (k, r, c)243register int k;244register int *r;245register int *c;246{247- register ret = 1;248+ register int ret = 1;249250/* watch out for conflict of VI_* amd KEY_* definitions */251if (k == KEY_UP || k == VI_UP)252@@ -1673,6 +1683,7 @@253/*S dbg_msg - print a debug message */254/*H dbg_msg */255/*E*/256+void257dbg_msg (msg)258register char *msg;259{260@@ -1683,11 +1694,11 @@261262return;263}264-/*S instr - get a character string from the terminal */265-/*H instr */266+/*S myinstr - get a character string from the terminal */267+/*H myinstr */268/*E*/269char *270-instr ()271+myinstr ()272{273static char buf[512];274275@@ -1815,6 +1826,7 @@276/*S search - look for an ascii string in the file */277/*H search */278/*E*/279+void280search (fid)281register int fid;282{283@@ -1827,13 +1839,13 @@284register int matched = 0;285register int srch_len;286287- register char *cp = instr ();288+ register char *cp = myinstr ();289register char *rp;290291int row, col;292293srch_len = strlen (cp);294- copyrec (record, lrecord, sizeof record);295+ copyrec ((char *)record, lrecord, sizeof record);296lrecord[256] = '\0';297298pbrk = 0;299@@ -1875,7 +1887,7 @@300{301recno = currec;302stay = 0;303- copyrec (record, unch_rec, sizeof record);304+ copyrec ((char *)record, (char *)unch_rec, sizeof record);305werase (errwin);306touchwin (errwin);307wrefresh (errwin);308@@ -1891,6 +1903,7 @@309}310/* simplified call to errmsg(), by using sprintf to load format */311/* sleep_time is not implemented and awaits cleaning up of the signals (?) */312+void313errmsg (fmt, sleep_time)314unsigned sleep_time;315char *fmt;316@@ -1911,7 +1924,7 @@317}318else319{320- fprintf (stderr, fmt);321+ fprintf (stderr, "%s", fmt);322fprintf (stderr, "\n");323}324/* signal problem ?? if sleep called325@@ -1921,16 +1934,17 @@326return;327}328/* simplified call to outstr(), by using sprintf to load format */329+void330outstr (fmt)331char *fmt;332{333- if (dump) printf (fmt);334- else printw (fmt);335+ if (dump) printf ("%s", fmt);336+ else printw ("%s", fmt);337338return;339}340-outch (ch)341-register char ch;342+void343+outch (register char ch)344{345if (dump) putchar (ch);346else addch (ch);347348349