calctool.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: calctool.c /main/9 1996/09/25 11:28:16 rswiston $ */
  24. /* *
  25. * calctool.c *
  26. * Contains the none user interface portion of the Desktop *
  27. * Calculator. *
  28. * *
  29. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  30. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  31. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  32. * (c) Copyright 1993, 1994 Novell, Inc. *
  33. */
  34. #include <stdio.h>
  35. #include <ctype.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <unistd.h>
  39. #include <sys/types.h>
  40. #include <sys/file.h>
  41. #include <sys/param.h>
  42. #include <pwd.h>
  43. #include <math.h>
  44. #include "patchlevel.h"
  45. #include "calctool.h"
  46. #include "ds_common.h"
  47. time_t time() ;
  48. double max_fix[4] = {
  49. 6.871947674e+10, 3.245185537e+32,
  50. 1.000000000e+36, 2.230074520e+43
  51. } ;
  52. double min_fix0[4] = {
  53. 2.500000000e-1, 3.750000000e-1,
  54. 1.000000000e-1, 4.375000000e-1
  55. } ;
  56. double min_fix1[4] = {
  57. 1.250000000e-1, 4.687500000e-2,
  58. 1.000000000e-2, 2.734375000e-2
  59. } ;
  60. double min_fix2[4] = {
  61. 6.250000000e-2, 5.859375000e-3,
  62. 1.000000000e-3, 1.708984375e-3
  63. } ;
  64. double min_fix3[4] = {
  65. 3.125000000e-2, 7.324218750e-4,
  66. 1.000000000e-4, 1.068115234e-4
  67. } ;
  68. double min_fix4[4] = {
  69. 1.562500000e-2, 9.155273437e-5,
  70. 1.000000000e-5, 6.675720215e-6
  71. } ;
  72. double min_fix5[4] = {
  73. 7.812500000e-3, 1.144409180e-5,
  74. 1.000000000e-6, 4.172325134e-7
  75. } ;
  76. double min_fix6[4] = {
  77. 6.906250000e-3, 1.430511475e-6,
  78. 1.000000000e-7, 2.607703209e-8
  79. } ;
  80. double min_fix7[4] = {
  81. 1.953125000e-3, 1.788139343e-7,
  82. 1.000000000e-8, 1.629814506e-9
  83. } ;
  84. double min_fix8[4] = {
  85. 9.765625000e-4, 2.235174179e-8,
  86. 1.000000000e-9, 1.018634066e-10
  87. } ;
  88. double min_fix9[4] = {
  89. 4.882812500e-4, 2.793967724e-9,
  90. 1.000000000e-10, 6.366462912e-12
  91. } ;
  92. extern char *base_str[] ; /* Strings for each base value. */
  93. extern char *cmdstr[] ; /* Strings for each command line option. */
  94. extern char *dtype_str[] ; /* Strings for each display mode value. */
  95. extern char *lstrs[] ; /* Labels for various Motif items. */
  96. extern char *mess[] ; /* Message strings. */
  97. extern char *mode_str[] ; /* Strings for each mode value. */
  98. extern char *opts[] ; /* Command line option strings. */
  99. extern char *ttype_str[] ; /* Strings for each trig type value. */
  100. extern char *ustrs[] ; /* Usage message strings. */
  101. extern char *vstrs[] ; /* Various strings. */
  102. char digits[] = "0123456789ABCDEF" ;
  103. int basevals[4] = { 2, 8, 10, 16 } ;
  104. int left_pos[BCOLS] = { 3, 2, 1, 0 } ; /* Left positions. */
  105. int right_pos[BCOLS] = { 0, 1, 2, 3 } ; /* "Right" positions. */
  106. /* Valid keys when an error condition has occurred. */
  107. /* MEM KEYS clr clr QUIT REDRAW */
  108. char validkeys[MAXVKEYS] = { 'm', 'k', '\177', '\013', 'q', '\f' } ;
  109. Vars v ; /* Calctool variables and options. */
  110. struct menu_entry menu_entries[MAXENTRIES] ;
  111. struct menu cmenus[MAXMENUS] = { /* Calculator menus. */
  112. /* title total index defval */
  113. { (char *) NULL, 10, 0, 2 /* 2 places */ }, /* ACC */
  114. { (char *) NULL, 4, 20, 2 /* Decimal */ }, /* BASE TYPE */
  115. { (char *) NULL, 10, 0, 0 /* Con. 0 */ }, /* CON */
  116. { (char *) NULL, 10, 10, 0 /* Reg. 0 */ }, /* EXCH */
  117. { (char *) NULL, 10, 0, 0 /* Fun. 0 */ }, /* FUN */
  118. { (char *) NULL, 4, 30, 0 /* Basic */ }, /* MODE */
  119. { (char *) NULL, 3, 24, 1 /* Fixed */ }, /* NUM TYPE */
  120. { (char *) NULL, 10, 10, 0 /* Reg. 0 */ }, /* RCL */
  121. { (char *) NULL, 10, 10, 0 /* Reg. 0 */ }, /* STO */
  122. { (char *) NULL, 3, 27, 0 /* Degrees */ }, /* TRIG TYPE */
  123. } ;
  124. /* This table shows the keyboard values that are currently being used:
  125. *
  126. * | a b c d e f g h i j k l m n o p q r s t u v w x y z
  127. *-------------+--------------------------------------------------
  128. * Control: | a c d f h i l m r s t u y
  129. * Lower: | a b c d e f h i k m n p q r s v x y
  130. * Upper: | A B C D E F G M N P Q R S T X
  131. * Numeric: | 0 1 2 3 4 5 6 7 8 9
  132. * Other: | @ . + - * / = % ( ) # < > [ ] { } | & ~ ^ ? ! \177
  133. *----------------------------------------------------------------
  134. */
  135. /* Calculator button values. */
  136. struct button buttons[TITEMS] = {
  137. /* str str2 value opdisp menutype resname func */
  138. /* Row 1. */
  139. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "blank", do_none },
  140. { (char *)NULL, (char *)NULL, 0, OP_SET, M_FUN, "fun", do_pending },
  141. { (char *)NULL, (char *)NULL, 0, OP_SET, M_CON, "con", do_pending },
  142. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "keys", do_keys },
  143. /* Row 2. */
  144. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "int", do_portion },
  145. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "frac", do_portion },
  146. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "abs", do_portion },
  147. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "exp", do_expno },
  148. /* Row 3. */
  149. { (char *)NULL, (char *)NULL, 0, OP_SET, M_ACC, "acc", do_pending },
  150. { (char *)NULL, (char *)NULL, 0, OP_SET, M_STO, "sto", do_pending },
  151. { (char *)NULL, (char *)NULL, 0, OP_SET, M_RCL, "rcl", do_pending },
  152. { (char *)NULL, (char *)NULL, 0, OP_SET, M_EXCH, "exch", do_pending },
  153. /* Row 4. */
  154. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "blank", do_none },
  155. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "blank", do_none },
  156. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "blank", do_none },
  157. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "blank", do_none },
  158. /* Row 5. */
  159. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "blank", do_none },
  160. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "blank", do_none },
  161. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "blank", do_none },
  162. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "blank", do_none },
  163. /* Row 6. */
  164. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "blank", do_none },
  165. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "blank", do_none },
  166. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "blank", do_none },
  167. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "blank", do_none },
  168. /* Row 7. */
  169. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "recip", do_immed },
  170. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "square", do_immed },
  171. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "sqrt", do_immed },
  172. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "percent", do_calc },
  173. /* Row 8. */
  174. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "lparen", do_paren },
  175. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "rparen", do_paren },
  176. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "bsp", do_delete },
  177. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "clr", do_clear },
  178. /* Row 9. */
  179. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "numd", do_number },
  180. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "nume", do_number },
  181. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "numf", do_number },
  182. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "chs", do_immed },
  183. /* Row 10. */
  184. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "numa", do_number },
  185. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "numb", do_number },
  186. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "numc", do_number },
  187. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "mul", do_calc },
  188. /* Row 11. */
  189. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num7", do_number },
  190. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num8", do_number },
  191. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num9", do_number },
  192. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "div", do_calc },
  193. /* Row 12. */
  194. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num4", do_number },
  195. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num5", do_number },
  196. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num6", do_number },
  197. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "sub", do_calc },
  198. /* Row 13. */
  199. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num1", do_number },
  200. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num2", do_number },
  201. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num3", do_number },
  202. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "add", do_calc },
  203. /* Row 14. */
  204. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "num0", do_number },
  205. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "point", do_point },
  206. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "equals", do_calc },
  207. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "quit", do_frame },
  208. { (char *)NULL, (char *)NULL, 0, OP_SET, M_BASE, "base", do_pending },
  209. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NUM, "disp", do_pending },
  210. { (char *)NULL, (char *)NULL, 0, OP_SET, M_MODE, "mode", do_pending },
  211. { (char *)NULL, (char *)NULL, 0, OP_SET, M_TRIG, "trig", do_pending },
  212. } ;
  213. struct button mode_buttons[MAXMODES * MODEKEYS] = {
  214. /* str str2 value opdisp menutype resname func */
  215. /* Financial. */
  216. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "term", do_business},
  217. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "rate", do_business},
  218. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "pv", do_business},
  219. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "pmt", do_business},
  220. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "fv", do_business},
  221. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "ppy", do_business},
  222. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "clrreg", do_business},
  223. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "ctrm", do_business},
  224. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "ddb", do_business},
  225. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "sln", do_business},
  226. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "syd", do_business},
  227. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
  228. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
  229. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
  230. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
  231. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
  232. /* Logical. */
  233. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "lshift", do_pending },
  234. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "rshift", do_pending },
  235. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "and16", do_immed },
  236. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "and32", do_immed },
  237. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "or", do_calc },
  238. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "and", do_calc },
  239. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "not", do_immed },
  240. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "xor", do_calc },
  241. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "xnor", do_calc },
  242. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
  243. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
  244. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
  245. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
  246. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
  247. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
  248. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
  249. /* Scientific. */
  250. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "inv", do_immed },
  251. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "hyp", do_immed },
  252. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "etox", do_immed },
  253. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "tentox", do_immed },
  254. { (char *)NULL, (char *)NULL, 0, OP_SET, M_NONE, "ytox", do_calc },
  255. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "xfact", do_immed },
  256. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "cos", do_trig },
  257. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "sin", do_trig },
  258. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "tan", do_trig },
  259. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "ln", do_immed },
  260. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "log", do_immed },
  261. { (char *)NULL, (char *)NULL, 0, OP_CLEAR, M_NONE, "rand", do_immed },
  262. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
  263. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
  264. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
  265. { (char *)NULL, (char *)NULL, 0, OP_NOP, M_NONE, "", do_none },
  266. } ;
  267. int
  268. char_val(char chr)
  269. {
  270. if (chr >= '0' && chr <= '9') return(chr - '0') ;
  271. else if (chr >= 'a' && chr <= 'f') return(chr - 'a' + 10) ;
  272. else if (chr >= 'A' && chr <= 'F') return(chr - 'A' + 10) ;
  273. else return(-1) ;
  274. }
  275. void
  276. clear_display(void)
  277. {
  278. int i ;
  279. v->pointed = 0 ;
  280. v->toclear = 1 ;
  281. v->defState = 1 ;
  282. i = 0 ;
  283. mpcim(&i, v->MPdisp_val) ;
  284. STRCPY(v->display, make_number(v->MPdisp_val, FALSE)) ;
  285. set_item(DISPLAYITEM, v->display) ;
  286. v->hyperbolic = 0 ;
  287. v->inverse = 0 ;
  288. v->show_paren = 0 ;
  289. v->opsptr = 0 ; /* Clear parentheses stacks. */
  290. v->numsptr = 0 ;
  291. set_item(HYPITEM, " ") ;
  292. set_item(INVITEM, " ") ;
  293. }
  294. /* Convert .dtcalcrc line to ascii values. */
  295. /* line Input line to be converted. */
  296. char *
  297. convert(char *line)
  298. {
  299. static char output[MAXLINE] ; /* Converted output record. */
  300. int ctrl = 0 ; /* Set if we are processing a control character. */
  301. int i ; /* Position within input line. */
  302. int len ;
  303. int n = 0 ; /* Position within output line. */
  304. len = strlen(line) ;
  305. for (i = 0; i < len; i++)
  306. {
  307. if (line[i] == ' ') continue ;
  308. else if (line[i] == '\\') ctrl = 1 ;
  309. else if (ctrl)
  310. {
  311. output[n++] = CTL(line[i]) ;
  312. ctrl = 0 ;
  313. }
  314. else output[n++] = line[i] ;
  315. }
  316. output[n] = '\0' ;
  317. return(output) ;
  318. }
  319. void
  320. do_dtcalc(int argc, char **argv)
  321. {
  322. char *ptr ;
  323. v->progname = argv[0] ; /* Save programs name. */
  324. v->appname = NULL ;
  325. init_cmdline_opts() ; /* Initialize command line option strings. */
  326. if ((ptr = strrchr(argv[0], '/')) != NULL)
  327. read_str(&v->appname, ptr+1) ;
  328. else read_str(&v->appname, argv[0]) ;
  329. init_text() ; /* Setup text strings depending upon language. */
  330. init_vars() ; /* Setup default values for variables. */
  331. key_init() ; /* Determine numeric function keys. */
  332. load_resources() ; /* Get resources from various places. */
  333. read_resources() ; /* Read resources from merged database. */
  334. get_options(argc, argv) ; /* Get command line arguments. */
  335. if(application_args.session != NULL)
  336. {
  337. RestoreSession();
  338. }
  339. read_rcfiles() ; /* Read .dtcalcrc's files. */
  340. init_graphics() ;
  341. make_frames() ; /* Create dtcalc window frames. */
  342. v->shelf = NULL ; /* No selection for shelf initially. */
  343. v->noparens = 0 ; /* No unmatched brackets initially. */
  344. v->opsptr = 0 ; /* Nothing on the parentheses op stack. */
  345. v->numsptr = 0 ; /* Nothing on the parenthese numeric stack. */
  346. v->pending = 0 ; /* No initial pending command. */
  347. if(application_args.session == NULL)
  348. v->tstate = 0 ; /* Button values displayed first. */
  349. v->hyperbolic = 0 ; /* Normal trig functions initially. */
  350. v->inverse = 0 ; /* No inverse functions initially. */
  351. srand48((long) time((time_t *) 0)) ; /* Seed random number generator. */
  352. make_items() ; /* Create server images and fir frames. */
  353. if(v->display[0] == 0)
  354. do_clear() ; /* Initialize and clear display. */
  355. if (v->rstate == TRUE) /* Show the memory register window? */
  356. {
  357. make_registers(MEM) ;
  358. if (!v->iconic) win_display(FCP_REG, TRUE) ;
  359. }
  360. if (!v->iconic) win_display(FCP_MODE, TRUE) ;
  361. show_display(v->MPdisp_val) ; /* Output in correct display mode. */
  362. save_cmdline(argc, argv) ; /* Setup dtcalc command line. */
  363. start_tool() ; /* Display the calculator. */
  364. }
  365. /* Dtcalc's customised math library error-handling routine. */
  366. void
  367. doerr(char *errmes)
  368. {
  369. if (!v->started) return ;
  370. STRCPY(v->display, errmes) ;
  371. set_item(DISPLAYITEM, v->display) ;
  372. v->error = 1 ;
  373. beep() ;
  374. set_item(OPITEM, vstrs[(int) V_CLR]) ;
  375. }
  376. /* Get boolean resource from database. */
  377. int
  378. get_bool_resource(enum res_type rtype, int *boolval)
  379. {
  380. char *val, tempstr[MAXLINE] ;
  381. int len, n ;
  382. if ((val = get_resource(rtype)) == NULL) return(0) ;
  383. STRCPY(tempstr, val) ;
  384. len = strlen(tempstr) ;
  385. for (n = 0; n < len; n++)
  386. if (isupper(tempstr[n])) tempstr[n] = tolower(tempstr[n]) ;
  387. if (EQUAL(tempstr, vstrs[(int) V_TRUE])) *boolval = TRUE ;
  388. else *boolval = FALSE ;
  389. return(1) ;
  390. }
  391. /* Get button index for given character value, setting curwin,
  392. * row and column appropriately. Note that if the value isn't found,
  393. * then a value of TITEMS is returned. This is "chucked out" by
  394. * process_item as being invalid.
  395. *
  396. * XXX: This routine can be improved by using a hash lookup table.
  397. */
  398. int
  399. get_index(char ch)
  400. {
  401. int n ;
  402. for (n = 0; n < TITEMS; n++) {
  403. if (ch == buttons[n].value)
  404. break ;
  405. }
  406. if (n < TITEMS)
  407. v->curwin = FCP_KEY ;
  408. else
  409. {
  410. return(TITEMS) ;
  411. }
  412. v->row = n / MAXCOLS ;
  413. v->column = n - (v->row * MAXCOLS) ;
  414. return(n) ;
  415. }
  416. /* Get integer resource from database. */
  417. int
  418. get_int_resource(enum res_type rtype, int *intval)
  419. {
  420. char *val ;
  421. if ((val = get_resource(rtype)) == NULL) return(0) ;
  422. *intval = atoi(val) ;
  423. return(1) ;
  424. }
  425. /* Get keyboard equivalent from first character of localised string. */
  426. void
  427. get_key_val(char *val, char *str)
  428. {
  429. *val = str[0] ;
  430. }
  431. void
  432. get_label(int n)
  433. {
  434. char *temp;
  435. if (v->tstate)
  436. temp = buttons[n].str2;
  437. else
  438. temp = buttons[n].str;
  439. if(temp != NULL)
  440. STRCPY(v->pstr, temp) ;
  441. else
  442. STRCPY(v->pstr, "");
  443. }
  444. /* Extract command line options. */
  445. void
  446. get_options(int argc, char *argv[])
  447. {
  448. char next[MAXLINE] ; /* The next command line parameter. */
  449. char strval[MAXLINE] ;
  450. char *msg;
  451. int i, len;
  452. INC ;
  453. while (argc > 0)
  454. {
  455. if (argv[0][0] == '-' || argv[0][0] == '+')
  456. {
  457. switch (argv[0][1])
  458. {
  459. case 'D' : v->MPdebug = TRUE ; /* MP debug info. to stderr. */
  460. break ;
  461. case 'a' : INC ;
  462. getparam(next, argv, opts[(int) O_ACCVAL]) ;
  463. v->accuracy = atoi(next) ;
  464. if (v->accuracy < 0 || v->accuracy > 9)
  465. {
  466. msg = (char *) XtMalloc(strlen(
  467. opts[(int) O_ACCRANGE]) + 3);
  468. sprintf(msg, "%s", opts[(int) O_ACCRANGE]);
  469. _DtSimpleError (v->appname, DtWarning, NULL, msg);
  470. XtFree(msg);
  471. v->accuracy = 2 ;
  472. }
  473. break ;
  474. case 'm' : INC ;
  475. msg = (char *) XtMalloc(strlen(opts[(int) O_MODE])+
  476. strlen(next) + 3);
  477. sprintf(msg, opts[(int) O_MODE], next);
  478. getparam(next, argv, msg) ;
  479. XtFree(msg);
  480. STRCPY(strval, next) ;
  481. len = strlen(strval) ;
  482. for (i = 0; i < len; i++)
  483. {
  484. if (islower(strval[i]))
  485. strval[i] = toupper(strval[i]) ;
  486. }
  487. if(strcmp(strval, "FINANCIAL") == 0)
  488. v->modetype = FINANCIAL ;
  489. else if(strcmp(strval, "LOGICAL") == 0)
  490. v->modetype = LOGICAL ;
  491. else if(strcmp(strval, "SCIENTIFIC") == 0)
  492. v->modetype = SCIENTIFIC ;
  493. else
  494. {
  495. msg = (char *) XtMalloc(strlen(opts[(int) O_MODE])+
  496. strlen(next) + 3);
  497. sprintf(msg, opts[(int) O_MODE], next);
  498. _DtSimpleError (v->appname, DtWarning, NULL, msg);
  499. XtFree(msg);
  500. v->modetype = SCIENTIFIC ;
  501. }
  502. break ;
  503. case 'b' : INC ;
  504. getparam(next, argv, opts[(int) O_BASE]) ;
  505. STRCPY(strval, next) ;
  506. len = strlen(strval) ;
  507. for (i = 0; i < len; i++)
  508. {
  509. if (islower(strval[i]))
  510. strval[i] = toupper(strval[i]) ;
  511. }
  512. if(strncmp(strval, "BIN", 3) == 0)
  513. v->base = BIN ;
  514. else if(strncmp(strval, "OCT", 3) == 0)
  515. v->base = OCT ;
  516. else if(strncmp(strval, "DEC", 3) == 0)
  517. v->base = DEC ;
  518. else if(strncmp(strval, "HEX", 3) == 0)
  519. v->base = HEX ;
  520. else
  521. {
  522. msg = (char *) XtMalloc(strlen(
  523. opts[(int) O_BASE]) + 3);
  524. sprintf(msg, "%s", opts[(int) O_BASE]);
  525. _DtSimpleError (v->appname, DtWarning, NULL, msg);
  526. XtFree(msg);
  527. v->base = DEC ;
  528. }
  529. break ;
  530. case 'n' : if(strcmp(&argv[0][1], "notation") == 0)
  531. {
  532. INC ;
  533. msg = (char *) XtMalloc(strlen(
  534. opts[(int) O_DISPLAY]) +
  535. strlen(next) + 3);
  536. sprintf(msg, opts[(int) O_DISPLAY], next);
  537. getparam(next, argv, msg) ;
  538. XtFree(msg);
  539. STRCPY(strval, next) ;
  540. len = strlen(strval) ;
  541. for (i = 0; i < len; i++)
  542. {
  543. if (islower(strval[i]))
  544. strval[i] = toupper(strval[i]) ;
  545. }
  546. if(strncmp(strval, "FIX", 3) == 0)
  547. v->dtype = FIX ;
  548. else if(strncmp(strval, "ENG", 3) == 0)
  549. v->dtype = ENG ;
  550. else if(strncmp(strval, "SCI", 3) == 0)
  551. v->dtype = SCI ;
  552. else
  553. {
  554. msg = (char *) XtMalloc(strlen(
  555. opts[(int) O_DISPLAY]) +
  556. strlen(next) + 3);
  557. sprintf(msg, opts[(int) O_DISPLAY], next);
  558. _DtSimpleError (v->appname, DtWarning, NULL, msg);
  559. XtFree(msg);
  560. v->dtype = FIX ;
  561. }
  562. break ;
  563. }
  564. else if(strcmp(&argv[0][1], "no_menu_bar") == 0)
  565. {
  566. INC ;
  567. application_args.menuBar = False;
  568. break ;
  569. }
  570. case 't' : if(strcmp(&argv[0][1], "trig") == 0)
  571. {
  572. INC ;
  573. msg = (char *) XtMalloc(strlen(
  574. opts[(int) O_TRIG]) +
  575. strlen(next) + 3);
  576. sprintf(msg, opts[(int) O_TRIG], next);
  577. getparam(next, argv, msg) ;
  578. XtFree(msg);
  579. STRCPY(strval, next) ;
  580. len = strlen(strval) ;
  581. for (i = 0; i < len; i++)
  582. {
  583. if (islower(strval[i]))
  584. strval[i] = toupper(strval[i]) ;
  585. }
  586. if(strncmp(strval, "DEG", 3) == 0)
  587. v->ttype = DEG ;
  588. else if(strncmp(strval, "RAD", 3) == 0)
  589. v->ttype = RAD ;
  590. else if(strncmp(strval, "GRAD", 4) == 0)
  591. v->ttype = GRAD ;
  592. else
  593. {
  594. msg = (char *) XtMalloc(strlen(
  595. opts[(int) O_TRIG]) +
  596. strlen(next) + 3);
  597. sprintf(msg, opts[(int) O_TRIG], next);
  598. _DtSimpleError (v->appname, DtWarning, NULL, msg);
  599. XtFree(msg);
  600. v->ttype = DEG ;
  601. }
  602. break ;
  603. }
  604. case 's' : if(strcmp(&argv[0][1], "session") == 0)
  605. {
  606. INC ;
  607. getparam(next, argv, opts[(int) O_SESSION]) ;
  608. application_args.session = XtNewString(next);
  609. break ;
  610. }
  611. case '?' :
  612. case 'v' : usage(v->progname) ;
  613. break ;
  614. default :
  615. usage(v->progname) ;
  616. }
  617. INC ;
  618. }
  619. else INC ;
  620. }
  621. }
  622. void
  623. getparam(char *s, char *argv[], char *errmes)
  624. {
  625. char *msg;
  626. if (*argv != NULL && argv[0][0] != '-') STRCPY(s, *argv) ;
  627. else
  628. {
  629. msg = (char *) XtMalloc(strlen(mess[(int) MESS_PARAM]) +
  630. strlen(errmes) + 3);
  631. sprintf(msg, mess[(int) MESS_PARAM], errmes);
  632. _DtSimpleError (v->appname, DtError, NULL, msg);
  633. FPRINTF(stderr, mess[(int) MESS_PARAM], errmes) ;
  634. exit(1) ;
  635. }
  636. }
  637. /* Read .dtcalcrc file. */
  638. void
  639. get_rcfile(char *name)
  640. {
  641. char line[MAXLINE] ; /* Current line from the .dtcalcrc file. */
  642. char tmp[MAXLINE] ; /* Used to extract definitions. */
  643. double cval ; /* Current constant value being converted. */
  644. int i ; /* Index to constant or function array. */
  645. int isval ; /* Set to 'c' or 'f' for convertable line. */
  646. int len, n ;
  647. FILE *rcfd ; /* File descriptor for dtcalc rc file. */
  648. if ((rcfd = fopen(name, "r")) == NULL) return ;
  649. /* Process the .dtcalcrc file. There are currently four types of
  650. * records to look for:
  651. *
  652. * 1) Those starting with a hash in the first column are comments.
  653. *
  654. * 2) Lines starting with 'c' or 'C' in the first column are
  655. * definitions for constants. The cC is followed by a digit in
  656. * the range 0-9, then a space. This is followed by a number
  657. * in fixed or scientific notation. Following this is an optional
  658. * comment, which if found, will be used in the popup menu for
  659. * the constants. If the comment is present, there must be at
  660. * least one space between this and the preceding number.
  661. *
  662. * 3) Those starting with a 'f' or a 'F' in the first column are
  663. * definitions for functions. The fF is followed by a digit in
  664. * the range 0-9, then a space. This is followed by a function
  665. * definition. Following this is an optional comment, which if
  666. * found, will be used in the popup menu for the functions.
  667. * If the comment is present, there must be at least one space
  668. * between this and the preceding function definition.
  669. *
  670. * 4) Lines starting with a 'r' or a 'R' in the first column are
  671. * definitions for the initial contents of the calculators
  672. * memory registers. The rR is followed by a digit in the
  673. * range 0-9, then a space. This is followed by a number in
  674. * fixed or scientific notation. The rest of the line is ignored.
  675. *
  676. * All other lines are ignored.
  677. *
  678. * Two other things to note. There should be no embedded spaces in
  679. * the function definitions, and whenever a backslash is found, that
  680. * and the following character signify a control character, for
  681. * example \g would be ascii 7.
  682. */
  683. while (fgets(line, MAXLINE, rcfd) != NULL)
  684. {
  685. isval = 0 ;
  686. if (line[0] == 'c' || line[0] == 'C') isval = 'c' ;
  687. else if (line[0] == 'f' || line[0] == 'F') isval = 'f' ;
  688. else if (line[0] == 'r' || line[0] == 'R') isval = 'r' ;
  689. if (isval)
  690. if (line[1] >= '0' && line[1] <= '9' && line[2] == ' ')
  691. {
  692. i = char_val(line[1]) ;
  693. if (isval == 'c')
  694. {
  695. n = sscanf(&line[3], "%lf", &cval) ;
  696. if (n == 1)
  697. {
  698. if(line[3] == '-')
  699. {
  700. MPstr_to_num(&line[4], DEC, v->MPcon_vals[i]) ;
  701. mpneg(v->MPcon_vals[i], v->MPcon_vals[i]) ;
  702. }
  703. else
  704. MPstr_to_num(&line[3], DEC, v->MPcon_vals[i]) ;
  705. }
  706. }
  707. else if (isval == 'f')
  708. {
  709. SSCANF(&line[3], "%s", tmp) ;
  710. STRCPY(v->fun_vals[i], convert(tmp)) ;
  711. }
  712. else if (isval == 'r')
  713. {
  714. n = sscanf(&line[3], "%lf", &cval) ;
  715. if (n == 1) MPstr_to_num(&line[3], DEC, v->MPmvals[i]) ;
  716. continue ;
  717. }
  718. len = strlen(line) ;
  719. for (n = 3; n < len; n++)
  720. if (line[n] == ' ' || line[n] == '\n')
  721. {
  722. while (line[n] == ' ') n++ ;
  723. line[strlen(line)-1] = '\0' ;
  724. if (isval == 'c')
  725. {
  726. STRCPY(tmp, make_number(v->MPcon_vals[i], TRUE)) ;
  727. SPRINTF(v->con_names[i], "%1d: %s [%s]",
  728. i, tmp, &line[n]) ;
  729. }
  730. else
  731. SPRINTF(v->fun_names[i], "%1d: %s [%s]",
  732. i, tmp, &line[n]) ;
  733. break ;
  734. }
  735. }
  736. }
  737. FCLOSE(rcfd) ;
  738. }
  739. /* Get a string resource from database. */
  740. int
  741. get_str_resource(enum res_type rtype, char *strval)
  742. {
  743. char *val ;
  744. int i, len ;
  745. if ((val = get_resource(rtype)) == NULL) return(0) ;
  746. STRCPY(strval, val) ;
  747. len = strlen(strval) ;
  748. if(rtype != R_TRIG && rtype != R_DISPLAY)
  749. {
  750. for (i = 0; i < len; i++)
  751. if (islower(strval[i])) strval[i] = toupper(strval[i]) ;
  752. }
  753. return(1) ;
  754. }
  755. /* Grey out numeric buttons depending upon base. */
  756. void
  757. grey_buttons(enum base_type base)
  758. {
  759. char val ;
  760. int column, dim, i, n, row ;
  761. for (i = 0; i < 16; i++)
  762. {
  763. val = digits[i] ;
  764. if (isupper(val)) val = tolower(val) ;
  765. for (n = 0; n < TITEMS; n++)
  766. if (val == buttons[n].value) break ;
  767. row = n / MAXCOLS ;
  768. column = n - (row * MAXCOLS) ;
  769. if (i < basevals[(int) base]) dim = FALSE ;
  770. else dim = TRUE ;
  771. grey_button(row, column, dim) ;
  772. }
  773. }
  774. /* Process right button menu selection. */
  775. void
  776. handle_menu_selection(int n, int item)
  777. {
  778. if (item != -1)
  779. {
  780. if (IS_KEY(v->pending, KEY_LPAR)) /* Are we inside parentheses? */
  781. {
  782. v->current = buttons[n].value ;
  783. do_paren() ;
  784. v->current = item ;
  785. do_paren() ;
  786. }
  787. else
  788. {
  789. save_pending_values(buttons[n].value) ;
  790. v->current = item ;
  791. v->ismenu = 1 ; /* To prevent grey buttons being redrawn. */
  792. do_pending() ;
  793. v->ismenu = 0 ;
  794. }
  795. }
  796. }
  797. /* Setup default values for various variables. */
  798. void
  799. init_vars(void)
  800. {
  801. int acc, i, n, size ;
  802. v->accuracy = 2 ; /* Initial accuracy. */
  803. v->base = DEC ; /* Initial base. */
  804. v->dtype = FIX ; /* Initial number display mode. */
  805. v->ttype = DEG ; /* Initial trigonometric type. */
  806. v->modetype = SCIENTIFIC; /* Initial calculator mode. */
  807. v->rstate = 0 ; /* No memory register frame display initially. */
  808. v->frstate = 0 ; /* No fin. memory register frame display
  809. initially. */
  810. v->iconic = FALSE ; /* Calctool not iconic by default. */
  811. v->MPdebug = FALSE ; /* No debug info by default. */
  812. v->MPerrors = TRUE ; /* No error information. */
  813. acc = MAX_DIGITS + 12 ; /* MP internal accuracy. */
  814. size = MP_SIZE ;
  815. mpset(&acc, &size, &size) ;
  816. v->hasicon = FALSE ; /* Use standard dtcalc icon by default. */
  817. v->beep = TRUE ; /* Beep on error by default. */
  818. v->error = 0 ; /* No calculator error initially. */
  819. v->key_exp = 0 ; /* Not entering an exponent number. */
  820. v->pending_op = 0 ; /* No pending arithmetic operation. */
  821. v->titleline = NULL ; /* No User supplied title line. */
  822. v->display[0] = 0 ;
  823. v->x = 0;
  824. v->x = 0;
  825. v->width = 0;
  826. v->height = 0;
  827. v->workspaces = NULL;
  828. read_str(&v->iconlabel, lstrs[(int) L_LCALC]) ; /* Default icon label. */
  829. MPstr_to_num("0.621", DEC, v->MPcon_vals[0]) ; /* kms/hr <=> miles/hr. */
  830. MPstr_to_num("1.4142135623", DEC, v->MPcon_vals[1]) ; /* square root of 2 */
  831. MPstr_to_num("2.7182818284", DEC, v->MPcon_vals[2]) ; /* e */
  832. MPstr_to_num("3.1415926535", DEC, v->MPcon_vals[3]) ; /* pi */
  833. MPstr_to_num("2.54", DEC, v->MPcon_vals[4]) ; /* cms <=> inch. */
  834. MPstr_to_num("57.295779513", DEC, v->MPcon_vals[5]) ; /* degrees/radian. */
  835. MPstr_to_num("1048576.0", DEC, v->MPcon_vals[6]) ; /* 2 ^ 20. */
  836. MPstr_to_num("0.0353", DEC, v->MPcon_vals[7]) ; /* grams <=> ounce. */
  837. MPstr_to_num("0.948", DEC, v->MPcon_vals[8]) ; /* Kjoules <=> BTU's. */
  838. MPstr_to_num("0.0610", DEC, v->MPcon_vals[9]) ; /* cms3 <=> inches3. */
  839. n = 0 ;
  840. for (i = 0; i < MAXREGS; i++) mpcim(&n, v->MPmvals[i]) ;
  841. for (i = 0; i < FINREGS; i++)
  842. v->MPfvals[i] = (double)n;
  843. v->MPfvals[FINREGS - 1] = (double)12;
  844. }
  845. void
  846. initialize(void)
  847. {
  848. int i ;
  849. v->error = 0 ; /* Currently no display error. */
  850. v->cur_op = '?' ; /* No arithmetic operator defined yet. */
  851. v->old_cal_value = '?' ;
  852. i = 0 ;
  853. mpcim(&i, v->MPresult) ; /* No previous result yet. */
  854. mpcim(&i, v->MPlast_input) ;
  855. }
  856. /* Convert engineering or scientific number. */
  857. char *
  858. make_eng_sci(int *MPnumber)
  859. {
  860. char fixed[MAX_DIGITS+1], *optr ;
  861. int MP1[MP_SIZE], MPatmp[MP_SIZE], MPval[MP_SIZE] ;
  862. int MP1base[MP_SIZE], MP3base[MP_SIZE], MP10base[MP_SIZE] ;
  863. int i, dval, len, n ;
  864. int MPmant[MP_SIZE] ; /* Mantissa. */
  865. int ddig ; /* Number of digits in exponent. */
  866. int eng = 0 ; /* Set if this is an engineering number. */
  867. int exp = 0 ; /* Exponent */
  868. if (v->dtype == ENG) eng = 1 ;
  869. optr = v->snum ;
  870. mpabs(MPnumber, MPval) ;
  871. n = 0 ;
  872. mpcim(&n, MP1) ;
  873. if (mplt(MPnumber, MP1)) *optr++ = '-' ;
  874. mpstr(MPval, MPmant) ;
  875. mpcim(&basevals[(int) v->base], MP1base) ;
  876. n = 3 ;
  877. mppwr(MP1base, &n, MP3base) ;
  878. n = 10 ;
  879. mppwr(MP1base, &n, MP10base) ;
  880. n = 1 ;
  881. mpcim(&n, MP1) ;
  882. mpdiv(MP1, MP10base, MPatmp) ;
  883. n = 0 ;
  884. mpcim(&n, MP1) ;
  885. if (!mpeq(MPmant, MP1))
  886. {
  887. while (mpge(MPmant, MP10base))
  888. {
  889. exp += 10 ;
  890. mpmul(MPmant, MPatmp, MPmant) ;
  891. }
  892. while ((!eng && mpge(MPmant, MP1base)) ||
  893. (eng && (mpge(MPmant, MP3base) || exp % 3 != 0)))
  894. {
  895. exp += 1 ;
  896. mpdiv(MPmant, MP1base, MPmant) ;
  897. }
  898. while (mplt(MPmant, MPatmp))
  899. {
  900. exp -= 10 ;
  901. mpmul(MPmant, MP10base, MPmant) ;
  902. }
  903. n = 1 ;
  904. mpcim(&n, MP1) ;
  905. while (mplt(MPmant, MP1) || (eng && exp % 3 != 0))
  906. {
  907. exp -= 1 ;
  908. mpmul(MPmant, MP1base, MPmant) ;
  909. }
  910. }
  911. STRCPY(fixed, make_fixed(MPmant, MAX_DIGITS-6)) ;
  912. len = strlen(fixed) ;
  913. for (i = 0; i < len; i++) *optr++ = fixed[i] ;
  914. *optr++ = 'e' ;
  915. if (exp < 0)
  916. {
  917. exp = -exp ;
  918. *optr++ = '-' ;
  919. }
  920. else *optr++ = '+' ;
  921. MPstr_to_num("0.5", DEC, MP1) ;
  922. mpaddi(MP1, &exp, MPval) ;
  923. n = 1 ;
  924. mpcim(&n, MP1) ;
  925. for (ddig = 0; mpge(MPval, MP1); ddig++)
  926. mpdiv(MPval, MP1base, MPval) ;
  927. if (ddig == 0) *optr++ = '0' ;
  928. while (ddig-- > 0)
  929. {
  930. mpmul(MPval, MP1base, MPval) ;
  931. mpcmi(MPval, &dval) ;
  932. *optr++ = digits[dval] ;
  933. dval = -dval ;
  934. mpaddi(MPval, &dval, MPval) ;
  935. }
  936. *optr++ = '\0' ;
  937. v->toclear = 1 ;
  938. v->pointed = 0 ;
  939. return(v->snum) ;
  940. }
  941. /* Convert MP number to fixed number string. */
  942. /* cmax Maximum characters to generate. */
  943. char *
  944. make_fixed(int *MPnumber, int cmax)
  945. {
  946. char *optr ;
  947. int MP1base[MP_SIZE], MP1[MP_SIZE], MP2[MP_SIZE], MPval[MP_SIZE] ;
  948. int ndig ; /* Total number of digits to generate. */
  949. int ddig ; /* Number of digits to left of . */
  950. int dval, n ;
  951. optr = v->fnum ;
  952. mpabs(MPnumber, MPval) ;
  953. n = 0 ;
  954. mpcim(&n, MP1) ;
  955. if (mplt(MPnumber, MP1)) *optr++ = '-' ;
  956. mpcim(&basevals[(int) v->base], MP1base) ;
  957. mppwr(MP1base, &v->accuracy, MP1) ;
  958. MPstr_to_num("0.5", DEC, MP2) ;
  959. mpdiv(MP2, MP1, MP1) ;
  960. mpadd(MPval, MP1, MPval) ;
  961. n = 1 ;
  962. mpcim(&n, MP2) ;
  963. if (mplt(MPval, MP2))
  964. {
  965. ddig = 0 ;
  966. *optr++ = '0' ;
  967. cmax-- ;
  968. }
  969. else
  970. for (ddig = 0; mpge(MPval, MP2); ddig++)
  971. mpdiv(MPval, MP1base, MPval) ;
  972. ndig = MIN(ddig + v->accuracy, --cmax) ;
  973. while (ndig-- > 0)
  974. {
  975. if (ddig-- == 0) *optr++ = '.' ;
  976. mpmul(MPval, MP1base, MPval) ;
  977. mpcmi(MPval, &dval) ;
  978. *optr++ = digits[dval] ;
  979. dval = -dval ;
  980. mpaddi(MPval, &dval, MPval) ;
  981. }
  982. *optr++ = '\0' ;
  983. v->toclear = 1 ;
  984. v->pointed = 0 ;
  985. return(v->fnum) ;
  986. }
  987. void
  988. make_items(void)
  989. {
  990. set_item(DISPLAYITEM, v->display) ;
  991. set_item(OPITEM, " ") ;
  992. set_item(HYPITEM, " ") ;
  993. set_item(INVITEM, " ") ;
  994. }
  995. /* Convert MP number to character string. */
  996. char *
  997. make_number(int *MPnumber, BOOLEAN mkFix)
  998. {
  999. double number, val ;
  1000. /* NOTE: make_number can currently set v->error when converting to a double.
  1001. * This is to provide the same look&feel as V3 even though dtcalc
  1002. * now does internal arithmetic to "infinite" precision.
  1003. *
  1004. * XXX: Needs to be improved. Shouldn't need to convert to a double in
  1005. * order to do these tests.
  1006. */
  1007. mpcmd(MPnumber, &number) ;
  1008. val = fabs(number) ;
  1009. if (v->error) return(vstrs[(int) V_ERROR]) ;
  1010. if (v->dtype == ENG || v->dtype == SCI ||
  1011. (v->dtype == FIX && val != 0.0 && (val > max_fix[(int) v->base])))
  1012. return(make_eng_sci(MPnumber)) ;
  1013. else if (v->dtype == FIX && val != 0.0 && mkFix)
  1014. {
  1015. if(v->accuracy == 0)
  1016. {
  1017. if(val <= min_fix0[(int) v->base])
  1018. return(make_eng_sci(MPnumber)) ;
  1019. else
  1020. return(make_fixed(MPnumber, MAX_DIGITS)) ;
  1021. }
  1022. else if(v->accuracy == 1)
  1023. {
  1024. if(val <= min_fix1[(int) v->base])
  1025. return(make_eng_sci(MPnumber)) ;
  1026. else
  1027. return(make_fixed(MPnumber, MAX_DIGITS)) ;
  1028. }
  1029. else if(v->accuracy == 2)
  1030. {
  1031. if(val <= min_fix2[(int) v->base])
  1032. return(make_eng_sci(MPnumber)) ;
  1033. else
  1034. return(make_fixed(MPnumber, MAX_DIGITS)) ;
  1035. }
  1036. else if(v->accuracy == 3)
  1037. {
  1038. if(val <= min_fix3[(int) v->base])
  1039. return(make_eng_sci(MPnumber)) ;
  1040. else
  1041. return(make_fixed(MPnumber, MAX_DIGITS)) ;
  1042. }
  1043. else if(v->accuracy == 4)
  1044. {
  1045. if(val <= min_fix4[(int) v->base])
  1046. return(make_eng_sci(MPnumber)) ;
  1047. else
  1048. return(make_fixed(MPnumber, MAX_DIGITS)) ;
  1049. }
  1050. else if(v->accuracy == 5)
  1051. {
  1052. if(val <= min_fix5[(int) v->base])
  1053. return(make_eng_sci(MPnumber)) ;
  1054. else
  1055. return(make_fixed(MPnumber, MAX_DIGITS)) ;
  1056. }
  1057. else if(v->accuracy == 6)
  1058. {
  1059. if(val <= min_fix6[(int) v->base])
  1060. return(make_eng_sci(MPnumber)) ;
  1061. else
  1062. return(make_fixed(MPnumber, MAX_DIGITS)) ;
  1063. }
  1064. else if(v->accuracy == 7)
  1065. {
  1066. if(val <= min_fix7[(int) v->base])
  1067. return(make_eng_sci(MPnumber)) ;
  1068. else
  1069. return(make_fixed(MPnumber, MAX_DIGITS)) ;
  1070. }
  1071. else if(v->accuracy == 8)
  1072. {
  1073. if(val <= min_fix8[(int) v->base])
  1074. return(make_eng_sci(MPnumber)) ;
  1075. else
  1076. return(make_fixed(MPnumber, MAX_DIGITS)) ;
  1077. }
  1078. else if(v->accuracy == 9)
  1079. {
  1080. if(val <= min_fix9[(int) v->base])
  1081. return(make_eng_sci(MPnumber)) ;
  1082. else
  1083. return(make_fixed(MPnumber, MAX_DIGITS)) ;
  1084. }
  1085. else
  1086. {
  1087. return(make_fixed(MPnumber, MAX_DIGITS)) ;
  1088. }
  1089. }
  1090. else
  1091. return(make_fixed(MPnumber, MAX_DIGITS)) ;
  1092. }
  1093. /*ARGSUSED*/
  1094. /* Default math library exception handling routine. */
  1095. int
  1096. matherr(struct exception *exc)
  1097. {
  1098. #if 0
  1099. char msg[100];
  1100. if (exc) {
  1101. strcpy(msg, exc->name);
  1102. strcat(msg, ": ");
  1103. if(exc->type == DOMAIN)
  1104. strcat(msg, "DOMAIN ");
  1105. else if(exc->type == SING)
  1106. strcat(msg, "SING ");
  1107. else if(exc->type == OVERFLOW)
  1108. strcat(msg, "OVERFLOW ");
  1109. else if(exc->type == UNDERFLOW)
  1110. strcat(msg, "UNDERFLOW ");
  1111. else if(exc->type == TLOSS)
  1112. strcat(msg, "TLOSS ");
  1113. else if(exc->type == PLOSS)
  1114. strcat(msg, "PLOSS ");
  1115. strcat(msg, vstrs[(int) V_ERROR]);
  1116. _DtSimpleError (v->appname, DtWarning, NULL, msg);
  1117. }
  1118. #endif
  1119. doerr(vstrs[(int) V_ERROR]) ;
  1120. return(1) ; /* Value ignored. */
  1121. }
  1122. /* Convert string into an MP number. */
  1123. void
  1124. MPstr_to_num(char *str, enum base_type base, int *MPval)
  1125. {
  1126. char *optr ;
  1127. int MP1[MP_SIZE], MP2[MP_SIZE], MPbase[MP_SIZE] ;
  1128. int i, inum ;
  1129. int neg = 0 ;
  1130. int exp = 0 ;
  1131. int exp_sign = 1 ;
  1132. i = 0 ;
  1133. mpcim(&i, MPval) ;
  1134. mpcim(&basevals[(int) base], MPbase) ;
  1135. optr = str ;
  1136. while (*optr == ' ') optr++ ;
  1137. if(*optr == '-')
  1138. {
  1139. /* negative number */
  1140. optr++ ;
  1141. neg = 1;
  1142. }
  1143. while ((inum = char_val(*optr)) >= 0)
  1144. {
  1145. mpmul(MPval, MPbase, MPval) ;
  1146. mpaddi(MPval, &inum, MPval) ;
  1147. optr++ ;
  1148. }
  1149. if (*optr == '.')
  1150. for (i = 1; (inum = char_val(*++optr)) >= 0; i++)
  1151. {
  1152. mppwr(MPbase, &i, MP1) ;
  1153. mpcim(&inum, MP2) ;
  1154. mpdiv(MP2, MP1, MP1) ;
  1155. mpadd(MPval, MP1, MPval) ;
  1156. }
  1157. while (*optr == ' ') optr++ ;
  1158. if (*optr != '\0')
  1159. {
  1160. if (*optr == '-') exp_sign = -1 ;
  1161. while ((inum = char_val(*++optr)) >= 0)
  1162. exp = exp * basevals[(int) base] + inum ;
  1163. }
  1164. exp *= exp_sign ;
  1165. if (v->key_exp || exp_sign == -1)
  1166. {
  1167. mppwr(MPbase, &exp, MP1) ;
  1168. mpmul(MPval, MP1, MPval) ;
  1169. }
  1170. if( neg == 1)
  1171. {
  1172. i = -1 ;
  1173. mpcim(&i, MP1) ;
  1174. mpmul(MPval, MP1, MPval) ;
  1175. }
  1176. }
  1177. /* Append the latest parenthesis char to the display item. */
  1178. void
  1179. paren_disp(char c)
  1180. {
  1181. int i, n ;
  1182. /* If the character is a Delete, clear the whole line, and exit parenthesis
  1183. * processing.
  1184. *
  1185. * If the character is a Back Space, remove the last character. If the last
  1186. * character was a left parenthesis, decrement the parentheses count. If
  1187. * the parentheses count is zero, exit parenthesis processing.
  1188. *
  1189. * If the character is a control character (not Ctrl-h), then append ^(char).
  1190. *
  1191. * Otherwise just append the character.
  1192. */
  1193. n = strlen(v->display) ;
  1194. if (IS_KEY(c, KEY_CLR)) /* Is it a Delete character? */
  1195. {
  1196. v->noparens = v->pending = v->opsptr = v->numsptr = 0 ;
  1197. v->cur_op = '?' ;
  1198. set_item(OPITEM, "") ;
  1199. i = 0 ;
  1200. mpcim(&i, v->MPdisp_val) ;
  1201. show_display(v->MPdisp_val) ;
  1202. set_base(v->base);
  1203. set_numtype(v->dtype);
  1204. set_option_menu((int) TTYPEITEM, (int)v->ttype);
  1205. make_modewin() ;
  1206. v->curwin = FCP_KEY ;
  1207. v->defState = 1;
  1208. return ;
  1209. }
  1210. else if (IS_KEY(c, KEY_BSP)) /* Is is a Back Space character? */
  1211. {
  1212. if (!n) return ;
  1213. if (v->display[n-1] == '(')
  1214. {
  1215. v->noparens-- ;
  1216. if (!v->noparens)
  1217. {
  1218. v->pending = v->opsptr = v->numsptr = 0 ;
  1219. v->cur_op = '?' ;
  1220. set_item(OPITEM, "") ;
  1221. show_display(v->MPdisp_val) ;
  1222. return ;
  1223. }
  1224. }
  1225. v->display[n-1] = '\0' ;
  1226. }
  1227. else if (c <= CTL('z')) /* Is it a control character? */
  1228. {
  1229. if (n < MAXLINE-2)
  1230. {
  1231. v->display[n] = '^' ;
  1232. v->display[n+1] = c + 96 ;
  1233. v->display[n+2] = '\0' ;
  1234. }
  1235. }
  1236. else /* It must be an ordinary character. */
  1237. {
  1238. if (n < MAXLINE-1)
  1239. {
  1240. v->display[n] = c ;
  1241. v->display[n+1] = '\0' ;
  1242. }
  1243. }
  1244. n = (n < MAX_DIGITS) ? 0 : n - MAX_DIGITS ;
  1245. v->show_paren = 1 ; /* Hack to get set_item to really display it. */
  1246. set_item(DISPLAYITEM, &v->display[n]) ;
  1247. v->show_paren = 0 ;
  1248. }
  1249. /* Process this event. */
  1250. void
  1251. process_event(int type)
  1252. {
  1253. int ival ;
  1254. switch (type)
  1255. {
  1256. case KEYBOARD_DOWN : if (v->pending)
  1257. {
  1258. v->current = v->cur_ch ;
  1259. do_pending() ;
  1260. }
  1261. else
  1262. {
  1263. ival = get_index(v->cur_ch) ;
  1264. if (ival < TITEMS - EXTRA) {
  1265. if ((v->modetype == FINANCIAL &&
  1266. ival == 23) ||
  1267. (v->modetype == LOGICAL && (
  1268. ival == 21 || ival == 22 ||
  1269. ival == 23)))
  1270. break;
  1271. else
  1272. /* go arm the button */
  1273. draw_button(ival, v->curwin, v->row,
  1274. v->column, TRUE) ;
  1275. }
  1276. process_item(ival) ;
  1277. }
  1278. break ;
  1279. case KEYBOARD_UP : ival = get_index(v->cur_ch) ;
  1280. if (ival < TITEMS - EXTRA) {
  1281. if ((v->modetype == FINANCIAL &&
  1282. ival == 23) ||
  1283. (v->modetype == LOGICAL && (
  1284. ival == 21 || ival == 22 ||
  1285. ival == 23)))
  1286. break;
  1287. else
  1288. /* go disarm the button */
  1289. draw_button(ival, v->curwin, v->row,
  1290. v->column, FALSE) ;
  1291. }
  1292. /* go disarm the button */
  1293. draw_button(ival, v->curwin, v->row,
  1294. v->column, FALSE) ;
  1295. break ;
  1296. }
  1297. }
  1298. void
  1299. process_item(int n)
  1300. {
  1301. int i,isvalid ;
  1302. if (n < 0 || n >= TITEMS)
  1303. {
  1304. beep() ;
  1305. v->error = TRUE;
  1306. }
  1307. v->current = buttons[n].value ;
  1308. if (v->current == '*') v->current = 'x' ; /* Reassign "extra" values. */
  1309. if (v->current == '\015') v->current = '=' ;
  1310. if (v->current == 'Q') v->current = 'q' ;
  1311. if (v->error)
  1312. {
  1313. isvalid = 0 ; /* Must press a valid key first. */
  1314. for (i = 0; i < MAXVKEYS; i++)
  1315. if (v->current == validkeys[i]) isvalid = 1 ;
  1316. if (v->pending == '?') isvalid = 1 ;
  1317. if (!isvalid) return ;
  1318. v->error = 0 ;
  1319. }
  1320. if (v->pending)
  1321. {
  1322. if (v->pending_win == FCP_KEY) (*buttons[v->pending_n].func)() ;
  1323. else (*mode_buttons[MODEKEYS * ((int) v->pending_mode - 1) +
  1324. v->pending_n].func)() ;
  1325. return ;
  1326. }
  1327. switch (buttons[n].opdisp)
  1328. {
  1329. case OP_SET :
  1330. if (v->current == 'T')
  1331. {
  1332. if (v->modetype == SCIENTIFIC)
  1333. set_item(OPITEM, buttons[n].str) ;
  1334. else
  1335. v->pending = 0;
  1336. }
  1337. else
  1338. set_item(OPITEM, buttons[n].str) ;
  1339. break ;
  1340. case OP_CLEAR :
  1341. if (v->error)
  1342. set_item(OPITEM, vstrs[(int) V_CLR]) ;
  1343. else
  1344. set_item(OPITEM, "") ;
  1345. break;
  1346. default:
  1347. break;
  1348. }
  1349. (*buttons[n].func)() ;
  1350. if(strcmp(buttons[n].resname, "clr") != 0)
  1351. v->defState = 0;
  1352. }
  1353. /* Process a portion of the parentheses stack.
  1354. startop Initial position in the operand stack.
  1355. startnum Initial position in the numeric stack.
  1356. n Number of items to process.
  1357. */
  1358. void
  1359. process_stack(int startop, int startnum, int n)
  1360. {
  1361. char sdisp[MAXLINE] ; /* Used to save display contents. */
  1362. int i ;
  1363. int nptr ; /* Pointer to next number from numeric stack. */
  1364. STRCPY(sdisp, v->display) ; /* Save current display. */
  1365. nptr = startnum ;
  1366. v->pending = 0 ;
  1367. v->cur_op = '?' ; /* Current operation is initially undefined. */
  1368. for (i = 0; i < n; i++)
  1369. {
  1370. if (v->opstack[startop + i] == -1)
  1371. {
  1372. mpstr(v->MPnumstack[nptr++], v->MPdisp_val) ;
  1373. }
  1374. else
  1375. {
  1376. v->cur_ch = v->opstack[startop + i] ;
  1377. if (v->cur_ch == '^') /* Control character? */
  1378. {
  1379. i++ ;
  1380. v->cur_ch = CTL(v->opstack[startop + i]) ;
  1381. }
  1382. if (v->pending)
  1383. {
  1384. v->current = v->cur_ch ;
  1385. do_pending() ;
  1386. }
  1387. else process_item(get_index(v->cur_ch)) ;
  1388. }
  1389. }
  1390. v->numsptr = startnum ;
  1391. push_num(v->MPdisp_val) ;
  1392. v->opsptr = startop - 1 ;
  1393. push_op(-1) ;
  1394. save_pending_values(KEY_LPAR) ;
  1395. STRCPY(v->display, sdisp) ; /* Restore current display. */
  1396. }
  1397. void
  1398. process_str(char *str, enum menu_type mtype)
  1399. {
  1400. int i, len ;
  1401. char save[80];
  1402. len = strlen(str) ;
  1403. STRCPY(save, v->display) ;
  1404. STRCPY(v->display, " ") ;
  1405. set_item(DISPLAYITEM, v->display);
  1406. for (i = 0 ; i < len; i++)
  1407. {
  1408. if(str[i] == '*')
  1409. str[i] = 'x';
  1410. if (v->error)
  1411. {
  1412. if(mtype == M_FUN)
  1413. {
  1414. STRCPY(v->display, save);
  1415. set_item(DISPLAYITEM, v->display);
  1416. v->error = 0;
  1417. }
  1418. return ;
  1419. }
  1420. if (v->pending)
  1421. {
  1422. v->current = str[i] ;
  1423. do_pending() ;
  1424. }
  1425. else
  1426. switch(v->base)
  1427. {
  1428. case DEC:
  1429. if(str[i] == 'a' ||
  1430. str[i] == 'b' ||
  1431. str[i] == 'c' ||
  1432. str[i] == 'd' ||
  1433. str[i] == 'e' ||
  1434. str[i] == 'f')
  1435. {
  1436. v->error = True;
  1437. beep();
  1438. break;
  1439. }
  1440. default:
  1441. process_item(get_index(str[i])) ;
  1442. }
  1443. }
  1444. }
  1445. void
  1446. read_rcfiles(void) /* Read .dtcalcrc's from home and current directories. */
  1447. {
  1448. char *home ; /* Pathname for users home directory. */
  1449. char name[MAXPATHLEN + 50] ; /* Full name of users .dtcalcrc file. */
  1450. char pathname[MAXPATHLEN + 5] ; /* Current working directory. */
  1451. char tmp[MAXLINE] ; /* For temporary constant string creation. */
  1452. int n ;
  1453. struct passwd *entry ;
  1454. for (n = 0; n < MAXREGS; n++)
  1455. {
  1456. STRCPY(tmp, make_number(v->MPcon_vals[n], FALSE)) ;
  1457. SPRINTF(name, "%1d: %s [%s]", n, tmp, v->con_names[n]) ;
  1458. STRCPY(v->con_names[n], name) ;
  1459. STRCPY(v->fun_vals[n], "") ; /* Initially empty function strings. */
  1460. }
  1461. if ((home = getenv("HOME")) == NULL)
  1462. {
  1463. if ((entry = getpwuid(getuid())) == NULL) return ;
  1464. home = entry->pw_dir ;
  1465. }
  1466. snprintf(name, MAXPATHLEN, "%s/%s", home, RCNAME) ;
  1467. get_rcfile(name) ; /* Read .dtcalcrc from users home directory. */
  1468. snprintf(name, MAXPATHLEN, "%s/%s", getcwd(pathname, MAXPATHLEN+1), RCNAME) ;
  1469. get_rcfile(name) ; /* Read .dtcalcrc file from current directory. */
  1470. }
  1471. void
  1472. show_display(int *MPval)
  1473. {
  1474. if (!v->error)
  1475. {
  1476. STRCPY(v->display, make_number(MPval, TRUE)) ;
  1477. set_item(DISPLAYITEM, v->display) ;
  1478. }
  1479. }
  1480. void
  1481. usage(char *progname)
  1482. {
  1483. FPRINTF(stderr, ustrs[(int) USAGE1], PATCHLEVEL) ;
  1484. FPRINTF(stderr, "%s", ustrs[(int) USAGE2]) ;
  1485. FPRINTF(stderr, "%s", ustrs[(int) USAGE3]) ;
  1486. exit(1) ;
  1487. }
  1488. void
  1489. write_rcfile(enum menu_type mtype, int exists, int cfno, char *val, char *comment)
  1490. {
  1491. char *home ; /* Pathname for users home directory. */
  1492. char pathname[MAXPATHLEN] ; /* Current working directory. */
  1493. char rcname[MAXPATHLEN] ; /* Full name of users .dtcalcrc file. */
  1494. char str[MAXLINE] ; /* Temporary buffer. */
  1495. char sval[3] ; /* Used for string comparisons. */
  1496. char tmp_filename[MAXLINE] ; /* Used to construct temp filename. */
  1497. int rcexists ; /* Set to 1, if .dtcalcrc file exists. */
  1498. FILE *rcfd ; /* File descriptor for .dtcalcrc file. */
  1499. FILE *tmpfd ; /* File descriptor for new temp .dtcalcrc. */
  1500. struct passwd *entry ; /* The user's /etc/passwd entry. */
  1501. rcexists = 0 ;
  1502. SPRINTF(rcname, "%s/%s", getcwd(pathname, MAXPATHLEN), RCNAME) ;
  1503. if (access(rcname, F_OK) == 0) rcexists = 1 ;
  1504. else
  1505. {
  1506. if ((home = getenv("HOME")) == NULL)
  1507. {
  1508. if ((entry = getpwuid(getuid())) == NULL) return ;
  1509. home = entry->pw_dir ;
  1510. }
  1511. SPRINTF(rcname, "%s/%s", home, RCNAME) ;
  1512. if (access(rcname, F_OK) == 0) rcexists = 1 ;
  1513. }
  1514. STRCPY(tmp_filename, "/tmp/.dtcalcrcXXXXXX") ;
  1515. MKTEMP(tmp_filename) ;
  1516. if ((tmpfd = fopen(tmp_filename, "w+")) == NULL) return ;
  1517. if (rcexists)
  1518. {
  1519. rcfd = fopen(rcname, "r") ;
  1520. SPRINTF(sval, " %1d", cfno) ;
  1521. while (fgets(str, MAXLINE, rcfd))
  1522. {
  1523. if (exists)
  1524. {
  1525. switch (mtype)
  1526. {
  1527. case M_CON : sval[0] = 'c' ;
  1528. if (!strncmp(str, sval, 2)) FPUTS("#", tmpfd) ;
  1529. sval[0] = 'C' ;
  1530. if (!strncmp(str, sval, 2)) FPUTS("#", tmpfd) ;
  1531. break ;
  1532. case M_FUN : sval[0] = 'f' ;
  1533. if (!strncmp(str, sval, 2)) FPUTS("#", tmpfd) ;
  1534. sval[0] = 'F' ;
  1535. if (!strncmp(str, sval, 2)) FPUTS("#", tmpfd) ;
  1536. break;
  1537. default: break;
  1538. }
  1539. }
  1540. FPRINTF(tmpfd, "%s", str) ;
  1541. }
  1542. FCLOSE(rcfd) ;
  1543. }
  1544. switch (mtype)
  1545. {
  1546. case M_CON : FPRINTF(tmpfd, "\nC%1d %s %s\n", cfno, val, comment) ;
  1547. break ;
  1548. case M_FUN :
  1549. if(strcmp(val, "") != 0)
  1550. FPRINTF(tmpfd, "\nF%1d %s %s\n", cfno, val, comment) ;
  1551. break;
  1552. default: break;
  1553. }
  1554. UNLINK(rcname) ;
  1555. rcfd = fopen(rcname, "w") ;
  1556. REWIND(tmpfd) ;
  1557. while (fgets(str, MAXLINE, tmpfd)) FPRINTF(rcfd, "%s", str) ;
  1558. FCLOSE(rcfd) ;
  1559. FCLOSE(tmpfd);
  1560. UNLINK(tmp_filename) ;
  1561. }
  1562. void
  1563. write_resources(char *filename)
  1564. {
  1565. char intval[5] ;
  1566. int MPtemp[MP_SIZE];
  1567. SPRINTF(intval, "%d", v->accuracy) ;
  1568. put_resource(R_ACCURACY, intval) ;
  1569. put_resource(R_DISPLAYED, v->display) ;
  1570. put_resource(R_BASE, base_str[(int) v->base]) ;
  1571. put_resource(R_DISPLAY, dtype_str[(int) v->dtype]) ;
  1572. put_resource(R_MODE, mode_str[(int) v->modetype]) ;
  1573. put_resource(R_TRIG, ttype_str[(int) v->ttype]) ;
  1574. put_resource(R_REGS, set_bool(v->rstate == TRUE)) ;
  1575. put_resource(R_REG0, make_number(v->MPmvals[0], FALSE)) ;
  1576. put_resource(R_REG1, make_number(v->MPmvals[1], FALSE)) ;
  1577. put_resource(R_REG2, make_number(v->MPmvals[2], FALSE)) ;
  1578. put_resource(R_REG3, make_number(v->MPmvals[3], FALSE)) ;
  1579. put_resource(R_REG4, make_number(v->MPmvals[4], FALSE)) ;
  1580. put_resource(R_REG5, make_number(v->MPmvals[5], FALSE)) ;
  1581. put_resource(R_REG6, make_number(v->MPmvals[6], FALSE)) ;
  1582. put_resource(R_REG7, make_number(v->MPmvals[7], FALSE)) ;
  1583. put_resource(R_REG8, make_number(v->MPmvals[8], FALSE)) ;
  1584. put_resource(R_REG9, make_number(v->MPmvals[9], FALSE)) ;
  1585. mpcdm(&(v->MPfvals[0]), MPtemp);
  1586. put_resource(R_FREG0, make_number(MPtemp, FALSE)) ;
  1587. mpcdm(&(v->MPfvals[1]), MPtemp);
  1588. put_resource(R_FREG1, make_number(MPtemp, FALSE)) ;
  1589. mpcdm(&(v->MPfvals[2]), MPtemp);
  1590. put_resource(R_FREG2, make_number(MPtemp, FALSE)) ;
  1591. mpcdm(&(v->MPfvals[3]), MPtemp);
  1592. put_resource(R_FREG3, make_number(MPtemp, FALSE)) ;
  1593. mpcdm(&(v->MPfvals[4]), MPtemp);
  1594. put_resource(R_FREG4, make_number(MPtemp, FALSE)) ;
  1595. mpcdm(&(v->MPfvals[5]), MPtemp);
  1596. put_resource(R_FREG5, make_number(MPtemp, FALSE)) ;
  1597. save_resources(filename) ;
  1598. }