summaryrefslogtreecommitdiff
path: root/usr/gri/pretty/godoc.go
blob: f79749f2b30d372ede3fe744b277503a52717fd4 (plain)
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
// Copyright 2009 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// godoc: Go Documentation Server

// Web server tree:
//
//	http://godoc/	main landing page
//	http://godoc/doc/	serve from $GOROOT/doc - spec, mem, tutorial, etc.
//	http://godoc/src/	serve files from $GOROOT/src; .go gets pretty-printed
//	http://godoc/cmd/	serve documentation about commands (TODO)
//	http://godoc/pkg/	serve documentation about packages
//		(idea is if you say import "compress/zlib", you go to
//		http://godoc/pkg/compress/zlib)
//
// Command-line interface:
//
//	godoc packagepath [name ...]
//
//	godoc compress/zlib
//		- prints doc for package proto
//	godoc compress/zlib Cipher NewCMAC
//		- prints doc for Cipher and NewCMAC in package crypto/block


package main

import (
	"container/vector";
	"flag";
	"fmt";
	"go/ast";
	"go/doc";
	"go/parser";
	"go/token";
	"http";
	"io";
	"log";
	"net";
	"os";
	pathutil "path";
	"sort";
	"strings";
	"tabwriter";
	"template";
	"time";

	"astprinter";
	"comment";
)


const Pkg = "/pkg/"	// name for auto-generated package documentation tree


var (
	verbose = flag.Bool("v", false, "verbose mode");

	// file system roots
	goroot string;
	pkgroot = flag.String("pkgroot", "src/lib", "root package source directory (if unrooted, relative to goroot)");
	tmplroot = flag.String("tmplroot", "usr/gri/pretty", "root template directory (if unrooted, relative to goroot)");

	// layout control
	tabwidth = flag.Int("tabwidth", 4, "tab width");
	usetabs = flag.Bool("tabs", false, "align with tabs instead of spaces");
	html = flag.Bool("html", false, "print HTML in command-line mode");

	// server control
	httpaddr = flag.String("http", "", "HTTP service address (e.g., ':6060')");
)


func init() {
	var err os.Error;
	goroot, err = os.Getenv("GOROOT");
	if err != nil {
		goroot = "/home/r/go-release/go";
	}
	flag.StringVar(&goroot, "goroot", goroot, "Go root directory");
}


// ----------------------------------------------------------------------------
// Support

func isGoFile(dir *os.Dir) bool {
	return dir.IsRegular() && strings.HasSuffix(dir.Name, ".go");
}


func isDir(name string) bool {
	d, err := os.Stat(name);
	return err == nil && d.IsDirectory();
}


func makeTabwriter(writer io.Writer) *tabwriter.Writer {
	padchar := byte(' ');
	if *usetabs {
		padchar = '\t';
	}
	return tabwriter.NewWriter(writer, *tabwidth, 1, padchar, tabwriter.FilterHTML);
}


// TODO(rsc): this belongs in a library somewhere, maybe os
func ReadFile(name string) ([]byte, os.Error) {
	f, err := os.Open(name, os.O_RDONLY, 0);
	if err != nil {
		return nil, err;
	}
	defer f.Close();
	var b io.ByteBuffer;
	if n, err := io.Copy(f, &b); err != nil {
		return nil, err;
	}
	return b.Data(), nil;
}


// ----------------------------------------------------------------------------
// Parsing

type rawError struct {
	pos token.Position;
	msg string;
}


type rawErrorVector struct {
	vector.Vector;
}


func (v *rawErrorVector) At(i int) rawError { return v.Vector.At(i).(rawError) }
func (v *rawErrorVector) Less(i, j int) bool { return v.At(i).pos.Offset < v.At(j).pos.Offset; }


func (v *rawErrorVector) Error(pos token.Position, msg string) {
	// only collect errors that are on a new line
	// in the hope to avoid most follow-up errors
	lastLine := 0;
	if n := v.Len(); n > 0 {
		lastLine = v.At(n - 1).pos.Line;
	}
	if lastLine != pos.Line {
		v.Push(rawError{pos, msg});
	}
}


// A single error in the parsed file.
type parseError struct {
	src []byte;	// source before error
	line int;	// line number of error
	msg string;	// error message
}


// All the errors in the parsed file, plus surrounding source code.
// Each error has a slice giving the source text preceding it
// (starting where the last error occurred).  The final element in list[]
// has msg = "", to give the remainder of the source code.
// This data structure is handed to the templates parseerror.txt and parseerror.html.
//
type parseErrors struct {
	filename string;	// path to file
	list []parseError;	// the errors
	src []byte;	// the file's entire source code
}


// Parses a file (path) and returns the corresponding AST and
// a sorted list (by file position) of errors, if any.
//
func parse(filename string, mode uint) (*ast.Program, *parseErrors) {
	src, err := ReadFile(filename);
	if err != nil {
		log.Stderrf("ReadFile %s: %v", filename, err);
		errs := []parseError{parseError{nil, 0, err.String()}};
		return nil, &parseErrors{filename, errs, nil};
	}

	var raw rawErrorVector;
	prog, ok := parser.Parse(src, &raw, mode);
	if !ok {
		// sort and convert error list
		sort.Sort(&raw);
		errs := make([]parseError, raw.Len() + 1);	// +1 for final fragment of source
		offs := 0;
		for i := 0; i < raw.Len(); i++ {
			r := raw.At(i);
			// Should always be true, but check for robustness.
			if 0 <= r.pos.Offset && r.pos.Offset <= len(src) {
				errs[i].src = src[offs : r.pos.Offset];
				offs = r.pos.Offset;
			}
			errs[i].line = r.pos.Line;
			errs[i].msg = r.msg;
		}
		errs[raw.Len()].src = src[offs : len(src)];
		return nil, &parseErrors{filename, errs, src};
	}

	return prog, nil;
}


// ----------------------------------------------------------------------------
// Templates

// Return text for decl.
func DeclText(d ast.Decl) []byte {
	var b io.ByteBuffer;
	var p astPrinter.Printer;
	p.Init(&b, nil, nil, false);
	d.Visit(&p);
	return b.Data();
}


// Return text for expr.
func ExprText(d ast.Expr) []byte {
	var b io.ByteBuffer;
	var p astPrinter.Printer;
	p.Init(&b, nil, nil, false);
	d.Visit(&p);
	return b.Data();
}


// Convert x, whatever it is, to text form.
func toText(x interface{}) []byte {
	type String interface { String() string }

	switch v := x.(type) {
	case []byte:
		return v;
	case string:
		return io.StringBytes(v);
	case String:
		return io.StringBytes(v.String());
	case ast.Decl:
		return DeclText(v);
	case ast.Expr:
		return ExprText(v);
	}
	var b io.ByteBuffer;
	fmt.Fprint(&b, x);
	return b.Data();
}


// Template formatter for "html" format.
func htmlFmt(w io.Writer, x interface{}, format string) {
	// Can do better than text in some cases.
	switch v := x.(type) {
	case ast.Decl:
		var p astPrinter.Printer;
		tw := makeTabwriter(w);
		p.Init(tw, nil, nil, true);
		v.Visit(&p);
		tw.Flush();
	case ast.Expr:
		var p astPrinter.Printer;
		tw := makeTabwriter(w);
		p.Init(tw, nil, nil, true);
		v.Visit(&p);
		tw.Flush();
	default:
		template.HtmlEscape(w, toText(x));
	}
}


// Template formatter for "html-comment" format.
func htmlCommentFmt(w io.Writer, x interface{}, format string) {
	comment.ToHtml(w, toText(x));
}


// Template formatter for "" (default) format.
func textFmt(w io.Writer, x interface{}, format string) {
	w.Write(toText(x));
}


// Template formatter for "dir/" format.
// Writes out "/" if the os.Dir argument is a directory.
var slash = io.StringBytes("/");

func dirSlashFmt(w io.Writer, x interface{}, format string) {
	d := x.(os.Dir);	// TODO(rsc): want *os.Dir
	if d.IsDirectory() {
		w.Write(slash);
	}
}


var fmap = template.FormatterMap{
	"": textFmt,
	"html": htmlFmt,
	"html-comment": htmlCommentFmt,
	"dir/": dirSlashFmt,
}


func readTemplate(name string) *template.Template {
	path := pathutil.Join(*tmplroot, name);
	data, err := ReadFile(path);
	if err != nil {
		log.Exitf("ReadFile %s: %v", path, err);
	}
	t, err1 := template.Parse(string(data), fmap);
	if err1 != nil {
		log.Exitf("%s: %v", name, err);
	}
	return t;
}


var godocHtml *template.Template
var packageHtml *template.Template
var packageText *template.Template
var packagelistHtml *template.Template;
var packagelistText *template.Template;
var parseerrorHtml *template.Template;
var parseerrorText *template.Template;

func readTemplates() {
	// have to delay until after flags processing,
	// so that main has chdir'ed to goroot.
	godocHtml = readTemplate("godoc.html");
	packageHtml = readTemplate("package.html");
	packageText = readTemplate("package.txt");
	packagelistHtml = readTemplate("packagelist.html");
	packagelistText = readTemplate("packagelist.txt");
	parseerrorHtml = readTemplate("parseerror.html");
	parseerrorText = readTemplate("parseerror.txt");
}


// ----------------------------------------------------------------------------
// Generic HTML wrapper

func servePage(c *http.Conn, title, content interface{}) {
	type Data struct {
		title interface{};
		header interface{};
		timestamp string;
		content interface{};
	}

	var d Data;
	d.title = title;
	d.header = title;
	d.timestamp = time.UTC().String();
	d.content = content;
	godocHtml.Execute(&d, c);
}


func serveText(c *http.Conn, text []byte) {
	c.SetHeader("content-type", "text/plain; charset=utf-8");
	c.Write(text);
}


func serveError(c *http.Conn, err, arg string) {
	servePage(c, "Error", fmt.Sprintf("%v (%s)\n", err, arg));
}


// ----------------------------------------------------------------------------
// Files

func serveParseErrors(c *http.Conn, errors *parseErrors) {
	// format errors
	var b io.ByteBuffer;
	parseerrorHtml.Execute(errors, &b);
	servePage(c, errors.filename + " - Parse Errors", b.Data());
}


func serveGoSource(c *http.Conn, name string) {
	prog, errors := parse(name, parser.ParseComments);
	if errors != nil {
		serveParseErrors(c, errors);
		return;
	}

	var b io.ByteBuffer;
	fmt.Fprintln(&b, "<pre>");
	var p astPrinter.Printer;
	writer := makeTabwriter(&b);  // for nicely formatted output
	p.Init(writer, nil, nil, true);
	p.DoProgram(prog);
	writer.Flush();  // ignore errors
	fmt.Fprintln(&b, "</pre>");

	servePage(c, name + " - Go source", b.Data());
}


var fileServer = http.FileServer(".", "");

func serveFile(c *http.Conn, req *http.Request) {
	// pick off special cases and hand the rest to the standard file server
	switch {
	case req.Url.Path == "/":
		// serve landing page.
		// TODO: hide page from ordinary file serving.
		// writing doc/index.html will take care of that.
		http.ServeFile(c, req, "doc/root.html");

	case req.Url.Path == "/doc/root.html":
		// hide landing page from its real name
		// TODO why - there is no reason for this (remove eventually)
		http.NotFound(c, req);

	case pathutil.Ext(req.Url.Path) == ".go":
		serveGoSource(c, req.Url.Path[1 : len(req.Url.Path)]);  // strip leading '/' from name

	default:
		// TODO not good enough - don't want to download files
		// want to see them
		fileServer.ServeHTTP(c, req);
	}
}


// ----------------------------------------------------------------------------
// Packages

type pakDesc struct {
	dirname string;  // relative to goroot
	pakname string;  // relative to directory
	importpath string;	// import "___"
	filenames map[string] bool;  // set of file (names) belonging to this package
}


type pakArray []*pakDesc
func (p pakArray) Len() int            { return len(p); }
func (p pakArray) Less(i, j int) bool  { return p[i].pakname < p[j].pakname; }
func (p pakArray) Swap(i, j int)       { p[i], p[j] = p[j], p[i]; }


func addFile(pmap map[string]*pakDesc, dirname, filename, importprefix string) {
	if strings.HasSuffix(filename, "_test.go") {
		// ignore package tests
		return;
	}
	// determine package name
	path := pathutil.Join(dirname, filename);
	prog, errors := parse(path, parser.PackageClauseOnly);
	if prog == nil {
		return;
	}
	if prog.Name.Value == "main" {
		// ignore main packages for now
		return;
	}

	var importpath string;
	dir, name := pathutil.Split(importprefix);
	if name == prog.Name.Value {	// package math in directory "math"
		importpath = importprefix;
	} else {
		importpath = pathutil.Clean(importprefix + "/" + prog.Name.Value);
	}

	// find package descriptor
	pakdesc, found := pmap[importpath];
	if !found {
		// add a new descriptor
		pakdesc = &pakDesc{dirname, prog.Name.Value, importpath, make(map[string]bool)};
		pmap[importpath] = pakdesc;
	}

	//fmt.Printf("pak = %s, file = %s\n", pakname, filename);

	// add file to package desc
	if tmp, found := pakdesc.filenames[filename]; found {
		panic("internal error: same file added more then once: " + filename);
	}
	pakdesc.filenames[filename] = true;
}


func addDirectory(pmap map[string]*pakDesc, dirname, importprefix string, subdirs *[]os.Dir) {
	path := dirname;
	fd, err1 := os.Open(path, os.O_RDONLY, 0);
	if err1 != nil {
		log.Stderrf("open %s: %v", path, err1);
		return;
	}

	list, err2 := fd.Readdir(-1);
	if err2 != nil {
		log.Stderrf("readdir %s: %v", path, err2);
		return;
	}

	nsub := 0;
	for i, entry := range list {
		switch {
		case isGoFile(&entry):
			addFile(pmap, dirname, entry.Name, importprefix);
		case entry.IsDirectory():
			nsub++;
		}
	}

	if subdirs != nil && nsub > 0 {
		*subdirs = make([]os.Dir, nsub);
		nsub = 0;
		for i, entry := range list {
			if entry.IsDirectory() {
				subdirs[nsub] = entry;
				nsub++;
			}
		}
	}
}


func mapValues(pmap map[string]*pakDesc) pakArray {
	// build sorted package list
	plist := make(pakArray, len(pmap));
	i := 0;
	for tmp, pakdesc := range pmap {
		plist[i] = pakdesc;
		i++;
	}
	sort.Sort(plist);
	return plist;
}


func (p *pakDesc) Doc() (*doc.PackageDoc, *parseErrors) {
	// compute documentation
	var r doc.DocReader;
	i := 0;
	for filename := range p.filenames {
		path := p.dirname + "/" + filename;
		prog, err := parse(path, parser.ParseComments);
		if err != nil {
			return nil, err;
		}

		if i == 0 {
			// first file - initialize doc
			r.Init(prog.Name.Value, p.importpath);
		}
		i++;
		r.AddProgram(prog);
	}
	return r.Doc(), nil;
}


func servePackage(c *http.Conn, p *pakDesc) {
	doc, errors := p.Doc();
	if errors != nil {
		serveParseErrors(c, errors);
		return;
	}

	var b io.ByteBuffer;
	if false {	// TODO req.Params["format"] == "text"
		err := packageText.Execute(doc, &b);
		if err != nil {
			log.Stderrf("packageText.Execute: %s", err);
		}
		serveText(c, b.Data());
		return;
	}
	err := packageHtml.Execute(doc, &b);
	if err != nil {
		log.Stderrf("packageHtml.Execute: %s", err);
	}
	servePage(c, doc.ImportPath + " - Go package documentation", b.Data());
}


type pakInfo struct {
	Path string;
	Package *pakDesc;
	Packages pakArray;
	Subdirs []os.Dir;	// TODO(rsc): []*os.Dir
}


func servePackageList(c *http.Conn, info *pakInfo) {
	var b io.ByteBuffer;
	err := packagelistHtml.Execute(info, &b);
	if err != nil {
		log.Stderrf("packagelistHtml.Execute: %s", err);
	}
	servePage(c, info.Path + " - Go packages", b.Data());
}


// Return package or packages named by name.
// Name is either an import string or a directory,
// like you'd see in $GOROOT/pkg/.
//
// Examples:
//	"math"	- single package made up of directory
//	"container"	- directory listing
//	"container/vector"	- single package in container directory
//
func findPackages(name string) *pakInfo {
	info := new(pakInfo);

	// Build list of packages.
	pmap := make(map[string]*pakDesc);

	// If the path names a directory, scan that directory
	// for a package with the name matching the directory name.
	// Otherwise assume it is a package name inside
	// a directory, so scan the parent.
	cname := pathutil.Clean(name);
	if cname == "" {
		cname = "."
	}
	dir := pathutil.Join(*pkgroot, cname);

	if isDir(dir) {
		addDirectory(pmap, dir, cname, &info.Subdirs);
		paks := mapValues(pmap);
		if len(paks) == 1 {
			p := paks[0];
			_, pak := pathutil.Split(dir);
			if p.dirname == dir && p.pakname == pak {
				info.Package = p;
				info.Path = cname;
				return info;
			}
		}
		
		info.Packages = paks;
		if cname == "." {
			info.Path = "";
		} else {
			info.Path = cname + "/";
		}
		return info;
	}

	// Otherwise, have parentdir/pak.  Look for package pak in parentdir.
	parentdir, _ := pathutil.Split(dir);
	parentname, _ := pathutil.Split(cname);
	if parentname == "" {
		parentname = "."
	}

	addDirectory(pmap, parentdir, parentname, nil);
	if p, ok := pmap[cname]; ok {
		info.Package = p;
		info.Path = cname;
		return info;
	}

	info.Path = name;	// original, uncleaned name
	return info;
}


func servePkg(c *http.Conn, r *http.Request) {
	path := r.Url.Path;
	path = path[len(Pkg) : len(path)];
	info := findPackages(path);
	if r.Url.Path != Pkg + info.Path {
		http.Redirect(c, info.Path);
		return;
	}

	if info.Package != nil {
		servePackage(c, info.Package);
	} else {
		servePackageList(c, info);
	}
}


// ----------------------------------------------------------------------------
// Server

func LoggingHandler(h http.Handler) http.Handler {
	return http.HandlerFunc(func(c *http.Conn, req *http.Request) {
		log.Stderrf("%s\t%s", req.Host, req.Url.Path);
		h.ServeHTTP(c, req);
	})
}


func usage() {
	fmt.Fprintf(os.Stderr,
		"usage: godoc package [name ...]\n"
		"	godoc -http=:6060\n"
	);
	flag.PrintDefaults();
	sys.Exit(1);
}


func main() {
	flag.Parse();

	// Check usage first; get usage message out early.
	switch {
	case *httpaddr != "":
		if flag.NArg() != 0 {
			usage();
		}
	default:
		if flag.NArg() == 0 {
			usage();
		}
	}

	if err := os.Chdir(goroot); err != nil {
		log.Exitf("chdir %s: %v", goroot, err);
	}

	readTemplates();

	if *httpaddr != "" {
		var handler http.Handler = http.DefaultServeMux;
		if *verbose {
			log.Stderrf("Go Documentation Server\n");
			log.Stderrf("address = %s\n", *httpaddr);
			log.Stderrf("goroot = %s\n", goroot);
			log.Stderrf("pkgroot = %s\n", *pkgroot);
			log.Stderrf("tmplroot = %s\n", *tmplroot);
			handler = LoggingHandler(handler);
		}

		http.Handle(Pkg, http.HandlerFunc(servePkg));
		http.Handle("/", http.HandlerFunc(serveFile));

		if err := http.ListenAndServe(*httpaddr, handler); err != nil {
			log.Exitf("ListenAndServe %s: %v", *httpaddr, err)
		}
		return;
	}

	if *html {
		packageText = packageHtml;
		packagelistText = packagelistHtml;
		parseerrorText = parseerrorHtml;
	}

	info := findPackages(flag.Arg(0));
	if info.Package == nil {
		err := packagelistText.Execute(info, os.Stderr);
		if err != nil {
			log.Stderrf("packagelistText.Execute: %s", err);
		}
		sys.Exit(1);
	}

	doc, errors := info.Package.Doc();
	if errors != nil {
		err := parseerrorText.Execute(errors, os.Stderr);
		if err != nil {
			log.Stderrf("parseerrorText.Execute: %s", err);
		}
		sys.Exit(1);
	}

	if flag.NArg() > 1 {
		args := flag.Args();
		doc.Filter(args[1 : len(args)]);
	}

	packageText.Execute(doc, os.Stdout);
}