start on improving __paginate

This commit is contained in:
Gavin Hayes 2024-04-12 01:59:54 -04:00
parent 69db501c68
commit 2a2c1ad806

View file

@ -16,7 +16,9 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/calls/calls.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/limits.h"
#include "libc/runtime/runtime.h"
@ -29,16 +31,22 @@
void __paginate(int fd, const char *s) {
int tfd, pid;
char *args[3] = {0};
char tmppath[] = "/tmp/paginate.XXXXXX";
char tmppath[PATH_MAX-32] = "/tmp/paginate.XXXXXX";
char progpath[PATH_MAX];
if (strcmp(nulltoempty(getenv("TERM")), "dumb") && isatty(0) && isatty(1) &&
((args[0] = commandv("less", progpath, sizeof(progpath))) ||
(args[0] = commandv("more", progpath, sizeof(progpath))) ||
(args[0] = commandv("more.exe", progpath, sizeof(progpath))))) {
(args[0] = commandv("more.exe", progpath, sizeof(progpath))) ||
(args[0] = commandv("more.com", progpath, sizeof(progpath))))) {
if ((tfd = mkstemp(tmppath)) != -1) {
write(tfd, s, strlen(s));
close(tfd);
args[1] = tmppath;
if (IsWindows() && strcmp(args[0], "/C/Windows/System32/more.com") == 0) {
char16_t widepath[PATH_MAX];
__mkntpath(tmppath, widepath);
tprecode16to8(tmppath, sizeof(tmppath), widepath);
}
if ((pid = fork()) != -1) {
putenv("LC_ALL=C.UTF-8");
putenv("LESSCHARSET=utf-8");