home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume13 / elmedit / part01 next >
Encoding:
Text File  |  1990-06-21  |  8.1 KB  |  302 lines

  1. Newsgroups: comp.sources.misc
  2. organization: Emtronix Data Services, Randallstown, Maryland
  3. keywords: simple editor for ELM mailer
  4. subject: v13i062: Elmedit
  5. From: smarc@uunet.uu.net@mas.UUCP
  6. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  7.  
  8. Posting-number: Volume 13, Issue 62
  9. Submitted-by: smarc@uunet.uu.net@mas.UUCP
  10. Archive-name: elmedit/part01
  11.  
  12. This is the source for a simple editor we wrote for our users.
  13. Teaching them how to use VI was not a promising prospect. While it's
  14. not very fancy, it gets the job done. You may use this freely, but
  15. you can't sell or otherwise profit from it. 8-)
  16.  
  17. This runs very slowly on older NCR's, and SCO Xenix. It's probably
  18. due to the curses package on those machines.
  19.  
  20.  
  21. #! /bin/sh
  22. # This file was wrapped with "dummyshar".  "sh" this file to extract.
  23. # Contents:  elmedit.c
  24. echo extracting 'elmedit.c'
  25. if test -f 'elmedit.c' -a -z "$1"; then echo Not overwriting 'elmedit.c'; else
  26. sed 's/^X//' << \EOF > 'elmedit.c'
  27. X /*
  28. X  * elmedit.c 
  29. X  * Mark Winsor & Marc Siegel
  30. X  * This is intended as a VERY simple ASCII editor for the ELM mailer.
  31. X  * It allows naive users to use ELM without learning how to use an editor.
  32. X  * It only handles 23 lines * 80 columns. There are no editing capabilities
  33. X  * other than cursor movement.
  34. X  * 
  35. X  * WARNING: If you invoke editor on a file with more than 23 lines, the 
  36. X  *          file will be truncated when you write it out. 
  37. X  *          In other words, one screenfull of text only!
  38. X  *     
  39. X  * You are free to use this code, provided that you do not
  40. X  * sell or otherwise profit from it. These comments must remain intact!
  41. X  *
  42. X  * Please send comments to:
  43. X  * Marc Siegel  {uunet}!wb3ffv!mas!smarc || smarc@mas.wb3ffv.AMPR.ORG
  44. X  * or
  45. X  * Mark Winsor  {uunet}!wb3ffv!mas!wmark
  46. X  */
  47. X
  48. X#include <curses.h>
  49. X#include <signal.h>
  50. X
  51. X#define BUFSIZE  1840
  52. X#define LINESIZE 80
  53. X#define CTRLJ    '\012'
  54. X#define CTRLL    '\014'
  55. X#define ESC      '\033'
  56. X
  57. Xint lastindex;
  58. X
  59. Xmain(argc,argv)
  60. Xint argc;
  61. Xchar *argv[];
  62. X{
  63. X    if (argc != 2) {
  64. X        (void) usage(argv[0]);
  65. X        (void) exit(1);
  66. X    }
  67. X    (void) startedit(argv[0],argv[1]);
  68. X    (void) bye();
  69. X}
  70. X
  71. Xstartedit(progname,filename)
  72. Xchar *progname;
  73. Xchar *filename;
  74. X{
  75. X    FILE *output;
  76. X    char *editbuf;
  77. X    int i;
  78. X
  79. X    if ((editbuf = (char *)malloc(BUFSIZE)) == NULL) {
  80. X        (void) fprintf(stderr,"\n%s: Cannot allocate %d bytes\n",
  81. X                 progname,BUFSIZE);
  82. X        (void) exit(3);
  83. X    }
  84. X    for (i = 0; i < BUFSIZE; i++) {
  85. X        *(editbuf + i) = ' ';
  86. X    }
  87. X
  88. X    lastindex = 0;
  89. X    if ((output = fopen(filename,"r")) != NULL) {
  90. X        (void) loadbuf(output,editbuf);
  91. X        (void) fclose(output);
  92. X    }
  93. X
  94. X    (void) edit(progname,output,editbuf);
  95. X    if (lastindex > 0) {
  96. X        if ((output = fopen(filename,"w")) == NULL) {
  97. X            (void) move(23,0);
  98. X            (void) clrtoeol();
  99. X            (void) mvprintw(23,0,"%s: Cannot open \"%s\"",progname,filename);
  100. X            (void) refresh();
  101. X            (void) bye();
  102. X        }
  103. X        (void) writebuf(output,editbuf);
  104. X    }
  105. X    (void) fclose(output);
  106. X    return(0);
  107. X}
  108. X
  109. Xedit(progname,output,editbuf)
  110. Xchar *progname;
  111. XFILE *output;
  112. Xchar *editbuf;
  113. X{
  114. X    int row;
  115. X    int col;
  116. X    int ch;
  117. X    int index;
  118. X    int bye();
  119. X
  120. X    (void) initscr();
  121. X    (void) signal(SIGINT,bye);
  122. X    (void) signal(SIGQUIT,bye);
  123. X    (void) signal(SIGTERM,bye);
  124. X    (void) nonl();
  125. X    (void) noecho();
  126. X    (void) keypad(stdscr,TRUE);
  127. X
  128. X    index = row = col = 0;
  129. X    (void) dispbuf(editbuf);
  130. X
  131. X    for (;;) {
  132. X        row = index / LINESIZE;
  133. X        col = index % LINESIZE;
  134. X        (void) mvprintw(23,10,
  135. X                "ESC to end, DEL to abort      Row: %2d   Col: %2d",
  136. X                 row + 1,col + 1);
  137. X        (void) move(row,col);
  138. X        (void) refresh();
  139. X        ch = getch();
  140. X        switch(ch) {
  141. X            case(KEY_DOWN):
  142. X            case(CTRLJ):
  143. X                            index += LINESIZE;
  144. X                            if (index > (BUFSIZE - 1)) {
  145. X                                index -= BUFSIZE;
  146. X                            }
  147. X                            break;
  148. X
  149. X            case(KEY_UP):
  150. X                            index -= LINESIZE;
  151. X                            if (index < 0) {
  152. X                                index += BUFSIZE;
  153. X                            }
  154. X                            break;
  155. X
  156. X            case(KEY_RIGHT):
  157. X                            ++index;
  158. X                            if (index > (BUFSIZE - 1)) {
  159. X                                index = 0;
  160. X                            }
  161. X                            break;
  162. X
  163. X            case(KEY_LEFT):
  164. X            case('\b'):
  165. X                            --index;
  166. X                            if (index < 0) {
  167. X                                index = BUFSIZE - 1;
  168. X                            }
  169. X                            break;
  170. X
  171. X            case('\r'):
  172. X                            index = index + (80 - col);
  173. X                            if (index > (BUFSIZE - 1)) {
  174. X                                index = 0;
  175. X                            }
  176. X                            break;
  177. X
  178. X            case(CTRLL):  
  179. X                            (void) clearok(stdscr);
  180. X                            break;
  181. X
  182. X            case(ESC):
  183. X                            return(0);
  184. X
  185. X            default:
  186. X                            if (ch < ' ' || ch > '~') {
  187. X                                (void) beep();
  188. X                            }
  189. X                            else {
  190. X                                editbuf[index] = ch;
  191. X                                (void) mvprintw(row,col,"%c",ch);
  192. X                                (void) refresh();
  193. X                                if (index > lastindex) {
  194. X                                    lastindex = index;
  195. X                                }    
  196. X                                ++index;
  197. X                                if (index >= BUFSIZE) {
  198. X                                    index = 0;
  199. X                                }
  200. X                            }
  201. X        }
  202. X    }
  203. X}
  204. X
  205. Xwritebuf(output,editbuf)
  206. XFILE *output;
  207. Xchar *editbuf;
  208. X{
  209. X    int index;
  210. X    int count;
  211. X    char linebuf[LINESIZE + 1];
  212. X
  213. X    index = 0;
  214. X    for (;;) {
  215. X        (void) strncpy(linebuf,(editbuf+index),LINESIZE);
  216. X        linebuf[LINESIZE] = '\0';
  217. X        for (count = (LINESIZE-1); linebuf[count] == ' ' && count > 0; count--)
  218. X            ;
  219. X        linebuf[(count + 1)] = '\0';
  220. X        fprintf(output,"%s\n",linebuf);
  221. X        index += LINESIZE;
  222. X        if (index > lastindex) {
  223. X            return(0);
  224. X        }
  225. X    }
  226. X}
  227. X
  228. Xloadbuf(output,editbuf)
  229. XFILE *output;
  230. Xchar *editbuf;
  231. X{
  232. X    char line[LINESIZE + 2];
  233. X    int index;
  234. X    int len;
  235. X    int row;
  236. X    int col;
  237. X    int i;
  238. X
  239. X    index = 0;
  240. X    while ((fgets(line,LINESIZE+2,output)) != NULL) {
  241. X        len = strlen(line) - 1;
  242. X        line[len] = '\0';
  243. X        for (i = 0; i < len; i++) {
  244. X            *(editbuf + index + i) = line[i];
  245. X        }
  246. X        index += LINESIZE;
  247. X        if (index > (BUFSIZE - 1)) {
  248. X            index -= LINESIZE;
  249. X            break;
  250. X        }
  251. X        lastindex = index + i;
  252. X    }
  253. X}
  254. X
  255. Xdispbuf(editbuf)
  256. Xchar *editbuf;
  257. X{
  258. X    char line[LINESIZE + 1];
  259. X    int index;
  260. X    int row;
  261. X
  262. X    index = 0;
  263. X    while (index < BUFSIZE) {
  264. X        (void) memcpy(line,(editbuf + index),LINESIZE);
  265. X        line[LINESIZE] = '\0';
  266. X        row = index / LINESIZE;
  267. X        (void) mvprintw(row,0,"%s",line);
  268. X        index += LINESIZE;
  269. X    }
  270. X    (void) refresh();
  271. X}
  272. X
  273. Xusage(progname)
  274. Xchar *progname;
  275. X{
  276. X    (void) fprintf(stderr,"\n%s: Bad argument count\n",progname);
  277. X}
  278. X
  279. Xbye()
  280. X{
  281. X    (void) move(23,0);
  282. X    (void) clrtoeol();
  283. X    (void) refresh();
  284. X    (void) endwin();
  285. X    (void) exit(0);
  286. X}
  287. X
  288. X/******* END OF ELMEDIT.C ************/
  289. EOF
  290. chars=`wc -c < 'elmedit.c'`
  291. if test $chars !=     6554; then echo 'elmedit.c' is $chars characters, should be     6554 characters!; fi
  292. fi
  293. exit 0
  294.  
  295.        _                             |      Marc Siegel
  296.       ' )--,--,                      |      Randallstown, Maryland 
  297.        /  /  / __.  __  _            |      {uunet}!wb3ffv!mas!smarc
  298.       /  /  (_(_/|_/ (_(_'           |      smarc@mas.wb3ffv.AMPR.ORG
  299. ===============================================================================
  300.  
  301.  
  302.