15104ee6eSmrg/* $XTermId: version.c,v 1.8 2024/12/01 20:27:00 tom Exp $ */
20bd37d32Smrg
30bd37d32Smrg/*
45104ee6eSmrg * Copyright 2013-2020,2024 by Thomas E. Dickey
50bd37d32Smrg *
60bd37d32Smrg *                         All Rights Reserved
70bd37d32Smrg *
80bd37d32Smrg * Permission is hereby granted, free of charge, to any person obtaining a
90bd37d32Smrg * copy of this software and associated documentation files (the
100bd37d32Smrg * "Software"), to deal in the Software without restriction, including
110bd37d32Smrg * without limitation the rights to use, copy, modify, merge, publish,
120bd37d32Smrg * distribute, sublicense, and/or sell copies of the Software, and to
130bd37d32Smrg * permit persons to whom the Software is furnished to do so, subject to
140bd37d32Smrg * the following conditions:
150bd37d32Smrg *
160bd37d32Smrg * The above copyright notice and this permission notice shall be included
170bd37d32Smrg * in all copies or substantial portions of the Software.
180bd37d32Smrg *
190bd37d32Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
200bd37d32Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
210bd37d32Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
220bd37d32Smrg * IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
230bd37d32Smrg * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
240bd37d32Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
250bd37d32Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
260bd37d32Smrg *
270bd37d32Smrg * Except as contained in this notice, the name(s) of the above copyright
280bd37d32Smrg * holders shall not be used in advertising or otherwise to promote the
290bd37d32Smrg * sale, use or other dealings in this Software without prior written
300bd37d32Smrg * authorization.
310bd37d32Smrg */
320bd37d32Smrg
330bd37d32Smrg#include <ctype.h>
340bd37d32Smrg#include <xterm.h>
350bd37d32Smrg#include <version.h>
360bd37d32Smrg
370bd37d32Smrg/*
380bd37d32Smrg * Returns the version-string used in the "-v' message as well as a few other
390bd37d32Smrg * places.  It is derived (when possible) from the __vendorversion__ symbol
400bd37d32Smrg * that some newer imake configurations define.
410bd37d32Smrg */
4201037d57Smrgconst char *
430bd37d32SmrgxtermVersion(void)
440bd37d32Smrg{
4501037d57Smrg    static const char vendor_version[] = __vendorversion__;
4601037d57Smrg    static char *buffer;
4701037d57Smrg    const char *result;
480bd37d32Smrg
495104ee6eSmrg    if (buffer == NULL) {
5001037d57Smrg	const char *vendor = vendor_version;
510bd37d32Smrg
52f2e35a3aSmrg	buffer = malloc(sizeof(vendor_version) + 256);
535104ee6eSmrg	if (buffer == NULL) {
540bd37d32Smrg	    result = vendor;
5501037d57Smrg	} else {
56f2e35a3aSmrg	    char first[sizeof(vendor_version)];
57f2e35a3aSmrg	    char second[sizeof(vendor_version)];
582e4f8982Smrg
590bd37d32Smrg	    /* some vendors leave trash in this string */
600bd37d32Smrg	    for (;;) {
610bd37d32Smrg		if (!strncmp(vendor, "Version ", (size_t) 8))
620bd37d32Smrg		    vendor += 8;
630bd37d32Smrg		else if (isspace(CharOf(*vendor)))
640bd37d32Smrg		    ++vendor;
650bd37d32Smrg		else
660bd37d32Smrg		    break;
670bd37d32Smrg	    }
680bd37d32Smrg	    if (strlen(vendor) < BUFSIZ &&
6901037d57Smrg		sscanf(vendor, "%[0-9.] %[A-Za-z_0-9.]", first, second) == 2) {
70f2e35a3aSmrg		sprintf(buffer, "%.80s %.80s(%d)", second, first, XTERM_PATCH);
7101037d57Smrg	    } else {
72f2e35a3aSmrg		sprintf(buffer, "%.80s(%d)", vendor, XTERM_PATCH);
7301037d57Smrg	    }
7401037d57Smrg	    result = buffer;
750bd37d32Smrg	}
7601037d57Smrg    } else {
7701037d57Smrg	result = buffer;
780bd37d32Smrg    }
790bd37d32Smrg    return result;
800bd37d32Smrg}
81