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

Annotation of src/usr.bin/cvs/history.c, Revision 1.18

1.18    ! xsa         1: /*     $OpenBSD: history.c,v 1.17 2005/05/31 08:26:40 xsa Exp $        */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.5       tedu        4:  * All rights reserved.
1.1       jfb         5:  *
1.5       tedu        6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
1.1       jfb         9:  *
1.5       tedu       10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
1.1       jfb        12:  * 2. The name of the author may not be used to endorse or promote products
1.5       tedu       13:  *    derived from this software without specific prior written permission.
1.1       jfb        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
1.5       tedu       24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        25:  */
                     26:
                     27: #include <sys/param.h>
                     28: #include <sys/stat.h>
                     29:
                     30: #include <errno.h>
1.18    ! xsa        31: #include <fcntl.h>
1.1       jfb        32: #include <stdio.h>
                     33: #include <stdlib.h>
1.18    ! xsa        34: #include <string.h>
1.1       jfb        35: #include <unistd.h>
                     36:
                     37: #include "cvs.h"
                     38: #include "log.h"
1.3       jfb        39: #include "proto.h"
1.1       jfb        40:
                     41: #define CVS_HISTORY_MAXMOD    16
                     42:
                     43: /* history flags */
                     44: #define CVS_HF_A     0x01
                     45: #define CVS_HF_C     0x02
                     46: #define CVS_HF_E     0x04
                     47: #define CVS_HF_L     0x08
                     48: #define CVS_HF_M     0x10
                     49: #define CVS_HF_O     0x20
                     50: #define CVS_HF_T     0x40
                     51: #define CVS_HF_W     0x80
                     52:
                     53: #define CVS_HF_EXCL (CVS_HF_C|CVS_HF_E|CVS_HF_M|CVS_HF_O|CVS_HF_T|CVS_HF_X)
                     54:
1.14      jfb        55: static int  cvs_history_init      (struct cvs_cmd *, int, char **, int *);
                     56: static void cvs_history_print     (struct cvs_hent *);
                     57: static int  cvs_history_pre_exec (struct cvsroot *);
1.1       jfb        58:
                     59: extern char *__progname;
                     60:
1.14      jfb        61: struct cvs_cmd cvs_cmd_history = {
                     62:        CVS_OP_HISTORY, CVS_REQ_HISTORY, "history",
                     63:        { "hi", "his" },
                     64:        "Show repository access history",
1.15      xsa        65:        "[-aceloTw] [-b str] [-D date] [-f file] [-m module] [-n module] "
                     66:        "[-p path] [-r rev] [-t tag] [-u user] [-x ACEFGMORTUW] [-z tz]",
                     67:        "ab:cD:ef:lm:n:op:r:Tt:u:wx:z:",
1.14      jfb        68:        NULL,
                     69:        0,
                     70:        cvs_history_init,
                     71:        cvs_history_pre_exec,
                     72:        NULL,
1.9       joris      73:        NULL,
                     74:        NULL,
                     75:        NULL,
                     76:        CVS_CMD_SENDDIR
                     77: };
                     78:
                     79: static int flags = 0;
1.16      xsa        80: static char *rev = NULL;
1.9       joris      81: static char *user = NULL;
                     82: static char *zone = "+0000";
                     83: static char *tag = NULL;
                     84: static u_int nbmod = 0;
                     85: static u_int rep = 0;
                     86: static char *modules[CVS_HISTORY_MAXMOD];
1.1       jfb        87:
1.14      jfb        88: static int
                     89: cvs_history_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1       jfb        90: {
1.9       joris      91:        int ch;
1.1       jfb        92:
1.14      jfb        93:        while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1       jfb        94:                switch (ch) {
                     95:                case 'a':
                     96:                        flags |= CVS_HF_A;
                     97:                        break;
1.15      xsa        98:                case 'b':
                     99:                        break;
1.1       jfb       100:                case 'c':
                    101:                        rep++;
                    102:                        flags |= CVS_HF_C;
                    103:                        break;
1.15      xsa       104:                case 'D':
                    105:                        break;
1.1       jfb       106:                case 'e':
                    107:                        rep++;
                    108:                        flags |= CVS_HF_E;
                    109:                        break;
1.15      xsa       110:                case 'f':
                    111:                        break;
1.1       jfb       112:                case 'l':
                    113:                        flags |= CVS_HF_L;
                    114:                        break;
                    115:                case 'm':
                    116:                        rep++;
                    117:                        flags |= CVS_HF_M;
                    118:                        if (nbmod == CVS_HISTORY_MAXMOD) {
                    119:                                cvs_log(LP_ERR, "too many `-m' options");
1.13      joris     120:                                return (CVS_EX_USAGE);
1.1       jfb       121:                        }
                    122:                        modules[nbmod++] = optarg;
                    123:                        break;
1.15      xsa       124:                case 'n':
                    125:                        break;
1.1       jfb       126:                case 'o':
                    127:                        rep++;
                    128:                        flags |= CVS_HF_O;
                    129:                        break;
1.16      xsa       130:                case 'r':
                    131:                        rev = optarg;
                    132:                        break;
1.1       jfb       133:                case 'T':
                    134:                        rep++;
                    135:                        flags |= CVS_HF_T;
                    136:                        break;
                    137:                case 't':
                    138:                        tag = optarg;
                    139:                        break;
                    140:                case 'u':
                    141:                        user = optarg;
                    142:                        break;
                    143:                case 'w':
                    144:                        flags |= CVS_HF_W;
                    145:                        break;
                    146:                case 'x':
                    147:                        rep++;
                    148:                        break;
                    149:                case 'z':
                    150:                        zone = optarg;
                    151:                        break;
                    152:                default:
1.13      joris     153:                        return (CVS_EX_USAGE);
1.1       jfb       154:                }
                    155:        }
                    156:
                    157:        if (rep > 1) {
                    158:                cvs_log(LP_ERR,
                    159:                    "Only one report type allowed from: \"-Tcomxe\"");
1.13      joris     160:                return (CVS_EX_USAGE);
1.4       deraadt   161:        } else if (rep == 0)
1.1       jfb       162:                flags |= CVS_HF_O;    /* use -o as default */
                    163:
1.9       joris     164:        *arg = optind;
                    165:        return (0);
                    166: }
1.1       jfb       167:
1.14      jfb       168: static int
                    169: cvs_history_pre_exec(struct cvsroot *root)
1.9       joris     170: {
1.15      xsa       171:        if ((flags & CVS_HF_A) && (cvs_sendarg(root, "-a", 0) < 0))
                    172:                return (CVS_EX_PROTO);
1.1       jfb       173:
1.9       joris     174:        if ((flags & CVS_HF_C) && (cvs_sendarg(root, "-c", 0) < 0))
1.13      joris     175:                return (CVS_EX_PROTO);
1.1       jfb       176:
1.9       joris     177:        if ((flags & CVS_HF_O) && (cvs_sendarg(root, "-o", 0) < 0))
1.13      joris     178:                return (CVS_EX_PROTO);
1.16      xsa       179:
                    180:        if (rev != NULL) {
                    181:                if ((cvs_sendarg(root, "-r", 0) < 0) ||
                    182:                    (cvs_sendarg(root, rev, 0) < 0))
                    183:                        return (CVS_EX_PROTO);
                    184:        }
1.1       jfb       185:
1.9       joris     186:        if (tag != NULL) {
                    187:                if ((cvs_sendarg(root, "-t", 0) < 0) ||
                    188:                    (cvs_sendarg(root, tag, 0) < 0))
1.13      joris     189:                        return (CVS_EX_PROTO);
1.9       joris     190:        }
1.1       jfb       191:
1.15      xsa       192:        /* if no user is specified, get login name of command issuer */
                    193:        if (!(flags & CVS_HF_A) && (user == NULL)) {
                    194:                if ((user = getlogin()) == NULL) {
                    195:                        cvs_log(LP_ERRNO, "cannot get login name");
                    196:                        return (CVS_EX_DATA);
                    197:                }
                    198:        }
                    199:        if (!(flags & CVS_HF_A))
1.9       joris     200:                if ((cvs_sendarg(root, "-u", 0) < 0) ||
                    201:                    (cvs_sendarg(root, user, 0) < 0))
1.13      joris     202:                        return (CVS_EX_PROTO);
1.9       joris     203:
                    204:        if ((cvs_sendarg(root, "-z", 0) < 0) ||
                    205:            (cvs_sendarg(root, zone, 0) < 0))
1.13      joris     206:                return (CVS_EX_PROTO);
1.1       jfb       207:
                    208:        return (0);
                    209: }
                    210:
                    211:
                    212: static void
                    213: cvs_history_print(struct cvs_hent *hent)
                    214: {
                    215:        struct tm etime;
                    216:
                    217:        if (localtime_r(&(hent->ch_date), &etime) == NULL) {
1.17      xsa       218:                cvs_log(LP_ERR, "failed to convert timestamp to structure");
1.1       jfb       219:                return;
                    220:        }
                    221:
                    222:        printf("%c %4d-%02d-%02d %02d:%02d +%04d %-16s %-16s\n",
                    223:            hent->ch_event, etime.tm_year + 1900, etime.tm_mon + 1,
                    224:            etime.tm_mday, etime.tm_hour, etime.tm_min,
                    225:            0, hent->ch_user, hent->ch_repo);
                    226: }