/*-------------------------------------------------------------------------*/ /* Program: fortune */ /* Programmer: George Kerber */ /* Written: 06/19/89 */ /* Purpose: Similar to the UNIX Fortune, but centers the lines. */ /* Compiler: Lattice 5.04 */ /*-------------------------------------------------------------------------*/ #include #include #include #include #include #define VERSION "v2.04c" #define WRITTEN "06/19/89 - 01/07/90" void main(); void helpscreen(); void mistake(); void cleanup(); void makeline(); void print_rule(); void main(int argc, char *argv[]) { char officialrule[256] = "Multiple color options", option, opts[] = ""; char *tempy, node[35], pathtorules[80] = "s:fortunes"; int next, error, character, counter = 0, linesflag = 1, sflag = 0; int escape_char = '*', cflag = 2, cflag1 = 0; unsigned char clock[8]; long linecount, rndnumber; FILE *fp, *voice; /*----- Sets escape value -------- default is * ---------------------------*/ tempy = getenv("ESCAPE"); if(tempy != NULL) escape_char = *tempy; /*-------------------------------------------------------------------------*/ getclk(clock); stcgfn(node,argv[0]); if(strcmp(argv[1],"?") == 0 && argc > 1) helpscreen(escape_char,node,cflag); /*---- Error Checking and Option Check ----------------------------------*/ for( next = 1 ; (argopt(argc,argv,opts,&next,&option)) != NULL ; ) { switch(toupper(option)) { case '1': if(cflag1) mistake(officialrule,node); cflag = 1; cflag1 = 1; break; case '2': if(cflag1) mistake(officialrule,node); cflag = 2; cflag1 = 1; break; case '3': if(cflag1) mistake(officialrule,node); cflag = 3; cflag = 1; break; case 'S': sflag = 1; voice = fopen("SPEAK:OPT/d0", "w"); if(voice == (FILE *)NULL) mistake("Can't open SPEAK: Is it MOUNTED?",node); break; case 'L': linesflag = 0; break; default: mistake("Invalid Option",node); } } if(sflag) linesflag = 0; if(argc > next) { strcpy(pathtorules,(argv[next])); } else { if(!access("ram:fortunes",4)) strcpy(pathtorules,"ram:fortunes"); } if(argc > (next + 1)) mistake("Invalid Option Count",node); if(access(pathtorules,4)) mistake("Can't Locate File",node); /*---- Open "fortunes" file -------------------------------------------*/ fp = fopen(pathtorules,"r"); if(fp == (FILE *)NULL) mistake("Can't Open File",node); srand(clock[7] + clock[6] + clock[3] + clock[4]); fseek(fp,-1L,2); linecount = ftell(fp); counter = 0; while(1) { rndnumber = ((rand() % linecount) + 1) ; error = fseek(fp,rndnumber,0); if(error != 0) { if(counter++ > 25) mistake("#1",node); } counter = 0; while((character = fgetc(fp)) != EOF) { if(character == '\n') break; if(counter++ > 100) mistake("#2",node); continue; } if(fgets(officialrule,256,fp) == (char *)NULL) { continue; } else { if(officialrule[0] == ' ' || officialrule[0] == '#') continue; if(linesflag == 1) makeline(cflag); if(!sflag) printf("\n"); print_rule(81,officialrule,escape_char,sflag,voice); } counter = 0; while(1) { if(fgets(officialrule,256,fp) == (char *)NULL) { cleanup(linesflag,sflag,cflag); } else { if(officialrule[0] != ' ' || officialrule[0] == '\n') { cleanup(linesflag,sflag,cflag); } print_rule(80,officialrule,escape_char,sflag,voice); if(counter++ >= 10) cleanup(linesflag,sflag,cflag); } continue; } if(linesflag == 1) makeline(cflag); } } /*-------------------------------------------------------------------------*/ /* End of main program, functions are below */ /*-------------------------------------------------------------------------*/ /* CLEANUP FUNCTION */ void cleanup(int linesflag,int sflag,int cflag) { if(linesflag == 1) makeline(cflag); if(!sflag) printf("\n"); fcloseall(); exit(0); } /*-------------------------------------------------------------------------*/ /* HELPSCREEN FUNCTION */ void helpscreen(char escape_char, char node[],int cflag) { printf("\x0c"); makeline(cflag); printf("\n %s George Kerber %10s %25s\n\n",node,VERSION,WRITTEN); printf(" %s randomly selects a fortune from the s:fortunes file.\n\n",node); printf(" Escape Character is currently set to \"%c\"\n\n",escape_char); printf(" SYNTAX: %s [-l | -s | [-1 | -2 | -3]] [alternative path/name]\n\n",node); printf(" -s fortune is spoken using the Amiga SPEAK: device.\n"); printf(" -l fortune is displayed with no outline.\n"); printf(" -1 fortune is displayed with color 1 (white) outline.\n"); printf(" -2 fortune is displayed with color 2 (black) outline (default)\n"); printf(" -3 fortune is displayed with color 3 (orange) outline.\n\n"); printf(" The fortunes file should be located in the s: directory or provide\n"); printf(" an alternative path/name as an option.\n"); printf(" See program documentation for fortunes file syntax.\n"); makeline(cflag); printf("\n"); exit(0); } /*-------------------------------------------------------------------------*/ /* MAKELINE FUNCTION */ void makeline(int cflag) { printf(""); if(cflag == 1) printf(""); if(cflag == 2) printf(""); if(cflag == 3) printf(""); printf(" ___________________________________________________________________________\n"); } /*-------------------------------------------------------------------------*/ /* MISTAKE FUNCTION */ void mistake(char description[],char node[35]) { printf("\n\n\07 ERROR: %s -->> %s.\n\n", node,description); exit(5); } /*-------------------------------------------------------------------------*/ /* PRINT_RULE FUNCTION */ void print_rule(int number, char rule[],int escape_char,int sflag,FILE *voice) { int counter = -1, offset = 0, i; char fortune[256]; fortune[0] = '\0'; while(rule[++counter] != (char *)NULL) { if(rule[counter] == escape_char) { if(rule[++counter] == escape_char) { i = strlen(fortune) - 1; fortune[i + 1] = escape_char; fortune[i + 2] = '\0'; offset++; continue; } if(!sflag) { switch(rule[counter]) { case '0': strcat(fortune,""); break; /* color 0 char */ case '1': strcat(fortune,""); break; /* color 1 char */ case '2': strcat(fortune,""); break; /* color 2 char */ case '3': strcat(fortune,""); break; /* color 3 char */ case '4': strcat(fortune,""); break; /* default attr */ case '5': strcat(fortune,""); break; /* bold char */ case '6': strcat(fortune,""); break; /* underline char */ case '7': strcat(fortune,""); break; /* italics char */ case ')': strcat(fortune,""); break; /* color 0 bkgnd */ case '!': strcat(fortune,""); break; /* color 1 bkgnd */ case '@': strcat(fortune,""); break; /* color 2 bkgnd */ case '#': strcat(fortune,""); break; /* color 3 bkgnd */ } } } else { i = strlen(fortune) - 1; fortune[i + 1] = rule[counter]; fortune[i + 2] = '\0'; offset++; } continue; } offset = ((number - offset) / 2); for( ; offset > 0 ; strins(fortune," "),offset--); if(!sflag) { printf("%s",fortune); } else { fprintf(voice,"%s",fortune); } } /*-------------------------------------------------------------------------*\ 10/07/89 v2.03: Added escape and color options 11/01/89 v2.03b: Recompiled with Lattice 5.04 01/07/90 v2.03c: Removed all global variables \*-------------------------------------------------------------------------*/