Annotation of src/usr.bin/rcs/ident.c, Revision 1.22
1.22 ! ray 1: /* $OpenBSD: ident.c,v 1.21 2006/08/01 05:14:17 ray 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.16 xsa 35: static int flags = 0;
1.1 joris 36:
1.14 xsa 37: static void ident_file(const char *, FILE *);
38: static void ident_line(FILE *);
1.1 joris 39:
40: int
41: ident_main(int argc, char **argv)
42: {
43: int i, ch;
44: FILE *fp;
45:
1.5 joris 46: while ((ch = rcs_getopt(argc, argv, "qV")) != -1) {
1.1 joris 47: switch(ch) {
48: case 'q':
1.16 xsa 49: flags |= QUIET;
1.1 joris 50: break;
51: case 'V':
52: printf("%s\n", rcs_version);
53: exit(0);
54: default:
55: (usage)();
56: exit(1);
57: }
58: }
59:
1.5 joris 60: argc -= rcs_optind;
61: argv += rcs_optind;
1.1 joris 62:
1.2 xsa 63: if (argc == 0)
1.1 joris 64: ident_file(NULL, stdin);
1.2 xsa 65: else {
1.1 joris 66: for (i = 0; i < argc; i++) {
67: if ((fp = fopen(argv[i], "r")) == NULL) {
1.17 xsa 68: warn("%s", argv[i]);
1.1 joris 69: continue;
70: }
71:
72: ident_file(argv[i], fp);
1.17 xsa 73: (void)fclose(fp);
1.1 joris 74: }
75: }
76:
77: return (0);
78: }
79:
80:
1.14 xsa 81: static void
1.1 joris 82: ident_file(const char *filename, FILE *fp)
83: {
84: int c;
85:
1.7 xsa 86: if (filename != NULL)
1.1 joris 87: printf("%s:\n", filename);
1.7 xsa 88: else
89: filename = "standard output";
1.1 joris 90:
1.15 deraadt 91: for (c = 0; c != EOF; c = getc(fp)) {
92: if (feof(fp) || ferror(fp))
1.1 joris 93: break;
94: if (c == KEYDELIM)
95: ident_line(fp);
96: }
1.7 xsa 97:
1.16 xsa 98: if (found == 0 && !(flags & QUIET))
1.7 xsa 99: fprintf(stderr, "ident warning: no id keywords in %s\n",
1.12 deraadt 100: filename);
1.7 xsa 101:
102: found = 0;
1.1 joris 103: }
104:
1.14 xsa 105: static void
1.1 joris 106: ident_line(FILE *fp)
107: {
108: int c;
1.19 joris 109: BUF *bp;
110: size_t len;
1.1 joris 111:
1.19 joris 112: bp = rcs_buf_alloc(512, BUF_AUTOEXT);
1.1 joris 113:
114: while ((c = getc(fp)) != VALDELIM) {
1.15 deraadt 115: if (c == EOF && (feof(fp) | ferror(fp)))
1.19 joris 116: goto out;
1.1 joris 117:
118: if (isalpha(c))
1.19 joris 119: rcs_buf_putc(bp, c);
1.1 joris 120: else
1.19 joris 121: goto out;
1.1 joris 122: }
123:
1.19 joris 124: rcs_buf_putc(bp, VALDELIM);
1.1 joris 125:
126: while ((c = getc(fp)) != KEYDELIM) {
1.15 deraadt 127: if (c == EOF && (feof(fp) | ferror(fp)))
1.19 joris 128: goto out;
1.1 joris 129:
130: if (c == '\n')
1.19 joris 131: goto out;
1.1 joris 132:
1.19 joris 133: rcs_buf_putc(bp, c);
1.1 joris 134: }
135:
1.19 joris 136: len = rcs_buf_len(bp);
137: if (rcs_buf_getc(bp, len - 1) != ' ')
138: goto out;
1.1 joris 139:
140: /* append trailing KEYDELIM */
1.19 joris 141: rcs_buf_putc(bp, c);
1.21 ray 142:
143: /* Append newline for printing. */
144: rcs_buf_putc(bp, '\n');
145: printf(" %c", KEYDELIM);
1.22 ! ray 146: fflush(stdout);
1.21 ray 147: rcs_buf_write_fd(bp, STDOUT_FILENO);
1.1 joris 148:
1.3 joris 149: found++;
1.19 joris 150: out:
151: if (bp != NULL)
152: rcs_buf_free(bp);
1.1 joris 153: }
154:
155: void
156: ident_usage(void)
157: {
1.18 jmc 158: fprintf(stderr, "usage: ident [-qV] file ...\n");
1.1 joris 159: }