[BACK]Return to ident.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rcs

Annotation of src/usr.bin/rcs/ident.c, Revision 1.2

1.2     ! xsa         1: /*     $OpenBSD: ident.c,v 1.1 2005/10/06 15:39:11 joris Exp $ */
1.1       joris       2: /*
                      3:  * Copyright (c) 2005 Xavier Santolaria <xsa@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
                     27: #include <sys/param.h>
                     28:
                     29: #include <ctype.h>
                     30: #include <stdio.h>
                     31: #include <stdlib.h>
                     32: #include <string.h>
                     33: #include <unistd.h>
                     34:
                     35: #include "log.h"
                     36: #include "rcs.h"
                     37: #include "rcsprog.h"
                     38:
                     39: #define KEYDELIM       '$'     /* keywords delimitor */
                     40: #define VALDELIM       ':'     /* values delimitor */
                     41:
                     42: extern char *__progname;
                     43:
                     44: static int     ident_file(const char *, FILE *);
                     45: static int     ident_line(FILE *);
                     46:
                     47: int
                     48: ident_main(int argc, char **argv)
                     49: {
                     50:        int i, ch;
                     51:        FILE *fp;
                     52:
                     53:        while ((ch = getopt(argc, argv, "qV")) != -1) {
                     54:                switch(ch) {
                     55:                case 'q':
                     56:                        verbose = 0;
                     57:                        break;
                     58:                case 'V':
                     59:                        printf("%s\n", rcs_version);
                     60:                        exit(0);
                     61:                default:
                     62:                        (usage)();
                     63:                        exit(1);
                     64:                }
                     65:        }
                     66:
                     67:        argc -= optind;
                     68:        argv += optind;
                     69:
1.2     ! xsa        70:        if (argc == 0)
1.1       joris      71:                ident_file(NULL, stdin);
1.2     ! xsa        72:        else {
1.1       joris      73:                for (i = 0; i < argc; i++) {
                     74:                        if ((fp = fopen(argv[i], "r")) == NULL) {
                     75:                                cvs_log(LP_ERRNO, "%s", argv[i]);
                     76:                                continue;
                     77:                        }
                     78:
                     79:                        ident_file(argv[i], fp);
                     80:                        fclose(fp);
                     81:                }
                     82:        }
                     83:
                     84:        return (0);
                     85: }
                     86:
                     87:
                     88: static int
                     89: ident_file(const char *filename, FILE *fp)
                     90: {
                     91:        int c;
                     92:
                     93:        if (fp != stdin)
                     94:                printf("%s:\n", filename);
                     95:
                     96:        for (c = 0; c != EOF; (c = getc(fp))) {
                     97:                if ((feof(fp)) || (ferror(fp)))
                     98:                        break;
                     99:                if (c == KEYDELIM)
                    100:                        ident_line(fp);
                    101:        }
                    102:
                    103:        return (0);
                    104: }
                    105:
                    106: static int
                    107: ident_line(FILE *fp)
                    108: {
                    109:        int c;
                    110:        char *p, linebuf[1024];
                    111:
                    112:        p = linebuf;
                    113:
                    114:        while ((c = getc(fp)) != VALDELIM) {
                    115:                if ((c == EOF) && (feof(fp) | ferror(fp)))
                    116:                        return (0);
                    117:
                    118:                if (isalpha(c))
                    119:                        *(p++) = c;
                    120:                else
                    121:                        return (0);
                    122:        }
                    123:
1.2     ! xsa       124:        *(p++) = VALDELIM;
1.1       joris     125:
                    126:        while ((c = getc(fp)) != KEYDELIM) {
                    127:                if ((c == EOF) && (feof(fp) | ferror(fp)))
                    128:                        return (0);
                    129:
                    130:                if (c == '\n')
                    131:                        return (0);
                    132:
                    133:                *(p++) = c;
                    134:        }
                    135:
                    136:        if (p[-1] != ' ')
                    137:                return (0);
                    138:
                    139:        /* append trailing KEYDELIM */
                    140:        *(p++) = c;
                    141:        *p = '\0';
                    142:
1.2     ! xsa       143:        printf("     %c%s\n", KEYDELIM, linebuf);
1.1       joris     144:
                    145:        return (0);
                    146: }
                    147:
                    148: void
                    149: ident_usage(void)
                    150: {
                    151:        fprintf(stderr, "Usage: %s [-qV] file ...\n", __progname);
                    152: }