|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T s
Length: 5065 (0x13c9) Types: TextFile Names: »sonnet.c«
└─⟦b20c6495f⟧ Bits:30007238 EUUGD18: Wien-båndet, efterår 1987 └─⟦this⟧ »EUUGD18/General/Sonnet/sonnet.c«
/* ** SONNET ** Curses-based driver for the poetry composer. */ #include <curses.h> #include "sonnet.h" static char RCS[] = "$Header: sonnet.c,v 2.0 89/02/08 16:30:38 rsalz Release1 $"; static int safe = FALSE; extern LINETYPE *Lines; extern int Lcount; /* ** Display the current sonnet */ static void ShowIt() { register LINETYPE *L; register int i; for (L = Lines, i = Lcount; --i >= 0; L++) { (void)move(L->Line + 2, 0); (void)clrtoeol(); if (L->Active) Printw("%2d%c\t%s", L->Active, L->Marked ? '*' : ' ', L->Text); } Printw("\n"); (void)refresh(); } /* ** Mark or un-mark a line. */ static void MarkLine(p, on) register char *p; register int on; { register LINETYPE *L; register int i; register int offset; while (*p && isspace(*p)) p++; if (*p) for (offset = atoi(p), L = Lines, i = Lcount; --i >= 0; L++) if (L->Active == offset) { L->Marked = on; break; } } /* ** Help the user out by listing the legal commands. */ static void DoHelp() { (void)move(0, 0); (void)clrtobot(); Printw("\nSonnet commands:\n"); Printw("\tM #\t\tMark the indicated line\n"); Printw("\tU #\t\tUnmark the indicated line\n"); Printw("\tR\t\tRecompose all unmarked lines\n"); if (!safe) Printw("\t[RETURN]\talso means recompose\n"); Printw("\tQ\t\tQuit\n"); Printw("\tW name\t\tAppend poem to indicated file\n"); Printw("\t?\t\tShow this help text\n"); Printw("\tH\t\tShow this help text\n"); Printw("\n"); Printw(" Any other key redraws the screen\n"); Printw( " Commands may be given in either case and must be ended with a RETURN.\n" ); Printw("\n\n[Press return to continue]"); (void)refresh(); while ((getchar()) != '\n') ; (void)move(0, 0); (void)clrtobot(); (void)refresh(); } \f /* ** Main program. */ main(ac, av) int ac; char *av[]; { register FILE *F; register LINETYPE *L; register char *p; register int i; char *outfile; char *infile; char buff[MAXLEN]; outfile = DEFAULT_OUT_FILE; infile = NULL; while ((i = getopt(ac, av, "f:l:sv")) != EOF) switch (i) { default: Fprintf(stderr, "Usage: %s [-l input] [-f outfilename]\n", av[0]); exit(1); /* NOTREACHED */ case 'f': outfile = optarg; break; case 'l': infile = optarg; break; case 's': safe = TRUE; break; case 'v': printf("Sonnet program Version %d, Patchlevel %d:\n\t%s\n", VERSION, PATCHLEVEL, RCS); exit(0); /* NOTREACHED */ } /* Set up curses. */ (void)initscr(); (void)move(0, 0); (void)clrtobot(); (void)refresh(); /* Wake up, Mr. Shakespeare! */ ComposeInit((char *)NULL, 0); /* Start with nothing, or from an existing poem. */ if (infile == NULL) for (L = Lines, i = Lcount; --i >= 0; L++) ComposeLine(L); else { if ((F = fopen(optarg, "r")) == NULL) { Fprintf(stderr, "Can't open %s for input.\n", optarg); exit(1); /* NOTREACHED */ } for (L = Lines, i = Lcount; --i >= 0; L++) { /* Get a good line or give up. */ if (fgets(L->Text, sizeof L->Text, F) == NULL) { Fprintf(stderr, "Too little data in file.\n"); (void)fclose(F); exit(1); /* NOTREACHED */ } if ((p = IDX(L->Text, '\n')) == NULL) { Fprintf(stderr, "Bad data in file.\n"); (void)fclose(F); exit(1); /* NOTREACHED */ } *p = '\0'; L->Marked = TRUE; SetRhyme(L); } (void)fclose(F); } /* Show what we're starting off with. */ ShowIt(); /* Main processing loop. */ for ( ; ; ) { /* Show old poem and prompt. Cautious curses calls. */ ShowIt(); (void)move(20, 0); (void)clrtobot(); (void)move(20, 0); Printw("[m# u# r w[file] q; ? for help]: "); (void)clrtoeol(); (void)refresh(); /* Get input. */ if (fgets(buff, sizeof buff, stdin) == NULL) buff[0] = 'q'; else if (p = IDX(buff, '\n')) *p = '\0'; /* Blank line in unsafe mode means rewrite. */ if (buff[0] == '\0' && !safe) buff[0] = 'r'; /* Dispatch. */ switch (buff[0]) { default: clearok(curscr, TRUE); touchwin(curscr); (void)refresh(); break; case '?': case 'H': case 'h': DoHelp(); break; case 'M': case 'm': MarkLine(&buff[1], TRUE); break; case 'Q': case 'q': (void)move(LINES - 1, 0); (void)refresh(); Printf("\n\n"); (void)endwin(); exit(0); /* NOTREACHED */ case 'R': case 'r': for (L = Lines, i = Lcount; --i >= 0; L++) ComposeLine(L); break; case 'W': case 'w': if (buff[1]) for (p = &buff[1]; *p && isspace(*p); p++) ; else p = outfile; if ((F = fopen(p, "a")) == NULL) { Printf("Cannot open %s for output.\n", p); exit(1); /* NOTREACHED */ } for (L = Lines, i = Lcount; --i >= 0; L++) Fprintf(F, "%s\n", L->Text); (void)fclose(F); break; case 'U': case 'u': MarkLine(&buff[1], FALSE); break; } } /* NOTREACHED */ }