[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.11

1.11    ! xsa         1: /*     $OpenBSD: ident.c,v 1.10 2006/01/05 10:28:24 xsa 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:
1.9       xsa        27: #include "includes.h"
1.1       joris      28:
                     29: #include "rcsprog.h"
                     30:
1.11    ! xsa        31: #define KEYDELIM       '$'     /* keywords delimiter */
        !            32: #define VALDELIM       ':'     /* values delimiter */
1.1       joris      33:
1.3       joris      34: static int found = 0;
1.1       joris      35:
                     36: static int     ident_file(const char *, FILE *);
                     37: static int     ident_line(FILE *);
                     38:
                     39: int
                     40: ident_main(int argc, char **argv)
                     41: {
                     42:        int i, ch;
                     43:        FILE *fp;
                     44:
1.5       joris      45:        while ((ch = rcs_getopt(argc, argv, "qV")) != -1) {
1.1       joris      46:                switch(ch) {
                     47:                case 'q':
                     48:                        verbose = 0;
                     49:                        break;
                     50:                case 'V':
                     51:                        printf("%s\n", rcs_version);
                     52:                        exit(0);
                     53:                default:
                     54:                        (usage)();
                     55:                        exit(1);
                     56:                }
                     57:        }
                     58:
1.5       joris      59:        argc -= rcs_optind;
                     60:        argv += rcs_optind;
1.1       joris      61:
1.2       xsa        62:        if (argc == 0)
1.1       joris      63:                ident_file(NULL, stdin);
1.2       xsa        64:        else {
1.1       joris      65:                for (i = 0; i < argc; i++) {
                     66:                        if ((fp = fopen(argv[i], "r")) == NULL) {
                     67:                                cvs_log(LP_ERRNO, "%s", argv[i]);
                     68:                                continue;
                     69:                        }
                     70:
                     71:                        ident_file(argv[i], fp);
                     72:                        fclose(fp);
                     73:                }
                     74:        }
                     75:
                     76:        return (0);
                     77: }
                     78:
                     79:
                     80: static int
                     81: ident_file(const char *filename, FILE *fp)
                     82: {
                     83:        int c;
                     84:
1.7       xsa        85:        if (filename != NULL)
1.1       joris      86:                printf("%s:\n", filename);
1.7       xsa        87:        else
                     88:                filename = "standard output";
1.1       joris      89:
                     90:        for (c = 0; c != EOF; (c = getc(fp))) {
                     91:                if ((feof(fp)) || (ferror(fp)))
                     92:                        break;
                     93:                if (c == KEYDELIM)
                     94:                        ident_line(fp);
                     95:        }
1.7       xsa        96:
                     97:        if ((found == 0) && (verbose == 1))
                     98:                fprintf(stderr, "ident warning: no id keywords in %s\n",
                     99:                    filename);
                    100:
                    101:        found = 0;
1.1       joris     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.3       joris     143:        found++;
1.2       xsa       144:        printf("     %c%s\n", KEYDELIM, linebuf);
1.1       joris     145:
                    146:        return (0);
                    147: }
                    148:
                    149: void
                    150: ident_usage(void)
                    151: {
1.8       xsa       152:        fprintf(stderr, "usage: ident [-qV] [file ...]\n");
1.1       joris     153: }