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