From 5e501cf05fd1997018613304761d1104879353aa Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Tue, 3 Dec 2013 15:32:12 +0200 Subject: Implement sg --list * etc/Makefile.am (FLNFILES): Add lsattr.fln (.fln.forlan): Use all prerequisites, not only the first one. (FORMATFILES): Remove lsattr.forlan (lsattr.forlan): Use implicit rule. (sg.forlan): New rule * etc/flncat.c (comment): Optionally output the comment. (main): Output " else " only if some non-whitespace and non-comment characters were output before. * etc/lsattr.fln: New file * src/sg-cl.opt: New option --list. * src/sg.c (list_option): New variable. --- etc/flncat.c | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) (limited to 'etc/flncat.c') diff --git a/etc/flncat.c b/etc/flncat.c index 0c29453..82c4086 100644 --- a/etc/flncat.c +++ b/etc/flncat.c @@ -19,30 +19,44 @@ #include int -comment(FILE *fp, int c) +comment(FILE *fp, int c, int echo) { if (c != '/') { ungetc(c, fp); return 0; - } + } else if (echo) + fputc(c, stdout); if ((c = getc(fp)) == '/') { - while ((c = getc(fp)) != EOF && c != '\n') - ; + do + if (echo) fputc(c, stdout); + while ((c = getc(fp)) != EOF && c != '\n'); return 1; } + + if (echo) + fputc(c, stdout); if (c == '*') { do { - while ((c = getc(fp)) != '*') { + do { + c = getc(fp); if (c == EOF) return 0; - } - } while ((c = getc(fp)) != '/'); + else if (echo) + fputc(c, stdout); + } while (c != '*'); + c = getc(fp); + if (echo) + fputc(c, stdout); + } while (c != '/'); return 1; + } else + + if (!echo) { + ungetc(c, fp); + ungetc('/', fp); } - ungetc(c, fp); - ungetc('/', fp); return 0; } @@ -54,6 +68,7 @@ main(int argc, char **argv) int c; char buf[128]; size_t lev = 0; + int outchars = 0; for (i = 1; i < argc; i++) { fp = fopen(*++argv, "r"); @@ -62,16 +77,19 @@ main(int argc, char **argv) return 1; } - if (i > 1) { - /* Skip whitespace and comment lines */ - do { - while ((c = fgetc(fp)) != EOF && isspace(c)) - ; - } while (c != EOF && comment(fp, c)); + /* Skip initial whitespace and comment lines, unless it is the + first file we process. */ + do { + while ((c = fgetc(fp)) != EOF && isspace(c)) + if (i == 1) fputc(c, stdout); + } while (c != EOF && comment(fp, c, i == 1)); + + if (outchars) { printf(" else "); } while ((c = fgetc(fp)) != EOF) { + outchars = 1; putchar(c); if (c == '}') { lev = 0; -- cgit v1.2.1