main.c revision 146559
1/*
2 * FreeBSD install - a package for the installation and maintainance
3 * of non-core utilities.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * Jeremy D. Lea.
15 * 11 May 2002
16 *
17 * This is the version module. Based on pkg_version.pl by Bruce A. Mah.
18 *
19 */
20
21#include <sys/cdefs.h>
22__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/version/main.c 146559 2005-05-24 08:26:44Z cperciva $");
23
24#include "lib.h"
25#include "version.h"
26#include <err.h>
27
28static char Options[] = "dIhl:L:qs:XtTv";
29
30char	*LimitChars = NULL;
31char	*PreventChars = NULL;
32char	*MatchName = NULL;
33Boolean RegexExtended = FALSE;
34Boolean UseINDEXOnly = FALSE;
35
36static void usage __P((void));
37
38int
39main(int argc, char **argv)
40{
41    int ch, cmp = 0;
42
43    if (argc == 4 && !strcmp(argv[1], "-t")) {
44	cmp = version_cmp(argv[2], argv[3]);
45	printf(cmp > 0 ? ">\n" : (cmp < 0 ? "<\n" : "=\n"));
46	exit(0);
47    }
48    else if (argc == 4 && !strcmp(argv[1], "-T")) {
49	cmp = version_match(argv[3], argv[2]);
50	exit(cmp == 1 ? 0 : 1);
51    }
52    else while ((ch = getopt(argc, argv, Options)) != -1) {
53	switch(ch) {
54	case 'v':
55	    Verbose = TRUE;
56	    break;
57
58	case 'I':
59	    UseINDEXOnly = TRUE;
60	    break;
61
62	case 'l':
63	    LimitChars = optarg;
64	    break;
65
66	case 'L':
67	    PreventChars = optarg;
68	    break;
69
70	case 'q':
71	    Quiet = TRUE;
72	    break;
73
74	case 's':
75	    MatchName = optarg;
76	    break;
77
78	case 't':
79	    errx(2, "Invalid -t usage.");
80	    break;
81
82	case 'T':
83	    errx(2, "Invalid -T usage.");
84	    break;
85
86	case 'X':
87	    RegexExtended = TRUE;
88	    break;
89
90	case 'h':
91	case '?':
92	default:
93	    usage();
94	    break;
95	}
96    }
97
98    argc -= optind;
99    argv += optind;
100
101    return pkg_perform(argv);
102}
103
104static void
105usage()
106{
107    fprintf(stderr, "%s\n%s\n%s\n",
108	"usage: pkg_version [-hIqv] [-l limchar] [-L limchar] [[-X] -s string] [index]",
109	"       pkg_version -t v1 v2",
110	"       pkg_version -T name pattern");
111    exit(1);
112}
113