9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

ni.c (8480B)


      1 #include <stdio.h>
      2 #include "tdef.h"
      3 #include "fns.h"
      4 #include "ext.h"
      5 
      6 char	termtab[NS];	/* term type added in ptinit() */
      7 char	fontdir[NS];	/* added in casefp; not used by nroff */
      8 char	devname[20];	/* default output device */
      9 
     10 Numtab numtab[NN] = {
     11 	{ PAIR('%', 0) },
     12 	{ PAIR('n', 'l') },
     13 	{ PAIR('y', 'r') },
     14 	{ PAIR('h', 'p') },
     15 	{ PAIR('c', 't') },
     16 	{ PAIR('d', 'n') },
     17 	{ PAIR('m', 'o') },
     18 	{ PAIR('d', 'y') },
     19 	{ PAIR('d', 'w') },
     20 	{ PAIR('l', 'n') },
     21 	{ PAIR('d', 'l') },
     22 	{ PAIR('s', 't') },
     23 	{ PAIR('s', 'b') },
     24 	{ PAIR('c', '.') },
     25 	{ PAIR('$', '$') }
     26 };
     27 
     28 
     29 int	alphabet	= 256;	/* latin-1 */
     30 int	pto	= 10000;
     31 int	pfrom	= 1;
     32 int	print	= 1;
     33 char	nextf[NS]	= TMACDIR;
     34 char	mfiles[NMF][NS];
     35 int	nmfi	= 0;
     36 int	oldbits	= -1;
     37 int	init	= 1;
     38 int	fc	= IMP;	/* field character */
     39 int	eschar	= '\\';
     40 int	pl;
     41 int	po;
     42 FILE	*ptid;
     43 
     44 int	dfact	= 1;
     45 int	dfactd	= 1;
     46 int	res	= 1;
     47 int	smnt	= 0;	/* beginning of special fonts */
     48 int	ascii	= 0;	/* ascii normally off for troff, on for nroff;  -a turns on */
     49 int	lg;
     50 int	pnlist[NPN] = { -1 };
     51 
     52 
     53 int	*pnp	= pnlist;
     54 int	npn	= 1;
     55 int	npnflg	=  1;
     56 int	dpn	=  -1;
     57 int	totout	=  1;
     58 int	ulfont	=  ULFONT;
     59 int	tabch	=  TAB;
     60 int	ldrch	=  LEADER;
     61 
     62 
     63 Contab contab[NM] = {
     64 	C(PAIR('d', 's'), caseds),
     65 	C(PAIR('a', 's'), caseas),
     66 	C(PAIR('s', 'p'), casesp),
     67 	C(PAIR('f', 't'), caseft),
     68 	C(PAIR('p', 's'), caseps),
     69 	C(PAIR('v', 's'), casevs),
     70 	C(PAIR('n', 'r'), casenr),
     71 	C(PAIR('i', 'f'), caseif),
     72 	C(PAIR('i', 'e'), caseie),
     73 	C(PAIR('e', 'l'), caseel),
     74 	C(PAIR('p', 'o'), casepo),
     75 	C(PAIR('t', 'l'), casetl),
     76 	C(PAIR('t', 'm'), casetm),
     77 	C(PAIR('f', 'm'), casefm),
     78 	C(PAIR('b', 'p'), casebp),
     79 	C(PAIR('c', 'h'), casech),
     80 	C(PAIR('p', 'n'), casepn),
     81 	C(PAIR('b', 'r'), tbreak),
     82 	C(PAIR('t', 'i'), caseti),
     83 	C(PAIR('n', 'e'), casene),
     84 	C(PAIR('n', 'f'), casenf),
     85 	C(PAIR('c', 'e'), casece),
     86 	C(PAIR('f', 'i'), casefi),
     87 	C(PAIR('i', 'n'), casein),
     88 	C(PAIR('l', 'l'), casell),
     89 	C(PAIR('n', 's'), casens),
     90 	C(PAIR('m', 'k'), casemk),
     91 	C(PAIR('r', 't'), casert),
     92 	C(PAIR('a', 'm'), caseam),
     93 	C(PAIR('d', 'e'), casede),
     94 	C(PAIR('d', 'i'), casedi),
     95 	C(PAIR('d', 'a'), caseda),
     96 	C(PAIR('w', 'h'), casewh),
     97 	C(PAIR('d', 't'), casedt),
     98 	C(PAIR('i', 't'), caseit),
     99 	C(PAIR('r', 'm'), caserm),
    100 	C(PAIR('r', 'r'), caserr),
    101 	C(PAIR('r', 'n'), casern),
    102 	C(PAIR('a', 'd'), casead),
    103 	C(PAIR('r', 's'), casers),
    104 	C(PAIR('n', 'a'), casena),
    105 	C(PAIR('p', 'l'), casepl),
    106 	C(PAIR('t', 'a'), caseta),
    107 	C(PAIR('t', 'r'), casetr),
    108 	C(PAIR('u', 'l'), caseul),
    109 	C(PAIR('c', 'u'), casecu),
    110 	C(PAIR('l', 't'), caselt),
    111 	C(PAIR('n', 'x'), casenx),
    112 	C(PAIR('s', 'o'), caseso),
    113 	C(PAIR('i', 'g'), caseig),
    114 	C(PAIR('t', 'c'), casetc),
    115 	C(PAIR('f', 'c'), casefc),
    116 	C(PAIR('e', 'c'), caseec),
    117 	C(PAIR('e', 'o'), caseeo),
    118 	C(PAIR('l', 'c'), caselc),
    119 	C(PAIR('e', 'v'), caseev),
    120 	C(PAIR('r', 'd'), caserd),
    121 	C(PAIR('a', 'b'), caseab),
    122 	C(PAIR('f', 'l'), casefl),
    123 	C(PAIR('e', 'x'), caseex),
    124 	C(PAIR('s', 's'), casess),
    125 	C(PAIR('f', 'p'), casefp),
    126 	C(PAIR('c', 's'), casecs),
    127 	C(PAIR('b', 'd'), casebd),
    128 	C(PAIR('l', 'g'), caselg),
    129 	C(PAIR('h', 'c'), casehc),
    130 	C(PAIR('h', 'y'), casehy),
    131 	C(PAIR('n', 'h'), casenh),
    132 	C(PAIR('n', 'm'), casenm),
    133 	C(PAIR('n', 'n'), casenn),
    134 	C(PAIR('s', 'v'), casesv),
    135 	C(PAIR('o', 's'), caseos),
    136 	C(PAIR('l', 's'), casels),
    137 	C(PAIR('c', 'c'), casecc),
    138 	C(PAIR('c', '2'), casec2),
    139 	C(PAIR('e', 'm'), caseem),
    140 	C(PAIR('a', 'f'), caseaf),
    141 	C(PAIR('h', 'a'), caseha),
    142 	C(PAIR('h', 'w'), casehw),
    143 	C(PAIR('m', 'c'), casemc),
    144 	C(PAIR('p', 'm'), casepm),
    145 	C(PAIR('p', 'i'), casepi),
    146 	C(PAIR('u', 'f'), caseuf),
    147 	C(PAIR('p', 'c'), casepc),
    148 	C(PAIR('h', 't'), caseht),
    149 	C(PAIR('c', 'f'), casecf),
    150 	C(PAIR('s', 'y'), casesy),
    151 	C(PAIR('l', 'f'), caself),
    152 	C(PAIR('p', 't'), casept),
    153 	C(PAIR('g', 'd'), casegd)
    154 };
    155 
    156 
    157 Tbuf _oline;
    158 
    159 /*
    160  * troff environment block
    161  */
    162 
    163 Env env[NEV] = { {	/* this sets up env[0] */
    164 /* int	ics	 */	0,	/* insertion character space, set by .mc */
    165 /* int	sps	 */	0,
    166 /* int	spacesz	 */	0,
    167 /* int	lss	 */	0,
    168 /* int	lss1	 */	0,
    169 /* int	ll	 */	0,
    170 /* int	ll1	 */	0,
    171 /* int	lt	 */	0,
    172 /* int	lt1	 */	0,
    173 /* Tchar ic	 */	0,	/* insertion character (= margin character) */
    174 /* int	icf	 */	0,	/* insertion character flag */
    175 /* Tchar chbits	 */	0,	/* size+font bits for current character */
    176 /* Tchar spbits	 */	0,
    177 /* Tchar nmbits	 */	0,	/* size+font bits for number from .nm */
    178 /* int	apts	 */	PS,	/* actual point size -- as requested by user */
    179 /* int	apts1	 */	PS,	/* need not match an existent size */
    180 /* int	pts	 */	PS,	/* hence, this is the size that really exists */
    181 /* int	pts1	 */	PS,
    182 /* int	font	 */	FT,
    183 /* int	font1	 */	FT,
    184 /* int	ls	 */	1,
    185 /* int	ls1	 */	1,
    186 /* int	ad	 */	1,
    187 /* int	nms	 */	1,	/* .nm multiplier */
    188 /* int	ndf	 */	1,	/* .nm separator */
    189 /* int	nmwid	 */	3,	/* max width of .nm numbers */
    190 /* int	fi	 */	1,
    191 /* int	cc	 */	'.',
    192 /* int	c2	 */	'\'',
    193 /* int	ohc	 */	OHC,
    194 /* int	tdelim	 */	IMP,
    195 /* int	hyf	 */	1,
    196 /* int	hyoff	 */	0,
    197 /* int	hyphalg  */	HYPHALG,
    198 /* int	un1	 */	-1,
    199 /* int	tabc	 */	0,
    200 /* int	dotc	 */	'.',
    201 /* int	adsp	 */	0,	/* add this much space to each padding point */
    202 /* int	adrem	 */	0,	/* excess space to add until it runs out */
    203 /* int	lastl	 */	0,	/* last text on current output line */
    204 /* int	nel	 */	0,	/* how much space left on current output line */
    205 /* int	admod	 */	0,	/* adjust mode */
    206 /* Tchar *wordp	 */	0,
    207 /* int	spflg	 */	0,	/* probably to indicate space after punctuation needed */
    208 /* Tchar *linep	 */	0,
    209 /* Tchar *wdend	 */	0,
    210 /* Tchar *wdstart */	0,
    211 /* int	wne	 */	0,
    212 /* int	ne	 */	0,	/* how much space taken on current output line */
    213 /* int	nc	 */	0,	/* #characters (incl blank) on output line */
    214 /* int	nb	 */	0,
    215 /* int	lnmod	 */	0,	/* line number mode, set by .nm */
    216 /* int	nwd	 */	0,	/* number of words on current output line */
    217 /* int	nn	 */	0,	/* from .nn command */
    218 /* int	ni	 */	0,	/* indent of .nm numbers, probably */
    219 /* int	ul	 */	0,
    220 /* int	cu	 */	0,
    221 /* int	ce	 */	0,
    222 /* int	in	 */	0,	/* indent and previous value */
    223 /* int	in1	 */	0,
    224 /* int	un	 */	0,	/* unindent of left margin in some way */
    225 /* int	wch	 */	0,
    226 /* int	pendt	 */	0,
    227 /* Tchar *pendw	 */	(Tchar *)0,
    228 /* int	pendnf	 */	0,
    229 /* int	spread	 */	0,
    230 /* int	it	 */	0,	/* input trap count */
    231 /* int	itmac	 */	0
    232 } };
    233 
    234 Env	*envp	= env;	/* start off in env 0 */
    235 
    236 Numerr	numerr;
    237 
    238 Stack	*frame, *stk, *ejl;
    239 Stack	*nxf;
    240 
    241 int	pipeflg;
    242 int	hflg;	/* used in nroff only */
    243 int	eqflg;	/* used in nroff only */
    244 
    245 int	xpts;
    246 int	ppts;
    247 int	pfont;
    248 int	mpts;
    249 int	mfont;
    250 int	cs;
    251 int	ccs;
    252 int	bd;
    253 
    254 int	stdi;
    255 int	quiet;
    256 int	stop;
    257 char	ibuf[IBUFSZ];
    258 char	xbuf[IBUFSZ];
    259 char	*ibufp;
    260 char	*xbufp;
    261 char	*eibuf;
    262 char	*xeibuf;
    263 Tchar	pbbuf[NC];		/* pushback buffer for arguments, \n, etc. */
    264 Tchar	*pbp = pbbuf;		/* next free slot in pbbuf */
    265 Tchar	*lastpbp = pbbuf;	/* pbp in previous stack frame */
    266 int	nx;
    267 int	mflg;
    268 Tchar	ch = 0;
    269 int	ibf;
    270 int	ifi;
    271 int	iflg;
    272 int	rargc;
    273 char	**argp;
    274 Ushort	trtab[NTRTAB];
    275 int	lgf;
    276 int	copyf;
    277 Offset	ip;
    278 int	nlflg;
    279 int	donef;
    280 int	nflush;
    281 int	nfo;
    282 int	padc;
    283 int	raw;
    284 int	flss;
    285 int	nonumb;
    286 int	trap;
    287 int	tflg;
    288 int	ejf;
    289 int	dilev;
    290 Offset	offset;
    291 int	em;
    292 int	ds;
    293 Offset	woff;
    294 int	app;
    295 int	ndone;
    296 int	lead;
    297 int	ralss;
    298 Offset	nextb;
    299 Tchar	nrbits;
    300 int	nform;
    301 int	oldmn;
    302 int	newmn;
    303 int	macerr;
    304 Offset	apptr;
    305 int	diflg;
    306 int	evi;
    307 int	vflag;
    308 int	noscale;
    309 int	po1;
    310 int	nlist[NTRAP];
    311 int	mlist[NTRAP];
    312 int	evlist[EVLSZ];
    313 int	ev;
    314 int	tty;
    315 int	sfont	= FT;	/* appears to be "standard" font; used by .ul */
    316 int	sv;
    317 int	esc;
    318 int	widthp;
    319 int	xfont;
    320 int	setwdf;
    321 int	over;
    322 int	nhyp;
    323 Tchar	**hyp;
    324 Tchar	*olinep;
    325 int	dotT;
    326 char	*unlkp;
    327 Wcache	widcache[NWIDCACHE];
    328 Diver	d[NDI];
    329 Diver	*dip;
    330 
    331 int	c_hyphen;
    332 int	c_emdash;
    333 int	c_rule;
    334 int	c_minus;
    335 int	c_fi;
    336 int	c_fl;
    337 int	c_ff;
    338 int	c_ffi;
    339 int	c_ffl;
    340 int	c_acute;
    341 int	c_grave;
    342 int	c_under;
    343 int	c_rooten;
    344 int	c_boxrule;
    345 int	c_lefthand;
    346 int	c_dagger;
    347 int	c_isalnum;
    348 
    349 Spnames	spnames[] =
    350 {
    351 	&c_hyphen,	"hy",
    352 	&c_emdash,	"em",
    353 	&c_rule,	"ru",
    354 	&c_minus,	"\\-",
    355 	&c_fi,		"fi",
    356 	&c_fl,		"fl",
    357 	&c_ff,		"ff",
    358 	&c_ffi,		"Fi",
    359 	&c_ffl,		"Fl",
    360 	&c_acute,	"aa",
    361 	&c_grave,	"ga",
    362 	&c_under,	"ul",
    363 	&c_rooten,	"rn",
    364 	&c_boxrule,	"br",
    365 	&c_lefthand,	"lh",
    366 	&c_dagger,	"dg",	/* not in nroff?? */
    367 	&c_isalnum,	"__",
    368 	0, 0
    369 };
    370 
    371 
    372 Tchar	(*hmot)(void);
    373 Tchar	(*makem)(int i);
    374 Tchar	(*setabs)(void);
    375 Tchar	(*setch)(int c);
    376 Tchar	(*sethl)(int k);
    377 Tchar	(*setht)(void);
    378 Tchar	(*setslant)(void);
    379 Tchar	(*vmot)(void);
    380 Tchar	(*xlss)(void);
    381 int	(*findft)(int i);
    382 int	(*width)(Tchar j);
    383 void	(*mchbits)(void);
    384 void	(*ptlead)(void);
    385 void	(*ptout)(Tchar i);
    386 void	(*ptpause)(void);
    387 void	(*setfont)(int a);
    388 void	(*setps)(void);
    389 void	(*setwd)(void);
    390