/*---------------------------------------------------------------------- * * jpe.c A program, based on 'jpd', to print envelopes * *---------------------------------------------------------------------- * $Id: jpe.c,v 1.12 2003/10/03 15:50:53 cmb Exp $ * * $Log: jpe.c,v $ * Revision 1.12 2003/10/03 15:50:53 cmb * Updated * * Revision 1.11 2002/12/28 22:58:39 cmb * Added code to replace an embedded carriage return with the proper * '\r'! * * Revision 1.10 2002/12/20 14:44:54 cmb * Added code to more properly clear the screen. * * Revision 1.9 2002/11/06 11:44:42 cmb * Updated. * * Revision 1.8 2002/09/03 13:30:07 cmb * Made sure there was only one command, regardless of the filtering * being employed, so allowing consistency between cases where no command * line arguments are specified and cases where they are... * * Revision 1.7 2002/09/03 12:54:04 cmb * Added extra sed commands to strip out spaces just before and just * after brackets. * * Revision 1.6 2002/08/28 14:46:52 cmb * Remove empty brackets to get rid of null lines. * * Revision 1.5 2002/08/27 17:16:25 cmb * Updated. * * Revision 1.4 2002/08/27 16:02:06 cmb * Updated. * * Revision 1.3 2002/03/05 21:21:10 cmb * Updated. * * Revision 1.2 2002/02/13 16:43:12 cmb * Updates to allow an address to be edited/customised before printing, * and interface changes to allow that to happen. * * Revision 1.1 2001/12/15 01:22:51 cmb * Initial revision * *----------------------------------------------------------------------*/ #include "cmb.h" #define MAX_RECORDS 5000 #define BUF_SIZE 1024 char *fname; char *address[MAX_RECORDS]; int num_records = 0; typedef enum Esize { ENV_DL, ENV_C5, /* Envelope sizes */ ENV_UNKNOWN } Esize; int Color = TRUE, /* Print on color printer */ AirMail = FALSE, /* Add air-mail thingy */ FirstClass = FALSE; /* Add first class */ Esize EnvelopeSize = ENV_DL; /* Default envelope size */ /*----------------------------------------------------------------------*/ void finish(void) { StopCurses(); remove(fname); exit(0); } /*---------------------------------------------------------------------- * read_records Routine to read in records from the relevant * information temporary file *----------------------------------------------------------------------*/ void read_records(void) { FILE *f; char linebuf[BUF_SIZE], *pos; f = fopen(fname, "r"); while (TRUE) { if (fgets(linebuf, BUF_SIZE, f) == NULL) break; address[num_records] = strdup(linebuf); pos = strchr(address[num_records], '\n'); if (pos != NULL) *pos = '\0'; num_records++; } fclose(f); } /*---------------------------------------------------------------------- * collect_data Routine to extract relevant information from * the jpilot database into a manageable format *----------------------------------------------------------------------*/ void collect_data(int argc, char **argv) { char command[BUF_SIZE], buf[BUF_SIZE]; int i; fname = strdup("/tmp/jpe-temp-XXXXXX"); fname = mktemp(fname); bzero(buf, BUF_SIZE); if (argc > 1) { for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-C5") == 0) { EnvelopeSize = ENV_C5; continue; } if (strcmp(argv[i], "-laser") == 0) { Color = FALSE; continue; } strcat(buf, "| grep -i '"); strcat(buf, argv[i]); strcat(buf, "' "); } } sprintf(command, "jpilot-dump +A'(%%z) (%%s) (%%T) (%%a) " "(%%m %%f %%l)' -A %s | sed -e 's/()//g' | sed -e 's/( /(/g'" " | sed -e 's/( /(/g' | sed -e 's/ )/)/g' > %s", buf, fname); system(command); } /*---------------------------------------------------------------------- * display_records Routine to display a few records *----------------------------------------------------------------------*/ void display_records(int start, int highlight) { int i, num, attribute; if ((num = (num_records - start)) > (LINES - 2)) { num = LINES - 2; } for (i = 0; i < num; i++) { if ((start + i) == highlight) { attribute = A_BOLD | COLOR_PAIR(BLUE_ON_WHITE); } else { attribute = A_NORMAL | COLOR_PAIR(BLACK_ON_WHITE); } attrset(attribute); mvaddstr(2 + i, 0, address[start + i]); } refresh(); } /*----------------------------------------------------------------------*/ void titleline(void) { int x, y; clear(); for (y = 0; y < LINES; y++) { for (x = 0; x < COLS; x++) { mvaddch(y, x, ' '); } } attrset(A_BOLD | COLOR_PAIR(RED_ON_WHITE)); mvaddstr(0, 12, "Jpilot Envelope Printer"); mvaddstr(1, 0, "--------------------------------------------------------" "-----------------------"); } /*---------------------------------------------------------------------- * select_data *----------------------------------------------------------------------*/ int select_data(void) { int base = 0, current = 0, oldbase, ch; titleline(); while (TRUE) { oldbase = base; display_records(base, current); ch = getch(); switch (ch) { case KEY_ENTER: case '\r': return(current); break; case 'q': case 'Q': case '': finish(); break; case KEY_UP: if (current > 0) current--; if (current < base) base -= (LINES - 2); if (base < 0) base = 0; break; case KEY_DOWN: if (current < (num_records - 1)) current++; if (current == (base + LINES - 2)) base = current; break; case KEY_RIGHT: base += (LINES - 2); if (base > num_records) { base = oldbase; current = num_records - 1; } else { current = base; } break; case KEY_LEFT: base -= (LINES - 2); if (base < 0) { base = 0; current = 0; } else { current = base + (LINES - 3); } break; default: break; } if (base != oldbase) { titleline(); } } } /*---------------------------------------------------------------------- * select_envelope Routine to select printer and size for an * envelope *----------------------------------------------------------------------*/ void select_envelope(int i) { FILE *f; int ch; char command[BUF_SIZE]; titleline(); mvaddstr(3,0, address[i]); attrset(A_BOLD | COLOR_PAIR(RED_ON_WHITE)); mvaddstr(5, 3, "P"); mvaddstr(6, 3, "E"); mvaddstr(7, 3, "F"); mvaddstr(8, 3, "A"); mvaddstr(11, 3, "^E"); mvaddstr(12, 0, "ENTER"); mvaddstr(13, 3, "^Q"); attrset(A_BOLD | COLOR_PAIR(BLUE_ON_WHITE)); mvaddstr(5, 4, "rinter :"); mvaddstr(6, 4, "nvelope size :"); mvaddstr(7, 4, "irst Class :"); mvaddstr(8, 4, "irMail :"); attrset(A_NORMAL | COLOR_PAIR(BLACK_ON_WHITE)); mvaddstr(10, 0, "Press the first letter to toggle a setting... "); mvaddstr(11, 6, "to edit the address before printing"); mvaddstr(12, 6, "when happy"); mvaddstr(13, 6, "to quit"); while (TRUE) { attrset(A_BOLD | COLOR_PAIR(RED_ON_WHITE)); mvaddstr(5, 19, Color ? "Color" : "Laser"); mvaddstr(6, 19, (EnvelopeSize == ENV_DL) ? "DL" : "C5"); mvaddstr(7, 19, FirstClass ? "First class " : "Second Class"); mvaddstr(8, 19, AirMail ? "Air mail" : "Surface "); refresh(); ch = getch(); switch (ch) { case 'q': case 'Q': case '': finish(); break; case KEY_ENTER: case '\r': return; break; case 'P': case 'p': Color = ! Color; break; case 'A': case 'a': AirMail = ! AirMail; break; case 'F': case 'f': FirstClass = ! FirstClass; break; case 'E': case 'e': if (EnvelopeSize == ENV_DL) EnvelopeSize = ENV_C5; else EnvelopeSize = ENV_DL; break; /*----------------------------------------------- * Edit the content of the address before printing *-----------------------------------------------*/ case '': f = fopen(fname, "w"); fprintf(f, address[i]); fclose(f); sprintf(command, "emacsclient %s", fname); system(command); f = fopen(fname, "r"); fgets(address[i], BUF_SIZE, f); mvaddstr(3,0, " " " "); mvaddstr(3,0, address[i]); break; default: break; } } refresh(); sleep(5); } /*---------------------------------------------------------------------- * print_envelope Little routine to print an envelope *----------------------------------------------------------------------*/ void print_envelope(int i) { char buf[BUF_SIZE]; FILE *f; remove(fname); f = fopen(fname, "w"); copy_file(f, "/home/cmb/code/ParishBase/PostScript_headers/pb_Envelope.ps", NULL); fprintf(f, "\n%s\n%d %d %senvelope\n\n", address[i], FirstClass ? 1 : 0, AirMail ? 1 : 0, (EnvelopeSize == ENV_DL) ? "DL" : "C5"); fclose(f); attrset(A_BOLD | COLOR_PAIR(BLUE_ON_WHITE)); mvaddstr(15, 3, "Printing...."); refresh(); sprintf(buf, "p -ps %s %s", Color ? "-color " : "", fname); system(buf); } /*----------------------------------------------------------------------*/ int main(int argc, char **argv) { int i; collect_data(argc, argv); read_records(); StartCurses(); i = select_data(); select_envelope(i); print_envelope(i); finish(); exit(0); }