PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/SRC/TW/TWSETUP.C

http://thai-writer.googlecode.com/
C | 406 lines | 333 code | 51 blank | 22 comment | 74 complexity | b51e35ae20e87d75f9a4ba9e408cb860 MD5 | raw file
  1. #include <io.h>
  2. #include "inc.h"
  3. #include "convert.h"
  4. #include "..\thai\grphc.h"
  5. #include "..\thai\grdetect.h"
  6. void set_option_to_default(void);
  7. extern int herc_align; /* from herc.asm */
  8. static char cup_option[16]; /* assume that only 16 char max for argument */
  9. static char cuf_option[16]; /* assume that only 16 char max for argument */
  10. void twsetup(int argc, char *argv[])
  11. {
  12. unsigned dummy;
  13. register int i;
  14. progname = argv[0];
  15. while((--argc > 0) && ((i = (*++argv)[0]) == '/' || i == '-'))
  16. {
  17. strcat(cup_option, argv[0]);
  18. strcat(cup_option, " ");
  19. strupr(++argv[0]);
  20. while(i = *argv[0]++)
  21. switch(i)
  22. {
  23. case 'H' :
  24. scrmode = HERCMONO;
  25. break;
  26. /* e alone = ega, em = ega monochrome */
  27. case 'E' :
  28. scrmode = EGA;
  29. break;
  30. /* m alone = mcga */
  31. case 'M' :
  32. if (scrmode == EGA)
  33. scrmode = EGAMONO;
  34. else
  35. scrmode = MCGA;
  36. break;
  37. case 'V' :
  38. scrmode = VGA;
  39. break;
  40. case 'A' :
  41. scrmode = ATT400;
  42. break;
  43. /* /L for Hercules, left-justified */
  44. case 'L' :
  45. herc_align = 0;
  46. break;
  47. case 'N' :
  48. create_bak = NO;
  49. break;
  50. case 'W' :
  51. wordwrap = NO;
  52. break;
  53. case 'P' :
  54. pagebreak = NO;
  55. break;
  56. default :
  57. usage();
  58. }
  59. }
  60. if(cup_option[0] == '\0')
  61. strcpy(cup_option, "/");
  62. if(argc >= 1)
  63. {
  64. placekey(' ');
  65. if(file_exist(argv[0]) || strchr(argv[0], '*') || strchr(argv[0], '?'))
  66. placekey(DNKEY);
  67. placekey(RETKEY);
  68. while(i = *argv[0]++)
  69. placekey(i);
  70. placekey(RETKEY);
  71. }
  72. set_directory();
  73. if(readoption(AUTO_FIND) == 0)
  74. set_option_to_default();
  75. readscrfont(dfont, "NORMAL.FON", tw_dir);
  76. readscrfont(ditalicfont, "ITALIC.FON", tw_dir);
  77. harderr(handler);
  78. initvalue(&dummy, &dummy);
  79. for(i = 0; i < MAXCOL; ++i)
  80. tab[i] = NO;
  81. for(i = 5; i < (rightmar - 6); i += 5)
  82. tab[i] = YES;
  83. for(i = 0; i <= 10; i++)
  84. macro[i][0] = '\0';
  85. filename[0] = '\0';
  86. initscrn();
  87. wind.row = 4; /* set window of edit area */
  88. wind.col = 1;
  89. wind.length = 78;
  90. wind.width = (scrmode == VGA) ? 19 : (scrmode == ATT400) ? 15 : 13;
  91. offbreak(); /* Turn off Ctrl-C checking */
  92. }
  93. void usage(void)
  94. {
  95. fputs("Usage: tw [option] [file]\n", stderr);
  96. fputs("Options\n", stderr);
  97. fputs("\t-h,\t/h Hercules graphic adapter\n", stderr);
  98. fputs("\t-hl,\t/hl Hercules graphic adapter, left justify\n", stderr);
  99. fputs("\t-v,\t/v vga\n", stderr);
  100. fputs("\t-e,\t/e ega with ega display\n", stderr);
  101. fputs("\t-em,\t/em ega with monochrome display\n", stderr);
  102. fputs("\t-m,\t/m mcga\n", stderr);
  103. fputs("\t-a,\t/a at&t400\n", stderr);
  104. fputs("\t-n,\t/n no backup file created\n", stderr);
  105. fputs("\t-w,\t/w no word wrap\n", stderr);
  106. fputs("\t-p,\t/p no page break\n", stderr);
  107. exit(1);
  108. }
  109. void set_directory(void)
  110. {
  111. char drive[MAXDRIVE], dir[MAXDIR], name[MAXFILE], ext[MAXEXT];
  112. /* build tw_dir like this -> C:\EDITOR\TW */
  113. fnsplit(progname, drive, dir, name, ext);
  114. sprintf(tw_dir, "%s%s", drive, dir);
  115. tw_dir[strlen(tw_dir) - 1] = '\0'; /* clear \ */
  116. }
  117. typedef struct
  118. {
  119. char *option_name;
  120. int *p_option_value;
  121. int p_option_default;
  122. unsigned char maxlen;
  123. strtype stype;
  124. } each_option_setup;
  125. each_option_setup option_setup[] = {
  126. {"???? (1.???. 2.?????)", (int *) &stdcode, YES, 1, ONEORTWO},
  127. {"???????? (1.???? 2.???????)", (int *) &pagebreak, YES, 1, ONEORTWO},
  128. {"?????????????? (1.??? 2.??????)", (int *) & wordwrap, YES, 1, ONEORTWO},
  129. {"???????????????????????????????????????? (1.????? 2.????????)", (int *) & create_bak, YES, 1, ONEORTWO},
  130. {"????????????", &leftmar, 1, 3, NUMBER},
  131. {"??????????? (??????? 256)", &rightmar, 65, 3, NUMBER},
  132. {"??????????????????", &lineperpage, 33, 3, NUMBER}
  133. };
  134. /* number of element of option_setup */
  135. #define NELEM_OPTION (sizeof(option_setup) / sizeof(option_setup[0]))
  136. /* read option from file TW.CFG
  137. mode == AUTO try reading from current directoy first, then tw_dir
  138. if not found use the default value
  139. mode == CUR_DIR read from current dir
  140. if not found display error
  141. mode == TW_DIR read from TW.EXE directory
  142. if not found display error
  143. */
  144. int readoption(search_file_mode mode)
  145. {
  146. FILE *fp;
  147. char fname[MAXPATH];
  148. int field, temp;
  149. each_option_setup *op;
  150. sprintf(fname, "%s\\TW.CFG", tw_dir);
  151. if(mode == CUR_DIR || mode == AUTO_FIND)
  152. {
  153. fp = fopen("TW.CFG", "rt");
  154. }
  155. else
  156. {
  157. fp = fopen(fname, "rt");
  158. }
  159. if(fp == NULL && mode == AUTO_FIND)
  160. {
  161. /* fopen file not succes */
  162. /* continue searching in tw_dir */
  163. if((fp = fopen(fname, "rt")) == NULL)
  164. return 0; /* if not found in both dir return 0 for autofind */
  165. }
  166. if(fp == NULL)
  167. {
  168. showerrno();
  169. /* if search in CUR_DIR or TW_DIR and not found return -1 */
  170. return -1 ;
  171. }
  172. /* If we reach here we succesfully open TW.CFG file */
  173. for(op = option_setup; op < option_setup + NELEM_OPTION; op++)
  174. {
  175. field = fscanf(fp, "%d", &temp);
  176. if(field == 1) /* succesfully scan */
  177. {
  178. if(*op -> p_option_value == -1) /* not set from command line */
  179. /* so set it */
  180. *op -> p_option_value = temp;
  181. }
  182. else /* error */
  183. break;
  184. }
  185. fclose(fp);
  186. return 1;
  187. }
  188. /* save option to file TW.CFG in either current directory or tw_dir */
  189. void saveoption(search_file_mode mode)
  190. {
  191. FILE *fp;
  192. char config_file[MAXPATH];
  193. each_option_setup *op;
  194. if(mode == CUR_DIR || mode == AUTO_FIND)
  195. {
  196. fp = fopen("TW.CFG", "wt");
  197. }
  198. else
  199. {
  200. sprintf(config_file, "%s\\TW.CFG", tw_dir);
  201. fp = fopen(config_file, "wt");
  202. }
  203. if(fp == NULL)
  204. return ;
  205. for(op = option_setup; op < option_setup + NELEM_OPTION; op++)
  206. {
  207. fprintf(fp, "%d ", *op -> p_option_value);
  208. }
  209. fprintf(fp, "\n");
  210. fclose(fp);
  211. }
  212. void dispoption(int i)
  213. {
  214. dispstr(option_setup[i].option_name, 65 - CENTER_FACTOR - thaistrlen(option_setup[i].option_name), i + 6, 0);
  215. dispprintf(65 - CENTER_FACTOR, i + 6, 0, " :%d", *option_setup[i].p_option_value);
  216. }
  217. void setoption(void)
  218. {
  219. int i, c;
  220. char st[5];
  221. pagecomplete = NO;
  222. if(stdcode == 0) stdcode = 2;
  223. if(pagebreak == 0) pagebreak = 2;
  224. if(wordwrap == 0) wordwrap = 2;
  225. if(create_bak == 0) create_bak = 2;
  226. framebox(14 - CENTER_FACTOR, 5, (14 - CENTER_FACTOR) + 59, 5 + 1 + NELEM_OPTION, 0);
  227. for(i = 0; i < NELEM_OPTION; i++)
  228. {
  229. dispoption(i);
  230. }
  231. i = 0;
  232. do
  233. {
  234. itoa(*option_setup[i].p_option_value, st, 10);
  235. c = getstring(st, 65 + 2 - CENTER_FACTOR , 6 + i, option_setup[i].maxlen, 0, option_setup[i].stype);
  236. *option_setup[i].p_option_value = atoi(st);
  237. switch(c)
  238. {
  239. case UPKEY :
  240. dispoption(i);
  241. if(i == 0)
  242. i = NELEM_OPTION - 1;
  243. else
  244. i--;
  245. break;
  246. case DNKEY :
  247. dispoption(i);
  248. if(i == NELEM_OPTION - 1)
  249. i = 0;
  250. else
  251. i++;
  252. break;
  253. case YES :
  254. dispoption(i);
  255. if(i < NELEM_OPTION - 1)
  256. {
  257. i++;
  258. break;
  259. }
  260. case ESCKEY :
  261. if(stdcode == 2) stdcode = 0;
  262. if(pagebreak == 2) pagebreak = 0;
  263. if(wordwrap == 2) wordwrap = 0;
  264. if(create_bak == 2) create_bak = 0;
  265. return;
  266. }
  267. }
  268. while(1);
  269. }
  270. void set_option_to_default(void)
  271. {
  272. each_option_setup *op;
  273. for(op = option_setup; op < option_setup + NELEM_OPTION; op++)
  274. {
  275. /* not set from command line, or from config file */
  276. if(*op -> p_option_value == -1)
  277. /* so set it */
  278. *op -> p_option_value = op -> p_option_default;
  279. }
  280. }
  281. void initvalue(unsigned *x, unsigned *y)
  282. {
  283. lineno = 1;
  284. *x = 0;
  285. *y = 0;
  286. firstcol = 0;
  287. blkbegin.lineno = 1;
  288. blkend.lineno = 1;
  289. blkbegin.column = 0;
  290. blkend.column = 0;
  291. dispblock = YES;
  292. }
  293. void print_file(void)
  294. {
  295. static char cup[] = "TWPRINT.EXE";
  296. char ncup[40];
  297. savepic ();
  298. if(changeflag)
  299. {
  300. framebox(4, 3, 4 + 2 + 37 + thaistrlen(filename) + 2, 10, NORMALATTR);
  301. dispprintf(4 + (2 + 37 + thaistrlen(filename) + 2 - 13) / 2, 4, BOLDATTR, "**???????????**");
  302. dispprintf(4 + 2, 5, NORMALATTR, "TWPRINT ???????????????????? ???????????????");
  303. dispprintf(4 + 2, 6, NORMALATTR, "??????? %.30s ???????????????????????????????????", filename);
  304. dispprintf(4 + 2, 7, NORMALATTR, "???????????????????????????????????????????????");
  305. dispprintf(4 + (2 + 37 + thaistrlen(filename) + 2 - 17) / 2, 9, NORMALATTR, "??????????????????????");
  306. ebioskey(0);
  307. }
  308. settext();
  309. /* Find TWPRINT by PATH */
  310. if(spawnlp(P_WAIT, cup, cup, cup_option, filename, NULL) == -1)
  311. {
  312. strcpy(ncup, tw_dir); /* Not found */
  313. strcat(ncup, "\\"); /* Search at TW dir */
  314. strcat(ncup, cup);
  315. if(spawnl(P_WAIT, ncup, ncup, cup_option, filename, NULL) == -1)
  316. {
  317. printf("\n\nCannot find file : TWPRINT.EXE\nPress any key\n\007");
  318. getch();
  319. }
  320. }
  321. initscrn ();
  322. retpic();
  323. showpageall();
  324. }
  325. void edit_font(void)
  326. {
  327. static char cuf[] = "TWFONT.EXE";
  328. char ncuf[40];
  329. /* Find TWFONT by PATH */
  330. settext();
  331. if(spawnlp(P_WAIT, cuf, cuf, cuf_option, NULL) == -1)
  332. {
  333. strcpy(ncuf, tw_dir); /* Not found */
  334. strcat(ncuf, "\\"); /* Search at TW dir */
  335. strcat(ncuf, cuf);
  336. if(spawnl(P_WAIT, ncuf, ncuf, cuf_option, filename, NULL) == -1)
  337. {
  338. printf("\n\nCannot find file : TWFONT.EXE\nPress any key\n\007");
  339. getch();
  340. }
  341. }
  342. initscrn();
  343. showpageall();
  344. }
  345. void offbreak()
  346. {
  347. union REGS inreg,outreg;
  348. inreg.x.ax = 0x3301;
  349. inreg.h.dl = 0;
  350. intdos(&inreg, &outreg);
  351. }