123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- /* shell.c, Ait, BSD 3-Clause, Kevin Bloom, 2023-2024 */
- #include <stdio.h>
- #include "header.h"
- #include "termbox.h"
- #include "util.h"
- char opcmdtext[STRBUF_M];
- char ipcmdtext[STRBUF_M];
- /* TODO: make this generic
- M-e will display "Shell Command: " in the msgline. You then input the command
- you want.
- Eventually maybe make it so that there are different types of commands:
- - input, inputs something at the point
- - open, runs a command and ait will open the output (this currently works)
- - region/replace, use the region as the input to the shell cmd and then
- replace the region with the output
- - new buffer, runs the command and the output is placed in a new buffer
- I probably would want some keybinds to certain commands, however.
- Also, I'd like to make it so that if you have a region selected, it can be
- executed much like how acme does it.
- io = Insert = 0, Open = 1, Control = 2
- */
- void get_popen_data(int io) {
- FILE *pf;
- char *command = NULL;
- char *data = NULL;
- buffer_t *bp;
- char *insertp = "Shell Command", *openp = "Open Via", *controlp = "Control Via";
- char prompt[STRBUF_M + 12 + strlen(insertp)];
- int cpos = 0, done = FALSE;
- int c = 0, k = 0, hasregion = FALSE, cmdsize = 0, escaped_size = 0;
- int start_col = strlen(prompt), didtry, fd;
- int line = 0, onscrap = 0;
- int newlines = 0, oline = curbp->b_line;
- struct tb_event ev;
- char cmdtext[STRBUF_L], *cbuf; /* cbuf is the colon buffer for line number */
- memset(cmdtext, 0, STRBUF_L);
- point_t point;
- char_t *oscrap = NULL, *escaped_region = NULL;
- const char* path = getenv("PATH");
- FILE *fp = NULL;
- char sys_command[CHUNK];
- static char temp_file[] = TEMPFILE;
- if(is_file_modified(curbp->b_fname) && !file_was_modified_prompt()) {
- return;
- }
- if(io == 1) {
- sprintf(prompt, "%s", openp);
- if(opcmdtext[0] != '\0') {
- strcat(prompt, " (default ");
- strcat(prompt, opcmdtext);
- strcat(prompt, ")");
- }
- } else if(io == 2) {
- sprintf(prompt, "%s", controlp);
- /* if(ipcmdtext[0] != '\0') {
- strcat(prompt, " (default ");
- strcat(prompt, ipcmdtext);
- strcat(prompt, ")");
- }*/
- } else {
- sprintf(prompt, "%s", insertp);
- if(ipcmdtext[0] != '\0') {
- strcat(prompt, " (default ");
- strcat(prompt, ipcmdtext);
- strcat(prompt, ")");
- }
- }
- strcat(prompt, ": ");
- start_col = strlen(prompt);
- display_prompt_and_response(prompt, cmdtext);
- cpos = strlen(cmdtext);
- for (;;) {
- didtry = (k == 0x09); /* Was last command tab-completion? */
- tb_present();
- if(execute_kbd_macro) {
- use_kbd_macro(&ev);
- } else if(tb_poll_event(&ev) != TB_OK) return;
- if(msgline_editor(ev, prompt, cmdtext, STRBUF_L, &cpos)) {
- continue;
- }
- if(!ev.mod)
- k = ev.ch;
- else
- k = ev.key;
- if(record_input) {
- record_buffer[record_buffer_index] = ev;
- record_buffer_index++;
- }
- /* ignore control keys other than return, C-g, backspace, CR, C-s, C-R, ESC, tab */
- if (k < 32 &&
- k != TB_KEY_CTRL_G &&
- k != TB_KEY_BACKSPACE &&
- k != TB_KEY_BACKSPACE2 &&
- k != TB_KEY_ENTER &&
- k != TB_KEY_ESC &&
- k != TB_KEY_TAB)
- continue;
- switch(k) {
- case TB_KEY_ENTER: /* return */
- done = TRUE;
- break;
- case TB_KEY_ESC: /* esc */
- case TB_KEY_CTRL_G: /* ctrl-g */
- if (fp != NULL) fclose(fp);
- tb_set_cursor(0, MSGLINE);
- clrtoeol("", MSGLINE);
- return;
- case TB_KEY_BACKSPACE2: /* del, erase */
- case TB_KEY_BACKSPACE: /* backspace */
- if (cpos == 0)
- continue;
- cmdtext[--cpos] = '\0';
- tb_set_cursor(start_col + cpos, MSGLINE);
- display_prompt_and_response(prompt, cmdtext);
- break;
- do_tab:
- case TB_KEY_TAB: {
- char curpath[PATH_MAX];
- char pcmd[PATH_MAX];
- int ii = 0;
- for(; !isspace(cmdtext[cpos]) && cpos > 0; cpos--)
- ;;
- for(int iii = 0; cmdtext[cpos+iii] != '\0'; iii++){
- if(!isspace(cmdtext[cpos+iii])) {
- pcmd[ii] = cmdtext[cpos+iii];
- ii++;
- }
- }
- if(cpos > 0)
- cpos++;
- pcmd[ii] = '\0';
- if(!didtry) {
- if (fp != NULL) fclose(fp);
- strcpy(temp_file, TEMPFILE);
- if (-1 == (fd = mkstemp(temp_file)))
- fatal("%s: Failed to create temp file\n");
- strcpy(sys_command, "printf \"%s\\n\" ");
- for(int i = 0, ii = 0; path[i] != '\0'; i++, ii++) {
- if(path[i] == ':') {
- curpath[ii] = '\0';
- strcat(sys_command, curpath);
- strcat(sys_command, "/");
- strcat(sys_command, pcmd);
- strcat(sys_command, "* ");
- ii = 0;
- i++;
- } else
- curpath[ii] = path[i];
- }
- strcat(sys_command, "| awk '$0 !~ \"\\\\*\" { split($0, a, \"/\"); print a[length(a)] }' | sort -u >");
- strcat(sys_command, temp_file);
- // strcat(sys_command, " 2>&1");
- (void) ! system(sys_command); /* stop compiler unused result warning */
- fp = fdopen(fd, "r");
- unlink(temp_file);
- }
- while ((c = getc(fp)) != EOF && c != '\n') {
- if (cpos < PATH_MAX - 1 && c != '*') {
- cmdtext[cpos++] = c;
- cmdtext[cpos] = '\0';
- }
- }
- cmdtext[cpos] = '\0';
- for(int i = cpos+1; cmdtext[i] != '\0'; i++)
- cmdtext[i] = 0;
- if (c != '\n' || c == -1) rewind(fp);
- if(c == -1) goto do_tab;
- didtry = 1;
- tb_set_cursor(start_col, MSGLINE);
- clrtoeol(prompt, MSGLINE);
- addstr(cmdtext);
- break;
- }
- default:
- if (cpos < STRBUF_M - 1) {
- for(int i = strlen(cmdtext); i > cpos; i--) {
- cmdtext[i] = cmdtext[i - 1];
- }
- cmdtext[cpos] = k;
- cmdtext[strlen(cmdtext)] = '\0';
- tb_set_cursor(start_col, MSGLINE);
- addstr(cmdtext);
- cpos++;
- tb_set_cursor(start_col + cpos, MSGLINE);
- }
- break;
- }
- if(done)
- break;
- }
- if(cmdtext[0] == '\0') {
- if(io == 1 && opcmdtext[0] != '\0')
- strncpy(cmdtext, opcmdtext, STRBUF_M);
- else if(io == 0 && ipcmdtext[0] != '\0')
- strncpy(cmdtext, ipcmdtext, STRBUF_M);
- else {
- clrtoeol("", MSGLINE);
- return;
- }
- }
- if(io == 1)
- strncpy(opcmdtext, cmdtext, STRBUF_M);
- else if(io == 0)
- strncpy(ipcmdtext, cmdtext, STRBUF_M);
- if (curbp->b_mark != NOMARK && curbp->b_point != curbp->b_mark) {
- if(io == 0) {
- oscrap = (char_t*) malloc(scrap.len);
- onscrap = scrap.len;
- (void) memcpy(oscrap, scrap.data, scrap.len * sizeof (char_t));
- copy_cut(TRUE, TRUE, FALSE);
- }
- hasregion = TRUE;
- }
- strcpy(temp, editor_dir);
- tb_shutdown();
- if(hasregion && io == 0) {
- /* Find all dollar signs and increase the size by one for each sign. */
- for(int i = 0; scrap.data[i] != '\0'; i++) {
- if(scrap.data[i] == '$' || scrap.data[i] == '`' || scrap.data[i] == '"')
- escaped_size += 2;
- else
- escaped_size++;
- }
- escaped_region = malloc(sizeof(char_t *)*escaped_size+1);
- /* Escape all $ with \$, ` with \`, and " with \". This prevents
- the echo command from trying to do a variable substitution,
- command execution, and removal of double quotes.
- */
- for(int i = 0, k = 0; scrap.data[i] != '\0'; i++, k++) {
- if(scrap.data[i] == '$' || scrap.data[i] == '`' || scrap.data[i] == '"') {
- escaped_region[k] = '\\';
- k++;
- escaped_region[k] = scrap.data[i];
- } else {
- escaped_region[k] = scrap.data[i];
- }
- }
- escaped_region[escaped_size] = '\0';
- cmdsize = 6*4*escaped_size*strlen(cmdtext);
- command = malloc(sizeof(char *)*cmdsize);
- strcpy(command, "echo \"");
- strcat(command, (char *)escaped_region);
- strcat(command, "\" | ");
- strcat(command, cmdtext);
- } else if(io == 2) {
- if(hasregion) {
- // TODO: support multi-line and UTF-8
- int col = curwp->w_col - curwp->w_left, ecol;
- if(curbp->b_mark > curbp->b_point) {
- ecol = curbp->b_mark - curbp->b_point;
- } else {
- ecol = curbp->b_point - curbp->b_mark;
- }
- asprintf((char **)&command, "%s %s %d %d %d %d", cmdtext,
- curbp->b_fname, curbp->b_line, col, curbp->b_line,
- ecol);
- } else {
- asprintf((char **)&command, "%s %s %d %d", cmdtext,
- curbp->b_fname, curbp->b_line, curwp->w_col - curwp->w_left);
- }
- cmdsize = strlen(command); // control commands are long
- } else {
- cmdsize = strlen(cmdtext);
- command = malloc(sizeof(char *)*cmdsize);
- strcpy(command, cmdtext);
- }
- command[cmdsize] = '\0';
- memset(cmdtext, 0, STRBUF_L);
- // Setup our pipe for reading and execute our command.
- pf = popen(command,"r");
- if(pf == NULL){
- msg("Could not open pipe for output.");
- return;
- }
- data = malloc(sizeof(char *) * 512 * 3);
- fgets(data, sizeof(char *) * 512 * 3, pf);
- tb_init();
- LINES = tb_height();
- COLS = tb_width();
- MSGLINE = LINES-1;
- tb_set_input_mode(TB_INPUT_ALT);
- /* Mark the log for update */
- redraw();
- /* check if canceled command */
- if(data[0] == -1 || data[0] == 0) {
- if(io == 0) {
- /* put the original contents back in the buffer and reset scrap */
- paste_internal(FALSE);
- free(scrap.data);
- scrap.len = onscrap;
- scrap.data = (char_t*) malloc(scrap.len);
- (void) memcpy(scrap.data, oscrap, scrap.len * sizeof (char_t));
- }
- } else {
- switch(io) {
- case 0: {
- for(int z = 0; data[z] != '\0'; z++) {
- input[0] = (char_t)data[z];
- input[1] = '\0';
- if(input[0] != '\n')
- printf("%s", input);
- undoset(INSERT, z != 0);
- insert();
- if(input[0] == '\n')
- newlines++;
- }
- memset(data, 0, sizeof(char *) * 512 * 3);
- while(fgets(data, sizeof(char *)*512*3, pf) != NULL && data[0] != '\0') {
- for(int z = 0; data[z] != '\0'; z++) {
- input[0] = (char_t)data[z];
- input[1] = '\0';
- undoset(INSERT, TRUE);
- insert();
- if(input[0] == '\n')
- newlines++;
- }
- memset(data, 0, sizeof(char *) * 512 * 3);
- }
- if (curbp->b_point >= curbp->b_epage)
- curbp->b_reframe = 1;
- /* This is ran only for region shell commands, the newlines is
- required to keep the line count correct.
- */
- if(scrap.len > 0) {
- curbp->b_line += newlines;
- currentcommand = KBD_DELETE_WORD;
- backsp();
- }
- if(oscrap != NULL) {
- free(oscrap);
- oscrap = NULL;
- }
- break;
- }
- case 1: {
- data[strlen(data)-1] = '\0';
- /* Find the file name and find the line number */
- if(data[0] == '\0')
- goto do_finish;
- cbuf = strtok(data, ":");
- strcat(temp, cbuf);
- cbuf = strtok(NULL, ":");
- if(cbuf != NULL && (line = atoi(cbuf)) == 0) {
- strcat(temp, ":");
- strcat(temp, cbuf);
- }
- strcat(temp, "\0");
- if(line < 1)
- free(cbuf);
- bp = find_buffer(temp, TRUE, FALSE);
- disassociate_b(curwp);
- curbp = bp;
- associate_b2w(curbp, curwp);
- if (!growgap(curbp, CHUNK))
- fatal("%s: Failed to allocate required memory.\n");
- movegap(curbp, 0);
- /* load the file if not already loaded */
- if (bp != NULL && bp->b_fname[0] == '\0') {
- if (!load_file(temp)) {
- msg("New file %s", temp);
- }
- strncpy(curbp->b_fname, temp, PATH_MAX);
- curbp->b_fname[PATH_MAX] = '\0'; /* truncate if required */
- if(line > 0) {
- point = line_to_point(line);
- if (point != -1) {
- curbp->b_point = point;
- if (curbp->b_epage < pos(curbp, curbp->b_ebuf))
- curbp->b_reframe = 1;
- msg("Line %d", line);
- } else {
- msg("Line %d, not found", line);
- }
- update_display();
- }
- }
- break;
- }
- case 2: {
- char_t *str;
- point_t op = curbp->b_point;
- int len;
- data[strlen(data)-1] = '\0';
- left();
- while(!isspace(*ptr(curbp, curbp->b_point)))
- left();
- right();
- len = op - curbp->b_point;
- str = (char_t *) strndup((const char *)ptr(curbp, curbp->b_point), len);
- int replace = strncmp((const char *)str, data, len);
- if(replace == 0) {
- for(int i = len; i > 0; i--)
- delete();
- } else {
- curbp->b_point = op;
- }
- if (curbp->b_egap == curbp->b_gap && !growgap(curbp, CHUNK))
- return;
- curbp->b_point = movegap(curbp, curbp->b_point);
- for(int i = 0; data[i] != '\0'; i++) {
- *curbp->b_gap++ = data[i];
- }
- curbp->b_point = pos(curbp, curbp->b_egap);
- curbp->b_flags |= B_MODIFIED;
- free(str);
- str = NULL;
- break;
- }
- }
- }
- do_finish:
- /* Some commands, such as ones that copy from region, will mess up
- curbp->b_line due to the cut that is preformed. We want to reset
- that mistake.
- */
- if(curbp->b_line != oline && newlines == 0 && io == 0)
- curbp->b_line = oline;
- if(command != NULL) {
- free(command);
- command = NULL;
- }
- if(data != NULL) {
- free(data);
- data = NULL;
- }
- if (pclose(pf) != 0) {
- msg("Error: Failed to close command stream: %s", strerror(errno));
- }
- }
|