-
Notifications
You must be signed in to change notification settings - Fork 5
/
unixcall.c
602 lines (481 loc) · 10.2 KB
/
unixcall.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
* LEVEE, or Captain Video; A vi clone
*
* Copyright (c) 1982-2007 David L Parsons
* All rights reserved.
*
* Redistribution and use in source and binary forms, without or
* without modification, are permitted provided that the above
* copyright notice and this paragraph are duplicated in all such
* forms and that any documentation, advertising materials, and
* other materials related to such distribution and use acknowledge
* that the software was developed by David L Parsons (orc@pell.portland.or.us).
* My name may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE.
*/
/*
* Unix interface for levee
*/
#include "levee.h"
#include "extern.h"
#ifdef OS_UNIX
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#if HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/ioctl.h>
#if HAVE_PWD_H
#include <pwd.h>
#if HAVE_UUID_UUID_H
#include <uuid/uuid.h>
#endif
#endif
#include <errno.h>
/* **** FILE IO ABSTRACTIONS **** */
FILEDESC
OPEN_OLD(char *name)
{
int fd = open(name, O_RDONLY);
return (fd == -1) ? NOWAY : (FILEDESC)fd;
}
FILEDESC
OPEN_NEW(char *name)
{
int fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0666);
return (fd == -1) ? NOWAY : (FILEDESC)fd;
}
int
CLOSE_FILE(FILEDESC f)
{
return close( (int)f );
}
long
SEEK_POSITION(FILEDESC f, long offset, int mode)
{
return lseek((int)f, offset, mode);
}
int
READ_TEXT(FILEDESC f, void *buf, int size)
{
return read((int)f, buf, size);
}
int
WRITE_TEXT(FILEDESC f, void *buf, int size)
{
return write((int)f, buf, size);
}
/* *** UNIX-SPECIFIC CONSOLE I/O *** */
int
os_write(char *s,int len)
{
return 0;
}
int
os_gotoxy(int x, int y)
{
return 0;
}
int
os_clearscreen(void)
{
return 0;
}
int
os_clear_to_eol(void)
{
return 0;
}
int
os_cursor(int visible)
{
return 0;
}
int
os_scrollback(void)
{
return 0;
}
int
os_newline(void)
{
return 0;
}
int
os_openline(void)
{
return 0;
}
int
os_highlight(int yes_or_no)
{
return 0;
}
int
os_Ping(void)
{
return 0;
}
/* get the screensize, if we can
*/
int
os_screensize(int *x,int *y)
{
#if defined(TIOCGSIZE)
struct ttysize tty;
if (ioctl(0, TIOCGSIZE, &tty) == 0) {
if (tty.ts_lines) (*y)=tty.ts_lines;
if (tty.ts_cols) (*x)=tty.ts_cols;
logit(("os_screensize: col=%d,row=%d", tty.ts_cols, tty.ts_lines));
return 1;
}
#elif defined(TIOCGWINSZ)
struct winsize tty;
if (ioctl(0, TIOCGWINSZ, &tty) == 0) {
if (tty.ws_row) (*y)=tty.ws_row;
if (tty.ws_col) (*x)=tty.ws_col;
logit(("os_screensize: col=%d,row=%d", tty.ws_col, tty.ws_row));
return 1;
}
#endif
return 0;
}
#if !HAVE_TCGETATTR
#define tcgetattr(fd,t) ioctl(fd, TCGETS, t)
#define tcsetattr(fd,n,t) ioctl(fd, n, t)
#define TCSANOW TCSETAF
#endif
static struct termios old;
int
os_initialize(void)
{
tcgetattr(0, &old); /* get editing keys */
Erasechar = old.c_cc[VERASE];
Eraseline = old.c_cc[VKILL];
return 0;
}
int
os_restore(void)
{
return 1;
}
int
os_rename(char *from, char *to)
{
return rename(from, to);
}
int
os_unlink(char *file)
{
return unlink(file);
}
int
os_mktemp(char *dest, int size, const char *template)
{
#if USING_MKTEMP
static char Xes[] = ".XXXXXX";
#define SZuniq sizeof(Xes) /* uniqueifier is mktemp */
#else
#define SZuniq 10 /* uniqueifier is getpid() */
#endif
static char tmp[] = "/tmp/";
/* assert |dest| > |/tmp/|+|template|+|XXXXXX| */
unless (size > sizeof(tmp) + SZuniq + strlen(template)) {
errno = E2BIG;
return 0;
}
#if USING_MKTEMP
sprintf(dest, "%s%s%s", tmp, template, Xes);
strcpy(dest, template);
return mktemp(dest) != 0;
#else
strcpy(dest, tmp);
strcat(dest, template);
strcat(dest, ntoa(getpid()));
return 1;
#endif
}
/*
* implement the glob() command (with GLOB_NOSORT always set)
*/
int
os_glob(const char* pattern, int flags, glob_t *result)
{
#if USING_GLOB
if ( result->gl_pathc == 0 )
flags &= ~GLOB_APPEND;
return glob(pattern, flags|GLOB_NOSORT, 0, result);
#else
/* whoops. No wildcards here.
*/
char **newlist;
int count = 1 + (result->gl_flags & GLOB_DOOFFS ? result->gl_offs : 0);
#if LOGGING
if ( result->gl_flags & GLOB_APPEND )
logit(("os_glob: add %s to arglist", pattern));
else
logit(("os_glob: create new arglist, initialized with %s", pattern));
#endif
if ( result->gl_pathc == 0 )
result->gl_flags = flags;
if ( result->gl_flags & GLOB_APPEND )
count += result->gl_pathc;
unless ( flags & GLOB_NOMAGIC ) {
if ( strcspn(pattern, "?*") < strlen(pattern) ) {
result->gl_flags |= GLOB_MAGCHAR;
unless (result->gl_flags & GLOB_NOCHECK)
return GLOB_NOMATCH;
}
else
result->gl_flags &= ~GLOB_MAGCHAR;
}
if ( count >= result->gl_pathalloc ) {
result->gl_pathalloc += 50;
logit("os_glob: expanding gl_pathv (old pathv=%p, count=%d, pathc=%d)",
result->gl_pathv, count, result->gl_pathc);
if ( result->gl_pathv )
newlist = realloc(result->gl_pathv,
result->gl_pathalloc * sizeof result->gl_pathv[0]);
else
newlist = calloc(result->gl_pathalloc, sizeof result->gl_pathv[0]);
unless (newlist)
return GLOB_NOSPACE;
result->gl_pathv = newlist;
}
unless (result->gl_pathv[count-1] = malloc(strlen(pattern)+2))
return GLOB_NOSPACE;
strcpy(result->gl_pathv[count-1], pattern);
if ( result->gl_flags & GLOB_MARK )
strcat(result->gl_pathv[count-1], "/");
result->gl_pathv[count] = 0;
result->gl_pathc++;
result->gl_matchc = 1;
return 0;
#endif
}
/*
* clean up a glob_t after use.
*/
void
os_globfree(glob_t *collection)
{
#if USING_GLOB
globfree(collection);
#else
int x, start;
start = collection->gl_flags & GLOB_DOOFFS ? collection->gl_offs : 0;
for (x=0; x < collection->gl_pathc; x++)
if ( collection->gl_pathv[start+x] )
free(collection->gl_pathv[start+x]);
if ( collection->gl_pathc )
free(collection->gl_pathv);
#endif
memset(collection, 0, sizeof(collection[0]));
}
/*
* do ~username expansions on a filename
*/
char *
os_tilde(char *path)
{
#if HAVE_PWD_H
char *name, *slash, *expanded;
char *dir = 0;
struct passwd *user=0;
logit(("os_tilde %s", path));
unless ( path && (path[0] == '~') )
return 0;
/* ~ by itself gets special handling; $HOME, then getpwuid */
if ( path[1] == '/' ) {
logit(("os_tilde: just ~"));
slash = 1+path;
unless (dir = getenv("HOME")) {
unless (user = getpwuid(getuid()))
return 0;
dir = user->pw_dir;
}
}
else {
logit(("os_tilde: ~name"));
/* ourself or someone else? */
unless ( slash = strchr(path, '/') )
return 0;
unless ( name = malloc(slash-path) )
return 0;
strncpy(name, 1+path, (slash-path)-1);
name[(slash-path)-1] = 0;
user = getpwnam(name);
free(name);
unless (user)
return 0;
dir = user->pw_dir;
}
logit(("os_tilde: dir is [%s]", dir));
if (expanded = malloc(strlen(dir)+strlen(slash)+2)) {
strcpy(expanded, dir);
strcat(expanded, "/");
strcat(expanded, slash+1);
logit(("os_expand -> %s", expanded));
}
return expanded;
#else
return 0;
#endif
}
/*
* return the backup name for a file
*/
char *
os_backupname(char *file)
{
int size;
char *p;
size = strlen(file) + 2;
unless ( p = calloc(1,size) )
return 0;
strcpy(p, file);
strcat(p, "~");
return p;
}
/*
* implement the :! command
*/
int
os_subshell(char *commandline)
{
return system(commandline);
}
/*
* plumbing for the ! command: fork off a child process to process
* a workfile, return a FILE* that will hold the output.
*/
FILE *
os_cmdopen(char *cmdline, char *workfile, os_pid_t *child)
{
os_pid_t job;
int io[2];
if ( pipe(io) < 0 )
return NULL;
if ( (job = fork()) < 0 ) {
close(io[0]);
close(io[1]);
return NULL;
}
if ( job < 0 )
return NULL;
else if ( job == 0 ) {
/* child */
int ifd = open(workfile ? workfile : "/dev/null", O_RDONLY);
close(io[0]);
if ( (ifd < 0) || (dup2(ifd, 0) < 0) ) {
close(io[1]);
exit(1);
}
dup2(io[1], 1);
dup2(1,2);
execl("/bin/sh", "sh", "-c", cmdline, NULL);
close(io[1]);
exit(1);
}
else {
/* parent */
close(io[1]);
*child = job;
return fdopen(io[0], "r");
}
}
/*
* plumbing for the ! command: wait for the child to clean up, the
* return exit status.
*/
int
os_cmdclose(FILE *input, os_pid_t child)
{
int status;
waitpid(child, &status, 0);
fclose(input);
return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
}
int
os_cclass(unsigned int c)
{
if (c == '\t' && !list)
return CC_TAB;
if ( c < 32 || c == 127 )
return CC_CTRL;
if (c & 0x80)
return CC_OTHER;
return CC_PRINT;
}
/* put the terminal into raw mode
*/
void
set_input(void)
{
struct termios new = old;
new.c_iflag &= ~(IXON|IXOFF|IXANY|ICRNL|INLCR);
new.c_lflag &= ~(ICANON|ISIG|ECHO);
new.c_oflag = 0;
tcsetattr(0, TCSANOW, &new);
}
/* reset the terminal to what is was before
*/
void
reset_input(void)
{
#if USING_STDIO
fflush(stdout);
#endif
tcsetattr(0, TCSANOW, &old);
}
/* what does our dotfile look like
*/
char *
dotfile(void)
{
/* should expand username */
return "~/.lvrc";
}
/* get a single keypress from the console
*/
int
getKey(void)
{
unsigned char c[1];
fd_set input;
#if USING_STDIO
logit(("getKey: flush stdout"));
fflush(stdout);
#endif
/* we're using Unix select() to wait for input, so lets hope that
* all the Unices out there support select(). If your Unix doesn't,
* you can make this work by replacing this select loop with:
*
* while (read(0,c,1) != 1)
* ;
* return c[1];
*
* ... and watch your load-average peg.
*/
while (1) {
FD_ZERO(&input);
FD_SET(0, &input);
select(1, &input, 0, 0, 0);
if( read(0,c,1) == 1) {
logit(("getKey -> %c", c[0]));
return c[0];
}
}
}
#endif