Annotation of src/usr.bin/rcs/ident.c, Revision 1.27
1.27 ! ray 1: /* $OpenBSD: ident.c,v 1.26 2009/10/15 10:08:43 sobrado 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.24 xsa 27: #include <ctype.h>
28: #include <err.h>
29: #include <stdio.h>
30: #include <stdlib.h>
31: #include <string.h>
32: #include <unistd.h>
1.1 joris 33:
34: #include "rcsprog.h"
35:
1.11 xsa 36: #define KEYDELIM '$' /* keywords delimiter */
37: #define VALDELIM ':' /* values delimiter */
1.1 joris 38:
1.3 joris 39: static int found = 0;
1.16 xsa 40: static int flags = 0;
1.1 joris 41:
1.14 xsa 42: static void ident_file(const char *, FILE *);
43: static void ident_line(FILE *);
1.1 joris 44:
45: int
46: ident_main(int argc, char **argv)
47: {
1.25 ray 48: int i, ch, status;
1.1 joris 49: FILE *fp;
50:
1.25 ray 51: status = 0;
52:
1.5 joris 53: while ((ch = rcs_getopt(argc, argv, "qV")) != -1) {
1.1 joris 54: switch(ch) {
55: case 'q':
1.16 xsa 56: flags |= QUIET;
1.1 joris 57: break;
58: case 'V':
59: printf("%s\n", rcs_version);
60: exit(0);
61: default:
62: (usage)();
63: exit(1);
64: }
65: }
66:
1.5 joris 67: argc -= rcs_optind;
68: argv += rcs_optind;
1.1 joris 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) {
1.17 xsa 75: warn("%s", argv[i]);
1.25 ray 76: status = 1;
1.1 joris 77: continue;
78: }
79:
80: ident_file(argv[i], fp);
1.17 xsa 81: (void)fclose(fp);
1.25 ray 82: if (i != argc - 1)
83: printf("\n");
1.1 joris 84: }
85: }
86:
1.25 ray 87: return (status);
1.1 joris 88: }
89:
90:
1.14 xsa 91: static void
1.1 joris 92: ident_file(const char *filename, FILE *fp)
93: {
94: int c;
95:
1.7 xsa 96: if (filename != NULL)
1.1 joris 97: printf("%s:\n", filename);
1.7 xsa 98: else
1.26 sobrado 99: filename = "standard input";
1.1 joris 100:
1.15 deraadt 101: for (c = 0; c != EOF; c = getc(fp)) {
102: if (feof(fp) || ferror(fp))
1.1 joris 103: break;
104: if (c == KEYDELIM)
105: ident_line(fp);
106: }
1.7 xsa 107:
1.16 xsa 108: if (found == 0 && !(flags & QUIET))
1.7 xsa 109: fprintf(stderr, "ident warning: no id keywords in %s\n",
1.12 deraadt 110: filename);
1.7 xsa 111:
112: found = 0;
1.1 joris 113: }
114:
1.14 xsa 115: static void
1.1 joris 116: ident_line(FILE *fp)
117: {
118: int c;
1.19 joris 119: BUF *bp;
120: size_t len;
1.1 joris 121:
1.27 ! ray 122: bp = buf_alloc(512, BUF_AUTOEXT);
1.1 joris 123:
124: while ((c = getc(fp)) != VALDELIM) {
1.23 ray 125: if (c == EOF)
1.19 joris 126: goto out;
1.1 joris 127:
128: if (isalpha(c))
1.27 ! ray 129: buf_putc(bp, c);
1.1 joris 130: else
1.19 joris 131: goto out;
1.1 joris 132: }
133:
1.27 ! ray 134: buf_putc(bp, VALDELIM);
1.1 joris 135:
136: while ((c = getc(fp)) != KEYDELIM) {
1.23 ray 137: if (c == EOF)
1.19 joris 138: goto out;
1.1 joris 139:
140: if (c == '\n')
1.19 joris 141: goto out;
1.1 joris 142:
1.27 ! ray 143: buf_putc(bp, c);
1.1 joris 144: }
145:
1.27 ! ray 146: len = buf_len(bp);
! 147: if (buf_getc(bp, len - 1) != ' ')
1.19 joris 148: goto out;
1.1 joris 149:
150: /* append trailing KEYDELIM */
1.27 ! ray 151: buf_putc(bp, c);
1.21 ray 152:
153: /* Append newline for printing. */
1.27 ! ray 154: buf_putc(bp, '\n');
1.21 ray 155: printf(" %c", KEYDELIM);
1.22 ray 156: fflush(stdout);
1.27 ! ray 157: buf_write_fd(bp, STDOUT_FILENO);
1.1 joris 158:
1.3 joris 159: found++;
1.19 joris 160: out:
161: if (bp != NULL)
1.27 ! ray 162: buf_free(bp);
1.1 joris 163: }
164:
165: void
166: ident_usage(void)
167: {
1.26 sobrado 168: fprintf(stderr, "usage: ident [-qV] [file ...]\n");
1.1 joris 169: }