st-flexipatch

My st-flexipatch configuration
git clone git://git.ethandl.dev/st-flexipatch
Log | Files | Refs | README | LICENSE

osc7.c (2070B)


      1 static int
      2 hex2int(char c)
      3 {
      4 	if (c >= '0' && c <= '9')
      5 		return c - '0';
      6 	else if (c >= 'a' && c <= 'f')
      7 		return c - 'a' + 10;
      8 	else if (c >= 'A' && c <= 'F')
      9 		return c - 'A' + 10;
     10 	else
     11 		return -1;
     12 }
     13 
     14 int
     15 osc7parsecwd(const char *uri)
     16 {
     17 	const char *auth, *host, *hostend;
     18 	char *path, decoded[PATH_MAX], thishost[_POSIX_HOST_NAME_MAX];
     19 	size_t i, decodedlen, hostlen, urilen;
     20 	int h1, h2;
     21 	if (!term.cwd) {
     22 		term.cwd = xmalloc(sizeof(decoded));
     23 		term.cwd[0] = '\0';
     24 	}
     25 	/* reset cwd if uri is empty */
     26 	if ((urilen = strlen(uri)) == 0) {
     27 		term.cwd[0] = '\0';
     28 		return 1;
     29 	}
     30 	/* decode uri */
     31 	for (decodedlen = 0, i = 0; i < urilen; i++) {
     32 		if (uri[i] == '%' && i <= urilen-3 &&
     33 		    (h1 = hex2int(uri[i+1])) >= 0 && (h2 = hex2int(uri[i+2])) >= 0) {
     34 			decoded[decodedlen++] = (h1 << 4) | h2;
     35 			i += 2;
     36 		} else {
     37 			decoded[decodedlen++] = uri[i];
     38 		}
     39 		if (decodedlen == sizeof(decoded)) {
     40 			fprintf(stderr, "erresc (OSC 7): uri is too long\n");
     41 			return 0;
     42 		}
     43 	}
     44 	decoded[decodedlen] = '\0';
     45 	/* check scheme */
     46 	if (decodedlen < 5 || strncmp("file:", decoded, 5) != 0) {
     47 		fprintf(stderr, "erresc (OSC 7): scheme is not supported: '%s'\n", uri);
     48 		return 0;
     49 	}
     50 	/* find start of authority */
     51 	if (decodedlen < 7 || decoded[5] != '/' || decoded[6] != '/') {
     52 		fprintf(stderr, "erresc (OSC 7): invalid uri: '%s'\n", uri);
     53 		return 0;
     54 	}
     55 	auth = decoded + 7;
     56 	/* find start of path and reset cwd if path is missing */
     57 	if ((path = strchr(auth, '/')) == NULL) {
     58 		term.cwd[0] = '\0';
     59 		return 1;
     60 	}
     61 	/* ignore path if host is not localhost */
     62 	host = ((host = memchr(auth, '@', path - auth)) != NULL) ? host+1 : auth;
     63 	hostend = ((hostend = memchr(host, ':', path - host)) != NULL) ? hostend : path;
     64 	hostlen = hostend - host;
     65 	if (gethostname(thishost, sizeof(thishost)) < 0)
     66 		thishost[0] = '\0';
     67 	if (hostlen > 0 &&
     68 	    !(hostlen == 9 && strncmp("localhost", host, hostlen) == 0) &&
     69 	    !(hostlen == strlen(thishost) && strncmp(host, thishost, hostlen) == 0)) {
     70 		return 0;
     71 	}
     72 	memcpy(term.cwd, path, decodedlen - (path - decoded) + 1);
     73 	return 1;
     74 }