/*---------------------------------------------------------------------- * * jpd.c A quick program to extract phone numbers from jpilot's * database files, and dial a selected one. * *---------------------------------------------------------------------- * $Id: jpd.c,v 1.7 2002/12/28 22:59:19 cmb Exp $ * * $Log: jpd.c,v $ * Revision 1.7 2002/12/28 22:59:19 cmb * Added code to replace an embedded carriage return with the proper * '\r'! * * Revision 1.6 2002/12/20 14:44:07 cmb * Added code to properly clear the screen!! * * Revision 1.5 2002/12/17 22:36:45 cmb * Removed extra CR * * Revision 1.4 2002/09/03 13:24:08 cmb * Added the address to the dialer output, so it is easier to select * which of the possibly multiple matches should be dialed... Since the * address fields are more likely to have newlines embedded in them * (which makes parsing the input tricky), cases where this comes up are * just skipped over... * * Revision 1.3 2001/12/15 00:16:50 cmb * Removed tracing options. * * Revision 1.2 2001/12/15 00:16:07 cmb * Fixed an end-case bug when moving to last record. * * Revision 1.1 2001/12/15 00:09:29 cmb * Initial revision * *----------------------------------------------------------------------*/ #include "cmb.h" #define MAX_RECORDS 5000 #define BUF_SIZE 1024 char *fname; char *names[MAX_RECORDS]; char *numbers[MAX_RECORDS]; int num_records = 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; int name_len; f = fopen(fname, "r"); while (TRUE) { if (fgets(linebuf, BUF_SIZE, f) == NULL) break; pos = strstr(linebuf, " -::- "); if (pos == NULL) continue; /* Maybe a newline in an address... */ name_len = pos - linebuf; pos += 6; if (strpbrk(pos, "0123456789") == NULL) continue; names[num_records] = (char *) malloc(name_len + 1); numbers[num_records] = (char *) malloc(strlen(pos) + 1); strncpy(names[num_records], linebuf, name_len); strcpy(numbers[num_records], pos); pos = strchr(numbers[num_records], '\n'); if (pos != NULL) *pos = '\0'; num_records++; } } /*---------------------------------------------------------------------- * 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/jpdXXXXXX"); fname = mktemp(fname); if (argc > 1) { bzero(buf, BUF_SIZE); for (i = 1; i < argc; i++) { strcat(buf, "| grep -i '"); strcat(buf, argv[i]); strcat(buf, "' "); } sprintf(command, "jpilot-dump +A'%%l, %%m %%f, %%a %%T -::- %%p1' -A " "%s | sort > %s", buf, fname); } else { sprintf(command, "jpilot-dump +A'%%l, %%m %%f, %%a %%T -::- %%p1' -A " "| sort > %s", 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; } else { attribute = A_NORMAL; } attrset(attribute | COLOR_PAIR(BLACK_ON_WHITE)); mvaddstr(2 + i, 0, names[start + i]); mvaddch(2 + i, 64, ' '); attrset(attribute | COLOR_PAIR(BLUE_ON_WHITE)); mvaddstr(2 + i, 65, numbers[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 dialer"); 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 '': return(-1); 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(); } } } /*---------------------------------------------------------------------- * dial Little routine to dial a number *----------------------------------------------------------------------*/ void dial(int i) { char buf[BUF_SIZE]; int l, j, k = 7; bzero(buf, BUF_SIZE); sprintf(buf, "dialer "); l = strlen(numbers[i]); for (j = 0; j < l; j++) { if (isdigit(numbers[i][j])) { buf[k++] = numbers[i][j]; } } titleline(); attrset(A_BOLD | COLOR_PAIR(BLUE_ON_WHITE)); mvaddstr(5, 5, "Dialling "); addstr(numbers[i]); addstr(" ...."); refresh(); system(buf); } /*----------------------------------------------------------------------*/ int main(int argc, char **argv) { int i; collect_data(argc, argv); read_records(); StartCurses(); i = select_data(); if (i >= 0) dial(i); StopCurses(); remove(fname); exit(0); }