/*****************************************************************
		 	       SILLYDIET :-)

  A simple diet planning program
  
  Copyright (©) 1992-95  The Xperts Group Inc.
  Author: Manolis S Pappas, Thermopilon 24, 14231 Nea Ionia, Athens GREECE
  
*******************************************************************/


#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <libraries/dos.h>
#include <stdio.h>
#include <intuition/intuition.h>
#include <graphics/text.h>
#include <exec/types.h>
#include <graphics/gfxbase.h>
#include <intuition/intuitionbase.h>


#define L_INTUITION 0x000001
#define L_GRAPHICS 0x000002
#define W_CONSOLE 0x000004

#define VERSION "1.1.10"
static const char CLI_VERS[]="$VER: SillyDiet v1.1.10 (28.10.95)";

struct itemvalues {
double quantity;
char name[26];
char measurement; /* 'C'=cup,'P'=pound, (a standard measure) */
char state; /* cooked or uncooked (if not always used in the same way)*/
int calories;
double protein; /* grams */
double carbohydrate; /* grams */
double fat; /* grams */
};

struct   GfxBase       *GfxBase;
struct   IntuitionBase *IntuitionBase;

FILE *console;
int used_resource = NULL;
extern void addtorecipe(),clearlist(),cutoff(),divide();
extern void loadrecipe(),saverecipe(),showdata();
extern short int saved;

/* Definitions for the main program */

#define MAXITEMS 202    /* total items in ingredient list (ingredients.c) */
struct itemvalues ingredient[31];  /* up to 30 ingredients per recipe */
char ingred[28]="     Ingredient            ";
char carbo[13]="Carbohydrate";
char pro[10]=" Protein ";
char fats[8]=" Fat   ";
char cal[9]="Calories";
char amount[9]="  Amount";
int number=0;  /* number of ingredients in recipe */
int adding;  /* set flag to add ingredients until "NONE" entered for name */
int valid; /* set flag on valid name (anything but "NONE" or "NULL") */
int totalcal;  /* total calories in recipe */
short int saved=1; /* set flag until new recipe is saved */
double totalcar,totalfat,totalpro; /* totals for recipe */
void addtorecipe(),clearlist(),cutoff(),divide(),showdata();
void saverecipe(),loadrecipe();
char getanswer();
short int matched; /* set flag when ingredient data is known */
extern FILE *console;
char *p,q,buffer[31];
short int waittable;

/* The ingredients of the program (ingredients.c) */

struct itemvalues ingredval[202] = {
{ 1.0,"ACORN_SQUASH",'C','_',86,3.0,22.0,.2 },
{ 1.0,"ALMONDS",'C','_',849,26.0,28.0,76.0 },
{ 1.0,"APPLES",'W','_',96,.3,24.0,1.0 },
{ 1.0,"APPLESAUCE",'C','_',100,.5,26.0,.5 },
{ 1.0,"APPLE_CIDER_VINEGAR",'T','_',2,0,1.0,0 },
{ 1.0,"APPLE_JUICE",'C','_',117,.2,30.0,0 },
{ 1.0,"APRICOT_WINE",'O','_',40,0,2.2,0 },
{ 1.0,"ARROWROOT",'T','_',29,0,7.0,0 },
{ 1.0,"ASPARAGUS",'P','_',90,9.7,14.9,.75 },
{ 1.0,"AVACADO",'W','_',369,4.7,13.0,37.0 },
{ 1.0,"BAMBOO_SHOOTS",'C','_',41,3.9,8.0,.5 },
{ 1.0,"BANANAS",'W','_',101,1.3,26,.2 },
{ 1.0,"BANANA_SQUASH",'C','_',103,3.7,24.0,.8 },
{ 1.0,"BARLEY",'C','C',199,4.7,45.0,.6 },
{ 1.0,"BARLEY",'C','U',698,16.0,160.0,2.0 },
{ 1.0,"BEAN_SPROUTS",'C','_',37,4.0,7.0,.2 },
{ 1.0,"BEER",'O','_',13,.1,1.1,0 },
{ 1.0,"BEETS",'C','_',54,1.9,12.0,.2 },
{ 1.0,"BELL_PEPPERS",'W','_',16,.9,.4,.1 },
{ 1.0,"BLACK-EYED_PEAS",'C','C',190,13.0,34,.8 },
{ 1.0,"BLACK-EYED_PEAS",'C','U',583,39.0,100.0,2.6 },
{ 1.0,"BLACKBERRIES",'C','_',84,1.7,.8,0 },
{ 1.0,"BLACKSTRAP_MOLASSES",'C','_',699,0,180.0,0 },
{ 1.0,"BLACK_BEANS",'C','C',337,22.0,61.0,1.4 },
{ 1.0,"BLACK_BEANS",'C','U',678,45.0,120.0,3.0 },
{ 1.0,"BLUEBERRIES",'C','_',90,1.0,22.0,.7 },
{ 1.0,"BOK_CHOY",'C','C',24,2.4,4.0,.3 },
{ 1.0,"BOK_CHOY",'C','U',11,1.1,2.0,.1 },
{ 1.0,"BRAZIL_NUTS",'P','_',1424,30.4,32.0,304.0 },
{ 1.0,"BREWERS_YEAST",'T','_',23,3.1,3.0,.1 },
{ 1.0,"BROCCOLI",'C','C',40,4.8,7.0,.5 },
{ 1.0,"BROCCOLI",'P','U',145,16.0,27.0,1.4 },
{ 1.0,"BROWN_RICE",'C','C',232,4.9,50.0,1.6 },
{ 1.0,"BROWN_RICE",'C','U',690,14.5,145.5,4.3 },
{ 1.0,"BROWN_SUGAR",'C','_',821,0,220.0,0 },
{ 1.0,"BUCKWHEAT_FLOUR",'C','_',340,6.3,78.0,1.2 },
{ 1.0,"BUCKWHEAT_GROATS",'C','C',218,7.6,47.0,1.6 },
{ 1.0,"BUCKWHEAT_GROATS",'C','U',546,19.0,120.0,3.9 },
{ 1.0,"BULGUR_WHEAT",'C','C',452,14.0,97.0,1.9 },
{ 1.0,"BULGUR_WHEAT",'C','U',602,19.0,130.0,2.6 },
{ 1.0,"BUTTER",'T','_',102,.1,0,11 },
{ 1.0,"BUTTERMILK",'C','_',88,8.8,12.0,.2 },
{ 1.0,"CABBAGE",'C','C',29,1.6,6.0,.3 },
{ 1.0,"CABBAGE",'C','U',17,.9,4.0,.1 },
{ 1.0,"CAROB",'C','_',252,6.4,112.0,2.0 },
{ 1.0,"CARROTS",'C','C',45,1.3,10.0,.3 },
{ 1.0,"CARROTS",'W','U',30,.8,7.0,.1 },
{ 1.0,"CASHEWS",'C','_',785,24.0,41.0,64.0 },
{ 1.0,"CAULIFLOWER",'C','C',28,2.9,5.0,.3 },
{ 1.0,"CAULIFLOWER",'C','U',27,2.7,5.0,.2 },
{ 1.0,"CELERY",'C','C',21,1.2,5.0,.2 },
{ 1.0,"CELERY_STALKS",'W','U',7,.4,2.0,0 },
{ 1.0,"CHARD",'C','_',32,3.2,1.2,0 },
{ 1.0,"CHEDDAR_CHEESE",'O','_',113,7.1,1.0,9.2 },
{ 1.0,"COCOA",'O','_',75,4.9,15.0,5.4 },
{ 1.0,"COCONUT",'C','_',344,2.2,33.0,24.0 },
{ 1.0,"CORN",'C','_',130,5.0,31.0,.8 },
{ 1.0,"CORNMEAL",'C','C',144,3.7,30.0,1.6 },
{ 1.0,"CORNMEAL",'C','U',442,11.0,91.0,4.1 },
{ 1.0,"CORNSTARCH",'T','_',29,0,7.0,0 },
{ 1.0,"CORN_SYRUP",'C','_',951,0,250.0,0 },
{ 1.0,"COTTAGE_CHEESE",'C','_',200,28.0,8.0,4.0 },
{ 1.0,"CRACKED_WHEAT",'C','C',217,6.8,48.0,1.4 },
{ 1.0,"CRACKED_WHEAT",'C','U',509,16.0,110.0,3.3 },
{ 1.0,"CRANBERRIES",'C','_',44,.4,10.0,.7 },
{ 1.0,"CREAM",'C','_',324,7.7,11.0,28.0 },
{ 1.0,"CREAM_CHEESE",'O','_',104,2.3,1.0,9.5 },
{ 1.0,"DARK_RYE_FLOUR",'C','_',419,21.0,87.0,3.3 },
{ 1.0,"DATES",'C','_',488,3.9,130,.9 },
{ 1.0,"DRIED_COCONUT",'C','_',344,2.2,33.0,24.0 },
{ 1.0,"DRY_ACTIVE_YEAST",'T','_',20,2.6,3.0,.1 },
{ 1.0,"EGGPLANT",'C','_',38,2.0,8.0,.4 },
{ 1.0,"EGGS",'W','_',94,7.4,0,6.6 },
{ 1.0,"EGG_WHITES",'W','_',17,3.6,0,0 },
{ 1.0,"EGG_YOLKS",'W','_',59,2.7,0,5.7 },
{ 1.0,"FILBERTS",'P','_',2784,54.4,64.0,291.2 },
{ 1.0,"GARBANZOS",'C','C',338,20.0,61.0,4.6 },
{ 1.0,"GARBANZOS",'C','U',720,41.0,120.0,9.6 },
{ 1.0,"GARBANZO_BEANS",'C','C',338,20.0,61.0,4.6 },
{ 1.0,"GARBANZO_BEANS",'C','U',720,41.0,120.0,9.6 },
{ 1.0,"GIN",'O','_',70,0,0,0 },
{ 1.0,"GREAT_NORTHERN_BEANS",'C','C',212,14.0,38.0,1.1 },
{ 1.0,"GREAT_NORTHERN_BEANS",'C','U',612,40.0,110.0,2.9 },
{ 1.0,"GREEN_BEANS",'C','C',31,2.0,7.0,0 },
{ 1.0,"GREEN_BEANS",'C','U',35,2.1,8.0,2 },
{ 1.0,"GREEN_CABBAGE",'C','C',29,1.6,6.0,.3 },
{ 1.0,"GREEN_CABBAGE",'C','U',17,.9,4.0,.1 },
{ 1.0,"GREEN_LIMA_BEANS",'C','_',189,13.0,34.0,.9 },
{ 1.0,"GREEN_PEAS",'C','C',109,8.2,19.0,.5 },
{ 1.0,"GREEN_PEAS",'C','U',122,9.1,21.0,.6 },
{ 1.0,"GREEN_PEPPERS",'W','_',16,.9,.4,.1 },
{ 1.0,"HONEY",'C','_',1031,1.0,280.0,0 },
{ 1.0,"HUBBARD_SQUASH",'C','_',103,3.7,24.0,.8 },
{ 1.0,"JACK_CHEESE",'O','_',105,6.3,0,8.2 },
{ 1.0,"KALE",'C','_',43,5.0,7.0,.8 },
{ 1.0,"KIDNEY_BEANS",'C','C',218,14.0,40.0,.9 },
{ 1.0,"KIDNEY_BEANS",'C','U',635,42.0,110.0,2.8 },
{ 1.0,"LARGE_WHITE_BEANS",'C','C',212,14.0,38.0,1.1 },
{ 1.0,"LARGE_WHITE_BEANS",'C','U',612,40.0,110.0,2.9 },
{ 1.0,"LEMON_JUICE",'C','_',61,1.2,20.0,.5 },
{ 1.0,"LENTILS",'C','C',212,16.0,39.0,0 },
{ 1.0,"LENTILS",'C','U',646,47.0,110.0,2.1 },
{ 1.0,"LIGHT_RYE_FLOUR",'C','_',319,8.3,69.0,.9 },
{ 1.0,"LIMA_BEANS",'C','C',262,16.0,49.0,1.1 },
{ 1.0,"LIMA_BEANS",'C','U',621,37.0,120.0,2.9 },
{ 1.0,"MACADAMIAS",'P','_',3255,41.8,59.7,328.5 },
{ 1.0,"MACADAMIA_NUTS",'P','_',3255,41.8,59.7,328.5 },
{ 1.0,"MAPLE_SYRUP",'C','_',794,0,200.0,0 },
{ 1.0,"MAYONAISE",'C','_',1580,2.4,5.0,180.0 },
{ 1.0,"MILK",'C','_',145,10.0,15.0,4.9 },
{ 1.0,"MILLET",'C','C',190,5.7,42.0,1.7 },
{ 1.0,"MILLET",'C','U',660,20.0,150.0,5.9 },
{ 1.0,"MISO",'T','_',29,1.8,4.0,.8 },
{ 1.0,"MOZZARELLA",'O','_',60,7.7,0,4.6 },
{ 1.0,"MUSHROOMS",'C','_',20,1.9,3.0,.2 },
{ 1.0,"NAVY_BEANS",'C','C',224,15.0,40.0,1.1 },
{ 1.0,"NAVY_BEANS",'C','U',697,46.0,130.0,3.3 },
{ 1.0,"OATMEAL",'C','C',132,4.8,23.0,2.4 },
{ 1.0,"OIL",'T','_',120,0,0,14.0 },
{ 1.0,"OKRA",'C','_',46,3.2,10.0,.5 },
{ 1.0,"OLIVES",'W','_',7,.1,.8 },
{ 1.0,"OLIVE_OIL",'T','_',119,0,0,14.0 },
{ 1.0,"ONIONS",'C','C',61,2.5,14.0,.2 },
{ 1.0,"ONIONS",'C','U',65,2.6,15.0,.2 },
{ 1.0,"ORANGES",'W','_',71,1.8,18.0,.1 },
{ 1.0,"ORANGE_JUICE",'C','_',112,1.7,26.0,.5 },
{ 1.0,"PARMESAN_CHEESE",'T','_',23,2.1,0,.1 },
{ 1.0,"PARSNIPS",'C','C',102,2.3,23,.8 },
{ 1.0,"PEANUTS",'C','_',838,38.0,30.0,72.0 },
{ 1.0,"PEANUT_BUTTER",'C','_',1499,72.0,44.0,130.0 },
{ 1.0,"PEAPODS",'C','C',64,4.4,14.0,.3 },
{ 1.0,"PEAPODS",'C','U',53,3.4,12.0,.2 },
{ 1.0,"PECANS",'C','_',742,9.9,16.0,77.0 },
{ 1.0,"PECAN_HALVES",'C','_',742,9.9,16.0,77.0 },
{ 1.0,"PIGNOLIAS",'P','_',2496,140.8,48.0,208.0 },
{ 1.0,"PINEAPPLE_JUICE",'C','_',138,1.0,34.0,.3 },
{ 1.0,"PINE_NUTS",'P','_',2880,59.2,96.0,99.2 },
{ 1.0,"PINTO_BEANS",'C','C',330,22.0,61.0,1.1 },
{ 1.0,"PINTO_BEANS",'C','U',663,44.0,120.0,2.3 },
{ 1.0,"PISTACHIOS",'P','_',2628,86.6,89.6,238.9 },
{ 1.0,"POTATOES",'C','U',114,3.2,26.0,.2 },
{ 1.0,"POTATOES",'W','C',173,4.8,39,.2 },
{ 1.0,"RAISINS",'C','_',419,3.6,110.0,3.0 },
{ 1.0,"RED_BEANS",'C','C',218,14.0,40.0,.9 },
{ 1.0,"RED_BEANS",'C','U',635,42.0,110.0,2.8 },
{ 1.0,"RED_CABBAGE",'C','_',22,1.4,5.0,.1 },
{ 1.0,"RICOTTA",'C','_',200,25.0,0,20.0 },
{ 1.0,"ROLLED_OATS",'C','C',132,4.8,23.0,2.4 },
{ 1.0,"ROLLED_OATS",'C','U',312,11.0,55.0,5.9 },
{ 1.0,"RUM",'O','_',70,0,0,0 },
{ 1.0,"SESAME_SEEDS",'C','_',816,27.0,31.0,71.0 },
{ 1.0,"SMALL_WHITE_BEANS",'C','C',224,15.0,40.0,1.1 },
{ 1.0,"SMALL_WHITE_BEANS",'C','U',697,46.0,130.0,3.3 },
{ 1.0,"SORGHUM",'C','_',848,0,220.0,0 },
{ 1.0,"SOUR_CREAM",'C','_',454,6.7,8.0,44.0 },
{ 1.0,"SOYBEANS",'C','C',234,20.0,19.0,10.0 },
{ 1.0,"SOYBEANS",'C','U',846,72.0,70.0,37.0 },
{ 1.0,"SOY_FLOUR",'C','_',295,26.0,21.0,14.0 },
{ 1.0,"SOY_GRITS",'C','C',303,26.0,25.0,13.0 },
{ 1.0,"SOY_GRITS",'C','U',617,52.0,51.0,27.0 },
{ 1.0,"SOY_SAUCE",'T','_',12,1.0,2.0,.2 },
{ 1.0,"SPINACH",'C','C',41,5.4,6.0,.5 },
{ 1.0,"SPINACH",'P','U',114,14.7,16.2,1.6 },
{ 1.0,"SPLIT_PEAS",'C','C',230,16.0,42.0,.6 },
{ 1.0,"SPLIT_PEAS",'C','U',696,48.0,120.0,2.0 },
{ 1.0,"STRAWBERRIES",'C','_',55,1.0,12.0,.7 },
{ 1.0,"SUMMER_SQUASH",'C','C',27,1.8,6.0,.4 },
{ 1.0,"SUMMER_SQUASH",'C','U',26,1.6,6.0,.3 },
{ 1.0,"SUNFLOWER_SEEDS",'C','_',812,35.0,29.0,69.0 },
{ 1.0,"SWEET_POTATO",'C','_',291,4.3,67.0,1.0 },
{ 1.0,"SWEET_WINE",'O','_',40,0,2.2,0 },
{ 1.0,"SWISS_CHARD",'C','_',32,3.2,1.2,0 },
{ 1.0,"SWISS_CHEESE",'O','_',105,7.8,0,7.7 },
{ 1.0,"TABLE_WINE",'O','_',25,0,1.1,0 },
{ 1.0,"TOFU",'P','_',108,35.0,11.0,18.6 },
{ 1.0,"TOMATOES",'C','C',63,3.1,13.0,.5 },
{ 1.0,"TOMATOES",'P','U',98,5.0,21.8,.73 },
{ 1.0,"TOMATO_PASTE",'C','_',184,7.6,44.0,.8 },
{ 1.0,"TORULA_YEAST",'T','_',22,3.1,3.0,.1 },
{ 1.0,"TURNIPS",'C','C',36,1.2,8.0,.3 },
{ 1.0,"TURNIPS",'C','U',39,1.3,9.0,.3 },
{ 1.0,"VINEGAR",'T','_',2,0,1.0,0 },
{ 1.0,"WALNUTS",'C','_',696,18.0,16.0,67.8 },
{ 1.0,"WALNUT_PIECES",'C','_',696,18.0,16.0,67.8 },
{ 1.0,"WATER_CHESTNUTS",'P','_',358,7.1,89.6,1.8 },
{ 1.0,"WHEAT_BRAN",'C','_',111,8.3,32.0,2.4 },
{ 1.0,"WHEAT_GERM",'C','_',298,22.0,38.0,8.9 },
{ 1.0,"WHISKEY",'O','_',70,0,0,0 },
{ 1.0,"WHITE_SUGAR",'C','_',770,0,200,0 },
{ 1.0,"WHOLE_WHEAT_FLOUR",'C','_',400,16.0,85.0,1.8 },
{ 1.0,"WHOLE_WHEAT_MACARONI",'P','U',1600,61.0,310.0,13.0 },
{ 1.0,"WHOLE_WHEAT_NOODLES",'P','U',1600,61.0,310.0,13.0 },
{ 1.0,"WHOLE_WHEAT_PASTRY_FLOUR",'C','_',496,14.0,110.0,3.0 },
{ 1.0,"WHOLE_WHEAT_SPAGHETTI",'P','U',1600,61.0,310.0,13.0 },
{ 1.0,"WILD_RICE",'C','C',141,5.6,30.0,.3 },
{ 1.0,"WILD_RICE",'C','U',565,23.0,120.0,1.1 },
{ 1.0,"WINE",'O','_',25,0,1.1,0 },
{ 1.0,"WINTER_SQUASH",'C','C',103,3.7,24.0,.8 },
{ 1.0,"YAMS",'C','_',210,4.8,48.0,.4 },
{ 1.0,"YOGURT",'C','_',123,8.3,13.0,3.7 },
{ 1.0,"ZUCCHINI",'C','C',22,1.8,4.0,.2 },
{ 1.0,"ZUCCHINI",'C','U',22,1.6,5.0,.1 }
};



/* Main control program begins here! */

void main()
{
   FILE *fopen();
   char req[31];

   GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
   if (GfxBase == NULL)
      cleanup("Can't open graphics.library");
   used_resource |= L_GRAPHICS;
 
   IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0);
   if (IntuitionBase == NULL)
      cleanup("Can't open intuition.library");
   used_resource |= L_INTUITION;

   console = fopen("con:0/0/640/200/SillyDiet v1.1. Copyright (©) 1992-95, The Xperts Group Inc. :-)","w+");
   if (console == NULL)
      cleanup("Can't open console window");
   used_resource |= W_CONSOLE;

   fprintf(console,"\n\n\n");
   fprintf(console,"\t\t         Available Commands\n");
   fprintf(console,"\t\t------------------------------------\n");
   fprintf(console,"\t\t 'A' to add ingredients to recipe\n");
   fprintf(console,"\t\t 'C' to cut ingredients from recipe\n");
   fprintf(console,"\t\t 'D' to divide recipe into portions\n");
   fprintf(console,"\t\t 'L' to load recipe from diskette\n");
   fprintf(console,"\t\t 'N' to enter new recipe into memory\n");
   fprintf(console,"\t\t 'Q' to quit program\n");
   fprintf(console,"\t\t 'R' to review recipe in memory\n");
   fprintf(console,"\t\t 'S' to save recipe to diskette\n");
   fprintf(console,"\t\t 'T' to show ingredient table\n");
   fprintf(console,"\t\t '?' to display this list\n");
   fprintf(console,"\t\t 'I' for information about this!\n");
   fprintf(console,"\n");
   fprintf(console,"\t\t\t");


   for (;;) {

      fprintf(console,"What is your request? ");
      rewind(console);
      fgets(req,30,console);
      rewind(console);
      switch (toupper(req[0])) {
         case 'A':
            fprintf(console,"\f");
            addtorecipe();
            break;
         case 'N':
            fprintf(console,"\f");
            newrecipe();
            break;
         case 'C':
            fprintf(console,"\f");
            cutoff();
            break;
         case 'D':
            fprintf(console,"\f");
            divide();
            break;
         case 'L':
            fprintf(console,"\f");
            loadrecipe();
            break;
         case 'Q':
            if (NOT saved)
               saveoption();
            cleanup("Dieting can be creative!");
            break;
         case 'R':
            showdata();
            break;
         case 'S':
            fprintf(console,"\f");
            saverecipe();
            break;
         case 'T':
            fprintf(console,"\f");
            showtable();
            break;
         case 'I':
	    fprintf(console,"\f");
	    aboutprg();
	 default:
	   fprintf(console,"\n\n\n");
 	   fprintf(console,"\t\t         Available Commands\n");
	   fprintf(console,"\t\t------------------------------------\n");
	   fprintf(console,"\t\t 'A' to add ingredients to recipe\n");
	   fprintf(console,"\t\t 'C' to cut ingredients from recipe\n");
	   fprintf(console,"\t\t 'D' to divide recipe into portions\n");
	   fprintf(console,"\t\t 'L' to load recipe from diskette\n");
	   fprintf(console,"\t\t 'N' to enter new recipe into memory\n");
	   fprintf(console,"\t\t 'Q' to quit program\n");
	   fprintf(console,"\t\t 'R' to review recipe in memory\n");
	   fprintf(console,"\t\t 'S' to save recipe to diskette\n");
	   fprintf(console,"\t\t 'T' to show ingredient table\n");
	   fprintf(console,"\t\t '?' to display this list\n");
	   fprintf(console,"\t\t 'I' for information about this!\n");
	   fprintf(console,"\n");
	   fprintf(console,"\t\t\t");
           break;
      }
   }
}

cleanup(s)
char *s;
{
   if (used_resource & W_CONSOLE)
      fclose(console);
   if (used_resource & L_GRAPHICS)
      CloseLibrary(GfxBase);
   if (used_resource & L_INTUITION)
      CloseLibrary(IntuitionBase);
   printf("%s\n",s);
   exit(0);
}


/* From now on begins the REAL program */

newrecipe()
{
   register short int i,n;

   if (NOT saved)
      saveoption();
   number=0;
   saved=1;
   clearlist();
   for (i=0;i<30;i++){   /* add up to 30 ingredients per recipe */
      valid=0;
      adding=1;
      matched=0;
      while ((NOT matched)||(NOT valid)){
         getname(i);    /* get name of ingredient */
         if (NOT adding) /* no more ingredients to add */
            break;
         for (n=0;n<MAXITEMS;n++){ /* check for item name in data list */
            checklist(n,i);
            if (matched){
               break;
            }
         }  /* for every item in data list checked until matched */
         if (NOT matched){
            manual(i); /* manual entry of ingredient data */
         }
      } /* while ingredient name being entered and validated */
      number=i+1;
      if (NOT adding){
         --number;
         break;
      }
   } /* last ingredient entered */

   if (number>0){
      showdata();
      saved=0; /* protect data from being overwritten until
                  save option is used or offered */
   }
}

showtable()
{
   register short int i,n,a,b;

   b=0;
   a=0;
   n=1;
   waittable=0;
   fprintf(console,"These are the ingredients I know values for: \n");
   for (i=0;i<MAXITEMS;i++){
      if (n==22){  /* show list in 22-line pages */
         waittable=1;
         waitreturn();
         if(toupper(q)=='Q')
            break;
         fprintf(console,"\f");
         n=0;
      }
      a++;
      if (a<MAXITEMS){   /* eliminate duplicates */
         if (strcmp(ingredval[i].name,ingredval[a].name)!=0){
            if (b<2)
               fprintf(console,"%-25s ",ingredval[i].name);
            else
               fprintf(console,"%-25s\n",ingredval[i].name);
            b++;
         }
      }
      else{
         if (b<2)
            fprintf(console,"%-25s ",ingredval[i].name);
         else
            fprintf(console,"%-25s\n",ingredval[i].name);
         b++;
      }
      if (b==3){
         n++;
         b=0;
      }
   }
   waittable=0;
   fprintf(console,"\n\n");
}

void clearlist()
{
   register short int i;
   for (i=0;i<31;i++)
   {    /* clear ingredient list before adding recipe */
      strcpy(ingredient[i].name,"NULL");
      ingredient[i].quantity= 0;
      ingredient[i].measurement='N';
      ingredient[i].calories= -1;
      ingredient[i].protein= -1.0;
      ingredient[i].carbohydrate= -1.0;
      ingredient[i].fat= -1.0;
   }
}

getname(i)
register short int i;
{
   register short int n;
   int length;

   while (NOT valid){
      fprintf(console,"What's the Name of Ingredient #");
      fprintf(console,"%d? ('NONE' to quit) ",i+1);
      rewind(console);
      fgets(buffer,25,console);
      n=strlen(buffer)-1;
      buffer[n]='\0';
      strcpy(ingredient[i].name,buffer);
      valid=strcmp(ingredient[i].name,"NULL");
      if (valid!=0)
         valid=1;
      if (strlen(ingredient[i].name)<1)
         valid=0;
      rewind(console);
   }
   length=strlen(ingredient[i].name);
   for (n=0;n<length;n++){
      if (ingredient[i].name[n]==' ')
         ingredient[i].name[n]='_';
      else
         ingredient[i].name[n]=toupper(ingredient[i].name[n]);
   }
   adding=strcmp(ingredient[i].name,"NONE");
   if (adding==0)
      strcpy(ingredient[i].name,"NULL");
   if(adding!=0)
      adding=1;
}

checklist(n,i)
register short int n,i; 
{
   int status;
   char answer;

   status=strcmp(ingredval[n].name,ingredient[i].name);
   if (status==0){
      fprintf(console,"Okay, I know the value of that ingredient");
      switch (ingredval[n].state){
         case 'U':
            fprintf(console,", raw.\n Is that what you're using?");
            fprintf(console," (y/n)");
            rewind(console);
            answer=getanswer();
            rewind(console);
            answer=toupper(answer);
            if (answer == 'Y'){
               matched=1;
               ingredient[i].measurement=ingredval[n].measurement;
               ingredient[i].calories=ingredval[n].calories;
               ingredient[i].protein=ingredval[n].protein;
               ingredient[i].carbohydrate=ingredval[n].carbohydrate;
               ingredient[i].fat=ingredval[n].fat;
            }
            break;
         case 'C':
            fprintf(console,", cooked.\n Is that what you're ");
            fprintf(console,"using? (y/n)");
            rewind(console);
            answer=getanswer();
            rewind(console);
            answer=toupper(answer);
            if (answer == 'Y'){
               matched=1;
               ingredient[i].measurement=ingredval[n].measurement;
               ingredient[i].calories=ingredval[n].calories;
               ingredient[i].protein=ingredval[n].protein;
               ingredient[i].carbohydrate=ingredval[n].carbohydrate;
               ingredient[i].fat=ingredval[n].fat;
            }
            break;
         default:
            fprintf(console,".\n");
            matched=1;
            ingredient[i].measurement=ingredval[n].measurement;
            ingredient[i].calories=ingredval[n].calories;
            ingredient[i].protein=ingredval[n].protein;
            ingredient[i].carbohydrate=ingredval[n].carbohydrate;
            ingredient[i].fat=ingredval[n].fat;
            break;
      }
      if (matched){
         ingredient[i].quantity=0;
         while(ingredient[i].quantity<=0){
            switch(ingredient[i].measurement){
               case 'C':
                  fprintf(console,"How many cups of ");
                  fprintf(console,"%s ",ingredient[i].name);
                  fprintf(console,"in this recipe?\n");
                  rewind(console);
                  fscanf(console,"%lf",&ingredient[i].quantity);
                  rewind(console);
                  showingred(i);
                  break;
               case 'P':
                  fprintf(console,"How many pounds of ");
                  fprintf(console,"%s ",ingredient[i].name);
                  fprintf(console,"in this recipe?\n");
                  rewind(console);
                  fscanf(console,"%lf",&ingredient[i].quantity);
                  rewind(console);
                  showingred(i);
                  break;
               case 't':
                  fprintf(console,"How many teaspoons of ");
                  fprintf(console,"%s ",ingredient[i].name);
                  fprintf(console,"in this recipe?\n");
                  rewind(console);
                  fscanf(console,"%lf",&ingredient[i].quantity);
                  rewind(console);
                  showingred(i);
                  break;
               case 'T':
                  fprintf(console,"How many tablespoons of ");
                  fprintf(console,"%s ",ingredient[i].name);
                  fprintf(console,"in this recipe?\n");
                  rewind(console);
                  fscanf(console,"%lf",&ingredient[i].quantity);
                  rewind(console);
                  showingred(i);
                  break;
               case 'O':
                  fprintf(console,"How many ounces of ");
                  fprintf(console,"%s ",ingredient[i].name);
                  fprintf(console,"in this recipe?\n");
                  rewind(console);
                  fscanf(console,"%lf",&ingredient[i].quantity);
                  rewind(console);
                  showingred(i);
                  break;
               default:
                  fprintf(console,"How many ");
                  fprintf(console,"%s ",ingredient[i].name);
                  fprintf(console,"in this recipe?\n");
                  rewind(console);
                  fscanf(console,"%lf",&ingredient[i].quantity);
                  rewind(console);
                  showingred(i);
                  break;
            }
         }
      }
   }
}

char getanswer()
{
   int n,yn;
   char answer[30];

   yn=0;
   while (NOT yn){
      rewind(console);
      fgets(answer,29,console);
      n=strlen(answer)-1;
      answer[n]='\0';
      rewind(console);
      if (strlen(answer)==0)
         fprintf(console,"Answer 'yes' or 'no', please.");
      else
         yn=1;
   }
   return(answer[0]);
}

manual(i)
register short int i;
{
   short int validmeasure;
   char answer,measure[12];

   fprintf(console,"That's not in my data list.\n");
   fprintf(console,"Do you want to enter the data manually? (y/n)");
   rewind(console);
   answer=getanswer();
   rewind(console);
   if (toupper(answer)=='Y'){  /* ask for quantity, measurement, calories,
                                     protein, carbohydrate, fat */
      matched=1;
      validmeasure=0;
      while(NOT validmeasure){
         fprintf(console,"What is the standard measure used?\n");
         fprintf(console,"('C'=cup,'t'=teaspoon,'T'=tablespoon,\n");
         fprintf(console," 'P'=pound,'O'=ounce,'W'=whole\n");
         rewind(console);
         fscanf(console,"%c",&ingredient[i].measurement);
         rewind(console);
         if (ingredient[i].measurement!='t');
            ingredient[i].measurement=toupper(ingredient[i].measurement);
         switch(ingredient[i].measurement){
            case 'C':
               strcpy(measure,"cup");
               validmeasure=1;
               break;
            case 't':
               strcpy(measure,"teaspoon");
               validmeasure=1;
               break;
            case 'T':
               strcpy(measure,"tablespoon");
               validmeasure=1;
               break;
            case 'P':
               strcpy(measure,"pound");
               validmeasure=1;
               break;
            case 'O':
               strcpy(measure,"ounce");
               validmeasure=1;
               break;
            case 'W':
               validmeasure=1;
               break;
            default:
               fprintf(console,"Not a standard measure!\n");
         }
      }
      while(ingredient[i].quantity<=0){
         switch(ingredient[i].measurement){
            case 'C':
               fprintf(console,"How many cups of ");
               fprintf(console,"%s ",ingredient[i].name);
               fprintf(console,"in this recipe?\n");
               rewind(console);
               fscanf(console,"%lf",&ingredient[i].quantity);
               rewind(console);
               break;
            case 'P':
               fprintf(console,"How many pounds of ");
               fprintf(console,"%s ",ingredient[i].name);
               fprintf(console,"in this recipe?\n");
               rewind(console);
               fscanf(console,"%lf",&ingredient[i].quantity);
               rewind(console);
               break;
            case 't':
               fprintf(console,"How many teaspoons of ");
               fprintf(console,"%s ",ingredient[i].name);
               fprintf(console,"in this recipe?\n");
               rewind(console);
               fscanf(console,"%lf",&ingredient[i].quantity);
               rewind(console);
               break;
            case 'T':
               fprintf(console,"How many tablespoons of ");
               fprintf(console,"%s ",ingredient[i].name);
               fprintf(console,"in this recipe?\n");
               rewind(console);
               fscanf(console,"%lf",&ingredient[i].quantity);
               rewind(console);
               break;
            case 'O':
               fprintf(console,"How many ounces of ");
               fprintf("%s ",ingredient[i].name);
               fprintf("in this recipe?\n");
               rewind(console);
               fscanf(console,"%lf",&ingredient[i].quantity);
               rewind(console);
               break;
         }
      }
      fprintf(console,"How many calories in one %s",measure);
      fprintf(console," of %s?\n",ingredient[i].name);
      rewind(console);
      fscanf(console,"%d",&ingredient[i].calories);
      rewind(console);
      fprintf(console,"How much protein in one %s",measure);
      fprintf(console," of %s?\n",ingredient[i].name);
      rewind(console);
      fscanf(console,"%lf",&ingredient[i].protein);
      rewind(console);
      fprintf(console,"How much carbohydrate in one %s",measure);
      fprintf(console," of %s?\n",ingredient[i].name);
      rewind(console);
      fscanf(console,"%lf",&ingredient[i].carbohydrate);
      rewind(console);
      fprintf(console,"How much fat in one %s",measure);
      fprintf(console," of %s?\n",ingredient[i].name);
      rewind(console);
      fscanf(console,"%lf",&ingredient[i].fat);
      rewind(console);
      showingred(i);
   }
   else {  /* re-enter name, if mistake */
      strcpy(ingredient[i].name,"NULL");
      valid=0;
   }
}

void showdata()
{
   register short int i;
   int calo;
   double fa,prot,carb;

   if (number==0){
      fprintf(console,"\f");
      fprintf(console,"There's no recipe to show!\n");
      return;
   }
   totalcal=0;
   totalpro=0;
   totalcar=0;
   totalfat=0;
   fprintf(console,"\f");
   fprintf(console,"%s%s%s%s%s%s\n\n",amount,ingred,cal,pro,carbo,fats);
   for (i=0;i<number;i++){
      if (ingredient[i].quantity!=1.0){
         calo=(int)(ingredient[i].quantity*ingredient[i].calories);
         prot=ingredient[i].quantity*ingredient[i].protein;
         carb=ingredient[i].quantity*ingredient[i].carbohydrate;
         fa=ingredient[i].quantity*ingredient[i].fat;
      }
      else{
         calo=ingredient[i].calories;
         prot=ingredient[i].protein;
         carb=ingredient[i].carbohydrate;
         fa=ingredient[i].fat;
      }
      totalcal=totalcal+calo;
      totalfat=totalfat+fa;
      totalpro=totalpro+prot;
      totalcar=totalcar+carb;
      if (i==18){
         fprintf(console,"\n");
         waitreturn();
         fprintf(console,"\f");
         fprintf(console,"%s%s%s",amount,ingred,cal);
         fprintf(console,"%s%s%s\n\n",pro,carbo,fats);
      }
      fprintf(console,"%2d %2.2lf ",i+1,ingredient[i].quantity);
      fprintf(console,"%c ",ingredient[i].measurement);
      fprintf(console,"%-25s",ingredient[i].name);
      fprintf(console,"%6d  ",calo);
      fprintf(console,"  %5.1lf",prot);
      fprintf(console,"     %5.1lf",carb);
      fprintf(console,"  %5.1lf\n",fa);
   }
   fprintf(console,"\n%s","Totals:                             ");
   fprintf(console,"%6d  ",totalcal);
   fprintf (console,"  %5.1lf",totalpro);
   fprintf(console,"     %5.1lf",totalcar);
   fprintf(console,"  %5.1lf\n",totalfat);
}

showingred(i)
short int i;
{
   int calo;
   double fa,prot,carb;

      if (ingredient[i].quantity!=1.0){
         calo=(int)(ingredient[i].quantity*ingredient[i].calories);
         prot=ingredient[i].quantity*ingredient[i].protein;
         carb=ingredient[i].quantity*ingredient[i].carbohydrate;
         fa=ingredient[i].quantity*ingredient[i].fat;
      }
      else{
         calo=ingredient[i].calories;
         prot=ingredient[i].protein;
         carb=ingredient[i].carbohydrate;
         fa=ingredient[i].fat;
      }
      fprintf(console,"\n%2d %2.2lf ",i+1,ingredient[i].quantity);
      fprintf(console,"%c ",ingredient[i].measurement);
      fprintf(console,"%-25s",ingredient[i].name);
      fprintf(console,"  %6d",calo);
      fprintf(console,"  %5.1lf",prot);
      fprintf(console,"     %5.1lf",carb);
      fprintf(console,"  %5.1lf\n\n",fa);
}

saveoption()
{
   char answer;

   fprintf(console,"Do you want to save the current recipe before ");
   fprintf(console,"proceeding? (y/n)");
   rewind(console);
   answer=getanswer();
   rewind(console);
   if(toupper(answer)=='Y')
      saverecipe();
   saved=1;
}

waitreturn()
{
   q='_';
   fprintf(console,"Type <RETURN> to Continue with Listing");
   if (waittable)
      fprintf(console,", 'Q' to quit");
   rewind(console);
   fscanf(console,"%c",&q);
   rewind(console);
}

void addtorecipe()
{
   register short int i,n,start;

   if (number==0){
      fprintf(console,"Use 'N' to enter a new recipe\n");
      return;
   }
   if (number==30){
      fprintf(console,"Aren't 30 ingredients enough for one recipe?\n");
      fprintf(console,"One has to draw the line somewhere. . .\n\n");
      return;
   }
   start=number; /* start adding from ingredient # */
   for (i=number;i<30;i++)
   {   /* add up to 30 ingredients per recipe */
      valid=0;
      adding=1;
      matched=0;
      while ((NOT matched)||(NOT valid)){
         getname(i);    /* get name of ingredient */
         if (NOT adding) /* no more ingredients to add */
            break;
         matched=0; /* name not valid until checked against data list or
                     ingredient data entered manually */
         for (n=0;n<MAXITEMS;n++){ /* check for item name in data list */
            checklist(n,i);
            if (matched){
               break;
            }
         }  /* for every item in data list checked until matched */
         if (NOT matched){
            manual(i); /* manual entry of ingredient data */
         }
      } /* while ingredient name being entered and validated */
      number=i+1;
      if (NOT adding){
         --number;
         break;
      }
   } /* last ingredient entered */

   if (number>start){
      showdata();
      saved=0; /* protect data from being overwritten until
                  save option is used or offered */
   }
}

void cutoff()
{
   register short inc,n;
   int calo,i;
   double carb,fa,prot;
   char answer;

   if (number==0){
      fprintf(console,"There's no ingredient to delete!\n");
      return;
   }
   i=0;
   fprintf(console,"Delete which ingredient (1-%d)?",number);
   rewind(console);
   fscanf(console,"%d",&i);
   rewind(console);
   if ((i>number)||(i<=0)){
      fprintf(console,"No such ingredient!\n");
      return;
   }
   --i;
   if (ingredient[i].quantity!=1.0){
      calo=(int)(ingredient[i].quantity*ingredient[i].calories);
      prot=ingredient[i].quantity*ingredient[i].protein;
      carb=ingredient[i].quantity*ingredient[i].carbohydrate;
      fa=ingredient[i].quantity*ingredient[i].fat;
   }
   else{
      calo=ingredient[i].calories;
      prot=ingredient[i].protein;
      carb=ingredient[i].carbohydrate;
      fa=ingredient[i].fat;
   }
   fprintf(console,"\n%2d %2.2lf ",i+1,ingredient[i].quantity);
   fprintf(console,"%c ",ingredient[i].measurement);
   fprintf(console,"%-28s",ingredient[i].name);
   fprintf(console," %6d   ",calo);
   fprintf(console,"%5.1lf     ",prot);
   fprintf(console,"%5.1lf  ",carb);
   fprintf(console,"%5.1lf\n\n",fa);
   fprintf(console,"Is this the ingredient you want to delete?");
   rewind(console);
   answer=getanswer();
   rewind(console);
   if (toupper(answer)=='Y'){
      --number;
      for (n=i;n<number;n++){
         inc=n+1;
         ingredient[n].quantity=ingredient[inc].quantity;
         strcpy(ingredient[n].name,ingredient[inc].name);
         ingredient[n].measurement=ingredient[inc].measurement;
         ingredient[n].calories=ingredient[inc].calories;
         ingredient[n].protein=ingredient[inc].protein;
         ingredient[n].carbohydrate=ingredient[inc].carbohydrate;
         ingredient[n].fat=ingredient[inc].fat;
      }
      fprintf(console,"Done.\n");
      saved=0;
   }
}

void divide()
{
   double div;
   register short int i;

   if (number==0){
      fprintf(console,"There's no recipe to divide!\n");
      return;
   }
   fprintf(console,"What factor should I divide this recipe by?");
   rewind(console);
   fscanf(console,"%lf",&div);
   rewind(console);
   for (i=0;i<number;i++)
      ingredient[i].quantity/=div;
   showdata();
}

void loadrecipe()
{
   char filename[31];
   FILE *fp;
   short int i;
   char id[255];
   char temp[2];

   if (NOT saved)
      saveoption();
   fprintf(console,"What's the name of the recipe to load?");
   rewind(console);
   fscanf(console,"%s",filename);
   rewind(console);
   fp=fopen(filename,"r");
   if (fp==NULL){
      fprintf(console,"\nCan't open file\n");
      return;
   }
   fscanf(fp,"%s",id);
   if (strcmp(id,"RECIPE")!=0){
      fprintf(console,"Not a recipe file!\n");
      fclose(fp);
      return;
   }
   fscanf(fp,"%d",&number);
   for (i=0;i<31;i++){
      fscanf(fp,"%lf%s",&ingredient[i].quantity,temp);
      ingredient[i].measurement=temp[0];
      fscanf(fp,"%s%d",ingredient[i].name,&ingredient[i].calories);
      fscanf(fp,"%lf",&ingredient[i].protein);
      fscanf(fp,"%lf%lf",&ingredient[i].carbohydrate,&ingredient[i].fat);
   }
   fclose(fp);
   showdata();
}

void saverecipe()
{
   char filename[31];
   FILE *fp;
   short int i,n;
   char temp[2];
   int named;

   if (number==0){
      fprintf(console,"There's no recipe to save!\n");
      return;
   }
   named = 0;
   while (NOT named){
      fprintf(console,"What's the name of this recipe?");
      rewind(console);
      fgets(filename,30,console);
      n=strlen(filename)-1;
      filename[n]='\0';
      rewind(console);
      if (strlen(filename)>0)
         named=1;
   }
   fp=fopen(filename,"w");
   if (fp==NULL){
      fprintf(console,"\nCan't open file\n");
      return;
   }
   fprintf(fp,"%s\t%d\n","RECIPE",number);
   for (i=0;i<31;i++){
      temp[0]=ingredient[i].measurement;
      temp[1]='\0';
      fprintf(fp,"%lf\t%s",ingredient[i].quantity,temp);
      fprintf(fp,"\t%s\t%d",ingredient[i].name,ingredient[i].calories);
      fprintf(fp,"\t%lf\t",ingredient[i].protein);
      fprintf(fp,"%lf\t%lf\n",ingredient[i].carbohydrate,ingredient[i].fat);
   }
   fclose(fp);
   saved=1;
}


aboutprg()
{
        fprintf(console,"SillyDiet v%s\n",VERSION);
	fprintf(console,"\nA Diet Planning program.");
	fprintf(console,"\nCopyright (©) 1992-95, The Xperts Group Inc.");
	fprintf(console,"\nAuthor: Manolis S Pappas.\n");
}

