📄 browser.c
字号:
/************************************************************************* **** browser.c **** **** File browser. This program is a replacement for more(1). It can **** accept input from a file named on the command line, or from Std **** Input. It can not handle multiple files (but who uses more for **** that anyway?). **** *************************************************************************/#include <stdio.h>#include <Xm/Text.h>void LoadFile(); /* FORWARD Definitions */void UseStdIn();void InputCB();Widget appshell, /* Application Shell */ the_text; /* The text widget */long text_cnt = 0; /* Number of bytes in buffer */void main( argc, argv ) int argc; char *argv[];{ appshell = XtInitialize( argv[0], "Browser", NULL, 0, &argc, argv ); the_text = XmCreateScrolledText( appshell, "TheText", NULL, 0 ); XtManageChild( the_text ); if (argc > 2) { fprintf( stderr, "\nbrowser: Usage:\n" ); fprintf( stderr, " browser FILENAME\n" ); fprintf( stderr, " (or)\n" ); fprintf( stderr, " ??? | browser\n" ); exit( 1 ); } else if (argc == 2) LoadFile( argv[1] ); else UseStdIn(); XtRealizeWidget( appshell ); XtMainLoop();}/***** LoadFile( fname )****** Called when the text is to come from a file, this function*** opens the file and loads it into the text widget.**/void LoadFile( fname ) char *fname;{ FILE *infile; long fsize; char *lclptr; infile = fopen( fname, "r" ); if (infile == NULL) { perror( "browser: unable to open input file" ); exit( 2 ); } fseek( infile, 0, 2 ); fsize = ftell( infile ); rewind( infile ); lclptr = (char *)XtMalloc( fsize + 1 ); fread( lclptr, sizeof(char), fsize, infile ); lclptr[fsize] = '\0'; XmTextSetString( the_text, lclptr ); XtFree( lclptr ); fclose( infile );}/***** UseStdIn()****** This function is called when input is to come from StdIn. It*** simply attaches an input callback.**/void UseStdIn(){ XtAddInput( fileno(stdin), XtInputReadMask, InputCB, NULL );}/***** InputCB( client_data, source, id )****** Called whenever data is present on StdIn. This attempts to*** read 255 bytes, or the number of bytes in the stdin buffer, *** whichever is smaller. It will read at least one byte, which*** will fill an empty buffer.**/void InputCB( client_data, source, id ) caddr_t client_data; int *source; XtInputId *id;{ char lcl_buf[256]; int in_char; int buf_idx = 0; int read_cnt = (stdin->_cnt > 255 ? 255 : stdin->_cnt); if (read_cnt == 0) read_cnt = 1; while (read_cnt--) if ((in_char = getchar()) != EOF) lcl_buf[buf_idx++] = in_char; else { XtRemoveInput( *id ); break; } lcl_buf[buf_idx] = '\0'; XmTextReplace( the_text, text_cnt, text_cnt, lcl_buf ); text_cnt += buf_idx;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -