Path: utzoo!bnr-vpa!bnr-fos!bigsur!bnrgate!bnr!janick@crk56.bnr.ca From: janick@crk56.bnr.ca (Janick Bergeron) Newsgroups: comp.sys.apollo Subject: Re: DM editor as a process (Source Code included) Message-ID: <109@crk56.bnr.com> Date: 24 Nov 89 13:06:30 GMT References: <5431@ncar.ucar.edu> Sender: janick.dept_5l41@bnr.CA Reply-To: janick@crk56.bnr.ca Organization: bnr Lines: 223 In article <5431@ncar.ucar.edu> jimr@summer.su.oz.au (Jim Richardson) writes: > >Is there a way of using the DM editor from within a process, for example, >in place of EDITOR=/ucb/bin/vi for editing from within mail, etc? > >Note that > > xdmc ce filename > >is *not* suitable, because the xdmc command returns immediately instead of >waiting for you to exit from the edit pad. > Here is a small C program (I called it dmvi) that will pop-up an edit pad and wait for it to be close before exiting. I use it with rn (I am writing this with it) and its perfect. I am running SR10 so I don't know if it'll work under SR9. Usage: dmvi [-w] [-l #] fname -w : Do not wait for window to close -l : Goto specified line number ---------8<------8<-------8<------ Cut here ----8<------8<------ #include #include #include #include #include /***************************************************************************\ ** ** ** Function name: getopt() ** ** Author: Henry Spencer, UofT ** ** Coding date: 84/04/28 ** ** ** ** Description: ** ** ** ** Parses argv[] for arguments. ** ** Works with Whitesmith's C compiler. ** ** ** ** Inputs - The number of arguments ** ** - The base address of the array of arguments ** ** - A string listing the valid options (':' indicates an ** ** argument to the preceding option is required, a ';' ** ** indicates an argument to the preceding option is optional) ** ** ** ** Outputs - Returns the next option character, ** ** '?' for non '-' arguments ** ** or ':' when there is no more arguments. ** ** ** ** Side Effects + The argument to an option is pointed to by 'optarg' ** ** ** ***************************************************************************** ** ** ** REVISION HISTORY: ** ** ** ** DATE NAME DESCRIPTION ** ** YY/MM/DD ------------------ ------------------------------------ ** ** 88/10/20 Janick Bergeron Returns '?' on unamed arguments ** ** returns '!' on unknown options ** ** and 'EOF' only when exhausted. ** ** 88/11/18 Janick Bergeron Return ':' when no more arguments ** ** 89/08/11 Janick Bergeron Optional optarg when ';' in optstring ** ** ** \***************************************************************************/ char * optarg; /* Global argument pointer. */ char getopt( argc, argv, optstring) int argc; char ** argv; char * optstring; { register int c; register char * place; extern char * index(); static int optind = 0; static char * scan = NULL; optarg = NULL; if ( scan == NULL || *scan == '\0') { if ( optind == 0 ) optind++; if ( optind >= argc ) return ':'; optarg = place = argv[ optind++ ]; if ( place[ 0 ] != '-' || place[ 1 ] == '\0' ) return '?'; if ( place[ 1 ] == '-' && place[ 2 ] == '\0' ) return '?'; scan = place + 1; } c = *scan++; place = index( optstring, c ); if ( place == NULL || c == ':' || c == ';' ) { (void) fprintf( stderr, "%s: unknown option %c\n", argv[ 0 ], c ); scan = NULL; return '!'; } if ( *++place == ':' ) { if ( *scan != '\0') { optarg = scan; scan = NULL; } else { if ( optind >= argc ) { (void) fprintf( stderr, "%s: %c requires an argument\n", argv[ 0 ], c ); return '!'; } optarg = argv[ optind ]; optind++; } } else if ( *place == ';' ) { if ( *scan != '\0' ) { optarg = scan; scan = NULL; } else { if ( optind >= argc || *argv[ optind ] == '-' ) optarg = NULL; else { optarg = argv[ optind ]; optind++; } } } return c; } int main( int argc, char ** argv ) { char opt; char * fname = NULL; char * lineno = NULL; char * p; int wait = 1; int usage = 0; pad_$window_desc_t window; ios_$id_t stream; status_$t status; while( ( opt = getopt( argc, argv, "l:w" ) ) != ':' ) { switch ( opt ) { case 'l': if ( lineno != NULL ) { fprintf( stderr, "%s: Option -l : only one line number can be specified.\n", argv[ 0 ] ); usage = 1; } else { for( p = optarg; *p != '\0'; p++ ) { if ( !isdigit( *p ) ) { fprintf( stderr, "%s: Option -l : Invalid character '%c'.\n", argv[ 0 ], *p ); usage = 1; break; } } if ( *p == '\0' ) lineno = optarg; } break; case 'w': wait = 0; break; case '?': if ( fname != NULL ) { fprintf( stderr, "%s: Only one file to edit can be specified.\n", argv[ 0 ] ); usage = 1; } else fname = optarg; break; default: usage = 1; break; } } if ( fname == NULL ) { fprintf( stderr, "%s: No file to edit were specified.\n", argv[ 0 ] ); usage = 1; } if ( usage ) { fprintf( stderr, "Usage: %s [-l lineno] [-w] fname\n", argv[ 0 ] ); exit( -1 ); } /* Create the window in the next DM default region */ window.width = window.height = 0; pad_$create_window( fname, strlen( fname ), pad_$edit, 1, window, &stream, &status ); if ( lineno != NULL ) { pad_$dm_cmd( stream, lineno, strlen( lineno ), &status ); } if ( wait ) pad_$edit_wait( stream, &status ); exit( 0 ); } -- Janick Bergeron Bell-Northern Research (613) 763-5457 janick@crk56.bnr.ca -- Janick Bergeron Bell-Northern Research (613) 763-5457 janick@crk56.bnr.ca