#include <windows.h> #include <stdio.h>
/* vim: set sw=4 ts=4: */
char *sMyName;
void usage(void){ fprintf(stderr,"Usage: %s [options] [filename]\n",sMyName); fprintf(stderr," -buffer Exit value is buffer size (width*1000)+height\n"); fprintf(stderr," -cursor Exit value is cursor position, (X*1000)+Y\n"); fprintf(stderr," -size Exit value is console size (width*1000)+height\n"); fprintf(stderr," Default action is to write settings and console characters to file.\n"); fprintf(stderr," Default filename is 'scrndump.kix'\n"); return; }
int main(int argc, char **argv){
HANDLE hStdout; CONSOLE_SCREEN_BUFFER_INFO csbiInfo; CHAR_INFO *chiBuffer; CHAR_INFO *chiElement; COORD coordBufferSize; COORD coordBufferStart; FILE *fhScript=NULL; int X,Y; int iMode=0; char *sFilename=NULL;
sMyName=argv[0]; /* Open output file, binary mode */ while(--argc){ ++argv; if(strcmp(*argv,"-cursor")==0) iMode='c'; else if(strcmp(*argv,"-buffer")==0) iMode='b'; else if(strcmp(*argv,"-size")==0) iMode='s'; else if(*argv[0]=='-') { fprintf(stderr,"Illegal option '%s'\n",*argv); usage(); return -1; } else { if(sFilename){ fprintf(stderr,"Invalid option '%s', file already opened as '%s'\n",argv[0],sFilename); usage(); return -1; } else { sFilename=argv[0]; } } }
/* Get inherited console handle */ hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
/* Get console attributes */ if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo)){ fprintf(stderr,"GetConsoleScreenBufferInfo failed.\n"); return 1; }
/* Populate "screen size" array */ coordBufferSize.X=csbiInfo.srWindow.Right-csbiInfo.srWindow.Left+1; coordBufferSize.Y=csbiInfo.srWindow.Bottom-csbiInfo.srWindow.Top+1; /* Copy info to top left of our local buffer */ coordBufferStart.X=0; coordBufferStart.Y=0;
/* Allocate buffer for console window information */ if((chiBuffer=(CHAR_INFO *)malloc(coordBufferSize.X*coordBufferSize.Y*sizeof(CHAR_INFO)))==NULL){ fprintf(stderr,"Cannot allocate memory for screen buffer. So Sorry.\n"); return 1; }
/* Copy window area of console buffer into local array */ if(! ReadConsoleOutput( hStdout, chiBuffer, coordBufferSize, coordBufferStart, &csbiInfo.srWindow)){ fprintf(stderr,"Cannot read screen buffer. So Sorry.\n"); return 1; } /* Quick modes */ if(iMode=='c') return (csbiInfo.dwCursorPosition.X*1000+csbiInfo.dwCursorPosition.Y); if(iMode=='s') return (coordBufferSize.X*1000+coordBufferSize.Y); if(iMode=='b') return (csbiInfo.dwSize.X*1000+csbiInfo.dwSize.Y);
/* Dump console attributes */ if(sFilename==NULL) sFilename=".\\scrndump.kix"; if((fhScript=fopen(sFilename,"wb"))==NULL){ fprintf(stderr,"Cannot open dump file '%s' for writing\n",sFilename); return 1; } fprintf(fhScript,"; Screen dump for KiXtart.\r\n; Richard Howarth rhowarth@sgb.co.uk \r\n"); fprintf(fhScript,"$CONSOLE_Height=%d\r\n",coordBufferSize.Y); fprintf(fhScript,"$CONSOLE_Width=%d\r\n",coordBufferSize.X); fprintf(fhScript,"\r\n"); fprintf(fhScript,"$CONSOLE_BUFFER_Height=%d\r\n",csbiInfo.dwSize.Y); fprintf(fhScript,"$CONSOLE_BUFFER_Width=%d\r\n",csbiInfo.dwSize.X); fprintf(fhScript,"\r\n"); fprintf(fhScript,"$CONSOLE_CursorY=%d\r\n",csbiInfo.dwCursorPosition.Y); fprintf(fhScript,"$CONSOLE_CursorX=%d\r\n",csbiInfo.dwCursorPosition.X); fprintf(fhScript,"\r\n"); fprintf(fhScript,"ReDim $CONSOLE_Chars[$CONSOLE_Height]\r\n"); fprintf(fhScript,"\r\n");
chiElement=chiBuffer; for(Y=0;Y<coordBufferSize.Y;Y++){ fprintf(fhScript,"$CONSOLE_Chars[%d]='",Y); for(X=0;X<coordBufferSize.X;X++){ if((chiElement->Char.AsciiChar)=='\''){ fprintf(fhScript,"'+\"'\"+'"); } else { fputc(chiElement->Char.AsciiChar,fhScript); } ++chiElement; } fprintf(fhScript,"'\r\n"); }
close(fhScript);
return 0; }
|