| 1 | /* $Id: ignore.c,v 1.2 2002-03-10 04:56:58 bird Exp $
|
|---|
| 2 | *
|
|---|
| 3 | * Ignores lines matching a certain pattern.
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright (c) 2001-2002 knut st. osmundsen (bird@anduin.net)
|
|---|
| 6 | *
|
|---|
| 7 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 8 | *
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 | /*******************************************************************************
|
|---|
| 13 | * Defined Constants And Macros *
|
|---|
| 14 | *******************************************************************************/
|
|---|
| 15 | #define HF_STDIN 0
|
|---|
| 16 | #define HF_STDOUT 1
|
|---|
| 17 | #define HF_STDERR 2
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | /*******************************************************************************
|
|---|
| 22 | * Header Files *
|
|---|
| 23 | *******************************************************************************/
|
|---|
| 24 | #define INCL_BASE
|
|---|
| 25 | #include <os2.h>
|
|---|
| 26 | #include <process.h>
|
|---|
| 27 | #include <stdio.h>
|
|---|
| 28 | #include <stdlib.h>
|
|---|
| 29 | #include <string.h>
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | /*******************************************************************************
|
|---|
| 34 | * Global Variables *
|
|---|
| 35 | *******************************************************************************/
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | int main(int argc, char **argv)
|
|---|
| 39 | {
|
|---|
| 40 | int rc;
|
|---|
| 41 | int iArgCmd;
|
|---|
| 42 | char szBuffer[4096];
|
|---|
| 43 | int iBuffer;
|
|---|
| 44 | ULONG cchRead;
|
|---|
| 45 | BOOL fIgnore;
|
|---|
| 46 |
|
|---|
| 47 | RESULTCODES Res;
|
|---|
| 48 | PID pid;
|
|---|
| 49 | HFILE hStdOut = HF_STDOUT;
|
|---|
| 50 | HFILE hStdErr = HF_STDERR;
|
|---|
| 51 | HFILE hStdOutSave = -1;
|
|---|
| 52 | HFILE hStdErrSave = -1;
|
|---|
| 53 | HPIPE hPipeR = NULLHANDLE;
|
|---|
| 54 | HPIPE hPipeW = NULLHANDLE;
|
|---|
| 55 |
|
|---|
| 56 | /*
|
|---|
| 57 | * Find the command. (first argument without dash)
|
|---|
| 58 | */
|
|---|
| 59 | for (iArgCmd = 1; iArgCmd < argc; iArgCmd++)
|
|---|
| 60 | if (argv[iArgCmd][0] != '-')
|
|---|
| 61 | break;
|
|---|
| 62 |
|
|---|
| 63 | /*
|
|---|
| 64 | * Fail if no command.
|
|---|
| 65 | */
|
|---|
| 66 | if (iArgCmd >= argc)
|
|---|
| 67 | {
|
|---|
| 68 | fprintf(stderr, "Fatal error: no command given\n");
|
|---|
| 69 | return 8;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /*
|
|---|
| 73 | * Redirect.
|
|---|
| 74 | */
|
|---|
| 75 | rc = DosCreatePipe(&hPipeR, &hPipeW, sizeof(szBuffer) - 1);
|
|---|
| 76 | if (rc)
|
|---|
| 77 | {
|
|---|
| 78 | fprintf(stderr, "Internal Error: Failed to create pipe! rc=%d\n", rc);
|
|---|
| 79 | return 8;
|
|---|
| 80 | }
|
|---|
| 81 | DosDupHandle(HF_STDOUT, &hStdOutSave);
|
|---|
| 82 | DosDupHandle(HF_STDERR, &hStdErrSave);
|
|---|
| 83 | DosDupHandle(hPipeW, &hStdOut);
|
|---|
| 84 | DosDupHandle(hPipeW, &hStdErr);
|
|---|
| 85 |
|
|---|
| 86 | /*
|
|---|
| 87 | * Star child and restore filehandles.
|
|---|
| 88 | */
|
|---|
| 89 | /*rc = DosExecPgm(szObj, sizeof(szObj), EXEC_ASYNCRESULT,
|
|---|
| 90 | szArg, pJob->JobInfo.szzEnv, &Res, szArg);*/
|
|---|
| 91 | rc = spawnvp(P_NOWAIT, argv[iArgCmd], &argv[iArgCmd]);
|
|---|
| 92 | pid = (PID)rc;
|
|---|
| 93 | DosClose(hStdOut); hStdOut = HF_STDOUT;
|
|---|
| 94 | DosClose(hStdErr); hStdErr = HF_STDERR;
|
|---|
| 95 | DosDupHandle(hStdOutSave, &hStdOut);
|
|---|
| 96 | DosDupHandle(hStdErrSave, &hStdErr);
|
|---|
| 97 | DosClose(hStdOutSave);
|
|---|
| 98 | DosClose(hStdErrSave);
|
|---|
| 99 | DosClose(hPipeW);
|
|---|
| 100 |
|
|---|
| 101 | /*
|
|---|
| 102 | * Check for errors.
|
|---|
| 103 | */
|
|---|
| 104 | if ((int)rc < 0)
|
|---|
| 105 | {
|
|---|
| 106 | fprintf(stderr, "Fatal error: Failed to execute command (%s)\n", argv[iArgCmd]);
|
|---|
| 107 | return 8;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /*
|
|---|
| 111 | * Read Output.
|
|---|
| 112 | */
|
|---|
| 113 | fIgnore = FALSE;
|
|---|
| 114 | while (( (rc = DosRead(hPipeR, &szBuffer[iBuffer], 256, &cchRead)) == NO_ERROR
|
|---|
| 115 | || rc == ERROR_MORE_DATA
|
|---|
| 116 | )
|
|---|
| 117 | && cchRead != 0
|
|---|
| 118 | )
|
|---|
| 119 | {
|
|---|
| 120 | int i;
|
|---|
| 121 | int iStartLine;
|
|---|
| 122 | ULONG cchWritten;
|
|---|
| 123 | cchRead += iBuffer;
|
|---|
| 124 | szBuffer[cchRead] = '\0';
|
|---|
| 125 |
|
|---|
| 126 | /*
|
|---|
| 127 | * Process string.
|
|---|
| 128 | */
|
|---|
| 129 | for (iStartLine = i = 0; i < cchRead; i++)
|
|---|
| 130 | {
|
|---|
| 131 | if (fIgnore)
|
|---|
| 132 | { /* currently in ignore mode */
|
|---|
| 133 | if (szBuffer[i] == '\r')
|
|---|
| 134 | {
|
|---|
| 135 | if (szBuffer[i + 1] == '\n')
|
|---|
| 136 | i++;
|
|---|
| 137 | fIgnore = FALSE;
|
|---|
| 138 | }
|
|---|
| 139 | else if (szBuffer[i] == '\n')
|
|---|
| 140 | fIgnore = FALSE;
|
|---|
| 141 |
|
|---|
| 142 | if (!fIgnore)
|
|---|
| 143 | {
|
|---|
| 144 | memmove(&szBuffer[iStartLine], &szBuffer[i + 1], cchRead - i); /* could possibly get away without this move somehow.. not sure if we will gain anything... */
|
|---|
| 145 | cchRead -= i - iStartLine + 1;
|
|---|
| 146 | i = iStartLine - 1;
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 | else
|
|---|
| 150 | {
|
|---|
| 151 | /* check for end of line */
|
|---|
| 152 | if (szBuffer[i] == '\r' || szBuffer[i] == '\n')
|
|---|
| 153 | {
|
|---|
| 154 | if (szBuffer[i] == '\r' && szBuffer[i + 1] == '\n')
|
|---|
| 155 | i++;
|
|---|
| 156 | iStartLine = i + 1;
|
|---|
| 157 | }
|
|---|
| 158 | else
|
|---|
| 159 | { /* check for ignore pattern */
|
|---|
| 160 | int j;
|
|---|
| 161 | for (j = 1; j < iArgCmd; j++)
|
|---|
| 162 | if (!strnicmp(&argv[j][1], &szBuffer[i], strlen(&argv[j][1])))
|
|---|
| 163 | {
|
|---|
| 164 | fIgnore = TRUE;
|
|---|
| 165 | break;
|
|---|
| 166 | }
|
|---|
| 167 | }
|
|---|
| 168 | }
|
|---|
| 169 | } /* for - scan output */
|
|---|
| 170 |
|
|---|
| 171 | /*
|
|---|
| 172 | * Write output.
|
|---|
| 173 | */
|
|---|
| 174 | if (iStartLine > 0)
|
|---|
| 175 | DosWrite(HF_STDOUT, &szBuffer[0], iStartLine, &cchWritten);
|
|---|
| 176 |
|
|---|
| 177 | /*
|
|---|
| 178 | * Move last (unfinished) line to start of buffer.
|
|---|
| 179 | */
|
|---|
| 180 | if (iStartLine > 0)
|
|---|
| 181 | memmove(&szBuffer[0], &szBuffer[iStartLine], cchRead - iStartLine + 1);
|
|---|
| 182 | iBuffer = cchRead - iStartLine;
|
|---|
| 183 |
|
|---|
| 184 | } /* while - read pipe */
|
|---|
| 185 |
|
|---|
| 186 | /* finished reading */
|
|---|
| 187 | DosClose(hPipeR);
|
|---|
| 188 |
|
|---|
| 189 | /*
|
|---|
| 190 | * Write remaining part of buffer.
|
|---|
| 191 | */
|
|---|
| 192 | if (!fIgnore && iBuffer > 0)
|
|---|
| 193 | DosWrite(HF_STDOUT, &szBuffer[0], strlen(szBuffer), &cchRead);
|
|---|
| 194 |
|
|---|
| 195 | /*
|
|---|
| 196 | * Get result.
|
|---|
| 197 | */
|
|---|
| 198 | DosWaitChild(DCWA_PROCESS, DCWW_WAIT, &Res, &pid, Res.codeTerminate);
|
|---|
| 199 | if (Res.codeTerminate == TC_EXIT)
|
|---|
| 200 | return Res.codeResult;
|
|---|
| 201 | return Res.codeResult != 0 ? Res.codeResult : 8;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|