[go: up one dir, main page]

File: lister.c

package info (click to toggle)
a2ps 1%3A4.14-1.3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,324 kB
  • sloc: ansic: 26,966; sh: 11,844; lex: 2,286; perl: 1,156; yacc: 757; makefile: 609; lisp: 398; ada: 263; objc: 189; f90: 109; ml: 85; sql: 74; pascal: 57; modula3: 33; haskell: 32; sed: 30; java: 29; python: 24
file content (695 lines) | stat: -rw-r--r-- 18,303 bytes parent folder | download | duplicates (8)
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
/* lister.c -- Listing data in various formats
   Copyright (C) 1998-1999 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

/*

  This file still needs quite a lot of work.  In particular, its
  interaction with tterm, and the fact that the user might want to use
  another stream than the one given at initialization.

 */


#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
#  define PARAM_UNUSED __attribute__ ((unused))
#else
#  define PARAM_UNUSED
#endif

#include <stdio.h>

#if HAVE_STRING_H
# if !STDC_HEADERS && HAVE_MEMORY_H
#  include <memory.h>
# endif
# include <string.h>
#else
# if HAVE_STRINGS_H
#  include <strings.h>
# endif
#endif
#if HAVE_STDLIB_H
# include <stdlib.h>
#endif

#if HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#if HAVE_INTTYPES_H
# include <inttypes.h>
#endif

#if NEED_FPUTS_DECL
extern void fputs ();
#endif

#if NEED_STRLEN_DECL
extern int strlen ();
#endif

#include "xalloc.h"
#include "tterm.h"
#include "lister.h"

/* Information about filling a column.  */
struct world
{
  size_t *widths;	/* Array of the widths of the columns */
  size_t width;		/* Sum of the widths of the columns */
  size_t valid_len;	/* This world respects the width of the line */
};

/* All the needed information for multicolumn listings */

struct multicol
{
  /* Number of spaces between columns. */
  size_t between;

  /* Justification of the items. */
  size_t justify;

  /* Array with information about column filledness.  Indexed by the
     number of columns minus one (used for hypothetical reasonning.
     Only one is actually selected to print.) */
  struct world *worlds;
};

/* List with separators.  See lister_print_separated for more
   information. */

struct separated
{
  /* String between items. */
  const char *separator;

  /* Spaces after the separator. */
  size_t between;

  /* String after the last item. */
  const char *final;
};


struct lister
{
  /* The tinyterm under which displays are done. */
  struct tterm *tterm;

  /* Default output stream. */
  FILE *stream;

  /* Default width function to be applied on items. */
  lister_width_t width_fn;

  /* Default print function to be applied on items. */
  lister_print_t print_fn;

  /* Number of spaces to leave at the beginning of the line. */
  size_t before;

  /* Number of spaces to leave before the end of the line. */
  size_t after;

  /* All the needed information for multicolumn listings. */
  struct multicol multicol;

  /* All the needed information for separated listings. */
  struct separated separated;
};

/* Default setting */
static struct lister lister_default =
{
  /* tterm. Lister's default tterm, is the default tterm */
  NULL,

  /* stream.  We cannot initialize statically to stdout, EGCS won't
     let us do. */
  NULL,

  /* width_fn, print_fn */
  (lister_width_t) strlen,  (lister_print_t) fputs,

  /* Before, after */
  0, 0,

  /* multicol structure. */
  {
    /* between, justify, worlds */
    2, lister_left, NULL
  },

  /* separated structure. */
  {
    /* separator, between, final. */
    ",", 2, "."
  }
};

/* Maximum number of columns ever possible for this display.  */
static size_t max_idx;

/* The minimum width of a colum is 1 character for the name. */
#define MIN_COLUMN_WIDTH	1


/* Initialize the LISTER for the STREAM. */

void
lister_initialize (struct lister *lister, FILE *stream)
{
  struct lister * l = lister ? lister : &lister_default;
  tterm_initialize (NULL, stream);
  l->stream = stream;
}

/* Assuming cursor is at position FROM, indent up to position TO.
   Use a TAB character instead of two or more spaces whenever possible.  */

#define INDENT(_from_,_to_)				\
  do {							\
    size_t from = _from_;				\
    size_t to = _to_;					\
    while (from < to)					\
      {							\
  	if (tabsize > 0 				\
  	    && to / tabsize > (from + 1) / tabsize)	\
  	  {						\
  	    putc ('\t', stream);			\
  	    from += tabsize - from % tabsize;		\
  	  }						\
  	else						\
  	  {						\
  	    putc (' ', stream);				\
  	    from++;					\
  	  }						\
      }							\
  } while (0)

/* FIXME: comment */
static void
init_worlds (struct lister * l)
{
  size_t i;
  size_t line_width = tterm_width (l->tterm);

  /* Prepare the structure to the maximum number of columns, hence do
     not depend on BEFORE, BETWEEN and AFTER which can change while
     running, but only on LINE_WIDTH which is a constant. */
  if (l->multicol.worlds == NULL)
    {
      l->multicol.worlds = XMALLOC (struct world, line_width);
      for (i = 0; i < line_width; ++i)
	l->multicol.worlds[i].widths = XMALLOC (size_t, i + 1);
    }

  max_idx = ((line_width
	      - l->before - l->after - l->multicol.between)
             / (MIN_COLUMN_WIDTH + l->multicol.between));
  if (max_idx == 0)
    max_idx = 1;

  for (i = 0; i < max_idx; ++i)
    {
      size_t j;

      l->multicol.worlds[i].valid_len = 1;
      l->multicol.worlds[i].width = (i + 1) * MIN_COLUMN_WIDTH;

      for (j = 0; j <= i; ++j)
	l->multicol.worlds[i].widths[j] = MIN_COLUMN_WIDTH;
    }
}

/* Set the tiny term of LISTER to TTERM.  Returns the previous value. */

struct tterm *
lister_tterm_set (struct lister * lister, struct tterm *tterm)
{
  struct lister * l = lister ? lister : &lister_default;
  struct tterm *old = l->tterm;
  l->tterm = tterm;
  return old;
}

/* Set the width of the white prefix in LISTER to SIZE.  Returns the
   previous value. */

size_t
lister_before_set (struct lister * lister, size_t size)
{
  struct lister * l = lister ? lister : &lister_default;
  size_t old = l->before;
  l->before = size;
  return old;
}

/* Set the width of the white suffix in LISTER to SIZE.  Returns the
   previous value. */

size_t
lister_after_set (struct lister * lister, size_t size)
{
  struct lister * l = lister ? lister : &lister_default;
  size_t old = l->after;
  l->after = size;
  return old;
}


static size_t
lister_vertical_format (struct lister * l,
			void **items, size_t item_number,
			lister_width_t item_width_fn,
			struct world ** line_fmt)
{
  size_t max_cols;	/* Maximum number of columns usable */
  size_t cols;
  size_t itemno;
  size_t item_width;
  struct multicol * m = &l->multicol;
  size_t available_width = tterm_width (l->tterm) - l->after - l->before;
  struct world * worlds = m->worlds;

  /* Normally the maximum number of columns is determined by the
     screen width.  But if few files are available this might limit it
     as well.  */
  max_cols = max_idx > item_number ? item_number : max_idx;

  /* Compute the maximum number of possible columns.  */
  for (itemno = 0; itemno < item_number; ++itemno)
    {
      size_t i;

      item_width = item_width_fn (items[itemno]);

      for (i = 0; i < max_cols; ++i)
	{
	  if (worlds[i].valid_len)
	    {
	      size_t effective_width = available_width - i * m->between;
	      size_t idx = itemno / ((item_number + i) / (i + 1));
	      size_t real_width = item_width;

	      if (real_width > worlds[i].widths[idx])
		{
		  worlds[i].width += (real_width - worlds[i].widths[idx]);
		  worlds[i].widths[idx] = real_width;
		  worlds[i].valid_len = worlds[i].width <= effective_width;
		}
	    }
	}
    }

  /* Find maximum allowed columns.  */
  for (cols = max_cols; cols > 1; --cols)
    {
      if (worlds[cols - 1].valid_len)
	break;
    }

  *line_fmt = &worlds[cols - 1];
  return cols;
}

void
lister_fprint_vertical (struct lister * lister, PARAM_UNUSED FILE *unused,
			void **items, size_t item_number,
			lister_width_t item_width_fn,
			lister_print_t item_print_fn)
{
  struct world *line_fmt;
  size_t itemno;		/* Index size_to files. */
  size_t row;			/* Current row. */
  size_t col_width;     	/* Width of longest file name + frills. */
  size_t item_width;		/* Width of each file name + frills. */
  size_t pos;			/* Current character column. */
  size_t cols;			/* Number of files across. */
  size_t rows;			/* Maximum number of files down. */
  struct lister * l = lister ? lister : &lister_default;
  struct multicol * m = &l->multicol;
  size_t tabsize = tterm_tabsize (l->tterm);
  FILE *stream = l->stream;

  init_worlds (l);

  /* If not known, compute the actually number of elements.
     FIXME: there are probably more intelligent scheme to get item_number
     on the fly, but currently it is need in lister_vertical_format
     real soon, so just compute it right now. */
  if (item_number == (size_t) -1)
    {
      for (item_number = 0 ; items[item_number] ; item_number++)
	/* nothing */ ;
    }

  cols = lister_vertical_format (l, items, item_number,
				 item_width_fn, &line_fmt);

  /* Calculate the number of rows that will be in each column except possibly
     for a short column on the right. */
  rows = item_number / cols + (item_number % cols != 0);

  for (row = 0; row < rows; row++)
    {
      size_t col = 0;
      size_t nextpos, nextcolpos;
      itemno = row;

      /* Print the next row.  */
      pos = 0;
      nextcolpos = nextpos = l->before;
      while (1)
	{
	  col_width = line_fmt->widths[col++];
          item_width = (*item_width_fn) (items[itemno]);
	  nextpos += (col_width - item_width) * m->justify /2 ;
	  nextcolpos += col_width + m->between;

          INDENT (pos, nextpos);
          (*item_print_fn) (items[itemno], stream);

	  itemno += rows;
	  if (itemno >= item_number)
	    break;

	  pos = nextpos + item_width;
	  nextpos = nextcolpos;
	}
      putc ('\n', stream);
    }
}

/* Same as LISTER_FPRINT_VERTICAL, but using LISTER's default value
   for width_fn and print_fn. */

void
lister_print_vertical (struct lister *lister,
		       void **items, size_t item_number)
{
  struct lister *l = lister ? lister : &lister_default;
  lister_fprint_vertical (lister, NULL,
			  items, item_number,
			  l->width_fn, l->print_fn);
}


/* Listing in horizontal format.  Columns are built to minimize the
   number of needed rows.  For instance:

   +---------------------------+
   | a   bbbbb  c  dddddddd    |
   | ee  ff     g  h           |
   | i   j      k  lllllllllll |
   +---------------------------+

   */

static size_t
lister_horizontal_format (struct lister * l,
			  void **items, size_t item_number,
			  lister_width_t item_width_fn,
			  struct world **line_fmt)
{
  size_t max_cols;	/* Maximum number of columns usable */
  size_t cols;
  size_t itemno;
  size_t item_width;
  struct multicol * m = &l->multicol;
  size_t available_width = tterm_width (l->tterm) - l->after - l->before;
  struct world * worlds = m->worlds;

  /* Normally the maximum number of columns is determined by the
     screen width.  But if few files are available this might limit it
     as well.  */
  max_cols = max_idx > item_number ? item_number : max_idx;

  /* Compute the maximum file name width.  */
  for (itemno = 0; itemno < item_number; ++itemno)
    {
      size_t i;

      item_width = (*item_width_fn) (items[itemno]);

      for (i = 0; i < max_cols; ++i)
	{
	  if (worlds[i].valid_len)
	    {
	      size_t effective_width = available_width - i * m->between;
	      size_t idx = itemno % (i + 1);
	      size_t real_width = item_width;

	      if (real_width > worlds[i].widths[idx])
		{
		  worlds[i].width += (real_width - worlds[i].widths[idx]);
		  worlds[i].widths[idx] = real_width;
		  worlds[i].valid_len = worlds[i].width <= effective_width;
		}
	    }
	}
    }

  /* Find maximum allowed columns.  */
  for (cols = max_cols; cols > 1; --cols)
    {
      if (worlds[cols - 1].valid_len)
	break;
    }

  *line_fmt = &worlds[cols - 1];
  return cols;
}

/* FIXME: document */

void
lister_fprint_horizontal (struct lister * lister, PARAM_UNUSED FILE *unused,
			  void **items, size_t item_number,
			  lister_width_t item_width_fn,
			  lister_print_t item_print_fn)
{
  struct world *line_fmt;
  size_t itemno;
  size_t col_width;
  size_t item_width;
  size_t cols;
  size_t pos;
  size_t nextpos, nextcolpos;
  struct lister * l = lister ? lister : &lister_default;
  struct multicol * m = &l->multicol;
  size_t tabsize = tterm_tabsize (l->tterm);
  FILE *stream = l->stream;

  init_worlds (l);

  /* If not known, compute the actually number of elements.
     FIXME: there are probably more intelligent scheme to get item_number
     on the fly, but currently it is need in lister_vertical_format
     real soon, so just compute it right now. */
  if (item_number == (size_t) -1)
    {
      for (item_number = 0 ; items[item_number] ; item_number++)
	/* nothing */ ;
    }

  cols = lister_horizontal_format (l, items, item_number,
				   item_width_fn, &line_fmt);

  /* Print first entry.  */
  pos = 0;
  nextcolpos = nextpos = l->before;

  /* Now the rest.  */
  for (itemno = 0; itemno < item_number; ++itemno)
    {
      size_t col = itemno % cols;

      item_width = strlen (items[itemno]);
      col_width = line_fmt->widths[col];

      if (col == 0 && itemno != 0)
	{
	  putc ('\n', stream);
	  pos = 0;
	  nextcolpos = nextpos = l->before;
	}

      nextpos += (col_width - item_width) * m->justify / 2;
      INDENT (pos, nextpos);
      (*item_print_fn) (items [itemno], stream);
      pos = nextpos + item_width;
      nextcolpos += col_width + m->between;
      nextpos = nextcolpos;
    }
  putc ('\n', stream);
}


/* Same as LISTER_FPRINT_HORIZONTAL, but using LISTER's default value
   for width_fn and print_fn. */

void
lister_print_horizontal (struct lister *lister,
			 void **items, size_t item_number)
{
  struct lister *l = lister ? lister : &lister_default;
  lister_fprint_horizontal (lister, NULL,
			   items, item_number,
			   l->width_fn, l->print_fn);
}

/*
   Listing thing separated by spaces and strings.  For instance:

   +------------------------------+
   |  one, two, three, four, five,|
   |  six, seven, eight, nine,    |
   |  ten, eleven.                |
   +------------------------------+

   Note that in the first line, `,' is written though there is not
   enough room for the ` ' behind.  Note also that `ten' is not
   written on the second line, because there is no room enough for the
   `,'.

   This corresponds to the settings: before = 2, after = 0,
   between_width = 1, between_string = `,' after_string = `.'.
*/

void
lister_fprint_separated (struct lister * lister,PARAM_UNUSED FILE *unused,
			 void **items, size_t item_number,
			 lister_width_t item_width_fn,
			 lister_print_t item_print_fn)
{
  size_t itemno;
  size_t pos;
  struct lister * l = lister ? lister : &lister_default;
  struct separated * s = &l->separated;
  size_t final_width = strlen (s->final);
  size_t separator_width = strlen (s->separator);
  size_t tabsize = tterm_tabsize (l->tterm);
  FILE *stream = l->stream;

  /* The BEFORE prefix must not be `smartly' hidden in line_width,
     since INDENT needs the absolute position on the screen in order
     to synchronize correctly the tabulations.  */
  size_t line_width = tterm_width (l->tterm) - l->after;
  size_t old_pos;

  pos = l->before;
  INDENT (0, pos);

  for (itemno = 0;
       (item_number != (size_t) -1
	? (itemno < item_number)
	: items[itemno] != NULL);
       itemno++)
    {
      old_pos = pos;

      pos += (*item_width_fn) (items[itemno]);
      pos += (itemno + 1 < item_number) ? separator_width : final_width;

      if (itemno)
	{
	  if (pos + s->between > line_width)
	    {
	      putc ('\n', stream);
	      INDENT (0, l->before);
	      pos = pos - old_pos + l->before;
	    }
	  else
	    {
	      INDENT (old_pos, old_pos + s->between);
	      pos += s->between;
	    }
	}
      (*item_print_fn) (items[itemno], stream);
      fputs ((itemno + 1 < item_number) ? s->separator : s->final, stream);
    }
  putc ('\n', stream);
}

/* Same as LISTER_FPRINT_SEPARATED, but using LISTER's default value
   for width_fn and print_fn. */

void
lister_print_separated (struct lister *lister,
			void **items, size_t item_number)
{
  struct lister *l = lister ? lister : &lister_default;
  lister_fprint_separated (lister, NULL,
			   items, item_number,
			   l->width_fn, l->print_fn);
}


#ifdef TEST

const char * program_name = "lister";

int
main (int argc, char **argv)
{
  static const char * liste[] =
  {
    "1", "22", "333", "4444", "55555", "666666", "7777777",
    "88888888", "999999999"
  };
#if 0
  {
    "68000.ssh", "a2ps.ssh", "a2psrc.ssh", "ada.ssh", "c.ssh",
    "caml.ssh", "card.ssh", "chlog.ssh", "claire.ssh", "clisp.ssh",
    "coqv.ssh", "cpp.ssh", "csh.ssh", "eiffel.ssh", "elisp.ssh",
    "eps.ssh", "fortran.ssh", "gnuc.ssh", "html.ssh", "idl.ssh",
    "initora.ssh", "is5rul.ssh", "java.ssh", "lace.ssh", "lex.ssh",
    "mail.ssh", "make.ssh", "matlab4.ssh", "mib.ssh", "modula2.ssh",
    "modula3.ssh", "o2c.ssh", "oberon.ssh", "objc.ssh", "octave.ssh",
    "oldperl.ssh", "oracle.ssh", "pascal.ssh", "perl.ssh", "plsql.ssh",
    "ppd.ssh", "pre.ssh", "pretex.ssh", "prolog.ssh", "promela.ssh",
    "ps.ssh", "python.ssh", "sather.ssh", "scheme.ssh", "sdl88.ssh",
    "sh.ssh", "sql.ssh", "sql92.ssh", "ssh.ssh", "symbols.ssh",
    "tcl.ssh", "tcsh.ssh", "tex.ssh", "texinfo.ssh", "texscript.ssh",
    "tk.ssh", "udiff.ssh", "unity.ssh", "verilog.ssh", "vhdl.ssh",
    "vrml.ssh", "wdiff.ssh", "yacc.ssh", "zsh.ssh",
    "1111111111", "22",
    "1111111111", "22",
    "1111111111", "22",
    "1111111111", "22",
    "1111111111", "22",
    "1111111111", "22",
    "1111111111", "22",
    "1111111111", "22",
    "1111111111", "22"
  } ;
#endif
/*  lister_default.line_width = 67;*/
  lister_init (stdout);

  lister_fprint_separated (NULL,
			   liste, sizeof (liste) / sizeof (*liste),
			   strlen, fputs);
  return 0;
}
#endif