11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1992, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes * 4. Neither the name of the University nor the names of its contributors
141556Srgrimes *    may be used to endorse or promote products derived from this software
151556Srgrimes *    without specific prior written permission.
161556Srgrimes *
171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271556Srgrimes * SUCH DAMAGE.
281556Srgrimes */
291556Srgrimes
3090143Smarkm#if 0
311556Srgrimes#ifndef lint
3236049Scharnierstatic char sccsid[] = "@(#)fmt.c	8.4 (Berkeley) 4/15/94";
3336049Scharnier#endif
3490143Smarkm#endif
35110391Scharnier
3699110Sobrien#include <sys/cdefs.h>
3799110Sobrien__FBSDID("$FreeBSD$");
381556Srgrimes
3941580Sbde#include <sys/types.h>
401556Srgrimes#include <sys/time.h>
411556Srgrimes#include <sys/resource.h>
4290143Smarkm
4341580Sbde#include <err.h>
4496053Sjmallett#include <limits.h>
451556Srgrimes#include <stdio.h>
4699457Smike#include <stdint.h>
471556Srgrimes#include <stdlib.h>
481556Srgrimes#include <string.h>
4937027Sjkoshy#include <unistd.h>
501556Srgrimes#include <vis.h>
5190143Smarkm
521556Srgrimes#include "ps.h"
531556Srgrimes
54130896Sgadstatic char *cmdpart(char *);
55130896Sgadstatic char *shquote(char **);
561556Srgrimes
57130896Sgadstatic char *
58130896Sgadshquote(char **argv)
591556Srgrimes{
6096053Sjmallett	long arg_max;
6196053Sjmallett	static size_t buf_size;
6290143Smarkm	size_t len;
63130896Sgad	char **p, *dst, *src;
6437027Sjkoshy	static char *buf = NULL;
651556Srgrimes
6637027Sjkoshy	if (buf == NULL) {
6737027Sjkoshy		if ((arg_max = sysconf(_SC_ARG_MAX)) == -1)
6837027Sjkoshy			errx(1, "sysconf _SC_ARG_MAX failed");
69114600Smarkm		if (arg_max >= LONG_MAX / 4 || arg_max >= (long)(SIZE_MAX / 4))
7096053Sjmallett			errx(1, "sysconf _SC_ARG_MAX preposterously large");
7196053Sjmallett		buf_size = 4 * arg_max + 1;
7296053Sjmallett		if ((buf = malloc(buf_size)) == NULL)
7396053Sjmallett			errx(1, "malloc failed");
7437027Sjkoshy	}
7537027Sjkoshy
7697845Sjmallett	if (*argv == NULL) {
7797845Sjmallett		buf[0] = '\0';
781556Srgrimes		return (buf);
791556Srgrimes	}
801556Srgrimes	dst = buf;
8197845Sjmallett	for (p = argv; (src = *p++) != NULL; ) {
8297845Sjmallett		if (*src == '\0')
831556Srgrimes			continue;
8496053Sjmallett		len = (buf_size - 1 - (dst - buf)) / 4;
8589575Smikeh		strvisx(dst, src, strlen(src) < len ? strlen(src) : len,
8689575Smikeh		    VIS_NL | VIS_CSTYLE);
8797845Sjmallett		while (*dst != '\0')
881556Srgrimes			dst++;
8996053Sjmallett		if ((buf_size - 1 - (dst - buf)) / 4 > 0)
9089575Smikeh			*dst++ = ' ';
911556Srgrimes	}
926971Sdg	/* Chop off trailing space */
9389575Smikeh	if (dst != buf && dst[-1] == ' ')
946971Sdg		dst--;
951556Srgrimes	*dst = '\0';
961556Srgrimes	return (buf);
971556Srgrimes}
981556Srgrimes
99130896Sgadstatic char *
100130896Sgadcmdpart(char *arg0)
1011556Srgrimes{
102130896Sgad	char *cp;
1031556Srgrimes
1041556Srgrimes	return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
1051556Srgrimes}
1061556Srgrimes
10790143Smarkmconst char *
108245610Sjhbfmt_argv(char **argv, char *cmd, char *thread, size_t maxlen)
1091556Srgrimes{
11090143Smarkm	size_t len;
111130896Sgad	char *ap, *cp;
1121556Srgrimes
11390143Smarkm	if (argv == NULL || argv[0] == NULL) {
1141556Srgrimes		if (cmd == NULL)
1151556Srgrimes			return ("");
1161556Srgrimes		ap = NULL;
1171556Srgrimes		len = maxlen + 3;
1181556Srgrimes	} else {
1191556Srgrimes		ap = shquote(argv);
1201556Srgrimes		len = strlen(ap) + maxlen + 4;
1211556Srgrimes	}
12290143Smarkm	cp = malloc(len);
12390143Smarkm	if (cp == NULL)
12497847Sjmallett		errx(1, "malloc failed");
125245610Sjhb	if (ap == NULL) {
126245635Sjhb		if (thread != NULL) {
127245610Sjhb			asprintf(&ap, "%s/%s", cmd, thread);
128245610Sjhb			sprintf(cp, "[%.*s]", (int)maxlen, ap);
129245610Sjhb			free(ap);
130245610Sjhb		} else
131245610Sjhb			sprintf(cp, "[%.*s]", (int)maxlen, cmd);
132245610Sjhb	} else if (strncmp(cmdpart(argv[0]), cmd, maxlen) != 0)
13390143Smarkm		sprintf(cp, "%s (%.*s)", ap, (int)maxlen, cmd);
1341556Srgrimes	else
135130897Sgad		strcpy(cp, ap);
1361556Srgrimes	return (cp);
1371556Srgrimes}
138