Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #79175 > unrolled thread
| Started by | jacobnavia <jacob@jacob.remcomp.fr> |
|---|---|
| First post | 2021-04-22 18:58 +0200 |
| Last post | 2021-04-26 18:04 +0200 |
| Articles | 20 on this page of 96 — 27 participants |
Back to article view | Back to comp.lang.c++
Text editor project in C jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-22 18:58 +0200
Re: Text editor project in C Bonita Montero <Bonita.Montero@gmail.com> - 2021-04-22 19:05 +0200
Re: Text editor project in C wij <wyniijj@gmail.com> - 2021-04-22 12:41 -0700
Re: Text editor project in C Juha Nieminen <nospam@thanks.invalid> - 2021-04-23 07:38 +0000
Re: Text editor project in C wij <wyniijj@gmail.com> - 2021-04-23 05:12 -0700
Re: Text editor project in C red floyd <myob@its.invalid> - 2021-04-23 09:24 -0700
Re: Text editor project in C James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-04-23 16:59 -0400
Re: Text editor project in C wij <wyniijj@gmail.com> - 2021-04-23 21:43 -0700
Re: Text editor project in C Ian Collins <ian-news@hotmail.com> - 2021-04-24 17:05 +1200
Re: Text editor project in C wij <wyniijj@gmail.com> - 2021-04-24 00:50 -0700
Re: Text editor project in C "daniel...@gmail.com" <danielaparker@gmail.com> - 2021-04-24 08:50 -0700
Re: Text editor project in C wij <wyniijj@gmail.com> - 2021-04-24 10:41 -0700
Re: Text editor project in C Öö Tiib <ootiib@hot.ee> - 2021-04-25 06:19 -0700
Re: Text editor project in C wij <wyniijj@gmail.com> - 2021-04-25 10:23 -0700
Re: Text editor project in C Öö Tiib <ootiib@hot.ee> - 2021-04-25 12:46 -0700
Re: Text editor project in C wij <wyniijj@gmail.com> - 2021-04-25 20:34 -0700
Re: Text editor project in C Öö Tiib <ootiib@hot.ee> - 2021-04-28 09:57 -0700
Re: Text editor project in C wij <wyniijj@gmail.com> - 2021-04-29 10:56 -0700
Re: Text editor project in C jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-27 18:31 +0200
Re: Text editor project in C David Brown <david.brown@hesbynett.no> - 2021-04-28 09:01 +0200
Re: Text editor project in C Manfred <noname@add.invalid> - 2021-04-28 14:54 +0200
Re: Text editor project in C Juha Nieminen <nospam@thanks.invalid> - 2021-04-29 04:47 +0000
Re: Text editor project in C Paavo Helde <myfirstname@osa.pri.ee> - 2021-04-29 11:42 +0300
Re: Text editor project in C Christian Gollwitzer <auriocus@gmx.de> - 2021-04-29 23:04 +0200
Re: Text editor project in C Juha Nieminen <nospam@thanks.invalid> - 2021-04-23 07:55 +0000
Re: Text editor project in C jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-23 11:25 +0200
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 12:45 -0700
Re: Text editor project in C jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-23 22:29 +0200
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 13:34 -0700
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 13:37 -0700
Re: Text editor project in C jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-23 22:40 +0200
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 13:51 -0700
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 13:55 -0700
Re: Text editor project in C jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-23 23:02 +0200
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 14:13 -0700
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 15:39 -0700
Re: Text editor project in C Bart <bc@freeuk.com> - 2021-04-24 01:20 +0100
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 17:43 -0700
Re: Text editor project in C kegs@provalid.com (Kent Dickey) - 2021-04-25 00:04 -0500
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-24 22:29 -0700
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-24 23:05 -0700
Re: Text editor project in C Paavo Helde <myfirstname@osa.pri.ee> - 2021-04-25 10:57 +0300
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-25 01:18 -0700
Re: Text editor project in C Paavo Helde <myfirstname@osa.pri.ee> - 2021-04-25 11:28 +0300
Re: Text editor project in C James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-04-25 15:02 -0400
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-26 01:25 -0700
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-26 01:26 -0700
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 14:15 -0700
Re: Text editor project in C scott@slp53.sl.home (Scott Lurndal) - 2021-04-23 22:25 +0000
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 15:31 -0700
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 13:38 -0700
Re: Text editor project in C scott@slp53.sl.home (Scott Lurndal) - 2021-04-23 22:27 +0000
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-23 15:34 -0700
Re: Text editor project in C scott@slp53.sl.home (Scott Lurndal) - 2021-04-24 16:33 +0000
Re: Text editor project in C "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-24 22:59 -0700
Re: Text editor project in C Juha Nieminen <nospam@thanks.invalid> - 2021-04-26 05:22 +0000
Re: Text editor project in C jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-26 10:00 +0200
Re: Text editor project in C Juha Nieminen <nospam@thanks.invalid> - 2021-04-26 09:57 +0000
Re: Text editor project in C Robert Latest <boblatest@yahoo.com> - 2021-04-26 10:41 +0000
Re: Text editor project in C jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-26 13:20 +0200
Re: Text editor project in C Juha Nieminen <nospam@thanks.invalid> - 2021-04-26 13:17 +0000
Re: Text editor project in C jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-26 19:53 +0200
Re: Text editor project in C David Brown <david.brown@hesbynett.no> - 2021-04-26 20:34 +0200
Re: Text editor project in C jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-26 22:49 +0200
Re: Text editor project in C David Brown <david.brown@hesbynett.no> - 2021-04-27 10:59 +0200
Re: Text editor project in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-26 22:04 +0100
Re: Text editor project in C Eli the Bearded <*@eli.users.panix.com> - 2021-04-26 22:19 +0000
Re: Text editor project in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-04-26 16:07 -0700
Re: Text editor project in C Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2021-04-26 17:08 -0600
Re: Text editor project in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-04-26 16:57 -0700
Re: Text editor project in C Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2021-04-26 21:42 -0600
Re: Text editor project in C Kaz Kylheku <563-365-8930@kylheku.com> - 2021-04-27 00:46 +0000
Re: Text editor project in C Juha Nieminen <nospam@thanks.invalid> - 2021-04-27 04:51 +0000
Re: Text editor project in C Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2021-04-27 10:03 -0600
Re: Text editor project in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-04-27 09:20 -0700
Re: Text editor project in C scott@slp53.sl.home (Scott Lurndal) - 2021-04-27 16:34 +0000
Re: Text editor project in C Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2021-04-27 10:55 -0600
Re: Text editor project in C Kaz Kylheku <563-365-8930@kylheku.com> - 2021-04-27 17:13 +0000
Re: Text editor project in C Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-04-27 17:28 +0000
Re: Text editor project in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-04-27 13:33 -0700
Re: Text editor project in C scott@slp53.sl.home (Scott Lurndal) - 2021-04-27 19:27 +0000
Re: Text editor project in C Kaz Kylheku <563-365-8930@kylheku.com> - 2021-04-27 17:11 +0000
Re: Text editor project in C scott@slp53.sl.home (Scott Lurndal) - 2021-04-27 14:28 +0000
Re: Text editor project in C Juha Nieminen <nospam@thanks.invalid> - 2021-04-27 15:11 +0000
Re: Text editor project in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-26 21:37 +0100
Re: Text editor project in C jacobnavia <jacob@jacob.remcomp.fr> - 2021-04-26 22:48 +0200
Re: Text editor project in C scott@slp53.sl.home (Scott Lurndal) - 2021-04-26 21:53 +0000
Re: Text editor project in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-27 01:34 +0100
Re: Text editor project in C Guillaume <message@bottle.org> - 2021-04-26 18:48 +0200
Re: Text editor project in C James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-04-26 13:54 -0400
Re: Text editor project in C Jorgen Grahn <grahn+nntp@snipabacken.se> - 2021-04-23 20:00 +0000
Re: Text editor project in C Juha Nieminen <nospam@thanks.invalid> - 2021-04-26 05:29 +0000
Re: Text editor project in C om@iki.fi (Otto J. Makela) - 2021-04-23 19:25 +0300
Re: Text editor project in C wij <wyniijj@gmail.com> - 2021-04-24 10:55 -0700
Re: Text editor project in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-04-24 14:21 -0700
Re: Text editor project in C Bonita Montero <Bonita.Montero@gmail.com> - 2021-04-26 18:04 +0200
Page 1 of 5 [1] 2 3 4 5 Next page →
| From | jacobnavia <jacob@jacob.remcomp.fr> |
|---|---|
| Date | 2021-04-22 18:58 +0200 |
| Subject | Text editor project in C |
| Message-ID | <s5s9vl$v5a$1@dont-email.me> |
Recently, somebody started a thread about how C wasn't a simple
language. The excuse was a text editor project for a beginner.
Attached is the source code for the 10th edition of Unix of the "ed"
text editor, published by Brian Kernighan for his CS classes at
Princeton in 2001.
Maybe C is not a simple language but one of its incredible strengths is
its stability. This code is around 40 (yes FORTY) years old and you can
compile it without any trouble today and it will work in any UNIX system.
----------------------------------------------------------------cut here
/* This file contains the source for the 10th Edition Unix version of
ed, which is
1700 lines long. It dates from about 1989, but is typical of Unix
code from the
mid 1970's: terse, tight, efficient, and largely uncommented
This is a slightly modified version that compiles without any
warnings under gcc in 2021
See https://www.cs.princeton.edu/courses/archive/spring01/cs333/ed.c
Compile with gcc -O2 -o ed -Wall -Wno-parentheses ed.c
*/
#include <signal.h>
#include <stdlib.h>
#include <setjmp.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <wait.h>
#include <string.h>
#define BLKSIZE 4096 /* make BLKSIZE and LBSIZE 512 for smaller machines */
#define NBLK 2047
#define FNSIZE 128
#define LBSIZE 4096
#define ESIZE 256
#define GBSIZE 256
#define NBRA 5
#define KSIZE 9
#define CBRA 1
#define CCHR 2
#define CDOT 4
#define CCL 6
#define NCCL 8
#define CDOL 10
#define CEOF 11
#define CKET 12
#define CBACK 14
#define CCIRC 15
#define STAR 01
static unsigned char Q[] = "";
static unsigned char T[] = "TMP";
#define READ 0
#define WRITE 1
static int peekc;
static int lastc;
static unsigned char savedfile[FNSIZE];
static unsigned char file[FNSIZE];
static unsigned char linebuf[LBSIZE];
static unsigned char rhsbuf[LBSIZE/2];
static unsigned char expbuf[ESIZE+4];
static int given;
static unsigned int *addr1, *addr2;
static unsigned int *dot, *dol, *zero;
static unsigned char genbuf[LBSIZE];
static long count;
static unsigned char *nextip;
static unsigned char *linebp;
static int ninbuf;
static int io;
static int pflag;
static int vflag = 1;
static int oflag;
static int listf;
static int listn;
static int col;
static unsigned char *globp;
static int tfile = -1;
static int tline;
static char tfname[50];
static unsigned char *loc1;
static unsigned char *loc2;
static unsigned char ibuff[BLKSIZE];
static int iblock = -1;
static unsigned char obuff[BLKSIZE];
static int oblock = -1;
static int ichanged;
static int nleft;
static char WRERR[] = "WRITE ERROR";
static int names[26];
static int anymarks;
static unsigned char *braslist[NBRA];
static unsigned char *braelist[NBRA];
static int nbra;
static int subnewa;
static int subolda;
static int fchange;
static int wrapp;
static unsigned nlall = 128;
static char tmpXXXXX[50] = "/tmp/eXXXXXX";
static unsigned char *getblock(unsigned int atl, int iof);
static unsigned char *GetLine(unsigned int tl);
static unsigned char *place(unsigned char *sp,unsigned char *l1,unsigned
char *l2);
static void add(int i);
static int advance(unsigned char *lp, unsigned char *ep);
static int append(int (*f)(void), unsigned int *a);
static int backref(int i,unsigned char *lp);
static void blkio(int b, unsigned char *buf, int iof);
static void callunix(void);
static int cclass(unsigned char *set, int c, int af);
static void commands(void);
static void compile(int eof);
static int compsub(void);
static void dosub(void);
static void error(unsigned char *s);
static int execute(unsigned int *addr);
static void exfile(void);
static void filename(int comm);
static void gdelete(void);
static int getchr(void);
static int getcopy(void);
static int getfile(void);
static int getnum(void);
static int getsub(void);
static int gettty(void);
static int gety(void);
static void global(int k);
static void init(void);
static unsigned int *address(void);
static void join(void);
static void move(int cflag);
static void newline(void);
static void nonzero(void);
static void onhup(int n);
static void onintr(int n);
static void print(void);
static void putchr(int ac);
static void putd(void);
static void putfile(void);
static int putline(void);
static void quit(int n);
static void rdelete(unsigned int *ad1, unsigned int *ad2);
static void reverse(unsigned int *a1, unsigned int *a2);
static void setwide(void);
static void setnoaddr(void);
static void squeeze(int i);
static void substitute(int inglob);
static jmp_buf savej;
typedef void (*SIG_TYP)(int);
static SIG_TYP oldhup;
static SIG_TYP oldquit;
/* these two are not in ansi, but we need them */
#define SIGHUP 1 /* hangup */
#define SIGQUIT 3 /* quit (ASCII FS) */
int main(int argc, char *argv[])
{
unsigned char *p1, *p2;
SIG_TYP oldintr;
oldquit = signal(SIGQUIT, SIG_IGN);
oldhup = signal(SIGHUP, SIG_IGN);
oldintr = signal(SIGINT, SIG_IGN);
if (signal(SIGTERM, SIG_IGN) == SIG_DFL)
signal(SIGTERM, quit);
argv++;
while (argc > 1 && **argv=='-') {
switch((*argv)[1]) {
case '\0':
vflag = 0;
break;
case 'q':
signal(SIGQUIT, SIG_DFL);
vflag = 1;
break;
case 'o':
oflag = 1;
break;
}
argv++;
argc--;
}
if (oflag) {
p1 = (unsigned char *)"/dev/stdout";
p2 = savedfile;
while (*p2++ = *p1++)
;
}
if (argc>1) {
p1 = (unsigned char *)*argv;
p2 = savedfile;
while (*p2++ = *p1++)
if (p2 >= &savedfile[sizeof(savedfile)])
p2--;
globp = (unsigned char *)"r";
}
zero = (unsigned *)malloc(nlall*sizeof(unsigned));
strcpy(tfname,tmpXXXXX);
mkstemp(tfname);
init();
if (oldintr!=SIG_IGN)
signal(SIGINT, onintr);
if (oldhup!=SIG_IGN)
signal(SIGHUP, onhup);
setjmp(savej);
commands();
quit(0);
return 0;
}
static void commands(void)
{
unsigned int *a1;
int c;
int temp;
unsigned char lastsep;
for (;;) {
if (pflag) {
pflag = 0;
addr1 = addr2 = dot;
print();
}
c = '\n';
for (addr1 = 0;;) {
lastsep = c;
a1 = address();
c = getchr();
if (c!=',' && c!=';')
break;
if (lastsep==',')
error(Q);
if (a1==0) {
a1 = zero+1;
if (a1>dol)
a1--;
}
addr1 = a1;
if (c==';')
dot = a1;
}
if (lastsep!='\n' && a1==0)
a1 = dol;
if ((addr2=a1)==0) {
given = 0;
addr2 = dot;
}
else
given = 1;
if (addr1==0)
addr1 = addr2;
switch(c) {
case 'a':
add(0);
continue;
case 'c':
nonzero();
newline();
rdelete(addr1, addr2);
append(gettty, addr1-1);
continue;
case 'd':
nonzero();
newline();
rdelete(addr1, addr2);
continue;
case 'E':
fchange = 0;
c = 'e';
case 'e':
setnoaddr();
if (vflag && fchange) {
fchange = 0;
error(Q);
}
filename(c);
init();
addr2 = zero;
goto caseread;
case 'f':
setnoaddr();
filename(c);
puts((const char *)savedfile);
continue;
case 'g':
global(1);
continue;
case 'i':
add(-1);
continue;
case 'j':
if (!given)
addr2++;
newline();
join();
continue;
case 'k':
nonzero();
if ((c = getchr()) < 'a' || c > 'z')
error(Q);
newline();
names[c-'a'] = *addr2 & ~01;
anymarks |= 01;
continue;
case 'm':
move(0);
continue;
case 'n':
listn++;
newline();
print();
continue;
case '\n':
if (a1==0) {
a1 = dot+1;
addr2 = a1;
addr1 = a1;
}
if (lastsep==';')
addr1 = a1;
print();
continue;
case 'l':
listf++;
case 'p':
case 'P':
newline();
print();
continue;
case 'Q':
fchange = 0;
case 'q':
setnoaddr();
newline();
quit(0);
case 'r':
filename(c);
caseread:
if ((io = open((const char *)file, 0)) < 0) {
lastc = '\n';
error(file);
}
setwide();
squeeze(0);
ninbuf = 0;
c = zero != dol;
append(getfile, addr2);
exfile();
fchange = c;
continue;
case 's':
nonzero();
substitute(globp!=0);
continue;
case 't':
move(1);
continue;
case 'u':
nonzero();
newline();
if ((*addr2&~01) != subnewa)
error(Q);
*addr2 = subolda;
dot = addr2;
continue;
case 'v':
global(0);
continue;
case 'W':
wrapp++;
case 'w':
setwide();
squeeze(dol>zero);
if ((temp = getchr()) != 'q' && temp != 'Q') {
peekc = temp;
temp = 0;
}
filename(c);
if(!wrapp ||
((io = open((const char *)file,1)) == -1) ||
((lseek(io, 0L, 2)) == -1))
if ((io = creat((const char *)file, 0666)) < 0)
error(file);
wrapp = 0;
if (dol > zero)
putfile();
exfile();
if (addr1<=zero+1 && addr2==dol)
fchange = 0;
if (temp == 'Q')
fchange = 0;
if (temp)
quit(0);
continue;
case '=':
setwide();
squeeze(0);
newline();
count = addr2 - zero;
putd();
putchr('\n');
continue;
case '!':
callunix();
continue;
case EOF:
return;
}
error(Q);
}
}
static void print(void)
{
unsigned int *a1;
nonzero();
a1 = addr1;
do {
if (listn) {
count = a1-zero;
putd();
putchr('\t');
}
puts((const char *)GetLine(*a1++));
} while (a1 <= addr2);
dot = addr2;
listf = 0;
listn = 0;
pflag = 0;
}
static unsigned int * address(void)
{
int sign;
unsigned int *a, *b;
int opcnt, nextopand;
int c;
nextopand = -1;
sign = 1;
opcnt = 0;
a = dot;
do {
do c = getchr(); while (c==' ' || c=='\t');
if ('0'<=c && c<='9') {
peekc = c;
if (!opcnt) a = zero;
a += sign*getnum();
} else switch (c) {
case '$':
a = dol;
/* fall through */
case '.':
if (opcnt) error(Q);
break;
case '\'':
c = getchr();
if (opcnt || c<'a' || 'z'<c) error(Q);
a = zero;
do a++; while (a<=dol && names[c-'a']!=(*a&~01));
break;
case '?':
sign = -sign;
/* fall through */
case '/':
compile(c);
b = a;
for (;;) {
a += sign;
if (a<=zero) a = dol;
if (a>dol) a = zero;
if (execute(a)) break;
if (a==b) error(Q);
}
break;
default:
if (nextopand == opcnt) {
a += sign;
if (a<zero || dol<a)
continue; /* error(Q); */
}
if (c!='+' && c!='-' && c!='^') {
peekc = c;
if (opcnt==0) a = 0;
return (a);
}
sign = 1;
if (c!='+') sign = -sign;
nextopand = ++opcnt;
continue;
}
sign = 1;
opcnt++;
} while (zero<=a && a<=dol);
error(Q);
/*NOTREACHED*/
return 0;
}
static int getnum(void)
{
int r, c;
r = 0;
while ((c=getchr())>='0' && c<='9')
r = r*10 + c - '0';
peekc = c;
return (r);
}
static void setwide(void)
{
if (!given) {
addr1 = zero + (dol>zero);
addr2 = dol;
}
}
static void setnoaddr(void)
{
if (given) error(Q);
}
static void nonzero(void)
{
squeeze(1);
}
static void squeeze(int i)
{
if (addr1<zero+i || addr2>dol || addr1>addr2)
error(Q);
}
static void newline(void)
{
int c;
if ((c = getchr()) == '\n' || c == EOF) return;
if (c=='p' || c=='l' || c=='n') {
pflag++;
if (c=='l') listf++;
else if (c=='n') listn++;
if ((c=getchr())=='\n') return;
}
error(Q);
}
static void filename(int comm)
{
unsigned char *p1, *p2;
int c;
count = 0;
c = getchr();
if (c=='\n' || c==EOF) {
p1 = savedfile;
if (*p1==0 && comm!='f') error(Q);
p2 = file;
while (*p2++ = *p1++)
;
return;
}
if (c!=' ')
error(Q);
while ((c = getchr()) == ' ')
;
if (c=='\n') error(Q);
p1 = file;
do {
if (p1 >= &file[sizeof(file)-1] || c==' ' || c==EOF) error(Q);
*p1++ = c;
} while ((c = getchr()) != '\n');
*p1++ = 0;
if (savedfile[0]==0 || comm=='e' || comm=='f') {
p1 = savedfile;
p2 = file;
while (*p1++ = *p2++)
;
}
}
static void exfile(void)
{
close(io);
io = -1;
if (vflag) {
putd();
putchr('\n');
}
}
static void onintr(int n)
{
signal(SIGINT, onintr);
putchr('\n');
lastc = '\n';
error(Q);
}
static void onhup(int n)
{
signal(SIGINT, SIG_IGN);
signal(SIGHUP, SIG_IGN);
if (dol > zero) {
addr1 = zero+1;
addr2 = dol;
io = creat("ed.hup", 0600);
if (io > 0)
putfile();
}
fchange = 0;
quit(0);
}
static void error(unsigned char *s)
{
int c;
wrapp = 0;
listf = 0;
listn = 0;
putchr('?');
puts((const char *)s);
count = 0;
lseek(0, (long)0, 2);
pflag = 0;
if (globp)
lastc = '\n';
globp = 0;
peekc = lastc;
if(lastc)
while ((c = getchr()) != '\n' && c != EOF)
;
if (io > 0) {
close(io);
io = -1;
}
longjmp(savej, 1);
}
static int getchr(void)
{
char c;
if (lastc=peekc) {
peekc = 0;
return(lastc);
}
if (globp) {
if ((lastc = *globp++) != 0)
return(lastc);
globp = 0;
return(EOF);
}
if (read(0, &c, 1) <= 0)
return(lastc = EOF);
lastc = c&0177;
return(lastc);
}
static int gettty(void)
{
int rc;
if (rc = gety())
return(rc);
if (linebuf[0]=='.' && linebuf[1]==0)
return(EOF);
return(0);
}
static int gety(void)
{
int c;
unsigned char *gf;
unsigned char *p;
p = linebuf;
gf = globp;
while ((c = getchr()) != '\n') {
if (c==EOF) {
if (gf)
peekc = c;
return(c);
}
if ((c &= 0177) == 0)
continue;
*p++ = c;
if (p >= &linebuf[LBSIZE-2])
error(Q);
}
*p++ = 0;
return(0);
}
static int getfile(void)
{
int c;
unsigned char *lp, *fp;
lp = linebuf;
fp = nextip;
do {
if (--ninbuf < 0) {
if ((ninbuf = read(io, genbuf, LBSIZE)-1) < 0)
if (lp>linebuf) {
puts("'\\n' appended");
*genbuf = '\n';
}
else return(EOF);
fp = genbuf;
while(fp < &genbuf[ninbuf]) {
if (*fp++ & 0200)
break;
}
fp = genbuf;
}
c = *fp++;
if (c=='\0')
continue;
if (c&0200 || lp >= &linebuf[LBSIZE]) {
lastc = '\n';
error(Q);
}
*lp++ = c;
count++;
} while (c != '\n');
*--lp = 0;
nextip = fp;
return(0);
}
static void putfile(void)
{
unsigned int *a1;
int n;
unsigned char *fp, *lp;
int nib;
nib = BLKSIZE;
fp = genbuf;
a1 = addr1;
do {
lp = GetLine(*a1++);
for (;;) {
if (--nib < 0) {
n = fp-genbuf;
if(write(io, genbuf, n) != n) {
puts((const char *)WRERR);
error(Q);
}
nib = BLKSIZE-1;
fp = genbuf;
}
count++;
if ((*fp++ = *lp++) == 0) {
fp[-1] = '\n';
break;
}
}
} while (a1 <= addr2);
n = fp-genbuf;
if(write(io, genbuf, n) != n) {
puts((const char *)WRERR);
error(Q);
}
}
static int append(int (*f)(void), unsigned int *a)
{
unsigned int *a1, *a2, *rdot;
int nline, tl;
nline = 0;
dot = a;
while ((*f)() == 0) {
if ((dol-zero)+1 >= nlall) {
unsigned *ozero = zero;
nlall += 1024;
if ((zero = (unsigned *)realloc((unsigned char *)zero,
nlall*sizeof(unsigned)))==NULL) {
error((unsigned char *)"MEM?");
onhup(0);
}
dot += zero - ozero;
dol += zero - ozero;
}
tl = putline();
nline++;
a1 = ++dol;
a2 = a1+1;
rdot = ++dot;
while (a1 > rdot)
*--a2 = *--a1;
*rdot = tl;
}
return(nline);
}
static void add(int i)
{
if (i && (given || dol>zero)) {
addr1--;
addr2--;
}
squeeze(0);
newline();
append(gettty, addr2);
}
static void callunix(void)
{
SIG_TYP savint;
int pid, rpid;
int retcode;
setnoaddr();
if ((pid = fork()) == 0) {
signal(SIGHUP, oldhup);
signal(SIGQUIT, oldquit);
execl("/bin/sh", "sh", "-t", NULL);
exit(0100);
}
savint = signal(SIGINT, SIG_IGN);
while ((rpid = wait(&retcode)) != pid && rpid != -1)
;
signal(SIGINT, savint);
if (vflag) {
puts("!");
}
}
static void quit(int n)
{
if (vflag && fchange && dol!=zero) {
fchange = 0;
error(Q);
}
unlink(tfname);
exit(0);
}
static void rdelete(unsigned int *ad1, unsigned int *ad2)
{
unsigned int *a1, *a2, *a3;
a1 = ad1;
a2 = ad2+1;
a3 = dol;
dol -= a2 - a1;
do {
*a1++ = *a2++;
} while (a2 <= a3);
a1 = ad1;
if (a1 > dol)
a1 = dol;
dot = a1;
fchange = 1;
}
static void gdelete(void)
{
unsigned int *a1, *a2, *a3;
a3 = dol;
for (a1=zero; (*a1&01)==0; a1++)
if (a1>=a3)
return;
for (a2=a1+1; a2<=a3;) {
if (*a2&01) {
a2++;
dot = a1;
} else
*a1++ = *a2++;
}
dol = a1-1;
if (dot>dol)
dot = dol;
fchange = 1;
}
static unsigned char *GetLine(unsigned int tl)
{
unsigned char *bp, *lp;
int nl;
lp = linebuf;
bp = getblock(tl, READ);
nl = nleft;
tl &= ~((BLKSIZE/2)-1);
while (*lp++ = *bp++)
if (--nl == 0) {
bp = getblock(tl+=(BLKSIZE/2), READ);
nl = nleft;
}
return(linebuf);
}
static int putline(void)
{
unsigned char *bp, *lp;
int nl;
unsigned int tl;
fchange = 1;
lp = linebuf;
tl = tline;
bp = getblock(tl, WRITE);
nl = nleft;
tl &= ~((BLKSIZE/2)-1);
while (*bp = *lp++) {
if (*bp++ == '\n') {
*--bp = 0;
linebp = lp;
break;
}
if (--nl == 0) {
bp = getblock(tl+=(BLKSIZE/2), WRITE);
nl = nleft;
}
}
nl = tline;
tline += (((lp-linebuf)+03)>>1)&077776;
return(nl);
}
static unsigned char * getblock(unsigned int atl, int iof)
{
int bno, off;
bno = (atl/(BLKSIZE/2));
off = (atl<<1) & (BLKSIZE-1) & ~03;
if (bno >= NBLK) {
lastc = '\n';
error(T);
}
nleft = BLKSIZE - off;
if (bno==iblock) {
ichanged |= iof;
return(ibuff+off);
}
if (bno==oblock)
return(obuff+off);
if (iof==READ) {
if (ichanged)
blkio(iblock, ibuff, iof);
ichanged = 0;
iblock = bno;
blkio(bno, ibuff, iof);
return(ibuff+off);
}
if (oblock>=0)
blkio(oblock, obuff, iof);
oblock = bno;
return(obuff+off);
}
static void blkio(int b, unsigned char *buf, int iof)
{
lseek(tfile, (long)b*BLKSIZE, 0);
if (iof == READ) {
if (read(tfile, buf, BLKSIZE) != BLKSIZE) {
error(T);
}
}
else {
if (write(tfile, buf, BLKSIZE) != BLKSIZE) {
error(T);
}
}
}
static void init(void)
{
int *markp;
close(tfile);
tline = 2;
for (markp = names; markp < &names[26]; )
*markp++ = 0;
subnewa = 0;
anymarks = 0;
iblock = -1;
oblock = -1;
ichanged = 0;
close(creat(tfname, 0600));
tfile = open(tfname, 2);
dot = dol = zero;
}
static void global(int k)
{
unsigned char *gp;
int c;
unsigned int *a1;
unsigned char globuf[GBSIZE];
if (globp)
error(Q);
setwide();
squeeze(dol>zero);
if ((c=getchr())=='\n')
error(Q);
compile(c);
gp = globuf;
while ((c = getchr()) != '\n') {
if (c==EOF)
error(Q);
if (c=='\\') {
c = getchr();
if (c!='\n')
*gp++ = '\\';
}
*gp++ = c;
if (gp >= &globuf[GBSIZE-2])
error(Q);
}
if (gp == globuf)
*gp++ = 'p';
*gp++ = '\n';
*gp++ = 0;
for (a1=zero; a1<=dol; a1++) {
*a1 &= ~01;
if (a1>=addr1 && a1<=addr2 && execute(a1)==k)
*a1 |= 01;
}
/*
* Special case: g/.../d (avoid n^2 algorithm)
*/
if (globuf[0]=='d' && globuf[1]=='\n' && globuf[2]=='\0') {
gdelete();
return;
}
for (a1=zero; a1<=dol; a1++) {
if (*a1 & 01) {
*a1 &= ~01;
dot = a1;
globp = globuf;
commands();
a1 = zero;
}
}
}
static void join(void)
{
unsigned char *gp, *lp;
unsigned int *a1;
nonzero();
gp = genbuf;
for (a1=addr1; a1<=addr2; a1++) {
lp = GetLine(*a1);
while (*gp = *lp++)
if (gp++ >= &genbuf[LBSIZE-2])
error(Q);
}
lp = linebuf;
gp = genbuf;
while (*lp++ = *gp++)
;
*addr1 = putline();
if (addr1<addr2)
rdelete(addr1+1, addr2);
dot = addr1;
}
static void substitute(int inglob)
{
int *mp, nl;
unsigned int *a1;
int gsubf;
int n;
n = getnum(); /* OK even if n==0 */
gsubf = compsub();
for (a1 = addr1; a1 <= addr2; a1++) {
if (execute(a1)){
unsigned *ozero;
int m = n;
do {
int span = loc2-loc1;
if (--m <= 0) {
dosub();
if (!gsubf)
break;
if (span==0) { /* null RE match */
if (*loc2=='\0')
break;
loc2++;
}
}
} while (execute((unsigned *)0));
if (m <= 0) {
inglob |= 01;
subnewa = putline();
*a1 &= ~01;
if (anymarks) {
for (mp = names; mp < &names[26]; mp++)
if (*mp == *a1)
*mp = subnewa;
}
subolda = *a1;
*a1 = subnewa;
ozero = zero;
nl = append(getsub, a1);
nl += zero-ozero;
a1 += nl;
addr2 += nl;
}
}
}
if (inglob==0)
error(Q);
}
static int compsub(void)
{
int seof, c;
unsigned char *p;
if ((seof = getchr()) == '\n' || seof == ' ')
error(Q);
compile(seof);
p = rhsbuf;
for (;;) {
c = getchr();
if (c=='\\')
c = getchr() | 0200;
if (c=='\n') {
if (globp && globp[0]) /* last '\n' does not count */
c |= 0200;
else {
peekc = c;
pflag++;
break;
}
}
if (c==seof)
break;
*p++ = c;
if (p >= &rhsbuf[LBSIZE/2])
error(Q);
}
*p++ = 0;
if ((peekc = getchr()) == 'g') {
peekc = 0;
newline();
return(1);
}
newline();
return(0);
}
static int getsub(void)
{
unsigned char *p1, *p2;
p1 = linebuf;
if ((p2 = linebp) == 0)
return(EOF);
while (*p1++ = *p2++)
;
linebp = 0;
return(0);
}
static void dosub(void)
{
unsigned char *lp, *sp, *rp;
int c;
lp = linebuf;
sp = genbuf;
rp = rhsbuf;
while (lp < loc1)
*sp++ = *lp++;
while (c = *rp++&0377) {
if (c=='&') {
sp = place(sp, loc1, loc2);
continue;
} else if (c&0200 && (c &= 0177) >='1' && c < nbra+'1') {
sp = place(sp, braslist[c-'1'], braelist[c-'1']);
continue;
}
*sp++ = c&0177;
if (sp >= &genbuf[LBSIZE])
error(Q);
}
lp = loc2;
loc2 = sp - genbuf + linebuf;
while (*sp++ = *lp++)
if (sp >= &genbuf[LBSIZE])
error(Q);
lp = linebuf;
sp = genbuf;
while (*lp++ = *sp++)
;
}
static unsigned char * place(unsigned char *sp, unsigned char
*l1,unsigned char *l2)
{
while (l1 < l2) {
*sp++ = *l1++;
if (sp >= &genbuf[LBSIZE])
error(Q);
}
return(sp);
}
static void move(int cflag)
{
unsigned int *adt, *ad1, *ad2;
nonzero();
if ((adt = address())==0) /* address() guarantees addr is in range */
error(Q);
newline();
if (cflag) {
unsigned int *ozero;
int delta;
ad1 = dol;
ozero = zero;
append(getcopy, ad1++);
ad2 = dol;
delta = zero - ozero;
ad1 += delta;
adt += delta;
} else {
ad2 = addr2;
for (ad1 = addr1; ad1 <= ad2;)
*ad1++ &= ~01;
ad1 = addr1;
}
ad2++;
if (adt<ad1) {
dot = adt + (ad2-ad1);
if ((++adt)==ad1)
return;
reverse(adt, ad1);
reverse(ad1, ad2);
reverse(adt, ad2);
} else if (adt >= ad2) {
dot = adt++;
reverse(ad1, ad2);
reverse(ad2, adt);
reverse(ad1, adt);
} else
error(Q);
fchange = 1;
}
static void reverse(unsigned int *a1, unsigned int *a2)
{
int t;
for (;;) {
t = *--a2;
if (a2 <= a1)
return;
*a2 = *a1;
*a1++ = t;
}
}
static int getcopy(void)
{
if (addr1 > addr2)
return(EOF);
GetLine(*addr1++);
return(0);
}
static void compile(int eof)
{
int c;
unsigned char *ep;
unsigned char *lastep;
unsigned char bracket[NBRA], *bracketp;
int cclcnt;
ep = expbuf;
bracketp = bracket;
if ((c = getchr()) == '\n') {
peekc = c;
c = eof;
}
if (c == eof) {
if (*ep==0)
error(Q);
return;
}
nbra = 0;
if (c=='^') {
c = getchr();
*ep++ = CCIRC;
}
peekc = c;
lastep = 0;
for (;;) {
if (ep >= &expbuf[ESIZE])
goto cerror;
c = getchr();
if (c == '\n') {
peekc = c;
c = eof;
}
if (c==eof) {
if (bracketp != bracket)
goto cerror;
*ep++ = CEOF;
return;
}
if (c!='*')
lastep = ep;
switch (c) {
case '\\':
if ((c = getchr())=='(') {
if (nbra >= NBRA)
goto cerror;
*bracketp++ = nbra;
*ep++ = CBRA;
*ep++ = nbra++;
continue;
}
if (c == ')') {
if (bracketp <= bracket)
goto cerror;
*ep++ = CKET;
*ep++ = *--bracketp;
continue;
}
if (c>='1' && c<'1'+NBRA) {
*ep++ = CBACK;
*ep++ = c-'1';
continue;
}
*ep++ = CCHR;
if (c=='\n')
goto cerror;
*ep++ = c;
continue;
case '.':
*ep++ = CDOT;
continue;
case '\n':
goto cerror;
case '*':
if (lastep==0 || *lastep==CBRA || *lastep==CKET)
goto defchar;
*lastep |= STAR;
continue;
case '$':
if ((peekc=getchr()) != eof && peekc!='\n')
goto defchar;
*ep++ = CDOL;
continue;
case '[':
*ep++ = CCL;
*ep++ = 0;
cclcnt = 1;
if ((c=getchr()) == '^') {
c = getchr();
ep[-2] = NCCL;
}
do {
if (c=='\n')
goto cerror;
if (c=='-' && ep[-1]!=0) {
if ((c=getchr())==']') {
*ep++ = '-';
cclcnt++;
break;
}
while (ep[-1]<c) {
*ep = ep[-1]+1;
ep++;
cclcnt++;
if (ep>=&expbuf[ESIZE])
goto cerror;
}
}
*ep++ = c;
cclcnt++;
if (ep >= &expbuf[ESIZE])
goto cerror;
} while ((c = getchr()) != ']');
lastep[1] = cclcnt;
continue;
defchar:
default:
*ep++ = CCHR;
*ep++ = c;
}
}
cerror:
expbuf[0] = 0;
nbra = 0;
error(Q);
}
static int execute(unsigned int *addr)
{
unsigned char *p1, *p2;
int c;
for (c=0; c<NBRA; c++) {
braslist[c] = 0;
braelist[c] = 0;
}
p2 = expbuf;
if (addr == (unsigned *)0) {
if (*p2==CCIRC)
return(0);
p1 = loc2;
} else if (addr==zero)
return(0);
else
p1 = GetLine(*addr);
if (*p2==CCIRC) {
loc1 = p1;
return(advance(p1, p2+1));
}
/* fast check for first character */
if (*p2==CCHR) {
c = p2[1];
do {
if (*p1!=c)
continue;
if (advance(p1, p2)) {
loc1 = p1;
return(1);
}
} while (*p1++);
return(0);
}
/* regular algorithm */
do {
if (advance(p1, p2)) {
loc1 = p1;
return(1);
}
} while (*p1++);
return(0);
}
static int advance(unsigned char *lp,unsigned char *ep)
{
unsigned char *curlp;
int i;
for (;;) switch (*ep++) {
case CCHR:
if (*ep++ == *lp++)
continue;
return(0);
case CDOT:
if (*lp++)
continue;
return(0);
case CDOL:
if (*lp==0)
continue;
return(0);
case CEOF:
loc2 = lp;
return(1);
case CCL:
if (cclass(ep, *lp++, 1)) {
ep += *ep;
continue;
}
return(0);
case NCCL:
if (cclass(ep, *lp++, 0)) {
ep += *ep;
continue;
}
return(0);
case CBRA:
braslist[*ep++] = lp;
continue;
case CKET:
braelist[*ep++] = lp;
continue;
case CBACK:
if (braelist[i = *ep++]==0)
error(Q);
if (backref(i, lp)) {
lp += braelist[i] - braslist[i];
continue;
}
return(0);
case CBACK|STAR:
if (braelist[i = *ep++] == 0)
error(Q);
curlp = lp;
while (backref(i, lp))
lp += braelist[i] - braslist[i];
while (lp >= curlp) {
if (advance(lp, ep))
return(1);
lp -= braelist[i] - braslist[i];
}
continue;
case CDOT|STAR:
curlp = lp;
while (*lp++)
;
goto star;
case CCHR|STAR:
curlp = lp;
while (*lp++ == *ep)
;
ep++;
goto star;
case CCL|STAR:
case NCCL|STAR:
curlp = lp;
while (cclass(ep, *lp++, ep[-1]==(CCL|STAR)))
;
ep += *ep;
goto star;
star:
do {
lp--;
if (advance(lp, ep))
return(1);
} while (lp > curlp);
return(0);
default:
error(Q);
}
}
static int backref(int i,unsigned char *lp)
{
unsigned char *bp;
bp = braslist[i];
while (*bp++ == *lp++)
if (bp >= braelist[i])
return(1);
return(0);
}
static int cclass(unsigned char *set, int c, int af)
{
int n;
if (c==0)
return(0);
n = *set++;
while (--n)
if (*set++ == c)
return(af);
return(!af);
}
static void putd(void)
{
int r;
r = count%10;
count /= 10;
if (count)
putd();
putchr(r + '0');
}
static unsigned char line[70];
static unsigned char *linp = line;
static void putchr(int ac)
{
unsigned char *lp;
int c;
lp = linp;
c = ac;
if (listf) {
if (c=='\n') {
if (linp!=line && linp[-1]==' ') {
*lp++ = '\\';
*lp++ = 'n';
}
} else {
if (col > (72-4-2)) {
col = 8;
*lp++ = '\\';
*lp++ = '\n';
*lp++ = '\t';
}
col++;
if (c=='\b' || c=='\t' || c=='\\') {
*lp++ = '\\';
if (c=='\b')
c = 'b';
else if (c=='\t')
c = 't';
col++;
} else if (c<' ' || c=='\177') {
*lp++ = '\\';
*lp++ = (c>>6) +'0';
*lp++ = ((c>>3)&07)+'0';
c = ( c &07)+'0';
col += 3;
}
}
}
*lp++ = c;
if(c == '\n' || lp >= &line[64]) {
linp = line;
write(oflag?2:1, line, lp-line);
return;
}
linp = lp;
}
[toc] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2021-04-22 19:05 +0200 |
| Message-ID | <s5sacc$29s$1@dont-email.me> |
| In reply to | #79175 |
> Recently, somebody started a thread about how C wasn't a simple > language. The excuse was a text editor project for a beginner. It is simple, but as we can see from your code it's actually also very ugly.
[toc] | [prev] | [next] | [standalone]
| From | wij <wyniijj@gmail.com> |
|---|---|
| Date | 2021-04-22 12:41 -0700 |
| Message-ID | <ff0dcc85-70c3-4959-82e6-c0a322bf6f8fn@googlegroups.com> |
| In reply to | #79175 |
On Friday, 23 April 2021 at 00:58:41 UTC+8, jacobnavia wrote:
> Recently, somebody started a thread about how C wasn't a simple
> language. The excuse was a text editor project for a beginner.
>
> Attached is the source code for the 10th edition of Unix of the "ed"
> text editor, published by Brian Kernighan for his CS classes at
> Princeton in 2001.
>
> Maybe C is not a simple language but one of its incredible strengths is
> its stability. This code is around 40 (yes FORTY) years old and you can
> compile it without any trouble today and it will work in any UNIX system.
>
> ----------------------------------------------------------------cut here
> /* This file contains the source for the 10th Edition Unix version of
> ed, which is
> 1700 lines long. It dates from about 1989, but is typical of Unix
> code from the
> mid 1970's: terse, tight, efficient, and largely uncommented
> This is a slightly modified version that compiles without any
> warnings under gcc in 2021
> See https://www.cs.princeton.edu/courses/archive/spring01/cs333/ed.c
> Compile with gcc -O2 -o ed -Wall -Wno-parentheses ed.c
> */
> #include <signal.h>
> #include <stdlib.h>
> #include <setjmp.h>
> #include <stdio.h>
> #include <unistd.h>
> #include <fcntl.h>
> #include <wait.h>
> #include <string.h>
> #define BLKSIZE 4096 /* make BLKSIZE and LBSIZE 512 for smaller machines */
> #define NBLK 2047
> #define FNSIZE 128
> #define LBSIZE 4096
> #define ESIZE 256
> #define GBSIZE 256
> #define NBRA 5
> #define KSIZE 9
> #define CBRA 1
> #define CCHR 2
> #define CDOT 4
> #define CCL 6
> #define NCCL 8
> #define CDOL 10
> #define CEOF 11
> #define CKET 12
> #define CBACK 14
> #define CCIRC 15
> #define STAR 01
> static unsigned char Q[] = "";
> static unsigned char T[] = "TMP";
> #define READ 0
> #define WRITE 1
> static int peekc;
> static int lastc;
> static unsigned char savedfile[FNSIZE];
> static unsigned char file[FNSIZE];
> static unsigned char linebuf[LBSIZE];
> static unsigned char rhsbuf[LBSIZE/2];
> static unsigned char expbuf[ESIZE+4];
> static int given;
> static unsigned int *addr1, *addr2;
> static unsigned int *dot, *dol, *zero;
> static unsigned char genbuf[LBSIZE];
> static long count;
> static unsigned char *nextip;
> static unsigned char *linebp;
> static int ninbuf;
> static int io;
> static int pflag;
> static int vflag = 1;
> static int oflag;
> static int listf;
> static int listn;
> static int col;
> static unsigned char *globp;
> static int tfile = -1;
> static int tline;
> static char tfname[50];
> static unsigned char *loc1;
> static unsigned char *loc2;
> static unsigned char ibuff[BLKSIZE];
> static int iblock = -1;
> static unsigned char obuff[BLKSIZE];
> static int oblock = -1;
> static int ichanged;
> static int nleft;
> static char WRERR[] = "WRITE ERROR";
> static int names[26];
> static int anymarks;
> static unsigned char *braslist[NBRA];
> static unsigned char *braelist[NBRA];
> static int nbra;
> static int subnewa;
> static int subolda;
> static int fchange;
> static int wrapp;
> static unsigned nlall = 128;
> static char tmpXXXXX[50] = "/tmp/eXXXXXX";
>
> static unsigned char *getblock(unsigned int atl, int iof);
> static unsigned char *GetLine(unsigned int tl);
> static unsigned char *place(unsigned char *sp,unsigned char *l1,unsigned
> char *l2);
> static void add(int i);
> static int advance(unsigned char *lp, unsigned char *ep);
> static int append(int (*f)(void), unsigned int *a);
> static int backref(int i,unsigned char *lp);
> static void blkio(int b, unsigned char *buf, int iof);
> static void callunix(void);
> static int cclass(unsigned char *set, int c, int af);
> static void commands(void);
> static void compile(int eof);
> static int compsub(void);
> static void dosub(void);
> static void error(unsigned char *s);
> static int execute(unsigned int *addr);
> static void exfile(void);
> static void filename(int comm);
> static void gdelete(void);
> static int getchr(void);
> static int getcopy(void);
> static int getfile(void);
> static int getnum(void);
> static int getsub(void);
> static int gettty(void);
> static int gety(void);
> static void global(int k);
> static void init(void);
> static unsigned int *address(void);
> static void join(void);
> static void move(int cflag);
> static void newline(void);
> static void nonzero(void);
> static void onhup(int n);
> static void onintr(int n);
> static void print(void);
> static void putchr(int ac);
> static void putd(void);
> static void putfile(void);
> static int putline(void);
> static void quit(int n);
> static void rdelete(unsigned int *ad1, unsigned int *ad2);
> static void reverse(unsigned int *a1, unsigned int *a2);
> static void setwide(void);
> static void setnoaddr(void);
> static void squeeze(int i);
> static void substitute(int inglob);
> static jmp_buf savej;
> typedef void (*SIG_TYP)(int);
> static SIG_TYP oldhup;
> static SIG_TYP oldquit;
> /* these two are not in ansi, but we need them */
> #define SIGHUP 1 /* hangup */
> #define SIGQUIT 3 /* quit (ASCII FS) */
>
> int main(int argc, char *argv[])
> {
> unsigned char *p1, *p2;
> SIG_TYP oldintr;
>
> oldquit = signal(SIGQUIT, SIG_IGN);
> oldhup = signal(SIGHUP, SIG_IGN);
> oldintr = signal(SIGINT, SIG_IGN);
> if (signal(SIGTERM, SIG_IGN) == SIG_DFL)
> signal(SIGTERM, quit);
> argv++;
> while (argc > 1 && **argv=='-') {
> switch((*argv)[1]) {
>
> case '\0':
> vflag = 0;
> break;
>
> case 'q':
> signal(SIGQUIT, SIG_DFL);
> vflag = 1;
> break;
>
> case 'o':
> oflag = 1;
> break;
> }
> argv++;
> argc--;
> }
> if (oflag) {
> p1 = (unsigned char *)"/dev/stdout";
> p2 = savedfile;
> while (*p2++ = *p1++)
> ;
> }
> if (argc>1) {
> p1 = (unsigned char *)*argv;
> p2 = savedfile;
> while (*p2++ = *p1++)
> if (p2 >= &savedfile[sizeof(savedfile)])
> p2--;
> globp = (unsigned char *)"r";
> }
> zero = (unsigned *)malloc(nlall*sizeof(unsigned));
> strcpy(tfname,tmpXXXXX);
> mkstemp(tfname);
>
> init();
> if (oldintr!=SIG_IGN)
> signal(SIGINT, onintr);
> if (oldhup!=SIG_IGN)
> signal(SIGHUP, onhup);
> setjmp(savej);
> commands();
> quit(0);
> return 0;
> }
> static void commands(void)
> {
> unsigned int *a1;
> int c;
> int temp;
> unsigned char lastsep;
>
> for (;;) {
> if (pflag) {
> pflag = 0;
> addr1 = addr2 = dot;
> print();
> }
> c = '\n';
> for (addr1 = 0;;) {
> lastsep = c;
> a1 = address();
> c = getchr();
> if (c!=',' && c!=';')
> break;
> if (lastsep==',')
> error(Q);
> if (a1==0) {
> a1 = zero+1;
> if (a1>dol)
> a1--;
> }
> addr1 = a1;
> if (c==';')
> dot = a1;
> }
> if (lastsep!='\n' && a1==0)
> a1 = dol;
> if ((addr2=a1)==0) {
> given = 0;
> addr2 = dot;
> }
> else
> given = 1;
> if (addr1==0)
> addr1 = addr2;
> switch(c) {
> case 'a':
> add(0);
> continue;
> case 'c':
> nonzero();
> newline();
> rdelete(addr1, addr2);
> append(gettty, addr1-1);
> continue;
> case 'd':
> nonzero();
> newline();
> rdelete(addr1, addr2);
> continue;
> case 'E':
> fchange = 0;
> c = 'e';
> case 'e':
> setnoaddr();
> if (vflag && fchange) {
> fchange = 0;
> error(Q);
> }
> filename(c);
> init();
> addr2 = zero;
> goto caseread;
> case 'f':
> setnoaddr();
> filename(c);
> puts((const char *)savedfile);
> continue;
> case 'g':
> global(1);
> continue;
> case 'i':
> add(-1);
> continue;
> case 'j':
> if (!given)
> addr2++;
> newline();
> join();
> continue;
> case 'k':
> nonzero();
> if ((c = getchr()) < 'a' || c > 'z')
> error(Q);
> newline();
> names[c-'a'] = *addr2 & ~01;
> anymarks |= 01;
> continue;
> case 'm':
> move(0);
> continue;
> case 'n':
> listn++;
> newline();
> print();
> continue;
> case '\n':
> if (a1==0) {
> a1 = dot+1;
> addr2 = a1;
> addr1 = a1;
> }
> if (lastsep==';')
> addr1 = a1;
> print();
> continue;
> case 'l':
> listf++;
> case 'p':
> case 'P':
> newline();
> print();
> continue;
> case 'Q':
> fchange = 0;
> case 'q':
> setnoaddr();
> newline();
> quit(0);
> case 'r':
> filename(c);
> caseread:
> if ((io = open((const char *)file, 0)) < 0) {
> lastc = '\n';
> error(file);
> }
> setwide();
> squeeze(0);
> ninbuf = 0;
> c = zero != dol;
> append(getfile, addr2);
> exfile();
> fchange = c;
> continue;
> case 's':
> nonzero();
> substitute(globp!=0);
> continue;
> case 't':
> move(1);
> continue;
> case 'u':
> nonzero();
> newline();
> if ((*addr2&~01) != subnewa)
> error(Q);
> *addr2 = subolda;
> dot = addr2;
> continue;
> case 'v':
> global(0);
> continue;
> case 'W':
> wrapp++;
> case 'w':
> setwide();
> squeeze(dol>zero);
> if ((temp = getchr()) != 'q' && temp != 'Q') {
> peekc = temp;
> temp = 0;
> }
> filename(c);
> if(!wrapp ||
> ((io = open((const char *)file,1)) == -1) ||
> ((lseek(io, 0L, 2)) == -1))
> if ((io = creat((const char *)file, 0666)) < 0)
> error(file);
> wrapp = 0;
> if (dol > zero)
> putfile();
> exfile();
> if (addr1<=zero+1 && addr2==dol)
> fchange = 0;
> if (temp == 'Q')
> fchange = 0;
> if (temp)
> quit(0);
> continue;
> case '=':
> setwide();
> squeeze(0);
> newline();
> count = addr2 - zero;
> putd();
> putchr('\n');
> continue;
> case '!':
> callunix();
> continue;
> case EOF:
> return;
> }
> error(Q);
> }
> }
> static void print(void)
> {
> unsigned int *a1;
>
> nonzero();
> a1 = addr1;
> do {
> if (listn) {
> count = a1-zero;
> putd();
> putchr('\t');
> }
> puts((const char *)GetLine(*a1++));
> } while (a1 <= addr2);
> dot = addr2;
> listf = 0;
> listn = 0;
> pflag = 0;
> }
> static unsigned int * address(void)
> {
> int sign;
> unsigned int *a, *b;
> int opcnt, nextopand;
> int c;
>
> nextopand = -1;
> sign = 1;
> opcnt = 0;
> a = dot;
> do {
> do c = getchr(); while (c==' ' || c=='\t');
> if ('0'<=c && c<='9') {
> peekc = c;
> if (!opcnt) a = zero;
> a += sign*getnum();
> } else switch (c) {
> case '$':
> a = dol;
> /* fall through */
> case '.':
> if (opcnt) error(Q);
> break;
> case '\'':
> c = getchr();
> if (opcnt || c<'a' || 'z'<c) error(Q);
> a = zero;
> do a++; while (a<=dol && names[c-'a']!=(*a&~01));
> break;
> case '?':
> sign = -sign;
> /* fall through */
> case '/':
> compile(c);
> b = a;
> for (;;) {
> a += sign;
> if (a<=zero) a = dol;
> if (a>dol) a = zero;
> if (execute(a)) break;
> if (a==b) error(Q);
> }
> break;
> default:
> if (nextopand == opcnt) {
> a += sign;
> if (a<zero || dol<a)
> continue; /* error(Q); */
> }
> if (c!='+' && c!='-' && c!='^') {
> peekc = c;
> if (opcnt==0) a = 0;
> return (a);
> }
> sign = 1;
> if (c!='+') sign = -sign;
> nextopand = ++opcnt;
> continue;
> }
> sign = 1;
> opcnt++;
> } while (zero<=a && a<=dol);
> error(Q);
> /*NOTREACHED*/
> return 0;
> }
> static int getnum(void)
> {
> int r, c;
>
> r = 0;
> while ((c=getchr())>='0' && c<='9')
> r = r*10 + c - '0';
> peekc = c;
> return (r);
> }
> static void setwide(void)
> {
> if (!given) {
> addr1 = zero + (dol>zero);
> addr2 = dol;
> }
> }
> static void setnoaddr(void)
> {
> if (given) error(Q);
> }
> static void nonzero(void)
> {
> squeeze(1);
> }
> static void squeeze(int i)
> {
> if (addr1<zero+i || addr2>dol || addr1>addr2)
> error(Q);
> }
> static void newline(void)
> {
> int c;
>
> if ((c = getchr()) == '\n' || c == EOF) return;
> if (c=='p' || c=='l' || c=='n') {
> pflag++;
> if (c=='l') listf++;
> else if (c=='n') listn++;
> if ((c=getchr())=='\n') return;
> }
> error(Q);
> }
> static void filename(int comm)
> {
> unsigned char *p1, *p2;
> int c;
>
> count = 0;
> c = getchr();
> if (c=='\n' || c==EOF) {
> p1 = savedfile;
> if (*p1==0 && comm!='f') error(Q);
> p2 = file;
> while (*p2++ = *p1++)
> ;
> return;
> }
> if (c!=' ')
> error(Q);
> while ((c = getchr()) == ' ')
> ;
> if (c=='\n') error(Q);
> p1 = file;
> do {
> if (p1 >= &file[sizeof(file)-1] || c==' ' || c==EOF) error(Q);
> *p1++ = c;
> } while ((c = getchr()) != '\n');
> *p1++ = 0;
> if (savedfile[0]==0 || comm=='e' || comm=='f') {
> p1 = savedfile;
> p2 = file;
> while (*p1++ = *p2++)
> ;
> }
> }
> static void exfile(void)
> {
> close(io);
> io = -1;
> if (vflag) {
> putd();
> putchr('\n');
> }
> }
> static void onintr(int n)
> {
> signal(SIGINT, onintr);
> putchr('\n');
> lastc = '\n';
> error(Q);
> }
> static void onhup(int n)
> {
> signal(SIGINT, SIG_IGN);
> signal(SIGHUP, SIG_IGN);
> if (dol > zero) {
> addr1 = zero+1;
> addr2 = dol;
> io = creat("ed.hup", 0600);
> if (io > 0)
> putfile();
> }
> fchange = 0;
> quit(0);
> }
> static void error(unsigned char *s)
> {
> int c;
>
> wrapp = 0;
> listf = 0;
> listn = 0;
> putchr('?');
> puts((const char *)s);
> count = 0;
> lseek(0, (long)0, 2);
> pflag = 0;
> if (globp)
> lastc = '\n';
> globp = 0;
> peekc = lastc;
> if(lastc)
> while ((c = getchr()) != '\n' && c != EOF)
> ;
> if (io > 0) {
> close(io);
> io = -1;
> }
> longjmp(savej, 1);
> }
> static int getchr(void)
> {
> char c;
> if (lastc=peekc) {
> peekc = 0;
> return(lastc);
> }
> if (globp) {
> if ((lastc = *globp++) != 0)
> return(lastc);
> globp = 0;
> return(EOF);
> }
> if (read(0, &c, 1) <= 0)
> return(lastc = EOF);
> lastc = c&0177;
> return(lastc);
> }
> static int gettty(void)
> {
> int rc;
>
> if (rc = gety())
> return(rc);
> if (linebuf[0]=='.' && linebuf[1]==0)
> return(EOF);
> return(0);
> }
> static int gety(void)
> {
> int c;
> unsigned char *gf;
> unsigned char *p;
>
> p = linebuf;
> gf = globp;
> while ((c = getchr()) != '\n') {
> if (c==EOF) {
> if (gf)
> peekc = c;
> return(c);
> }
> if ((c &= 0177) == 0)
> continue;
> *p++ = c;
> if (p >= &linebuf[LBSIZE-2])
> error(Q);
> }
>
> *p++ = 0;
> return(0);
> }
> static int getfile(void)
> {
> int c;
> unsigned char *lp, *fp;
>
> lp = linebuf;
> fp = nextip;
> do {
> if (--ninbuf < 0) {
> if ((ninbuf = read(io, genbuf, LBSIZE)-1) < 0)
> if (lp>linebuf) {
> puts("'\\n' appended");
> *genbuf = '\n';
> }
> else return(EOF);
> fp = genbuf;
> while(fp < &genbuf[ninbuf]) {
> if (*fp++ & 0200)
> break;
> }
> fp = genbuf;
> }
> c = *fp++;
> if (c=='\0')
> continue;
> if (c&0200 || lp >= &linebuf[LBSIZE]) {
> lastc = '\n';
> error(Q);
> }
> *lp++ = c;
> count++;
> } while (c != '\n');
> *--lp = 0;
> nextip = fp;
> return(0);
> }
> static void putfile(void)
> {
> unsigned int *a1;
> int n;
> unsigned char *fp, *lp;
> int nib;
>
> nib = BLKSIZE;
> fp = genbuf;
> a1 = addr1;
> do {
> lp = GetLine(*a1++);
> for (;;) {
> if (--nib < 0) {
> n = fp-genbuf;
> if(write(io, genbuf, n) != n) {
> puts((const char *)WRERR);
> error(Q);
> }
> nib = BLKSIZE-1;
> fp = genbuf;
> }
> count++;
> if ((*fp++ = *lp++) == 0) {
> fp[-1] = '\n';
> break;
> }
> }
> } while (a1 <= addr2);
> n = fp-genbuf;
> if(write(io, genbuf, n) != n) {
> puts((const char *)WRERR);
> error(Q);
> }
> }
> static int append(int (*f)(void), unsigned int *a)
> {
> unsigned int *a1, *a2, *rdot;
> int nline, tl;
>
> nline = 0;
> dot = a;
> while ((*f)() == 0) {
> if ((dol-zero)+1 >= nlall) {
> unsigned *ozero = zero;
>
> nlall += 1024;
> if ((zero = (unsigned *)realloc((unsigned char *)zero,
> nlall*sizeof(unsigned)))==NULL) {
> error((unsigned char *)"MEM?");
> onhup(0);
> }
> dot += zero - ozero;
> dol += zero - ozero;
> }
> tl = putline();
> nline++;
> a1 = ++dol;
> a2 = a1+1;
> rdot = ++dot;
> while (a1 > rdot)
> *--a2 = *--a1;
> *rdot = tl;
> }
> return(nline);
> }
> static void add(int i)
> {
> if (i && (given || dol>zero)) {
> addr1--;
> addr2--;
> }
> squeeze(0);
> newline();
> append(gettty, addr2);
> }
> static void callunix(void)
> {
> SIG_TYP savint;
> int pid, rpid;
> int retcode;
>
> setnoaddr();
> if ((pid = fork()) == 0) {
> signal(SIGHUP, oldhup);
> signal(SIGQUIT, oldquit);
> execl("/bin/sh", "sh", "-t", NULL);
> exit(0100);
> }
> savint = signal(SIGINT, SIG_IGN);
> while ((rpid = wait(&retcode)) != pid && rpid != -1)
> ;
> signal(SIGINT, savint);
> if (vflag) {
> puts("!");
> }
> }
> static void quit(int n)
> {
> if (vflag && fchange && dol!=zero) {
> fchange = 0;
> error(Q);
> }
> unlink(tfname);
> exit(0);
> }
> static void rdelete(unsigned int *ad1, unsigned int *ad2)
> {
> unsigned int *a1, *a2, *a3;
>
> a1 = ad1;
> a2 = ad2+1;
> a3 = dol;
> dol -= a2 - a1;
> do {
> *a1++ = *a2++;
> } while (a2 <= a3);
> a1 = ad1;
> if (a1 > dol)
> a1 = dol;
> dot = a1;
> fchange = 1;
> }
> static void gdelete(void)
> {
> unsigned int *a1, *a2, *a3;
>
> a3 = dol;
> for (a1=zero; (*a1&01)==0; a1++)
> if (a1>=a3)
> return;
> for (a2=a1+1; a2<=a3;) {
> if (*a2&01) {
> a2++;
> dot = a1;
> } else
> *a1++ = *a2++;
> }
> dol = a1-1;
> if (dot>dol)
> dot = dol;
> fchange = 1;
> }
> static unsigned char *GetLine(unsigned int tl)
> {
> unsigned char *bp, *lp;
> int nl;
>
> lp = linebuf;
> bp = getblock(tl, READ);
> nl = nleft;
> tl &= ~((BLKSIZE/2)-1);
> while (*lp++ = *bp++)
> if (--nl == 0) {
> bp = getblock(tl+=(BLKSIZE/2), READ);
> nl = nleft;
> }
> return(linebuf);
> }
> static int putline(void)
> {
> unsigned char *bp, *lp;
> int nl;
> unsigned int tl;
>
> fchange = 1;
> lp = linebuf;
> tl = tline;
> bp = getblock(tl, WRITE);
> nl = nleft;
> tl &= ~((BLKSIZE/2)-1);
> while (*bp = *lp++) {
> if (*bp++ == '\n') {
> *--bp = 0;
> linebp = lp;
> break;
> }
> if (--nl == 0) {
> bp = getblock(tl+=(BLKSIZE/2), WRITE);
> nl = nleft;
> }
> }
> nl = tline;
> tline += (((lp-linebuf)+03)>>1)&077776;
> return(nl);
> }
> static unsigned char * getblock(unsigned int atl, int iof)
> {
> int bno, off;
>
> bno = (atl/(BLKSIZE/2));
> off = (atl<<1) & (BLKSIZE-1) & ~03;
> if (bno >= NBLK) {
> lastc = '\n';
> error(T);
> }
> nleft = BLKSIZE - off;
> if (bno==iblock) {
> ichanged |= iof;
> return(ibuff+off);
> }
> if (bno==oblock)
> return(obuff+off);
> if (iof==READ) {
> if (ichanged)
> blkio(iblock, ibuff, iof);
> ichanged = 0;
> iblock = bno;
> blkio(bno, ibuff, iof);
> return(ibuff+off);
> }
> if (oblock>=0)
> blkio(oblock, obuff, iof);
> oblock = bno;
> return(obuff+off);
> }
> static void blkio(int b, unsigned char *buf, int iof)
> {
> lseek(tfile, (long)b*BLKSIZE, 0);
> if (iof == READ) {
> if (read(tfile, buf, BLKSIZE) != BLKSIZE) {
> error(T);
> }
> }
> else {
> if (write(tfile, buf, BLKSIZE) != BLKSIZE) {
> error(T);
> }
> }
> }
> static void init(void)
> {
> int *markp;
>
> close(tfile);
> tline = 2;
> for (markp = names; markp < &names[26]; )
> *markp++ = 0;
> subnewa = 0;
> anymarks = 0;
> iblock = -1;
> oblock = -1;
> ichanged = 0;
> close(creat(tfname, 0600));
> tfile = open(tfname, 2);
> dot = dol = zero;
> }
> static void global(int k)
> {
> unsigned char *gp;
> int c;
> unsigned int *a1;
> unsigned char globuf[GBSIZE];
>
> if (globp)
> error(Q);
> setwide();
> squeeze(dol>zero);
> if ((c=getchr())=='\n')
> error(Q);
> compile(c);
> gp = globuf;
> while ((c = getchr()) != '\n') {
> if (c==EOF)
> error(Q);
> if (c=='\\') {
> c = getchr();
> if (c!='\n')
> *gp++ = '\\';
> }
> *gp++ = c;
> if (gp >= &globuf[GBSIZE-2])
> error(Q);
> }
> if (gp == globuf)
> *gp++ = 'p';
> *gp++ = '\n';
> *gp++ = 0;
> for (a1=zero; a1<=dol; a1++) {
> *a1 &= ~01;
> if (a1>=addr1 && a1<=addr2 && execute(a1)==k)
> *a1 |= 01;
> }
> /*
> * Special case: g/.../d (avoid n^2 algorithm)
> */
> if (globuf[0]=='d' && globuf[1]=='\n' && globuf[2]=='\0') {
> gdelete();
> return;
> }
> for (a1=zero; a1<=dol; a1++) {
> if (*a1 & 01) {
> *a1 &= ~01;
> dot = a1;
> globp = globuf;
> commands();
> a1 = zero;
> }
> }
> }
> static void join(void)
> {
> unsigned char *gp, *lp;
> unsigned int *a1;
>
> nonzero();
> gp = genbuf;
> for (a1=addr1; a1<=addr2; a1++) {
> lp = GetLine(*a1);
> while (*gp = *lp++)
> if (gp++ >= &genbuf[LBSIZE-2])
> error(Q);
> }
> lp = linebuf;
> gp = genbuf;
> while (*lp++ = *gp++)
> ;
> *addr1 = putline();
> if (addr1<addr2)
> rdelete(addr1+1, addr2);
> dot = addr1;
> }
> static void substitute(int inglob)
> {
> int *mp, nl;
> unsigned int *a1;
> int gsubf;
> int n;
>
> n = getnum(); /* OK even if n==0 */
> gsubf = compsub();
> for (a1 = addr1; a1 <= addr2; a1++) {
> if (execute(a1)){
> unsigned *ozero;
> int m = n;
> do {
> int span = loc2-loc1;
> if (--m <= 0) {
> dosub();
> if (!gsubf)
> break;
> if (span==0) { /* null RE match */
> if (*loc2=='\0')
> break;
> loc2++;
> }
> }
> } while (execute((unsigned *)0));
> if (m <= 0) {
> inglob |= 01;
> subnewa = putline();
> *a1 &= ~01;
> if (anymarks) {
> for (mp = names; mp < &names[26]; mp++)
> if (*mp == *a1)
> *mp = subnewa;
> }
> subolda = *a1;
> *a1 = subnewa;
> ozero = zero;
> nl = append(getsub, a1);
> nl += zero-ozero;
> a1 += nl;
> addr2 += nl;
> }
> }
> }
> if (inglob==0)
> error(Q);
> }
> static int compsub(void)
> {
> int seof, c;
> unsigned char *p;
>
> if ((seof = getchr()) == '\n' || seof == ' ')
> error(Q);
> compile(seof);
> p = rhsbuf;
> for (;;) {
> c = getchr();
> if (c=='\\')
> c = getchr() | 0200;
> if (c=='\n') {
> if (globp && globp[0]) /* last '\n' does not count */
> c |= 0200;
> else {
> peekc = c;
> pflag++;
> break;
> }
> }
> if (c==seof)
> break;
> *p++ = c;
> if (p >= &rhsbuf[LBSIZE/2])
> error(Q);
> }
> *p++ = 0;
> if ((peekc = getchr()) == 'g') {
> peekc = 0;
> newline();
> return(1);
> }
> newline();
> return(0);
> }
> static int getsub(void)
> {
> unsigned char *p1, *p2;
>
> p1 = linebuf;
> if ((p2 = linebp) == 0)
> return(EOF);
> while (*p1++ = *p2++)
> ;
> linebp = 0;
> return(0);
> }
> static void dosub(void)
> {
> unsigned char *lp, *sp, *rp;
> int c;
>
> lp = linebuf;
> sp = genbuf;
> rp = rhsbuf;
> while (lp < loc1)
> *sp++ = *lp++;
> while (c = *rp++&0377) {
> if (c=='&') {
> sp = place(sp, loc1, loc2);
> continue;
> } else if (c&0200 && (c &= 0177) >='1' && c < nbra+'1') {
> sp = place(sp, braslist[c-'1'], braelist[c-'1']);
> continue;
> }
> *sp++ = c&0177;
> if (sp >= &genbuf[LBSIZE])
> error(Q);
> }
> lp = loc2;
> loc2 = sp - genbuf + linebuf;
> while (*sp++ = *lp++)
> if (sp >= &genbuf[LBSIZE])
> error(Q);
> lp = linebuf;
> sp = genbuf;
> while (*lp++ = *sp++)
> ;
> }
> static unsigned char * place(unsigned char *sp, unsigned char
> *l1,unsigned char *l2)
> {
>
> while (l1 < l2) {
> *sp++ = *l1++;
> if (sp >= &genbuf[LBSIZE])
> error(Q);
> }
> return(sp);
> }
> static void move(int cflag)
> {
> unsigned int *adt, *ad1, *ad2;
>
> nonzero();
> if ((adt = address())==0) /* address() guarantees addr is in range */
> error(Q);
> newline();
> if (cflag) {
> unsigned int *ozero;
> int delta;
>
> ad1 = dol;
> ozero = zero;
> append(getcopy, ad1++);
> ad2 = dol;
> delta = zero - ozero;
> ad1 += delta;
> adt += delta;
> } else {
> ad2 = addr2;
> for (ad1 = addr1; ad1 <= ad2;)
> *ad1++ &= ~01;
> ad1 = addr1;
> }
> ad2++;
> if (adt<ad1) {
> dot = adt + (ad2-ad1);
> if ((++adt)==ad1)
> return;
> reverse(adt, ad1);
> reverse(ad1, ad2);
> reverse(adt, ad2);
> } else if (adt >= ad2) {
> dot = adt++;
> reverse(ad1, ad2);
> reverse(ad2, adt);
> reverse(ad1, adt);
> } else
> error(Q);
> fchange = 1;
> }
> static void reverse(unsigned int *a1, unsigned int *a2)
> {
> int t;
>
> for (;;) {
> t = *--a2;
> if (a2 <= a1)
> return;
> *a2 = *a1;
> *a1++ = t;
> }
> }
> static int getcopy(void)
> {
> if (addr1 > addr2)
> return(EOF);
> GetLine(*addr1++);
> return(0);
> }
> static void compile(int eof)
> {
> int c;
> unsigned char *ep;
> unsigned char *lastep;
> unsigned char bracket[NBRA], *bracketp;
> int cclcnt;
>
> ep = expbuf;
> bracketp = bracket;
> if ((c = getchr()) == '\n') {
> peekc = c;
> c = eof;
> }
> if (c == eof) {
> if (*ep==0)
> error(Q);
> return;
> }
> nbra = 0;
> if (c=='^') {
> c = getchr();
> *ep++ = CCIRC;
> }
> peekc = c;
> lastep = 0;
> for (;;) {
> if (ep >= &expbuf[ESIZE])
> goto cerror;
> c = getchr();
> if (c == '\n') {
> peekc = c;
> c = eof;
> }
> if (c==eof) {
> if (bracketp != bracket)
> goto cerror;
> *ep++ = CEOF;
> return;
> }
> if (c!='*')
> lastep = ep;
> switch (c) {
>
> case '\\':
> if ((c = getchr())=='(') {
> if (nbra >= NBRA)
> goto cerror;
> *bracketp++ = nbra;
> *ep++ = CBRA;
> *ep++ = nbra++;
> continue;
> }
> if (c == ')') {
> if (bracketp <= bracket)
> goto cerror;
> *ep++ = CKET;
> *ep++ = *--bracketp;
> continue;
> }
> if (c>='1' && c<'1'+NBRA) {
> *ep++ = CBACK;
> *ep++ = c-'1';
> continue;
> }
> *ep++ = CCHR;
> if (c=='\n')
> goto cerror;
> *ep++ = c;
> continue;
>
> case '.':
> *ep++ = CDOT;
> continue;
>
> case '\n':
> goto cerror;
>
> case '*':
> if (lastep==0 || *lastep==CBRA || *lastep==CKET)
> goto defchar;
> *lastep |= STAR;
> continue;
>
> case '$':
> if ((peekc=getchr()) != eof && peekc!='\n')
> goto defchar;
> *ep++ = CDOL;
> continue;
>
> case '[':
> *ep++ = CCL;
> *ep++ = 0;
> cclcnt = 1;
> if ((c=getchr()) == '^') {
> c = getchr();
> ep[-2] = NCCL;
> }
> do {
> if (c=='\n')
> goto cerror;
> if (c=='-' && ep[-1]!=0) {
> if ((c=getchr())==']') {
> *ep++ = '-';
> cclcnt++;
> break;
> }
> while (ep[-1]<c) {
> *ep = ep[-1]+1;
> ep++;
> cclcnt++;
> if (ep>=&expbuf[ESIZE])
> goto cerror;
> }
> }
> *ep++ = c;
> cclcnt++;
> if (ep >= &expbuf[ESIZE])
> goto cerror;
> } while ((c = getchr()) != ']');
> lastep[1] = cclcnt;
> continue;
>
> defchar:
> default:
> *ep++ = CCHR;
> *ep++ = c;
> }
> }
> cerror:
> expbuf[0] = 0;
> nbra = 0;
> error(Q);
> }
> static int execute(unsigned int *addr)
> {
> unsigned char *p1, *p2;
> int c;
>
> for (c=0; c<NBRA; c++) {
> braslist[c] = 0;
> braelist[c] = 0;
> }
> p2 = expbuf;
> if (addr == (unsigned *)0) {
> if (*p2==CCIRC)
> return(0);
> p1 = loc2;
> } else if (addr==zero)
> return(0);
> else
> p1 = GetLine(*addr);
> if (*p2==CCIRC) {
> loc1 = p1;
> return(advance(p1, p2+1));
> }
> /* fast check for first character */
> if (*p2==CCHR) {
> c = p2[1];
> do {
> if (*p1!=c)
> continue;
> if (advance(p1, p2)) {
> loc1 = p1;
> return(1);
> }
> } while (*p1++);
> return(0);
> }
> /* regular algorithm */
> do {
> if (advance(p1, p2)) {
> loc1 = p1;
> return(1);
> }
> } while (*p1++);
> return(0);
> }
> static int advance(unsigned char *lp,unsigned char *ep)
> {
> unsigned char *curlp;
> int i;
>
> for (;;) switch (*ep++) {
>
> case CCHR:
> if (*ep++ == *lp++)
> continue;
> return(0);
>
> case CDOT:
> if (*lp++)
> continue;
> return(0);
>
> case CDOL:
> if (*lp==0)
> continue;
> return(0);
>
> case CEOF:
> loc2 = lp;
> return(1);
>
> case CCL:
> if (cclass(ep, *lp++, 1)) {
> ep += *ep;
> continue;
> }
> return(0);
>
> case NCCL:
> if (cclass(ep, *lp++, 0)) {
> ep += *ep;
> continue;
> }
> return(0);
>
> case CBRA:
> braslist[*ep++] = lp;
> continue;
>
> case CKET:
> braelist[*ep++] = lp;
> continue;
>
> case CBACK:
> if (braelist[i = *ep++]==0)
> error(Q);
> if (backref(i, lp)) {
> lp += braelist[i] - braslist[i];
> continue;
> }
> return(0);
>
> case CBACK|STAR:
> if (braelist[i = *ep++] == 0)
> error(Q);
> curlp = lp;
> while (backref(i, lp))
> lp += braelist[i] - braslist[i];
> while (lp >= curlp) {
> if (advance(lp, ep))
> return(1);
> lp -= braelist[i] - braslist[i];
> }
> continue;
>
> case CDOT|STAR:
> curlp = lp;
> while (*lp++)
> ;
> goto star;
>
> case CCHR|STAR:
> curlp = lp;
> while (*lp++ == *ep)
> ;
> ep++;
> goto star;
>
> case CCL|STAR:
> case NCCL|STAR:
> curlp = lp;
> while (cclass(ep, *lp++, ep[-1]==(CCL|STAR)))
> ;
> ep += *ep;
> goto star;
>
> star:
> do {
> lp--;
> if (advance(lp, ep))
> return(1);
> } while (lp > curlp);
> return(0);
>
> default:
> error(Q);
> }
> }
> static int backref(int i,unsigned char *lp)
> {
> unsigned char *bp;
>
> bp = braslist[i];
> while (*bp++ == *lp++)
> if (bp >= braelist[i])
> return(1);
> return(0);
> }
> static int cclass(unsigned char *set, int c, int af)
> {
> int n;
>
> if (c==0)
> return(0);
> n = *set++;
> while (--n)
> if (*set++ == c)
> return(af);
> return(!af);
> }
> static void putd(void)
> {
> int r;
>
> r = count%10;
> count /= 10;
> if (count)
> putd();
> putchr(r + '0');
> }
> static unsigned char line[70];
> static unsigned char *linp = line;
> static void putchr(int ac)
> {
> unsigned char *lp;
> int c;
>
> lp = linp;
> c = ac;
> if (listf) {
> if (c=='\n') {
> if (linp!=line && linp[-1]==' ') {
> *lp++ = '\\';
> *lp++ = 'n';
> }
> } else {
> if (col > (72-4-2)) {
> col = 8;
> *lp++ = '\\';
> *lp++ = '\n';
> *lp++ = '\t';
> }
> col++;
> if (c=='\b' || c=='\t' || c=='\\') {
> *lp++ = '\\';
> if (c=='\b')
> c = 'b';
> else if (c=='\t')
> c = 't';
> col++;
> } else if (c<' ' || c=='\177') {
> *lp++ = '\\';
> *lp++ = (c>>6) +'0';
> *lp++ = ((c>>3)&07)+'0';
> c = ( c &07)+'0';
> col += 3;
> }
> }
> }
> *lp++ = c;
> if(c == '\n' || lp >= &line[64]) {
> linp = line;
> write(oflag?2:1, line, lp-line);
> return;
> }
> linp = lp;
> }
Never mind with such kind of people (this example is too complex for him).
When feeling is good, C++ is a super-set of C
When feeling is not so good, C++ is not C.
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2021-04-23 07:38 +0000 |
| Message-ID | <s5tthu$q2b$3@gioia.aioe.org> |
| In reply to | #79184 |
wij <wyniijj@gmail.com> wrote: > Never mind with such kind of people (this example is too complex for him). > When feeling is good, C++ is a super-set of C > When feeling is not so good, C++ is not C. You had to quote 1600 lines of code in order to say that? You do know that you can edit and shorten quotes, don't you?
[toc] | [prev] | [next] | [standalone]
| From | wij <wyniijj@gmail.com> |
|---|---|
| Date | 2021-04-23 05:12 -0700 |
| Message-ID | <94b4e0ad-5460-4974-a2e3-b0cbfb347c98n@googlegroups.com> |
| In reply to | #79189 |
On Friday, 23 April 2021 at 15:38:49 UTC+8, Juha Nieminen wrote:
> wij <wyn...@gmail.com> wrote:
> > Never mind with such kind of people (this example is too complex for him).
> > When feeling is good, C++ is a super-set of C
> > When feeling is not so good, C++ is not C.
> You had to quote 1600 lines of code in order to say that?
>
> You do know that you can edit and shorten quotes, don't you?
I do not really understand what 'quote' means here. So I reply
I can rewrite/translate ed.c as presented, using my own library easily and even
more powerfully and flexibly (file size would be larger and consume more resources).
My codes will not use 'stream stuff'(AT&T implicitly makes people believe it is the
'standard' everybody should follow), I use pure C-lib function calls, I follow the
standard C++ must follow as well.
If you rewrite ed.c (not yet a fully functional text editor) in pure C++,
what would be it look like? What would your evaluation be?
E.g. Let along signal and setjmp (and streaming I/O), I just pick one randomly.
I guess putchr('\n') might be translated to std::cout << std::endl. The point
here is std::endl. Could it be more elegantly and efficiently than
#define ENDL "\n" (C code)?
Maybe you would say you are talking memory allocation issues, but basically,
the main issue of your question is "No, C is not a simple language".
[toc] | [prev] | [next] | [standalone]
| From | red floyd <myob@its.invalid> |
|---|---|
| Date | 2021-04-23 09:24 -0700 |
| Message-ID | <s5use2$546$1@redfloyd.dont-email.me> |
| In reply to | #79198 |
On 4/23/2021 5:12 AM, wij wrote: > On Friday, 23 April 2021 at 15:38:49 UTC+8, Juha Nieminen wrote: >> wij <wyn...@gmail.com> wrote: >>> Never mind with such kind of people (this example is too complex for him). >>> When feeling is good, C++ is a super-set of C >>> When feeling is not so good, C++ is not C. >> You had to quote 1600 lines of code in order to say that? >> >> You do know that you can edit and shorten quotes, don't you? > > I do not really understand what 'quote' means here. [redacted] What he means is, you didn't have to repost the entire C program to make a four line comment on it. You could have edited it out, as I did here with most of your comment
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@alumni.caltech.edu> |
|---|---|
| Date | 2021-04-23 16:59 -0400 |
| Message-ID | <s5vcet$tme$1@dont-email.me> |
| In reply to | #79198 |
On 4/23/21 8:12 AM, wij wrote: > On Friday, 23 April 2021 at 15:38:49 UTC+8, Juha Nieminen wrote: >> wij <wyn...@gmail.com> wrote: >>> Never mind with such kind of people (this example is too complex for him). >>> When feeling is good, C++ is a super-set of C >>> When feeling is not so good, C++ is not C. >> You had to quote 1600 lines of code in order to say that? >> >> You do know that you can edit and shorten quotes, don't you? > > I do not really understand what 'quote' means here. When you send a message containing material not written by you (such as part of the message that you're responding to) it's called a quotation, or simply a quote. The general rule you should follow on usenet when quoting material from the message you're responding to is to quote as little as possible, while providing other readers enough information so they can understand the context of your response. The only thing you actually said about the material you were responding to was that it was too complex. The most I would have quoted from the previous message would have been the following: On Friday, 23 April 2021 at 00:58:41 UTC+8, jacobnavia wrote: > Recently, somebody started a thread about how C wasn't a simple > language. The excuse was a text editor project for a beginner. > > Attached is the source code for the 10th edition of Unix of the "ed" > text editor, published by Brian Kernighan for his CS classes at > Princeton in 2001. > > Maybe C is not a simple language but one of its incredible strengths is > its stability. This code is around 40 (yes FORTY) years old and you can > compile it without any trouble today and it will work in any UNIX system. > > ----------------------------------------------------------------cut here > /* This file contains the source for the 10th Edition Unix version of > ed, which is > 1700 lines long. It dates from about 1989, but is typical of Unix > code from the > mid 1970's: terse, tight, efficient, and largely uncommented > This is a slightly modified version that compiles without any > warnings under gcc in 2021 "1700 lines", and "terse, tight, efficient, and largely uncommented" is all the context you really needed to establish.
[toc] | [prev] | [next] | [standalone]
| From | wij <wyniijj@gmail.com> |
|---|---|
| Date | 2021-04-23 21:43 -0700 |
| Message-ID | <bcc6f32e-b750-4b91-b0a1-37825f26adc1n@googlegroups.com> |
| In reply to | #79222 |
On Saturday, 24 April 2021 at 04:59:23 UTC+8, james...@alumni.caltech.edu wrote: > On 4/23/21 8:12 AM, wij wrote: > > On Friday, 23 April 2021 at 15:38:49 UTC+8, Juha Nieminen wrote: > >> wij <wyn...@gmail.com> wrote: > >>> Never mind with such kind of people (this example is too complex for him). > >>> When feeling is good, C++ is a super-set of C > >>> When feeling is not so good, C++ is not C. > >> You had to quote 1600 lines of code in order to say that? > >> > >> You do know that you can edit and shorten quotes, don't you? > > > > I do not really understand what 'quote' means here. > When you send a message containing material not written by you (such as > part of the message that you're responding to) it's called a quotation, > or simply a quote. > The general rule you should follow on usenet when quoting material from > the message you're responding to is to quote as little as possible, > while providing other readers enough information so they can understand > the context of your response. The only thing you actually said about the > material you were responding to was that it was too complex. The most I > would have quoted from the previous message would have been the following: > On Friday, 23 April 2021 at 00:58:41 UTC+8, jacobnavia wrote: > > Recently, somebody started a thread about how C wasn't a simple > > language. The excuse was a text editor project for a beginner. > > > > Attached is the source code for the 10th edition of Unix of the "ed" > > text editor, published by Brian Kernighan for his CS classes at > > Princeton in 2001. > > > > Maybe C is not a simple language but one of its incredible strengths is > > its stability. This code is around 40 (yes FORTY) years old and you can > > compile it without any trouble today and it will work in any UNIX system. > > > > ----------------------------------------------------------------cut here > > /* This file contains the source for the 10th Edition Unix version of > > ed, which is > > 1700 lines long. It dates from about 1989, but is typical of Unix > > code from the > > mid 1970's: terse, tight, efficient, and largely uncommented > > This is a slightly modified version that compiles without any > > warnings under gcc in 2021 > "1700 lines", and "terse, tight, efficient, and largely uncommented" is > all the context you really needed to establish. I like the style you did. But too formal in "Conversation". When looking back, many of my replies are somewhat indirect. But I would like to keep this way for a moment. Almost all UTM programs (executables) can be analyzed or viewed as a decision graph (note that physical objects and more abstract things are similar). What the so called high level language does are actually about decision and grouping of those left non-decision parts. The desired, net result is to reduce or hide those 'unusual low level' decision makings and non-decision parts from programmers (or theorists). The real implements of these two parts depend on problem domain. As to the issue that C++ codes are simpler and easier. In basics I agree, but probably not much different from other popular high level languages in application, except the method. Approaching the more general problem from the real world of software engineer market, while people saying C++ is the choice for high performance language, such as gaming, banking, AI, VR,..., I saw the need for C++ engineer shrinks very fast these years. Most companies are trying to reduce cost by shrinking existing C++ codes to minimal. Fortran, Cobol are still active and high paid for 'high performance' need. To be more on topic, to say C++ is better over C, support from hardware is a must.
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2021-04-24 17:05 +1200 |
| Message-ID | <iehn98FehboU1@mid.individual.net> |
| In reply to | #79236 |
On 24/04/2021 16:43, wij wrote: > > As to the issue that C++ codes are simpler and easier. In basics I agree, but > probably not much different from other popular high level languages in > application, except the method. > > Approaching the more general problem from the real world of software engineer market, > while people saying C++ is the choice for high performance language, such as gaming, > banking, AI, VR,..., I saw the need for C++ engineer shrinks very fast these years. > Most companies are trying to reduce cost by shrinking existing C++ codes to minimal. > Fortran, Cobol are still active and high paid for 'high performance' need. We have the opposite problem, our ever expanding core product is C++ with no viable alternatives. With our borders basically shut, we are also finding it hard to get staff... > To be more on topic, to say C++ is better over C, support from hardware is a must. They are pretty much equal except at the extreme low end, that is targets not supported by gcc. -- Ian.
[toc] | [prev] | [next] | [standalone]
| From | wij <wyniijj@gmail.com> |
|---|---|
| Date | 2021-04-24 00:50 -0700 |
| Message-ID | <87a30c79-2fd3-42b2-9dea-3797e8a870c3n@googlegroups.com> |
| In reply to | #79237 |
On Saturday, 24 April 2021 at 13:06:00 UTC+8, Ian Collins wrote: > On 24/04/2021 16:43, wij wrote: > > > > As to the issue that C++ codes are simpler and easier. In basics I agree, but > > probably not much different from other popular high level languages in > > application, except the method. > > > > Approaching the more general problem from the real world of software engineer market, > > while people saying C++ is the choice for high performance language, such as gaming, > > banking, AI, VR,..., I saw the need for C++ engineer shrinks very fast these years. > > Most companies are trying to reduce cost by shrinking existing C++ codes to minimal. > > Fortran, Cobol are still active and high paid for 'high performance' need. > We have the opposite problem, our ever expanding core product is C++ > with no viable alternatives. With our borders basically shut, we are > also finding it hard to get staff... Make me think of QT,..., How it survive is interesting. I understand why most of companies in my country abandon C++, because investment on hardware is cheaper and efficient than on software. > > To be more on topic, to say C++ is better over C, support from hardware is a must. > They are pretty much equal except at the extreme low end, that is > targets not supported by gcc. > > -- > Ian.
[toc] | [prev] | [next] | [standalone]
| From | "daniel...@gmail.com" <danielaparker@gmail.com> |
|---|---|
| Date | 2021-04-24 08:50 -0700 |
| Message-ID | <730b6601-8f16-4247-b2e3-02a7afca6a67n@googlegroups.com> |
| In reply to | #79238 |
On Saturday, April 24, 2021 at 3:50:10 AM UTC-4, wyn...@gmail.com wrote: > Make me think of QT,..., How it survive is interesting. > > I understand why most of companies in my country abandon C++, because investment > on hardware is cheaper and efficient than on software. In my field, capital markets, in the places I worked, C and C++ were dominant in the mid to early nineties, in all of IT data processing, middleware and messaging vendors, internal trading systems, and vendor trading systems. That started to change in the late nineties with the advent of Java. It happened first in IT, where Java quickly replaced C and C++ for internal data processing. Then the middleware and messaging vendors such as IBM and TIBCO dropped their legacy C products and replaced them with new products written in Java. This happened simultaneously with the requirement to support XML and related technologies, networking, multi-threading and Unicode, all of which were well supported in Java, and less so in C++. Then vendor derivative trading systems written in Java such as Calypso began to replace the older systems written in C++ such as Infinity. C++ survived in quant groups and vetting groups into the 2000's, but began to feel competition from C# into the late 2000's. Then, new vendors risk systems that involved many millions of portfolio valuations appeared in C#, replacing a previous generation written in C and C++. There were of course a lot of reasons for that, as I'm sure everybody knows. Some of it had to do with working with data, third party C++ library API's were abysmal for working with XML and other data forms, still are, and that won't change until C++ has meta programming, which likely won't be until 2026. It was vastly easier for vendors to distribute user extendable software in Java or C# than in C++. C++ lacked and still does basic types such as big decimal and date. Java and C# were more productive to work in than C++. Massively parallel execution could substitute to some extent for fast execution in a single process. There were many reasons. There were some gains for C++ in the last decade, particularly in high frequency trading, where performance is paramount. But it became niche. Daniel
[toc] | [prev] | [next] | [standalone]
| From | wij <wyniijj@gmail.com> |
|---|---|
| Date | 2021-04-24 10:41 -0700 |
| Message-ID | <3f730976-e40b-4b3d-8b67-0415c1b99fd5n@googlegroups.com> |
| In reply to | #79241 |
On Saturday, 24 April 2021 at 23:51:07 UTC+8, daniel...@gmail.com wrote: > On Saturday, April 24, 2021 at 3:50:10 AM UTC-4, wyn...@gmail.com wrote: > > Make me think of QT,..., How it survive is interesting. > > > > I understand why most of companies in my country abandon C++, because investment > > on hardware is cheaper and efficient than on software. > In my field, capital markets, in the places I worked, C and C++ were dominant > in the mid to early nineties, in all of IT data processing, middleware and messaging > vendors, internal trading systems, and vendor trading systems. That started to change in > the late nineties with the advent of Java. It happened first in IT, where Java quickly > replaced C and C++ for internal data processing. Then the middleware and messaging > vendors such as IBM and TIBCO dropped their legacy C products and replaced them > with new products written in Java. This happened simultaneously with the requirement to > support XML and related technologies, networking, multi-threading and Unicode, all > of which were well supported in Java, and less so in C++. Then vendor derivative > trading systems written in Java such as Calypso began to replace the older systems > written in C++ such as Infinity. C++ survived in quant groups and vetting groups > into the 2000's, but began to feel competition from C# into the late 2000's. Then, > new vendors risk systems that involved many millions of portfolio valuations > appeared in C#, replacing a previous generation written in C and C++. > > There were of course a lot of reasons for that, as I'm sure everybody knows. Some of > it had to do with working with data, third party C++ library API's were abysmal for > working with XML and other data forms, still are, and that won't change until C++ has > meta programming, which likely won't be until 2026. It was vastly easier for vendors to > distribute user extendable software in Java or C# than in C++. C++ lacked and still does > basic types such as big decimal and date. Java and C# were more productive to > work in than C++. Massively parallel execution could substitute to some extent > for fast execution in a single process. There were many reasons. > > There were some gains for C++ in the last decade, particularly in high frequency > trading, where performance is paramount. But it became niche. > > Daniel Why bother (from the view point of a company)? Just wait for another couple years, every thing will automatically be faster and cheaper, while programmer is a very unpredictable factor, and might demand more pay. I used to think neural network is a dead end, but after BP algorithm is found every changes and changed fast. I just hope the same thing happen to C++.
[toc] | [prev] | [next] | [standalone]
| From | Öö Tiib <ootiib@hot.ee> |
|---|---|
| Date | 2021-04-25 06:19 -0700 |
| Message-ID | <0ef0e9a6-9638-47e2-a58b-f8d401581602n@googlegroups.com> |
| In reply to | #79237 |
On Saturday, 24 April 2021 at 08:06:00 UTC+3, Ian Collins wrote: > On 24/04/2021 16:43, wij wrote: > > > > As to the issue that C++ codes are simpler and easier. In basics I agree, but > > probably not much different from other popular high level languages in > > application, except the method. > > > > Approaching the more general problem from the real world of software engineer market, > > while people saying C++ is the choice for high performance language, such as gaming, > > banking, AI, VR,..., I saw the need for C++ engineer shrinks very fast these years. > > Most companies are trying to reduce cost by shrinking existing C++ codes to minimal. > > Fortran, Cobol are still active and high paid for 'high performance' need. I do not see that point ... What companies want to reduce C++ by using more COBOL, FORTRAN, Ada, D or C? Can anyone point at such companies? Any cite? > We have the opposite problem, our ever expanding core product is C++ > with no viable alternatives. With our borders basically shut, we are > also finding it hard to get staff... I also experience same even with borders being open. Wherever there are some real sensors / actuators into real world to develop there are only those few alternatives and next to nothing can be done with that Python or JavaScript. So how to reduce C++ code there? Current main issue is that people want remote work when there is pandemic but for systems expensive to transport, consisting of number of optional components or expensive peripherals that have part of control manual it is quite painful to set up. So the developers have to be physically present at least part of the time. Budgets available and used are absurd but still the products lag behind because of lack of C++ developers. Writing parts in C can only relieve it very slightly as availability of potent developers of C is even worse or those are often very same persons fluent in both but preferring C++. > > To be more on topic, to say C++ is better over C, support from hardware is a must. > They are pretty much equal except at the extreme low end, that is > targets not supported by gcc. Yes, and where we have targets supported by gcc there it does not matter at all that gcc adds technically Fortran, Ada and D as alternatives as companies do not want to move to those.
[toc] | [prev] | [next] | [standalone]
| From | wij <wyniijj@gmail.com> |
|---|---|
| Date | 2021-04-25 10:23 -0700 |
| Message-ID | <a418494b-cf7c-464f-8785-e1c90484c58fn@googlegroups.com> |
| In reply to | #79253 |
On Sunday, 25 April 2021 at 21:19:34 UTC+8, Öö Tiib wrote: > On Saturday, 24 April 2021 at 08:06:00 UTC+3, Ian Collins wrote: > > On 24/04/2021 16:43, wij wrote: > > > > > > As to the issue that C++ codes are simpler and easier. In basics I agree, but > > > probably not much different from other popular high level languages in > > > application, except the method. > > > > > > Approaching the more general problem from the real world of software engineer market, > > > while people saying C++ is the choice for high performance language, such as gaming, > > > banking, AI, VR,..., I saw the need for C++ engineer shrinks very fast these years. > > > Most companies are trying to reduce cost by shrinking existing C++ codes to minimal. > > > Fortran, Cobol are still active and high paid for 'high performance' need. > I do not see that point ... What companies want to reduce C++ by using more COBOL, > FORTRAN, Ada, D or C? Can anyone point at such companies? Any cite? That saying is based on my own investigation of local job market several years ago. And, for the question of this Conversation thread, I checked the software engineer need of the market in my country again for just about 10 pages (no company wants C++ programer). I do not remember exactly which company, the one that I remember requires FORTRAN/C skill is a company or institute for high speed computation (clustered, large computers). Surely, COBOL is from some banking company, you can also assume they also need 'high performance'. Probably a little surprise to me is gaming companies seemingly began to focus on using 'engines'. I myself have some career with gaming and security surveillance.., firmware and hardware. From the experience of hardware development, cost-reduction is a tough issue. Basically or in such experiences, truly real tech. innovation means lower cost, vise versa. Q: ...By using *more* COBOL, FORTRAN, Ada, D or C? A: The reason to using more FORTRAN and C is reasonable as it occurred. I can only imagine what such a company really want is mathematical skill and do not want more other restrictions and introducing unnecessary complications. As to using more COBOL codes, I do not really know, but you can come up with some reasonable scenario. As to Ada or D, none in my country I know requires such skill.
[toc] | [prev] | [next] | [standalone]
| From | Öö Tiib <ootiib@hot.ee> |
|---|---|
| Date | 2021-04-25 12:46 -0700 |
| Message-ID | <933423b5-0708-4ad6-916b-09210b0d306en@googlegroups.com> |
| In reply to | #79256 |
On Sunday, 25 April 2021 at 20:24:07 UTC+3, wyn...@gmail.com wrote: > On Sunday, 25 April 2021 at 21:19:34 UTC+8, Öö Tiib wrote: > > On Saturday, 24 April 2021 at 08:06:00 UTC+3, Ian Collins wrote: > > > On 24/04/2021 16:43, wij wrote: > > > > > > > > As to the issue that C++ codes are simpler and easier. In basics I agree, but > > > > probably not much different from other popular high level languages in > > > > application, except the method. > > > > > > > > Approaching the more general problem from the real world of software engineer market, > > > > while people saying C++ is the choice for high performance language, such as gaming, > > > > banking, AI, VR,..., I saw the need for C++ engineer shrinks very fast these years. > > > > Most companies are trying to reduce cost by shrinking existing C++ codes to minimal. > > > > Fortran, Cobol are still active and high paid for 'high performance' need. > > I do not see that point ... What companies want to reduce C++ by using more COBOL, > > FORTRAN, Ada, D or C? Can anyone point at such companies? Any cite? > That saying is based on my own investigation of local job market several years ago. > And, for the question of this Conversation thread, I checked the software engineer > need of the market in my country again for just about 10 pages (no company wants > C++ programer). Huh? Few look at those ads that all places are full of. These do not indicate who wants to reduce C++ in any manner. <https://stackoverflow.com/jobs/companies?q=c%2B%2B> 321 results. Job market is nontransparent. Our recruiters periodically ask every single C++-capable individual whom we want in our country personally if they are interested in new work or when it is student then even first work. Also they check globally who wants to come into our little and relatively cold country. They have helped to form migration papers from places like Russia, Belarus, Argentina, Ecuador and Philippines. Sure the list of people whom we do not want is lot longer. There are no point to announce anything as it is more likely that someone whom we don't want will apply. It feels unrelated to what I was asking. > I do not remember exactly which company, the one that I remember requires > FORTRAN/C skill is a company or institute for high speed computation (clustered, > large computers). Surely, COBOL is from some banking company, you can also > assume they also need 'high performance'. > Probably a little surprise to me is gaming companies seemingly began to focus > on using 'engines'. > I myself have some career with gaming and security surveillance.., firmware and > hardware. From the experience of hardware development, cost-reduction is a > tough issue. Basically or in such experiences, truly real tech. innovation means > lower cost, vise versa. > > Q: ...By using *more* COBOL, FORTRAN, Ada, D or C? > A: The reason to using more FORTRAN and C is reasonable as it occurred. > I can only imagine what such a company really want is mathematical skill and > do not want more other restrictions and introducing unnecessary complications. > As to using more COBOL codes, I do not really know, but you can come up with > some reasonable scenario. > As to Ada or D, none in my country I know requires such skill. New projects can be started in any language. Where gcc is compiler there these start in C++ far more frequently than in FORTRAN or C. But that wasn't even a question as I was particularly asking who wants to migrate/translate/switch from C++ product/code base to any of those now and why as I know outright totally none cases.
[toc] | [prev] | [next] | [standalone]
| From | wij <wyniijj@gmail.com> |
|---|---|
| Date | 2021-04-25 20:34 -0700 |
| Message-ID | <41f88ab3-5bb1-4319-8ad6-cc745807ec75n@googlegroups.com> |
| In reply to | #79258 |
> > Q: ...By using *more* COBOL, FORTRAN, Ada, D or C? > > A: The reason to using more FORTRAN and C is reasonable as it occurred. > > I can only imagine what such a company really want is mathematical skill and > > do not want more other restrictions and introducing unnecessary complications. > > As to using more COBOL codes, I do not really know, but you can come up with > > some reasonable scenario. > > As to Ada or D, none in my country I know requires such skill. > New projects can be started in any language. > Where gcc is compiler there these start in C++ far more frequently than in > FORTRAN or C. But that wasn't even a question as I was particularly asking who > wants to migrate/translate/switch from C++ product/code base to any of those > now and why as I know outright totally none cases. Who? Who can say that specifically except themselves? For many companies/application users, a program is just a program of which the language written is irrelevant. The product of many companies in my country are physical things. They hire programmers because of the functions needed change frequently in various production phase/line. In their view, it the question of changing programs, not programming languages. Thus, the hired programmers have to adapt, probably endup with script/mixed languages. Qt is the thing I know better because I had a commercial license (Qt3, I just use several basic elements), they send me email regularly. That Qt itself is in C++ probably will not change, but their products seems changing from pure C++ codes to QML and tool set. In the end, I fear the C++ core part may shrink accordingly. Who? you ask them.
[toc] | [prev] | [next] | [standalone]
| From | Öö Tiib <ootiib@hot.ee> |
|---|---|
| Date | 2021-04-28 09:57 -0700 |
| Message-ID | <b16ecee1-d2c9-4a71-898f-956a717204b9n@googlegroups.com> |
| In reply to | #79259 |
On Monday, 26 April 2021 at 06:34:52 UTC+3, wyn...@gmail.com wrote: > Öö Tiib wrote: > > But that wasn't even a question as I was particularly asking who > > wants to migrate/translate/switch from C++ product/code base to any of those > > now and why as I know outright totally none cases. > Who? Who can say that specifically except themselves? I specifically asked for quote or cite of concrete "themselves" as technology companies say such kind of things (like their trend in choice of technology stacks) about themselves quite often. If it is something noteworthy like moving to C or Fortran then I expect similar amount of noise like people do who about themselves (and their kids) being vegetarians. It is because everybody want talk about themselves and to find and to cooperate with others who share their attitude.
[toc] | [prev] | [next] | [standalone]
| From | wij <wyniijj@gmail.com> |
|---|---|
| Date | 2021-04-29 10:56 -0700 |
| Message-ID | <6fe0524b-3a84-40be-a89a-7e7fae5b13b5n@googlegroups.com> |
| In reply to | #79352 |
On Thursday, 29 April 2021 at 00:57:11 UTC+8, Öö Tiib wrote: > On Monday, 26 April 2021 at 06:34:52 UTC+3, wyn...@gmail.com wrote: > > Öö Tiib wrote: > > > But that wasn't even a question as I was particularly asking who > > > wants to migrate/translate/switch from C++ product/code base to any of those > > > now and why as I know outright totally none cases. > > Who? Who can say that specifically except themselves? > > I specifically asked for quote or cite of concrete "themselves" as technology > companies say such kind of things (like their trend in choice of technology stacks) > about themselves quite often. If it is something noteworthy like moving to C > or Fortran then I expect similar amount of noise like people do who about > themselves (and their kids) being vegetarians. It is because everybody want > talk about themselves and to find and to cooperate with others who share > their attitude. Many 'larger' companies in my place are production companies. If C++ is listed in the job requirement, often associated with are database, web,HMI,AI,motor,vehicle,...,for hardware verification/simulation/analysis. Rare companies like these would intentionally announce changes of their tools, internal affairs. E.g. Garmin (because the job requirement is in English, and is typical form) wants C/C++ people. 1. Good understanding in C/C++ programming. 2. Fitness GPS product design – Running Wearables, cycling products and wellness products. 3. Good English communication ability to communicate with foreign engineers. Will be a plus if have experience about: 1. Computer architecture and ARM Compiler. 2. Have experience about embedded system or RTOS. 3. Knowledge about how to use logic analyzer and Jtag ICE. 4. Other OS related knowledge. ---------------------------- In this case, normal C++ applicant would headache with ARM compiler, RTOS, ICE, and hardware things. And, "C/C++" is a general term. I can not think of any reason, any company like this would say anything too specific about their production tools.
[toc] | [prev] | [next] | [standalone]
| From | jacobnavia <jacob@jacob.remcomp.fr> |
|---|---|
| Date | 2021-04-27 18:31 +0200 |
| Message-ID | <s69e8j$vap$1@dont-email.me> |
| In reply to | #79253 |
Le 25/04/2021 à 15:19, Öö Tiib a écrit : > I do not see that point ... What companies want to reduce C++ by using more COBOL, > FORTRAN, Ada, D or C? Can anyone point at such companies? Any cite? Look. C is the most popular programming language, according to the TIOBE index https://www.tiobe.com/tiobe-index/ SOME companies must be using it, don't you think so? C++ comes 4th.
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2021-04-28 09:01 +0200 |
| Message-ID | <s6b17t$45e$1@dont-email.me> |
| In reply to | #79333 |
On 27/04/2021 18:31, jacobnavia wrote: > Le 25/04/2021 à 15:19, Öö Tiib a écrit : >> I do not see that point ... What companies want to reduce C++ by using >> more COBOL, >> FORTRAN, Ada, D or C? Can anyone point at such companies? Any cite? > > Look. C is the most popular programming language, according to the TIOBE > index > > https://www.tiobe.com/tiobe-index/ > > SOME companies must be using it, don't you think so? > > C++ comes 4th. The TIOBE index is well-known for being no more than a vague indication of the popularity of a language - it is certainly not the ranking some people would believe. But let us assume that C is significantly more used than C++ - it is not an unreasonable assumption, regardless of TIOBE. The question asked was "what companies want to reduce C++ by using more C?" It was not "what companies use C?". In my experience, companies and developers sometimes move from C to C++. They rarely move back. But if you know differently, tell us.
[toc] | [prev] | [next] | [standalone]
Page 1 of 5 [1] 2 3 4 5 Next page →
Back to top | Article view | comp.lang.c++
csiph-web