xplor9 / xplor.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <thread.h>
#include <plumb.h>
#include "win.h"

#define ROOTSIZE 256
#define NBUF 256

enum
{
	STACK = 8192,
};

enum {
	wait1 = (1<<0),
	wait2 = (1<<1),
	winwait = (1<<2),
	xplorwait = (1<<3),
};

char *progname = "xplor";
char *prog = "/bin/xplor";
char *winprog = "/bin/win";
char *root;
char INDENT = '	';
char *dirflag = "+ ";
char *args[3];
Win Mwin;

void
setAddr(char *addr) {
	Win *w = &Mwin;

	if(w->addr < 0)
		w->addr = openfile(w, "addr");
	if(write(w->addr, addr, strlen(addr)) < 0){
		fprint(2, "readLine: warning: bad address %s:%r\n", addr);
		return;
	}
}

char *
readLine(char *addr) {
	Win *w = &Mwin;
	char b[NBUF];
	char *data, *end;
	int n, len;

	if(w->addr < 0)
		w->addr = openfile(w, "addr");
	if(w->data < 0)
		w->data = openfile(w, "xdata");
	if(write(w->addr, addr, strlen(addr)) < 0){
		fprint(2, "readLine: warning: bad address %s:%r\n", addr);
		return nil;
	}
	n = read(w->data, b, sizeof b);
	if(n <= 0)
		fprint(2,"reading data: %r");
	end = strchr(&b[0], '\n');
	len = end - &b[0];
	if (len <= 3) {
		// happens when reading blank lines at the end
		return nil;
	}
	b[len] = 0;
	// remove dirflag, if any
	data = malloc(len - 1);
	strncpy(data, &b[2], len - 2);
	data[len - 2] = 0;
	return data;
}

int
getDepth(char *line) {
	int pos = 0;

	while (line[pos] == INDENT) {
		pos++;
	}
	strcpy(line, &line[pos]);
	return pos;
}

char *
getParents(char *charaddr, int depth, int prevline) {
	int len, len2, newdepth;
	char *addr, *b, *fullpath;

// what's the diff between that and "" ?
// do it with a {} construct if possible.
	if (depth == 0) {
		fullpath = malloc(1);
		fullpath[0] = 0;
		return fullpath;
	}
	len = strlen(charaddr);
//TODO: this will fail if prevline - 1 goes over 999, do better.
	addr = malloc(6 + len);
	addr[0] = '#';
	strcpy(&addr[1], charaddr);
	if (prevline == 1) {
		strcpy(&addr[1+len], "-+");
		addr[len+3] = 0;
	} else {
		addr[1+len] = '-';
		sprint(&addr[2+len], "%d", prevline-1);
		if (prevline - 1 > 99) {			
			addr[len+5] = 0;
		} else {
			if (prevline - 1 > 9) {
				addr[len+4] = 0;
			} else {
				addr[len+3] = 0;
			}
		}		
	}
	while(1) {
		b = readLine(addr);
		newdepth = getDepth(b);
		if (newdepth < depth) {
			fullpath = getParents(charaddr, newdepth, prevline);
			len2 = strlen(fullpath);
			fullpath = realloc(fullpath, len2 + 2 + strlen(b));
			fullpath[len2] = '/';
			fullpath[len2 + 1] = 0;
			fullpath = strcat(fullpath, b);
			free(b);
			free(addr);
			return fullpath;
		}
		prevline++;
		sprint(&addr[2+len], "%d", prevline-1);
		if (prevline - 1 > 99) {		
			addr[len+5] = 0;
		} else {
			if (prevline - 1 > 9) {
				addr[len+4] = 0;
			} else {
				addr[len+3] = 0;
			}
		}		
	}
	return "";
}

char *
mkAddr(char *charaddr, char *terminator) {
	char *b;
	int len, len2;

	len = strlen(charaddr);
	len2 = strlen(terminator);
	b = malloc(2 + len + len2);
	b[0] = '#';
	strcpy(&b[1], charaddr);
	strcpy(&b[1 + len], terminator);
	b[1 + len + len2] = 0;
	return b;
}


int 
isFolded(char *charaddr) {
	char *b, *addr;
	int depth, nextdepth;

	addr = mkAddr(charaddr, "+1-");
	b = readLine(addr);
	depth = getDepth(b);
	free(addr);
	free(b);

	addr = mkAddr(charaddr, "+-");
	b = readLine(addr);
	if (nil == b) {
		// probably because it was the very bottom line
		// hence it is indeed folded
		free(addr);
		return 1;
	}
	nextdepth = getDepth(b);
	free(addr);
	free(b);

	if (nextdepth <= depth) {
		return 1;
	} else {
		return 0;
	}
}

char *
getFullPath(char *charaddr, int *depth, char **parent) {
	char *addr, *terminator, *b, *line;
	char *fullpath = malloc(NBUF);
	int dpth, len, pos;
	Dir *dir;

	// reconstruct full path
	terminator = smprint("+1-");
	addr = mkAddr(charaddr, terminator);
	b = readLine(addr);
	dpth = getDepth(b);
	line = b;
	strcpy(fullpath, root);
	len = strlen(root);
	fullpath[len] = '/';
	b = getParents(charaddr, dpth, 1);
	pos = len + 1;
	strcpy(&fullpath[pos], b);
	pos += strlen(b);
	fullpath[pos] = '/';
	pos++;
	strcpy(&fullpath[pos], line);
	pos += strlen(line);
	fullpath[pos] = 0;
	fullpath = cleanname(fullpath);
	
	if (parent != nil && *parent == nil) {
		dir = dirstat(fullpath);
		if (dir == nil) {
			fprint(2,"dir nil \n");
//TODO: better error handling
			return;
		}
		if ((dir->mode & DMDIR) == 0) {
			// not a dir -> strip last element of fullpath and provide that as a parent
			len = strlen(fullpath) - strlen(dir->name);
			*parent = malloc(len + 1);
			strncpy(*parent, fullpath, len);
			(*parent)[len] = 0;
		} else {
			*parent = smprint("%s", fullpath);
		}
	}
	if (depth != nil) {
		*depth = dpth;
	}
	free(terminator);
	free(addr);
	free(b);
	free(line);	
	return fullpath;
}

static int
compar(const void *first, const void *second)
{
	Dir *dir1 = (Dir *) first;
	Dir *dir2 = (Dir *) second;

	return strcmp(dir1->name, dir2->name);
}

void
printDirContents(char *dirpath, int depth) {
	int n, len, currentDir, pos;
	Dir *dirs;
	char *indents, *name, *line;
	int dirflaglen = strlen(dirflag);

	dirpath = cleanname(dirpath);
	currentDir = open(dirpath, OREAD);
	if (currentDir < 0) {
		return;
	}

	n = dirreadall(currentDir, &dirs);
	if (n <= 0) {
		return;
	}
	close(currentDir);

	qsort(dirs, n, sizeof(Dir), compar);
	indents = malloc(depth * sizeof(char) + 1);
	for (int i = 0; i < depth; i++) {
		indents[i] = '	';
	}
	indents[depth] = 0;
	line = malloc(1);
	for (int i = 0; i< n; i++) {
		name = dirs[i].name;
		len = strlen(name);
		line = erealloc(line, dirflaglen + depth + len + 1 + 1);
		if ((dirs[i].mode & DMDIR) != 0) {
			// it's a dir
			for (int i=0; i<dirflaglen; i++) {
				line[i] = dirflag[i];
			}
		} else {
			for (int i=0; i<dirflaglen; i++) {
				line[i] = ' ';
			}
		}
		pos = dirflaglen;
		strcpy(&line[pos], indents);
		pos += depth;
		strcpy(&line[pos], name);
		pos += len;
		line[pos] = '\n';
		pos++;
		line[pos] = 0;
		wwritedata(&Mwin, line, strlen(line));
	}

	free(line);	
	free(indents);
	free(dirs);

	if (depth == 0) {
	//lame trick for now to dodge the out of range issue, until my address-foo gets better
		for (int i=0; i<3; i++) {
			wwritedata(&Mwin, "\n", 2);
		}
	} 
	return;
}

void 
onLook(char *charaddr) {
	char *addr, *terminator, *b, *line, *fullpath;
	int depth, port, nextdepth, nextline;
	Dir *dir;

	// reconstruct full path and check if file or dir
	fullpath = getFullPath(charaddr, &depth, nil);

	dir = dirstat(fullpath);
	if (dir == nil) {
		fprint(2,"dir nil \n");
		return;
	}
	if ((dir->mode & DMDIR) == 0) {
		// not a dir -> send that file to the plumber
		port = plumbopen("send", OWRITE);
		if (port <= 0) {
			fprint(2,"plumbopen failed \n");
			return;
		}
		if (plumbsendtext(port, "xplor", "", "/", fullpath) <=0) {
			fprint(2,"plumbopen failed \n");
		} 
		return;
	}

	if (isFolded(charaddr) == 1) {
		// print dir contents
		addr = mkAddr(charaddr, "+2-1-#0");
		setAddr(addr);
		printDirContents(fullpath, depth+1);
		free(addr);
		free(fullpath);
	} else {
		free(fullpath);
		// fold, ie delete lines below dir until we hit a dir of the same depth
		addr = mkAddr(charaddr, "+-");
		nextdepth = depth + 1;
		nextline = 1;
		terminator = malloc(8);
		terminator[0] = '+';
//TODO(mpl): too many frees and mallocs in the loop (readLine), use buffs instead to fix that.
		while (nextdepth > depth) {
			setAddr(addr);
			b = readLine(addr);
			if (b != nil) {
				// b == nil when we reach the very bottom line
				nextdepth = getDepth(b);
				free(b);
			}
			nextline++;
			line = smprint("%d", nextline - 1);
			strcpy(&terminator[1], line);
			terminator[1+strlen(line)] = 0;
			free(line);
			free(addr);
			addr = mkAddr(charaddr, terminator);
			if (b == nil) {
				break;
			}
		}
		nextline--;
		line = malloc(strlen(charaddr) * 2 + 8);
		strcpy(line, charaddr);
		strcpy(&line[strlen(charaddr)], "+-#0,#");
		strcpy(&line[strlen(charaddr) + 6], charaddr);
		line[2 * strlen(charaddr) + 6] = '+';
		line[2 * strlen(charaddr) + 7] = 0;
		free(terminator);
		terminator = smprint("%d", nextline - 2);
		addr = mkAddr(line, terminator);
		free(terminator);
		setAddr(addr);
		free(addr);
		wwritedata(&Mwin, "", 1);
	}
	return;
}

char *
getCharAddr(char *msgBody) {
	char *temp, *prefix, *out;
	char *tokens[2];
	int pos, len;

	len = strlen(progname);
	prefix = malloc(len + 2);
	pos = 0;
	strcpy(prefix, progname);
	pos = len;
	prefix[pos] = '-';
	pos++;
	prefix[pos] = 0;
	if (strncmp(msgBody, prefix, len + 1) != 0) {
		//this message is not from xplor's body, so ignore it
		free(prefix);
		return nil;
	}
	free(prefix);
	if (gettokens(msgBody, tokens, 2, ":#") < 2) {
		return nil;
	}
	temp = smprint("%s", tokens[1]);
	if (gettokens(temp, tokens, 2, ",#") < 2) {
		free(temp);
		return nil;
	}
	out = smprint("%s", tokens[0]);
	free(temp);
	return out;
}

void
doDotDot(void) {
	Win *w = &Mwin;
	char title[256];
	
	// blank the window
	setAddr("0,$");
	wwritedata(w, "", 1);
	// restart from ..
	root = realloc(root, strlen(root) + 5);
	root = strcat(root, "/../");
	root = cleanname(root);
	sprint(title,"%s-%s", progname, root);
	wname(w, title);
	setAddr("#0");
	printDirContents(root, 0);
}

void
pexec(void *v)
{
	void **arg;
	char *prog;
	char **args;
	Channel *c;

	arg = v;
	prog = arg[0];
	args = arg[1];
	c = arg[2];

	if (args != nil) {
		if (args[1] != nil) {
			chdir(args[1]);
			free(args[1]);
			args[1] = nil;
		}
	}
	procexec(c, prog, args);
	fprint(2, "Remote execution failed: %s %r\n", prog);
	abort();
	threadexits(nil);
}

void
procpexec(char *prog, char **args)
{
	void *rexarg[4];
	Channel *c;

	c = chancreate(sizeof(ulong), 0);
	rexarg[0] = prog;
	rexarg[1] = args;
	rexarg[2] = c;

	proccreate(pexec, rexarg, STACK);
	recvul(c);
	chanfree(c);
}

void
handle(void)
{
	Event e;
	Win *w = &Mwin;
	int waitforit = 0;
	char *tmp, *tmp2;

	while (True) {
		wevent(w, &e);
		switch(e.c2){
		default:
			/* fprint(2,"unknown message %c%c\n", e.c1, e.c2); */
			break;
		case 'i':
			/* fprint(2,"'%s' inserted in tag at %d\n", e.b, e.q0);*/
			break;
		case 'I':
			/* fprint(2,"'%s' inserted in body at %d\n", e.b, e.q0);*/
			break;
		case 'd':
			/* fprint(2, "'%s' deleted in tag at %d\n", e.b, e.q0);*/
			break;
		case 'D':
			/* fprint(2, "'%s' deleted in body at %d\n", e.b, e.q0);*/
			break;
		case 'x':
//			fprint(2, "%c, %s \n", e.c2, e.b);
			if (e.flag & 8) {
				//chorded command, let's see the rest incoming in next messages
				waitforit = wait1;
				break;
			}
//TODO: factorize Xplor and Win cases in functions
			if ( (strncmp(e.b, "Win", 3) == 0) || 
			((waitforit & winwait) && ((waitforit & wait1) || (waitforit & wait2)))) {
				if (waitforit & wait2) {
					goto win;
				}
				if (waitforit & winwait) {
					waitforit |= wait2;
					break;
				} 								
				if (waitforit & wait1) {
					waitforit |= winwait;
					break;
				}
				win:
				args[1] = nil;
				if (waitforit & winwait) {
					waitforit = 0;
					tmp = getCharAddr(e.b);
					if (tmp != nil) {
						tmp2 = getFullPath(tmp, nil, &args[1]);
						free(tmp);
						free(tmp2);
					}
				}
				args[0] = winprog;
				args[2] = nil;
				procpexec(winprog, args);
				break;
			}
			if ( (strncmp(e.b, "Xplor", 4) == 0) || 
			((waitforit & xplorwait) && ((waitforit & wait1) || (waitforit & wait2)))) {
				if (waitforit & wait2) {
					goto xplor;
				}
				if (waitforit & xplorwait) {
					waitforit |= wait2;
					break;
				} 								
				if (waitforit & wait1) {
					waitforit |= xplorwait;
					break;
				}
				xplor:
				args[1] = nil;
				if (waitforit & xplorwait) {
					waitforit = 0;
					tmp = getCharAddr(e.b);
					if (tmp != nil) {
						tmp2 = getFullPath(tmp, nil, &args[1]);
						free(tmp);
						free(tmp2);
					}
				}				
				args[0] = prog;
				args[2] = nil;
				procpexec(prog, args);
				break;
			}
			if (strncmp(e.b, "DotDot", 6) == 0) {
				doDotDot();
				break;				
			}
			if (strncmp(e.b, "Del", 3) == 0) {
//TODO: better clean up when closing
				ctlwrite(w, "delete");
				return;
			}
			if (strncmp(e.b, "Look", 4) == 0) {
				// just resend the event to be caught above
				wwriteevent(w, &e);
			}	
//			fprint(2, "%s \n", e.b);
			break;	
//		case 'X':				/* Execute command. */
// this is for when the message is too long. see if we want to use it later
//			if (e.flag & 2)
//				wevent(w, &e2);
//			break;
		case 'l':
			// just resend the event so that acme catches it
			wwriteevent(w, &e);
			break;
		case 'L':				/* Look for something. */
//			if (e.flag & 2)
//				wevent(w, &e);
//			wclean(w);		/* Set clean bit. */
			//ignore expansions
			if (e.q0 != e.q1) {
				continue;
			}
//			fprint(2, "%d, %d \n", e.q0, e.q1);
			onLook(smprint("%d",e.q0));
			break;
		}
	}
}


void
openwin(char *buttons)
{
	Win *w = &Mwin;
	char buf[80];

	wnew(w);
	sprint(buf,"%s-%s", progname, root);
	wname(w, buf);
	wtagwrite(w, buttons, strlen(buttons));
	wclean(w);
	wdormant(w);
	setAddr("#0");
	printDirContents(root, 0);
	handle();
}

void
usage(void)
{
	fprint(2, "usage: %s [path]\n", argv0);
	threadexitsall(nil);
}

void
threadmain(int argc, char** argv)
{
	char *temp;

	ARGBEGIN{
	default:
		usage();
	}ARGEND


	root = malloc(ROOTSIZE);
	switch(argc){
	case 0:
		root = getwd(root, ROOTSIZE);
		break;
	case 1:
		if (argv[0][0] != '/') {
			// it's a relative path
			root = getwd(root, ROOTSIZE);
			temp = &root[strlen(root)];
			strcpy(temp, argv[0]);
		} else {
			strcpy(root, argv[0]);
		}
		break;
	default:
		usage();
	}
	root = cleanname(root);
	
	openwin("DotDot Win Xplor");
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.