Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!uakari.primate.wisc.edu!crdgw1!uunet!sparky!kent From: marty@wuphys.wustl.edu (Marty Olevitch) Newsgroups: comp.sources.misc Subject: v17i066: shmed - shell form editor, Part01/01 Message-ID: <1991Mar22.222546.18556@sparky.IMD.Sterling.COM> Date: 22 Mar 91 22:25:46 GMT Sender: kent@sparky.IMD.Sterling.COM (Kent Landfield) Organization: Sterling Software, IMD Lines: 1047 Approved: kent@sparky.imd.sterling.com X-Checksum-Snefru: d84230e9 0a11fd70 1378afa5 9db8f30a Submitted-by: Marty Olevitch Posting-number: Volume 17, Issue 66 Archive-name: shmed/part01 This is shmed, version 1.0, a simple editor that gives a shell or perl program form editing capabilities for user input. After reading in a file specifying the form, it is displayed on the screen and the user can edit it. Upon exit, shmed writes out the variable assignment statements to another file which can then be source'd (or .'d or do'd) by the shell. See the manual page for the details. Marty Olevitch Internet: marty@wuphys.wustl.edu Washington University UUCP: uunet!wugate!wuphys!marty Physics Department, Campus Box 1105 Bitnet: marty@wuphys.WUSTL St Louis MO 63130 USA Tel: (314) 889-6285 ------ : This is a shar archive. Extract with sh, not csh. : This archive ends with exit, so do not worry about trailing junk. echo 'Extracting README' sed 's/^X//' > README << '+ END-OF-FILE README' XMarch, 1991 X XThis is shmed, version 1.0, a simple editor that gives a shell or perl Xprogram form editing capabilities for user input. After reading in a Xfile specifying the form, it is displayed on the screen and the user can Xedit it. Upon exit, shmed writes out the variable assignment statements Xto another file which can then be source'd (or .'d or do'd) by the Xshell. See the manual page for the details. X X Copyright (c) 1991 by Marty Olevitch. X Not derived from licensed software. X X Permission is granted to anyone to use this software for any X purpose on any computer system, and to redistribute it freely, X subject to the following restrictions: X X 1. The author is not responsible for the consequences of use of X this software, no matter how awful, even if they arise X from defects in it. X X 2. The origin of this software must not be misrepresented, either X by explicit claim or by omission. X X 3. Altered versions must be plainly marked as such, and must not X be misrepresented as being the original software. X XInstallation: X X1) Edit the Makefile and change the options at the top of the file if Xyou don't like them. X X2) Run "make". This should produce the executable "shmed". X X3) Run "shmed -f testform -o outfile" to try it out. Check the outfile to Xsee what is produced. A trivial perl example is in the file perldemo and one Xfor the shell is in shdemo. Roff is a more complex and possibly even useful Xsh program. X X4) Run "make install" to put the binary and man page in the correct place. X XShmed uses the curses library (the BSD version). It has been tested under XSunOS 4.1, BSD 4.3 (actually mt Xinu MORE/BSD), and the Next machine, Xalthough not a whole lot (this IS version 1.0). X XPlease report any bugs or improvements to me. X XMarty Olevitch Internet: marty@wuphys.wustl.edu XWashington University UUCP: uunet!wugate!wuphys!marty XPhysics Department, Campus Box 1105 Bitnet: marty@wuphys.WUSTL XSt Louis MO 63130 USA Tel: (314) 889-6285 X X XNote: There was another shell form editor, "sf" by Paul Lew, which came out Xon the net a few years ago. I used it for a while, but found that it did Xbizarre things with reverse video mode on some of our terminals. After a Xnot-too-lengthy look at the sf code I decided that it would be easier to Xwrite my own than try to find the problem and fix it. Although, sf inspired Xthe writing of it, shmed is completely different and of course, the code and Xinternal structure are not the same. Ditto for the format of the form Xdefintion file. If you're interested, sf can be found in the archives of Xcomp.sources.unix, volume 14 under the name "shellforms". X + END-OF-FILE README chmod 'u=rw,g=r,o=r' 'README' echo ' -rw-r--r-- 1 marty 2681 Mar 7 15:23 README (as sent)' echo -n ' ' /bin/ls -l README echo 'Extracting Makefile' sed 's/^X//' > Makefile << '+ END-OF-FILE Makefile' X# shmed makefile X XDESTDIR = /usr/local XMANSUFFIX = l XMANDIR = /usr/man/man$(MANSUFFIX) XOWNER = bin XGROUP = bin X XCC = cc X# CFLAGS = -g -Bstatic XLIBS = -lcurses -ltermcap XFILES = README Makefile shmed.man shmed.c util.c formfile.doc \ X Roff perldemo shdemo testform X Xshmed: shmed.o util.o X $(CC) -o shmed shmed.o util.o $(LIBS) X Xclean: X rm *.o X Xinstall: shmed X strip shmed X mv shmed $(DESTDIR) X chmod 755 $(DESTDIR)/shmed X chown $(OWNER) $(DESTDIR)/shmed X chown $(GROUP) $(DESTDIR)/shmed X cp shmed.man $(MANDIR)/shmed.$(MANSUFFIX) X Xshar: shmed.shar X Xshmed.shar: $(FILES) X shar -o shmed.shar $(FILES) + END-OF-FILE Makefile chmod 'u=rw,g=r,o=r' 'Makefile' echo ' -rw-r--r-- 1 marty 598 Mar 7 15:23 Makefile (as sent)' echo -n ' ' /bin/ls -l Makefile echo 'Extracting shmed.man' sed 's/^X//' > shmed.man << '+ END-OF-FILE shmed.man' X.TH shmed L "March 7, 1991" "WU Cosmic Ray Lab" X.SH NAME Xshmed - simple shell form editor X.SH USAGE X.nf Xshmed [-pbc] [-f formfile] -o outfile X-p perl output (default) X-b Bourne shell output X-c csh output X-f file form template file (default stdin) X-o file name of output file (no default) X.SH DIAGNOSTICS Xshmed returns non-zero if there are startup Xproblems or if it is interrupted, else 0. X.SH FILES XFormat of the form template file X.RS XNormal text lines are printed as they appear. XEditable lines should begin with a '.' (dot), and Xcontain 2 or 3 fields separated by a '~' (tilde). X.RS X1st field: label to be printed in the editor. X.br X2nd field: name of variable to assign the value to X.br X3rd field: (optional) default value for variable X.RE X.RE X.RS XOutput file X.RS XThe output file contains a bunch of variable assignments Xin the language you selected (perl, sh, or csh). It should Xbe do'd, .'d, or source'd after shmed is run. X.RE X.SH AUTHOR X.nf XMarty Olevitch XPhysics Dept. XWashington University XSt. Louis, MO 63130 X Xmarty@wuphys.wustl.edu X XMarch, 1991 + END-OF-FILE shmed.man chmod 'u=rw,g=r,o=r' 'shmed.man' echo ' -rw-r--r-- 1 marty 1066 Mar 7 15:23 shmed.man (as sent)' echo -n ' ' /bin/ls -l shmed.man echo 'Extracting shmed.c' sed 's/^X//' > shmed.c << '+ END-OF-FILE shmed.c' X/* shmed - simple shell form editor X * X * USAGE: X * shmed [-pbc] [-f formfile] -o outfile X * -p perl output (default) X * -b Bourne shell output X * -c csh output X * -f formfile name of file containing form (default stdin) X * -o outfile name of output file (no default) X * X * DIAGNOSTICS: X * shmed returns non-zero if there are startup X * problems or if it is interrupted, else 0. X * X * FILES: X * format of the form file X * Normal text lines are printed as they appear. X * Editable lines should begin with a '.' (dot), and X * contain 2 or 3 fields separated by a '~' (tilde). X * 1st field: label to be printed in the editor. X * 2nd field: name of variable to assign the value to X * 3rd field: (optional) default value for the variable X * X * output file X * The output file contains a bunch of variable assignments X * in the language you selected (perl, sh, or csh). It should X * be require'd, .'d, or source'd after shmed is run. X * X * AUTHOR X * X * Marty Olevitch X * Physics Dept. X * Washington University X * St. Louis, MO 63130 X * X * marty@wuphys.wustl.edu X * X * March, 1991 X */ X X#define VERSION "1.0" X X#include X#include X#include X X#define CTL(x) ((x) & 0x3f) X#define ESC 0x1b X#define DEL 0x7f X X#define MAXSTR 500 /* For sizing strings -- DON'T use BUFSIZ! */ X#define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0) X#define BEEP putchar(CTL('G')) X X/* CHOP - replace last character of a string */ X#define CHOP(s, c) ((s)[strlen(s)-1] = (c)) X X#define FIELDSEP "~" /* form file field separator string */ X#define PERLFMT "$%s = '%s';\n" X#define BOURNEFMT "%s='%s'\n" X#define CSHFMT "set %s='%s'\n" X X#define MAXFORMROWS 24 /* max. no. of rows in a form */ Xchar answer[MAXFORMROWS][MAXSTR]; /* response lines */ Xint editable[MAXFORMROWS]; /* tells if form line is editable */ Xchar label[MAXFORMROWS][MAXSTR]; /* labels for editable lines */ Xchar variable[MAXFORMROWS][MAXSTR]; /* variable names for editable lines */ Xint longval; /* length of longest formrow string */ Xint nrows; /* number of rows in our form */ X XWINDOW *helpwin; /* help screen */ XWINDOW *altwin; /* for saving real screen when help is displayed */ X Xchar *progname; XFILE *efopen(); X X/* X - usage - print usage message X */ Xusage() X{ X fprintf(stderr,"USAGE: %s [-pbc] [-f formfile] -o outfile\n", X progname); X} X X/* X - main X */ Xmain(argc, argv) X int argc; X char *argv[]; X{ X int bourneout = 0; /* bourne shell output */ X int cshout = 0; /* csh shell output */ X int done; X char filename[MAXSTR]; /* form file */ X char fmt[MAXSTR]; /* printf format for output */ X int i; X FILE *outfp; X char outputfile[MAXSTR]; /* output file name */ X int outputflag = 0; /* 1 if we have a valid -o argument */ X int perlout = 0; /* perl output */ X int sighandle(); X X int c; X int errflg = 0; X extern int optind; X extern char *optarg; X X progname = argv[0]; X strcpy(filename, "-"); X X while ((c = getopt(argc, argv, "pcbf:o:")) != EOF) { X switch (c) { X case 'p': /* perl output */ X if(bourneout || cshout) { X fprintf(stderr,"Use only one of -c, -b and -p flags\n"); X errflg++; X } else X perlout = 1; X break; X case 'b': /* bourne shell output */ X if(perlout || cshout) { X fprintf(stderr,"Use only one of -c, -b and -p flags\n"); X errflg++; X } else X bourneout = 1; X break; X case 'c': /* csh output */ X if(perlout || bourneout) { X fprintf(stderr,"Use only one of -c, -b and -p flags\n"); X errflg++; X } else X cshout = 1; X break; X case 'f': /* form file name */ X strcpy(filename, optarg); X break; X case 'o': /* output file name */ X outputflag++; X strcpy(outputfile, optarg); X break; X case '?': X default: X errflg++; X break; X } X } X X if (errflg) { X usage(); X exit(2); X } X X if(outputflag == 0) { X fprintf(stderr,"No output file specified\n"); X usage(); X exit(2); X } X X if(perlout == 0 && bourneout == 0 && cshout == 0) X perlout = 1; X X outfp = efopen(outputfile, "w"); X X for(i=0; i= startc ) ? c - 1 : c; X move(r, c); X break; X case CTL('E'): X if(endc < COLS-2) X c = endc; X move(r, c); X break; X case CTL('D'): X delch(); X endc--; X break; X case CTL('G'): X help(); X move(r, c); X break; X case CTL('H'): X case DEL: X if(c != startc) { X c--; X move(r, c); X delch(); X endc--; X } X break; X case CTL('K'): /* delete to end of line */ X clrtoeol(); X endc = c; X break; X case CTL('L'): X wrefresh(curscr); X break; X case CTL('I'): X case CTL('N'): /* next line */ X case '\n': X case '\r': X retcode = 0; X goto finito; X break; X case CTL('P'): X retcode = -1; X goto finito; X break; X case ESC: /* done with form */ X retcode = 1; X goto finito; X break; X default: X if(isprint(k)) { X addch(k); X if(c++ >= COLS-2) { X c = COLS-2; X move(r, c); X } X if(c > endc) X endc++; X } else { X BEEP; X } X break; X } X refresh(); X } Xfinito: X instr(r, longval, endc, str); X return(retcode); X} X X/* X - instr - get the characters between startc and endc on a line X */ Xinstr(row, startc, endc, str) X int row; X int startc; X int endc; X char *str; X{ X int i; X char *cp; X X if(row < 0 || row > LINES - 1) X return(-1); X if(startc < 0 || startc > COLS - 1) X return(-2); X if(endc < 0 || endc > COLS - 1) X return(-3); X X if(startc < endc) { X for(cp=str,i=startc; iendc; i--) { X move(row, i); X *cp++ = inch(); X } X *cp = '\0'; X } X return(0); X} X X/* X - readform - read the lines from the formfile X * X * format of the form file: X * normal text lines - printed as they appear X * . lines - begin with a dot; editable lines X * .