rendered paste body/* $Id: edfile.c,v 1.2 2007-03-04 22:29:55-08 - - $ */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include "auxlib.h"
#include "list.h"
struct options {
bool eflag; /* was the -e flag given? */
bool wflag; /* was the -s flag given? */
char *fileargs; /* points at argv[optind]; */
int fileargc; /* dimension of fileargs */
int optend;
char* filecmd;
};
void f_a(char* stdinline, list_ref list);
void f_i(char* stdinline, list_ref list);
void f_dollar(list_ref list);
void f_zero (list_ref list);
void f_LT(list_ref list);
void f_GT(list_ref list);
void f_dot(list_ref list);
void f_star(list_ref list);
void f_d(list_ref list);
void scanoptions (struct options *options, int argc, char **argv) {
extern int opterr, optind, optopt;
extern char *optarg;
for (;;) {
int option = getopt (argc, argv, "we:");
if (option == EOF) break;
switch (option) {
case 'e': options->eflag = TRUE;
//options->filecmd = optarg;
break;
case 'w': options->wflag = TRUE; options->eflag = FALSE; break;
default : eprintf (" -%c: invalid option\n",
optopt);
//exit_status = EXIT_FAILURE;
};
options->optend = optind;
};
STUBPRINTF ("scanoptions: not implemented\n");
}
void badline (int stdincount, char *stdinline) {
eprintf ("Bad input line %d: %s\n", stdincount, stdinline);
}
void editfile (struct options *options, list_ref list) {
char stdinline[1024];
int stdincount = 0;
for(;; ++stdincount) {
char *linepos = fgets (stdinline, sizeof stdinline, stdin);
if (linepos == NULL) break;
linepos = strchr (stdinline, '\n');
if (linepos == NULL || stdinline[0] == '\0') {
badline (stdincount, stdinline);
}else {
*linepos = '\0';
switch (stdinline[0]) {
case '$': f_dollar(list);
STUBPRINTF ("stdin[%d]: $\n", stdincount); break;
case '*': f_star(list);
STUBPRINTF ("stdin[%d]: *\n", stdincount); break;
case '.': f_dot(list);
STUBPRINTF ("stdin[%d]: .\n", stdincount); break;
case '0': f_zero(list);
STUBPRINTF ("stdin[%d]: 0\n", stdincount); break;
case '<': f_LT(list);
STUBPRINTF ("stdin[%d]: <\n", stdincount); break;
case '>': f_GT(list);
STUBPRINTF ("stdin[%d]: >\n", stdincount); break;
case '@': debugdump_list (list); break;
case 'a': f_a(stdinline, list);
STUBPRINTF ("stdin[%d]: a\n", stdincount); break;
case 'd': f_d(list);
STUBPRINTF ("stdin[%d]: d\n", stdincount); break;
case 'i': f_i(stdinline,list);
STUBPRINTF ("stdin[%d]: i\n", stdincount);
break;
case 'r': STUBPRINTF ("stdin[%d]: r\n", stdincount); break;
case 'w': STUBPRINTF ("stdin[%d]: w\n", stdincount); break;
default : badline (stdincount, stdinline);
};
};
};
}
/* function a appends after the cursor */
void f_a(char* stdinline, list_ref list){
char* newstring = strdup(stdinline)+1;
char* viewitem;
assert(newstring);
insert_list(list, newstring);
//setmove_list(list, MOVE_NEXT);
viewitem = viewcurr_list(list);
printf("%s\n",viewitem);
}
/* function i appends before the cursor */
void f_i(char* stdinline, list_ref list){
char* newstring = strdup(stdinline)+1;
char* viewitem;
assert(newstring);
setmove_list(list,MOVE_PREV);
insert_list(list, newstring);
viewitem = viewcurr_list(list);
printf("%s\n",viewitem);
}
/*function a delete the node that cursor points at */
void f_d(list_ref list){
if(!empty_list(list)){
delete_list(list);
}
}
/* function dollar allows the user
to view the last item.
*/
void f_dollar(list_ref list){
char *viewitem;
setmove_list(list, MOVE_LAST);
viewitem = viewcurr_list(list);
printf("%s\n",viewitem);
}
/* function zero allows the user to view the head item */
void f_zero (list_ref list){
char *viewitem;
setmove_list(list, MOVE_HEAD);
viewitem = viewcurr_list(list);
printf("%s\n",viewitem);
}
/* function < moves the cursor to the prev */
void f_LT(list_ref list){
char *viewitem;
setmove_list(list, MOVE_PREV);
viewitem = viewcurr_list(list);
printf("%s\n",viewitem);
}
/* function > moves the cursor to the next */
void f_GT(list_ref list){
char *viewitem;
setmove_list(list, MOVE_NEXT);
viewitem = viewcurr_list(list);
printf("%s\n",viewitem);
}
/* function dot allows the user to view the current item */
void f_dot(list_ref list){
char *viewitem;
viewitem = viewcurr_list(list);
printf("%s\n",viewitem);
}
/* function star prints out everything in the list,
leaves the cursor at the end */
void f_star(list_ref list){
char *viewitem;
bool thisisahack = FALSE;
for(setmove_list(list, MOVE_HEAD); hasNext(list);
setmove_list(list, MOVE_NEXT)){
viewitem = viewcurr_list(list);
printf("%s\n",viewitem);
thisisahack = TRUE;
}
if(thisisahack == TRUE){
viewitem = viewcurr_list(list);
printf("%s\n",viewitem);
}
}
void putinqueue (list_ref list, FILE *input, char *filename) {
char buffer[1024];
int linenr;
for (linenr = 1; ; ++linenr) {
char *linepos = fgets (buffer, sizeof buffer, input);
if (linepos == NULL) break;
linepos = strchr (buffer, '\n');
if (linepos == NULL) {
eprintf ("%: %s[%d]: unterminated line\n", filename, linenr);
}else {
*linepos = '\0';
};
linepos = strdup (buffer);
assert (linepos != NULL);
f_a ( linepos, list);
};
}
void putfileinqueue (list_ref list, char *filename) {
FILE *input = fopen (filename, "r");
if (input == NULL) {
syseprintf (filename);
}else {
putinqueue (list, input, filename);
if (fclose (input)) syseprintf (filename);
};
}
/* main program */
int main (int argc, char **argv) {
struct options options;
list_ref list = NULL;
int argi = 0;
bzero (&options, sizeof options);
set_progname (argv[0]);
scanoptions (&options, argc, argv);
argi = 1;
list = new_list();
for (argi=options.optend; argi < argc; ++argi) {
printf("dfkjdslkf\n");
putfileinqueue(list, argv[argi]);
}
editfile (&options, list);
free_list (list); list = NULL;
return get_exitcode();
}