/* ***** WtPPN.C -- User Id manager for WizTerm ***** */ #include #include #include #include #include #include #include #define PPNFUNC 1 #include "wt.h" #include "wtbuf.h" #include "wtppn.h" #include "wtwin.h" /* ===== VARIABLES ===== */ int ppns_visible = 0; /* PUBLIC! */ extern int level_colors[]; extern int watching; extern int winsplit; static char *ppn_file = PPNFILENAME; /* PPN file, disguised as "notes" */ static int h_ppn = 0; /* PPN file handle */ static long uidr1, uidr9; /* 1st, last rec for UID */ static long namr1, namr9; /* 1st, last rec for name */ static long r1, r9, r5; /* current record number */ static t_ppnrec ppn_buf; /* single buffer for ppn rec */ static char uidnamkey[12]; /* search key */ static int uidnamlen; /* search key length */ static char *uidnamfld; /* search key field in ppn_buf */ static char *months[] = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" }; /* ===== INTERNAL FUNCTIONS ===== */ void ppn_format(void); /*****************************************************/ /* read_ppnrec(): Read PPN record with given rec no. */ /*****************************************************/ void read_ppnrec(long recno) { long lptr; int rcnt; long byteofs; byteofs = recno * (long) l_ppnrec; lptr = lseek(h_ppn, byteofs, SEEK_SET); assert(lptr == byteofs); rcnt = read(h_ppn, &ppn_buf, l_ppnrec); assert(rcnt == l_ppnrec); ppn_buf.crlf[0] = '\0'; } /* end read_ppnrec() */ /******************************************************/ /* any_first(): Find first uid or name with given key */ /* return ptr to static ppnrec if found, else NULL. */ /******************************************************/ int any_first(void) { while (r1 <= r9) { r5 = (r1 + r9) / 2; read_ppnrec(r5); if (strncmp(uidnamfld, uidnamkey, uidnamlen) >= 0) { r9 = r5 - 1; } else { r1 = r5 + 1; } } /* now r1 should point at first match */ read_ppnrec(r1); if (strncmp(uidnamfld, uidnamkey, uidnamlen)) { r5 = -1L; return 0; } else { r5 = r1; return 1; } } /* end any_first() */ /******************************************************/ /* any_next(): Return the next matching uid or name. */ /* return 1 if match found, else 0. */ /******************************************************/ int any_next(void) { read_ppnrec(++r5); if (strncmp(uidnamfld, uidnamkey, uidnamlen)) { r5 = -1L; return 0; } else { return 1; } } /* end any_next() */ /*********************************************/ /* nam_first(): Find first matching name. */ /* returns record buf if found, NULL if not. */ /*********************************************/ t_ppnrec *nam_first(char *snam) { if (strlen(snam) < 2) { return NULL; } r1 = namr1; r9 = namr9; strcpy(uidnamkey, snam); (void) strlwr(uidnamkey); uidnamkey[0] = (char) toupper(uidnamkey[0]); uidnamlen = strlen(snam); uidnamfld = ppn_buf.user.name; if (any_first()) { return &ppn_buf; } else { return NULL; } } /* end nam_first() */ /************************************************/ /* ppn_fmt_date(): Format date from PPN record. */ /************************************************/ char *ppn_fmt_date(char *date) { static char tmp_date[8]; int tmp_month; tmp_date[0] = date[2]; tmp_date[1] = date[3]; tmp_date[2] = '\0'; tmp_month = atoi(tmp_date); tmp_date[0] = date[4]; tmp_date[1] = date[5]; if (tmp_month>=1 && tmp_month<=12) { strcpy(tmp_date+2, months[tmp_month-1]); } tmp_date[5] = date[0]; tmp_date[6] = date[1]; tmp_date[7] = '\0'; return tmp_date; } /* end ppn_fmt_date() */ /*************************************************/ /* ppn_format(): nice output format for ppn info */ /*************************************************/ void ppn_format() { char tmp_fmt[40]; int level; win_att(MK_ATT(PPN_FG,PPN_BG)); sprintf(tmp_fmt, "%-11.11s ", ppn_buf.user.uid); win_puts(tmp_fmt); level = ppn_buf.user.mxlv - '0'; if (ppn_buf.user.mxlv == 'A') level = 10; if (level <= 10) { win_att(MK_ATT(PPN_FG,level_colors[1+level])); } sprintf(tmp_fmt, "%-9.9s", ppn_buf.user.name); win_puts(tmp_fmt); win_att(MK_ATT(PPN_FG,PPN_BG)); sprintf(tmp_fmt, " (%c) ", ppn_buf.user.mxlv); win_puts(tmp_fmt); win_puts(ppn_fmt_date(ppn_buf.date1)); win_puts("-"); win_puts(ppn_fmt_date(ppn_buf.date2)); win_puts("\r\n"); } /* end ppn_format() */ /*******************************************/ /* uid_first(): Find first matching UID. */ /* returns record num if found, -1 if not. */ /*******************************************/ t_ppnrec *uid_first(char *suid) { r1 = uidr1; r9 = uidr9; strcpy(uidnamkey, suid); uidnamlen = strlen(suid); uidnamfld = ppn_buf.user.uid; if (any_first()) { return &ppn_buf; } else { return NULL; } } /* end uid_first() */ /* ===== EXTERNAL FUNCTIONS ===== */ /*******************************/ /* ppn_exi(): Exit PPN manager */ /*******************************/ void ppn_exi() { if (h_ppn) { close(h_ppn); } } /* end ppn_exi() */ /*************************************/ /* ppn_ini(): Initialize PPN manager */ /*************************************/ void ppn_init(void) { h_ppn = open(ppn_file, O_BINARY|O_RDONLY); if (h_ppn == -1) { /* can't open file -- error message, maybe? */ h_ppn = 0; return; } uidr1 = 0L; namr9 = lseek(h_ppn, 0, SEEK_END) / l_ppnrec - 1; namr1 = (namr9 + 1L) / 2; uidr9 = namr1 - 1; } /* end ppn_init() */ /*********************************************/ /* ppn_lookup(): Find and display a user ID. */ /*********************************************/ void ppn_lookup(char *look_uid) { struct { char uid_fill[6]; char uid_data[12]; } uid_key = { { ' ', ' ', ' ', ' ', ' ', ' ' }, { '\0' } }; char *key_ptr; int comma_dist; if (h_ppn == 0) return; if (look_uid[0] == '\0' && ppns_visible) { win_select(WIN_PPNS); win_hide(); ppns_visible = 0; if (winsplit & !watching) { set_winsplit(0); } return; } while (*look_uid == ' ') { look_uid++; } if (!*look_uid || strlen(look_uid) > sizeof(ppn_buf.user.uid)) { return; } strcpy(uid_key.uid_data, look_uid); key_ptr = uid_key.uid_data; if (isdigit(look_uid[0]) && strchr(look_uid, ',')) { comma_dist = (word) (strchr(look_uid, ',') - look_uid); if (comma_dist < 3 || comma_dist > 6) { return; } key_ptr -= (6 - comma_dist); if (! uid_first(key_ptr)) { return; } } else { if (! nam_first(key_ptr)) { return; } } if (! ppns_visible) { set_winsplit(1); } win_select(WIN_PPNS); win_clear(); win_show(); for (;;) { ppn_format(); if (!any_next()) { break; } } win_puts(" "); win_att(MK_ATT(RED,PPN_BG)); win_puts("* Hit ESC to close this window! *"); win_att(MK_ATT(PPN_FG,PPN_BG)); ppns_visible = 1; } /* end ppn_lookup() */ /****************************************/ /* ppn_position(): Scroll in PPN buffer */ /****************************************/ #ifdef __TURBOC__ #pragma argsused #endif void ppn_position(int where) { #ifndef __TURBOC__ if (where) ; #endif } /* end ppn_position() */ /* EOF(WtPpn.C) */