/*************************************************************************** * Copyright (C) 2009 by NPP SEMIKO * * mail@semico.ru * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifdef HAVE_CONFIG_H #include #endif #include #include #include // Коды выхода BSD from @(#)sysexits.h 8.1 (Berkeley) 6/2/93 // Copyright (c) 1987, 1993 // The Regents of the University of California. All rights reserved. #define EX_USAGE 64 /* command line usage error */ #define EX_DATAERR 65 /* data format error */ #define EX_NOINPUT 66 /* cannot open input */ #define EX_NOUSER 67 /* addressee unknown */ #define EX_NOHOST 68 /* host name unknown */ #define EX_UNAVAILABLE 69 /* service unavailable */ #define EX_SOFTWARE 70 /* internal software error */ #define EX_OSERR 71 /* system error (e.g., can't fork) */ #define EX_OSFILE 72 /* critical OS file missing */ #define EX_CANTCREAT 73 /* can't create (user) output file */ #define EX_IOERR 74 /* input/output error */ #define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */ #define EX_PROTOCOL 76 /* remote error in protocol */ #define EX_NOPERM 77 /* permission denied */ #define EX_CONFIG 78 /* configuration error */ // end sysexits.h char mkversion[]="Version 0.1"; // 0.1 - первая версия int dos=0; // 0-GNU/Linux 1-DOS 2-Windows (Win32) char mes0[8]; // перевод строки char mes[24][256]; // сообщения FILE *fp0; int mkiocharset; // 0=CP866|1=CP1251|2=KOI8R|3=UTF8|4=UCS2 кодировка ввода-вывода char ctemp[512]; /* вспомогательная строка */ long mkcharset[68][5]= //а=0 я=32 А=33 Я=65 0-866 1-1251 2-koi8 3-utf8 4-ucs2 {0xA0,0xE0,0xC1,0xD0B0,0x0430, // а 0xA1,0xE1,0xC2,0xD0B1,0x0431, // б 0xA2,0xE2,0xD7,0xD0B2,0x0432, 0xA3,0xE3,0xC7,0xD0B3,0x0433, 0xA4,0xE4,0xC4,0xD0B4,0x0434, 0xA5,0xE5,0xC5,0xD0B5,0x0435, // е 0xF1,0xB8,0xA3,0xD191,0x0451, // ё 0xA6,0xE6,0xD6,0xD0B6,0x0436, // ж 0xA7,0xE7,0xDA,0xD0B7,0x0437, 0xA8,0xE8,0xC9,0xD0B8,0x0438, 0xA9,0xE9,0xCA,0xD0B9,0x0439, 0xAA,0xEA,0xCB,0xD0BA,0x043A, 0xAB,0xEB,0xCC,0xD0BB,0x043B, 0xAC,0xEC,0xCD,0xD0BC,0x043C, 0xAD,0xED,0xCE,0xD0BD,0x043D, 0xAE,0xEE,0xCF,0xD0BE,0x043E, 0xAF,0xEF,0xD0,0xD0BF,0x043F, // п 0xE0,0xF0,0xD2,0xD180,0x0440, // р 0xE1,0xF1,0xD3,0xD181,0x0441, 0xE2,0xF2,0xD4,0xD182,0x0442, 0xE3,0xF3,0xD5,0xD183,0x0443, 0xE4,0xF4,0xC6,0xD184,0x0444, 0xE5,0xF5,0xC8,0xD185,0x0445, 0xE6,0xF6,0xC3,0xD186,0x0446, 0xE7,0xF7,0xDE,0xD187,0x0447, 0xE8,0xF8,0xDB,0xD188,0x0448, 0xE9,0xF9,0xDD,0xD189,0x0449, 0xEA,0xFA,0xDF,0xD18A,0x044A, 0xEB,0xFB,0xD9,0xD18B,0x044B, 0xEC,0xFC,0xD8,0xD18C,0x044C, 0xED,0xFD,0xDC,0xD18D,0x044D, 0xEE,0xFE,0xC0,0xD18E,0x044E, 0xEF,0xFF,0xD1,0xD18F,0x044F, // я 0x80,0xC0,0xE1,0xD090,0x0410, // А 0x81,0xC1,0xE2,0xD091,0x0411, 0x82,0xC2,0xF7,0xD092,0x0412, 0x83,0xC3,0xE7,0xD093,0x0413, 0x84,0xC4,0xE4,0xD094,0x0414, 0x85,0xC5,0xE5,0xD095,0x0415, 0xF0,0xA8,0xB3,0xD081,0x0401, // Ё 0x86,0xC6,0xF6,0xD096,0x0416, 0x87,0xC7,0xFA,0xD097,0x0417, 0x88,0xC8,0xE9,0xD098,0x0418, 0x89,0xC9,0xEA,0xD099,0x0419, 0x8A,0xCA,0xEB,0xD09A,0x041A, 0x8B,0xCB,0xEC,0xD09B,0x041B, 0x8C,0xCC,0xED,0xD09C,0x041C, 0x8D,0xCD,0xEE,0xD09D,0x041D, 0x8E,0xCE,0xEF,0xD09E,0x041E, 0x8F,0xCF,0xF0,0xD09F,0x041F, // П 0x90,0xD0,0xF2,0xD0A0,0x0420, // Р 0x91,0xD1,0xF3,0xD0A1,0x0421, 0x92,0xD2,0xF4,0xD0A2,0x0422, 0x93,0xD3,0xF5,0xD0A3,0x0423, 0x94,0xD4,0xE6,0xD0A4,0x0424, //Ф 0x95,0xD5,0xE8,0xD0A5,0x0425, 0x96,0xD6,0xE3,0xD0A6,0x0426, 0x97,0xD7,0xFE,0xD0A7,0x0427, 0x98,0xD8,0xFB,0xD0A8,0x0428, 0x99,0xD9,0xFD,0xD0A9,0x0429, 0x9A,0xDA,0xFF,0xD0AA,0x042A, 0x9B,0xDB,0xF9,0xD0AB,0x042B, 0x9C,0xDC,0xF8,0xD0AC,0x042C, 0x9D,0xDD,0xFC,0xD0AD,0x042D, 0x9E,0xDE,0xE0,0xD0AE,0x042E, 0x9F,0xDF,0xF1,0xD0AF,0x042F, // Я 0,0,0,0,0}; /*---------------------------------*/ int init_msg(void); void mkstrcpy (char* ,char* ,int ,int ); /*---------------------------------*/ int init_msg(void) { // инициализация списка сообщений 0-по умолчанию int re=0; if ((dos==1)||(dos==2)) strcpy(mes0,"\r\n"); // DOS & Win - ВК + ПС else strcpy(mes0,"\n"); // Linux - только ПС strcpy(mes[0],"CP866"); strcpy(mes[1],"CP1251"); strcpy(mes[2],"KOI8R"); strcpy(mes[3],"UTF8"); strcpy(mes[4],"USC2"); mkstrcpy(mes[10],"Последовательный порт для связи с ЭКВМ",mkiocharset,0); mkstrcpy(mes[11],"и т.д.",mkiocharset,0); mkstrcpy(mes[12],"по умолчанию",mkiocharset,0); mkstrcpy(mes[13],"Скорость обмена с ЭКВМ",mkiocharset,0); mkstrcpy(mes[14],"Сетевой номер ЭКВМ (R9049)",mkiocharset,0); mkstrcpy(mes[15],"Кодировка",mkiocharset,0); mkstrcpy(mes[16],"Сохранить значения? (Y/N)",mkiocharset,0); mkstrcpy(mes[17],"Повторить ввод? (Y/N)",mkiocharset,0); mkstrcpy(mes[18],"Ошибка, файл mk.cfg не открыт.",mkiocharset,0); end: return(re); } /*------------------------------------------------------ Перекодировка --------------*/ void mkstrcpy (char* str2, char* str1, int chset2, int chset1) { // перенос из str1 в str2 в кодировках chset1 chset2 соответственно //chset= 0=CP866|1=CP1251|2=KOI8R|3=UTF-8|4=UCS-2 int i,j,k,m; long l,l1,l2; if (chset1==chset2) {strcpy(str2,str1); goto end;} k=strlen(str1); if ((k==0) ||(chset1<0)||(chset1>4)||(chset2<0)||(chset2>4)) {str2[0]=0; goto end; } i=0; j=0; if (chset1==4) i=2; if (chset2==4) {str2[0]=0xFF; str2[1]=0xFE; j=2; } ci: if ((chset1==0)||(chset1==1)||(chset1==2)) {l=str1[i]; i++; if (l<0) l+=256; goto ci1; } if (chset1==3) {l1=str1[i]; l2=str1[i+1]; if (l1<0) l1+=256; if (l1<128) {i+=1; l=l1;} else {if (l2<0) l2+=256; i+=2; l=l1*256+l2; } goto ci1; } if (chset1==4) {l1=str1[i]; l2=str1[i+1]; if (l1<0) l1+=256; if (l2<0) l2+=256; i+=2; l=l1+l2*256; goto ci1; } ci1: l2=l; // поиск по массиву. если ничего не будет найдено - не перекодировать for (m=0; m<66; m++) { l1=mkcharset[m][chset1]; if (l1==l) l2=mkcharset[m][chset2]; } if ((chset2==0)||(chset2==1)||(chset2==2)) { // вставить один байт в строку str2[j]=(char)(l2%256); j++; goto cinext; } if (chset2==3) { if (l2<128) { // вставить один байт в строку str2[j]=(char)(l2%256); j++; goto cinext; } str2[j]=(char)((l2/256)%256); str2[j+1]=(char)(l2%256); j+=2; goto cinext; } if (chset2==4) { str2[j]=(char)(l2%256); str2[j+1]=(char)((l2/256)%256); j+=2; } cinext: if ((i>=k)||(j>254)) { str2[j]=0; str2[j+1]=0; goto end; } else goto ci; end: return; } /*---------------------------------------------------------- MAIN ---------------*/ int main(int argc, char *argv[]) { int i,j; int otv[10]; // ответы char c; int re=EXIT_SUCCESS; mkiocharset=0; init_msg(); if (argc<=1) goto nach; for (i=1; i=0)&&(otv[0]<=4)) printf ("%s %s",mes[otv[0]],mes0); else printf("? %s",mes0); printf("%s: ",mes[10]); // Порт if (dos==0) printf("/dev/ttyS%d %s",otv[1],mes0); else printf("COM%d %s",otv[1]+1,mes0); printf("%s: ",mes[13]); // Скорость обмена switch(otv[2]) { case 0: case 9:printf("9600 %s",mes0); break; case 1: printf("600 %s",mes0); break; case 2: printf("900 %s",mes0); break; case 3: printf("1200 %s",mes0); break; case 4: printf("1800 %s",mes0); break; case 5: printf("2400 %s",mes0); break; case 6: printf("3600 %s",mes0); break; case 7: printf("4800 %s",mes0); break; case 8: printf("7200 %s",mes0); break; case 10: printf("14400 %s",mes0); break; case 11: printf("19200 %s",mes0); break; case 12: printf("38400 %s",mes0); break; case 13: printf("57600 %s",mes0); break; default: printf("? %s",mes0); } printf("%s: %d %s",mes[14],otv[3],mes0); // Сетевой номер printf("%s ",mes[16],mes0); // Сохранить? ctemp[0]=0; fgets(ctemp,8,stdin); if ((ctemp[0]=='Y')||(ctemp[0]=='y')) { fp0=fopen("mk.cfg","wb"); // открыть файл CFG для записи if (fp0==NULL) {printf("%s %s",mes[18],mes0); re=EX_CANTCREAT; goto end;} for (i=0; i<4; i++) { if (fputc(otv[i],fp0)==EOF) {re=EX_IOERR; fclose(fp0); goto end;}; } goto end; } printf("%s ",mes[17],mes0); // Повторить? fgets(ctemp,8,stdin); if ((ctemp[0]=='Y')||(ctemp[0]=='y')) goto nach; end: return(re); }