/* ** Asks a Yes/No question. ** If the user says Yes (key # pressed) the function returns 1. ** IF the answer is No (key * pressed) the function return 0. */ int brl_yesno_question(char *prompt) { unsigned char buf[BUFFER_SIZE]; brl_message(prompt, 0); while (1) { brl_read(buf); if (!strncmp((char*)buf, "\003KT#", 4)) return 1; if (!strncmp((char*)buf, "\003KT*", 4)) return 0; } }
/* ** Get model identification ** ident must be a buffer of at least 20 bytes. */ void get_ident(char* ident) { char buf[256]; char *p; unsigned char ln = 0; brl_writeStr("SI"); brl_read((unsigned char*)buf); p = buf; while (1) { ln = *(p++); if (ln == 22 && !strncmp(p, "SI", 2)) { memcpy(ident, p + 2, 20); break; } else p += ln; } }
static int showbrfile(t_env* env) { unsigned char cursorpos = positions[env->status]; unsigned char ext = extensions[env->curext]; unsigned char* buf = malloc(256); unsigned char* str = malloc(256); /* the string to display */ buf[0] = '\005'; buf[1] = 'F'; buf[2] = 'N'; buf[3] = ext; buf[4] = (env->brfilenum & 0xff00) >> 2; buf[5] = env->brfilenum & 0x00ff; brl_write(buf+1, 5); while (1) { brl_read(buf); if (!strncmp((char*)buf, "\003KT", 3)) continue; else break; } if (!strncmp((char*)buf, "\003FE", 3)) { env->brfilenum--; return 0; } strcpy((char*)str, env->brpc ? "BR>PC " : "PC>BR "); strncat((char*)str, ((char*)&(buf[6])), buf[0] - 5); strcat((char*)str, "."); strncat((char*)str, (char*)&ext, 1); brl_message((char*)str, cursorpos); free(buf); free(str); return 0; }
int brtopc(t_env* env) { int fd; /* fd of a temporary file */ unsigned int lines = 0; /* number of packets transfered */ unsigned char destext = 0; /* conversion of the destination file */ unsigned char buf[BUFFER_SIZE]; /* received buffer */ unsigned char str[BUFFER_SIZE]; /* sent buffer */ unsigned char ext = extensions[env->curext]; /* current extension */ unsigned char filename[15]; /* The transfered file name */ unsigned char filename1[15]; /* The filename without extension */ while (1) { /* Asking name of current selected file */ strcpy((char*)buf, "\005FN"); buf[3] = ext; buf[4] = (env->brfilenum & 0xff00) >> 2; buf[5] = env->brfilenum & 0x00ff; brl_write(buf+1, 5); brl_read(buf); strcpy((char*)str, "Conv "); strncat((char*)str, ((char*)&(buf[6])), buf[0] - 5); filename[0] = 0; strncat((char*)filename, ((char*)&(buf[6])), buf[0] - 5); strcat((char*)str, "."); remove_blanks(filename); strcpy((char*)filename1, (char*)filename); strcat((char*)filename, "."); if (destext == 0) { strncat((char*)str, ((char*)&(extensions[env->curext])), 1); strncat((char*)filename, ((char*)&(extensions[env->curext])), 1); } else { strcat((char*)str, "TXT"); strcat((char*)filename, "TXT"); } brl_message((char*)str, 0); brl_read(buf); if (!strncmp((char*)buf, "\003KT*", 4)) return 1; if (!strncmp((char*)buf, "\003KT#", 4)) break; if (!strncmp((char*)buf, "\003KT8", 4)) destext = !destext; if (!strncmp((char*)buf, "\003KT2", 4)) destext = !destext; } strcpy((char*)buf, "\005FO"); buf[3] = ext; buf[4] = (env->brfilenum & 0xff00) >> 2; buf[5] = env->brfilenum & 0x00ff; brl_write(buf+1, 5); brl_read(buf); if (strncmp((char*)buf, "\003FE\x10", 4)) { brl_lasting_message("Erreur ouv br"); return 1; } if ((fd = open((char*)filename, O_WRONLY | O_CREAT | O_TRUNC, 0600)) == -1) { perror("open"); brl_message("! Err ecriture PC", 0); sleep(1); return 1; } while (1) { brl_writeStr(READ_LINE); brl_read(buf); if (!strncmp((char*)buf, "\003KT", 3)) { printf("touche appuyée\n"); if (!strncmp((char*)buf, "\003KT*", 4)) { printf("touche * appuyée\n"); brl_lasting_message("! interrompu "); return 1; } } if (!strncmp((char*)buf, "\003FE", 3)) { if (buf[3] == '\x13') break; else { printf("code inattendu\n"); brl_lasting_message("! transfert interrompu"); return 1; } } sprintf((char*)str, "... %s %d", filename, lines); brl_message((char*)str, 0); write(fd, &(buf[3]), buf[0] - 2); lines++; } brl_writeStr(CLOSE_FILE); brl_read(buf); if (strncmp((char*)buf, "\003FE\x10", 4)) { brl_lasting_message("! erreur"); return 1; } printf("fichier fermé\n"); close(fd); if (destext) { strcat((char*)filename1, ".TXT"); if (!k2txt(env, (char*)filename, (char*)filename1)) { brl_lasting_message("! Erreur conversion"); return 1; } } brl_lasting_message("! Fin transfert"); return 1; }
/* ** PC to BR meta-function */ int pctobr(t_env* env) { char end = 0; int i = 0; unsigned int count = 0; unsigned char conv = 0; unsigned int lines = 0; unsigned char buf[BUFFER_SIZE]; /* received buffer */ unsigned char str[BUFFER_SIZE]; /* sent buffer */ char* tmpfilename = "/tmp/eutp.tmp"; for (i = 0; i < BUFFER_SIZE; i++) { buf[i] = 0; str[i] = 0; } conv = normalize_filename(env); if ((env->fd = open(env->list[env->pcfilenum]->d_name, O_RDONLY)) == -1) { brl_message("!Erreur ouv pc", 0); sleep(1); return 0; } env->fs = fdopen(env->fd, "r"); /* Ouverture fichier en écriture sur le terminal braille */ str[0] = '\x0C'; strncpy((char*)&str[1], "Fo\x00", 3); str[4] = env->curext; strncpy((char*)&str[5], env->filename, 8); brl_write(str+1, 12); brl_read(buf); if (!strncmp((char*)buf, "\003FE\x21", 4)) { if (brl_yesno_question("! Remplacer ? #")) str[3] = 1; else return 1; brl_write(str+1, 12); brl_read(buf); } if (strncmp((char*)buf, "\002FW", 3)) { brl_lasting_message("! erreur ouv br"); brl_writeStr(CLOSE_FILE); return 1; } while (!end) { end = read_trame_from_file(env, str, &count); for (i = 0; i < 30; i++) printf(",%d,", str[i]); printf("\n"); if (count == 4242) /* fin fichier */ { printf("fin fichier\n"); end = 1; } brl_write(str+1, count); brl_read(buf); if (strncmp((char*)buf, (char*)"\002FW", 3)) { printf("erreur transfert\n"); for (i = 0; i < 30; i++) printf(",%d,", buf[i]); printf("\n"); brl_lasting_message("! Erreur transfert"); brl_writeStr(CLOSE_FILE); return 1; } lines++; sprintf((char*)str, "... %s.%c %d", env->filename, env->curext, lines); brl_message((char*)str, 0); } brl_writeStr(CLOSE_FILE); brl_read(buf); if (strncmp((char*)buf, "\003FE\x10", 4)) { printf("errer fermeture\n"); for (i = 0; i < 30; i++) printf(",%d,", buf[i]); printf("\n"); brl_lasting_message("! err fermeture"); fclose(env->fs); close(env->fd); return 1; } brl_message("! Fin transfert", 0); sleep(1); fclose(env->fs); close(env->fd); if (conv) unlink(tmpfilename); return 1; }
/* ** Show the list of files either of the braille terminal or of the PC ** It is the main loop of the program. */ int brl_listfiles(t_env* env) { unsigned char end = 0; unsigned char* buf = malloc(256); env->curext = 0; env->brpc = 1; env->brfilenum = 1; env->pcfilenum = 0; env->status = 1; while (!end) { if (env->brpc == 1) showbrfile(env); else showpcfiles(env); brl_read(buf); if (!strncmp("\003KT*", (char*)buf, 4)) end = 1; if (!strncmp("\003KT4", (char*)buf, 4) && env->status) env->status--; if (!strncmp("\003KT6", (char*)buf, 4) && ((env->status != 2 && env->brpc) || (!env->brpc && env->status != 1))) env->status++; if (!strncmp("\003KT8", (char*)buf, 4)) { if (env->status == 0) env->brpc = !env->brpc; if (env->status == 1 && env->brpc) env->brfilenum++; if (env->status == 1 && !env->brpc && env->pcfilenum < env->n - 1) env->pcfilenum++; if (env->status == 2 && env->curext < MAXENT - 1) env->curext++, env->brfilenum = 1; } if (!strncmp("\003KT2", (char*)buf, 4)) { if (env->status == 0) env->brpc = !env->brpc; if (env->status == 1 && env->brfilenum > 1 && env->brpc) env->brfilenum--; if (env->status == 1 && env->pcfilenum > 0) env->pcfilenum--; if (env->status == 2 && env->curext > 0) env->curext--, env->brfilenum = 1; } if (!strncmp("\003KT#", (char*)buf, 4)) { if (env->brpc) { if (!brtopc(env)) end = 1; } else { if (!pctobr(env)) end = 1; } } } free(buf); return 0; }