/*
P_SEARCH.C
*/

#include <plib.h>

LOCAL_D VOID *fcb=NULL;

LOCAL_C VOID panic(TEXT *msg, INT errno)
    {
    TEXT bb[E_MAX_ERROR_TEXT_SIZE];

    p_errs(&bb[0],errno);
    p_printf("%s: %s",msg,&bb[0]);
    p_leave(errno);
    }

#pragma save
#pragma ENTER_CALL

LOCAL_C INT CDECL SearchFile(TEXT *file, TEXT *pattern)
    {
    INT ret;
    TEXT line[P_FMAXRSIZE];

    if ((ret=p_open(&fcb,file,P_FTEXT))<0)
        panic("Failed to open file",ret);
    while ((ret=p_read(fcb,&line[0],sizeof(line)))>=0)
        {
        line[ret]=0;
        if (ret && p_ssubi(&line[0],pattern)>=0)
            p_printf(&line[0]);
        }
    p_close(fcb);
    if (ret!=E_FILE_EOF)
        panic("Failed to read file",ret);
    return(0);
    }

#pragma restore

GLDEF_C INT main(VOID)
    {
    TEXT *p;
    TEXT bb[P_FNAMESIZE];

    while (p_getl(">",&bb[0],P_FNAMESIZE))
        {
        p=p_skipch(&bb[0]);
        *p++=0;
        p=p_skipwh(p);
        p_printf("Searching %s for %s",&bb[0],p);
        p_enter3(SearchFile,&bb[0],p);
        }
    return(0);
    }
