[摳頂] /cmds/wiz/_crontab.c

看板mud_sanc (Sanctuary - 聖殿)作者 (揮淚斬馬雲)時間7年前 (2018/01/08 21:50), 7年前編輯推噓0(001)
留言1則, 1人參與, 7年前最新討論串1/1
今天忙死了。總之,新的工作室 day2,今天晚上看底下的東西 能不能 coding 完。 // _crontab.c // Laechan@Sanc add in 2018/01/08 // * * * * * 要做啥工作 格式大概長這樣 // 目前已知有 a-b 及 */n 這兩種 // n 若給錯導致不能被整除的情況發生時就不會 match inherit DAEMON; mixed crontabs; void create() { ::create(); seteuid(getuid(this_object())); crontabs=({}); if(file_exists("/open/cmds/crontab.o")) restore_object("/open/cmds/crontab"); } int save_room() { save_object("/open/cmds/crontab"); return 1; } // 到以上為止的寫法都很固定 // 暫時不開放 a,b,c * * * * 這種給法, 事實上它還能混合 // 例如 a,b,*/10 * * * * 這樣會沒完沒了, 逗號先不開放 int illegel_check(string tmp,string kind,int min,int max) { int t,t1,t2; t=atoi(tmp); if(tmp=="*") return 0; else if(sscanf(min,"%d-%d",t1,t2)==2) { if(t1<min || t1>max || t2<min || t2>max || t1>=t2) { write("crontab -add: 你所給的"+kind+" '"+tmp+"' 格式不符喔.\n"); return 1; } return 0; } // 週不能有 / 號 else if(kind!="週" && sscanf(tmp,"*/%d",t1)==1) { // 要能整除 if(t1<min || t1>max || (t1!=0 && t%t1!=0)) { write("crontab -add: 你所給的"+kind+" '"+tmp+"' 格式不符喔.\n"); return 1; } return 0; } else if(sscanf(tmp,"%d",t)==1) { if(t<min || t>max) { write("crontab -add: 你所給的"+kind+" '"+tmp+"' 格式不符喔.\n"); return 1; } return 0; } write("crontab -add: 你所給的"+kind+" '"+tmp+"' 格式不符喔.\n"); return 1; } int cmd_crontab(string str,object me) { string what,files,funs,t1,t2,t3,t4,t5; int n,n1,n2; object ob; mixed tmps=({}); if(!str || str=="") return notify_fail(@LONG 例行排程(crontab)指令說明: ====================================================== crontab -list 例行排程列表 crontab -add xxx 將 xxx 加進排程中 crontab -del xxx 刪掉 xxx 這個排程 每一個排程都是一行,前五個分別代表 分 時 日 月 週, 每一個都可以是 數字、a-b、或 a/b 或 * 的形式。再之後 所接的東西固定為 什麼物件->呼叫什麼函數 例子 * * * * * /d/event/control->newyear 每分鐘的呼叫 0 * * * * /d/event/control->newyear 每小時整點呼叫 */10 * * * * /d/event/control->newyear 每10分鐘的呼叫 ====================================================== LONG ); else if(str=="-l" || str=="-list") return notify_fail(implode(crontabs,"\n")+"\n"); else if(sscanf(str,"-add %s",what)==1) { if(sscanf(what,"%s %s %s %s %s %s->%s", t1,t2,t3,t4,t5,files,funs)!=7) return notify_fail("格式: 分 時 日 月 週 物件檔->函數名\n"+ " 前五個可給 數字,a-b,a/b 及 * 等.\n"); // 不能有完全一模一樣的 if(member_array(what,crontabs)!=-1) return notify_fail("crontab -add: 這條例行排程已經有了喔!\n"); if(illegel_check(t1,"分",0,59)>0) return notify_fail(""); if(illegel_check(t2,"時",0,23)>0) return notify_fail(""); if(illegel_check(t3,"日",1,31)>0) return notify_fail(""); if(illegel_check(t4,"月",1,12)>0) return notify_fail(""); // 週不能用 / 號 if(illegel_check(t5,"週",1,7)>0) return notify_fail(""); // 通過上面的判斷就可以加進去 crontabs+=({what}); save_room(); return notify_fail("crontab -add: 這個例行排程已加入.\n"); } else if(sscanf(str,"-del %s",what)==1 || sscanf(str,"-delete %s",what)==1) { if(member_array(what,crontabs)==-1) return notify_fail("crontab -del: 沒有這個例行排程喔.\n"); crontabs-=({what}); save_room(); return notify_fail("crontab -del: 這個例行排程已刪除.\n"); } return notify_fail("沒有 crontab "+str+" 這個語法喔.\n"); } 大概是上面的感覺吧,明天再放到 sanc 跑看看。 Laechan -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.117.106.224 ※ 文章網址: https://www.ptt.cc/bbs/mud_sanc/M.1515419449.A.7CC.html 寫一下在 times_check 內的判斷好了 總之要從 ctime 裡面抽出 min hour day mon week int s; mixed times=({0,0,0,0,0}); times=({min,hour,day,mon,week}); foreach(c in crontabs) { sscanf(c,"%s %s %s %s %s %s->%s",c1,c2,c3,c4,c5,c6,c7); tmps=({c1,c2,c3,c4,c5}); s=0; for(i=0;i<5;i++) { tmp=tmps[i]; if(tmp=="*") s++; else if(sscanf(tmp,"%d-%d",n1,n2)==2) { if(times[i]>=n1 && times[i]<=n2) s++; } else if(sscanf(tmp,"*/%d",n1)==1) { if(times[i]%n1==0) s++; } else if(sscanf(tmp,"%d",n1)==1) { if(times[i]==n1) s++; } } // 能執行到這裡代表 s = 5, 那就做 files->funs 的動作 if(s==5) catch(call_other(c6,substr(c7,"()","")); } 上面都是變數內的整數及字串判斷,就算每分鐘執行一次,對系統 的負擔也不大。

01/09 07:15, 7年前 , 1F
修正了一些寫法
01/09 07:15, 1F
※ 編輯: laechan (122.117.106.224), 01/09/2018 07:48:39
文章代碼(AID): #1QKtSvVC (mud_sanc)
文章代碼(AID): #1QKtSvVC (mud_sanc)