#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <libraries/dosextens.h>
#include <proto/dos.h>

#include "/lib/iomodes.h"
#include "/lib/misc.h"

/* manual-program for AXsh. Programmed by Pasi Ojala. Last update 1-Dec-91 */

char	Manual[64]="AXsh:man/";
int		Rows=24;

FILE *fopen(),*handle;

void __regargs __chkabort(void);
void __regargs __chkabort(void){}
void chkabort(void) {}
/*int CXBRK() {return 0;}*/
int instr(char *,char *);
int	more(char *filename);

void main(int argc,char *argv[])
{	char file[128],line[128];
	int fine=0,i,done,count;

	setmode(MODE_RAW);
	if(who(COM_GET,NULL,"rows",line))	Rows=24; /* default value */
	else								Rows=atoi(line);
	if(Rows<5)		Rows=5;
	if(Rows>100)	Rows=100;

	if(argc>1)
	{
		if(!strcmp(argv[1],"-k"))
		{
			if(argc<3)
				printf("You need a search word. \"man -k -\" to list all commands.\n");
			else
			{
				sprintf(file,"%sReference",Manual);
				if(handle=fopen(file,"rb"))
				{
					count=0;
					while(!feof(handle))
					{
						i=0;
						while((line[i]=fgetc(handle))!=EOF && line[i]!=0x0a && line[i]!=0x0d)	i++;
						if(line[i]==EOF) break;
						line[i]=0;
						if(instr(line,argv[2]))
						{
							printf("%s\n",line);
							if(++count>Rows-3)
							{	printf("\033[7mMore?\033[m");
								if(getchar()==0x03)		break;
								printf("\b\b\b\b\b\033[K");
								count=0;
							}
						}
					}
					fclose(handle);
					fine=1;
				}
				else
				{
					printf("Manual reference file '%s' not found!\n",file);
					fine=1;
				}
			}
		}
		else
		{
			done=0;
			sprintf(file,"%s%s.man",Manual,argv[1]);
			sprintf(line,"%sManualAliases",Manual);
			if(handle=fopen(line,"rb"))
			{
				while(!feof(handle))
				{
					i=0;
					while((line[i]=fgetc(handle))!=EOF && line[i]!=0x0a && line[i]!=0x0d)	i++;
					if(line[i]==EOF) break;
					line[i]=0;	/* ex: 'alias axsh' allows 'alias'->'axsh', but not 'al'->'ias axsh' */
					if(!strncmp(line,argv[1],strlen(argv[1])) && line[strlen(argv[1])]==' ')
					{
						sprintf(file,"%s%s.man",Manual,line+strlen(argv[1])+1);
						break;
					}
				}
				fclose(handle);
			}
			if(!more(file))	printf("No manual page for %s\n",argv[1]);
			fine=1;
		}
	}
	if (!fine)	printf("Usage: 'man [-k word] [program]'\n");
	setmode(MODE_CONSOLE);
}

int instr(char *a,char *b)
{	register int c2=strlen(b),c1=strlen(a)-c2,found=0;
	if(c1>=0)
	{	while(c1>=0 && !found)
		{	found=!strncmp(a+c1,b,c2);
			c1--;
		}
	}
	return found;
}

int more(char *filename)
{	FILE *handle;
	int c,i,skip=0,lines=0;
	char line[132],a,inte=IsInteractive(Output());

	if(handle=fopen(filename,"rb")) 
	{
		i=0;
		do
		{
			line[i++]=c=fgetc(handle);
			if (c==0x0a || c==0x0d || i>130)
			{
				if (skip)
				{	skip--;
				}
				else
				{
					if(inte && (lines++>(Rows-2) || WaitForChar(Input(),1)))
					{	printf("\033[7m-More-\033[m");
						do
						{	Read(Input(),&a,1);
							if(a=='h' || a=='?')
							{	printf("More help:\nq,^c - quit\nf - skip full page\nSPACE - next page\n");
								printf("CR,LF - next line\nany other - next half page\n");
								printf("\033[7m-More-\033[m");
							}
						}
						while(a=='h');
						printf("\b\b\b\b\b\b\033[K");
						if(a=='q' || a==0x03)
						{
							printf("***Quit***\n");
							fclose(handle);
							return -1;
						}
						if(a=='f')
						{
							printf("-skipped one page-\n");
							skip=Rows-2;
							lines-=Rows-2;
						}
						if(a==' ') lines-=Rows-2;	/* full page */
						if(a==0x0a || a==0x0d) lines--;	/* one line */
						if(a!='q' && a!=' ' && a!='f' && a!=0x0a && a!=0x0d)
							lines-=(Rows-2)/2+1;	/* half page */
						if(lines<0) lines=0;
					}
					line[i]=0;
					if (!skip)
					{	printf("%s",line);
					}
				}
				i=0;
			}
		}
		while(c!=EOF);
		fclose(handle);
		return -1;
	}
	else return 0;
}
