198766Smarkm/*
298766Smarkm * FreeBSD install - a package for the installation and maintainance
398766Smarkm * of non-core utilities.
498766Smarkm *
598766Smarkm * Redistribution and use in source and binary forms, with or without
698766Smarkm * modification, are permitted provided that the following conditions
798766Smarkm * are met:
898766Smarkm * 1. Redistributions of source code must retain the above copyright
998766Smarkm *    notice, this list of conditions and the following disclaimer.
1098766Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1198766Smarkm *    notice, this list of conditions and the following disclaimer in the
1298766Smarkm *    documentation and/or other materials provided with the distribution.
1398766Smarkm *
1498766Smarkm * Jeremy D. Lea.
1598766Smarkm * 11 May 2002
1698766Smarkm *
1798766Smarkm * This is the version module. Based on pkg_version.pl by Bruce A. Mah.
1898766Smarkm *
1998766Smarkm */
2098766Smarkm
2198766Smarkm#include <sys/cdefs.h>
2298766Smarkm__FBSDID("$FreeBSD$");
2398766Smarkm
24179433Sflz
25179433Sflz#include <getopt.h>
26179433Sflz#include <err.h>
27179433Sflz
28222035Sflz#include "lib.h"
2998766Smarkm#include "version.h"
3098766Smarkm
3198766Smarkmchar	*LimitChars = NULL;
3298766Smarkmchar	*PreventChars = NULL;
3398766Smarkmchar	*MatchName = NULL;
34148435Skrionchar	*LookUpOrigin = NULL;
35131275SeikBoolean RegexExtended = FALSE;
36146559ScpercivaBoolean UseINDEXOnly = FALSE;
37148435SkrionBoolean ShowOrigin = FALSE;
3898766Smarkm
39173412Skevlostatic void usage(void);
4098766Smarkm
41179433Sflzstatic char opts[] = "dIhl:L:qs:XtTO:ov";
42179433Sflzstatic struct option longopts[] = {
43179433Sflz	{ "extended",	no_argument,		NULL,		'X' },
44179433Sflz	{ "help",	no_argument,		NULL,		'h' },
45179433Sflz	{ "match",	required_argument,	NULL,		's' },
46179433Sflz	{ "no-status",	required_argument,	NULL,		'L' },
47179433Sflz	{ "origin",	required_argument,	NULL,		'O' },
48179433Sflz	{ "quiet",	no_argument,		NULL,		'q' },
49179433Sflz	{ "show-origin",no_argument,		NULL,		'o' },
50179433Sflz	{ "status",	required_argument,	NULL,		'l' },
51179433Sflz	{ "index-only",	no_argument,		NULL,		'I' },
52179433Sflz	{ "verbose",	no_argument,		NULL,		'v' },
53179433Sflz	{ NULL,		0,			NULL,		0 }
54179433Sflz};
55179433Sflz
5698766Smarkmint
5798766Smarkmmain(int argc, char **argv)
5898766Smarkm{
5998766Smarkm    int ch, cmp = 0;
6098766Smarkm
6198766Smarkm    if (argc == 4 && !strcmp(argv[1], "-t")) {
6298766Smarkm	cmp = version_cmp(argv[2], argv[3]);
6398766Smarkm	printf(cmp > 0 ? ">\n" : (cmp < 0 ? "<\n" : "=\n"));
6498766Smarkm	exit(0);
6598766Smarkm    }
66131275Seik    else if (argc == 4 && !strcmp(argv[1], "-T")) {
67131275Seik	cmp = version_match(argv[3], argv[2]);
68131275Seik	exit(cmp == 1 ? 0 : 1);
69131275Seik    }
70179433Sflz    else while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
7198766Smarkm	switch(ch) {
7298766Smarkm	case 'v':
73159554Sobrien	    Verbose++;
7498766Smarkm	    break;
7598766Smarkm
76146559Scperciva	case 'I':
77146559Scperciva	    UseINDEXOnly = TRUE;
78146559Scperciva	    break;
79146559Scperciva
8098766Smarkm	case 'l':
8198766Smarkm	    LimitChars = optarg;
8298766Smarkm	    break;
8398766Smarkm
8498766Smarkm	case 'L':
8598766Smarkm	    PreventChars = optarg;
8698766Smarkm	    break;
8798766Smarkm
88136643Sobrien	case 'q':
89136643Sobrien	    Quiet = TRUE;
90136643Sobrien	    break;
91136643Sobrien
9298766Smarkm	case 's':
9398766Smarkm	    MatchName = optarg;
9498766Smarkm	    break;
9598766Smarkm
96148435Skrion	case 'O':
97148435Skrion	    LookUpOrigin = optarg;
98148435Skrion	    break;
99148435Skrion
100148435Skrion	case 'o':
101148435Skrion	    ShowOrigin = TRUE;
102148435Skrion	    break;
103148435Skrion
10498766Smarkm	case 't':
10598766Smarkm	    errx(2, "Invalid -t usage.");
10698766Smarkm	    break;
10798766Smarkm
108131275Seik	case 'T':
109131275Seik	    errx(2, "Invalid -T usage.");
110131275Seik	    break;
111131275Seik
112131275Seik	case 'X':
113131275Seik	    RegexExtended = TRUE;
114131275Seik	    break;
115131275Seik
11698766Smarkm	case 'h':
11798766Smarkm	default:
11898766Smarkm	    usage();
11998766Smarkm	    break;
12098766Smarkm	}
12198766Smarkm    }
12298766Smarkm
12398766Smarkm    argc -= optind;
12498766Smarkm    argv += optind;
12598766Smarkm
12698766Smarkm    return pkg_perform(argv);
12798766Smarkm}
12898766Smarkm
12998766Smarkmstatic void
130201226Sedusage(void)
13198766Smarkm{
132131275Seik    fprintf(stderr, "%s\n%s\n%s\n",
133148435Skrion	"usage: pkg_version [-hIoqv] [-l limchar] [-L limchar] [[-X] -s string] [-O origin] [index]",
134131275Seik	"       pkg_version -t v1 v2",
135131275Seik	"       pkg_version -T name pattern");
13698766Smarkm    exit(1);
13798766Smarkm}
138